From 1cfb3a14e39cb745c35ee82eefa8c32a5a0cf3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 22 Dec 2015 12:50:45 +0000 Subject: [PATCH 001/890] init project --- .gitignore | 4 ++++ mpush-api/pom.xml | 15 ++++++++++++++ .../shinemo/mpush/api/protocol/Packet.java | 16 +++++++++++++++ mpush-client/pom.xml | 15 ++++++++++++++ mpush-connection/pom.xml | 15 ++++++++++++++ mpush-core/pom.xml | 15 ++++++++++++++ mpush-gateway/pom.xml | 15 ++++++++++++++ pom.xml | 20 +++++++++++++++++++ 8 files changed, 115 insertions(+) create mode 100644 .gitignore create mode 100644 mpush-api/pom.xml create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java create mode 100644 mpush-client/pom.xml create mode 100644 mpush-connection/pom.xml create mode 100644 mpush-core/pom.xml create mode 100644 mpush-gateway/pom.xml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..bef8b10e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea +/*.iml +/*/*.iml +/*/target diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml new file mode 100644 index 00000000..9572871c --- /dev/null +++ b/mpush-api/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-api + + + \ No newline at end of file diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java new file mode 100644 index 00000000..be3389c9 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.api.protocol; + +/** + * Created by ohun on 2015/12/19. + * +-----------+-------+-----+---------+--------------+------+ + * |msgic num 2| cmd 1| id 4| flags 1 | data length 4| body n + * +-----------+------+-----+---------+--------------+------+ + */ +public class Packet { + public byte command; + public int version; + public byte flags; + public int msgId; + public int msgType; + public byte[] body; +} diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml new file mode 100644 index 00000000..c9ee7144 --- /dev/null +++ b/mpush-client/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-client + + + \ No newline at end of file diff --git a/mpush-connection/pom.xml b/mpush-connection/pom.xml new file mode 100644 index 00000000..11369906 --- /dev/null +++ b/mpush-connection/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-connection + + + \ No newline at end of file diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml new file mode 100644 index 00000000..c00e9486 --- /dev/null +++ b/mpush-core/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-core + + + \ No newline at end of file diff --git a/mpush-gateway/pom.xml b/mpush-gateway/pom.xml new file mode 100644 index 00000000..5d2b6727 --- /dev/null +++ b/mpush-gateway/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-gateway + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..473dd7c6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.shinemo.mpush + mpush + pom + 1.0-SNAPSHOT + + mpush-api + mpush-connection + mpush-gateway + mpush-core + mpush-client + + + + \ No newline at end of file From 144e581ebd11cc75a544ecf8b6cc97c27f315077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 22 Dec 2015 15:52:37 +0000 Subject: [PATCH 002/890] init project --- .../com/shinemo/mpush/api/Connection.java | 12 ++ .../com/shinemo/mpush/api/ConnectionInfo.java | 20 ++++ .../java/com/shinemo/mpush/api/Message.java | 7 ++ .../com/shinemo/mpush/api/MessageHandler.java | 10 ++ .../java/com/shinemo/mpush/api/Receiver.java | 8 ++ .../java/com/shinemo/mpush/api/Request.java | 35 ++++++ .../java/com/shinemo/mpush/api/Response.java | 26 ++++ .../shinemo/mpush/api/protocol/Command.java | 23 ++++ mpush-connection/pom.xml | 13 +- .../mpush/connection/ConnectionHandler.java | 50 ++++++++ .../mpush/connection/ConnectionServer.java | 113 ++++++++++++++++++ .../mpush/connection/PacketDecode.java | 18 +++ .../mpush/connection/PacketEncode.java | 17 +++ mpush-core/pom.xml | 20 +++- .../shinemo/mpush/core/ConnectionImpl.java | 45 +++++++ .../shinemo/mpush/core/ConnectionManager.java | 26 ++++ .../shinemo/mpush/core/MessageReceiver.java | 30 +++++ .../core/handler/BaseMessageHandler.java | 21 ++++ .../core/handler/LoginMessageHandler.java | 20 ++++ .../mpush/core/message/LoginMessage.java | 9 ++ pom.xml | 7 ++ 21 files changed, 528 insertions(+), 2 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Message.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Request.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Response.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java new file mode 100644 index 00000000..16105553 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -0,0 +1,12 @@ +package com.shinemo.mpush.api; + +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public interface Connection { + String getId(); + + void send(Packet packet); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java new file mode 100644 index 00000000..770c46e2 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/22. + */ +public class ConnectionInfo { + public final String clientIp; + public final int clientPort; + public final String clientVersion; + public final String deviceId; + public final String desKey; + + public ConnectionInfo(String clientIp, int clientPort, String clientVersion, String deviceId, String desKey) { + this.clientIp = clientIp; + this.clientPort = clientPort; + this.clientVersion = clientVersion; + this.deviceId = deviceId; + this.desKey = desKey; + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java new file mode 100644 index 00000000..84df570e --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/22. + */ +public interface Message { +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java new file mode 100644 index 00000000..f1ed34b5 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java @@ -0,0 +1,10 @@ +package com.shinemo.mpush.api; + +import com.shinemo.mpush.api.Request; + +/** + * Created by ohun on 2015/12/22. + */ +public interface MessageHandler { + void handle(Request request); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java new file mode 100644 index 00000000..083177d0 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java @@ -0,0 +1,8 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/22. + */ +public interface Receiver { + void onMessage(Request request); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java new file mode 100644 index 00000000..7a28e496 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java @@ -0,0 +1,35 @@ +package com.shinemo.mpush.api; + +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public class Request { + private final Command command; + private final Packet message; + private final Connection connection; + + public Request(Packet message, Connection connection) { + this.message = message; + this.connection = connection; + this.command = Command.toCMD(message.command); + } + + public Command getCommand() { + return command; + } + + public Packet getMessage() { + return message; + } + + public Connection getConnection() { + return connection; + } + + public Response getResponse() { + return new Response(packet, connection); + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java new file mode 100644 index 00000000..1a835cef --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.api; + +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public class Response { + private final Packet packet; + private final Connection connection; + + public Response(Packet packet, Connection connection) { + this.packet = packet; + this.connection = connection; + } + + public void send(byte[] body) { + packet.body = body; + connection.send(packet); + } + + + public void sendError(byte[] reson) { + + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java new file mode 100644 index 00000000..23be28cf --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -0,0 +1,23 @@ +package com.shinemo.mpush.api.protocol; + +/** + * Created by ohun on 2015/12/22. + */ +public enum Command { + Heartbeat(1), + Handshake(2), + Login(3), + Kick(4), + Unknown(-1); + + Command(int cmd) { + this.cmd = (byte) cmd; + } + + public final byte cmd; + + public static Command toCMD(byte b) { + if (b > 0 && b < values().length) return values()[b - 1]; + return Unknown; + } +} diff --git a/mpush-connection/pom.xml b/mpush-connection/pom.xml index 11369906..112aa410 100644 --- a/mpush-connection/pom.xml +++ b/mpush-connection/pom.xml @@ -10,6 +10,17 @@ 4.0.0 mpush-connection - + + + com.shinemo.mpush + mpush-api + 1.0-SNAPSHOT + + + com.shinemo.mpush + mpush-core + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java new file mode 100644 index 00000000..67595de7 --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java @@ -0,0 +1,50 @@ +package com.shinemo.mpush.connection; + + +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.core.ConnectionImpl; +import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.core.MessageReceiver; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; + +import java.net.SocketAddress; + +/** + * Created by ohun on 2015/12/19. + */ +public class ConnectionHandler extends ChannelHandlerAdapter { + private MessageReceiver receiver = new MessageReceiver(); + private ConnectionImpl connection = new ConnectionImpl(); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + receiver.onMessage(new Request((Packet) msg, connection)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + super.exceptionCaught(ctx, cause); + } + + @Override + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { + super.connect(ctx, remoteAddress, localAddress, promise); + connection.init(ctx.channel()); + ConnectionManager.INSTANCE.add(connection); + } + + @Override + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + super.disconnect(ctx, promise); + ConnectionManager.INSTANCE.remove(connection); + } + + @Override + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + super.close(ctx, promise); + ConnectionManager.INSTANCE.remove(connection); + } +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java new file mode 100644 index 00000000..4bff802a --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java @@ -0,0 +1,113 @@ +package com.shinemo.mpush.connection; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +/** + * Created by ohun on 2015/12/22. + */ +public class ConnectionServer implements Runnable { + private int port; + + public void start() { + + } + + public void stop() { + + } + + public void run() { + /*** + * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, + * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 + * 在这个例子中我们实现了一个服务端的应用, + * 因此会有2个NioEventLoopGroup会被使用。 + * 第一个经常被叫做‘boss’,用来接收进来的连接。 + * 第二个经常被叫做‘worker’,用来处理已经被接收的连接, + * 一旦‘boss’接收到连接,就会把连接信息注册到‘worker’上。 + * 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现, + * 并且可以通过构造函数来配置他们的关系。 + */ + EventLoopGroup bossGroup = new NioEventLoopGroup(); + EventLoopGroup workerGroup = new NioEventLoopGroup(); + + try { + + /** + * ServerBootstrap 是一个启动NIO服务的辅助启动类 + * 你可以在这个服务中直接使用Channel + */ + + ServerBootstrap b = new ServerBootstrap(); + + /** + * 这一步是必须的,如果没有设置group将会报java.lang.IllegalStateException: group not set异常 + */ + b.group(bossGroup, workerGroup); + + /*** + * ServerSocketChannel以NIO的selector为基础进行实现的,用来接收新的连接 + * 这里告诉Channel如何获取新的连接. + */ + b.channel(NioServerSocketChannel.class); + + /*** + * 这里的事件处理类经常会被用来处理一个最近的已经接收的Channel。 + * ChannelInitializer是一个特殊的处理类, + * 他的目的是帮助使用者配置一个新的Channel。 + * 也许你想通过增加一些处理类比如NettyServerHandler来配置一个新的Channel + * 或者其对应的ChannelPipeline来实现你的网络程序。 + * 当你的程序变的复杂时,可能你会增加更多的处理类到pipline上, + * 然后提取这些匿名类到最顶层的类上。 + */ + b.childHandler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecode()); + ch.pipeline().addLast(new PacketEncode()); + ch.pipeline().addLast(new ConnectionHandler()); + } + }); + + /*** + * 你可以设置这里指定的通道实现的配置参数。 + * 我们正在写一个TCP/IP的服务端, + * 因此我们被允许设置socket的参数选项比如tcpNoDelay和keepAlive。 + * 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。 + */ + b.option(ChannelOption.SO_BACKLOG, 1024); + + /*** + * option()是提供给NioServerSocketChannel用来接收进来的连接。 + * childOption()是提供给由父管道ServerChannel接收到的连接, + * 在这个例子中也是NioServerSocketChannel。 + */ + b.childOption(ChannelOption.SO_KEEPALIVE, true); + + /*** + * 绑定端口并启动去接收进来的连接 + */ + ChannelFuture f = b.bind(port).sync(); + + /** + * 这里会一直等待,直到socket被关闭 + */ + f.channel().closeFuture().sync(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + /*** + * 优雅关闭 + */ + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + } +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java new file mode 100644 index 00000000..96ccbc11 --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java @@ -0,0 +1,18 @@ +package com.shinemo.mpush.connection; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +/** + * Created by ohun on 2015/12/19. + */ +public class PacketDecode extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + + } +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java new file mode 100644 index 00000000..daba2844 --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.connection; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +/** + * Created by ohun on 2015/12/19. + */ +@ChannelHandler.Sharable +public class PacketEncode extends MessageToByteEncoder { + @Override + protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { + + } +} diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index c00e9486..190d532f 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -10,6 +10,24 @@ 4.0.0 mpush-core - + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + + + com.shinemo.mpush + mpush-api + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java new file mode 100644 index 00000000..c7bad156 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.core; + +import com.shinemo.mpush.api.protocol.Connection; +import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; + +/** + * Created by ohun on 2015/12/22. + */ +public class ConnectionImpl implements Connection { + private ConnectionInfo info; + private Channel channel; + private int status = 0; + + public void init(Channel channel) { + this.channel = channel; + } + + @Override + public String getId() { + return channel.id().asShortText(); + } + + public ChannelFuture send(Packet packet) { + return null; + } + + public ChannelFuture close() { + return null; + } + + public ConnectionInfo getInfo() { + return info; + } + + public void setInfo(ConnectionInfo info) { + this.info = info; + } + + public Channel getChannel() { + return channel; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java new file mode 100644 index 00000000..16185364 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.core; + +import com.shinemo.mpush.api.protocol.Connection; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by ohun on 2015/12/22. + */ +public class ConnectionManager { + public static final ConnectionManager INSTANCE = new ConnectionManager(); + private Map connections = new ConcurrentHashMap(); + + public Connection get(String channelId) { + return connections.get(channelId); + } + + public void add(Connection connection) { + connections.put(connection.getId(), connection); + } + + public void remove(Connection connection) { + connections.remove(connection.getId()); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java new file mode 100644 index 00000000..6aa571ec --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -0,0 +1,30 @@ +package com.shinemo.mpush.core; + +import com.shinemo.mpush.api.MessageHandler; +import com.shinemo.mpush.api.Receiver; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.core.handler.LoginMessageHandler; + +/** + * Created by ohun on 2015/12/22. + */ +public class MessageReceiver implements Receiver { + public static final MessageHandler LOGIN_MESSAGE_HANDLER = new LoginMessageHandler(); + + @Override + public void onMessage(Request request) { + switch (request.getCommand()) { + case Heartbeat: + break; + case Handshake: + break; + case Login: + LOGIN_MESSAGE_HANDLER.handle(request); + break; + case Kick: + break; + case Unknown: + break; + } + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java new file mode 100644 index 00000000..614fe4e0 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.core.handler; + + +import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.MessageHandler; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public abstract class BaseMessageHandler implements MessageHandler { + @Override + public void handle(Request request) { + + } + + public abstract T decodeBody(Packet packet); + + public abstract void handle(T t, Request request); +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java new file mode 100644 index 00000000..2200212e --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.core.handler; + +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.message.LoginMessage; + +/** + * Created by ohun on 2015/12/22. + */ +public class LoginMessageHandler extends BaseMessageHandler { + @Override + public LoginMessage decodeBody(Packet packet) { + return null; + } + + @Override + public void handle(LoginMessage o, Request request) { + request.getResponse().send("login success".getBytes()); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java new file mode 100644 index 00000000..991b4c23 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java @@ -0,0 +1,9 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Message; + +/** + * Created by ohun on 2015/12/22. + */ +public class LoginMessage implements Message { +} diff --git a/pom.xml b/pom.xml index 473dd7c6..9fb229b3 100644 --- a/pom.xml +++ b/pom.xml @@ -16,5 +16,12 @@ mpush-client + + + io.netty + netty-all + 5.0.0.Alpha2 + + \ No newline at end of file From 6b59c1b990b255abf6d9f65d1925ca76797c024c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 22 Dec 2015 15:53:36 +0000 Subject: [PATCH 003/890] init project --- .../com/shinemo/mpush/core/handler/LoginMessageHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java index 2200212e..87814c6f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java @@ -10,7 +10,7 @@ public class LoginMessageHandler extends BaseMessageHandler { @Override public LoginMessage decodeBody(Packet packet) { - return null; + return new LoginMessage(); } @Override From 271ecf4762cff5946d72e0072867f4999a00ed36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 23 Dec 2015 09:27:27 +0000 Subject: [PATCH 004/890] init project --- .../java/com/shinemo/mpush/api/Constants.java | 10 ++++++ .../java/com/shinemo/mpush/api/Request.java | 5 +++ .../java/com/shinemo/mpush/api/Router.java | 11 ++++++ .../com/shinemo/mpush/api/RouterInfo.java | 20 +++++++++++ .../com/shinemo/mpush/api/RouterManager.java | 13 +++++++ .../shinemo/mpush/api/protocol/Command.java | 7 ++-- .../mpush/connection/ConnectionHandler.java | 1 + mpush-core/pom.xml | 5 +++ .../shinemo/mpush/core/ConnectionImpl.java | 7 ++-- .../shinemo/mpush/core/ConnectionManager.java | 3 +- .../shinemo/mpush/core/MessageReceiver.java | 11 ++++-- .../core/handler/BaseMessageHandler.java | 3 +- .../mpush/core/handler/BindHandler.java | 23 +++++++++++++ ...nMessageHandler.java => LoginHandler.java} | 2 +- mpush-gateway/pom.xml | 8 ++++- .../shinemo/mpush/gateway/GatewayServer.java | 7 ++++ .../mpush/gateway/router/LocalRouter.java | 24 +++++++++++++ .../gateway/router/LocalRouterManager.java | 28 +++++++++++++++ .../mpush/gateway/router/RemoteRouter.java | 24 +++++++++++++ .../gateway/router/RemoteRouterManager.java | 22 ++++++++++++ .../mpush/gateway/router/RouterCenter.java | 34 +++++++++++++++++++ mpush-tools/pom.xml | 15 ++++++++ .../java/com/shinemo/mpush/tools/Strings.java | 7 ++++ pom.xml | 6 ++++ 24 files changed, 283 insertions(+), 13 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Router.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java rename mpush-core/src/main/java/com/shinemo/mpush/core/handler/{LoginMessageHandler.java => LoginHandler.java} (86%) create mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java create mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java create mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java create mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java create mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java create mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java create mode 100644 mpush-tools/pom.xml create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java new file mode 100644 index 00000000..4d565e01 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -0,0 +1,10 @@ +package com.shinemo.mpush.api; + +import java.nio.charset.Charset; + +/** + * Created by ohun on 2015/12/23. + */ +public interface Constants { + Charset UTF_8 = Charset.forName("UTF-8"); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java index 7a28e496..3f2be441 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java @@ -30,6 +30,11 @@ public Connection getConnection() { } public Response getResponse() { + Packet packet = new Packet(); + packet.command = message.command; + packet.msgId = message.msgId; + packet.version = message.version; + packet.msgType = message.msgType; return new Response(packet, connection); } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java new file mode 100644 index 00000000..cfa1692a --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java @@ -0,0 +1,11 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/23. + */ +public interface Router { + + Connection getConnect(); + + RouterInfo getRouterInfo(); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java new file mode 100644 index 00000000..003ec8c0 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/23. + */ +public class RouterInfo { + private String ip; + + public RouterInfo(String ip) { + this.ip = ip; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java new file mode 100644 index 00000000..cee7cf89 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/23. + */ +public interface RouterManager { + + boolean publish(long userId, Router route); + + boolean unPublish(long userId); + + Router getRouter(long userId); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index 23be28cf..9a6d1fb2 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -7,7 +7,10 @@ public enum Command { Heartbeat(1), Handshake(2), Login(3), - Kick(4), + Logout(4), + Bind(5), + Unbind(6), + Kick(7), Unknown(-1); Command(int cmd) { @@ -17,7 +20,7 @@ public enum Command { public final byte cmd; public static Command toCMD(byte b) { - if (b > 0 && b < values().length) return values()[b - 1]; + if (b > 0 && b < values().length - 1) return values()[b - 1]; return Unknown; } } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java index 67595de7..487f68c9 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java @@ -27,6 +27,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); + ConnectionManager.INSTANCE.remove(connection); } @Override diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 190d532f..2b17a316 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -28,6 +28,11 @@ mpush-api 1.0-SNAPSHOT + + com.shinemo.mpush + mpush-gateway + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java index c7bad156..900cd0fc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core; -import com.shinemo.mpush.api.protocol.Connection; +import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.ConnectionInfo; import com.shinemo.mpush.api.protocol.Packet; import io.netty.channel.Channel; @@ -23,8 +23,9 @@ public String getId() { return channel.id().asShortText(); } - public ChannelFuture send(Packet packet) { - return null; + @Override + public void send(Packet packet) { + } public ChannelFuture close() { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index 16185364..158415c0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.core; -import com.shinemo.mpush.api.protocol.Connection; + +import com.shinemo.mpush.api.Connection; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java index 6aa571ec..090650eb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -3,13 +3,15 @@ import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.core.handler.LoginMessageHandler; +import com.shinemo.mpush.core.handler.BindHandler; +import com.shinemo.mpush.core.handler.LoginHandler; /** * Created by ohun on 2015/12/22. */ public class MessageReceiver implements Receiver { - public static final MessageHandler LOGIN_MESSAGE_HANDLER = new LoginMessageHandler(); + public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); + public static final MessageHandler BIND_HANDLER = new BindHandler(); @Override public void onMessage(Request request) { @@ -19,7 +21,10 @@ public void onMessage(Request request) { case Handshake: break; case Login: - LOGIN_MESSAGE_HANDLER.handle(request); + LOGIN_HANDLER.handle(request); + break; + case Bind: + BIND_HANDLER.handle(request); break; case Kick: break; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java index 614fe4e0..3cca9cb1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.api.protocol.Packet; @@ -9,7 +8,7 @@ /** * Created by ohun on 2015/12/22. */ -public abstract class BaseMessageHandler implements MessageHandler { +public abstract class BaseMessageHandler implements MessageHandler { @Override public void handle(Request request) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java new file mode 100644 index 00000000..a896fefe --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java @@ -0,0 +1,23 @@ +package com.shinemo.mpush.core.handler; + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.gateway.router.RouterCenter; + +/** + * Created by ohun on 2015/12/23. + */ +public class BindHandler extends BaseMessageHandler { + @Override + public String decodeBody(Packet packet) { + return new String(packet.body, Constants.UTF_8); + } + + @Override + public void handle(String body, Request request) { + long userId = Long.parseLong(body); + boolean success = RouterCenter.INSTANCE.publish(userId, request.getConnection()); + request.getResponse().send(new byte[]{success ? (byte) 1 : (byte) 0}); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java similarity index 86% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java index 87814c6f..347d7d80 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java @@ -7,7 +7,7 @@ /** * Created by ohun on 2015/12/22. */ -public class LoginMessageHandler extends BaseMessageHandler { +public class LoginHandler extends BaseMessageHandler { @Override public LoginMessage decodeBody(Packet packet) { return new LoginMessage(); diff --git a/mpush-gateway/pom.xml b/mpush-gateway/pom.xml index 5d2b6727..f9d3ccd4 100644 --- a/mpush-gateway/pom.xml +++ b/mpush-gateway/pom.xml @@ -10,6 +10,12 @@ 4.0.0 mpush-gateway - + + + com.shinemo.mpush + mpush-api + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java new file mode 100644 index 00000000..72a3a61b --- /dev/null +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.gateway; + +/** + * Created by ohun on 2015/12/23. + */ +public class GatewayServer { +} diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java new file mode 100644 index 00000000..ca38e975 --- /dev/null +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java @@ -0,0 +1,24 @@ +package com.shinemo.mpush.gateway.router; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Router; +import com.shinemo.mpush.api.RouterInfo; + +/** + * Created by ohun on 2015/12/23. + */ +public class LocalRouter implements Router { + private final Connection connection; + + public LocalRouter(Connection connection) { + this.connection = connection; + } + + public Connection getConnect() { + return connection; + } + + public RouterInfo getRouterInfo() { + return null; + } +} diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java new file mode 100644 index 00000000..9ad17c0a --- /dev/null +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java @@ -0,0 +1,28 @@ +package com.shinemo.mpush.gateway.router; + +import com.shinemo.mpush.api.Router; +import com.shinemo.mpush.api.RouterManager; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by ohun on 2015/12/23. + */ +public class LocalRouterManager implements RouterManager { + private final Map routerMap = new ConcurrentHashMap(); + + public boolean publish(long userId, Router route) { + routerMap.put(userId, route); + return true; + } + + public boolean unPublish(long userId) { + routerMap.remove(userId); + return true; + } + + public Router getRouter(long userId) { + return routerMap.get(userId); + } +} diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java new file mode 100644 index 00000000..c1c4cd7e --- /dev/null +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java @@ -0,0 +1,24 @@ +package com.shinemo.mpush.gateway.router; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Router; +import com.shinemo.mpush.api.RouterInfo; + +/** + * Created by ohun on 2015/12/23. + */ +public class RemoteRouter implements Router { + private final RouterInfo routerInfo; + + public RemoteRouter(RouterInfo routerInfo) { + this.routerInfo = routerInfo; + } + + public Connection getConnect() { + return null; + } + + public RouterInfo getRouterInfo() { + return null; + } +} diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java new file mode 100644 index 00000000..da5ef1ca --- /dev/null +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java @@ -0,0 +1,22 @@ +package com.shinemo.mpush.gateway.router; + +import com.shinemo.mpush.api.Router; +import com.shinemo.mpush.api.RouterManager; + +/** + * Created by ohun on 2015/12/23. + */ +public class RemoteRouterManager implements RouterManager { + + public boolean publish(long userId, Router route) { + return true; + } + + public boolean unPublish(long userId) { + return true; + } + + public Router getRouter(long userId) { + return null; + } +} diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java new file mode 100644 index 00000000..0112014a --- /dev/null +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java @@ -0,0 +1,34 @@ +package com.shinemo.mpush.gateway.router; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Router; +import com.shinemo.mpush.api.RouterInfo; + +/** + * Created by ohun on 2015/12/23. + */ +public class RouterCenter { + public static final RouterCenter INSTANCE = new RouterCenter(); + + private final LocalRouterManager localRouterManager = new LocalRouterManager(); + private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); + + public boolean publish(long userId, Connection connection) { + localRouterManager.publish(userId, new LocalRouter(connection)); + remoteRouterManager.publish(userId, new RemoteRouter(new RouterInfo("127.0.0.1"))); + return true; + } + + public boolean unPublish(long userId) { + localRouterManager.unPublish(userId); + remoteRouterManager.unPublish(userId); + return true; + } + + public Router lookup(long userId) { + Router local = localRouterManager.getRouter(userId); + if (local != null) return local; + Router remote = remoteRouterManager.getRouter(userId); + return remote; + } +} diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml new file mode 100644 index 00000000..943d82e3 --- /dev/null +++ b/mpush-tools/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-tools + + + \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java new file mode 100644 index 00000000..2ed2aa1b --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.tools; + +/** + * Created by ohun on 2015/12/23. + */ +public class Strings { +} diff --git a/pom.xml b/pom.xml index 9fb229b3..835d2bd8 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ mpush-gateway mpush-core mpush-client + mpush-tools @@ -22,6 +23,11 @@ netty-all 5.0.0.Alpha2 + + com.google.guava + guava + 19.0 + \ No newline at end of file From d4d28225c3cd383f6ade9407e2aec88af794f947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 23 Dec 2015 20:05:48 +0800 Subject: [PATCH 005/890] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96pom=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 17 +++- conf-daily.properties | 25 +++++ conf-online.properties | 21 ++++ conf-pre.properties | 21 ++++ mpush-api/pom.xml | 3 +- mpush-client/pom.xml | 3 +- mpush-connection/pom.xml | 5 +- mpush-core/pom.xml | 4 +- mpush-gateway/pom.xml | 4 +- mpush-tools/pom.xml | 3 +- pom.xml | 203 ++++++++++++++++++++++++++++++++++++--- 11 files changed, 283 insertions(+), 26 deletions(-) create mode 100644 conf-daily.properties create mode 100644 conf-online.properties create mode 100644 conf-pre.properties diff --git a/.gitignore b/.gitignore index bef8b10e..99cbf641 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,13 @@ -/.idea -/*.iml -/*/*.iml -/*/target +*.class +*.classpath +*.project +*/.settings/* +*/target/* +*/bin/* +*/WebContent/* + +.idea/* +*.jar +*.war +*.ear +*.iml \ No newline at end of file diff --git a/conf-daily.properties b/conf-daily.properties new file mode 100644 index 00000000..8279c9b1 --- /dev/null +++ b/conf-daily.properties @@ -0,0 +1,25 @@ +#主库 +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://111.1.57.148:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 +jdbc.user=root +jdbc.password=kYCAxx6s9qe90HWnIPxWMQ +#备库 +jdbc.url.second=jdbc:mysql://111.1.57.148:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 +jdbc.user.second=root +jdbc.password.second=kYCAxx6s9qe90HWnIPxWMQ + +#日志根目录 +#catalina.base=/opt/logs/mydb +catalina.base=/opt/logs/mpush +loglevel=DEBUG + +dubbo.zookeeper.registry.address=127.0.0.1:2181 +medical.consumer.version=1.0.0.daily +datacenterdubbo.provider.port=20882 +medicaldubbo.provider.port=20881 +dubbo.provider.version=1.0.0.datacenter.daily + + + +dc.task.host=localhost +#end diff --git a/conf-online.properties b/conf-online.properties new file mode 100644 index 00000000..8732a9a7 --- /dev/null +++ b/conf-online.properties @@ -0,0 +1,21 @@ +#主库 +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://10.161.223.238:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 +jdbc.user=yimi +jdbc.password=0HXu5asddX6 + +#备库 +jdbc.url.second=jdbc:mysql://10.161.155.50:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 +jdbc.user.second=readonlyuser +jdbc.password.second=Hello2015 + +#日志根目录 +catalina.base=/opt/logs/mpush +loglevel=WARN +dubbo.zookeeper.registry.address=10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 +medical.consumer.version=1.0.0 +datacenterdubbo.provider.port=20882 +medicaldubbo.provider.port=20881 +dubbo.provider.version=1.0.0.datacenter.online + +dc.task.host=html5-1 diff --git a/conf-pre.properties b/conf-pre.properties new file mode 100644 index 00000000..d7a6a01e --- /dev/null +++ b/conf-pre.properties @@ -0,0 +1,21 @@ +#主库 +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://10.161.223.238:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 +jdbc.user=yimi +jdbc.password=0HXu5asddX6 +#备库 +jdbc.url.second=jdbc:mysql://10.161.155.50:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 +jdbc.user.second=readonlyuser +jdbc.password.second=Hello2015 + +#日志根目录 +catalina.base=/opt/logs/mpush +loglevel=DEBUG + +dubbo.zookeeper.registry.address=10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 +medical.consumer.version=1.0.0.pre +datacenterdubbo.provider.port=20882 +medicaldubbo.provider.port=20881 +dubbo.provider.version=1.0.0.datacenter.pre + +dc.task.host=html5-1 diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 9572871c..b3547d00 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -10,6 +10,7 @@ 4.0.0 mpush-api - + 1.0-SNAPSHOT + jar \ No newline at end of file diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index c9ee7144..1ccd550e 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -10,6 +10,7 @@ 4.0.0 mpush-client - + 1.0-SNAPSHOT + jar \ No newline at end of file diff --git a/mpush-connection/pom.xml b/mpush-connection/pom.xml index 112aa410..916ca370 100644 --- a/mpush-connection/pom.xml +++ b/mpush-connection/pom.xml @@ -10,16 +10,17 @@ 4.0.0 mpush-connection + 1.0-SNAPSHOT + jar + com.shinemo.mpush mpush-api - 1.0-SNAPSHOT com.shinemo.mpush mpush-core - 1.0-SNAPSHOT diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 2b17a316..751087ea 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -10,6 +10,8 @@ 4.0.0 mpush-core + 1.0-SNAPSHOT + jar @@ -26,12 +28,10 @@ com.shinemo.mpush mpush-api - 1.0-SNAPSHOT com.shinemo.mpush mpush-gateway - 1.0-SNAPSHOT diff --git a/mpush-gateway/pom.xml b/mpush-gateway/pom.xml index f9d3ccd4..dfcea2f0 100644 --- a/mpush-gateway/pom.xml +++ b/mpush-gateway/pom.xml @@ -10,11 +10,13 @@ 4.0.0 mpush-gateway + 1.0-SNAPSHOT + jar + com.shinemo.mpush mpush-api - 1.0-SNAPSHOT diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 943d82e3..6acb8537 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -10,6 +10,7 @@ 4.0.0 mpush-tools - + 1.0-SNAPSHOT + jar \ No newline at end of file diff --git a/pom.xml b/pom.xml index 835d2bd8..c3672684 100644 --- a/pom.xml +++ b/pom.xml @@ -4,6 +4,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + + com.shinemo + parent + 1.0.0 + + com.shinemo.mpush mpush pom @@ -17,17 +23,186 @@ mpush-tools - - - io.netty - netty-all - 5.0.0.Alpha2 - - - com.google.guava - guava - 19.0 - - - - \ No newline at end of file + + 1.7 + 4.0.0.RELEASE + 1.0-SNAPSHOT + 1.0-SNAPSHOT + 1.0-SNAPSHOT + 1.0-SNAPSHOT + 1.0-SNAPSHOT + 1.0-SNAPSHOT + 1.0.0-SNAPSHOT + 1.0.0-SNAPSHOT + 1.0.0-SNAPSHOT + 5.0.0.Alpha2 + + + + + + + + + + + + io.netty + netty-all + ${netty.version} + + + + + + + com.google.guava + guava + 19.0 + + + + + + + com.shinemo.mpush + mpush-api + ${mpush-api-version} + + + com.shinemo.mpush + mpush-client + ${mpush-client-version} + + + com.shinemo.mpush + mpush-connection + ${mpush-connection-version} + + + com.shinemo.mpush + mpush-core + ${mpush-core-version} + + + com.shinemo.mpush + mpush-gateway + ${mpush-gateway-version} + + + com.shinemo.mpush + mpush-tools + ${mpush-tools-version} + + + + + + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + jcl-over-slf4j + 1.7.5 + + + ch.qos.logback + logback-classic + 1.0.13 + + + commons-logging + commons-logging + 1.1.3 + + + log4j + log4j + 1.2.17 + + + org.logback-extensions + logback-ext-spring + 0.1.2 + + + + + + + redis.clients + jedis + 2.7.2 + + + org.springframework.data + spring-data-redis + 1.6.1.RELEASE + + + + + + + + + + + + src/main/resources + + **/*.xml + + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + + + + + + daily + + true + + + daily + + + + pre + + pre + + + + online + + online + + + + + + From 5799f4cfcf058987dc3f82aead7e51043479c1ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 23 Dec 2015 12:31:34 +0000 Subject: [PATCH 006/890] init project --- .../com/shinemo/mpush/api/Connection.java | 5 ++ .../com/shinemo/mpush/api/ConnectionInfo.java | 12 ++-- .../java/com/shinemo/mpush/api/Constants.java | 4 ++ .../com/shinemo/mpush/api/RouterInfo.java | 18 +++++ .../mpush/api/exception/DecodeException.java | 10 +++ .../shinemo/mpush/api/protocol/Packet.java | 11 +-- .../mpush/api/protocol/PacketDecoder.java | 71 +++++++++++++++++++ .../mpush/api/protocol/PacketEncoder.java | 29 ++++++++ .../mpush/connection/ConnectionServer.java | 24 ++++--- .../mpush/connection/PacketDecode.java | 18 ----- .../mpush/connection/PacketEncode.java | 17 ----- .../shinemo/mpush/core/ConnectionImpl.java | 11 +++ 12 files changed, 173 insertions(+), 57 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java delete mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java delete mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 16105553..177d5e6c 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -6,7 +6,12 @@ * Created by ohun on 2015/12/22. */ public interface Connection { + String getId(); void send(Packet packet); + + boolean isClosed(); + + boolean isOpen(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java index 770c46e2..95f1c532 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java @@ -4,16 +4,14 @@ * Created by ohun on 2015/12/22. */ public class ConnectionInfo { - public final String clientIp; - public final int clientPort; - public final String clientVersion; + public final String os; + public final String clientVer; public final String deviceId; public final String desKey; - public ConnectionInfo(String clientIp, int clientPort, String clientVersion, String deviceId, String desKey) { - this.clientIp = clientIp; - this.clientPort = clientPort; - this.clientVersion = clientVersion; + public ConnectionInfo(String os, String clientVer, String deviceId, String desKey) { + this.os = os; + this.clientVer = clientVer; this.deviceId = deviceId; this.desKey = desKey; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 4d565e01..4440e227 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -7,4 +7,8 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); + int MAX_PACKET_SIZE = 1024; + int HEADER_LEN = 13; + byte MAGIC_NUM1 = (byte) 33; + byte MAGIC_NUM2 = (byte) 99; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java index 003ec8c0..e4d7ba0e 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java @@ -5,11 +5,29 @@ */ public class RouterInfo { private String ip; + private String os; + private String clientVer; public RouterInfo(String ip) { this.ip = ip; } + public String getOs() { + return os; + } + + public void setOs(String os) { + this.os = os; + } + + public String getClientVer() { + return clientVer; + } + + public void setClientVer(String clientVer) { + this.clientVer = clientVer; + } + public String getIp() { return ip; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java new file mode 100644 index 00000000..543768df --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java @@ -0,0 +1,10 @@ +package com.shinemo.mpush.api.exception; + +/** + * Created by ohun on 2015/12/23. + */ +public class DecodeException extends RuntimeException { + public DecodeException(String message) { + super(message); + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index be3389c9..b31fc008 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -2,15 +2,16 @@ /** * Created by ohun on 2015/12/19. - * +-----------+-------+-----+---------+--------------+------+ - * |msgic num 2| cmd 1| id 4| flags 1 | data length 4| body n - * +-----------+------+-----+---------+--------------+------+ + * magic(2)+cmd(1)+version(1)+flags(1)+msgId(4)+length(4)+body(n) */ public class Packet { public byte command; - public int version; + public byte version; public byte flags; public int msgId; - public int msgType; public byte[] body; + + public int getBodyLength() { + return body == null ? 0 : body.length; + } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java new file mode 100644 index 00000000..c1198f3c --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java @@ -0,0 +1,71 @@ +package com.shinemo.mpush.api.protocol; + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.exception.DecodeException; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.DecoderException; + +import java.util.List; + +/** + * Created by ohun on 2015/12/19. + * magic(2)+length(4)+cmd(1)+version(1)+flags(1)+msgId(4)+body(n) + */ +public class PacketDecoder extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + decodeFrames(in, out); + } + + private void decodeFrames(ByteBuf in, List out) throws Exception { + try { + while (in.readableBytes() >= Constants.HEADER_LEN) { + //1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 + in.markReaderIndex(); + out.add(decodeFrame(in)); + } + } catch (DecodeException e) { + //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 + in.resetReaderIndex(); + } + } + + private Packet decodeFrame(ByteBuf in) throws Exception { + int bufferSize = in.readableBytes(); + if (in.readByte() != Constants.MAGIC_NUM1 || in.readByte() != Constants.MAGIC_NUM2) { + throw new RuntimeException("ERROR MAGIC_NUM"); + } + int bodyLength = in.readInt(); + if (bufferSize < (bodyLength + Constants.HEADER_LEN)) { + throw new DecodeException("invalid frame"); + } + return readPacket(in, bodyLength); + } + + private Packet readPacket(ByteBuf in, int bodyLength) { + byte command = in.readByte(); + byte version = in.readByte(); + byte flags = in.readByte(); + int msgId = in.readInt(); + byte[] body = null; + if (bodyLength > 0) { + if (bodyLength > Constants.MAX_PACKET_SIZE) { + throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); + } + body = new byte[bodyLength]; + in.readBytes(body); + } + Packet packet = new Packet(); + packet.command = command; + packet.version = version; + packet.flags = flags; + packet.msgId = msgId; + packet.body = body; + return packet; + } + +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java new file mode 100644 index 00000000..5068e5c9 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java @@ -0,0 +1,29 @@ +package com.shinemo.mpush.api.protocol; + +import com.shinemo.mpush.api.Constants; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +/** + * Created by ohun on 2015/12/19. + */ +@ChannelHandler.Sharable +public class PacketEncoder extends MessageToByteEncoder { + public static final PacketEncoder INSTANCE = new PacketEncoder(); + + @Override + protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { + out.writeByte(Constants.MAGIC_NUM1); + out.writeByte(Constants.MAGIC_NUM2); + out.writeInt(packet.getBodyLength()); + out.writeByte(packet.command); + out.writeByte(packet.flags); + out.writeByte(packet.version); + out.writeInt(packet.msgId); + if (packet.getBodyLength() > 0) { + out.writeBytes(packet.body); + } + } +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java index 4bff802a..10b3ac02 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.connection; +import com.shinemo.mpush.api.protocol.PacketDecoder; +import com.shinemo.mpush.api.protocol.PacketEncoder; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; @@ -12,18 +14,21 @@ /** * Created by ohun on 2015/12/22. */ -public class ConnectionServer implements Runnable { - private int port; - - public void start() { +public class ConnectionServer { + private final int port; + private EventLoopGroup bossGroup; + private EventLoopGroup workerGroup; + public ConnectionServer(int port) { + this.port = port; } public void stop() { - + if (workerGroup != null) workerGroup.shutdownGracefully(); + if (bossGroup != null) bossGroup.shutdownGracefully(); } - public void run() { + public void start() { /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 @@ -70,8 +75,8 @@ public void run() { b.childHandler(new ChannelInitializer() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecode()); - ch.pipeline().addLast(new PacketEncode()); + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); ch.pipeline().addLast(new ConnectionHandler()); } }); @@ -106,8 +111,7 @@ public void initChannel(SocketChannel ch) throws Exception { /*** * 优雅关闭 */ - workerGroup.shutdownGracefully(); - bossGroup.shutdownGracefully(); + stop(); } } } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java deleted file mode 100644 index 96ccbc11..00000000 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.shinemo.mpush.connection; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -/** - * Created by ohun on 2015/12/19. - */ -public class PacketDecode extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - - } -} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java deleted file mode 100644 index daba2844..00000000 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.shinemo.mpush.connection; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; - -/** - * Created by ohun on 2015/12/19. - */ -@ChannelHandler.Sharable -public class PacketEncode extends MessageToByteEncoder { - @Override - protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { - - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java index 900cd0fc..06e77851 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java @@ -13,6 +13,7 @@ public class ConnectionImpl implements Connection { private ConnectionInfo info; private Channel channel; private int status = 0; + private long lastHeartbeatTime = 0; public void init(Channel channel) { this.channel = channel; @@ -28,6 +29,16 @@ public void send(Packet packet) { } + @Override + public boolean isClosed() { + return false; + } + + @Override + public boolean isOpen() { + return false; + } + public ChannelFuture close() { return null; } From 3ba747ccc845ee7ea6185066010b8de529b6387b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 23 Dec 2015 23:25:24 +0800 Subject: [PATCH 007/890] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/api/Connection.java | 5 + .../shinemo/mpush/api/protocol/Packet.java | 15 ++- mpush-connection/pom.xml | 6 +- .../connection/netty/NettySharedHolder.java | 21 ++++ .../netty/encoder}/PacketDecoder.java | 2 +- .../netty/encoder}/PacketEncoder.java | 4 +- .../handler}/ConnectionHandler.java | 2 +- .../{ => netty/server}/ConnectionServer.java | 38 +++++- mpush-core/pom.xml | 37 +++++- .../shinemo/mpush/core/ConnectionImpl.java | 57 --------- .../shinemo/mpush/core/ConnectionManager.java | 54 +++++++-- .../shinemo/mpush/core/NettyConnection.java | 108 ++++++++++++++++++ .../com/shinemo/mpush/core/task/ScanTask.java | 9 ++ .../mpush/core/thread/NamedThreadFactory.java | 47 ++++++++ .../mpush/core/thread/ThreadNameSpace.java | 20 ++++ .../mpush/core/util/InetAddressUtil.java | 41 +++++++ 16 files changed, 387 insertions(+), 79 deletions(-) create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java rename {mpush-api/src/main/java/com/shinemo/mpush/api/protocol => mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder}/PacketDecoder.java (97%) rename {mpush-api/src/main/java/com/shinemo/mpush/api/protocol => mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder}/PacketEncoder.java (90%) rename mpush-connection/src/main/java/com/shinemo/mpush/connection/{ => netty/handler}/ConnectionHandler.java (97%) rename mpush-connection/src/main/java/com/shinemo/mpush/connection/{ => netty/server}/ConnectionServer.java (81%) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 177d5e6c..947ae981 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -14,4 +14,9 @@ public interface Connection { boolean isClosed(); boolean isOpen(); + + void refreshLastReadTime(long lastReadTime); + + void close(); + } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index b31fc008..48735fa8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -1,11 +1,15 @@ package com.shinemo.mpush.api.protocol; +import java.io.Serializable; +import java.util.Arrays; + /** * Created by ohun on 2015/12/19. * magic(2)+cmd(1)+version(1)+flags(1)+msgId(4)+length(4)+body(n) */ -public class Packet { - public byte command; +public class Packet implements Serializable{ + private static final long serialVersionUID = -2725825199998223372L; + public byte command; public byte version; public byte flags; public int msgId; @@ -14,4 +18,11 @@ public class Packet { public int getBodyLength() { return body == null ? 0 : body.length; } + + @Override + public String toString() { + return "Packet [command=" + command + ", version=" + version + ", flags=" + flags + ", msgId=" + msgId + ", body=" + Arrays.toString(body) + "]"; + } + + } diff --git a/mpush-connection/pom.xml b/mpush-connection/pom.xml index 916ca370..a61e11e9 100644 --- a/mpush-connection/pom.xml +++ b/mpush-connection/pom.xml @@ -22,6 +22,10 @@ com.shinemo.mpush mpush-core + + io.netty + netty-all + - \ No newline at end of file + diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java new file mode 100644 index 00000000..62686ed1 --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.connection.netty; + +import com.shinemo.mpush.core.thread.NamedThreadFactory; +import com.shinemo.mpush.core.thread.ThreadNameSpace; + +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.UnpooledByteBufAllocator; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timer; + +public class NettySharedHolder { + + public static final Timer timer = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); + + public static final ByteBufAllocator byteBufAllocator; + + static { + byteBufAllocator = UnpooledByteBufAllocator.DEFAULT; + } + +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java similarity index 97% rename from mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java rename to mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java index c1198f3c..42e0a5c0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.protocol; +package com.shinemo.mpush.connection.netty.encoder; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.exception.DecodeException; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketEncoder.java similarity index 90% rename from mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java rename to mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketEncoder.java index 5068e5c9..ef4cfb75 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketEncoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketEncoder.java @@ -1,6 +1,8 @@ -package com.shinemo.mpush.api.protocol; +package com.shinemo.mpush.connection.netty.encoder; import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Packet; + import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java similarity index 97% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java rename to mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 487f68c9..a60d1e4d 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection; +package com.shinemo.mpush.connection.netty.handler; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java similarity index 81% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java rename to mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index 10b3ac02..93c5d5ef 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -1,7 +1,15 @@ -package com.shinemo.mpush.connection; +package com.shinemo.mpush.connection.netty.server; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.connection.netty.NettySharedHolder; +import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; +import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; +import com.shinemo.mpush.connection.netty.handler.ConnectionHandler; -import com.shinemo.mpush.api.protocol.PacketDecoder; -import com.shinemo.mpush.api.protocol.PacketEncoder; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; @@ -15,6 +23,11 @@ * Created by ohun on 2015/12/22. */ public class ConnectionServer { + + private static final Logger log = LoggerFactory.getLogger(ConnectionServer.class); + + private final AtomicBoolean startFlag = new AtomicBoolean(false); + private final int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; @@ -24,11 +37,18 @@ public ConnectionServer(int port) { } public void stop() { + log.info("netty server stop now"); + this.startFlag.set(false); if (workerGroup != null) workerGroup.shutdownGracefully(); if (bossGroup != null) bossGroup.shutdownGracefully(); } public void start() { + + if (!startFlag.compareAndSet(false, true)) { + return; + } + /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 @@ -88,13 +108,17 @@ public void initChannel(SocketChannel ch) throws Exception { * 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。 */ b.option(ChannelOption.SO_BACKLOG, 1024); - + /*** * option()是提供给NioServerSocketChannel用来接收进来的连接。 * childOption()是提供给由父管道ServerChannel接收到的连接, * 在这个例子中也是NioServerSocketChannel。 */ b.childOption(ChannelOption.SO_KEEPALIVE, true); + + b.option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); + b.childOption(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); + /*** * 绑定端口并启动去接收进来的连接 @@ -105,9 +129,11 @@ public void initChannel(SocketChannel ch) throws Exception { * 这里会一直等待,直到socket被关闭 */ f.channel().closeFuture().sync(); + + log.info("server start ok on:"+port); + } catch (Exception e) { - e.printStackTrace(); - } finally { + log.error("server start exception",e); /*** * 优雅关闭 */ diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 751087ea..eae3c30f 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -33,6 +33,41 @@ com.shinemo.mpush mpush-gateway + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + commons-logging + commons-logging + + + log4j + log4j + + + org.logback-extensions + logback-ext-spring + + + + io.netty + netty-all + + + + com.google.guava + guava + + - \ No newline at end of file + diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java deleted file mode 100644 index 06e77851..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.shinemo.mpush.core; - -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.ConnectionInfo; -import com.shinemo.mpush.api.protocol.Packet; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; - -/** - * Created by ohun on 2015/12/22. - */ -public class ConnectionImpl implements Connection { - private ConnectionInfo info; - private Channel channel; - private int status = 0; - private long lastHeartbeatTime = 0; - - public void init(Channel channel) { - this.channel = channel; - } - - @Override - public String getId() { - return channel.id().asShortText(); - } - - @Override - public void send(Packet packet) { - - } - - @Override - public boolean isClosed() { - return false; - } - - @Override - public boolean isOpen() { - return false; - } - - public ChannelFuture close() { - return null; - } - - public ConnectionInfo getInfo() { - return info; - } - - public void setInfo(ConnectionInfo info) { - this.info = info; - } - - public Channel getChannel() { - return channel; - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index 158415c0..467fbaf4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -1,27 +1,63 @@ package com.shinemo.mpush.core; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; import com.shinemo.mpush.api.Connection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/22. */ public class ConnectionManager { public static final ConnectionManager INSTANCE = new ConnectionManager(); - private Map connections = new ConcurrentHashMap(); +// private final ConcurrentMap connections = new ConcurrentHashMapV8(); - public Connection get(String channelId) { - return connections.get(channelId); + private final Cache cacherClients = CacheBuilder.newBuilder() + .maximumSize(2<<17) + .expireAfterAccess(27, TimeUnit.MINUTES) + .removalListener(new RemovalListener() { + public void onRemoval(RemovalNotification notification) { + if(notification.getValue().isClosed()){ +// notification.getValue().close("[Remoting] removed from cache"); + } + }; + }).build(); + + public Connection get(final String channelId) throws ExecutionException { + + NettyConnection client = cacherClients.get(channelId, new Callable() { + @Override + public NettyConnection call() throws Exception { + NettyConnection client = getFromRedis(channelId); + return client; + } + }); + if (client == null || !client.isClosed()) { + cacherClients.invalidate(channelId); + return null; + } + return client; + } - public void add(Connection connection) { - connections.put(connection.getId(), connection); + public void add(NettyConnection connection) { + cacherClients.put(connection.getId(), connection); } - public void remove(Connection connection) { - connections.remove(connection.getId()); + public void remove(String channelId) { + cacherClients.invalidate(channelId); + } + + private NettyConnection getFromRedis(String channelId){ + return null; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java new file mode 100644 index 00000000..4c776b8b --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -0,0 +1,108 @@ +package com.shinemo.mpush.core; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public class NettyConnection implements Connection { + + private static final Logger log = LoggerFactory.getLogger(NettyConnection.class); + + private ConnectionInfo info; + private Channel channel; + private int status = 0; + + private volatile long lastReadTime = System.currentTimeMillis(); + + public void init(Channel channel) { + this.channel = channel; + } + + @Override + public String getId() { + return channel.id().asShortText(); + } + + @Override + public void send(final Packet packet) { + if (packet != null) { + if (channel.isWritable()) { + ChannelFuture wf = channel.writeAndFlush(packet); + wf.addListener(new ChannelFutureListener() { + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + log.error("server write response error,request id is: " + packet.toString()); + if (!channel.isActive()) { + channel.close(); + } + } + } + }); + }else{ + //TODO + } + } + } + + @Override + public boolean isClosed() { + return false; + } + + @Override + public boolean isOpen() { + return false; + } + + public ConnectionInfo getInfo() { + return info; + } + + public void setInfo(ConnectionInfo info) { + this.info = info; + } + + public Channel getChannel() { + return channel; + } + + @Override + public void refreshLastReadTime(long lastReadTime) { + this.lastReadTime = lastReadTime; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public long getLastReadTime() { + return lastReadTime; + } + + public void setLastReadTime(long lastReadTime) { + this.lastReadTime = lastReadTime; + } + + public void setChannel(Channel channel) { + this.channel = channel; + } + + @Override + public void close() { + this.channel.close(); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java new file mode 100644 index 00000000..177ee414 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java @@ -0,0 +1,9 @@ +package com.shinemo.mpush.core.task; + +import com.shinemo.mpush.core.NettyConnection; + +public interface ScanTask { + + public void visit(long now,NettyConnection client); + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java new file mode 100644 index 00000000..780b030d --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java @@ -0,0 +1,47 @@ +package com.shinemo.mpush.core.thread; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class NamedThreadFactory implements ThreadFactory{ + + private static final AtomicInteger poolNum = new AtomicInteger(1); + + private final AtomicInteger threadNum = new AtomicInteger(1); + + private final ThreadGroup group; + private final String namePre; + private final boolean isDaemon; + + public NamedThreadFactory(){ + this("pool"); + } + + public NamedThreadFactory(String prefix){ + this(prefix,true); + } + + public NamedThreadFactory(String prefix,boolean daemon) { + SecurityManager manager = System.getSecurityManager(); + if(manager!=null){ + group = manager.getThreadGroup(); + }else{ + group = Thread.currentThread().getThreadGroup(); + } + isDaemon = daemon; + namePre = prefix+"-"+poolNum.getAndIncrement()+"-thread-"; + } + + /** + * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 + */ + @Override + public Thread newThread(Runnable runnable) { + Thread t = new Thread(group, runnable,namePre+threadNum.getAndIncrement(),0); + t.setContextClassLoader(NamedThreadFactory.class.getClassLoader()); + t.setPriority(Thread.MAX_PRIORITY); + t.setDaemon(isDaemon); + return t; + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java new file mode 100644 index 00000000..6abab035 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.core.thread; + +public class ThreadNameSpace { + + /** + * netty boss 线程 + */ + public static final String NETTY_BOSS = "mg-boss"; + + /** + * netty worker 线程 + */ + public static final String NETTY_WORKER = "mg-worker"; + + /** + * connection 定期检测线程 + */ + public static final String NETTY_TIMER = "mg-timer"; + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java b/mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java new file mode 100644 index 00000000..bc515cc0 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java @@ -0,0 +1,41 @@ +package com.shinemo.mpush.core.util; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class InetAddressUtil { + + private static final Logger log = LoggerFactory.getLogger(InetAddressUtil.class); + + /** + * 获取本机ip + * 只获取第一块网卡绑定的ip地址 + * @return + */ + public static String getInetAddress(){ + try{ + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress address = null; + while(interfaces.hasMoreElements()){ + NetworkInterface ni = interfaces.nextElement(); + Enumeration addresses = ni.getInetAddresses(); + while(addresses.hasMoreElements()){ + address = addresses.nextElement(); + if(!address.isLoopbackAddress()&&address.getHostAddress().indexOf(":")==-1&&address.isSiteLocalAddress()){ + return address.getHostAddress(); + } + } + } + log.warn("[InetAddressUtil] getInetAddress is null"); + return null; + }catch(Throwable e){ + log.warn("[InetAddressUtil] getInetAddress exception",e); + return null; + } + } + +} From 5f6789bc75c1ac769ba44ad97213cd18bfb23309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 09:13:58 +0800 Subject: [PATCH 008/890] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 40 ++++++++++- .../com/shinemo/mpush/api/Connection.java | 10 ++- .../java/com/shinemo/mpush/api/Request.java | 1 - .../netty/handler/ConnectionHandler.java | 6 +- .../shinemo/mpush/core/ConnectionManager.java | 69 +++++++++---------- .../shinemo/mpush/core/NettyConnection.java | 36 ++++++---- .../com/shinemo/mpush/core/task/ScanTask.java | 5 ++ 7 files changed, 113 insertions(+), 54 deletions(-) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index b3547d00..32e966be 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -13,4 +13,42 @@ 1.0-SNAPSHOT jar - \ No newline at end of file + + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + commons-logging + commons-logging + + + log4j + log4j + + + org.logback-extensions + logback-ext-spring + + + + io.netty + netty-all + + + + com.google.guava + guava + + + + + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 947ae981..5a7ae807 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.api; +import io.netty.channel.Channel; + import com.shinemo.mpush.api.protocol.Packet; /** @@ -15,8 +17,14 @@ public interface Connection { boolean isOpen(); - void refreshLastReadTime(long lastReadTime); + int getHbTimes(); void close(); + + boolean isConnected(); + + boolean isEnable(); + + void init(Channel channel); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java index 3f2be441..2e8388af 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java @@ -34,7 +34,6 @@ public Response getResponse() { packet.command = message.command; packet.msgId = message.msgId; packet.version = message.version; - packet.msgType = message.msgType; return new Response(packet, connection); } } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index a60d1e4d..d1d01ac2 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -2,10 +2,12 @@ import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.core.ConnectionImpl; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.MessageReceiver; +import com.shinemo.mpush.core.NettyConnection; + import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; @@ -17,7 +19,7 @@ */ public class ConnectionHandler extends ChannelHandlerAdapter { private MessageReceiver receiver = new MessageReceiver(); - private ConnectionImpl connection = new ConnectionImpl(); + private Connection connection = new NettyConnection(); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index 467fbaf4..c133c20f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -1,63 +1,58 @@ package com.shinemo.mpush.core; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; import com.shinemo.mpush.api.Connection; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; -import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/22. */ public class ConnectionManager { public static final ConnectionManager INSTANCE = new ConnectionManager(); -// private final ConcurrentMap connections = new ConcurrentHashMapV8(); - - private final Cache cacherClients = CacheBuilder.newBuilder() - .maximumSize(2<<17) - .expireAfterAccess(27, TimeUnit.MINUTES) - .removalListener(new RemovalListener() { - public void onRemoval(RemovalNotification notification) { - if(notification.getValue().isClosed()){ -// notification.getValue().close("[Remoting] removed from cache"); - } - }; - }).build(); + + //可能会有20w的链接数 + private final ConcurrentMap connections = new ConcurrentHashMapV8(); + +// private final Cache cacherClients = CacheBuilder.newBuilder() +// .maximumSize(2<<17) +// .expireAfterAccess(27, TimeUnit.MINUTES) +// .removalListener(new RemovalListener() { +// public void onRemoval(RemovalNotification notification) { +// if(notification.getValue().isClosed()){ +//// notification.getValue().close("[Remoting] removed from cache"); +// } +// }; +// }).build(); public Connection get(final String channelId) throws ExecutionException { - NettyConnection client = cacherClients.get(channelId, new Callable() { - @Override - public NettyConnection call() throws Exception { - NettyConnection client = getFromRedis(channelId); - return client; - } - }); - if (client == null || !client.isClosed()) { - cacherClients.invalidate(channelId); - return null; - } - return client; +// NettyConnection client = cacherClients.get(channelId, new Callable() { +// @Override +// public NettyConnection call() throws Exception { +// NettyConnection client = getFromRedis(channelId); +// return client; +// } +// }); +// if (client == null || !client.isClosed()) { +// cacherClients.invalidate(channelId); +// return null; +// } +// return client; + + return connections.get(channelId); } - public void add(NettyConnection connection) { - cacherClients.put(connection.getId(), connection); + public void add(Connection connection) { + connections.putIfAbsent(connection.getId(), connection); } - public void remove(String channelId) { - cacherClients.invalidate(channelId); + public void remove(Connection connection) { + connections.remove(connection.getId()); } - private NettyConnection getFromRedis(String channelId){ - return null; - } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 4c776b8b..600e6891 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -22,8 +22,9 @@ public class NettyConnection implements Connection { private Channel channel; private int status = 0; - private volatile long lastReadTime = System.currentTimeMillis(); - + private int hbTimes; + + @Override public void init(Channel channel) { this.channel = channel; } @@ -76,9 +77,12 @@ public Channel getChannel() { return channel; } - @Override - public void refreshLastReadTime(long lastReadTime) { - this.lastReadTime = lastReadTime; + public int increaseAndGetHbTimes(){ + return ++hbTimes; + } + + public void resetHbTimes(){ + hbTimes = 0; } public int getStatus() { @@ -89,13 +93,6 @@ public void setStatus(int status) { this.status = status; } - public long getLastReadTime() { - return lastReadTime; - } - - public void setLastReadTime(long lastReadTime) { - this.lastReadTime = lastReadTime; - } public void setChannel(Channel channel) { this.channel = channel; @@ -105,4 +102,19 @@ public void setChannel(Channel channel) { public void close() { this.channel.close(); } + + @Override + public int getHbTimes() { + return hbTimes; + } + + @Override + public boolean isConnected(){ + return channel.isActive(); + } + + @Override + public boolean isEnable(){ + return channel.isWritable(); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java index 177ee414..f2b3b930 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java @@ -4,6 +4,11 @@ public interface ScanTask { + /** + * + * @param now 扫描触发的时间点 + * @param client 当前扫描到的连接 + */ public void visit(long now,NettyConnection client); } From 4fd09b233daaae57a8f218caf25a2a09e20b7f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 09:30:33 +0800 Subject: [PATCH 009/890] =?UTF-8?q?nettyconnection=20=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/handler/ConnectionHandler.java | 9 +++++---- .../com/shinemo/mpush/core/ConnectionManager.java | 15 +++++++++++++++ .../com/shinemo/mpush/core/NettyConnection.java | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index d1d01ac2..163f0a6f 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -19,22 +19,23 @@ */ public class ConnectionHandler extends ChannelHandlerAdapter { private MessageReceiver receiver = new MessageReceiver(); - private Connection connection = new NettyConnection(); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); receiver.onMessage(new Request((Packet) msg, connection)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); - ConnectionManager.INSTANCE.remove(connection); + ConnectionManager.INSTANCE.remove(ctx.channel()); } @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { super.connect(ctx, remoteAddress, localAddress, promise); + Connection connection = new NettyConnection(); connection.init(ctx.channel()); ConnectionManager.INSTANCE.add(connection); } @@ -42,12 +43,12 @@ public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, Sock @Override public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { super.disconnect(ctx, promise); - ConnectionManager.INSTANCE.remove(connection); + ConnectionManager.INSTANCE.remove(ctx.channel()); } @Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { super.close(ctx, promise); - ConnectionManager.INSTANCE.remove(connection); + ConnectionManager.INSTANCE.remove(ctx.channel()); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index c133c20f..6f77ea06 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -3,6 +3,7 @@ import com.shinemo.mpush.api.Connection; +import io.netty.channel.Channel; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import java.util.concurrent.ConcurrentMap; @@ -46,13 +47,27 @@ public Connection get(final String channelId) throws ExecutionException { return connections.get(channelId); } + + public Connection get(final Channel channel){ + return connections.get(channel.id().asLongText()); + } public void add(Connection connection) { connections.putIfAbsent(connection.getId(), connection); } + + public void add(Channel channel){ + Connection connection = new NettyConnection(); + connection.init(channel); + connections.putIfAbsent(connection.getId(), connection); + } public void remove(Connection connection) { connections.remove(connection.getId()); } + public void remove(Channel channel){ + connections.remove(channel.id().asLongText()); + } + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 600e6891..36dbc91f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -31,7 +31,7 @@ public void init(Channel channel) { @Override public String getId() { - return channel.id().asShortText(); + return channel.id().asLongText(); } @Override From 94c70fbf22e42a5dfcecfa61c8d2435e2fde93b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 09:42:27 +0800 Subject: [PATCH 010/890] add log --- .../netty/handler/ConnectionHandler.java | 26 ++++++++++++-- .../shinemo/mpush/core/ConnectionManager.java | 36 ++++++------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 163f0a6f..4140e0d0 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -14,40 +14,62 @@ import java.net.SocketAddress; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Created by ohun on 2015/12/19. */ public class ConnectionHandler extends ChannelHandlerAdapter { + + private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class); + private MessageReceiver receiver = new MessageReceiver(); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.warn("",ctx.channel().remoteAddress()+", channelRead"); Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); receiver.onMessage(new Request((Packet) msg, connection)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - super.exceptionCaught(ctx, cause); ConnectionManager.INSTANCE.remove(ctx.channel()); + log.warn("",ctx.channel().remoteAddress()+", exceptionCaught"); } @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - super.connect(ctx, remoteAddress, localAddress, promise); + log.warn("",ctx.channel().remoteAddress()+", connect"); + super.connect(ctx, remoteAddress, localAddress, promise); Connection connection = new NettyConnection(); connection.init(ctx.channel()); ConnectionManager.INSTANCE.add(connection); } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + log.warn("",ctx.channel().remoteAddress()+", channelActive"); + super.channelActive(ctx); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + log.warn("",ctx.channel().remoteAddress()+", channelInactive"); + super.channelInactive(ctx); + } @Override public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + log.warn("",ctx.channel().remoteAddress()+", disconnect"); super.disconnect(ctx, promise); ConnectionManager.INSTANCE.remove(ctx.channel()); } @Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + log.warn("",ctx.channel().remoteAddress()+", close"); super.close(ctx, promise); ConnectionManager.INSTANCE.remove(ctx.channel()); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index 6f77ea06..6a32a69e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -6,6 +6,8 @@ import io.netty.channel.Channel; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; @@ -18,34 +20,8 @@ public class ConnectionManager { //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8(); -// private final Cache cacherClients = CacheBuilder.newBuilder() -// .maximumSize(2<<17) -// .expireAfterAccess(27, TimeUnit.MINUTES) -// .removalListener(new RemovalListener() { -// public void onRemoval(RemovalNotification notification) { -// if(notification.getValue().isClosed()){ -//// notification.getValue().close("[Remoting] removed from cache"); -// } -// }; -// }).build(); - public Connection get(final String channelId) throws ExecutionException { - -// NettyConnection client = cacherClients.get(channelId, new Callable() { -// @Override -// public NettyConnection call() throws Exception { -// NettyConnection client = getFromRedis(channelId); -// return client; -// } -// }); -// if (client == null || !client.isClosed()) { -// cacherClients.invalidate(channelId); -// return null; -// } -// return client; - return connections.get(channelId); - } public Connection get(final Channel channel){ @@ -70,4 +46,12 @@ public void remove(Channel channel){ connections.remove(channel.id().asLongText()); } + public List getConnectionIds(){ + return new ArrayList(connections.keySet()); + } + + public List getConnections(){ + return new ArrayList(connections.values()); + } + } From f8689c2510682e7d09b6881301b6955309f6782c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 10:13:19 +0800 Subject: [PATCH 011/890] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/api/Connection.java | 2 + .../java/com/shinemo/mpush/api/Constants.java | 1 + .../netty/encoder/PacketDecoder.java | 1 - .../netty/server/ConnectionServer.java | 2 + .../netty/task/ScanAllClientConnection.java | 56 +++++++++++++++++++ .../mpush/connection/netty/task/ScanTask.java | 14 +++++ .../shinemo/mpush/core/NettyConnection.java | 5 ++ .../com/shinemo/mpush/core/task/ScanTask.java | 14 ----- 8 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 5a7ae807..df6ed642 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -26,5 +26,7 @@ public interface Connection { boolean isEnable(); void init(Channel channel); + + String remoteIp(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 4440e227..1eed1343 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -11,4 +11,5 @@ public interface Constants { int HEADER_LEN = 13; byte MAGIC_NUM1 = (byte) 33; byte MAGIC_NUM2 = (byte) 99; + long TIME_DELAY = 58L; } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java index 42e0a5c0..43cc5afa 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java @@ -6,7 +6,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.DecoderException; import java.util.List; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index 93c5d5ef..ca8251b6 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -1,10 +1,12 @@ package com.shinemo.mpush.connection.netty.server; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.connection.netty.NettySharedHolder; import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java new file mode 100644 index 00000000..572821aa --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java @@ -0,0 +1,56 @@ +package com.shinemo.mpush.connection.netty.task; + +import io.netty.util.Timeout; +import io.netty.util.TimerTask; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.connection.netty.NettySharedHolder; +import com.shinemo.mpush.connection.netty.task.ScanTask; +import com.shinemo.mpush.core.ConnectionManager; + +/** + * 定时全量扫描connection + * + */ +public class ScanAllClientConnection implements TimerTask{ + + private static final Logger log = LoggerFactory.getLogger(ScanAllClientConnection.class); + + private final List taskList = new ArrayList(); + + public ScanAllClientConnection(final ScanTask...scanTasks) { + if(scanTasks!=null){ + for(final ScanTask task:scanTasks){ + this.taskList.add(task); + } + } + } + + @Override + public void run(Timeout timeout) throws Exception { + try{ + final long now = System.currentTimeMillis(); + List connections = ConnectionManager.INSTANCE.getConnections(); + if(connections!=null){ + for(Connection conn:connections){ + for(final ScanTask task:this.taskList){ + task.visit(now, conn); + } + } + } + }catch(Exception e){ + log.error("","exception on scan",e); + }finally{ + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + } + +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java new file mode 100644 index 00000000..6b4db50c --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.connection.netty.task; + +import com.shinemo.mpush.api.Connection; + +public interface ScanTask { + + /** + * + * @param now 扫描触发的时间点 + * @param client 当前扫描到的连接 + */ + public void visit(long now,Connection conn); + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 36dbc91f..b88d1989 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -117,4 +117,9 @@ public boolean isConnected(){ public boolean isEnable(){ return channel.isWritable(); } + + @Override + public String remoteIp() { + return channel.remoteAddress().toString(); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java deleted file mode 100644 index f2b3b930..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.shinemo.mpush.core.task; - -import com.shinemo.mpush.core.NettyConnection; - -public interface ScanTask { - - /** - * - * @param now 扫描触发的时间点 - * @param client 当前扫描到的连接 - */ - public void visit(long now,NettyConnection client); - -} From 537e34eb0a8fb1778ad60da764304c88c04dfb76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 10:53:55 +0800 Subject: [PATCH 012/890] add thread pool manage --- .../java/com/shinemo/mpush/api/Constants.java | 6 + .../netty/handler/ConnectionHandler.java | 2 + .../mpush/core/thread/ThreadNameSpace.java | 4 + .../mpush/core/thread/ThreadPoolManager.java | 179 ++++++++++++++++++ .../mpush/core/thread/ThreadPoolUtil.java | 15 ++ .../com/shinemo/mpush/core/util/JVMUtil.java | 31 +++ 6 files changed, 237 insertions(+) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 1eed1343..278f3470 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -12,4 +12,10 @@ public interface Constants { byte MAGIC_NUM1 = (byte) 33; byte MAGIC_NUM2 = (byte) 99; long TIME_DELAY = 58L; + + String JVM_LOG_PATH = "/opt/"; + + int THREAD_QUEUE_SIZE = 10000; + int MIN_POOL_SIZE = 50; + int MAX_POOL_SIZE = 500; } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 4140e0d0..06666f7c 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -73,4 +73,6 @@ public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exce super.close(ctx, promise); ConnectionManager.INSTANCE.remove(ctx.channel()); } + + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java index 6abab035..c4d6d237 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java @@ -16,5 +16,9 @@ public class ThreadNameSpace { * connection 定期检测线程 */ public static final String NETTY_TIMER = "mg-timer"; + + public static final String getUniqueName(String serviceName){ + return "mg-sn-"+serviceName; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java new file mode 100644 index 00000000..fb8ab89d --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java @@ -0,0 +1,179 @@ +package com.shinemo.mpush.core.thread; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.core.util.JVMUtil; + +public class ThreadPoolManager { + + private static final long keepAliveTime = 300L; + + private final RejectedExecutionHandler handler = new IgnoreRunsPolicy(); + + private final ThreadPoolExecutor defaultPoolExecutor; + + private final Map poolCache = new HashMap(); + + public ThreadPoolManager(int corePoolSize,int maxPoolSize,int queueSize){ + final BlockingQueue workQueue = new SynchronousQueue(); + final ThreadFactory threadFactory = new NamedThreadFactory(ThreadNameSpace.NETTY_WORKER); + defaultPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, + TimeUnit.SECONDS, workQueue,threadFactory,handler); + } + + + private static class IgnoreRunsPolicy implements RejectedExecutionHandler{ + + private final static Logger log = LoggerFactory.getLogger(IgnoreRunsPolicy.class); + + private volatile boolean dump = false; + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + dumpJVMInfo(); + throw new RejectedExecutionException(); + } + + private void dumpJVMInfo(){ + if (!dump) { + dump = true; + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + String logPath = Constants.JVM_LOG_PATH; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, "jstack.log")); + JVMUtil.jstack(jstackStream); + } catch (FileNotFoundException e) { + log.error("", "Dump JVM cache Error!", e); + } catch (Throwable t) { + log.error("", "Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { + } + } + } + } + }); + + } + } + + } + + + public void allocThreadpool(final String serviceUniqueName, int corePoolSize, int maximumPoolSize) + throws Exception { + if (poolCache.containsKey(serviceUniqueName)) { // 对同一个服务重复分配线程池时,抛出异常 + throw new Exception(MessageFormat.format( + "[ThreadPool Manager] Duplicated thread pool allocation request for service [{0}].", + new Object[] { serviceUniqueName })); + } + + if (defaultPoolExecutor == null || defaultPoolExecutor.isShutdown()) { // 线程池已关闭 + throw new Exception(MessageFormat.format( + "[ThreadPool Manager] Can not allocate thread pool for service [{0}].", + new Object[] { serviceUniqueName })); + } + + int balance = defaultPoolExecutor.getMaximumPoolSize(); // 剩余线程数量 + // 剩余线程数量小于申请的线程数量 + if (balance < maximumPoolSize) { + throw new Exception( + MessageFormat + .format("[ThreadPool Manager] Thread pool allocated failed for service [{0}]: balance [{1}] require [{2}].", + new Object[] { serviceUniqueName, balance, maximumPoolSize })); + } + + ThreadFactory threadFactory = new NamedThreadFactory( + ThreadNameSpace.getUniqueName(serviceUniqueName)); + try { + ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, + TimeUnit.SECONDS, new SynchronousQueue(), threadFactory, handler); + poolCache.put(serviceUniqueName, executor); + } catch (Exception e) { + throw new Exception("[ThreadPool Manager] Thread pool allocated failed!", e); + } + + // 重新设置剩余线程数量 + int newBalance = balance - maximumPoolSize; + if (newBalance == 0) { + defaultPoolExecutor.shutdown(); + } else { + if (newBalance < defaultPoolExecutor.getCorePoolSize()) { + defaultPoolExecutor.setCorePoolSize(newBalance); + } + defaultPoolExecutor.setMaximumPoolSize(newBalance); + } + } + + public Executor getThreadExecutor(String serviceUniqueName) { + if (!poolCache.isEmpty()) { + ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); + if (executor != null) { + return executor; + } + } + return defaultPoolExecutor; + } + + public void shutdown() { + if (defaultPoolExecutor != null && !defaultPoolExecutor.isShutdown()) { + defaultPoolExecutor.shutdown(); + } + + if (!poolCache.isEmpty()) { + Iterator ite = poolCache.values().iterator(); + while (ite.hasNext()) { + ThreadPoolExecutor poolExecutor = ite.next(); + poolExecutor.shutdown(); + } + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("当前线程池分配策略:"); + Iterator> ite = poolCache.entrySet().iterator(); + while (ite.hasNext()) { + Map.Entry entry = ite.next(); + String serviceUniqName = entry.getKey(); + ThreadPoolExecutor executor = entry.getValue(); + sb.append("服务[" + serviceUniqName + "]核心线程数量:" + executor.getCorePoolSize() + " 最大线程数量:" + + executor.getMaximumPoolSize() + " 活动线程数量:" + executor.getActiveCount()); + } + + if (!defaultPoolExecutor.isShutdown()) { + sb.append("服务默认使用的核心线程数量:" + defaultPoolExecutor.getCorePoolSize() + " 最大线程数量: " + + defaultPoolExecutor.getMaximumPoolSize() + " 活动线程数量:" + defaultPoolExecutor.getActiveCount()); + } + + return sb.toString(); + } + + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java new file mode 100644 index 00000000..1c3ebc49 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.core.thread; + +import com.shinemo.mpush.api.Constants; + + +public class ThreadPoolUtil { + + private static final ThreadPoolManager threadPoolManager = new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, + Constants.THREAD_QUEUE_SIZE); + + public static ThreadPoolManager getThreadPoolManager() { + return threadPoolManager; + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java b/mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java new file mode 100644 index 00000000..063d1b17 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.core.util; + +import java.io.OutputStream; +import java.util.Iterator; +import java.util.Map; + +public class JVMUtil { + + public static void jstack(OutputStream stream) throws Exception { + try { + Map map = Thread.getAllStackTraces(); + Iterator> ite = map.entrySet().iterator(); + while (ite.hasNext()) { + Map.Entry entry = ite.next(); + StackTraceElement[] elements = entry.getValue(); + if (elements != null && elements.length > 0) { + String threadName = entry.getKey().getName(); + stream.write(("Thread Name :[" + threadName + "]\n").getBytes()); + for (StackTraceElement el : elements) { + String stack = el.toString() + "\n"; + stream.write(stack.getBytes()); + } + stream.write("\n".getBytes()); + } + } + } catch (Exception e) { + throw e; + } + } + +} From 7ea74c375affb8236a8951bf9517a14e6821aa05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 24 Dec 2015 02:55:52 +0000 Subject: [PATCH 013/890] add test --- .../shinemo/mpush/api/protocol/Packet.java | 24 +++++---- .../mpush/connection/ConnectionHandler.java | 50 +++++++++++++++++++ .../netty/server/ConnectionServer.java | 4 +- .../mpush/connection/ClientHandler.java | 44 ++++++++++++++++ .../shinemo/mpush/connection/ClientTest.java | 45 +++++++++++++++++ .../connection/ConnectionServerTest.java | 18 +++++++ pom.xml | 17 ++++++- 7 files changed, 189 insertions(+), 13 deletions(-) create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java create mode 100644 mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java create mode 100644 mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java create mode 100644 mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 48735fa8..0fa8f0f1 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -1,15 +1,16 @@ package com.shinemo.mpush.api.protocol; import java.io.Serializable; + import java.util.Arrays; /** * Created by ohun on 2015/12/19. * magic(2)+cmd(1)+version(1)+flags(1)+msgId(4)+length(4)+body(n) */ -public class Packet implements Serializable{ - private static final long serialVersionUID = -2725825199998223372L; - public byte command; +public class Packet implements Serializable { + private static final long serialVersionUID = -2725825199998223372L; + public byte command; public byte version; public byte flags; public int msgId; @@ -19,10 +20,15 @@ public int getBodyLength() { return body == null ? 0 : body.length; } - @Override - public String toString() { - return "Packet [command=" + command + ", version=" + version + ", flags=" + flags + ", msgId=" + msgId + ", body=" + Arrays.toString(body) + "]"; - } - - + + @Override + public String toString() { + return "Packet{" + + "command=" + command + + ", version=" + version + + ", flags=" + flags + + ", msgId=" + msgId + + ", body=" + Arrays.toString(body) + + '}'; + } } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java new file mode 100644 index 00000000..a920a8a6 --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java @@ -0,0 +1,50 @@ +package com.shinemo.mpush.connection; + + +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.core.MessageReceiver; +import com.shinemo.mpush.core.NettyConnection; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/19. + */ +public class ConnectionHandler extends ChannelHandlerAdapter { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private MessageReceiver receiver = new MessageReceiver(); + private NettyConnection connection = new NettyConnection(); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + System.out.println("channelRead msg=" + msg); + logger.debug("channelRead msg=" + msg); + receiver.onMessage(new Request((Packet) msg, connection)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + super.exceptionCaught(ctx, cause); + ConnectionManager.INSTANCE.remove(connection); + System.err.println("exceptionCaught"); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + connection.init(ctx.channel()); + ConnectionManager.INSTANCE.add(connection); + System.out.println("server receive channelActive"); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + ConnectionManager.INSTANCE.remove(connection); + System.out.println("server receive channelInactive"); + } +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index ca8251b6..87a4d70f 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -62,8 +62,8 @@ public void start() { * 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现, * 并且可以通过构造函数来配置他们的关系。 */ - EventLoopGroup bossGroup = new NioEventLoopGroup(); - EventLoopGroup workerGroup = new NioEventLoopGroup(); + this.bossGroup = new NioEventLoopGroup(); + this.workerGroup = new NioEventLoopGroup(); try { diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java new file mode 100644 index 00000000..60116ea3 --- /dev/null +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -0,0 +1,44 @@ +package com.shinemo.mpush.connection; + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.SocketAddress; + +/** + * Created by ohun on 2015/12/24. + */ +public class ClientHandler extends ChannelHandlerAdapter { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + Packet packet = new Packet(); + packet.command = Command.Handshake.cmd; + packet.version = 0; + packet.flags = 0; + packet.msgId = 1; + packet.body = "hello word".getBytes(Constants.UTF_8); + ctx.writeAndFlush(packet); + System.out.println("client connect"); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + System.out.println("client channelInactive"); + } + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + super.channelRead(ctx, msg); + System.out.println(msg); + logger.debug("channelRead msg=" + msg); + } +} diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java new file mode 100644 index 00000000..dc771ff3 --- /dev/null +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.connection; + +import com.shinemo.mpush.api.protocol.PacketDecoder; +import com.shinemo.mpush.api.protocol.PacketEncoder; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; + +/** + * Created by ohun on 2015/12/24. + */ +public class ClientTest { + @org.junit.Test + public void testClient() { + String host = "127.0.0.1"; + int port = 3000; + EventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap(); // (1) + b.group(workerGroup); // (2) + b.channel(NioSocketChannel.class); // (3) + b.option(ChannelOption.SO_KEEPALIVE, true); // (4) + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(new ClientHandler()); + } + }); + ChannelFuture f = b.connect(host, port).sync(); // (5) + f.channel().closeFuture().sync(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + workerGroup.shutdownGracefully(); + } + + } +} diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java new file mode 100644 index 00000000..8bf9f1a0 --- /dev/null +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java @@ -0,0 +1,18 @@ +package com.shinemo.mpush.connection; + +/** + * Created by ohun on 2015/12/24. + */ +public class ConnectionServerTest { + + @org.junit.Test + public void testStop() throws Exception { + + } + + @org.junit.Test + public void testStart() throws Exception { + ConnectionServer server = new ConnectionServer(3000); + server.start(); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index c3672684..9de87d41 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,6 @@ 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT 5.0.0.Alpha2 - @@ -145,7 +144,21 @@ - + + + junit + junit + 4.12 + + + org.slf4j + slf4j-api + + + io.netty + netty-all + + From f40f0c325da46656b428abf1180a0bb8bd3a3efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 11:15:06 +0800 Subject: [PATCH 014/890] change connection handler --- .../mpush/connection/netty/handler/ConnectionHandler.java | 6 +++++- .../mpush/connection/netty/server/ConnectionServer.java | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 06666f7c..0c85ea5b 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -24,7 +24,11 @@ public class ConnectionHandler extends ChannelHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class); - private MessageReceiver receiver = new MessageReceiver(); + private MessageReceiver receiver; + + public ConnectionHandler(MessageReceiver receiver) { + this.receiver = receiver; + } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index ca8251b6..e85f6e51 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -11,9 +11,11 @@ import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; import com.shinemo.mpush.connection.netty.handler.ConnectionHandler; +import com.shinemo.mpush.core.MessageReceiver; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; @@ -84,6 +86,10 @@ public void start() { * 这里告诉Channel如何获取新的连接. */ b.channel(NioServerSocketChannel.class); + + MessageReceiver receiver = new MessageReceiver(); + + final ConnectionHandler connectionHandler = new ConnectionHandler(receiver); /*** * 这里的事件处理类经常会被用来处理一个最近的已经接收的Channel。 @@ -99,7 +105,7 @@ public void start() { public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(new ConnectionHandler()); + ch.pipeline().addLast(connectionHandler); } }); From 8543f9884fdf389d801b2174f42084fbdd257f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 11:36:42 +0800 Subject: [PATCH 015/890] add logback.xml --- mpush-connection/pom.xml | 4 +++ .../src/test/resources/logback.xml | 30 +++++++++++++++++++ pom.xml | 6 ++++ 3 files changed, 40 insertions(+) create mode 100644 mpush-connection/src/test/resources/logback.xml diff --git a/mpush-connection/pom.xml b/mpush-connection/pom.xml index a61e11e9..f1e7518f 100644 --- a/mpush-connection/pom.xml +++ b/mpush-connection/pom.xml @@ -26,6 +26,10 @@ io.netty netty-all + + junit + junit + diff --git a/mpush-connection/src/test/resources/logback.xml b/mpush-connection/src/test/resources/logback.xml new file mode 100644 index 00000000..e638aca3 --- /dev/null +++ b/mpush-connection/src/test/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + INFO + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/pom.xml b/pom.xml index 9de87d41..683d615a 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,12 @@ guava 19.0 + + + junit + junit + 4.10 + From d242f5cb7578466161fc5aa7da4aa33b8f5cf3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 11:45:25 +0800 Subject: [PATCH 016/890] add test log --- .../com/shinemo/mpush/connection/ClientHandler.java | 12 ++++-------- .../com/shinemo/mpush/connection/ClientTest.java | 12 ++++++++---- .../mpush/connection/ConnectionServerTest.java | 8 ++++++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java index 60116ea3..4e5dda2a 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -5,17 +5,14 @@ import com.shinemo.mpush.api.protocol.Packet; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.net.SocketAddress; - /** * Created by ohun on 2015/12/24. */ public class ClientHandler extends ChannelHandlerAdapter { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private static final Logger logger = LoggerFactory.getLogger(ClientHandler.class); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { @@ -27,18 +24,17 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { packet.msgId = 1; packet.body = "hello word".getBytes(Constants.UTF_8); ctx.writeAndFlush(packet); - System.out.println("client connect"); + logger.info("client,",ctx.channel().remoteAddress().toString(),"channelActive"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); - System.out.println("client channelInactive"); + logger.info("client,",ctx.channel().remoteAddress().toString(),"channelInactive"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); - System.out.println(msg); - logger.debug("channelRead msg=" + msg); + logger.info("client,",ctx.channel().remoteAddress().toString(),"channelRead",msg); } } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index dc771ff3..d4ec9635 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -1,7 +1,10 @@ package com.shinemo.mpush.connection; -import com.shinemo.mpush.api.protocol.PacketDecoder; -import com.shinemo.mpush.api.protocol.PacketEncoder; +import org.junit.Test; + +import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; +import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; + import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; @@ -15,8 +18,9 @@ * Created by ohun on 2015/12/24. */ public class ClientTest { - @org.junit.Test - public void testClient() { + + @Test + public void testClient() { String host = "127.0.0.1"; int port = 3000; EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java index 8bf9f1a0..cbb7a63d 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java @@ -1,16 +1,20 @@ package com.shinemo.mpush.connection; +import org.junit.Test; + +import com.shinemo.mpush.connection.netty.server.ConnectionServer; + /** * Created by ohun on 2015/12/24. */ public class ConnectionServerTest { - @org.junit.Test + @Test public void testStop() throws Exception { } - @org.junit.Test + @Test public void testStart() throws Exception { ConnectionServer server = new ConnectionServer(3000); server.start(); From fb3f91fe8e29d29f27b867cb89809032384ac01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 17:40:15 +0800 Subject: [PATCH 017/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dlog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connection/netty/handler/ConnectionHandler.java | 12 ++++++------ .../connection/netty/server/ConnectionServer.java | 4 ++-- .../com/shinemo/mpush/connection/ClientHandler.java | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 0c85ea5b..fa65caee 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -32,7 +32,7 @@ public ConnectionHandler(MessageReceiver receiver) { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - log.warn("",ctx.channel().remoteAddress()+", channelRead"); + log.warn(ctx.channel().remoteAddress()+", channelRead"); Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); receiver.onMessage(new Request((Packet) msg, connection)); } @@ -45,7 +45,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - log.warn("",ctx.channel().remoteAddress()+", connect"); + log.warn(ctx.channel().remoteAddress()+", connect"); super.connect(ctx, remoteAddress, localAddress, promise); Connection connection = new NettyConnection(); connection.init(ctx.channel()); @@ -54,26 +54,26 @@ public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, Sock @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - log.warn("",ctx.channel().remoteAddress()+", channelActive"); + log.warn(ctx.channel().remoteAddress()+", channelActive"); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.warn("",ctx.channel().remoteAddress()+", channelInactive"); + log.warn(ctx.channel().remoteAddress()+", channelInactive"); super.channelInactive(ctx); } @Override public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - log.warn("",ctx.channel().remoteAddress()+", disconnect"); + log.warn(ctx.channel().remoteAddress()+", disconnect"); super.disconnect(ctx, promise); ConnectionManager.INSTANCE.remove(ctx.channel()); } @Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - log.warn("",ctx.channel().remoteAddress()+", close"); + log.warn(ctx.channel().remoteAddress()+", close"); super.close(ctx, promise); ConnectionManager.INSTANCE.remove(ctx.channel()); } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index ff1ae8f6..7b1bf401 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -133,13 +133,13 @@ public void initChannel(SocketChannel ch) throws Exception { */ ChannelFuture f = b.bind(port).sync(); + log.info("server start ok on:"+port); + /** * 这里会一直等待,直到socket被关闭 */ f.channel().closeFuture().sync(); - log.info("server start ok on:"+port); - } catch (Exception e) { log.error("server start exception",e); /*** diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java index 4e5dda2a..cac26346 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -24,17 +24,17 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { packet.msgId = 1; packet.body = "hello word".getBytes(Constants.UTF_8); ctx.writeAndFlush(packet); - logger.info("client,",ctx.channel().remoteAddress().toString(),"channelActive"); + logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelActive"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); - logger.info("client,",ctx.channel().remoteAddress().toString(),"channelInactive"); + logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelInactive"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); - logger.info("client,",ctx.channel().remoteAddress().toString(),"channelRead",msg); + logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelRead",msg); } } From f2f526f77886a6a14bb3ca08c23efdcf626d1ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 24 Dec 2015 20:52:27 +0800 Subject: [PATCH 018/890] add heart beat test --- .../java/com/shinemo/mpush/api/Message.java | 3 + .../netty/client/ConnectionClient.java | 19 ++++ .../netty/handler/ConnectionHandler.java | 18 ++- .../shinemo/mpush/connection/ClientTest.java | 104 +++++++++++++----- .../shinemo/mpush/core/MessageReceiver.java | 3 + .../shinemo/mpush/core/NettyConnection.java | 15 ++- .../mpush/core/handler/HeartBeatHandler.java | 24 ++++ .../mpush/core/message/HeartBeatMessage.java | 15 +++ 8 files changed, 161 insertions(+), 40 deletions(-) create mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java index 84df570e..8bfc3e04 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java @@ -4,4 +4,7 @@ * Created by ohun on 2015/12/22. */ public interface Message { + + + } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java new file mode 100644 index 00000000..bd8f25d9 --- /dev/null +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java @@ -0,0 +1,19 @@ +package com.shinemo.mpush.connection.netty.client; + +import io.netty.channel.Channel; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConnectionClient { + + private static final Logger log = LoggerFactory.getLogger(ConnectionClient.class); + + private final Channel channel; + private int hbTimes = 0; + + public ConnectionClient(final String remoteIp,final int remotePort,final Channel channel) { + this.channel = channel; + } + +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index fa65caee..f94805e2 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -3,16 +3,22 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.NettyConnection; +import com.shinemo.mpush.core.thread.ThreadNameSpace; +import com.shinemo.mpush.core.thread.ThreadPoolUtil; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import java.net.SocketAddress; +import java.util.concurrent.Executor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +32,8 @@ public class ConnectionHandler extends ChannelHandlerAdapter { private MessageReceiver receiver; + private static Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor(ThreadNameSpace.NETTY_WORKER); + public ConnectionHandler(MessageReceiver receiver) { this.receiver = receiver; } @@ -43,15 +51,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E log.warn("",ctx.channel().remoteAddress()+", exceptionCaught"); } - @Override - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - log.warn(ctx.channel().remoteAddress()+", connect"); - super.connect(ctx, remoteAddress, localAddress, promise); - Connection connection = new NettyConnection(); - connection.init(ctx.channel()); - ConnectionManager.INSTANCE.add(connection); - } - @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { log.warn(ctx.channel().remoteAddress()+", channelActive"); @@ -78,5 +77,4 @@ public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exce ConnectionManager.INSTANCE.remove(ctx.channel()); } - } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index d4ec9635..1644fda7 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -1,49 +1,103 @@ package com.shinemo.mpush.connection; +import java.util.concurrent.TimeUnit; + import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.connection.netty.NettySharedHolder; import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; +import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.core.message.HeartBeatMessage; import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.Timeout; +import io.netty.util.TimerTask; /** * Created by ohun on 2015/12/24. */ public class ClientTest { + + private static final Logger log = LoggerFactory.getLogger(ClientTest.class); @Test public void testClient() { - String host = "127.0.0.1"; - int port = 3000; - EventLoopGroup workerGroup = new NioEventLoopGroup(); - try { - Bootstrap b = new Bootstrap(); // (1) - b.group(workerGroup); // (2) - b.channel(NioSocketChannel.class); // (3) - b.option(ChannelOption.SO_KEEPALIVE, true); // (4) - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(new ClientHandler()); - } - }); - ChannelFuture f = b.connect(host, port).sync(); // (5) - f.channel().closeFuture().sync(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - workerGroup.shutdownGracefully(); - } - - } + String host = "127.0.0.1"; + int port = 3000; + EventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap(); // (1) + b.group(workerGroup); // (2) + b.channel(NioSocketChannel.class); // (3) + b.option(ChannelOption.SO_KEEPALIVE, true); // (4) + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(new ClientHandler()); + } + }); + ChannelFuture future = b.connect(host, port).sync(); // (5) + + if (future.awaitUninterruptibly(4000) && future.isSuccess()) { + final Channel channel = future.channel(); + NettySharedHolder.timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + final Packet packet = buildHeartBeat(); + ChannelFuture channelFuture = channel.writeAndFlush(packet); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if(!future.isSuccess()){ + if(!channel.isActive()){ + log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active"); + ConnectionManager.INSTANCE.remove(channel); + } + log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet); + }else{ + log.warn("client send msg success:"+channel.remoteAddress().toString()+","+packet); + } + } + }); + } + }, Constants.TIME_DELAY, TimeUnit.SECONDS); + } else { + future.cancel(true); + future.channel().close(); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + workerGroup.shutdownGracefully(); + } + + } + + private static Packet buildHeartBeat() { + Packet packet = new Packet(); + packet.command = Command.Heartbeat.cmd; + packet.version = 0; + packet.flags = 0; + packet.msgId = 1; + return packet; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java index 090650eb..558701d1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.core.handler.BindHandler; +import com.shinemo.mpush.core.handler.HeartBeatHandler; import com.shinemo.mpush.core.handler.LoginHandler; /** @@ -12,11 +13,13 @@ public class MessageReceiver implements Receiver { public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); public static final MessageHandler BIND_HANDLER = new BindHandler(); + public static final HeartBeatHandler HEART_HANDLER = new HeartBeatHandler(); @Override public void onMessage(Request request) { switch (request.getCommand()) { case Heartbeat: + HEART_HANDLER.handle(request); break; case Handshake: break; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index b88d1989..7749cc4b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -40,17 +40,22 @@ public void send(final Packet packet) { if (channel.isWritable()) { ChannelFuture wf = channel.writeAndFlush(packet); wf.addListener(new ChannelFutureListener() { + + @Override public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - log.error("server write response error,request id is: " + packet.toString()); - if (!channel.isActive()) { - channel.close(); + if(!future.isSuccess()){ + if(!channel.isActive()){ + log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active"); + ConnectionManager.INSTANCE.remove(channel); } + log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet); + }else{ + log.warn("send msg success:"+channel.remoteAddress().toString()+","+packet); } } }); }else{ - //TODO + log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet+", channel is not writable"); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java new file mode 100644 index 00000000..73ae059d --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -0,0 +1,24 @@ +package com.shinemo.mpush.core.handler; + +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.core.message.HeartBeatMessage; +import com.shinemo.mpush.core.message.LoginMessage; + +/** + * Created by ohun on 2015/12/22. + */ +public class HeartBeatHandler extends BaseMessageHandler { + + @Override + public HeartBeatMessage decodeBody(Packet packet) { + return null; + } + + @Override + public void handle(HeartBeatMessage message, Request request) { + request.getResponse().send(new byte[]{}); + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java new file mode 100644 index 00000000..4dea6303 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Message; + +/** + * Created by ohun on 2015/12/22. + */ +public class HeartBeatMessage implements Message { + + + public HeartBeatMessage() { + + } + +} From 5598e33746034703ce625dfbf67bc70797a1088e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 25 Dec 2015 10:03:41 +0800 Subject: [PATCH 019/890] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Constants.java | 6 +++ .../netty/handler/ConnectionHandler.java | 2 +- .../netty/server/ConnectionServer.java | 11 ++--- .../shinemo/mpush/connection/ClientTest.java | 2 + .../connection/ConnectionServerTest.java | 11 ++++- .../mpush/core/thread/ThreadPoolManager.java | 40 +++++++++++++++++++ .../mpush/core/thread/ThreadPoolUtil.java | 16 +++++++- 7 files changed, 80 insertions(+), 8 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 278f3470..42736cc5 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -18,4 +18,10 @@ public interface Constants { int THREAD_QUEUE_SIZE = 10000; int MIN_POOL_SIZE = 50; int MAX_POOL_SIZE = 500; + + int MIN_BOSS_POOL_SIZE = 10; + int MAX_BOSS_POLL_SIZE = 50; + + int MIN_WORK_POOL_SIZE = 10; + int MAX_WORK_POOL_SIZE = 250; } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index f94805e2..78fd7c26 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -32,7 +32,7 @@ public class ConnectionHandler extends ChannelHandlerAdapter { private MessageReceiver receiver; - private static Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor(ThreadNameSpace.NETTY_WORKER); + public ConnectionHandler(MessageReceiver receiver) { this.receiver = receiver; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index 7b1bf401..a24449f4 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -1,21 +1,19 @@ package com.shinemo.mpush.connection.netty.server; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.connection.netty.NettySharedHolder; import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; import com.shinemo.mpush.connection.netty.handler.ConnectionHandler; import com.shinemo.mpush.core.MessageReceiver; +import com.shinemo.mpush.core.thread.ThreadPoolUtil; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; @@ -32,6 +30,8 @@ public class ConnectionServer { private final AtomicBoolean startFlag = new AtomicBoolean(false); + + private final int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; @@ -64,8 +64,8 @@ public void start() { * 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现, * 并且可以通过构造函数来配置他们的关系。 */ - this.bossGroup = new NioEventLoopGroup(); - this.workerGroup = new NioEventLoopGroup(); + this.bossGroup = new NioEventLoopGroup(0,ThreadPoolUtil.getBossExecutor()); + this.workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(),ThreadPoolUtil.getWorkExecutor()); try { @@ -135,6 +135,7 @@ public void initChannel(SocketChannel ch) throws Exception { log.info("server start ok on:"+port); + /** * 这里会一直等待,直到socket被关闭 */ diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index 1644fda7..ae0695c7 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -83,6 +83,8 @@ public void operationComplete(ChannelFuture future) throws Exception { future.cancel(true); future.channel().close(); } + + log.error("for test"); } catch (Exception e) { e.printStackTrace(); diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java index cbb7a63d..17312541 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java @@ -16,7 +16,16 @@ public void testStop() throws Exception { @Test public void testStart() throws Exception { - ConnectionServer server = new ConnectionServer(3000); + final ConnectionServer server = new ConnectionServer(3000); server.start(); + + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + try { + server.stop(); + } catch (Exception e) { + } + } + }); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java index fb8ab89d..89b06d89 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java @@ -26,6 +26,8 @@ public class ThreadPoolManager { + private static final Logger log = LoggerFactory.getLogger(ThreadPoolManager.class); + private static final long keepAliveTime = 300L; private final RejectedExecutionHandler handler = new IgnoreRunsPolicy(); @@ -131,6 +133,44 @@ public void allocThreadpool(final String serviceUniqueName, int corePoolSize, in } } + /** + * 不存在,则创建 + * @param serviceUniqueName + * @param corePoolSize + * @param maximumPoolSize + * @return + * @throws Exception + */ + public Executor getThreadExecutor(String serviceUniqueName,int corePoolSize, int maximumPoolSize) { + if (!poolCache.isEmpty()) { + ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); + if (executor != null) { + return executor; + }else{ + try{ + allocThreadpool(serviceUniqueName, corePoolSize, maximumPoolSize); + }catch(Exception e){ + log.error("allocThreadPool exception",e); + } + } + executor = poolCache.get(serviceUniqueName); + if(executor!=null){ + return executor; + } + }else{ + try{ + allocThreadpool(serviceUniqueName, corePoolSize, maximumPoolSize); + }catch(Exception e){ + log.error("allocThreadPool exception",e); + } + ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); + if (executor != null) { + return executor; + } + } + return defaultPoolExecutor; + } + public Executor getThreadExecutor(String serviceUniqueName) { if (!poolCache.isEmpty()) { ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java index 1c3ebc49..a0648ace 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.core.thread; +import java.util.concurrent.Executor; + import com.shinemo.mpush.api.Constants; @@ -7,9 +9,21 @@ public class ThreadPoolUtil { private static final ThreadPoolManager threadPoolManager = new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, Constants.THREAD_QUEUE_SIZE); + + private static Executor bossExecutor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor(ThreadNameSpace.NETTY_BOSS,Constants.MIN_BOSS_POOL_SIZE,Constants.MAX_BOSS_POLL_SIZE); + private static Executor workExecutor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor(ThreadNameSpace.NETTY_WORKER,Constants.MIN_WORK_POOL_SIZE,Constants.MAX_WORK_POOL_SIZE); public static ThreadPoolManager getThreadPoolManager() { return threadPoolManager; } - + + public static Executor getBossExecutor(){ + return bossExecutor; + } + + public static Executor getWorkExecutor(){ + return workExecutor; + } + + } From 3c96abf661ed67b3f0d6d5b6907bd82005bfcda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 25 Dec 2015 10:47:15 +0800 Subject: [PATCH 020/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0sharable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/handler/ConnectionHandler.java | 14 +---- .../shinemo/mpush/connection/ClientTest.java | 58 +++++++++++-------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 78fd7c26..3693b0d7 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -3,37 +3,27 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.MessageReceiver; -import com.shinemo.mpush.core.NettyConnection; -import com.shinemo.mpush.core.thread.ThreadNameSpace; -import com.shinemo.mpush.core.thread.ThreadPoolUtil; - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; -import java.net.SocketAddress; -import java.util.concurrent.Executor; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/19. */ +@ChannelHandler.Sharable public class ConnectionHandler extends ChannelHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class); private MessageReceiver receiver; - - public ConnectionHandler(MessageReceiver receiver) { this.receiver = receiver; } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index ae0695c7..c04a021f 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -13,7 +13,6 @@ import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.core.message.HeartBeatMessage; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; @@ -57,28 +56,7 @@ public void initChannel(SocketChannel ch) throws Exception { if (future.awaitUninterruptibly(4000) && future.isSuccess()) { final Channel channel = future.channel(); - NettySharedHolder.timer.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - final Packet packet = buildHeartBeat(); - ChannelFuture channelFuture = channel.writeAndFlush(packet); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if(!future.isSuccess()){ - if(!channel.isActive()){ - log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active"); - ConnectionManager.INSTANCE.remove(channel); - } - log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet); - }else{ - log.warn("client send msg success:"+channel.remoteAddress().toString()+","+packet); - } - } - }); - } - }, Constants.TIME_DELAY, TimeUnit.SECONDS); + startHeartBeat(channel); } else { future.cancel(true); future.channel().close(); @@ -93,7 +71,36 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - + + public void startHeartBeat(final Channel channel){ + NettySharedHolder.timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + try{ + final Packet packet = buildHeartBeat(); + ChannelFuture channelFuture = channel.writeAndFlush(packet); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if(!future.isSuccess()){ + if(!channel.isActive()){ + log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active"); + ConnectionManager.INSTANCE.remove(channel); + } + log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet); + }else{ + log.warn("client send msg success:"+channel.remoteAddress().toString()+","+packet); + } + } + }); + }finally{ + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + } + }, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + private static Packet buildHeartBeat() { Packet packet = new Packet(); packet.command = Command.Heartbeat.cmd; @@ -102,4 +109,7 @@ private static Packet buildHeartBeat() { packet.msgId = 1; return packet; } + + + } From 8bbae9c2bd45ec2c639239cc7f220ae422873b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Dec 2015 07:31:05 +0000 Subject: [PATCH 021/890] add hand shake --- .../com/shinemo/mpush/api/Connection.java | 19 +- .../com/shinemo/mpush/api/ConnectionInfo.java | 12 +- .../java/com/shinemo/mpush/api/Constants.java | 10 +- .../com/shinemo/mpush/api/RouterInfo.java | 24 +- .../java/com/shinemo/mpush/api/Server.java | 14 ++ .../netty/handler/ConnectionHandler.java | 47 ++-- .../netty/server/ConnectionServer.java | 67 +++--- .../mpush/connection/ClientHandler.java | 1 + mpush-core/pom.xml | 5 +- .../shinemo/mpush/core/NettyConnection.java | 5 + .../core/handler/BaseMessageHandler.java | 2 +- .../mpush/core/handler/HandShakeHandler.java | 54 +++++ .../mpush/core/message/HandShakeMessage.java | 15 ++ .../core/security/CredentialManager.java | 17 ++ .../mpush/core/security/ReusableToken.java | 15 ++ .../core/security/ReusableTokenManager.java | 52 +++++ mpush-tools/pom.xml | 15 +- .../com/shinemo/mpush/tools/Constants.java | 11 + .../java/com/shinemo/mpush/tools/IOUtils.java | 23 ++ .../java/com/shinemo/mpush/tools/Jsons.java | 78 +++++++ .../com/shinemo/mpush/tools/MPushUtil.java | 23 ++ .../java/com/shinemo/mpush/tools/Pair.java | 26 +++ .../java/com/shinemo/mpush/tools/Strings.java | 19 ++ .../mpush/tools/crypto/CryptoUtils.java | 82 +++++++ .../shinemo/mpush/tools/crypto/DESUtils.java | 53 +++++ .../shinemo/mpush/tools/crypto/MD5Utils.java | 103 ++++++++ .../shinemo/mpush/tools/crypto/RSAUtils.java | 221 ++++++++++++++++++ pom.xml | 11 +- 28 files changed, 935 insertions(+), 89 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Server.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index df6ed642..1d77392d 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -9,6 +9,8 @@ */ public interface Connection { + void setConnectionInfo(ConnectionInfo info); + String getId(); void send(Packet packet); @@ -16,17 +18,18 @@ public interface Connection { boolean isClosed(); boolean isOpen(); - + int getHbTimes(); - + void close(); - boolean isConnected(); + boolean isConnected(); + + boolean isEnable(); + + void init(Channel channel); + + String remoteIp(); - boolean isEnable(); - void init(Channel channel); - - String remoteIp(); - } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java index 95f1c532..23a9d411 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java @@ -4,14 +4,16 @@ * Created by ohun on 2015/12/22. */ public class ConnectionInfo { - public final String os; - public final String clientVer; + public final String osName; + public final String osVersion; + public final String clientVersion; public final String deviceId; public final String desKey; - public ConnectionInfo(String os, String clientVer, String deviceId, String desKey) { - this.os = os; - this.clientVer = clientVer; + public ConnectionInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) { + this.osName = osName; + this.osVersion = osVersion; + this.clientVersion = clientVersion; this.deviceId = deviceId; this.desKey = desKey; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 42736cc5..465fd721 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -7,21 +7,23 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); + byte[] EMPTY_BYTES = new byte[0]; int MAX_PACKET_SIZE = 1024; int HEADER_LEN = 13; byte MAGIC_NUM1 = (byte) 33; byte MAGIC_NUM2 = (byte) 99; long TIME_DELAY = 58L; - + String JVM_LOG_PATH = "/opt/"; - + int THREAD_QUEUE_SIZE = 10000; int MIN_POOL_SIZE = 50; int MAX_POOL_SIZE = 500; - + int MIN_BOSS_POOL_SIZE = 10; int MAX_BOSS_POLL_SIZE = 50; - + int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; + int HEARTBEAT_TIME = 60 * 2 * 1000;//2min } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java index e4d7ba0e..49040fd8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java @@ -4,20 +4,20 @@ * Created by ohun on 2015/12/23. */ public class RouterInfo { - private String ip; - private String os; + private String serverIp; + private String osName; private String clientVer; - public RouterInfo(String ip) { - this.ip = ip; + public RouterInfo(String serverIp) { + this.serverIp = serverIp; } - public String getOs() { - return os; + public String getOsName() { + return osName; } - public void setOs(String os) { - this.os = os; + public void setOsName(String osName) { + this.osName = osName; } public String getClientVer() { @@ -28,11 +28,11 @@ public void setClientVer(String clientVer) { this.clientVer = clientVer; } - public String getIp() { - return ip; + public String getServerIp() { + return serverIp; } - public void setIp(String ip) { - this.ip = ip; + public void setServerIp(String serverIp) { + this.serverIp = serverIp; } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java new file mode 100644 index 00000000..99719ad2 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/24. + */ +public interface Server { + void init(); + + void start(); + + void stop(); + + boolean isRunning(); +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 3693b0d7..9345b983 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -6,6 +6,7 @@ import com.shinemo.mpush.api.Request; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.MessageReceiver; +import com.shinemo.mpush.core.NettyConnection; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -19,52 +20,40 @@ */ @ChannelHandler.Sharable public class ConnectionHandler extends ChannelHandlerAdapter { - - private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class); - - private MessageReceiver receiver; - + + private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class); + + private final MessageReceiver receiver; + public ConnectionHandler(MessageReceiver receiver) { - this.receiver = receiver; - } + this.receiver = receiver; + } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - log.warn(ctx.channel().remoteAddress()+", channelRead"); - Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); + log.warn(ctx.channel().remoteAddress() + ", channelRead"); + Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); + receiver.onMessage(new Request((Packet) msg, connection)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ConnectionManager.INSTANCE.remove(ctx.channel()); - log.warn("",ctx.channel().remoteAddress()+", exceptionCaught"); + log.warn(ctx.channel().remoteAddress() + ", exceptionCaught"); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress()+", channelActive"); - super.channelActive(ctx); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress()+", channelInactive"); - super.channelInactive(ctx); - } - - @Override - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - log.warn(ctx.channel().remoteAddress()+", disconnect"); - super.disconnect(ctx, promise); - ConnectionManager.INSTANCE.remove(ctx.channel()); + log.warn(ctx.channel().remoteAddress() + ", channelActive"); + Connection connection = new NettyConnection(); + connection.init(ctx.channel()); + ConnectionManager.INSTANCE.add(connection); } @Override - public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - log.warn(ctx.channel().remoteAddress()+", close"); - super.close(ctx, promise); + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelInactive"); ConnectionManager.INSTANCE.remove(ctx.channel()); } - } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index a24449f4..a783c50d 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.api.Server; import com.shinemo.mpush.connection.netty.NettySharedHolder; import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; @@ -20,18 +21,19 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.atomic.AtomicBoolean; /** * Created by ohun on 2015/12/22. */ -public class ConnectionServer { - - private static final Logger log = LoggerFactory.getLogger(ConnectionServer.class); - +public class ConnectionServer implements Server { + + private static final Logger log = LoggerFactory.getLogger(ConnectionServer.class); + private final AtomicBoolean startFlag = new AtomicBoolean(false); - - - private final int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; @@ -40,19 +42,30 @@ public ConnectionServer(int port) { this.port = port; } + @Override + public void init() { + + } + + @Override + public boolean isRunning() { + return startFlag.get(); + } + + @Override public void stop() { - log.info("netty server stop now"); + log.info("netty server stop now"); this.startFlag.set(false); if (workerGroup != null) workerGroup.shutdownGracefully(); if (bossGroup != null) bossGroup.shutdownGracefully(); } + @Override public void start() { - if (!startFlag.compareAndSet(false, true)) { return; } - + /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 @@ -64,8 +77,8 @@ public void start() { * 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现, * 并且可以通过构造函数来配置他们的关系。 */ - this.bossGroup = new NioEventLoopGroup(0,ThreadPoolUtil.getBossExecutor()); - this.workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(),ThreadPoolUtil.getWorkExecutor()); + this.bossGroup = new NioEventLoopGroup(0, ThreadPoolUtil.getBossExecutor()); + this.workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), ThreadPoolUtil.getWorkExecutor()); try { @@ -86,10 +99,8 @@ public void start() { * 这里告诉Channel如何获取新的连接. */ b.channel(NioServerSocketChannel.class); - - MessageReceiver receiver = new MessageReceiver(); - - final ConnectionHandler connectionHandler = new ConnectionHandler(receiver); + + final ConnectionHandler connectionHandler = new ConnectionHandler(new MessageReceiver()); /*** * 这里的事件处理类经常会被用来处理一个最近的已经接收的Channel。 @@ -97,7 +108,7 @@ public void start() { * 他的目的是帮助使用者配置一个新的Channel。 * 也许你想通过增加一些处理类比如NettyServerHandler来配置一个新的Channel * 或者其对应的ChannelPipeline来实现你的网络程序。 - * 当你的程序变的复杂时,可能你会增加更多的处理类到pipline上, + * 当你的程序变的复杂时,可能你会增加更多的处理类到pipeline上, * 然后提取这些匿名类到最顶层的类上。 */ b.childHandler(new ChannelInitializer() { // (4) @@ -116,33 +127,35 @@ public void initChannel(SocketChannel ch) throws Exception { * 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。 */ b.option(ChannelOption.SO_BACKLOG, 1024); - + /*** * option()是提供给NioServerSocketChannel用来接收进来的连接。 * childOption()是提供给由父管道ServerChannel接收到的连接, * 在这个例子中也是NioServerSocketChannel。 */ b.childOption(ChannelOption.SO_KEEPALIVE, true); - - b.option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); - b.childOption(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); - + + b.option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); + b.childOption(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); + /*** * 绑定端口并启动去接收进来的连接 */ ChannelFuture f = b.bind(port).sync(); - log.info("server start ok on:"+port); - - + log.info("server start ok on:" + port); + + /** * 这里会一直等待,直到socket被关闭 */ f.channel().closeFuture().sync(); - + + log.info("server start ok on:" + port); + } catch (Exception e) { - log.error("server start exception",e); + log.error("server start exception", e); /*** * 优雅关闭 */ diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java index cac26346..b5c8be47 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -32,6 +32,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelInactive"); } + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index eae3c30f..72c1226c 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -67,7 +67,10 @@ com.google.guava guava - + + com.shinemo.mpush + mpush-tools + diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 7749cc4b..bb625037 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -29,6 +29,11 @@ public void init(Channel channel) { this.channel = channel; } + @Override + public void setConnectionInfo(ConnectionInfo info) { + this.info = info; + } + @Override public String getId() { return channel.id().asLongText(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java index 3cca9cb1..59459d6a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java @@ -16,5 +16,5 @@ public void handle(Request request) { public abstract T decodeBody(Packet packet); - public abstract void handle(T t, Request request); + public abstract void handle(T body, Request request); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java new file mode 100644 index 00000000..752f61ee --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -0,0 +1,54 @@ +package com.shinemo.mpush.core.handler; + +import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.message.HandShakeMessage; +import com.shinemo.mpush.core.security.CredentialManager; +import com.shinemo.mpush.core.security.ReusableToken; +import com.shinemo.mpush.core.security.ReusableTokenManager; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.crypto.CryptoUtils; +import com.shinemo.mpush.tools.crypto.DESUtils; +import com.shinemo.mpush.tools.crypto.RSAUtils; +import org.apache.commons.lang3.RandomStringUtils; + +import java.io.Serializable; +import java.security.interfaces.RSAPrivateKey; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by ohun on 2015/12/24. + */ +public class HandShakeHandler extends BaseMessageHandler { + + @Override + public HandShakeMessage decodeBody(Packet packet) { + RSAPrivateKey privateKey = CredentialManager.INSTANCE.getPrivateKey(); + byte[] unwarp = RSAUtils.decryptByPrivateKey(packet.body, privateKey); + return new HandShakeMessage(); + } + + @Override + public void handle(HandShakeMessage body, Request request) { + String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); + String clientKey = CryptoUtils.fill2Length(body.clientKey, CryptoUtils.DES_KEY_SIZE); + String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥 + ConnectionInfo info = new ConnectionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); + request.getConnection().setConnectionInfo(info); + ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info); + ReusableTokenManager.INSTANCE.saveToken(token); + Map resp = new HashMap(); + resp.put("serverKey", serverKey); + resp.put("serverHost", MPushUtil.getLocalIp()); + resp.put("serverTime", System.currentTimeMillis()); + resp.put("heartbeat", Constants.HEARTBEAT_TIME); + resp.put("tokenId", token.tokenId); + resp.put("tokenExpire", token.expireTime); + byte[] responseData = DESUtils.decryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), desKey); + request.getResponse().send(responseData); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java new file mode 100644 index 00000000..d47b3245 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Message; + +/** + * Created by ohun on 2015/12/24. + */ +public class HandShakeMessage implements Message { + public String deviceId; + public String osName; + public String osVersion; + public String clientVersion; + public String clientKey; + public long timestamp; +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java new file mode 100644 index 00000000..b52c92dd --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.core.security; + +import com.shinemo.mpush.tools.crypto.RSAUtils; + +import java.security.interfaces.RSAPrivateKey; + +/** + * Created by ohun on 2015/12/24. + */ +public class CredentialManager { + public static final CredentialManager INSTANCE = new CredentialManager(); + + public RSAPrivateKey getPrivateKey() { + return RSAUtils.getPrivateKey("", ""); + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java new file mode 100644 index 00000000..0f20ca92 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.core.security; + +/** + * Created by ohun on 2015/12/25. + */ +public class ReusableToken { + public transient String tokenId; + public String deviceId; + public String osName; + public String osVersion; + public String clientVersion; + public String desKey; + public long expireTime; + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java new file mode 100644 index 00000000..c16d5828 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java @@ -0,0 +1,52 @@ +package com.shinemo.mpush.core.security; + +import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.tools.crypto.MD5Utils; + +/** + * Created by ohun on 2015/12/25. + */ +public class ReusableTokenManager { + public static final ReusableTokenManager INSTANCE = new ReusableTokenManager(); + private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; + + + public boolean saveToken(ReusableToken token) { + return true; + } + + public ReusableToken getToken() { + return new ReusableToken(); + } + + public ReusableToken genToken(ConnectionInfo info) { + /** + * 先生成key,需要保证半个周期内同一个设备生成的key是相同的 + */ + long partition = System.currentTimeMillis() / (EXPIRE_TIME / 2);//把当前时间按照半个周期划分出一个当前所属于的区域 + StringBuilder sb = new StringBuilder(); + sb.append(info.deviceId).append('_').append(partition).append("_R_S_T"); + ReusableToken v = new ReusableToken(); + v.tokenId = MD5Utils.encrypt(sb.toString()); + v.clientVersion = info.clientVersion; + v.deviceId = info.deviceId; + v.osName = info.osName; + v.osVersion = info.osVersion; + v.desKey = info.desKey; + /** + * 计算失效时间 + */ + long nowTime = System.currentTimeMillis(); + long willExpire = (nowTime / EXPIRE_TIME + 1) * EXPIRE_TIME;//预计的到下个周期的失效时间 + + //有可能到绝对周期的时间已经非常短了,如果已经非常短的话,再补充一个周期 + int exp; + if (willExpire - nowTime > EXPIRE_TIME / 2) { + exp = (int) (willExpire - nowTime); + } else { + exp = (int) (willExpire - nowTime) + EXPIRE_TIME; + } + v.expireTime = System.currentTimeMillis() + exp;//存储绝对过期时间 + return v; + } +} diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 6acb8537..a3302831 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -12,5 +12,18 @@ mpush-tools 1.0-SNAPSHOT jar - + + + com.google.code.gson + gson + + + com.google.guava + guava + + + org.apache.commons + commons-lang3 + + \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java new file mode 100644 index 00000000..6bc40436 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -0,0 +1,11 @@ +package com.shinemo.mpush.tools; + +import java.nio.charset.Charset; + +/** + * Created by ohun on 2015/12/25. + */ +public interface Constants { + Charset UTF_8 = Charset.forName("UTF-8"); + byte[] EMPTY_BYTES = new byte[0]; +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java new file mode 100644 index 00000000..9c337898 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java @@ -0,0 +1,23 @@ +package com.shinemo.mpush.tools; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Closeable; + +/** + * Created by ohun on 2015/12/25. + */ +public final class IOUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(IOUtils.class); + + public static void close(Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (Exception e) { + LOGGER.error("close closeable ex", e); + } + } + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java new file mode 100644 index 00000000..47e7c89f --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -0,0 +1,78 @@ +package com.shinemo.mpush.tools; + + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Type; +import java.util.Iterator; +import java.util.Map; + +/** + * Created by xiaoxu.yxx on 15/8/7. + */ +public final class Jsons { + private static final Logger LOGGER = LoggerFactory.getLogger(Jsons.class); + public static final Gson GSON = new GsonBuilder().create(); + + + public static String toJson(Object bean) { + try { + return GSON.toJson(bean); + } catch (Exception e) { + LOGGER.error("Jsons.toJson ex, bean=" + bean, e); + } + return null; + } + + public static T fromJson(String json, Class clazz) { + try { + return GSON.fromJson(json, clazz); + } catch (Exception e) { + LOGGER.error("Jsons.fromJson ex, json=" + json + ", clazz=" + clazz, e); + } + return null; + } + + public static T fromJson(String json, Type type) { + try { + return GSON.fromJson(json, type); + } catch (Exception e) { + LOGGER.error("Jsons.fromJson ex, json=" + json + ", type=" + type, e); + } + return null; + } + + public static boolean mayJson(String json) { + if (Strings.isBlank(json)) return false; + if (json.charAt(0) == '{' && json.charAt(json.length() - 1) == '}') return true; + if (json.charAt(0) == '[' && json.charAt(json.length() - 1) == ']') return true; + return false; + } + + public static String toJson(Map map) { + if (map == null || map.isEmpty()) return "{}"; + StringBuilder sb = new StringBuilder(64 * map.size()); + sb.append('{'); + Iterator> it = map.entrySet().iterator(); + if (it.hasNext()) { + append(it.next(), sb); + } + while (it.hasNext()) { + sb.append(','); + append(it.next(), sb); + } + sb.append('}'); + return sb.toString(); + } + + private static void append(Map.Entry entry, StringBuilder sb) { + String key = entry.getKey(), value = entry.getValue(); + if (value == null) value = Strings.EMPTY; + sb.append('"').append(key).append('"'); + sb.append(':'); + sb.append('"').append(value).append('"'); + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java new file mode 100644 index 00000000..63a715ad --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -0,0 +1,23 @@ +package com.shinemo.mpush.tools; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * Created by ohun on 2015/12/25. + */ +public final class MPushUtil { + private static String LOCAL_IP; + + public static String getLocalIp() { + if (LOCAL_IP == null) { + try { + LOCAL_IP = InetAddress.getLocalHost().getHostAddress(); + } catch (Exception e) { + LOCAL_IP = "127.0.0.1"; + } + } + return LOCAL_IP; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java new file mode 100644 index 00000000..32c7f143 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.tools; + +/** + * Created by ohun on 2015/12/24. + */ +public class Pair { + public final K key; + public final V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K first() { + return key; + } + + public V second() { + return value; + } + + public static Pair of(K k, V v) { + return new Pair<>(k, v); + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java index 2ed2aa1b..335160a3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java @@ -4,4 +4,23 @@ * Created by ohun on 2015/12/23. */ public class Strings { + public static final String EMPTY = ""; + + public static boolean isBlank(CharSequence text) { + if (text == null || text.length() == 0) return true; + for (int i = 0, L = text.length(); i < L; i++) { + if (!Character.isWhitespace(text.charAt(i))) return false; + } + return true; + } + + public static String trimAll(CharSequence s) { + if (s == null || s.length() == 0) return Strings.EMPTY; + StringBuilder sb = new StringBuilder(s.length()); + for (int i = 0, L = s.length(); i < L; i++) { + char c = s.charAt(i); + if (c != ' ') sb.append(c); + } + return sb.toString(); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java new file mode 100644 index 00000000..f02df257 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java @@ -0,0 +1,82 @@ +package com.shinemo.mpush.tools.crypto; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; + +/** + * Created by ohun on 2015/12/24. + */ +public class CryptoUtils { + public static final int[] DES_KEY_OFFSET_ARRAY = new int[]{12, 8, 4, 52, 33, 24, 12, 43, 5, 86, 79, 44, 21, 1, 66, 88}; + public static final int DES_KEY_SIZE = 8; + + public byte[] decode(byte[] data, PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher c2 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + c2.init(Cipher.DECRYPT_MODE, privateKey); + return c2.doFinal(data); + } + + public byte[] encode(byte[] data, PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher c2 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + c2.init(Cipher.DECRYPT_MODE, privateKey); + return c2.doFinal(data); + } + + + + public static String fill2Length(String str, int length) { + if (str.length() == length) { + return str; + } else if (str.length() < length) { + char[] cs = str.toCharArray(); + char[] fcs = new char[length]; + int less = length - cs.length; + System.arraycopy(cs, 0, fcs, less, length); + for (int i = less; i >= 0; i--) { + fcs[i] = '*'; + } + return new String(fcs); + } else { + char[] cs = str.toCharArray(); + char[] fcs = new char[length]; + System.arraycopy(cs, 0, fcs, 0, length); + return new String(fcs); + } + } + + + public static String mixString(String a, String b) { + char[] charsA = fill2Length(a, DES_KEY_SIZE).toCharArray(); + char[] charsB = fill2Length(b, DES_KEY_SIZE).toCharArray(); + char[] charsC = new char[DES_KEY_SIZE]; + for (int i = 0; i < DES_KEY_SIZE; i++) { + char charA = charsA[i]; + char charB = charsB[i]; + int sum = charA + charB; + int offset = DES_KEY_OFFSET_ARRAY[sum % DES_KEY_OFFSET_ARRAY.length]; + boolean chooseA = (sum % 2 == 0); + boolean upOrDown = (charA % 2 == 0); + char charC; + if (chooseA) { + charC = charA; + } else { + charC = charB; + } + if (upOrDown) { + offset = -offset; + } + int result = charC + offset; + if (result > 126 || result < 33) { + result = 33 + Math.abs(result % (126 - 33)); + } + charC = (char) result; + charsC[i] = charC; + } + return new String(charsC); + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java new file mode 100644 index 00000000..fcf97d5c --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java @@ -0,0 +1,53 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.security.NoSuchAlgorithmException; + +/** + * Created by ohun on 2015/12/25. + */ +public final class DESUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(DESUtils.class); + private static byte[] iv = {8, 7, 6, 5, 5, 6, 7, 8}; + + public static byte[] encryptDES(byte[] encryptBytes, String encryptKey) { + return encryptDES(encryptBytes, 0, encryptBytes.length, encryptKey); + } + + public static byte[] encryptDES(byte[] encryptBytes, int offset, int length, String encryptKey) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), "DES"); + try { + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); + return cipher.doFinal(encryptBytes, offset, length); + } catch (Exception e) { + LOGGER.error("encryptDES ex, decryptKey=" + encryptKey, e); + } + return Constants.EMPTY_BYTES; + } + + public static byte[] decryptDES(byte[] byteMi, String decryptKey) { + return decryptDES(byteMi, 0, byteMi.length, decryptKey); + } + + public static byte[] decryptDES(byte[] byteMi, int offset, int length, String decryptKey) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(Constants.UTF_8), "DES"); + try { + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); + return cipher.doFinal(byteMi, offset, length); + } catch (Exception e) { + LOGGER.error("decryptDES ex, decryptKey=" + decryptKey, e); + } + return Constants.EMPTY_BYTES; + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java new file mode 100644 index 00000000..8a68fa2a --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java @@ -0,0 +1,103 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Constants; +import com.shinemo.mpush.tools.IOUtils; +import com.shinemo.mpush.tools.Strings; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.security.MessageDigest; + +/** + * Created by ohun on 2015/12/25. + */ +public class MD5Utils { + public static String encrypt(File file) { + InputStream in = null; + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + in = new FileInputStream(file); + byte[] buffer = new byte[10240];//10k + int readLen; + while ((readLen = in.read(buffer)) != -1) { + digest.update(buffer, 0, readLen); + } + return toHex(digest.digest()); + } catch (Exception e) { + return Strings.EMPTY; + } finally { + IOUtils.close(in); + } + } + + + public static String encrypt(String text) { + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + digest.update(text.getBytes(Constants.UTF_8)); + return toHex(digest.digest()); + } catch (Exception e) { + return Strings.EMPTY; + } + } + + public static String encrypt(byte[] bytes) { + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + digest.update(bytes); + return toHex(digest.digest()); + } catch (Exception e) { + return Strings.EMPTY; + } + } + + private static String toHex(byte[] bytes) { + StringBuffer buffer = new StringBuffer(bytes.length * 2); + + for (int i = 0; i < bytes.length; ++i) { + buffer.append(Character.forDigit((bytes[i] & 240) >> 4, 16)); + buffer.append(Character.forDigit(bytes[i] & 15, 16)); + } + + return buffer.toString(); + } + + + /** + * HmacSHA1 加密 + * + * @param data + * @param encryptKey + * @return + */ + public static String hmacSha1(String data, String encryptKey) { + final String HMAC_SHA1 = "HmacSHA1"; + SecretKeySpec signingKey = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), HMAC_SHA1); + try { + Mac mac = Mac.getInstance(HMAC_SHA1); + mac.init(signingKey); + mac.update(data.getBytes(Constants.UTF_8)); + return toHex(mac.doFinal()); + } catch (Exception e) { + return Strings.EMPTY; + } + } + + /** + * HmacSHA1 加密 + * + * @param data + * @return + */ + public static String sha1(String data) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + return toHex(digest.digest(data.getBytes(Constants.UTF_8))); + } catch (Exception e) { + return Strings.EMPTY; + } + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java new file mode 100644 index 00000000..c8380bc7 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -0,0 +1,221 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Pair; + +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import java.math.BigInteger; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.RSAPrivateKeySpec; +import java.security.spec.RSAPublicKeySpec; + +public class RSAUtils { + + /** + * 生成公钥和私钥 + * + * @throws NoSuchAlgorithmException + */ + public static Pair getKeys() throws NoSuchAlgorithmException { + KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); + keyPairGen.initialize(1024); + KeyPair keyPair = keyPairGen.generateKeyPair(); + RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); + RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); + return Pair.of(publicKey, privateKey); + } + + /** + * 使用模和指数生成RSA公钥 + * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA + * /None/NoPadding】 + * + * @param modulus 模 + * @param exponent 指数 + * @return + */ + public static RSAPublicKey getPublicKey(String modulus, String exponent) { + try { + BigInteger b1 = new BigInteger(modulus); + BigInteger b2 = new BigInteger(exponent); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); + return (RSAPublicKey) keyFactory.generatePublic(keySpec); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 使用模和指数生成RSA私钥 + * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA + * /None/NoPadding】 + * + * @param modulus 模 + * @param exponent 指数 + * @return + */ + public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { + try { + BigInteger b1 = new BigInteger(modulus); + BigInteger b2 = new BigInteger(exponent); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2); + return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 公钥加密 + * + * @param data + * @param publicKey + * @return + * @throws Exception + */ + public static String encryptByPublicKey(String data, RSAPublicKey publicKey) + throws Exception { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + // 模长 + int key_len = publicKey.getModulus().bitLength() / 8; + // 加密数据长度 <= 模长-11 + String[] datas = splitString(data, key_len - 11); + String mi = ""; + //如果明文长度大于模长-11则要分组加密 + for (String s : datas) { + mi += bcd2Str(cipher.doFinal(s.getBytes())); + } + return mi; + } + + /** + * 私钥解密 + * + * @param data + * @param privateKey + * @return + * @throws Exception + */ + public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) { + try { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, privateKey); + //模长 + int key_len = privateKey.getModulus().bitLength() / 8; + // byte[] bcd = ASCII_To_BCD(data, data.length); + //如果密文长度大于模长则要分组解密 + byte[][] arrays = splitArray(data, key_len); + byte[] result = new byte[0]; + for (byte[] arr : arrays) { + byte[] buffer = cipher.doFinal(arr); + byte[] tmp = new byte[result.length + buffer.length]; + System.arraycopy(result, 0, tmp, 0, result.length); + System.arraycopy(buffer, 0, tmp, result.length, tmp.length); + result = tmp; + } + return result; + } catch (Exception e) { + } + return new byte[0]; + } + + /** + * ASCII码转BCD码 + */ + public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) { + byte[] bcd = new byte[asc_len / 2]; + int j = 0; + for (int i = 0; i < (asc_len + 1) / 2; i++) { + bcd[i] = asc_to_bcd(ascii[j++]); + bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4)); + } + return bcd; + } + + public static byte asc_to_bcd(byte asc) { + byte bcd; + + if ((asc >= '0') && (asc <= '9')) + bcd = (byte) (asc - '0'); + else if ((asc >= 'A') && (asc <= 'F')) + bcd = (byte) (asc - 'A' + 10); + else if ((asc >= 'a') && (asc <= 'f')) + bcd = (byte) (asc - 'a' + 10); + else + bcd = (byte) (asc - 48); + return bcd; + } + + /** + * BCD转字符串 + */ + public static String bcd2Str(byte[] bytes) { + char temp[] = new char[bytes.length * 2], val; + + for (int i = 0; i < bytes.length; i++) { + val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f); + temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); + + val = (char) (bytes[i] & 0x0f); + temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); + } + return new String(temp); + } + + /** + * 拆分字符串 + */ + public static String[] splitString(String string, int len) { + int x = string.length() / len; + int y = string.length() % len; + int z = 0; + if (y != 0) { + z = 1; + } + String[] strings = new String[x + z]; + String str = ""; + for (int i = 0; i < x + z; i++) { + if (i == x + z - 1 && y != 0) { + str = string.substring(i * len, i * len + y); + } else { + str = string.substring(i * len, i * len + len); + } + strings[i] = str; + } + return strings; + } + + /** + * 拆分数组 + */ + public static byte[][] splitArray(byte[] data, int len) { + int x = data.length / len; + int y = data.length % len; + int z = 0; + if (y != 0) { + z = 1; + } + byte[][] arrays = new byte[x + z][]; + byte[] arr; + for (int i = 0; i < x + z; i++) { + arr = new byte[len]; + if (i == x + z - 1 && y != 0) { + System.arraycopy(data, i * len, arr, 0, y); + } else { + System.arraycopy(data, i * len, arr, 0, len); + } + arrays[i] = arr; + } + return arrays; + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 683d615a..3f8cc585 100644 --- a/pom.xml +++ b/pom.xml @@ -147,7 +147,16 @@ spring-data-redis 1.6.1.RELEASE - + + org.apache.commons + commons-lang3 + 3.4 + + + com.google.code.gson + gson + 2.5 + From d350455a8c9460291383472e8641cf1f8fbc3c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Dec 2015 08:01:03 +0000 Subject: [PATCH 022/890] add hand shake --- .../netty/{encoder => codec}/PacketDecoder.java | 2 +- .../netty/{encoder => codec}/PacketEncoder.java | 2 +- .../mpush/connection/netty/server/ConnectionServer.java | 8 ++------ .../java/com/shinemo/mpush/connection/ClientTest.java | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) rename mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/{encoder => codec}/PacketDecoder.java (97%) rename mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/{encoder => codec}/PacketEncoder.java (94%) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java similarity index 97% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java rename to mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java index 43cc5afa..e57240fc 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketDecoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.encoder; +package com.shinemo.mpush.connection.netty.codec; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.exception.DecodeException; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketEncoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java similarity index 94% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketEncoder.java rename to mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java index ef4cfb75..194408f1 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/encoder/PacketEncoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.encoder; +package com.shinemo.mpush.connection.netty.codec; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java index a783c50d..4c522be4 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java @@ -7,8 +7,8 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.connection.netty.NettySharedHolder; -import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; -import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; +import com.shinemo.mpush.connection.netty.codec.PacketDecoder; +import com.shinemo.mpush.connection.netty.codec.PacketEncoder; import com.shinemo.mpush.connection.netty.handler.ConnectionHandler; import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.thread.ThreadPoolUtil; @@ -21,10 +21,6 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.atomic.AtomicBoolean; /** * Created by ohun on 2015/12/22. diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index c04a021f..51b2e83b 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -10,8 +10,8 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.connection.netty.NettySharedHolder; -import com.shinemo.mpush.connection.netty.encoder.PacketDecoder; -import com.shinemo.mpush.connection.netty.encoder.PacketEncoder; +import com.shinemo.mpush.connection.netty.codec.PacketDecoder; +import com.shinemo.mpush.connection.netty.codec.PacketEncoder; import com.shinemo.mpush.core.ConnectionManager; import io.netty.bootstrap.Bootstrap; From 29179b45c30dcf25b1feab7bbdf03e2636e199cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Dec 2015 08:02:47 +0000 Subject: [PATCH 023/890] add hand shake --- .../netty/task/ScanAllClientConnection.java | 67 +++++++++---------- .../mpush/connection/netty/task/ScanTask.java | 13 ++-- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java index 572821aa..5dfaa6ba 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java @@ -17,40 +17,39 @@ import com.shinemo.mpush.core.ConnectionManager; /** - * 定时全量扫描connection - * + * 定时全量扫描connection */ -public class ScanAllClientConnection implements TimerTask{ - - private static final Logger log = LoggerFactory.getLogger(ScanAllClientConnection.class); - - private final List taskList = new ArrayList(); - - public ScanAllClientConnection(final ScanTask...scanTasks) { - if(scanTasks!=null){ - for(final ScanTask task:scanTasks){ - this.taskList.add(task); - } - } - } - - @Override - public void run(Timeout timeout) throws Exception { - try{ - final long now = System.currentTimeMillis(); - List connections = ConnectionManager.INSTANCE.getConnections(); - if(connections!=null){ - for(Connection conn:connections){ - for(final ScanTask task:this.taskList){ - task.visit(now, conn); - } - } - } - }catch(Exception e){ - log.error("","exception on scan",e); - }finally{ - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - } +public class ScanAllClientConnection implements TimerTask { + + private static final Logger log = LoggerFactory.getLogger(ScanAllClientConnection.class); + + private final List taskList = new ArrayList(); + + public ScanAllClientConnection(final ScanTask... scanTasks) { + if (scanTasks != null) { + for (final ScanTask task : scanTasks) { + this.taskList.add(task); + } + } + } + + @Override + public void run(Timeout timeout) throws Exception { + try { + final long now = System.currentTimeMillis(); + List connections = ConnectionManager.INSTANCE.getConnections(); + if (connections != null) { + for (Connection conn : connections) { + for (ScanTask task : this.taskList) { + task.visit(now, conn); + } + } + } + } catch (Exception e) { + log.error("", "exception on scan", e); + } finally { + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + } } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java index 6b4db50c..e3dfa05c 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java @@ -3,12 +3,11 @@ import com.shinemo.mpush.api.Connection; public interface ScanTask { - - /** - * - * @param now 扫描触发的时间点 - * @param client 当前扫描到的连接 - */ - public void visit(long now,Connection conn); + + /** + * @param now 扫描触发的时间点 + * @param conn 当前扫描到的连接 + */ + void visit(long now, Connection conn); } From a62592286635f8a5753d51c735ae37193999c345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Dec 2015 09:50:23 +0000 Subject: [PATCH 024/890] add hand shake --- .../{ConnectionInfo.java => ClientInfo.java} | 4 +- .../com/shinemo/mpush/api/Connection.java | 4 +- .../java/com/shinemo/mpush/api/Receiver.java | 4 +- .../java/com/shinemo/mpush/api/Request.java | 31 ++-------- .../java/com/shinemo/mpush/api/Response.java | 21 ++----- .../mpush/connection/ConnectionHandler.java | 2 +- .../shinemo/mpush/core/MessageReceiver.java | 8 ++- .../shinemo/mpush/core/NettyConnection.java | 12 ++-- .../mpush/core/handler/HandShakeHandler.java | 13 +++-- .../mpush/core/message/NettyRequest.java | 42 ++++++++++++++ .../mpush/core/message/NettyResponse.java | 34 +++++++++++ .../core/security/ReusableTokenManager.java | 4 +- .../shinemo/mpush/tools/crypto/DESUtils.java | 2 - .../shinemo/mpush/tools/crypto/RSAUtils.java | 57 ++++++++++++++----- 14 files changed, 157 insertions(+), 81 deletions(-) rename mpush-api/src/main/java/com/shinemo/mpush/api/{ConnectionInfo.java => ClientInfo.java} (74%) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/ClientInfo.java similarity index 74% rename from mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/ClientInfo.java index 23a9d411..4dbcaa48 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/ConnectionInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/ClientInfo.java @@ -3,14 +3,14 @@ /** * Created by ohun on 2015/12/22. */ -public class ConnectionInfo { +public class ClientInfo { public final String osName; public final String osVersion; public final String clientVersion; public final String deviceId; public final String desKey; - public ConnectionInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) { + public ClientInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) { this.osName = osName; this.osVersion = osVersion; this.clientVersion = clientVersion; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 1d77392d..7296fd86 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -9,7 +9,9 @@ */ public interface Connection { - void setConnectionInfo(ConnectionInfo info); + void setClientInfo(ClientInfo info); + + ClientInfo getClientInfo(); String getId(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java index 083177d0..6925ba9f 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java @@ -1,8 +1,10 @@ package com.shinemo.mpush.api; +import com.shinemo.mpush.api.protocol.Packet; + /** * Created by ohun on 2015/12/22. */ public interface Receiver { - void onMessage(Request request); + void onMessage(Packet packet, Connection connection); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java index 2e8388af..f18e778f 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java @@ -6,34 +6,13 @@ /** * Created by ohun on 2015/12/22. */ -public class Request { - private final Command command; - private final Packet message; - private final Connection connection; +public interface Request { - public Request(Packet message, Connection connection) { - this.message = message; - this.connection = connection; - this.command = Command.toCMD(message.command); - } + Command getCommand(); - public Command getCommand() { - return command; - } + Packet getMessage(); - public Packet getMessage() { - return message; - } + Connection getConnection(); - public Connection getConnection() { - return connection; - } - - public Response getResponse() { - Packet packet = new Packet(); - packet.command = message.command; - packet.msgId = message.msgId; - packet.version = message.version; - return new Response(packet, connection); - } + Response getResponse(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java index 1a835cef..639a6c5f 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java @@ -1,26 +1,13 @@ package com.shinemo.mpush.api; -import com.shinemo.mpush.api.protocol.Packet; - /** * Created by ohun on 2015/12/22. */ -public class Response { - private final Packet packet; - private final Connection connection; - - public Response(Packet packet, Connection connection) { - this.packet = packet; - this.connection = connection; - } - - public void send(byte[] body) { - packet.body = body; - connection.send(packet); - } +public interface Response { + void send(byte[] body); - public void sendError(byte[] reson) { + void sendRaw(byte[] body); - } + void sendError(byte[] reason); } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java index a920a8a6..c46078ba 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java @@ -23,7 +23,7 @@ public class ConnectionHandler extends ChannelHandlerAdapter { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("channelRead msg=" + msg); logger.debug("channelRead msg=" + msg); - receiver.onMessage(new Request((Packet) msg, connection)); + receiver.onMessage((Packet) msg, connection); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java index 558701d1..c4f92feb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -1,11 +1,14 @@ package com.shinemo.mpush.core; +import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.handler.BindHandler; import com.shinemo.mpush.core.handler.HeartBeatHandler; import com.shinemo.mpush.core.handler.LoginHandler; +import com.shinemo.mpush.core.message.NettyRequest; /** * Created by ohun on 2015/12/22. @@ -16,10 +19,11 @@ public class MessageReceiver implements Receiver { public static final HeartBeatHandler HEART_HANDLER = new HeartBeatHandler(); @Override - public void onMessage(Request request) { + public void onMessage(Packet packet, Connection connection) { + Request request = new NettyRequest(packet, connection); switch (request.getCommand()) { case Heartbeat: - HEART_HANDLER.handle(request); + HEART_HANDLER.handle(request); break; case Handshake: break; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index bb625037..49ad411f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -8,7 +8,7 @@ import io.netty.channel.ChannelFutureListener; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.api.ClientInfo; import com.shinemo.mpush.api.protocol.Packet; /** @@ -18,7 +18,7 @@ public class NettyConnection implements Connection { private static final Logger log = LoggerFactory.getLogger(NettyConnection.class); - private ConnectionInfo info; + private ClientInfo info; private Channel channel; private int status = 0; @@ -30,7 +30,7 @@ public void init(Channel channel) { } @Override - public void setConnectionInfo(ConnectionInfo info) { + public void setClientInfo(ClientInfo info) { this.info = info; } @@ -75,14 +75,10 @@ public boolean isOpen() { return false; } - public ConnectionInfo getInfo() { + public ClientInfo getClientInfo() { return info; } - public void setInfo(ConnectionInfo info) { - this.info = info; - } - public Channel getChannel() { return channel; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 752f61ee..e1cf6da3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.api.ClientInfo; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.api.protocol.Packet; @@ -29,16 +29,17 @@ public class HandShakeHandler extends BaseMessageHandler { public HandShakeMessage decodeBody(Packet packet) { RSAPrivateKey privateKey = CredentialManager.INSTANCE.getPrivateKey(); byte[] unwarp = RSAUtils.decryptByPrivateKey(packet.body, privateKey); + return new HandShakeMessage(); } @Override public void handle(HandShakeMessage body, Request request) { String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); - String clientKey = CryptoUtils.fill2Length(body.clientKey, CryptoUtils.DES_KEY_SIZE); + String clientKey = body.clientKey; String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥 - ConnectionInfo info = new ConnectionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); - request.getConnection().setConnectionInfo(info); + ClientInfo info = new ClientInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); + request.getConnection().setInfo(info); ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info); ReusableTokenManager.INSTANCE.saveToken(token); Map resp = new HashMap(); @@ -48,7 +49,7 @@ public void handle(HandShakeMessage body, Request request) { resp.put("heartbeat", Constants.HEARTBEAT_TIME); resp.put("tokenId", token.tokenId); resp.put("tokenExpire", token.expireTime); - byte[] responseData = DESUtils.decryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), desKey); - request.getResponse().send(responseData); + byte[] responseData = DESUtils.decryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey); + request.getResponse().sendRaw(responseData); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java new file mode 100644 index 00000000..7a4dc4bb --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java @@ -0,0 +1,42 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.Response; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public class NettyRequest implements Request{ + private final Command command; + private final Packet message; + private final Connection connection; + + public NettyRequest(Packet message, Connection connection) { + this.message = message; + this.connection = connection; + this.command = Command.toCMD(message.command); + } + + public Command getCommand() { + return command; + } + + public Packet getMessage() { + return message; + } + + public Connection getConnection() { + return connection; + } + + public Response getResponse() { + Packet packet = new Packet(); + packet.command = message.command; + packet.msgId = message.msgId; + packet.version = message.version; + return new NettyResponse(packet, connection); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java new file mode 100644 index 00000000..fd6f796f --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java @@ -0,0 +1,34 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Response; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.crypto.DESUtils; + +/** + * Created by ohun on 2015/12/22. + */ +public class NettyResponse implements Response { + private final Packet packet; + private final Connection connection; + + public NettyResponse(Packet packet, Connection connection) { + this.packet = packet; + this.connection = connection; + } + + public void send(byte[] body) { + packet.body = DESUtils.decryptDES(body, connection.getInfo().desKey); + connection.send(packet); + } + + public void sendRaw(byte[] body) { + packet.body = body; + connection.send(packet); + } + + + public void sendError(byte[] reason) { + + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java index c16d5828..62b03fc2 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.security; -import com.shinemo.mpush.api.ConnectionInfo; +import com.shinemo.mpush.api.ClientInfo; import com.shinemo.mpush.tools.crypto.MD5Utils; /** @@ -19,7 +19,7 @@ public ReusableToken getToken() { return new ReusableToken(); } - public ReusableToken genToken(ConnectionInfo info) { + public ReusableToken genToken(ClientInfo info) { /** * 先生成key,需要保证半个周期内同一个设备生成的key是相同的 */ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java index fcf97d5c..55ff05d9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java @@ -5,10 +5,8 @@ import org.slf4j.LoggerFactory; import javax.crypto.Cipher; -import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import java.security.NoSuchAlgorithmException; /** * Created by ohun on 2015/12/25. diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index c8380bc7..330f1f41 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -1,20 +1,21 @@ package com.shinemo.mpush.tools.crypto; +import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import java.math.BigInteger; -import java.security.KeyFactory; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.NoSuchAlgorithmException; +import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; public class RSAUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class); /** * 生成公钥和私钥 @@ -47,9 +48,9 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { - e.printStackTrace(); - return null; + LOGGER.error("getPublicKey ex modulus={}, exponent={}", modulus, exponent, e); } + return null; } /** @@ -69,7 +70,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { - e.printStackTrace(); + LOGGER.error("getPrivateKey ex modulus={}, exponent={}", modulus, exponent, e); return null; } } @@ -82,20 +83,24 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { * @return * @throws Exception */ - public static String encryptByPublicKey(String data, RSAPublicKey publicKey) + public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 模长 int key_len = publicKey.getModulus().bitLength() / 8; // 加密数据长度 <= 模长-11 - String[] datas = splitString(data, key_len - 11); - String mi = ""; + byte[][] arrays = splitArray(data, key_len - 11); //如果明文长度大于模长-11则要分组加密 - for (String s : datas) { - mi += bcd2Str(cipher.doFinal(s.getBytes())); + byte[] result = new byte[0]; + for (byte[] arr : arrays) { + byte[] buffer = cipher.doFinal(arr); + byte[] tmp = new byte[result.length + buffer.length]; + System.arraycopy(result, 0, tmp, 0, result.length); + System.arraycopy(buffer, 0, tmp, result.length, tmp.length); + result = tmp; } - return mi; + return result; } /** @@ -218,4 +223,30 @@ public static byte[][] splitArray(byte[] data, int len) { } return arrays; } + + public static void main(String[] args) throws Exception { + // TODO Auto-generated method stub + Pair pair = RSAUtils.getKeys(); + //生成公钥和私钥 + RSAPublicKey publicKey = pair.key; + RSAPrivateKey privateKey = pair.value; + + //模 + String modulus = publicKey.getModulus().toString(); + //公钥指数 + String public_exponent = publicKey.getPublicExponent().toString(); + //私钥指数 + String private_exponent = privateKey.getPrivateExponent().toString(); + //明文 + byte[] ming = "123456789".getBytes(Constants.UTF_8); + //使用模和指数生成公钥和私钥 + RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); + RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); + //加密后的密文 + byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey); + System.err.println(new String(mi, Constants.UTF_8)); + //解密后的明文 + ming = RSAUtils.decryptByPrivateKey(mi, priKey); + System.err.println(new String(ming, Constants.UTF_8)); + } } \ No newline at end of file From 0bb4d35f67c0a3664e6e24e2e7e9f905c86b866a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Dec 2015 10:05:46 +0000 Subject: [PATCH 025/890] add hand shake --- .../com/shinemo/mpush/api/Connection.java | 4 +- .../api/{ClientInfo.java => SessionInfo.java} | 4 +- .../shinemo/mpush/core/NettyConnection.java | 226 +++++++++--------- .../mpush/core/handler/HandShakeHandler.java | 6 +- .../core/security/ReusableTokenManager.java | 4 +- .../shinemo/mpush/tools/crypto/RSAUtils.java | 111 ++++----- 6 files changed, 178 insertions(+), 177 deletions(-) rename mpush-api/src/main/java/com/shinemo/mpush/api/{ClientInfo.java => SessionInfo.java} (75%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 7296fd86..8ca40ed9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -9,9 +9,9 @@ */ public interface Connection { - void setClientInfo(ClientInfo info); + void setSessionInfo(SessionInfo info); - ClientInfo getClientInfo(); + SessionInfo getSessionInfo(); String getId(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/ClientInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java similarity index 75% rename from mpush-api/src/main/java/com/shinemo/mpush/api/ClientInfo.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java index 4dbcaa48..009fa407 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/ClientInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java @@ -3,14 +3,14 @@ /** * Created by ohun on 2015/12/22. */ -public class ClientInfo { +public class SessionInfo { public final String osName; public final String osVersion; public final String clientVersion; public final String deviceId; public final String desKey; - public ClientInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) { + public SessionInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) { this.osName = osName; this.osVersion = osVersion; this.clientVersion = clientVersion; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 49ad411f..ddf70437 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -8,7 +8,7 @@ import io.netty.channel.ChannelFutureListener; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.ClientInfo; +import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.protocol.Packet; /** @@ -16,116 +16,116 @@ */ public class NettyConnection implements Connection { - private static final Logger log = LoggerFactory.getLogger(NettyConnection.class); - - private ClientInfo info; - private Channel channel; - private int status = 0; - - private int hbTimes; - - @Override - public void init(Channel channel) { - this.channel = channel; - } - - @Override - public void setClientInfo(ClientInfo info) { - this.info = info; - } - - @Override - public String getId() { - return channel.id().asLongText(); - } - - @Override - public void send(final Packet packet) { - if (packet != null) { - if (channel.isWritable()) { - ChannelFuture wf = channel.writeAndFlush(packet); - wf.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if(!future.isSuccess()){ - if(!channel.isActive()){ - log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active"); - ConnectionManager.INSTANCE.remove(channel); - } - log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet); - }else{ - log.warn("send msg success:"+channel.remoteAddress().toString()+","+packet); - } - } - }); - }else{ - log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet+", channel is not writable"); - } - } - } - - @Override - public boolean isClosed() { - return false; - } - - @Override - public boolean isOpen() { - return false; - } - - public ClientInfo getClientInfo() { - return info; - } - - public Channel getChannel() { - return channel; - } - - public int increaseAndGetHbTimes(){ - return ++hbTimes; - } - - public void resetHbTimes(){ - hbTimes = 0; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - } - - - public void setChannel(Channel channel) { - this.channel = channel; - } - - @Override - public void close() { - this.channel.close(); - } - - @Override - public int getHbTimes() { - return hbTimes; - } - - @Override - public boolean isConnected(){ - return channel.isActive(); - } - - @Override - public boolean isEnable(){ - return channel.isWritable(); - } - - @Override - public String remoteIp() { - return channel.remoteAddress().toString(); - } + private static final Logger log = LoggerFactory.getLogger(NettyConnection.class); + + private SessionInfo info; + private Channel channel; + private int status = 0; + + private int hbTimes; + + @Override + public void init(Channel channel) { + this.channel = channel; + } + + @Override + public void setSessionInfo(SessionInfo info) { + this.info = info; + } + + @Override + public SessionInfo getSessionInfo() { + return info; + } + + @Override + public String getId() { + return channel.id().asLongText(); + } + + @Override + public void send(final Packet packet) { + if (packet != null) { + if (channel.isWritable()) { + ChannelFuture wf = channel.writeAndFlush(packet); + wf.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); + ConnectionManager.INSTANCE.remove(channel); + } + log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet); + } else { + log.warn("send msg success:" + channel.remoteAddress().toString() + "," + packet); + } + } + }); + } else { + log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet + ", channel is not writable"); + } + } + } + + @Override + public boolean isClosed() { + return false; + } + + @Override + public boolean isOpen() { + return false; + } + + public Channel getChannel() { + return channel; + } + + public void setChannel(Channel channel) { + this.channel = channel; + } + + public int increaseAndGetHbTimes() { + return ++hbTimes; + } + + public void resetHbTimes() { + hbTimes = 0; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + @Override + public void close() { + this.channel.close(); + } + + @Override + public int getHbTimes() { + return hbTimes; + } + + @Override + public boolean isConnected() { + return channel.isActive(); + } + + @Override + public boolean isEnable() { + return channel.isWritable(); + } + + @Override + public String remoteIp() { + return channel.remoteAddress().toString(); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index e1cf6da3..4a811191 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.ClientInfo; +import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.api.protocol.Packet; @@ -38,8 +38,8 @@ public void handle(HandShakeMessage body, Request request) { String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); String clientKey = body.clientKey; String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥 - ClientInfo info = new ClientInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); - request.getConnection().setInfo(info); + SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); + request.getConnection().setClientInfo(info); ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info); ReusableTokenManager.INSTANCE.saveToken(token); Map resp = new HashMap(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java index 62b03fc2..ceb0b871 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.security; -import com.shinemo.mpush.api.ClientInfo; +import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.tools.crypto.MD5Utils; /** @@ -19,7 +19,7 @@ public ReusableToken getToken() { return new ReusableToken(); } - public ReusableToken genToken(ClientInfo info) { + public ReusableToken genToken(SessionInfo info) { /** * 先生成key,需要保证半个周期内同一个设备生成的key是相同的 */ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index 330f1f41..e6fede23 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -5,7 +5,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.crypto.BadPaddingException; import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.math.BigInteger; import java.security.*; @@ -83,24 +85,19 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { * @return * @throws Exception */ - public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) - throws Exception { - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.ENCRYPT_MODE, publicKey); - // 模长 - int key_len = publicKey.getModulus().bitLength() / 8; - // 加密数据长度 <= 模长-11 - byte[][] arrays = splitArray(data, key_len - 11); - //如果明文长度大于模长-11则要分组加密 - byte[] result = new byte[0]; - for (byte[] arr : arrays) { - byte[] buffer = cipher.doFinal(arr); - byte[] tmp = new byte[result.length + buffer.length]; - System.arraycopy(result, 0, tmp, 0, result.length); - System.arraycopy(buffer, 0, tmp, result.length, tmp.length); - result = tmp; + public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { + try { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + // 模长 + int key_len = publicKey.getModulus().bitLength() / 8; + // 加密数据长度 <= 模长-11 + byte[][] arrays = splitArray(data, key_len - 11); + //如果明文长度大于模长-11则要分组加密 + return doFinal(cipher, arrays); + } catch (Exception e) { } - return result; + return Constants.EMPTY_BYTES; } /** @@ -117,21 +114,49 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) cipher.init(Cipher.DECRYPT_MODE, privateKey); //模长 int key_len = privateKey.getModulus().bitLength() / 8; - // byte[] bcd = ASCII_To_BCD(data, data.length); + //byte[] bcd = ASCII_To_BCD(data, data.length); //如果密文长度大于模长则要分组解密 byte[][] arrays = splitArray(data, key_len); - byte[] result = new byte[0]; - for (byte[] arr : arrays) { - byte[] buffer = cipher.doFinal(arr); - byte[] tmp = new byte[result.length + buffer.length]; - System.arraycopy(result, 0, tmp, 0, result.length); - System.arraycopy(buffer, 0, tmp, result.length, tmp.length); - result = tmp; - } - return result; + return doFinal(cipher, arrays); } catch (Exception e) { } - return new byte[0]; + return Constants.EMPTY_BYTES; + } + + private static byte[] doFinal(Cipher cipher, byte[][] arrays) throws BadPaddingException, IllegalBlockSizeException { + byte[] result = new byte[0]; + for (byte[] arr : arrays) { + byte[] buffer = cipher.doFinal(arr); + byte[] tmp = new byte[result.length + buffer.length]; + System.arraycopy(result, 0, tmp, 0, result.length); + System.arraycopy(buffer, 0, tmp, result.length, tmp.length); + result = tmp; + } + return result; + } + + /** + * 拆分数组 + */ + public static byte[][] splitArray(byte[] data, int len) { + int x = data.length / len; + int y = data.length % len; + int z = 0; + if (y != 0) { + z = 1; + } + byte[][] arrays = new byte[x + z][]; + byte[] arr; + for (int i = 0; i < x + z; i++) { + arr = new byte[len]; + if (i == x + z - 1 && y != 0) { + System.arraycopy(data, i * len, arr, 0, y); + } else { + System.arraycopy(data, i * len, arr, 0, len); + } + arrays[i] = arr; + } + return arrays; } /** @@ -200,37 +225,12 @@ public static String[] splitString(String string, int len) { return strings; } - /** - * 拆分数组 - */ - public static byte[][] splitArray(byte[] data, int len) { - int x = data.length / len; - int y = data.length % len; - int z = 0; - if (y != 0) { - z = 1; - } - byte[][] arrays = new byte[x + z][]; - byte[] arr; - for (int i = 0; i < x + z; i++) { - arr = new byte[len]; - if (i == x + z - 1 && y != 0) { - System.arraycopy(data, i * len, arr, 0, y); - } else { - System.arraycopy(data, i * len, arr, 0, len); - } - arrays[i] = arr; - } - return arrays; - } public static void main(String[] args) throws Exception { - // TODO Auto-generated method stub Pair pair = RSAUtils.getKeys(); //生成公钥和私钥 RSAPublicKey publicKey = pair.key; RSAPrivateKey privateKey = pair.value; - //模 String modulus = publicKey.getModulus().toString(); //公钥指数 @@ -239,14 +239,15 @@ public static void main(String[] args) throws Exception { String private_exponent = privateKey.getPrivateExponent().toString(); //明文 byte[] ming = "123456789".getBytes(Constants.UTF_8); + System.out.println("明文:" + new String(ming, Constants.UTF_8)); //使用模和指数生成公钥和私钥 RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); //加密后的密文 byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey); - System.err.println(new String(mi, Constants.UTF_8)); + System.out.println("密文:" + new String(mi, Constants.UTF_8)); //解密后的明文 ming = RSAUtils.decryptByPrivateKey(mi, priKey); - System.err.println(new String(ming, Constants.UTF_8)); + System.out.println("解密:" + new String(ming, Constants.UTF_8)); } } \ No newline at end of file From 8389da0e1814de93b4b9d01e54f6cd4c8c52a7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 25 Dec 2015 18:06:36 +0800 Subject: [PATCH 026/890] add timer --- .../test/java/com/shinemo/mpush/connection/ClientTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index c04a021f..acb11984 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -95,7 +95,9 @@ public void operationComplete(ChannelFuture future) throws Exception { } }); }finally{ - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + if(channel.isActive()){ + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } } } }, Constants.TIME_DELAY, TimeUnit.SECONDS); From a26543de81932261f146c8f5fae31db879aac52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Dec 2015 16:30:01 +0000 Subject: [PATCH 027/890] =?UTF-8?q?=E6=8F=A1=E6=89=8B=E5=92=8C=E5=BF=AB?= =?UTF-8?q?=E9=80=9F=E9=87=8D=E8=BF=9E=E6=A0=A1=E9=AA=8C=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/api/MessageHandler.java | 2 - .../java/com/shinemo/mpush/api/Response.java | 6 + .../shinemo/mpush/api/protocol/Command.java | 3 +- .../shinemo/mpush/api/protocol/Packet.java | 6 + .../netty/handler/ConnectionHandler.java | 4 +- .../mpush/connection/ClientHandler.java | 97 +++++- .../shinemo/mpush/connection/ClientTest.java | 3 +- .../shinemo/mpush/core/MessageReceiver.java | 12 +- .../core/handler/BaseMessageHandler.java | 3 +- .../core/handler/FastConnectHandler.java | 43 +++ .../mpush/core/handler/HandShakeHandler.java | 14 +- .../core/message/FastConnectMessage.java | 11 + .../mpush/core/message/NettyResponse.java | 19 +- .../core/security/CredentialManager.java | 71 ++++- .../core/security/ReusableTokenManager.java | 10 +- .../core/security/CredentialManagerTest.java | 21 ++ .../mpush/tools/crypto/Base64Utils.java | 136 +++++++++ .../shinemo/mpush/tools/crypto/DESUtils.java | 8 +- .../shinemo/mpush/tools/crypto/RSAUtils.java | 285 +++++++++++------- .../mpush/tools/crypto/DESUtilsTest.java | 29 ++ .../mpush/tools/crypto/RSAUtilsTest.java | 110 +++++++ 21 files changed, 756 insertions(+), 137 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java create mode 100644 mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java index f1ed34b5..05cfc614 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java @@ -1,7 +1,5 @@ package com.shinemo.mpush.api; -import com.shinemo.mpush.api.Request; - /** * Created by ohun on 2015/12/22. */ diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java index 639a6c5f..2b43c70b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java @@ -10,4 +10,10 @@ public interface Response { void sendRaw(byte[] body); void sendError(byte[] reason); + + void send(String body); + + void sendRaw(String body); + + void sendError(String reason); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index 9a6d1fb2..dbacad21 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -11,6 +11,7 @@ public enum Command { Bind(5), Unbind(6), Kick(7), + FastConnect(8), Unknown(-1); Command(int cmd) { @@ -20,7 +21,7 @@ public enum Command { public final byte cmd; public static Command toCMD(byte b) { - if (b > 0 && b < values().length - 1) return values()[b - 1]; + if (b > 0 && b < values().length) return values()[b - 1]; return Unknown; } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 0fa8f0f1..1d3ada6c 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -1,5 +1,8 @@ package com.shinemo.mpush.api.protocol; +import com.google.common.base.Strings; +import com.shinemo.mpush.api.Constants; + import java.io.Serializable; import java.util.Arrays; @@ -20,6 +23,9 @@ public int getBodyLength() { return body == null ? 0 : body.length; } + public String getStringBody() { + return body == null ? "" : new String(body, Constants.UTF_8); + } @Override public String toString() { diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 9345b983..3afaa69b 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -34,13 +34,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception log.warn(ctx.channel().remoteAddress() + ", channelRead"); Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); - receiver.onMessage(new Request((Packet) msg, connection)); + receiver.onMessage((Packet) msg, connection); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ConnectionManager.INSTANCE.remove(ctx.channel()); - log.warn(ctx.channel().remoteAddress() + ", exceptionCaught"); + log.error(ctx.channel().remoteAddress() + ", exceptionCaught", cause); } @Override diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java index b5c8be47..234195cd 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -3,39 +3,114 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.message.FastConnectMessage; +import com.shinemo.mpush.core.message.HandShakeMessage; +import com.shinemo.mpush.core.security.CredentialManager; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.Strings; +import com.shinemo.mpush.tools.crypto.CryptoUtils; +import com.shinemo.mpush.tools.crypto.DESUtils; +import com.shinemo.mpush.tools.crypto.RSAUtils; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.*; +import java.security.interfaces.RSAPublicKey; +import java.util.HashMap; +import java.util.Map; + /** * Created by ohun on 2015/12/24. */ public class ClientHandler extends ChannelHandlerAdapter { - private static final Logger logger = LoggerFactory.getLogger(ClientHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class); + private String clientKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); - Packet packet = new Packet(); - packet.command = Command.Handshake.cmd; - packet.version = 0; - packet.flags = 0; - packet.msgId = 1; - packet.body = "hello word".getBytes(Constants.UTF_8); - ctx.writeAndFlush(packet); - logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelActive"); + String token = getToken(); + if (Strings.isBlank(token)) { + RSAPublicKey publicKey = CredentialManager.INSTANCE.getPublicKey(); + HandShakeMessage message = new HandShakeMessage(); + message.clientKey = clientKey; + message.clientVersion = "1.0.1"; + message.deviceId = "1111111111111"; + message.osName = "android"; + message.osVersion = "5.0"; + message.timestamp = System.currentTimeMillis(); + + Packet packet = new Packet(); + packet.command = Command.Handshake.cmd; + packet.version = 0; + packet.flags = 0; + packet.msgId = 1; + packet.body = RSAUtils.encryptByPublicKey(Jsons.toJson(message).getBytes(Constants.UTF_8), publicKey); + ctx.writeAndFlush(packet); + } else { + FastConnectMessage message = new FastConnectMessage(); + message.deviceId = "1111111111111"; + message.tokenId = token; + Packet packet = new Packet(); + packet.command = Command.FastConnect.cmd; + packet.version = 0; + packet.flags = 0; + packet.msgId = 1; + packet.body = Jsons.toJson(message).getBytes(Constants.UTF_8); + ctx.writeAndFlush(packet); + } + LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelActive"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); - logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelInactive"); + LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelInactive"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); - logger.info("client,"+ctx.channel().remoteAddress().toString(),"channelRead",msg); + LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelRead", msg); + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + Command command = Command.toCMD(packet.command); + if (command == Command.Handshake) { + String raw = new String(DESUtils.decryptDES(packet.body, clientKey), Constants.UTF_8); + Map resp = Jsons.fromJson(raw, Map.class); + LOGGER.info("hand shake success, message=" + raw); + String desKey = CryptoUtils.mixString(clientKey, (String) resp.get("serverKey")); + LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", desKey, clientKey, resp.get("serverKey")); + saveToken((String) resp.get("tokenId")); + } else if (command == Command.FastConnect) { + LOGGER.info("fast connect success, message=" + packet.getStringBody()); + } + } + + } + + + private void saveToken(String token) { + try { + String path = this.getClass().getResource("/").getFile(); + FileOutputStream out = new FileOutputStream(new File(path, "token.dat")); + out.write(token.getBytes()); + out.close(); + } catch (Exception e) { + } + } + + private String getToken() { + try { + InputStream in = this.getClass().getResourceAsStream("/token.dat"); + byte[] bytes = new byte[in.available()]; + in.read(bytes); + return new String(bytes); + } catch (Exception e) { + } + return Strings.EMPTY; } } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index ea67e428..21fe537d 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -53,9 +53,10 @@ public void initChannel(SocketChannel ch) throws Exception { } }); ChannelFuture future = b.connect(host, port).sync(); // (5) - + future.channel().closeFuture().sync(); if (future.awaitUninterruptibly(4000) && future.isSuccess()) { final Channel channel = future.channel(); + startHeartBeat(channel); } else { future.cancel(true); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java index c4f92feb..155ddc00 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -5,9 +5,7 @@ import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.Request; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.handler.BindHandler; -import com.shinemo.mpush.core.handler.HeartBeatHandler; -import com.shinemo.mpush.core.handler.LoginHandler; +import com.shinemo.mpush.core.handler.*; import com.shinemo.mpush.core.message.NettyRequest; /** @@ -16,7 +14,9 @@ public class MessageReceiver implements Receiver { public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); public static final MessageHandler BIND_HANDLER = new BindHandler(); - public static final HeartBeatHandler HEART_HANDLER = new HeartBeatHandler(); + public static final MessageHandler HEART_HANDLER = new HeartBeatHandler(); + public static final MessageHandler HAND_SHAKE_HANDLER = new HandShakeHandler(); + public static final MessageHandler FAST_CONNECT_HANDLER = new FastConnectHandler(); @Override public void onMessage(Packet packet, Connection connection) { @@ -26,6 +26,7 @@ public void onMessage(Packet packet, Connection connection) { HEART_HANDLER.handle(request); break; case Handshake: + HAND_SHAKE_HANDLER.handle(request); break; case Login: LOGIN_HANDLER.handle(request); @@ -35,6 +36,9 @@ public void onMessage(Packet packet, Connection connection) { break; case Kick: break; + case FastConnect: + FAST_CONNECT_HANDLER.handle(request); + break; case Unknown: break; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java index 59459d6a..9b017962 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java @@ -11,7 +11,8 @@ public abstract class BaseMessageHandler implements MessageHandler { @Override public void handle(Request request) { - + T t = decodeBody(request.getMessage()); + handle(t, request); } public abstract T decodeBody(Packet packet); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java new file mode 100644 index 00000000..55fa7dbb --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -0,0 +1,43 @@ +package com.shinemo.mpush.core.handler; + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.SessionInfo; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.message.FastConnectMessage; +import com.shinemo.mpush.core.security.ReusableToken; +import com.shinemo.mpush.core.security.ReusableTokenManager; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.MPushUtil; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by ohun on 2015/12/25. + */ +public class FastConnectHandler extends BaseMessageHandler { + @Override + public FastConnectMessage decodeBody(Packet packet) { + return Jsons.fromJson(packet.getStringBody(), FastConnectMessage.class); + } + + @Override + public void handle(FastConnectMessage body, Request request) { + ReusableToken token = ReusableTokenManager.INSTANCE.getToken(body.tokenId); + if (token == null) { + request.getResponse().sendRaw("token expire".getBytes(Constants.UTF_8)); + } else if (!token.deviceId.equals(body.deviceId)) { + request.getResponse().sendRaw("error device".getBytes(Constants.UTF_8)); + } else { + SessionInfo info = new SessionInfo(token.osName, token.osVersion, token.clientVersion, token.deviceId, token.desKey); + request.getConnection().setSessionInfo(info); + Map resp = new HashMap(); + resp.put("serverHost", MPushUtil.getLocalIp()); + resp.put("serverTime", System.currentTimeMillis()); + resp.put("heartbeat", Constants.HEARTBEAT_TIME); + request.getResponse().sendRaw(Jsons.toJson(resp).getBytes(Constants.UTF_8)); + } + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 4a811191..9eceab4a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -14,6 +14,8 @@ import com.shinemo.mpush.tools.crypto.DESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; import org.apache.commons.lang3.RandomStringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.Serializable; import java.security.interfaces.RSAPrivateKey; @@ -24,13 +26,13 @@ * Created by ohun on 2015/12/24. */ public class HandShakeHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); @Override public HandShakeMessage decodeBody(Packet packet) { RSAPrivateKey privateKey = CredentialManager.INSTANCE.getPrivateKey(); - byte[] unwarp = RSAUtils.decryptByPrivateKey(packet.body, privateKey); - - return new HandShakeMessage(); + byte[] rawData = RSAUtils.decryptByPrivateKey(packet.body, privateKey); + return Jsons.fromJson(new String(rawData, Constants.UTF_8), HandShakeMessage.class); } @Override @@ -39,7 +41,7 @@ public void handle(HandShakeMessage body, Request request) { String clientKey = body.clientKey; String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥 SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); - request.getConnection().setClientInfo(info); + request.getConnection().setSessionInfo(info); ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info); ReusableTokenManager.INSTANCE.saveToken(token); Map resp = new HashMap(); @@ -49,7 +51,9 @@ public void handle(HandShakeMessage body, Request request) { resp.put("heartbeat", Constants.HEARTBEAT_TIME); resp.put("tokenId", token.tokenId); resp.put("tokenExpire", token.expireTime); - byte[] responseData = DESUtils.decryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey); + byte[] responseData = DESUtils.encryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey); request.getResponse().sendRaw(responseData); + LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", desKey, clientKey, serverKey); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java new file mode 100644 index 00000000..e229191e --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java @@ -0,0 +1,11 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Message; + +/** + * Created by ohun on 2015/12/25. + */ +public class FastConnectMessage implements Message { + public String tokenId; + public String deviceId; +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java index fd6f796f..9075fca3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.core.message; import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Response; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.tools.crypto.DESUtils; @@ -18,7 +19,7 @@ public NettyResponse(Packet packet, Connection connection) { } public void send(byte[] body) { - packet.body = DESUtils.decryptDES(body, connection.getInfo().desKey); + packet.body = DESUtils.decryptDES(body, connection.getSessionInfo().desKey); connection.send(packet); } @@ -29,6 +30,22 @@ public void sendRaw(byte[] body) { public void sendError(byte[] reason) { + packet.body = reason; + connection.send(packet); + } + + @Override + public void send(String body) { + send(body.getBytes(Constants.UTF_8)); + } + + @Override + public void sendRaw(String body) { + sendRaw(body.getBytes(Constants.UTF_8)); + } + @Override + public void sendError(String reason) { + sendError(reason.getBytes(Constants.UTF_8)); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java index b52c92dd..d4af719e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java @@ -1,17 +1,86 @@ package com.shinemo.mpush.core.security; +import com.shinemo.mpush.tools.Pair; import com.shinemo.mpush.tools.crypto.RSAUtils; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; /** * Created by ohun on 2015/12/24. */ public class CredentialManager { public static final CredentialManager INSTANCE = new CredentialManager(); + private RSAPrivateKey privateKey; + private RSAPublicKey publicKey; + + public void init() { + readFromFile(); + if (publicKey == null || privateKey == null) { + Pair pair = RSAUtils.genKeyPair(); + //生成公钥和私钥 + publicKey = pair.key; + privateKey = pair.value; + //模 + String modulus = publicKey.getModulus().toString(); + //公钥指数 + String public_exponent = publicKey.getPublicExponent().toString(); + //私钥指数 + String private_exponent = privateKey.getPrivateExponent().toString(); + //使用模和指数生成公钥和私钥 + publicKey = RSAUtils.getPublicKey(modulus, public_exponent); + privateKey = RSAUtils.getPrivateKey(modulus, private_exponent); + writeToFile(); + } + } + + private void writeToFile() { + try { + String publicKeyStr = RSAUtils.encodeBase64(INSTANCE.publicKey); + String privateKeyStr = RSAUtils.encodeBase64(INSTANCE.privateKey); + String path = this.getClass().getResource("/").getPath(); + FileOutputStream out = new FileOutputStream(new File(path, "private.key")); + out.write(privateKeyStr.getBytes()); + out.close(); + out = new FileOutputStream(new File(path, "public.key")); + out.write(publicKeyStr.getBytes()); + out.close(); + System.out.println("write key=" + path); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void readFromFile() { + try { + InputStream in = this.getClass().getResourceAsStream("/private.key"); + byte[] buffer = new byte[in.available()]; + in.read(buffer); + in.close(); + INSTANCE.privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(new String(buffer)); + in = this.getClass().getResourceAsStream("/public.key"); + in.read(buffer); + in.close(); + INSTANCE.publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(new String(buffer)); + System.out.println("save privateKey=" + privateKey); + System.out.println("save publicKey=" + publicKey); + } catch (Exception e) { + e.printStackTrace(); + } + } public RSAPrivateKey getPrivateKey() { - return RSAUtils.getPrivateKey("", ""); + if (INSTANCE.privateKey == null) init(); + return INSTANCE.privateKey; + } + + public RSAPublicKey getPublicKey() { + if (INSTANCE.publicKey == null) init(); + return INSTANCE.publicKey; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java index ceb0b871..59e0b35d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java @@ -3,20 +3,24 @@ import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.tools.crypto.MD5Utils; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * Created by ohun on 2015/12/25. */ public class ReusableTokenManager { public static final ReusableTokenManager INSTANCE = new ReusableTokenManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; - + private final Map tokenCache = new ConcurrentHashMap(); public boolean saveToken(ReusableToken token) { + tokenCache.put(token.tokenId, token); return true; } - public ReusableToken getToken() { - return new ReusableToken(); + public ReusableToken getToken(String tokenId) { + return tokenCache.get(tokenId); } public ReusableToken genToken(SessionInfo info) { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java new file mode 100644 index 00000000..3e0c3f1f --- /dev/null +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.core.security; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by ohun on 2015/12/25. + */ +public class CredentialManagerTest { + + @Test + public void testGetPrivateKey() throws Exception { + CredentialManager.INSTANCE.getPrivateKey(); + } + + @Test + public void testGetPublicKey() throws Exception { + + } +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java new file mode 100644 index 00000000..455b1f3b --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java @@ -0,0 +1,136 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Constants; +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; + +import java.io.*; + +/** + *

+ * BASE64编码解码工具包 + *

+ *

+ * 依赖javabase64-1.3.1.jar + *

+ * + */ +public class Base64Utils { + + /** + * 文件读取缓冲区大小 + */ + private static final int CACHE_SIZE = 1024; + + /** + *

+ * BASE64字符串解码为二进制数据 + *

+ * + * @param base64 + * @return + * @throws Exception + */ + public static byte[] decode(String base64) throws Exception { + return Base64.decode(base64); + } + + /** + *

+ * 二进制数据编码为BASE64字符串 + *

+ * + * @param bytes + * @return + * @throws Exception + */ + public static String encode(byte[] bytes) throws Exception { + return Base64.encode(bytes); + } + + /** + *

+ * 将文件编码为BASE64字符串 + *

+ *

+ * 大文件慎用,可能会导致内存溢出 + *

+ * + * @param filePath 文件绝对路径 + * @return + * @throws Exception + */ + public static String encodeFile(String filePath) throws Exception { + byte[] bytes = fileToByte(filePath); + return encode(bytes); + } + + /** + *

+ * BASE64字符串转回文件 + *

+ * + * @param filePath 文件绝对路径 + * @param base64 编码字符串 + * @throws Exception + */ + public static void decodeToFile(String filePath, String base64) throws Exception { + byte[] bytes = decode(base64); + byteArrayToFile(bytes, filePath); + } + + /** + *

+ * 文件转换为二进制数组 + *

+ * + * @param filePath 文件路径 + * @return + * @throws Exception + */ + public static byte[] fileToByte(String filePath) throws Exception { + byte[] data = new byte[0]; + File file = new File(filePath); + if (file.exists()) { + FileInputStream in = new FileInputStream(file); + ByteArrayOutputStream out = new ByteArrayOutputStream(2048); + byte[] cache = new byte[CACHE_SIZE]; + int nRead = 0; + while ((nRead = in.read(cache)) != -1) { + out.write(cache, 0, nRead); + out.flush(); + } + out.close(); + in.close(); + data = out.toByteArray(); + } + return data; + } + + /** + *

+ * 二进制数据写文件 + *

+ * + * @param bytes 二进制数据 + * @param filePath 文件生成目录 + */ + public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { + InputStream in = new ByteArrayInputStream(bytes); + File destFile = new File(filePath); + if (!destFile.getParentFile().exists()) { + destFile.getParentFile().mkdirs(); + } + destFile.createNewFile(); + OutputStream out = new FileOutputStream(destFile); + byte[] cache = new byte[CACHE_SIZE]; + int nRead = 0; + while ((nRead = in.read(cache)) != -1) { + out.write(cache, 0, nRead); + out.flush(); + } + out.close(); + in.close(); + } + + +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java index 55ff05d9..9266696b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java @@ -19,6 +19,10 @@ public static byte[] encryptDES(byte[] encryptBytes, String encryptKey) { return encryptDES(encryptBytes, 0, encryptBytes.length, encryptKey); } + public static byte[] decryptDES(byte[] byteMi, String decryptKey) { + return decryptDES(byteMi, 0, byteMi.length, decryptKey); + } + public static byte[] encryptDES(byte[] encryptBytes, int offset, int length, String encryptKey) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), "DES"); @@ -32,10 +36,6 @@ public static byte[] encryptDES(byte[] encryptBytes, int offset, int length, Str return Constants.EMPTY_BYTES; } - public static byte[] decryptDES(byte[] byteMi, String decryptKey) { - return decryptDES(byteMi, 0, byteMi.length, decryptKey); - } - public static byte[] decryptDES(byte[] byteMi, int offset, int length, String decryptKey) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(Constants.UTF_8), "DES"); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index e6fede23..bbb7935c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -9,30 +9,125 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; +import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; +import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; +import java.security.spec.X509EncodedKeySpec; +/** + * *

+ * RSA公钥/私钥/签名工具包 + *

+ *

+ * 罗纳德·李维斯特(Ron [R]ivest)、阿迪·萨莫尔(Adi [S]hamir)和伦纳德·阿德曼(Leonard [A]dleman) + *

+ *

+ * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式
+ * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,
+ * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 + */ public class RSAUtils { private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class); + /** + * 加密算法RSA + */ + public static final String KEY_ALGORITHM = "RSA"; + + /** + * 签名算法 + */ + public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; + + /** + * RSA最大加密明文大小 + */ + private static final int MAX_ENCRYPT_BLOCK = 117; + + /** + * RSA最大解密密文大小 + */ + private static final int MAX_DECRYPT_BLOCK = 128; /** * 生成公钥和私钥 * * @throws NoSuchAlgorithmException */ - public static Pair getKeys() throws NoSuchAlgorithmException { - KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); - keyPairGen.initialize(1024); - KeyPair keyPair = keyPairGen.generateKeyPair(); - RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); - RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); - return Pair.of(publicKey, privateKey); + public static Pair genKeyPair() { + try { + KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); + keyPairGen.initialize(1024); + KeyPair keyPair = keyPairGen.generateKeyPair(); + RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); + RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); + return Pair.of(publicKey, privateKey); + } catch (NoSuchAlgorithmException e) { + LOGGER.error("getKeys ex ", e); + } + return null; + } + + + public static String encodeBase64(Key key) throws Exception { + return Base64Utils.encode(key.getEncoded()); + } + + public static PrivateKey decodePrivateKey(String key) throws Exception { + byte[] keyBytes = Base64Utils.decode(key); + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + return keyFactory.generatePrivate(pkcs8KeySpec); + } + + public static PublicKey decodePublicKey(String publicKey) throws Exception { + byte[] keyBytes = Base64Utils.decode(publicKey); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + return keyFactory.generatePublic(x509KeySpec); + } + + + /** + *

+ * 用私钥对信息生成数字签名 + *

+ * + * @param data 已加密数据 + * @param privateKey 私钥(BASE64编码) + * @return + * @throws Exception + */ + public static String sign(byte[] data, String privateKey) throws Exception { + Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); + signature.initSign(decodePrivateKey(privateKey)); + signature.update(data); + return Base64Utils.encode(signature.sign()); + } + + /** + *

+ * 校验数字签名 + *

+ * + * @param data 已加密数据 + * @param publicKey 公钥(BASE64编码) + * @param sign 数字签名 + * @return + * @throws Exception + */ + public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { + Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); + signature.initVerify(decodePublicKey(publicKey)); + signature.update(data); + return signature.verify(Base64Utils.decode(sign)); } + /** * 使用模和指数生成RSA公钥 * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA @@ -46,7 +141,7 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { try { BigInteger b1 = new BigInteger(modulus); BigInteger b2 = new BigInteger(exponent); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { @@ -68,7 +163,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { try { BigInteger b1 = new BigInteger(modulus); BigInteger b2 = new BigInteger(exponent); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { @@ -87,15 +182,15 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { */ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { try { - Cipher cipher = Cipher.getInstance("RSA"); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 模长 int key_len = publicKey.getModulus().bitLength() / 8; // 加密数据长度 <= 模长-11 - byte[][] arrays = splitArray(data, key_len - 11); //如果明文长度大于模长-11则要分组加密 - return doFinal(cipher, arrays); + return doFinal(cipher, data, key_len - 11); } catch (Exception e) { + LOGGER.error("encryptByPublicKey ex", e); } return Constants.EMPTY_BYTES; } @@ -110,124 +205,112 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { */ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) { try { - Cipher cipher = Cipher.getInstance("RSA"); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); //模长 int key_len = privateKey.getModulus().bitLength() / 8; - //byte[] bcd = ASCII_To_BCD(data, data.length); //如果密文长度大于模长则要分组解密 - byte[][] arrays = splitArray(data, key_len); - return doFinal(cipher, arrays); + return doFinal(cipher, data, key_len); } catch (Exception e) { + LOGGER.error("decryptByPrivateKey ex", e); } return Constants.EMPTY_BYTES; } - private static byte[] doFinal(Cipher cipher, byte[][] arrays) throws BadPaddingException, IllegalBlockSizeException { - byte[] result = new byte[0]; - for (byte[] arr : arrays) { - byte[] buffer = cipher.doFinal(arr); - byte[] tmp = new byte[result.length + buffer.length]; - System.arraycopy(result, 0, tmp, 0, result.length); - System.arraycopy(buffer, 0, tmp, result.length, tmp.length); - result = tmp; - } - return result; - } - /** - * 拆分数组 + * 注意:RSA加密明文最大长度117字节,解密要求密文最大长度为128字节,所以在加密和解密的过程中需要分块进行。 + * + * @param cipher + * @param data + * @return + * @throws BadPaddingException + * @throws IllegalBlockSizeException */ - public static byte[][] splitArray(byte[] data, int len) { - int x = data.length / len; - int y = data.length % len; - int z = 0; - if (y != 0) { - z = 1; + private static byte[] doFinal(Cipher cipher, byte[] data, int key_len) throws BadPaddingException, IllegalBlockSizeException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int inputLen = data.length, offSet = 0; + byte[] tmp; + while (inputLen > 0) { + tmp = cipher.doFinal(data, offSet, Math.min(key_len, inputLen)); + out.write(tmp, 0, tmp.length); + offSet += key_len; + inputLen -= key_len; } - byte[][] arrays = new byte[x + z][]; - byte[] arr; - for (int i = 0; i < x + z; i++) { - arr = new byte[len]; - if (i == x + z - 1 && y != 0) { - System.arraycopy(data, i * len, arr, 0, y); - } else { - System.arraycopy(data, i * len, arr, 0, len); - } - arrays[i] = arr; - } - return arrays; + return out.toByteArray(); } /** - * ASCII码转BCD码 + *

+ * 私钥解密 + *

+ * + * @param data 已加密数据 + * @param privateKey 私钥(BASE64编码) + * @return + * @throws Exception */ - public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) { - byte[] bcd = new byte[asc_len / 2]; - int j = 0; - for (int i = 0; i < (asc_len + 1) / 2; i++) { - bcd[i] = asc_to_bcd(ascii[j++]); - bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4)); - } - return bcd; + public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + PrivateKey key = decodePrivateKey(privateKey); + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + cipher.init(Cipher.DECRYPT_MODE, key); + return doFinal(cipher, data, MAX_DECRYPT_BLOCK); } - public static byte asc_to_bcd(byte asc) { - byte bcd; - - if ((asc >= '0') && (asc <= '9')) - bcd = (byte) (asc - '0'); - else if ((asc >= 'A') && (asc <= 'F')) - bcd = (byte) (asc - 'A' + 10); - else if ((asc >= 'a') && (asc <= 'f')) - bcd = (byte) (asc - 'a' + 10); - else - bcd = (byte) (asc - 48); - return bcd; + /** + *

+ * 公钥解密 + *

+ * + * @param data 已加密数据 + * @param publicKey 公钥(BASE64编码) + * @return + * @throws Exception + */ + public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Exception { + PublicKey key = decodePublicKey(publicKey); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, key); + return doFinal(cipher, data, MAX_DECRYPT_BLOCK); } /** - * BCD转字符串 + *

+ * 公钥加密 + *

+ * + * @param data 源数据 + * @param publicKey 公钥(BASE64编码) + * @return + * @throws Exception */ - public static String bcd2Str(byte[] bytes) { - char temp[] = new char[bytes.length * 2], val; - - for (int i = 0; i < bytes.length; i++) { - val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f); - temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); - - val = (char) (bytes[i] & 0x0f); - temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); - } - return new String(temp); + public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { + PublicKey key = decodePublicKey(publicKey); + // 对数据加密 + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + cipher.init(Cipher.ENCRYPT_MODE, key); + return doFinal(cipher, data, MAX_ENCRYPT_BLOCK); } /** - * 拆分字符串 + *

+ * 私钥加密 + *

+ * + * @param data 源数据 + * @param privateKey 私钥(BASE64编码) + * @return + * @throws Exception */ - public static String[] splitString(String string, int len) { - int x = string.length() / len; - int y = string.length() % len; - int z = 0; - if (y != 0) { - z = 1; - } - String[] strings = new String[x + z]; - String str = ""; - for (int i = 0; i < x + z; i++) { - if (i == x + z - 1 && y != 0) { - str = string.substring(i * len, i * len + y); - } else { - str = string.substring(i * len, i * len + len); - } - strings[i] = str; - } - return strings; + public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception { + PrivateKey key = decodePrivateKey(privateKey); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + cipher.init(Cipher.ENCRYPT_MODE, key); + return doFinal(cipher, data, MAX_ENCRYPT_BLOCK); } - public static void main(String[] args) throws Exception { - Pair pair = RSAUtils.getKeys(); + Pair pair = RSAUtils.genKeyPair(); //生成公钥和私钥 RSAPublicKey publicKey = pair.key; RSAPrivateKey privateKey = pair.value; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java new file mode 100644 index 00000000..b8aee148 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java @@ -0,0 +1,29 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Constants; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by ohun on 2015/12/25. + */ +public class DESUtilsTest { + + @Test + public void testEncryptDES() throws Exception { + String data = "似的士大夫士大夫士大夫首发式发生士大夫"; + System.out.println("原文:\n" + data); + String key = RandomStringUtils.randomAscii(8); + byte[] d1 = DESUtils.encryptDES(data.getBytes(Constants.UTF_8), key); + System.out.println("加密后:\n" + new String(d1)); + byte[] d2 = DESUtils.decryptDES(d1, key); + System.out.println("解密后:\n" + new String(d2, Constants.UTF_8)); + } + + @Test + public void testDecryptDES() throws Exception { + + } +} \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java new file mode 100644 index 00000000..552df98a --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java @@ -0,0 +1,110 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Pair; +import org.junit.Before; +import org.junit.Test; + +import java.net.URL; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; + +import static org.junit.Assert.*; + +/** + * Created by ohun on 2015/12/25. + */ +public class RSAUtilsTest { + String publicKey; + String privateKey; + + @Before + public void setUp() throws Exception { + try { + Pair pair = RSAUtils.genKeyPair(); + publicKey = RSAUtils.encodeBase64(pair.key); + privateKey = RSAUtils.encodeBase64(pair.value); + System.out.println("公钥: \n\r" + publicKey); + System.out.println("私钥: \n\r" + privateKey); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testGetKeys() throws Exception { + + } + + @Test + public void testGetPrivateKey() throws Exception { + + } + + @Test + public void testGetPublicKey() throws Exception { + + } + + @Test + public void testSign() throws Exception { + String source = "这是一行测试RSA数字签名的无意义文字"; + System.out.println("===========私钥加密——公钥解密======================="); + System.out.println("原文字:\n" + source); + byte[] data = source.getBytes(); + byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey); + System.out.println("加密后:\n" + new String(encodedData)); + byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey); + String target = new String(decodedData); + System.out.println("解密后:\n" + target); + System.out.println("============私钥签名——公钥验证签名==================="); + String sign = RSAUtils.sign(encodedData, privateKey); + System.out.println("签名:\n" + sign); + boolean status = RSAUtils.verify(encodedData, publicKey, sign); + System.out.println("验证结果:\n" + status); + } + + @Test + public void test1() throws Exception { + System.err.println("公钥加密——私钥解密"); + String source = "这是一行没有任何意义的文字,你看完了等于没看,不是吗?"; + System.out.println("\r加密前文字:\r\n" + source); + byte[] data = source.getBytes(); + byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey); + System.out.println("加密后文字:\r\n" + new String(encodedData)); + byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey); + String target = new String(decodedData); + System.out.println("解密后文字: \r\n" + target); + } + + @Test + public void testGetPrivateKey1() throws Exception { + URL url = this.getClass().getResource("/"); + System.out.println(url.getPath()); + System.out.println(url.getFile()); + } + + @Test + public void testEncryptByPublicKey() throws Exception { + + } + + @Test + public void testDecryptByPrivateKey() throws Exception { + + } + + @Test + public void testDecryptByPrivateKey1() throws Exception { + + } + + @Test + public void testEncryptByPublicKey1() throws Exception { + + } + + @Test + public void testEncryptByPrivateKey() throws Exception { + + } +} \ No newline at end of file From 27f21a3115ad9233c86748a4b364f1e788b7ebc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 27 Dec 2015 04:14:23 +0000 Subject: [PATCH 028/890] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E5=92=8C=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Constants.java | 6 +- .../com/shinemo/mpush/api/SessionInfo.java | 8 +- .../shinemo/mpush/api/protocol/Packet.java | 17 +- .../connection/netty/codec/PacketDecoder.java | 33 +- .../connection/netty/codec/PacketEncoder.java | 24 +- .../netty/handler/ConnectionHandler.java | 2 - .../mpush/connection/ClientHandler.java | 44 +- .../shinemo/mpush/connection/ClientTest.java | 145 ++- .../shinemo/mpush/core/MessageReceiver.java | 2 +- .../core/handler/FastConnectHandler.java | 14 +- .../mpush/core/handler/HeartBeatHandler.java | 15 +- .../mpush/core/message/HandShakeMessage.java | 5 +- .../core/message/HandshakeSuccessMsg.java | 13 + .../mpush/core/message/HeartBeatMessage.java | 15 - .../mpush/core/message/NettyRequest.java | 7 +- .../mpush/core/message/NettyResponse.java | 4 +- .../mpush/core/request/HandshakeRequest.java | 21 + ...dentialManager.java => CipherManager.java} | 32 +- .../mpush/core/security/ReusableSession.java | 13 + ...nager.java => ReusableSessionManager.java} | 28 +- .../mpush/core/security/ReusableToken.java | 15 - .../core/security/CipherManagerTest.java | 30 + .../core/security/CredentialManagerTest.java | 21 - .../shinemo/mpush/tools/crypto/AESUtils.java | 53 + .../shinemo/mpush/tools/crypto/Base64.java | 1000 +++++++++++++++++ .../mpush/tools/crypto/Base64Utils.java | 45 +- .../mpush/tools/crypto/CryptoUtils.java | 74 -- .../shinemo/mpush/tools/crypto/DESUtils.java | 51 - .../shinemo/mpush/tools/crypto/RSAUtils.java | 14 +- .../{DESUtilsTest.java => AESUtilsTest.java} | 11 +- 30 files changed, 1370 insertions(+), 392 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java rename mpush-core/src/main/java/com/shinemo/mpush/core/security/{CredentialManager.java => CipherManager.java} (75%) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java rename mpush-core/src/main/java/com/shinemo/mpush/core/security/{ReusableTokenManager.java => ReusableSessionManager.java} (60%) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java create mode 100644 mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java delete mode 100644 mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java rename mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/{DESUtilsTest.java => AESUtilsTest.java} (69%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 465fd721..9b5bd742 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -10,9 +10,9 @@ public interface Constants { byte[] EMPTY_BYTES = new byte[0]; int MAX_PACKET_SIZE = 1024; int HEADER_LEN = 13; - byte MAGIC_NUM1 = (byte) 33; - byte MAGIC_NUM2 = (byte) 99; - long TIME_DELAY = 58L; + short MAGIC_NUM = 1122; + byte HB = '\n'; + long TIME_DELAY = 1L; String JVM_LOG_PATH = "/opt/"; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java index 009fa407..6dd21d5a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java @@ -8,13 +8,15 @@ public class SessionInfo { public final String osVersion; public final String clientVersion; public final String deviceId; - public final String desKey; + public final byte[] sessionKey; + public final byte[] iv; - public SessionInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) { + public SessionInfo(String osName, String osVersion, String clientVersion, String deviceId, byte[] sessionKey, byte[] iv) { this.osName = osName; this.osVersion = osVersion; this.clientVersion = clientVersion; this.deviceId = deviceId; - this.desKey = desKey; + this.sessionKey = sessionKey; + this.iv = iv; } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 1d3ada6c..fc982c06 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.api.protocol; -import com.google.common.base.Strings; import com.shinemo.mpush.api.Constants; import java.io.Serializable; @@ -9,14 +8,15 @@ /** * Created by ohun on 2015/12/19. - * magic(2)+cmd(1)+version(1)+flags(1)+msgId(4)+length(4)+body(n) + * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ public class Packet implements Serializable { private static final long serialVersionUID = -2725825199998223372L; - public byte command; - public byte version; + public byte cmd; + public short cc; public byte flags; - public int msgId; + public int sessionId; + public byte lrc; public byte[] body; public int getBodyLength() { @@ -30,10 +30,11 @@ public String getStringBody() { @Override public String toString() { return "Packet{" + - "command=" + command + - ", version=" + version + + "cmd=" + cmd + + ", cc=" + cc + ", flags=" + flags + - ", msgId=" + msgId + + ", sessionId=" + sessionId + + ", lrc=" + lrc + ", body=" + Arrays.toString(body) + '}'; } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java index e57240fc..859f79fe 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java @@ -2,6 +2,7 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.exception.DecodeException; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -11,15 +12,29 @@ /** * Created by ohun on 2015/12/19. - * magic(2)+length(4)+cmd(1)+version(1)+flags(1)+msgId(4)+body(n) + * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ public class PacketDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + decodeHeartbeat(in, out); decodeFrames(in, out); } + private void decodeHeartbeat(ByteBuf in, List out) { + while (in.isReadable()) { + if (in.readByte() == Constants.HB) { + Packet packet = new Packet(); + packet.cmd = Command.Heartbeat.cmd; + out.add(packet); + } else { + in.readerIndex(in.readerIndex() - 1); + break; + } + } + } + private void decodeFrames(ByteBuf in, List out) throws Exception { try { while (in.readableBytes() >= Constants.HEADER_LEN) { @@ -35,9 +50,9 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { private Packet decodeFrame(ByteBuf in) throws Exception { int bufferSize = in.readableBytes(); - if (in.readByte() != Constants.MAGIC_NUM1 || in.readByte() != Constants.MAGIC_NUM2) { + /*if (in.readShort() != Constants.MAGIC_NUM) { throw new RuntimeException("ERROR MAGIC_NUM"); - } + }*/ int bodyLength = in.readInt(); if (bufferSize < (bodyLength + Constants.HEADER_LEN)) { throw new DecodeException("invalid frame"); @@ -47,9 +62,10 @@ private Packet decodeFrame(ByteBuf in) throws Exception { private Packet readPacket(ByteBuf in, int bodyLength) { byte command = in.readByte(); - byte version = in.readByte(); + short cc = in.readShort(); byte flags = in.readByte(); - int msgId = in.readInt(); + int sessionId = in.readInt(); + byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { if (bodyLength > Constants.MAX_PACKET_SIZE) { @@ -59,10 +75,11 @@ private Packet readPacket(ByteBuf in, int bodyLength) { in.readBytes(body); } Packet packet = new Packet(); - packet.command = command; - packet.version = version; + packet.cmd = command; + packet.cc = cc; packet.flags = flags; - packet.msgId = msgId; + packet.sessionId = sessionId; + packet.lrc = lrc; packet.body = body; return packet; } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java index 194408f1..6f353684 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java @@ -1,8 +1,8 @@ package com.shinemo.mpush.connection.netty.codec; import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; - import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -10,6 +10,7 @@ /** * Created by ohun on 2015/12/19. + * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ @ChannelHandler.Sharable public class PacketEncoder extends MessageToByteEncoder { @@ -17,15 +18,18 @@ public class PacketEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { - out.writeByte(Constants.MAGIC_NUM1); - out.writeByte(Constants.MAGIC_NUM2); - out.writeInt(packet.getBodyLength()); - out.writeByte(packet.command); - out.writeByte(packet.flags); - out.writeByte(packet.version); - out.writeInt(packet.msgId); - if (packet.getBodyLength() > 0) { - out.writeBytes(packet.body); + if (packet.cmd == Command.Heartbeat.cmd) { + out.writeByte(Constants.HB); + } else { + out.writeInt(packet.getBodyLength()); + out.writeByte(packet.cmd); + out.writeShort(packet.cc); + out.writeByte(packet.flags); + out.writeInt(packet.sessionId); + out.writeByte(packet.lrc); + if (packet.getBodyLength() > 0) { + out.writeBytes(packet.body); + } } } } diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java index 3afaa69b..ee544608 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java @@ -31,9 +31,7 @@ public ConnectionHandler(MessageReceiver receiver) { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelRead"); Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); - receiver.onMessage((Packet) msg, connection); } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java index 234195cd..eb0e45d8 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -4,39 +4,39 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.message.FastConnectMessage; -import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.security.CredentialManager; +import com.shinemo.mpush.core.message.HandshakeMessage; +import com.shinemo.mpush.core.message.HandshakeSuccessMsg; +import com.shinemo.mpush.core.security.CipherManager; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.crypto.CryptoUtils; -import com.shinemo.mpush.tools.crypto.DESUtils; +import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.security.interfaces.RSAPublicKey; -import java.util.HashMap; -import java.util.Map; /** * Created by ohun on 2015/12/24. */ public class ClientHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class); - private String clientKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); + private byte[] clientKey = CipherManager.INSTANCE.randomAESKey(); + private byte[] iv = CipherManager.INSTANCE.randomAESIV(); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); String token = getToken(); - if (Strings.isBlank(token)) { - RSAPublicKey publicKey = CredentialManager.INSTANCE.getPublicKey(); - HandShakeMessage message = new HandShakeMessage(); + if (!Strings.isBlank(token)) { + RSAPublicKey publicKey = CipherManager.INSTANCE.getPublicKey(); + HandshakeMessage message = new HandshakeMessage(); message.clientKey = clientKey; + message.iv = iv; message.clientVersion = "1.0.1"; message.deviceId = "1111111111111"; message.osName = "android"; @@ -44,10 +44,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { message.timestamp = System.currentTimeMillis(); Packet packet = new Packet(); - packet.command = Command.Handshake.cmd; - packet.version = 0; - packet.flags = 0; - packet.msgId = 1; + packet.cmd = Command.Handshake.cmd; + packet.sessionId = 1; packet.body = RSAUtils.encryptByPublicKey(Jsons.toJson(message).getBytes(Constants.UTF_8), publicKey); ctx.writeAndFlush(packet); } else { @@ -55,10 +53,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { message.deviceId = "1111111111111"; message.tokenId = token; Packet packet = new Packet(); - packet.command = Command.FastConnect.cmd; - packet.version = 0; - packet.flags = 0; - packet.msgId = 1; + packet.cmd = Command.FastConnect.cmd; + packet.sessionId = 1; packet.body = Jsons.toJson(message).getBytes(Constants.UTF_8); ctx.writeAndFlush(packet); } @@ -77,14 +73,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelRead", msg); if (msg instanceof Packet) { Packet packet = (Packet) msg; - Command command = Command.toCMD(packet.command); + Command command = Command.toCMD(packet.cmd); if (command == Command.Handshake) { - String raw = new String(DESUtils.decryptDES(packet.body, clientKey), Constants.UTF_8); - Map resp = Jsons.fromJson(raw, Map.class); + String raw = new String(AESUtils.decrypt(packet.body, clientKey, iv), Constants.UTF_8); + HandshakeSuccessMsg resp = Jsons.fromJson(raw, HandshakeSuccessMsg.class); LOGGER.info("hand shake success, message=" + raw); - String desKey = CryptoUtils.mixString(clientKey, (String) resp.get("serverKey")); - LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", desKey, clientKey, resp.get("serverKey")); - saveToken((String) resp.get("tokenId")); + byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, resp.serverKey); + LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, resp.serverKey); + saveToken(resp.sessionId); } else if (command == Command.FastConnect) { LOGGER.info("fast connect success, message=" + packet.getStringBody()); } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java index 21fe537d..03280543 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java @@ -31,88 +31,77 @@ * Created by ohun on 2015/12/24. */ public class ClientTest { - - private static final Logger log = LoggerFactory.getLogger(ClientTest.class); - @Test - public void testClient() { - String host = "127.0.0.1"; - int port = 3000; - EventLoopGroup workerGroup = new NioEventLoopGroup(); - try { - Bootstrap b = new Bootstrap(); // (1) - b.group(workerGroup); // (2) - b.channel(NioSocketChannel.class); // (3) - b.option(ChannelOption.SO_KEEPALIVE, true); // (4) - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(new ClientHandler()); - } - }); - ChannelFuture future = b.connect(host, port).sync(); // (5) - future.channel().closeFuture().sync(); - if (future.awaitUninterruptibly(4000) && future.isSuccess()) { - final Channel channel = future.channel(); + private static final Logger log = LoggerFactory.getLogger(ClientTest.class); - startHeartBeat(channel); - } else { - future.cancel(true); - future.channel().close(); - } - - log.error("for test"); + @Test + public void testClient() { + String host = "127.0.0.1"; + int port = 3000; + EventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap(); // (1) + b.group(workerGroup); // (2) + b.channel(NioSocketChannel.class); // (3) + b.option(ChannelOption.SO_KEEPALIVE, true); // (4) + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(new ClientHandler()); + } + }); + ChannelFuture future = b.connect(host, port).sync(); // (5) + if (future.awaitUninterruptibly(4000) && future.isSuccess()) { + startHeartBeat(future.channel()); + future.channel().closeFuture().sync(); + } else { + future.cancel(true); + future.channel().close(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + workerGroup.shutdownGracefully(); + } - } catch (Exception e) { - e.printStackTrace(); - } finally { - workerGroup.shutdownGracefully(); - } - - } - - public void startHeartBeat(final Channel channel){ - NettySharedHolder.timer.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try{ - final Packet packet = buildHeartBeat(); - ChannelFuture channelFuture = channel.writeAndFlush(packet); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if(!future.isSuccess()){ - if(!channel.isActive()){ - log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active"); - ConnectionManager.INSTANCE.remove(channel); - } - log.warn("client send msg false:"+channel.remoteAddress().toString()+","+packet); - }else{ - log.warn("client send msg success:"+channel.remoteAddress().toString()+","+packet); - } - } - }); - }finally{ - if(channel.isActive()){ - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - } - } - }, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - - private static Packet buildHeartBeat() { - Packet packet = new Packet(); - packet.command = Command.Heartbeat.cmd; - packet.version = 0; - packet.flags = 0; - packet.msgId = 1; - return packet; - } + } + public void startHeartBeat(final Channel channel) { + NettySharedHolder.timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + try { + final Packet packet = buildHeartBeat(); + ChannelFuture channelFuture = channel.writeAndFlush(packet); + channelFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + log.warn("client send msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); + ConnectionManager.INSTANCE.remove(channel); + } + log.warn("client send msg false:" + channel.remoteAddress().toString() + "," + packet); + } else { + log.debug("client send msg success:" + channel.remoteAddress().toString() + "," + packet); + } + } + }); + } finally { + if (channel.isActive()) { + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + } + } + }, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + private static Packet buildHeartBeat() { + Packet packet = new Packet(); + packet.cmd = Command.Heartbeat.cmd; + return packet; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java index 155ddc00..66f636da 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -15,7 +15,7 @@ public class MessageReceiver implements Receiver { public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); public static final MessageHandler BIND_HANDLER = new BindHandler(); public static final MessageHandler HEART_HANDLER = new HeartBeatHandler(); - public static final MessageHandler HAND_SHAKE_HANDLER = new HandShakeHandler(); + public static final MessageHandler HAND_SHAKE_HANDLER = new HandshakeHandler(); public static final MessageHandler FAST_CONNECT_HANDLER = new FastConnectHandler(); @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 55fa7dbb..aa506af6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -2,11 +2,10 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.message.FastConnectMessage; -import com.shinemo.mpush.core.security.ReusableToken; -import com.shinemo.mpush.core.security.ReusableTokenManager; +import com.shinemo.mpush.core.security.ReusableSession; +import com.shinemo.mpush.core.security.ReusableSessionManager; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; @@ -25,14 +24,13 @@ public FastConnectMessage decodeBody(Packet packet) { @Override public void handle(FastConnectMessage body, Request request) { - ReusableToken token = ReusableTokenManager.INSTANCE.getToken(body.tokenId); - if (token == null) { + ReusableSession session = ReusableSessionManager.INSTANCE.getSession(body.tokenId); + if (session == null) { request.getResponse().sendRaw("token expire".getBytes(Constants.UTF_8)); - } else if (!token.deviceId.equals(body.deviceId)) { + } else if (!session.sessionInfo.deviceId.equals(body.deviceId)) { request.getResponse().sendRaw("error device".getBytes(Constants.UTF_8)); } else { - SessionInfo info = new SessionInfo(token.osName, token.osVersion, token.clientVersion, token.deviceId, token.desKey); - request.getConnection().setSessionInfo(info); + request.getConnection().setSessionInfo(session.sessionInfo); Map resp = new HashMap(); resp.put("serverHost", MPushUtil.getLocalIp()); resp.put("serverTime", System.currentTimeMillis()); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 73ae059d..9769d9b2 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -2,23 +2,20 @@ import com.shinemo.mpush.api.Request; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.core.message.HeartBeatMessage; -import com.shinemo.mpush.core.message.LoginMessage; /** * Created by ohun on 2015/12/22. */ -public class HeartBeatHandler extends BaseMessageHandler { - +public class HeartBeatHandler extends BaseMessageHandler { + @Override - public HeartBeatMessage decodeBody(Packet packet) { + public Void decodeBody(Packet packet) { return null; } @Override - public void handle(HeartBeatMessage message, Request request) { - request.getResponse().send(new byte[]{}); + public void handle(Void message, Request request) { + System.err.println("receive client heartbeat, time=" + System.currentTimeMillis()); } - + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java index d47b3245..a89f82bf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java @@ -5,11 +5,12 @@ /** * Created by ohun on 2015/12/24. */ -public class HandShakeMessage implements Message { +public class HandshakeMessage implements Message { public String deviceId; public String osName; public String osVersion; public String clientVersion; - public String clientKey; + public byte[] clientKey; + public byte[] iv; public long timestamp; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java new file mode 100644 index 00000000..4ad73e52 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush.core.message; + +/** + * Created by ohun on 2015/12/27. + */ +public class HandshakeSuccessMsg { + public byte[] serverKey; + public String serverHost; + public long serverTime; + public int heartbeat; + public String sessionId; + public long expireTime; +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java deleted file mode 100644 index 4dea6303..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartBeatMessage.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.shinemo.mpush.core.message; - -import com.shinemo.mpush.api.Message; - -/** - * Created by ohun on 2015/12/22. - */ -public class HeartBeatMessage implements Message { - - - public HeartBeatMessage() { - - } - -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java index 7a4dc4bb..32318eb6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java @@ -17,7 +17,7 @@ public class NettyRequest implements Request{ public NettyRequest(Packet message, Connection connection) { this.message = message; this.connection = connection; - this.command = Command.toCMD(message.command); + this.command = Command.toCMD(message.cmd); } public Command getCommand() { @@ -34,9 +34,8 @@ public Connection getConnection() { public Response getResponse() { Packet packet = new Packet(); - packet.command = message.command; - packet.msgId = message.msgId; - packet.version = message.version; + packet.cmd = message.cmd; + packet.sessionId = message.sessionId; return new NettyResponse(packet, connection); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java index 9075fca3..2e1b1726 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java @@ -4,7 +4,7 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Response; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.crypto.DESUtils; +import com.shinemo.mpush.tools.crypto.AESUtils; /** * Created by ohun on 2015/12/22. @@ -19,7 +19,7 @@ public NettyResponse(Packet packet, Connection connection) { } public void send(byte[] body) { - packet.body = DESUtils.decryptDES(body, connection.getSessionInfo().desKey); + packet.body = AESUtils.decrypt(body, connection.getSessionInfo().sessionKey, connection.getSessionInfo().iv); connection.send(packet); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java new file mode 100644 index 00000000..8709669d --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.core.request; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.message.NettyRequest; + +/** + * Created by ohun on 2015/12/27. + */ +public class HandshakeRequest extends NettyRequest { + public String deviceId; + public String osName; + public String osVersion; + public String clientVersion; + public byte[] clientKey; + public long timestamp; + + public HandshakeRequest(Packet message, Connection connection) { + super(message, connection); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java similarity index 75% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java index d4af719e..95245fe4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CredentialManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java @@ -1,20 +1,23 @@ package com.shinemo.mpush.core.security; import com.shinemo.mpush.tools.Pair; +import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; +import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; /** * Created by ohun on 2015/12/24. */ -public class CredentialManager { - public static final CredentialManager INSTANCE = new CredentialManager(); +public class CipherManager { + public static final CipherManager INSTANCE = new CipherManager(); + private SecureRandom random = new SecureRandom(); + private RSAPrivateKey privateKey; private RSAPublicKey publicKey; @@ -83,4 +86,27 @@ public RSAPublicKey getPublicKey() { return INSTANCE.publicKey; } + public byte[] randomAESKey() { + byte[] bytes = new byte[AESUtils.AES_KEY_LENGTH]; + random.nextBytes(bytes); + return bytes; + } + + public byte[] randomAESIV() { + byte[] bytes = new byte[AESUtils.AES_KEY_LENGTH]; + random.nextBytes(bytes); + return bytes; + } + + public byte[] mixKey(byte[] clientKey, byte[] serverKey) { + byte[] sessionKey = new byte[AESUtils.AES_KEY_LENGTH]; + for (int i = 0; i < AESUtils.AES_KEY_LENGTH; i++) { + byte a = clientKey[i]; + byte b = serverKey[i]; + int sum = Math.abs(a + b); + byte c = (sum % 2 == 0) ? a : b; + sessionKey[i] = c; + } + return sessionKey; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java new file mode 100644 index 00000000..3ab7079b --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush.core.security; + +import com.shinemo.mpush.api.SessionInfo; + +/** + * Created by ohun on 2015/12/25. + */ +public class ReusableSession { + public transient String sessionId; + public long expireTime; + public SessionInfo sessionInfo; + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java similarity index 60% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java index 59e0b35d..e0312926 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableTokenManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java @@ -9,34 +9,30 @@ /** * Created by ohun on 2015/12/25. */ -public class ReusableTokenManager { - public static final ReusableTokenManager INSTANCE = new ReusableTokenManager(); +public class ReusableSessionManager { + public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; - private final Map tokenCache = new ConcurrentHashMap(); + private final Map tokenCache = new ConcurrentHashMap(); - public boolean saveToken(ReusableToken token) { - tokenCache.put(token.tokenId, token); + public boolean saveSession(ReusableSession session) { + tokenCache.put(session.sessionId, session); return true; } - public ReusableToken getToken(String tokenId) { - return tokenCache.get(tokenId); + public ReusableSession getSession(String sessionId) { + return tokenCache.get(sessionId); } - public ReusableToken genToken(SessionInfo info) { + public ReusableSession genSession(SessionInfo info) { /** * 先生成key,需要保证半个周期内同一个设备生成的key是相同的 */ long partition = System.currentTimeMillis() / (EXPIRE_TIME / 2);//把当前时间按照半个周期划分出一个当前所属于的区域 StringBuilder sb = new StringBuilder(); - sb.append(info.deviceId).append('_').append(partition).append("_R_S_T"); - ReusableToken v = new ReusableToken(); - v.tokenId = MD5Utils.encrypt(sb.toString()); - v.clientVersion = info.clientVersion; - v.deviceId = info.deviceId; - v.osName = info.osName; - v.osVersion = info.osVersion; - v.desKey = info.desKey; + sb.append(info.deviceId).append(partition); + ReusableSession v = new ReusableSession(); + v.sessionInfo = info; + v.sessionId = MD5Utils.encrypt(sb.toString()); /** * 计算失效时间 */ diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java deleted file mode 100644 index 0f20ca92..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableToken.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.shinemo.mpush.core.security; - -/** - * Created by ohun on 2015/12/25. - */ -public class ReusableToken { - public transient String tokenId; - public String deviceId; - public String osName; - public String osVersion; - public String clientVersion; - public String desKey; - public long expireTime; - -} diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java new file mode 100644 index 00000000..85864568 --- /dev/null +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java @@ -0,0 +1,30 @@ +package com.shinemo.mpush.core.security; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by ohun on 2015/12/25. + */ +public class CipherManagerTest { + + @Test + public void testGetPrivateKey() throws Exception { + CipherManager.INSTANCE.getPrivateKey(); + } + + @Test + public void testGetPublicKey() throws Exception { + for (int i = 0; i < 1000; i++) { + byte[] clientKey = CipherManager.INSTANCE.randomAESKey(); + byte[] serverKey = CipherManager.INSTANCE.randomAESKey(); + byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, serverKey); + System.out.println("clientKey:" + Arrays.toString(clientKey)); + System.out.println("serverKey:" + Arrays.toString(serverKey)); + System.out.println("sessionKey:" + Arrays.toString(sessionKey)); + + } + + } +} \ No newline at end of file diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java deleted file mode 100644 index 3e0c3f1f..00000000 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CredentialManagerTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.shinemo.mpush.core.security; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Created by ohun on 2015/12/25. - */ -public class CredentialManagerTest { - - @Test - public void testGetPrivateKey() throws Exception { - CredentialManager.INSTANCE.getPrivateKey(); - } - - @Test - public void testGetPublicKey() throws Exception { - - } -} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java new file mode 100644 index 00000000..654aba74 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java @@ -0,0 +1,53 @@ +package com.shinemo.mpush.tools.crypto; + +import com.shinemo.mpush.tools.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +/** + * Created by ohun on 2015/12/25. + */ +public final class AESUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(AESUtils.class); + public static final int AES_KEY_LENGTH = 16; + public static final String KEY_ALGORITHM = "AES"; + public static final String PKCS_ALGORITHM = "AES/CBC/PKCS5Padding"; + + public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { + return encrypt(data, 0, data.length, encryptKey, iv); + } + + public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { + return decrypt(data, 0, data.length, decryptKey, iv); + } + + public static byte[] encrypt(byte[] data, int offset, int length, byte[] encryptKey, byte[] iv) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); + try { + Cipher cipher = Cipher.getInstance(PKCS_ALGORITHM); + cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); + return cipher.doFinal(data, offset, length); + } catch (Exception e) { + LOGGER.error("encrypt ex, decryptKey=" + encryptKey, e); + } + return Constants.EMPTY_BYTES; + } + + public static byte[] decrypt(byte[] data, int offset, int length, byte[] decryptKey, byte[] iv) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + try { + Cipher cipher = Cipher.getInstance(PKCS_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); + return cipher.doFinal(data, offset, length); + } catch (Exception e) { + LOGGER.error("decrypt ex, decryptKey=" + decryptKey, e); + } + return Constants.EMPTY_BYTES; + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java new file mode 100644 index 00000000..f68d1b86 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java @@ -0,0 +1,1000 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ + +package com.shinemo.mpush.tools.crypto; + +import java.io.FilterOutputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Objects; + +/** + * This class consists exclusively of static methods for obtaining + * encoders and decoders for the Base64 encoding scheme. The + * implementation of this class supports the following types of Base64 + * as specified in + * RFC 4648 and + * RFC 2045. + * + *
    + *
  • Basic + *

    Uses "The Base64 Alphabet" as specified in Table 1 of + * RFC 4648 and RFC 2045 for encoding and decoding operation. + * The encoder does not add any line feed (line separator) + * character. The decoder rejects data that contains characters + * outside the base64 alphabet.

  • + * + *
  • URL and Filename safe + *

    Uses the "URL and Filename safe Base64 Alphabet" as specified + * in Table 2 of RFC 4648 for encoding and decoding. The + * encoder does not add any line feed (line separator) character. + * The decoder rejects data that contains characters outside the + * base64 alphabet.

  • + * + *
  • MIME + *

    Uses the "The Base64 Alphabet" as specified in Table 1 of + * RFC 2045 for encoding and decoding operation. The encoded output + * must be represented in lines of no more than 76 characters each + * and uses a carriage return {@code '\r'} followed immediately by + * a linefeed {@code '\n'} as the line separator. No line separator + * is added to the end of the encoded output. All line separators + * or other characters not found in the base64 alphabet table are + * ignored in decoding operation.

  • + *
+ * + *

Unless otherwise noted, passing a {@code null} argument to a + * method of this class will cause a {@link java.lang.NullPointerException + * NullPointerException} to be thrown. + * + * @author Xueming Shen + * @since 1.8 + */ + +public class Base64 { + + private Base64() {} + + /** + * Returns a {@link Encoder} that encodes using the + * Basic type base64 encoding scheme. + * + * @return A Base64 encoder. + */ + public static Encoder getEncoder() { + return Encoder.RFC4648; + } + + /** + * Returns a {@link Encoder} that encodes using the + * URL and Filename safe type base64 + * encoding scheme. + * + * @return A Base64 encoder. + */ + public static Encoder getUrlEncoder() { + return Encoder.RFC4648_URLSAFE; + } + + /** + * Returns a {@link Encoder} that encodes using the + * MIME type base64 encoding scheme. + * + * @return A Base64 encoder. + */ + public static Encoder getMimeEncoder() { + return Encoder.RFC2045; + } + + /** + * Returns a {@link Encoder} that encodes using the + * MIME type base64 encoding scheme + * with specified line length and line separators. + * + * @param lineLength + * the length of each output line (rounded down to nearest multiple + * of 4). If {@code lineLength <= 0} the output will not be separated + * in lines + * @param lineSeparator + * the line separator for each output line + * + * @return A Base64 encoder. + * + * @throws IllegalArgumentException if {@code lineSeparator} includes any + * character of "The Base64 Alphabet" as specified in Table 1 of + * RFC 2045. + */ + public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) { + Objects.requireNonNull(lineSeparator); + int[] base64 = Decoder.fromBase64; + for (byte b : lineSeparator) { + if (base64[b & 0xff] != -1) + throw new IllegalArgumentException( + "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); + } + if (lineLength <= 0) { + return Encoder.RFC4648; + } + return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); + } + + /** + * Returns a {@link Decoder} that decodes using the + * Basic type base64 encoding scheme. + * + * @return A Base64 decoder. + */ + public static Decoder getDecoder() { + return Decoder.RFC4648; + } + + /** + * Returns a {@link Decoder} that decodes using the + * URL and Filename safe type base64 + * encoding scheme. + * + * @return A Base64 decoder. + */ + public static Decoder getUrlDecoder() { + return Decoder.RFC4648_URLSAFE; + } + + /** + * Returns a {@link Decoder} that decodes using the + * MIME type base64 decoding scheme. + * + * @return A Base64 decoder. + */ + public static Decoder getMimeDecoder() { + return Decoder.RFC2045; + } + + /** + * This class implements an encoder for encoding byte data using + * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. + * + *

Instances of {@link Encoder} class are safe for use by + * multiple concurrent threads. + * + *

Unless otherwise noted, passing a {@code null} argument to + * a method of this class will cause a + * {@link java.lang.NullPointerException NullPointerException} to + * be thrown. + * + * @see Decoder + * @since 1.8 + */ + public static class Encoder { + + private final byte[] newline; + private final int linemax; + private final boolean isURL; + private final boolean doPadding; + + private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { + this.isURL = isURL; + this.newline = newline; + this.linemax = linemax; + this.doPadding = doPadding; + } + + /** + * This array is a lookup table that translates 6-bit positive integer + * index values into their "Base64 Alphabet" equivalents as specified + * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648). + */ + private static final char[] toBase64 = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' + }; + + /** + * It's the lookup table for "URL and Filename safe Base64" as specified + * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and + * '_'. This table is used when BASE64_URL is specified. + */ + private static final char[] toBase64URL = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' + }; + + private static final int MIMELINEMAX = 76; + private static final byte[] CRLF = new byte[] {'\r', '\n'}; + + static final Encoder RFC4648 = new Encoder(false, null, -1, true); + static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true); + static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true); + + private final int outLength(int srclen) { + int len = 0; + if (doPadding) { + len = 4 * ((srclen + 2) / 3); + } else { + int n = srclen % 3; + len = 4 * (srclen / 3) + (n == 0 ? 0 : n + 1); + } + if (linemax > 0) // line separators + len += (len - 1) / linemax * newline.length; + return len; + } + + /** + * Encodes all bytes from the specified byte array into a newly-allocated + * byte array using the {@link Base64} encoding scheme. The returned byte + * array is of the length of the resulting bytes. + * + * @param src + * the byte array to encode + * @return A newly-allocated byte array containing the resulting + * encoded bytes. + */ + public byte[] encode(byte[] src) { + int len = outLength(src.length); // dst array size + byte[] dst = new byte[len]; + int ret = encode0(src, 0, src.length, dst); + if (ret != dst.length) + return Arrays.copyOf(dst, ret); + return dst; + } + + /** + * Encodes all bytes from the specified byte array using the + * {@link Base64} encoding scheme, writing the resulting bytes to the + * given output byte array, starting at offset 0. + * + *

It is the responsibility of the invoker of this method to make + * sure the output byte array {@code dst} has enough space for encoding + * all bytes from the input byte array. No bytes will be written to the + * output byte array if the output byte array is not big enough. + * + * @param src + * the byte array to encode + * @param dst + * the output byte array + * @return The number of bytes written to the output byte array + * + * @throws IllegalArgumentException if {@code dst} does not have enough + * space for encoding all input bytes. + */ + public int encode(byte[] src, byte[] dst) { + int len = outLength(src.length); // dst array size + if (dst.length < len) + throw new IllegalArgumentException( + "Output byte array is too small for encoding all input bytes"); + return encode0(src, 0, src.length, dst); + } + + /** + * Encodes the specified byte array into a String using the {@link Base64} + * encoding scheme. + * + *

This method first encodes all input bytes into a base64 encoded + * byte array and then constructs a new String by using the encoded byte + * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1 + * ISO-8859-1} charset. + * + *

In other words, an invocation of this method has exactly the same + * effect as invoking + * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}. + * + * @param src + * the byte array to encode + * @return A String containing the resulting Base64 encoded characters + */ + @SuppressWarnings("deprecation") + public String encodeToString(byte[] src) { + byte[] encoded = encode(src); + return new String(encoded, 0, 0, encoded.length); + } + + /** + * Encodes all remaining bytes from the specified byte buffer into + * a newly-allocated ByteBuffer using the {@link Base64} encoding + * scheme. + * + * Upon return, the source buffer's position will be updated to + * its limit; its limit will not have been changed. The returned + * output buffer's position will be zero and its limit will be the + * number of resulting encoded bytes. + * + * @param buffer + * the source ByteBuffer to encode + * @return A newly-allocated byte buffer containing the encoded bytes. + */ + public ByteBuffer encode(ByteBuffer buffer) { + int len = outLength(buffer.remaining()); + byte[] dst = new byte[len]; + int ret = 0; + if (buffer.hasArray()) { + ret = encode0(buffer.array(), + buffer.arrayOffset() + buffer.position(), + buffer.arrayOffset() + buffer.limit(), + dst); + buffer.position(buffer.limit()); + } else { + byte[] src = new byte[buffer.remaining()]; + buffer.get(src); + ret = encode0(src, 0, src.length, dst); + } + if (ret != dst.length) + dst = Arrays.copyOf(dst, ret); + return ByteBuffer.wrap(dst); + } + + /** + * Wraps an output stream for encoding byte data using the {@link Base64} + * encoding scheme. + * + *

It is recommended to promptly close the returned output stream after + * use, during which it will flush all possible leftover bytes to the underlying + * output stream. Closing the returned output stream will close the underlying + * output stream. + * + * @param os + * the output stream. + * @return the output stream for encoding the byte data into the + * specified Base64 encoded format + */ + public OutputStream wrap(OutputStream os) { + Objects.requireNonNull(os); + return new EncOutputStream(os, isURL ? toBase64URL : toBase64, + newline, linemax, doPadding); + } + + /** + * Returns an encoder instance that encodes equivalently to this one, + * but without adding any padding character at the end of the encoded + * byte data. + * + *

The encoding scheme of this encoder instance is unaffected by + * this invocation. The returned encoder instance should be used for + * non-padding encoding operation. + * + * @return an equivalent encoder that encodes without adding any + * padding character at the end + */ + public Encoder withoutPadding() { + if (!doPadding) + return this; + return new Encoder(isURL, newline, linemax, false); + } + + private int encode0(byte[] src, int off, int end, byte[] dst) { + char[] base64 = isURL ? toBase64URL : toBase64; + int sp = off; + int slen = (end - off) / 3 * 3; + int sl = off + slen; + if (linemax > 0 && slen > linemax / 4 * 3) + slen = linemax / 4 * 3; + int dp = 0; + while (sp < sl) { + int sl0 = Math.min(sp + slen, sl); + for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) { + int bits = (src[sp0++] & 0xff) << 16 | + (src[sp0++] & 0xff) << 8 | + (src[sp0++] & 0xff); + dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f]; + dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f]; + dst[dp0++] = (byte)base64[(bits >>> 6) & 0x3f]; + dst[dp0++] = (byte)base64[bits & 0x3f]; + } + int dlen = (sl0 - sp) / 3 * 4; + dp += dlen; + sp = sl0; + if (dlen == linemax && sp < end) { + for (byte b : newline){ + dst[dp++] = b; + } + } + } + if (sp < end) { // 1 or 2 leftover bytes + int b0 = src[sp++] & 0xff; + dst[dp++] = (byte)base64[b0 >> 2]; + if (sp == end) { + dst[dp++] = (byte)base64[(b0 << 4) & 0x3f]; + if (doPadding) { + dst[dp++] = '='; + dst[dp++] = '='; + } + } else { + int b1 = src[sp++] & 0xff; + dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; + dst[dp++] = (byte)base64[(b1 << 2) & 0x3f]; + if (doPadding) { + dst[dp++] = '='; + } + } + } + return dp; + } + } + + /** + * This class implements a decoder for decoding byte data using the + * Base64 encoding scheme as specified in RFC 4648 and RFC 2045. + * + *

The Base64 padding character {@code '='} is accepted and + * interpreted as the end of the encoded byte data, but is not + * required. So if the final unit of the encoded byte data only has + * two or three Base64 characters (without the corresponding padding + * character(s) padded), they are decoded as if followed by padding + * character(s). If there is a padding character present in the + * final unit, the correct number of padding character(s) must be + * present, otherwise {@code IllegalArgumentException} ( + * {@code IOException} when reading from a Base64 stream) is thrown + * during decoding. + * + *

Instances of {@link Decoder} class are safe for use by + * multiple concurrent threads. + * + *

Unless otherwise noted, passing a {@code null} argument to + * a method of this class will cause a + * {@link java.lang.NullPointerException NullPointerException} to + * be thrown. + * + * @see Encoder + * @since 1.8 + */ + public static class Decoder { + + private final boolean isURL; + private final boolean isMIME; + + private Decoder(boolean isURL, boolean isMIME) { + this.isURL = isURL; + this.isMIME = isMIME; + } + + /** + * Lookup table for decoding unicode characters drawn from the + * "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into + * their 6-bit positive integer equivalents. Characters that + * are not in the Base64 alphabet but fall within the bounds of + * the array are encoded to -1. + * + */ + private static final int[] fromBase64 = new int[256]; + static { + Arrays.fill(fromBase64, -1); + for (int i = 0; i < Encoder.toBase64.length; i++) + fromBase64[Encoder.toBase64[i]] = i; + fromBase64['='] = -2; + } + + /** + * Lookup table for decoding "URL and Filename safe Base64 Alphabet" + * as specified in Table2 of the RFC 4648. + */ + private static final int[] fromBase64URL = new int[256]; + + static { + Arrays.fill(fromBase64URL, -1); + for (int i = 0; i < Encoder.toBase64URL.length; i++) + fromBase64URL[Encoder.toBase64URL[i]] = i; + fromBase64URL['='] = -2; + } + + static final Decoder RFC4648 = new Decoder(false, false); + static final Decoder RFC4648_URLSAFE = new Decoder(true, false); + static final Decoder RFC2045 = new Decoder(false, true); + + /** + * Decodes all bytes from the input byte array using the {@link Base64} + * encoding scheme, writing the results into a newly-allocated output + * byte array. The returned byte array is of the length of the resulting + * bytes. + * + * @param src + * the byte array to decode + * + * @return A newly-allocated byte array containing the decoded bytes. + * + * @throws IllegalArgumentException + * if {@code src} is not in valid Base64 scheme + */ + public byte[] decode(byte[] src) { + byte[] dst = new byte[outLength(src, 0, src.length)]; + int ret = decode0(src, 0, src.length, dst); + if (ret != dst.length) { + dst = Arrays.copyOf(dst, ret); + } + return dst; + } + + /** + * Decodes a Base64 encoded String into a newly-allocated byte array + * using the {@link Base64} encoding scheme. + * + *

An invocation of this method has exactly the same effect as invoking + * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))} + * + * @param src + * the string to decode + * + * @return A newly-allocated byte array containing the decoded bytes. + * + * @throws IllegalArgumentException + * if {@code src} is not in valid Base64 scheme + */ + public byte[] decode(String src) { + return decode(src.getBytes(StandardCharsets.ISO_8859_1)); + } + + /** + * Decodes all bytes from the input byte array using the {@link Base64} + * encoding scheme, writing the results into the given output byte array, + * starting at offset 0. + * + *

It is the responsibility of the invoker of this method to make + * sure the output byte array {@code dst} has enough space for decoding + * all bytes from the input byte array. No bytes will be be written to + * the output byte array if the output byte array is not big enough. + * + *

If the input byte array is not in valid Base64 encoding scheme + * then some bytes may have been written to the output byte array before + * IllegalargumentException is thrown. + * + * @param src + * the byte array to decode + * @param dst + * the output byte array + * + * @return The number of bytes written to the output byte array + * + * @throws IllegalArgumentException + * if {@code src} is not in valid Base64 scheme, or {@code dst} + * does not have enough space for decoding all input bytes. + */ + public int decode(byte[] src, byte[] dst) { + int len = outLength(src, 0, src.length); + if (dst.length < len) + throw new IllegalArgumentException( + "Output byte array is too small for decoding all input bytes"); + return decode0(src, 0, src.length, dst); + } + + /** + * Decodes all bytes from the input byte buffer using the {@link Base64} + * encoding scheme, writing the results into a newly-allocated ByteBuffer. + * + *

Upon return, the source buffer's position will be updated to + * its limit; its limit will not have been changed. The returned + * output buffer's position will be zero and its limit will be the + * number of resulting decoded bytes + * + *

{@code IllegalArgumentException} is thrown if the input buffer + * is not in valid Base64 encoding scheme. The position of the input + * buffer will not be advanced in this case. + * + * @param buffer + * the ByteBuffer to decode + * + * @return A newly-allocated byte buffer containing the decoded bytes + * + * @throws IllegalArgumentException + * if {@code src} is not in valid Base64 scheme. + */ + public ByteBuffer decode(ByteBuffer buffer) { + int pos0 = buffer.position(); + try { + byte[] src; + int sp, sl; + if (buffer.hasArray()) { + src = buffer.array(); + sp = buffer.arrayOffset() + buffer.position(); + sl = buffer.arrayOffset() + buffer.limit(); + buffer.position(buffer.limit()); + } else { + src = new byte[buffer.remaining()]; + buffer.get(src); + sp = 0; + sl = src.length; + } + byte[] dst = new byte[outLength(src, sp, sl)]; + return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst)); + } catch (IllegalArgumentException iae) { + buffer.position(pos0); + throw iae; + } + } + + /** + * Returns an input stream for decoding {@link Base64} encoded byte stream. + * + *

The {@code read} methods of the returned {@code InputStream} will + * throw {@code IOException} when reading bytes that cannot be decoded. + * + *

Closing the returned input stream will close the underlying + * input stream. + * + * @param is + * the input stream + * + * @return the input stream for decoding the specified Base64 encoded + * byte stream + */ + public InputStream wrap(InputStream is) { + Objects.requireNonNull(is); + return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME); + } + + private int outLength(byte[] src, int sp, int sl) { + int[] base64 = isURL ? fromBase64URL : fromBase64; + int paddings = 0; + int len = sl - sp; + if (len == 0) + return 0; + if (len < 2) { + if (isMIME && base64[0] == -1) + return 0; + throw new IllegalArgumentException( + "Input byte[] should at least have 2 bytes for base64 bytes"); + } + if (isMIME) { + // scan all bytes to fill out all non-alphabet. a performance + // trade-off of pre-scan or Arrays.copyOf + int n = 0; + while (sp < sl) { + int b = src[sp++] & 0xff; + if (b == '=') { + len -= (sl - sp + 1); + break; + } + if ((b = base64[b]) == -1) + n++; + } + len -= n; + } else { + if (src[sl - 1] == '=') { + paddings++; + if (src[sl - 2] == '=') + paddings++; + } + } + if (paddings == 0 && (len & 0x3) != 0) + paddings = 4 - (len & 0x3); + return 3 * ((len + 3) / 4) - paddings; + } + + private int decode0(byte[] src, int sp, int sl, byte[] dst) { + int[] base64 = isURL ? fromBase64URL : fromBase64; + int dp = 0; + int bits = 0; + int shiftto = 18; // pos of first byte of 4-byte atom + while (sp < sl) { + int b = src[sp++] & 0xff; + if ((b = base64[b]) < 0) { + if (b == -2) { // padding byte '=' + // = shiftto==18 unnecessary padding + // x= shiftto==12 a dangling single x + // x to be handled together with non-padding case + // xx= shiftto==6&&sp==sl missing last = + // xx=y shiftto==6 last is not = + if (shiftto == 6 && (sp == sl || src[sp++] != '=') || + shiftto == 18) { + throw new IllegalArgumentException( + "Input byte array has wrong 4-byte ending unit"); + } + break; + } + if (isMIME) // skip if for rfc2045 + continue; + else + throw new IllegalArgumentException( + "Illegal base64 character " + + Integer.toString(src[sp - 1], 16)); + } + bits |= (b << shiftto); + shiftto -= 6; + if (shiftto < 0) { + dst[dp++] = (byte)(bits >> 16); + dst[dp++] = (byte)(bits >> 8); + dst[dp++] = (byte)(bits); + shiftto = 18; + bits = 0; + } + } + // reached end of byte array or hit padding '=' characters. + if (shiftto == 6) { + dst[dp++] = (byte)(bits >> 16); + } else if (shiftto == 0) { + dst[dp++] = (byte)(bits >> 16); + dst[dp++] = (byte)(bits >> 8); + } else if (shiftto == 12) { + // dangling single "x", incorrectly encoded. + throw new IllegalArgumentException( + "Last unit does not have enough valid bits"); + } + // anything left is invalid, if is not MIME. + // if MIME, ignore all non-base64 character + while (sp < sl) { + if (isMIME && base64[src[sp++]] < 0) + continue; + throw new IllegalArgumentException( + "Input byte array has incorrect ending byte at " + sp); + } + return dp; + } + } + + /* + * An output stream for encoding bytes into the Base64. + */ + private static class EncOutputStream extends FilterOutputStream { + + private int leftover = 0; + private int b0, b1, b2; + private boolean closed = false; + + private final char[] base64; // byte->base64 mapping + private final byte[] newline; // line separator, if needed + private final int linemax; + private final boolean doPadding;// whether or not to pad + private int linepos = 0; + + EncOutputStream(OutputStream os, char[] base64, + byte[] newline, int linemax, boolean doPadding) { + super(os); + this.base64 = base64; + this.newline = newline; + this.linemax = linemax; + this.doPadding = doPadding; + } + + @Override + public void write(int b) throws IOException { + byte[] buf = new byte[1]; + buf[0] = (byte)(b & 0xff); + write(buf, 0, 1); + } + + private void checkNewline() throws IOException { + if (linepos == linemax) { + out.write(newline); + linepos = 0; + } + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (closed) + throw new IOException("Stream is closed"); + if (off < 0 || len < 0 || off + len > b.length) + throw new ArrayIndexOutOfBoundsException(); + if (len == 0) + return; + if (leftover != 0) { + if (leftover == 1) { + b1 = b[off++] & 0xff; + len--; + if (len == 0) { + leftover++; + return; + } + } + b2 = b[off++] & 0xff; + len--; + checkNewline(); + out.write(base64[b0 >> 2]); + out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); + out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]); + out.write(base64[b2 & 0x3f]); + linepos += 4; + } + int nBits24 = len / 3; + leftover = len - (nBits24 * 3); + while (nBits24-- > 0) { + checkNewline(); + int bits = (b[off++] & 0xff) << 16 | + (b[off++] & 0xff) << 8 | + (b[off++] & 0xff); + out.write(base64[(bits >>> 18) & 0x3f]); + out.write(base64[(bits >>> 12) & 0x3f]); + out.write(base64[(bits >>> 6) & 0x3f]); + out.write(base64[bits & 0x3f]); + linepos += 4; + } + if (leftover == 1) { + b0 = b[off++] & 0xff; + } else if (leftover == 2) { + b0 = b[off++] & 0xff; + b1 = b[off++] & 0xff; + } + } + + @Override + public void close() throws IOException { + if (!closed) { + closed = true; + if (leftover == 1) { + checkNewline(); + out.write(base64[b0 >> 2]); + out.write(base64[(b0 << 4) & 0x3f]); + if (doPadding) { + out.write('='); + out.write('='); + } + } else if (leftover == 2) { + checkNewline(); + out.write(base64[b0 >> 2]); + out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); + out.write(base64[(b1 << 2) & 0x3f]); + if (doPadding) { + out.write('='); + } + } + leftover = 0; + out.close(); + } + } + } + + /* + * An input stream for decoding Base64 bytes + */ + private static class DecInputStream extends InputStream { + + private final InputStream is; + private final boolean isMIME; + private final int[] base64; // base64 -> byte mapping + private int bits = 0; // 24-bit buffer for decoding + private int nextin = 18; // next available "off" in "bits" for input; + // -> 18, 12, 6, 0 + private int nextout = -8; // next available "off" in "bits" for output; + // -> 8, 0, -8 (no byte for output) + private boolean eof = false; + private boolean closed = false; + + DecInputStream(InputStream is, int[] base64, boolean isMIME) { + this.is = is; + this.base64 = base64; + this.isMIME = isMIME; + } + + private byte[] sbBuf = new byte[1]; + + @Override + public int read() throws IOException { + return read(sbBuf, 0, 1) == -1 ? -1 : sbBuf[0] & 0xff; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (closed) + throw new IOException("Stream is closed"); + if (eof && nextout < 0) // eof and no leftover + return -1; + if (off < 0 || len < 0 || len > b.length - off) + throw new IndexOutOfBoundsException(); + int oldOff = off; + if (nextout >= 0) { // leftover output byte(s) in bits buf + do { + if (len == 0) + return off - oldOff; + b[off++] = (byte)(bits >> nextout); + len--; + nextout -= 8; + } while (nextout >= 0); + bits = 0; + } + while (len > 0) { + int v = is.read(); + if (v == -1) { + eof = true; + if (nextin != 18) { + if (nextin == 12) + throw new IOException("Base64 stream has one un-decoded dangling byte."); + // treat ending xx/xxx without padding character legal. + // same logic as v == '=' below + b[off++] = (byte)(bits >> (16)); + len--; + if (nextin == 0) { // only one padding byte + if (len == 0) { // no enough output space + bits >>= 8; // shift to lowest byte + nextout = 0; + } else { + b[off++] = (byte) (bits >> 8); + } + } + } + if (off == oldOff) + return -1; + else + return off - oldOff; + } + if (v == '=') { // padding byte(s) + // = shiftto==18 unnecessary padding + // x= shiftto==12 dangling x, invalid unit + // xx= shiftto==6 && missing last '=' + // xx=y or last is not '=' + if (nextin == 18 || nextin == 12 || + nextin == 6 && is.read() != '=') { + throw new IOException("Illegal base64 ending sequence:" + nextin); + } + b[off++] = (byte)(bits >> (16)); + len--; + if (nextin == 0) { // only one padding byte + if (len == 0) { // no enough output space + bits >>= 8; // shift to lowest byte + nextout = 0; + } else { + b[off++] = (byte) (bits >> 8); + } + } + eof = true; + break; + } + if ((v = base64[v]) == -1) { + if (isMIME) // skip if for rfc2045 + continue; + else + throw new IOException("Illegal base64 character " + + Integer.toString(v, 16)); + } + bits |= (v << nextin); + if (nextin == 0) { + nextin = 18; // clear for next + nextout = 16; + while (nextout >= 0) { + b[off++] = (byte)(bits >> nextout); + len--; + nextout -= 8; + if (len == 0 && nextout >= 0) { // don't clean "bits" + return off - oldOff; + } + } + bits = 0; + } else { + nextin -= 6; + } + } + return off - oldOff; + } + + @Override + public int available() throws IOException { + if (closed) + throw new IOException("Stream is closed"); + return is.available(); // TBD: + } + + @Override + public void close() throws IOException { + if (!closed) { + closed = true; + is.close(); + } + } + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java index 455b1f3b..b6fa6dc5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.tools.crypto; + import com.shinemo.mpush.tools.Constants; -import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import java.io.*; @@ -12,41 +12,40 @@ *

* 依赖javabase64-1.3.1.jar *

- * */ public class Base64Utils { - + /** * 文件读取缓冲区大小 */ private static final int CACHE_SIZE = 1024; - + /** *

* BASE64字符串解码为二进制数据 *

- * + * * @param base64 * @return * @throws Exception */ public static byte[] decode(String base64) throws Exception { - return Base64.decode(base64); + return Base64.getDecoder().decode(base64); } - + /** *

* 二进制数据编码为BASE64字符串 *

- * + * * @param bytes * @return * @throws Exception */ public static String encode(byte[] bytes) throws Exception { - return Base64.encode(bytes); + return new String(Base64.getEncoder().encode(bytes), Constants.UTF_8); } - + /** *

* 将文件编码为BASE64字符串 @@ -54,7 +53,7 @@ public static String encode(byte[] bytes) throws Exception { *

* 大文件慎用,可能会导致内存溢出 *

- * + * * @param filePath 文件绝对路径 * @return * @throws Exception @@ -63,26 +62,26 @@ public static String encodeFile(String filePath) throws Exception { byte[] bytes = fileToByte(filePath); return encode(bytes); } - + /** *

* BASE64字符串转回文件 *

- * + * * @param filePath 文件绝对路径 - * @param base64 编码字符串 + * @param base64 编码字符串 * @throws Exception */ public static void decodeToFile(String filePath, String base64) throws Exception { byte[] bytes = decode(base64); byteArrayToFile(bytes, filePath); } - + /** *

* 文件转换为二进制数组 *

- * + * * @param filePath 文件路径 * @return * @throws Exception @@ -102,16 +101,16 @@ public static byte[] fileToByte(String filePath) throws Exception { out.close(); in.close(); data = out.toByteArray(); - } + } return data; } - + /** *

* 二进制数据写文件 *

- * - * @param bytes 二进制数据 + * + * @param bytes 二进制数据 * @param filePath 文件生成目录 */ public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { @@ -124,13 +123,13 @@ public static void byteArrayToFile(byte[] bytes, String filePath) throws Excepti OutputStream out = new FileOutputStream(destFile); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; - while ((nRead = in.read(cache)) != -1) { + while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } out.close(); in.close(); } - - + + } \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java index f02df257..e43b997b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java @@ -1,82 +1,8 @@ package com.shinemo.mpush.tools.crypto; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; - /** * Created by ohun on 2015/12/24. */ public class CryptoUtils { - public static final int[] DES_KEY_OFFSET_ARRAY = new int[]{12, 8, 4, 52, 33, 24, 12, 43, 5, 86, 79, 44, 21, 1, 66, 88}; - public static final int DES_KEY_SIZE = 8; - - public byte[] decode(byte[] data, PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { - Cipher c2 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); - c2.init(Cipher.DECRYPT_MODE, privateKey); - return c2.doFinal(data); - } - - public byte[] encode(byte[] data, PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { - Cipher c2 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); - c2.init(Cipher.DECRYPT_MODE, privateKey); - return c2.doFinal(data); - } - - - - public static String fill2Length(String str, int length) { - if (str.length() == length) { - return str; - } else if (str.length() < length) { - char[] cs = str.toCharArray(); - char[] fcs = new char[length]; - int less = length - cs.length; - System.arraycopy(cs, 0, fcs, less, length); - for (int i = less; i >= 0; i--) { - fcs[i] = '*'; - } - return new String(fcs); - } else { - char[] cs = str.toCharArray(); - char[] fcs = new char[length]; - System.arraycopy(cs, 0, fcs, 0, length); - return new String(fcs); - } - } - - public static String mixString(String a, String b) { - char[] charsA = fill2Length(a, DES_KEY_SIZE).toCharArray(); - char[] charsB = fill2Length(b, DES_KEY_SIZE).toCharArray(); - char[] charsC = new char[DES_KEY_SIZE]; - for (int i = 0; i < DES_KEY_SIZE; i++) { - char charA = charsA[i]; - char charB = charsB[i]; - int sum = charA + charB; - int offset = DES_KEY_OFFSET_ARRAY[sum % DES_KEY_OFFSET_ARRAY.length]; - boolean chooseA = (sum % 2 == 0); - boolean upOrDown = (charA % 2 == 0); - char charC; - if (chooseA) { - charC = charA; - } else { - charC = charB; - } - if (upOrDown) { - offset = -offset; - } - int result = charC + offset; - if (result > 126 || result < 33) { - result = 33 + Math.abs(result % (126 - 33)); - } - charC = (char) result; - charsC[i] = charC; - } - return new String(charsC); - } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java deleted file mode 100644 index 9266696b..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/DESUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.shinemo.mpush.tools.crypto; - -import com.shinemo.mpush.tools.Constants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -/** - * Created by ohun on 2015/12/25. - */ -public final class DESUtils { - private static final Logger LOGGER = LoggerFactory.getLogger(DESUtils.class); - private static byte[] iv = {8, 7, 6, 5, 5, 6, 7, 8}; - - public static byte[] encryptDES(byte[] encryptBytes, String encryptKey) { - return encryptDES(encryptBytes, 0, encryptBytes.length, encryptKey); - } - - public static byte[] decryptDES(byte[] byteMi, String decryptKey) { - return decryptDES(byteMi, 0, byteMi.length, decryptKey); - } - - public static byte[] encryptDES(byte[] encryptBytes, int offset, int length, String encryptKey) { - IvParameterSpec zeroIv = new IvParameterSpec(iv); - SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), "DES"); - try { - Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); - return cipher.doFinal(encryptBytes, offset, length); - } catch (Exception e) { - LOGGER.error("encryptDES ex, decryptKey=" + encryptKey, e); - } - return Constants.EMPTY_BYTES; - } - - public static byte[] decryptDES(byte[] byteMi, int offset, int length, String decryptKey) { - IvParameterSpec zeroIv = new IvParameterSpec(iv); - SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(Constants.UTF_8), "DES"); - try { - Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); - cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); - return cipher.doFinal(byteMi, offset, length); - } catch (Exception e) { - LOGGER.error("decryptDES ex, decryptKey=" + decryptKey, e); - } - return Constants.EMPTY_BYTES; - } -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index bbb7935c..fed66135 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -37,6 +37,7 @@ public class RSAUtils { * 加密算法RSA */ public static final String KEY_ALGORITHM = "RSA"; + public static final String KEY_ALGORITHM_PADDING = "RSA/ECB/PKCS1Padding"; /** * 签名算法 @@ -182,7 +183,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { */ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { try { - Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 模长 int key_len = publicKey.getModulus().bitLength() / 8; @@ -205,7 +206,7 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { */ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) { try { - Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.DECRYPT_MODE, privateKey); //模长 int key_len = privateKey.getModulus().bitLength() / 8; @@ -250,9 +251,8 @@ private static byte[] doFinal(Cipher cipher, byte[] data, int key_len) throws Ba * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception { - KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey key = decodePrivateKey(privateKey); - Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.DECRYPT_MODE, key); return doFinal(cipher, data, MAX_DECRYPT_BLOCK); } @@ -269,7 +269,7 @@ public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws */ public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Exception { PublicKey key = decodePublicKey(publicKey); - Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.DECRYPT_MODE, key); return doFinal(cipher, data, MAX_DECRYPT_BLOCK); } @@ -287,7 +287,7 @@ public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Ex public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { PublicKey key = decodePublicKey(publicKey); // 对数据加密 - Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.ENCRYPT_MODE, key); return doFinal(cipher, data, MAX_ENCRYPT_BLOCK); } @@ -304,7 +304,7 @@ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Ex */ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception { PrivateKey key = decodePrivateKey(privateKey); - Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.ENCRYPT_MODE, key); return doFinal(cipher, data, MAX_ENCRYPT_BLOCK); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java similarity index 69% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java rename to mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java index b8aee148..0f482419 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/DESUtilsTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java @@ -4,21 +4,22 @@ import org.apache.commons.lang3.RandomStringUtils; import org.junit.Test; -import static org.junit.Assert.*; +import java.util.Random; /** * Created by ohun on 2015/12/25. */ -public class DESUtilsTest { +public class AESUtilsTest { @Test public void testEncryptDES() throws Exception { String data = "似的士大夫士大夫士大夫首发式发生士大夫"; System.out.println("原文:\n" + data); - String key = RandomStringUtils.randomAscii(8); - byte[] d1 = DESUtils.encryptDES(data.getBytes(Constants.UTF_8), key); + byte[] key = new byte[AESUtils.AES_KEY_LENGTH]; + new Random().nextBytes(key); + byte[] d1 = AESUtils.encrypt(data.getBytes(Constants.UTF_8), key,key); System.out.println("加密后:\n" + new String(d1)); - byte[] d2 = DESUtils.decryptDES(d1, key); + byte[] d2 = AESUtils.decrypt(d1, key,key); System.out.println("解密后:\n" + new String(d2, Constants.UTF_8)); } From 5e69daa144c3062e849222ec6d7ba65765d82d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 27 Dec 2015 05:42:33 +0000 Subject: [PATCH 029/890] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E5=92=8C=E5=8E=8B=E7=BC=A9flags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Constants.java | 3 + .../java/com/shinemo/mpush/api/Request.java | 2 +- .../shinemo/mpush/api/protocol/Packet.java | 8 +++ .../connection/netty/codec/PacketDecoder.java | 1 - .../mpush/connection/ClientHandler.java | 1 - .../core/handler/BaseMessageHandler.java | 5 +- .../mpush/core/handler/BindHandler.java | 4 +- .../core/handler/FastConnectHandler.java | 4 +- .../mpush/core/handler/HandShakeHandler.java | 64 ++++++++--------- .../mpush/core/handler/HeartBeatHandler.java | 2 +- .../mpush/core/handler/LoginHandler.java | 2 +- .../mpush/core/message/NettyRequest.java | 42 ++++++++--- .../mpush/core/message/NettyResponse.java | 19 ++++- .../mpush/core/security/CipherManager.java | 4 +- .../core/security/CipherManagerTest.java | 4 +- .../java/com/shinemo/mpush/tools/IOUtils.java | 40 +++++++++++ .../java/com/shinemo/mpush/tools/Jsons.java | 4 ++ .../shinemo/mpush/tools/crypto/AESUtils.java | 21 ++---- .../mpush/tools/crypto/CryptoUtils.java | 8 --- .../shinemo/mpush/tools/crypto/RSAUtils.java | 70 ++++++++++++------- 20 files changed, 199 insertions(+), 109 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 9b5bd742..1f1cbad9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -12,6 +12,9 @@ public interface Constants { int HEADER_LEN = 13; short MAGIC_NUM = 1122; byte HB = '\n'; + int COMPRESS_LIMIT = 1024 * 10; + byte CRYPTO_FLAG = 0x01; + byte COMPRESS_FLAG = 0x02; long TIME_DELAY = 1L; String JVM_LOG_PATH = "/opt/"; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java index f18e778f..74b337ef 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java @@ -10,7 +10,7 @@ public interface Request { Command getCommand(); - Packet getMessage(); + byte[] getBody(); Connection getConnection(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index fc982c06..d1125949 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -27,6 +27,14 @@ public String getStringBody() { return body == null ? "" : new String(body, Constants.UTF_8); } + public void setFlag(byte flag) { + this.flags |= flag; + } + + public boolean hasFlag(byte flag) { + return (flags & flag) != 0; + } + @Override public String toString() { return "Packet{" + diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java index 859f79fe..d747a77c 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java +++ b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java @@ -83,5 +83,4 @@ private Packet readPacket(ByteBuf in, int bodyLength) { packet.body = body; return packet; } - } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java index eb0e45d8..555c480d 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java @@ -9,7 +9,6 @@ import com.shinemo.mpush.core.security.CipherManager; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.Strings; -import com.shinemo.mpush.tools.crypto.CryptoUtils; import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; import io.netty.channel.ChannelHandlerAdapter; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java index 9b017962..d315ae13 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java @@ -3,7 +3,6 @@ import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. @@ -11,11 +10,11 @@ public abstract class BaseMessageHandler implements MessageHandler { @Override public void handle(Request request) { - T t = decodeBody(request.getMessage()); + T t = decodeBody(request.getBody()); handle(t, request); } - public abstract T decodeBody(Packet packet); + public abstract T decodeBody(byte[] data); public abstract void handle(T body, Request request); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java index a896fefe..94ac5ba0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java @@ -10,8 +10,8 @@ */ public class BindHandler extends BaseMessageHandler { @Override - public String decodeBody(Packet packet) { - return new String(packet.body, Constants.UTF_8); + public String decodeBody(byte[] body) { + return new String(body, Constants.UTF_8); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index aa506af6..c5543d96 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -18,8 +18,8 @@ */ public class FastConnectHandler extends BaseMessageHandler { @Override - public FastConnectMessage decodeBody(Packet packet) { - return Jsons.fromJson(packet.getStringBody(), FastConnectMessage.class); + public FastConnectMessage decodeBody(byte[] body) { + return Jsons.fromJson(body, FastConnectMessage.class); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 9eceab4a..47fa7dc4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -1,59 +1,57 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.security.CredentialManager; -import com.shinemo.mpush.core.security.ReusableToken; -import com.shinemo.mpush.core.security.ReusableTokenManager; +import com.shinemo.mpush.core.message.HandshakeMessage; +import com.shinemo.mpush.core.message.HandshakeSuccessMsg; +import com.shinemo.mpush.core.security.CipherManager; +import com.shinemo.mpush.core.security.ReusableSession; +import com.shinemo.mpush.core.security.ReusableSessionManager; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.crypto.CryptoUtils; -import com.shinemo.mpush.tools.crypto.DESUtils; +import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; -import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.Serializable; import java.security.interfaces.RSAPrivateKey; -import java.util.HashMap; -import java.util.Map; /** * Created by ohun on 2015/12/24. */ -public class HandShakeHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); +public class HandshakeHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); @Override - public HandShakeMessage decodeBody(Packet packet) { - RSAPrivateKey privateKey = CredentialManager.INSTANCE.getPrivateKey(); - byte[] rawData = RSAUtils.decryptByPrivateKey(packet.body, privateKey); - return Jsons.fromJson(new String(rawData, Constants.UTF_8), HandShakeMessage.class); + public HandshakeMessage decodeBody(byte[] body) { + RSAPrivateKey privateKey = CipherManager.INSTANCE.getPrivateKey(); + byte[] rawData = RSAUtils.decryptByPrivateKey(body, privateKey); + return Jsons.fromJson(new String(rawData, Constants.UTF_8), HandshakeMessage.class); } @Override - public void handle(HandShakeMessage body, Request request) { - String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); - String clientKey = body.clientKey; - String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥 - SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); + public void handle(HandshakeMessage body, Request request) { + byte[] iv = body.iv; + byte[] clientKey = body.clientKey; + byte[] serverKey = CipherManager.INSTANCE.randomAESKey(); + byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, serverKey);//会话密钥混淆 Client random + SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, + body.deviceId, sessionKey, iv); request.getConnection().setSessionInfo(info); - ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info); - ReusableTokenManager.INSTANCE.saveToken(token); - Map resp = new HashMap(); - resp.put("serverKey", serverKey); - resp.put("serverHost", MPushUtil.getLocalIp()); - resp.put("serverTime", System.currentTimeMillis()); - resp.put("heartbeat", Constants.HEARTBEAT_TIME); - resp.put("tokenId", token.tokenId); - resp.put("tokenExpire", token.expireTime); - byte[] responseData = DESUtils.encryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey); + ReusableSession session = ReusableSessionManager.INSTANCE.genSession(info); + ReusableSessionManager.INSTANCE.saveSession(session); + HandshakeSuccessMsg resp = new HandshakeSuccessMsg(); + resp.serverKey = serverKey; + resp.serverHost = MPushUtil.getLocalIp(); + resp.serverTime = System.currentTimeMillis(); + resp.heartbeat = Constants.HEARTBEAT_TIME; + resp.sessionId = session.sessionId; + resp.expireTime = session.expireTime; + byte[] responseData = AESUtils.encrypt(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey, iv); request.getResponse().sendRaw(responseData); - LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", desKey, clientKey, serverKey); + LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 9769d9b2..efeb3f8e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -9,7 +9,7 @@ public class HeartBeatHandler extends BaseMessageHandler { @Override - public Void decodeBody(Packet packet) { + public Void decodeBody(byte[] body) { return null; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java index 347d7d80..da870175 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java @@ -9,7 +9,7 @@ */ public class LoginHandler extends BaseMessageHandler { @Override - public LoginMessage decodeBody(Packet packet) { + public LoginMessage decodeBody(byte[] body) { return new LoginMessage(); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java index 32318eb6..685eccc8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java @@ -1,31 +1,51 @@ package com.shinemo.mpush.core.message; -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.Response; +import com.shinemo.mpush.api.*; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.IOUtils; +import com.shinemo.mpush.tools.crypto.AESUtils; /** * Created by ohun on 2015/12/22. */ -public class NettyRequest implements Request{ - private final Command command; +public class NettyRequest implements Request { private final Packet message; private final Connection connection; + private Command command; + private byte[] body; public NettyRequest(Packet message, Connection connection) { this.message = message; this.connection = connection; - this.command = Command.toCMD(message.cmd); } public Command getCommand() { - return command; + return command == null ? command = Command.toCMD(message.cmd) : command; } - public Packet getMessage() { - return message; + public byte[] getBody() { + if (message.body == null) return null; + if (body == null) { + //1.解密 + byte[] tmp = message.body; + if (message.hasFlag(Constants.CRYPTO_FLAG)) { + SessionInfo info = connection.getSessionInfo(); + if (info != null && info.sessionKey != null) { + tmp = AESUtils.decrypt(tmp, info.sessionKey, info.iv); + } + } + + //2.解压 + if (message.hasFlag(Constants.COMPRESS_FLAG)) { + byte[] result = IOUtils.uncompress(tmp); + if (result.length > 0) { + tmp = result; + } + } + this.body = tmp; + } + return body; } public Connection getConnection() { @@ -34,8 +54,8 @@ public Connection getConnection() { public Response getResponse() { Packet packet = new Packet(); - packet.cmd = message.cmd; - packet.sessionId = message.sessionId; + packet.cmd = this.message.cmd; + packet.sessionId = this.message.sessionId; return new NettyResponse(packet, connection); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java index 2e1b1726..ff8acd5e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java @@ -3,7 +3,9 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Response; +import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.IOUtils; import com.shinemo.mpush.tools.crypto.AESUtils; /** @@ -19,7 +21,22 @@ public NettyResponse(Packet packet, Connection connection) { } public void send(byte[] body) { - packet.body = AESUtils.decrypt(body, connection.getSessionInfo().sessionKey, connection.getSessionInfo().iv); + byte[] tmp = body; + //1.压缩 + if (tmp.length > Constants.COMPRESS_LIMIT) { + byte[] result = IOUtils.compress(tmp); + if (result.length > 0) { + tmp = result; + packet.setFlag(Constants.COMPRESS_FLAG); + } + } + //2.加密 + SessionInfo info = connection.getSessionInfo(); + if (info != null && info.sessionKey != null) { + tmp = AESUtils.encrypt(tmp, info.sessionKey, info.iv); + packet.setFlag(Constants.CRYPTO_FLAG); + } + packet.body = tmp; connection.send(packet); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java index 95245fe4..d7f0507b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java @@ -104,8 +104,8 @@ public byte[] mixKey(byte[] clientKey, byte[] serverKey) { byte a = clientKey[i]; byte b = serverKey[i]; int sum = Math.abs(a + b); - byte c = (sum % 2 == 0) ? a : b; - sessionKey[i] = c; + int c = (sum % 2 == 0) ? a ^ b : b ^ a; + sessionKey[i] = (byte) c; } return sessionKey; } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java index 85864568..ee563abd 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java @@ -20,8 +20,8 @@ public void testGetPublicKey() throws Exception { byte[] clientKey = CipherManager.INSTANCE.randomAESKey(); byte[] serverKey = CipherManager.INSTANCE.randomAESKey(); byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, serverKey); - System.out.println("clientKey:" + Arrays.toString(clientKey)); - System.out.println("serverKey:" + Arrays.toString(serverKey)); + //System.out.println("clientKey:" + Arrays.toString(clientKey)); + //System.out.println("serverKey:" + Arrays.toString(serverKey)); System.out.println("sessionKey:" + Arrays.toString(sessionKey)); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java index 9c337898..90569e35 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java @@ -3,7 +3,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.Closeable; +import java.io.IOException; +import java.util.zip.DeflaterOutputStream; +import java.util.zip.InflaterInputStream; /** * Created by ohun on 2015/12/25. @@ -20,4 +25,39 @@ public static void close(Closeable closeable) { } } } + + + public static byte[] compress(byte[] data) { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + DeflaterOutputStream zipOut = new DeflaterOutputStream(byteStream); + try { + zipOut.write(data); + zipOut.finish(); + zipOut.close(); + } catch (IOException e) { + LOGGER.error("compress ex", e); + return Constants.EMPTY_BYTES; + } finally { + close(zipOut); + } + return byteStream.toByteArray(); + } + + public static byte[] uncompress(byte[] data) { + InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(data)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[data.length * 2]; + int length; + try { + while ((length = in.read(buffer)) != -1) { + out.write(buffer, 0, length); + } + } catch (IOException e) { + LOGGER.error("uncompress ex", e); + return Constants.EMPTY_BYTES; + } finally { + close(in); + } + return out.toByteArray(); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index 47e7c89f..3b79d65f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -36,6 +36,10 @@ public static T fromJson(String json, Class clazz) { return null; } + public static T fromJson(byte[] json, Class clazz) { + return fromJson(new String(json, Constants.UTF_8), clazz); + } + public static T fromJson(String json, Type type) { try { return GSON.fromJson(json, type); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java index 654aba74..1e4dd370 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java @@ -15,36 +15,29 @@ public final class AESUtils { private static final Logger LOGGER = LoggerFactory.getLogger(AESUtils.class); public static final int AES_KEY_LENGTH = 16; public static final String KEY_ALGORITHM = "AES"; - public static final String PKCS_ALGORITHM = "AES/CBC/PKCS5Padding"; + public static final String KEY_ALGORITHM_PADDING = "AES/CBC/PKCS5Padding"; - public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { - return encrypt(data, 0, data.length, encryptKey, iv); - } - public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { - return decrypt(data, 0, data.length, decryptKey, iv); - } - - public static byte[] encrypt(byte[] data, int offset, int length, byte[] encryptKey, byte[] iv) { + public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); try { - Cipher cipher = Cipher.getInstance(PKCS_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); - return cipher.doFinal(data, offset, length); + return cipher.doFinal(data); } catch (Exception e) { LOGGER.error("encrypt ex, decryptKey=" + encryptKey, e); } return Constants.EMPTY_BYTES; } - public static byte[] decrypt(byte[] data, int offset, int length, byte[] decryptKey, byte[] iv) { + public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); try { - Cipher cipher = Cipher.getInstance(PKCS_ALGORITHM); + Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); - return cipher.doFinal(data, offset, length); + return cipher.doFinal(data); } catch (Exception e) { LOGGER.error("decrypt ex, decryptKey=" + decryptKey, e); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java deleted file mode 100644 index e43b997b..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/CryptoUtils.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.shinemo.mpush.tools.crypto; - -/** - * Created by ohun on 2015/12/24. - */ -public class CryptoUtils { - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index fed66135..15842347 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -20,12 +20,7 @@ import java.security.spec.X509EncodedKeySpec; /** - * *

* RSA公钥/私钥/签名工具包 - *

- *

- * 罗纳德·李维斯特(Ron [R]ivest)、阿迪·萨莫尔(Adi [S]hamir)和伦纳德·阿德曼(Leonard [A]dleman) - *

*

* 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式
* 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,
@@ -33,10 +28,20 @@ */ public class RSAUtils { private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class); + + /** + * 密钥位数 + */ + private static final int RAS_KEY_SIZE = 1024; + /** * 加密算法RSA */ public static final String KEY_ALGORITHM = "RSA"; + + /** + * 填充方式 + */ public static final String KEY_ALGORITHM_PADDING = "RSA/ECB/PKCS1Padding"; /** @@ -45,14 +50,15 @@ public class RSAUtils { public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; /** - * RSA最大加密明文大小 + * RSA最大解密密文大小 */ - private static final int MAX_ENCRYPT_BLOCK = 117; + private static final int MAX_DECRYPT_BLOCK = 128; /** - * RSA最大解密密文大小 + * RSA最大加密明文大小 */ - private static final int MAX_DECRYPT_BLOCK = 128; + private static final int MAX_ENCRYPT_BLOCK = 128 - 11; + /** * 生成公钥和私钥 @@ -62,7 +68,7 @@ public class RSAUtils { public static Pair genKeyPair() { try { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); - keyPairGen.initialize(1024); + keyPairGen.initialize(RAS_KEY_SIZE); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); @@ -73,11 +79,24 @@ public static Pair genKeyPair() { return null; } - + /** + * 编码密钥,便于存储 + * + * @param key + * @return + * @throws Exception + */ public static String encodeBase64(Key key) throws Exception { return Base64Utils.encode(key.getEncoded()); } + /** + * 从字符串解码私钥 + * + * @param key + * @return + * @throws Exception + */ public static PrivateKey decodePrivateKey(String key) throws Exception { byte[] keyBytes = Base64Utils.decode(key); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); @@ -85,6 +104,13 @@ public static PrivateKey decodePrivateKey(String key) throws Exception { return keyFactory.generatePrivate(pkcs8KeySpec); } + /** + * 从字符串解码公钥 + * + * @param publicKey + * @return + * @throws Exception + */ public static PublicKey decodePublicKey(String publicKey) throws Exception { byte[] keyBytes = Base64Utils.decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); @@ -94,9 +120,7 @@ public static PublicKey decodePublicKey(String publicKey) throws Exception { /** - *

* 用私钥对信息生成数字签名 - *

* * @param data 已加密数据 * @param privateKey 私钥(BASE64编码) @@ -111,9 +135,7 @@ public static String sign(byte[] data, String privateKey) throws Exception { } /** - *

* 校验数字签名 - *

* * @param data 已加密数据 * @param publicKey 公钥(BASE64编码) @@ -131,7 +153,8 @@ public static boolean verify(byte[] data, String publicKey, String sign) throws /** * 使用模和指数生成RSA公钥 - * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA + * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding, + * 不同JDK默认的补位方式可能不同,如Android默认是RSA * /None/NoPadding】 * * @param modulus 模 @@ -153,7 +176,8 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { /** * 使用模和指数生成RSA私钥 - * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA + * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding, + * 不同JDK默认的补位方式可能不同,如Android默认是RSA * /None/NoPadding】 * * @param modulus 模 @@ -219,7 +243,9 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) } /** - * 注意:RSA加密明文最大长度117字节,解密要求密文最大长度为128字节,所以在加密和解密的过程中需要分块进行。 + * 注意:RSA加密明文最大长度117字节, + * 解密要求密文最大长度为128字节, + * 所以在加密和解密的过程中需要分块进行。 * * @param cipher * @param data @@ -241,9 +267,7 @@ private static byte[] doFinal(Cipher cipher, byte[] data, int key_len) throws Ba } /** - *

* 私钥解密 - *

* * @param data 已加密数据 * @param privateKey 私钥(BASE64编码) @@ -258,9 +282,7 @@ public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws } /** - *

* 公钥解密 - *

* * @param data 已加密数据 * @param publicKey 公钥(BASE64编码) @@ -275,9 +297,7 @@ public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Ex } /** - *

* 公钥加密 - *

* * @param data 源数据 * @param publicKey 公钥(BASE64编码) @@ -293,9 +313,7 @@ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Ex } /** - *

* 私钥加密 - *

* * @param data 源数据 * @param privateKey 私钥(BASE64编码) From cb730a644acc25a62caba45e77db478a4eed091c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 27 Dec 2015 17:12:34 +0800 Subject: [PATCH 030/890] =?UTF-8?q?=E6=8A=BD=E5=8F=96netty=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/api/protocol/Handler.java | 30 +++++ .../shinemo/mpush/api/protocol/Packet.java | 4 +- .../shinemo/mpush/api/protocol/Protocol.java | 7 ++ mpush-client/.gitignore | 1 + mpush-client/pom.xml | 2 +- .../mpush/connection/ConnectionHandler.java | 50 -------- mpush-core/pom.xml | 4 + .../shinemo/mpush/core/MessageReceiver.java | 2 +- .../mpush/core/handler/HandShakeHandler.java | 5 +- .../mpush/core/handler/ServerHandler.java | 43 +++++-- .../mpush/core/message/HandShakeMessage.java | 2 +- .../core}/task/ScanAllClientConnection.java | 5 +- .../shinemo/mpush/core}/task/ScanTask.java | 2 +- .../mpush/core/netty}/ClientHandler.java | 44 +++++-- .../mpush/core/netty/NettyClientTest.java | 20 ++-- .../mpush/core/netty/NettyServerTest.java | 15 ++- mpush-core/src/test/resources/logback.xml | 30 +++++ mpush-netty/pom.xml | 51 ++++++++ .../mpush/netty/client/NettyClient.java | 5 + .../mpush}/netty/codec/PacketDecoder.java | 2 +- .../mpush}/netty/codec/PacketEncoder.java | 2 +- .../mpush/netty/server/NettyServer.java | 28 ++--- .../mpush/netty/util/NettySharedHandler.java | 110 ++++++++++++++++++ .../mpush/netty/util}/NettySharedHolder.java | 7 +- mpush-netty/src/test/resources/logback.xml | 30 +++++ .../com/shinemo/mpush/tools/Constants.java | 12 ++ .../shinemo/mpush/tools}/InetAddressUtil.java | 4 +- .../com/shinemo/mpush/tools}/JVMUtil.java | 2 +- .../tools}/thread/NamedThreadFactory.java | 2 +- .../mpush/tools}/thread/ThreadNameSpace.java | 2 +- .../tools}/thread/ThreadPoolManager.java | 7 +- .../mpush/tools}/thread/ThreadPoolUtil.java | 5 +- pom.xml | 8 +- 33 files changed, 421 insertions(+), 122 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java create mode 100644 mpush-client/.gitignore delete mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java rename mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java => mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java (63%) rename {mpush-connection/src/main/java/com/shinemo/mpush/connection/netty => mpush-core/src/main/java/com/shinemo/mpush/core}/task/ScanAllClientConnection.java (90%) rename {mpush-connection/src/main/java/com/shinemo/mpush/connection/netty => mpush-core/src/main/java/com/shinemo/mpush/core}/task/ScanTask.java (81%) rename {mpush-connection/src/test/java/com/shinemo/mpush/connection => mpush-core/src/test/java/com/shinemo/mpush/core/netty}/ClientHandler.java (82%) rename mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java => mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java (85%) rename mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java => mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java (50%) create mode 100644 mpush-core/src/test/resources/logback.xml create mode 100644 mpush-netty/pom.xml create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java rename {mpush-connection/src/main/java/com/shinemo/mpush/connection => mpush-netty/src/main/java/com/shinemo/mpush}/netty/codec/PacketDecoder.java (98%) rename {mpush-connection/src/main/java/com/shinemo/mpush/connection => mpush-netty/src/main/java/com/shinemo/mpush}/netty/codec/PacketEncoder.java (95%) rename mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java => mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java (87%) create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java rename {mpush-connection/src/main/java/com/shinemo/mpush/connection/netty => mpush-netty/src/main/java/com/shinemo/mpush/netty/util}/NettySharedHolder.java (73%) create mode 100644 mpush-netty/src/test/resources/logback.xml rename {mpush-core/src/main/java/com/shinemo/mpush/core/util => mpush-tools/src/main/java/com/shinemo/mpush/tools}/InetAddressUtil.java (96%) rename {mpush-core/src/main/java/com/shinemo/mpush/core/util => mpush-tools/src/main/java/com/shinemo/mpush/tools}/JVMUtil.java (96%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-tools/src/main/java/com/shinemo/mpush/tools}/thread/NamedThreadFactory.java (96%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-tools/src/main/java/com/shinemo/mpush/tools}/thread/ThreadNameSpace.java (91%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-tools/src/main/java/com/shinemo/mpush/tools}/thread/ThreadPoolManager.java (98%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-tools/src/main/java/com/shinemo/mpush/tools}/thread/ThreadPoolUtil.java (91%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java new file mode 100644 index 00000000..38d6926e --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java @@ -0,0 +1,30 @@ +package com.shinemo.mpush.api.protocol; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; + +import java.net.SocketAddress; +import java.util.List; + +import com.shinemo.mpush.api.Connection; + +public interface Handler { + + public void channelActive(ChannelHandlerContext ctx) throws Exception; + + public void channelInactive(ChannelHandlerContext ctx) throws Exception; + + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception; + + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception; + + public void channelRegistered(ChannelHandlerContext ctx) throws Exception; + + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception; + + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception; + + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception; + + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception; + +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index fc982c06..e1a44b2e 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -18,7 +18,7 @@ public class Packet implements Serializable { public int sessionId; public byte lrc; public byte[] body; - + public int getBodyLength() { return body == null ? 0 : body.length; } @@ -26,7 +26,7 @@ public int getBodyLength() { public String getStringBody() { return body == null ? "" : new String(body, Constants.UTF_8); } - + @Override public String toString() { return "Packet{" + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java new file mode 100644 index 00000000..26732911 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.api.protocol; + +public interface Protocol { + + + +} diff --git a/mpush-client/.gitignore b/mpush-client/.gitignore new file mode 100644 index 00000000..ae3c1726 --- /dev/null +++ b/mpush-client/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 1ccd550e..4f0ee4df 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -13,4 +13,4 @@ 1.0-SNAPSHOT jar - \ No newline at end of file + diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java deleted file mode 100644 index c46078ba..00000000 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/ConnectionHandler.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shinemo.mpush.connection; - - -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.core.MessageReceiver; -import com.shinemo.mpush.core.NettyConnection; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/19. - */ -public class ConnectionHandler extends ChannelHandlerAdapter { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private MessageReceiver receiver = new MessageReceiver(); - private NettyConnection connection = new NettyConnection(); - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - System.out.println("channelRead msg=" + msg); - logger.debug("channelRead msg=" + msg); - receiver.onMessage((Packet) msg, connection); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - super.exceptionCaught(ctx, cause); - ConnectionManager.INSTANCE.remove(connection); - System.err.println("exceptionCaught"); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - connection.init(ctx.channel()); - ConnectionManager.INSTANCE.add(connection); - System.out.println("server receive channelActive"); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - super.channelInactive(ctx); - ConnectionManager.INSTANCE.remove(connection); - System.out.println("server receive channelInactive"); - } -} diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 72c1226c..3647eef9 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -33,6 +33,10 @@ com.shinemo.mpush mpush-gateway + + com.shinemo.mpush + mpush-netty + org.slf4j slf4j-api diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java index 66f636da..155ddc00 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java @@ -15,7 +15,7 @@ public class MessageReceiver implements Receiver { public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); public static final MessageHandler BIND_HANDLER = new BindHandler(); public static final MessageHandler HEART_HANDLER = new HeartBeatHandler(); - public static final MessageHandler HAND_SHAKE_HANDLER = new HandshakeHandler(); + public static final MessageHandler HAND_SHAKE_HANDLER = new HandShakeHandler(); public static final MessageHandler FAST_CONNECT_HANDLER = new FastConnectHandler(); @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 9eceab4a..de769e4c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -5,14 +5,11 @@ import com.shinemo.mpush.api.Request; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.security.CredentialManager; -import com.shinemo.mpush.core.security.ReusableToken; -import com.shinemo.mpush.core.security.ReusableTokenManager; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.crypto.CryptoUtils; -import com.shinemo.mpush.tools.crypto.DESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; + import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java similarity index 63% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java index ee544608..ae970425 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/handler/ConnectionHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java @@ -1,14 +1,15 @@ -package com.shinemo.mpush.connection.netty.handler; +package com.shinemo.mpush.core.handler; +import java.net.SocketAddress; + +import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Request; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.NettyConnection; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; + import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; @@ -18,14 +19,13 @@ /** * Created by ohun on 2015/12/19. */ -@ChannelHandler.Sharable -public class ConnectionHandler extends ChannelHandlerAdapter { +public class ServerHandler implements Handler { - private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class); + private static final Logger log = LoggerFactory.getLogger(ServerHandler.class); private final MessageReceiver receiver; - public ConnectionHandler(MessageReceiver receiver) { + public ServerHandler(MessageReceiver receiver) { this.receiver = receiver; } @@ -54,4 +54,29 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { log.warn(ctx.channel().remoteAddress() + ", channelInactive"); ConnectionManager.INSTANCE.remove(ctx.channel()); } -} + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void channelRegistered(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { + + } + + @Override + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + + } +} \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java index a89f82bf..0615ed73 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java @@ -5,7 +5,7 @@ /** * Created by ohun on 2015/12/24. */ -public class HandshakeMessage implements Message { +public class HandShakeMessage implements Message { public String deviceId; public String osName; public String osVersion; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java similarity index 90% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java index 5dfaa6ba..9040f067 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanAllClientConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.task; +package com.shinemo.mpush.core.task; import io.netty.util.Timeout; import io.netty.util.TimerTask; @@ -12,9 +12,8 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.connection.netty.NettySharedHolder; -import com.shinemo.mpush.connection.netty.task.ScanTask; import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.netty.util.NettySharedHolder; /** * 定时全量扫描connection diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java similarity index 81% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java index e3dfa05c..1a93f391 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/task/ScanTask.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.task; +package com.shinemo.mpush.core.task; import com.shinemo.mpush.api.Connection; diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java similarity index 82% rename from mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java index eb0e45d8..c530e91d 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java @@ -1,7 +1,8 @@ -package com.shinemo.mpush.connection; +package com.shinemo.mpush.core.netty; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.message.FastConnectMessage; import com.shinemo.mpush.core.message.HandshakeMessage; @@ -9,28 +10,29 @@ import com.shinemo.mpush.core.security.CipherManager; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.Strings; -import com.shinemo.mpush.tools.crypto.CryptoUtils; import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; -import io.netty.channel.ChannelHandlerAdapter; + import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; +import java.net.SocketAddress; import java.security.interfaces.RSAPublicKey; /** * Created by ohun on 2015/12/24. */ -public class ClientHandler extends ChannelHandlerAdapter { +public class ClientHandler implements Handler { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class); private byte[] clientKey = CipherManager.INSTANCE.randomAESKey(); private byte[] iv = CipherManager.INSTANCE.randomAESIV(); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); String token = getToken(); if (!Strings.isBlank(token)) { RSAPublicKey publicKey = CipherManager.INSTANCE.getPublicKey(); @@ -63,13 +65,11 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - super.channelInactive(ctx); LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelInactive"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - super.channelRead(ctx, msg); LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelRead", msg); if (msg instanceof Packet) { Packet packet = (Packet) msg; @@ -109,4 +109,34 @@ private String getToken() { } return Strings.EMPTY; } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void channelRegistered(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + + } + + @Override + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { + + } + + @Override + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + + } } diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java similarity index 85% rename from mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 03280543..2580ac75 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection; +package com.shinemo.mpush.core.netty; import java.util.concurrent.TimeUnit; @@ -8,11 +8,13 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.connection.netty.NettySharedHolder; -import com.shinemo.mpush.connection.netty.codec.PacketDecoder; -import com.shinemo.mpush.connection.netty.codec.PacketEncoder; import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.netty.codec.PacketDecoder; +import com.shinemo.mpush.netty.codec.PacketEncoder; +import com.shinemo.mpush.netty.util.NettySharedHandler; +import com.shinemo.mpush.netty.util.NettySharedHolder; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; @@ -30,9 +32,9 @@ /** * Created by ohun on 2015/12/24. */ -public class ClientTest { +public class NettyClientTest { - private static final Logger log = LoggerFactory.getLogger(ClientTest.class); + private static final Logger log = LoggerFactory.getLogger(NettyClientTest.class); @Test public void testClient() { @@ -44,12 +46,16 @@ public void testClient() { b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) + + Handler handler = new ClientHandler(); + final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); + b.handler(new ChannelInitializer() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(new ClientHandler()); + ch.pipeline().addLast(nettySharedHandler); } }); ChannelFuture future = b.connect(host, port).sync(); // (5) diff --git a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java similarity index 50% rename from mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 17312541..1b07d8c9 100644 --- a/mpush-connection/src/test/java/com/shinemo/mpush/connection/ConnectionServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,13 +1,16 @@ -package com.shinemo.mpush.connection; +package com.shinemo.mpush.core.netty; import org.junit.Test; -import com.shinemo.mpush.connection.netty.server.ConnectionServer; +import com.shinemo.mpush.api.protocol.Handler; +import com.shinemo.mpush.core.MessageReceiver; +import com.shinemo.mpush.core.handler.ServerHandler; +import com.shinemo.mpush.netty.server.NettyServer; /** * Created by ohun on 2015/12/24. */ -public class ConnectionServerTest { +public class NettyServerTest { @Test public void testStop() throws Exception { @@ -16,7 +19,11 @@ public void testStop() throws Exception { @Test public void testStart() throws Exception { - final ConnectionServer server = new ConnectionServer(3000); + + MessageReceiver receiver = new MessageReceiver(); + Handler handler = new ServerHandler(receiver); + + final NettyServer server = new NettyServer(3000,handler); server.start(); Runtime.getRuntime().addShutdownHook(new Thread() { diff --git a/mpush-core/src/test/resources/logback.xml b/mpush-core/src/test/resources/logback.xml new file mode 100644 index 00000000..e638aca3 --- /dev/null +++ b/mpush-core/src/test/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + INFO + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml new file mode 100644 index 00000000..8614196d --- /dev/null +++ b/mpush-netty/pom.xml @@ -0,0 +1,51 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-netty + 1.0-SNAPSHOT + jar + + + + com.shinemo.mpush + mpush-api + + + com.shinemo.mpush + mpush-tools + + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + commons-logging + commons-logging + + + log4j + log4j + + + org.logback-extensions + logback-ext-spring + + + + diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java new file mode 100644 index 00000000..96a69fe9 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -0,0 +1,5 @@ +package com.shinemo.mpush.netty.client; + +public class NettyClient { + +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java similarity index 98% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java rename to mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index 859f79fe..1a42a916 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.codec; +package com.shinemo.mpush.netty.codec; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.exception.DecodeException; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java similarity index 95% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java rename to mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java index 6f353684..25ed2892 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.codec; +package com.shinemo.mpush.netty.codec; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java similarity index 87% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java rename to mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 4c522be4..1e1b8776 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/server/ConnectionServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.connection.netty.server; +package com.shinemo.mpush.netty.server; import java.util.concurrent.atomic.AtomicBoolean; @@ -6,12 +6,12 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.connection.netty.NettySharedHolder; -import com.shinemo.mpush.connection.netty.codec.PacketDecoder; -import com.shinemo.mpush.connection.netty.codec.PacketEncoder; -import com.shinemo.mpush.connection.netty.handler.ConnectionHandler; -import com.shinemo.mpush.core.MessageReceiver; -import com.shinemo.mpush.core.thread.ThreadPoolUtil; +import com.shinemo.mpush.api.protocol.Handler; +import com.shinemo.mpush.netty.codec.PacketDecoder; +import com.shinemo.mpush.netty.codec.PacketEncoder; +import com.shinemo.mpush.netty.util.NettySharedHandler; +import com.shinemo.mpush.netty.util.NettySharedHolder; +import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; @@ -25,17 +25,19 @@ /** * Created by ohun on 2015/12/22. */ -public class ConnectionServer implements Server { +public class NettyServer implements Server { - private static final Logger log = LoggerFactory.getLogger(ConnectionServer.class); + private static final Logger log = LoggerFactory.getLogger(NettyServer.class); private final AtomicBoolean startFlag = new AtomicBoolean(false); private final int port; + private final Handler channelHandler; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; - public ConnectionServer(int port) { + public NettyServer(int port,Handler channelHandler) { this.port = port; + this.channelHandler = channelHandler; } @Override @@ -95,8 +97,8 @@ public void start() { * 这里告诉Channel如何获取新的连接. */ b.channel(NioServerSocketChannel.class); - - final ConnectionHandler connectionHandler = new ConnectionHandler(new MessageReceiver()); + + final NettySharedHandler nettySharedHandler = new NettySharedHandler(channelHandler); /*** * 这里的事件处理类经常会被用来处理一个最近的已经接收的Channel。 @@ -112,7 +114,7 @@ public void start() { public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(connectionHandler); + ch.pipeline().addLast(nettySharedHandler); } }); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java new file mode 100644 index 00000000..6a367e0f --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java @@ -0,0 +1,110 @@ +package com.shinemo.mpush.netty.util; + +import java.net.SocketAddress; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.api.protocol.Handler; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; + +@ChannelHandler.Sharable +public class NettySharedHandler extends ChannelHandlerAdapter{ + + private static final Logger log = LoggerFactory.getLogger(NettySharedHandler.class); + + private Handler channelHandler; + + public NettySharedHandler(Handler channelHandler){ + this.channelHandler = channelHandler; + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelActive"); + if(channelHandler!=null){ + channelHandler.channelActive(ctx); + } + super.channelActive(ctx); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelInactive"); + if(channelHandler!=null){ + channelHandler.channelInactive(ctx); + } + super.channelInactive(ctx); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelRead:"+ToStringBuilder.reflectionToString(msg, ToStringStyle.DEFAULT_STYLE)); + if(channelHandler!=null){ + channelHandler.channelRead(ctx, msg); + } + super.channelRead(ctx, msg); + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelReadComplete"); + if(channelHandler!=null){ + channelHandler.channelReadComplete(ctx); + } + super.channelReadComplete(ctx); + } + + @Override + public void channelRegistered(ChannelHandlerContext ctx) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelRegistered"); + if(channelHandler!=null){ + channelHandler.channelRegistered(ctx); + } + super.channelRegistered(ctx); + } + + @Override + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", channelUnregistered"); + if(channelHandler!=null){ + channelHandler.channelUnregistered(ctx); + } + super.channelUnregistered(ctx); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", exceptionCaught",cause); + if(channelHandler!=null){ + channelHandler.exceptionCaught(ctx, cause); + } + super.exceptionCaught(ctx, cause); + } + + @Override + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", connect"); + if(channelHandler!=null){ + channelHandler.connect(ctx, remoteAddress, localAddress, promise); + } + super.connect(ctx, remoteAddress, localAddress, promise); + } + + @Override + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + log.warn(ctx.channel().remoteAddress() + ", disconnect"); + if(channelHandler!=null){ + channelHandler.disconnect(ctx, promise); + } + super.disconnect(ctx, promise); + } + + +} diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java similarity index 73% rename from mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java rename to mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java index 62686ed1..4719a8f8 100644 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/NettySharedHolder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java @@ -1,7 +1,8 @@ -package com.shinemo.mpush.connection.netty; +package com.shinemo.mpush.netty.util; -import com.shinemo.mpush.core.thread.NamedThreadFactory; -import com.shinemo.mpush.core.thread.ThreadNameSpace; + +import com.shinemo.mpush.tools.thread.NamedThreadFactory; +import com.shinemo.mpush.tools.thread.ThreadNameSpace; import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.UnpooledByteBufAllocator; diff --git a/mpush-netty/src/test/resources/logback.xml b/mpush-netty/src/test/resources/logback.xml new file mode 100644 index 00000000..e638aca3 --- /dev/null +++ b/mpush-netty/src/test/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + INFO + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 6bc40436..3390f7d8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -8,4 +8,16 @@ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); byte[] EMPTY_BYTES = new byte[0]; + + String JVM_LOG_PATH = "/opt/"; + + int THREAD_QUEUE_SIZE = 10000; + int MIN_POOL_SIZE = 50; + int MAX_POOL_SIZE = 500; + + int MIN_BOSS_POOL_SIZE = 10; + int MAX_BOSS_POLL_SIZE = 50; + + int MIN_WORK_POOL_SIZE = 10; + int MAX_WORK_POOL_SIZE = 250; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java similarity index 96% rename from mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java index bc515cc0..ede57b69 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/util/InetAddressUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java @@ -31,10 +31,10 @@ public static String getInetAddress(){ } } log.warn("[InetAddressUtil] getInetAddress is null"); - return null; + return "127.0.0.1"; }catch(Throwable e){ log.warn("[InetAddressUtil] getInetAddress exception",e); - return null; + return "127.0.0.1"; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java similarity index 96% rename from mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java index 063d1b17..2d5f57b1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/util/JVMUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.util; +package com.shinemo.mpush.tools; import java.io.OutputStream; import java.util.Iterator; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/NamedThreadFactory.java similarity index 96% rename from mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/NamedThreadFactory.java index 780b030d..1338dc9c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/NamedThreadFactory.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.thread; +package com.shinemo.mpush.tools.thread; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java similarity index 91% rename from mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index c4d6d237..8d997d56 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.thread; +package com.shinemo.mpush.tools.thread; public class ThreadNameSpace { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java similarity index 98% rename from mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java index 89b06d89..64b8ba5f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.thread; +package com.shinemo.mpush.tools.thread; import java.io.File; import java.io.FileNotFoundException; @@ -21,8 +21,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.core.util.JVMUtil; +import com.shinemo.mpush.tools.Constants; +import com.shinemo.mpush.tools.JVMUtil; + public class ThreadPoolManager { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java similarity index 91% rename from mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java index a0648ace..d53176a9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/thread/ThreadPoolUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java @@ -1,8 +1,9 @@ -package com.shinemo.mpush.core.thread; +package com.shinemo.mpush.tools.thread; import java.util.concurrent.Executor; -import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.tools.Constants; + public class ThreadPoolUtil { diff --git a/pom.xml b/pom.xml index 3f8cc585..ff7104d0 100644 --- a/pom.xml +++ b/pom.xml @@ -19,15 +19,15 @@ mpush-connection mpush-gateway mpush-core - mpush-client mpush-tools + mpush-netty 1.7 4.0.0.RELEASE 1.0-SNAPSHOT - 1.0-SNAPSHOT + 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT @@ -76,8 +76,8 @@ com.shinemo.mpush - mpush-client - ${mpush-client-version} + mpush-netty + ${mpush-netty-version} com.shinemo.mpush From 8ec54ea562d73797e1ebcb56b1936b8afd5c4458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 27 Dec 2015 20:12:18 +0800 Subject: [PATCH 031/890] add netty client --- .../java/com/shinemo/mpush/api/Client.java | 28 +++++ .../mpush/core/netty/NettyClientTest.java | 111 +++-------------- .../client/AbstractNettyClientFactory.java | 85 +++++++++++++ .../mpush/netty/client/NettyClient.java | 112 +++++++++++++++++- .../netty/client/NettyClientFactory.java | 61 ++++++++++ .../mpush/netty/server/NettyServer.java | 4 + 6 files changed, 306 insertions(+), 95 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Client.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java new file mode 100644 index 00000000..89a9d33f --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -0,0 +1,28 @@ +package com.shinemo.mpush.api; + +public interface Client { + + public void close(final String cause); + + public String toString(); + + public boolean isEnabled(); + + public boolean isConnected(); + + public void resetHbTimes(); + + public int inceaseAndGetHbTimes(); + + public void startHeartBeat() throws Exception; + + /** + * host:port + */ + public String getUrl(); + + public String getRemoteHost(); + + public int getRemotePort(); + +} diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 2580ac75..eeb96c5c 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -1,33 +1,15 @@ package com.shinemo.mpush.core.netty; -import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.protocol.Handler; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.netty.codec.PacketDecoder; -import com.shinemo.mpush.netty.codec.PacketEncoder; -import com.shinemo.mpush.netty.util.NettySharedHandler; -import com.shinemo.mpush.netty.util.NettySharedHolder; - -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; +import com.shinemo.mpush.netty.client.NettyClientFactory; /** * Created by ohun on 2015/12/24. @@ -35,79 +17,20 @@ public class NettyClientTest { private static final Logger log = LoggerFactory.getLogger(NettyClientTest.class); - + + private String host = "127.0.0.1"; + private int port = 3000; + private Handler handler = new ClientHandler(); + @Test - public void testClient() { - String host = "127.0.0.1"; - int port = 3000; - EventLoopGroup workerGroup = new NioEventLoopGroup(); - try { - Bootstrap b = new Bootstrap(); // (1) - b.group(workerGroup); // (2) - b.channel(NioSocketChannel.class); // (3) - b.option(ChannelOption.SO_KEEPALIVE, true); // (4) - - Handler handler = new ClientHandler(); - final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); - - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(nettySharedHandler); - } - }); - ChannelFuture future = b.connect(host, port).sync(); // (5) - if (future.awaitUninterruptibly(4000) && future.isSuccess()) { - startHeartBeat(future.channel()); - future.channel().closeFuture().sync(); - } else { - future.cancel(true); - future.channel().close(); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - workerGroup.shutdownGracefully(); - } - + public void testClient() throws Exception { + + Client client = NettyClientFactory.instance.get(host, port, handler); + + Thread.sleep(1000); + + log.error(ToStringBuilder.reflectionToString(client, ToStringStyle.MULTI_LINE_STYLE)); + } - public void startHeartBeat(final Channel channel) { - NettySharedHolder.timer.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - final Packet packet = buildHeartBeat(); - ChannelFuture channelFuture = channel.writeAndFlush(packet); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); - ConnectionManager.INSTANCE.remove(channel); - } - log.warn("client send msg false:" + channel.remoteAddress().toString() + "," + packet); - } else { - log.debug("client send msg success:" + channel.remoteAddress().toString() + "," + packet); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - } - } - }, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - - private static Packet buildHeartBeat() { - Packet packet = new Packet(); - packet.cmd = Command.Heartbeat.cmd; - return packet; - } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java new file mode 100644 index 00000000..158dbf51 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -0,0 +1,85 @@ +package com.shinemo.mpush.netty.client; + +import java.text.MessageFormat; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.protocol.Handler; + +public abstract class AbstractNettyClientFactory { + + private static final String format = "%s:%s"; + + private static final Logger log = LoggerFactory.getLogger(AbstractNettyClientFactory.class); + + /** + * host:port + */ + private final Cache cachedClients = CacheBuilder.newBuilder()// + .maximumSize(2 << 17)// 最大是65535*2 + .expireAfterAccess(2*60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力 + .removalListener(new RemovalListener() { + @Override + public void onRemoval(RemovalNotification notification) { + if (notification.getValue().isConnected()) { + notification.getValue().close("[Remoting] removed from cache"); + } + } + })// + .build(); + + /** + * 不存在,则创建 + * @param remoteHost + * @param port + * @param handler + * @return + * @throws Exception + */ + public Client get(final String remoteHost,final int port,final Handler handler) throws Exception { + final String key = String.format(format, remoteHost,port); + Client client = cachedClients.get(key, new Callable() { + @Override + public Client call() throws Exception { + Client client = createClient(remoteHost,port,handler); + if (client != null) { + client.startHeartBeat(); + } + return client; + } + }); + if (client == null || !client.isConnected()) { + cachedClients.invalidate(key); + return null; + } + return client; + } + + public Client get(final String remoteHost,final int port) throws Exception { + return get(remoteHost, port, null); + } + + + protected Client createClient(final String remoteHost,final int port) throws Exception{ + return createClient(remoteHost, port, null); + } + + protected abstract Client createClient(final String remoteHost,final int port,Handler handler) throws Exception; + + + public void remove(Client client) { + if (client != null) { + cachedClients.invalidate(client.getUrl()); + log.warn(MessageFormat.format("[Remoting] {0} is removed", client)); + } + } + +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index 96a69fe9..e08e7c06 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -1,5 +1,115 @@ package com.shinemo.mpush.netty.client; -public class NettyClient { +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.util.Timeout; +import io.netty.util.TimerTask; + +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.netty.util.NettySharedHolder; + +public class NettyClient implements Client { + + private static final Logger log = LoggerFactory.getLogger(NettyClient.class); + + private final String remoteHost; + private final int remotePort; + private final Channel channel; + private int hbTimes = 0; + + public NettyClient(final String remoteHost, final int remotePort, Channel channel) { + this.remoteHost = remoteHost; + this.remotePort = remotePort; + this.channel = channel; + } + + @Override + public void close(String cause) { + if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { + log.error("close channel:"+cause); + } + this.channel.close(); + } + + @Override + public boolean isEnabled() { + return channel.isWritable(); + } + + @Override + public boolean isConnected() { + return channel.isActive(); + } + + @Override + public void resetHbTimes() { + hbTimes = 0; + } + + @Override + public int inceaseAndGetHbTimes() { + return ++hbTimes; + } + + @Override + public void startHeartBeat() throws Exception { + NettySharedHolder.timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + try { + final Packet packet = buildHeartBeat(); + ChannelFuture channelFuture = channel.writeAndFlush(packet); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + log.warn("client send hb msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); + } + log.warn("client send msg hb false:" + channel.remoteAddress().toString() + "," + packet); + } else { + log.warn("client send msg hb success:" + channel.remoteAddress().toString() + "," + packet); + } + } + }); + } finally { + if (channel.isActive()) { + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + } + } + }, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + + private static Packet buildHeartBeat() { + Packet packet = new Packet(); + packet.cmd = Command.Heartbeat.cmd; + return packet; + } + + @Override + public String getUrl() { + return String.format("%s:%s", remoteHost, remotePort); + } + + @Override + public String getRemoteHost() { + return remoteHost; + } + + @Override + public int getRemotePort() { + return remotePort; + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java new file mode 100644 index 00000000..3ceac073 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -0,0 +1,61 @@ +package com.shinemo.mpush.netty.client; + +import java.net.InetSocketAddress; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.protocol.Handler; +import com.shinemo.mpush.netty.util.NettySharedHandler; +import com.shinemo.mpush.netty.util.NettySharedHolder; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; + +public class NettyClientFactory extends AbstractNettyClientFactory{ + + private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); + + public static NettyClientFactory instance = new NettyClientFactory(); + + protected Client createClient(final String host,final int port,final Handler handler) throws Exception { + EventLoopGroup workerGroup = new NioEventLoopGroup(); + final Bootstrap bootstrap = new Bootstrap(); + NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator)// + .channel(NioSocketChannel.class)// + .handler(nettySharedHandler); + bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { + Channel channel = future.channel(); + Client client = new NettyClient(host,port, channel); + return client; + } else { + future.cancel(true); + future.channel().close(); + log.warn("[remoting] failure to connect:" + host+","+port); + } + throw new Exception("create client exception"); + } + + public Client getClient(final Client client) throws Exception { + return get(client.getRemoteHost(),client.getRemotePort()); + } + + public void remove(final Client client) { + super.remove(client); + } + +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 1e1b8776..c196998f 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -39,6 +39,10 @@ public NettyServer(int port,Handler channelHandler) { this.port = port; this.channelHandler = channelHandler; } + + public NettyServer(int port) { + this(port,null); + } @Override public void init() { From 54afa50c1e0e1e8093abcf5cba88c544917499b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 28 Dec 2015 09:06:52 +0800 Subject: [PATCH 032/890] add event --- .../com/shinemo/mpush/tools/event/Event.java | 21 +++++++++++++++++++ .../mpush/tools/event/EventDispatcher.java | 19 +++++++++++++++++ .../mpush/tools/event/EventListener.java | 7 +++++++ .../shinemo/mpush/tools/event/EventType.java | 5 +++++ 4 files changed, 52 insertions(+) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java new file mode 100644 index 00000000..416222a8 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.tools.event; + +public class Event { + + private final EventType eventType; + private final Object source; + + public Event(final EventType eventType, final Object source) { + this.eventType = eventType; + this.source = source; + } + + public EventType getEventType() { + return eventType; + } + + public Object getSource() { + return source; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java new file mode 100644 index 00000000..ea528cba --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java @@ -0,0 +1,19 @@ +package com.shinemo.mpush.tools.event; + +import java.util.ArrayList; +import java.util.List; + +public class EventDispatcher { + + private static final List listeners = new ArrayList(); + + public static void addEventListener(EventListener listener) { + listeners.add(listener); + } + + public static void fireEvent(Event event) { + for (EventListener listener : listeners) { + listener.onEvent(event); + } + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java new file mode 100644 index 00000000..ada094cb --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.tools.event; + +public interface EventListener { + + public void onEvent(Event event); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java new file mode 100644 index 00000000..b5c9b0f4 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java @@ -0,0 +1,5 @@ +package com.shinemo.mpush.tools.event; + +public enum EventType { + +} From 8d4c62620a83c210c94daff9148c54d6f502338f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 28 Dec 2015 09:11:34 +0800 Subject: [PATCH 033/890] add default handler --- .../shinemo/mpush/api/AbstractHandler.java | 45 +++++++++++++++++++ .../shinemo/mpush/api/protocol/Protocol.java | 7 --- .../mpush/core/handler/ServerHandler.java | 30 +------------ 3 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java new file mode 100644 index 00000000..7cc53082 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.api; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; + +import java.net.SocketAddress; + +public abstract class AbstractHandler { + + public void channelActive(ChannelHandlerContext ctx) throws Exception{ + + } + + public void channelInactive(ChannelHandlerContext ctx) throws Exception{ + + } + + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{ + + } + + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{ + + } + + public void channelRegistered(ChannelHandlerContext ctx) throws Exception{ + + } + + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception{ + + } + + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{ + + } + + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception{ + + } + + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception{ + + } + +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java deleted file mode 100644 index 26732911..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Protocol.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.shinemo.mpush.api.protocol; - -public interface Protocol { - - - -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java index ae970425..6aafed54 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java @@ -1,17 +1,15 @@ package com.shinemo.mpush.core.handler; -import java.net.SocketAddress; - import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.AbstractHandler; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.NettyConnection; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +17,7 @@ /** * Created by ohun on 2015/12/19. */ -public class ServerHandler implements Handler { +public class ServerHandler extends AbstractHandler implements Handler { private static final Logger log = LoggerFactory.getLogger(ServerHandler.class); @@ -55,28 +53,4 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { ConnectionManager.INSTANCE.remove(ctx.channel()); } - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void channelRegistered(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - - } - - @Override - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - - } } \ No newline at end of file From 37edbb60a45a8cde56ce73a8a88c53fc8cb1e428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 28 Dec 2015 10:36:46 +0800 Subject: [PATCH 034/890] netty client add code --- .../java/com/shinemo/mpush/api/Constants.java | 2 +- .../mpush/core/netty/ClientHandler.java | 24 ++++++++--------- .../mpush/core/netty/NettyClientTest.java | 7 ++++- .../netty/client/NettyClientFactory.java | 26 +++++++++++++++---- .../mpush/netty/util/NettySharedHandler.java | 22 ++-------------- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 1f1cbad9..06dbef71 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -15,7 +15,7 @@ public interface Constants { int COMPRESS_LIMIT = 1024 * 10; byte CRYPTO_FLAG = 0x01; byte COMPRESS_FLAG = 0x02; - long TIME_DELAY = 1L; + long TIME_DELAY = 120L; String JVM_LOG_PATH = "/opt/"; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java index 2890a594..5ec55435 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java @@ -33,8 +33,8 @@ public class ClientHandler implements Handler { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - String token = getToken(); - if (!Strings.isBlank(token)) { +// String token = getToken(); + //if (!Strings.isBlank(token)) { RSAPublicKey publicKey = CipherManager.INSTANCE.getPublicKey(); HandShakeMessage message = new HandShakeMessage(); message.clientKey = clientKey; @@ -50,16 +50,16 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { packet.sessionId = 1; packet.body = RSAUtils.encryptByPublicKey(Jsons.toJson(message).getBytes(Constants.UTF_8), publicKey); ctx.writeAndFlush(packet); - } else { - FastConnectMessage message = new FastConnectMessage(); - message.deviceId = "1111111111111"; - message.tokenId = token; - Packet packet = new Packet(); - packet.cmd = Command.FastConnect.cmd; - packet.sessionId = 1; - packet.body = Jsons.toJson(message).getBytes(Constants.UTF_8); - ctx.writeAndFlush(packet); - } +// } else { +// FastConnectMessage message = new FastConnectMessage(); +// message.deviceId = "1111111111111"; +// message.tokenId = token; +// Packet packet = new Packet(); +// packet.cmd = Command.FastConnect.cmd; +// packet.sessionId = 1; +// packet.body = Jsons.toJson(message).getBytes(Constants.UTF_8); +// ctx.writeAndFlush(packet); +// } LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelActive"); } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index eeb96c5c..5e1bfc6a 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.core.netty; +import java.util.concurrent.locks.LockSupport; + import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Test; @@ -27,7 +29,10 @@ public void testClient() throws Exception { Client client = NettyClientFactory.instance.get(host, port, handler); - Thread.sleep(1000); + + LockSupport.park(); + + client.close(""); log.error(ToStringBuilder.reflectionToString(client, ToStringStyle.MULTI_LINE_STYLE)); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 3ceac073..38c70cc6 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -7,15 +7,19 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.protocol.Handler; +import com.shinemo.mpush.netty.codec.PacketDecoder; +import com.shinemo.mpush.netty.codec.PacketEncoder; import com.shinemo.mpush.netty.util.NettySharedHandler; import com.shinemo.mpush.netty.util.NettySharedHolder; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class NettyClientFactory extends AbstractNettyClientFactory{ @@ -27,16 +31,24 @@ public class NettyClientFactory extends AbstractNettyClientFactory{ protected Client createClient(final String host,final int port,final Handler handler) throws Exception { EventLoopGroup workerGroup = new NioEventLoopGroup(); final Bootstrap bootstrap = new Bootstrap(); - NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); + final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); bootstrap.group(workerGroup)// .option(ChannelOption.TCP_NODELAY, true)// .option(ChannelOption.SO_REUSEADDR, true)// .option(ChannelOption.SO_KEEPALIVE, true)// .option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator)// - .channel(NioSocketChannel.class)// - .handler(nettySharedHandler); - bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(nettySharedHandler); + } + }); + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { Channel channel = future.channel(); @@ -57,5 +69,9 @@ public Client getClient(final Client client) throws Exception { public void remove(final Client client) { super.remove(client); } + + public void close(final Client client){ + + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java index 6a367e0f..3d8990d2 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java @@ -63,7 +63,7 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelRegistered"); +// log.warn(ctx.channel().remoteAddress() + ", channelRegistered"); if(channelHandler!=null){ channelHandler.channelRegistered(ctx); } @@ -72,7 +72,7 @@ public void channelRegistered(ChannelHandlerContext ctx) throws Exception { @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelUnregistered"); +// log.warn(ctx.channel().remoteAddress() + ", channelUnregistered"); if(channelHandler!=null){ channelHandler.channelUnregistered(ctx); } @@ -88,23 +88,5 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E super.exceptionCaught(ctx, cause); } - @Override - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", connect"); - if(channelHandler!=null){ - channelHandler.connect(ctx, remoteAddress, localAddress, promise); - } - super.connect(ctx, remoteAddress, localAddress, promise); - } - - @Override - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", disconnect"); - if(channelHandler!=null){ - channelHandler.disconnect(ctx, promise); - } - super.disconnect(ctx, promise); - } - } From f7d02e3742a7bd1d0342e3c627778aa097779867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 28 Dec 2015 11:02:30 +0800 Subject: [PATCH 035/890] change name message --- .../java/com/shinemo/mpush/api/protocol/Packet.java | 10 +++++----- .../{MessageReceiver.java => MessageDispatcher.java} | 2 +- .../com/shinemo/mpush/core/handler/ServerHandler.java | 6 +++--- .../com/shinemo/mpush/core/netty/NettyServerTest.java | 4 +++- 4 files changed, 12 insertions(+), 10 deletions(-) rename mpush-core/src/main/java/com/shinemo/mpush/core/{MessageReceiver.java => MessageDispatcher.java} (96%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 2bf37404..6b8de5c1 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -12,11 +12,11 @@ */ public class Packet implements Serializable { private static final long serialVersionUID = -2725825199998223372L; - public byte cmd; - public short cc; - public byte flags; - public int sessionId; - public byte lrc; + public byte cmd; //命令 + public short cc; //校验码 暂时没有用到 + public byte flags; //特性,如是否加密,是否压缩等 + public int sessionId; // 会话id。客户端生成。 + public byte lrc; // 校验,纵向冗余校验。只校验body public byte[] body; public int getBodyLength() { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java similarity index 96% rename from mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java index 155ddc00..06313ab7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageReceiver.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java @@ -11,7 +11,7 @@ /** * Created by ohun on 2015/12/22. */ -public class MessageReceiver implements Receiver { +public class MessageDispatcher implements Receiver { public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); public static final MessageHandler BIND_HANDLER = new BindHandler(); public static final MessageHandler HEART_HANDLER = new HeartBeatHandler(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java index 6aafed54..9947ea9a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java @@ -5,8 +5,8 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.AbstractHandler; import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.NettyConnection; import io.netty.channel.ChannelHandlerContext; @@ -21,9 +21,9 @@ public class ServerHandler extends AbstractHandler implements Handler { private static final Logger log = LoggerFactory.getLogger(ServerHandler.class); - private final MessageReceiver receiver; + private final Receiver receiver; - public ServerHandler(MessageReceiver receiver) { + public ServerHandler(Receiver receiver) { this.receiver = receiver; } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 1b07d8c9..ea16478a 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -2,7 +2,9 @@ import org.junit.Test; +import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.protocol.Handler; +import com.shinemo.mpush.core.MessageDispatcher; import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.handler.ServerHandler; import com.shinemo.mpush.netty.server.NettyServer; @@ -20,7 +22,7 @@ public void testStop() throws Exception { @Test public void testStart() throws Exception { - MessageReceiver receiver = new MessageReceiver(); + Receiver receiver = new MessageDispatcher(); Handler handler = new ServerHandler(receiver); final NettyServer server = new NettyServer(3000,handler); From c6aebd8dd75beb526fb757167fe568da894b1d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 03:47:53 +0000 Subject: [PATCH 036/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/netty/server/NettyServer.java | 19 ++++++++- .../mpush/netty/util/NettySharedHolder.java | 7 +--- .../mpush/tools/thread/ThreadPoolManager.java | 6 +-- .../mpush/tools/thread/ThreadPoolUtil.java | 40 ++++++++++--------- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index c196998f..854ea2ab 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -2,6 +2,7 @@ import java.util.concurrent.atomic.AtomicBoolean; +import io.netty.buffer.PooledByteBufAllocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -130,6 +131,14 @@ public void initChannel(SocketChannel ch) throws Exception { */ b.option(ChannelOption.SO_BACKLOG, 1024); + /** + * TCP层面的接收和发送缓冲区大小设置, + * 在Netty中分别对应ChannelOption的SO_SNDBUF和SO_RCVBUF, + * 需要根据推送消息的大小,合理设置,对于海量长连接,通常32K是个不错的选择。 + */ + b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); + b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); + /*** * option()是提供给NioServerSocketChannel用来接收进来的连接。 * childOption()是提供给由父管道ServerChannel接收到的连接, @@ -137,9 +146,15 @@ public void initChannel(SocketChannel ch) throws Exception { */ b.childOption(ChannelOption.SO_KEEPALIVE, true); - b.option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); - b.childOption(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator); + /** + * 在Netty 4中实现了一个新的ByteBuf内存池,它是一个纯Java版本的 jemalloc (Facebook也在用)。 + * 现在,Netty不会再因为用零填充缓冲区而浪费内存带宽了。不过,由于它不依赖于GC,开发人员需要小心内存泄漏。 + * 如果忘记在处理程序中释放缓冲区,那么内存使用率会无限地增长。 + * Netty默认不使用内存池,需要在创建客户端或者服务端的时候进行指定 + */ + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); /*** * 绑定端口并启动去接收进来的连接 diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java index 4719a8f8..bda1bb41 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java @@ -11,12 +11,7 @@ public class NettySharedHolder { - public static final Timer timer = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); + public static final Timer timer = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); - public static final ByteBufAllocator byteBufAllocator; - - static { - byteBufAllocator = UnpooledByteBufAllocator.DEFAULT; - } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java index 64b8ba5f..aea701e7 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java @@ -89,7 +89,7 @@ public void run() { } - public void allocThreadpool(final String serviceUniqueName, int corePoolSize, int maximumPoolSize) + public void allocThreadPool(final String serviceUniqueName, int corePoolSize, int maximumPoolSize) throws Exception { if (poolCache.containsKey(serviceUniqueName)) { // 对同一个服务重复分配线程池时,抛出异常 throw new Exception(MessageFormat.format( @@ -149,7 +149,7 @@ public Executor getThreadExecutor(String serviceUniqueName,int corePoolSize, int return executor; }else{ try{ - allocThreadpool(serviceUniqueName, corePoolSize, maximumPoolSize); + allocThreadPool(serviceUniqueName, corePoolSize, maximumPoolSize); }catch(Exception e){ log.error("allocThreadPool exception",e); } @@ -160,7 +160,7 @@ public Executor getThreadExecutor(String serviceUniqueName,int corePoolSize, int } }else{ try{ - allocThreadpool(serviceUniqueName, corePoolSize, maximumPoolSize); + allocThreadPool(serviceUniqueName, corePoolSize, maximumPoolSize); }catch(Exception e){ log.error("allocThreadPool exception",e); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java index d53176a9..c081075d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java @@ -8,23 +8,25 @@ public class ThreadPoolUtil { - private static final ThreadPoolManager threadPoolManager = new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, - Constants.THREAD_QUEUE_SIZE); - - private static Executor bossExecutor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor(ThreadNameSpace.NETTY_BOSS,Constants.MIN_BOSS_POOL_SIZE,Constants.MAX_BOSS_POLL_SIZE); - private static Executor workExecutor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor(ThreadNameSpace.NETTY_WORKER,Constants.MIN_WORK_POOL_SIZE,Constants.MAX_WORK_POOL_SIZE); - - public static ThreadPoolManager getThreadPoolManager() { - return threadPoolManager; - } - - public static Executor getBossExecutor(){ - return bossExecutor; - } - - public static Executor getWorkExecutor(){ - return workExecutor; - } - - + private static final ThreadPoolManager threadPoolManager = + new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, Constants.THREAD_QUEUE_SIZE); + + private static Executor bossExecutor = ThreadPoolUtil.getThreadPoolManager() + .getThreadExecutor(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE); + private static Executor workExecutor = ThreadPoolUtil.getThreadPoolManager() + .getThreadExecutor(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE); + + public static ThreadPoolManager getThreadPoolManager() { + return threadPoolManager; + } + + public static Executor getBossExecutor() { + return bossExecutor; + } + + public static Executor getWorkExecutor() { + return workExecutor; + } + + } From b5fb4d8de350e3e3da384e38b56db9b566a09723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 10:57:40 +0000 Subject: [PATCH 037/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Cipher.java | 12 ++ .../com/shinemo/mpush/api/Connection.java | 4 +- .../java/com/shinemo/mpush/api/Message.java | 9 +- .../com/shinemo/mpush/api/MessageHandler.java | 4 +- .../com/shinemo/mpush/api/SessionContext.java | 31 +++++ .../com/shinemo/mpush/api/SessionInfo.java | 22 ---- .../shinemo/mpush/api/protocol/Packet.java | 2 +- .../shinemo/mpush/core/MessageDispatcher.java | 25 ++-- .../shinemo/mpush/core/NettyConnection.java | 15 ++- .../core/handler/BaseMessageHandler.java | 13 +- .../mpush/core/handler/BindHandler.java | 17 +-- .../core/handler/FastConnectHandler.java | 27 +--- .../mpush/core/handler/HandShakeHandler.java | 44 +++---- .../mpush/core/handler/HeartBeatHandler.java | 12 +- .../mpush/core/handler/LoginHandler.java | 20 --- .../core/message/BaseBufferBodyMessage.java | 63 +++++++++ .../mpush/core/message/BaseMessage.java | 94 ++++++++++++++ .../core/message/FastConnectMessage.java | 20 ++- .../mpush/core/message/HandShakeMessage.java | 44 ++++++- .../core/message/HandshakeSuccessMessage.java | 41 ++++++ .../core/message/HandshakeSuccessMsg.java | 13 -- .../mpush/core/message/HeartbeatMessage.java | 31 +++++ .../mpush/core/message/LoginMessage.java | 9 -- .../mpush/core/message/NettyRequest.java | 2 +- .../mpush/core/message/NettyResponse.java | 4 +- .../mpush/core/security/AesCipher.java | 53 ++++++++ .../{CipherManager.java => CipherBox.java} | 28 ++-- .../mpush/core/security/ReusableSession.java | 4 +- .../core/security/ReusableSessionManager.java | 6 +- .../mpush/core/security/RsaCipher.java | 31 +++++ .../mpush/core/netty/ClientHandler.java | 122 ++++++++---------- .../mpush/core/netty/NettyServerTest.java | 22 ++-- ...herManagerTest.java => CipherBoxTest.java} | 10 +- .../netty/client/NettyClientFactory.java | 79 ++++++------ .../mpush/netty/codec/PacketDecoder.java | 3 - .../mpush/netty/util/NettySharedHandler.java | 13 +- 36 files changed, 615 insertions(+), 334 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java rename mpush-core/src/main/java/com/shinemo/mpush/core/security/{CipherManager.java => CipherBox.java} (83%) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java rename mpush-core/src/test/java/com/shinemo/mpush/core/security/{CipherManagerTest.java => CipherBoxTest.java} (64%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java new file mode 100644 index 00000000..efe2b2c9 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java @@ -0,0 +1,12 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/28. + */ +public interface Cipher { + + byte[] decrypt(byte[] data); + + byte[] encrypt(byte[] data); + +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 8ca40ed9..95071cd9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -9,9 +9,9 @@ */ public interface Connection { - void setSessionInfo(SessionInfo info); + void setSessionInfo(SessionContext context); - SessionInfo getSessionInfo(); + SessionContext getSessionContext(); String getId(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java index 8bfc3e04..08706665 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java @@ -4,7 +4,10 @@ * Created by ohun on 2015/12/22. */ public interface Message { - - - + + Connection getConnection(); + + void send(); + + void sendRaw(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java index 05cfc614..9fcd9211 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java @@ -3,6 +3,6 @@ /** * Created by ohun on 2015/12/22. */ -public interface MessageHandler { - void handle(Request request); +public interface MessageHandler { + void handle(T message); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java new file mode 100644 index 00000000..41610a4e --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.api; + +/** + * Created by ohun on 2015/12/22. + */ +public class SessionContext { + public String osName; + public String osVersion; + public String clientVersion; + public String deviceId; + public byte[] sessionKey; + public byte[] iv; + public Cipher cipher; + + public SessionContext() { + + } + + public SessionContext(String osName, String osVersion, String clientVersion, String deviceId, byte[] sessionKey, byte[] iv) { + this.osName = osName; + this.osVersion = osVersion; + this.clientVersion = clientVersion; + this.deviceId = deviceId; + this.sessionKey = sessionKey; + this.iv = iv; + } + + public void changeCipher(Cipher cipher) { + this.cipher = cipher; + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java deleted file mode 100644 index 6dd21d5a..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionInfo.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.shinemo.mpush.api; - -/** - * Created by ohun on 2015/12/22. - */ -public class SessionInfo { - public final String osName; - public final String osVersion; - public final String clientVersion; - public final String deviceId; - public final byte[] sessionKey; - public final byte[] iv; - - public SessionInfo(String osName, String osVersion, String clientVersion, String deviceId, byte[] sessionKey, byte[] iv) { - this.osName = osName; - this.osVersion = osVersion; - this.clientVersion = clientVersion; - this.deviceId = deviceId; - this.sessionKey = sessionKey; - this.iv = iv; - } -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 6b8de5c1..c5010dab 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -18,7 +18,7 @@ public class Packet implements Serializable { public int sessionId; // 会话id。客户端生成。 public byte lrc; // 校验,纵向冗余校验。只校验body public byte[] body; - + public int getBodyLength() { return body == null ? 0 : body.length; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java index 06313ab7..4c45f61a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java @@ -3,41 +3,40 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.Receiver; -import com.shinemo.mpush.api.Request; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.handler.*; -import com.shinemo.mpush.core.message.NettyRequest; +import com.shinemo.mpush.core.message.HandShakeMessage; +import com.shinemo.mpush.core.message.HeartbeatMessage; /** * Created by ohun on 2015/12/22. */ public class MessageDispatcher implements Receiver { - public static final MessageHandler LOGIN_HANDLER = new LoginHandler(); public static final MessageHandler BIND_HANDLER = new BindHandler(); - public static final MessageHandler HEART_HANDLER = new HeartBeatHandler(); - public static final MessageHandler HAND_SHAKE_HANDLER = new HandShakeHandler(); - public static final MessageHandler FAST_CONNECT_HANDLER = new FastConnectHandler(); + public static final HeartBeatHandler HEART_HANDLER = new HeartBeatHandler(); + public static final HandShakeHandler HAND_SHAKE_HANDLER = new HandShakeHandler(); + public static final FastConnectHandler FAST_CONNECT_HANDLER = new FastConnectHandler(); @Override public void onMessage(Packet packet, Connection connection) { - Request request = new NettyRequest(packet, connection); - switch (request.getCommand()) { + Command command = Command.toCMD(packet.cmd); + switch (command) { case Heartbeat: - HEART_HANDLER.handle(request); + HEART_HANDLER.handle(new HeartbeatMessage(connection)); break; case Handshake: - HAND_SHAKE_HANDLER.handle(request); + HAND_SHAKE_HANDLER.handle(new HandShakeMessage(packet, connection)); break; case Login: - LOGIN_HANDLER.handle(request); break; case Bind: - BIND_HANDLER.handle(request); + BIND_HANDLER.handle(null); break; case Kick: break; case FastConnect: - FAST_CONNECT_HANDLER.handle(request); + FAST_CONNECT_HANDLER.handle(null); break; case Unknown: break; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index ddf70437..2e72c10c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.core; +import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.core.security.CipherBox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,7 +10,6 @@ import io.netty.channel.ChannelFutureListener; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.SessionInfo; import com.shinemo.mpush.api.protocol.Packet; /** @@ -18,7 +19,7 @@ public class NettyConnection implements Connection { private static final Logger log = LoggerFactory.getLogger(NettyConnection.class); - private SessionInfo info; + private SessionContext context; private Channel channel; private int status = 0; @@ -27,16 +28,18 @@ public class NettyConnection implements Connection { @Override public void init(Channel channel) { this.channel = channel; + this.context = new SessionContext(); + this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); } @Override - public void setSessionInfo(SessionInfo info) { - this.info = info; + public void setSessionInfo(SessionContext context) { + this.context = context; } @Override - public SessionInfo getSessionInfo() { - return info; + public SessionContext getSessionContext() { + return context; } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java index d315ae13..eba9ff65 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java @@ -1,20 +1,11 @@ package com.shinemo.mpush.core.handler; +import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.Request; /** * Created by ohun on 2015/12/22. */ -public abstract class BaseMessageHandler implements MessageHandler { - @Override - public void handle(Request request) { - T t = decodeBody(request.getBody()); - handle(t, request); - } - - public abstract T decodeBody(byte[] data); - - public abstract void handle(T body, Request request); +public abstract class BaseMessageHandler implements MessageHandler { } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java index 94ac5ba0..d2898bd8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java @@ -1,23 +1,14 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.gateway.router.RouterCenter; +import com.shinemo.mpush.api.Message; /** * Created by ohun on 2015/12/23. */ -public class BindHandler extends BaseMessageHandler { - @Override - public String decodeBody(byte[] body) { - return new String(body, Constants.UTF_8); - } +public class BindHandler extends BaseMessageHandler { @Override - public void handle(String body, Request request) { - long userId = Long.parseLong(body); - boolean success = RouterCenter.INSTANCE.publish(userId, request.getConnection()); - request.getResponse().send(new byte[]{success ? (byte) 1 : (byte) 0}); + public void handle(Message message) { + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index c5543d96..17edc928 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -1,41 +1,28 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.message.FastConnectMessage; import com.shinemo.mpush.core.security.ReusableSession; import com.shinemo.mpush.core.security.ReusableSessionManager; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; /** * Created by ohun on 2015/12/25. */ public class FastConnectHandler extends BaseMessageHandler { - @Override - public FastConnectMessage decodeBody(byte[] body) { - return Jsons.fromJson(body, FastConnectMessage.class); - } @Override - public void handle(FastConnectMessage body, Request request) { - ReusableSession session = ReusableSessionManager.INSTANCE.getSession(body.tokenId); + public void handle(FastConnectMessage message) { + ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.tokenId); if (session == null) { - request.getResponse().sendRaw("token expire".getBytes(Constants.UTF_8)); - } else if (!session.sessionInfo.deviceId.equals(body.deviceId)) { - request.getResponse().sendRaw("error device".getBytes(Constants.UTF_8)); + //message.sendRaw("token expire".getBytes(Constants.UTF_8)); + } else if (!session.sessionContext.deviceId.equals(message.deviceId)) { + //message.sendRaw("error device".getBytes(Constants.UTF_8)); } else { - request.getConnection().setSessionInfo(session.sessionInfo); + /*request.getConnection().setSessionInfo(session.sessionContext); Map resp = new HashMap(); resp.put("serverHost", MPushUtil.getLocalIp()); resp.put("serverTime", System.currentTimeMillis()); resp.put("heartbeat", Constants.HEARTBEAT_TIME); - request.getResponse().sendRaw(Jsons.toJson(resp).getBytes(Constants.UTF_8)); + request.getResponse().sendRaw(Jsons.toJson(resp).getBytes(Constants.UTF_8));*/ } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index d4563801..267d3755 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -1,23 +1,17 @@ package com.shinemo.mpush.core.handler; import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.SessionInfo; +import com.shinemo.mpush.api.SessionContext; import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.message.HandshakeSuccessMsg; -import com.shinemo.mpush.core.security.CipherManager; +import com.shinemo.mpush.core.message.HandshakeSuccessMessage; +import com.shinemo.mpush.core.security.AesCipher; +import com.shinemo.mpush.core.security.CipherBox; import com.shinemo.mpush.core.security.ReusableSession; import com.shinemo.mpush.core.security.ReusableSessionManager; -import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.crypto.AESUtils; -import com.shinemo.mpush.tools.crypto.RSAUtils; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.security.interfaces.RSAPrivateKey; - /** * Created by ohun on 2015/12/24. */ @@ -25,32 +19,26 @@ public class HandShakeHandler extends BaseMessageHandler { public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); @Override - public HandShakeMessage decodeBody(byte[] body) { - RSAPrivateKey privateKey = CipherManager.INSTANCE.getPrivateKey(); - byte[] rawData = RSAUtils.decryptByPrivateKey(body, privateKey); - return Jsons.fromJson(new String(rawData, Constants.UTF_8), HandShakeMessage.class); - } - - @Override - public void handle(HandShakeMessage body, Request request) { - byte[] iv = body.iv; - byte[] clientKey = body.clientKey; - byte[] serverKey = CipherManager.INSTANCE.randomAESKey(); - byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, serverKey);//会话密钥混淆 Client random - SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, - body.deviceId, sessionKey, iv); - request.getConnection().setSessionInfo(info); + public void handle(HandShakeMessage message) { + byte[] iv = message.iv; + byte[] clientKey = message.clientKey; + byte[] serverKey = CipherBox.INSTANCE.randomAESKey(); + byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥混淆 Client random + SessionContext info = new SessionContext(message.osName, message.osVersion, + message.clientVersion, message.deviceId, sessionKey, iv); + info.changeCipher(new AesCipher(clientKey, iv)); + message.getConnection().setSessionInfo(info); ReusableSession session = ReusableSessionManager.INSTANCE.genSession(info); ReusableSessionManager.INSTANCE.saveSession(session); - HandshakeSuccessMsg resp = new HandshakeSuccessMsg(); + HandshakeSuccessMessage resp = message.createSuccessMessage(); resp.serverKey = serverKey; resp.serverHost = MPushUtil.getLocalIp(); resp.serverTime = System.currentTimeMillis(); resp.heartbeat = Constants.HEARTBEAT_TIME; resp.sessionId = session.sessionId; resp.expireTime = session.expireTime; - byte[] responseData = AESUtils.encrypt(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey, iv); - request.getResponse().sendRaw(responseData); + resp.send(); + info.changeCipher(new AesCipher(sessionKey, iv)); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index efeb3f8e..0584b232 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -1,20 +1,14 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.core.message.HeartbeatMessage; /** * Created by ohun on 2015/12/22. */ -public class HeartBeatHandler extends BaseMessageHandler { +public class HeartBeatHandler extends BaseMessageHandler { @Override - public Void decodeBody(byte[] body) { - return null; - } - - @Override - public void handle(Void message, Request request) { + public void handle(HeartbeatMessage message) { System.err.println("receive client heartbeat, time=" + System.currentTimeMillis()); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java deleted file mode 100644 index da870175..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/LoginHandler.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.shinemo.mpush.core.handler; - -import com.shinemo.mpush.api.Request; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.message.LoginMessage; - -/** - * Created by ohun on 2015/12/22. - */ -public class LoginHandler extends BaseMessageHandler { - @Override - public LoginMessage decodeBody(byte[] body) { - return new LoginMessage(); - } - - @Override - public void handle(LoginMessage o, Request request) { - request.getResponse().send("login success".getBytes()); - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java new file mode 100644 index 00000000..ec99c413 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java @@ -0,0 +1,63 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +/** + * Created by ohun on 2015/12/28. + */ +public abstract class BaseBufferBodyMessage extends BaseMessage { + + public BaseBufferBodyMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(byte[] body) { + decode(Unpooled.wrappedBuffer(body)); + } + + @Override + public byte[] encode() { + ByteBuf body = Unpooled.buffer(); + encode(body); + return body.array(); + } + + public abstract void decode(ByteBuf body); + + public abstract void encode(ByteBuf body); + + public void encodeString(ByteBuf body, String field) { + if (field == null) { + body.writeShort(0); + } else { + body.writeShort(field.length()).writeBytes(field.getBytes(Constants.UTF_8)); + } + } + + public void encodeBytes(ByteBuf body, byte[] field) { + if (field == null || field.length == 0) { + body.writeShort(0); + } else { + body.writeShort(field.length).writeBytes(field); + } + } + + public String decodeString(ByteBuf body) { + byte[] bytes = decodeBytes(body); + if (bytes == null) return null; + return new String(bytes, Constants.UTF_8); + } + + public byte[] decodeBytes(ByteBuf body) { + int fieldLength = body.readShort(); + if (fieldLength == 0) return null; + byte[] bytes = new byte[fieldLength]; + body.readBytes(bytes); + return bytes; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java new file mode 100644 index 00000000..d4c83347 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java @@ -0,0 +1,94 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.IOUtils; + +/** + * Created by ohun on 2015/12/28. + */ +public abstract class BaseMessage implements Message { + protected final Packet message; + protected final Connection connection; + + public BaseMessage(Packet message, Connection connection) { + this.message = message; + this.connection = connection; + this.decodeBody(); + } + + protected void decodeBody() { + if (message.body != null) { + //1.解密 + byte[] tmp = message.body; + if (message.hasFlag(Constants.CRYPTO_FLAG)) { + SessionContext info = connection.getSessionContext(); + if (info.cipher != null) { + tmp = info.cipher.decrypt(tmp); + } + } + + //2.解压 + if (message.hasFlag(Constants.COMPRESS_FLAG)) { + byte[] result = IOUtils.uncompress(tmp); + if (result.length > 0) { + tmp = result; + } + } + message.body = tmp; + decode(message.body); + } + } + + protected void encodeBody() { + byte[] tmp = encode(); + if (tmp != null) { + //1.压缩 + if (tmp.length > Constants.COMPRESS_LIMIT) { + byte[] result = IOUtils.compress(tmp); + if (result.length > 0) { + tmp = result; + message.setFlag(Constants.COMPRESS_FLAG); + } + } + //2.加密 + SessionContext context = connection.getSessionContext(); + if (context.cipher != null) { + tmp = context.cipher.encrypt(tmp); + message.setFlag(Constants.CRYPTO_FLAG); + } + message.body = tmp; + } + } + + public abstract void decode(byte[] body); + + public abstract byte[] encode(); + + @Override + public Connection getConnection() { + return connection; + } + + public Packet createResponse() { + Packet packet = new Packet(); + packet.cmd = message.cmd; + packet.sessionId = message.sessionId; + return packet; + } + + @Override + public void send() { + encodeBody(); + connection.send(message); + } + + @Override + public void sendRaw() { + message.body = encode(); + connection.send(message); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java index e229191e..76f192a8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java @@ -1,11 +1,27 @@ package com.shinemo.mpush.core.message; -import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; /** * Created by ohun on 2015/12/25. */ -public class FastConnectMessage implements Message { +public class FastConnectMessage extends BaseBufferBodyMessage { public String tokenId; public String deviceId; + + public FastConnectMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + + } + + @Override + public void encode(ByteBuf body) { + + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java index 0615ed73..ff98633e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java @@ -1,16 +1,54 @@ package com.shinemo.mpush.core.message; -import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; /** * Created by ohun on 2015/12/24. */ -public class HandShakeMessage implements Message { +public class HandShakeMessage extends BaseBufferBodyMessage { public String deviceId; public String osName; public String osVersion; public String clientVersion; - public byte[] clientKey; public byte[] iv; + public byte[] clientKey; public long timestamp; + + public HandShakeMessage(int sessionId, Connection connection) { + super(new Packet(), connection); + super.message.cmd = Command.Handshake.cmd; + super.message.sessionId = sessionId; + } + + public HandShakeMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + deviceId = decodeString(body); + osName = decodeString(body); + osVersion = decodeString(body); + clientVersion = decodeString(body); + iv = decodeBytes(body); + clientKey = decodeBytes(body); + timestamp = body.readLong(); + } + + public void encode(ByteBuf body) { + encodeString(body, deviceId); + encodeString(body, osName); + encodeString(body, osVersion); + encodeString(body, clientVersion); + encodeBytes(body, iv); + encodeBytes(body, clientKey); + body.writeLong(timestamp); + } + + public HandshakeSuccessMessage createSuccessMessage() { + return new HandshakeSuccessMessage(createResponse(), getConnection()); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java new file mode 100644 index 00000000..689a5363 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java @@ -0,0 +1,41 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +/** + * Created by ohun on 2015/12/27. + */ +public class HandshakeSuccessMessage extends BaseBufferBodyMessage { + public byte[] serverKey; + public String serverHost; + public long serverTime; + public int heartbeat; + public String sessionId; + public long expireTime; + + public HandshakeSuccessMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + serverKey = decodeBytes(body); + serverHost = decodeString(body); + serverTime = body.readLong(); + heartbeat = body.readInt(); + sessionId = decodeString(body); + expireTime = body.readLong(); + } + + @Override + public void encode(ByteBuf body) { + encodeBytes(body, serverKey); + encodeString(body, serverHost); + body.writeLong(serverTime); + body.writeInt(heartbeat); + encodeString(body, sessionId); + body.writeLong(expireTime); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java deleted file mode 100644 index 4ad73e52..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMsg.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shinemo.mpush.core.message; - -/** - * Created by ohun on 2015/12/27. - */ -public class HandshakeSuccessMsg { - public byte[] serverKey; - public String serverHost; - public long serverTime; - public int heartbeat; - public String sessionId; - public long expireTime; -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java new file mode 100644 index 00000000..bf6f1a43 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.protocol.Command; + +/** + * Created by ohun on 2015/12/28. + */ +public class HeartbeatMessage implements Message { + private final Connection connection; + + public HeartbeatMessage(Connection connection) { + this.connection = connection; + } + + @Override + public Connection getConnection() { + return connection; + } + + @Override + public void send() { + + } + + @Override + public void sendRaw() { + + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java deleted file mode 100644 index 991b4c23..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/LoginMessage.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.shinemo.mpush.core.message; - -import com.shinemo.mpush.api.Message; - -/** - * Created by ohun on 2015/12/22. - */ -public class LoginMessage implements Message { -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java index 685eccc8..9af67f72 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java @@ -30,7 +30,7 @@ public byte[] getBody() { //1.解密 byte[] tmp = message.body; if (message.hasFlag(Constants.CRYPTO_FLAG)) { - SessionInfo info = connection.getSessionInfo(); + SessionContext info = connection.getSessionContext(); if (info != null && info.sessionKey != null) { tmp = AESUtils.decrypt(tmp, info.sessionKey, info.iv); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java index ff8acd5e..89b0d046 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java @@ -3,7 +3,7 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Response; -import com.shinemo.mpush.api.SessionInfo; +import com.shinemo.mpush.api.SessionContext; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.tools.IOUtils; import com.shinemo.mpush.tools.crypto.AESUtils; @@ -31,7 +31,7 @@ public void send(byte[] body) { } } //2.加密 - SessionInfo info = connection.getSessionInfo(); + SessionContext info = connection.getSessionContext(); if (info != null && info.sessionKey != null) { tmp = AESUtils.encrypt(tmp, info.sessionKey, info.iv); packet.setFlag(Constants.CRYPTO_FLAG); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java new file mode 100644 index 00000000..e28d6c84 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java @@ -0,0 +1,53 @@ +package com.shinemo.mpush.core.security; + +import com.shinemo.mpush.api.Cipher; +import com.shinemo.mpush.tools.Strings; +import com.shinemo.mpush.tools.crypto.AESUtils; +import com.shinemo.mpush.tools.crypto.Base64Utils; + +/** + * Created by ohun on 2015/12/28. + */ +public class AesCipher implements Cipher { + private final byte[] key; + private final byte[] iv; + + public AesCipher(byte[] key, byte[] iv) { + this.key = key; + this.iv = iv; + } + + @Override + public byte[] decrypt(byte[] data) { + return AESUtils.decrypt(data, key, iv); + } + + @Override + public byte[] encrypt(byte[] data) { + return AESUtils.encrypt(data, key, iv); + } + + + public static String encodeCipher(AesCipher aesCipher) { + try { + return Base64Utils.encode(aesCipher.key) + " " + Base64Utils.encode(aesCipher.iv); + } catch (Exception e) { + return Strings.EMPTY; + } + } + + public static AesCipher decodeCipher(String keys) { + if (Strings.isBlank(keys)) return null; + String[] array = keys.split(" "); + if (array.length != 2) return null; + try { + byte[] key = Base64Utils.decode(array[0]); + byte[] iv = Base64Utils.decode(array[1]); + if (key.length == AESUtils.AES_KEY_LENGTH && iv.length == AESUtils.AES_KEY_LENGTH) { + return new AesCipher(key, iv); + } + } catch (Exception e) { + } + return null; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java similarity index 83% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java index d7f0507b..308bc24b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java @@ -14,13 +14,17 @@ /** * Created by ohun on 2015/12/24. */ -public class CipherManager { - public static final CipherManager INSTANCE = new CipherManager(); +public class CipherBox { + public static final CipherBox INSTANCE = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; private RSAPublicKey publicKey; + public CipherBox() { + init(); + } + public void init() { readFromFile(); if (publicKey == null || privateKey == null) { @@ -43,8 +47,8 @@ public void init() { private void writeToFile() { try { - String publicKeyStr = RSAUtils.encodeBase64(INSTANCE.publicKey); - String privateKeyStr = RSAUtils.encodeBase64(INSTANCE.privateKey); + String publicKeyStr = RSAUtils.encodeBase64(publicKey); + String privateKeyStr = RSAUtils.encodeBase64(privateKey); String path = this.getClass().getResource("/").getPath(); FileOutputStream out = new FileOutputStream(new File(path, "private.key")); out.write(privateKeyStr.getBytes()); @@ -64,11 +68,11 @@ private void readFromFile() { byte[] buffer = new byte[in.available()]; in.read(buffer); in.close(); - INSTANCE.privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(new String(buffer)); + privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(new String(buffer)); in = this.getClass().getResourceAsStream("/public.key"); in.read(buffer); in.close(); - INSTANCE.publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(new String(buffer)); + publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(new String(buffer)); System.out.println("save privateKey=" + privateKey); System.out.println("save publicKey=" + publicKey); } catch (Exception e) { @@ -77,13 +81,13 @@ private void readFromFile() { } public RSAPrivateKey getPrivateKey() { - if (INSTANCE.privateKey == null) init(); - return INSTANCE.privateKey; + if (privateKey == null) init(); + return privateKey; } public RSAPublicKey getPublicKey() { - if (INSTANCE.publicKey == null) init(); - return INSTANCE.publicKey; + if (publicKey == null) init(); + return publicKey; } public byte[] randomAESKey() { @@ -109,4 +113,8 @@ public byte[] mixKey(byte[] clientKey, byte[] serverKey) { } return sessionKey; } + + public RsaCipher getRsaCipher() { + return new RsaCipher(privateKey, publicKey); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java index 3ab7079b..c6e3d85b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.security; -import com.shinemo.mpush.api.SessionInfo; +import com.shinemo.mpush.api.SessionContext; /** * Created by ohun on 2015/12/25. @@ -8,6 +8,6 @@ public class ReusableSession { public transient String sessionId; public long expireTime; - public SessionInfo sessionInfo; + public SessionContext sessionContext; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java index e0312926..38c9a960 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.security; -import com.shinemo.mpush.api.SessionInfo; +import com.shinemo.mpush.api.SessionContext; import com.shinemo.mpush.tools.crypto.MD5Utils; import java.util.Map; @@ -23,7 +23,7 @@ public ReusableSession getSession(String sessionId) { return tokenCache.get(sessionId); } - public ReusableSession genSession(SessionInfo info) { + public ReusableSession genSession(SessionContext info) { /** * 先生成key,需要保证半个周期内同一个设备生成的key是相同的 */ @@ -31,7 +31,7 @@ public ReusableSession genSession(SessionInfo info) { StringBuilder sb = new StringBuilder(); sb.append(info.deviceId).append(partition); ReusableSession v = new ReusableSession(); - v.sessionInfo = info; + v.sessionContext = info; v.sessionId = MD5Utils.encrypt(sb.toString()); /** * 计算失效时间 diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java new file mode 100644 index 00000000..43aa28f4 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.core.security; + +import com.shinemo.mpush.api.Cipher; +import com.shinemo.mpush.tools.crypto.RSAUtils; + +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; + +/** + * Created by ohun on 2015/12/28. + */ +public class RsaCipher implements Cipher { + private final RSAPrivateKey privateKey; + + private final RSAPublicKey publicKey; + + public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { + this.privateKey = privateKey; + this.publicKey = publicKey; + } + + @Override + public byte[] decrypt(byte[] data) { + return RSAUtils.decryptByPrivateKey(data, privateKey); + } + + @Override + public byte[] encrypt(byte[] data) { + return RSAUtils.encryptByPublicKey(data, publicKey); + } +} diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java index 5ec55435..7262f32c 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java @@ -1,17 +1,15 @@ package com.shinemo.mpush.core.netty; -import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.message.FastConnectMessage; +import com.shinemo.mpush.core.NettyConnection; import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.message.HandshakeSuccessMsg; -import com.shinemo.mpush.core.security.CipherManager; -import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.core.message.HandshakeSuccessMessage; +import com.shinemo.mpush.core.security.AesCipher; +import com.shinemo.mpush.core.security.CipherBox; import com.shinemo.mpush.tools.Strings; -import com.shinemo.mpush.tools.crypto.AESUtils; -import com.shinemo.mpush.tools.crypto.RSAUtils; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; @@ -21,45 +19,28 @@ import java.io.*; import java.net.SocketAddress; -import java.security.interfaces.RSAPublicKey; /** * Created by ohun on 2015/12/24. */ public class ClientHandler implements Handler { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class); - private byte[] clientKey = CipherManager.INSTANCE.randomAESKey(); - private byte[] iv = CipherManager.INSTANCE.randomAESIV(); + private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); + private byte[] iv = CipherBox.INSTANCE.randomAESIV(); + private Connection connection = new NettyConnection(); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { -// String token = getToken(); - //if (!Strings.isBlank(token)) { - RSAPublicKey publicKey = CipherManager.INSTANCE.getPublicKey(); - HandShakeMessage message = new HandShakeMessage(); - message.clientKey = clientKey; - message.iv = iv; - message.clientVersion = "1.0.1"; - message.deviceId = "1111111111111"; - message.osName = "android"; - message.osVersion = "5.0"; - message.timestamp = System.currentTimeMillis(); - - Packet packet = new Packet(); - packet.cmd = Command.Handshake.cmd; - packet.sessionId = 1; - packet.body = RSAUtils.encryptByPublicKey(Jsons.toJson(message).getBytes(Constants.UTF_8), publicKey); - ctx.writeAndFlush(packet); -// } else { -// FastConnectMessage message = new FastConnectMessage(); -// message.deviceId = "1111111111111"; -// message.tokenId = token; -// Packet packet = new Packet(); -// packet.cmd = Command.FastConnect.cmd; -// packet.sessionId = 1; -// packet.body = Jsons.toJson(message).getBytes(Constants.UTF_8); -// ctx.writeAndFlush(packet); -// } + connection.init(ctx.channel()); + HandShakeMessage message = new HandShakeMessage(1, connection); + message.clientKey = clientKey; + message.iv = iv; + message.clientVersion = "1.0.1"; + message.deviceId = "1111111111111"; + message.osName = "android"; + message.osVersion = "5.0"; + message.timestamp = System.currentTimeMillis(); + message.send(); LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelActive"); } @@ -75,17 +56,16 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); if (command == Command.Handshake) { - String raw = new String(AESUtils.decrypt(packet.body, clientKey, iv), Constants.UTF_8); - HandshakeSuccessMsg resp = Jsons.fromJson(raw, HandshakeSuccessMsg.class); - LOGGER.info("hand shake success, message=" + raw); - byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, resp.serverKey); + connection.getSessionContext().changeCipher(new AesCipher(clientKey, iv)); + HandshakeSuccessMessage resp = new HandshakeSuccessMessage(packet, connection); + byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, resp.serverKey); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, resp.serverKey); saveToken(resp.sessionId); + connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); } else if (command == Command.FastConnect) { LOGGER.info("fast connect success, message=" + packet.getStringBody()); } } - } @@ -110,33 +90,33 @@ private String getToken() { return Strings.EMPTY; } - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void channelRegistered(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - - } - - @Override - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - - } - - @Override - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - - } + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void channelRegistered(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { + + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + + } + + @Override + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { + + } + + @Override + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + + } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index ea16478a..e75b4278 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,37 +1,35 @@ package com.shinemo.mpush.core.netty; -import org.junit.Test; - import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.core.MessageDispatcher; -import com.shinemo.mpush.core.MessageReceiver; import com.shinemo.mpush.core.handler.ServerHandler; import com.shinemo.mpush.netty.server.NettyServer; +import org.junit.Test; /** * Created by ohun on 2015/12/24. */ public class NettyServerTest { - @Test + @Test public void testStop() throws Exception { } - @Test + @Test public void testStart() throws Exception { - - Receiver receiver = new MessageDispatcher(); - Handler handler = new ServerHandler(receiver); - - final NettyServer server = new NettyServer(3000,handler); + + Receiver receiver = new MessageDispatcher(); + Handler handler = new ServerHandler(receiver); + + final NettyServer server = new NettyServer(3000, handler); server.start(); - + Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { - server.stop(); + server.stop(); } catch (Exception e) { } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java similarity index 64% rename from mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java index ee563abd..d23e8e56 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherManagerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java @@ -7,19 +7,19 @@ /** * Created by ohun on 2015/12/25. */ -public class CipherManagerTest { +public class CipherBoxTest { @Test public void testGetPrivateKey() throws Exception { - CipherManager.INSTANCE.getPrivateKey(); + CipherBox.INSTANCE.getPrivateKey(); } @Test public void testGetPublicKey() throws Exception { for (int i = 0; i < 1000; i++) { - byte[] clientKey = CipherManager.INSTANCE.randomAESKey(); - byte[] serverKey = CipherManager.INSTANCE.randomAESKey(); - byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, serverKey); + byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); + byte[] serverKey = CipherBox.INSTANCE.randomAESKey(); + byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey); //System.out.println("clientKey:" + Arrays.toString(clientKey)); //System.out.println("serverKey:" + Arrays.toString(serverKey)); System.out.println("sessionKey:" + Arrays.toString(sessionKey)); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 38c70cc6..53db14f3 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -2,6 +2,7 @@ import java.net.InetSocketAddress; +import io.netty.buffer.PooledByteBufAllocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,25 +23,25 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -public class NettyClientFactory extends AbstractNettyClientFactory{ - - private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); +public class NettyClientFactory extends AbstractNettyClientFactory { - public static NettyClientFactory instance = new NettyClientFactory(); + private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); - protected Client createClient(final String host,final int port,final Handler handler) throws Exception { + public static NettyClientFactory instance = new NettyClientFactory(); + + protected Client createClient(final String host, final int port, final Handler handler) throws Exception { EventLoopGroup workerGroup = new NioEventLoopGroup(); - final Bootstrap bootstrap = new Bootstrap(); - final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); - bootstrap.group(workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, NettySharedHolder.byteBufAllocator)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) + final Bootstrap bootstrap = new Bootstrap(); + final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); @@ -48,30 +49,30 @@ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(nettySharedHandler); } }); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - Client client = new NettyClient(host,port, channel); - return client; - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + host+","+port); - } - throw new Exception("create client exception"); - } - public Client getClient(final Client client) throws Exception { - return get(client.getRemoteHost(),client.getRemotePort()); - } + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { + Channel channel = future.channel(); + Client client = new NettyClient(host, port, channel); + return client; + } else { + future.cancel(true); + future.channel().close(); + log.warn("[remoting] failure to connect:" + host + "," + port); + } + throw new Exception("create client exception"); + } + + public Client getClient(final Client client) throws Exception { + return get(client.getRemoteHost(), client.getRemotePort()); + } + + public void remove(final Client client) { + super.remove(client); + } + + public void close(final Client client) { - public void remove(final Client client) { - super.remove(client); - } - - public void close(final Client client){ - - } + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index f77a7e88..ace1d155 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -50,9 +50,6 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { private Packet decodeFrame(ByteBuf in) throws Exception { int bufferSize = in.readableBytes(); - /*if (in.readShort() != Constants.MAGIC_NUM) { - throw new RuntimeException("ERROR MAGIC_NUM"); - }*/ int bodyLength = in.readInt(); if (bufferSize < (bodyLength + Constants.HEADER_LEN)) { throw new DecodeException("invalid frame"); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java index 3d8990d2..20627b51 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java @@ -1,18 +1,13 @@ package com.shinemo.mpush.netty.util; -import java.net.SocketAddress; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.shinemo.mpush.api.protocol.Handler; - import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @ChannelHandler.Sharable public class NettySharedHandler extends ChannelHandlerAdapter{ From 61af041873e21467eba839ad90889752cc71e4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 15:39:45 +0000 Subject: [PATCH 038/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/api/Connection.java | 8 +- .../java/com/shinemo/mpush/api/Receiver.java | 2 +- .../com/shinemo/mpush/api/RouterManager.java | 6 +- .../com/shinemo/mpush/api/SessionContext.java | 32 ++- .../shinemo/mpush/api/protocol/Command.java | 21 +- .../shinemo/mpush/api/protocol/Packet.java | 9 + .../shinemo/mpush/core/ConnectionManager.java | 38 ++-- .../shinemo/mpush/core/MessageDispatcher.java | 55 +++--- .../shinemo/mpush/core/NettyConnection.java | 16 +- .../mpush/core/handler/BindHandler.java | 29 ++- .../core/handler/FastConnectHandler.java | 26 ++- .../mpush/core/handler/HandShakeHandler.java | 70 +++++-- .../mpush/core/handler/HeartBeatHandler.java | 6 +- .../mpush/core/handler/ServerHandler.java | 2 +- .../mpush/core/message/BaseMessage.java | 10 +- .../mpush/core/message/BindMessage.java | 31 +++ ...erBodyMessage.java => ByteBufMessage.java} | 28 ++- .../mpush/core/message/ErrorMessage.java | 58 ++++++ .../core/message/FastConnectMessage.java | 10 +- .../message/FastConnectSuccessMessage.java | 52 +++++ .../mpush/core/message/HandShakeMessage.java | 14 +- .../core/message/HandshakeSuccessMessage.java | 48 ++++- .../mpush/core/message/HeartbeatMessage.java | 2 +- .../mpush/core/message/NettyRequest.java | 8 +- .../mpush/core/message/NettyResponse.java | 6 +- .../mpush/core/message/SuccessMessage.java | 40 ++++ .../mpush/core/request/HandshakeRequest.java | 21 -- .../mpush/core/security/AesCipher.java | 4 +- .../mpush/core/security/CipherBox.java | 12 +- .../mpush/core/security/ReusableSession.java | 4 +- .../core/security/ReusableSessionManager.java | 2 +- .../mpush/core/security/RsaCipher.java | 2 +- .../mpush/core/netty/ClientHandler.java | 4 +- .../gateway/router/LocalRouterManager.java | 11 +- .../gateway/router/RemoteRouterManager.java | 9 +- .../mpush/gateway/router/RouterCenter.java | 6 +- .../mpush/netty/client/NettyClient.java | 182 +++++++++--------- .../mpush/netty/codec/PacketDecoder.java | 7 +- .../mpush/netty/codec/PacketEncoder.java | 2 +- .../shinemo/mpush/tools/crypto/AESUtils.java | 1 - .../mpush/tools/crypto/AESUtilsTest.java | 2 +- 41 files changed, 601 insertions(+), 295 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java rename mpush-core/src/main/java/com/shinemo/mpush/core/message/{BaseBufferBodyMessage.java => ByteBufMessage.java} (70%) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 95071cd9..7430ef0f 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -9,14 +9,16 @@ */ public interface Connection { - void setSessionInfo(SessionContext context); + void init(Channel channel); SessionContext getSessionContext(); - String getId(); + void setSessionContext(SessionContext context); void send(Packet packet); + String getId(); + boolean isClosed(); boolean isOpen(); @@ -29,8 +31,6 @@ public interface Connection { boolean isEnable(); - void init(Channel channel); - String remoteIp(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java index 6925ba9f..5929f7a0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java @@ -6,5 +6,5 @@ * Created by ohun on 2015/12/22. */ public interface Receiver { - void onMessage(Packet packet, Connection connection); + void onReceive(Packet packet, Connection connection); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java index cee7cf89..fc6d9448 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java @@ -5,9 +5,9 @@ */ public interface RouterManager { - boolean publish(long userId, Router route); + boolean publish(String userId, Router route); - boolean unPublish(long userId); + boolean unPublish(String userId); - Router getRouter(long userId); + Router getRouter(String userId); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java index 41610a4e..1d84f280 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.api; +import com.google.common.base.Strings; + /** * Created by ohun on 2015/12/22. */ @@ -8,24 +10,44 @@ public class SessionContext { public String osVersion; public String clientVersion; public String deviceId; - public byte[] sessionKey; - public byte[] iv; public Cipher cipher; public SessionContext() { } - public SessionContext(String osName, String osVersion, String clientVersion, String deviceId, byte[] sessionKey, byte[] iv) { + public SessionContext(String osName, String osVersion, String clientVersion, String deviceId) { this.osName = osName; this.osVersion = osVersion; this.clientVersion = clientVersion; this.deviceId = deviceId; - this.sessionKey = sessionKey; - this.iv = iv; } public void changeCipher(Cipher cipher) { this.cipher = cipher; } + + public SessionContext setOsName(String osName) { + this.osName = osName; + return this; + } + + public SessionContext setOsVersion(String osVersion) { + this.osVersion = osVersion; + return this; + } + + public SessionContext setClientVersion(String clientVersion) { + this.clientVersion = clientVersion; + return this; + } + + public SessionContext setDeviceId(String deviceId) { + this.deviceId = deviceId; + return this; + } + + public boolean handshakeOk() { + return !Strings.isNullOrEmpty(deviceId); + } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index dbacad21..45fe33af 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -4,15 +4,16 @@ * Created by ohun on 2015/12/22. */ public enum Command { - Heartbeat(1), - Handshake(2), - Login(3), - Logout(4), - Bind(5), - Unbind(6), - Kick(7), - FastConnect(8), - Unknown(-1); + HEARTBEAT(1), + HANDSHAKE(2), + LOGIN(3), + LOGOUT(4), + BIND(5), + UNBIND(6), + KICK(7), + FAST_CONNECT(8), + ERROR(9), + UNKNOWN(-1); Command(int cmd) { this.cmd = (byte) cmd; @@ -22,6 +23,6 @@ public enum Command { public static Command toCMD(byte b) { if (b > 0 && b < values().length) return values()[b - 1]; - return Unknown; + return UNKNOWN; } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index c5010dab..e706e8d2 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -19,6 +19,15 @@ public class Packet implements Serializable { public byte lrc; // 校验,纵向冗余校验。只校验body public byte[] body; + public Packet(byte cmd) { + this.cmd = cmd; + } + + public Packet(byte cmd, int sessionId) { + this.cmd = cmd; + this.sessionId = sessionId; + } + public int getBodyLength() { return body == null ? 0 : body.length; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index 6a32a69e..b08f2df9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -16,42 +16,42 @@ */ public class ConnectionManager { public static final ConnectionManager INSTANCE = new ConnectionManager(); - + //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8(); public Connection get(final String channelId) throws ExecutionException { - return connections.get(channelId); + return connections.get(channelId); } - - public Connection get(final Channel channel){ - return connections.get(channel.id().asLongText()); + + public Connection get(final Channel channel) { + return connections.get(channel.id().asLongText()); } public void add(Connection connection) { - connections.putIfAbsent(connection.getId(), connection); + connections.putIfAbsent(connection.getId(), connection); } - - public void add(Channel channel){ + + public void add(Channel channel) { Connection connection = new NettyConnection(); connection.init(channel); connections.putIfAbsent(connection.getId(), connection); } public void remove(Connection connection) { - connections.remove(connection.getId()); + connections.remove(connection.getId()); } - - public void remove(Channel channel){ - connections.remove(channel.id().asLongText()); + + public void remove(Channel channel) { + connections.remove(channel.id().asLongText()); } - - public List getConnectionIds(){ - return new ArrayList(connections.keySet()); + + public List getConnectionIds() { + return new ArrayList(connections.keySet()); } - - public List getConnections(){ - return new ArrayList(connections.values()); + + public List getConnections() { + return new ArrayList(connections.values()); } - + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java index 4c45f61a..36dae7a7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java @@ -1,45 +1,50 @@ package com.shinemo.mpush.core; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.handler.*; +import com.shinemo.mpush.core.message.BindMessage; +import com.shinemo.mpush.core.message.FastConnectMessage; import com.shinemo.mpush.core.message.HandShakeMessage; import com.shinemo.mpush.core.message.HeartbeatMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/22. */ public class MessageDispatcher implements Receiver { - public static final MessageHandler BIND_HANDLER = new BindHandler(); - public static final HeartBeatHandler HEART_HANDLER = new HeartBeatHandler(); - public static final HandShakeHandler HAND_SHAKE_HANDLER = new HandShakeHandler(); - public static final FastConnectHandler FAST_CONNECT_HANDLER = new FastConnectHandler(); + public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); + public final BindHandler bindHandler = new BindHandler(); + public final HandShakeHandler handShakeHandler = new HandShakeHandler(); + public final FastConnectHandler fastConnectHandler = new FastConnectHandler(); + public final HeartBeatHandler heartBeatHandler = new HeartBeatHandler(); @Override - public void onMessage(Packet packet, Connection connection) { + public void onReceive(Packet packet, Connection connection) { Command command = Command.toCMD(packet.cmd); - switch (command) { - case Heartbeat: - HEART_HANDLER.handle(new HeartbeatMessage(connection)); - break; - case Handshake: - HAND_SHAKE_HANDLER.handle(new HandShakeMessage(packet, connection)); - break; - case Login: - break; - case Bind: - BIND_HANDLER.handle(null); - break; - case Kick: - break; - case FastConnect: - FAST_CONNECT_HANDLER.handle(null); - break; - case Unknown: - break; + try { + switch (command) { + case HEARTBEAT: + heartBeatHandler.handle(new HeartbeatMessage(connection)); + break; + case HANDSHAKE: + handShakeHandler.handle(new HandShakeMessage(packet, connection)); + break; + case BIND: + bindHandler.handle(new BindMessage(packet, connection)); + break; + case FAST_CONNECT: + fastConnectHandler.handle(new FastConnectMessage(packet, connection)); + break; + case UNKNOWN: + break; + } + } catch (Throwable throwable) { + LOGGER.error("dispatch message ex, packet={},conn={}", packet, connection, throwable); + connection.close(); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 2e72c10c..4092f961 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -15,9 +15,8 @@ /** * Created by ohun on 2015/12/22. */ -public class NettyConnection implements Connection { - - private static final Logger log = LoggerFactory.getLogger(NettyConnection.class); +public final class NettyConnection implements Connection { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); private SessionContext context; private Channel channel; @@ -33,7 +32,7 @@ public void init(Channel channel) { } @Override - public void setSessionInfo(SessionContext context) { + public void setSessionContext(SessionContext context) { this.context = context; } @@ -58,17 +57,18 @@ public void send(final Packet packet) { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { if (!channel.isActive()) { - log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); + LOGGER.warn("send msg failed, channel is not active clientIp={}, packet={}", channel.remoteAddress().toString(), packet); ConnectionManager.INSTANCE.remove(channel); + channel.close(); } - log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet); + LOGGER.warn("send msg failed clientIp={}, packet={}", channel.remoteAddress().toString(), packet); } else { - log.warn("send msg success:" + channel.remoteAddress().toString() + "," + packet); + LOGGER.warn("send msg success clientIp={}, packet={}", channel.remoteAddress().toString(), packet); } } }); } else { - log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet + ", channel is not writable"); + LOGGER.warn("send msg failed, channel is not writable clientIp={}, packet={}", channel.remoteAddress().toString(), packet); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java index d2898bd8..97299d97 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java @@ -1,14 +1,35 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Message; +import com.google.common.base.Strings; +import com.shinemo.mpush.api.MessageHandler; +import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.core.message.BindMessage; +import com.shinemo.mpush.core.message.ErrorMessage; +import com.shinemo.mpush.core.message.SuccessMessage; +import com.shinemo.mpush.gateway.router.RouterCenter; /** * Created by ohun on 2015/12/23. */ -public class BindHandler extends BaseMessageHandler { +public final class BindHandler implements MessageHandler { @Override - public void handle(Message message) { - + public void handle(BindMessage message) { + if (Strings.isNullOrEmpty(message.userId)) { + ErrorMessage.from(message).setReason("invalid param").send(); + return; + } + SessionContext context = message.getConnection().getSessionContext(); + if (context.handshakeOk()) { + boolean success = RouterCenter.INSTANCE.publish(message.userId, message.getConnection()); + if (success) { + SuccessMessage.from(message).setData("bind success").send(); + //TODO kick user + } else { + ErrorMessage.from(message).setReason("bind failed").send(); + } + } else { + ErrorMessage.from(message).setReason("not handshake").send(); + } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 17edc928..194132e5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -1,28 +1,34 @@ package com.shinemo.mpush.core.handler; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.MessageHandler; +import com.shinemo.mpush.core.message.ErrorMessage; import com.shinemo.mpush.core.message.FastConnectMessage; +import com.shinemo.mpush.core.message.FastConnectSuccessMessage; import com.shinemo.mpush.core.security.ReusableSession; import com.shinemo.mpush.core.security.ReusableSessionManager; +import com.shinemo.mpush.tools.MPushUtil; /** * Created by ohun on 2015/12/25. */ -public class FastConnectHandler extends BaseMessageHandler { +public final class FastConnectHandler implements MessageHandler { @Override public void handle(FastConnectMessage message) { - ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.tokenId); + ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); if (session == null) { - //message.sendRaw("token expire".getBytes(Constants.UTF_8)); + ErrorMessage.from(message).setReason("token expire").send(); } else if (!session.sessionContext.deviceId.equals(message.deviceId)) { - //message.sendRaw("error device".getBytes(Constants.UTF_8)); + ErrorMessage.from(message).setReason("error device").send(); } else { - /*request.getConnection().setSessionInfo(session.sessionContext); - Map resp = new HashMap(); - resp.put("serverHost", MPushUtil.getLocalIp()); - resp.put("serverTime", System.currentTimeMillis()); - resp.put("heartbeat", Constants.HEARTBEAT_TIME); - request.getResponse().sendRaw(Jsons.toJson(resp).getBytes(Constants.UTF_8));*/ + message.getConnection().setSessionContext(session.sessionContext); + FastConnectSuccessMessage + .from(message) + .setServerHost(MPushUtil.getLocalIp()) + .setServerTime(System.currentTimeMillis()) + .setHeartbeat(Constants.HEARTBEAT_TIME) + .send(); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 267d3755..261eb308 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -1,7 +1,10 @@ package com.shinemo.mpush.core.handler; +import com.google.common.base.Strings; import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.core.message.ErrorMessage; import com.shinemo.mpush.core.message.HandShakeMessage; import com.shinemo.mpush.core.message.HandshakeSuccessMessage; import com.shinemo.mpush.core.security.AesCipher; @@ -15,31 +18,58 @@ /** * Created by ohun on 2015/12/24. */ -public class HandShakeHandler extends BaseMessageHandler { +public final class HandShakeHandler implements MessageHandler { public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); @Override public void handle(HandShakeMessage message) { - byte[] iv = message.iv; - byte[] clientKey = message.clientKey; - byte[] serverKey = CipherBox.INSTANCE.randomAESKey(); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥混淆 Client random - SessionContext info = new SessionContext(message.osName, message.osVersion, - message.clientVersion, message.deviceId, sessionKey, iv); - info.changeCipher(new AesCipher(clientKey, iv)); - message.getConnection().setSessionInfo(info); - ReusableSession session = ReusableSessionManager.INSTANCE.genSession(info); + byte[] iv = message.iv;//AES密钥向量16 + byte[] clientKey = message.clientKey;//客户端随机数 + byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数 + byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥 + + //1.校验客户端消息字段 + if (Strings.isNullOrEmpty(message.deviceId) + || iv.length != CipherBox.AES_KEY_LENGTH + || clientKey.length != CipherBox.AES_KEY_LENGTH) { + ErrorMessage.from(message).setReason("Param invalid").send(); + return; + } + + //2.重复握手判断 + SessionContext context = message.getConnection().getSessionContext(); + if (message.deviceId.equals(context.deviceId)) { + ErrorMessage.from(message).setReason("Repeat handshake").send(); + return; + } + + //3.更换会话密钥RSA=>AES(clientKey) + context.changeCipher(new AesCipher(clientKey, iv)); + + //4.生成可复用session, 用于快速重连 + ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); ReusableSessionManager.INSTANCE.saveSession(session); - HandshakeSuccessMessage resp = message.createSuccessMessage(); - resp.serverKey = serverKey; - resp.serverHost = MPushUtil.getLocalIp(); - resp.serverTime = System.currentTimeMillis(); - resp.heartbeat = Constants.HEARTBEAT_TIME; - resp.sessionId = session.sessionId; - resp.expireTime = session.expireTime; - resp.send(); - info.changeCipher(new AesCipher(sessionKey, iv)); - LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); + //5.响应握手成功消息 + HandshakeSuccessMessage + .from(message) + .setServerKey(serverKey) + .setServerHost(MPushUtil.getLocalIp()) + .setServerTime(System.currentTimeMillis()) + .setHeartbeat(Constants.HEARTBEAT_TIME) + .setSessionId(session.sessionId) + .setExpireTime(session.expireTime) + .send(); + + //6.更换会话密钥AES(clientKey)=>AES(sessionKey) + context.changeCipher(new AesCipher(sessionKey, iv)); + + //7.保存client信息 + context.setOsName(message.osName) + .setOsVersion(message.osVersion) + .setClientVersion(message.clientVersion) + .setDeviceId(message.deviceId); + + LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 0584b232..313661ec 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -1,15 +1,17 @@ package com.shinemo.mpush.core.handler; +import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.core.message.HeartbeatMessage; /** * Created by ohun on 2015/12/22. */ -public class HeartBeatHandler extends BaseMessageHandler { +public final class HeartBeatHandler implements MessageHandler { @Override public void handle(HeartbeatMessage message) { - System.err.println("receive client heartbeat, time=" + System.currentTimeMillis()); + System.err.println("receive client heartbeat, time=" + + System.currentTimeMillis()); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java index 9947ea9a..b4bf29a6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java @@ -30,7 +30,7 @@ public ServerHandler(Receiver receiver) { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); - receiver.onMessage((Packet) msg, connection); + receiver.onReceive((Packet) msg, connection); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java index d4c83347..fc3db7d5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java @@ -21,7 +21,7 @@ public BaseMessage(Packet message, Connection connection) { } protected void decodeBody() { - if (message.body != null) { + if (message.body != null && message.body.length > 0) { //1.解密 byte[] tmp = message.body; if (message.hasFlag(Constants.CRYPTO_FLAG)) { @@ -45,7 +45,7 @@ protected void decodeBody() { protected void encodeBody() { byte[] tmp = encode(); - if (tmp != null) { + if (tmp != null && tmp.length > 0) { //1.压缩 if (tmp.length > Constants.COMPRESS_LIMIT) { byte[] result = IOUtils.compress(tmp); @@ -54,6 +54,7 @@ protected void encodeBody() { message.setFlag(Constants.COMPRESS_FLAG); } } + //2.加密 SessionContext context = connection.getSessionContext(); if (context.cipher != null) { @@ -74,10 +75,7 @@ public Connection getConnection() { } public Packet createResponse() { - Packet packet = new Packet(); - packet.cmd = message.cmd; - packet.sessionId = message.sessionId; - return packet; + return new Packet(message.cmd, message.sessionId); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java new file mode 100644 index 00000000..b4f34df6 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.core.message; + +import com.google.common.base.Strings; +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/28. + */ +public final class BindMessage extends BaseMessage { + public String userId; + + public BindMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(byte[] body) { + if (body != null && body.length > 0) { + userId = new String(body, Constants.UTF_8); + } + } + + @Override + public byte[] encode() { + return Strings.isNullOrEmpty(userId) + ? null : + userId.getBytes(Constants.UTF_8); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ByteBufMessage.java similarity index 70% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/message/ByteBufMessage.java index ec99c413..aaee09ac 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseBufferBodyMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ByteBufMessage.java @@ -9,9 +9,9 @@ /** * Created by ohun on 2015/12/28. */ -public abstract class BaseBufferBodyMessage extends BaseMessage { +public abstract class ByteBufMessage extends BaseMessage { - public BaseBufferBodyMessage(Packet message, Connection connection) { + public ByteBufMessage(Packet message, Connection connection) { super(message, connection); } @@ -39,6 +39,18 @@ public void encodeString(ByteBuf body, String field) { } } + public void encodeByte(ByteBuf body, byte field) { + body.writeByte(field); + } + + public void encodeInt(ByteBuf body, int field) { + body.writeInt(field); + } + + public void encodeLong(ByteBuf body, long field) { + body.writeLong(field); + } + public void encodeBytes(ByteBuf body, byte[] field) { if (field == null || field.length == 0) { body.writeShort(0); @@ -60,4 +72,16 @@ public byte[] decodeBytes(ByteBuf body) { body.readBytes(bytes); return bytes; } + + public byte decodeByte(ByteBuf body) { + return body.readByte(); + } + + public int decodeInt(ByteBuf body) { + return body.readInt(); + } + + public long decodeLong(ByteBuf body) { + return body.readLong(); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java new file mode 100644 index 00000000..9230a985 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java @@ -0,0 +1,58 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +/** + * Created by ohun on 2015/12/28. + */ +public final class ErrorMessage extends ByteBufMessage { + public String reason; + public byte errorCode; + + public ErrorMessage(String reason, Connection connection) { + super(new Packet(Command.ERROR.cmd), connection); + this.reason = reason; + } + + public ErrorMessage(int sessionId, Connection connection) { + super(new Packet(Command.ERROR.cmd, sessionId), connection); + } + + public ErrorMessage(Packet message, Connection connection) { + super(message, connection); + } + + public static ErrorMessage from(BaseMessage src) { + return new ErrorMessage(src.createResponse(), src.connection); + } + + public ErrorMessage setReason(String reason) { + this.reason = reason; + return this; + } + + public ErrorMessage setErrorCode(byte errorCode) { + this.errorCode = errorCode; + return this; + } + + @Override + public void decode(ByteBuf body) { + reason = decodeString(body); + errorCode = decodeByte(body); + } + + @Override + public void encode(ByteBuf body) { + encodeString(body, reason); + encodeByte(body, errorCode); + } + + @Override + public void send() { + super.sendRaw(); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java index 76f192a8..1dd919d6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java @@ -7,8 +7,8 @@ /** * Created by ohun on 2015/12/25. */ -public class FastConnectMessage extends BaseBufferBodyMessage { - public String tokenId; +public final class FastConnectMessage extends ByteBufMessage { + public String sessionId; public String deviceId; public FastConnectMessage(Packet message, Connection connection) { @@ -17,11 +17,13 @@ public FastConnectMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { - + sessionId = decodeString(body); + deviceId = decodeString(body); } @Override public void encode(ByteBuf body) { - + encodeString(body, sessionId); + encodeString(body, deviceId); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java new file mode 100644 index 00000000..f39259b7 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java @@ -0,0 +1,52 @@ +package com.shinemo.mpush.core.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +/** + * Created by ohun on 2015/12/28. + */ +public final class FastConnectSuccessMessage extends ByteBufMessage { + public String serverHost; + public long serverTime; + public int heartbeat; + + public FastConnectSuccessMessage(Packet message, Connection connection) { + super(message, connection); + } + + public static FastConnectSuccessMessage from(BaseMessage src) { + return new FastConnectSuccessMessage(src.createResponse(), src.connection); + } + + @Override + public void decode(ByteBuf body) { + serverHost = decodeString(body); + serverTime = decodeLong(body); + heartbeat = decodeInt(body); + } + + @Override + public void encode(ByteBuf body) { + encodeString(body, serverHost); + encodeLong(body, serverTime); + encodeInt(body, heartbeat); + } + + + public FastConnectSuccessMessage setServerHost(String serverHost) { + this.serverHost = serverHost; + return this; + } + + public FastConnectSuccessMessage setServerTime(long serverTime) { + this.serverTime = serverTime; + return this; + } + + public FastConnectSuccessMessage setHeartbeat(int heartbeat) { + this.heartbeat = heartbeat; + return this; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java index ff98633e..ba7461ab 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2015/12/24. */ -public class HandShakeMessage extends BaseBufferBodyMessage { +public final class HandShakeMessage extends ByteBufMessage { public String deviceId; public String osName; public String osVersion; @@ -18,9 +18,7 @@ public class HandShakeMessage extends BaseBufferBodyMessage { public long timestamp; public HandShakeMessage(int sessionId, Connection connection) { - super(new Packet(), connection); - super.message.cmd = Command.Handshake.cmd; - super.message.sessionId = sessionId; + super(new Packet(Command.HANDSHAKE.cmd, sessionId), connection); } public HandShakeMessage(Packet message, Connection connection) { @@ -35,7 +33,7 @@ public void decode(ByteBuf body) { clientVersion = decodeString(body); iv = decodeBytes(body); clientKey = decodeBytes(body); - timestamp = body.readLong(); + timestamp = decodeLong(body); } public void encode(ByteBuf body) { @@ -45,10 +43,6 @@ public void encode(ByteBuf body) { encodeString(body, clientVersion); encodeBytes(body, iv); encodeBytes(body, clientKey); - body.writeLong(timestamp); - } - - public HandshakeSuccessMessage createSuccessMessage() { - return new HandshakeSuccessMessage(createResponse(), getConnection()); + encodeLong(body, timestamp); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java index 689a5363..85dcb00e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java @@ -7,7 +7,7 @@ /** * Created by ohun on 2015/12/27. */ -public class HandshakeSuccessMessage extends BaseBufferBodyMessage { +public final class HandshakeSuccessMessage extends ByteBufMessage { public byte[] serverKey; public String serverHost; public long serverTime; @@ -23,19 +23,53 @@ public HandshakeSuccessMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { serverKey = decodeBytes(body); serverHost = decodeString(body); - serverTime = body.readLong(); - heartbeat = body.readInt(); + serverTime = decodeLong(body); + heartbeat = decodeInt(body); sessionId = decodeString(body); - expireTime = body.readLong(); + expireTime = decodeLong(body); } @Override public void encode(ByteBuf body) { encodeBytes(body, serverKey); encodeString(body, serverHost); - body.writeLong(serverTime); - body.writeInt(heartbeat); + encodeLong(body, serverTime); + encodeInt(body, heartbeat); encodeString(body, sessionId); - body.writeLong(expireTime); + encodeLong(body, expireTime); + } + + public static HandshakeSuccessMessage from(BaseMessage src) { + return new HandshakeSuccessMessage(src.createResponse(), src.connection); + } + + public HandshakeSuccessMessage setServerKey(byte[] serverKey) { + this.serverKey = serverKey; + return this; + } + + public HandshakeSuccessMessage setServerHost(String serverHost) { + this.serverHost = serverHost; + return this; + } + + public HandshakeSuccessMessage setServerTime(long serverTime) { + this.serverTime = serverTime; + return this; + } + + public HandshakeSuccessMessage setHeartbeat(int heartbeat) { + this.heartbeat = heartbeat; + return this; + } + + public HandshakeSuccessMessage setSessionId(String sessionId) { + this.sessionId = sessionId; + return this; + } + + public HandshakeSuccessMessage setExpireTime(long expireTime) { + this.expireTime = expireTime; + return this; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java index bf6f1a43..29b900ad 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java @@ -7,7 +7,7 @@ /** * Created by ohun on 2015/12/28. */ -public class HeartbeatMessage implements Message { +public final class HeartbeatMessage implements Message { private final Connection connection; public HeartbeatMessage(Connection connection) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java index 9af67f72..b0c972c1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java @@ -31,8 +31,8 @@ public byte[] getBody() { byte[] tmp = message.body; if (message.hasFlag(Constants.CRYPTO_FLAG)) { SessionContext info = connection.getSessionContext(); - if (info != null && info.sessionKey != null) { - tmp = AESUtils.decrypt(tmp, info.sessionKey, info.iv); + if (info.cipher != null) { + tmp = info.cipher.decrypt(tmp); } } @@ -53,9 +53,7 @@ public Connection getConnection() { } public Response getResponse() { - Packet packet = new Packet(); - packet.cmd = this.message.cmd; - packet.sessionId = this.message.sessionId; + Packet packet = new Packet(this.message.cmd, this.message.sessionId); return new NettyResponse(packet, connection); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java index 89b0d046..c1f03af1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java @@ -31,9 +31,9 @@ public void send(byte[] body) { } } //2.加密 - SessionContext info = connection.getSessionContext(); - if (info != null && info.sessionKey != null) { - tmp = AESUtils.encrypt(tmp, info.sessionKey, info.iv); + SessionContext context = connection.getSessionContext(); + if (context.cipher != null) { + tmp = context.cipher.encrypt(tmp); packet.setFlag(Constants.CRYPTO_FLAG); } packet.body = tmp; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java new file mode 100644 index 00000000..a854e424 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.core.message; + +import com.google.common.base.Strings; +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/28. + */ +public final class SuccessMessage extends BaseMessage { + public String data; + + public SuccessMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(byte[] body) { + if (body != null && body.length > 0) { + data = new String(body, Constants.UTF_8); + } + } + + @Override + public byte[] encode() { + return Strings.isNullOrEmpty(data) + ? null : + data.getBytes(Constants.UTF_8); + } + + public static SuccessMessage from(BaseMessage message) { + return new SuccessMessage(message.createResponse(), message.connection); + } + + public SuccessMessage setData(String data) { + this.data = data; + return this; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java deleted file mode 100644 index 8709669d..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/request/HandshakeRequest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.shinemo.mpush.core.request; - -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.message.NettyRequest; - -/** - * Created by ohun on 2015/12/27. - */ -public class HandshakeRequest extends NettyRequest { - public String deviceId; - public String osName; - public String osVersion; - public String clientVersion; - public byte[] clientKey; - public long timestamp; - - public HandshakeRequest(Packet message, Connection connection) { - super(message, connection); - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java index e28d6c84..08ffa700 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2015/12/28. */ -public class AesCipher implements Cipher { +public final class AesCipher implements Cipher { private final byte[] key; private final byte[] iv; @@ -43,7 +43,7 @@ public static AesCipher decodeCipher(String keys) { try { byte[] key = Base64Utils.decode(array[0]); byte[] iv = Base64Utils.decode(array[1]); - if (key.length == AESUtils.AES_KEY_LENGTH && iv.length == AESUtils.AES_KEY_LENGTH) { + if (key.length == CipherBox.AES_KEY_LENGTH && iv.length == CipherBox.AES_KEY_LENGTH) { return new AesCipher(key, iv); } } catch (Exception e) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java index 308bc24b..a487d32c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java @@ -14,10 +14,10 @@ /** * Created by ohun on 2015/12/24. */ -public class CipherBox { +public final class CipherBox { + public static final int AES_KEY_LENGTH = 16; public static final CipherBox INSTANCE = new CipherBox(); private SecureRandom random = new SecureRandom(); - private RSAPrivateKey privateKey; private RSAPublicKey publicKey; @@ -91,20 +91,20 @@ public RSAPublicKey getPublicKey() { } public byte[] randomAESKey() { - byte[] bytes = new byte[AESUtils.AES_KEY_LENGTH]; + byte[] bytes = new byte[AES_KEY_LENGTH]; random.nextBytes(bytes); return bytes; } public byte[] randomAESIV() { - byte[] bytes = new byte[AESUtils.AES_KEY_LENGTH]; + byte[] bytes = new byte[AES_KEY_LENGTH]; random.nextBytes(bytes); return bytes; } public byte[] mixKey(byte[] clientKey, byte[] serverKey) { - byte[] sessionKey = new byte[AESUtils.AES_KEY_LENGTH]; - for (int i = 0; i < AESUtils.AES_KEY_LENGTH; i++) { + byte[] sessionKey = new byte[AES_KEY_LENGTH]; + for (int i = 0; i < AES_KEY_LENGTH; i++) { byte a = clientKey[i]; byte b = serverKey[i]; int sum = Math.abs(a + b); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java index c6e3d85b..c24fffa5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java @@ -5,8 +5,8 @@ /** * Created by ohun on 2015/12/25. */ -public class ReusableSession { - public transient String sessionId; +public final class ReusableSession { + public String sessionId; public long expireTime; public SessionContext sessionContext; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java index 38c9a960..f8659276 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java @@ -9,7 +9,7 @@ /** * Created by ohun on 2015/12/25. */ -public class ReusableSessionManager { +public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; private final Map tokenCache = new ConcurrentHashMap(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java index 43aa28f4..9ecf9038 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java @@ -9,7 +9,7 @@ /** * Created by ohun on 2015/12/28. */ -public class RsaCipher implements Cipher { +public final class RsaCipher implements Cipher { private final RSAPrivateKey privateKey; private final RSAPublicKey publicKey; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java index 7262f32c..85a1e819 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java @@ -55,14 +55,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception if (msg instanceof Packet) { Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); - if (command == Command.Handshake) { + if (command == Command.HANDSHAKE) { connection.getSessionContext().changeCipher(new AesCipher(clientKey, iv)); HandshakeSuccessMessage resp = new HandshakeSuccessMessage(packet, connection); byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, resp.serverKey); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, resp.serverKey); saveToken(resp.sessionId); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); - } else if (command == Command.FastConnect) { + } else if (command == Command.FAST_CONNECT) { LOGGER.info("fast connect success, message=" + packet.getStringBody()); } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java index 9ad17c0a..c399512d 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java @@ -10,19 +10,22 @@ * Created by ohun on 2015/12/23. */ public class LocalRouterManager implements RouterManager { - private final Map routerMap = new ConcurrentHashMap(); + private final Map routerMap = new ConcurrentHashMap<>(); - public boolean publish(long userId, Router route) { + @Override + public boolean publish(String userId, Router route) { routerMap.put(userId, route); return true; } - public boolean unPublish(long userId) { + @Override + public boolean unPublish(String userId) { routerMap.remove(userId); return true; } - public Router getRouter(long userId) { + @Override + public Router getRouter(String userId) { return routerMap.get(userId); } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java index da5ef1ca..eb7b1430 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java @@ -8,15 +8,18 @@ */ public class RemoteRouterManager implements RouterManager { - public boolean publish(long userId, Router route) { + @Override + public boolean publish(String userId, Router route) { return true; } - public boolean unPublish(long userId) { + @Override + public boolean unPublish(String userId) { return true; } - public Router getRouter(long userId) { + @Override + public Router getRouter(String userId) { return null; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java index 0112014a..bf8640eb 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java @@ -13,19 +13,19 @@ public class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); - public boolean publish(long userId, Connection connection) { + public boolean publish(String userId, Connection connection) { localRouterManager.publish(userId, new LocalRouter(connection)); remoteRouterManager.publish(userId, new RemoteRouter(new RouterInfo("127.0.0.1"))); return true; } - public boolean unPublish(long userId) { + public boolean unPublish(String userId) { localRouterManager.unPublish(userId); remoteRouterManager.unPublish(userId); return true; } - public Router lookup(long userId) { + public Router lookup(String userId) { Router local = localRouterManager.getRouter(userId); if (local != null) return local; Router remote = remoteRouterManager.getRouter(userId); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index e08e7c06..81acd4bf 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -20,96 +20,94 @@ public class NettyClient implements Client { - private static final Logger log = LoggerFactory.getLogger(NettyClient.class); - - private final String remoteHost; - private final int remotePort; - private final Channel channel; - private int hbTimes = 0; - - public NettyClient(final String remoteHost, final int remotePort, Channel channel) { - this.remoteHost = remoteHost; - this.remotePort = remotePort; - this.channel = channel; - } - - @Override - public void close(String cause) { - if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { - log.error("close channel:"+cause); - } - this.channel.close(); - } - - @Override - public boolean isEnabled() { - return channel.isWritable(); - } - - @Override - public boolean isConnected() { - return channel.isActive(); - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } - - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void startHeartBeat() throws Exception { - NettySharedHolder.timer.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - final Packet packet = buildHeartBeat(); - ChannelFuture channelFuture = channel.writeAndFlush(packet); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send hb msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); - } - log.warn("client send msg hb false:" + channel.remoteAddress().toString() + "," + packet); - } else { - log.warn("client send msg hb success:" + channel.remoteAddress().toString() + "," + packet); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - } - } - }, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - - private static Packet buildHeartBeat() { - Packet packet = new Packet(); - packet.cmd = Command.Heartbeat.cmd; - return packet; - } - - @Override - public String getUrl() { - return String.format("%s:%s", remoteHost, remotePort); - } - - @Override - public String getRemoteHost() { - return remoteHost; - } - - @Override - public int getRemotePort() { - return remotePort; - } + private static final Logger log = LoggerFactory.getLogger(NettyClient.class); + + private final String remoteHost; + private final int remotePort; + private final Channel channel; + private int hbTimes = 0; + + public NettyClient(final String remoteHost, final int remotePort, Channel channel) { + this.remoteHost = remoteHost; + this.remotePort = remotePort; + this.channel = channel; + } + + @Override + public void close(String cause) { + if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { + log.error("close channel:" + cause); + } + this.channel.close(); + } + + @Override + public boolean isEnabled() { + return channel.isWritable(); + } + + @Override + public boolean isConnected() { + return channel.isActive(); + } + + @Override + public void resetHbTimes() { + hbTimes = 0; + } + + @Override + public int inceaseAndGetHbTimes() { + return ++hbTimes; + } + + @Override + public void startHeartBeat() throws Exception { + NettySharedHolder.timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + try { + final Packet packet = buildHeartBeat(); + ChannelFuture channelFuture = channel.writeAndFlush(packet); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + log.warn("client send hb msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); + } + log.warn("client send msg hb false:" + channel.remoteAddress().toString() + "," + packet); + } else { + log.warn("client send msg hb success:" + channel.remoteAddress().toString() + "," + packet); + } + } + }); + } finally { + if (channel.isActive()) { + NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + } + } + }, Constants.TIME_DELAY, TimeUnit.SECONDS); + } + + private static Packet buildHeartBeat() { + return new Packet(Command.HEARTBEAT.cmd); + } + + @Override + public String getUrl() { + return String.format("%s:%s", remoteHost, remotePort); + } + + @Override + public String getRemoteHost() { + return remoteHost; + } + + @Override + public int getRemotePort() { + return remotePort; + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index ace1d155..e9f59705 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -25,9 +25,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { if (in.readByte() == Constants.HB) { - Packet packet = new Packet(); - packet.cmd = Command.Heartbeat.cmd; - out.add(packet); + out.add(new Packet(Command.HEARTBEAT.cmd)); } else { in.readerIndex(in.readerIndex() - 1); break; @@ -71,8 +69,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { body = new byte[bodyLength]; in.readBytes(body); } - Packet packet = new Packet(); - packet.cmd = command; + Packet packet = new Packet(command); packet.cc = cc; packet.flags = flags; packet.sessionId = sessionId; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java index 25ed2892..1829a1a9 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java @@ -18,7 +18,7 @@ public class PacketEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { - if (packet.cmd == Command.Heartbeat.cmd) { + if (packet.cmd == Command.HEARTBEAT.cmd) { out.writeByte(Constants.HB); } else { out.writeInt(packet.getBodyLength()); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java index 1e4dd370..fb53d2b0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java @@ -13,7 +13,6 @@ */ public final class AESUtils { private static final Logger LOGGER = LoggerFactory.getLogger(AESUtils.class); - public static final int AES_KEY_LENGTH = 16; public static final String KEY_ALGORITHM = "AES"; public static final String KEY_ALGORITHM_PADDING = "AES/CBC/PKCS5Padding"; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java index 0f482419..48bc40e7 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java @@ -15,7 +15,7 @@ public class AESUtilsTest { public void testEncryptDES() throws Exception { String data = "似的士大夫士大夫士大夫首发式发生士大夫"; System.out.println("原文:\n" + data); - byte[] key = new byte[AESUtils.AES_KEY_LENGTH]; + byte[] key = new byte[16]; new Random().nextBytes(key); byte[] d1 = AESUtils.encrypt(data.getBytes(Constants.UTF_8), key,key); System.out.println("加密后:\n" + new String(d1)); From a7327e009492458cc0b62483925b79dace20f46a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 15:44:34 +0000 Subject: [PATCH 039/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/message/ErrorMessage.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java index 9230a985..a2f11705 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java @@ -12,21 +12,12 @@ public final class ErrorMessage extends ByteBufMessage { public String reason; public byte errorCode; - public ErrorMessage(String reason, Connection connection) { - super(new Packet(Command.ERROR.cmd), connection); - this.reason = reason; - } - - public ErrorMessage(int sessionId, Connection connection) { - super(new Packet(Command.ERROR.cmd, sessionId), connection); - } - public ErrorMessage(Packet message, Connection connection) { super(message, connection); } public static ErrorMessage from(BaseMessage src) { - return new ErrorMessage(src.createResponse(), src.connection); + return new ErrorMessage(new Packet(Command.ERROR.cmd, src.message.sessionId), src.connection); } public ErrorMessage setReason(String reason) { From 88b7de9d4aea19f1e3280b7abd3b38e1d369baa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 15:46:20 +0000 Subject: [PATCH 040/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/api/RouterInfo.java | 2 +- .../java/com/shinemo/mpush/api/SessionContext.java | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java index 49040fd8..55cafc14 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java @@ -3,7 +3,7 @@ /** * Created by ohun on 2015/12/23. */ -public class RouterInfo { +public final class RouterInfo { private String serverIp; private String osName; private String clientVer; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java index 1d84f280..d88d0796 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java @@ -5,24 +5,13 @@ /** * Created by ohun on 2015/12/22. */ -public class SessionContext { +public final class SessionContext { public String osName; public String osVersion; public String clientVersion; public String deviceId; public Cipher cipher; - public SessionContext() { - - } - - public SessionContext(String osName, String osVersion, String clientVersion, String deviceId) { - this.osName = osName; - this.osVersion = osVersion; - this.clientVersion = clientVersion; - this.deviceId = deviceId; - } - public void changeCipher(Cipher cipher) { this.cipher = cipher; } From 26cb37ae84fa15556fb220717bffa57877c10558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 15:47:02 +0000 Subject: [PATCH 041/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/api/protocol/Packet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index e706e8d2..75dba802 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -10,7 +10,7 @@ * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ -public class Packet implements Serializable { +public final class Packet implements Serializable { private static final long serialVersionUID = -2725825199998223372L; public byte cmd; //命令 public short cc; //校验码 暂时没有用到 From df6e19012d2f59afaa0551be8de746881a8fcc2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 15:48:33 +0000 Subject: [PATCH 042/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/SessionContext.java | 11 +++++++++++ .../java/com/shinemo/mpush/core/NettyConnection.java | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java index d88d0796..64619054 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java @@ -39,4 +39,15 @@ public SessionContext setDeviceId(String deviceId) { public boolean handshakeOk() { return !Strings.isNullOrEmpty(deviceId); } + + + @Override + public String toString() { + return "SessionContext{" + + "osName='" + osName + '\'' + + ", osVersion='" + osVersion + '\'' + + ", clientVersion='" + clientVersion + '\'' + + ", deviceId='" + deviceId + '\'' + + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index 4092f961..e5a8181b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -131,4 +131,14 @@ public boolean isEnable() { public String remoteIp() { return channel.remoteAddress().toString(); } + + + @Override + public String toString() { + return "NettyConnection{" + + "context=" + context + + ", channel=" + channel + + ", status=" + status + + '}'; + } } From c589edbc914219e9ff053702bdb511cd8a476479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Dec 2015 15:50:40 +0000 Subject: [PATCH 043/890] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/core/handler/FastConnectHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 194132e5..efc43230 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -23,6 +23,7 @@ public void handle(FastConnectMessage message) { ErrorMessage.from(message).setReason("error device").send(); } else { message.getConnection().setSessionContext(session.sessionContext); + FastConnectSuccessMessage .from(message) .setServerHost(MPushUtil.getLocalIp()) From 0435b6049c4839b3b9932126a6838a04535a84bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Dec 2015 01:49:20 +0000 Subject: [PATCH 044/890] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/api/AbstractHandler.java | 45 --------- .../shinemo/mpush/api/protocol/Handler.java | 30 ------ .../mpush/core/handler/ServerHandler.java | 5 +- .../mpush/core/netty/ClientHandler.java | 34 +------ .../mpush/core/netty/NettyClientTest.java | 4 +- .../mpush/core/netty/NettyServerTest.java | 4 +- .../client/AbstractNettyClientFactory.java | 93 ++++++++++--------- .../netty/client/NettyClientFactory.java | 14 +-- .../mpush/netty/server/NettyServer.java | 21 ++--- .../mpush/netty/util/NettySharedHandler.java | 87 ----------------- 10 files changed, 65 insertions(+), 272 deletions(-) delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java delete mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java deleted file mode 100644 index 7cc53082..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.shinemo.mpush.api; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.SocketAddress; - -public abstract class AbstractHandler { - - public void channelActive(ChannelHandlerContext ctx) throws Exception{ - - } - - public void channelInactive(ChannelHandlerContext ctx) throws Exception{ - - } - - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{ - - } - - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{ - - } - - public void channelRegistered(ChannelHandlerContext ctx) throws Exception{ - - } - - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception{ - - } - - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{ - - } - - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception{ - - } - - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception{ - - } - -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java deleted file mode 100644 index 38d6926e..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Handler.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.shinemo.mpush.api.protocol; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.SocketAddress; -import java.util.List; - -import com.shinemo.mpush.api.Connection; - -public interface Handler { - - public void channelActive(ChannelHandlerContext ctx) throws Exception; - - public void channelInactive(ChannelHandlerContext ctx) throws Exception; - - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception; - - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception; - - public void channelRegistered(ChannelHandlerContext ctx) throws Exception; - - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception; - - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception; - - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception; - - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception; - -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java index b4bf29a6..1f6dcda3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java @@ -1,14 +1,13 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.AbstractHandler; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.NettyConnection; +import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import org.slf4j.Logger; @@ -17,7 +16,7 @@ /** * Created by ohun on 2015/12/19. */ -public class ServerHandler extends AbstractHandler implements Handler { +public class ServerHandler extends ChannelHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(ServerHandler.class); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java index 85a1e819..1aa21308 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java @@ -2,7 +2,6 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.NettyConnection; import com.shinemo.mpush.core.message.HandShakeMessage; @@ -11,6 +10,7 @@ import com.shinemo.mpush.core.security.CipherBox; import com.shinemo.mpush.tools.Strings; +import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; @@ -23,7 +23,7 @@ /** * Created by ohun on 2015/12/24. */ -public class ClientHandler implements Handler { +public class ClientHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class); private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); private byte[] iv = CipherBox.INSTANCE.randomAESIV(); @@ -89,34 +89,4 @@ private String getToken() { } return Strings.EMPTY; } - - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void channelRegistered(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { - - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - - } - - @Override - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { - - } - - @Override - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - - } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 5e1bfc6a..268c0f16 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -3,6 +3,7 @@ import java.util.concurrent.locks.LockSupport; +import io.netty.channel.ChannelHandler; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Test; @@ -10,7 +11,6 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.netty.client.NettyClientFactory; /** @@ -22,7 +22,7 @@ public class NettyClientTest { private String host = "127.0.0.1"; private int port = 3000; - private Handler handler = new ClientHandler(); + private ChannelHandler handler = new ClientHandler(); @Test public void testClient() throws Exception { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index e75b4278..40c6d5cb 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,10 +1,10 @@ package com.shinemo.mpush.core.netty; import com.shinemo.mpush.api.Receiver; -import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.core.MessageDispatcher; import com.shinemo.mpush.core.handler.ServerHandler; import com.shinemo.mpush.netty.server.NettyServer; +import io.netty.channel.ChannelHandler; import org.junit.Test; /** @@ -21,7 +21,7 @@ public void testStop() throws Exception { public void testStart() throws Exception { Receiver receiver = new MessageDispatcher(); - Handler handler = new ServerHandler(receiver); + ChannelHandler handler = new ServerHandler(receiver); final NettyServer server = new NettyServer(3000, handler); server.start(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index 158dbf51..65cf7b48 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -4,6 +4,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; +import io.netty.channel.ChannelHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,44 +13,44 @@ import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalNotification; import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.protocol.Handler; public abstract class AbstractNettyClientFactory { - - private static final String format = "%s:%s"; - - private static final Logger log = LoggerFactory.getLogger(AbstractNettyClientFactory.class); - /** - * host:port - */ - private final Cache cachedClients = CacheBuilder.newBuilder()// - .maximumSize(2 << 17)// 最大是65535*2 - .expireAfterAccess(2*60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力 - .removalListener(new RemovalListener() { - @Override - public void onRemoval(RemovalNotification notification) { - if (notification.getValue().isConnected()) { - notification.getValue().close("[Remoting] removed from cache"); - } - } - })// - .build(); - - /** - * 不存在,则创建 - * @param remoteHost - * @param port - * @param handler - * @return - * @throws Exception - */ - public Client get(final String remoteHost,final int port,final Handler handler) throws Exception { - final String key = String.format(format, remoteHost,port); + private static final String format = "%s:%s"; + + private static final Logger log = LoggerFactory.getLogger(AbstractNettyClientFactory.class); + + /** + * host:port + */ + private final Cache cachedClients = CacheBuilder.newBuilder()// + .maximumSize(2 << 17)// 最大是65535*2 + .expireAfterAccess(2 * 60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力 + .removalListener(new RemovalListener() { + @Override + public void onRemoval(RemovalNotification notification) { + if (notification.getValue().isConnected()) { + notification.getValue().close("[Remoting] removed from cache"); + } + } + })// + .build(); + + /** + * 不存在,则创建 + * + * @param remoteHost + * @param port + * @param handler + * @return + * @throws Exception + */ + public Client get(final String remoteHost, final int port, final ChannelHandler handler) throws Exception { + final String key = String.format(format, remoteHost, port); Client client = cachedClients.get(key, new Callable() { @Override public Client call() throws Exception { - Client client = createClient(remoteHost,port,handler); + Client client = createClient(remoteHost, port, handler); if (client != null) { client.startHeartBeat(); } @@ -57,27 +58,27 @@ public Client call() throws Exception { } }); if (client == null || !client.isConnected()) { - cachedClients.invalidate(key); + cachedClients.invalidate(key); return null; } return client; } - - public Client get(final String remoteHost,final int port) throws Exception { - return get(remoteHost, port, null); + + public Client get(final String remoteHost, final int port) throws Exception { + return get(remoteHost, port, null); } - - - protected Client createClient(final String remoteHost,final int port) throws Exception{ - return createClient(remoteHost, port, null); - } - - protected abstract Client createClient(final String remoteHost,final int port,Handler handler) throws Exception; - - + + + protected Client createClient(final String remoteHost, final int port) throws Exception { + return createClient(remoteHost, port, null); + } + + protected abstract Client createClient(final String remoteHost, final int port, ChannelHandler handler) throws Exception; + + public void remove(Client client) { if (client != null) { - cachedClients.invalidate(client.getUrl()); + cachedClients.invalidate(client.getUrl()); log.warn(MessageFormat.format("[Remoting] {0} is removed", client)); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 53db14f3..70ef2f89 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -3,22 +3,15 @@ import java.net.InetSocketAddress; import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; -import com.shinemo.mpush.netty.util.NettySharedHandler; -import com.shinemo.mpush.netty.util.NettySharedHolder; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; @@ -29,10 +22,9 @@ public class NettyClientFactory extends AbstractNettyClientFactory { public static NettyClientFactory instance = new NettyClientFactory(); - protected Client createClient(final String host, final int port, final Handler handler) throws Exception { + protected Client createClient(final String host, final int port, final ChannelHandler handler) throws Exception { EventLoopGroup workerGroup = new NioEventLoopGroup(); final Bootstrap bootstrap = new Bootstrap(); - final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler); bootstrap.group(workerGroup)// .option(ChannelOption.TCP_NODELAY, true)// .option(ChannelOption.SO_REUSEADDR, true)// @@ -46,7 +38,7 @@ protected Client createClient(final String host, final int port, final Handler h public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(nettySharedHandler); + ch.pipeline().addLast(handler); } }); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 854ea2ab..a9a992b7 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -3,22 +3,16 @@ import java.util.concurrent.atomic.AtomicBoolean; import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.api.protocol.Handler; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; -import com.shinemo.mpush.netty.util.NettySharedHandler; -import com.shinemo.mpush.netty.util.NettySharedHolder; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; @@ -32,17 +26,17 @@ public class NettyServer implements Server { private final AtomicBoolean startFlag = new AtomicBoolean(false); private final int port; - private final Handler channelHandler; + private final ChannelHandler channelHandler; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; - public NettyServer(int port,Handler channelHandler) { + public NettyServer(int port, ChannelHandler channelHandler) { this.port = port; this.channelHandler = channelHandler; } - + public NettyServer(int port) { - this(port,null); + this(port, null); } @Override @@ -102,8 +96,7 @@ public void start() { * 这里告诉Channel如何获取新的连接. */ b.channel(NioServerSocketChannel.class); - - final NettySharedHandler nettySharedHandler = new NettySharedHandler(channelHandler); + /*** * 这里的事件处理类经常会被用来处理一个最近的已经接收的Channel。 @@ -119,7 +112,7 @@ public void start() { public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(nettySharedHandler); + ch.pipeline().addLast(channelHandler); } }); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java deleted file mode 100644 index 20627b51..00000000 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHandler.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.shinemo.mpush.netty.util; - -import com.shinemo.mpush.api.protocol.Handler; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@ChannelHandler.Sharable -public class NettySharedHandler extends ChannelHandlerAdapter{ - - private static final Logger log = LoggerFactory.getLogger(NettySharedHandler.class); - - private Handler channelHandler; - - public NettySharedHandler(Handler channelHandler){ - this.channelHandler = channelHandler; - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelActive"); - if(channelHandler!=null){ - channelHandler.channelActive(ctx); - } - super.channelActive(ctx); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelInactive"); - if(channelHandler!=null){ - channelHandler.channelInactive(ctx); - } - super.channelInactive(ctx); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelRead:"+ToStringBuilder.reflectionToString(msg, ToStringStyle.DEFAULT_STYLE)); - if(channelHandler!=null){ - channelHandler.channelRead(ctx, msg); - } - super.channelRead(ctx, msg); - } - - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelReadComplete"); - if(channelHandler!=null){ - channelHandler.channelReadComplete(ctx); - } - super.channelReadComplete(ctx); - } - - @Override - public void channelRegistered(ChannelHandlerContext ctx) throws Exception { -// log.warn(ctx.channel().remoteAddress() + ", channelRegistered"); - if(channelHandler!=null){ - channelHandler.channelRegistered(ctx); - } - super.channelRegistered(ctx); - } - - @Override - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { -// log.warn(ctx.channel().remoteAddress() + ", channelUnregistered"); - if(channelHandler!=null){ - channelHandler.channelUnregistered(ctx); - } - super.channelUnregistered(ctx); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", exceptionCaught",cause); - if(channelHandler!=null){ - channelHandler.exceptionCaught(ctx, cause); - } - super.exceptionCaught(ctx, cause); - } - - -} From 06760dcde0b5275b846bb42d8f08e1a2901de911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Dec 2015 03:29:44 +0000 Subject: [PATCH 045/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Client.java | 43 +++--- .../com/shinemo/mpush/api/Connection.java | 2 + .../java/com/shinemo/mpush/api/Constants.java | 7 +- .../com/shinemo/mpush/api/event/Event.java | 7 + .../mpush/api/event/HandshakeEvent.java | 16 +++ .../shinemo/mpush/api/protocol/Packet.java | 7 + .../shinemo/mpush/core/ConnectionManager.java | 21 +++ .../java/com/shinemo/mpush/core/EventBus.java | 45 +++++++ .../shinemo/mpush/core/NettyConnection.java | 5 + .../mpush/core/handler/HandShakeHandler.java | 4 + .../core/handler/ServerChannelHandler.java | 81 +++++++++++ .../mpush/core/handler/ServerHandler.java | 55 -------- .../mpush/core/message/BaseMessage.java | 9 ++ .../mpush/core/message/ErrorMessage.java | 9 ++ .../core/task/ScanAllClientConnection.java | 2 +- ...Handler.java => ClientChannelHandler.java} | 60 +++++++-- .../mpush/core/netty/NettyClientTest.java | 32 ++--- .../mpush/core/netty/NettyServerTest.java | 4 +- .../client/AbstractNettyClientFactory.java | 7 +- .../mpush/netty/client/NettyClient.java | 127 ++++++++++-------- .../netty/client/NettyClientFactory.java | 51 +------ .../mpush/netty/codec/PacketDecoder.java | 2 +- .../mpush/netty/codec/PacketEncoder.java | 2 +- .../mpush/netty/server/NettyServer.java | 10 +- .../mpush/netty/util/NettySharedHolder.java | 2 +- 25 files changed, 375 insertions(+), 235 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java rename mpush-core/src/test/java/com/shinemo/mpush/core/netty/{ClientHandler.java => ClientChannelHandler.java} (54%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index 89a9d33f..c66ec484 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -2,27 +2,24 @@ public interface Client { - public void close(final String cause); - - public String toString(); - - public boolean isEnabled(); - - public boolean isConnected(); - - public void resetHbTimes(); - - public int inceaseAndGetHbTimes(); - - public void startHeartBeat() throws Exception; - - /** - * host:port - */ - public String getUrl(); - - public String getRemoteHost(); - - public int getRemotePort(); - + void init(); + + void start(); + + void close(final String cause); + + boolean isEnabled(); + + boolean isConnected(); + + void resetHbTimes(); + + int inceaseAndGetHbTimes(); + + String getHost(); + + int getPort(); + + String getUri(); + } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java index 7430ef0f..b51efdd8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java @@ -17,6 +17,8 @@ public interface Connection { void send(Packet packet); + Channel channel(); + String getId(); boolean isClosed(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 06dbef71..9b38ba1f 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -10,12 +10,11 @@ public interface Constants { byte[] EMPTY_BYTES = new byte[0]; int MAX_PACKET_SIZE = 1024; int HEADER_LEN = 13; - short MAGIC_NUM = 1122; - byte HB = '\n'; + int COMPRESS_LIMIT = 1024 * 10; byte CRYPTO_FLAG = 0x01; byte COMPRESS_FLAG = 0x02; - long TIME_DELAY = 120L; + long TIME_DELAY = 1L; String JVM_LOG_PATH = "/opt/"; @@ -28,5 +27,5 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; - int HEARTBEAT_TIME = 60 * 2 * 1000;//2min + int HEARTBEAT_TIME = 1000;//2min } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java new file mode 100644 index 00000000..b9ac7669 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.api.event; + +/** + * Created by ohun on 2015/12/29. + */ +public interface Event { +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java new file mode 100644 index 00000000..0d302b78 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.api.event; + +import com.shinemo.mpush.api.Connection; + +/** + * Created by ohun on 2015/12/29. + */ +public class HandshakeEvent implements Event { + public final Connection connection; + public final int heartbeat; + + public HandshakeEvent(Connection connection, int heartbeat) { + this.connection = connection; + this.heartbeat = heartbeat; + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 75dba802..63b3e8fe 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.api.protocol; import com.shinemo.mpush.api.Constants; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import java.io.Serializable; @@ -11,6 +13,7 @@ * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ public final class Packet implements Serializable { + public static final byte HB_PACKET = '\n'; private static final long serialVersionUID = -2725825199998223372L; public byte cmd; //命令 public short cc; //校验码 暂时没有用到 @@ -55,4 +58,8 @@ public String toString() { ", body=" + Arrays.toString(body) + '}'; } + + public static ByteBuf getHBPacket() { + return Unpooled.buffer(1).writeByte(HB_PACKET); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java index b08f2df9..3eceefb3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java @@ -1,22 +1,34 @@ package com.shinemo.mpush.core; +import com.google.common.eventbus.Subscribe; import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.event.HandshakeEvent; import io.netty.channel.Channel; +import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/22. */ public class ConnectionManager { + private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class); + private static final String IDLE_HANDLER_NAME = "heartbeatHandler"; public static final ConnectionManager INSTANCE = new ConnectionManager(); + public ConnectionManager() { + EventBus.INSTANCE.register(this); + } + //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8(); @@ -54,4 +66,13 @@ public List getConnections() { return new ArrayList(connections.values()); } + + @Subscribe + public void onHandshakeSuccess(HandshakeEvent event) { + int r = event.heartbeat + 3000; + int w = event.heartbeat + 3000; + Channel channel = event.connection.channel(); + channel.pipeline().addFirst(new IdleStateHandler(r, w, 0, TimeUnit.MILLISECONDS)); + LOGGER.warn("NettyChannel setHeartbeat readTimeout={}, writeTimeout={}", r, w); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java b/mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java new file mode 100644 index 00000000..e4cceb42 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java @@ -0,0 +1,45 @@ + +package com.shinemo.mpush.core; + +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.SubscriberExceptionContext; +import com.google.common.eventbus.SubscriberExceptionHandler; +import com.shinemo.mpush.api.event.Event; +import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Executor; + +/** + * Created by ohun on 2015/12/29. + */ +public class EventBus { + private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); + public static final EventBus INSTANCE = new EventBus(); + private final com.google.common.eventbus.EventBus eventBus; + + public EventBus() { + Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor("event-bus-pool", 4, 4); + eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { + @Override + public void handleException(Throwable exception, SubscriberExceptionContext context) { + LOGGER.error("event bus subscriber ex", exception); + } + }); + } + + public void post(Event event) { + eventBus.post(event); + } + + + public void register(Object bean) { + eventBus.register(bean); + } + + public void unregister(Object bean) { + eventBus.unregister(bean); + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java index e5a8181b..f81d86f7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java @@ -73,6 +73,11 @@ public void operationComplete(ChannelFuture future) throws Exception { } } + @Override + public Channel channel() { + return channel; + } + @Override public boolean isClosed() { return false; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 261eb308..2b3b5c79 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -4,6 +4,8 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.event.HandshakeEvent; +import com.shinemo.mpush.core.EventBus; import com.shinemo.mpush.core.message.ErrorMessage; import com.shinemo.mpush.core.message.HandShakeMessage; import com.shinemo.mpush.core.message.HandshakeSuccessMessage; @@ -70,6 +72,8 @@ public void handle(HandShakeMessage message) { .setClientVersion(message.clientVersion) .setDeviceId(message.deviceId); + //8.触发握手成功事件 + EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), Constants.HEARTBEAT_TIME)); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java new file mode 100644 index 00000000..893a1c65 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java @@ -0,0 +1,81 @@ +package com.shinemo.mpush.core.handler; + + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.Receiver; +import com.shinemo.mpush.core.ConnectionManager; +import com.shinemo.mpush.core.NettyConnection; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; + +import io.netty.handler.timeout.IdleStateEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/19. + */ +@ChannelHandler.Sharable +public class ServerChannelHandler extends ChannelHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); + + private final Receiver receiver; + + public ServerChannelHandler(Receiver receiver) { + this.receiver = receiver; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); + receiver.onReceive((Packet) msg, connection); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + ConnectionManager.INSTANCE.remove(ctx.channel()); + LOGGER.error(ctx.channel().remoteAddress() + ", exceptionCaught", cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.warn(ctx.channel().remoteAddress() + ", channelActive"); + Connection connection = new NettyConnection(); + connection.init(ctx.channel()); + ConnectionManager.INSTANCE.add(connection); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + LOGGER.warn(ctx.channel().remoteAddress() + ", channelInactive"); + ConnectionManager.INSTANCE.remove(ctx.channel()); + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if (evt instanceof IdleStateEvent) { + IdleStateEvent stateEvent = (IdleStateEvent) evt; + switch (stateEvent.state()) { + case READER_IDLE: + ConnectionManager.INSTANCE.remove(ctx.channel()); + ctx.close(); + LOGGER.warn("heartbeat read timeout, chanel closed!"); + break; + case WRITER_IDLE: + ctx.writeAndFlush(Packet.getHBPacket()); + LOGGER.warn("heartbeat write timeout, do write an EOL."); + break; + case ALL_IDLE: + } + } else { + LOGGER.warn("One user event Triggered. evt=" + evt); + super.userEventTriggered(ctx, evt); + } + } +} \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java deleted file mode 100644 index 1f6dcda3..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.shinemo.mpush.core.handler; - - -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Receiver; -import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.core.NettyConnection; - -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/19. - */ -public class ServerHandler extends ChannelHandlerAdapter { - - private static final Logger log = LoggerFactory.getLogger(ServerHandler.class); - - private final Receiver receiver; - - public ServerHandler(Receiver receiver) { - this.receiver = receiver; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); - receiver.onReceive((Packet) msg, connection); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - ConnectionManager.INSTANCE.remove(ctx.channel()); - log.error(ctx.channel().remoteAddress() + ", exceptionCaught", cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelActive"); - Connection connection = new NettyConnection(); - connection.init(ctx.channel()); - ConnectionManager.INSTANCE.add(connection); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.warn(ctx.channel().remoteAddress() + ", channelInactive"); - ConnectionManager.INSTANCE.remove(ctx.channel()); - } - -} \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java index fc3db7d5..8b3a9a4e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java @@ -89,4 +89,13 @@ public void sendRaw() { message.body = encode(); connection.send(message); } + + + @Override + public String toString() { + return "BaseMessage{" + + "message=" + message + + ", connection=" + connection + + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java index a2f11705..4d9d9589 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java @@ -46,4 +46,13 @@ public void encode(ByteBuf body) { public void send() { super.sendRaw(); } + + @Override + public String toString() { + return "ErrorMessage{" + + "reason='" + reason + '\'' + + ", errorCode=" + errorCode + + ", message=" + message + + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java index 9040f067..89dc55dc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java @@ -47,7 +47,7 @@ public void run(Timeout timeout) throws Exception { } catch (Exception e) { log.error("", "exception on scan", e); } finally { - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java similarity index 54% rename from mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 1aa21308..bbd3e9f6 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -4,31 +4,34 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.NettyConnection; +import com.shinemo.mpush.core.message.ErrorMessage; import com.shinemo.mpush.core.message.HandShakeMessage; import com.shinemo.mpush.core.message.HandshakeSuccessMessage; import com.shinemo.mpush.core.security.AesCipher; import com.shinemo.mpush.core.security.CipherBox; +import com.shinemo.mpush.netty.util.NettySharedHolder; import com.shinemo.mpush.tools.Strings; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; +import io.netty.channel.*; +import io.netty.util.Timeout; +import io.netty.util.TimerTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; -import java.net.SocketAddress; +import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/24. */ -public class ClientHandler extends ChannelHandlerAdapter { - private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class); +public class ClientChannelHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); private byte[] iv = CipherBox.INSTANCE.randomAESIV(); private Connection connection = new NettyConnection(); + @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { connection.init(ctx.channel()); @@ -41,33 +44,64 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { message.osVersion = "5.0"; message.timestamp = System.currentTimeMillis(); message.send(); - LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelActive"); + LOGGER.info("client channel Active"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelInactive"); + LOGGER.info("client channel Inactive"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - LOGGER.info("client," + ctx.channel().remoteAddress().toString(), "channelRead", msg); + LOGGER.info("client read new message=" + msg); if (msg instanceof Packet) { Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); if (command == Command.HANDSHAKE) { connection.getSessionContext().changeCipher(new AesCipher(clientKey, iv)); - HandshakeSuccessMessage resp = new HandshakeSuccessMessage(packet, connection); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, resp.serverKey); - LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, resp.serverKey); - saveToken(resp.sessionId); + HandshakeSuccessMessage message = new HandshakeSuccessMessage(packet, connection); + byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, message.serverKey); + saveToken(message.sessionId); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); + startHeartBeat(message.heartbeat, ctx.channel()); + LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, message.serverKey); } else if (command == Command.FAST_CONNECT) { LOGGER.info("fast connect success, message=" + packet.getStringBody()); + } else if (command == Command.ERROR) { + ErrorMessage errorMessage = new ErrorMessage(packet, connection); + LOGGER.error("receive an error message=" + errorMessage); } } } + public void startHeartBeat(final int heartbeat, final Channel channel) throws Exception { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + LOGGER.warn("client send hb failed:" + channel.remoteAddress().toString() + ",channel is not active"); + } else { + LOGGER.warn("client send hb failed:" + channel.remoteAddress().toString()); + } + } else { + LOGGER.warn("client send hb success:" + channel.remoteAddress().toString()); + } + } + }); + if (channel.isActive()) { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + } + } + }, heartbeat, TimeUnit.MILLISECONDS); + } + private void saveToken(String token) { try { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 268c0f16..2a6d8cbd 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -1,41 +1,33 @@ package com.shinemo.mpush.core.netty; -import java.util.concurrent.locks.LockSupport; - -import io.netty.channel.ChannelHandler; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.netty.client.NettyClientFactory; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.netty.client.NettyClientFactory; - /** * Created by ohun on 2015/12/24. */ public class NettyClientTest { - private static final Logger log = LoggerFactory.getLogger(NettyClientTest.class); - + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); + private String host = "127.0.0.1"; private int port = 3000; - private ChannelHandler handler = new ClientHandler(); - + private ClientChannelHandler handler = new ClientChannelHandler(); + @Test public void testClient() throws Exception { - - Client client = NettyClientFactory.instance.get(host, port, handler); - - - LockSupport.park(); - - client.close(""); - - log.error(ToStringBuilder.reflectionToString(client, ToStringStyle.MULTI_LINE_STYLE)); - + Client client = NettyClientFactory.INSTANCE.get(host, port, handler); + + client.init(); + client.start(); + + LOGGER.error(ToStringBuilder.reflectionToString(client, ToStringStyle.MULTI_LINE_STYLE)); } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 40c6d5cb..c5495bdc 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -2,7 +2,7 @@ import com.shinemo.mpush.api.Receiver; import com.shinemo.mpush.core.MessageDispatcher; -import com.shinemo.mpush.core.handler.ServerHandler; +import com.shinemo.mpush.core.handler.ServerChannelHandler; import com.shinemo.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; import org.junit.Test; @@ -21,7 +21,7 @@ public void testStop() throws Exception { public void testStart() throws Exception { Receiver receiver = new MessageDispatcher(); - ChannelHandler handler = new ServerHandler(receiver); + ChannelHandler handler = new ServerChannelHandler(receiver); final NettyServer server = new NettyServer(3000, handler); server.start(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index 65cf7b48..b9e2b574 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -51,13 +51,10 @@ public Client get(final String remoteHost, final int port, final ChannelHandler @Override public Client call() throws Exception { Client client = createClient(remoteHost, port, handler); - if (client != null) { - client.startHeartBeat(); - } return client; } }); - if (client == null || !client.isConnected()) { + if (client == null) { cachedClients.invalidate(key); return null; } @@ -78,7 +75,7 @@ protected Client createClient(final String remoteHost, final int port) throws Ex public void remove(Client client) { if (client != null) { - cachedClients.invalidate(client.getUrl()); + cachedClients.invalidate(client.getUri()); log.warn(MessageFormat.format("[Remoting] {0} is removed", client)); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index 81acd4bf..3843d0f4 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -1,14 +1,20 @@ package com.shinemo.mpush.netty.client; +import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; +import com.shinemo.mpush.netty.codec.PacketDecoder; +import com.shinemo.mpush.netty.codec.PacketEncoder; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import io.netty.util.Timeout; import io.netty.util.TimerTask; @@ -19,26 +25,70 @@ import com.shinemo.mpush.netty.util.NettySharedHolder; public class NettyClient implements Client { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); - private static final Logger log = LoggerFactory.getLogger(NettyClient.class); - - private final String remoteHost; - private final int remotePort; - private final Channel channel; + private final ChannelHandler handler; + private final String host; + private final int port; + private Channel channel; private int hbTimes = 0; - public NettyClient(final String remoteHost, final int remotePort, Channel channel) { - this.remoteHost = remoteHost; - this.remotePort = remotePort; - this.channel = channel; + public NettyClient(final String host, final int port, ChannelHandler handler) { + this.host = host; + this.port = port; + this.handler = handler; + } + + @Override + public void init() { + this.close("re init"); + EventLoopGroup workerGroup = new NioEventLoopGroup(); + final Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(handler); + } + }); + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { + channel = future.channel(); + } else { + future.cancel(true); + future.channel().close(); + LOGGER.warn("[remoting] failure to connect:" + host + "," + port); + } + } + + @Override + public void start() { + if (channel != null) { + try { + channel.closeFuture().sync(); + } catch (InterruptedException e) { + } + } } @Override public void close(String cause) { if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { - log.error("close channel:" + cause); + LOGGER.error("close channel:" + cause); + } + if (channel != null) { + channel.close(); } - this.channel.close(); } @Override @@ -61,53 +111,16 @@ public int inceaseAndGetHbTimes() { return ++hbTimes; } - @Override - public void startHeartBeat() throws Exception { - NettySharedHolder.timer.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - final Packet packet = buildHeartBeat(); - ChannelFuture channelFuture = channel.writeAndFlush(packet); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send hb msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active"); - } - log.warn("client send msg hb false:" + channel.remoteAddress().toString() + "," + packet); - } else { - log.warn("client send msg hb success:" + channel.remoteAddress().toString() + "," + packet); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.timer.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - } - } - }, Constants.TIME_DELAY, TimeUnit.SECONDS); + public String getHost() { + return host; } - private static Packet buildHeartBeat() { - return new Packet(Command.HEARTBEAT.cmd); - } - - @Override - public String getUrl() { - return String.format("%s:%s", remoteHost, remotePort); - } - - @Override - public String getRemoteHost() { - return remoteHost; + public int getPort() { + return port; } @Override - public int getRemotePort() { - return remotePort; + public String getUri() { + return host + ":" + port; } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 70ef2f89..996f5d72 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -1,70 +1,27 @@ package com.shinemo.mpush.netty.client; -import java.net.InetSocketAddress; - -import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.netty.codec.PacketDecoder; -import com.shinemo.mpush.netty.codec.PacketEncoder; - -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; public class NettyClientFactory extends AbstractNettyClientFactory { - private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientFactory.class); - public static NettyClientFactory instance = new NettyClientFactory(); + public static final NettyClientFactory INSTANCE = new NettyClientFactory(); protected Client createClient(final String host, final int port, final ChannelHandler handler) throws Exception { - EventLoopGroup workerGroup = new NioEventLoopGroup(); - final Bootstrap bootstrap = new Bootstrap(); - bootstrap.group(workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(handler); - } - }); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - Client client = new NettyClient(host, port, channel); - return client; - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + host + "," + port); - } - throw new Exception("create client exception"); + return new NettyClient(host, port, handler); } public Client getClient(final Client client) throws Exception { - return get(client.getRemoteHost(), client.getRemotePort()); + return get(client.getHost(), client.getPort()); } public void remove(final Client client) { super.remove(client); } - public void close(final Client client) { - - } - } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index e9f59705..b832e853 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -24,7 +24,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { - if (in.readByte() == Constants.HB) { + if (in.readByte() == Packet.HB_PACKET) { out.add(new Packet(Command.HEARTBEAT.cmd)); } else { in.readerIndex(in.readerIndex() - 1); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java index 1829a1a9..93b769f3 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java @@ -19,7 +19,7 @@ public class PacketEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { if (packet.cmd == Command.HEARTBEAT.cmd) { - out.writeByte(Constants.HB); + out.writeByte(Packet.HB_PACKET); } else { out.writeInt(packet.getBodyLength()); out.writeByte(packet.cmd); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index a9a992b7..2aba14b1 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -22,7 +22,7 @@ */ public class NettyServer implements Server { - private static final Logger log = LoggerFactory.getLogger(NettyServer.class); + private static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class); private final AtomicBoolean startFlag = new AtomicBoolean(false); private final int port; @@ -51,7 +51,7 @@ public boolean isRunning() { @Override public void stop() { - log.info("netty server stop now"); + LOGGER.info("netty server stop now"); this.startFlag.set(false); if (workerGroup != null) workerGroup.shutdownGracefully(); if (bossGroup != null) bossGroup.shutdownGracefully(); @@ -154,7 +154,7 @@ public void initChannel(SocketChannel ch) throws Exception { */ ChannelFuture f = b.bind(port).sync(); - log.info("server start ok on:" + port); + LOGGER.info("server start ok on:" + port); /** @@ -162,10 +162,10 @@ public void initChannel(SocketChannel ch) throws Exception { */ f.channel().closeFuture().sync(); - log.info("server start ok on:" + port); + LOGGER.info("server start ok on:" + port); } catch (Exception e) { - log.error("server start exception", e); + LOGGER.error("server start exception", e); /*** * 优雅关闭 */ diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java index bda1bb41..fbc00f44 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java @@ -11,7 +11,7 @@ public class NettySharedHolder { - public static final Timer timer = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); + public static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); } From 31bd8e2921e06aab0b806db70b8b06217d237e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Dec 2015 08:35:53 +0000 Subject: [PATCH 046/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=92=8C=E8=B7=AF=E7=94=B1=E8=B8=A2=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 11 +--- .../java/com/shinemo/mpush/api/Client.java | 12 +---- .../java/com/shinemo/mpush/api/Constants.java | 2 +- .../java/com/shinemo/mpush/api/Message.java | 4 ++ .../{Receiver.java => PacketReceiver.java} | 2 +- .../java/com/shinemo/mpush/api/Router.java | 11 ++-- .../com/shinemo/mpush/api/RouterManager.java | 8 +-- .../{RouterInfo.java => UserConnConfig.java} | 16 +++--- .../mpush/api/event/KickUserEvent.java | 16 ++++++ .../mpush/api}/message/BaseMessage.java | 53 +++++++++++-------- .../mpush/api/message/BindUserMessage.java | 11 ++-- .../mpush/api}/message/ByteBufMessage.java | 2 +- .../mpush/api}/message/ErrorMessage.java | 6 +-- .../api}/message/FastConnectMessage.java | 2 +- .../message/FastConnectSuccessMessage.java | 2 +- .../mpush/api}/message/HandShakeMessage.java | 6 +-- .../api}/message/HandshakeSuccessMessage.java | 2 +- .../mpush/api}/message/HeartbeatMessage.java | 8 ++- .../mpush/api/message/KickUserMessage.java | 42 +++++++++++++++ .../mpush/api}/message/NettyRequest.java | 2 +- .../mpush/api}/message/NettyResponse.java | 2 +- .../shinemo/mpush/api/message/OkMessage.java | 20 ++++--- .../shinemo/mpush/core/MessageDispatcher.java | 16 +++--- .../mpush/core/handler/BindHandler.java | 14 ++--- .../core/handler/FastConnectHandler.java | 6 +-- .../mpush/core/handler/HandShakeHandler.java | 6 +-- .../mpush/core/handler/HeartBeatHandler.java | 2 +- .../core/handler/ServerChannelHandler.java | 8 ++- .../core/netty/ClientChannelHandler.java | 30 +++++++---- .../mpush/core/netty/NettyServerTest.java | 4 +- .../mpush/gateway/router/LocalRouter.java | 12 +++-- .../gateway/router/LocalRouterManager.java | 14 +++-- .../mpush/gateway/router/RemoteRouter.java | 21 ++++---- .../gateway/router/RemoteRouterManager.java | 11 ++-- .../mpush/gateway/router/RouterCenter.java | 42 +++++++++++---- .../client/AbstractNettyClientFactory.java | 9 ++-- .../mpush/netty/client/NettyClient.java | 31 +---------- .../netty/client/NettyClientFactory.java | 4 -- .../mpush/netty/server/NettyServer.java | 4 -- 39 files changed, 276 insertions(+), 198 deletions(-) rename mpush-api/src/main/java/com/shinemo/mpush/api/{Receiver.java => PacketReceiver.java} (84%) rename mpush-api/src/main/java/com/shinemo/mpush/api/{RouterInfo.java => UserConnConfig.java} (62%) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/BaseMessage.java (64%) rename mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java => mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java (64%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/ByteBufMessage.java (98%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/ErrorMessage.java (91%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/FastConnectMessage.java (94%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/FastConnectSuccessMessage.java (97%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/HandShakeMessage.java (87%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/HandshakeSuccessMessage.java (98%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/HeartbeatMessage.java (77%) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/NettyRequest.java (97%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-api/src/main/java/com/shinemo/mpush/api}/message/NettyResponse.java (97%) rename mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java => mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java (56%) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 32e966be..69b20bdb 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -38,17 +38,10 @@ org.logback-extensions logback-ext-spring - - - io.netty - netty-all - - - com.google.guava - guava + com.shinemo.mpush + mpush-tools - diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index c66ec484..c869d6bd 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -6,20 +6,10 @@ public interface Client { void start(); - void close(final String cause); - - boolean isEnabled(); + void stop(); boolean isConnected(); - void resetHbTimes(); - - int inceaseAndGetHbTimes(); - - String getHost(); - - int getPort(); - String getUri(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 9b38ba1f..f4d8b9bb 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -27,5 +27,5 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; - int HEARTBEAT_TIME = 1000;//2min + int HEARTBEAT_TIME = 1000 * 60 * 5;//5min } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java index 08706665..f0892311 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.api; +import com.shinemo.mpush.api.protocol.Packet; + /** * Created by ohun on 2015/12/22. */ @@ -10,4 +12,6 @@ public interface Message { void send(); void sendRaw(); + + Packet getPacket(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java similarity index 84% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java index 5929f7a0..3ea0e557 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java @@ -5,6 +5,6 @@ /** * Created by ohun on 2015/12/22. */ -public interface Receiver { +public interface PacketReceiver { void onReceive(Packet packet, Connection connection); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java index cfa1692a..db5ecb7a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java @@ -3,9 +3,14 @@ /** * Created by ohun on 2015/12/23. */ -public interface Router { +public interface Router { - Connection getConnect(); + T getRouteInfo(); + + RouterType getType(); + + enum RouterType { + LOCAL, REMOTE + } - RouterInfo getRouterInfo(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java index fc6d9448..26b0d1d8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java @@ -3,11 +3,11 @@ /** * Created by ohun on 2015/12/23. */ -public interface RouterManager { +public interface RouterManager { - boolean publish(String userId, Router route); + R register(String userId, R route); - boolean unPublish(String userId); + boolean unRegister(String userId); - Router getRouter(String userId); + R getRouter(String userId); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java b/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java similarity index 62% rename from mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java index 55cafc14..67ae7074 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterInfo.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java @@ -3,13 +3,13 @@ /** * Created by ohun on 2015/12/23. */ -public final class RouterInfo { - private String serverIp; +public final class UserConnConfig { + private String host; private String osName; private String clientVer; - public RouterInfo(String serverIp) { - this.serverIp = serverIp; + public UserConnConfig(String host) { + this.host = host; } public String getOsName() { @@ -28,11 +28,11 @@ public void setClientVer(String clientVer) { this.clientVer = clientVer; } - public String getServerIp() { - return serverIp; + public String getHost() { + return host; } - public void setServerIp(String serverIp) { - this.serverIp = serverIp; + public void setHost(String host) { + this.host = host; } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java new file mode 100644 index 00000000..52b6eb9b --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.api.event; + +/** + * Created by ohun on 2015/12/29. + */ +public class KickUserEvent implements Event { + public final String userId; + public final String deviceId; + public final String fromServer; + + public KickUserEvent(String userId, String deviceId, String fromServer) { + this.userId = userId; + this.deviceId = deviceId; + this.fromServer = fromServer; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/BaseMessage.java similarity index 64% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/BaseMessage.java index 8b3a9a4e..7092f2e5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BaseMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/BaseMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; @@ -7,24 +7,27 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.tools.IOUtils; +import java.util.concurrent.atomic.AtomicInteger; + /** * Created by ohun on 2015/12/28. */ public abstract class BaseMessage implements Message { - protected final Packet message; + private static final AtomicInteger ID_SEQ = new AtomicInteger(); + protected final Packet packet; protected final Connection connection; - public BaseMessage(Packet message, Connection connection) { - this.message = message; + public BaseMessage(Packet packet, Connection connection) { + this.packet = packet; this.connection = connection; this.decodeBody(); } protected void decodeBody() { - if (message.body != null && message.body.length > 0) { + if (packet.body != null && packet.body.length > 0) { //1.解密 - byte[] tmp = message.body; - if (message.hasFlag(Constants.CRYPTO_FLAG)) { + byte[] tmp = packet.body; + if (packet.hasFlag(Constants.CRYPTO_FLAG)) { SessionContext info = connection.getSessionContext(); if (info.cipher != null) { tmp = info.cipher.decrypt(tmp); @@ -32,14 +35,14 @@ protected void decodeBody() { } //2.解压 - if (message.hasFlag(Constants.COMPRESS_FLAG)) { + if (packet.hasFlag(Constants.COMPRESS_FLAG)) { byte[] result = IOUtils.uncompress(tmp); if (result.length > 0) { tmp = result; } } - message.body = tmp; - decode(message.body); + packet.body = tmp; + decode(packet.body); } } @@ -51,7 +54,7 @@ protected void encodeBody() { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; - message.setFlag(Constants.COMPRESS_FLAG); + packet.setFlag(Constants.COMPRESS_FLAG); } } @@ -59,9 +62,9 @@ protected void encodeBody() { SessionContext context = connection.getSessionContext(); if (context.cipher != null) { tmp = context.cipher.encrypt(tmp); - message.setFlag(Constants.CRYPTO_FLAG); + packet.setFlag(Constants.CRYPTO_FLAG); } - message.body = tmp; + packet.body = tmp; } } @@ -69,32 +72,40 @@ protected void encodeBody() { public abstract byte[] encode(); + public Packet createResponse() { + return new Packet(packet.cmd, packet.sessionId); + } + @Override - public Connection getConnection() { - return connection; + public Packet getPacket() { + return packet; } - public Packet createResponse() { - return new Packet(message.cmd, message.sessionId); + @Override + public Connection getConnection() { + return connection; } @Override public void send() { encodeBody(); - connection.send(message); + connection.send(packet); } @Override public void sendRaw() { - message.body = encode(); - connection.send(message); + packet.body = encode(); + connection.send(packet); } + protected static int genSessionId() { + return ID_SEQ.incrementAndGet(); + } @Override public String toString() { return "BaseMessage{" + - "message=" + message + + "packet=" + packet + ", connection=" + connection + '}'; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java similarity index 64% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java index b4f34df6..a83ec915 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/BindMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java @@ -1,17 +1,22 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.google.common.base.Strings; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/28. */ -public final class BindMessage extends BaseMessage { +public final class BindUserMessage extends BaseMessage { public String userId; - public BindMessage(Packet message, Connection connection) { + public BindUserMessage(Connection connection) { + super(new Packet(Command.BIND.cmd, genSessionId()), connection); + } + + public BindUserMessage(Packet message, Connection connection) { super(message, connection); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ByteBufMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/ByteBufMessage.java similarity index 98% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/ByteBufMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/ByteBufMessage.java index aaee09ac..3ae15619 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ByteBufMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/ByteBufMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/ErrorMessage.java similarity index 91% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/ErrorMessage.java index 4d9d9589..ab2cdb1d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/ErrorMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/ErrorMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Command; @@ -17,7 +17,7 @@ public ErrorMessage(Packet message, Connection connection) { } public static ErrorMessage from(BaseMessage src) { - return new ErrorMessage(new Packet(Command.ERROR.cmd, src.message.sessionId), src.connection); + return new ErrorMessage(new Packet(Command.ERROR.cmd, src.packet.sessionId), src.connection); } public ErrorMessage setReason(String reason) { @@ -52,7 +52,7 @@ public String toString() { return "ErrorMessage{" + "reason='" + reason + '\'' + ", errorCode=" + errorCode + - ", message=" + message + + ", packet=" + packet + '}'; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectMessage.java similarity index 94% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectMessage.java index 1dd919d6..cf7fa299 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java similarity index 97% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java index f39259b7..9edcae38 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/FastConnectSuccessMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandShakeMessage.java similarity index 87% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/HandShakeMessage.java index ba7461ab..920676d6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandShakeMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandShakeMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Command; @@ -17,8 +17,8 @@ public final class HandShakeMessage extends ByteBufMessage { public byte[] clientKey; public long timestamp; - public HandShakeMessage(int sessionId, Connection connection) { - super(new Packet(Command.HANDSHAKE.cmd, sessionId), connection); + public HandShakeMessage(Connection connection) { + super(new Packet(Command.HANDSHAKE.cmd, genSessionId()), connection); } public HandShakeMessage(Packet message, Connection connection) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java similarity index 98% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java index 85dcb00e..0e9276fc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HandshakeSuccessMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/HeartbeatMessage.java similarity index 77% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/HeartbeatMessage.java index 29b900ad..c4196ad9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/HeartbeatMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/HeartbeatMessage.java @@ -1,8 +1,9 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/28. @@ -28,4 +29,9 @@ public void send() { public void sendRaw() { } + + @Override + public Packet getPacket() { + return null; + } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java new file mode 100644 index 00000000..74f63884 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java @@ -0,0 +1,42 @@ +package com.shinemo.mpush.api.message; + +import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +/** + * Created by ohun on 2015/12/29. + */ +public class KickUserMessage extends ByteBufMessage { + public String deviceId; + public String userId; + + public KickUserMessage(Connection connection) { + super(new Packet(Command.KICK.cmd), connection); + } + + public KickUserMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + deviceId = decodeString(body); + userId = decodeString(body); + } + + @Override + public void encode(ByteBuf body) { + encodeString(body, deviceId); + encodeString(body, userId); + } + + @Override + public String toString() { + return "KickUserMessage{" + + "deviceId='" + deviceId + '\'' + + ", userId='" + userId + '\'' + + '}'; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java similarity index 97% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java index b0c972c1..04a3b4d6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyRequest.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.*; import com.shinemo.mpush.api.protocol.Command; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java similarity index 97% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java index c1f03af1..d45f5982 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/NettyResponse.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Constants; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java similarity index 56% rename from mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java index a854e424..e9d0ec64 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/message/SuccessMessage.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.message; +package com.shinemo.mpush.api.message; import com.google.common.base.Strings; import com.shinemo.mpush.api.Connection; @@ -8,10 +8,10 @@ /** * Created by ohun on 2015/12/28. */ -public final class SuccessMessage extends BaseMessage { +public final class OkMessage extends BaseMessage { public String data; - public SuccessMessage(Packet message, Connection connection) { + public OkMessage(Packet message, Connection connection) { super(message, connection); } @@ -29,12 +29,20 @@ public byte[] encode() { data.getBytes(Constants.UTF_8); } - public static SuccessMessage from(BaseMessage message) { - return new SuccessMessage(message.createResponse(), message.connection); + public static OkMessage from(BaseMessage message) { + return new OkMessage(message.createResponse(), message.connection); } - public SuccessMessage setData(String data) { + public OkMessage setData(String data) { this.data = data; return this; } + + @Override + public String toString() { + return "OkMessage{" + + "data='" + data + '\'' + + "packet='" + packet + '\'' + + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java index 36dae7a7..46c36dfa 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java @@ -1,21 +1,21 @@ package com.shinemo.mpush.core; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Receiver; +import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.handler.*; -import com.shinemo.mpush.core.message.BindMessage; -import com.shinemo.mpush.core.message.FastConnectMessage; -import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.message.HeartbeatMessage; +import com.shinemo.mpush.api.message.BindUserMessage; +import com.shinemo.mpush.api.message.FastConnectMessage; +import com.shinemo.mpush.api.message.HandShakeMessage; +import com.shinemo.mpush.api.message.HeartbeatMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/22. */ -public class MessageDispatcher implements Receiver { +public class MessageDispatcher implements PacketReceiver { public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); public final BindHandler bindHandler = new BindHandler(); public final HandShakeHandler handShakeHandler = new HandShakeHandler(); @@ -34,7 +34,7 @@ public void onReceive(Packet packet, Connection connection) { handShakeHandler.handle(new HandShakeMessage(packet, connection)); break; case BIND: - bindHandler.handle(new BindMessage(packet, connection)); + bindHandler.handle(new BindUserMessage(packet, connection)); break; case FAST_CONNECT: fastConnectHandler.handle(new FastConnectMessage(packet, connection)); @@ -43,7 +43,7 @@ public void onReceive(Packet packet, Connection connection) { break; } } catch (Throwable throwable) { - LOGGER.error("dispatch message ex, packet={},conn={}", packet, connection, throwable); + LOGGER.error("dispatch packet ex, packet={},conn={}", packet, connection, throwable); connection.close(); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java index 97299d97..0e228d17 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java @@ -3,27 +3,27 @@ import com.google.common.base.Strings; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.SessionContext; -import com.shinemo.mpush.core.message.BindMessage; -import com.shinemo.mpush.core.message.ErrorMessage; -import com.shinemo.mpush.core.message.SuccessMessage; +import com.shinemo.mpush.api.message.BindUserMessage; +import com.shinemo.mpush.api.message.ErrorMessage; +import com.shinemo.mpush.api.message.OkMessage; import com.shinemo.mpush.gateway.router.RouterCenter; /** * Created by ohun on 2015/12/23. */ -public final class BindHandler implements MessageHandler { +public final class BindHandler implements MessageHandler { @Override - public void handle(BindMessage message) { + public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").send(); return; } SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { - boolean success = RouterCenter.INSTANCE.publish(message.userId, message.getConnection()); + boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { - SuccessMessage.from(message).setData("bind success").send(); + OkMessage.from(message).setData("bind success").send(); //TODO kick user } else { ErrorMessage.from(message).setReason("bind failed").send(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index efc43230..e45c0d4a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -2,9 +2,9 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.core.message.ErrorMessage; -import com.shinemo.mpush.core.message.FastConnectMessage; -import com.shinemo.mpush.core.message.FastConnectSuccessMessage; +import com.shinemo.mpush.api.message.ErrorMessage; +import com.shinemo.mpush.api.message.FastConnectMessage; +import com.shinemo.mpush.api.message.FastConnectSuccessMessage; import com.shinemo.mpush.core.security.ReusableSession; import com.shinemo.mpush.core.security.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 2b3b5c79..7a543c02 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -6,9 +6,9 @@ import com.shinemo.mpush.api.SessionContext; import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.core.EventBus; -import com.shinemo.mpush.core.message.ErrorMessage; -import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.message.HandshakeSuccessMessage; +import com.shinemo.mpush.api.message.ErrorMessage; +import com.shinemo.mpush.api.message.HandShakeMessage; +import com.shinemo.mpush.api.message.HandshakeSuccessMessage; import com.shinemo.mpush.core.security.AesCipher; import com.shinemo.mpush.core.security.CipherBox; import com.shinemo.mpush.core.security.ReusableSession; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 313661ec..ee5d2a40 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.core.handler; import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.core.message.HeartbeatMessage; +import com.shinemo.mpush.api.message.HeartbeatMessage; /** * Created by ohun on 2015/12/22. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java index 893a1c65..545b0a4a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java @@ -1,14 +1,12 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Receiver; +import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.core.NettyConnection; -import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -25,9 +23,9 @@ public class ServerChannelHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); - private final Receiver receiver; + private final PacketReceiver receiver; - public ServerChannelHandler(Receiver receiver) { + public ServerChannelHandler(PacketReceiver receiver) { this.receiver = receiver; } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index bbd3e9f6..d9eed92d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -1,12 +1,10 @@ package com.shinemo.mpush.core.netty; import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.message.*; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.core.NettyConnection; -import com.shinemo.mpush.core.message.ErrorMessage; -import com.shinemo.mpush.core.message.HandShakeMessage; -import com.shinemo.mpush.core.message.HandshakeSuccessMessage; import com.shinemo.mpush.core.security.AesCipher; import com.shinemo.mpush.core.security.CipherBox; import com.shinemo.mpush.netty.util.NettySharedHolder; @@ -30,16 +28,17 @@ public class ClientChannelHandler extends ChannelHandlerAdapter { private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); private byte[] iv = CipherBox.INSTANCE.randomAESIV(); private Connection connection = new NettyConnection(); - + private String deviceId = "test-device-id-100"; + private String userId = "1010"; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { connection.init(ctx.channel()); - HandShakeMessage message = new HandShakeMessage(1, connection); + HandShakeMessage message = new HandShakeMessage(connection); message.clientKey = clientKey; message.iv = iv; message.clientVersion = "1.0.1"; - message.deviceId = "1111111111111"; + message.deviceId = deviceId; message.osName = "android"; message.osVersion = "5.0"; message.timestamp = System.currentTimeMillis(); @@ -54,7 +53,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - LOGGER.info("client read new message=" + msg); + LOGGER.info("client read new packet=" + msg); if (msg instanceof Packet) { Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); @@ -66,15 +65,28 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); startHeartBeat(message.heartbeat, ctx.channel()); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, message.serverKey); + bindUser(); } else if (command == Command.FAST_CONNECT) { - LOGGER.info("fast connect success, message=" + packet.getStringBody()); + LOGGER.info("fast connect success, packet=" + packet.getStringBody()); + } else if (command == Command.KICK) { + LOGGER.error("receive kick user message=" + new KickUserMessage(packet, connection)); + ctx.close(); } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error message=" + errorMessage); + LOGGER.error("receive an error packet=" + errorMessage); + } else if (command == Command.BIND) { + OkMessage okMessage = new OkMessage(packet, connection); + LOGGER.info("receive an success packet=" + okMessage); } } } + private void bindUser() { + BindUserMessage message = new BindUserMessage(connection); + message.userId = userId; + message.send(); + } + public void startHeartBeat(final int heartbeat, final Channel channel) throws Exception { NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { @Override diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index c5495bdc..dcd16b40 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.netty; -import com.shinemo.mpush.api.Receiver; +import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.core.MessageDispatcher; import com.shinemo.mpush.core.handler.ServerChannelHandler; import com.shinemo.mpush.netty.server.NettyServer; @@ -20,7 +20,7 @@ public void testStop() throws Exception { @Test public void testStart() throws Exception { - Receiver receiver = new MessageDispatcher(); + PacketReceiver receiver = new MessageDispatcher(); ChannelHandler handler = new ServerChannelHandler(receiver); final NettyServer server = new NettyServer(3000, handler); diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java index ca38e975..8a307bfb 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java @@ -2,23 +2,25 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.RouterInfo; +import com.shinemo.mpush.api.UserConnConfig; /** * Created by ohun on 2015/12/23. */ -public class LocalRouter implements Router { +public class LocalRouter implements Router { private final Connection connection; public LocalRouter(Connection connection) { this.connection = connection; } - public Connection getConnect() { + @Override + public Connection getRouteInfo() { return connection; } - public RouterInfo getRouterInfo() { - return null; + @Override + public RouterType getType() { + return RouterType.LOCAL; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java index c399512d..13c0a320 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.gateway.router; -import com.shinemo.mpush.api.Router; import com.shinemo.mpush.api.RouterManager; import java.util.Map; @@ -9,23 +8,22 @@ /** * Created by ohun on 2015/12/23. */ -public class LocalRouterManager implements RouterManager { - private final Map routerMap = new ConcurrentHashMap<>(); +public class LocalRouterManager implements RouterManager { + private final Map routerMap = new ConcurrentHashMap<>(); @Override - public boolean publish(String userId, Router route) { - routerMap.put(userId, route); - return true; + public LocalRouter register(String userId, LocalRouter route) { + return routerMap.put(userId, route); } @Override - public boolean unPublish(String userId) { + public boolean unRegister(String userId) { routerMap.remove(userId); return true; } @Override - public Router getRouter(String userId) { + public LocalRouter getRouter(String userId) { return routerMap.get(userId); } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java index c1c4cd7e..68d45de6 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java @@ -1,24 +1,25 @@ package com.shinemo.mpush.gateway.router; -import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.RouterInfo; +import com.shinemo.mpush.api.UserConnConfig; /** * Created by ohun on 2015/12/23. */ -public class RemoteRouter implements Router { - private final RouterInfo routerInfo; +public class RemoteRouter implements Router { + private final UserConnConfig userConnConfig; - public RemoteRouter(RouterInfo routerInfo) { - this.routerInfo = routerInfo; + public RemoteRouter(UserConnConfig userConnConfig) { + this.userConnConfig = userConnConfig; } - public Connection getConnect() { - return null; + @Override + public UserConnConfig getRouteInfo() { + return userConnConfig; } - public RouterInfo getRouterInfo() { - return null; + @Override + public RouterType getType() { + return RouterType.REMOTE; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java index eb7b1430..d6721e73 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java @@ -1,25 +1,24 @@ package com.shinemo.mpush.gateway.router; -import com.shinemo.mpush.api.Router; import com.shinemo.mpush.api.RouterManager; /** * Created by ohun on 2015/12/23. */ -public class RemoteRouterManager implements RouterManager { +public class RemoteRouterManager implements RouterManager { @Override - public boolean publish(String userId, Router route) { - return true; + public RemoteRouter register(String userId, RemoteRouter route) { + return null; } @Override - public boolean unPublish(String userId) { + public boolean unRegister(String userId) { return true; } @Override - public Router getRouter(String userId) { + public RemoteRouter getRouter(String userId) { return null; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java index bf8640eb..0fe55e64 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java @@ -2,7 +2,9 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.RouterInfo; +import com.shinemo.mpush.api.UserConnConfig; +import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.message.KickUserMessage; /** * Created by ohun on 2015/12/23. @@ -13,22 +15,42 @@ public class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); - public boolean publish(String userId, Connection connection) { - localRouterManager.publish(userId, new LocalRouter(connection)); - remoteRouterManager.publish(userId, new RemoteRouter(new RouterInfo("127.0.0.1"))); + public boolean register(String userId, Connection connection) { + LocalRouter oldLocalRouter = localRouterManager.register(userId, new LocalRouter(connection)); + RemoteRouter oldRemoteRouter = remoteRouterManager.register(userId, new RemoteRouter(new UserConnConfig("127.0.0.1"))); + if (oldLocalRouter != null) { + kickLocalUser(userId, oldLocalRouter); + } + + if (oldRemoteRouter != null) { + kickRemoteUser(userId, oldRemoteRouter); + } return true; } - public boolean unPublish(String userId) { - localRouterManager.unPublish(userId); - remoteRouterManager.unPublish(userId); + public boolean unRegister(String userId) { + localRouterManager.unRegister(userId); + remoteRouterManager.unRegister(userId); return true; } - public Router lookup(String userId) { - Router local = localRouterManager.getRouter(userId); + public Router lookup(String userId) { + LocalRouter local = localRouterManager.getRouter(userId); if (local != null) return local; - Router remote = remoteRouterManager.getRouter(userId); + RemoteRouter remote = remoteRouterManager.getRouter(userId); return remote; } + + private void kickLocalUser(String userId, LocalRouter router) { + Connection connection = router.getRouteInfo(); + SessionContext context = connection.getSessionContext(); + KickUserMessage message = new KickUserMessage(connection); + message.deviceId = context.deviceId; + message.userId = userId; + message.send(); + } + + private void kickRemoteUser(String userId, RemoteRouter router) { + //send msg to zk + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index b9e2b574..8c9d7b32 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -18,7 +18,7 @@ public abstract class AbstractNettyClientFactory { private static final String format = "%s:%s"; - private static final Logger log = LoggerFactory.getLogger(AbstractNettyClientFactory.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNettyClientFactory.class); /** * host:port @@ -30,10 +30,11 @@ public abstract class AbstractNettyClientFactory { @Override public void onRemoval(RemovalNotification notification) { if (notification.getValue().isConnected()) { - notification.getValue().close("[Remoting] removed from cache"); + notification.getValue().stop(); + LOGGER.warn("[Remoting] removed from cache"); } } - })// + }) .build(); /** @@ -76,7 +77,7 @@ protected Client createClient(final String remoteHost, final int port) throws Ex public void remove(Client client) { if (client != null) { cachedClients.invalidate(client.getUri()); - log.warn(MessageFormat.format("[Remoting] {0} is removed", client)); + LOGGER.warn(MessageFormat.format("[Remoting] {0} is removed", client)); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index 3843d0f4..d61112dc 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -31,7 +31,6 @@ public class NettyClient implements Client { private final String host; private final int port; private Channel channel; - private int hbTimes = 0; public NettyClient(final String host, final int port, ChannelHandler handler) { this.host = host; @@ -41,7 +40,7 @@ public NettyClient(final String host, final int port, ChannelHandler handler) { @Override public void init() { - this.close("re init"); + this.stop(); EventLoopGroup workerGroup = new NioEventLoopGroup(); final Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup)// @@ -82,43 +81,17 @@ public void start() { } @Override - public void close(String cause) { - if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { - LOGGER.error("close channel:" + cause); - } + public void stop() { if (channel != null) { channel.close(); } } - @Override - public boolean isEnabled() { - return channel.isWritable(); - } - @Override public boolean isConnected() { return channel.isActive(); } - @Override - public void resetHbTimes() { - hbTimes = 0; - } - - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - public String getHost() { - return host; - } - - public int getPort() { - return port; - } - @Override public String getUri() { return host + ":" + port; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 996f5d72..96c2a83d 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -16,10 +16,6 @@ protected Client createClient(final String host, final int port, final ChannelHa return new NettyClient(host, port, handler); } - public Client getClient(final Client client) throws Exception { - return get(client.getHost(), client.getPort()); - } - public void remove(final Client client) { super.remove(client); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 2aba14b1..cacc1cda 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -35,10 +35,6 @@ public NettyServer(int port, ChannelHandler channelHandler) { this.channelHandler = channelHandler; } - public NettyServer(int port) { - this(port, null); - } - @Override public void init() { From 4e865ac455ce728064eb14686376daf222b5fc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Dec 2015 08:56:37 +0000 Subject: [PATCH 047/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=92=8C=E8=B7=AF=E7=94=B1=E8=B8=A2=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/api/RouterManager.java | 21 +++++++++++- .../com/shinemo/mpush/api/UserConnConfig.java | 32 +++++++++++++++---- .../shinemo/mpush/core/MessageDispatcher.java | 4 +-- ...{BindHandler.java => BindUserHandler.java} | 3 +- .../gateway/router/LocalRouterManager.java | 2 +- .../gateway/router/RemoteRouterManager.java | 2 +- .../mpush/gateway/router/RouterCenter.java | 20 +++++++++--- 7 files changed, 66 insertions(+), 18 deletions(-) rename mpush-core/src/main/java/com/shinemo/mpush/core/handler/{BindHandler.java => BindUserHandler.java} (91%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java index 26b0d1d8..e60ac0d1 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java @@ -5,9 +5,28 @@ */ public interface RouterManager { + /** + * 注册路由 + * + * @param userId + * @param route + * @return + */ R register(String userId, R route); + /** + * 删除路由 + * + * @param userId + * @return + */ boolean unRegister(String userId); - R getRouter(String userId); + /** + * 查询路由 + * + * @param userId + * @return + */ + R lookup(String userId); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java b/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java index 67ae7074..ff3e1c61 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java @@ -1,15 +1,24 @@ package com.shinemo.mpush.api; +import com.shinemo.mpush.tools.MPushUtil; + /** * Created by ohun on 2015/12/23. */ public final class UserConnConfig { private String host; private String osName; - private String clientVer; + private String clientVersion; + private String deviceId; - public UserConnConfig(String host) { - this.host = host; + + public String getDeviceId() { + return deviceId; + } + + public UserConnConfig setDeviceId(String deviceId) { + this.deviceId = deviceId; + return this; } public String getOsName() { @@ -20,12 +29,12 @@ public void setOsName(String osName) { this.osName = osName; } - public String getClientVer() { - return clientVer; + public String getClientVersion() { + return clientVersion; } - public void setClientVer(String clientVer) { - this.clientVer = clientVer; + public void setClientVersion(String clientVersion) { + this.clientVersion = clientVersion; } public String getHost() { @@ -35,4 +44,13 @@ public String getHost() { public void setHost(String host) { this.host = host; } + + public static UserConnConfig from(SessionContext context) { + UserConnConfig config = new UserConnConfig(); + config.osName = context.osName; + config.clientVersion = context.clientVersion; + config.deviceId = context.deviceId; + config.host = MPushUtil.getLocalIp(); + return config; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java index 46c36dfa..a4a7076a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java @@ -17,7 +17,7 @@ */ public class MessageDispatcher implements PacketReceiver { public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); - public final BindHandler bindHandler = new BindHandler(); + public final BindUserHandler bindUserHandler = new BindUserHandler(); public final HandShakeHandler handShakeHandler = new HandShakeHandler(); public final FastConnectHandler fastConnectHandler = new FastConnectHandler(); public final HeartBeatHandler heartBeatHandler = new HeartBeatHandler(); @@ -34,7 +34,7 @@ public void onReceive(Packet packet, Connection connection) { handShakeHandler.handle(new HandShakeMessage(packet, connection)); break; case BIND: - bindHandler.handle(new BindUserMessage(packet, connection)); + bindUserHandler.handle(new BindUserMessage(packet, connection)); break; case FAST_CONNECT: fastConnectHandler.handle(new FastConnectMessage(packet, connection)); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java similarity index 91% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 0e228d17..f59d6863 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -11,7 +11,7 @@ /** * Created by ohun on 2015/12/23. */ -public final class BindHandler implements MessageHandler { +public final class BindUserHandler implements MessageHandler { @Override public void handle(BindUserMessage message) { @@ -24,7 +24,6 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { OkMessage.from(message).setData("bind success").send(); - //TODO kick user } else { ErrorMessage.from(message).setReason("bind failed").send(); } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java index 13c0a320..2bf52f8c 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java @@ -23,7 +23,7 @@ public boolean unRegister(String userId) { } @Override - public LocalRouter getRouter(String userId) { + public LocalRouter lookup(String userId) { return routerMap.get(userId); } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java index d6721e73..727a0d37 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java @@ -18,7 +18,7 @@ public boolean unRegister(String userId) { } @Override - public RemoteRouter getRouter(String userId) { + public RemoteRouter lookup(String userId) { return null; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java index 0fe55e64..dcc63a31 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java @@ -15,9 +15,21 @@ public class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); + /** + * 注册用户和链接 + * + * @param userId + * @param connection + * @return + */ public boolean register(String userId, Connection connection) { - LocalRouter oldLocalRouter = localRouterManager.register(userId, new LocalRouter(connection)); - RemoteRouter oldRemoteRouter = remoteRouterManager.register(userId, new RemoteRouter(new UserConnConfig("127.0.0.1"))); + UserConnConfig connConfig = UserConnConfig.from(connection.getSessionContext()); + + LocalRouter localRouter = new LocalRouter(connection); + RemoteRouter remoteRouter = new RemoteRouter(connConfig); + + LocalRouter oldLocalRouter = localRouterManager.register(userId, localRouter); + RemoteRouter oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); if (oldLocalRouter != null) { kickLocalUser(userId, oldLocalRouter); } @@ -35,9 +47,9 @@ public boolean unRegister(String userId) { } public Router lookup(String userId) { - LocalRouter local = localRouterManager.getRouter(userId); + LocalRouter local = localRouterManager.lookup(userId); if (local != null) return local; - RemoteRouter remote = remoteRouterManager.getRouter(userId); + RemoteRouter remote = remoteRouterManager.lookup(userId); return remote; } From 2ec52f01dcabb3b2360855b5721039e6fc6219f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Dec 2015 09:12:29 +0000 Subject: [PATCH 048/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=92=8C=E8=B7=AF=E7=94=B1=E8=B8=A2=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{UserConnConfig.java => ClientLocation.java} | 8 ++++---- .../main/java/com/shinemo/mpush/api/Router.java | 2 +- .../mpush/gateway/router/LocalRouter.java | 3 +-- .../mpush/gateway/router/RemoteRouter.java | 16 ++++++++-------- .../mpush/gateway/router/RouterCenter.java | 4 ++-- 5 files changed, 16 insertions(+), 17 deletions(-) rename mpush-api/src/main/java/com/shinemo/mpush/api/{UserConnConfig.java => ClientLocation.java} (83%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java b/mpush-api/src/main/java/com/shinemo/mpush/api/ClientLocation.java similarity index 83% rename from mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/ClientLocation.java index ff3e1c61..eb93e5c0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/UserConnConfig.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/ClientLocation.java @@ -5,7 +5,7 @@ /** * Created by ohun on 2015/12/23. */ -public final class UserConnConfig { +public final class ClientLocation { private String host; private String osName; private String clientVersion; @@ -16,7 +16,7 @@ public String getDeviceId() { return deviceId; } - public UserConnConfig setDeviceId(String deviceId) { + public ClientLocation setDeviceId(String deviceId) { this.deviceId = deviceId; return this; } @@ -45,8 +45,8 @@ public void setHost(String host) { this.host = host; } - public static UserConnConfig from(SessionContext context) { - UserConnConfig config = new UserConnConfig(); + public static ClientLocation from(SessionContext context) { + ClientLocation config = new ClientLocation(); config.osName = context.osName; config.clientVersion = context.clientVersion; config.deviceId = context.deviceId; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java index db5ecb7a..9a7bc3f1 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java @@ -7,7 +7,7 @@ public interface Router { T getRouteInfo(); - RouterType getType(); + RouterType getRouteType(); enum RouterType { LOCAL, REMOTE diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java index 8a307bfb..271c5683 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java @@ -2,7 +2,6 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.UserConnConfig; /** * Created by ohun on 2015/12/23. @@ -20,7 +19,7 @@ public Connection getRouteInfo() { } @Override - public RouterType getType() { + public RouterType getRouteType() { return RouterType.LOCAL; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java index 68d45de6..a2181708 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java @@ -1,25 +1,25 @@ package com.shinemo.mpush.gateway.router; import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.UserConnConfig; +import com.shinemo.mpush.api.ClientLocation; /** * Created by ohun on 2015/12/23. */ -public class RemoteRouter implements Router { - private final UserConnConfig userConnConfig; +public class RemoteRouter implements Router { + private final ClientLocation clientLocation; - public RemoteRouter(UserConnConfig userConnConfig) { - this.userConnConfig = userConnConfig; + public RemoteRouter(ClientLocation clientLocation) { + this.clientLocation = clientLocation; } @Override - public UserConnConfig getRouteInfo() { - return userConnConfig; + public ClientLocation getRouteInfo() { + return clientLocation; } @Override - public RouterType getType() { + public RouterType getRouteType() { return RouterType.REMOTE; } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java index dcc63a31..32cf9548 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java @@ -2,7 +2,7 @@ import com.shinemo.mpush.api.Connection; import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.UserConnConfig; +import com.shinemo.mpush.api.ClientLocation; import com.shinemo.mpush.api.SessionContext; import com.shinemo.mpush.api.message.KickUserMessage; @@ -23,7 +23,7 @@ public class RouterCenter { * @return */ public boolean register(String userId, Connection connection) { - UserConnConfig connConfig = UserConnConfig.from(connection.getSessionContext()); + ClientLocation connConfig = ClientLocation.from(connection.getSessionContext()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(connConfig); From 2575c35b632c6f417f1b899aed4142a7055fd260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Dec 2015 12:00:49 +0000 Subject: [PATCH 049/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=92=8C=E8=B7=AF=E7=94=B1=E8=B8=A2=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Router.java | 2 +- .../shinemo/mpush/api/protocol/Command.java | 2 ++ .../shinemo/mpush/gateway/GatewayServer.java | 23 ++++++++++++++++++- .../mpush/gateway/router/LocalRouter.java | 2 +- .../mpush/gateway/router/RemoteRouter.java | 2 +- .../mpush/gateway/router/RouterCenter.java | 2 +- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java index 9a7bc3f1..3dfcf82c 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java @@ -5,7 +5,7 @@ */ public interface Router { - T getRouteInfo(); + T getRouteValue(); RouterType getRouteType(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index 45fe33af..47337d88 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -13,6 +13,8 @@ public enum Command { KICK(7), FAST_CONNECT(8), ERROR(9), + PUSH(10), + API(11), UNKNOWN(-1); Command(int cmd) { diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java index 72a3a61b..5dbecaa1 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java @@ -1,7 +1,28 @@ package com.shinemo.mpush.gateway; +import com.shinemo.mpush.api.Server; + /** * Created by ohun on 2015/12/23. */ -public class GatewayServer { +public class GatewayServer implements Server { + @Override + public void init() { + + } + + @Override + public void start() { + + } + + @Override + public void stop() { + + } + + @Override + public boolean isRunning() { + return false; + } } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java index 271c5683..3ec9af78 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java @@ -14,7 +14,7 @@ public LocalRouter(Connection connection) { } @Override - public Connection getRouteInfo() { + public Connection getRouteValue() { return connection; } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java index a2181708..b3c89857 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java @@ -14,7 +14,7 @@ public RemoteRouter(ClientLocation clientLocation) { } @Override - public ClientLocation getRouteInfo() { + public ClientLocation getRouteValue() { return clientLocation; } diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java index 32cf9548..7526bad7 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java +++ b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java @@ -54,7 +54,7 @@ public Router lookup(String userId) { } private void kickLocalUser(String userId, LocalRouter router) { - Connection connection = router.getRouteInfo(); + Connection connection = router.getRouteValue(); SessionContext context = connection.getSessionContext(); KickUserMessage message = new KickUserMessage(connection); message.deviceId = context.deviceId; From d0f9a652fe84df73c0fe9354f00bf36e07bff34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 30 Dec 2015 02:10:06 +0000 Subject: [PATCH 050/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=92=8C=E8=B7=AF=E7=94=B1=E8=B8=A2=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/handler/HandShakeHandler.java | 2 +- .../mpush/core/security/ReusableSessionManager.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 7a543c02..a71e612f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -50,7 +50,7 @@ public void handle(HandShakeMessage message) { //4.生成可复用session, 用于快速重连 ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); - ReusableSessionManager.INSTANCE.saveSession(session); + ReusableSessionManager.INSTANCE.cacheSession(session); //5.响应握手成功消息 HandshakeSuccessMessage diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java index f8659276..5804382b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java @@ -12,15 +12,15 @@ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; - private final Map tokenCache = new ConcurrentHashMap(); + private final Map sessionCache = new ConcurrentHashMap(); - public boolean saveSession(ReusableSession session) { - tokenCache.put(session.sessionId, session); + public boolean cacheSession(ReusableSession session) { + sessionCache.put(session.sessionId, session); return true; } public ReusableSession getSession(String sessionId) { - return tokenCache.get(sessionId); + return sessionCache.get(sessionId); } public ReusableSession genSession(SessionContext info) { From bba16d0aa292c49dcd073bcf0410ff7e7bd3caf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 30 Dec 2015 13:22:49 +0800 Subject: [PATCH 051/890] add zk --- mpush-tools/pom.xml | 10 +- .../com/shinemo/mpush/tools/zk/ZkConfig.java | 64 +++++ .../com/shinemo/mpush/tools/zk/ZkUtil.java | 240 ++++++++++++++++++ .../com/shinemo/mpush/tools/zk/ClientApp.java | 49 ++++ .../shinemo/mpush/tools/zk/CuratorTest.java | 13 + .../mpush/tools/zk/InstanceDetails.java | 67 +++++ .../com/shinemo/mpush/tools/zk/ServerApp.java | 53 ++++ .../mpush/tools/zk/ServiceDiscoverer.java | 71 ++++++ .../mpush/tools/zk/ServiceRegistrar.java | 50 ++++ .../shinemo/mpush/tools/zk/ZkUtilTest.java | 36 +++ pom.xml | 10 + 11 files changed, 662 insertions(+), 1 deletion(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index a3302831..f6d06167 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -25,5 +25,13 @@ org.apache.commons commons-lang3 + + org.apache.curator + curator-recipes + + + org.apache.curator + curator-x-discovery + - \ No newline at end of file + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java new file mode 100644 index 00000000..a8e492da --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java @@ -0,0 +1,64 @@ +package com.shinemo.mpush.tools.zk; + +public class ZkConfig { + + private final String ipLists; + + private final String namespace; + + private final int maxRetry; + + private final int minTime; + + private final int maxTime; + + private final int sessionTimeout; + + private final int connectionTimeout; + + private final String digest; + + public ZkConfig(String ipLists, String namespace, int maxRetry, int minTime, int maxTime, int sessionTimeout, int connectionTimeout,String digest) { + this.ipLists = ipLists; + this.namespace = namespace; + this.maxRetry = maxRetry; + this.minTime = minTime; + this.maxTime = maxTime; + this.sessionTimeout = sessionTimeout; + this.connectionTimeout = connectionTimeout; + this.digest = digest; + } + + public String getIpLists() { + return ipLists; + } + + public String getNamespace() { + return namespace; + } + + public int getMaxRetry() { + return maxRetry; + } + + public int getMinTime() { + return minTime; + } + + public int getMaxTime() { + return maxTime; + } + + public int getSessionTimeout() { + return sessionTimeout; + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public String getDigest() { + return digest; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java new file mode 100644 index 00000000..3c6dedf0 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -0,0 +1,240 @@ +package com.shinemo.mpush.tools.zk; + +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.api.ACLProvider; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.utils.CloseableUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.curator.framework.CuratorFrameworkFactory.Builder; + +public class ZkUtil { + + private static final Logger log = LoggerFactory.getLogger(ZkUtil.class); + + private ZkConfig zkConfig; + private CuratorFramework client; + private TreeCache cache; + + public ZkUtil(ZkConfig zkConfig) { + this.zkConfig = zkConfig; + } + + public ZkConfig getZkConfig() { + return zkConfig; + } + + public CuratorFramework getClient() { + return client; + } + + /** + * 初始化 + */ + public void init() { + log.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); + Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); + if (zkConfig.getConnectionTimeout() > 0) { + builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); + } + if (zkConfig.getSessionTimeout() > 0) { + builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); + } + if (StringUtils.isNoneBlank(zkConfig.getDigest())) { + builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { + + @Override + public List getDefaultAcl() { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + + @Override + public List getAclForPath(final String path) { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + }); + } + client = builder.build(); + client.start(); + try { + client.blockUntilConnected(); + cacheData(); + } catch (final Exception ex) { + log.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); + } + + } + + public void cacheData() throws Exception { + cache = new TreeCache(client, "/"); + cache.start(); + } + + private void waitClose() { + try { + Thread.sleep(600); + } catch (final InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + + /** + * 关闭 + */ + public void close() { + waitClose(); + CloseableUtils.closeQuietly(client); + } + + /** + * 获取数据,先从本地获取,本地找不到,从远程获取 + * @param key + * @return + */ + public String get(final String key) { + if (null == cache) { + return null; + } + ChildData resultIncache = cache.getCurrentData(key); + if (null != resultIncache) { + return null == resultIncache.getData() ? null : new String(resultIncache.getData(), Charset.forName("UTF-8")); + } + return getFromRemote(key); + } + + /** + * 从远程获取数据 + * @param key + * @return + */ + public String getFromRemote(final String key) { + try { + return new String(client.getData().forPath(key), Charset.forName("UTF-8")); + } catch (final Exception ex) { + log.error("getDirectly" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE),ex); + return null; + } + } + + /** + * 获取子节点 + * @param key + * @return + */ + public List getChildrenKeys(final String key) { + try { + List result = client.getChildren().forPath(key); + Collections.sort(result, new Comparator() { + + @Override + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + return result; + } catch (final Exception ex) { + log.error("getChildrenKeys" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE),ex); + return Collections.emptyList(); + } + } + + /** + * 判断路径是否存在 + * @param key + * @return + */ + public boolean isExisted(final String key) { + try { + return null != client.checkExists().forPath(key); + } catch (final Exception ex) { + log.error("isExisted" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE),ex); + return false; + } + } + + /** + * 持久化数据 + * @param key + * @param value + */ + public void persist(final String key, final String value) { + try { + if (!isExisted(key)) { + client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); + } else { + update(key, value); + } + } catch (final Exception ex) { + log.error("persist" + key+","+value,ex); + } + } + + /** + * 更新数据 + * @param key + * @param value + */ + public void update(final String key, final String value) { + try { + client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); + } catch (final Exception ex) { + log.error("update" + key+","+value,ex); + } + } + + /** + * 注册临时数据 + * @param key + * @param value + */ + public void putEphemeral(final String key, final String value) { + try { + if (isExisted(key)) { + client.delete().deletingChildrenIfNeeded().forPath(key); + } + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); + } catch (final Exception ex) { + log.error("persistEphemeral" + key+","+value,ex); + } + } + + /** + * 注册临时顺序数据 + * @param key + */ + public void putEphemeralSequential(final String key) { + try { + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); + } catch (final Exception ex) { + log.error("persistEphemeralSequential" + key,ex); + } + } + + /** + * 删除数据 + * @param key + */ + public void remove(final String key) { + try { + client.delete().deletingChildrenIfNeeded().forPath(key); + } catch (final Exception ex) { + log.error("remove" + key,ex); + } + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java new file mode 100644 index 00000000..e48b3ae3 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.tools.zk; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.api.CuratorEvent; +import org.apache.curator.framework.api.CuratorListener; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.x.discovery.ServiceInstance; + +public class ClientApp { + + public static void main(String[] args) throws Exception { + CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3)); + + client.getCuratorListenable().addListener(new CuratorListener() { + + @Override + public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { + System.out.println("Node data is changed, new data: " + new String(event.getData())); + } + + }); + + client.start(); + ServiceDiscoverer serviceDiscoverer = new ServiceDiscoverer(client,"services"); + + ServiceInstance instance1 = serviceDiscoverer.getInstanceByName("service1"); + + if(instance1!=null){ + System.out.println(instance1.buildUriSpec()); + System.out.println(instance1.getPayload()); + }else{ + System.out.println("instance1 is null"); + } + + ServiceInstance instance2 = serviceDiscoverer.getInstanceByName("service1"); + + if(instance2!=null){ + System.out.println(instance2.buildUriSpec()); + System.out.println(instance2.getPayload()); + }else{ + System.out.println("instance2 is null"); + } + + serviceDiscoverer.close(); + CloseableUtils.closeQuietly(client); + } +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java new file mode 100644 index 00000000..672ac2d9 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush.tools.zk; + +public class CuratorTest { + + private static String zkAddress = "127.0.0.1:2081"; + + public static void main(String[] args) { + + + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java new file mode 100644 index 00000000..56be1069 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java @@ -0,0 +1,67 @@ +package com.shinemo.mpush.tools.zk; + +import org.codehaus.jackson.map.annotate.JsonRootName; + +@JsonRootName("details") +public class InstanceDetails { + + private String id; + + private String listenAddress; + + private int listenPort; + + private String interfaceName; + + public InstanceDetails(String id, String listenAddress, int listenPort,String interfaceName) { + this.id = id; + this.listenAddress = listenAddress; + this.listenPort = listenPort; + this.interfaceName = interfaceName; + } + + public InstanceDetails() { + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getListenAddress() { + return listenAddress; + } + + public void setListenAddress(String listenAddress) { + this.listenAddress = listenAddress; + } + + public int getListenPort() { + return listenPort; + } + + public void setListenPort(int listenPort) { + this.listenPort = listenPort; + } + + public String getInterfaceName() { + return interfaceName; + } + + public void setInterfaceName(String interfaceName) { + this.interfaceName = interfaceName; + } + + @Override + public String toString() { + return "InstanceDetails{" + + "id='" + id + '\'' + + ", listenAddress='" + listenAddress + '\'' + + ", listenPort=" + listenPort + + ", interfaceName='" + interfaceName + '\'' + + '}'; + } +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java new file mode 100644 index 00000000..7861748c --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java @@ -0,0 +1,53 @@ +package com.shinemo.mpush.tools.zk; + +import java.util.UUID; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.api.CuratorEvent; +import org.apache.curator.framework.api.CuratorListener; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.x.discovery.ServiceInstance; +import org.apache.curator.x.discovery.ServiceType; +import org.apache.curator.x.discovery.UriSpec; + +public class ServerApp { + + public static void main(String[] args) throws Exception { + CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(10000, 3)); + + client.getCuratorListenable().addListener(new CuratorListener() { + + @Override + public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { + System.out.println("Node data is changed, new data: " + new String(event.getData())); + } + }); + + client.start(); + final ServiceRegistrar serviceRegistrar = new ServiceRegistrar(client, "services"); + final ServiceInstance instance1 = ServiceInstance. builder().name("service1").port(12345).address("192.168.1.100").serviceType(ServiceType.DYNAMIC) + .payload(new InstanceDetails(UUID.randomUUID().toString(), "192.168.1.100", 12345, "Test.Service1")).uriSpec(new UriSpec("{scheme}://{address}:{port}")).build(); + final ServiceInstance instance2 = ServiceInstance. builder().name("service2").port(12345).address("192.168.1.100").serviceType(ServiceType.DYNAMIC) + .payload(new InstanceDetails(UUID.randomUUID().toString(), "192.168.1.100", 12345, "Test.Service2")).uriSpec(new UriSpec("{scheme}://{address}:{port}")).build(); + serviceRegistrar.registerService(instance1); + System.out.println("register instance1"); + serviceRegistrar.registerService(instance2); + System.out.println("register instance2"); + // 删除 + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + try { + serviceRegistrar.unregisterService(instance1); + System.out.println("unregister instance1"); + serviceRegistrar.unregisterService(instance2); + System.out.println("unregister instance2"); + } catch (Exception e) { + } + } + }); + + Thread.sleep(Integer.MAX_VALUE); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java new file mode 100644 index 00000000..0b9dc044 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java @@ -0,0 +1,71 @@ +package com.shinemo.mpush.tools.zk; + +import java.io.Closeable; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.x.discovery.ServiceDiscovery; +import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; +import org.apache.curator.x.discovery.ServiceInstance; +import org.apache.curator.x.discovery.ServiceProvider; +import org.apache.curator.x.discovery.details.JsonInstanceSerializer; +import org.apache.curator.x.discovery.strategies.RandomStrategy; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +public class ServiceDiscoverer { + + private ServiceDiscovery serviceDiscovery; + private Map> providers = Maps.newConcurrentMap(); + private List closeableList = Lists.newArrayList(); + private AtomicBoolean closed = new AtomicBoolean(false); + private Object lock = new Object(); + + + public ServiceDiscoverer(CuratorFramework client, String basePath) throws Exception { + JsonInstanceSerializer serializer = new JsonInstanceSerializer(InstanceDetails.class); + serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) + .client(client) + .basePath(basePath) + .serializer(serializer) + .build(); + + serviceDiscovery.start(); + } + + + public ServiceInstance getInstanceByName(String serviceName) throws Exception { + ServiceProvider provider = providers.get(serviceName); + if (provider == null) { + synchronized (lock) { + provider = providers.get(serviceName); + if (provider == null) { + provider = serviceDiscovery.serviceProviderBuilder(). + serviceName(serviceName). + providerStrategy(new RandomStrategy()) + .build(); + provider.start(); + closeableList.add(provider); + providers.put(serviceName, provider); + } + } + } + + return provider.getInstance(); + } + + + public void close() { + Preconditions.checkState(closed.compareAndSet(false, true), "discovery service already closed..."); + + for (Closeable closeable : closeableList) { + CloseableUtils.closeQuietly(closeable); + } + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java new file mode 100644 index 00000000..9f33a624 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java @@ -0,0 +1,50 @@ +package com.shinemo.mpush.tools.zk; + +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.x.discovery.ServiceDiscovery; +import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; +import org.apache.curator.x.discovery.ServiceInstance; +import org.apache.curator.x.discovery.details.JsonInstanceSerializer; + +import com.google.common.base.Preconditions; + +public class ServiceRegistrar { + + private ServiceDiscovery serviceDiscovery; + private final CuratorFramework client; + private AtomicBoolean closed = new AtomicBoolean(false); + + public ServiceRegistrar(CuratorFramework client,String basePath) throws Exception { + this.client = client; + JsonInstanceSerializer serializer = new JsonInstanceSerializer(InstanceDetails.class); + serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) + .client(client) + .serializer(serializer) + .basePath(basePath) + .build(); + serviceDiscovery.start(); + } + + public void registerService(ServiceInstance serviceInstance) throws Exception { + serviceDiscovery.registerService(serviceInstance); + } + + public void unregisterService(ServiceInstance serviceInstance) throws Exception { + serviceDiscovery.unregisterService(serviceInstance); + + } + + public void updateService(ServiceInstance serviceInstance) throws Exception { + serviceDiscovery.updateService(serviceInstance); + + } + + public void close() throws IOException { + Preconditions.checkState(closed.compareAndSet(false, true), "Registry service already closed..."); + serviceDiscovery.close(); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java new file mode 100644 index 00000000..4009290f --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -0,0 +1,36 @@ +package com.shinemo.mpush.tools.zk; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Test; + +public class ZkUtilTest { + + private ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "huang6", 5, 3000, 6000, 3000, 3000, null); + + @Test + public void test(){ + ZkUtil zkUtil = new ZkUtil(zkConfig); + + zkUtil.init(); + + String dubbo = zkUtil.get("/dubbo"); + System.out.println(dubbo); + + List child = zkUtil.getChildrenKeys("/dubbo"); + System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); + + zkUtil.putEphemeral("/huang", "hi"); + zkUtil.putEphemeralSequential("/huang2"); + + String huang = zkUtil.get("/huang"); + System.out.println(huang); + + String huang2 = zkUtil.get("/huang2"); + System.out.println(huang2); + + } + +} diff --git a/pom.xml b/pom.xml index ff7104d0..86905ecd 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,16 @@ junit 4.10 + + org.apache.curator + curator-recipes + 2.9.1 + + + org.apache.curator + curator-x-discovery + 2.9.1 + From c2dceaf6b2d5e3229d2d431243f53f5d008225d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 30 Dec 2015 07:57:59 +0000 Subject: [PATCH 052/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E5=85=B3server=20=20push=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 31 ++++ .../java/com/shinemo/mpush/api/Client.java | 4 + .../java/com/shinemo/mpush/api/Message.java | 6 +- .../com/shinemo/mpush/api/MessageHandler.java | 7 +- .../com/shinemo/mpush/api/PacketReceiver.java | 1 + .../com/shinemo/mpush/api/PushSender.java | 18 +++ .../java/com/shinemo/mpush/api/Request.java | 18 --- .../java/com/shinemo/mpush/api/Response.java | 19 --- .../java/com/shinemo/mpush/api/Server.java | 2 - .../mpush/api/{ => connection}/Cipher.java | 2 +- .../api/{ => connection}/Connection.java | 22 +-- .../api/connection/ConnectionManager.java | 14 ++ .../api/{ => connection}/SessionContext.java | 2 +- .../mpush/api/event/HandshakeEvent.java | 2 +- .../api/exception/SendMessageException.java | 7 + .../mpush/api/message/NettyRequest.java | 59 ------- .../mpush/api/message/NettyResponse.java | 68 -------- .../shinemo/mpush/api/protocol/Command.java | 6 +- .../api/{ => router}/ClientLocation.java | 3 +- .../mpush/api/{ => router}/Router.java | 2 +- .../mpush/api/{ => router}/RouterManager.java | 2 +- mpush-client/pom.xml | 11 +- .../com/shinemo/mpush/client/PushClient.java | 93 +++++++++++ .../client/PushClientChannelHandler.java | 41 +++++ {mpush-gateway => mpush-common}/pom.xml | 9 +- .../com/shinemo/mpush/common}/EventBus.java | 2 +- .../mpush/common/MessageDispatcher.java | 38 +++++ .../common/handler/BaseMessageHandler.java | 23 +++ .../common/handler/ErrorMessageHandler.java | 20 +++ .../common/handler/OkMessageHandler.java | 20 +++ .../mpush/common}/message/BaseMessage.java | 27 +++- .../common}/message/BindUserMessage.java | 4 +- .../mpush/common}/message/ByteBufMessage.java | 4 +- .../mpush/common}/message/ErrorMessage.java | 4 +- .../common}/message/FastConnectMessage.java | 4 +- .../common/message/FastConnectOkMessage.java | 18 +-- .../common}/message/HandShakeMessage.java | 4 +- .../common/message/HandshakeOkMessage.java | 24 +-- .../common}/message/HeartbeatMessage.java | 10 +- .../common}/message/KickUserMessage.java | 4 +- .../mpush/common}/message/OkMessage.java | 4 +- .../mpush/common/message/PushMessage.java | 29 ++++ .../message/gateway/GatewayPushMessage.java | 37 +++++ .../mpush/common}/router/LocalRouter.java | 6 +- .../common}/router/LocalRouterManager.java | 4 +- .../mpush/common}/router/RemoteRouter.java | 6 +- .../common}/router/RemoteRouterManager.java | 4 +- .../mpush/common}/router/RouterCenter.java | 21 ++- .../mpush/common}/security/AesCipher.java | 4 +- .../mpush/common}/security/CipherBox.java | 2 +- .../mpush/common}/security/RsaCipher.java | 4 +- mpush-connection/pom.xml | 35 ---- .../netty/client/ConnectionClient.java | 19 --- .../src/test/resources/logback.xml | 30 ---- mpush-core/pom.xml | 43 +++-- .../shinemo/mpush/core/MessageDispatcher.java | 50 ------ .../shinemo/mpush/core/NettyConnection.java | 149 ------------------ .../{handler => }/ServerChannelHandler.java | 30 ++-- .../core/handler/BaseMessageHandler.java | 11 -- .../mpush/core/handler/BindUserHandler.java | 27 ++-- .../core/handler/FastConnectHandler.java | 27 ++-- .../core/handler/GatewayPushHandler.java | 48 ++++++ .../mpush/core/handler/HandShakeHandler.java | 35 ++-- .../mpush/core/handler/HeartBeatHandler.java | 10 +- .../mpush/core/server/ConnectionServer.java | 36 +++++ .../mpush/core/server/GatewayServer.java | 15 ++ .../ReusableSession.java | 4 +- .../ReusableSessionManager.java | 4 +- .../core/task/ScanAllClientConnection.java | 5 +- .../com/shinemo/mpush/core/task/ScanTask.java | 2 +- .../core/netty/ClientChannelHandler.java | 14 +- .../mpush/core/netty/NettyServerTest.java | 20 ++- .../mpush/core/security/CipherBoxTest.java | 1 + .../shinemo/mpush/gateway/GatewayServer.java | 28 ---- mpush-netty/pom.xml | 5 + .../client/AbstractNettyClientFactory.java | 2 +- .../mpush/netty/client/NettyClient.java | 7 +- .../netty/client/NettyClientFactory.java | 2 +- .../mpush/netty/codec/PacketEncoder.java | 1 - .../netty/connection/NettyConnection.java | 93 +++++++++++ .../connection/NettyConnectionManager.java | 17 +- .../mpush/netty/server/NettyServer.java | 80 ++++------ pom.xml | 2 + 83 files changed, 894 insertions(+), 734 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Request.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/Response.java rename mpush-api/src/main/java/com/shinemo/mpush/api/{ => connection}/Cipher.java (76%) rename mpush-api/src/main/java/com/shinemo/mpush/api/{ => connection}/Connection.java (55%) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java rename mpush-api/src/main/java/com/shinemo/mpush/api/{ => connection}/SessionContext.java (96%) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java rename mpush-api/src/main/java/com/shinemo/mpush/api/{ => router}/ClientLocation.java (92%) rename mpush-api/src/main/java/com/shinemo/mpush/api/{ => router}/Router.java (82%) rename mpush-api/src/main/java/com/shinemo/mpush/api/{ => router}/RouterManager.java (92%) create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java rename {mpush-gateway => mpush-common}/pom.xml (77%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-common/src/main/java/com/shinemo/mpush/common}/EventBus.java (97%) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/BaseMessage.java (82%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/BindUserMessage.java (90%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/ByteBufMessage.java (95%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/ErrorMessage.java (93%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/FastConnectMessage.java (87%) rename mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java => mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java (57%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/HandShakeMessage.java (93%) rename mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java => mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java (62%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/HeartbeatMessage.java (67%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/KickUserMessage.java (91%) rename {mpush-api/src/main/java/com/shinemo/mpush/api => mpush-common/src/main/java/com/shinemo/mpush/common}/message/OkMessage.java (92%) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java rename {mpush-gateway/src/main/java/com/shinemo/mpush/gateway => mpush-common/src/main/java/com/shinemo/mpush/common}/router/LocalRouter.java (75%) rename {mpush-gateway/src/main/java/com/shinemo/mpush/gateway => mpush-common/src/main/java/com/shinemo/mpush/common}/router/LocalRouterManager.java (87%) rename {mpush-gateway/src/main/java/com/shinemo/mpush/gateway => mpush-common/src/main/java/com/shinemo/mpush/common}/router/RemoteRouter.java (76%) rename {mpush-gateway/src/main/java/com/shinemo/mpush/gateway => mpush-common/src/main/java/com/shinemo/mpush/common}/router/RemoteRouterManager.java (81%) rename {mpush-gateway/src/main/java/com/shinemo/mpush/gateway => mpush-common/src/main/java/com/shinemo/mpush/common}/router/RouterCenter.java (79%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-common/src/main/java/com/shinemo/mpush/common}/security/AesCipher.java (93%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-common/src/main/java/com/shinemo/mpush/common}/security/CipherBox.java (98%) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-common/src/main/java/com/shinemo/mpush/common}/security/RsaCipher.java (88%) delete mode 100644 mpush-connection/pom.xml delete mode 100644 mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java delete mode 100644 mpush-connection/src/test/resources/logback.xml delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java rename mpush-core/src/main/java/com/shinemo/mpush/core/{handler => }/ServerChannelHandler.java (71%) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java rename mpush-core/src/main/java/com/shinemo/mpush/core/{security => session}/ReusableSession.java (65%) rename mpush-core/src/main/java/com/shinemo/mpush/core/{security => session}/ReusableSessionManager.java (95%) delete mode 100644 mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java rename mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java => mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java (85%) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 69b20bdb..ac06d525 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -43,5 +43,36 @@ mpush-tools + + + + + + src/main/resources + + **/*.xml + + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index c869d6bd..ba722632 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.api; +import io.netty.channel.ChannelHandler; + public interface Client { void init(); @@ -12,4 +14,6 @@ public interface Client { String getUri(); + ChannelHandler getHandler(); + } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java index f0892311..93f1f09d 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.api; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; +import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2015/12/22. @@ -9,9 +11,9 @@ public interface Message { Connection getConnection(); - void send(); + void send(ChannelFutureListener listener); - void sendRaw(); + void sendRaw(ChannelFutureListener listener); Packet getPacket(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java index 9fcd9211..ec64e2cb 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java @@ -1,8 +1,11 @@ package com.shinemo.mpush.api; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; + /** * Created by ohun on 2015/12/22. */ -public interface MessageHandler { - void handle(T message); +public interface MessageHandler { + void handle(Packet packet, Connection connection); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java index 3ea0e557..7bd7511a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.api; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; /** diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java new file mode 100644 index 00000000..4fbe77a1 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java @@ -0,0 +1,18 @@ +package com.shinemo.mpush.api; + +import java.util.Collection; + +/** + * Created by ohun on 2015/12/30. + */ +public interface PushSender { + void send(String content, Collection userIds, Callback callback); + + interface Callback { + void onSuccess(String userId); + + void onFailure(String userId); + + void onLose(String userId); + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java deleted file mode 100644 index 74b337ef..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Request.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.shinemo.mpush.api; - -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; - -/** - * Created by ohun on 2015/12/22. - */ -public interface Request { - - Command getCommand(); - - byte[] getBody(); - - Connection getConnection(); - - Response getResponse(); -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java deleted file mode 100644 index 2b43c70b..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Response.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.shinemo.mpush.api; - -/** - * Created by ohun on 2015/12/22. - */ -public interface Response { - - void send(byte[] body); - - void sendRaw(byte[] body); - - void sendError(byte[] reason); - - void send(String body); - - void sendRaw(String body); - - void sendError(String reason); -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java index 99719ad2..344003fc 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java @@ -4,8 +4,6 @@ * Created by ohun on 2015/12/24. */ public interface Server { - void init(); - void start(); void stop(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Cipher.java similarity index 76% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/connection/Cipher.java index efe2b2c9..9f958e15 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Cipher.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Cipher.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.shinemo.mpush.api.connection; /** * Created by ohun on 2015/12/28. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java similarity index 55% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java index b51efdd8..350d9514 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java @@ -1,39 +1,31 @@ -package com.shinemo.mpush.api; +package com.shinemo.mpush.api.connection; import io.netty.channel.Channel; import com.shinemo.mpush.api.protocol.Packet; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2015/12/22. */ public interface Connection { - void init(Channel channel); + void init(Channel channel, boolean security); SessionContext getSessionContext(); void setSessionContext(SessionContext context); - void send(Packet packet); + ChannelFuture send(Packet packet); + + void send(Packet packet, ChannelFutureListener listener); Channel channel(); String getId(); - boolean isClosed(); - - boolean isOpen(); - - int getHbTimes(); - void close(); boolean isConnected(); - - boolean isEnable(); - - String remoteIp(); - - } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java new file mode 100644 index 00000000..dbec895e --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.api.connection; + +import io.netty.channel.Channel; + +/** + * Created by ohun on 2015/12/30. + */ +public interface ConnectionManager { + Connection get(Channel channel); + + void remove(Channel channel); + + void add(Connection connection); +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java similarity index 96% rename from mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java index 64619054..43ccac8b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.shinemo.mpush.api.connection; import com.google.common.base.Strings; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java index 0d302b78..8839ed8c 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.api.event; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; /** * Created by ohun on 2015/12/29. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java new file mode 100644 index 00000000..ba7c3c40 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.api.exception; + +/** + * Created by ohun on 2015/12/30. + */ +public class SendMessageException extends RuntimeException { +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java deleted file mode 100644 index 04a3b4d6..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyRequest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.shinemo.mpush.api.message; - -import com.shinemo.mpush.api.*; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.IOUtils; -import com.shinemo.mpush.tools.crypto.AESUtils; - -/** - * Created by ohun on 2015/12/22. - */ -public class NettyRequest implements Request { - private final Packet message; - private final Connection connection; - private Command command; - private byte[] body; - - public NettyRequest(Packet message, Connection connection) { - this.message = message; - this.connection = connection; - } - - public Command getCommand() { - return command == null ? command = Command.toCMD(message.cmd) : command; - } - - public byte[] getBody() { - if (message.body == null) return null; - if (body == null) { - //1.解密 - byte[] tmp = message.body; - if (message.hasFlag(Constants.CRYPTO_FLAG)) { - SessionContext info = connection.getSessionContext(); - if (info.cipher != null) { - tmp = info.cipher.decrypt(tmp); - } - } - - //2.解压 - if (message.hasFlag(Constants.COMPRESS_FLAG)) { - byte[] result = IOUtils.uncompress(tmp); - if (result.length > 0) { - tmp = result; - } - } - this.body = tmp; - } - return body; - } - - public Connection getConnection() { - return connection; - } - - public Response getResponse() { - Packet packet = new Packet(this.message.cmd, this.message.sessionId); - return new NettyResponse(packet, connection); - } -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java b/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java deleted file mode 100644 index d45f5982..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/NettyResponse.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.shinemo.mpush.api.message; - -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.Response; -import com.shinemo.mpush.api.SessionContext; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.IOUtils; -import com.shinemo.mpush.tools.crypto.AESUtils; - -/** - * Created by ohun on 2015/12/22. - */ -public class NettyResponse implements Response { - private final Packet packet; - private final Connection connection; - - public NettyResponse(Packet packet, Connection connection) { - this.packet = packet; - this.connection = connection; - } - - public void send(byte[] body) { - byte[] tmp = body; - //1.压缩 - if (tmp.length > Constants.COMPRESS_LIMIT) { - byte[] result = IOUtils.compress(tmp); - if (result.length > 0) { - tmp = result; - packet.setFlag(Constants.COMPRESS_FLAG); - } - } - //2.加密 - SessionContext context = connection.getSessionContext(); - if (context.cipher != null) { - tmp = context.cipher.encrypt(tmp); - packet.setFlag(Constants.CRYPTO_FLAG); - } - packet.body = tmp; - connection.send(packet); - } - - public void sendRaw(byte[] body) { - packet.body = body; - connection.send(packet); - } - - - public void sendError(byte[] reason) { - packet.body = reason; - connection.send(packet); - } - - @Override - public void send(String body) { - send(body.getBytes(Constants.UTF_8)); - } - - @Override - public void sendRaw(String body) { - sendRaw(body.getBytes(Constants.UTF_8)); - } - - @Override - public void sendError(String reason) { - sendError(reason.getBytes(Constants.UTF_8)); - } -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index 47337d88..b7673ce7 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -13,8 +13,10 @@ public enum Command { KICK(7), FAST_CONNECT(8), ERROR(9), - PUSH(10), - API(11), + OK(10), + PUSH(11), + API(12), + GATEWAY_PUSH(13), UNKNOWN(-1); Command(int cmd) { diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/ClientLocation.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java similarity index 92% rename from mpush-api/src/main/java/com/shinemo/mpush/api/ClientLocation.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java index eb93e5c0..d1a38080 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/ClientLocation.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java @@ -1,5 +1,6 @@ -package com.shinemo.mpush.api; +package com.shinemo.mpush.api.router; +import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.tools.MPushUtil; /** diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/Router.java similarity index 82% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Router.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/router/Router.java index 3dfcf82c..03af6152 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Router.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/Router.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.shinemo.mpush.api.router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java similarity index 92% rename from mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java rename to mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java index e60ac0d1..90801d27 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RouterManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.shinemo.mpush.api.router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 4f0ee4df..9c5b81cf 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -12,5 +12,14 @@ mpush-client 1.0-SNAPSHOT jar - + + + com.shinemo.mpush + mpush-api + + + com.shinemo.mpush + mpush-netty + + diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java new file mode 100644 index 00000000..3e2f46e9 --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -0,0 +1,93 @@ +package com.shinemo.mpush.client; + +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.router.ClientLocation; +import com.shinemo.mpush.common.MessageDispatcher; +import com.shinemo.mpush.common.handler.ErrorMessageHandler; +import com.shinemo.mpush.common.handler.OkMessageHandler; +import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.common.router.RemoteRouterManager; +import com.shinemo.mpush.common.router.RouterCenter; +import com.shinemo.mpush.netty.client.NettyClientFactory; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; + +import java.util.Collection; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushClient implements PushSender { + + public NettyClientFactory clientFactory; + private PacketReceiver receiver; + private String host = "127.0.0.1"; + private int port = 4000; + + public void init() throws Exception { + this.clientFactory = NettyClientFactory.INSTANCE; + MessageDispatcher receiver = new MessageDispatcher(); + receiver.register(Command.OK, new OkMessageHandler()); + receiver.register(Command.ERROR, new ErrorMessageHandler()); + this.receiver = receiver; + } + + private Connection getConnection(String ip) { + try { + Client client = clientFactory.get(ip, port); + if (client == null) { + final Client client2 = clientFactory.createClient(ip, + port, new PushClientChannelHandler(receiver)); + client2.init(); + new Thread(new Runnable() { + @Override + public void run() { + client2.start(); + } + }).start(); + client = client2; + } + return ((PushClientChannelHandler) client.getHandler()).getConnection(); + } catch (Exception e) { + + } + return null; + } + + @Override + public void send(String content, Collection userIds, final Callback callback) { + RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); + for (final String userId : userIds) { + RemoteRouter router = remoteRouterManager.lookup(userId); + if (router == null) { + callback.onLose(userId); + continue; + } + ClientLocation location = router.getRouteValue(); + Connection connection = getConnection(location.getHost()); + if (connection == null || !connection.isConnected()) { + callback.onFailure(userId); + continue; + } + + GatewayPushMessage pushMessage = new GatewayPushMessage(userId + , "push content", connection); + + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + callback.onFailure(userId); + } else { + callback.onSuccess(userId); + } + } + }); + } + } +} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java new file mode 100644 index 00000000..4889d646 --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java @@ -0,0 +1,41 @@ +package com.shinemo.mpush.client; + +import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.netty.connection.NettyConnection; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushClientChannelHandler extends ChannelHandlerAdapter { + private final Connection connection = new NettyConnection(); + private final PacketReceiver receiver; + + public PushClientChannelHandler(PacketReceiver receiver) { + this.receiver = receiver; + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + connection.init(ctx.channel(), false); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + connection.close(); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + super.channelRead(ctx, msg); + receiver.onReceive(((Packet) msg), connection); + } + + public Connection getConnection() { + return connection; + } +} diff --git a/mpush-gateway/pom.xml b/mpush-common/pom.xml similarity index 77% rename from mpush-gateway/pom.xml rename to mpush-common/pom.xml index dfcea2f0..d0433d21 100644 --- a/mpush-gateway/pom.xml +++ b/mpush-common/pom.xml @@ -9,15 +9,16 @@ 4.0.0 - mpush-gateway - 1.0-SNAPSHOT - jar - + mpush-common com.shinemo.mpush mpush-api + + com.shinemo.mpush + mpush-tools + \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java b/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java similarity index 97% rename from mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java index e4cceb42..35970894 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java @@ -1,5 +1,5 @@ -package com.shinemo.mpush.core; +package com.shinemo.mpush.common; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.SubscriberExceptionContext; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java new file mode 100644 index 00000000..ea6b07eb --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush.common; + +import com.shinemo.mpush.api.MessageHandler; +import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by ohun on 2015/12/22. + */ +public class MessageDispatcher implements PacketReceiver { + public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); + private final Map handlers = new HashMap<>(); + + public void register(Command command, MessageHandler handler) { + handlers.put(command.cmd, handler); + } + + + @Override + public void onReceive(Packet packet, Connection connection) { + try { + MessageHandler handler = handlers.get(packet.cmd); + if (handler != null) { + handler.handle(packet, connection); + } + } catch (Throwable throwable) { + LOGGER.error("dispatch packet ex, packet={},conn={}", packet, connection, throwable); + connection.close(); + } + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java new file mode 100644 index 00000000..b40cc3ff --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java @@ -0,0 +1,23 @@ +package com.shinemo.mpush.common.handler; + + +import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.MessageHandler; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/22. + */ +public abstract class BaseMessageHandler implements MessageHandler { + public abstract T decode(Packet packet, Connection connection); + + public abstract void handle(T message); + + public void handle(Packet packet, Connection connection) { + T t = decode(packet, connection); + if (t != null) { + handle(t); + } + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java new file mode 100644 index 00000000..9f96837f --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.common.handler; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/30. + */ +public class ErrorMessageHandler extends BaseMessageHandler { + @Override + public ErrorMessage decode(Packet packet, Connection connection) { + return new ErrorMessage(packet, connection); + } + + @Override + public void handle(ErrorMessage message) { + + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java new file mode 100644 index 00000000..b7be18ec --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.common.handler; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.OkMessage; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/30. + */ +public class OkMessageHandler extends BaseMessageHandler { + @Override + public OkMessage decode(Packet packet, Connection connection) { + return new OkMessage(packet, connection); + } + + @Override + public void handle(OkMessage message) { + + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java similarity index 82% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/BaseMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 7092f2e5..41c2af31 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -1,11 +1,12 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Message; -import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.tools.IOUtils; +import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; @@ -87,15 +88,27 @@ public Connection getConnection() { } @Override - public void send() { + public void send(ChannelFutureListener listener) { encodeBody(); - connection.send(packet); + connection.send(packet, listener); } @Override - public void sendRaw() { + public void sendRaw(ChannelFutureListener listener) { packet.body = encode(); - connection.send(packet); + connection.send(packet, listener); + } + + public void send() { + send(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + } + + public void sendRaw() { + sendRaw(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + } + + public void close() { + send(ChannelFutureListener.CLOSE); } protected static int genSessionId() { diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java similarity index 90% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java index a83ec915..5f28d3e0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; import com.google.common.base.Strings; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/ByteBufMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java similarity index 95% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/ByteBufMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java index 3ae15619..232dd801 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java similarity index 93% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/ErrorMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index ab2cdb1d..eec0bf21 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java similarity index 87% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java index cf7fa299..a25f54a0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java similarity index 57% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java index 9edcae38..ea6b48bc 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/FastConnectSuccessMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java @@ -1,23 +1,23 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; /** * Created by ohun on 2015/12/28. */ -public final class FastConnectSuccessMessage extends ByteBufMessage { +public final class FastConnectOkMessage extends ByteBufMessage { public String serverHost; public long serverTime; public int heartbeat; - public FastConnectSuccessMessage(Packet message, Connection connection) { + public FastConnectOkMessage(Packet message, Connection connection) { super(message, connection); } - public static FastConnectSuccessMessage from(BaseMessage src) { - return new FastConnectSuccessMessage(src.createResponse(), src.connection); + public static FastConnectOkMessage from(BaseMessage src) { + return new FastConnectOkMessage(src.createResponse(), src.connection); } @Override @@ -35,17 +35,17 @@ public void encode(ByteBuf body) { } - public FastConnectSuccessMessage setServerHost(String serverHost) { + public FastConnectOkMessage setServerHost(String serverHost) { this.serverHost = serverHost; return this; } - public FastConnectSuccessMessage setServerTime(long serverTime) { + public FastConnectOkMessage setServerTime(long serverTime) { this.serverTime = serverTime; return this; } - public FastConnectSuccessMessage setHeartbeat(int heartbeat) { + public FastConnectOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandShakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java similarity index 93% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/HandShakeMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java index 920676d6..4fc8ccf5 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandShakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java similarity index 62% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java index 0e9276fc..d09fa641 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/HandshakeSuccessMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; /** * Created by ohun on 2015/12/27. */ -public final class HandshakeSuccessMessage extends ByteBufMessage { +public final class HandshakeOkMessage extends ByteBufMessage { public byte[] serverKey; public String serverHost; public long serverTime; @@ -15,7 +15,7 @@ public final class HandshakeSuccessMessage extends ByteBufMessage { public String sessionId; public long expireTime; - public HandshakeSuccessMessage(Packet message, Connection connection) { + public HandshakeOkMessage(Packet message, Connection connection) { super(message, connection); } @@ -39,36 +39,36 @@ public void encode(ByteBuf body) { encodeLong(body, expireTime); } - public static HandshakeSuccessMessage from(BaseMessage src) { - return new HandshakeSuccessMessage(src.createResponse(), src.connection); + public static HandshakeOkMessage from(BaseMessage src) { + return new HandshakeOkMessage(src.createResponse(), src.connection); } - public HandshakeSuccessMessage setServerKey(byte[] serverKey) { + public HandshakeOkMessage setServerKey(byte[] serverKey) { this.serverKey = serverKey; return this; } - public HandshakeSuccessMessage setServerHost(String serverHost) { + public HandshakeOkMessage setServerHost(String serverHost) { this.serverHost = serverHost; return this; } - public HandshakeSuccessMessage setServerTime(long serverTime) { + public HandshakeOkMessage setServerTime(long serverTime) { this.serverTime = serverTime; return this; } - public HandshakeSuccessMessage setHeartbeat(int heartbeat) { + public HandshakeOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; } - public HandshakeSuccessMessage setSessionId(String sessionId) { + public HandshakeOkMessage setSessionId(String sessionId) { this.sessionId = sessionId; return this; } - public HandshakeSuccessMessage setExpireTime(long expireTime) { + public HandshakeOkMessage setExpireTime(long expireTime) { this.expireTime = expireTime; return this; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/HeartbeatMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java similarity index 67% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/HeartbeatMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java index c4196ad9..dc1cfe9f 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/HeartbeatMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Message; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2015/12/28. @@ -21,12 +21,12 @@ public Connection getConnection() { } @Override - public void send() { + public void send(ChannelFutureListener listener) { } @Override - public void sendRaw() { + public void sendRaw(ChannelFutureListener listener) { } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java similarity index 91% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java index 74f63884..07b84c18 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java similarity index 92% rename from mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java index e9d0ec64..5cd66105 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/message/OkMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.api.message; +package com.shinemo.mpush.common.message; import com.google.common.base.Strings; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Packet; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java new file mode 100644 index 00000000..99be5684 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java @@ -0,0 +1,29 @@ +package com.shinemo.mpush.common.message; + +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushMessage extends BaseMessage { + + public String content; + + public PushMessage(String content, Connection connection) { + super(new Packet(Command.PUSH.cmd, genSessionId()), connection); + this.content = content; + } + + @Override + public void decode(byte[] body) { + content = new String(body, Constants.UTF_8); + } + + @Override + public byte[] encode() { + return content == null ? null : content.getBytes(Constants.UTF_8); + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java new file mode 100644 index 00000000..ad115eff --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.common.message.gateway; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.ByteBufMessage; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +/** + * Created by ohun on 2015/12/30. + */ +public class GatewayPushMessage extends ByteBufMessage { + public String userId; + public String content; + + public GatewayPushMessage(String userId, String content, Connection connection) { + super(new Packet(Command.GATEWAY_PUSH.cmd, genSessionId()), connection); + this.userId = userId; + this.content = content; + } + + public GatewayPushMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + userId = decodeString(body); + content = decodeString(body); + } + + @Override + public void encode(ByteBuf body) { + encodeString(body, userId); + encodeString(body, content); + } +} diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouter.java similarity index 75% rename from mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouter.java index 3ec9af78..a020b7f6 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouter.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouter.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.gateway.router; +package com.shinemo.mpush.common.router; -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Router; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.router.Router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouterManager.java similarity index 87% rename from mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouterManager.java index 2bf52f8c..2f14cef8 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/LocalRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouterManager.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.gateway.router; +package com.shinemo.mpush.common.router; -import com.shinemo.mpush.api.RouterManager; +import com.shinemo.mpush.api.router.RouterManager; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java similarity index 76% rename from mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java index b3c89857..8628e168 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.gateway.router; +package com.shinemo.mpush.common.router; -import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.ClientLocation; +import com.shinemo.mpush.api.router.Router; +import com.shinemo.mpush.api.router.ClientLocation; /** * Created by ohun on 2015/12/23. diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java similarity index 81% rename from mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 727a0d37..8a1fe5e2 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.gateway.router; +package com.shinemo.mpush.common.router; -import com.shinemo.mpush.api.RouterManager; +import com.shinemo.mpush.api.router.RouterManager; /** * Created by ohun on 2015/12/23. diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java similarity index 79% rename from mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java index 7526bad7..bbe45cc9 100644 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/router/RouterCenter.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.gateway.router; +package com.shinemo.mpush.common.router; -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.Router; -import com.shinemo.mpush.api.ClientLocation; -import com.shinemo.mpush.api.SessionContext; -import com.shinemo.mpush.api.message.KickUserMessage; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.router.Router; +import com.shinemo.mpush.api.router.ClientLocation; +import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.common.message.KickUserMessage; /** * Created by ohun on 2015/12/23. @@ -65,4 +65,13 @@ private void kickLocalUser(String userId, LocalRouter router) { private void kickRemoteUser(String userId, RemoteRouter router) { //send msg to zk } + + + public LocalRouterManager getLocalRouterManager() { + return localRouterManager; + } + + public RemoteRouterManager getRemoteRouterManager() { + return remoteRouterManager; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java similarity index 93% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index 08ffa700..b35213f9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.core.security; +package com.shinemo.mpush.common.security; -import com.shinemo.mpush.api.Cipher; +import com.shinemo.mpush.api.connection.Cipher; import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.Base64Utils; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java similarity index 98% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java index a487d32c..b4be066d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/CipherBox.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.security; +package com.shinemo.mpush.common.security; import com.shinemo.mpush.tools.Pair; import com.shinemo.mpush.tools.crypto.AESUtils; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java similarity index 88% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java index 9ecf9038..b97f3e82 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.core.security; +package com.shinemo.mpush.common.security; -import com.shinemo.mpush.api.Cipher; +import com.shinemo.mpush.api.connection.Cipher; import com.shinemo.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; diff --git a/mpush-connection/pom.xml b/mpush-connection/pom.xml deleted file mode 100644 index f1e7518f..00000000 --- a/mpush-connection/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - - 4.0.0 - - mpush-connection - 1.0-SNAPSHOT - jar - - - - com.shinemo.mpush - mpush-api - - - com.shinemo.mpush - mpush-core - - - io.netty - netty-all - - - junit - junit - - - - diff --git a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java b/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java deleted file mode 100644 index bd8f25d9..00000000 --- a/mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/client/ConnectionClient.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.shinemo.mpush.connection.netty.client; - -import io.netty.channel.Channel; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ConnectionClient { - - private static final Logger log = LoggerFactory.getLogger(ConnectionClient.class); - - private final Channel channel; - private int hbTimes = 0; - - public ConnectionClient(final String remoteIp,final int remotePort,final Channel channel) { - this.channel = channel; - } - -} diff --git a/mpush-connection/src/test/resources/logback.xml b/mpush-connection/src/test/resources/logback.xml deleted file mode 100644 index e638aca3..00000000 --- a/mpush-connection/src/test/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 3647eef9..9cfb8593 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -12,18 +12,6 @@ mpush-core 1.0-SNAPSHOT jar - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.6 - 1.6 - - - - com.shinemo.mpush @@ -76,5 +64,36 @@ mpush-tools + + + + + + src/main/resources + + **/*.xml + + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java b/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java deleted file mode 100644 index a4a7076a..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/MessageDispatcher.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shinemo.mpush.core; - -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.PacketReceiver; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.handler.*; -import com.shinemo.mpush.api.message.BindUserMessage; -import com.shinemo.mpush.api.message.FastConnectMessage; -import com.shinemo.mpush.api.message.HandShakeMessage; -import com.shinemo.mpush.api.message.HeartbeatMessage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/22. - */ -public class MessageDispatcher implements PacketReceiver { - public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); - public final BindUserHandler bindUserHandler = new BindUserHandler(); - public final HandShakeHandler handShakeHandler = new HandShakeHandler(); - public final FastConnectHandler fastConnectHandler = new FastConnectHandler(); - public final HeartBeatHandler heartBeatHandler = new HeartBeatHandler(); - - @Override - public void onReceive(Packet packet, Connection connection) { - Command command = Command.toCMD(packet.cmd); - try { - switch (command) { - case HEARTBEAT: - heartBeatHandler.handle(new HeartbeatMessage(connection)); - break; - case HANDSHAKE: - handShakeHandler.handle(new HandShakeMessage(packet, connection)); - break; - case BIND: - bindUserHandler.handle(new BindUserMessage(packet, connection)); - break; - case FAST_CONNECT: - fastConnectHandler.handle(new FastConnectMessage(packet, connection)); - break; - case UNKNOWN: - break; - } - } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={},conn={}", packet, connection, throwable); - connection.close(); - } - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java deleted file mode 100644 index f81d86f7..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.shinemo.mpush.core; - -import com.shinemo.mpush.api.SessionContext; -import com.shinemo.mpush.core.security.CipherBox; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; - -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.protocol.Packet; - -/** - * Created by ohun on 2015/12/22. - */ -public final class NettyConnection implements Connection { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); - - private SessionContext context; - private Channel channel; - private int status = 0; - - private int hbTimes; - - @Override - public void init(Channel channel) { - this.channel = channel; - this.context = new SessionContext(); - this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); - } - - @Override - public void setSessionContext(SessionContext context) { - this.context = context; - } - - @Override - public SessionContext getSessionContext() { - return context; - } - - @Override - public String getId() { - return channel.id().asLongText(); - } - - @Override - public void send(final Packet packet) { - if (packet != null) { - if (channel.isWritable()) { - ChannelFuture wf = channel.writeAndFlush(packet); - wf.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - LOGGER.warn("send msg failed, channel is not active clientIp={}, packet={}", channel.remoteAddress().toString(), packet); - ConnectionManager.INSTANCE.remove(channel); - channel.close(); - } - LOGGER.warn("send msg failed clientIp={}, packet={}", channel.remoteAddress().toString(), packet); - } else { - LOGGER.warn("send msg success clientIp={}, packet={}", channel.remoteAddress().toString(), packet); - } - } - }); - } else { - LOGGER.warn("send msg failed, channel is not writable clientIp={}, packet={}", channel.remoteAddress().toString(), packet); - } - } - } - - @Override - public Channel channel() { - return channel; - } - - @Override - public boolean isClosed() { - return false; - } - - @Override - public boolean isOpen() { - return false; - } - - public Channel getChannel() { - return channel; - } - - public void setChannel(Channel channel) { - this.channel = channel; - } - - public int increaseAndGetHbTimes() { - return ++hbTimes; - } - - public void resetHbTimes() { - hbTimes = 0; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - } - - @Override - public void close() { - this.channel.close(); - } - - @Override - public int getHbTimes() { - return hbTimes; - } - - @Override - public boolean isConnected() { - return channel.isActive(); - } - - @Override - public boolean isEnable() { - return channel.isWritable(); - } - - @Override - public String remoteIp() { - return channel.remoteAddress().toString(); - } - - - @Override - public String toString() { - return "NettyConnection{" + - "context=" + context + - ", channel=" + channel + - ", status=" + status + - '}'; - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/ServerChannelHandler.java similarity index 71% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/ServerChannelHandler.java index 545b0a4a..9ecea8c7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/ServerChannelHandler.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.core.handler; +package com.shinemo.mpush.core; +import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; -import com.shinemo.mpush.core.ConnectionManager; -import com.shinemo.mpush.core.NettyConnection; +import com.shinemo.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; @@ -23,21 +23,24 @@ public class ServerChannelHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); + private final ConnectionManager connectionManager; private final PacketReceiver receiver; + private boolean security = true; - public ServerChannelHandler(PacketReceiver receiver) { + public ServerChannelHandler(ConnectionManager connectionManager, PacketReceiver receiver) { + this.connectionManager = connectionManager; this.receiver = receiver; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); + Connection connection = connectionManager.get(ctx.channel()); receiver.onReceive((Packet) msg, connection); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - ConnectionManager.INSTANCE.remove(ctx.channel()); + connectionManager.remove(ctx.channel()); LOGGER.error(ctx.channel().remoteAddress() + ", exceptionCaught", cause); } @@ -45,14 +48,14 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.warn(ctx.channel().remoteAddress() + ", channelActive"); Connection connection = new NettyConnection(); - connection.init(ctx.channel()); - ConnectionManager.INSTANCE.add(connection); + connection.init(ctx.channel(), security); + connectionManager.add(connection); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { LOGGER.warn(ctx.channel().remoteAddress() + ", channelInactive"); - ConnectionManager.INSTANCE.remove(ctx.channel()); + connectionManager.remove(ctx.channel()); } @Override @@ -61,7 +64,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc IdleStateEvent stateEvent = (IdleStateEvent) evt; switch (stateEvent.state()) { case READER_IDLE: - ConnectionManager.INSTANCE.remove(ctx.channel()); + connectionManager.remove(ctx.channel()); ctx.close(); LOGGER.warn("heartbeat read timeout, chanel closed!"); break; @@ -76,4 +79,9 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc super.userEventTriggered(ctx, evt); } } + + public ServerChannelHandler setSecurity(boolean security) { + this.security = security; + return this; + } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java deleted file mode 100644 index eba9ff65..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BaseMessageHandler.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.shinemo.mpush.core.handler; - - -import com.shinemo.mpush.api.Message; -import com.shinemo.mpush.api.MessageHandler; - -/** - * Created by ohun on 2015/12/22. - */ -public abstract class BaseMessageHandler implements MessageHandler { -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index f59d6863..0ed2daf9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -1,22 +1,29 @@ package com.shinemo.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.SessionContext; -import com.shinemo.mpush.api.message.BindUserMessage; -import com.shinemo.mpush.api.message.ErrorMessage; -import com.shinemo.mpush.api.message.OkMessage; -import com.shinemo.mpush.gateway.router.RouterCenter; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.common.message.BindUserMessage; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.OkMessage; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.handler.BaseMessageHandler; +import com.shinemo.mpush.common.router.RouterCenter; /** * Created by ohun on 2015/12/23. */ -public final class BindUserHandler implements MessageHandler { +public final class BindUserHandler extends BaseMessageHandler { + + @Override + public BindUserMessage decode(Packet packet, Connection connection) { + return new BindUserMessage(packet, connection); + } @Override public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { - ErrorMessage.from(message).setReason("invalid param").send(); + ErrorMessage.from(message).setReason("invalid param").close(); return; } SessionContext context = message.getConnection().getSessionContext(); @@ -25,10 +32,10 @@ public void handle(BindUserMessage message) { if (success) { OkMessage.from(message).setData("bind success").send(); } else { - ErrorMessage.from(message).setReason("bind failed").send(); + ErrorMessage.from(message).setReason("bind failed").close(); } } else { - ErrorMessage.from(message).setReason("not handshake").send(); + ErrorMessage.from(message).setReason("not handshake").close(); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index e45c0d4a..cfa32bed 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -1,30 +1,37 @@ package com.shinemo.mpush.core.handler; import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.message.ErrorMessage; -import com.shinemo.mpush.api.message.FastConnectMessage; -import com.shinemo.mpush.api.message.FastConnectSuccessMessage; -import com.shinemo.mpush.core.security.ReusableSession; -import com.shinemo.mpush.core.security.ReusableSessionManager; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.FastConnectMessage; +import com.shinemo.mpush.common.message.FastConnectOkMessage; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.handler.BaseMessageHandler; +import com.shinemo.mpush.core.session.ReusableSession; +import com.shinemo.mpush.core.session.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; /** * Created by ohun on 2015/12/25. */ -public final class FastConnectHandler implements MessageHandler { +public final class FastConnectHandler extends BaseMessageHandler { + + @Override + public FastConnectMessage decode(Packet packet, Connection connection) { + return new FastConnectMessage(packet, connection); + } @Override public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); if (session == null) { - ErrorMessage.from(message).setReason("token expire").send(); + ErrorMessage.from(message).setReason("token expire").close(); } else if (!session.sessionContext.deviceId.equals(message.deviceId)) { - ErrorMessage.from(message).setReason("error device").send(); + ErrorMessage.from(message).setReason("error device").close(); } else { message.getConnection().setSessionContext(session.sessionContext); - FastConnectSuccessMessage + FastConnectOkMessage .from(message) .setServerHost(MPushUtil.getLocalIp()) .setServerTime(System.currentTimeMillis()) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java new file mode 100644 index 00000000..f3bb0d52 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -0,0 +1,48 @@ +package com.shinemo.mpush.core.handler; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.OkMessage; +import com.shinemo.mpush.common.message.PushMessage; +import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.router.Router; +import com.shinemo.mpush.common.handler.BaseMessageHandler; +import com.shinemo.mpush.common.router.RouterCenter; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; + +/** + * Created by ohun on 2015/12/30. + */ +public class GatewayPushHandler extends BaseMessageHandler { + @Override + public GatewayPushMessage decode(Packet packet, Connection connection) { + return new GatewayPushMessage(packet, connection); + } + + @Override + public void handle(final GatewayPushMessage message) { + Router router = RouterCenter.INSTANCE.lookup(message.userId); + if (router.getRouteType() == Router.RouterType.LOCAL) { + Connection connection = (Connection) router.getRouteValue(); + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + OkMessage.from(message).send(); + } else { + ErrorMessage + .from(message) + .setErrorCode((byte) 1) + .setReason("push to client error") + .send(); + } + } + }); + } else { + // TODO: 2015/12/30 send message to other server + } + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index a71e612f..9c43b634 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -2,17 +2,19 @@ import com.google.common.base.Strings; import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.event.HandshakeEvent; -import com.shinemo.mpush.core.EventBus; -import com.shinemo.mpush.api.message.ErrorMessage; -import com.shinemo.mpush.api.message.HandShakeMessage; -import com.shinemo.mpush.api.message.HandshakeSuccessMessage; -import com.shinemo.mpush.core.security.AesCipher; -import com.shinemo.mpush.core.security.CipherBox; -import com.shinemo.mpush.core.security.ReusableSession; -import com.shinemo.mpush.core.security.ReusableSessionManager; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.HandShakeMessage; +import com.shinemo.mpush.common.message.HandshakeOkMessage; +import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.handler.BaseMessageHandler; +import com.shinemo.mpush.common.security.AesCipher; +import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.core.session.ReusableSession; +import com.shinemo.mpush.core.session.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,9 +22,14 @@ /** * Created by ohun on 2015/12/24. */ -public final class HandShakeHandler implements MessageHandler { +public final class HandShakeHandler extends BaseMessageHandler { public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); + @Override + public HandShakeMessage decode(Packet packet, Connection connection) { + return new HandShakeMessage(packet, connection); + } + @Override public void handle(HandShakeMessage message) { byte[] iv = message.iv;//AES密钥向量16 @@ -34,14 +41,14 @@ public void handle(HandShakeMessage message) { if (Strings.isNullOrEmpty(message.deviceId) || iv.length != CipherBox.AES_KEY_LENGTH || clientKey.length != CipherBox.AES_KEY_LENGTH) { - ErrorMessage.from(message).setReason("Param invalid").send(); + ErrorMessage.from(message).setReason("Param invalid").close(); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - ErrorMessage.from(message).setReason("Repeat handshake").send(); + ErrorMessage.from(message).setReason("Repeat handshake").close(); return; } @@ -53,7 +60,7 @@ public void handle(HandShakeMessage message) { ReusableSessionManager.INSTANCE.cacheSession(session); //5.响应握手成功消息 - HandshakeSuccessMessage + HandshakeOkMessage .from(message) .setServerKey(serverKey) .setServerHost(MPushUtil.getLocalIp()) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index ee5d2a40..0827460e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -1,17 +1,17 @@ package com.shinemo.mpush.core.handler; import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.message.HeartbeatMessage; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. */ -public final class HeartBeatHandler implements MessageHandler { - +public final class HeartBeatHandler implements MessageHandler { @Override - public void handle(HeartbeatMessage message) { + public void handle(Packet packet, Connection connection) { System.err.println("receive client heartbeat, time=" + System.currentTimeMillis()); - } + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java new file mode 100644 index 00000000..624f1fbb --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -0,0 +1,36 @@ +package com.shinemo.mpush.core.server; + +import com.shinemo.mpush.netty.server.NettyServer; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelOption; + +/** + * Created by ohun on 2015/12/30. + */ +public class ConnectionServer extends NettyServer { + + public ConnectionServer(int port, ChannelHandler channelHandler) { + super(port, channelHandler); + } + + @Override + protected void initOptions(ServerBootstrap b) { + super.initOptions(b); + /*** + * 你可以设置这里指定的通道实现的配置参数。 + * 我们正在写一个TCP/IP的服务端, + * 因此我们被允许设置socket的参数选项比如tcpNoDelay和keepAlive。 + * 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。 + */ + b.option(ChannelOption.SO_BACKLOG, 1024); + + /** + * TCP层面的接收和发送缓冲区大小设置, + * 在Netty中分别对应ChannelOption的SO_SNDBUF和SO_RCVBUF, + * 需要根据推送消息的大小,合理设置,对于海量长连接,通常32K是个不错的选择。 + */ + b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); + b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java new file mode 100644 index 00000000..d2af5172 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.core.server; + +import com.shinemo.mpush.netty.server.NettyServer; +import io.netty.channel.ChannelHandler; + +/** + * Created by ohun on 2015/12/30. + */ +public class GatewayServer extends NettyServer { + + public GatewayServer(int port, ChannelHandler channelHandler) { + super(port, channelHandler); + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java similarity index 65% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java index c24fffa5..0acefa44 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.core.security; +package com.shinemo.mpush.core.session; -import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.connection.SessionContext; /** * Created by ohun on 2015/12/25. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java similarity index 95% rename from mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index 5804382b..ac07764f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/security/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.core.security; +package com.shinemo.mpush.core.session; -import com.shinemo.mpush.api.SessionContext; +import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.tools.crypto.MD5Utils; import java.util.Map; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java index 89dc55dc..ca78fd3a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java @@ -10,9 +10,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.core.ConnectionManager; import com.shinemo.mpush.netty.util.NettySharedHolder; /** @@ -36,7 +35,7 @@ public ScanAllClientConnection(final ScanTask... scanTasks) { public void run(Timeout timeout) throws Exception { try { final long now = System.currentTimeMillis(); - List connections = ConnectionManager.INSTANCE.getConnections(); + List connections = null; //NettyConnectionManager.INSTANCE.getConnections(); if (connections != null) { for (Connection conn : connections) { for (ScanTask task : this.taskList) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java index 1a93f391..be53bb92 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.core.task; -import com.shinemo.mpush.api.Connection; +import com.shinemo.mpush.api.connection.Connection; public interface ScanTask { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index d9eed92d..f09071a6 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -1,12 +1,12 @@ package com.shinemo.mpush.core.netty; -import com.shinemo.mpush.api.Connection; -import com.shinemo.mpush.api.message.*; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.message.*; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.core.NettyConnection; -import com.shinemo.mpush.core.security.AesCipher; -import com.shinemo.mpush.core.security.CipherBox; +import com.shinemo.mpush.common.security.AesCipher; +import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.netty.connection.NettyConnection; import com.shinemo.mpush.netty.util.NettySharedHolder; import com.shinemo.mpush.tools.Strings; @@ -33,7 +33,7 @@ public class ClientChannelHandler extends ChannelHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - connection.init(ctx.channel()); + connection.init(ctx.channel(), true); HandShakeMessage message = new HandShakeMessage(connection); message.clientKey = clientKey; message.iv = iv; @@ -59,7 +59,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Command command = Command.toCMD(packet.cmd); if (command == Command.HANDSHAKE) { connection.getSessionContext().changeCipher(new AesCipher(clientKey, iv)); - HandshakeSuccessMessage message = new HandshakeSuccessMessage(packet, connection); + HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, message.serverKey); saveToken(message.sessionId); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index dcd16b40..617661f7 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,8 +1,13 @@ package com.shinemo.mpush.core.netty; import com.shinemo.mpush.api.PacketReceiver; -import com.shinemo.mpush.core.MessageDispatcher; -import com.shinemo.mpush.core.handler.ServerChannelHandler; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.common.MessageDispatcher; +import com.shinemo.mpush.core.ServerChannelHandler; +import com.shinemo.mpush.core.handler.BindUserHandler; +import com.shinemo.mpush.core.handler.HandShakeHandler; +import com.shinemo.mpush.netty.connection.NettyConnectionManager; +import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; import org.junit.Test; @@ -20,10 +25,15 @@ public void testStop() throws Exception { @Test public void testStart() throws Exception { - PacketReceiver receiver = new MessageDispatcher(); - ChannelHandler handler = new ServerChannelHandler(receiver); + MessageDispatcher receiver = new MessageDispatcher(); + receiver.register(Command.HANDSHAKE, new HandShakeHandler()); + receiver.register(Command.HEARTBEAT, new BindUserHandler()); + receiver.register(Command.BIND, new BindUserHandler()); + NettyConnectionManager connectionManager = new NettyConnectionManager(); + connectionManager.registerEventBus(); + ChannelHandler handler = new ServerChannelHandler(connectionManager, receiver); - final NettyServer server = new NettyServer(3000, handler); + final NettyServer server = new ConnectionServer(3000, handler); server.start(); Runtime.getRuntime().addShutdownHook(new Thread() { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java index d23e8e56..3e52345f 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.core.security; +import com.shinemo.mpush.common.security.CipherBox; import org.junit.Test; import java.util.Arrays; diff --git a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java b/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java deleted file mode 100644 index 5dbecaa1..00000000 --- a/mpush-gateway/src/main/java/com/shinemo/mpush/gateway/GatewayServer.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.shinemo.mpush.gateway; - -import com.shinemo.mpush.api.Server; - -/** - * Created by ohun on 2015/12/23. - */ -public class GatewayServer implements Server { - @Override - public void init() { - - } - - @Override - public void start() { - - } - - @Override - public void stop() { - - } - - @Override - public boolean isRunning() { - return false; - } -} diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 8614196d..df917b5b 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -46,6 +46,11 @@ org.logback-extensions logback-ext-spring + + com.shinemo.mpush + mpush-common + 1.0-SNAPSHOT + diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index 8c9d7b32..f59c48ce 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -71,7 +71,7 @@ protected Client createClient(final String remoteHost, final int port) throws Ex return createClient(remoteHost, port, null); } - protected abstract Client createClient(final String remoteHost, final int port, ChannelHandler handler) throws Exception; + public abstract Client createClient(final String remoteHost, final int port, ChannelHandler handler) throws Exception; public void remove(Client client) { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index d61112dc..78c03450 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -24,7 +24,7 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.netty.util.NettySharedHolder; -public class NettyClient implements Client { +public class NettyClient implements Client { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); private final ChannelHandler handler; @@ -96,4 +96,9 @@ public boolean isConnected() { public String getUri() { return host + ":" + port; } + + @Override + public ChannelHandler getHandler() { + return handler; + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 96c2a83d..1a928829 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -12,7 +12,7 @@ public class NettyClientFactory extends AbstractNettyClientFactory { public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - protected Client createClient(final String host, final int port, final ChannelHandler handler) throws Exception { + public Client createClient(final String host, final int port, final ChannelHandler handler) throws Exception { return new NettyClient(host, port, handler); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java index 93b769f3..e0bb066f 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.netty.codec; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java new file mode 100644 index 00000000..36a21a73 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -0,0 +1,93 @@ +package com.shinemo.mpush.netty.connection; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.security.CipherBox; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/22. + */ +public final class NettyConnection implements Connection { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); + + private SessionContext context; + private Channel channel; + private boolean security; + private volatile int status = 0; + + @Override + public void init(Channel channel, boolean security) { + this.channel = channel; + this.security = security; + this.context = new SessionContext(); + if (security) { + this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); + } + this.status = 1; + } + + @Override + public void setSessionContext(SessionContext context) { + this.context = context; + } + + @Override + public SessionContext getSessionContext() { + return context; + } + + @Override + public String getId() { + return channel.id().asLongText(); + } + + @Override + public ChannelFuture send(final Packet packet) { + return channel.writeAndFlush(packet); + } + + @Override + public void send(Packet packet, ChannelFutureListener listener) { + if (listener == null) channel.writeAndFlush(packet); + else channel.writeAndFlush(packet).addListener(listener); + } + + @Override + public Channel channel() { + return channel; + } + + public Channel getChannel() { + return channel; + } + + public void setChannel(Channel channel) { + this.channel = channel; + } + + @Override + public void close() { + this.status = 0; + this.channel.close(); + } + + @Override + public boolean isConnected() { + return status == 0 || channel.isActive(); + } + + @Override + public String toString() { + return "NettyConnection{" + + "context=" + context + + ", channel=" + channel + + ", status=" + status + + '}'; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java similarity index 85% rename from mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java rename to mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 3eceefb3..564c8f75 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -1,10 +1,11 @@ -package com.shinemo.mpush.core; +package com.shinemo.mpush.netty.connection; import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.Connection; - +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; +import com.shinemo.mpush.common.EventBus; import io.netty.channel.Channel; import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; @@ -20,12 +21,10 @@ /** * Created by ohun on 2015/12/22. */ -public class ConnectionManager { - private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class); - private static final String IDLE_HANDLER_NAME = "heartbeatHandler"; - public static final ConnectionManager INSTANCE = new ConnectionManager(); +public class NettyConnectionManager implements ConnectionManager { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectionManager.class); - public ConnectionManager() { + public void registerEventBus() { EventBus.INSTANCE.register(this); } @@ -46,7 +45,7 @@ public void add(Connection connection) { public void add(Channel channel) { Connection connection = new NettyConnection(); - connection.init(channel); + connection.init(channel, true); connections.putIfAbsent(connection.getId(), connection); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index cacc1cda..08158653 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -1,44 +1,37 @@ package com.shinemo.mpush.netty.server; -import java.util.concurrent.atomic.AtomicBoolean; - -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.shinemo.mpush.api.Server; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; - import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.atomic.AtomicBoolean; /** * Created by ohun on 2015/12/22. */ -public class NettyServer implements Server { +public abstract class NettyServer implements Server { private static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class); - private final AtomicBoolean startFlag = new AtomicBoolean(false); - private final int port; - private final ChannelHandler channelHandler; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; + private final int port; + private ChannelHandler channelHandler; public NettyServer(int port, ChannelHandler channelHandler) { this.port = port; this.channelHandler = channelHandler; } - @Override - public void init() { - - } @Override public boolean isRunning() { @@ -112,38 +105,7 @@ public void initChannel(SocketChannel ch) throws Exception { } }); - /*** - * 你可以设置这里指定的通道实现的配置参数。 - * 我们正在写一个TCP/IP的服务端, - * 因此我们被允许设置socket的参数选项比如tcpNoDelay和keepAlive。 - * 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。 - */ - b.option(ChannelOption.SO_BACKLOG, 1024); - - /** - * TCP层面的接收和发送缓冲区大小设置, - * 在Netty中分别对应ChannelOption的SO_SNDBUF和SO_RCVBUF, - * 需要根据推送消息的大小,合理设置,对于海量长连接,通常32K是个不错的选择。 - */ - b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); - b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); - - /*** - * option()是提供给NioServerSocketChannel用来接收进来的连接。 - * childOption()是提供给由父管道ServerChannel接收到的连接, - * 在这个例子中也是NioServerSocketChannel。 - */ - b.childOption(ChannelOption.SO_KEEPALIVE, true); - - - /** - * 在Netty 4中实现了一个新的ByteBuf内存池,它是一个纯Java版本的 jemalloc (Facebook也在用)。 - * 现在,Netty不会再因为用零填充缓冲区而浪费内存带宽了。不过,由于它不依赖于GC,开发人员需要小心内存泄漏。 - * 如果忘记在处理程序中释放缓冲区,那么内存使用率会无限地增长。 - * Netty默认不使用内存池,需要在创建客户端或者服务端的时候进行指定 - */ - b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + initOptions(b); /*** * 绑定端口并启动去接收进来的连接 @@ -158,8 +120,6 @@ public void initChannel(SocketChannel ch) throws Exception { */ f.channel().closeFuture().sync(); - LOGGER.info("server start ok on:" + port); - } catch (Exception e) { LOGGER.error("server start exception", e); /*** @@ -168,4 +128,24 @@ public void initChannel(SocketChannel ch) throws Exception { stop(); } } + + protected void initOptions(ServerBootstrap b) { + + /*** + * option()是提供给NioServerSocketChannel用来接收进来的连接。 + * childOption()是提供给由父管道ServerChannel接收到的连接, + * 在这个例子中也是NioServerSocketChannel。 + */ + b.childOption(ChannelOption.SO_KEEPALIVE, true); + + + /** + * 在Netty 4中实现了一个新的ByteBuf内存池,它是一个纯Java版本的 jemalloc (Facebook也在用)。 + * 现在,Netty不会再因为用零填充缓冲区而浪费内存带宽了。不过,由于它不依赖于GC,开发人员需要小心内存泄漏。 + * 如果忘记在处理程序中释放缓冲区,那么内存使用率会无限地增长。 + * Netty默认不使用内存池,需要在创建客户端或者服务端的时候进行指定 + */ + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + } } diff --git a/pom.xml b/pom.xml index 86905ecd..1c0cdd85 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,8 @@ mpush-core mpush-tools mpush-netty + mpush-common + mpush-client From 5c955744023ef795ae9e9b77a6b61468a1622744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 30 Dec 2015 07:59:33 +0000 Subject: [PATCH 053/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E5=85=B3server=20=20push=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/common/message/HandShakeMessage.java | 6 +++--- .../com/shinemo/mpush/core/netty/ClientChannelHandler.java | 2 +- .../java/com/shinemo/mpush/core/netty/NettyServerTest.java | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java index 4fc8ccf5..d3d6a2ff 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2015/12/24. */ -public final class HandShakeMessage extends ByteBufMessage { +public final class HandshakeMessage extends ByteBufMessage { public String deviceId; public String osName; public String osVersion; @@ -17,11 +17,11 @@ public final class HandShakeMessage extends ByteBufMessage { public byte[] clientKey; public long timestamp; - public HandShakeMessage(Connection connection) { + public HandshakeMessage(Connection connection) { super(new Packet(Command.HANDSHAKE.cmd, genSessionId()), connection); } - public HandShakeMessage(Packet message, Connection connection) { + public HandshakeMessage(Packet message, Connection connection) { super(message, connection); } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index f09071a6..3eb7e73d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -34,7 +34,7 @@ public class ClientChannelHandler extends ChannelHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { connection.init(ctx.channel(), true); - HandShakeMessage message = new HandShakeMessage(connection); + HandshakeMessage message = new HandshakeMessage(connection); message.clientKey = clientKey; message.iv = iv; message.clientVersion = "1.0.1"; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 617661f7..03308c45 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,11 +1,10 @@ package com.shinemo.mpush.core.netty; -import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; import com.shinemo.mpush.core.ServerChannelHandler; import com.shinemo.mpush.core.handler.BindUserHandler; -import com.shinemo.mpush.core.handler.HandShakeHandler; +import com.shinemo.mpush.core.handler.HandshakeHandler; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; @@ -26,7 +25,7 @@ public void testStop() throws Exception { public void testStart() throws Exception { MessageDispatcher receiver = new MessageDispatcher(); - receiver.register(Command.HANDSHAKE, new HandShakeHandler()); + receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.HEARTBEAT, new BindUserHandler()); receiver.register(Command.BIND, new BindUserHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); From bb429deba8f10673e7b6c74b6036c64eab1456c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 30 Dec 2015 19:39:51 +0800 Subject: [PATCH 054/890] add zk --- mpush-core/pom.xml | 4 - mpush-tools/pom.xml | 24 +++++ .../com/shinemo/mpush/tools/Constants.java | 18 ++++ .../java/com/shinemo/mpush/tools/Jsons.java | 2 +- .../com/shinemo/mpush/tools/zk/PathEnum.java | 43 +++++++++ .../com/shinemo/mpush/tools/zk/ServerApp.java | 24 +++++ .../com/shinemo/mpush/tools/zk/ZkConfig.java | 17 +++- .../com/shinemo/mpush/tools/zk/ZkUtil.java | 18 +++- .../mpush/tools/zk/listener/CallBack.java | 10 ++ .../tools/zk/listener/ListenerDispatcher.java | 45 +++++++++ .../listener/impl/ConnectionPathListener.java | 28 ++++++ .../zk/listener/impl/KickPathListener.java | 28 ++++++ .../mpush/tools/zk/manage/ServerManage.java | 66 +++++++++++++ .../mpush/tools/zk/DistributedQueueTest.java | 93 ++++++++++++++++++ .../zk/{ServerApp.java => ServerAppTest.java} | 2 +- .../mpush/tools/zk/ServerManageTest.java | 64 +++++++++++++ .../shinemo/mpush/tools/zk/ZkUtilTest.java | 96 +++++++++++++++++-- mpush-tools/src/test/resources/logback.xml | 30 ++++++ pom.xml | 12 --- 19 files changed, 594 insertions(+), 30 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java rename mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/{ServerApp.java => ServerAppTest.java} (98%) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java create mode 100644 mpush-tools/src/test/resources/logback.xml diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 9cfb8593..c082fb4b 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -17,10 +17,6 @@ com.shinemo.mpush mpush-api - - com.shinemo.mpush - mpush-gateway - com.shinemo.mpush mpush-netty diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index f6d06167..d6669330 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -33,5 +33,29 @@ org.apache.curator curator-x-discovery + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + commons-logging + commons-logging + + + log4j + log4j + + + org.logback-extensions + logback-ext-spring + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 3390f7d8..86d4403a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -20,4 +20,22 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; + + //zk + int ZK_MAX_RETRY = 3; + int ZK_MIN_TIME = 5000; + int ZK_MAX_TIME = 5000; + int ZK_SESSION_TIMEOUT = 5000; + int ZK_CONNECTION_TIMEOUT = 5000; + String ZK_DEFAULT_CACHE_PATH = "/"; + String ZK_DEFAULT_DIGEST = "shinemo"; + + + //zk cs + String ZK_NAME_SPACE = "/mpush"; + //所有机器启动的时候注册ip的地方 + String ZK_REGISTER_HOST = "/allhost"; + String ZK_REGISTER_PREFIX_NAME = "machine"; + String ZK_KICK = "kickoff"; + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index 3b79d65f..f1976e70 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -26,7 +26,7 @@ public static String toJson(Object bean) { } return null; } - + public static T fromJson(String json, Class clazz) { try { return GSON.fromJson(json, clazz); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java new file mode 100644 index 00000000..ac3e83b8 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -0,0 +1,43 @@ +package com.shinemo.mpush.tools.zk; + + +public enum PathEnum { + + CONNECTION_SERVER_ALL_HOST("/cs/allhost/machine","连接服务器应用注册的路径"){ + @Override + public String getPathByIp(String ip) { + return getPath(); + } + }, + CONNECTION_SERVER_KICK("/cs/%s/kick","连接服务器踢人的路径"){ + @Override + public String getPathByIp(String ip) { + return String.format(getPath(), ip); + } + }; + + PathEnum(String path, String desc) { + this.path = path; + this.desc = desc; + } + + private final String path; + private final String desc; + + public String getPath() { + return path; + } + + public String getDesc() { + return desc; + } + + public abstract String getPathByIp(String ip); + + public static void main(String[] args) { + String test = "/cs/%s/kick"; + + System.out.println(String.format(test, "10.1.10.65")); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java new file mode 100644 index 00000000..da784a3d --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java @@ -0,0 +1,24 @@ +package com.shinemo.mpush.tools.zk; + +import java.io.Serializable; + +public class ServerApp implements Serializable{ + + private static final long serialVersionUID = 5495972321679092837L; + + private String ip; + private String port; + public String getIp() { + return ip; + } + public void setIp(String ip) { + this.ip = ip; + } + public String getPort() { + return port; + } + public void setPort(String port) { + this.port = port; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java index a8e492da..eb9cdde8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java @@ -1,4 +1,6 @@ package com.shinemo.mpush.tools.zk; +import com.shinemo.mpush.tools.Constants; + public class ZkConfig { @@ -17,8 +19,14 @@ public class ZkConfig { private final int connectionTimeout; private final String digest; + + private final String localCachePath; - public ZkConfig(String ipLists, String namespace, int maxRetry, int minTime, int maxTime, int sessionTimeout, int connectionTimeout,String digest) { + public ZkConfig(String ipLists, String namespace) { + this(ipLists, namespace, Constants.ZK_MAX_RETRY, Constants.ZK_MIN_TIME, Constants.ZK_MAX_TIME, Constants.ZK_SESSION_TIMEOUT, Constants.ZK_CONNECTION_TIMEOUT,null,Constants.ZK_DEFAULT_CACHE_PATH); + } + + public ZkConfig(String ipLists, String namespace, int maxRetry, int minTime, int maxTime, int sessionTimeout, int connectionTimeout,String digest,String localCachePath) { this.ipLists = ipLists; this.namespace = namespace; this.maxRetry = maxRetry; @@ -27,6 +35,7 @@ public ZkConfig(String ipLists, String namespace, int maxRetry, int minTime, int this.sessionTimeout = sessionTimeout; this.connectionTimeout = connectionTimeout; this.digest = digest; + this.localCachePath = localCachePath; } public String getIpLists() { @@ -60,5 +69,9 @@ public int getConnectionTimeout() { public String getDigest() { return digest; } - + + public String getLocalCachePath() { + return localCachePath; + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java index 3c6dedf0..1c971136 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -33,6 +33,8 @@ public class ZkUtil { public ZkUtil(ZkConfig zkConfig) { this.zkConfig = zkConfig; } + + public ZkConfig getZkConfig() { return zkConfig; @@ -80,8 +82,9 @@ public List getAclForPath(final String path) { } + //本地缓存 public void cacheData() throws Exception { - cache = new TreeCache(client, "/"); + cache = new TreeCache(client, zkConfig.getLocalCachePath()); cache.start(); } @@ -97,6 +100,9 @@ private void waitClose() { * 关闭 */ public void close() { + if (null != cache) { + cache.close(); + } waitClose(); CloseableUtils.closeQuietly(client); } @@ -172,7 +178,7 @@ public boolean isExisted(final String key) { * @param key * @param value */ - public void persist(final String key, final String value) { + public void registerPersist(final String key, final String value) { try { if (!isExisted(key)) { client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); @@ -202,7 +208,7 @@ public void update(final String key, final String value) { * @param key * @param value */ - public void putEphemeral(final String key, final String value) { + public void registerEphemeral(final String key, final String value) { try { if (isExisted(key)) { client.delete().deletingChildrenIfNeeded().forPath(key); @@ -217,7 +223,7 @@ public void putEphemeral(final String key, final String value) { * 注册临时顺序数据 * @param key */ - public void putEphemeralSequential(final String key) { + public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { @@ -236,5 +242,9 @@ public void remove(final String key) { log.error("remove" + key,ex); } } + + public TreeCache getCache() { + return cache; + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java new file mode 100644 index 00000000..df2721dd --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java @@ -0,0 +1,10 @@ +package com.shinemo.mpush.tools.zk.listener; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; + +public interface CallBack { + + public void handler(CuratorFramework client, TreeCacheEvent event,String path); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java new file mode 100644 index 00000000..9fe13f1b --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.tools.zk.listener; + +import java.util.Iterator; +import java.util.Map; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; +import com.shinemo.mpush.tools.zk.listener.impl.KickPathListener; + +public class ListenerDispatcher implements CallBack{ + + private static final Logger log = LoggerFactory.getLogger(ListenerDispatcher.class); + + private Map holder = Maps.newTreeMap(); + + public static ListenerDispatcher instance = new ListenerDispatcher(); + + private ListenerDispatcher() { + holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath(), new ConnectionPathListener()); + holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress()), new KickPathListener()); + } + + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + + Iterator> it = holder.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + if (path.startsWith(entry.getKey())) { + entry.getValue().handler(client, event, path); + } else { // 其他路径的事件,暂时不关心 + log.warn("path:" + path + "," + event.getType().name()); + } + } + + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java new file mode 100644 index 00000000..7bb8a64f --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -0,0 +1,28 @@ +package com.shinemo.mpush.tools.zk.listener.impl; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.listener.CallBack; + +public class ConnectionPathListener implements CallBack{ + + private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class); + + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + if (Type.NODE_ADDED == event.getType()) { + log.warn("path:" + path + ", node Add"); + } else if (Type.NODE_REMOVED == event.getType()) { + log.warn("path:" + path + ", node Remove"); + } else if (Type.NODE_UPDATED == event.getType()) { + log.warn("path:" + path + "," + "node update"); + } else { + log.warn("path:" + path + "," + event.getType().name()); + } + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java new file mode 100644 index 00000000..18605def --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java @@ -0,0 +1,28 @@ +package com.shinemo.mpush.tools.zk.listener.impl; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.listener.CallBack; + +public class KickPathListener implements CallBack{ + + private static final Logger log = LoggerFactory.getLogger(KickPathListener.class); + + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + if (Type.NODE_ADDED == event.getType()) { + log.warn("path:" + path + ", node Add"); + } else if (Type.NODE_REMOVED == event.getType()) { + log.warn("path:" + path + ", node Remove"); + } else if (Type.NODE_UPDATED == event.getType()) { + log.warn("path:" + path + "," + "node update"); + } else { + log.warn("path:" + path + "," + event.getType().name()); + } + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java new file mode 100644 index 00000000..d1335917 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -0,0 +1,66 @@ +package com.shinemo.mpush.tools.zk.manage; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.ZkConfig; +import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.listener.CallBack; +import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; + +public class ServerManage { + + private static final Logger log = LoggerFactory.getLogger(ServerManage.class); + + private static final ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "mpush"); + + private static final ZkUtil zkUtil = new ZkUtil(zkConfig); + + static { + zkUtil.init(); + } + + // 注册机器到zk中 + public void start(String ip) { + zkUtil.registerEphemeralSequential("/cs/allhost/machine"); + registerConnectionLostListener(); + registerDataChange(ListenerDispatcher.instance); + } + + // 注册连接状态监听器 + private void registerConnectionLostListener() { + zkUtil.getClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { + + @Override + public void stateChanged(final CuratorFramework client, final ConnectionState newState) { + if (ConnectionState.LOST == newState) { + log.warn(InetAddressUtil.getInetAddress() + ", lost connection"); + } else if (ConnectionState.RECONNECTED == newState) { + log.warn(InetAddressUtil.getInetAddress() + ", reconnected"); + } + } + }); + } + + // 注册节点数据变化 + private void registerDataChange(final CallBack callBack) { + zkUtil.getCache().getListenable().addListener(new TreeCacheListener() { + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + String path = null == event.getData() ? "" : event.getData().getPath(); + if (path.isEmpty()) { + return; + } + callBack.handler(client, event, path); + } + }); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java new file mode 100644 index 00000000..9e32dac7 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java @@ -0,0 +1,93 @@ +package com.shinemo.mpush.tools.zk; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.api.CuratorEvent; +import org.apache.curator.framework.api.CuratorListener; +import org.apache.curator.framework.recipes.queue.DistributedQueue; +import org.apache.curator.framework.recipes.queue.QueueBuilder; +import org.apache.curator.framework.recipes.queue.QueueConsumer; +import org.apache.curator.framework.recipes.queue.QueueSerializer; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.utils.CloseableUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DistributedQueueTest { + + private ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "mpush"); + + private ZkUtil zkUtil = new ZkUtil(zkConfig); + + private static final String PATH = "/example/queue"; + + @Before + public void setup(){ + zkUtil.init(); + zkUtil.getClient().getCuratorListenable().addListener(new CuratorListener() { + @Override + public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { + System.out.println("CuratorEvent: " + + event.getType().name()); + } + }); + } + + @Test + public void test() throws Exception{ + DistributedQueue queue = null; + QueueConsumer consumer = createQueueConsumer(); + QueueBuilder builder = QueueBuilder.builder(zkUtil.getClient(), + consumer, createQueueSerializer(), PATH); + queue = builder.buildQueue(); + queue.start(); + for (int i = 0; i < 10; i++) { + queue.put(" test-" + i); + Thread.sleep((long) (3 * Math.random())); + } + + Thread.sleep(20000); + + CloseableUtils.closeQuietly(queue); + } + + @After + public void close(){ + zkUtil.close(); + } + + + private static QueueSerializer createQueueSerializer() { + return new QueueSerializer() { + + @Override + public byte[] serialize(String item) { + return item.getBytes(); + } + + @Override + public String deserialize(byte[] bytes) { + return new String(bytes); + } + + }; + } + + private static QueueConsumer createQueueConsumer() { + + return new QueueConsumer() { + + @Override + public void consumeMessage(String message) throws Exception { + System.out.println("consume one message: " + message); + } + + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) { + System.out.println("connection new state: " + newState.name()); + } + }; + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java similarity index 98% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java rename to mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java index 7861748c..1dab9f3a 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerApp.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java @@ -11,7 +11,7 @@ import org.apache.curator.x.discovery.ServiceType; import org.apache.curator.x.discovery.UriSpec; -public class ServerApp { +public class ServerAppTest { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(10000, 3)); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java new file mode 100644 index 00000000..40b3f3a4 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -0,0 +1,64 @@ +package com.shinemo.mpush.tools.zk; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +public class ServerManageTest { + + private static Executor executor = Executors.newCachedThreadPool(); + + @Test + public void testMulThread() throws InterruptedException{ + CountDownLatch latch = new CountDownLatch(1); + for(int i = 1;i<=10;i++){ + executor.execute(new Worker("192.168.1."+i, latch)); + } + latch.countDown(); + + Thread.sleep(Integer.MAX_VALUE); + } + + + @Test + public void testUpdate(){ + ServerManage manage = new ServerManage(); + manage.start("192.168.1.1"); + + } + + + private static class Worker implements Runnable{ + + private static final Logger log = LoggerFactory.getLogger(Worker.class); + + private final String ip; + private final CountDownLatch latch; + + public Worker(String ip, CountDownLatch latch) { + this.ip = ip; + this.latch = latch; + } + + @Override + public void run() { + try { + latch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + log.warn("start init "+ip); + ServerManage manage = new ServerManage(); + manage.start(ip); + log.warn("end init "+ip); + } + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 4009290f..89509fd9 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -4,17 +4,25 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; import org.junit.Test; +import com.shinemo.mpush.tools.Constants; +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.Jsons; public class ZkUtilTest { - private ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "huang6", 5, 3000, 6000, 3000, 3000, null); + private ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "mpush"); + + private ZkUtil zkUtil = new ZkUtil(zkConfig); + + @Before + public void setup(){ + zkUtil.init(); + } @Test public void test(){ - ZkUtil zkUtil = new ZkUtil(zkConfig); - - zkUtil.init(); String dubbo = zkUtil.get("/dubbo"); System.out.println(dubbo); @@ -22,8 +30,10 @@ public void test(){ List child = zkUtil.getChildrenKeys("/dubbo"); System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); - zkUtil.putEphemeral("/huang", "hi"); - zkUtil.putEphemeralSequential("/huang2"); + zkUtil.registerPersist("/hi", "hello world"); + + zkUtil.registerEphemeral("/huang", "hi"); + zkUtil.registerEphemeralSequential("/huang2"); String huang = zkUtil.get("/huang"); System.out.println(huang); @@ -32,5 +42,79 @@ public void test(){ System.out.println(huang2); } + + @Test + public void getTest(){ + String value = zkUtil.get("/hi"); + System.out.println(value); + } + + /** + * 注册机器到/mpush/allhost 目录下边 + */ + @Test + public void testRegister(){ + + String path = Constants.ZK_NAME_SPACE; + + String prefix = Constants.ZK_REGISTER_PREFIX_NAME; + + List hosts = zkUtil.getChildrenKeys(path); + + System.out.println("before register"); + + for(int i = 0;i + + + System.out + UTF-8 + + INFO + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/pom.xml b/pom.xml index 1c0cdd85..88894b18 100644 --- a/pom.xml +++ b/pom.xml @@ -16,8 +16,6 @@ 1.0-SNAPSHOT mpush-api - mpush-connection - mpush-gateway mpush-core mpush-tools mpush-netty @@ -91,21 +89,11 @@ mpush-netty ${mpush-netty-version} - - com.shinemo.mpush - mpush-connection - ${mpush-connection-version} - com.shinemo.mpush mpush-core ${mpush-core-version} - - com.shinemo.mpush - mpush-gateway - ${mpush-gateway-version} - com.shinemo.mpush mpush-tools From 0763628258608e18199ab73f5053d11e1879005b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 30 Dec 2015 19:41:06 +0800 Subject: [PATCH 055/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/tools/zk/manage/ServerManage.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index d1335917..e37e097e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; @@ -28,7 +29,7 @@ public class ServerManage { // 注册机器到zk中 public void start(String ip) { - zkUtil.registerEphemeralSequential("/cs/allhost/machine"); + zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); registerConnectionLostListener(); registerDataChange(ListenerDispatcher.instance); } From 2a7d021e798eae9332dfd923e5dcf91e948c973a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 30 Dec 2015 19:59:07 +0800 Subject: [PATCH 056/890] add queue test --- .../com/shinemo/mpush/tools/zk/PathEnum.java | 2 +- .../mpush/tools/zk/manage/ServerManage.java | 15 ++++ .../zk/DistributedQueueConsumerTest.java | 71 +++++++++++++++++++ ...java => DistributedQueueProviderTest.java} | 26 +++---- 4 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java rename mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/{DistributedQueueTest.java => DistributedQueueProviderTest.java} (72%) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java index ac3e83b8..8aac5cd3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -9,7 +9,7 @@ public String getPathByIp(String ip) { return getPath(); } }, - CONNECTION_SERVER_KICK("/cs/%s/kick","连接服务器踢人的路径"){ + CONNECTION_SERVER_KICK("/cs/%s/kick/con","连接服务器踢人的路径"){ @Override public String getPathByIp(String ip) { return String.format(getPath(), ip); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index e37e097e..35f40d61 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.tools.zk.manage; import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; import org.apache.curator.framework.state.ConnectionState; @@ -63,5 +64,19 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc } }); } + + public CuratorFramework getClient() { + return zkUtil.getClient(); + } + + public TreeCache getCache() { + return zkUtil.getCache(); + } + + public void close(){ + zkUtil.close(); + } + + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java new file mode 100644 index 00000000..3f4cc90d --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -0,0 +1,71 @@ +package com.shinemo.mpush.tools.zk; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.queue.DistributedQueue; +import org.apache.curator.framework.recipes.queue.QueueBuilder; +import org.apache.curator.framework.recipes.queue.QueueConsumer; +import org.apache.curator.framework.recipes.queue.QueueSerializer; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.utils.CloseableUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +public class DistributedQueueConsumerTest { + + private ServerManage manage = new ServerManage(); + + @Before + public void setup(){ + } + + @Test + public void test() throws Exception{ + + QueueConsumer consumer = createQueueConsumer(); + + } + + @After + public void close(){ + manage.close(); + } + + + private static QueueSerializer createQueueSerializer() { + return new QueueSerializer() { + + @Override + public byte[] serialize(String item) { + return item.getBytes(); + } + + @Override + public String deserialize(byte[] bytes) { + return new String(bytes); + } + + }; + } + + private static QueueConsumer createQueueConsumer() { + + return new QueueConsumer() { + + @Override + public void consumeMessage(String message) throws Exception { + System.out.println("consume one message: " + message); + } + + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) { + System.out.println("connection new state: " + newState.name()); + } + }; + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java similarity index 72% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java rename to mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index 9e32dac7..894412ef 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -13,32 +13,22 @@ import org.junit.Before; import org.junit.Test; -public class DistributedQueueTest { +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.manage.ServerManage; - private ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "mpush"); - - private ZkUtil zkUtil = new ZkUtil(zkConfig); - - private static final String PATH = "/example/queue"; +public class DistributedQueueProviderTest { + + private ServerManage manage = new ServerManage(); @Before public void setup(){ - zkUtil.init(); - zkUtil.getClient().getCuratorListenable().addListener(new CuratorListener() { - @Override - public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { - System.out.println("CuratorEvent: " - + event.getType().name()); - } - }); } @Test public void test() throws Exception{ DistributedQueue queue = null; - QueueConsumer consumer = createQueueConsumer(); - QueueBuilder builder = QueueBuilder.builder(zkUtil.getClient(), - consumer, createQueueSerializer(), PATH); + QueueBuilder builder = QueueBuilder.builder(manage.getClient(), + null, createQueueSerializer(), PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(InetAddressUtil.getInetAddress())); queue = builder.buildQueue(); queue.start(); for (int i = 0; i < 10; i++) { @@ -53,7 +43,7 @@ public void test() throws Exception{ @After public void close(){ - zkUtil.close(); + manage.close(); } From 4ada367bc759204ff20c56f0377317bed535b01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 30 Dec 2015 12:09:19 +0000 Subject: [PATCH 057/890] add push callback --- .../com/shinemo/mpush/api/PushSender.java | 4 +- .../shinemo/mpush/client/PushCallback.java | 70 +++++++++++++++++++ .../shinemo/mpush/client/PushCallbackBus.java | 40 +++++++++++ .../com/shinemo/mpush/client/PushClient.java | 14 ++-- .../mpush/common/message/BaseMessage.java | 4 ++ .../mpush/common/message/ErrorMessage.java | 47 ++++++++----- .../mpush/common/message/OkMessage.java | 37 ++++++---- .../common/router/RemoteRouterManager.java | 1 + .../mpush/common/router/RouterCenter.java | 4 +- .../core/handler/GatewayPushHandler.java | 2 +- .../mpush/core/handler/HandShakeHandler.java | 12 ++-- .../mpush/core/netty/NettyClientTest.java | 3 - 12 files changed, 190 insertions(+), 48 deletions(-) create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java index 4fbe77a1..b4ec1bb9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java @@ -13,6 +13,8 @@ interface Callback { void onFailure(String userId); - void onLose(String userId); + void onOffline(String userId); + + void onTimeout(String userId); } } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java new file mode 100644 index 00000000..398f799b --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java @@ -0,0 +1,70 @@ +package com.shinemo.mpush.client; + +import com.shinemo.mpush.api.PushSender; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushCallback implements PushSender.Callback, Runnable { + private final PushSender.Callback callback; + private final String userId; + private int status = 0; + private long timeout; + + public PushCallback(PushSender.Callback callback, String userId, int timeout) { + this.callback = callback; + this.userId = userId; + this.timeout = timeout + System.currentTimeMillis(); + } + + @Override + public void onSuccess(String userId) { + submit(1); + } + + @Override + public void onFailure(String userId) { + submit(2); + } + + @Override + public void onOffline(String userId) { + submit(3); + } + + @Override + public void onTimeout(String userId) { + submit(4); + } + + private void submit(int status) { + this.status = status; + if (callback != null) { + PushCallbackBus.INSTANCE.getExecutor().execute(this); + } else { + + } + } + + @Override + public void run() { + switch (status) { + case 1: + callback.onSuccess(userId); + case 2: + callback.onFailure(userId); + case 3: + callback.onOffline(userId); + case 4: + callback.onTimeout(userId); + } + } + + public void timeout() { + onTimeout(userId); + } + + public boolean isTimeout() { + return System.currentTimeMillis() > timeout; + } +} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java new file mode 100644 index 00000000..4c1ae197 --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.client; + +import java.util.Map; +import java.util.concurrent.*; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushCallbackBus implements Runnable { + public static final PushCallbackBus INSTANCE = new PushCallbackBus(); + private Map callbacks = new ConcurrentHashMap<>(); + private Executor executor = Executors.newFixedThreadPool(5);//test + private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test + + public PushCallbackBus() { + scheduledExecutor.scheduleAtFixedRate(this, 1, 3, TimeUnit.SECONDS); + } + + public void register(int reqId, PushCallback callback) { + callbacks.put(reqId, callback); + } + + public PushCallback get(int reqId) { + return callbacks.get(reqId); + } + + public Executor getExecutor() { + return executor; + } + + @Override + public void run() { + if (callbacks.isEmpty()) return; + for (PushCallback callback : callbacks.values()) { + if (callback.isTimeout()) { + callback.timeout(); + } + } + } +} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 3e2f46e9..adfe7c1f 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -28,6 +28,7 @@ public class PushClient implements PushSender { private PacketReceiver receiver; private String host = "127.0.0.1"; private int port = 4000; + private int defaultTimeout = 3000; public void init() throws Exception { this.clientFactory = NettyClientFactory.INSTANCE; @@ -63,28 +64,29 @@ public void run() { public void send(String content, Collection userIds, final Callback callback) { RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); for (final String userId : userIds) { + final PushCallback cb = new PushCallback(callback, userId, defaultTimeout); RemoteRouter router = remoteRouterManager.lookup(userId); if (router == null) { - callback.onLose(userId); + cb.onOffline(userId); continue; } ClientLocation location = router.getRouteValue(); Connection connection = getConnection(location.getHost()); if (connection == null || !connection.isConnected()) { - callback.onFailure(userId); + cb.onFailure(userId); continue; } GatewayPushMessage pushMessage = new GatewayPushMessage(userId , "push content", connection); - + final int reqId = pushMessage.getSessionId(); pushMessage.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - callback.onFailure(userId); + if (future.isSuccess()) { + PushCallbackBus.INSTANCE.register(reqId, cb); } else { - callback.onSuccess(userId); + callback.onFailure(userId); } } }); diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 41c2af31..cb64b1a2 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -115,6 +115,10 @@ protected static int genSessionId() { return ID_SEQ.incrementAndGet(); } + public int getSessionId() { + return packet.sessionId; + } + @Override public String toString() { return "BaseMessage{" + diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index eec0bf21..ed833d18 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -4,20 +4,42 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2015/12/28. */ public final class ErrorMessage extends ByteBufMessage { + public byte cmd; + public byte code; public String reason; - public byte errorCode; + + public ErrorMessage(byte cmd, Packet message, Connection connection) { + super(message, connection); + this.cmd = cmd; + } public ErrorMessage(Packet message, Connection connection) { super(message, connection); } + @Override + public void decode(ByteBuf body) { + cmd = decodeByte(body); + code = decodeByte(body); + reason = decodeString(body); + } + + @Override + public void encode(ByteBuf body) { + encodeByte(body, cmd); + encodeByte(body, code); + encodeString(body, reason); + } + public static ErrorMessage from(BaseMessage src) { - return new ErrorMessage(new Packet(Command.ERROR.cmd, src.packet.sessionId), src.connection); + return new ErrorMessage(new Packet(Command.ERROR.cmd + , src.packet.sessionId), src.connection); } public ErrorMessage setReason(String reason) { @@ -25,33 +47,26 @@ public ErrorMessage setReason(String reason) { return this; } - public ErrorMessage setErrorCode(byte errorCode) { - this.errorCode = errorCode; + public ErrorMessage setCode(byte code) { + this.code = code; return this; } @Override - public void decode(ByteBuf body) { - reason = decodeString(body); - errorCode = decodeByte(body); - } - - @Override - public void encode(ByteBuf body) { - encodeString(body, reason); - encodeByte(body, errorCode); + public void send() { + super.sendRaw(); } @Override - public void send() { - super.sendRaw(); + public void close() { + sendRaw(ChannelFutureListener.CLOSE); } @Override public String toString() { return "ErrorMessage{" + "reason='" + reason + '\'' + - ", errorCode=" + errorCode + + ", code=" + code + ", packet=" + packet + '}'; } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java index 5cd66105..7082302c 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java @@ -1,36 +1,47 @@ package com.shinemo.mpush.common.message; -import com.google.common.base.Strings; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; /** * Created by ohun on 2015/12/28. */ -public final class OkMessage extends BaseMessage { +public final class OkMessage extends ByteBufMessage { + public byte cmd; + public byte code; public String data; + public OkMessage(byte cmd, Packet message, Connection connection) { + super(message, connection); + this.cmd = cmd; + } + public OkMessage(Packet message, Connection connection) { super(message, connection); } @Override - public void decode(byte[] body) { - if (body != null && body.length > 0) { - data = new String(body, Constants.UTF_8); - } + public void decode(ByteBuf body) { + cmd = decodeByte(body); + code = decodeByte(body); + data = decodeString(body); } @Override - public byte[] encode() { - return Strings.isNullOrEmpty(data) - ? null : - data.getBytes(Constants.UTF_8); + public void encode(ByteBuf body) { + encodeByte(body, cmd); + encodeByte(body, code); + encodeString(body, data); } - public static OkMessage from(BaseMessage message) { - return new OkMessage(message.createResponse(), message.connection); + public static OkMessage from(BaseMessage src) { + return new OkMessage(src.packet.cmd, src.createResponse(), src.connection); + } + + public OkMessage setCode(byte code) { + this.code = code; + return this; } public OkMessage setData(String data) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 8a1fe5e2..efb37e6b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -6,6 +6,7 @@ * Created by ohun on 2015/12/23. */ public class RemoteRouterManager implements RouterManager { + // TODO: 2015/12/30 add local cache @Override public RemoteRouter register(String userId, RemoteRouter route) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java index bbe45cc9..72ad40b5 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java @@ -53,7 +53,7 @@ public Router lookup(String userId) { return remote; } - private void kickLocalUser(String userId, LocalRouter router) { + public void kickLocalUser(String userId, LocalRouter router) { Connection connection = router.getRouteValue(); SessionContext context = connection.getSessionContext(); KickUserMessage message = new KickUserMessage(connection); @@ -62,7 +62,7 @@ private void kickLocalUser(String userId, LocalRouter router) { message.send(); } - private void kickRemoteUser(String userId, RemoteRouter router) { + public void kickRemoteUser(String userId, RemoteRouter router) { //send msg to zk } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index f3bb0d52..a4b63efc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -35,7 +35,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } else { ErrorMessage .from(message) - .setErrorCode((byte) 1) + .setCode((byte) 1) .setReason("push to client error") .send(); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java index 9c43b634..3e85e5f4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java @@ -7,7 +7,7 @@ import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.HandShakeMessage; +import com.shinemo.mpush.common.message.HandshakeMessage; import com.shinemo.mpush.common.message.HandshakeOkMessage; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.handler.BaseMessageHandler; @@ -22,16 +22,16 @@ /** * Created by ohun on 2015/12/24. */ -public final class HandShakeHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); +public final class HandshakeHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); @Override - public HandShakeMessage decode(Packet packet, Connection connection) { - return new HandShakeMessage(packet, connection); + public HandshakeMessage decode(Packet packet, Connection connection) { + return new HandshakeMessage(packet, connection); } @Override - public void handle(HandShakeMessage message) { + public void handle(HandshakeMessage message) { byte[] iv = message.iv;//AES密钥向量16 byte[] clientKey = message.clientKey;//客户端随机数 byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数 diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 2a6d8cbd..f0ab0873 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -23,11 +23,8 @@ public class NettyClientTest { @Test public void testClient() throws Exception { Client client = NettyClientFactory.INSTANCE.get(host, port, handler); - client.init(); client.start(); - - LOGGER.error(ToStringBuilder.reflectionToString(client, ToStringStyle.MULTI_LINE_STYLE)); } } From e9e6894fceb0dc4c6a21a2f5f781d5115e19e5a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 30 Dec 2015 21:18:20 +0800 Subject: [PATCH 058/890] add distribute queue test --- .../com/shinemo/mpush/tools/zk/manage/ServerManage.java | 1 - .../mpush/tools/zk/DistributedQueueConsumerTest.java | 7 ++++++- .../mpush/tools/zk/DistributedQueueProviderTest.java | 2 +- .../test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 35f40d61..6d960a9b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -77,6 +77,5 @@ public void close(){ zkUtil.close(); } - } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index 3f4cc90d..7788820d 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -24,8 +24,13 @@ public void setup(){ @Test public void test() throws Exception{ + DistributedQueue queue = null; + QueueBuilder builder = QueueBuilder.builder(manage.getClient(), + createQueueConsumer(), createQueueSerializer(), PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress())); + queue = builder.buildQueue(); + queue.start(); - QueueConsumer consumer = createQueueConsumer(); + CloseableUtils.closeQuietly(queue); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index 894412ef..c992baee 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -28,7 +28,7 @@ public void setup(){ public void test() throws Exception{ DistributedQueue queue = null; QueueBuilder builder = QueueBuilder.builder(manage.getClient(), - null, createQueueSerializer(), PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(InetAddressUtil.getInetAddress())); + null, createQueueSerializer(), PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress())); queue = builder.buildQueue(); queue.start(); for (int i = 0; i < 10; i++) { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 89509fd9..a2983025 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -104,7 +104,7 @@ public void testRegisterIp(){ @Test public void testRemove(){ - zkUtil.remove("/"); + zkUtil.remove("/example/queue"); } @Test From ca350854ab69391632c8e53c38e34a61f00cab8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 30 Dec 2015 21:20:07 +0800 Subject: [PATCH 059/890] bug fix for zk --- .../java/com/shinemo/mpush/tools/zk/manage/ServerManage.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 6d960a9b..4fa09c72 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -28,10 +28,13 @@ public class ServerManage { zkUtil.init(); } - // 注册机器到zk中 + public void start(String ip) { + //注册机器到zk中 zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + // 注册连接状态监听器 registerConnectionLostListener(); + // 注册节点数据变化 registerDataChange(ListenerDispatcher.instance); } From 60f77bea361b8faad061a8f681246db87fd08241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 30 Dec 2015 12:12:10 +0000 Subject: [PATCH 060/890] add push callback --- .../java/com/shinemo/mpush/core/handler/GatewayPushHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index a4b63efc..f06ec490 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -15,7 +15,7 @@ /** * Created by ohun on 2015/12/30. */ -public class GatewayPushHandler extends BaseMessageHandler { +public final class GatewayPushHandler extends BaseMessageHandler { @Override public GatewayPushMessage decode(Packet packet, Connection connection) { return new GatewayPushMessage(packet, connection); From d24c6a2bf0520c097f797a49ee9ecfda09820132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 30 Dec 2015 13:53:11 +0000 Subject: [PATCH 061/890] add push callback --- .../shinemo/mpush/api/protocol/Command.java | 21 ++++++++----- .../com/shinemo/mpush/client/PushClient.java | 2 +- .../com/shinemo/mpush/common/ErrorCode.java | 17 +++++++++++ .../mpush/common/message/ErrorMessage.java | 6 ++-- .../common/router/RemoteRouterManager.java | 30 +++++++++++++++++-- .../mpush/core/client/GatewayClient.java | 8 +++++ .../core/handler/GatewayPushHandler.java | 25 ++++++++++++---- .../mpush/core/handler/HeartBeatHandler.java | 2 +- .../mpush/core/server/ConnectionServer.java | 2 +- .../mpush/core/server/GatewayServer.java | 2 +- .../{ => server}/ServerChannelHandler.java | 2 +- .../mpush/core/netty/NettyServerTest.java | 10 +++---- .../mpush/core/security/CipherBoxTest.java | 1 - .../connection/NettyConnectionManager.java | 2 +- .../com/shinemo/mpush/tools/zk/PathEnum.java | 2 +- 15 files changed, 100 insertions(+), 32 deletions(-) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java rename mpush-core/src/main/java/com/shinemo/mpush/core/{ => server}/ServerChannelHandler.java (98%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index b7673ce7..0775ad97 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -10,13 +10,20 @@ public enum Command { LOGOUT(4), BIND(5), UNBIND(6), - KICK(7), - FAST_CONNECT(8), - ERROR(9), - OK(10), - PUSH(11), - API(12), - GATEWAY_PUSH(13), + FAST_CONNECT(7), + ERROR(8), + OK(9), + API(10), + KICK(11), + GATEWAY_KICK(12), + PUSH(13), + GATEWAY_PUSH(14), + NOTIFICATION(15), + GATEWAY_NOTIFICATION(16), + CHAT(17), + GATEWAY_CHAT(18), + GROUP(19), + GATEWAY_GROUP(20), UNKNOWN(-1); Command(int cmd) { diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index adfe7c1f..9174af3b 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -4,12 +4,12 @@ import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.router.ClientLocation; import com.shinemo.mpush.common.MessageDispatcher; import com.shinemo.mpush.common.handler.ErrorMessageHandler; import com.shinemo.mpush.common.handler.OkMessageHandler; +import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; import com.shinemo.mpush.common.router.RouterCenter; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java new file mode 100644 index 00000000..5d9acfde --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.common; + +/** + * Created by ohun on 2015/12/30. + */ +public enum ErrorCode { + OFFLINE(1, "user offline"), + PUSH_CLIENT_FAILURE(2, "push to client failure"),; + + ErrorCode(int code, String errorMsg) { + this.errorMsg = errorMsg; + this.errorCode = (byte) code; + } + + public final byte errorCode; + public final String errorMsg; +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index ed833d18..fb161b59 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -3,6 +3,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.ErrorCode; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; @@ -47,8 +48,9 @@ public ErrorMessage setReason(String reason) { return this; } - public ErrorMessage setCode(byte code) { - this.code = code; + public ErrorMessage setErrorCode(ErrorCode code) { + this.code = code.errorCode; + this.reason = code.errorMsg; return this; } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index efb37e6b..5261745e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -1,25 +1,49 @@ package com.shinemo.mpush.common.router; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.shinemo.mpush.api.router.RouterManager; +import java.util.concurrent.TimeUnit; + /** * Created by ohun on 2015/12/23. */ public class RemoteRouterManager implements RouterManager { - // TODO: 2015/12/30 add local cache + // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 + private final Cache cache = CacheBuilder + .newBuilder() + .expireAfterWrite(5, TimeUnit.MINUTES) + .expireAfterAccess(5, TimeUnit.MINUTES) + .build(); + @Override public RemoteRouter register(String userId, RemoteRouter route) { - return null; + RemoteRouter old = cache.getIfPresent(userId); + cache.put(userId, route); + return old; } @Override public boolean unRegister(String userId) { + cache.invalidate(userId); return true; } @Override public RemoteRouter lookup(String userId) { - return null; + return cache.getIfPresent(userId); + } + + /** + * 如果推送失败,可能是缓存不一致了,可以让本地缓存失效 + *

+ * 失效对应的本地缓存 + * + * @param userId + */ + public void invalidateLocalCache(String userId) { + cache.invalidate(userId); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java new file mode 100644 index 00000000..f9fc8a85 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java @@ -0,0 +1,8 @@ +package com.shinemo.mpush.core.client; + +/** + * Created by ohun on 2015/12/30. + */ +public class GatewayClient { + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index f06ec490..ecb73850 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.core.handler; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.ErrorCode; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.common.message.PushMessage; @@ -24,24 +25,36 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { @Override public void handle(final GatewayPushMessage message) { Router router = RouterCenter.INSTANCE.lookup(message.userId); - if (router.getRouteType() == Router.RouterType.LOCAL) { + if (router == null) { + //1.路由信息不存在说明用户此时不在线 + ErrorMessage + .from(message) + .setErrorCode(ErrorCode.OFFLINE) + .send(); + } else if (router.getRouteType() == Router.RouterType.LOCAL) { + //2.如果是本地路由信息,说明用户链接在当前机器,直接把消息下发到客户端 Connection connection = (Connection) router.getRouteValue(); PushMessage pushMessage = new PushMessage(message.content, connection); pushMessage.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - OkMessage.from(message).send(); - } else { + if (future.isSuccess()) {//推送成功 + OkMessage + .from(message) + .setData(message.userId) + .send(); + } else {//推送失败 ErrorMessage .from(message) - .setCode((byte) 1) - .setReason("push to client error") + .setErrorCode(ErrorCode.PUSH_CLIENT_FAILURE) .send(); } } }); + } else { + //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 + // 需要通过GatewayClient或ZK把消息推送到另外一台机器上 // TODO: 2015/12/30 send message to other server } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 0827460e..2aafbab9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -7,7 +7,7 @@ /** * Created by ohun on 2015/12/22. */ -public final class HeartBeatHandler implements MessageHandler { +public final class HeartbeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { System.err.println("receive client heartbeat, time=" diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 624f1fbb..e9658c0d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2015/12/30. */ -public class ConnectionServer extends NettyServer { +public final class ConnectionServer extends NettyServer { public ConnectionServer(int port, ChannelHandler channelHandler) { super(port, channelHandler); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java index d2af5172..ba83879a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -6,7 +6,7 @@ /** * Created by ohun on 2015/12/30. */ -public class GatewayServer extends NettyServer { +public final class GatewayServer extends NettyServer { public GatewayServer(int port, ChannelHandler channelHandler) { super(port, channelHandler); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java similarity index 98% rename from mpush-core/src/main/java/com/shinemo/mpush/core/ServerChannelHandler.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 9ecea8c7..93c8c56d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core; +package com.shinemo.mpush.core.server; import com.shinemo.mpush.api.connection.ConnectionManager; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 03308c45..ea9f237f 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -2,9 +2,10 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.core.ServerChannelHandler; +import com.shinemo.mpush.core.server.ServerChannelHandler; import com.shinemo.mpush.core.handler.BindUserHandler; import com.shinemo.mpush.core.handler.HandshakeHandler; +import com.shinemo.mpush.core.handler.HeartbeatHandler; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; @@ -26,8 +27,8 @@ public void testStart() throws Exception { MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); - receiver.register(Command.HEARTBEAT, new BindUserHandler()); receiver.register(Command.BIND, new BindUserHandler()); + receiver.register(Command.HEARTBEAT, new HeartbeatHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); connectionManager.registerEventBus(); ChannelHandler handler = new ServerChannelHandler(connectionManager, receiver); @@ -37,10 +38,7 @@ public void testStart() throws Exception { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { - try { - server.stop(); - } catch (Exception e) { - } + server.stop(); } }); } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java index 3e52345f..f518f4ae 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java @@ -12,7 +12,6 @@ public class CipherBoxTest { @Test public void testGetPrivateKey() throws Exception { - CipherBox.INSTANCE.getPrivateKey(); } @Test diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 564c8f75..78ab35fa 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -21,7 +21,7 @@ /** * Created by ohun on 2015/12/22. */ -public class NettyConnectionManager implements ConnectionManager { +public final class NettyConnectionManager implements ConnectionManager { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectionManager.class); public void registerEventBus() { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java index 8aac5cd3..614a6f5f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -3,7 +3,7 @@ public enum PathEnum { - CONNECTION_SERVER_ALL_HOST("/cs/allhost/machine","连接服务器应用注册的路径"){ + CONNECTION_SERVER_ALL_HOST("/cs/hosts/machine","连接服务器应用注册的路径"){ @Override public String getPathByIp(String ip) { return getPath(); From 612c6fa1b9c7bd32fff02c5559d791d75d4bea3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 09:23:02 +0800 Subject: [PATCH 062/890] =?UTF-8?q?=E6=9B=B4=E6=96=B0zk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/tools/Constants.java | 4 ++-- .../zk/listener/impl/ConnectionPathListener.java | 4 ++++ .../tools/zk/listener/impl/KickPathListener.java | 12 ++++++++---- .../shinemo/mpush/tools/zk/manage/ServerManage.java | 4 ++-- .../java/com/shinemo/mpush/tools/zk/ZkUtilTest.java | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 86d4403a..64d9e8f4 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -29,10 +29,10 @@ public interface Constants { int ZK_CONNECTION_TIMEOUT = 5000; String ZK_DEFAULT_CACHE_PATH = "/"; String ZK_DEFAULT_DIGEST = "shinemo"; - + String ZK_IPS = "127.0.0.1:2181"; //zk cs - String ZK_NAME_SPACE = "/mpush"; + String ZK_NAME_SPACE = "mpush"; //所有机器启动的时候注册ip的地方 String ZK_REGISTER_HOST = "/allhost"; String ZK_REGISTER_PREFIX_NAME = "machine"; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 7bb8a64f..9492246a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -8,6 +8,10 @@ import com.shinemo.mpush.tools.zk.listener.CallBack; +/** + *注册的应用的发生变化 + * + */ public class ConnectionPathListener implements CallBack{ private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java index 18605def..2786a1a0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java @@ -8,6 +8,10 @@ import com.shinemo.mpush.tools.zk.listener.CallBack; +/** + * 当前应用下踢人的目录发生变化 + * + */ public class KickPathListener implements CallBack{ private static final Logger log = LoggerFactory.getLogger(KickPathListener.class); @@ -15,13 +19,13 @@ public class KickPathListener implements CallBack{ @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { if (Type.NODE_ADDED == event.getType()) { - log.warn("path:" + path + ", node Add"); + log.warn("path:" + path + ", node Add" +","+event.getData().getData()); } else if (Type.NODE_REMOVED == event.getType()) { - log.warn("path:" + path + ", node Remove"); + log.warn("path:" + path + ", node Remove"+","+event.getData().getData()); } else if (Type.NODE_UPDATED == event.getType()) { - log.warn("path:" + path + "," + "node update"); + log.warn("path:" + path + "," + "node update"+","+event.getData().getData()); } else { - log.warn("path:" + path + "," + event.getType().name()); + log.warn("path:" + path + "," + event.getType().name()+","+event.getData().getData()); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 4fa09c72..f511871f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -9,6 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ZkConfig; @@ -20,7 +21,7 @@ public class ServerManage { private static final Logger log = LoggerFactory.getLogger(ServerManage.class); - private static final ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "mpush"); + private static final ZkConfig zkConfig = new ZkConfig(Constants.ZK_IPS, Constants.ZK_NAME_SPACE); private static final ZkUtil zkUtil = new ZkUtil(zkConfig); @@ -79,6 +80,5 @@ public TreeCache getCache() { public void close(){ zkUtil.close(); } - } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index a2983025..eabb16c3 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -55,7 +55,7 @@ public void getTest(){ @Test public void testRegister(){ - String path = Constants.ZK_NAME_SPACE; + String path = "/"+Constants.ZK_NAME_SPACE; String prefix = Constants.ZK_REGISTER_PREFIX_NAME; From 57d2447d7f8e58e64e0c2d3042b48763f65dada3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 09:55:00 +0800 Subject: [PATCH 063/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0zk=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/tools/zk/ZkUtil.java | 14 +++++-- .../mpush/tools/zk/listener/CallBack.java | 14 +++++++ .../tools/zk/listener/ListenerDispatcher.java | 42 +++++++++++++------ .../listener/impl/ConnectionPathListener.java | 26 ++++++++++++ .../zk/listener/impl/KickPathListener.java | 6 +++ .../mpush/tools/zk/manage/ServerManage.java | 29 +++++++------ .../shinemo/mpush/tools/zk/ZkUtilTest.java | 10 +---- 7 files changed, 104 insertions(+), 37 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java index 1c971136..8d7f83bd 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -22,20 +22,28 @@ import org.slf4j.LoggerFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; +import com.shinemo.mpush.tools.Constants; + public class ZkUtil { private static final Logger log = LoggerFactory.getLogger(ZkUtil.class); + + private static final ZkConfig config = new ZkConfig(Constants.ZK_IPS, Constants.ZK_NAME_SPACE); + public static final ZkUtil instance = new ZkUtil(config); + + static { + instance.init(); + } + private ZkConfig zkConfig; private CuratorFramework client; private TreeCache cache; - public ZkUtil(ZkConfig zkConfig) { + private ZkUtil(ZkConfig zkConfig) { this.zkConfig = zkConfig; } - - public ZkConfig getZkConfig() { return zkConfig; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java index df2721dd..74d8c8b6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java @@ -3,8 +3,22 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + public interface CallBack { + /** + * 处理目录发生变化的事件 + * @param client + * @param event + * @param path + */ public void handler(CuratorFramework client, TreeCacheEvent event,String path); + /** + * 应用起来的时候初始化数据 + * @param manage + */ + public void initData(ServerManage manage); + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index 9fe13f1b..c06818a6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -13,33 +13,49 @@ import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; import com.shinemo.mpush.tools.zk.listener.impl.KickPathListener; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +public class ListenerDispatcher implements CallBack { -public class ListenerDispatcher implements CallBack{ - private static final Logger log = LoggerFactory.getLogger(ListenerDispatcher.class); - - private Map holder = Maps.newTreeMap(); - + + private Map holder = Maps.newTreeMap(); + public static ListenerDispatcher instance = new ListenerDispatcher(); - + private ListenerDispatcher() { holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath(), new ConnectionPathListener()); holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress()), new KickPathListener()); } - + @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - + Iterator> it = holder.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = it.next(); - if (path.startsWith(entry.getKey())) { - entry.getValue().handler(client, event, path); + while (it.hasNext()) { + Map.Entry entry = it.next(); + if (path.startsWith(entry.getKey())) { + entry.getValue().handler(client, event, path); } else { // 其他路径的事件,暂时不关心 log.warn("path:" + path + "," + event.getType().name()); } - } + } + + } + + @Override + public void initData(ServerManage manage) { + + Iterator> it = holder.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + entry.getValue().initData(manage); + } } + + public CallBack getListener(PathEnum pathEnum,String ip){ + return holder.get(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(ip)); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 9492246a..07e4bb87 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -1,12 +1,19 @@ package com.shinemo.mpush.tools.zk.listener.impl; +import java.util.ArrayList; +import java.util.List; + import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.listener.CallBack; +import com.shinemo.mpush.tools.zk.manage.ServerManage; /** *注册的应用的发生变化 @@ -16,6 +23,8 @@ public class ConnectionPathListener implements CallBack{ private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class); + private List appList = new ArrayList(); + @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { if (Type.NODE_ADDED == event.getType()) { @@ -29,4 +38,21 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) } } + @Override + public void initData(ServerManage manage) { + log.warn("start init data"); + List rawData = manage.getZkUtil().getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + List newAppList = new ArrayList(); + for(String raw:rawData){ + ServerApp app = Jsons.fromJson(raw, ServerApp.class); + newAppList.add(app); + } + appList = newAppList; + log.warn("end init data"); + } + + public List getAppList(){ + return appList; + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java index 2786a1a0..0418bfcc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.zk.listener.CallBack; +import com.shinemo.mpush.tools.zk.manage.ServerManage; /** * 当前应用下踢人的目录发生变化 @@ -29,4 +30,9 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) } } + @Override + public void initData(ServerManage manage) { + + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index f511871f..3a25ffb9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -9,10 +9,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.zk.PathEnum; -import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; @@ -21,22 +19,21 @@ public class ServerManage { private static final Logger log = LoggerFactory.getLogger(ServerManage.class); - private static final ZkConfig zkConfig = new ZkConfig(Constants.ZK_IPS, Constants.ZK_NAME_SPACE); - - private static final ZkUtil zkUtil = new ZkUtil(zkConfig); - - static { - zkUtil.init(); - } - - + private static ZkUtil zkUtil = ZkUtil.instance; + public void start(String ip) { //注册机器到zk中 - zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + registerApp(); // 注册连接状态监听器 registerConnectionLostListener(); // 注册节点数据变化 registerDataChange(ListenerDispatcher.instance); + //获取应用起来的时候的初始化数据 + initAppData(ListenerDispatcher.instance); + } + + private void registerApp(){ + zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); } // 注册连接状态监听器 @@ -69,6 +66,10 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc }); } + private void initAppData(final CallBack callBack){ + callBack.initData(this); + } + public CuratorFramework getClient() { return zkUtil.getClient(); } @@ -80,5 +81,9 @@ public TreeCache getCache() { public void close(){ zkUtil.close(); } + + public ZkUtil getZkUtil(){ + return zkUtil; + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index eabb16c3..b45ff690 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -4,7 +4,6 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; -import org.junit.Before; import org.junit.Test; import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.InetAddressUtil; @@ -12,14 +11,7 @@ public class ZkUtilTest { - private ZkConfig zkConfig = new ZkConfig("127.0.0.1:2181", "mpush"); - - private ZkUtil zkUtil = new ZkUtil(zkConfig); - - @Before - public void setup(){ - zkUtil.init(); - } + private ZkUtil zkUtil = ZkUtil.instance; @Test public void test(){ From 0f6c060084a95df9e40ede0e2e4ef4304747118f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 10:13:59 +0800 Subject: [PATCH 064/890] remove ip --- .../shinemo/mpush/tools/zk/manage/ServerManage.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 3a25ffb9..fd234770 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.tools.zk.manage; +import java.util.concurrent.atomic.AtomicBoolean; + import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; @@ -21,7 +23,14 @@ public class ServerManage { private static ZkUtil zkUtil = ZkUtil.instance; - public void start(String ip) { + private final AtomicBoolean startFlag = new AtomicBoolean(false); + + public void start() { + + if (!startFlag.compareAndSet(false, true)) { + return; + } + //注册机器到zk中 registerApp(); // 注册连接状态监听器 @@ -30,6 +39,7 @@ public void start(String ip) { registerDataChange(ListenerDispatcher.instance); //获取应用起来的时候的初始化数据 initAppData(ListenerDispatcher.instance); + } private void registerApp(){ From 038bd3d6e53d4d30e23cef94c62c642a10c7c5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 10:27:49 +0800 Subject: [PATCH 065/890] change name for compile --- .../{HandShakeMessage.java => HandshakeMessage.java} | 0 .../{HandShakeHandler.java => HandshakeHandler.java} | 0 .../com/shinemo/mpush/core/handler/HeartBeatHandler.java | 2 +- .../java/com/shinemo/mpush/core/netty/NettyServerTest.java | 6 ++++-- .../java/com/shinemo/mpush/tools/zk/ServerManageTest.java | 4 ++-- 5 files changed, 7 insertions(+), 5 deletions(-) rename mpush-common/src/main/java/com/shinemo/mpush/common/message/{HandShakeMessage.java => HandshakeMessage.java} (100%) rename mpush-core/src/main/java/com/shinemo/mpush/core/handler/{HandShakeHandler.java => HandshakeHandler.java} (100%) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java similarity index 100% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/HandShakeMessage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java similarity index 100% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 2aafbab9..0827460e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -7,7 +7,7 @@ /** * Created by ohun on 2015/12/22. */ -public final class HeartbeatHandler implements MessageHandler { +public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { System.err.println("receive client heartbeat, time=" diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index ea9f237f..63fe0b8b 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -5,11 +5,13 @@ import com.shinemo.mpush.core.server.ServerChannelHandler; import com.shinemo.mpush.core.handler.BindUserHandler; import com.shinemo.mpush.core.handler.HandshakeHandler; -import com.shinemo.mpush.core.handler.HeartbeatHandler; +import com.shinemo.mpush.core.handler.HeartBeatHandler; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; + import io.netty.channel.ChannelHandler; + import org.junit.Test; /** @@ -28,7 +30,7 @@ public void testStart() throws Exception { MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.BIND, new BindUserHandler()); - receiver.register(Command.HEARTBEAT, new HeartbeatHandler()); + receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); connectionManager.registerEventBus(); ChannelHandler handler = new ServerChannelHandler(connectionManager, receiver); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 40b3f3a4..12475215 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -29,7 +29,7 @@ public void testMulThread() throws InterruptedException{ @Test public void testUpdate(){ ServerManage manage = new ServerManage(); - manage.start("192.168.1.1"); + manage.start(); } @@ -55,7 +55,7 @@ public void run() { } log.warn("start init "+ip); ServerManage manage = new ServerManage(); - manage.start(ip); + manage.start(); log.warn("end init "+ip); } From 5f2397fab6b70fad88ba32c92b41c340ac20b840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 10:31:26 +0800 Subject: [PATCH 066/890] =?UTF-8?q?=E9=87=8D=E6=9E=84zk=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/tools/zk/manage/ServerManage.java | 6 +++++- .../mpush/tools/zk/DistributedQueueConsumerTest.java | 2 +- .../mpush/tools/zk/DistributedQueueProviderTest.java | 2 +- .../com/shinemo/mpush/tools/zk/ServerManageTest.java | 10 ++++++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index fd234770..6f6fd49b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -23,7 +23,11 @@ public class ServerManage { private static ZkUtil zkUtil = ZkUtil.instance; - private final AtomicBoolean startFlag = new AtomicBoolean(false); + private static final AtomicBoolean startFlag = new AtomicBoolean(false); + + public static ServerManage instance = new ServerManage(); + + private ServerManage(){} public void start() { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index 7788820d..ae78a015 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -16,7 +16,7 @@ public class DistributedQueueConsumerTest { - private ServerManage manage = new ServerManage(); + private ServerManage manage = ServerManage.instance; @Before public void setup(){ diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index c992baee..55da5a16 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -18,7 +18,7 @@ public class DistributedQueueProviderTest { - private ServerManage manage = new ServerManage(); + private ServerManage manage = ServerManage.instance; @Before public void setup(){ diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 12475215..4b651769 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -28,11 +28,17 @@ public void testMulThread() throws InterruptedException{ @Test public void testUpdate(){ - ServerManage manage = new ServerManage(); + ServerManage manage = ServerManage.instance; manage.start(); } + @Test + public void testServerManageStart(){ + ServerManage manage = ServerManage.instance; + manage.start(); + } + private static class Worker implements Runnable{ @@ -54,7 +60,7 @@ public void run() { e.printStackTrace(); } log.warn("start init "+ip); - ServerManage manage = new ServerManage(); + ServerManage manage = ServerManage.instance; manage.start(); log.warn("end init "+ip); } From dcb83c0ec8eb6eab58c27b83658c9986506a2a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 10:37:20 +0800 Subject: [PATCH 067/890] change zk path --- .../src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java | 4 ++-- .../shinemo/mpush/tools/zk/listener/ListenerDispatcher.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java index 614a6f5f..ff300a79 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -3,10 +3,10 @@ public enum PathEnum { - CONNECTION_SERVER_ALL_HOST("/cs/hosts/machine","连接服务器应用注册的路径"){ + CONNECTION_SERVER_ALL_HOST("/cs/hosts","连接服务器应用注册的路径"){ @Override public String getPathByIp(String ip) { - return getPath(); + return getPath()+"/machine"; } }, CONNECTION_SERVER_KICK("/cs/%s/kick/con","连接服务器踢人的路径"){ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index c06818a6..cb1492de 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -24,7 +24,7 @@ public class ListenerDispatcher implements CallBack { public static ListenerDispatcher instance = new ListenerDispatcher(); private ListenerDispatcher() { - holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath(), new ConnectionPathListener()); + holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(InetAddressUtil.getInetAddress()), new ConnectionPathListener()); holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress()), new KickPathListener()); } @@ -55,7 +55,7 @@ public void initData(ServerManage manage) { } public CallBack getListener(PathEnum pathEnum,String ip){ - return holder.get(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(ip)); + return holder.get(pathEnum.getPathByIp(ip)); } } From 23f42ceb9ef5dab7ccd1d58a44b134f6f5952790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 11:00:00 +0800 Subject: [PATCH 068/890] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E9=87=8D=E6=9E=84zk?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/tools/zk/ServerApp.java | 16 ++++----- .../com/shinemo/mpush/tools/zk/ZkUtil.java | 12 +++++++ .../tools/zk/listener/ListenerDispatcher.java | 10 +++--- .../mpush/tools/zk/manage/ServerManage.java | 34 ++++++++++++------- .../zk/DistributedQueueConsumerTest.java | 3 +- .../zk/DistributedQueueProviderTest.java | 3 +- .../mpush/tools/zk/ServerManageTest.java | 10 ++++-- .../shinemo/mpush/tools/zk/ZkUtilTest.java | 4 +-- 8 files changed, 57 insertions(+), 35 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java index da784a3d..067eba98 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java @@ -6,19 +6,19 @@ public class ServerApp implements Serializable{ private static final long serialVersionUID = 5495972321679092837L; - private String ip; - private String port; + private final String ip; + private final String port; + + public ServerApp(String ip, String port) { + this.ip = ip; + this.port = port; + } + public String getIp() { return ip; } - public void setIp(String ip) { - this.ip = ip; - } public String getPort() { return port; } - public void setPort(String port) { - this.port = port; - } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java index 8d7f83bd..a2eaf8ad 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -227,6 +227,18 @@ public void registerEphemeral(final String key, final String value) { } } + /** + * 注册临时顺序数据 + * @param key + */ + public void registerEphemeralSequential(final String key,final String value) { + try { + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); + } catch (final Exception ex) { + log.error("persistEphemeralSequential" + key,ex); + } + } + /** * 注册临时顺序数据 * @param key diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index cb1492de..c7025497 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -9,8 +9,8 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Maps; -import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; import com.shinemo.mpush.tools.zk.listener.impl.KickPathListener; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -21,11 +21,9 @@ public class ListenerDispatcher implements CallBack { private Map holder = Maps.newTreeMap(); - public static ListenerDispatcher instance = new ListenerDispatcher(); - - private ListenerDispatcher() { - holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(InetAddressUtil.getInetAddress()), new ConnectionPathListener()); - holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress()), new KickPathListener()); + public ListenerDispatcher(ServerApp app) { + holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()), new ConnectionPathListener()); + holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), new KickPathListener()); } @Override diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 6f6fd49b..6d83dc0a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -11,8 +11,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; @@ -25,41 +26,48 @@ public class ServerManage { private static final AtomicBoolean startFlag = new AtomicBoolean(false); - public static ServerManage instance = new ServerManage(); + private final ServerApp app; + + public ServerManage(ServerApp app){ + this.app = app; + } - private ServerManage(){} - public void start() { if (!startFlag.compareAndSet(false, true)) { return; } + + ListenerDispatcher dispatcher = new ListenerDispatcher(app); //注册机器到zk中 - registerApp(); + registerApp(app); + // 注册连接状态监听器 - registerConnectionLostListener(); + registerConnectionLostListener(app); + // 注册节点数据变化 - registerDataChange(ListenerDispatcher.instance); + registerDataChange(dispatcher); + //获取应用起来的时候的初始化数据 - initAppData(ListenerDispatcher.instance); + initAppData(dispatcher); } - private void registerApp(){ - zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + private void registerApp(ServerApp app){ + zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()),Jsons.toJson(app)); } // 注册连接状态监听器 - private void registerConnectionLostListener() { + private void registerConnectionLostListener(final ServerApp app) { zkUtil.getClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { if (ConnectionState.LOST == newState) { - log.warn(InetAddressUtil.getInetAddress() + ", lost connection"); + log.warn(app.getIp() + ", lost connection"); } else if (ConnectionState.RECONNECTED == newState) { - log.warn(InetAddressUtil.getInetAddress() + ", reconnected"); + log.warn(app.getIp() + ", reconnected"); } } }); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index ae78a015..a6fd9f55 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -16,7 +16,8 @@ public class DistributedQueueConsumerTest { - private ServerManage manage = ServerManage.instance; + private ServerApp app = new ServerApp("127.0.0.1","3000"); + private ServerManage manage = new ServerManage(app); @Before public void setup(){ diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index 55da5a16..2e45645f 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -18,7 +18,8 @@ public class DistributedQueueProviderTest { - private ServerManage manage = ServerManage.instance; + private ServerApp app = new ServerApp("127.0.0.1","3000"); + private ServerManage manage = new ServerManage(app); @Before public void setup(){ diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 4b651769..fd16d024 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -14,6 +14,10 @@ public class ServerManageTest { private static Executor executor = Executors.newCachedThreadPool(); + private ServerApp app = new ServerApp("127.0.0.1","3000"); + + private ServerManage manage = new ServerManage(app); + @Test public void testMulThread() throws InterruptedException{ CountDownLatch latch = new CountDownLatch(1); @@ -28,14 +32,13 @@ public void testMulThread() throws InterruptedException{ @Test public void testUpdate(){ - ServerManage manage = ServerManage.instance; + manage.start(); } @Test public void testServerManageStart(){ - ServerManage manage = ServerManage.instance; manage.start(); } @@ -60,7 +63,8 @@ public void run() { e.printStackTrace(); } log.warn("start init "+ip); - ServerManage manage = ServerManage.instance; + ServerApp app = new ServerApp(ip,"3000"); + ServerManage manage = new ServerManage(app); manage.start(); log.warn("end init "+ip); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index b45ff690..671eb0ab 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -86,9 +86,7 @@ public void testLocalIp(){ @Test public void testRegisterIp(){ String localIp = InetAddressUtil.getInetAddress(); - ServerApp app = new ServerApp(); - app.setIp(localIp); - app.setPort("3000"); + ServerApp app = new ServerApp(localIp,"3000"); zkUtil.registerPersist("/"+localIp, Jsons.toJson(app)); String value = zkUtil.get("/"+localIp); System.out.println(value); From 54646a725cb82ae5656720e3156f29cd12c31b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 11:56:18 +0800 Subject: [PATCH 069/890] zk multhread test update --- .../com/shinemo/mpush/tools/zk/PathEnum.java | 12 +++++++ .../tools/zk/listener/ListenerDispatcher.java | 2 +- .../listener/impl/ConnectionPathListener.java | 32 ++++++++++++++----- .../zk/listener/impl/KickPathListener.java | 9 +++--- .../mpush/tools/zk/manage/ServerManage.java | 15 ++++++--- .../mpush/tools/zk/ServerManageTest.java | 22 +++++++------ 6 files changed, 64 insertions(+), 28 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java index ff300a79..3a1dfceb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -8,12 +8,20 @@ public enum PathEnum { public String getPathByIp(String ip) { return getPath()+"/machine"; } + @Override + public String getPathByName(String name) { + return getPath()+"/"+name; + } }, CONNECTION_SERVER_KICK("/cs/%s/kick/con","连接服务器踢人的路径"){ @Override public String getPathByIp(String ip) { return String.format(getPath(), ip); } + @Override + public String getPathByName(String name) { + return getPath()+"/"+name; + } }; PathEnum(String path, String desc) { @@ -32,7 +40,11 @@ public String getDesc() { return desc; } + //不同的机器,注册到不同的路径 public abstract String getPathByIp(String ip); + + //根据从zk中获取的app的值,拼装全路径 + public abstract String getPathByName(String name); public static void main(String[] args) { String test = "/cs/%s/kick"; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index c7025497..824b4ddf 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -35,7 +35,7 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) if (path.startsWith(entry.getKey())) { entry.getValue().handler(client, event, path); } else { // 其他路径的事件,暂时不关心 - log.warn("path:" + path + "," + event.getType().name()); + log.warn("ListenerDispatcher other path:" + path + "," + event.getType().name()); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 07e4bb87..2f3b6104 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; @@ -12,6 +14,7 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -27,32 +30,45 @@ public class ConnectionPathListener implements CallBack{ @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + String data = new String(event.getData().getData()); if (Type.NODE_ADDED == event.getType()) { - log.warn("path:" + path + ", node Add"); + log.warn("ConnectionPathListener path:" + path + ", node Add"+","+data); } else if (Type.NODE_REMOVED == event.getType()) { - log.warn("path:" + path + ", node Remove"); + log.warn("ConnectionPathListener path:" + path + ", node Remove"+","+data); } else if (Type.NODE_UPDATED == event.getType()) { - log.warn("path:" + path + "," + "node update"); + log.warn("ConnectionPathListener path:" + path + "," + "node update"+","+data); } else { - log.warn("path:" + path + "," + event.getType().name()); + log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data); } } @Override public void initData(ServerManage manage) { - log.warn("start init data"); - List rawData = manage.getZkUtil().getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + log.warn("start init app data"); + getData(); + printAppList(); + log.warn("end init app data"); + } + + private void getData(){ + //获取机器列表 + List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); List newAppList = new ArrayList(); for(String raw:rawData){ - ServerApp app = Jsons.fromJson(raw, ServerApp.class); + String rawApp = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw)); + ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); newAppList.add(app); } appList = newAppList; - log.warn("end init data"); } public List getAppList(){ return appList; } + private void printAppList(){ + for(ServerApp app:appList){ + log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + } + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java index 0418bfcc..a1041a25 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java @@ -19,14 +19,15 @@ public class KickPathListener implements CallBack{ @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + String data = new String(event.getData().getData()); if (Type.NODE_ADDED == event.getType()) { - log.warn("path:" + path + ", node Add" +","+event.getData().getData()); + log.warn("path:" + path + ", node Add"+","+data); } else if (Type.NODE_REMOVED == event.getType()) { - log.warn("path:" + path + ", node Remove"+","+event.getData().getData()); + log.warn("path:" + path + ", node Remove"+","+data); } else if (Type.NODE_UPDATED == event.getType()) { - log.warn("path:" + path + "," + "node update"+","+event.getData().getData()); + log.warn("path:" + path + "," + "node update"+","+data); } else { - log.warn("path:" + path + "," + event.getType().name()+","+event.getData().getData()); + log.warn("other path:" + path + "," + event.getType().name()+","+data); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 6d83dc0a..571a3edf 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -24,7 +24,7 @@ public class ServerManage { private static ZkUtil zkUtil = ZkUtil.instance; - private static final AtomicBoolean startFlag = new AtomicBoolean(false); + private final AtomicBoolean startFlag = new AtomicBoolean(false); private final ServerApp app; @@ -41,10 +41,10 @@ public void start() { ListenerDispatcher dispatcher = new ListenerDispatcher(app); //注册机器到zk中 - registerApp(app); + registerApp(); // 注册连接状态监听器 - registerConnectionLostListener(app); + registerConnectionLostListener(); // 注册节点数据变化 registerDataChange(dispatcher); @@ -54,12 +54,16 @@ public void start() { } - private void registerApp(ServerApp app){ + private void registerApp(){ zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()),Jsons.toJson(app)); } + + public void unregisterApp(){ + zkUtil.remove(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp())); + } // 注册连接状态监听器 - private void registerConnectionLostListener(final ServerApp app) { + private void registerConnectionLostListener() { zkUtil.getClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override @@ -81,6 +85,7 @@ private void registerDataChange(final CallBack callBack) { public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { String path = null == event.getData() ? "" : event.getData().getPath(); if (path.isEmpty()) { + log.warn("registerDataChange empty path:" + path + "," + event.getType().name()); return; } callBack.handler(client, event, path); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index fd16d024..db638ea6 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -14,14 +14,14 @@ public class ServerManageTest { private static Executor executor = Executors.newCachedThreadPool(); - private ServerApp app = new ServerApp("127.0.0.1","3000"); + private ServerApp app = new ServerApp("10.1.10.65","3000"); private ServerManage manage = new ServerManage(app); @Test - public void testMulThread() throws InterruptedException{ + public void testMulThreadRegisterApp() throws InterruptedException{ CountDownLatch latch = new CountDownLatch(1); - for(int i = 1;i<=10;i++){ + for(int i = 1;i<=2;i++){ executor.execute(new Worker("192.168.1."+i, latch)); } latch.countDown(); @@ -30,13 +30,6 @@ public void testMulThread() throws InterruptedException{ } - @Test - public void testUpdate(){ - - manage.start(); - - } - @Test public void testServerManageStart(){ manage.start(); @@ -66,6 +59,15 @@ public void run() { ServerApp app = new ServerApp(ip,"3000"); ServerManage manage = new ServerManage(app); manage.start(); + + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + manage.close(); + log.warn("end init "+ip); } From c78ec029042c2a59c50b309a5ab47e10939869c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 11:59:52 +0800 Subject: [PATCH 070/890] change data log --- .../mpush/tools/zk/listener/ListenerDispatcher.java | 8 ++++++-- .../tools/zk/listener/impl/ConnectionPathListener.java | 5 ++++- .../mpush/tools/zk/listener/impl/KickPathListener.java | 7 ++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index 824b4ddf..37a2e6d7 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -30,14 +30,18 @@ public ListenerDispatcher(ServerApp app) { public void handler(CuratorFramework client, TreeCacheEvent event, String path) { Iterator> it = holder.entrySet().iterator(); + boolean hasHandler = false; while (it.hasNext()) { Map.Entry entry = it.next(); if (path.startsWith(entry.getKey())) { + hasHandler = true; entry.getValue().handler(client, event, path); - } else { // 其他路径的事件,暂时不关心 - log.warn("ListenerDispatcher other path:" + path + "," + event.getType().name()); } } + + if(!hasHandler){ + log.warn("ListenerDispatcher other path:" + path + "," + event.getType().name()); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 2f3b6104..cc5b57a3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -30,7 +30,10 @@ public class ConnectionPathListener implements CallBack{ @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - String data = new String(event.getData().getData()); + String data = ""; + if(event.getData()!=null){ + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } if (Type.NODE_ADDED == event.getType()) { log.warn("ConnectionPathListener path:" + path + ", node Add"+","+data); } else if (Type.NODE_REMOVED == event.getType()) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java index a1041a25..a2726242 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.tools.zk.listener.impl; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; @@ -19,7 +21,10 @@ public class KickPathListener implements CallBack{ @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - String data = new String(event.getData().getData()); + String data = ""; + if(event.getData()!=null){ + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } if (Type.NODE_ADDED == event.getType()) { log.warn("path:" + path + ", node Add"+","+data); } else if (Type.NODE_REMOVED == event.getType()) { From c5ed8f93364d4769ff8edd9c365619ea39f91ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 13:57:22 +0800 Subject: [PATCH 071/890] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9app?= =?UTF-8?q?=E5=86=85=E5=AD=98=E4=B8=AD=E7=9A=84=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/impl/ConnectionPathListener.java | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index cc5b57a3..866d977f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -1,16 +1,20 @@ package com.shinemo.mpush.tools.zk.listener.impl; -import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Maps; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; @@ -26,7 +30,7 @@ public class ConnectionPathListener implements CallBack{ private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class); - private List appList = new ArrayList(); + private static Map holder = Maps.newConcurrentMap(); @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { @@ -35,10 +39,13 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); } if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); log.warn("ConnectionPathListener path:" + path + ", node Add"+","+data); } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); log.warn("ConnectionPathListener path:" + path + ", node Remove"+","+data); } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); log.warn("ConnectionPathListener path:" + path + "," + "node update"+","+data); } else { log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data); @@ -56,21 +63,37 @@ public void initData(ServerManage manage) { private void getData(){ //获取机器列表 List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); - List newAppList = new ArrayList(); for(String raw:rawData){ - String rawApp = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw)); - ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); - newAppList.add(app); + String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw); + ServerApp app = getServerApp(raw); + holder.put(fullPath, app); } - appList = newAppList; } - public List getAppList(){ - return appList; + private void dataRemove(ChildData data){ + String path = data.getPath(); + holder.remove(path); + } + + private void dataAddOrUpdate(ChildData data){ + String path = data.getPath(); + byte[] rawData = data.getData(); + ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); + holder.put(path, serverApp); + } + + private ServerApp getServerApp(String fullPath){ + String rawApp = ZkUtil.instance.get(fullPath); + ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); + return app; + } + + public Collection getAppList(){ + return Collections.unmodifiableCollection(holder.values()); } private void printAppList(){ - for(ServerApp app:appList){ + for(ServerApp app:holder.values()){ log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); } } From 07fc60b52d608f393d0fa819931ccf38900d20f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 13:58:19 +0800 Subject: [PATCH 072/890] add print log --- .../mpush/tools/zk/listener/impl/ConnectionPathListener.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 866d977f..adc3d8bf 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -73,6 +73,7 @@ private void getData(){ private void dataRemove(ChildData data){ String path = data.getPath(); holder.remove(path); + printAppList(); } private void dataAddOrUpdate(ChildData data){ @@ -80,6 +81,7 @@ private void dataAddOrUpdate(ChildData data){ byte[] rawData = data.getData(); ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); holder.put(path, serverApp); + printAppList(); } private ServerApp getServerApp(String fullPath){ From c9df4d002653818d0eb8ae166fa8640fa10f1ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 16:10:08 +0800 Subject: [PATCH 073/890] add test --- .../mpush/tools/zk/listener/impl/ConnectionPathListener.java | 2 +- .../src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index adc3d8bf..9d099d5d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -65,7 +65,7 @@ private void getData(){ List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); for(String raw:rawData){ String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw); - ServerApp app = getServerApp(raw); + ServerApp app = getServerApp(fullPath); holder.put(fullPath, app); } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 671eb0ab..0ddc7a15 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -94,7 +94,7 @@ public void testRegisterIp(){ @Test public void testRemove(){ - zkUtil.remove("/example/queue"); + zkUtil.remove("/"); } @Test From bb33f2c32c56860c7070237580f2a1e84faa6a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 16:15:32 +0800 Subject: [PATCH 074/890] add test --- .../com/shinemo/mpush/tools/zk/manage/ServerManage.java | 8 +++++++- .../java/com/shinemo/mpush/tools/zk/ServerManageTest.java | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 571a3edf..76880885 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -28,6 +28,8 @@ public class ServerManage { private final ServerApp app; + private ListenerDispatcher dispatcher; + public ServerManage(ServerApp app){ this.app = app; } @@ -38,7 +40,7 @@ public void start() { return; } - ListenerDispatcher dispatcher = new ListenerDispatcher(app); + dispatcher = new ListenerDispatcher(app); //注册机器到zk中 registerApp(); @@ -112,5 +114,9 @@ public void close(){ public ZkUtil getZkUtil(){ return zkUtil; } + + public ListenerDispatcher getDispatcher(){ + return dispatcher; + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index db638ea6..a208f6e4 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -31,8 +31,9 @@ public void testMulThreadRegisterApp() throws InterruptedException{ @Test - public void testServerManageStart(){ + public void testServerManageStart() throws InterruptedException{ manage.start(); + Thread.sleep(Integer.MAX_VALUE); } From 2f35c994b3ce21510c3045c923b63f2e08566b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 16:32:11 +0800 Subject: [PATCH 075/890] =?UTF-8?q?open=20appList=20=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zk/listener/impl/ConnectionPathListener.java | 15 ++++++--------- .../mpush/tools/zk/manage/ServerManage.java | 9 +++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 9d099d5d..45efe45e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -40,13 +40,10 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) } if (Type.NODE_ADDED == event.getType()) { dataAddOrUpdate(event.getData()); - log.warn("ConnectionPathListener path:" + path + ", node Add"+","+data); } else if (Type.NODE_REMOVED == event.getType()) { dataRemove(event.getData()); - log.warn("ConnectionPathListener path:" + path + ", node Remove"+","+data); } else if (Type.NODE_UPDATED == event.getType()) { dataAddOrUpdate(event.getData()); - log.warn("ConnectionPathListener path:" + path + "," + "node update"+","+data); } else { log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data); } @@ -55,12 +52,12 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) @Override public void initData(ServerManage manage) { log.warn("start init app data"); - getData(); + _initData(); printAppList(); log.warn("end init app data"); } - private void getData(){ + private void _initData(){ //获取机器列表 List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); for(String raw:rawData){ @@ -90,13 +87,13 @@ private ServerApp getServerApp(String fullPath){ return app; } - public Collection getAppList(){ - return Collections.unmodifiableCollection(holder.values()); - } - private void printAppList(){ for(ServerApp app:holder.values()){ log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); } } + + public Collection getAppList() { + return Collections.unmodifiableCollection(holder.values()); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 76880885..e6acd325 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.tools.zk.manage; +import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.curator.framework.CuratorFramework; @@ -11,12 +12,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; +import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; public class ServerManage { @@ -118,5 +121,11 @@ public ZkUtil getZkUtil(){ public ListenerDispatcher getDispatcher(){ return dispatcher; } + + public Collection getServerApp(){ + CallBack callback = dispatcher.getListener(PathEnum.CONNECTION_SERVER_ALL_HOST, InetAddressUtil.getInetAddress()); + ConnectionPathListener listener = (ConnectionPathListener)callback; + return listener.getAppList(); + } } From d94ecfef08c59b010174d50efe008b6de1241360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 16:55:58 +0800 Subject: [PATCH 076/890] =?UTF-8?q?=E6=8A=BD=E5=8F=96app=20manage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/impl/ConnectionPathListener.java | 26 ++-------- .../tools/zk/manage/ServerAppManage.java | 51 +++++++++++++++++++ .../mpush/tools/zk/manage/ServerManage.java | 13 ----- 3 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 45efe45e..13317fcf 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -1,9 +1,6 @@ package com.shinemo.mpush.tools.zk.listener.impl; -import java.util.Collection; -import java.util.Collections; import java.util.List; -import java.util.Map; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -14,12 +11,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Maps; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; import com.shinemo.mpush.tools.zk.manage.ServerManage; /** @@ -30,8 +27,6 @@ public class ConnectionPathListener implements CallBack{ private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class); - private static Map holder = Maps.newConcurrentMap(); - @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { String data = ""; @@ -53,7 +48,6 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) public void initData(ServerManage manage) { log.warn("start init app data"); _initData(); - printAppList(); log.warn("end init app data"); } @@ -63,22 +57,20 @@ private void _initData(){ for(String raw:rawData){ String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw); ServerApp app = getServerApp(fullPath); - holder.put(fullPath, app); + ServerAppManage.instance.addOrUpdate(fullPath, app); } } private void dataRemove(ChildData data){ String path = data.getPath(); - holder.remove(path); - printAppList(); + ServerAppManage.instance.remove(path); } private void dataAddOrUpdate(ChildData data){ String path = data.getPath(); byte[] rawData = data.getData(); ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); - holder.put(path, serverApp); - printAppList(); + ServerAppManage.instance.addOrUpdate(path, serverApp); } private ServerApp getServerApp(String fullPath){ @@ -86,14 +78,6 @@ private ServerApp getServerApp(String fullPath){ ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); return app; } - - private void printAppList(){ - for(ServerApp app:holder.values()){ - log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); - } - } - public Collection getAppList() { - return Collections.unmodifiableCollection(holder.values()); - } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java new file mode 100644 index 00000000..3964b728 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java @@ -0,0 +1,51 @@ +package com.shinemo.mpush.tools.zk.manage; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.zk.ServerApp; + +/** + * 系统中当前可用的app列表 + * + */ +public class ServerAppManage { + + private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); + + private static Map holder = Maps.newConcurrentMap(); + + public static final ServerAppManage instance = new ServerAppManage(); + + private ServerAppManage() { + } + + public void addOrUpdate(String fullPath,ServerApp app){ + printAppList(); + } + + public void remove(String fullPath){ + printAppList(); + } + + public void init(){ + printAppList(); + } + + public Collection getAppList() { + return Collections.unmodifiableCollection(holder.values()); + } + + private void printAppList(){ + for(ServerApp app:holder.values()){ + log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + } + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index e6acd325..73f7bd35 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.tools.zk.manage; -import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.curator.framework.CuratorFramework; @@ -12,14 +11,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; -import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; public class ServerManage { @@ -118,14 +115,4 @@ public ZkUtil getZkUtil(){ return zkUtil; } - public ListenerDispatcher getDispatcher(){ - return dispatcher; - } - - public Collection getServerApp(){ - CallBack callback = dispatcher.getListener(PathEnum.CONNECTION_SERVER_ALL_HOST, InetAddressUtil.getInetAddress()); - ConnectionPathListener listener = (ConnectionPathListener)callback; - return listener.getAppList(); - } - } From e914128adb789af8e77406d21acda25426bd0e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 17:01:30 +0800 Subject: [PATCH 077/890] server manage test --- .../com/shinemo/mpush/tools/zk/manage/ServerAppManage.java | 6 ++---- .../java/com/shinemo/mpush/tools/zk/ServerManageTest.java | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java index 3964b728..5a64b337 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java @@ -28,14 +28,12 @@ private ServerAppManage() { } public void addOrUpdate(String fullPath,ServerApp app){ + holder.put(fullPath, app); printAppList(); } public void remove(String fullPath){ - printAppList(); - } - - public void init(){ + holder.remove(fullPath); printAppList(); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index a208f6e4..1e405c4d 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -8,13 +8,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.zk.manage.ServerManage; public class ServerManageTest { private static Executor executor = Executors.newCachedThreadPool(); - private ServerApp app = new ServerApp("10.1.10.65","3000"); + private ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(),"3000"); private ServerManage manage = new ServerManage(app); From 5b84df4c4e73ab31444e9953a92a83a4fd0a8971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 17:56:59 +0800 Subject: [PATCH 078/890] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E9=98=9F?= =?UTF-8?q?=E5=88=97=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/tools/Jsons.java | 10 +++ .../mpush/tools/zk/queue/BaseQueue.java | 51 +++++++++++ .../mpush/tools/zk/queue/Consumer.java | 29 ++++++ .../mpush/tools/zk/queue/Provider.java | 37 ++++++++ .../zk/DistributedQueueConsumerTest.java | 63 +++---------- .../zk/DistributedQueueProviderTest.java | 88 ++++++------------- 6 files changed, 167 insertions(+), 111 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index f1976e70..629a91e1 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -19,6 +19,7 @@ public final class Jsons { public static String toJson(Object bean) { + try { return GSON.toJson(bean); } catch (Exception e) { @@ -28,6 +29,7 @@ public static String toJson(Object bean) { } public static T fromJson(String json, Class clazz) { + try { return GSON.fromJson(json, clazz); } catch (Exception e) { @@ -79,4 +81,12 @@ private static void append(Map.Entry entry, StringBuilder sb) { sb.append(':'); sb.append('"').append(value).append('"'); } + + public static void main(String[] args) { + String test = "test"; + String ret = Jsons.toJson(test); + String ret2 = Jsons.fromJson(ret, String.class); + System.out.println(ret); + System.out.println(ret2); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java new file mode 100644 index 00000000..e46a4545 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java @@ -0,0 +1,51 @@ +package com.shinemo.mpush.tools.zk.queue; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.queue.QueueConsumer; +import org.apache.curator.framework.recipes.queue.QueueSerializer; +import org.apache.curator.framework.state.ConnectionState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.Jsons; + +public class BaseQueue { + + private static final Logger log = LoggerFactory.getLogger(BaseQueue.class); + + public static QueueSerializer createQueueSerializer(final Class clazz) { + return new QueueSerializer() { + + @Override + public byte[] serialize(T item) { + return Jsons.toJson(item).getBytes(); + } + + @Override + public T deserialize(byte[] bytes) { + return Jsons.fromJson(bytes, clazz); + } + + }; + } + + public static QueueConsumer createQueueConsumer(final Class clazz) { + + return new QueueConsumer() { + + @Override + public void consumeMessage(T message) throws Exception { + log.warn("consume one message:"+message); + } + + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) { + log.warn("connection new state:"+newState.name()); + } + + }; + + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java new file mode 100644 index 00000000..858e8589 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java @@ -0,0 +1,29 @@ +package com.shinemo.mpush.tools.zk.queue; + +import org.apache.curator.framework.recipes.queue.DistributedQueue; +import org.apache.curator.framework.recipes.queue.QueueBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.ZkUtil; + +public class Consumer extends BaseQueue{ + + private static final Logger log = LoggerFactory.getLogger(Consumer.class); + + private DistributedQueue queue = null; + private String path; + + public Consumer(String path,final Class clazz){ + QueueBuilder builder = QueueBuilder.builder(ZkUtil.instance.getClient(), + createQueueConsumer(clazz), createQueueSerializer(clazz), path); + queue = builder.buildQueue(); + this.path = path; + } + + public void start() throws Exception{ + queue.start(); + log.warn("consumer start:"+path); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java new file mode 100644 index 00000000..6d5ecf7b --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.tools.zk.queue; + +import org.apache.curator.framework.recipes.queue.DistributedQueue; +import org.apache.curator.framework.recipes.queue.QueueBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.ZkUtil; + +public class Provider extends BaseQueue{ + + private static final Logger log = LoggerFactory.getLogger(Provider.class); + + private DistributedQueue queue = null; + + private String path; + + + public Provider(String path,final Class clazz){ + + QueueBuilder builder = QueueBuilder.builder(ZkUtil.instance.getClient(), + null, createQueueSerializer(clazz), path).lockPath(path); + queue = builder.buildQueue(); + this.path = path; + } + + public void start() throws Exception{ + queue.start(); + log.warn("provider start:"+path); + } + + public void put(T item) throws Exception{ + queue.put(item); + log.warn("provider put:"+item+","+path); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index a6fd9f55..d054e6b4 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -1,77 +1,36 @@ package com.shinemo.mpush.tools.zk; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.queue.DistributedQueue; -import org.apache.curator.framework.recipes.queue.QueueBuilder; -import org.apache.curator.framework.recipes.queue.QueueConsumer; -import org.apache.curator.framework.recipes.queue.QueueSerializer; -import org.apache.curator.framework.state.ConnectionState; -import org.apache.curator.utils.CloseableUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.zk.manage.ServerManage; +import com.shinemo.mpush.tools.zk.queue.Consumer; public class DistributedQueueConsumerTest { - private ServerApp app = new ServerApp("127.0.0.1","3000"); + private ServerApp app = new ServerApp("10.1.10.65", "3000"); + private ServerManage manage = new ServerManage(app); - + @Before - public void setup(){ + public void setup() { + manage.start(); } - + @Test public void test() throws Exception{ - DistributedQueue queue = null; - QueueBuilder builder = QueueBuilder.builder(manage.getClient(), - createQueueConsumer(), createQueueSerializer(), PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress())); - queue = builder.buildQueue(); - queue.start(); - CloseableUtils.closeQuietly(queue); + Consumer consumer = new Consumer(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), ServerApp.class); + consumer.start(); } - + @After - public void close(){ + public void close() { manage.close(); } - - private static QueueSerializer createQueueSerializer() { - return new QueueSerializer() { - @Override - public byte[] serialize(String item) { - return item.getBytes(); - } - - @Override - public String deserialize(byte[] bytes) { - return new String(bytes); - } - - }; - } - - private static QueueConsumer createQueueConsumer() { - - return new QueueConsumer() { - - @Override - public void consumeMessage(String message) throws Exception { - System.out.println("consume one message: " + message); - } - - @Override - public void stateChanged(CuratorFramework client, ConnectionState newState) { - System.out.println("connection new state: " + newState.name()); - } - }; - - } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index 2e45645f..812006b2 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -1,84 +1,54 @@ package com.shinemo.mpush.tools.zk; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.api.CuratorEvent; -import org.apache.curator.framework.api.CuratorListener; -import org.apache.curator.framework.recipes.queue.DistributedQueue; -import org.apache.curator.framework.recipes.queue.QueueBuilder; -import org.apache.curator.framework.recipes.queue.QueueConsumer; -import org.apache.curator.framework.recipes.queue.QueueSerializer; -import org.apache.curator.framework.state.ConnectionState; -import org.apache.curator.utils.CloseableUtils; +import java.util.Iterator; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.tools.InetAddressUtil; +import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; import com.shinemo.mpush.tools.zk.manage.ServerManage; +import com.shinemo.mpush.tools.zk.queue.Provider; public class DistributedQueueProviderTest { - private ServerApp app = new ServerApp("127.0.0.1","3000"); + private ServerApp app = new ServerApp("10.1.10.64", "3000"); + private ServerManage manage = new ServerManage(app); - + @Before - public void setup(){ + public void setup() { + manage.start(); } - + @Test public void test() throws Exception{ - DistributedQueue queue = null; - QueueBuilder builder = QueueBuilder.builder(manage.getClient(), - null, createQueueSerializer(), PathEnum.CONNECTION_SERVER_KICK.getPathByIp(InetAddressUtil.getInetAddress())); - queue = builder.buildQueue(); - queue.start(); - for (int i = 0; i < 10; i++) { - queue.put(" test-" + i); - Thread.sleep((long) (3 * Math.random())); + + Iterator iterator = ServerAppManage.instance.getAppList().iterator(); + + List> providers = Lists.newArrayList(); + while (iterator.hasNext()) { + ServerApp app = iterator.next(); + if(!app.getIp().equals(this.app.getIp())){ + Provider provider = new Provider(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), ServerApp.class); + providers.add(provider); + provider.start(); + } + } + + for(int i = 0;i<10;i++){ + providers.get(0).put(new ServerApp("hi"+i, "hello world")); } Thread.sleep(20000); - CloseableUtils.closeQuietly(queue); } - + @After - public void close(){ + public void close() { manage.close(); } - - - private static QueueSerializer createQueueSerializer() { - return new QueueSerializer() { - @Override - public byte[] serialize(String item) { - return item.getBytes(); - } - - @Override - public String deserialize(byte[] bytes) { - return new String(bytes); - } - - }; - } - - private static QueueConsumer createQueueConsumer() { - - return new QueueConsumer() { - - @Override - public void consumeMessage(String message) throws Exception { - System.out.println("consume one message: " + message); - } - - @Override - public void stateChanged(CuratorFramework client, ConnectionState newState) { - System.out.println("connection new state: " + newState.name()); - } - }; - - } - } From d72a9bc106a14f0ed651f1d65deec1f3556fb8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 31 Dec 2015 18:03:47 +0800 Subject: [PATCH 079/890] add kick provider queue manage --- .../tools/zk/manage/KickProviderQueueManage.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java new file mode 100644 index 00000000..c6bd3a5c --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush.tools.zk.manage; + +import java.util.List; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.queue.Provider; + +public class KickProviderQueueManage { + + private static List> providers = Lists.newArrayList(); + +} From 10e4d8fc0df4466c246fdd960a443f84d07da99e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 2 Jan 2016 10:24:37 +0800 Subject: [PATCH 080/890] add queue --- .../tools/zk/consumer/ConsumerCallBack.java | 7 +++++ .../zk/manage/KickProviderQueueManage.java | 29 ++++++++++++++++++- .../mpush/tools/zk/manage/ServerManage.java | 4 +++ .../mpush/tools/zk/queue/BaseQueue.java | 4 ++- .../mpush/tools/zk/queue/Consumer.java | 5 ++-- 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java new file mode 100644 index 00000000..8400a252 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.tools.zk.consumer; + +public interface ConsumerCallBack { + + public void handler(T message); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java index c6bd3a5c..98d7208e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java @@ -1,13 +1,40 @@ package com.shinemo.mpush.tools.zk.manage; +import java.util.Iterator; import java.util.List; +import java.util.Map; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.queue.Provider; public class KickProviderQueueManage { - private static List> providers = Lists.newArrayList(); + private static Map> providers = Maps.newConcurrentMap(); + + private ServerManage serverManage; + + public KickProviderQueueManage(ServerManage serverManage) { + this.serverManage = serverManage; + Iterator iterator = ServerAppManage.instance.getAppList().iterator(); + while (iterator.hasNext()) { + ServerApp app = iterator.next(); + if(!app.getIp().equals(this.serverManage.getServerApp().getIp())){ + Provider provider = new Provider(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), ServerApp.class); + providers.put(app, provider); + } + } + } + + public void start() throws Exception{ + Iterator> iterator = providers.values().iterator(); + while(iterator.hasNext()){ + Provider provider = iterator.next(); + provider.start(); + } + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 73f7bd35..36ac3455 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -115,4 +115,8 @@ public ZkUtil getZkUtil(){ return zkUtil; } + public ServerApp getServerApp(){ + return app; + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java index e46a4545..9b2a5b83 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.consumer.ConsumerCallBack; public class BaseQueue { @@ -29,12 +30,13 @@ public T deserialize(byte[] bytes) { }; } - public static QueueConsumer createQueueConsumer(final Class clazz) { + public static QueueConsumer createQueueConsumer(final Class clazz,final ConsumerCallBack callBack) { return new QueueConsumer() { @Override public void consumeMessage(T message) throws Exception { + callBack.handler(message); log.warn("consume one message:"+message); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java index 858e8589..22302c25 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.consumer.ConsumerCallBack; public class Consumer extends BaseQueue{ @@ -14,9 +15,9 @@ public class Consumer extends BaseQueue{ private DistributedQueue queue = null; private String path; - public Consumer(String path,final Class clazz){ + public Consumer(String path,final Class clazz,final ConsumerCallBack callBack){ QueueBuilder builder = QueueBuilder.builder(ZkUtil.instance.getClient(), - createQueueConsumer(clazz), createQueueSerializer(clazz), path); + createQueueConsumer(clazz,callBack), createQueueSerializer(clazz), path); queue = builder.buildQueue(); this.path = path; } From 59cb4a50eada1008243388fc7c4872d63fd2e419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 4 Jan 2016 09:23:51 +0800 Subject: [PATCH 081/890] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=98=9F=E5=88=97?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/zk/KickConnection.java | 5 +++++ .../consumer/impl/ConsumerKickListener.java | 20 +++++++++++++++++++ .../tools/zk/listener/ListenerDispatcher.java | 3 ++- .../zk/DistributedQueueConsumerTest.java | 5 +++-- 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java new file mode 100644 index 00000000..1d89324c --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java @@ -0,0 +1,5 @@ +package com.shinemo.mpush.tools.zk; + +public class KickConnection { + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java new file mode 100644 index 00000000..a90fb472 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.tools.zk.consumer.impl; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.KickConnection; +import com.shinemo.mpush.tools.zk.consumer.ConsumerCallBack; + +public class ConsumerKickListener implements ConsumerCallBack{ + + private static final Logger log = LoggerFactory.getLogger(ConsumerKickListener.class); + + @Override + public void handler(KickConnection message) { + //TODO 删除本地持有的引用 + log.warn("consumer kick:"+ToStringBuilder.reflectionToString(message)); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index 37a2e6d7..3b264b57 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -23,7 +23,8 @@ public class ListenerDispatcher implements CallBack { public ListenerDispatcher(ServerApp app) { holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()), new ConnectionPathListener()); - holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), new KickPathListener()); + //踢人的目录已经交给队列处理了,这里不需要重复处理 +// holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), new KickPathListener()); } @Override diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index d054e6b4..56409b0e 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -4,6 +4,7 @@ import org.junit.Before; import org.junit.Test; +import com.shinemo.mpush.tools.zk.consumer.impl.ConsumerKickListener; import com.shinemo.mpush.tools.zk.manage.ServerManage; import com.shinemo.mpush.tools.zk.queue.Consumer; @@ -21,8 +22,8 @@ public void setup() { @Test public void test() throws Exception{ - Consumer consumer = new Consumer(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), ServerApp.class); - consumer.start(); +// Consumer consumer = ; +// consumer.start(); } From 62745c6fb3baaf6451cbf5f753ebf3e713838ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 4 Jan 2016 02:29:17 +0000 Subject: [PATCH 082/890] add push callback --- .../shinemo/mpush/client/PushCallback.java | 70 ----------- .../com/shinemo/mpush/client/PushClient.java | 75 ++++++----- .../client/PushClientChannelHandler.java | 35 ++++-- .../com/shinemo/mpush/client/PushRequest.java | 117 ++++++++++++++++++ ...shCallbackBus.java => PushRequestBus.java} | 20 +-- .../com/shinemo/mpush/common/ErrorCode.java | 3 +- .../core/handler/GatewayPushHandler.java | 5 +- 7 files changed, 195 insertions(+), 130 deletions(-) delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java rename mpush-client/src/main/java/com/shinemo/mpush/client/{PushCallbackBus.java => PushRequestBus.java} (54%) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java deleted file mode 100644 index 398f799b..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallback.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.shinemo.mpush.client; - -import com.shinemo.mpush.api.PushSender; - -/** - * Created by ohun on 2015/12/30. - */ -public class PushCallback implements PushSender.Callback, Runnable { - private final PushSender.Callback callback; - private final String userId; - private int status = 0; - private long timeout; - - public PushCallback(PushSender.Callback callback, String userId, int timeout) { - this.callback = callback; - this.userId = userId; - this.timeout = timeout + System.currentTimeMillis(); - } - - @Override - public void onSuccess(String userId) { - submit(1); - } - - @Override - public void onFailure(String userId) { - submit(2); - } - - @Override - public void onOffline(String userId) { - submit(3); - } - - @Override - public void onTimeout(String userId) { - submit(4); - } - - private void submit(int status) { - this.status = status; - if (callback != null) { - PushCallbackBus.INSTANCE.getExecutor().execute(this); - } else { - - } - } - - @Override - public void run() { - switch (status) { - case 1: - callback.onSuccess(userId); - case 2: - callback.onFailure(userId); - case 3: - callback.onOffline(userId); - case 4: - callback.onTimeout(userId); - } - } - - public void timeout() { - onTimeout(userId); - } - - public boolean isTimeout() { - return System.currentTimeMillis() > timeout; - } -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 9174af3b..659ca091 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -1,14 +1,9 @@ package com.shinemo.mpush.client; import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.common.handler.ErrorMessageHandler; -import com.shinemo.mpush.common.handler.OkMessageHandler; import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; @@ -25,17 +20,12 @@ public class PushClient implements PushSender { public NettyClientFactory clientFactory; - private PacketReceiver receiver; private String host = "127.0.0.1"; private int port = 4000; private int defaultTimeout = 3000; public void init() throws Exception { this.clientFactory = NettyClientFactory.INSTANCE; - MessageDispatcher receiver = new MessageDispatcher(); - receiver.register(Command.OK, new OkMessageHandler()); - receiver.register(Command.ERROR, new ErrorMessageHandler()); - this.receiver = receiver; } private Connection getConnection(String ip) { @@ -43,7 +33,7 @@ private Connection getConnection(String ip) { Client client = clientFactory.get(ip, port); if (client == null) { final Client client2 = clientFactory.createClient(ip, - port, new PushClientChannelHandler(receiver)); + port, new PushClientChannelHandler()); client2.init(); new Thread(new Runnable() { @Override @@ -61,35 +51,42 @@ public void run() { } @Override - public void send(String content, Collection userIds, final Callback callback) { - RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); - for (final String userId : userIds) { - final PushCallback cb = new PushCallback(callback, userId, defaultTimeout); - RemoteRouter router = remoteRouterManager.lookup(userId); - if (router == null) { - cb.onOffline(userId); - continue; - } - ClientLocation location = router.getRouteValue(); - Connection connection = getConnection(location.getHost()); - if (connection == null || !connection.isConnected()) { - cb.onFailure(userId); - continue; - } + public void send(String content, Collection userIds, Callback callback) { + for (String userId : userIds) { + PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(defaultTimeout) + .send(); + } + } - GatewayPushMessage pushMessage = new GatewayPushMessage(userId - , "push content", connection); - final int reqId = pushMessage.getSessionId(); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - PushCallbackBus.INSTANCE.register(reqId, cb); - } else { - callback.onFailure(userId); - } - } - }); + + public void send(String content, final String userId, final PushRequest callback) { + RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); + RemoteRouter router = remoteRouterManager.lookup(userId); + if (router == null) { + callback.onOffline(userId); + return; } + ClientLocation location = router.getRouteValue(); + Connection connection = getConnection(location.getHost()); + if (connection == null || !connection.isConnected()) { + callback.onFailure(userId); + return; + } + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, connection); + PushRequestBus.INSTANCE.register(pushMessage.getSessionId(), callback); + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + } else { + callback.onFailure(userId); + } + } + }); } } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java index 4889d646..e0059710 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java @@ -1,8 +1,10 @@ package com.shinemo.mpush.client; -import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.ErrorCode; +import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -12,11 +14,6 @@ */ public class PushClientChannelHandler extends ChannelHandlerAdapter { private final Connection connection = new NettyConnection(); - private final PacketReceiver receiver; - - public PushClientChannelHandler(PacketReceiver receiver) { - this.receiver = receiver; - } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { @@ -25,14 +22,34 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - super.channelInactive(ctx); connection.close(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - super.channelRead(ctx, msg); - receiver.onReceive(((Packet) msg), connection); + if (msg instanceof Packet) { + Packet packet = ((Packet) msg); + PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); + if (request == null) { + return; + } + + if (packet.cmd == Command.OK.cmd) { + request.success(); + } else if (packet.cmd == Command.ERROR.cmd) { + ErrorMessage message = new ErrorMessage(packet, connection); + byte errorCode = message.code; + if (errorCode == ErrorCode.OFFLINE.errorCode) { + request.offline(); + } else if (errorCode == ErrorCode.PUSH_CLIENT_FAILURE.errorCode) { + request.failure(); + } else if (errorCode == ErrorCode.ROUTER_CHANGE.errorCode) { + request.redirect(); + } + } else { + + } + } } public Connection getConnection() { diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java new file mode 100644 index 00000000..fdc69b3f --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java @@ -0,0 +1,117 @@ +package com.shinemo.mpush.client; + +import com.shinemo.mpush.api.PushSender; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushRequest implements PushSender.Callback, Runnable { + private PushSender.Callback callback; + private String userId; + private String content; + private long timeout; + private PushClient pushClient; + private int status = 0; + private long timeout_; + + public PushRequest(PushClient pushClient) { + this.pushClient = pushClient; + } + + public static PushRequest build(PushClient pushClient) { + return new PushRequest(pushClient); + } + + public PushRequest setCallback(PushSender.Callback callback) { + this.callback = callback; + return this; + } + + public PushRequest setUserId(String userId) { + this.userId = userId; + return this; + } + + public PushRequest setContent(String content) { + this.content = content; + return this; + } + + public PushRequest setTimeout(long timeout) { + this.timeout = timeout; + return this; + } + + @Override + public void onSuccess(String userId) { + submit(1); + } + + @Override + public void onFailure(String userId) { + submit(2); + } + + @Override + public void onOffline(String userId) { + submit(3); + } + + @Override + public void onTimeout(String userId) { + submit(4); + } + + private void submit(int status) { + this.status = status; + if (callback != null) { + PushRequestBus.INSTANCE.getExecutor().execute(this); + } else { + + } + } + + @Override + public void run() { + switch (status) { + case 1: + callback.onSuccess(userId); + case 2: + callback.onFailure(userId); + case 3: + callback.onOffline(userId); + case 4: + callback.onTimeout(userId); + } + } + + public void timeout() { + onTimeout(userId); + } + + public void success() { + onSuccess(userId); + } + + public void failure() { + onFailure(userId); + } + + public void offline() { + onOffline(userId); + } + + public void send() { + this.timeout_ = timeout + System.currentTimeMillis(); + pushClient.send(content, userId, this); + } + + public void redirect() { + this.timeout_ = timeout + System.currentTimeMillis(); + pushClient.send(content, userId, this); + } + + public boolean isTimeout() { + return System.currentTimeMillis() > timeout_; + } +} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java similarity index 54% rename from mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java rename to mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java index 4c1ae197..66b6626b 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushCallbackBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java @@ -6,22 +6,22 @@ /** * Created by ohun on 2015/12/30. */ -public class PushCallbackBus implements Runnable { - public static final PushCallbackBus INSTANCE = new PushCallbackBus(); - private Map callbacks = new ConcurrentHashMap<>(); +public class PushRequestBus implements Runnable { + public static final PushRequestBus INSTANCE = new PushRequestBus(); + private Map requests = new ConcurrentHashMap<>(); private Executor executor = Executors.newFixedThreadPool(5);//test private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test - public PushCallbackBus() { + public PushRequestBus() { scheduledExecutor.scheduleAtFixedRate(this, 1, 3, TimeUnit.SECONDS); } - public void register(int reqId, PushCallback callback) { - callbacks.put(reqId, callback); + public void register(int reqId, PushRequest callback) { + requests.put(reqId, callback); } - public PushCallback get(int reqId) { - return callbacks.get(reqId); + public PushRequest remove(int reqId) { + return requests.remove(reqId); } public Executor getExecutor() { @@ -30,8 +30,8 @@ public Executor getExecutor() { @Override public void run() { - if (callbacks.isEmpty()) return; - for (PushCallback callback : callbacks.values()) { + if (requests.isEmpty()) return; + for (PushRequest callback : requests.values()) { if (callback.isTimeout()) { callback.timeout(); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java index 5d9acfde..845ebae6 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java @@ -5,7 +5,8 @@ */ public enum ErrorCode { OFFLINE(1, "user offline"), - PUSH_CLIENT_FAILURE(2, "push to client failure"),; + PUSH_CLIENT_FAILURE(2, "push to client failure"), + ROUTER_CHANGE(3, "router change"); ErrorCode(int code, String errorMsg) { this.errorMsg = errorMsg; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index ecb73850..cf670185 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -55,7 +55,10 @@ public void operationComplete(ChannelFuture future) throws Exception { } else { //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 // 需要通过GatewayClient或ZK把消息推送到另外一台机器上 - // TODO: 2015/12/30 send message to other server + ErrorMessage + .from(message) + .setErrorCode(ErrorCode.ROUTER_CHANGE) + .send(); } } } From 3274e88af6879df349e6bb38b75d430071010d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 4 Jan 2016 07:52:24 +0000 Subject: [PATCH 083/890] add kick remote interface --- .../mpush/api/event/RouterChangeEvent.java | 16 ++++ .../com/shinemo/mpush/client/PushClient.java | 35 +-------- .../com/shinemo/mpush/client/PushRequest.java | 60 ++++++++++++-- .../shinemo/mpush/client/PushRequestBus.java | 4 +- .../router/ConnectionRouterManager.java | 49 ++++++++++++ .../common/router/RemoteRouterManager.java | 29 +------ .../mpush/core/handler/BindUserHandler.java | 2 +- .../core/handler/GatewayPushHandler.java | 2 +- .../mpush/core/router/KickRemoteMsg.java | 19 +++++ .../mpush/core}/router/LocalRouter.java | 2 +- .../core}/router/LocalRouterManager.java | 2 +- .../mpush/core}/router/RouterCenter.java | 30 +++---- .../core/router/RouterChangeListener.java | 78 +++++++++++++++++++ 13 files changed, 239 insertions(+), 89 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java rename {mpush-common/src/main/java/com/shinemo/mpush/common => mpush-core/src/main/java/com/shinemo/mpush/core}/router/LocalRouter.java (92%) rename {mpush-common/src/main/java/com/shinemo/mpush/common => mpush-core/src/main/java/com/shinemo/mpush/core}/router/LocalRouterManager.java (94%) rename {mpush-common/src/main/java/com/shinemo/mpush/common => mpush-core/src/main/java/com/shinemo/mpush/core}/router/RouterCenter.java (71%) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java new file mode 100644 index 00000000..505c1e3a --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.api.event; + +import com.shinemo.mpush.api.router.Router; + +/** + * Created by ohun on 2016/1/4. + */ +public class RouterChangeEvent implements Event { + public final String userId; + public final Router router; + + public RouterChangeEvent(String userId, Router router) { + this.userId = userId; + this.router = router; + } +} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 659ca091..c07d0ef4 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -3,14 +3,7 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.common.router.RemoteRouterManager; -import com.shinemo.mpush.common.router.RouterCenter; import com.shinemo.mpush.netty.client.NettyClientFactory; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import java.util.Collection; @@ -28,7 +21,7 @@ public void init() throws Exception { this.clientFactory = NettyClientFactory.INSTANCE; } - private Connection getConnection(String ip) { + public Connection getConnection(String ip) { try { Client client = clientFactory.get(ip, port); if (client == null) { @@ -64,29 +57,5 @@ public void send(String content, Collection userIds, Callback callback) } - public void send(String content, final String userId, final PushRequest callback) { - RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); - RemoteRouter router = remoteRouterManager.lookup(userId); - if (router == null) { - callback.onOffline(userId); - return; - } - ClientLocation location = router.getRouteValue(); - Connection connection = getConnection(location.getHost()); - if (connection == null || !connection.isConnected()) { - callback.onFailure(userId); - return; - } - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, connection); - PushRequestBus.INSTANCE.register(pushMessage.getSessionId(), callback); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - } else { - callback.onFailure(userId); - } - } - }); - } + } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java index fdc69b3f..4e26d59a 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java @@ -1,11 +1,21 @@ package com.shinemo.mpush.client; import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.router.ClientLocation; +import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.common.router.ConnectionRouterManager; +import com.shinemo.mpush.common.router.RemoteRouter; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/30. */ public class PushRequest implements PushSender.Callback, Runnable { + private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); private PushSender.Callback callback; private String userId; private String content; @@ -13,6 +23,8 @@ public class PushRequest implements PushSender.Callback, Runnable { private PushClient pushClient; private int status = 0; private long timeout_; + private int sessionId; + private long sendTime; public PushRequest(PushClient pushClient) { this.pushClient = pushClient; @@ -42,6 +54,14 @@ public PushRequest setTimeout(long timeout) { return this; } + public void setSessionId(int sessionId) { + this.sessionId = sessionId; + } + + public int getSessionId() { + return sessionId; + } + @Override public void onSuccess(String userId) { submit(1); @@ -64,10 +84,11 @@ public void onTimeout(String userId) { private void submit(int status) { this.status = status; + if (sessionId > 0) PushRequestBus.INSTANCE.remove(sessionId); if (callback != null) { PushRequestBus.INSTANCE.getExecutor().execute(this); } else { - + LOGGER.warn("callback is null"); } } @@ -85,6 +106,10 @@ public void run() { } } + public boolean isTimeout() { + return System.currentTimeMillis() > timeout_; + } + public void timeout() { onTimeout(userId); } @@ -103,15 +128,40 @@ public void offline() { public void send() { this.timeout_ = timeout + System.currentTimeMillis(); - pushClient.send(content, userId, this); + sendToConnectionServer(); } public void redirect() { this.timeout_ = timeout + System.currentTimeMillis(); - pushClient.send(content, userId, this); + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + sendToConnectionServer(); + LOGGER.warn("user route has changed, userId={}, content={}", userId, content); } - public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; + private void sendToConnectionServer() { + RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); + if (router == null) { + this.onOffline(userId); + return; + } + ClientLocation location = router.getRouteValue(); + Connection connection = pushClient.getConnection(location.getHost()); + if (connection == null || !connection.isConnected()) { + this.onFailure(userId); + return; + } + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, connection); + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + sendTime = System.currentTimeMillis(); + } else { + PushRequest.this.onFailure(userId); + } + } + }); + this.sessionId = pushMessage.getSessionId(); + PushRequestBus.INSTANCE.add(this); } } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java index 66b6626b..4dc43058 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java @@ -16,8 +16,8 @@ public PushRequestBus() { scheduledExecutor.scheduleAtFixedRate(this, 1, 3, TimeUnit.SECONDS); } - public void register(int reqId, PushRequest callback) { - requests.put(reqId, callback); + public void add(PushRequest request) { + requests.put(request.getSessionId(), request); } public PushRequest remove(int reqId) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java new file mode 100644 index 00000000..30f60689 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.common.router; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +import java.util.concurrent.TimeUnit; + +/** + * Created by ohun on 2016/1/4. + */ +public class ConnectionRouterManager extends RemoteRouterManager { + public static final ConnectionRouterManager INSTANCE = new ConnectionRouterManager(); + // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 + private final Cache cache = CacheBuilder + .newBuilder() + .expireAfterWrite(5, TimeUnit.MINUTES) + .expireAfterAccess(5, TimeUnit.MINUTES) + .build(); + + + @Override + public RemoteRouter register(String userId, RemoteRouter route) { + RemoteRouter old = cache.getIfPresent(userId); + cache.put(userId, route); + return old; + } + + @Override + public boolean unRegister(String userId) { + cache.invalidate(userId); + return true; + } + + @Override + public RemoteRouter lookup(String userId) { + return cache.getIfPresent(userId); + } + + /** + * 如果推送失败,可能是缓存不一致了,可以让本地缓存失效 + *

+ * 失效对应的本地缓存 + * + * @param userId + */ + public void invalidateLocalCache(String userId) { + cache.invalidate(userId); + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 5261745e..8a1fe5e2 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -1,49 +1,24 @@ package com.shinemo.mpush.common.router; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import com.shinemo.mpush.api.router.RouterManager; -import java.util.concurrent.TimeUnit; - /** * Created by ohun on 2015/12/23. */ public class RemoteRouterManager implements RouterManager { - // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 - private final Cache cache = CacheBuilder - .newBuilder() - .expireAfterWrite(5, TimeUnit.MINUTES) - .expireAfterAccess(5, TimeUnit.MINUTES) - .build(); - @Override public RemoteRouter register(String userId, RemoteRouter route) { - RemoteRouter old = cache.getIfPresent(userId); - cache.put(userId, route); - return old; + return null; } @Override public boolean unRegister(String userId) { - cache.invalidate(userId); return true; } @Override public RemoteRouter lookup(String userId) { - return cache.getIfPresent(userId); - } - - /** - * 如果推送失败,可能是缓存不一致了,可以让本地缓存失效 - *

- * 失效对应的本地缓存 - * - * @param userId - */ - public void invalidateLocalCache(String userId) { - cache.invalidate(userId); + return null; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 0ed2daf9..04e29b81 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -8,7 +8,7 @@ import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.router.RouterCenter; +import com.shinemo.mpush.core.router.RouterCenter; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index cf670185..9d5e495c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -9,7 +9,7 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.router.Router; import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.router.RouterCenter; +import com.shinemo.mpush.core.router.RouterCenter; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java new file mode 100644 index 00000000..c09142bd --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java @@ -0,0 +1,19 @@ +package com.shinemo.mpush.core.router; + +/** + * Created by ohun on 2016/1/4. + */ +public class KickRemoteMsg { + public String userId; + public String deviceId; + public String srcServer; + + @Override + public String toString() { + return "KickRemoteMsg{" + + "userId='" + userId + '\'' + + ", deviceId='" + deviceId + '\'' + + ", srcServer='" + srcServer + '\'' + + '}'; + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java similarity index 92% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouter.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java index a020b7f6..d99e7904 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common.router; +package com.shinemo.mpush.core.router; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.router.Router; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java similarity index 94% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouterManager.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 2f14cef8..c22cff08 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common.router; +package com.shinemo.mpush.core.router; import com.shinemo.mpush.api.router.RouterManager; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java similarity index 71% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 72ad40b5..74b85a3b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -1,10 +1,12 @@ -package com.shinemo.mpush.common.router; +package com.shinemo.mpush.core.router; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.event.RouterChangeEvent; import com.shinemo.mpush.api.router.Router; import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.common.message.KickUserMessage; +import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.common.router.RemoteRouterManager; /** * Created by ohun on 2015/12/23. @@ -14,6 +16,7 @@ public class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); + private final RouterChangeListener routerChangeListener = new RouterChangeListener(); /** * 注册用户和链接 @@ -31,11 +34,11 @@ public boolean register(String userId, Connection connection) { LocalRouter oldLocalRouter = localRouterManager.register(userId, localRouter); RemoteRouter oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); if (oldLocalRouter != null) { - kickLocalUser(userId, oldLocalRouter); + EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); } if (oldRemoteRouter != null) { - kickRemoteUser(userId, oldRemoteRouter); + EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); } return true; } @@ -53,19 +56,6 @@ public Router lookup(String userId) { return remote; } - public void kickLocalUser(String userId, LocalRouter router) { - Connection connection = router.getRouteValue(); - SessionContext context = connection.getSessionContext(); - KickUserMessage message = new KickUserMessage(connection); - message.deviceId = context.deviceId; - message.userId = userId; - message.send(); - } - - public void kickRemoteUser(String userId, RemoteRouter router) { - //send msg to zk - } - public LocalRouterManager getLocalRouterManager() { return localRouterManager; @@ -74,4 +64,8 @@ public LocalRouterManager getLocalRouterManager() { public RemoteRouterManager getRemoteRouterManager() { return remoteRouterManager; } + + public RouterChangeListener getRouterChangeListener() { + return routerChangeListener; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java new file mode 100644 index 00000000..c01d3511 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -0,0 +1,78 @@ +package com.shinemo.mpush.core.router; + +import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.event.RouterChangeEvent; +import com.shinemo.mpush.api.router.ClientLocation; +import com.shinemo.mpush.api.router.Router; +import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.message.KickUserMessage; +import com.shinemo.mpush.common.router.RemoteRouter; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2016/1/4. + */ +public class RouterChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); + + public RouterChangeListener() { + EventBus.INSTANCE.register(this); + // TODO: 2016/1/4 register this to redis server + } + + @Subscribe + void onRouteChangeEvent(RouterChangeEvent event) { + String userId = event.userId; + Router r = event.router; + if (r.getRouteType() == Router.RouterType.LOCAL) { + kickLocal(userId, (LocalRouter) r); + } else { + kickRemote(userId, (RemoteRouter) r); + } + } + + public void kickLocal(final String userId, LocalRouter router) { + Connection connection = router.getRouteValue(); + SessionContext context = connection.getSessionContext(); + KickUserMessage message = new KickUserMessage(connection); + message.deviceId = context.deviceId; + message.userId = userId; + message.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + LOGGER.info("kick local connection success, userId={}", userId); + } else { + LOGGER.error("kick local connection failure, userId={}", userId); + } + } + }); + } + + + public void kickRemote(String userId, RemoteRouter router) { + ClientLocation location = router.getRouteValue(); + KickRemoteMsg msg = new KickRemoteMsg(); + msg.deviceId = location.getDeviceId(); + msg.srcServer = location.getHost(); + msg.userId = userId; + // TODO: 2016/1/4 publish kick remote user msg to redis + } + + // TODO: 2016/1/4 receive msg from redis + public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { + String userId = msg.userId; + LocalRouter router = RouterCenter.INSTANCE.getLocalRouterManager().lookup(userId); + if (router != null) { + LOGGER.info("receive kick remote msg, msg={}", msg); + kickLocal(userId, router); + } else { + LOGGER.warn("no local router find, kick failure, msg={}", msg); + } + } +} From 5674bc5d1414ce735a9f9910eec657468e5feae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 4 Jan 2016 11:01:03 +0000 Subject: [PATCH 084/890] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Constants.java | 2 +- .../mpush/api/connection/Connection.java | 12 +++- .../mpush/api/connection/SessionContext.java | 5 ++ .../common/message/HandshakeMessage.java | 6 ++ .../mpush/core/handler/HandshakeHandler.java | 16 ++++-- .../mpush/core/handler/HeartBeatHandler.java | 4 +- .../core/server/ServerChannelHandler.java | 28 +-------- ...Connection.java => ConnectionScanner.java} | 20 +++---- .../netty/connection/NettyConnection.java | 51 +++++++++++------ .../connection/NettyConnectionManager.java | 57 +++++++++++++++---- .../com/shinemo/mpush/tools/Constants.java | 11 ++-- .../com/shinemo/mpush/tools/MPushUtil.java | 4 ++ 12 files changed, 133 insertions(+), 83 deletions(-) rename mpush-core/src/main/java/com/shinemo/mpush/core/task/{ScanAllClientConnection.java => ConnectionScanner.java} (83%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index f4d8b9bb..6b13ec26 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -27,5 +27,5 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; - int HEARTBEAT_TIME = 1000 * 60 * 5;//5min + int HEARTBEAT_TIME = 1000 * 60 * 4;//5min } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java index 350d9514..0b40eb24 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java @@ -10,6 +10,10 @@ * Created by ohun on 2015/12/22. */ public interface Connection { + int STATUS_NEW = 0; + int STATUS_CONNECTED = 1; + int STATUS_DISCONNECTED = 2; + int STATUS_TIMEOUT = 3; void init(Channel channel, boolean security); @@ -19,7 +23,7 @@ public interface Connection { ChannelFuture send(Packet packet); - void send(Packet packet, ChannelFutureListener listener); + ChannelFuture send(Packet packet, ChannelFutureListener listener); Channel channel(); @@ -28,4 +32,10 @@ public interface Connection { void close(); boolean isConnected(); + + boolean heartbeatTimeout(); + + void setLastReadTime(); + + } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java index 43ccac8b..a7d2033b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java @@ -10,6 +10,7 @@ public final class SessionContext { public String osVersion; public String clientVersion; public String deviceId; + public int heartbeat; public Cipher cipher; public void changeCipher(Cipher cipher) { @@ -36,6 +37,10 @@ public SessionContext setDeviceId(String deviceId) { return this; } + public void setHeartbeat(int heartbeat) { + this.heartbeat = heartbeat; + } + public boolean handshakeOk() { return !Strings.isNullOrEmpty(deviceId); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java index d3d6a2ff..3a1b2172 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java @@ -15,6 +15,8 @@ public final class HandshakeMessage extends ByteBufMessage { public String clientVersion; public byte[] iv; public byte[] clientKey; + public int minHeartbeat; + public int maxHeartbeat; public long timestamp; public HandshakeMessage(Connection connection) { @@ -33,6 +35,8 @@ public void decode(ByteBuf body) { clientVersion = decodeString(body); iv = decodeBytes(body); clientKey = decodeBytes(body); + minHeartbeat = decodeInt(body); + maxHeartbeat = decodeInt(body); timestamp = decodeLong(body); } @@ -43,6 +47,8 @@ public void encode(ByteBuf body) { encodeString(body, clientVersion); encodeBytes(body, iv); encodeBytes(body, clientKey); + encodeInt(body, minHeartbeat); + encodeInt(body, maxHeartbeat); encodeLong(body, timestamp); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 3e85e5f4..616a2926 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -59,27 +59,31 @@ public void handle(HandshakeMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); ReusableSessionManager.INSTANCE.cacheSession(session); - //5.响应握手成功消息 + //5.计算心跳时间 + int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + + //6.响应握手成功消息 HandshakeOkMessage .from(message) .setServerKey(serverKey) .setServerHost(MPushUtil.getLocalIp()) .setServerTime(System.currentTimeMillis()) - .setHeartbeat(Constants.HEARTBEAT_TIME) + .setHeartbeat(heartbeat) .setSessionId(session.sessionId) .setExpireTime(session.expireTime) .send(); - //6.更换会话密钥AES(clientKey)=>AES(sessionKey) + //7.更换会话密钥AES(clientKey)=>AES(sessionKey) context.changeCipher(new AesCipher(sessionKey, iv)); - //7.保存client信息 + //8.保存client信息 context.setOsName(message.osName) .setOsVersion(message.osVersion) .setClientVersion(message.clientVersion) - .setDeviceId(message.deviceId); + .setDeviceId(message.deviceId) + .setHeartbeat(heartbeat); - //8.触发握手成功事件 + //9.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), Constants.HEARTBEAT_TIME)); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 0827460e..5afb0d71 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -10,8 +10,6 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { - System.err.println("receive client heartbeat, time=" - + System.currentTimeMillis()); - + connection.send(packet); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 93c8c56d..f518f03d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -36,6 +36,7 @@ public ServerChannelHandler(ConnectionManager connectionManager, PacketReceiver public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); receiver.onReceive((Packet) msg, connection); + connection.setLastReadTime(); } @Override @@ -57,31 +58,4 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { LOGGER.warn(ctx.channel().remoteAddress() + ", channelInactive"); connectionManager.remove(ctx.channel()); } - - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - if (evt instanceof IdleStateEvent) { - IdleStateEvent stateEvent = (IdleStateEvent) evt; - switch (stateEvent.state()) { - case READER_IDLE: - connectionManager.remove(ctx.channel()); - ctx.close(); - LOGGER.warn("heartbeat read timeout, chanel closed!"); - break; - case WRITER_IDLE: - ctx.writeAndFlush(Packet.getHBPacket()); - LOGGER.warn("heartbeat write timeout, do write an EOL."); - break; - case ALL_IDLE: - } - } else { - LOGGER.warn("One user event Triggered. evt=" + evt); - super.userEventTriggered(ctx, evt); - } - } - - public ServerChannelHandler setSecurity(boolean security) { - this.security = security; - return this; - } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java similarity index 83% rename from mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java rename to mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java index ca78fd3a..bbe21c0f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanAllClientConnection.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java @@ -1,29 +1,27 @@ package com.shinemo.mpush.core.task; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.netty.util.NettySharedHolder; import io.netty.util.Timeout; import io.netty.util.TimerTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.netty.util.NettySharedHolder; - /** * 定时全量扫描connection */ -public class ScanAllClientConnection implements TimerTask { +public class ConnectionScanner implements TimerTask { - private static final Logger log = LoggerFactory.getLogger(ScanAllClientConnection.class); + private static final Logger log = LoggerFactory.getLogger(ConnectionScanner.class); private final List taskList = new ArrayList(); - public ScanAllClientConnection(final ScanTask... scanTasks) { + public ConnectionScanner(final ScanTask... scanTasks) { if (scanTasks != null) { for (final ScanTask task : scanTasks) { this.taskList.add(task); @@ -44,7 +42,7 @@ public void run(Timeout timeout) throws Exception { } } } catch (Exception e) { - log.error("", "exception on scan", e); + log.error("exception on scan", e); } finally { NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 36a21a73..4f464ce2 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -13,13 +13,15 @@ /** * Created by ohun on 2015/12/22. */ -public final class NettyConnection implements Connection { +public final class NettyConnection implements Connection, ChannelFutureListener { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); private SessionContext context; private Channel channel; private boolean security; - private volatile int status = 0; + private volatile int status = STATUS_NEW; + private long lastReadTime; + private long lastWriteTime; @Override public void init(Channel channel, boolean security) { @@ -29,7 +31,8 @@ public void init(Channel channel, boolean security) { if (security) { this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); } - this.status = 1; + this.lastReadTime = System.currentTimeMillis(); + this.status = STATUS_CONNECTED; } @Override @@ -48,14 +51,17 @@ public String getId() { } @Override - public ChannelFuture send(final Packet packet) { - return channel.writeAndFlush(packet); + public ChannelFuture send(Packet packet) { + return send(packet, null); } @Override - public void send(Packet packet, ChannelFutureListener listener) { - if (listener == null) channel.writeAndFlush(packet); - else channel.writeAndFlush(packet).addListener(listener); + public ChannelFuture send(Packet packet, ChannelFutureListener listener) { + if (listener != null) { + return channel.writeAndFlush(packet).addListener(listener).addListener(this); + } else { + return channel.writeAndFlush(packet).addListener(this); + } } @Override @@ -63,23 +69,34 @@ public Channel channel() { return channel; } - public Channel getChannel() { - return channel; + @Override + public void close() { + this.status = STATUS_DISCONNECTED; + this.channel.close(); } - public void setChannel(Channel channel) { - this.channel = channel; + @Override + public boolean isConnected() { + return status == STATUS_CONNECTED || channel.isActive(); } @Override - public void close() { - this.status = 0; - this.channel.close(); + public boolean heartbeatTimeout() { + return context.heartbeat > 0 && System.currentTimeMillis() - lastReadTime > context.heartbeat; } @Override - public boolean isConnected() { - return status == 0 || channel.isActive(); + public void setLastReadTime() { + lastReadTime = System.currentTimeMillis(); + } + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + lastWriteTime = System.currentTimeMillis(); + } else { + LOGGER.error("send msg error"); + } } @Override diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 78ab35fa..3aa2ae69 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -2,12 +2,16 @@ import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.common.EventBus; import io.netty.channel.Channel; -import io.netty.handler.timeout.IdleStateHandler; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timeout; +import io.netty.util.Timer; +import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,13 +27,18 @@ */ public final class NettyConnectionManager implements ConnectionManager { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectionManager.class); + private Timer wheelTimer; - public void registerEventBus() { + + public void init() { + long tickDuration = 1000;//1s + int ticksPerWheel = (int) (Constants.HEARTBEAT_TIME / tickDuration); + this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); EventBus.INSTANCE.register(this); } //可能会有20w的链接数 - private final ConcurrentMap connections = new ConcurrentHashMapV8(); + private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); public Connection get(final String channelId) throws ExecutionException { return connections.get(channelId); @@ -58,20 +67,44 @@ public void remove(Channel channel) { } public List getConnectionIds() { - return new ArrayList(connections.keySet()); + return new ArrayList<>(connections.keySet()); } public List getConnections() { - return new ArrayList(connections.values()); + return new ArrayList<>(connections.values()); } - @Subscribe - public void onHandshakeSuccess(HandshakeEvent event) { - int r = event.heartbeat + 3000; - int w = event.heartbeat + 3000; - Channel channel = event.connection.channel(); - channel.pipeline().addFirst(new IdleStateHandler(r, w, 0, TimeUnit.MILLISECONDS)); - LOGGER.warn("NettyChannel setHeartbeat readTimeout={}, writeTimeout={}", r, w); + public void onHandshakeOk(HandshakeEvent event) { + HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); + task.startTimeout(); + } + + private class HeartbeatCheckTask implements TimerTask { + private int expiredTimes = 0; + private final int heartbeat; + private final Connection connection; + + public HeartbeatCheckTask(int heartbeat, Connection connection) { + this.heartbeat = heartbeat; + this.connection = connection; + } + + public void startTimeout() { + wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + } + + @Override + public void run(Timeout timeout) throws Exception { + if (connection.heartbeatTimeout()) { + if (++expiredTimes > 5) { + connection.close(); + return; + } else { + LOGGER.error("connection heartbeat timeout, expiredTimes=" + expiredTimes); + } + } + startTimeout(); + } } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 64d9e8f4..f00c24f6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -7,8 +7,9 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); + int HEARTBEAT_TIME = 1000 * 60 * 5;//5min byte[] EMPTY_BYTES = new byte[0]; - + String JVM_LOG_PATH = "/opt/"; int THREAD_QUEUE_SIZE = 10000; @@ -20,22 +21,22 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; - + //zk int ZK_MAX_RETRY = 3; int ZK_MIN_TIME = 5000; int ZK_MAX_TIME = 5000; - int ZK_SESSION_TIMEOUT = 5000; + int ZK_SESSION_TIMEOUT = 5000; int ZK_CONNECTION_TIMEOUT = 5000; String ZK_DEFAULT_CACHE_PATH = "/"; String ZK_DEFAULT_DIGEST = "shinemo"; String ZK_IPS = "127.0.0.1:2181"; - + //zk cs String ZK_NAME_SPACE = "mpush"; //所有机器启动的时候注册ip的地方 String ZK_REGISTER_HOST = "/allhost"; String ZK_REGISTER_PREFIX_NAME = "machine"; String ZK_KICK = "kickoff"; - + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 63a715ad..36ac1f57 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -20,4 +20,8 @@ public static String getLocalIp() { return LOCAL_IP; } + public static int getHeartbeat(int min, int max) { + return Constants.HEARTBEAT_TIME; + } + } From 8c3297c8c7eb11b1ab76533935ba5bdc20f820ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 4 Jan 2016 11:01:51 +0000 Subject: [PATCH 085/890] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/shinemo/mpush/core/netty/NettyServerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 63fe0b8b..1fd2c535 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -32,7 +32,7 @@ public void testStart() throws Exception { receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); - connectionManager.registerEventBus(); + connectionManager.init(); ChannelHandler handler = new ServerChannelHandler(connectionManager, receiver); final NettyServer server = new ConnectionServer(3000, handler); From 579d7176f8d16b3f49cd0ab2cdd0d355d47cdf6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 4 Jan 2016 11:02:35 +0000 Subject: [PATCH 086/890] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/core/handler/HeartBeatHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 5afb0d71..3212f4ef 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -10,6 +10,6 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { - connection.send(packet); + connection.send(packet);//ping -> pong } } From c38af7edda7bb36b8a609d69174f52e9b26be063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 4 Jan 2016 20:53:04 +0800 Subject: [PATCH 087/890] add redis --- mpush-tools/pom.xml | 4 + .../com/shinemo/mpush/tools/Constants.java | 12 + .../java/com/shinemo/mpush/tools/Jsons.java | 4 +- .../shinemo/mpush/tools/redis/RedisGroup.java | 17 + .../mpush/tools/redis/RedisGroupManage.java | 47 +++ .../shinemo/mpush/tools/redis/RedisNode.java | 37 ++ .../mpush/tools/redis/RedisPoolConfig.java | 35 ++ .../shinemo/mpush/tools/redis/RedisUtil.java | 391 ++++++++++++++++++ .../redis/consistenthash/ConsistentHash.java | 66 +++ .../tools/redis/consistenthash/Node.java | 26 ++ .../zk/listener/impl/RedisPathListener.java | 83 ++++ .../mpush/tools/redis/ConsistentHashTest.java | 52 +++ .../mpush/tools/redis/RedisUtilTest.java | 137 ++++++ .../com/shinemo/mpush/tools/redis/User.java | 40 ++ pom.xml | 7 +- 15 files changed, 951 insertions(+), 7 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index d6669330..cee32028 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -57,5 +57,9 @@ org.logback-extensions logback-ext-spring + + redis.clients + jedis + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 64d9e8f4..45660063 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -38,4 +38,16 @@ public interface Constants { String ZK_REGISTER_PREFIX_NAME = "machine"; String ZK_KICK = "kickoff"; + //redis + int REDIS_TIMEOUT = 2000; + int REDIS_MAX_TOTAL = 8; + int REDIS_MAX_IDLE = 4; + int REDIS_MIN_IDLE = 1; + int REDIS_MAX_WAITMILLIS = 5000; + int REDIS_MIN_EVICTABLEIDLETIMEMILLIS = 300000; + int REDIS_NUMTESTSPEREVICTIONRUN = 3; + int REDIS_TIMEBETWEENEVICTIONRUNMILLIS = 60000; + boolean REDIS_TESTONBORROW = false; + boolean REDIS_TESTONRETURN = false; + boolean REDIS_TESTWHILEIDLE = false; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index 629a91e1..b4e01333 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -3,6 +3,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; + + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +39,7 @@ public static T fromJson(String json, Class clazz) { } return null; } - + public static T fromJson(byte[] json, Class clazz) { return fromJson(new String(json, Constants.UTF_8), clazz); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java new file mode 100644 index 00000000..f55f9014 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.List; + +import com.google.common.collect.Lists; + +/** + * redis 组 + * + */ +public class RedisGroup { + + List redisNodeList = Lists.newArrayList(); + + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java new file mode 100644 index 00000000..16e390d0 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java @@ -0,0 +1,47 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.Collections; +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import redis.clients.jedis.Jedis; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; + +public class RedisGroupManage { + + + private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); + + private static List holder = Lists.newArrayList(); + + public static final RedisGroupManage instance = new RedisGroupManage(); + + + private RedisGroupManage() { + } + + public void init(){ + + } + + + public List getAppList() { + return Collections.unmodifiableList(holder); + } + + private void printAppList(){ + for(RedisGroup app:holder){ + log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + } + } + + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java new file mode 100644 index 00000000..0b88cba8 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.tools.redis; + +/** + * redis 相关的配置信息 + * + */ +public class RedisNode { + + private String ip; + private int port; + private String password; + public String getIp() { + return ip; + } + public void setIp(String ip) { + this.ip = ip; + } + public int getPort() { + return port; + } + public void setPort(int port) { + this.port = port; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + public RedisNode(String ip, int port, String password) { + this.ip = ip; + this.port = port; + this.password = password; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java new file mode 100644 index 00000000..fc24eacc --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java @@ -0,0 +1,35 @@ +package com.shinemo.mpush.tools.redis; + +import com.shinemo.mpush.tools.Constants; + +import redis.clients.jedis.JedisPoolConfig; + +public class RedisPoolConfig { + + public static JedisPoolConfig config = new JedisPoolConfig(); + + static{ + //连接池中最大连接数。高版本:maxTotal,低版本:maxActive + config.setMaxTotal(Constants.REDIS_MAX_TOTAL); + //连接池中最大空闲的连接数 + config.setMaxIdle(Constants.REDIS_MAX_IDLE); + //连接池中最少空闲的连接数 + config.setMinIdle(Constants.REDIS_MIN_IDLE); + //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait + config.setMaxWaitMillis(Constants.REDIS_MAX_WAITMILLIS); + //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 + config.setMinEvictableIdleTimeMillis(Constants.REDIS_MIN_EVICTABLEIDLETIMEMILLIS); + //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 + config.setNumTestsPerEvictionRun(Constants.REDIS_NUMTESTSPEREVICTIONRUN); + //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 + config.setTimeBetweenEvictionRunsMillis(Constants.REDIS_TIMEBETWEENEVICTIONRUNMILLIS); + //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. + config.setTestOnBorrow(Constants.REDIS_TESTONBORROW); + //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 + config.setTestOnReturn(Constants.REDIS_TESTONRETURN); + //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. + config.setTestWhileIdle(Constants.REDIS_TESTWHILEIDLE); + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java new file mode 100644 index 00000000..3d2d6cb3 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -0,0 +1,391 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.Constants; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + + +public class RedisUtil { + + private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); + + private static Map holder = Maps.newConcurrentMap(); + + public static Jedis getClient(RedisNode node) { + JedisPool pool = holder.get(node); + if(pool == null){ + pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); + holder.put(node, pool); + } + return pool.getResource(); + } + + public static void close(Jedis jedis){ + jedis.close(); + } + + /*********************k v redis start********************************/ + /** + * + * @param node redis实例 + * @param key + * @param clazz + * @return + */ + public static T get(RedisNode node,String key,Class clazz) { + + String value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.get(key); + } catch (Exception e) { + log.warn("redis get exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + return Jsons.fromJson(value, clazz); + } + + public static void set(List nodeList,String key,String value) { + + set(nodeList, key, value, null); + + } + + public static void set(List nodeList,String key,T value) { + set(nodeList, key, value, null); + } + + public static void set(List nodeList,String key,T value,Integer time) { + String jsonValue = Jsons.toJson(value); + set(nodeList, key, jsonValue, time); + } + + /** + * + * @param nodeList + * @param key + * @param value + * @param time seconds + */ + public static void set(List nodeList,String key,String value,Integer time) { + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.set(key, value); + if(time!=null){ + jedis.expire(key, time); + } + } catch (Exception e) { + log.warn("redis set exception:"+key+","+value+","+time,e); + } finally { + //返还到连接池 + close(jedis); + } + } + } + + public static void del(List nodeList,String key) { + + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.del(key); + } catch (Exception e) { + log.warn("redis del exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + } + + } + + /*********************k v redis end********************************/ + + + /*********************hash redis start********************************/ + public static void hset(List nodeList,String key, String field, String value) { + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.hset(key, field, value); + } catch (Exception e) { + log.warn("redis hset exception:"+key+","+field+","+value,e); + } finally { + //返还到连接池 + close(jedis); + } + } + } + + public static T hget(RedisNode node,String key, String field,Class clazz) { + + String value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.get(key); + } catch (Exception e) { + log.warn("redis hget exception:"+key+","+field,e); + } finally { + //返还到连接池 + close(jedis); + } + return Jsons.fromJson(value, clazz); + } + + public static void hdel(List nodeList,String key, String field) { + + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.hdel(key, field); + } catch (Exception e) { + log.warn("redis hdel exception:"+key+","+field,e); + } finally { + //返还到连接池 + close(jedis); + } + } + } + + /** + * 存储REDIS队列 顺序存储 + */ + public static void lpush(List nodeList,String key, String value) { + + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.lpush(key, value); + } catch (Exception e) { + log.warn("redis hdel exception:"+key+","+value,e); + } finally { + //返还到连接池 + close(jedis); + } + } + + } + + /** + * 存储REDIS队列 反向存储 + */ + public static void rpush(List nodeList,String key, String value) { + + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.rpush(key, value); + } catch (Exception e) { + log.warn("redis hdel exception:"+key+","+value,e); + } finally { + //返还到连接池 + close(jedis); + } + } + } + + /** + * + */ + public static void rpoplpush(List nodeList,String srcKey, String desKey) { + + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.rpoplpush(srcKey, desKey); + } catch (Exception e) { + log.warn("redis rpoplpush exception:"+srcKey+","+desKey,e); + } finally { + //返还到连接池 + close(jedis); + } + } + + } + + /** + * 获取队列数据 + */ + public static List lpopList(RedisNode node,String key,Class clazz) { + List value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.lrange(key, 0, -1); + } catch (Exception e) { + log.warn("redis lpopList exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + if(value!=null){ + List newValue = Lists.newArrayList(); + for(String temp:value){ + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + } + + /** + * 获取队列数据 + */ + public static T rpop(RedisNode node,String key,Class clazz) { + + Jedis jedis = null; + String vaule = null; + try { + jedis = getClient(node); + vaule = jedis.rpop(key); + } catch (Exception e) { + log.warn("redis rpop exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + return Jsons.fromJson(vaule, clazz); + } + + public static void hmset(List nodeList,String key, Map hash,Integer time) { + + for(RedisNode node:nodeList){ + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.hmset(key, hash); + } catch (Exception e) { + log.warn("redis hmset exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + } + + } + + public static void hmset(List nodeList,String key, Map hash, int time) { + hmset(nodeList, key, hash, null); + } + + public static List hmget(RedisNode node ,String key, Class clazz,String... fields) { + + List value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.hmget(key.toString(), fields); + } catch (Exception e) { + log.warn("redis lpopList exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + if(value!=null){ + List newValue = Lists.newArrayList(); + for(String temp:value){ + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + + } + + public static Set hkeys(RedisNode node,String key) { + Set result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hkeys(key); + } catch (Exception e) { + log.warn("redis hkeys exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + + } + return result; + } + + public static List lrange(RedisNode node,String key, Class clazz ,int from, int to) { + List result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.lrange(key, from, to); + } catch (Exception e) { + log.warn("redis lrange exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + + } + if(result!=null){ + List newValue = Lists.newArrayList(); + for(String temp:result){ + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + } + + public static Map hgetAll(RedisNode node,String key) { + Map result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hgetAll(key); + } catch (Exception e) { + log.warn("redis hgetAll exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + return result; + } + + + + public static long llen(RedisNode node ,String key) { + + long len = 0; + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.llen(key); + } catch (Exception e) { + log.warn("redis llen exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + return len; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java new file mode 100644 index 00000000..f5ced20d --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java @@ -0,0 +1,66 @@ +package com.shinemo.mpush.tools.redis.consistenthash; + +import java.util.Collection; +import java.util.SortedMap; +import java.util.TreeMap; + +import redis.clients.util.Hashing; + +public class ConsistentHash { + + private final Hashing hash; + private final int numberOfReplicas; + private final SortedMap circle = new TreeMap(); + + public ConsistentHash(Hashing hash, int numberOfReplicas, + Collection nodes) { + super(); + this.hash = hash; + this.numberOfReplicas = numberOfReplicas; + for (Node node : nodes) { + add(node); + } + } + + /** + * 增加真实机器节点 + * + * @param node + */ + public void add(Node node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.put(this.hash.hash(node.toString() + i), node); + } + } + + /** + * 删除真实机器节点 + * + * @param node + */ + public void remove(String node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.remove(this.hash.hash(node.toString() + i)); + } + } + + /** + * 取得真实机器节点 + * + * @param key + * @return + */ + public Node get(String key) { + if (circle.isEmpty()) { + return null; + } + long hash = this.hash.hash(key); + if (!circle.containsKey(hash)) { + SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 + hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); + } + return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java new file mode 100644 index 00000000..0c2a7616 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.tools.redis.consistenthash; + +public class Node { + + private String ip; //机器ip + private String name;//名字 + + public Node(String ip, String name) { + this.ip = ip; + this.name = name; + } + + public String getIp() { + return ip; + } + public void setIp(String ip) { + this.ip = ip; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java new file mode 100644 index 00000000..5ee85339 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -0,0 +1,83 @@ +package com.shinemo.mpush.tools.zk.listener.impl; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.listener.CallBack; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +/** + *注册的应用的发生变化 + * + */ +public class RedisPathListener implements CallBack{ + + private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); + + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + String data = ""; + if(event.getData()!=null){ + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data); + } + } + + @Override + public void initData(ServerManage manage) { + log.warn("start init app data"); + _initData(); + log.warn("end init app data"); + } + + private void _initData(){ + //获取机器列表 + List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + for(String raw:rawData){ + String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw); + ServerApp app = getServerApp(fullPath); + ServerAppManage.instance.addOrUpdate(fullPath, app); + } + } + + private void dataRemove(ChildData data){ + String path = data.getPath(); + ServerAppManage.instance.remove(path); + } + + private void dataAddOrUpdate(ChildData data){ + String path = data.getPath(); + byte[] rawData = data.getData(); + ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); + ServerAppManage.instance.addOrUpdate(path, serverApp); + } + + private ServerApp getServerApp(String fullPath){ + String rawApp = ZkUtil.instance.get(fullPath); + ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); + return app; + } + + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java new file mode 100644 index 00000000..3a02124a --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java @@ -0,0 +1,52 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.junit.Test; + +import redis.clients.util.Hashing; +import redis.clients.util.MurmurHash; + +import com.shinemo.mpush.tools.redis.consistenthash.ConsistentHash; +import com.shinemo.mpush.tools.redis.consistenthash.Node; + +public class ConsistentHashTest { + + private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 + + @Test + public void test(){ + Map map = new HashMap();// 每台真实机器节点上保存的记录条数 + List nodes = new ArrayList();// 真实机器节点 + // 10台真实机器节点集群 + for (int i = 1; i <= 10; i++) { + map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 + Node node = new Node(IP_PREFIX + i, "node" + i); + nodes.add(node); + } + Hashing hashFunction = new MurmurHash(); // hash函数实例 + ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 + // 将5000条记录尽可能均匀的存储到10台机器节点 + for (int i = 0; i < 5000; i++) { + // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 + String data = UUID.randomUUID().toString() + i; + // 通过记录找到真实机器节点 + Node node = consistentHash.get(data); + // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 + // 每台真实机器节点上保存的记录条数加1 + map.put(node.getIp(), map.get(node.getIp()) + 1); + } + // 打印每台真实机器节点保存的记录条数 + for (int i = 1; i <= 10; i++) { + System.out.println(IP_PREFIX + i + "节点记录条数:" + + map.get("192.168.1." + i)); + } + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java new file mode 100644 index 00000000..42f5237b --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java @@ -0,0 +1,137 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; + +import com.google.common.collect.Lists; + +import redis.clients.jedis.Jedis; + +public class RedisUtilTest { + + RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); + RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + + List nodeList = Lists.newArrayList(node,node2); + + @Test + public void testAddAndGetAndDelete(){ + Jedis jedis = RedisUtil.getClient(node2); + jedis.set("hi", "huang"); + + String ret = jedis.get("hi"); + System.out.println(ret); + + jedis.del("hi"); + ret = jedis.get("hi"); + if(ret==null){ + System.out.println("ret is null"); + }else{ + System.out.println("ret is not null:"+ret); + } + + } + + @Test + public void testJedisPool(){ + //最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for(int i = 0;i<10;i++){ + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + } + } + + @Test + public void testJedisPool2(){ + //最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for(int i = 1;i<=8;i++){ + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + } + + System.out.println(jedisList.size()); + + try{ + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + System.out.println("first get jedis success"); + }catch(Exception e){ + System.out.println(e); + } + + //关闭一个链接 + RedisUtil.close(jedisList.get(0)); + + try{ + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + System.out.println("second get jedis success"); + }catch(Exception e){ + System.out.println(e); + } + + System.out.println(jedisList.size()); + } + + + @Test + public void testKV(){ + User user = new User("huang", 18, new Date()); + RedisUtil.set(nodeList, "test", user); + + User nowUser = RedisUtil.get(node, "test", User.class); + System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.get(node2, "test", User.class); + System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + + RedisUtil.del(nodeList, "test"); + + nowUser = RedisUtil.get(node2, "test", User.class); + if(nowUser==null){ + System.out.println("node2 nowUser is null"); + }else{ + System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + } + + + nowUser = RedisUtil.get(node, "test", User.class); + if(nowUser==null){ + System.out.println("node nowUser is null"); + }else{ + System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); + } + + RedisUtil.set(nodeList, "test", user,10); + + try { + Thread.sleep(12000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + nowUser = RedisUtil.get(node2, "test", User.class); + if(nowUser==null){ + System.out.println("node2 nowUser is null"); + }else{ + System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + } + + + nowUser = RedisUtil.get(node, "test", User.class); + if(nowUser==null){ + System.out.println("node nowUser is null"); + }else{ + System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); + } + + } + + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java new file mode 100644 index 00000000..1156dffa --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.tools.redis; + +import java.io.Serializable; +import java.util.Date; + +public class User implements Serializable{ + + private static final long serialVersionUID = -6269129553435313118L; + + private String name; + private int age; + private Date date; + + public User(String name, int age, Date date) { + this.name = name; + this.age = age; + this.date = date; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getAge() { + return age; + } + public void setAge(int age) { + this.age = age; + } + public Date getDate() { + return date; + } + public void setDate(Date date) { + this.date = date; + } + + +} diff --git a/pom.xml b/pom.xml index 88894b18..b42828b3 100644 --- a/pom.xml +++ b/pom.xml @@ -140,12 +140,7 @@ redis.clients jedis - 2.7.2 - - - org.springframework.data - spring-data-redis - 1.6.1.RELEASE + 2.8.0 org.apache.commons From b4bf630dd9786279e2fa2cab6cf8ee778f9e0482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 5 Jan 2016 09:51:23 +0800 Subject: [PATCH 088/890] redis util end --- .../shinemo/mpush/tools/redis/RedisUtil.java | 320 +++++++++++------- .../mpush/tools/redis/RedisUtilTest.java | 145 +++++--- 2 files changed, 282 insertions(+), 183 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 3d2d6cb3..bc7eae44 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.tools.redis; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -121,14 +122,14 @@ public static void del(List nodeList,String key) { /*********************hash redis start********************************/ - public static void hset(List nodeList,String key, String field, String value) { + public static void hset(List nodeList,String namespace, String key, String value) { for(RedisNode node:nodeList){ Jedis jedis = null; try { jedis = getClient(node); - jedis.hset(key, field, value); + jedis.hset(namespace, key, value); } catch (Exception e) { - log.warn("redis hset exception:"+key+","+field+","+value,e); + log.warn("redis hset exception:"+namespace+","+key+","+value,e); } finally { //返还到连接池 close(jedis); @@ -136,31 +137,36 @@ public static void hset(List nodeList,String key, String field, Strin } } - public static T hget(RedisNode node,String key, String field,Class clazz) { + public static void hset(List nodeList,String namespace, String key, T value) { + hset(nodeList, namespace, key, Jsons.toJson(value)); + } + + public static T hget(RedisNode node,String namespace, String key,Class clazz) { String value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.get(key); + value = jedis.hget(namespace, key); } catch (Exception e) { - log.warn("redis hget exception:"+key+","+field,e); + log.warn("redis hget exception:"+namespace+","+key,e); } finally { //返还到连接池 close(jedis); } return Jsons.fromJson(value, clazz); + } - public static void hdel(List nodeList,String key, String field) { + public static void hdel(List nodeList,String namespace, String key) { for(RedisNode node:nodeList){ Jedis jedis = null; try { jedis = getClient(node); - jedis.hdel(key, field); + jedis.hdel(namespace, key); } catch (Exception e) { - log.warn("redis hdel exception:"+key+","+field,e); + log.warn("redis hdel exception:"+namespace+","+key,e); } finally { //返还到连接池 close(jedis); @@ -168,18 +174,119 @@ public static void hdel(List nodeList,String key, String field) { } } + public static Map hgetAll(RedisNode node,String namespace) { + Map result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hgetAll(namespace); + } catch (Exception e) { + log.warn("redis hgetAll exception:"+namespace,e); + } finally { + //返还到连接池 + close(jedis); + } + return result; + } + + public static Map hgetAll(RedisNode node,String namespace,Class clazz) { + Map result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hgetAll(namespace); + } catch (Exception e) { + log.warn("redis hgetAll exception:"+namespace,e); + } finally { + //返还到连接池 + close(jedis); + } + if(result!=null){ + Map newMap = Maps.newHashMap(); + Iterator> iterator = result.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + String val = entry.getValue(); + newMap.put(key, Jsons.fromJson(val, clazz)); + } + return newMap; + }else{ + return null; + } + + } + /** - * 存储REDIS队列 顺序存储 + * 返回 key 指定的哈希集中所有字段的名字。 + * @param node + * @param key + * @return */ - public static void lpush(List nodeList,String key, String value) { + public static Set hkeys(RedisNode node,String key) { + Set result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hkeys(key); + } catch (Exception e) { + log.warn("redis hkeys exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); + } + return result; + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * @param node + * @param key + * @param clazz + * @param fields + * @return + */ + public static List hmget(RedisNode node ,String namespace, Class clazz,String... key) { + + List value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.hmget(namespace, key); + } catch (Exception e) { + log.warn("redis lpopList exception:"+namespace+","+key,e); + } finally { + //返还到连接池 + close(jedis); + } + if(value!=null){ + List newValue = Lists.newArrayList(); + for(String temp:value){ + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * @param nodeList + * @param key + * @param hash + * @param time + */ + public static void hmset(List nodeList,String namespace, Map hash,Integer time) { + for(RedisNode node:nodeList){ Jedis jedis = null; try { jedis = getClient(node); - jedis.lpush(key, value); + jedis.hmset(namespace, hash); } catch (Exception e) { - log.warn("redis hdel exception:"+key+","+value,e); + log.warn("redis hmset exception:"+namespace,e); } finally { //返还到连接池 close(jedis); @@ -187,120 +294,126 @@ public static void lpush(List nodeList,String key, String value) { } } + + public static void hmset(List nodeList,String key, Map hash, int time) { + hmset(nodeList, key, hash, null); + } + + /*********************hash redis end********************************/ + + + /*********************list redis start********************************/ /** - * 存储REDIS队列 反向存储 + * 从队列的左边入队 */ - public static void rpush(List nodeList,String key, String value) { + public static void lpush(List nodeList,String key, String value) { for(RedisNode node:nodeList){ Jedis jedis = null; try { jedis = getClient(node); - jedis.rpush(key, value); + jedis.lpush(key, value); } catch (Exception e) { - log.warn("redis hdel exception:"+key+","+value,e); + log.warn("redis lpush exception:"+key+","+value,e); } finally { //返还到连接池 close(jedis); } } + + } + + public static void lpush(List nodeList,String key, T value) { + + lpush(nodeList, key, Jsons.toJson(value)); + } /** - * + * 从队列的右边入队 */ - public static void rpoplpush(List nodeList,String srcKey, String desKey) { + public static void rpush(List nodeList,String key, String value) { for(RedisNode node:nodeList){ Jedis jedis = null; try { jedis = getClient(node); - jedis.rpoplpush(srcKey, desKey); + jedis.rpush(key, value); } catch (Exception e) { - log.warn("redis rpoplpush exception:"+srcKey+","+desKey,e); + log.warn("redis rpush exception:"+key+","+value,e); } finally { //返还到连接池 close(jedis); } } - } - + + public static void rpush(List nodeList,String key, T value) { + rpush(nodeList, key, Jsons.toJson(value)); + } + /** - * 获取队列数据 + * 移除并且返回 key 对应的 list 的第一个元素 */ - public static List lpopList(RedisNode node,String key,Class clazz) { - List value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.lrange(key, 0, -1); - } catch (Exception e) { - log.warn("redis lpopList exception:"+key,e); - } finally { - //返还到连接池 - close(jedis); - } - if(value!=null){ - List newValue = Lists.newArrayList(); - for(String temp:value){ - newValue.add(Jsons.fromJson(temp, clazz)); + public static T lpop(List nodeList,String key,Class clazz) { + String retValue = null; + for(RedisNode node:nodeList){ + Jedis jedis = null; + String vaule = null; + try { + jedis = getClient(node); + vaule = jedis.lpop(key); + retValue = vaule; + } catch (Exception e) { + log.warn("redis lpop exception:"+key,e); + } finally { + //返还到连接池 + close(jedis); } - return newValue; } - return null; + + return Jsons.fromJson(retValue, clazz); } - + /** - * 获取队列数据 + * 从队列的右边出队一个元素 */ - public static T rpop(RedisNode node,String key,Class clazz) { - - Jedis jedis = null; - String vaule = null; - try { - jedis = getClient(node); - vaule = jedis.rpop(key); - } catch (Exception e) { - log.warn("redis rpop exception:"+key,e); - } finally { - //返还到连接池 - close(jedis); - } - return Jsons.fromJson(vaule, clazz); - } - - public static void hmset(List nodeList,String key, Map hash,Integer time) { - + public static T rpop(List nodeList,String key,Class clazz) { + String retValue = null; for(RedisNode node:nodeList){ Jedis jedis = null; + String vaule = null; try { jedis = getClient(node); - jedis.hmset(key, hash); + vaule = jedis.rpop(key); + retValue = vaule; } catch (Exception e) { - log.warn("redis hmset exception:"+key,e); + log.warn("redis lpop exception:"+key,e); } finally { //返还到连接池 close(jedis); } } + return Jsons.fromJson(retValue, clazz); } + + - public static void hmset(List nodeList,String key, Map hash, int time) { - hmset(nodeList, key, hash, null); - } - - public static List hmget(RedisNode node ,String key, Class clazz,String... fields) { - + /** + * 从列表中获取指定返回的元素 + * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public static List lrange(RedisNode node,String key,int start,int end,Class clazz) { List value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hmget(key.toString(), fields); + value = jedis.lrange(key, start, end); } catch (Exception e) { - log.warn("redis lpopList exception:"+key,e); + log.warn("redis lrange exception:"+key,e); } finally { //返还到连接池 close(jedis); @@ -313,65 +426,11 @@ public static List hmget(RedisNode node ,String key, Class clazz,Stri return newValue; } return null; - } - - public static Set hkeys(RedisNode node,String key) { - Set result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hkeys(key); - } catch (Exception e) { - log.warn("redis hkeys exception:"+key,e); - } finally { - //返还到连接池 - close(jedis); - - } - return result; - } - - public static List lrange(RedisNode node,String key, Class clazz ,int from, int to) { - List result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.lrange(key, from, to); - } catch (Exception e) { - log.warn("redis lrange exception:"+key,e); - } finally { - //返还到连接池 - close(jedis); - - } - if(result!=null){ - List newValue = Lists.newArrayList(); - for(String temp:result){ - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - } - - public static Map hgetAll(RedisNode node,String key) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(key); - } catch (Exception e) { - log.warn("redis hgetAll exception:"+key,e); - } finally { - //返还到连接池 - close(jedis); - } - return result; - } - - + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + */ public static long llen(RedisNode node ,String key) { long len = 0; @@ -387,5 +446,8 @@ public static long llen(RedisNode node ,String key) { } return len; } + + /*********************list redis end********************************/ + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java index 42f5237b..33602fb4 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java @@ -1,7 +1,9 @@ package com.shinemo.mpush.tools.redis; import java.util.Date; +import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.commons.lang3.builder.ToStringBuilder; import org.junit.Test; @@ -14,117 +16,153 @@ public class RedisUtilTest { RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - - List nodeList = Lists.newArrayList(node,node2); - + + List nodeList = Lists.newArrayList(node, node2); + @Test - public void testAddAndGetAndDelete(){ + public void testAddAndGetAndDelete() { Jedis jedis = RedisUtil.getClient(node2); jedis.set("hi", "huang"); - + String ret = jedis.get("hi"); System.out.println(ret); - + jedis.del("hi"); ret = jedis.get("hi"); - if(ret==null){ + if (ret == null) { System.out.println("ret is null"); - }else{ - System.out.println("ret is not null:"+ret); + } else { + System.out.println("ret is not null:" + ret); } - + } - + @Test - public void testJedisPool(){ - //最大连接数是8,因此,获取10个链接会抛错误 + public void testJedisPool() { + // 最大连接数是8,因此,获取10个链接会抛错误 List jedisList = Lists.newArrayList(); - for(int i = 0;i<10;i++){ + for (int i = 0; i < 10; i++) { Jedis jedis = RedisUtil.getClient(node); jedisList.add(jedis); } } - + @Test - public void testJedisPool2(){ - //最大连接数是8,因此,获取10个链接会抛错误 + public void testJedisPool2() { + // 最大连接数是8,因此,获取10个链接会抛错误 List jedisList = Lists.newArrayList(); - for(int i = 1;i<=8;i++){ + for (int i = 1; i <= 8; i++) { Jedis jedis = RedisUtil.getClient(node); jedisList.add(jedis); } - + System.out.println(jedisList.size()); - - try{ + + try { Jedis jedis = RedisUtil.getClient(node); jedisList.add(jedis); System.out.println("first get jedis success"); - }catch(Exception e){ + } catch (Exception e) { System.out.println(e); } - - //关闭一个链接 + + // 关闭一个链接 RedisUtil.close(jedisList.get(0)); - - try{ + + try { Jedis jedis = RedisUtil.getClient(node); jedisList.add(jedis); System.out.println("second get jedis success"); - }catch(Exception e){ + } catch (Exception e) { System.out.println(e); } - + System.out.println(jedisList.size()); } - - + @Test - public void testKV(){ + public void testKV() { User user = new User("huang", 18, new Date()); RedisUtil.set(nodeList, "test", user); - + User nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); - + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + nowUser = RedisUtil.get(node2, "test", User.class); - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + RedisUtil.del(nodeList, "test"); - + nowUser = RedisUtil.get(node2, "test", User.class); - if(nowUser==null){ + if (nowUser == null) { System.out.println("node2 nowUser is null"); - }else{ - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); } - - + nowUser = RedisUtil.get(node, "test", User.class); - if(nowUser==null){ + if (nowUser == null) { System.out.println("node nowUser is null"); - }else{ - System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); } - - RedisUtil.set(nodeList, "test", user,10); - + + RedisUtil.set(nodeList, "test", user, 10); + try { Thread.sleep(12000); } catch (InterruptedException e) { e.printStackTrace(); } - + nowUser = RedisUtil.get(node2, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void hashTest(){ + + User user = new User("huang", 18, new Date()); + + RedisUtil.hset(nodeList, "hashhuang", "hi", user); + + User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); + System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); + System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + + Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); + Iterator> iterator = ret.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + User val = entry.getValue(); + System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); + } + + RedisUtil.hdel(nodeList, "hashhuang", "hi"); + + nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); if(nowUser==null){ System.out.println("node2 nowUser is null"); }else{ System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); } - - nowUser = RedisUtil.get(node, "test", User.class); + nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); if(nowUser==null){ System.out.println("node nowUser is null"); }else{ @@ -132,6 +170,5 @@ public void testKV(){ } } - - + } From a9c140a724ee6bcb71490bebcb5076b247d3d27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 5 Jan 2016 11:11:44 +0800 Subject: [PATCH 089/890] =?UTF-8?q?redis=20=E4=B8=8Ezk=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/tools/Jsons.java | 139 ++++++++++-------- .../shinemo/mpush/tools/redis/RedisGroup.java | 31 +++- .../mpush/tools/redis/RedisGroupManage.java | 11 +- .../com/shinemo/mpush/tools/zk/PathEnum.java | 10 ++ .../tools/zk/listener/ListenerDispatcher.java | 5 +- .../zk/listener/impl/RedisPathListener.java | 35 ++--- .../zk/manage/KickProviderQueueManage.java | 40 ----- .../tools/redis/RedisGroupManageTest.java | 37 +++++ .../mpush/tools/redis/RedisUtilTest.java | 3 + .../shinemo/mpush/tools/zk/ZkUtilTest.java | 36 +++++ 10 files changed, 212 insertions(+), 135 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index b4e01333..9fe7f58a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -1,90 +1,99 @@ package com.shinemo.mpush.tools; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Type; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.Map; /** * Created by xiaoxu.yxx on 15/8/7. */ public final class Jsons { - private static final Logger LOGGER = LoggerFactory.getLogger(Jsons.class); - public static final Gson GSON = new GsonBuilder().create(); + private static final Logger LOGGER = LoggerFactory.getLogger(Jsons.class); + public static final Gson GSON = new GsonBuilder().create(); + + public static String toJson(Object bean) { + + try { + return GSON.toJson(bean); + } catch (Exception e) { + LOGGER.error("Jsons.toJson ex, bean=" + bean, e); + } + return null; + } + public static T fromJson(String json, Class clazz) { - public static String toJson(Object bean) { - - try { - return GSON.toJson(bean); - } catch (Exception e) { - LOGGER.error("Jsons.toJson ex, bean=" + bean, e); - } - return null; - } - - public static T fromJson(String json, Class clazz) { - - try { - return GSON.fromJson(json, clazz); - } catch (Exception e) { - LOGGER.error("Jsons.fromJson ex, json=" + json + ", clazz=" + clazz, e); - } - return null; - } - - public static T fromJson(byte[] json, Class clazz) { - return fromJson(new String(json, Constants.UTF_8), clazz); - } + try { + return GSON.fromJson(json, clazz); + } catch (Exception e) { + LOGGER.error("Jsons.fromJson ex, json=" + json + ", clazz=" + clazz, e); + } + return null; + } - public static T fromJson(String json, Type type) { - try { - return GSON.fromJson(json, type); - } catch (Exception e) { - LOGGER.error("Jsons.fromJson ex, json=" + json + ", type=" + type, e); - } - return null; - } + public static T fromJson(byte[] json, Class clazz) { + return fromJson(new String(json, Constants.UTF_8), clazz); + } - public static boolean mayJson(String json) { - if (Strings.isBlank(json)) return false; - if (json.charAt(0) == '{' && json.charAt(json.length() - 1) == '}') return true; - if (json.charAt(0) == '[' && json.charAt(json.length() - 1) == ']') return true; - return false; - } + public static List fromJsonToList(String json, Class type) { + T[] list = GSON.fromJson(json, type); + return Arrays.asList(list); + } - public static String toJson(Map map) { - if (map == null || map.isEmpty()) return "{}"; - StringBuilder sb = new StringBuilder(64 * map.size()); - sb.append('{'); - Iterator> it = map.entrySet().iterator(); - if (it.hasNext()) { - append(it.next(), sb); - } - while (it.hasNext()) { - sb.append(','); - append(it.next(), sb); - } - sb.append('}'); - return sb.toString(); - } + public static T fromJson(String json, Type type) { + try { + return GSON.fromJson(json, type); + } catch (Exception e) { + LOGGER.error("Jsons.fromJson ex, json=" + json + ", type=" + type, e); + } + return null; + } + + public static boolean mayJson(String json) { + if (Strings.isBlank(json)) + return false; + if (json.charAt(0) == '{' && json.charAt(json.length() - 1) == '}') + return true; + if (json.charAt(0) == '[' && json.charAt(json.length() - 1) == ']') + return true; + return false; + } + + public static String toJson(Map map) { + if (map == null || map.isEmpty()) + return "{}"; + StringBuilder sb = new StringBuilder(64 * map.size()); + sb.append('{'); + Iterator> it = map.entrySet().iterator(); + if (it.hasNext()) { + append(it.next(), sb); + } + while (it.hasNext()) { + sb.append(','); + append(it.next(), sb); + } + sb.append('}'); + return sb.toString(); + } + + private static void append(Map.Entry entry, StringBuilder sb) { + String key = entry.getKey(), value = entry.getValue(); + if (value == null) + value = Strings.EMPTY; + sb.append('"').append(key).append('"'); + sb.append(':'); + sb.append('"').append(value).append('"'); + } - private static void append(Map.Entry entry, StringBuilder sb) { - String key = entry.getKey(), value = entry.getValue(); - if (value == null) value = Strings.EMPTY; - sb.append('"').append(key).append('"'); - sb.append(':'); - sb.append('"').append(value).append('"'); - } - - public static void main(String[] args) { + public static void main(String[] args) { String test = "test"; String ret = Jsons.toJson(test); String ret2 = Jsons.fromJson(ret, String.class); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java index f55f9014..638a2af1 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java @@ -4,14 +4,41 @@ import com.google.common.collect.Lists; + /** * redis 组 * */ public class RedisGroup { - List redisNodeList = Lists.newArrayList(); + private List redisNodeList; + + public List getRedisNodeList() { + return redisNodeList; + } + + public void setRedisNodeList(List redisNodeList) { + this.redisNodeList = redisNodeList; + } + + public void addRedisNode(RedisNode node){ + if(redisNodeList==null){ + redisNodeList = Lists.newArrayList(); + } + redisNodeList.add(node); + } + + public void remove(int i){ + if(redisNodeList!=null){ + redisNodeList.remove(i); + } + } + + public void clear(){ + if(redisNodeList!=null){ + redisNodeList.clear(); + } + } - } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java index 16e390d0..f028a664 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java @@ -8,10 +8,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.Jedis; import com.google.common.collect.Lists; -import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; public class RedisGroupManage { @@ -27,16 +25,17 @@ public class RedisGroupManage { private RedisGroupManage() { } - public void init(){ - + public void init(List group){ + holder = group; + printGroupList(); } - public List getAppList() { + public List getGroupList() { return Collections.unmodifiableList(holder); } - private void printAppList(){ + private void printGroupList(){ for(RedisGroup app:holder){ log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java index 3a1dfceb..6bb89727 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -13,6 +13,16 @@ public String getPathByName(String name) { return getPath()+"/"+name; } }, + CONNECTION_SERVER_REDIS("/cs/redis","连接服务器redis注册的地方"){ + @Override + public String getPathByIp(String ip) { + return getPath(); + } + @Override + public String getPathByName(String name) { + return getPath()+"/"+name; + } + }, CONNECTION_SERVER_KICK("/cs/%s/kick/con","连接服务器踢人的路径"){ @Override public String getPathByIp(String ip) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index 3b264b57..9063423b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -12,7 +12,7 @@ import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; -import com.shinemo.mpush.tools.zk.listener.impl.KickPathListener; +import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; import com.shinemo.mpush.tools.zk.manage.ServerManage; public class ListenerDispatcher implements CallBack { @@ -22,7 +22,10 @@ public class ListenerDispatcher implements CallBack { private Map holder = Maps.newTreeMap(); public ListenerDispatcher(ServerApp app) { + //所有connection server holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()), new ConnectionPathListener()); + //所有redis + holder.put(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(app.getIp()), new RedisPathListener()); //踢人的目录已经交给队列处理了,这里不需要重复处理 // holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), new KickPathListener()); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index 5ee85339..bc6dc945 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -11,12 +11,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisGroupManage; import com.shinemo.mpush.tools.zk.PathEnum; -import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; import com.shinemo.mpush.tools.zk.manage.ServerManage; /** @@ -46,37 +47,29 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) @Override public void initData(ServerManage manage) { - log.warn("start init app data"); + log.warn("start init redis data"); _initData(); - log.warn("end init app data"); + log.warn("end init redis data"); } private void _initData(){ - //获取机器列表 - List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); - for(String raw:rawData){ - String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw); - ServerApp app = getServerApp(fullPath); - ServerAppManage.instance.addOrUpdate(fullPath, app); - } + //获取redis列表 + List group = getRedisGroup(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); + RedisGroupManage.instance.init(group); } private void dataRemove(ChildData data){ - String path = data.getPath(); - ServerAppManage.instance.remove(path); + _initData(); } private void dataAddOrUpdate(ChildData data){ - String path = data.getPath(); - byte[] rawData = data.getData(); - ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); - ServerAppManage.instance.addOrUpdate(path, serverApp); + _initData(); } - private ServerApp getServerApp(String fullPath){ - String rawApp = ZkUtil.instance.get(fullPath); - ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); - return app; + private List getRedisGroup(String fullPath){ + String rawGroup = ZkUtil.instance.get(fullPath); + List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); + return group; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java deleted file mode 100644 index 98d7208e..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/KickProviderQueueManage.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.shinemo.mpush.tools.zk.manage; - -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.shinemo.mpush.tools.zk.PathEnum; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.queue.Provider; - -public class KickProviderQueueManage { - - private static Map> providers = Maps.newConcurrentMap(); - - private ServerManage serverManage; - - public KickProviderQueueManage(ServerManage serverManage) { - this.serverManage = serverManage; - Iterator iterator = ServerAppManage.instance.getAppList().iterator(); - while (iterator.hasNext()) { - ServerApp app = iterator.next(); - if(!app.getIp().equals(this.serverManage.getServerApp().getIp())){ - Provider provider = new Provider(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), ServerApp.class); - providers.put(app, provider); - } - } - } - - public void start() throws Exception{ - Iterator> iterator = providers.values().iterator(); - while(iterator.hasNext()){ - Provider provider = iterator.next(); - provider.start(); - } - } - - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java new file mode 100644 index 00000000..93e45209 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; +import org.junit.Test; + +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +public class RedisGroupManageTest { + + ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(),"3000"); + ServerManage manage = new ServerManage(app); + List groupList = null; + + @Before + public void init(){ + manage.start(); + groupList = RedisGroupManage.instance.getGroupList(); + } + + @Test + public void testGetRedisGroup(){ + for(RedisGroup group:groupList){ + for(RedisNode node:group.getRedisNodeList()){ + System.out.println(group+ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); + } + + } + } + + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java index 33602fb4..5bfc1443 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java @@ -9,11 +9,14 @@ import org.junit.Test; import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; import redis.clients.jedis.Jedis; public class RedisUtilTest { + + RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 0ddc7a15..62a35773 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -5,9 +5,13 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Test; + +import com.google.common.collect.Lists; import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisNode; public class ZkUtilTest { @@ -106,5 +110,37 @@ public void testAddKickOff(){ } + @Test + public void testAddRedis(){ + + RedisNode node1 = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); + RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + RedisGroup group1 = new RedisGroup(); + group1.addRedisNode(node1); + + RedisGroup group2 = new RedisGroup(); + group2.addRedisNode(node2); + + List groupList = Lists.newArrayList(group1,group2); + + zkUtil.registerPersist(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress()), Jsons.toJson(groupList)); + + + + } + + @Test + public void getRedisTest(){ + String value = zkUtil.get(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); + List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); + for(RedisGroup group:newGroupList){ + for(RedisNode node:group.getRedisNodeList()){ + System.out.println(group+ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); + } + + } + } + } From 777296aac5314daf67d333a564ccdf18a5e946b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 03:19:23 +0000 Subject: [PATCH 090/890] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Constants.java | 6 ++- .../mpush/api/connection/Connection.java | 6 +-- .../api/connection/ConnectionManager.java | 1 + .../core/router/RouterChangeListener.java | 4 +- .../core/server/ServerChannelHandler.java | 9 ++-- .../core/session/ReusableSessionManager.java | 36 +++++----------- .../netty/connection/NettyConnection.java | 18 ++++---- .../connection/NettyConnectionManager.java | 42 +++++++------------ .../com/shinemo/mpush/tools/Constants.java | 2 +- 9 files changed, 51 insertions(+), 73 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 6b13ec26..0718a21a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -27,5 +27,9 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; - int HEARTBEAT_TIME = 1000 * 60 * 4;//5min + int HEARTBEAT_TIME = 1000 * 60 * 1;//5min + /** + * 最大心跳超时次数,大于该次数要断开连接 + */ + int MAX_HB_TIMEOUT_TIMES = 2; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java index 0b40eb24..ca26c63d 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java @@ -29,13 +29,11 @@ public interface Connection { String getId(); - void close(); + ChannelFuture close(); boolean isConnected(); boolean heartbeatTimeout(); - void setLastReadTime(); - - + void updateLastReadTime(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java index dbec895e..3b475ab4 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java @@ -6,6 +6,7 @@ * Created by ohun on 2015/12/30. */ public interface ConnectionManager { + Connection get(Channel channel); void remove(Channel channel); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index c01d3511..519cbba8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -67,9 +67,11 @@ public void kickRemote(String userId, RemoteRouter router) { // TODO: 2016/1/4 receive msg from redis public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { String userId = msg.userId; - LocalRouter router = RouterCenter.INSTANCE.getLocalRouterManager().lookup(userId); + LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); + LocalRouter router = routerManager.lookup(userId); if (router != null) { LOGGER.info("receive kick remote msg, msg={}", msg); + routerManager.unRegister(userId); kickLocal(userId, router); } else { LOGGER.warn("no local router find, kick failure, msg={}", msg); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index f518f03d..8c60e19a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -11,7 +11,6 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.timeout.IdleStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,19 +34,19 @@ public ServerChannelHandler(ConnectionManager connectionManager, PacketReceiver @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); + connection.updateLastReadTime(); receiver.onReceive((Packet) msg, connection); - connection.setLastReadTime(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LOGGER.error(ctx.channel().remoteAddress() + ", exceptionCaught", cause); + LOGGER.error("caught an ex, client={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.warn(ctx.channel().remoteAddress() + ", channelActive"); + LOGGER.warn("a client connect client={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -55,7 +54,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LOGGER.warn(ctx.channel().remoteAddress() + ", channelInactive"); + LOGGER.warn("a client disconnect client={}", ctx.channel()); connectionManager.remove(ctx.channel()); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index ac07764f..7670972f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -2,6 +2,7 @@ import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.tools.crypto.MD5Utils; +import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -12,7 +13,7 @@ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; - private final Map sessionCache = new ConcurrentHashMap(); + private final Map sessionCache = new ConcurrentHashMapV8<>(); public boolean cacheSession(ReusableSession session) { sessionCache.put(session.sessionId, session); @@ -23,30 +24,13 @@ public ReusableSession getSession(String sessionId) { return sessionCache.get(sessionId); } - public ReusableSession genSession(SessionContext info) { - /** - * 先生成key,需要保证半个周期内同一个设备生成的key是相同的 - */ - long partition = System.currentTimeMillis() / (EXPIRE_TIME / 2);//把当前时间按照半个周期划分出一个当前所属于的区域 - StringBuilder sb = new StringBuilder(); - sb.append(info.deviceId).append(partition); - ReusableSession v = new ReusableSession(); - v.sessionContext = info; - v.sessionId = MD5Utils.encrypt(sb.toString()); - /** - * 计算失效时间 - */ - long nowTime = System.currentTimeMillis(); - long willExpire = (nowTime / EXPIRE_TIME + 1) * EXPIRE_TIME;//预计的到下个周期的失效时间 - - //有可能到绝对周期的时间已经非常短了,如果已经非常短的话,再补充一个周期 - int exp; - if (willExpire - nowTime > EXPIRE_TIME / 2) { - exp = (int) (willExpire - nowTime); - } else { - exp = (int) (willExpire - nowTime) + EXPIRE_TIME; - } - v.expireTime = System.currentTimeMillis() + exp;//存储绝对过期时间 - return v; + public ReusableSession genSession(SessionContext context) { + long now = System.currentTimeMillis(); + ReusableSession session = new ReusableSession(); + session.sessionContext = context; + session.sessionId = MD5Utils.encrypt(context.deviceId + now); + session.expireTime = now + EXPIRE_TIME; + return session; } + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 4f464ce2..ea0d3667 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -56,11 +56,15 @@ public ChannelFuture send(Packet packet) { } @Override - public ChannelFuture send(Packet packet, ChannelFutureListener listener) { - if (listener != null) { - return channel.writeAndFlush(packet).addListener(listener).addListener(this); + public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { + if (channel.isActive() && channel.isWritable()) { + if (listener != null) { + return channel.writeAndFlush(packet).addListener(listener).addListener(this); + } else { + return channel.writeAndFlush(packet).addListener(this); + } } else { - return channel.writeAndFlush(packet).addListener(this); + return this.close(); } } @@ -70,9 +74,9 @@ public Channel channel() { } @Override - public void close() { + public ChannelFuture close() { this.status = STATUS_DISCONNECTED; - this.channel.close(); + return this.channel.close(); } @Override @@ -86,7 +90,7 @@ public boolean heartbeatTimeout() { } @Override - public void setLastReadTime() { + public void updateLastReadTime() { lastReadTime = System.currentTimeMillis(); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 3aa2ae69..d7995264 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -27,23 +27,19 @@ */ public final class NettyConnectionManager implements ConnectionManager { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectionManager.class); - private Timer wheelTimer; + //可能会有20w的链接数 + private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); + private Timer wheelTimer; public void init() { + //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s int ticksPerWheel = (int) (Constants.HEARTBEAT_TIME / tickDuration); this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); EventBus.INSTANCE.register(this); } - //可能会有20w的链接数 - private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); - - public Connection get(final String channelId) throws ExecutionException { - return connections.get(channelId); - } - public Connection get(final Channel channel) { return connections.get(channel.id().asLongText()); } @@ -52,26 +48,11 @@ public void add(Connection connection) { connections.putIfAbsent(connection.getId(), connection); } - public void add(Channel channel) { - Connection connection = new NettyConnection(); - connection.init(channel, true); - connections.putIfAbsent(connection.getId(), connection); - } - - public void remove(Connection connection) { - connections.remove(connection.getId()); - } - public void remove(Channel channel) { - connections.remove(channel.id().asLongText()); - } - - public List getConnectionIds() { - return new ArrayList<>(connections.keySet()); - } - - public List getConnections() { - return new ArrayList<>(connections.values()); + Connection connection = connections.remove(channel.id().asLongText()); + if (connection != null) { + connection.close(); + } } @Subscribe @@ -96,13 +77,18 @@ public void startTimeout() { @Override public void run(Timeout timeout) throws Exception { + if (!connection.isConnected()) return; if (connection.heartbeatTimeout()) { - if (++expiredTimes > 5) { + if (++expiredTimes > Constants.MAX_HB_TIMEOUT_TIMES) { connection.close(); + LOGGER.error("connection heartbeat timeout, connection has bean closed"); return; } else { LOGGER.error("connection heartbeat timeout, expiredTimes=" + expiredTimes); } + } else { + expiredTimes = 0; + LOGGER.info("check heartbeat timeout"); } startTimeout(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 6a2124c0..a1b6a0ce 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -7,7 +7,7 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); - int HEARTBEAT_TIME = 1000 * 60 * 5;//5min + int HEARTBEAT_TIME = 1000 * 60 * 1;//5min byte[] EMPTY_BYTES = new byte[0]; String JVM_LOG_PATH = "/opt/"; From b4d56b605eaebe23c84a2291c723bf1dee222359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 04:02:38 +0000 Subject: [PATCH 091/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/api/protocol/Command.java | 28 ++++++++++--------- .../mpush/common/message/BindUserMessage.java | 23 +++++++-------- .../core/handler/FastConnectHandler.java | 2 +- .../mpush/core/handler/HandshakeHandler.java | 8 +++--- .../mpush/core/session/ReusableSession.java | 1 - .../mpush/core/netty/NettyServerTest.java | 10 ++++++- 6 files changed, 41 insertions(+), 31 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index 0775ad97..6024a2e0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -11,19 +11,21 @@ public enum Command { BIND(5), UNBIND(6), FAST_CONNECT(7), - ERROR(8), - OK(9), - API(10), - KICK(11), - GATEWAY_KICK(12), - PUSH(13), - GATEWAY_PUSH(14), - NOTIFICATION(15), - GATEWAY_NOTIFICATION(16), - CHAT(17), - GATEWAY_CHAT(18), - GROUP(19), - GATEWAY_GROUP(20), + PAUSE(8), + RESUME(9), + ERROR(10), + OK(11), + API(12), + KICK(13), + GATEWAY_KICK(14), + PUSH(15), + GATEWAY_PUSH(16), + NOTIFICATION(17), + GATEWAY_NOTIFICATION(18), + CHAT(19), + GATEWAY_CHAT(20), + GROUP(21), + GATEWAY_GROUP(22), UNKNOWN(-1); Command(int cmd) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java index 5f28d3e0..b510db9f 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java @@ -1,16 +1,17 @@ package com.shinemo.mpush.common.message; -import com.google.common.base.Strings; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; /** * Created by ohun on 2015/12/28. */ -public final class BindUserMessage extends BaseMessage { +public final class BindUserMessage extends ByteBufMessage { public String userId; + public String alias; + public String tags; public BindUserMessage(Connection connection) { super(new Packet(Command.BIND.cmd, genSessionId()), connection); @@ -21,16 +22,16 @@ public BindUserMessage(Packet message, Connection connection) { } @Override - public void decode(byte[] body) { - if (body != null && body.length > 0) { - userId = new String(body, Constants.UTF_8); - } + public void decode(ByteBuf body) { + userId = decodeString(body); + alias = decodeString(body); + tags = decodeString(body); } @Override - public byte[] encode() { - return Strings.isNullOrEmpty(userId) - ? null : - userId.getBytes(Constants.UTF_8); + public void encode(ByteBuf body) { + encodeString(body, userId); + encodeString(body, alias); + encodeString(body, tags); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index cfa32bed..e7897109 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -25,7 +25,7 @@ public FastConnectMessage decode(Packet packet, Connection connection) { public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); if (session == null) { - ErrorMessage.from(message).setReason("token expire").close(); + ErrorMessage.from(message).setReason("session expire").close(); } else if (!session.sessionContext.deviceId.equals(message.deviceId)) { ErrorMessage.from(message).setReason("error device").close(); } else { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 616a2926..12a0ff4c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -32,10 +32,10 @@ public HandshakeMessage decode(Packet packet, Connection connection) { @Override public void handle(HandshakeMessage message) { - byte[] iv = message.iv;//AES密钥向量16 - byte[] clientKey = message.clientKey;//客户端随机数 - byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数 - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥 + byte[] iv = message.iv;//AES密钥向量16位 + byte[] clientKey = message.clientKey;//客户端随机数16位 + byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数16位 + byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥16位 //1.校验客户端消息字段 if (Strings.isNullOrEmpty(message.deviceId) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java index 0acefa44..3c5401a0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java @@ -9,5 +9,4 @@ public final class ReusableSession { public String sessionId; public long expireTime; public SessionContext sessionContext; - } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 1fd2c535..95cab7f4 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -2,6 +2,7 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; +import com.shinemo.mpush.core.handler.FastConnectHandler; import com.shinemo.mpush.core.server.ServerChannelHandler; import com.shinemo.mpush.core.handler.BindUserHandler; import com.shinemo.mpush.core.handler.HandshakeHandler; @@ -10,6 +11,7 @@ import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; +import com.shinemo.mpush.tools.Jsons; import io.netty.channel.ChannelHandler; import org.junit.Test; @@ -18,9 +20,14 @@ * Created by ohun on 2015/12/24. */ public class NettyServerTest { + byte[] bytes = new byte[]{1, 2, 3}; @Test public void testStop() throws Exception { + String json = Jsons.toJson(this); + NettyServerTest test = Jsons.fromJson(json, NettyServerTest.class); + System.out.println(json); + System.out.println(test.bytes.length); } @@ -28,9 +35,10 @@ public void testStop() throws Exception { public void testStart() throws Exception { MessageDispatcher receiver = new MessageDispatcher(); + receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.BIND, new BindUserHandler()); - receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); + receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); connectionManager.init(); ChannelHandler handler = new ServerChannelHandler(connectionManager, receiver); From 08a2b42d9a209acc1072f24c45eaade361a5e4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 5 Jan 2016 12:05:13 +0800 Subject: [PATCH 092/890] redis manage end --- .../com/shinemo/mpush/tools/RandomUtil.java | 20 ++ .../shinemo/mpush/tools/redis/RedisGroup.java | 5 + .../shinemo/mpush/tools/redis/RedisUtil.java | 8 +- .../redis/{ => manage}/RedisGroupManage.java | 35 +++- .../mpush/tools/redis/manage/RedisManage.java | 192 ++++++++++++++++++ .../zk/listener/impl/RedisPathListener.java | 2 +- .../tools/redis/RedisGroupManageTest.java | 2 + 7 files changed, 259 insertions(+), 5 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java rename mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/{ => manage}/RedisGroupManage.java (54%) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java new file mode 100644 index 00000000..4e1e3138 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.tools; + +import java.util.Random; + +public class RandomUtil { + + public static int random(int total){ + Random ran = new Random(); + return ran.nextInt(total); + } + + public static void main(String[] args) { + + for(int i = 0;i<20;i++){ + System.out.println(random(3)); + } + + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java index 638a2af1..b165e27c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java @@ -40,5 +40,10 @@ public void clear(){ } } + public RedisNode get(String key){ + int i = key.hashCode() %redisNodeList.size(); + return redisNodeList.get(i); + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index bc7eae44..51ea5f46 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -285,6 +285,10 @@ public static void hmset(List nodeList,String namespace, Map nodeList,String namespace, Map nodeList,String key, Map hash, int time) { - hmset(nodeList, key, hash, null); + public static void hmset(List nodeList,String namespace, Map hash) { + hmset(nodeList, namespace, hash, null); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java similarity index 54% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java index f028a664..b3c18629 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroupManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java @@ -1,15 +1,18 @@ -package com.shinemo.mpush.tools.redis; +package com.shinemo.mpush.tools.redis.manage; import java.util.Collections; import java.util.List; +import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.RandomUtil; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; public class RedisGroupManage { @@ -41,6 +44,34 @@ private void printGroupList(){ } } + public int groupSize(){ + return holder.size(); + } + + /** + * 随机获取一个redis 实例 + * @param key + * @return + */ + public RedisNode randomGetRedisNode(String key){ + int i = RandomUtil.random(groupSize()); + RedisGroup group = holder.get(i); + return group.get(key); + } + + /** + * 写操作的时候,获取所有redis 实例 + * @param key + * @return + */ + public List hashSet(String key){ + List nodeList = Lists.newArrayList(); + for(RedisGroup group:holder){ + RedisNode node = group.get(key); + nodeList.add(node); + } + return nodeList; + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java new file mode 100644 index 00000000..cc751c87 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -0,0 +1,192 @@ +package com.shinemo.mpush.tools.redis.manage; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.redis.RedisUtil; + + + +public class RedisManage { + + public static T get(String key,Class clazz) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + return RedisUtil.get(node, key, clazz); + } + + public static void set(String key,T value) { + set(key, value, null); + } + + public static void set(String key,T value,Integer time) { + String jsonValue = Jsons.toJson(value); + set(key, jsonValue, time); + } + + /** + * + * @param nodeList + * @param key + * @param value + * @param time seconds + */ + public static void set(String key,String value,Integer time) { + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.set(nodeList, key, value, null); + } + + public static void del(String key) { + + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.del(nodeList, key); + + } + + /*********************k v redis end********************************/ + + + /*********************hash redis start********************************/ + public static void hset(String namespace, String key, String value) { + + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.hset(nodeList, namespace, key, value); + + } + + public static void hset(String namespace, String key, T value) { + hset(namespace, key, Jsons.toJson(value)); + } + + public static T hget(String namespace, String key,Class clazz) { + + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hget(node, namespace, key, clazz); + + } + + public static void hdel(String namespace, String key) { + List nodeList = RedisGroupManage.instance.hashSet(namespace); + RedisUtil.hdel(nodeList, namespace, key); + } + + public static Map hgetAll(String namespace) { + + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hgetAll(node, namespace); + + } + + public static Map hgetAll(String namespace,Class clazz) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hgetAll(node, namespace,clazz); + } + + /** + * 返回 key 指定的哈希集中所有字段的名字。 + * @param node + * @param key + * @return + */ + public static Set hkeys(String namespace) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hkeys(node, namespace); + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * @param node + * @param key + * @param clazz + * @param fields + * @return + */ + public static List hmget(String namespace, Class clazz,String... key) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hmget(node, namespace, clazz, key); + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * @param nodeList + * @param key + * @param hash + * @param time + */ + public static void hmset(String namespace, Map hash,Integer time) { + List nodeList = RedisGroupManage.instance.hashSet(namespace); + RedisUtil.hmset(nodeList, namespace, hash, time); + } + + public static void hmset(String namespace, Map hash) { + hmset(namespace, hash, null); + } + + + /*********************hash redis end********************************/ + + + /*********************list redis start********************************/ + /** + * 从队列的左边入队 + */ + public static void lpush(String key, String value) { + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.lpush(nodeList, key, value); + } + + public static void lpush(String key, T value) { + lpush(key, Jsons.toJson(value)); + } + + /** + * 从队列的右边入队 + */ + public static void rpush(String key, String value) { + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.rpush(nodeList, key, value); + } + + public static void rpush(String key, T value) { + rpush(key, Jsons.toJson(value)); + } + + /** + * 移除并且返回 key 对应的 list 的第一个元素 + */ + public static T lpop(String key,Class clazz) { + List nodeList = RedisGroupManage.instance.hashSet(key); + return RedisUtil.lpop(nodeList, key, clazz); + } + + /** + * 从队列的右边出队一个元素 + */ + public static T rpop(String key,Class clazz) { + List nodeList = RedisGroupManage.instance.hashSet(key); + return RedisUtil.rpop(nodeList, key, clazz); + } + + + + /** + * 从列表中获取指定返回的元素 + * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public static List lrange(String key,int start,int end,Class clazz) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + return RedisUtil.lrange(node, key, start, end, clazz); + } + + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + */ + public static long llen(String key) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + return RedisUtil.llen(node, key); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index bc6dc945..903fa81b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -14,7 +14,7 @@ import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisGroupManage; +import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 93e45209..48113bae 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -8,6 +8,7 @@ import org.junit.Test; import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -33,5 +34,6 @@ public void testGetRedisGroup(){ } } + } From f500883cc90a951562c10eccde800b4b2c12942c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 06:21:42 +0000 Subject: [PATCH 093/890] add config center --- config.properties | 5 ++ .../java/com/shinemo/mpush/api/Constants.java | 21 ----- .../mpush/common/message/BaseMessage.java | 5 +- .../common/message/FastConnectMessage.java | 6 ++ .../mpush/common/security/AesCipher.java | 26 ------- .../mpush/common/security/CipherBox.java | 16 ++-- .../core/handler/FastConnectHandler.java | 10 +-- .../mpush/core/handler/HandshakeHandler.java | 11 ++- .../mpush/core/server/ConnectionServer.java | 30 ++++++- .../mpush/core/server/GatewayServer.java | 22 +++++- .../mpush/core/task/ConnectionScanner.java | 51 ------------ .../com/shinemo/mpush/core/task/ScanTask.java | 13 ---- .../core/netty/ClientChannelHandler.java | 3 +- .../mpush/core/netty/NettyServerTest.java | 44 +---------- .../src/test/resources/config.properties | 5 ++ mpush-core/src/test/resources/logback.xml | 2 +- .../mpush/netty/codec/PacketDecoder.java | 3 +- .../connection/NettyConnectionManager.java | 5 +- .../mpush/netty/server/NettyServer.java | 18 +++-- .../com/shinemo/mpush/tools/ConfigCenter.java | 78 +++++++++++++++++++ .../com/shinemo/mpush/tools/Constants.java | 1 - .../shinemo/mpush/tools/InetAddressUtil.java | 59 +++++++------- .../com/shinemo/mpush/tools/MPushUtil.java | 14 ++-- 23 files changed, 224 insertions(+), 224 deletions(-) create mode 100644 config.properties delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java create mode 100644 mpush-core/src/test/resources/config.properties create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java diff --git a/config.properties b/config.properties new file mode 100644 index 00000000..5523e261 --- /dev/null +++ b/config.properties @@ -0,0 +1,5 @@ +MAX_PACKET_SIZE=10240 +COMPRESS_LIMIT=10240 +MIN_HEARTBEAT=10000 +MAX_HEARTBEAT=1800000 +MAX_HB_TIMEOUT_TIMES=2 \ No newline at end of file diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 0718a21a..e3898b81 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -7,29 +7,8 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); - byte[] EMPTY_BYTES = new byte[0]; - int MAX_PACKET_SIZE = 1024; int HEADER_LEN = 13; - int COMPRESS_LIMIT = 1024 * 10; byte CRYPTO_FLAG = 0x01; byte COMPRESS_FLAG = 0x02; - long TIME_DELAY = 1L; - - String JVM_LOG_PATH = "/opt/"; - - int THREAD_QUEUE_SIZE = 10000; - int MIN_POOL_SIZE = 50; - int MAX_POOL_SIZE = 500; - - int MIN_BOSS_POOL_SIZE = 10; - int MAX_BOSS_POLL_SIZE = 50; - - int MIN_WORK_POOL_SIZE = 10; - int MAX_WORK_POOL_SIZE = 250; - int HEARTBEAT_TIME = 1000 * 60 * 1;//5min - /** - * 最大心跳超时次数,大于该次数要断开连接 - */ - int MAX_HB_TIMEOUT_TIMES = 2; } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index cb64b1a2..8b8c8829 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -1,10 +1,11 @@ package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Message; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.IOUtils; import io.netty.channel.ChannelFutureListener; @@ -51,7 +52,7 @@ protected void encodeBody() { byte[] tmp = encode(); if (tmp != null && tmp.length > 0) { //1.压缩 - if (tmp.length > Constants.COMPRESS_LIMIT) { + if (tmp.length > ConfigCenter.INSTANCE.getCompressLimit()) { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java index a25f54a0..572c76e4 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java @@ -10,6 +10,8 @@ public final class FastConnectMessage extends ByteBufMessage { public String sessionId; public String deviceId; + public int minHeartbeat; + public int maxHeartbeat; public FastConnectMessage(Packet message, Connection connection) { super(message, connection); @@ -19,11 +21,15 @@ public FastConnectMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { sessionId = decodeString(body); deviceId = decodeString(body); + minHeartbeat = decodeInt(body); + maxHeartbeat = decodeInt(body); } @Override public void encode(ByteBuf body) { encodeString(body, sessionId); encodeString(body, deviceId); + encodeInt(body, minHeartbeat); + encodeInt(body, maxHeartbeat); } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index b35213f9..fc18d645 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -1,9 +1,7 @@ package com.shinemo.mpush.common.security; import com.shinemo.mpush.api.connection.Cipher; -import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.crypto.AESUtils; -import com.shinemo.mpush.tools.crypto.Base64Utils; /** * Created by ohun on 2015/12/28. @@ -26,28 +24,4 @@ public byte[] decrypt(byte[] data) { public byte[] encrypt(byte[] data) { return AESUtils.encrypt(data, key, iv); } - - - public static String encodeCipher(AesCipher aesCipher) { - try { - return Base64Utils.encode(aesCipher.key) + " " + Base64Utils.encode(aesCipher.iv); - } catch (Exception e) { - return Strings.EMPTY; - } - } - - public static AesCipher decodeCipher(String keys) { - if (Strings.isBlank(keys)) return null; - String[] array = keys.split(" "); - if (array.length != 2) return null; - try { - byte[] key = Base64Utils.decode(array[0]); - byte[] iv = Base64Utils.decode(array[1]); - if (key.length == CipherBox.AES_KEY_LENGTH && iv.length == CipherBox.AES_KEY_LENGTH) { - return new AesCipher(key, iv); - } - } catch (Exception e) { - } - return null; - } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java index b4be066d..81e1deae 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.common.security; +import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Pair; -import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.RSAUtils; import java.io.File; @@ -15,7 +15,7 @@ * Created by ohun on 2015/12/24. */ public final class CipherBox { - public static final int AES_KEY_LENGTH = 16; + public int aesKeyLength = ConfigCenter.INSTANCE.getAesKeyLength(); public static final CipherBox INSTANCE = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; @@ -91,20 +91,20 @@ public RSAPublicKey getPublicKey() { } public byte[] randomAESKey() { - byte[] bytes = new byte[AES_KEY_LENGTH]; + byte[] bytes = new byte[aesKeyLength]; random.nextBytes(bytes); return bytes; } public byte[] randomAESIV() { - byte[] bytes = new byte[AES_KEY_LENGTH]; + byte[] bytes = new byte[aesKeyLength]; random.nextBytes(bytes); return bytes; } public byte[] mixKey(byte[] clientKey, byte[] serverKey) { - byte[] sessionKey = new byte[AES_KEY_LENGTH]; - for (int i = 0; i < AES_KEY_LENGTH; i++) { + byte[] sessionKey = new byte[aesKeyLength]; + for (int i = 0; i < aesKeyLength; i++) { byte a = clientKey[i]; byte b = serverKey[i]; int sum = Math.abs(a + b); @@ -114,6 +114,10 @@ public byte[] mixKey(byte[] clientKey, byte[] serverKey) { return sessionKey; } + public int getAesKeyLength() { + return aesKeyLength; + } + public RsaCipher getRsaCipher() { return new RsaCipher(privateKey, publicKey); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index e7897109..50b64814 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -1,12 +1,11 @@ package com.shinemo.mpush.core.handler; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.FastConnectMessage; import com.shinemo.mpush.common.message.FastConnectOkMessage; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.core.session.ReusableSession; import com.shinemo.mpush.core.session.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; @@ -29,13 +28,14 @@ public void handle(FastConnectMessage message) { } else if (!session.sessionContext.deviceId.equals(message.deviceId)) { ErrorMessage.from(message).setReason("error device").close(); } else { + int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + session.sessionContext.setHeartbeat(heartbeat); message.getConnection().setSessionContext(session.sessionContext); - FastConnectOkMessage .from(message) .setServerHost(MPushUtil.getLocalIp()) .setServerTime(System.currentTimeMillis()) - .setHeartbeat(Constants.HEARTBEAT_TIME) + .setHeartbeat(heartbeat) .send(); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 12a0ff4c..48ba7954 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -1,16 +1,15 @@ package com.shinemo.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.HandshakeMessage; import com.shinemo.mpush.common.message.HandshakeOkMessage; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.security.AesCipher; import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.core.session.ReusableSession; @@ -39,8 +38,8 @@ public void handle(HandshakeMessage message) { //1.校验客户端消息字段 if (Strings.isNullOrEmpty(message.deviceId) - || iv.length != CipherBox.AES_KEY_LENGTH - || clientKey.length != CipherBox.AES_KEY_LENGTH) { + || iv.length != CipherBox.INSTANCE.getAesKeyLength() + || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); return; } @@ -84,7 +83,7 @@ public void handle(HandshakeMessage message) { .setHeartbeat(heartbeat); //9.触发握手成功事件 - EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), Constants.HEARTBEAT_TIME)); + EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index e9658c0d..7b99277b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -1,5 +1,12 @@ package com.shinemo.mpush.core.server; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.common.MessageDispatcher; +import com.shinemo.mpush.core.handler.BindUserHandler; +import com.shinemo.mpush.core.handler.FastConnectHandler; +import com.shinemo.mpush.core.handler.HandshakeHandler; +import com.shinemo.mpush.core.handler.HeartBeatHandler; +import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; @@ -9,11 +16,25 @@ * Created by ohun on 2015/12/30. */ public final class ConnectionServer extends NettyServer { + private ServerChannelHandler channelHandler; - public ConnectionServer(int port, ChannelHandler channelHandler) { - super(port, channelHandler); + public ConnectionServer(int port) { + super(port); } + @Override + public void init() { + MessageDispatcher receiver = new MessageDispatcher(); + receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); + receiver.register(Command.HANDSHAKE, new HandshakeHandler()); + receiver.register(Command.BIND, new BindUserHandler()); + receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); + NettyConnectionManager connectionManager = new NettyConnectionManager(); + connectionManager.init(); + channelHandler = new ServerChannelHandler(connectionManager, receiver); + } + + @Override protected void initOptions(ServerBootstrap b) { super.initOptions(b); @@ -33,4 +54,9 @@ protected void initOptions(ServerBootstrap b) { b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); } + + @Override + public ChannelHandler getChannelHandler() { + return channelHandler; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java index ba83879a..2b87255a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -1,5 +1,9 @@ package com.shinemo.mpush.core.server; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.common.MessageDispatcher; +import com.shinemo.mpush.core.handler.*; +import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; @@ -8,8 +12,22 @@ */ public final class GatewayServer extends NettyServer { - public GatewayServer(int port, ChannelHandler channelHandler) { - super(port, channelHandler); + private ServerChannelHandler channelHandler; + + public GatewayServer(int port) { + super(port); + } + + @Override + public void init() { + MessageDispatcher receiver = new MessageDispatcher(); + receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); + NettyConnectionManager connectionManager = new NettyConnectionManager(); + channelHandler = new ServerChannelHandler(connectionManager, receiver); } + @Override + public ChannelHandler getChannelHandler() { + return channelHandler; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java deleted file mode 100644 index bbe21c0f..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ConnectionScanner.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.shinemo.mpush.core.task; - -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.netty.util.NettySharedHolder; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * 定时全量扫描connection - */ -public class ConnectionScanner implements TimerTask { - - private static final Logger log = LoggerFactory.getLogger(ConnectionScanner.class); - - private final List taskList = new ArrayList(); - - public ConnectionScanner(final ScanTask... scanTasks) { - if (scanTasks != null) { - for (final ScanTask task : scanTasks) { - this.taskList.add(task); - } - } - } - - @Override - public void run(Timeout timeout) throws Exception { - try { - final long now = System.currentTimeMillis(); - List connections = null; //NettyConnectionManager.INSTANCE.getConnections(); - if (connections != null) { - for (Connection conn : connections) { - for (ScanTask task : this.taskList) { - task.visit(now, conn); - } - } - } - } catch (Exception e) { - log.error("exception on scan", e); - } finally { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, Constants.TIME_DELAY, TimeUnit.SECONDS); - } - } - -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java b/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java deleted file mode 100644 index be53bb92..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/task/ScanTask.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shinemo.mpush.core.task; - -import com.shinemo.mpush.api.connection.Connection; - -public interface ScanTask { - - /** - * @param now 扫描触发的时间点 - * @param conn 当前扫描到的连接 - */ - void visit(long now, Connection conn); - -} diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 3eb7e73d..50cb365e 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory; import java.io.*; +import java.util.Random; import java.util.concurrent.TimeUnit; /** @@ -28,7 +29,7 @@ public class ClientChannelHandler extends ChannelHandlerAdapter { private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); private byte[] iv = CipherBox.INSTANCE.randomAESIV(); private Connection connection = new NettyConnection(); - private String deviceId = "test-device-id-100"; + private String deviceId = "test-device-id-100" + new Random(5).nextInt(); private String userId = "1010"; @Override diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 95cab7f4..ca215578 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,55 +1,19 @@ package com.shinemo.mpush.core.netty; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.core.handler.FastConnectHandler; -import com.shinemo.mpush.core.server.ServerChannelHandler; -import com.shinemo.mpush.core.handler.BindUserHandler; -import com.shinemo.mpush.core.handler.HandshakeHandler; -import com.shinemo.mpush.core.handler.HeartBeatHandler; -import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; - -import com.shinemo.mpush.tools.Jsons; -import io.netty.channel.ChannelHandler; - +import com.shinemo.mpush.tools.ConfigCenter; import org.junit.Test; /** * Created by ohun on 2015/12/24. */ public class NettyServerTest { - byte[] bytes = new byte[]{1, 2, 3}; - - @Test - public void testStop() throws Exception { - String json = Jsons.toJson(this); - NettyServerTest test = Jsons.fromJson(json, NettyServerTest.class); - System.out.println(json); - System.out.println(test.bytes.length); - - } - @Test public void testStart() throws Exception { - - MessageDispatcher receiver = new MessageDispatcher(); - receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); - receiver.register(Command.HANDSHAKE, new HandshakeHandler()); - receiver.register(Command.BIND, new BindUserHandler()); - receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - NettyConnectionManager connectionManager = new NettyConnectionManager(); - connectionManager.init(); - ChannelHandler handler = new ServerChannelHandler(connectionManager, receiver); - - final NettyServer server = new ConnectionServer(3000, handler); + ConfigCenter.INSTANCE.init(); + ConnectionServer server = new ConnectionServer(3000); + server.init(); server.start(); - - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - server.stop(); - } - }); } } \ No newline at end of file diff --git a/mpush-core/src/test/resources/config.properties b/mpush-core/src/test/resources/config.properties new file mode 100644 index 00000000..5523e261 --- /dev/null +++ b/mpush-core/src/test/resources/config.properties @@ -0,0 +1,5 @@ +MAX_PACKET_SIZE=10240 +COMPRESS_LIMIT=10240 +MIN_HEARTBEAT=10000 +MAX_HEARTBEAT=1800000 +MAX_HB_TIMEOUT_TIMES=2 \ No newline at end of file diff --git a/mpush-core/src/test/resources/logback.xml b/mpush-core/src/test/resources/logback.xml index e638aca3..20979b40 100644 --- a/mpush-core/src/test/resources/logback.xml +++ b/mpush-core/src/test/resources/logback.xml @@ -4,7 +4,7 @@ System.out UTF-8 - INFO + DEBUG %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index b832e853..1ade78e8 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.exception.DecodeException; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.ConfigCenter; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -63,7 +64,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { - if (bodyLength > Constants.MAX_PACKET_SIZE) { + if (bodyLength > ConfigCenter.INSTANCE.getMaxPacketSize()) { throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); } body = new byte[bodyLength]; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index d7995264..fcd2bb36 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -7,6 +7,7 @@ import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.tools.ConfigCenter; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; @@ -35,7 +36,7 @@ public final class NettyConnectionManager implements ConnectionManager { public void init() { //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s - int ticksPerWheel = (int) (Constants.HEARTBEAT_TIME / tickDuration); + int ticksPerWheel = (int) (ConfigCenter.INSTANCE.getMaxHeartbeat() / tickDuration); this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); EventBus.INSTANCE.register(this); } @@ -79,7 +80,7 @@ public void startTimeout() { public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) return; if (connection.heartbeatTimeout()) { - if (++expiredTimes > Constants.MAX_HB_TIMEOUT_TIMES) { + if (++expiredTimes > ConfigCenter.INSTANCE.getMaxHBTimeoutTimes()) { connection.close(); LOGGER.error("connection heartbeat timeout, connection has bean closed"); return; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 08158653..4f455fc6 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -22,16 +22,16 @@ public abstract class NettyServer implements Server { private static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class); private final AtomicBoolean startFlag = new AtomicBoolean(false); + private final int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; - private final int port; - private ChannelHandler channelHandler; + private boolean hasInit = false; - public NettyServer(int port, ChannelHandler channelHandler) { + public NettyServer(int port) { this.port = port; - this.channelHandler = channelHandler; } + public abstract void init(); @Override public boolean isRunning() { @@ -51,7 +51,10 @@ public void start() { if (!startFlag.compareAndSet(false, true)) { return; } - + if (!hasInit) { + hasInit = true; + init(); + } /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 @@ -101,7 +104,7 @@ public void start() { public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(channelHandler); + ch.pipeline().addLast(getChannelHandler()); } }); @@ -148,4 +151,7 @@ protected void initOptions(ServerBootstrap b) { b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } + + + public abstract ChannelHandler getChannelHandler(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java new file mode 100644 index 00000000..4e40f79d --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java @@ -0,0 +1,78 @@ +package com.shinemo.mpush.tools; + +import java.io.IOException; +import java.util.Properties; + +/** + * Created by ohun on 2016/1/5. + */ +public final class ConfigCenter { + public static final ConfigCenter INSTANCE = new ConfigCenter(); + private transient Properties cfg = new Properties(); + private int maxPacketSize = 10240;//10k + private int compressLimit = 10240;//10k + private int minHeartbeat = 1000 * 10;//10s + private int maxHeartbeat = 1000 * 60 * 30;//30min + private int maxHBTimeoutTimes = 2; + private int rasKeyLength = 1024; + private int aesKeyLength = 16; + + public void init() throws IOException { + cfg.load(this.getClass().getResourceAsStream("/config.properties")); + maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); + compressLimit = getInt("COMPRESS_LIMIT", compressLimit); + minHeartbeat = getInt("MIN_HEARTBEAT", minHeartbeat); + maxHeartbeat = getInt("MAX_HEARTBEAT", maxHeartbeat); + maxHBTimeoutTimes = getInt("MAX_HB_TIMEOUT_TIMES", maxHBTimeoutTimes); + rasKeyLength = getInt("RAS_KEY_LENGTH", rasKeyLength); + aesKeyLength = getInt("AES_KEY_LENGTH", aesKeyLength); + maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); + } + + public String getString(String key, String defaultValue) { + return cfg.getProperty(key, defaultValue); + } + + public boolean getBoolean(String key, boolean defaultValue) { + String value = cfg.getProperty(key); + return value == null ? defaultValue : Boolean.valueOf(value); + } + + public int getInt(String key, int defaultValue) { + String value = cfg.getProperty(key); + return value == null ? defaultValue : Integer.parseInt(value); + } + + public long getLong(String key, long defaultValue) { + String value = cfg.getProperty(key); + return value == null ? defaultValue : Long.parseLong(value); + } + + public int getMaxPacketSize() { + return maxPacketSize; + } + + public int getCompressLimit() { + return compressLimit; + } + + public int getMinHeartbeat() { + return minHeartbeat; + } + + public int getMaxHeartbeat() { + return maxHeartbeat; + } + + public int getMaxHBTimeoutTimes() { + return maxHBTimeoutTimes; + } + + public int getRasKeyLength() { + return rasKeyLength; + } + + public int getAesKeyLength() { + return aesKeyLength; + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index a1b6a0ce..b833d7a8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -7,7 +7,6 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); - int HEARTBEAT_TIME = 1000 * 60 * 1;//5min byte[] EMPTY_BYTES = new byte[0]; String JVM_LOG_PATH = "/opt/"; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java index 5b1f1273..a4e28058 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java @@ -8,34 +8,35 @@ import org.slf4j.LoggerFactory; public class InetAddressUtil { - - private static final Logger log = LoggerFactory.getLogger(InetAddressUtil.class); - - /** - * 获取本机ip - * 只获取第一块网卡绑定的ip地址 - * @return - */ - public static String getInetAddress(){ - try{ - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; - while(interfaces.hasMoreElements()){ - NetworkInterface ni = interfaces.nextElement(); - Enumeration addresses = ni.getInetAddresses(); - while(addresses.hasMoreElements()){ - address = addresses.nextElement(); - if(!address.isLoopbackAddress()&&address.getHostAddress().indexOf(":")==-1&&address.isSiteLocalAddress()){ - return address.getHostAddress(); - } - } - } - log.warn("[InetAddressUtil] getInetAddress is null"); - return "127.0.0.1"; - }catch(Throwable e){ - log.warn("[InetAddressUtil] getInetAddress exception",e); - return "127.0.0.1"; - } - } + + private static final Logger log = LoggerFactory.getLogger(InetAddressUtil.class); + + /** + * 获取本机ip + * 只获取第一块网卡绑定的ip地址 + * + * @return + */ + public static String getInetAddress() { + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress address = null; + while (interfaces.hasMoreElements()) { + NetworkInterface ni = interfaces.nextElement(); + Enumeration addresses = ni.getInetAddresses(); + while (addresses.hasMoreElements()) { + address = addresses.nextElement(); + if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && address.isSiteLocalAddress()) { + return address.getHostAddress(); + } + } + } + log.warn("[InetAddressUtil] getInetAddress is null"); + return "127.0.0.1"; + } catch (Throwable e) { + log.error("[InetAddressUtil] getInetAddress exception", e); + return "127.0.0.1"; + } + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 36ac1f57..003e7350 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -1,8 +1,5 @@ package com.shinemo.mpush.tools; -import java.net.InetAddress; -import java.net.UnknownHostException; - /** * Created by ohun on 2015/12/25. */ @@ -11,17 +8,16 @@ public final class MPushUtil { public static String getLocalIp() { if (LOCAL_IP == null) { - try { - LOCAL_IP = InetAddress.getLocalHost().getHostAddress(); - } catch (Exception e) { - LOCAL_IP = "127.0.0.1"; - } + LOCAL_IP = InetAddressUtil.getInetAddress(); } return LOCAL_IP; } public static int getHeartbeat(int min, int max) { - return Constants.HEARTBEAT_TIME; + return Math.max( + ConfigCenter.INSTANCE.getMinHeartbeat(), + Math.min(max, ConfigCenter.INSTANCE.getMaxHeartbeat()) + ); } } From 9fb87499a7fd80914a5f9733f0e36b210b713a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 5 Jan 2016 14:41:51 +0800 Subject: [PATCH 094/890] add sub pub redis --- .../shinemo/mpush/tools/redis/RedisUtil.java | 328 ++++++++++-------- .../tools/redis/manage/RedisGroupManage.java | 1 - .../mpush/tools/redis/manage/RedisManage.java | 28 ++ .../mpush/tools/redis/pubsub/Subscriber.java | 62 ++++ .../tools/redis/RedisGroupManageTest.java | 51 +++ 5 files changed, 329 insertions(+), 141 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 51ea5f46..2234e019 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,36 +18,39 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; - +import redis.clients.jedis.JedisPubSub; public class RedisUtil { - + private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); + + private static Map holder = Maps.newConcurrentMap(); - private static Map holder = Maps.newConcurrentMap(); + private static Executor executor = Executors.newFixedThreadPool(10); public static Jedis getClient(RedisNode node) { JedisPool pool = holder.get(node); - if(pool == null){ + if (pool == null) { pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); holder.put(node, pool); } return pool.getResource(); } - - public static void close(Jedis jedis){ + + public static void close(Jedis jedis) { jedis.close(); } - - /*********************k v redis start********************************/ + + /********************* k v redis start ********************************/ /** * - * @param node redis实例 + * @param node + * redis实例 * @param key * @param clazz * @return */ - public static T get(RedisNode node,String key,Class clazz) { + public static T get(RedisNode node, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -53,95 +58,95 @@ public static T get(RedisNode node,String key,Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - log.warn("redis get exception:"+key,e); + log.warn("redis get exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } return Jsons.fromJson(value, clazz); } - - public static void set(List nodeList,String key,String value) { + + public static void set(List nodeList, String key, String value) { set(nodeList, key, value, null); - + } - - public static void set(List nodeList,String key,T value) { + + public static void set(List nodeList, String key, T value) { set(nodeList, key, value, null); } - - public static void set(List nodeList,String key,T value,Integer time) { + + public static void set(List nodeList, String key, T value, Integer time) { String jsonValue = Jsons.toJson(value); set(nodeList, key, jsonValue, time); } - + /** * * @param nodeList * @param key * @param value - * @param time seconds + * @param time + * seconds */ - public static void set(List nodeList,String key,String value,Integer time) { - for(RedisNode node:nodeList){ + public static void set(List nodeList, String key, String value, Integer time) { + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.set(key, value); - if(time!=null){ + if (time != null) { jedis.expire(key, time); } } catch (Exception e) { - log.warn("redis set exception:"+key+","+value+","+time,e); + log.warn("redis set exception:" + key + "," + value + "," + time, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } } - - public static void del(List nodeList,String key) { - for(RedisNode node:nodeList){ + public static void del(List nodeList, String key) { + + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - log.warn("redis del exception:"+key,e); + log.warn("redis del exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } - + } - - /*********************k v redis end********************************/ - - - /*********************hash redis start********************************/ - public static void hset(List nodeList,String namespace, String key, String value) { - for(RedisNode node:nodeList){ + + /********************* k v redis end ********************************/ + + /********************* hash redis start ********************************/ + public static void hset(List nodeList, String namespace, String key, String value) { + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.hset(namespace, key, value); } catch (Exception e) { - log.warn("redis hset exception:"+namespace+","+key+","+value,e); + log.warn("redis hset exception:" + namespace + "," + key + "," + value, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } } - - public static void hset(List nodeList,String namespace, String key, T value) { + + public static void hset(List nodeList, String namespace, String key, T value) { hset(nodeList, namespace, key, Jsons.toJson(value)); } - - public static T hget(RedisNode node,String namespace, String key,Class clazz) { + + public static T hget(RedisNode node, String namespace, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -149,220 +154,222 @@ public static T hget(RedisNode node,String namespace, String key,Class cl jedis = getClient(node); value = jedis.hget(namespace, key); } catch (Exception e) { - log.warn("redis hget exception:"+namespace+","+key,e); + log.warn("redis hget exception:" + namespace + "," + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } return Jsons.fromJson(value, clazz); - + } - - public static void hdel(List nodeList,String namespace, String key) { - for(RedisNode node:nodeList){ + public static void hdel(List nodeList, String namespace, String key) { + + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.hdel(namespace, key); } catch (Exception e) { - log.warn("redis hdel exception:"+namespace+","+key,e); + log.warn("redis hdel exception:" + namespace + "," + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } } - - public static Map hgetAll(RedisNode node,String namespace) { + + public static Map hgetAll(RedisNode node, String namespace) { Map result = null; Jedis jedis = null; try { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - log.warn("redis hgetAll exception:"+namespace,e); + log.warn("redis hgetAll exception:" + namespace, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } return result; } - - public static Map hgetAll(RedisNode node,String namespace,Class clazz) { + + public static Map hgetAll(RedisNode node, String namespace, Class clazz) { Map result = null; Jedis jedis = null; try { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - log.warn("redis hgetAll exception:"+namespace,e); + log.warn("redis hgetAll exception:" + namespace, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } - if(result!=null){ - Map newMap = Maps.newHashMap(); - Iterator> iterator = result.entrySet().iterator(); + if (result != null) { + Map newMap = Maps.newHashMap(); + Iterator> iterator = result.entrySet().iterator(); while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String key = entry.getKey(); - String val = entry.getValue(); - newMap.put(key, Jsons.fromJson(val, clazz)); + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + String val = entry.getValue(); + newMap.put(key, Jsons.fromJson(val, clazz)); } return newMap; - }else{ + } else { return null; } } - + /** * 返回 key 指定的哈希集中所有字段的名字。 + * * @param node * @param key * @return */ - public static Set hkeys(RedisNode node,String key) { + public static Set hkeys(RedisNode node, String key) { Set result = null; Jedis jedis = null; try { jedis = getClient(node); result = jedis.hkeys(key); } catch (Exception e) { - log.warn("redis hkeys exception:"+key,e); + log.warn("redis hkeys exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } return result; } - + /** * 返回 key 指定的哈希集中指定字段的值 + * * @param node * @param key * @param clazz * @param fields * @return */ - public static List hmget(RedisNode node ,String namespace, Class clazz,String... key) { - + public static List hmget(RedisNode node, String namespace, Class clazz, String... key) { + List value = null; Jedis jedis = null; try { jedis = getClient(node); value = jedis.hmget(namespace, key); } catch (Exception e) { - log.warn("redis lpopList exception:"+namespace+","+key,e); + log.warn("redis lpopList exception:" + namespace + "," + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } - if(value!=null){ + if (value != null) { List newValue = Lists.newArrayList(); - for(String temp:value){ + for (String temp : value) { newValue.add(Jsons.fromJson(temp, clazz)); } return newValue; } return null; - + } - + /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key + * 关联 + * * @param nodeList * @param key * @param hash * @param time */ - public static void hmset(List nodeList,String namespace, Map hash,Integer time) { - - for(RedisNode node:nodeList){ + public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.hmset(namespace, hash); - if(time!=null){ + if (time != null) { jedis.expire(namespace, time); } } catch (Exception e) { - log.warn("redis hmset exception:"+namespace,e); + log.warn("redis hmset exception:" + namespace, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } - + } - public static void hmset(List nodeList,String namespace, Map hash) { + public static void hmset(List nodeList, String namespace, Map hash) { hmset(nodeList, namespace, hash, null); } - - - /*********************hash redis end********************************/ - - - /*********************list redis start********************************/ + + /********************* hash redis end ********************************/ + + /********************* list redis start ********************************/ /** * 从队列的左边入队 */ - public static void lpush(List nodeList,String key, String value) { + public static void lpush(List nodeList, String key, String value) { - for(RedisNode node:nodeList){ + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.lpush(key, value); } catch (Exception e) { - log.warn("redis lpush exception:"+key+","+value,e); + log.warn("redis lpush exception:" + key + "," + value, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } - + } - - public static void lpush(List nodeList,String key, T value) { + + public static void lpush(List nodeList, String key, T value) { lpush(nodeList, key, Jsons.toJson(value)); - + } - + /** * 从队列的右边入队 */ - public static void rpush(List nodeList,String key, String value) { + public static void rpush(List nodeList, String key, String value) { - for(RedisNode node:nodeList){ + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.rpush(key, value); } catch (Exception e) { - log.warn("redis rpush exception:"+key+","+value,e); + log.warn("redis rpush exception:" + key + "," + value, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } } - - public static void rpush(List nodeList,String key, T value) { + + public static void rpush(List nodeList, String key, T value) { rpush(nodeList, key, Jsons.toJson(value)); } - + /** * 移除并且返回 key 对应的 list 的第一个元素 */ - public static T lpop(List nodeList,String key,Class clazz) { + public static T lpop(List nodeList, String key, Class clazz) { String retValue = null; - for(RedisNode node:nodeList){ + for (RedisNode node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -370,22 +377,22 @@ public static T lpop(List nodeList,String key,Class clazz) { vaule = jedis.lpop(key); retValue = vaule; } catch (Exception e) { - log.warn("redis lpop exception:"+key,e); + log.warn("redis lpop exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } - + return Jsons.fromJson(retValue, clazz); } - + /** * 从队列的右边出队一个元素 */ - public static T rpop(List nodeList,String key,Class clazz) { + public static T rpop(List nodeList, String key, Class clazz) { String retValue = null; - for(RedisNode node:nodeList){ + for (RedisNode node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -393,49 +400,48 @@ public static T rpop(List nodeList,String key,Class clazz) { vaule = jedis.rpop(key); retValue = vaule; } catch (Exception e) { - log.warn("redis lpop exception:"+key,e); + log.warn("redis lpop exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } } - + return Jsons.fromJson(retValue, clazz); } - - /** - * 从列表中获取指定返回的元素 - * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 从列表中获取指定返回的元素 start 和 end + * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List lrange(RedisNode node,String key,int start,int end,Class clazz) { + public static List lrange(RedisNode node, String key, int start, int end, Class clazz) { List value = null; Jedis jedis = null; try { jedis = getClient(node); value = jedis.lrange(key, start, end); } catch (Exception e) { - log.warn("redis lrange exception:"+key,e); + log.warn("redis lrange exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } - if(value!=null){ + if (value != null) { List newValue = Lists.newArrayList(); - for(String temp:value){ + for (String temp : value) { newValue.add(Jsons.fromJson(temp, clazz)); } return newValue; } return null; } - + /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key + * 里的值不是一个list的话,会返回error。 */ - public static long llen(RedisNode node ,String key) { + public static long llen(RedisNode node, String key) { long len = 0; Jedis jedis = null; @@ -443,15 +449,57 @@ public static long llen(RedisNode node ,String key) { jedis = getClient(node); jedis.llen(key); } catch (Exception e) { - log.warn("redis llen exception:"+key,e); + log.warn("redis llen exception:" + key, e); } finally { - //返还到连接池 + // 返还到连接池 close(jedis); } return len; } - - /*********************list redis end********************************/ + /********************* list redis end ********************************/ + + /********************* pubsub redis start ********************************/ + + /********************* pubsub redis end ********************************/ + public static void publish(RedisNode node, String channel, T message) { + Jedis jedis = null; + String value = Jsons.toJson(message); + try { + jedis = getClient(node); + jedis.publish(channel, value); + } catch (Exception e) { + log.warn("redis pub exception:" + channel + "," + value, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { + for (final RedisNode node : nodeList) { + executor.execute(new Runnable() { + @Override + public void run() { + subscribe(node, pubsub, channels); + } + }); + } + } + + public static void subscribe(RedisNode node, JedisPubSub pubsub, String... channel) { + + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.subscribe(pubsub, channel); + } catch (Exception e) { + log.warn("redis subscribe exception:" + channel, e); + } finally { + // 返还到连接池 + close(jedis); + } + + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java index b3c18629..c42d645b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java @@ -3,7 +3,6 @@ import java.util.Collections; import java.util.List; -import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.slf4j.Logger; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index cc751c87..a3c5f0cf 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -4,14 +4,20 @@ import java.util.Map; import java.util.Set; +import redis.clients.jedis.JedisPubSub; + +import com.google.common.collect.Sets; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.redis.RedisUtil; +import com.shinemo.mpush.tools.redis.pubsub.Subscriber; public class RedisManage { + /*********************k v redis start********************************/ + public static T get(String key,Class clazz) { RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); return RedisUtil.get(node, key, clazz); @@ -189,4 +195,26 @@ public static long llen(String key) { return RedisUtil.llen(node, key); } + public static void publish(String channel,T message){ + + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(channel); + RedisUtil.publish(node, channel, message); + + } + + public static void subscribe(JedisPubSub pubsub,String... channels){ + + Set set = Sets.newHashSet(); + for(String channel:channels){ + List nodeList = RedisGroupManage.instance.hashSet(channel); + set.addAll(nodeList); + } + + RedisUtil.subscribe(set, pubsub, channels); + } + + public static void subscribe(String... channel){ + subscribe(new Subscriber(), channel); + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java new file mode 100644 index 00000000..c4aee29d --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -0,0 +1,62 @@ +package com.shinemo.mpush.tools.redis.pubsub; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import redis.clients.jedis.JedisPubSub; + +public class Subscriber extends JedisPubSub{ + + private static final Logger log = LoggerFactory.getLogger(Subscriber.class); + + @Override + public void onMessage(String channel, String message) { + log.warn("onMessage channel:"+channel+","+message); + super.onMessage(channel, message); + } + + @Override + public void onPMessage(String pattern, String channel, String message) { + log.warn("onPMessage:"+pattern+","+channel+","+message); + super.onPMessage(pattern, channel, message); + } + + @Override + public void onPSubscribe(String pattern, int subscribedChannels) { + log.warn("onPSubscribe:"+pattern+","+subscribedChannels); + super.onPSubscribe(pattern, subscribedChannels); + } + + @Override + public void onPUnsubscribe(String pattern, int subscribedChannels) { + log.warn("onPUnsubscribe:"+pattern+","+subscribedChannels); + super.onPUnsubscribe(pattern, subscribedChannels); + } + + @Override + public void onSubscribe(String channel, int subscribedChannels) { + log.warn("onSubscribe:"+channel+","+subscribedChannels); + super.onSubscribe(channel, subscribedChannels); + } + + @Override + public void onUnsubscribe(String channel, int subscribedChannels) { + log.warn("onUnsubscribe:"+channel+","+subscribedChannels); + super.onUnsubscribe(channel, subscribedChannels); + } + + + @Override + public void unsubscribe() { + log.warn("unsubscribe:"); + super.unsubscribe(); + } + + @Override + public void unsubscribe(String... channels) { + log.warn("unsubscribe:"+channels); + super.unsubscribe(channels); + } + + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 48113bae..fdc8d403 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.tools.redis; +import java.util.Date; import java.util.List; import org.apache.commons.lang3.builder.ToStringBuilder; @@ -9,6 +10,7 @@ import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; +import com.shinemo.mpush.tools.redis.manage.RedisManage; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -18,6 +20,9 @@ public class RedisGroupManageTest { ServerManage manage = new ServerManage(app); List groupList = null; + RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); + RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + @Before public void init(){ manage.start(); @@ -34,6 +39,52 @@ public void testGetRedisGroup(){ } } + @Test + public void testAdd(){ + User user = RedisManage.get("huang2", User.class); + if(user == null){ + user = new User("hi", 10, new Date()); + RedisManage.set("huang2", user); + user = RedisManage.get("huang2", User.class); + } + System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); + + User nowUser = RedisUtil.get(node, "huang2", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.get(node2, "huang2", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisManage.del("huang2"); + + nowUser = RedisUtil.get(node2, "huang2", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.get(node, "huang2", User.class); + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + + } + + @Test + public void testPub(){ + User user = new User("pub", 10, new Date()); + RedisManage.publish("channel1", user); + RedisManage.publish("channel2", user); + } + + @Test + public void testSub(){ + RedisManage.subscribe("channel1","channel2"); + try { + Thread.sleep(Integer.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } From 6a62d4d19bff4b3ce33162210748ec0723508442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 5 Jan 2016 14:44:52 +0800 Subject: [PATCH 095/890] add redis pub sub tset --- .../shinemo/mpush/tools/redis/RedisGroupManageTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index fdc8d403..442e3c65 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -71,9 +71,11 @@ public void testAdd(){ @Test public void testPub(){ - User user = new User("pub", 10, new Date()); - RedisManage.publish("channel1", user); - RedisManage.publish("channel2", user); + for(int i = 0;i<20;i++){ + User user = new User("pub"+i, 10, new Date()); + RedisManage.publish("channel1", user); + RedisManage.publish("channel2", user); + } } @Test From 994e92ae071804a3981cacc6de0da0b1f9c1fe76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 5 Jan 2016 14:56:01 +0800 Subject: [PATCH 096/890] add listener redis --- .../redis/listener/ListenerDispatcher.java | 27 +++++++++++++++++++ .../tools/redis/listener/MessageListener.java | 8 ++++++ .../mpush/tools/redis/pubsub/Subscriber.java | 5 ++++ 3 files changed, 40 insertions(+) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java new file mode 100644 index 00000000..0e2b6749 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java @@ -0,0 +1,27 @@ +package com.shinemo.mpush.tools.redis.listener; + +import java.util.Iterator; +import java.util.Map; + +import com.google.common.collect.Maps; + +public class ListenerDispatcher implements MessageListener { + + private Map holder = Maps.newTreeMap(); + + public static ListenerDispatcher instance = new ListenerDispatcher(); + + private ListenerDispatcher() { + } + + @Override + public void onMessage(String channel, String message) { + Iterator> it = holder.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + entry.getValue().onMessage(channel, message); + } + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java new file mode 100644 index 00000000..f5967a39 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java @@ -0,0 +1,8 @@ +package com.shinemo.mpush.tools.redis.listener; + + +public interface MessageListener { + + void onMessage(String channel, String message); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index c4aee29d..5c46ec2f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -3,15 +3,20 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; + import redis.clients.jedis.JedisPubSub; public class Subscriber extends JedisPubSub{ private static final Logger log = LoggerFactory.getLogger(Subscriber.class); + private ListenerDispatcher dispatcher = ListenerDispatcher.instance; + @Override public void onMessage(String channel, String message) { log.warn("onMessage channel:"+channel+","+message); + dispatcher.onMessage(channel, message); super.onMessage(channel, message); } From 84d86d289eb2c3be823ef980ad232c354a07c577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 07:13:22 +0000 Subject: [PATCH 097/890] add session decode encode --- .../mpush/api/connection/SessionContext.java | 5 +-- .../mpush/common/security/AesCipher.java | 20 +++++++++- .../core/handler/FastConnectHandler.java | 6 +-- .../mpush/core/session/ReusableSession.java | 39 ++++++++++++++++++- .../core/session/ReusableSessionManager.java | 18 ++++++--- 5 files changed, 73 insertions(+), 15 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java index a7d2033b..bf23942c 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java @@ -1,7 +1,5 @@ package com.shinemo.mpush.api.connection; -import com.google.common.base.Strings; - /** * Created by ohun on 2015/12/22. */ @@ -42,10 +40,9 @@ public void setHeartbeat(int heartbeat) { } public boolean handshakeOk() { - return !Strings.isNullOrEmpty(deviceId); + return deviceId != null && deviceId.length() > 0; } - @Override public String toString() { return "SessionContext{" + diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index fc18d645..2ee0327b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -3,12 +3,14 @@ import com.shinemo.mpush.api.connection.Cipher; import com.shinemo.mpush.tools.crypto.AESUtils; +import java.util.Arrays; + /** * Created by ohun on 2015/12/28. */ public final class AesCipher implements Cipher { - private final byte[] key; - private final byte[] iv; + public final byte[] key; + public final byte[] iv; public AesCipher(byte[] key, byte[] iv) { this.key = key; @@ -24,4 +26,18 @@ public byte[] decrypt(byte[] data) { public byte[] encrypt(byte[] data) { return AESUtils.encrypt(data, key, iv); } + + @Override + public String toString() { + return toString(key) + ',' + toString(iv); + } + + public String toString(byte[] a) { + StringBuilder b = new StringBuilder(); + for (int i = 0; i < a.length; i++) { + if (i != 0) b.append('|'); + b.append(a[i]); + } + return b.toString(); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 50b64814..ef092d1f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -25,12 +25,12 @@ public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); if (session == null) { ErrorMessage.from(message).setReason("session expire").close(); - } else if (!session.sessionContext.deviceId.equals(message.deviceId)) { + } else if (!session.context.deviceId.equals(message.deviceId)) { ErrorMessage.from(message).setReason("error device").close(); } else { int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); - session.sessionContext.setHeartbeat(heartbeat); - message.getConnection().setSessionContext(session.sessionContext); + session.context.setHeartbeat(heartbeat); + message.getConnection().setSessionContext(session.context); FastConnectOkMessage .from(message) .setServerHost(MPushUtil.getLocalIp()) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java index 3c5401a0..2f71e41c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.core.session; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.common.security.AesCipher; +import com.shinemo.mpush.common.security.CipherBox; /** * Created by ohun on 2015/12/25. @@ -8,5 +10,40 @@ public final class ReusableSession { public String sessionId; public long expireTime; - public SessionContext sessionContext; + public SessionContext context; + + public String encode() { + StringBuffer sb = new StringBuffer(); + sb.append(context.osName).append(','); + sb.append(context.osVersion).append(','); + sb.append(context.clientVersion).append(','); + sb.append(context.deviceId).append(','); + sb.append(context.cipher).append(','); + return sb.toString(); + } + + public void decode(String value) throws Exception { + String[] array = value.split(","); + if (array.length != 6) throw new RuntimeException("decode session exception"); + SessionContext context = new SessionContext(); + context.osName = array[0]; + context.osVersion = array[1]; + context.clientVersion = array[2]; + context.deviceId = array[3]; + byte[] key = toArray(array[4]); + byte[] iv = toArray(array[5]); + context.cipher = new AesCipher(key, iv); + } + + private byte[] toArray(String str) { + String[] a = str.split("|"); + if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { + throw new RuntimeException("decode session cipher exception"); + } + byte[] bytes = new byte[a.length]; + for (int i = 0; i < a.length; i++) { + bytes[i] = Byte.parseByte(a[i]); + } + return bytes; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index 7670972f..8a9559ee 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -1,11 +1,11 @@ package com.shinemo.mpush.core.session; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.crypto.MD5Utils; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * Created by ohun on 2015/12/25. @@ -13,21 +13,29 @@ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; - private final Map sessionCache = new ConcurrentHashMapV8<>(); + private final Map sessionCache = new ConcurrentHashMapV8<>(); public boolean cacheSession(ReusableSession session) { - sessionCache.put(session.sessionId, session); + sessionCache.put(session.sessionId, session.encode()); return true; } public ReusableSession getSession(String sessionId) { - return sessionCache.get(sessionId); + String value = sessionCache.get(sessionId); + if (Strings.isBlank(value)) return null; + ReusableSession session = new ReusableSession(); + try { + session.decode(value); + } catch (Exception e) { + return null; + } + return session; } public ReusableSession genSession(SessionContext context) { long now = System.currentTimeMillis(); ReusableSession session = new ReusableSession(); - session.sessionContext = context; + session.context = context; session.sessionId = MD5Utils.encrypt(context.deviceId + now); session.expireTime = now + EXPIRE_TIME; return session; From b7c2c4a206e5831de29342e443220284292c0565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 07:27:27 +0000 Subject: [PATCH 098/890] add redis --- .../router/ConnectionRouterManager.java | 22 ++++++------------- .../common/router/RemoteRouterManager.java | 8 +++++-- .../core/session/ReusableSessionManager.java | 6 ++--- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java index 30f60689..39e35671 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java @@ -17,23 +17,15 @@ public class ConnectionRouterManager extends RemoteRouterManager { .expireAfterAccess(5, TimeUnit.MINUTES) .build(); - - @Override - public RemoteRouter register(String userId, RemoteRouter route) { - RemoteRouter old = cache.getIfPresent(userId); - cache.put(userId, route); - return old; - } - - @Override - public boolean unRegister(String userId) { - cache.invalidate(userId); - return true; - } - @Override public RemoteRouter lookup(String userId) { - return cache.getIfPresent(userId); + RemoteRouter cached = cache.getIfPresent(userId); + if (cached != null) return cached; + RemoteRouter router = super.lookup(userId); + if (router != null) { + cache.put(userId, cached); + } + return router; } /** diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 8a1fe5e2..560a6f60 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.common.router; import com.shinemo.mpush.api.router.RouterManager; +import com.shinemo.mpush.tools.redis.manage.RedisManage; /** * Created by ohun on 2015/12/23. @@ -9,16 +10,19 @@ public class RemoteRouterManager implements RouterManager { @Override public RemoteRouter register(String userId, RemoteRouter route) { - return null; + RemoteRouter old = RedisManage.get(userId, RemoteRouter.class); + RedisManage.set(userId, route); + return old; } @Override public boolean unRegister(String userId) { + RedisManage.del(userId); return true; } @Override public RemoteRouter lookup(String userId) { - return null; + return RedisManage.get(userId, RemoteRouter.class); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index 8a9559ee..a8b75700 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -3,6 +3,7 @@ import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.crypto.MD5Utils; +import com.shinemo.mpush.tools.redis.manage.RedisManage; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import java.util.Map; @@ -13,15 +14,14 @@ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; - private final Map sessionCache = new ConcurrentHashMapV8<>(); public boolean cacheSession(ReusableSession session) { - sessionCache.put(session.sessionId, session.encode()); + RedisManage.set(session.sessionId, session.encode(), EXPIRE_TIME); return true; } public ReusableSession getSession(String sessionId) { - String value = sessionCache.get(sessionId); + String value = RedisManage.get(sessionId, String.class); if (Strings.isBlank(value)) return null; ReusableSession session = new ReusableSession(); try { From b4973098791af2b7820253e5d96edaf2785a6ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 08:20:30 +0000 Subject: [PATCH 099/890] add redis subscribe --- .../core/router/RouterChangeListener.java | 26 +- .../shinemo/mpush/tools/redis/RedisUtil.java | 956 +++++++++--------- .../redis/listener/ListenerDispatcher.java | 51 +- .../mpush/tools/redis/manage/RedisManage.java | 407 ++++---- .../mpush/tools/redis/pubsub/Subscriber.java | 120 +-- .../tools/redis/RedisGroupManageTest.java | 152 +-- 6 files changed, 882 insertions(+), 830 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 519cbba8..e07d7732 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -9,6 +9,10 @@ import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.message.KickUserMessage; import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.listener.MessageListener; +import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.shinemo.mpush.tools.redis.pubsub.Subscriber; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; @@ -17,12 +21,13 @@ /** * Created by ohun on 2016/1/4. */ -public class RouterChangeListener { +public class RouterChangeListener implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); + public static final String KICK_CHANNEL = "__kick__"; public RouterChangeListener() { EventBus.INSTANCE.register(this); - // TODO: 2016/1/4 register this to redis server + RedisManage.subscribe(this, KICK_CHANNEL); } @Subscribe @@ -61,10 +66,9 @@ public void kickRemote(String userId, RemoteRouter router) { msg.deviceId = location.getDeviceId(); msg.srcServer = location.getHost(); msg.userId = userId; - // TODO: 2016/1/4 publish kick remote user msg to redis + RedisManage.publish(KICK_CHANNEL, msg); } - // TODO: 2016/1/4 receive msg from redis public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { String userId = msg.userId; LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); @@ -77,4 +81,18 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { LOGGER.warn("no local router find, kick failure, msg={}", msg); } } + + @Override + public void onMessage(String channel, String message) { + if (KICK_CHANNEL.equals(channel)) { + KickRemoteMsg msg = Jsons.fromJson(message, KickRemoteMsg.class); + if (msg != null) { + onReceiveKickRemoteMsg(msg); + } else { + LOGGER.warn("receive an error kick message={}", message); + } + } else { + LOGGER.warn("receive an error redis channel={}", channel); + } + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 2234e019..b9c7a28e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -22,484 +22,482 @@ public class RedisUtil { - private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); - - private static Map holder = Maps.newConcurrentMap(); - - private static Executor executor = Executors.newFixedThreadPool(10); - - public static Jedis getClient(RedisNode node) { - JedisPool pool = holder.get(node); - if (pool == null) { - pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); - holder.put(node, pool); - } - return pool.getResource(); - } - - public static void close(Jedis jedis) { - jedis.close(); - } - - /********************* k v redis start ********************************/ - /** - * - * @param node - * redis实例 - * @param key - * @param clazz - * @return - */ - public static T get(RedisNode node, String key, Class clazz) { - - String value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.get(key); - } catch (Exception e) { - log.warn("redis get exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - return Jsons.fromJson(value, clazz); - } - - public static void set(List nodeList, String key, String value) { - - set(nodeList, key, value, null); - - } - - public static void set(List nodeList, String key, T value) { - set(nodeList, key, value, null); - } - - public static void set(List nodeList, String key, T value, Integer time) { - String jsonValue = Jsons.toJson(value); - set(nodeList, key, jsonValue, time); - } - - /** - * - * @param nodeList - * @param key - * @param value - * @param time - * seconds - */ - public static void set(List nodeList, String key, String value, Integer time) { - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.set(key, value); - if (time != null) { - jedis.expire(key, time); - } - } catch (Exception e) { - log.warn("redis set exception:" + key + "," + value + "," + time, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static void del(List nodeList, String key) { - - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.del(key); - } catch (Exception e) { - log.warn("redis del exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - /********************* k v redis end ********************************/ - - /********************* hash redis start ********************************/ - public static void hset(List nodeList, String namespace, String key, String value) { - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.hset(namespace, key, value); - } catch (Exception e) { - log.warn("redis hset exception:" + namespace + "," + key + "," + value, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static void hset(List nodeList, String namespace, String key, T value) { - hset(nodeList, namespace, key, Jsons.toJson(value)); - } - - public static T hget(RedisNode node, String namespace, String key, Class clazz) { - - String value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.hget(namespace, key); - } catch (Exception e) { - log.warn("redis hget exception:" + namespace + "," + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - return Jsons.fromJson(value, clazz); - - } - - public static void hdel(List nodeList, String namespace, String key) { - - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.hdel(namespace, key); - } catch (Exception e) { - log.warn("redis hdel exception:" + namespace + "," + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static Map hgetAll(RedisNode node, String namespace) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(namespace); - } catch (Exception e) { - log.warn("redis hgetAll exception:" + namespace, e); - } finally { - // 返还到连接池 - close(jedis); - } - return result; - } - - public static Map hgetAll(RedisNode node, String namespace, Class clazz) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(namespace); - } catch (Exception e) { - log.warn("redis hgetAll exception:" + namespace, e); - } finally { - // 返还到连接池 - close(jedis); - } - if (result != null) { - Map newMap = Maps.newHashMap(); - Iterator> iterator = result.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String key = entry.getKey(); - String val = entry.getValue(); - newMap.put(key, Jsons.fromJson(val, clazz)); - } - return newMap; - } else { - return null; - } - - } - - /** - * 返回 key 指定的哈希集中所有字段的名字。 - * - * @param node - * @param key - * @return - */ - public static Set hkeys(RedisNode node, String key) { - Set result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hkeys(key); - } catch (Exception e) { - log.warn("redis hkeys exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - - } - return result; - } - - /** - * 返回 key 指定的哈希集中指定字段的值 - * - * @param node - * @param key - * @param clazz - * @param fields - * @return - */ - public static List hmget(RedisNode node, String namespace, Class clazz, String... key) { - - List value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.hmget(namespace, key); - } catch (Exception e) { - log.warn("redis lpopList exception:" + namespace + "," + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - - } - - /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key - * 关联 - * - * @param nodeList - * @param key - * @param hash - * @param time - */ - public static void hmset(List nodeList, String namespace, Map hash, Integer time) { - - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.hmset(namespace, hash); - if (time != null) { - jedis.expire(namespace, time); - } - - } catch (Exception e) { - log.warn("redis hmset exception:" + namespace, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - public static void hmset(List nodeList, String namespace, Map hash) { - hmset(nodeList, namespace, hash, null); - } - - /********************* hash redis end ********************************/ - - /********************* list redis start ********************************/ - /** - * 从队列的左边入队 - */ - public static void lpush(List nodeList, String key, String value) { - - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.lpush(key, value); - } catch (Exception e) { - log.warn("redis lpush exception:" + key + "," + value, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - public static void lpush(List nodeList, String key, T value) { - - lpush(nodeList, key, Jsons.toJson(value)); - - } - - /** - * 从队列的右边入队 - */ - public static void rpush(List nodeList, String key, String value) { - - for (RedisNode node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.rpush(key, value); - } catch (Exception e) { - log.warn("redis rpush exception:" + key + "," + value, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static void rpush(List nodeList, String key, T value) { - rpush(nodeList, key, Jsons.toJson(value)); - } - - /** - * 移除并且返回 key 对应的 list 的第一个元素 - */ - public static T lpop(List nodeList, String key, Class clazz) { - String retValue = null; - for (RedisNode node : nodeList) { - Jedis jedis = null; - String vaule = null; - try { - jedis = getClient(node); - vaule = jedis.lpop(key); - retValue = vaule; - } catch (Exception e) { - log.warn("redis lpop exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - return Jsons.fromJson(retValue, clazz); - } - - /** - * 从队列的右边出队一个元素 - */ - public static T rpop(List nodeList, String key, Class clazz) { - String retValue = null; - for (RedisNode node : nodeList) { - Jedis jedis = null; - String vaule = null; - try { - jedis = getClient(node); - vaule = jedis.rpop(key); - retValue = vaule; - } catch (Exception e) { - log.warn("redis lpop exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - return Jsons.fromJson(retValue, clazz); - } - - /** - * 从列表中获取指定返回的元素 start 和 end - * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List lrange(RedisNode node, String key, int start, int end, Class clazz) { - List value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.lrange(key, start, end); - } catch (Exception e) { - log.warn("redis lrange exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - } - - /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key - * 里的值不是一个list的话,会返回error。 - */ - public static long llen(RedisNode node, String key) { - - long len = 0; - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.llen(key); - } catch (Exception e) { - log.warn("redis llen exception:" + key, e); - } finally { - // 返还到连接池 - close(jedis); - } - return len; - } - - /********************* list redis end ********************************/ - - /********************* pubsub redis start ********************************/ - - /********************* pubsub redis end ********************************/ - public static void publish(RedisNode node, String channel, T message) { - Jedis jedis = null; - String value = Jsons.toJson(message); - try { - jedis = getClient(node); - jedis.publish(channel, value); - } catch (Exception e) { - log.warn("redis pub exception:" + channel + "," + value, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { - for (final RedisNode node : nodeList) { - executor.execute(new Runnable() { - @Override - public void run() { - subscribe(node, pubsub, channels); - } - }); - } - } - - public static void subscribe(RedisNode node, JedisPubSub pubsub, String... channel) { - - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.subscribe(pubsub, channel); - } catch (Exception e) { - log.warn("redis subscribe exception:" + channel, e); - } finally { - // 返还到连接池 - close(jedis); - } - - } + private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); + + private static Map holder = Maps.newConcurrentMap(); + + public static Jedis getClient(RedisNode node) { + JedisPool pool = holder.get(node); + if (pool == null) { + pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); + holder.put(node, pool); + } + return pool.getResource(); + } + + public static void close(Jedis jedis) { + jedis.close(); + } + + /********************* k v redis start ********************************/ + /** + * @param node redis实例 + * @param key + * @param clazz + * @return + */ + public static T get(RedisNode node, String key, Class clazz) { + + String value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.get(key); + } catch (Exception e) { + log.warn("redis get exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + return Jsons.fromJson(value, clazz); + } + + public static void set(List nodeList, String key, String value) { + + set(nodeList, key, value, null); + + } + + public static void set(List nodeList, String key, T value) { + set(nodeList, key, value, null); + } + + public static void set(List nodeList, String key, T value, Integer time) { + String jsonValue = Jsons.toJson(value); + set(nodeList, key, jsonValue, time); + } + + /** + * @param nodeList + * @param key + * @param value + * @param time seconds + */ + public static void set(List nodeList, String key, String value, Integer time) { + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.set(key, value); + if (time != null) { + jedis.expire(key, time); + } + } catch (Exception e) { + log.warn("redis set exception:" + key + "," + value + "," + time, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + } + + public static void del(List nodeList, String key) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.del(key); + } catch (Exception e) { + log.warn("redis del exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + } + + /********************* k v redis end ********************************/ + + /********************* + * hash redis start + ********************************/ + public static void hset(List nodeList, String namespace, String key, String value) { + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.hset(namespace, key, value); + } catch (Exception e) { + log.warn("redis hset exception:" + namespace + "," + key + "," + value, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + } + + public static void hset(List nodeList, String namespace, String key, T value) { + hset(nodeList, namespace, key, Jsons.toJson(value)); + } + + public static T hget(RedisNode node, String namespace, String key, Class clazz) { + + String value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.hget(namespace, key); + } catch (Exception e) { + log.warn("redis hget exception:" + namespace + "," + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + return Jsons.fromJson(value, clazz); + + } + + public static void hdel(List nodeList, String namespace, String key) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.hdel(namespace, key); + } catch (Exception e) { + log.warn("redis hdel exception:" + namespace + "," + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + } + + public static Map hgetAll(RedisNode node, String namespace) { + Map result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hgetAll(namespace); + } catch (Exception e) { + log.warn("redis hgetAll exception:" + namespace, e); + } finally { + // 返还到连接池 + close(jedis); + } + return result; + } + + public static Map hgetAll(RedisNode node, String namespace, Class clazz) { + Map result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hgetAll(namespace); + } catch (Exception e) { + log.warn("redis hgetAll exception:" + namespace, e); + } finally { + // 返还到连接池 + close(jedis); + } + if (result != null) { + Map newMap = Maps.newHashMap(); + Iterator> iterator = result.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + String val = entry.getValue(); + newMap.put(key, Jsons.fromJson(val, clazz)); + } + return newMap; + } else { + return null; + } + + } + + /** + * 返回 key 指定的哈希集中所有字段的名字。 + * + * @param node + * @param key + * @return + */ + public static Set hkeys(RedisNode node, String key) { + Set result = null; + Jedis jedis = null; + try { + jedis = getClient(node); + result = jedis.hkeys(key); + } catch (Exception e) { + log.warn("redis hkeys exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + + } + return result; + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * + * @param node + * @param key + * @param clazz + * @param fields + * @return + */ + public static List hmget(RedisNode node, String namespace, Class clazz, String... key) { + + List value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.hmget(namespace, key); + } catch (Exception e) { + log.warn("redis lpopList exception:" + namespace + "," + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + if (value != null) { + List newValue = Lists.newArrayList(); + for (String temp : value) { + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key + * 关联 + * + * @param nodeList + * @param key + * @param hash + * @param time + */ + public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.hmset(namespace, hash); + if (time != null) { + jedis.expire(namespace, time); + } + + } catch (Exception e) { + log.warn("redis hmset exception:" + namespace, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + } + + public static void hmset(List nodeList, String namespace, Map hash) { + hmset(nodeList, namespace, hash, null); + } + + /********************* hash redis end ********************************/ + + /********************* list redis start ********************************/ + /** + * 从队列的左边入队 + */ + public static void lpush(List nodeList, String key, String value) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.lpush(key, value); + } catch (Exception e) { + log.warn("redis lpush exception:" + key + "," + value, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + } + + public static void lpush(List nodeList, String key, T value) { + + lpush(nodeList, key, Jsons.toJson(value)); + + } + + /** + * 从队列的右边入队 + */ + public static void rpush(List nodeList, String key, String value) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.rpush(key, value); + } catch (Exception e) { + log.warn("redis rpush exception:" + key + "," + value, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + } + + public static void rpush(List nodeList, String key, T value) { + rpush(nodeList, key, Jsons.toJson(value)); + } + + /** + * 移除并且返回 key 对应的 list 的第一个元素 + */ + public static T lpop(List nodeList, String key, Class clazz) { + String retValue = null; + for (RedisNode node : nodeList) { + Jedis jedis = null; + String vaule = null; + try { + jedis = getClient(node); + vaule = jedis.lpop(key); + retValue = vaule; + } catch (Exception e) { + log.warn("redis lpop exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + return Jsons.fromJson(retValue, clazz); + } + + /** + * 从队列的右边出队一个元素 + */ + public static T rpop(List nodeList, String key, Class clazz) { + String retValue = null; + for (RedisNode node : nodeList) { + Jedis jedis = null; + String vaule = null; + try { + jedis = getClient(node); + vaule = jedis.rpop(key); + retValue = vaule; + } catch (Exception e) { + log.warn("redis lpop exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + return Jsons.fromJson(retValue, clazz); + } + + /** + * 从列表中获取指定返回的元素 start 和 end + * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public static List lrange(RedisNode node, String key, int start, int end, Class clazz) { + List value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.lrange(key, start, end); + } catch (Exception e) { + log.warn("redis lrange exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + if (value != null) { + List newValue = Lists.newArrayList(); + for (String temp : value) { + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + } + + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key + * 里的值不是一个list的话,会返回error。 + */ + public static long llen(RedisNode node, String key) { + + long len = 0; + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.llen(key); + } catch (Exception e) { + log.warn("redis llen exception:" + key, e); + } finally { + // 返还到连接池 + close(jedis); + } + return len; + } + + /********************* list redis end ********************************/ + + /********************* pubsub redis start ********************************/ + + /********************* + * pubsub redis end + ********************************/ + public static void publish(RedisNode node, String channel, T message) { + Jedis jedis = null; + String value = Jsons.toJson(message); + try { + jedis = getClient(node); + jedis.publish(channel, value); + } catch (Exception e) { + log.warn("redis pub exception:" + channel + "," + value, e); + } finally { + // 返还到连接池 + close(jedis); + } + } + + public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { + for (final RedisNode node : nodeList) { + new Thread(new Runnable() { + @Override + public void run() { + subscribe(node, pubsub, channels); + } + }).start(); + } + } + + public static void subscribe(RedisNode node, JedisPubSub pubsub, String... channel) { + + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.subscribe(pubsub, channel); + } catch (Exception e) { + log.warn("redis subscribe exception:" + channel, e); + } finally { + // 返还到连接池 + close(jedis); + } + + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java index 0e2b6749..0ec2ecea 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java @@ -1,27 +1,44 @@ package com.shinemo.mpush.tools.redis.listener; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import com.google.common.collect.Maps; public class ListenerDispatcher implements MessageListener { - private Map holder = Maps.newTreeMap(); - - public static ListenerDispatcher instance = new ListenerDispatcher(); - - private ListenerDispatcher() { - } - - @Override - public void onMessage(String channel, String message) { - Iterator> it = holder.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = it.next(); - entry.getValue().onMessage(channel, message); - } - } - - + public static final ListenerDispatcher INSTANCE = new ListenerDispatcher(); + + private Map> subscribes = Maps.newTreeMap(); + + private Executor executor = Executors.newFixedThreadPool(10); + + @Override + public void onMessage(final String channel, final String message) { + List listeners = subscribes.get(channel); + if (listeners == null) { + return; + } + for (final MessageListener l : listeners) { + executor.execute(new Runnable() { + @Override + public void run() { + l.onMessage(channel, message); + } + }); + } + } + + public void subscribe(String channel, MessageListener l) { + List listeners = subscribes.get(channel); + if (listeners == null) { + listeners = new ArrayList<>(); + subscribes.put(channel, listeners); + } + listeners.add(l); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index a3c5f0cf..d4fed50f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -4,6 +4,7 @@ import java.util.Map; import java.util.Set; +import com.shinemo.mpush.tools.redis.listener.MessageListener; import redis.clients.jedis.JedisPubSub; import com.google.common.collect.Sets; @@ -13,208 +14,214 @@ import com.shinemo.mpush.tools.redis.pubsub.Subscriber; - public class RedisManage { - /*********************k v redis start********************************/ - - public static T get(String key,Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); - return RedisUtil.get(node, key, clazz); - } - - public static void set(String key,T value) { - set(key, value, null); - } - - public static void set(String key,T value,Integer time) { - String jsonValue = Jsons.toJson(value); - set(key, jsonValue, time); - } - - /** - * - * @param nodeList - * @param key - * @param value - * @param time seconds - */ - public static void set(String key,String value,Integer time) { - List nodeList = RedisGroupManage.instance.hashSet(key); - RedisUtil.set(nodeList, key, value, null); - } - - public static void del(String key) { - - List nodeList = RedisGroupManage.instance.hashSet(key); - RedisUtil.del(nodeList, key); - - } - + /********************* + * k v redis start + ********************************/ + + public static T get(String key, Class clazz) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + return RedisUtil.get(node, key, clazz); + } + + public static void set(String key, T value) { + set(key, value, null); + } + + public static void set(String key, T value, Integer time) { + String jsonValue = Jsons.toJson(value); + set(key, jsonValue, time); + } + + /** + * @param nodeList + * @param key + * @param value + * @param time seconds + */ + public static void set(String key, String value, Integer time) { + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.set(nodeList, key, value, null); + } + + public static void del(String key) { + + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.del(nodeList, key); + + } + /*********************k v redis end********************************/ - - - /*********************hash redis start********************************/ - public static void hset(String namespace, String key, String value) { - - List nodeList = RedisGroupManage.instance.hashSet(key); - RedisUtil.hset(nodeList, namespace, key, value); - - } - - public static void hset(String namespace, String key, T value) { - hset(namespace, key, Jsons.toJson(value)); - } - - public static T hget(String namespace, String key,Class clazz) { - - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); - return RedisUtil.hget(node, namespace, key, clazz); - - } - - public static void hdel(String namespace, String key) { - List nodeList = RedisGroupManage.instance.hashSet(namespace); - RedisUtil.hdel(nodeList, namespace, key); - } - - public static Map hgetAll(String namespace) { - - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace); - - } - - public static Map hgetAll(String namespace,Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace,clazz); - } - - /** - * 返回 key 指定的哈希集中所有字段的名字。 - * @param node - * @param key - * @return - */ - public static Set hkeys(String namespace) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); - return RedisUtil.hkeys(node, namespace); - } - - /** - * 返回 key 指定的哈希集中指定字段的值 - * @param node - * @param key - * @param clazz - * @param fields - * @return - */ - public static List hmget(String namespace, Class clazz,String... key) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); - return RedisUtil.hmget(node, namespace, clazz, key); - } - - /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 - * @param nodeList - * @param key - * @param hash - * @param time - */ - public static void hmset(String namespace, Map hash,Integer time) { - List nodeList = RedisGroupManage.instance.hashSet(namespace); - RedisUtil.hmset(nodeList, namespace, hash, time); - } - - public static void hmset(String namespace, Map hash) { - hmset(namespace, hash, null); - } - - + + + /********************* + * hash redis start + ********************************/ + public static void hset(String namespace, String key, String value) { + + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.hset(nodeList, namespace, key, value); + + } + + public static void hset(String namespace, String key, T value) { + hset(namespace, key, Jsons.toJson(value)); + } + + public static T hget(String namespace, String key, Class clazz) { + + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hget(node, namespace, key, clazz); + + } + + public static void hdel(String namespace, String key) { + List nodeList = RedisGroupManage.instance.hashSet(namespace); + RedisUtil.hdel(nodeList, namespace, key); + } + + public static Map hgetAll(String namespace) { + + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hgetAll(node, namespace); + + } + + public static Map hgetAll(String namespace, Class clazz) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hgetAll(node, namespace, clazz); + } + + /** + * 返回 key 指定的哈希集中所有字段的名字。 + * + * @param node + * @param key + * @return + */ + public static Set hkeys(String namespace) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hkeys(node, namespace); + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * + * @param node + * @param key + * @param clazz + * @param fields + * @return + */ + public static List hmget(String namespace, Class clazz, String... key) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + return RedisUtil.hmget(node, namespace, clazz, key); + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * + * @param nodeList + * @param key + * @param hash + * @param time + */ + public static void hmset(String namespace, Map hash, Integer time) { + List nodeList = RedisGroupManage.instance.hashSet(namespace); + RedisUtil.hmset(nodeList, namespace, hash, time); + } + + public static void hmset(String namespace, Map hash) { + hmset(namespace, hash, null); + } + + /*********************hash redis end********************************/ - - - /*********************list redis start********************************/ - /** - * 从队列的左边入队 - */ - public static void lpush(String key, String value) { - List nodeList = RedisGroupManage.instance.hashSet(key); - RedisUtil.lpush(nodeList, key, value); - } - - public static void lpush(String key, T value) { - lpush(key, Jsons.toJson(value)); - } - - /** - * 从队列的右边入队 - */ - public static void rpush(String key, String value) { - List nodeList = RedisGroupManage.instance.hashSet(key); - RedisUtil.rpush(nodeList, key, value); - } - - public static void rpush(String key, T value) { - rpush(key, Jsons.toJson(value)); - } - - /** - * 移除并且返回 key 对应的 list 的第一个元素 - */ - public static T lpop(String key,Class clazz) { - List nodeList = RedisGroupManage.instance.hashSet(key); - return RedisUtil.lpop(nodeList, key, clazz); - } - - /** - * 从队列的右边出队一个元素 - */ - public static T rpop(String key,Class clazz) { - List nodeList = RedisGroupManage.instance.hashSet(key); - return RedisUtil.rpop(nodeList, key, clazz); - } - - - - /** - * 从列表中获取指定返回的元素 - * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List lrange(String key,int start,int end,Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); - return RedisUtil.lrange(node, key, start, end, clazz); - } - - /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 - */ - public static long llen(String key) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); - return RedisUtil.llen(node, key); - } - - public static void publish(String channel,T message){ - - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(channel); - RedisUtil.publish(node, channel, message); - - } - - public static void subscribe(JedisPubSub pubsub,String... channels){ - - Set set = Sets.newHashSet(); - for(String channel:channels){ - List nodeList = RedisGroupManage.instance.hashSet(channel); - set.addAll(nodeList); - } - - RedisUtil.subscribe(set, pubsub, channels); - } - - public static void subscribe(String... channel){ - subscribe(new Subscriber(), channel); - } - + + + /*********************list redis start********************************/ + /** + * 从队列的左边入队 + */ + public static void lpush(String key, String value) { + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.lpush(nodeList, key, value); + } + + public static void lpush(String key, T value) { + lpush(key, Jsons.toJson(value)); + } + + /** + * 从队列的右边入队 + */ + public static void rpush(String key, String value) { + List nodeList = RedisGroupManage.instance.hashSet(key); + RedisUtil.rpush(nodeList, key, value); + } + + public static void rpush(String key, T value) { + rpush(key, Jsons.toJson(value)); + } + + /** + * 移除并且返回 key 对应的 list 的第一个元素 + */ + public static T lpop(String key, Class clazz) { + List nodeList = RedisGroupManage.instance.hashSet(key); + return RedisUtil.lpop(nodeList, key, clazz); + } + + /** + * 从队列的右边出队一个元素 + */ + public static T rpop(String key, Class clazz) { + List nodeList = RedisGroupManage.instance.hashSet(key); + return RedisUtil.rpop(nodeList, key, clazz); + } + + + /** + * 从列表中获取指定返回的元素 + * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public static List lrange(String key, int start, int end, Class clazz) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + return RedisUtil.lrange(node, key, start, end, clazz); + } + + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + */ + public static long llen(String key) { + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + return RedisUtil.llen(node, key); + } + + public static void publish(String channel, T message) { + + RedisNode node = RedisGroupManage.instance.randomGetRedisNode(channel); + RedisUtil.publish(node, channel, message); + + } + + public static void subscribe(JedisPubSub pubsub, String... channels) { + + Set set = Sets.newHashSet(); + for (String channel : channels) { + List nodeList = RedisGroupManage.instance.hashSet(channel); + set.addAll(nodeList); + } + + RedisUtil.subscribe(set, pubsub, channels); + } + + public static void subscribe(MessageListener listener, String... channel) { + Subscriber subscriber = new Subscriber(); + subscriber.subscribe(listener, channel); + subscribe(subscriber, channel); + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 5c46ec2f..7d3b5099 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.tools.redis.pubsub; +import com.shinemo.mpush.tools.redis.listener.MessageListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -7,61 +8,66 @@ import redis.clients.jedis.JedisPubSub; -public class Subscriber extends JedisPubSub{ - - private static final Logger log = LoggerFactory.getLogger(Subscriber.class); - - private ListenerDispatcher dispatcher = ListenerDispatcher.instance; - - @Override - public void onMessage(String channel, String message) { - log.warn("onMessage channel:"+channel+","+message); - dispatcher.onMessage(channel, message); - super.onMessage(channel, message); - } - - @Override - public void onPMessage(String pattern, String channel, String message) { - log.warn("onPMessage:"+pattern+","+channel+","+message); - super.onPMessage(pattern, channel, message); - } - - @Override - public void onPSubscribe(String pattern, int subscribedChannels) { - log.warn("onPSubscribe:"+pattern+","+subscribedChannels); - super.onPSubscribe(pattern, subscribedChannels); - } - - @Override - public void onPUnsubscribe(String pattern, int subscribedChannels) { - log.warn("onPUnsubscribe:"+pattern+","+subscribedChannels); - super.onPUnsubscribe(pattern, subscribedChannels); - } - - @Override - public void onSubscribe(String channel, int subscribedChannels) { - log.warn("onSubscribe:"+channel+","+subscribedChannels); - super.onSubscribe(channel, subscribedChannels); - } - - @Override - public void onUnsubscribe(String channel, int subscribedChannels) { - log.warn("onUnsubscribe:"+channel+","+subscribedChannels); - super.onUnsubscribe(channel, subscribedChannels); - } - - - @Override - public void unsubscribe() { - log.warn("unsubscribe:"); - super.unsubscribe(); - } - - @Override - public void unsubscribe(String... channels) { - log.warn("unsubscribe:"+channels); - super.unsubscribe(channels); - } - - +public class Subscriber extends JedisPubSub { + + private static final Logger log = LoggerFactory.getLogger(Subscriber.class); + + private ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; + + @Override + public void onMessage(String channel, String message) { + log.warn("onMessage channel:" + channel + "," + message); + dispatcher.onMessage(channel, message); + super.onMessage(channel, message); + } + + @Override + public void onPMessage(String pattern, String channel, String message) { + log.warn("onPMessage:" + pattern + "," + channel + "," + message); + super.onPMessage(pattern, channel, message); + } + + @Override + public void onPSubscribe(String pattern, int subscribedChannels) { + log.warn("onPSubscribe:" + pattern + "," + subscribedChannels); + super.onPSubscribe(pattern, subscribedChannels); + } + + @Override + public void onPUnsubscribe(String pattern, int subscribedChannels) { + log.warn("onPUnsubscribe:" + pattern + "," + subscribedChannels); + super.onPUnsubscribe(pattern, subscribedChannels); + } + + @Override + public void onSubscribe(String channel, int subscribedChannels) { + log.warn("onSubscribe:" + channel + "," + subscribedChannels); + super.onSubscribe(channel, subscribedChannels); + } + + @Override + public void onUnsubscribe(String channel, int subscribedChannels) { + log.warn("onUnsubscribe:" + channel + "," + subscribedChannels); + super.onUnsubscribe(channel, subscribedChannels); + } + + + @Override + public void unsubscribe() { + log.warn("unsubscribe:"); + super.unsubscribe(); + } + + @Override + public void unsubscribe(String... channels) { + log.warn("unsubscribe:" + channels); + super.unsubscribe(channels); + } + + public void subscribe(MessageListener listener, String... channels) { + for (String channel : channels) { + dispatcher.subscribe(channel, listener); + } + } + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 442e3c65..3762158f 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -3,6 +3,7 @@ import java.util.Date; import java.util.List; +import com.shinemo.mpush.tools.redis.listener.MessageListener; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; @@ -15,78 +16,83 @@ import com.shinemo.mpush.tools.zk.manage.ServerManage; public class RedisGroupManageTest { - - ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(),"3000"); - ServerManage manage = new ServerManage(app); - List groupList = null; - - RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); - RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - @Before - public void init(){ - manage.start(); - groupList = RedisGroupManage.instance.getGroupList(); - } - - @Test - public void testGetRedisGroup(){ - for(RedisGroup group:groupList){ - for(RedisNode node:group.getRedisNodeList()){ - System.out.println(group+ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - - @Test - public void testAdd(){ - User user = RedisManage.get("huang2", User.class); - if(user == null){ - user = new User("hi", 10, new Date()); - RedisManage.set("huang2", user); - user = RedisManage.get("huang2", User.class); - } - System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); - - User nowUser = RedisUtil.get(node, "huang2", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node2, "huang2", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisManage.del("huang2"); - - nowUser = RedisUtil.get(node2, "huang2", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "huang2", User.class); - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - - } - - @Test - public void testPub(){ - for(int i = 0;i<20;i++){ - User user = new User("pub"+i, 10, new Date()); - RedisManage.publish("channel1", user); - RedisManage.publish("channel2", user); - } - } - - @Test - public void testSub(){ - RedisManage.subscribe("channel1","channel2"); - try { - Thread.sleep(Integer.MAX_VALUE); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - + + ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), "3000"); + ServerManage manage = new ServerManage(app); + List groupList = null; + + RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); + RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + @Before + public void init() { + manage.start(); + groupList = RedisGroupManage.instance.getGroupList(); + } + + @Test + public void testGetRedisGroup() { + for (RedisGroup group : groupList) { + for (RedisNode node : group.getRedisNodeList()) { + System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); + } + + } + } + + @Test + public void testAdd() { + User user = RedisManage.get("huang2", User.class); + if (user == null) { + user = new User("hi", 10, new Date()); + RedisManage.set("huang2", user); + user = RedisManage.get("huang2", User.class); + } + System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); + + User nowUser = RedisUtil.get(node, "huang2", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.get(node2, "huang2", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisManage.del("huang2"); + + nowUser = RedisUtil.get(node2, "huang2", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.get(node, "huang2", User.class); + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + + } + + @Test + public void testPub() { + for (int i = 0; i < 20; i++) { + User user = new User("pub" + i, 10, new Date()); + RedisManage.publish("channel1", user); + RedisManage.publish("channel2", user); + } + } + + @Test + public void testSub() { + RedisManage.subscribe(new MessageListener() { + @Override + public void onMessage(String channel, String message) { + System.out.printf("on message channel=%s, message=%s%n", channel, message); + } + }, "channel1", "channel2"); + try { + Thread.sleep(Integer.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } From c9a6f9574f831bf95ec573149b356a237b79fb65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 08:21:50 +0000 Subject: [PATCH 100/890] add redis subscribe --- .../shinemo/mpush/tools/redis/listener/ListenerDispatcher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java index 0ec2ecea..d2170f7d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java @@ -15,7 +15,7 @@ public class ListenerDispatcher implements MessageListener { private Map> subscribes = Maps.newTreeMap(); - private Executor executor = Executors.newFixedThreadPool(10); + private Executor executor = Executors.newFixedThreadPool(5); @Override public void onMessage(final String channel, final String message) { From 78e8c739fc99ddcc0c8d0a201fbd25272d06d9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 5 Jan 2016 11:23:40 +0000 Subject: [PATCH 101/890] add redis subscribe --- config.properties | 4 +- .../mpush/common/security/CipherBox.java | 83 ++++--------------- mpush-core/pom.xml | 3 - .../main/java/com/shinemo/mpush/core/App.java | 72 ++++++++++++++++ .../src/main/resources/config.properties | 7 ++ mpush-core/src/main/resources/logback.xml | 30 +++++++ .../mpush/core/netty/NettyClientTest.java | 9 +- .../src/test/resources/config.properties | 4 +- .../com/shinemo/mpush/tools/ConfigCenter.java | 24 ++++++ .../shinemo/mpush/tools/redis/RedisUtil.java | 25 +++--- 10 files changed, 176 insertions(+), 85 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/App.java create mode 100644 mpush-core/src/main/resources/config.properties create mode 100644 mpush-core/src/main/resources/logback.xml diff --git a/config.properties b/config.properties index 5523e261..1e1213f2 100644 --- a/config.properties +++ b/config.properties @@ -2,4 +2,6 @@ MAX_PACKET_SIZE=10240 COMPRESS_LIMIT=10240 MIN_HEARTBEAT=10000 MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 \ No newline at end of file +MAX_HB_TIMEOUT_TIMES=2 +PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java index 81e1deae..951d536a 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java @@ -1,12 +1,8 @@ package com.shinemo.mpush.common.security; import com.shinemo.mpush.tools.ConfigCenter; -import com.shinemo.mpush.tools.Pair; import com.shinemo.mpush.tools.crypto.RSAUtils; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; @@ -21,72 +17,27 @@ public final class CipherBox { private RSAPrivateKey privateKey; private RSAPublicKey publicKey; - public CipherBox() { - init(); - } - - public void init() { - readFromFile(); - if (publicKey == null || privateKey == null) { - Pair pair = RSAUtils.genKeyPair(); - //生成公钥和私钥 - publicKey = pair.key; - privateKey = pair.value; - //模 - String modulus = publicKey.getModulus().toString(); - //公钥指数 - String public_exponent = publicKey.getPublicExponent().toString(); - //私钥指数 - String private_exponent = privateKey.getPrivateExponent().toString(); - //使用模和指数生成公钥和私钥 - publicKey = RSAUtils.getPublicKey(modulus, public_exponent); - privateKey = RSAUtils.getPrivateKey(modulus, private_exponent); - writeToFile(); - } - } - - private void writeToFile() { - try { - String publicKeyStr = RSAUtils.encodeBase64(publicKey); - String privateKeyStr = RSAUtils.encodeBase64(privateKey); - String path = this.getClass().getResource("/").getPath(); - FileOutputStream out = new FileOutputStream(new File(path, "private.key")); - out.write(privateKeyStr.getBytes()); - out.close(); - out = new FileOutputStream(new File(path, "public.key")); - out.write(publicKeyStr.getBytes()); - out.close(); - System.out.println("write key=" + path); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private void readFromFile() { - try { - InputStream in = this.getClass().getResourceAsStream("/private.key"); - byte[] buffer = new byte[in.available()]; - in.read(buffer); - in.close(); - privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(new String(buffer)); - in = this.getClass().getResourceAsStream("/public.key"); - in.read(buffer); - in.close(); - publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(new String(buffer)); - System.out.println("save privateKey=" + privateKey); - System.out.println("save publicKey=" + publicKey); - } catch (Exception e) { - e.printStackTrace(); - } - } - public RSAPrivateKey getPrivateKey() { - if (privateKey == null) init(); + if (privateKey == null) { + String key = ConfigCenter.INSTANCE.getPrivateKey(); + try { + privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(key); + } catch (Exception e) { + throw new RuntimeException("load private key ex, key=" + key, e); + } + } return privateKey; } public RSAPublicKey getPublicKey() { - if (publicKey == null) init(); + if (publicKey == null) { + String key = ConfigCenter.INSTANCE.getPublicKey(); + try { + publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(key); + } catch (Exception e) { + throw new RuntimeException("load public key ex, key=" + key, e); + } + } return publicKey; } @@ -119,6 +70,6 @@ public int getAesKeyLength() { } public RsaCipher getRsaCipher() { - return new RsaCipher(privateKey, publicKey); + return new RsaCipher(getPrivateKey(), getPublicKey()); } } diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index c082fb4b..8399b1ad 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -66,9 +66,6 @@ src/main/resources - - **/*.xml - true diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java new file mode 100644 index 00000000..8a028675 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -0,0 +1,72 @@ +package com.shinemo.mpush.core; + +import com.shinemo.mpush.core.server.ConnectionServer; +import com.shinemo.mpush.core.server.GatewayServer; +import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +import java.io.IOException; + +/** + * Created by ohun on 2016/1/5. + */ +public final class App { + private static final App APP = new App(); + + public static void main(String[] args) throws Exception { + APP.init(); + APP.startRedisClient(); + APP.startConnectionServer(); + APP.startGatewayServer(); + APP.startZKClient(); + } + + private void init() throws IOException { + ConfigCenter.INSTANCE.init(); + } + + public void startRedisClient() { + + } + + public void startConnectionServer() { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + int port = ConfigCenter.INSTANCE.getConnectionServerPort(); + ConnectionServer server = new ConnectionServer(port); + server.init(); + server.start(); + } + }); + t.setDaemon(false); + t.setName("conn-server-thread"); + t.start(); + } + + public void startGatewayServer() { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + int port = ConfigCenter.INSTANCE.getGatewayServerPort(); + GatewayServer server = new GatewayServer(port); + server.init(); + server.start(); + } + }); + t.setDaemon(false); + t.setName("gateway-server-thread"); + t.start(); + } + + public void startZKClient() { + //register remote ip for allocate + //register local ip for push client + ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), + Integer.toString(ConfigCenter.INSTANCE.getConnectionServerPort())); + ServerManage manage = new ServerManage(app); + manage.start(); + } +} diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties new file mode 100644 index 00000000..1e1213f2 --- /dev/null +++ b/mpush-core/src/main/resources/config.properties @@ -0,0 +1,7 @@ +MAX_PACKET_SIZE=10240 +COMPRESS_LIMIT=10240 +MIN_HEARTBEAT=10000 +MAX_HEARTBEAT=1800000 +MAX_HB_TIMEOUT_TIMES=2 +PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-core/src/main/resources/logback.xml b/mpush-core/src/main/resources/logback.xml new file mode 100644 index 00000000..20979b40 --- /dev/null +++ b/mpush-core/src/main/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index f0ab0873..a81df4cc 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -3,8 +3,10 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.tools.ConfigCenter; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,8 +18,13 @@ public class NettyClientTest { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); + @Before + public void setUp() throws Exception { + ConfigCenter.INSTANCE.init(); + } + private String host = "127.0.0.1"; - private int port = 3000; + private int port = ConfigCenter.INSTANCE.getConnectionServerPort(); private ClientChannelHandler handler = new ClientChannelHandler(); @Test diff --git a/mpush-core/src/test/resources/config.properties b/mpush-core/src/test/resources/config.properties index 5523e261..1e1213f2 100644 --- a/mpush-core/src/test/resources/config.properties +++ b/mpush-core/src/test/resources/config.properties @@ -2,4 +2,6 @@ MAX_PACKET_SIZE=10240 COMPRESS_LIMIT=10240 MIN_HEARTBEAT=10000 MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 \ No newline at end of file +MAX_HB_TIMEOUT_TIMES=2 +PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java index 4e40f79d..faa7a06f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java @@ -16,9 +16,17 @@ public final class ConfigCenter { private int maxHBTimeoutTimes = 2; private int rasKeyLength = 1024; private int aesKeyLength = 16; + private int connectionServerPort = 3000; + private int gatewayServerPort = 4000; + private String privateKey; + private String publicKey; public void init() throws IOException { cfg.load(this.getClass().getResourceAsStream("/config.properties")); + connectionServerPort = getInt("CONNECTION_SERVER_PORT", connectionServerPort); + gatewayServerPort = getInt("GATEWAY_SERVER_PORT", gatewayServerPort); + privateKey = getString("PRIVATE_KEY", privateKey); + publicKey = getString("PUBLIC_KEY", privateKey); maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); compressLimit = getInt("COMPRESS_LIMIT", compressLimit); minHeartbeat = getInt("MIN_HEARTBEAT", minHeartbeat); @@ -75,4 +83,20 @@ public int getRasKeyLength() { public int getAesKeyLength() { return aesKeyLength; } + + public int getConnectionServerPort() { + return connectionServerPort; + } + + public int getGatewayServerPort() { + return gatewayServerPort; + } + + public String getPrivateKey() { + return privateKey; + } + + public String getPublicKey() { + return publicKey; + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index b9c7a28e..3de8df2d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -1,25 +1,21 @@ package com.shinemo.mpush.tools.redis; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPubSub; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + public class RedisUtil { private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); @@ -476,12 +472,15 @@ public static void publish(RedisNode node, String channel, T message) { public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { for (final RedisNode node : nodeList) { - new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { @Override public void run() { subscribe(node, pubsub, channels); } - }).start(); + }); + t.setDaemon(true); + t.setName("redis-subscribe-thread"); + t.start(); } } From f9a959757c38cec7a9c23a5c55b02c285ca93557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 6 Jan 2016 00:34:45 +0800 Subject: [PATCH 102/890] add redis cluster test --- .../shinemo/mpush/tools/redis/RedisUtil.java | 2 - .../redis/listener/ListenerDispatcher.java | 13 +++--- .../mpush/tools/redis/manage/RedisManage.java | 5 ++- .../mpush/tools/redis/RedisClusterTest.java | 45 +++++++++++++++++++ .../tools/redis/RedisGroupManageTest.java | 12 +++++ 5 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index b9c7a28e..01b656da 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -4,8 +4,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java index d2170f7d..a409bb0e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java @@ -1,12 +1,11 @@ package com.shinemo.mpush.tools.redis.listener; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; public class ListenerDispatcher implements MessageListener { @@ -23,22 +22,22 @@ public void onMessage(final String channel, final String message) { if (listeners == null) { return; } - for (final MessageListener l : listeners) { + for (final MessageListener listener : listeners) { executor.execute(new Runnable() { @Override public void run() { - l.onMessage(channel, message); + listener.onMessage(channel, message); } }); } } - public void subscribe(String channel, MessageListener l) { + public void subscribe(String channel, MessageListener listener) { List listeners = subscribes.get(channel); if (listeners == null) { - listeners = new ArrayList<>(); + listeners = Lists.newArrayList(); subscribes.put(channel, listeners); } - listeners.add(l); + listeners.add(listener); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index d4fed50f..24d946f5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -13,7 +13,10 @@ import com.shinemo.mpush.tools.redis.RedisUtil; import com.shinemo.mpush.tools.redis.pubsub.Subscriber; - +/** + * redis 对外封装接口 + * + */ public class RedisManage { /********************* diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java new file mode 100644 index 00000000..ddace43c --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; +import org.junit.Test; + +import com.shinemo.mpush.tools.Jsons; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; + +public class RedisClusterTest { + + Set jedisClusterNodes = new HashSet(); + + JedisCluster cluster = null; + + @Before + public void init() { + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); + cluster = new JedisCluster(jedisClusterNodes,RedisPoolConfig.config); + } + + @Test + public void test() { + + User user = new User("huang", 18, new Date()); + cluster.set("huang", Jsons.toJson(user)); + String ret = cluster.get("huang"); + User newUser = Jsons.fromJson(ret, User.class); + System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 3762158f..34523b4f 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -4,6 +4,7 @@ import java.util.List; import com.shinemo.mpush.tools.redis.listener.MessageListener; + import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; @@ -12,6 +13,7 @@ import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.shinemo.mpush.tools.redis.pubsub.Subscriber; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -93,6 +95,16 @@ public void onMessage(String channel, String message) { e.printStackTrace(); } } + + @Test + public void testSub2(){ + RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); + try { + Thread.sleep(Integer.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } From bd01a35fece28d066a868f0b2ae0408e50b7028d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 6 Jan 2016 05:49:22 +0000 Subject: [PATCH 103/890] add App Main --- .../java/com/shinemo/mpush/api/Server.java | 11 +- .../main/java/com/shinemo/mpush/core/App.java | 74 ++++- .../src/main/resources/config.properties | 1 + .../core/netty/ClientChannelHandler.java | 4 +- .../mpush/core/netty/NettyServerTest.java | 13 +- .../mpush/netty/server/NettyServer.java | 26 +- .../com/shinemo/mpush/tools/ConfigCenter.java | 27 +- .../com/shinemo/mpush/tools/Constants.java | 2 - .../com/shinemo/mpush/tools/zk/ZkUtil.java | 183 ++++++------ .../shinemo/mpush/tools/zk/ZkUtilTest.java | 264 +++++++++--------- .../src/test/resources/config.properties | 8 + 11 files changed, 362 insertions(+), 251 deletions(-) create mode 100644 mpush-tools/src/test/resources/config.properties diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java index 344003fc..a86cbeba 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java @@ -4,9 +4,16 @@ * Created by ohun on 2015/12/24. */ public interface Server { - void start(); - void stop(); + void start(Listener listener); + + void stop(Listener listener); boolean isRunning(); + + interface Listener { + void onSuccess(); + + void onFailure(String message); + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index 8a028675..a7d172d5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -1,44 +1,66 @@ package com.shinemo.mpush.core; +import com.google.common.collect.Lists; +import com.shinemo.mpush.api.Server; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.manage.ServerManage; +import org.apache.zookeeper.ZKUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.awt.*; import java.io.IOException; +import java.util.List; /** * Created by ohun on 2016/1/5. */ public final class App { + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final App APP = new App(); public static void main(String[] args) throws Exception { + LOGGER.error("mpush app start begin...."); APP.init(); - APP.startRedisClient(); APP.startConnectionServer(); APP.startGatewayServer(); - APP.startZKClient(); + LOGGER.error("mpush app start end...."); } private void init() throws IOException { ConfigCenter.INSTANCE.init(); - } - - public void startRedisClient() { - + LOGGER.error("mpush app config center init success...."); } public void startConnectionServer() { Thread t = new Thread(new Runnable() { @Override public void run() { - int port = ConfigCenter.INSTANCE.getConnectionServerPort(); + final int port = ConfigCenter.INSTANCE.getConnectionServerPort(); ConnectionServer server = new ConnectionServer(port); server.init(); - server.start(); + server.start(new Server.Listener() { + @Override + public void onSuccess() { + registerServerToZK(port); + LOGGER.error("mpush app start connection server success...."); + } + + @Override + public void onFailure(String message) { + LOGGER.error("mpush app start connection server failure, jvm exit with code -1"); + System.exit(-1); + } + }); } }); t.setDaemon(false); @@ -50,10 +72,22 @@ public void startGatewayServer() { Thread t = new Thread(new Runnable() { @Override public void run() { - int port = ConfigCenter.INSTANCE.getGatewayServerPort(); + final int port = ConfigCenter.INSTANCE.getGatewayServerPort(); GatewayServer server = new GatewayServer(port); server.init(); - server.start(); + server.start(new Server.Listener() { + @Override + public void onSuccess() { + registerServerToZK(port); + LOGGER.error("mpush app start gateway server success...."); + } + + @Override + public void onFailure(String message) { + System.exit(-2); + LOGGER.error("mpush app start gateway server failure, jvm exit with code -2"); + } + }); } }); t.setDaemon(false); @@ -61,12 +95,22 @@ public void run() { t.start(); } - public void startZKClient() { - //register remote ip for allocate - //register local ip for push client - ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), - Integer.toString(ConfigCenter.INSTANCE.getConnectionServerPort())); + private void registerServerToZK(int port) { + String p = Integer.toString(port); + ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), p); ServerManage manage = new ServerManage(app); manage.start(); + LOGGER.error("mpush app register server:{} to zk success", p); + } + + public void startRedisClient() { + RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); + + RedisGroup group1 = new RedisGroup(); + group1.addRedisNode(node1); + + List groupList = Lists.newArrayList(group1); + ZkUtil.instance.registerPersist(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress()) + , Jsons.toJson(groupList)); } } diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties index 1e1213f2..ef2b80aa 100644 --- a/mpush-core/src/main/resources/config.properties +++ b/mpush-core/src/main/resources/config.properties @@ -1,3 +1,4 @@ +ZK_SERVER=10.1.20.74:2181 MAX_PACKET_SIZE=10240 COMPRESS_LIMIT=10240 MIN_HEARTBEAT=10000 diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 50cb365e..552c328e 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -29,8 +29,8 @@ public class ClientChannelHandler extends ChannelHandlerAdapter { private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); private byte[] iv = CipherBox.INSTANCE.randomAESIV(); private Connection connection = new NettyConnection(); - private String deviceId = "test-device-id-100" + new Random(5).nextInt(); - private String userId = "1010"; + private String deviceId = "test-device-id-100" + new Random().nextInt(5); + private String userId = "user_" + new Random().nextInt(5); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index ca215578..4f88fa7d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.core.netty; +import com.shinemo.mpush.api.Server; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; import com.shinemo.mpush.tools.ConfigCenter; @@ -14,6 +15,16 @@ public void testStart() throws Exception { ConfigCenter.INSTANCE.init(); ConnectionServer server = new ConnectionServer(3000); server.init(); - server.start(); + server.start(new Server.Listener() { + @Override + public void onSuccess() { + + } + + @Override + public void onFailure(String message) { + + } + }); } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 4f455fc6..2d91c8f7 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -39,7 +39,7 @@ public boolean isRunning() { } @Override - public void stop() { + public void stop(Listener listener) { LOGGER.info("netty server stop now"); this.startFlag.set(false); if (workerGroup != null) workerGroup.shutdownGracefully(); @@ -47,7 +47,7 @@ public void stop() { } @Override - public void start() { + public void start(final Listener listener) { if (!startFlag.compareAndSet(false, true)) { return; } @@ -113,22 +113,32 @@ public void initChannel(SocketChannel ch) throws Exception { /*** * 绑定端口并启动去接收进来的连接 */ - ChannelFuture f = b.bind(port).sync(); - - LOGGER.info("server start ok on:" + port); - + ChannelFuture f = b.bind(port).sync().addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + LOGGER.info("server start success on:" + port); + if (listener != null) listener.onSuccess(); + } else { + LOGGER.error("server start failure on:" + port); + if (listener != null) listener.onFailure("start server failure"); + } + } + }); /** * 这里会一直等待,直到socket被关闭 */ f.channel().closeFuture().sync(); - } catch (Exception e) { LOGGER.error("server start exception", e); + if (listener != null) listener.onFailure("start server ex=" + e.getMessage()); + throw new RuntimeException("server start exception, port=" + port, e); + } finally { /*** * 优雅关闭 */ - stop(); + stop(null); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java index faa7a06f..1b747fed 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java @@ -18,15 +18,22 @@ public final class ConfigCenter { private int aesKeyLength = 16; private int connectionServerPort = 3000; private int gatewayServerPort = 4000; - private String privateKey; - private String publicKey; + private String privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; + private String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; + private String zkNamespace = "mpush"; + private String zkServer = "127.0.0.1:2181"; + private String redisServer = "127.0.0.1:6379:ShineMoIpo"; public void init() throws IOException { cfg.load(this.getClass().getResourceAsStream("/config.properties")); - connectionServerPort = getInt("CONNECTION_SERVER_PORT", connectionServerPort); - gatewayServerPort = getInt("GATEWAY_SERVER_PORT", gatewayServerPort); privateKey = getString("PRIVATE_KEY", privateKey); publicKey = getString("PUBLIC_KEY", privateKey); + zkNamespace = getString("ZK_NAMESPACE", zkNamespace); + zkServer = getString("ZK_SERVER", zkServer); + redisServer = getString("REDIS_SERVER", redisServer); + + gatewayServerPort = getInt("GATEWAY_SERVER_PORT", gatewayServerPort); + connectionServerPort = getInt("CONNECTION_SERVER_PORT", connectionServerPort); maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); compressLimit = getInt("COMPRESS_LIMIT", compressLimit); minHeartbeat = getInt("MIN_HEARTBEAT", minHeartbeat); @@ -99,4 +106,16 @@ public String getPrivateKey() { public String getPublicKey() { return publicKey; } + + public String getRedisServer() { + return redisServer; + } + + public String getZkServer() { + return zkServer; + } + + public String getZkNamespace() { + return zkNamespace; + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index b833d7a8..5f538792 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -29,10 +29,8 @@ public interface Constants { int ZK_CONNECTION_TIMEOUT = 5000; String ZK_DEFAULT_CACHE_PATH = "/"; String ZK_DEFAULT_DIGEST = "shinemo"; - String ZK_IPS = "127.0.0.1:2181"; //zk cs - String ZK_NAME_SPACE = "mpush"; //所有机器启动的时候注册ip的地方 String ZK_REGISTER_HOST = "/allhost"; String ZK_REGISTER_PREFIX_NAME = "machine"; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java index a2eaf8ad..96cba66b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.List; +import com.shinemo.mpush.tools.ConfigCenter; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -22,80 +23,74 @@ import org.slf4j.LoggerFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; -import com.shinemo.mpush.tools.Constants; - public class ZkUtil { - private static final Logger log = LoggerFactory.getLogger(ZkUtil.class); - - private static final ZkConfig config = new ZkConfig(Constants.ZK_IPS, Constants.ZK_NAME_SPACE); + private static final Logger LOGGER = LoggerFactory.getLogger(ZkUtil.class); + + public static final ZkUtil instance = new ZkUtil(); - public static final ZkUtil instance = new ZkUtil(config); + private ZkConfig zkConfig; - static { - instance.init(); - } - - private ZkConfig zkConfig; - private CuratorFramework client; + private CuratorFramework client; private TreeCache cache; - private ZkUtil(ZkConfig zkConfig) { - this.zkConfig = zkConfig; - } - - public ZkConfig getZkConfig() { - return zkConfig; - } + private ZkUtil() { + init(); + } + + public ZkConfig getZkConfig() { + return zkConfig; + } + + public CuratorFramework getClient() { + return client; + } - public CuratorFramework getClient() { - return client; - } - - /** - * 初始化 - */ - public void init() { - log.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); - Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) - .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); - if (zkConfig.getConnectionTimeout() > 0) { - builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); - } - if (zkConfig.getSessionTimeout() > 0) { - builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); - } - if (StringUtils.isNoneBlank(zkConfig.getDigest())) { - builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { + /** + * 初始化 + */ + public void init() { + zkConfig = new ZkConfig(ConfigCenter.INSTANCE.getZkServer(), ConfigCenter.INSTANCE.getZkNamespace()); + LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); + Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); + if (zkConfig.getConnectionTimeout() > 0) { + builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); + } + if (zkConfig.getSessionTimeout() > 0) { + builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); + } + if (StringUtils.isNoneBlank(zkConfig.getDigest())) { + builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { - @Override - public List getDefaultAcl() { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } + @Override + public List getDefaultAcl() { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } - @Override - public List getAclForPath(final String path) { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - }); - } - client = builder.build(); - client.start(); - try { - client.blockUntilConnected(); - cacheData(); - } catch (final Exception ex) { - log.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); - } + @Override + public List getAclForPath(final String path) { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + }); + } + client = builder.build(); + client.start(); + try { + client.blockUntilConnected(); + cacheData(); + } catch (final Exception ex) { + LOGGER.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); + } - } - - //本地缓存 + } + + //本地缓存 public void cacheData() throws Exception { cache = new TreeCache(client, zkConfig.getLocalCachePath()); cache.start(); } - + private void waitClose() { try { Thread.sleep(600); @@ -103,23 +98,24 @@ private void waitClose() { Thread.currentThread().interrupt(); } } - + /** * 关闭 */ - public void close() { + public void close() { if (null != cache) { cache.close(); } - waitClose(); + waitClose(); CloseableUtils.closeQuietly(client); } - - /** - * 获取数据,先从本地获取,本地找不到,从远程获取 - * @param key - * @return - */ + + /** + * 获取数据,先从本地获取,本地找不到,从远程获取 + * + * @param key + * @return + */ public String get(final String key) { if (null == cache) { return null; @@ -130,9 +126,10 @@ public String get(final String key) { } return getFromRemote(key); } - + /** * 从远程获取数据 + * * @param key * @return */ @@ -140,13 +137,14 @@ public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Charset.forName("UTF-8")); } catch (final Exception ex) { - log.error("getDirectly" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE),ex); + LOGGER.error("getDirectly" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); return null; } } - + /** * 获取子节点 + * * @param key * @return */ @@ -162,13 +160,14 @@ public int compare(final String o1, final String o2) { }); return result; } catch (final Exception ex) { - log.error("getChildrenKeys" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE),ex); + LOGGER.error("getChildrenKeys" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); return Collections.emptyList(); } } - + /** * 判断路径是否存在 + * * @param key * @return */ @@ -176,13 +175,14 @@ public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); } catch (final Exception ex) { - log.error("isExisted" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE),ex); + LOGGER.error("isExisted" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); return false; } } - + /** * 持久化数据 + * * @param key * @param value */ @@ -194,12 +194,13 @@ public void registerPersist(final String key, final String value) { update(key, value); } } catch (final Exception ex) { - log.error("persist" + key+","+value,ex); + LOGGER.error("persist" + key + "," + value, ex); } } - + /** * 更新数据 + * * @param key * @param value */ @@ -207,12 +208,13 @@ public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); } catch (final Exception ex) { - log.error("update" + key+","+value,ex); + LOGGER.error("update" + key + "," + value, ex); } } - + /** * 注册临时数据 + * * @param key * @param value */ @@ -223,46 +225,49 @@ public void registerEphemeral(final String key, final String value) { } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); } catch (final Exception ex) { - log.error("persistEphemeral" + key+","+value,ex); + LOGGER.error("persistEphemeral" + key + "," + value, ex); } } - + /** * 注册临时顺序数据 + * * @param key */ - public void registerEphemeralSequential(final String key,final String value) { + public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); } catch (final Exception ex) { - log.error("persistEphemeralSequential" + key,ex); + LOGGER.error("persistEphemeralSequential" + key, ex); } } - + /** * 注册临时顺序数据 + * * @param key */ public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { - log.error("persistEphemeralSequential" + key,ex); + LOGGER.error("persistEphemeralSequential" + key, ex); } } - + /** * 删除数据 + * * @param key */ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - log.error("remove" + key,ex); + LOGGER.error("remove" + key, ex); } } - + public TreeCache getCache() { return cache; } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 62a35773..cac382db 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -2,8 +2,10 @@ import java.util.List; +import com.shinemo.mpush.tools.ConfigCenter; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; import org.junit.Test; import com.google.common.collect.Lists; @@ -14,133 +16,139 @@ import com.shinemo.mpush.tools.redis.RedisNode; public class ZkUtilTest { - - private ZkUtil zkUtil = ZkUtil.instance; - - @Test - public void test(){ - - String dubbo = zkUtil.get("/dubbo"); - System.out.println(dubbo); - - List child = zkUtil.getChildrenKeys("/dubbo"); - System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); - - zkUtil.registerPersist("/hi", "hello world"); - - zkUtil.registerEphemeral("/huang", "hi"); - zkUtil.registerEphemeralSequential("/huang2"); - - String huang = zkUtil.get("/huang"); - System.out.println(huang); - - String huang2 = zkUtil.get("/huang2"); - System.out.println(huang2); - - } - - @Test - public void getTest(){ - String value = zkUtil.get("/hi"); - System.out.println(value); - } - - /** - * 注册机器到/mpush/allhost 目录下边 - */ - @Test - public void testRegister(){ - - String path = "/"+Constants.ZK_NAME_SPACE; - - String prefix = Constants.ZK_REGISTER_PREFIX_NAME; - - List hosts = zkUtil.getChildrenKeys(path); - - System.out.println("before register"); - - for(int i = 0;i groupList = Lists.newArrayList(group1,group2); - - zkUtil.registerPersist(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress()), Jsons.toJson(groupList)); - - - - } - - @Test - public void getRedisTest(){ - String value = zkUtil.get(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); - List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); - for(RedisGroup group:newGroupList){ - for(RedisNode node:group.getRedisNodeList()){ - System.out.println(group+ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - + + private ZkUtil zkUtil; + + @Before + public void setUp() throws Exception { + ConfigCenter.INSTANCE.init(); + zkUtil = ZkUtil.instance; + + } + + @Test + public void test() { + + String dubbo = zkUtil.get("/dubbo"); + System.out.println(dubbo); + + List child = zkUtil.getChildrenKeys("/dubbo"); + System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); + + zkUtil.registerPersist("/hi", "hello world"); + + zkUtil.registerEphemeral("/huang", "hi"); + zkUtil.registerEphemeralSequential("/huang2"); + + String huang = zkUtil.get("/huang"); + System.out.println(huang); + + String huang2 = zkUtil.get("/huang2"); + System.out.println(huang2); + + } + + @Test + public void getTest() { + String value = zkUtil.get("/hi"); + System.out.println(value); + } + + /** + * 注册机器到/mpush/allhost 目录下边 + */ + @Test + public void testRegister() { + + String path = "/" + zkUtil.getZkConfig().getNamespace(); + + String prefix = Constants.ZK_REGISTER_PREFIX_NAME; + + List hosts = zkUtil.getChildrenKeys(path); + + System.out.println("before register"); + + for (int i = 0; i < hosts.size(); i++) { + String value = zkUtil.get(hosts.get(i)); + System.out.println(hosts.get(i) + "," + value); + } + + System.out.println("start register"); + + zkUtil.registerEphemeralSequential(path + "/" + prefix); + + zkUtil.registerEphemeralSequential(path + "/" + prefix); + + hosts = zkUtil.getChildrenKeys(path); + + for (int i = 0; i < hosts.size(); i++) { + String value = zkUtil.get(path + "/" + hosts.get(i)); + System.out.println(hosts.get(i) + "," + value); + } + + System.out.println("end register"); + + } + + @Test + public void testLocalIp() { + System.out.println(InetAddressUtil.getInetAddress()); + + } + + @Test + public void testRegisterIp() { + String localIp = InetAddressUtil.getInetAddress(); + ServerApp app = new ServerApp(localIp, "3000"); + zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); + String value = zkUtil.get("/" + localIp); + System.out.println(value); + } + + @Test + public void testRemove() { + zkUtil.remove("/"); + } + + @Test + public void testAddKickOff() { + String localIp = InetAddressUtil.getInetAddress(); + String kick = Constants.ZK_KICK; + String ip = "10.1.10.65"; + zkUtil.registerEphemeral("/" + localIp + "/" + kick, ip); + + } + + @Test + public void testAddRedis() { + + RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); + //RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + RedisGroup group1 = new RedisGroup(); + group1.addRedisNode(node1); + + /*RedisGroup group2 = new RedisGroup(); + group2.addRedisNode(node2);*/ + + List groupList = Lists.newArrayList(group1); + + zkUtil.registerPersist(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress()), Jsons.toJson(groupList)); + + + } + + @Test + public void getRedisTest() { + String value = zkUtil.get(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); + List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); + for (RedisGroup group : newGroupList) { + for (RedisNode node : group.getRedisNodeList()) { + System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); + } + + } + } + } diff --git a/mpush-tools/src/test/resources/config.properties b/mpush-tools/src/test/resources/config.properties new file mode 100644 index 00000000..ef2b80aa --- /dev/null +++ b/mpush-tools/src/test/resources/config.properties @@ -0,0 +1,8 @@ +ZK_SERVER=10.1.20.74:2181 +MAX_PACKET_SIZE=10240 +COMPRESS_LIMIT=10240 +MIN_HEARTBEAT=10000 +MAX_HEARTBEAT=1800000 +MAX_HB_TIMEOUT_TIMES=2 +PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file From 96e70d7878bf7d651e56c83983159d35cccca22d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 01:39:02 +0000 Subject: [PATCH 104/890] =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 88 +++++++++++++++++++ config.properties | 1 + mpush-api/pom.xml | 45 +--------- .../mpush/api/router/ClientLocation.java | 2 - mpush-core/pom.xml | 76 +++++++++++----- .../mpush/core/router/RouterCenter.java | 3 +- mpush-netty/pom.xml | 37 ++------ mpush-tools/pom.xml | 26 +----- pom.xml | 81 +++++++++++------ 9 files changed, 210 insertions(+), 149 deletions(-) create mode 100644 assembly.xml diff --git a/assembly.xml b/assembly.xml new file mode 100644 index 00000000..3946f881 --- /dev/null +++ b/assembly.xml @@ -0,0 +1,88 @@ + + + jar-with-dependency + mpush + true + + tar.gz + + + + + mpush-core/target/ + + + lib/*.jar + + + + mpush-core/target/ + + + mpush.jar + + + + + + + + false + runtime + false + lib + + + + \ No newline at end of file diff --git a/config.properties b/config.properties index 1e1213f2..ef2b80aa 100644 --- a/config.properties +++ b/config.properties @@ -1,3 +1,4 @@ +ZK_SERVER=10.1.20.74:2181 MAX_PACKET_SIZE=10240 COMPRESS_LIMIT=10240 MIN_HEARTBEAT=10000 diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index ac06d525..89d79c1a 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -15,47 +15,12 @@ - org.slf4j - slf4j-api - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring - - - com.shinemo.mpush - mpush-tools + io.netty + netty-transport - - - - src/main/resources - - **/*.xml - - true - - - org.apache.maven.plugins @@ -67,12 +32,6 @@ UTF-8 - - org.apache.maven.plugins - maven-resources-plugin - 2.6 - - diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java index d1a38080..4233fbae 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.api.router; import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.tools.MPushUtil; /** * Created by ohun on 2015/12/23. @@ -51,7 +50,6 @@ public static ClientLocation from(SessionContext context) { config.osName = context.osName; config.clientVersion = context.clientVersion; config.deviceId = context.deviceId; - config.host = MPushUtil.getLocalIp(); return config; } } diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 8399b1ad..4f4c08ce 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -13,18 +13,10 @@ 1.0-SNAPSHOT jar - - com.shinemo.mpush - mpush-api - com.shinemo.mpush mpush-netty - - org.slf4j - slf4j-api - org.slf4j jcl-over-slf4j @@ -45,23 +37,10 @@ org.logback-extensions logback-ext-spring - - - io.netty - netty-all - - - - com.google.guava - guava - - - com.shinemo.mpush - mpush-tools - + mpush @@ -86,7 +65,58 @@ maven-resources-plugin 2.6 - + + + maven-dependency-plugin + + + copy + package + + copy-dependencies + + + + ${project.build.directory}/lib + + + + + + + maven-jar-plugin + + + + + false + + + + true + + lib/ + + com.shinemo.mpush.core.App + + + + + + package + + + + + diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 74b85a3b..2c2e802e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -7,6 +7,7 @@ import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; +import com.shinemo.mpush.tools.MPushUtil; /** * Created by ohun on 2015/12/23. @@ -27,7 +28,7 @@ public class RouterCenter { */ public boolean register(String userId, Connection connection) { ClientLocation connConfig = ClientLocation.from(connection.getSessionContext()); - + connConfig.setHost(MPushUtil.getLocalIp()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(connConfig); diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index df917b5b..27db2823 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -16,41 +16,16 @@ com.shinemo.mpush - mpush-api - - - com.shinemo.mpush - mpush-tools - - - org.slf4j - slf4j-api - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging + mpush-common - log4j - log4j + io.netty + netty-transport - org.logback-extensions - logback-ext-spring + io.netty + netty-codec - - com.shinemo.mpush - mpush-common - 1.0-SNAPSHOT - - + diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index cee32028..6bba15b3 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -34,32 +34,12 @@ curator-x-discovery - org.slf4j - slf4j-api + redis.clients + jedis org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring - - - redis.clients - jedis + slf4j-api diff --git a/pom.xml b/pom.xml index b42828b3..26a7b611 100644 --- a/pom.xml +++ b/pom.xml @@ -27,11 +27,11 @@ 1.7 4.0.0.RELEASE 1.0-SNAPSHOT + 1.0-SNAPSHOT + 1.0-SNAPSHOT 1.0-SNAPSHOT - 1.0-SNAPSHOT 1.0-SNAPSHOT - 1.0-SNAPSHOT - 1.0-SNAPSHOT + 1.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT @@ -41,16 +41,19 @@ - io.netty - netty-all + netty-codec + ${netty.version} + + + io.netty + netty-transport ${netty.version} - @@ -66,14 +69,14 @@ 4.10 - org.apache.curator - curator-recipes - 2.9.1 + org.apache.curator + curator-recipes + 2.9.1 - org.apache.curator - curator-x-discovery - 2.9.1 + org.apache.curator + curator-x-discovery + 2.9.1 @@ -84,6 +87,16 @@ mpush-api ${mpush-api-version} + + com.shinemo.mpush + mpush-tools + ${mpush-tools-version} + + + com.shinemo.mpush + mpush-common + ${mpush-common-version} + com.shinemo.mpush mpush-netty @@ -96,10 +109,9 @@ com.shinemo.mpush - mpush-tools - ${mpush-tools-version} + mpush-client + ${mpush-client-version} - @@ -154,19 +166,13 @@ + junit junit 4.12 - - - org.slf4j - slf4j-api - - - io.netty - netty-all + test @@ -199,8 +205,33 @@ maven-resources-plugin 2.6 + + org.apache.maven.plugins + maven-surefire-plugin + 2.4.2 + + true + + + + maven-assembly-plugin + 2.6 + + mpush + + assembly.xml + + + + + package + + single + + + + - @@ -226,6 +257,4 @@ - - From 42efe7168334baa093c37e70849b55019098b733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 01:47:44 +0000 Subject: [PATCH 105/890] =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-core/pom.xml | 1 + pom.xml | 1 + 2 files changed, 2 insertions(+) diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 4f4c08ce..5e9af561 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -84,6 +84,7 @@ + org.apache.maven.plugins maven-jar-plugin diff --git a/pom.xml b/pom.xml index 26a7b611..f1997e08 100644 --- a/pom.xml +++ b/pom.xml @@ -214,6 +214,7 @@ + org.apache.maven.plugins maven-assembly-plugin 2.6 From b304c84af94b00a8de7bda023b5a52036354299f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 02:12:32 +0000 Subject: [PATCH 106/890] =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 52 ---------------------------------------------- mpush-core/pom.xml | 2 -- pom.xml | 6 ++++-- 3 files changed, 4 insertions(+), 56 deletions(-) diff --git a/assembly.xml b/assembly.xml index 3946f881..4d0cf481 100644 --- a/assembly.xml +++ b/assembly.xml @@ -10,41 +10,6 @@ tar.gz - mpush-core/target/ @@ -59,30 +24,13 @@ mpush.jar - - false runtime false lib - \ No newline at end of file diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 5e9af561..eb3d75f8 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -84,7 +84,6 @@ - org.apache.maven.plugins maven-jar-plugin @@ -117,7 +116,6 @@ - diff --git a/pom.xml b/pom.xml index f1997e08..84ae0d99 100644 --- a/pom.xml +++ b/pom.xml @@ -171,9 +171,12 @@ junit junit - 4.12 test + + org.slf4j + slf4j-api + @@ -214,7 +217,6 @@ - org.apache.maven.plugins maven-assembly-plugin 2.6 From a6b714a26061a9b3523d9eb71d6c00f203741cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 02:32:26 +0000 Subject: [PATCH 107/890] =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/tools/Jsons.java | 152 +++++++++--------- .../zk/listener/impl/RedisPathListener.java | 97 +++++------ 2 files changed, 128 insertions(+), 121 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index 9fe7f58a..2dc1da7d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -16,88 +16,94 @@ * Created by xiaoxu.yxx on 15/8/7. */ public final class Jsons { - private static final Logger LOGGER = LoggerFactory.getLogger(Jsons.class); - public static final Gson GSON = new GsonBuilder().create(); + private static final Logger LOGGER = LoggerFactory.getLogger(Jsons.class); + public static final Gson GSON = new GsonBuilder().create(); - public static String toJson(Object bean) { + public static String toJson(Object bean) { - try { - return GSON.toJson(bean); - } catch (Exception e) { - LOGGER.error("Jsons.toJson ex, bean=" + bean, e); - } - return null; - } + try { + return GSON.toJson(bean); + } catch (Exception e) { + LOGGER.error("Jsons.toJson ex, bean=" + bean, e); + } + return null; + } - public static T fromJson(String json, Class clazz) { + public static T fromJson(String json, Class clazz) { - try { - return GSON.fromJson(json, clazz); - } catch (Exception e) { - LOGGER.error("Jsons.fromJson ex, json=" + json + ", clazz=" + clazz, e); - } - return null; - } + try { + return GSON.fromJson(json, clazz); + } catch (Exception e) { + LOGGER.error("Jsons.fromJson ex, json=" + json + ", clazz=" + clazz, e); + } + return null; + } - public static T fromJson(byte[] json, Class clazz) { - return fromJson(new String(json, Constants.UTF_8), clazz); - } + public static T fromJson(byte[] json, Class clazz) { + return fromJson(new String(json, Constants.UTF_8), clazz); + } - public static List fromJsonToList(String json, Class type) { - T[] list = GSON.fromJson(json, type); - return Arrays.asList(list); - } + public static List fromJsonToList(String json, Class type) { + try { + T[] list = GSON.fromJson(json, type); + if (list == null) return null; + return Arrays.asList(list); + } catch (Exception e) { + LOGGER.error("Jsons.fromJsonToList ex, json=" + json + ", type=" + type, e); + } + return null; + } - public static T fromJson(String json, Type type) { - try { - return GSON.fromJson(json, type); - } catch (Exception e) { - LOGGER.error("Jsons.fromJson ex, json=" + json + ", type=" + type, e); - } - return null; - } + public static T fromJson(String json, Type type) { + try { + return GSON.fromJson(json, type); + } catch (Exception e) { + LOGGER.error("Jsons.fromJson ex, json=" + json + ", type=" + type, e); + } + return null; + } - public static boolean mayJson(String json) { - if (Strings.isBlank(json)) - return false; - if (json.charAt(0) == '{' && json.charAt(json.length() - 1) == '}') - return true; - if (json.charAt(0) == '[' && json.charAt(json.length() - 1) == ']') - return true; - return false; - } + public static boolean mayJson(String json) { + if (Strings.isBlank(json)) + return false; + if (json.charAt(0) == '{' && json.charAt(json.length() - 1) == '}') + return true; + if (json.charAt(0) == '[' && json.charAt(json.length() - 1) == ']') + return true; + return false; + } - public static String toJson(Map map) { - if (map == null || map.isEmpty()) - return "{}"; - StringBuilder sb = new StringBuilder(64 * map.size()); - sb.append('{'); - Iterator> it = map.entrySet().iterator(); - if (it.hasNext()) { - append(it.next(), sb); - } - while (it.hasNext()) { - sb.append(','); - append(it.next(), sb); - } - sb.append('}'); - return sb.toString(); - } + public static String toJson(Map map) { + if (map == null || map.isEmpty()) + return "{}"; + StringBuilder sb = new StringBuilder(64 * map.size()); + sb.append('{'); + Iterator> it = map.entrySet().iterator(); + if (it.hasNext()) { + append(it.next(), sb); + } + while (it.hasNext()) { + sb.append(','); + append(it.next(), sb); + } + sb.append('}'); + return sb.toString(); + } - private static void append(Map.Entry entry, StringBuilder sb) { - String key = entry.getKey(), value = entry.getValue(); - if (value == null) - value = Strings.EMPTY; - sb.append('"').append(key).append('"'); - sb.append(':'); - sb.append('"').append(value).append('"'); - } + private static void append(Map.Entry entry, StringBuilder sb) { + String key = entry.getKey(), value = entry.getValue(); + if (value == null) + value = Strings.EMPTY; + sb.append('"').append(key).append('"'); + sb.append(':'); + sb.append('"').append(value).append('"'); + } - public static void main(String[] args) { - String test = "test"; - String ret = Jsons.toJson(test); - String ret2 = Jsons.fromJson(ret, String.class); - System.out.println(ret); - System.out.println(ret2); - } + public static void main(String[] args) { + String test = "test"; + String ret = Jsons.toJson(test); + String ret2 = Jsons.fromJson(ret, String.class); + System.out.println(ret); + System.out.println(ret2); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index 903fa81b..1203259c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -1,7 +1,9 @@ package com.shinemo.mpush.tools.zk.listener.impl; +import java.util.Collections; import java.util.List; +import com.google.common.base.Strings; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; @@ -21,56 +23,55 @@ import com.shinemo.mpush.tools.zk.manage.ServerManage; /** - *注册的应用的发生变化 - * + * 注册的应用的发生变化 */ -public class RedisPathListener implements CallBack{ - - private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); - - @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - String data = ""; - if(event.getData()!=null){ - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data); - } - } +public class RedisPathListener implements CallBack { - @Override - public void initData(ServerManage manage) { - log.warn("start init redis data"); - _initData(); - log.warn("end init redis data"); - } - - private void _initData(){ - //获取redis列表 - List group = getRedisGroup(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); - RedisGroupManage.instance.init(group); - } - - private void dataRemove(ChildData data){ - _initData(); - } - - private void dataAddOrUpdate(ChildData data){ - _initData(); - } - - private List getRedisGroup(String fullPath){ - String rawGroup = ZkUtil.instance.get(fullPath); - List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); - return group; - } + private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name() + "," + data); + } + } + @Override + public void initData(ServerManage manage) { + log.warn("start init redis data"); + _initData(); + log.warn("end init redis data"); + } + + private void _initData() { + //获取redis列表 + List group = getRedisGroup(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); + RedisGroupManage.instance.init(group); + } + + private void dataRemove(ChildData data) { + _initData(); + } + + private void dataAddOrUpdate(ChildData data) { + _initData(); + } + + private List getRedisGroup(String fullPath) { + String rawGroup = ZkUtil.instance.get(fullPath); + if (Strings.isNullOrEmpty(rawGroup)) return Collections.EMPTY_LIST; + List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); + if (group == null) return Collections.EMPTY_LIST; + return group; + } } From 407407e979d7572656420dfb9c9f28d35f4167e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 03:47:13 +0000 Subject: [PATCH 108/890] add client test --- .../main/java/com/shinemo/mpush/core/App.java | 12 +- .../mpush/core/router/RouterCenter.java | 2 +- .../mpush/core/netty/NettyClientTest.java | 37 +++- .../src/test/resources/config.properties | 1 + .../com/shinemo/mpush/tools/zk/PathEnum.java | 104 +++++------ .../tools/zk/listener/ListenerDispatcher.java | 4 +- .../listener/impl/ConnectionPathListener.java | 4 +- .../zk/listener/impl/RedisPathListener.java | 2 +- .../mpush/tools/zk/manage/ServerManage.java | 170 +++++++++--------- .../tools/redis/RedisGroupManageTest.java | 19 +- .../zk/DistributedQueueConsumerTest.java | 4 +- .../zk/DistributedQueueProviderTest.java | 4 +- .../mpush/tools/zk/ServerManageTest.java | 4 +- .../shinemo/mpush/tools/zk/ZkUtilTest.java | 4 +- 14 files changed, 197 insertions(+), 174 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index a7d172d5..3c1bf1ee 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -13,11 +13,9 @@ import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.manage.ServerManage; -import org.apache.zookeeper.ZKUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.awt.*; import java.io.IOException; import java.util.List; @@ -51,7 +49,7 @@ public void run() { server.start(new Server.Listener() { @Override public void onSuccess() { - registerServerToZK(port); + registerServerToZK(port, PathEnum.CONNECTION_SERVER); LOGGER.error("mpush app start connection server success...."); } @@ -78,7 +76,7 @@ public void run() { server.start(new Server.Listener() { @Override public void onSuccess() { - registerServerToZK(port); + registerServerToZK(port, PathEnum.GATEWAY_SERVER); LOGGER.error("mpush app start gateway server success...."); } @@ -95,10 +93,10 @@ public void onFailure(String message) { t.start(); } - private void registerServerToZK(int port) { + private void registerServerToZK(int port, PathEnum path) { String p = Integer.toString(port); ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), p); - ServerManage manage = new ServerManage(app); + ServerManage manage = new ServerManage(app, path); manage.start(); LOGGER.error("mpush app register server:{} to zk success", p); } @@ -110,7 +108,7 @@ public void startRedisClient() { group1.addRedisNode(node1); List groupList = Lists.newArrayList(group1); - ZkUtil.instance.registerPersist(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress()) + ZkUtil.instance.registerPersist(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress()) , Jsons.toJson(groupList)); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 2c2e802e..55afbad4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -39,7 +39,7 @@ public boolean register(String userId, Connection connection) { } if (oldRemoteRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); + EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldRemoteRouter)); } return true; } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index a81df4cc..fea831a1 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -4,18 +4,21 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.tools.ConfigCenter; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZkUtil; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + /** * Created by ohun on 2015/12/24. */ public class NettyClientTest { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); @Before @@ -23,15 +26,33 @@ public void setUp() throws Exception { ConfigCenter.INSTANCE.init(); } - private String host = "127.0.0.1"; - private int port = ConfigCenter.INSTANCE.getConnectionServerPort(); private ClientChannelHandler handler = new ClientChannelHandler(); @Test public void testClient() throws Exception { - Client client = NettyClientFactory.INSTANCE.get(host, port, handler); - client.init(); - client.start(); + List hosts = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER.getPath()); + if (hosts == null || hosts.isEmpty()) return; + for (String name : hosts) { + String json = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER.getPathByName(name)); + ServerApp server = Jsons.fromJson(json, ServerApp.class); + if (server == null) continue; + final Client client = NettyClientFactory.INSTANCE.get(server.getIp() + , Integer.parseInt(server.getPort()), handler); + client.init(); + Thread t = new Thread(new Runnable() { + @Override + public void run() { + client.start(); + } + }); + t.setDaemon(false); + t.start(); + } } + public static void main(String[] args) throws Exception { + NettyClientTest test = new NettyClientTest(); + test.setUp(); + test.testClient(); + } } diff --git a/mpush-core/src/test/resources/config.properties b/mpush-core/src/test/resources/config.properties index 1e1213f2..ef2b80aa 100644 --- a/mpush-core/src/test/resources/config.properties +++ b/mpush-core/src/test/resources/config.properties @@ -1,3 +1,4 @@ +ZK_SERVER=10.1.20.74:2181 MAX_PACKET_SIZE=10240 COMPRESS_LIMIT=10240 MIN_HEARTBEAT=10000 diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java index 6bb89727..ad81c72e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java @@ -2,64 +2,66 @@ public enum PathEnum { - - CONNECTION_SERVER_ALL_HOST("/cs/hosts","连接服务器应用注册的路径"){ - @Override - public String getPathByIp(String ip) { - return getPath()+"/machine"; - } - @Override - public String getPathByName(String name) { - return getPath()+"/"+name; - } - }, - CONNECTION_SERVER_REDIS("/cs/redis","连接服务器redis注册的地方"){ - @Override - public String getPathByIp(String ip) { - return getPath(); - } - @Override - public String getPathByName(String name) { - return getPath()+"/"+name; - } - }, - CONNECTION_SERVER_KICK("/cs/%s/kick/con","连接服务器踢人的路径"){ - @Override - public String getPathByIp(String ip) { - return String.format(getPath(), ip); - } - @Override - public String getPathByName(String name) { - return getPath()+"/"+name; - } - }; + REDIS_SERVER("/redis", "连接服务器redis注册的地方") { + @Override + public String getPathByIp(String ip) { + return getPath(); + } - PathEnum(String path, String desc) { - this.path = path; - this.desc = desc; + @Override + public String getPathByName(String name) { + return getPath() + "/" + name; + } + }, + CONNECTION_SERVER("/cs/hosts", "连接服务器应用注册的路径") { + @Override + public String getPathByIp(String ip) { + return getPath() + "/machine"; + } + + @Override + public String getPathByName(String name) { + return getPath() + "/" + name; + } + }, + GATEWAY_SERVER("/gs/hosts", "连接服务器应用注册的路径") { + @Override + public String getPathByIp(String ip) { + return getPath() + "/machine"; + } + + @Override + public String getPathByName(String name) { + return getPath() + "/" + name; + } + }; + + PathEnum(String path, String desc) { + this.path = path; + this.desc = desc; } private final String path; private final String desc; - + public String getPath() { - return path; - } + return path; + } - public String getDesc() { - return desc; - } - - //不同的机器,注册到不同的路径 - public abstract String getPathByIp(String ip); - - //根据从zk中获取的app的值,拼装全路径 - public abstract String getPathByName(String name); + public String getDesc() { + return desc; + } + + //不同的机器,注册到不同的路径 + public abstract String getPathByIp(String ip); + + //根据从zk中获取的app的值,拼装全路径 + public abstract String getPathByName(String name); - public static void main(String[] args) { - String test = "/cs/%s/kick"; - - System.out.println(String.format(test, "10.1.10.65")); - } + public static void main(String[] args) { + String test = "/cs/%s/kick"; + + System.out.println(String.format(test, "10.1.10.65")); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index 9063423b..038c13aa 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -23,9 +23,9 @@ public class ListenerDispatcher implements CallBack { public ListenerDispatcher(ServerApp app) { //所有connection server - holder.put(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()), new ConnectionPathListener()); + holder.put(PathEnum.CONNECTION_SERVER.getPathByIp(app.getIp()), new ConnectionPathListener()); //所有redis - holder.put(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(app.getIp()), new RedisPathListener()); + holder.put(PathEnum.REDIS_SERVER.getPathByIp(app.getIp()), new RedisPathListener()); //踢人的目录已经交给队列处理了,这里不需要重复处理 // holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), new KickPathListener()); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java index 13317fcf..abbde310 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java @@ -53,9 +53,9 @@ public void initData(ServerManage manage) { private void _initData(){ //获取机器列表 - List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath()); + List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER.getPath()); for(String raw:rawData){ - String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw); + String fullPath = PathEnum.CONNECTION_SERVER.getPathByName(raw); ServerApp app = getServerApp(fullPath); ServerAppManage.instance.addOrUpdate(fullPath, app); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index 1203259c..87e6303e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -55,7 +55,7 @@ public void initData(ServerManage manage) { private void _initData() { //获取redis列表 - List group = getRedisGroup(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); + List group = getRedisGroup(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress())); RedisGroupManage.instance.init(group); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 36ac3455..1b980be5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -20,103 +20,105 @@ public class ServerManage { - private static final Logger log = LoggerFactory.getLogger(ServerManage.class); + private static final Logger log = LoggerFactory.getLogger(ServerManage.class); + + private static ZkUtil zkUtil = ZkUtil.instance; - private static ZkUtil zkUtil = ZkUtil.instance; - private final AtomicBoolean startFlag = new AtomicBoolean(false); - + private final ServerApp app; - + private final PathEnum path; + private ListenerDispatcher dispatcher; - - public ServerManage(ServerApp app){ - this.app = app; + + public ServerManage(ServerApp app, PathEnum path) { + this.app = app; + this.path = path; } - - public void start() { - + + public void start() { + if (!startFlag.compareAndSet(false, true)) { return; } - + dispatcher = new ListenerDispatcher(app); - - //注册机器到zk中 - registerApp(); - - // 注册连接状态监听器 - registerConnectionLostListener(); - - // 注册节点数据变化 - registerDataChange(dispatcher); - - //获取应用起来的时候的初始化数据 - initAppData(dispatcher); - - } - - private void registerApp(){ - zkUtil.registerEphemeralSequential(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp()),Jsons.toJson(app)); - } - - public void unregisterApp(){ - zkUtil.remove(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByIp(app.getIp())); - } - - // 注册连接状态监听器 - private void registerConnectionLostListener() { - zkUtil.getClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { - - @Override - public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - if (ConnectionState.LOST == newState) { - log.warn(app.getIp() + ", lost connection"); - } else if (ConnectionState.RECONNECTED == newState) { - log.warn(app.getIp() + ", reconnected"); - } - } - }); - } - - // 注册节点数据变化 - private void registerDataChange(final CallBack callBack) { - zkUtil.getCache().getListenable().addListener(new TreeCacheListener() { - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - String path = null == event.getData() ? "" : event.getData().getPath(); - if (path.isEmpty()) { - log.warn("registerDataChange empty path:" + path + "," + event.getType().name()); - return; - } - callBack.handler(client, event, path); - } - }); - } - - private void initAppData(final CallBack callBack){ - callBack.initData(this); - } - - public CuratorFramework getClient() { - return zkUtil.getClient(); - } - + + //注册机器到zk中 + registerApp(); + + // 注册连接状态监听器 + registerConnectionLostListener(); + + // 注册节点数据变化 + registerDataChange(dispatcher); + + //获取应用起来的时候的初始化数据 + initAppData(dispatcher); + + } + + private void registerApp() { + zkUtil.registerEphemeralSequential(path.getPathByIp(app.getIp()), Jsons.toJson(app)); + } + + public void unregisterApp() { + zkUtil.remove(path.getPathByIp(app.getIp())); + } + + // 注册连接状态监听器 + private void registerConnectionLostListener() { + zkUtil.getClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { + + @Override + public void stateChanged(final CuratorFramework client, final ConnectionState newState) { + if (ConnectionState.LOST == newState) { + log.warn(app.getIp() + ", lost connection"); + } else if (ConnectionState.RECONNECTED == newState) { + log.warn(app.getIp() + ", reconnected"); + } + } + }); + } + + // 注册节点数据变化 + private void registerDataChange(final CallBack callBack) { + zkUtil.getCache().getListenable().addListener(new TreeCacheListener() { + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + String path = null == event.getData() ? "" : event.getData().getPath(); + if (path.isEmpty()) { + log.warn("registerDataChange empty path:" + path + "," + event.getType().name()); + return; + } + callBack.handler(client, event, path); + } + }); + } + + private void initAppData(final CallBack callBack) { + callBack.initData(this); + } + + public CuratorFramework getClient() { + return zkUtil.getClient(); + } + public TreeCache getCache() { return zkUtil.getCache(); } - - public void close(){ - zkUtil.close(); + + public void close() { + zkUtil.close(); } - - public ZkUtil getZkUtil(){ - return zkUtil; + + public ZkUtil getZkUtil() { + return zkUtil; } - - public ServerApp getServerApp(){ - return app; + + public ServerApp getServerApp() { + return app; } - + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 34523b4f..1d3010c2 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -5,6 +5,7 @@ import com.shinemo.mpush.tools.redis.listener.MessageListener; +import com.shinemo.mpush.tools.zk.PathEnum; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; @@ -20,7 +21,7 @@ public class RedisGroupManageTest { ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), "3000"); - ServerManage manage = new ServerManage(app); + ServerManage manage = new ServerManage(app, PathEnum.REDIS_SERVER); List groupList = null; RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); @@ -95,15 +96,15 @@ public void onMessage(String channel, String message) { e.printStackTrace(); } } - + @Test - public void testSub2(){ - RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); - try { - Thread.sleep(Integer.MAX_VALUE); - } catch (InterruptedException e) { - e.printStackTrace(); - } + public void testSub2() { + RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); + try { + Thread.sleep(Integer.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index 56409b0e..9444427b 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -4,15 +4,13 @@ import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.tools.zk.consumer.impl.ConsumerKickListener; import com.shinemo.mpush.tools.zk.manage.ServerManage; -import com.shinemo.mpush.tools.zk.queue.Consumer; public class DistributedQueueConsumerTest { private ServerApp app = new ServerApp("10.1.10.65", "3000"); - private ServerManage manage = new ServerManage(app); + private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); @Before public void setup() { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index 812006b2..a12504e8 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -16,7 +16,7 @@ public class DistributedQueueProviderTest { private ServerApp app = new ServerApp("10.1.10.64", "3000"); - private ServerManage manage = new ServerManage(app); + private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); @Before public void setup() { @@ -32,7 +32,7 @@ public void test() throws Exception{ while (iterator.hasNext()) { ServerApp app = iterator.next(); if(!app.getIp().equals(this.app.getIp())){ - Provider provider = new Provider(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), ServerApp.class); + Provider provider = new Provider<>(PathEnum.GATEWAY_SERVER.getPathByIp(app.getIp()), ServerApp.class); providers.add(provider); provider.start(); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 1e405c4d..124adb09 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -17,7 +17,7 @@ public class ServerManageTest { private ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(),"3000"); - private ServerManage manage = new ServerManage(app); + private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); @Test public void testMulThreadRegisterApp() throws InterruptedException{ @@ -59,7 +59,7 @@ public void run() { } log.warn("start init "+ip); ServerApp app = new ServerApp(ip,"3000"); - ServerManage manage = new ServerManage(app); + ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); manage.start(); try { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index cac382db..47a5fbf9 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -133,14 +133,14 @@ public void testAddRedis() { List groupList = Lists.newArrayList(group1); - zkUtil.registerPersist(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress()), Jsons.toJson(groupList)); + zkUtil.registerPersist(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress()), Jsons.toJson(groupList)); } @Test public void getRedisTest() { - String value = zkUtil.get(PathEnum.CONNECTION_SERVER_REDIS.getPathByIp(InetAddressUtil.getInetAddress())); + String value = zkUtil.get(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress())); List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); for (RedisGroup group : newGroupList) { for (RedisNode node : group.getRedisNodeList()) { From 91fe94b21b9c665cf07ac82f4440c56635586447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 04:04:40 +0000 Subject: [PATCH 109/890] kick user in local --- .../shinemo/mpush/common/router/RemoteRouterManager.java | 3 +++ .../com/shinemo/mpush/core/router/RouterChangeListener.java | 6 +++++- .../java/com/shinemo/mpush/core/netty/NettyClientTest.java | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 560a6f60..ce40ac96 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -11,6 +11,9 @@ public class RemoteRouterManager implements RouterManager { @Override public RemoteRouter register(String userId, RemoteRouter route) { RemoteRouter old = RedisManage.get(userId, RemoteRouter.class); + if (old != null) { + RedisManage.del(userId); + } RedisManage.set(userId, route); return old; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index e07d7732..8e562554 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -10,6 +10,7 @@ import com.shinemo.mpush.common.message.KickUserMessage; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.listener.MessageListener; import com.shinemo.mpush.tools.redis.manage.RedisManage; import com.shinemo.mpush.tools.redis.pubsub.Subscriber; @@ -59,9 +60,12 @@ public void operationComplete(ChannelFuture future) throws Exception { }); } - public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); + if (location.getHost().equals(MPushUtil.getLocalIp())) { + LOGGER.error("kick remote user but router in local, userId={}", userId); + return; + } KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); msg.srcServer = location.getHost(); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index fea831a1..26741744 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -26,7 +26,6 @@ public void setUp() throws Exception { ConfigCenter.INSTANCE.init(); } - private ClientChannelHandler handler = new ClientChannelHandler(); @Test public void testClient() throws Exception { @@ -36,6 +35,7 @@ public void testClient() throws Exception { String json = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER.getPathByName(name)); ServerApp server = Jsons.fromJson(json, ServerApp.class); if (server == null) continue; + ClientChannelHandler handler = new ClientChannelHandler(); final Client client = NettyClientFactory.INSTANCE.get(server.getIp() , Integer.parseInt(server.getPort()), handler); client.init(); From 6ef6469c15c0ffb5e5844d8d90f0a93e159fe36d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 05:08:44 +0000 Subject: [PATCH 110/890] remove log --- .../shinemo/mpush/core/netty/ClientChannelHandler.java | 8 ++++---- .../mpush/netty/connection/NettyConnectionManager.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 552c328e..1bc5dc6d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -54,7 +54,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - LOGGER.info("client read new packet=" + msg); + //LOGGER.info("client read new packet=" + msg); if (msg instanceof Packet) { Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); @@ -99,12 +99,12 @@ public void run(Timeout timeout) throws Exception { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { if (!channel.isActive()) { - LOGGER.warn("client send hb failed:" + channel.remoteAddress().toString() + ",channel is not active"); + LOGGER.warn("client send hb failed:" + channel + ",channel is not active"); } else { - LOGGER.warn("client send hb failed:" + channel.remoteAddress().toString()); + LOGGER.warn("client send hb failed:" + channel); } } else { - LOGGER.warn("client send hb success:" + channel.remoteAddress().toString()); + //LOGGER.debug("client send hb success:" + channel); } } }); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index fcd2bb36..94ca0050 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -89,7 +89,7 @@ public void run(Timeout timeout) throws Exception { } } else { expiredTimes = 0; - LOGGER.info("check heartbeat timeout"); + //LOGGER.info("check heartbeat timeout"); } startTimeout(); } From 38668ab06b0a40d926988253038512c0856755d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 09:05:26 +0000 Subject: [PATCH 111/890] add log --- .../mpush/api/connection/SessionContext.java | 3 +- .../mpush/api/router/ClientLocation.java | 10 ++ .../mpush/common/router/RemoteRouter.java | 5 + .../common/router/RemoteRouterManager.java | 4 + .../main/java/com/shinemo/mpush/core/App.java | 5 +- .../mpush/core/handler/BindUserHandler.java | 12 +- .../core/handler/FastConnectHandler.java | 6 + .../core/handler/GatewayPushHandler.java | 14 +- .../mpush/core/handler/HandshakeHandler.java | 2 +- .../mpush/core/router/LocalRouter.java | 5 + .../mpush/core/router/LocalRouterManager.java | 6 +- .../mpush/core/router/RouterCenter.java | 15 ++- .../core/netty/ClientChannelHandler.java | 7 +- .../mpush/core/netty/NettyClientTest.java | 35 +++-- .../com/shinemo/mpush/tools/zk/ServerApp.java | 39 +++--- .../tools/redis/RedisGroupManageTest.java | 2 +- .../zk/DistributedQueueConsumerTest.java | 2 +- .../zk/DistributedQueueProviderTest.java | 72 +++++----- .../mpush/tools/zk/ServerManageTest.java | 124 +++++++++--------- .../shinemo/mpush/tools/zk/ZkUtilTest.java | 2 +- 20 files changed, 214 insertions(+), 156 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java index bf23942c..8acffd8b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java @@ -46,10 +46,11 @@ public boolean handshakeOk() { @Override public String toString() { return "SessionContext{" + - "osName='" + osName + '\'' + + ", osName='" + osName + '\'' + ", osVersion='" + osVersion + '\'' + ", clientVersion='" + clientVersion + '\'' + ", deviceId='" + deviceId + '\'' + + ", heartbeat=" + heartbeat + '}'; } } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java index 4233fbae..39106d01 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java @@ -52,4 +52,14 @@ public static ClientLocation from(SessionContext context) { config.deviceId = context.deviceId; return config; } + + @Override + public String toString() { + return "ClientLocation{" + + "host='" + host + '\'' + + ", osName='" + osName + '\'' + + ", clientVersion='" + clientVersion + '\'' + + ", deviceId='" + deviceId + '\'' + + '}'; + } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java index 8628e168..5b739d0e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java @@ -22,4 +22,9 @@ public ClientLocation getRouteValue() { public RouterType getRouteType() { return RouterType.REMOTE; } + + @Override + public String toString() { + return "RemoteRouter{" + clientLocation + '}'; + } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index ce40ac96..75f2e46d 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -2,11 +2,14 @@ import com.shinemo.mpush.api.router.RouterManager; import com.shinemo.mpush.tools.redis.manage.RedisManage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/23. */ public class RemoteRouterManager implements RouterManager { + public static final Logger LOGGER = LoggerFactory.getLogger(RemoteRouterManager.class); @Override public RemoteRouter register(String userId, RemoteRouter route) { @@ -21,6 +24,7 @@ public RemoteRouter register(String userId, RemoteRouter route) { @Override public boolean unRegister(String userId) { RedisManage.del(userId); + LOGGER.info("unRegister local router success userId={}", userId); return true; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index 3c1bf1ee..9bd5c17e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -94,11 +94,10 @@ public void onFailure(String message) { } private void registerServerToZK(int port, PathEnum path) { - String p = Integer.toString(port); - ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), p); + ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), port); ServerManage manage = new ServerManage(app, path); manage.start(); - LOGGER.error("mpush app register server:{} to zk success", p); + LOGGER.error("mpush app register server:{} to zk success", port); } public void startRedisClient() { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 04e29b81..e495474c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -3,17 +3,20 @@ import com.google.common.base.Strings; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.BindUserMessage; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.core.router.RouterCenter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/23. */ public final class BindUserHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(BindUserHandler.class); @Override public BindUserMessage decode(Packet packet, Connection connection) { @@ -24,6 +27,7 @@ public BindUserMessage decode(Packet packet, Connection connection) { public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); + LOGGER.error("bind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } SessionContext context = message.getConnection().getSessionContext(); @@ -31,11 +35,15 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { OkMessage.from(message).setData("bind success").send(); + LOGGER.warn("bind user success, userId={}, session={}", message.userId, context); } else { ErrorMessage.from(message).setReason("bind failed").close(); + RouterCenter.INSTANCE.unRegister(message.userId); + LOGGER.error("bind user failure, register router failure, userId={}, session={}", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); + LOGGER.error("bind user failure not handshake, userId={}, session={}", message.userId, context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index ef092d1f..fe2c4cea 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -9,11 +9,14 @@ import com.shinemo.mpush.core.session.ReusableSession; import com.shinemo.mpush.core.session.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/25. */ public final class FastConnectHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(FastConnectHandler.class); @Override public FastConnectMessage decode(Packet packet, Connection connection) { @@ -25,8 +28,10 @@ public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); if (session == null) { ErrorMessage.from(message).setReason("session expire").close(); + LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { ErrorMessage.from(message).setReason("error device").close(); + LOGGER.warn("fast connect failure, not same device, deviceId={}, session={}", message.deviceId, session.context); } else { int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); session.context.setHeartbeat(heartbeat); @@ -37,6 +42,7 @@ public void handle(FastConnectMessage message) { .setServerTime(System.currentTimeMillis()) .setHeartbeat(heartbeat) .send(); + LOGGER.warn("fast connect success, session={}", message.deviceId, session.context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index 9d5e495c..e00d6400 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -1,22 +1,26 @@ package com.shinemo.mpush.core.handler; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.router.Router; import com.shinemo.mpush.common.ErrorCode; +import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.common.message.PushMessage; import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.core.router.RouterCenter; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/30. */ public final class GatewayPushHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(GatewayPushHandler.class); + @Override public GatewayPushMessage decode(Packet packet, Connection connection) { return new GatewayPushMessage(packet, connection); @@ -31,6 +35,7 @@ public void handle(final GatewayPushMessage message) { .from(message) .setErrorCode(ErrorCode.OFFLINE) .send(); + LOGGER.warn("gateway push router not exists user offline userId={}, content={}", message.userId, message.content); } else if (router.getRouteType() == Router.RouterType.LOCAL) { //2.如果是本地路由信息,说明用户链接在当前机器,直接把消息下发到客户端 Connection connection = (Connection) router.getRouteValue(); @@ -51,7 +56,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } } }); - + LOGGER.debug("gateway push router in local userId={}, connection={}", message.userId, connection); } else { //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 // 需要通过GatewayClient或ZK把消息推送到另外一台机器上 @@ -59,6 +64,7 @@ public void operationComplete(ChannelFuture future) throws Exception { .from(message) .setErrorCode(ErrorCode.ROUTER_CHANGE) .send(); + LOGGER.info("gateway push router in remote userId={}, router={}", message.userId, router); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 48ba7954..4fe8378e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -84,6 +84,6 @@ public void handle(HandshakeMessage message) { //9.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); - LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); + LOGGER.warn("handshake success, session={}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java index d99e7904..138c787c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java @@ -22,4 +22,9 @@ public Connection getRouteValue() { public RouterType getRouteType() { return RouterType.LOCAL; } + + @Override + public String toString() { + return "LocalRouter{" + connection + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index c22cff08..b2628e0c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.core.router; import com.shinemo.mpush.api.router.RouterManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -9,6 +11,7 @@ * Created by ohun on 2015/12/23. */ public class LocalRouterManager implements RouterManager { + public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); private final Map routerMap = new ConcurrentHashMap<>(); @Override @@ -18,7 +21,8 @@ public LocalRouter register(String userId, LocalRouter route) { @Override public boolean unRegister(String userId) { - routerMap.remove(userId); + LocalRouter router = routerMap.remove(userId); + LOGGER.info("unRegister local router success userId={}, router={}", userId, router); return true; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 55afbad4..4eb799a4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -8,11 +8,14 @@ import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; import com.shinemo.mpush.tools.MPushUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/23. */ public class RouterCenter { + public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); public static final RouterCenter INSTANCE = new RouterCenter(); private final LocalRouterManager localRouterManager = new LocalRouterManager(); @@ -31,15 +34,23 @@ public boolean register(String userId, Connection connection) { connConfig.setHost(MPushUtil.getLocalIp()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(connConfig); + LocalRouter oldLocalRouter = null; + RemoteRouter oldRemoteRouter = null; + try { + oldLocalRouter = localRouterManager.register(userId, localRouter); + oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); + } catch (Exception e) { + LOGGER.warn("register router ex, userId={}, connection={}", userId, connection, e); + } - LocalRouter oldLocalRouter = localRouterManager.register(userId, localRouter); - RemoteRouter oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); if (oldLocalRouter != null) { EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); + LOGGER.warn("register router success, find old local router={}, userId={}", oldLocalRouter, userId); } if (oldRemoteRouter != null) { EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldRemoteRouter)); + LOGGER.warn("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); } return true; } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 1bc5dc6d..3c2de02d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -70,8 +70,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.FAST_CONNECT) { LOGGER.info("fast connect success, packet=" + packet.getStringBody()); } else if (command == Command.KICK) { - LOGGER.error("receive kick user message=" + new KickUserMessage(packet, connection)); - ctx.close(); + KickUserMessage message = new KickUserMessage(packet, connection); + LOGGER.error("receive kick user userId={},deviceId={}, message={},", userId); + if (!message.deviceId.equals(deviceId)) { + ctx.close(); + } } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); LOGGER.error("receive an error packet=" + errorMessage); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 26741744..974f4d76 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -21,33 +21,28 @@ public class NettyClientTest { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); - @Before public void setUp() throws Exception { ConfigCenter.INSTANCE.init(); } - - @Test public void testClient() throws Exception { List hosts = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER.getPath()); if (hosts == null || hosts.isEmpty()) return; - for (String name : hosts) { - String json = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER.getPathByName(name)); - ServerApp server = Jsons.fromJson(json, ServerApp.class); - if (server == null) continue; - ClientChannelHandler handler = new ClientChannelHandler(); - final Client client = NettyClientFactory.INSTANCE.get(server.getIp() - , Integer.parseInt(server.getPort()), handler); - client.init(); - Thread t = new Thread(new Runnable() { - @Override - public void run() { - client.start(); - } - }); - t.setDaemon(false); - t.start(); - } + int index = (int) ((Math.random() % hosts.size()) * hosts.size()); + String name = hosts.get(index); + String json = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER.getPathByName(name)); + ServerApp server = Jsons.fromJson(json, ServerApp.class); + ClientChannelHandler handler = new ClientChannelHandler(); + final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler); + client.init(); + Thread t = new Thread(new Runnable() { + @Override + public void run() { + client.start(); + } + }); + t.setDaemon(false); + t.start(); } public static void main(String[] args) throws Exception { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java index 067eba98..cb5d03ab 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java @@ -2,23 +2,24 @@ import java.io.Serializable; -public class ServerApp implements Serializable{ - - private static final long serialVersionUID = 5495972321679092837L; - - private final String ip; - private final String port; - - public ServerApp(String ip, String port) { - this.ip = ip; - this.port = port; - } - - public String getIp() { - return ip; - } - public String getPort() { - return port; - } - +public class ServerApp implements Serializable { + + private static final long serialVersionUID = 5495972321679092837L; + + private final String ip; + private final int port; + + public ServerApp(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public String getIp() { + return ip; + } + + public int getPort() { + return port; + } + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 1d3010c2..da727922 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -20,7 +20,7 @@ public class RedisGroupManageTest { - ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), "3000"); + ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), 3000); ServerManage manage = new ServerManage(app, PathEnum.REDIS_SERVER); List groupList = null; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index 9444427b..ad5fd05c 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -8,7 +8,7 @@ public class DistributedQueueConsumerTest { - private ServerApp app = new ServerApp("10.1.10.65", "3000"); + private ServerApp app = new ServerApp("10.1.10.65", 3000); private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index a12504e8..24b61cb0 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -14,41 +14,41 @@ public class DistributedQueueProviderTest { - private ServerApp app = new ServerApp("10.1.10.64", "3000"); - - private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); - - @Before - public void setup() { - manage.start(); - } - - @Test - public void test() throws Exception{ - - Iterator iterator = ServerAppManage.instance.getAppList().iterator(); - - List> providers = Lists.newArrayList(); - while (iterator.hasNext()) { - ServerApp app = iterator.next(); - if(!app.getIp().equals(this.app.getIp())){ - Provider provider = new Provider<>(PathEnum.GATEWAY_SERVER.getPathByIp(app.getIp()), ServerApp.class); - providers.add(provider); - provider.start(); - } - } - - for(int i = 0;i<10;i++){ - providers.get(0).put(new ServerApp("hi"+i, "hello world")); - } - - Thread.sleep(20000); - - } - - @After - public void close() { - manage.close(); - } + private ServerApp app = new ServerApp("10.1.10.64", 3000); + + private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + + @Before + public void setup() { + manage.start(); + } + + @Test + public void test() throws Exception { + + Iterator iterator = ServerAppManage.instance.getAppList().iterator(); + + List> providers = Lists.newArrayList(); + while (iterator.hasNext()) { + ServerApp app = iterator.next(); + if (!app.getIp().equals(this.app.getIp())) { + Provider provider = new Provider<>(PathEnum.GATEWAY_SERVER.getPathByIp(app.getIp()), ServerApp.class); + providers.add(provider); + provider.start(); + } + } + + for (int i = 0; i < 10; i++) { + providers.get(0).put(new ServerApp("hi" + i, 1000)); + } + + Thread.sleep(20000); + + } + + @After + public void close() { + manage.close(); + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 124adb09..06f0da77 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -12,67 +12,67 @@ import com.shinemo.mpush.tools.zk.manage.ServerManage; public class ServerManageTest { - - private static Executor executor = Executors.newCachedThreadPool(); - - private ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(),"3000"); - - private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); - - @Test - public void testMulThreadRegisterApp() throws InterruptedException{ - CountDownLatch latch = new CountDownLatch(1); - for(int i = 1;i<=2;i++){ - executor.execute(new Worker("192.168.1."+i, latch)); - } - latch.countDown(); - - Thread.sleep(Integer.MAX_VALUE); - } - - - @Test - public void testServerManageStart() throws InterruptedException{ - manage.start(); - Thread.sleep(Integer.MAX_VALUE); - } - - - private static class Worker implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - private final String ip; - private final CountDownLatch latch; - - public Worker(String ip, CountDownLatch latch) { - this.ip = ip; - this.latch = latch; - } - - @Override - public void run() { - try { - latch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - log.warn("start init "+ip); - ServerApp app = new ServerApp(ip,"3000"); - ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); - manage.start(); - - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - manage.close(); - - log.warn("end init "+ip); - } - - } + + private static Executor executor = Executors.newCachedThreadPool(); + + private ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), 3000); + + private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + + @Test + public void testMulThreadRegisterApp() throws InterruptedException { + CountDownLatch latch = new CountDownLatch(1); + for (int i = 1; i <= 2; i++) { + executor.execute(new Worker("192.168.1." + i, latch)); + } + latch.countDown(); + + Thread.sleep(Integer.MAX_VALUE); + } + + + @Test + public void testServerManageStart() throws InterruptedException { + manage.start(); + Thread.sleep(Integer.MAX_VALUE); + } + + + private static class Worker implements Runnable { + + private static final Logger log = LoggerFactory.getLogger(Worker.class); + + private final String ip; + private final CountDownLatch latch; + + public Worker(String ip, CountDownLatch latch) { + this.ip = ip; + this.latch = latch; + } + + @Override + public void run() { + try { + latch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + log.warn("start init " + ip); + ServerApp app = new ServerApp(ip, 3000); + ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + manage.start(); + + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + manage.close(); + + log.warn("end init " + ip); + } + + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 47a5fbf9..e815c49d 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -99,7 +99,7 @@ public void testLocalIp() { @Test public void testRegisterIp() { String localIp = InetAddressUtil.getInetAddress(); - ServerApp app = new ServerApp(localIp, "3000"); + ServerApp app = new ServerApp(localIp, 3000); zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); String value = zkUtil.get("/" + localIp); System.out.println(value); From 2c5d7a1171b6749980fc60b1b306871964f87d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 11:04:38 +0000 Subject: [PATCH 112/890] add test fast connection --- .../common/message/FastConnectMessage.java | 5 + .../mpush/common/security/AesCipher.java | 12 ++ .../core/handler/FastConnectHandler.java | 5 +- .../mpush/core/handler/HandshakeHandler.java | 2 +- .../mpush/core/session/ReusableSession.java | 20 +--- .../core/session/ReusableSessionManager.java | 4 +- .../core/netty/ClientChannelHandler.java | 109 +++++++++++++++--- .../mpush/core/netty/NettyClientTest.java | 3 +- .../shinemo/mpush/tools/redis/RedisUtil.java | 5 +- .../mpush/tools/redis/manage/RedisManage.java | 2 +- 10 files changed, 129 insertions(+), 38 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java index 572c76e4..64295bf6 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; @@ -13,6 +14,10 @@ public final class FastConnectMessage extends ByteBufMessage { public int minHeartbeat; public int maxHeartbeat; + public FastConnectMessage(Connection connection) { + super(new Packet(Command.FAST_CONNECT.cmd, genSessionId()), connection); + } + public FastConnectMessage(Packet message, Connection connection) { super(message, connection); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index 2ee0327b..4eff437d 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -40,4 +40,16 @@ public String toString(byte[] a) { } return b.toString(); } + + public static byte[] toArray(String str) { + String[] a = str.split("\\|"); + if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { + throw new RuntimeException("decode cipher ex key length invalid"); + } + byte[] bytes = new byte[a.length]; + for (int i = 0; i < a.length; i++) { + bytes[i] = Byte.parseByte(a[i]); + } + return bytes; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index fe2c4cea..7d94a3cb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -6,6 +6,7 @@ import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.FastConnectMessage; import com.shinemo.mpush.common.message.FastConnectOkMessage; +import com.shinemo.mpush.core.router.RouterCenter; import com.shinemo.mpush.core.session.ReusableSession; import com.shinemo.mpush.core.session.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; @@ -27,10 +28,10 @@ public FastConnectMessage decode(Packet packet, Connection connection) { public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); if (session == null) { - ErrorMessage.from(message).setReason("session expire").close(); + ErrorMessage.from(message).setReason("session expire").send(); LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { - ErrorMessage.from(message).setReason("error device").close(); + ErrorMessage.from(message).setReason("error device").send(); LOGGER.warn("fast connect failure, not same device, deviceId={}, session={}", message.deviceId, session.context); } else { int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 4fe8378e..cbf051c7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -56,7 +56,6 @@ public void handle(HandshakeMessage message) { //4.生成可复用session, 用于快速重连 ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); - ReusableSessionManager.INSTANCE.cacheSession(session); //5.计算心跳时间 int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); @@ -81,6 +80,7 @@ public void handle(HandshakeMessage message) { .setClientVersion(message.clientVersion) .setDeviceId(message.deviceId) .setHeartbeat(heartbeat); + ReusableSessionManager.INSTANCE.cacheSession(session); //9.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java index 2f71e41c..9175a420 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java @@ -3,6 +3,7 @@ import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.common.security.AesCipher; import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.common.security.RsaCipher; /** * Created by ohun on 2015/12/25. @@ -18,7 +19,7 @@ public String encode() { sb.append(context.osVersion).append(','); sb.append(context.clientVersion).append(','); sb.append(context.deviceId).append(','); - sb.append(context.cipher).append(','); + sb.append(context.cipher); return sb.toString(); } @@ -30,20 +31,9 @@ public void decode(String value) throws Exception { context.osVersion = array[1]; context.clientVersion = array[2]; context.deviceId = array[3]; - byte[] key = toArray(array[4]); - byte[] iv = toArray(array[5]); + byte[] key = AesCipher.toArray(array[4]); + byte[] iv = AesCipher.toArray(array[5]); context.cipher = new AesCipher(key, iv); - } - - private byte[] toArray(String str) { - String[] a = str.split("|"); - if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { - throw new RuntimeException("decode session cipher exception"); - } - byte[] bytes = new byte[a.length]; - for (int i = 0; i < a.length; i++) { - bytes[i] = Byte.parseByte(a[i]); - } - return bytes; + this.context = context; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index a8b75700..306fd8a6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -13,7 +13,7 @@ */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000; + private static final int EXPIRE_TIME = 86400; public boolean cacheSession(ReusableSession session) { RedisManage.set(session.sessionId, session.encode(), EXPIRE_TIME); @@ -37,7 +37,7 @@ public ReusableSession genSession(SessionContext context) { ReusableSession session = new ReusableSession(); session.context = context; session.sessionId = MD5Utils.encrypt(context.deviceId + now); - session.expireTime = now + EXPIRE_TIME; + session.expireTime = now + EXPIRE_TIME * 1000; return session; } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 3c2de02d..ff0a012b 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.core.netty; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.common.message.*; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; @@ -8,7 +10,7 @@ import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.netty.connection.NettyConnection; import com.shinemo.mpush.netty.util.NettySharedHolder; -import com.shinemo.mpush.tools.Strings; +import com.shinemo.mpush.tools.Jsons; import io.netty.channel.*; @@ -18,6 +20,9 @@ import org.slf4j.LoggerFactory; import java.io.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -29,12 +34,26 @@ public class ClientChannelHandler extends ChannelHandlerAdapter { private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); private byte[] iv = CipherBox.INSTANCE.randomAESIV(); private Connection connection = new NettyConnection(); - private String deviceId = "test-device-id-100" + new Random().nextInt(5); - private String userId = "user_" + new Random().nextInt(5); + private String deviceId; + private String userId; + private Map sessionTickets; - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - connection.init(ctx.channel(), true); + public ClientChannelHandler() { + Map map = getToken(); + if (map != null && map.size() > 0) { + userId = (String) map.get("userId"); + deviceId = (String) map.get("deviceId"); + sessionTickets = map; + } + if (deviceId == null) { + deviceId = "test-device-id-100" + new Random().nextInt(5); + } + if (userId == null) { + userId = "user_" + new Random().nextInt(5); + } + } + + private void handshake() { HandshakeMessage message = new HandshakeMessage(connection); message.clientKey = clientKey; message.iv = iv; @@ -44,7 +63,44 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { message.osVersion = "5.0"; message.timestamp = System.currentTimeMillis(); message.send(); + } + + private void tryFastConnect() { + if (sessionTickets == null) { + handshake(); + return; + } + String sessionId = (String) sessionTickets.get("sessionId"); + if (sessionId == null) { + handshake(); + return; + } + String expireTime = (String) sessionTickets.get("expireTime"); + if (expireTime != null) { + long exp = Long.parseLong(expireTime); + if (exp < System.currentTimeMillis()) { + handshake(); + return; + } + } + FastConnectMessage message = new FastConnectMessage(connection); + message.deviceId = deviceId; + message.sessionId = sessionId; + message.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + if (!channelFuture.isSuccess()) { + handshake(); + } + } + }); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client channel Active"); + connection.init(ctx.channel(), true); + tryFastConnect(); } @Override @@ -62,13 +118,22 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().changeCipher(new AesCipher(clientKey, iv)); HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, message.serverKey); - saveToken(message.sessionId); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); startHeartBeat(message.heartbeat, ctx.channel()); LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, message.serverKey); + saveToken(message, connection.getSessionContext()); bindUser(); } else if (command == Command.FAST_CONNECT) { - LOGGER.info("fast connect success, packet=" + packet.getStringBody()); + String cipherStr = (String) sessionTickets.get("cipher"); + String[] cs = cipherStr.split(","); + byte[] key = AesCipher.toArray(cs[0]); + byte[] iv = AesCipher.toArray(cs[1]); + connection.getSessionContext().changeCipher(new AesCipher(key, iv)); + + FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); + startHeartBeat(message.heartbeat, ctx.channel()); + bindUser(); + LOGGER.info("fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); LOGGER.error("receive kick user userId={},deviceId={}, message={},", userId); @@ -119,24 +184,40 @@ public void operationComplete(ChannelFuture future) throws Exception { } - private void saveToken(String token) { + private void saveToken(HandshakeOkMessage message, SessionContext context) { try { + Map map = new HashMap<>(); + map.put("sessionId", message.sessionId); + map.put("serverHost", message.serverHost); + map.put("expireTime", Long.toString(message.expireTime)); + map.put("cipher", context.cipher.toString()); + map.put("deviceId", deviceId); + map.put("userId", userId); String path = this.getClass().getResource("/").getFile(); FileOutputStream out = new FileOutputStream(new File(path, "token.dat")); - out.write(token.getBytes()); + out.write(Jsons.toJson(map).getBytes(Constants.UTF_8)); out.close(); } catch (Exception e) { } } - private String getToken() { + private Map getToken() { + if (true) return Collections.EMPTY_MAP; try { InputStream in = this.getClass().getResourceAsStream("/token.dat"); byte[] bytes = new byte[in.available()]; - in.read(bytes); - return new String(bytes); + if (bytes.length > 0) { + in.read(bytes); + Map map = Jsons.fromJson(bytes, Map.class); + return map; + } + in.close(); } catch (Exception e) { } - return Strings.EMPTY; + return Collections.EMPTY_MAP; + } + + public String getLastServerHost() { + return sessionTickets == null ? null : (String) sessionTickets.get("serverHost"); } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 974f4d76..5f83b5dc 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -5,11 +5,10 @@ import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; -import org.junit.Before; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index e3452418..5cdaca73 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -56,6 +56,7 @@ public static T get(RedisNode node, String key, Class clazz) { // 返还到连接池 close(jedis); } + if (clazz == String.class) return (T) value; return Jsons.fromJson(value, clazz); } @@ -452,7 +453,9 @@ public static long llen(RedisNode node, String key) { /********************* list redis end ********************************/ - /********************* pubsub redis start ********************************/ + /********************* + * pubsub redis start + ********************************/ public static void publish(RedisNode node, String channel, T message) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index 24d946f5..420dbdb5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -45,7 +45,7 @@ public static void set(String key, T value, Integer time) { */ public static void set(String key, String value, Integer time) { List nodeList = RedisGroupManage.instance.hashSet(key); - RedisUtil.set(nodeList, key, value, null); + RedisUtil.set(nodeList, key, value, time); } public static void del(String key) { From 2b18bbdf835f83d97566f0abe6b7197cde4ba43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 7 Jan 2016 12:16:10 +0000 Subject: [PATCH 113/890] add push client --- .../com/shinemo/mpush/client/PushClient.java | 70 ++++++++++++------- .../com/shinemo/mpush/client/PushRequest.java | 3 + .../shinemo/mpush/client/PushClientTest.java | 50 +++++++++++++ .../src/test/resources/config.properties | 8 +++ mpush-client/src/test/resources/logback.xml | 30 ++++++++ .../main/java/com/shinemo/mpush/core/App.java | 15 ++-- .../core/netty/ClientChannelHandler.java | 2 +- .../mpush/core/netty/NettyClientTest.java | 2 +- .../client/AbstractNettyClientFactory.java | 36 +++------- .../netty/client/NettyClientFactory.java | 9 +-- .../shinemo/mpush/tools/redis/RedisUtil.java | 9 ++- .../mpush/tools/thread/ThreadPoolUtil.java | 13 +++- 12 files changed, 168 insertions(+), 79 deletions(-) create mode 100644 mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java create mode 100644 mpush-client/src/test/resources/config.properties create mode 100644 mpush-client/src/test/resources/logback.xml diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index c07d0ef4..8c94604d 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -4,43 +4,62 @@ import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZkUtil; +import java.io.IOException; +import java.util.Arrays; import java.util.Collection; +import java.util.List; +import java.util.concurrent.locks.LockSupport; /** * Created by ohun on 2015/12/30. */ public class PushClient implements PushSender { - public NettyClientFactory clientFactory; - private String host = "127.0.0.1"; - private int port = 4000; + public NettyClientFactory clientFactory = NettyClientFactory.INSTANCE; private int defaultTimeout = 3000; + private int port = 4000; - public void init() throws Exception { - this.clientFactory = NettyClientFactory.INSTANCE; - } - - public Connection getConnection(String ip) { + public void init() { try { - Client client = clientFactory.get(ip, port); - if (client == null) { - final Client client2 = clientFactory.createClient(ip, - port, new PushClientChannelHandler()); - client2.init(); - new Thread(new Runnable() { - @Override - public void run() { - client2.start(); - } - }).start(); - client = client2; - } - return ((PushClientChannelHandler) client.getHandler()).getConnection(); - } catch (Exception e) { + ConfigCenter.INSTANCE.init(); + } catch (IOException e) { + e.printStackTrace(); + } + List nodes = ZkUtil.instance.getChildrenKeys(PathEnum.GATEWAY_SERVER.getPath()); + if (nodes == null || nodes.isEmpty()) return; + for (String name : nodes) { + String json = ZkUtil.instance.get(PathEnum.GATEWAY_SERVER.getPathByName(name)); + ServerApp server = Jsons.fromJson(json, ServerApp.class); + if (server == null) continue; + createClient(server.getIp(), server.getPort()); + } + } + private void createClient(String ip, int port) { + Client client = clientFactory.get(ip, port); + if (client == null) { + final Client cli = clientFactory.createGet(ip, port, new PushClientChannelHandler()); + ThreadPoolUtil.newThread(new Runnable() { + @Override + public void run() { + cli.init(); + cli.start(); + } + }, "push-client-" + ip).start(); } - return null; + } + + public Connection getConnection(String ip) { + Client client = clientFactory.get(ip, port); + if (client == null) return null; + return ((PushClientChannelHandler) client.getHandler()).getConnection(); } @Override @@ -55,7 +74,4 @@ public void send(String content, Collection userIds, Callback callback) .send(); } } - - - } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java index 4e26d59a..4368ac49 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java @@ -144,12 +144,14 @@ private void sendToConnectionServer() { this.onOffline(userId); return; } + ClientLocation location = router.getRouteValue(); Connection connection = pushClient.getConnection(location.getHost()); if (connection == null || !connection.isConnected()) { this.onFailure(userId); return; } + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, connection); pushMessage.send(new ChannelFutureListener() { @Override @@ -161,6 +163,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } } }); + this.sessionId = pushMessage.getSessionId(); PushRequestBus.INSTANCE.add(this); } diff --git a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java new file mode 100644 index 00000000..5e9dda34 --- /dev/null +++ b/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java @@ -0,0 +1,50 @@ +package com.shinemo.mpush.client; + +import com.shinemo.mpush.api.PushSender; +import org.junit.Test; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + +import static org.junit.Assert.*; + +/** + * Created by ohun on 2016/1/7. + */ +public class PushClientTest { + + @Test + public void testSend() throws Exception { + + } + + public static void main(String[] args) throws InterruptedException { + PushClient client = new PushClient(); + client.init(); + Thread.sleep(5000); + client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), + new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + } + ); + LockSupport.park(); + } +} \ No newline at end of file diff --git a/mpush-client/src/test/resources/config.properties b/mpush-client/src/test/resources/config.properties new file mode 100644 index 00000000..ef2b80aa --- /dev/null +++ b/mpush-client/src/test/resources/config.properties @@ -0,0 +1,8 @@ +ZK_SERVER=10.1.20.74:2181 +MAX_PACKET_SIZE=10240 +COMPRESS_LIMIT=10240 +MIN_HEARTBEAT=10000 +MAX_HEARTBEAT=1800000 +MAX_HB_TIMEOUT_TIMES=2 +PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-client/src/test/resources/logback.xml b/mpush-client/src/test/resources/logback.xml new file mode 100644 index 00000000..20979b40 --- /dev/null +++ b/mpush-client/src/test/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index 9bd5c17e..113415e1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -9,6 +9,7 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.PathEnum; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; @@ -40,7 +41,7 @@ private void init() throws IOException { } public void startConnectionServer() { - Thread t = new Thread(new Runnable() { + ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { final int port = ConfigCenter.INSTANCE.getConnectionServerPort(); @@ -60,14 +61,11 @@ public void onFailure(String message) { } }); } - }); - t.setDaemon(false); - t.setName("conn-server-thread"); - t.start(); + }, "conn-server", false).start(); } public void startGatewayServer() { - Thread t = new Thread(new Runnable() { + ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { final int port = ConfigCenter.INSTANCE.getGatewayServerPort(); @@ -87,10 +85,7 @@ public void onFailure(String message) { } }); } - }); - t.setDaemon(false); - t.setName("gateway-server-thread"); - t.start(); + }, "gateway-server", false).start(); } private void registerServerToZK(int port, PathEnum path) { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index ff0a012b..7cd5f750 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -49,7 +49,7 @@ public ClientChannelHandler() { deviceId = "test-device-id-100" + new Random().nextInt(5); } if (userId == null) { - userId = "user_" + new Random().nextInt(5); + userId = "user-" + new Random().nextInt(5); } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 5f83b5dc..74177bed 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -32,7 +32,7 @@ public void testClient() throws Exception { String json = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER.getPathByName(name)); ServerApp server = Jsons.fromJson(json, ServerApp.class); ClientChannelHandler handler = new ClientChannelHandler(); - final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler); + final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); client.init(); Thread t = new Thread(new Runnable() { @Override diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index f59c48ce..4e63d9d7 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -42,37 +42,24 @@ public void onRemoval(RemovalNotification notification) { * * @param remoteHost * @param port - * @param handler * @return * @throws Exception */ - public Client get(final String remoteHost, final int port, final ChannelHandler handler) throws Exception { - final String key = String.format(format, remoteHost, port); - Client client = cachedClients.get(key, new Callable() { - @Override - public Client call() throws Exception { - Client client = createClient(remoteHost, port, handler); - return client; - } - }); - if (client == null) { - cachedClients.invalidate(key); - return null; - } - return client; + public Client get(final String remoteHost, final int port) { + String key = String.format(format, remoteHost, port); + return cachedClients.getIfPresent(key); } - public Client get(final String remoteHost, final int port) throws Exception { - return get(remoteHost, port, null); - } - - - protected Client createClient(final String remoteHost, final int port) throws Exception { - return createClient(remoteHost, port, null); + public Client createGet(String remoteHost, int port, ChannelHandler handler) { + Client client = createClient(remoteHost, port, handler); + if (client != null) { + String key = String.format(format, remoteHost, port); + cachedClients.put(key, client); + } + return client; } - public abstract Client createClient(final String remoteHost, final int port, ChannelHandler handler) throws Exception; - + abstract Client createClient(String remoteHost, int port, ChannelHandler handler); public void remove(Client client) { if (client != null) { @@ -80,5 +67,4 @@ public void remove(Client client) { LOGGER.warn(MessageFormat.format("[Remoting] {0} is removed", client)); } } - } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 1a928829..f229ba92 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -8,16 +8,9 @@ public class NettyClientFactory extends AbstractNettyClientFactory { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientFactory.class); - public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - public Client createClient(final String host, final int port, final ChannelHandler handler) throws Exception { + Client createClient(String host, int port, ChannelHandler handler) { return new NettyClient(host, port, handler); } - - public void remove(final Client client) { - super.remove(client); - } - } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 5cdaca73..d4488010 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.Set; +import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -473,16 +474,14 @@ public static void publish(RedisNode node, String channel, T message) { } public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { + int i = 0; for (final RedisNode node : nodeList) { - Thread t = new Thread(new Runnable() { + ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { subscribe(node, pubsub, channels); } - }); - t.setDaemon(true); - t.setName("redis-subscribe-thread"); - t.start(); + }, ("redis-subscribe-" + i++)).start(); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java index c081075d..58d7ef9c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java @@ -5,9 +5,8 @@ import com.shinemo.mpush.tools.Constants; - public class ThreadPoolUtil { - + public static final String THREAD_NAME_PREFIX = "mp-t-"; private static final ThreadPoolManager threadPoolManager = new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, Constants.THREAD_QUEUE_SIZE); @@ -29,4 +28,14 @@ public static Executor getWorkExecutor() { } + public static Thread newThread(Runnable r, String name, boolean daemon) { + Thread t = new Thread(r); + t.setDaemon(daemon); + t.setName(THREAD_NAME_PREFIX + name); + return t; + } + + public static Thread newThread(Runnable r, String name) { + return newThread(r, name, true); + } } From ea0e97e976b1348b076939ce10acf6467bd71ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 07:08:17 +0000 Subject: [PATCH 114/890] add push client --- .../mpush/api/router/RouterManager.java | 4 +- .../com/shinemo/mpush/client/PushClient.java | 101 ++++++++++++++---- .../com/shinemo/mpush/client/PushRequest.java | 6 ++ .../shinemo/mpush/client/PushRequestBus.java | 2 +- .../shinemo/mpush/client/PushClientTest.java | 4 +- .../mpush/common/message/ErrorMessage.java | 2 +- .../mpush/common/message/OkMessage.java | 4 +- .../mpush/common/message/PushMessage.java | 4 + .../router/ConnectionRouterManager.java | 2 +- .../common/router/RemoteRouterManager.java | 4 +- .../main/java/com/shinemo/mpush/core/App.java | 50 ++++++--- .../core/handler/GatewayPushHandler.java | 19 +++- .../mpush/core/router/LocalRouterManager.java | 9 +- .../core/router/RouterChangeListener.java | 6 +- .../core/netty/ClientChannelHandler.java | 9 +- .../mpush/core/netty/NettyClientTest.java | 27 +++-- .../mpush/netty/server/NettyServer.java | 4 +- .../com/shinemo/mpush/tools/zk/PathEnum.java | 67 ------------ .../com/shinemo/mpush/tools/zk/ZKPath.java | 38 +++++++ .../tools/zk/listener/ListenerDispatcher.java | 87 +++++++-------- .../zk/listener/impl/ConnPathListener.java | 82 ++++++++++++++ .../listener/impl/ConnectionPathListener.java | 83 -------------- ...Listener.java => GatewayPathListener.java} | 4 +- .../zk/listener/impl/RedisPathListener.java | 15 ++- .../mpush/tools/zk/manage/ServerManage.java | 10 +- .../tools/redis/RedisGroupManageTest.java | 4 +- .../zk/DistributedQueueConsumerTest.java | 2 +- .../zk/DistributedQueueProviderTest.java | 4 +- .../mpush/tools/zk/ServerManageTest.java | 4 +- .../shinemo/mpush/tools/zk/ZkUtilTest.java | 4 +- 30 files changed, 368 insertions(+), 293 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java rename mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/{KickPathListener.java => GatewayPathListener.java} (90%) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java index 90801d27..46114fea 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java @@ -9,10 +9,10 @@ public interface RouterManager { * 注册路由 * * @param userId - * @param route + * @param router * @return */ - R register(String userId, R route); + R register(String userId, R router); /** * 删除路由 diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 8c94604d..0626e558 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -1,51 +1,50 @@ package com.shinemo.mpush.client; +import com.google.common.base.Strings; import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.locks.LockSupport; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; /** * Created by ohun on 2015/12/30. */ public class PushClient implements PushSender { - public NettyClientFactory clientFactory = NettyClientFactory.INSTANCE; private int defaultTimeout = 3000; private int port = 4000; + private final Map clientMap = new ConcurrentHashMap<>(); + private final Map servers = new ConcurrentHashMap<>(); - public void init() { - try { - ConfigCenter.INSTANCE.init(); - } catch (IOException e) { - e.printStackTrace(); - } - List nodes = ZkUtil.instance.getChildrenKeys(PathEnum.GATEWAY_SERVER.getPath()); + public void init() throws Exception { + ConfigCenter.INSTANCE.init(); + initRedisClient(); + GatewayServerZKListener listener = new GatewayServerZKListener(); + Collection nodes = listener.getAllServers(); if (nodes == null || nodes.isEmpty()) return; - for (String name : nodes) { - String json = ZkUtil.instance.get(PathEnum.GATEWAY_SERVER.getPathByName(name)); - ServerApp server = Jsons.fromJson(json, ServerApp.class); - if (server == null) continue; + for (ServerApp server : nodes) { createClient(server.getIp(), server.getPort()); } } - private void createClient(String ip, int port) { - Client client = clientFactory.get(ip, port); + private void createClient(final String ip, int port) { + Client client = clientMap.get(ip); if (client == null) { - final Client cli = clientFactory.createGet(ip, port, new PushClientChannelHandler()); + final Client cli = new NettyClient(ip, port, new PushClientChannelHandler()); ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { @@ -53,11 +52,12 @@ public void run() { cli.start(); } }, "push-client-" + ip).start(); + clientMap.put(ip, cli); } } public Connection getConnection(String ip) { - Client client = clientFactory.get(ip, port); + Client client = clientMap.get(ip); if (client == null) return null; return ((PushClientChannelHandler) client.getHandler()).getConnection(); } @@ -65,6 +65,7 @@ public Connection getConnection(String ip) { @Override public void send(String content, Collection userIds, Callback callback) { for (String userId : userIds) { + System.out.println(userId); PushRequest .build(this) .setCallback(callback) @@ -74,4 +75,60 @@ public void send(String content, Collection userIds, Callback callback) .send(); } } + + public void initRedisClient() { + RedisPathListener listener = new RedisPathListener(); + ZkUtil.instance.getCache().getListenable().addListener(listener); + listener.initData(null); + } + + private class GatewayServerZKListener implements TreeCacheListener { + + public GatewayServerZKListener() { + ZkUtil.instance.getCache().getListenable().addListener(this); + } + + @Override + public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { + if (event.getData() == null) return; + String path = event.getData().getPath(); + if (Strings.isNullOrEmpty(path)) return; + + if (TreeCacheEvent.Type.NODE_ADDED == event.getType()) { + ServerApp serverApp = getServer(path); + if (serverApp != null) { + createClient(serverApp.getIp(), serverApp.getPort()); + servers.put(path, serverApp); + } + } else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) { + ServerApp serverApp = servers.remove(path); + if (serverApp != null) { + clientMap.remove(serverApp.getIp()); + } + } else if (TreeCacheEvent.Type.NODE_UPDATED == event.getType()) { + + } + } + + private ServerApp getServer(String path) { + String json = ZkUtil.instance.get(path); + if (Strings.isNullOrEmpty(json)) return null; + return Jsons.fromJson(json, ServerApp.class); + } + + private Collection getAllServers() { + List list = ZkUtil.instance.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); + if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; + for (String name : list) { + String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name); + String json = ZkUtil.instance.get(fullPath); + if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue; + ServerApp server = Jsons.fromJson(json, ServerApp.class); + if (server != null) { + servers.put(fullPath, server); + } + } + return servers.values(); + } + } } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java index 4368ac49..dea52f24 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java @@ -83,6 +83,7 @@ public void onTimeout(String userId) { } private void submit(int status) { + if (this.status != 0) return; this.status = status; if (sessionId > 0) PushRequestBus.INSTANCE.remove(sessionId); if (callback != null) { @@ -97,12 +98,16 @@ public void run() { switch (status) { case 1: callback.onSuccess(userId); + break; case 2: callback.onFailure(userId); + break; case 3: callback.onOffline(userId); + break; case 4: callback.onTimeout(userId); + break; } } @@ -132,6 +137,7 @@ public void send() { } public void redirect() { + this.status = 0; this.timeout_ = timeout + System.currentTimeMillis(); ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); sendToConnectionServer(); diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java index 4dc43058..330d0445 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java @@ -13,7 +13,7 @@ public class PushRequestBus implements Runnable { private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test public PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 1, 3, TimeUnit.SECONDS); + scheduledExecutor.scheduleAtFixedRate(this, 3, 3, TimeUnit.SECONDS); } public void add(PushRequest request) { diff --git a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java index 5e9dda34..2fc93266 100644 --- a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java +++ b/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java @@ -18,10 +18,10 @@ public void testSend() throws Exception { } - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { PushClient client = new PushClient(); client.init(); - Thread.sleep(5000); + Thread.sleep(1000); client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), new PushSender.Callback() { @Override diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index fb161b59..8d69e35a 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -39,7 +39,7 @@ public void encode(ByteBuf body) { } public static ErrorMessage from(BaseMessage src) { - return new ErrorMessage(new Packet(Command.ERROR.cmd + return new ErrorMessage(src.packet.cmd, new Packet(Command.ERROR.cmd , src.packet.sessionId), src.connection); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java index 7082302c..e784a40c 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; @@ -36,7 +37,8 @@ public void encode(ByteBuf body) { } public static OkMessage from(BaseMessage src) { - return new OkMessage(src.packet.cmd, src.createResponse(), src.connection); + return new OkMessage(src.packet.cmd, new Packet(Command.OK.cmd + , src.packet.sessionId), src.connection); } public OkMessage setCode(byte code) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java index 99be5684..c67558ee 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java @@ -12,6 +12,10 @@ public class PushMessage extends BaseMessage { public String content; + public PushMessage(Packet packet, Connection connection) { + super(packet, connection); + } + public PushMessage(String content, Connection connection) { super(new Packet(Command.PUSH.cmd, genSessionId()), connection); this.content = content; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java index 39e35671..6753cd43 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java @@ -23,7 +23,7 @@ public RemoteRouter lookup(String userId) { if (cached != null) return cached; RemoteRouter router = super.lookup(userId); if (router != null) { - cache.put(userId, cached); + cache.put(userId, router); } return router; } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 75f2e46d..ba8e7779 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -12,12 +12,12 @@ public class RemoteRouterManager implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(RemoteRouterManager.class); @Override - public RemoteRouter register(String userId, RemoteRouter route) { + public RemoteRouter register(String userId, RemoteRouter router) { RemoteRouter old = RedisManage.get(userId, RemoteRouter.class); if (old != null) { RedisManage.del(userId); } - RedisManage.set(userId, route); + RedisManage.set(userId, router); return old; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index 113415e1..d6cd9bb2 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -10,10 +10,11 @@ import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; -import com.shinemo.mpush.tools.zk.manage.ServerManage; +import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; +import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,12 +27,25 @@ public final class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final App APP = new App(); + private ConnectionServer connectionServer; + private GatewayServer gatewayServer; public static void main(String[] args) throws Exception { LOGGER.error("mpush app start begin...."); APP.init(); + APP.initRedisClient(); APP.startConnectionServer(); APP.startGatewayServer(); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + if (APP.connectionServer != null) { + APP.connectionServer.stop(null); + } + if (APP.gatewayServer != null) { + APP.gatewayServer.stop(null); + } + } + }); LOGGER.error("mpush app start end...."); } @@ -50,7 +64,7 @@ public void run() { server.start(new Server.Listener() { @Override public void onSuccess() { - registerServerToZK(port, PathEnum.CONNECTION_SERVER); + registerServerToZK(port, ZKPath.CONNECTION_SERVER); LOGGER.error("mpush app start connection server success...."); } @@ -60,6 +74,7 @@ public void onFailure(String message) { System.exit(-1); } }); + APP.connectionServer = server; } }, "conn-server", false).start(); } @@ -74,7 +89,7 @@ public void run() { server.start(new Server.Listener() { @Override public void onSuccess() { - registerServerToZK(port, PathEnum.GATEWAY_SERVER); + registerServerToZK(port, ZKPath.GATEWAY_SERVER); LOGGER.error("mpush app start gateway server success...."); } @@ -84,25 +99,28 @@ public void onFailure(String message) { LOGGER.error("mpush app start gateway server failure, jvm exit with code -2"); } }); + APP.gatewayServer = server; } }, "gateway-server", false).start(); } - private void registerServerToZK(int port, PathEnum path) { + private void registerServerToZK(int port, ZKPath path) { ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), port); - ServerManage manage = new ServerManage(app, path); - manage.start(); + ZkUtil.instance.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); LOGGER.error("mpush app register server:{} to zk success", port); } - public void startRedisClient() { - RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); - - RedisGroup group1 = new RedisGroup(); - group1.addRedisNode(node1); - - List groupList = Lists.newArrayList(group1); - ZkUtil.instance.registerPersist(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress()) - , Jsons.toJson(groupList)); + public void initRedisClient() throws Exception { + Stat stat = ZkUtil.instance.getClient().checkExists().forPath(ZKPath.REDIS_SERVER.getPath()); + if (stat == null) { + RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); + RedisGroup group1 = new RedisGroup(); + group1.addRedisNode(node1); + List groupList = Lists.newArrayList(group1); + ZkUtil.instance.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } + RedisPathListener listener = new RedisPathListener(); + ZkUtil.instance.getCache().getListenable().addListener(listener); + listener.initData(null); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index e00d6400..64107190 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -9,7 +9,9 @@ import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.common.message.PushMessage; import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.core.router.RouterCenter; +import com.shinemo.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; @@ -35,7 +37,7 @@ public void handle(final GatewayPushMessage message) { .from(message) .setErrorCode(ErrorCode.OFFLINE) .send(); - LOGGER.warn("gateway push router not exists user offline userId={}, content={}", message.userId, message.content); + LOGGER.warn("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); } else if (router.getRouteType() == Router.RouterType.LOCAL) { //2.如果是本地路由信息,说明用户链接在当前机器,直接把消息下发到客户端 Connection connection = (Connection) router.getRouteValue(); @@ -48,23 +50,34 @@ public void operationComplete(ChannelFuture future) throws Exception { .from(message) .setData(message.userId) .send(); + LOGGER.info("gateway push message to client success userId={}, content={}", message.userId, message.content); } else {//推送失败 ErrorMessage .from(message) .setErrorCode(ErrorCode.PUSH_CLIENT_FAILURE) .send(); + LOGGER.error("gateway push message to client failure userId={}, content={}", message.userId, message.content); } } }); - LOGGER.debug("gateway push router in local userId={}, connection={}", message.userId, connection); + LOGGER.info("gateway push, router in local userId={}, connection={}", message.userId, connection); } else { + RemoteRouter r = (RemoteRouter) router; + if (r.getRouteValue().getHost().equals(MPushUtil.getLocalIp())) { + ErrorMessage + .from(message) + .setErrorCode(ErrorCode.OFFLINE) + .send(); + LOGGER.error("gateway push error remote is local, userId={}, router={}", message.userId, router); + return; + } //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 // 需要通过GatewayClient或ZK把消息推送到另外一台机器上 ErrorMessage .from(message) .setErrorCode(ErrorCode.ROUTER_CHANGE) .send(); - LOGGER.info("gateway push router in remote userId={}, router={}", message.userId, router); + LOGGER.info("gateway push, router in remote userId={}, router={}", message.userId, router); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index b2628e0c..402c858d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -15,8 +15,9 @@ public class LocalRouterManager implements RouterManager { private final Map routerMap = new ConcurrentHashMap<>(); @Override - public LocalRouter register(String userId, LocalRouter route) { - return routerMap.put(userId, route); + public LocalRouter register(String userId, LocalRouter router) { + LOGGER.debug("register local router success userId={}, router={}", userId, router); + return routerMap.put(userId, router); } @Override @@ -28,6 +29,8 @@ public boolean unRegister(String userId) { @Override public LocalRouter lookup(String userId) { - return routerMap.get(userId); + LocalRouter router = routerMap.get(userId); + LOGGER.debug("lookup local router userId={}, router={}", userId, router); + return router; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 8e562554..2df56161 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -42,7 +42,7 @@ void onRouteChangeEvent(RouterChangeEvent event) { } } - public void kickLocal(final String userId, LocalRouter router) { + public void kickLocal(final String userId, final LocalRouter router) { Connection connection = router.getRouteValue(); SessionContext context = connection.getSessionContext(); KickUserMessage message = new KickUserMessage(connection); @@ -52,9 +52,9 @@ public void kickLocal(final String userId, LocalRouter router) { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - LOGGER.info("kick local connection success, userId={}", userId); + LOGGER.info("kick local connection success, userId={}, router={}", userId, router); } else { - LOGGER.error("kick local connection failure, userId={}", userId); + LOGGER.error("kick local connection failure, userId={}, router={}", userId, router); } } }); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 7cd5f750..9823406e 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -136,16 +136,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.info("fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={},deviceId={}, message={},", userId); - if (!message.deviceId.equals(deviceId)) { - ctx.close(); - } + LOGGER.error("receive kick user userId={}, deviceId={}, message={},", userId, deviceId, message); + ctx.close(); } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); LOGGER.error("receive an error packet=" + errorMessage); } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); LOGGER.info("receive an success packet=" + okMessage); + } else if (command == Command.PUSH) { + PushMessage message = new PushMessage(packet, connection); + LOGGER.info("receive an push message, content=" + message.content); } } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 74177bed..e5f0c468 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -6,12 +6,14 @@ import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.Strings; -import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -24,13 +26,24 @@ public void setUp() throws Exception { ConfigCenter.INSTANCE.init(); } + private List getAllServers() { + List list = ZkUtil.instance.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; + List servers = new ArrayList<>(); + for (String name : list) { + String json = ZkUtil.instance.get(ZKPath.CONNECTION_SERVER.getFullPath(name)); + if (Strings.isBlank(json)) continue; + ServerApp server = Jsons.fromJson(json, ServerApp.class); + if (server != null) servers.add(server); + } + return servers; + } + public void testClient() throws Exception { - List hosts = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER.getPath()); - if (hosts == null || hosts.isEmpty()) return; - int index = (int) ((Math.random() % hosts.size()) * hosts.size()); - String name = hosts.get(index); - String json = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER.getPathByName(name)); - ServerApp server = Jsons.fromJson(json, ServerApp.class); + List serverApps = getAllServers(); + if (serverApps == null || serverApps.isEmpty()) return; + int index = (int) ((Math.random() % serverApps.size()) * serverApps.size()); + ServerApp server = serverApps.get(index); ClientChannelHandler handler = new ClientChannelHandler(); final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); client.init(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 2d91c8f7..7bb9a971 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -42,8 +42,8 @@ public boolean isRunning() { public void stop(Listener listener) { LOGGER.info("netty server stop now"); this.startFlag.set(false); - if (workerGroup != null) workerGroup.shutdownGracefully(); - if (bossGroup != null) bossGroup.shutdownGracefully(); + if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); + if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); } @Override diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java deleted file mode 100644 index ad81c72e..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/PathEnum.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.shinemo.mpush.tools.zk; - - -public enum PathEnum { - REDIS_SERVER("/redis", "连接服务器redis注册的地方") { - @Override - public String getPathByIp(String ip) { - return getPath(); - } - - @Override - public String getPathByName(String name) { - return getPath() + "/" + name; - } - }, - CONNECTION_SERVER("/cs/hosts", "连接服务器应用注册的路径") { - @Override - public String getPathByIp(String ip) { - return getPath() + "/machine"; - } - - @Override - public String getPathByName(String name) { - return getPath() + "/" + name; - } - }, - GATEWAY_SERVER("/gs/hosts", "连接服务器应用注册的路径") { - @Override - public String getPathByIp(String ip) { - return getPath() + "/machine"; - } - - @Override - public String getPathByName(String name) { - return getPath() + "/" + name; - } - }; - - PathEnum(String path, String desc) { - this.path = path; - this.desc = desc; - } - - private final String path; - private final String desc; - - public String getPath() { - return path; - } - - public String getDesc() { - return desc; - } - - //不同的机器,注册到不同的路径 - public abstract String getPathByIp(String ip); - - //根据从zk中获取的app的值,拼装全路径 - public abstract String getPathByName(String name); - - public static void main(String[] args) { - String test = "/cs/%s/kick"; - - System.out.println(String.format(test, "10.1.10.65")); - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java new file mode 100644 index 00000000..baa2a793 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush.tools.zk; + + +import org.apache.curator.utils.ZKPaths; + +public enum ZKPath { + REDIS_SERVER("/redis", "machine", "连接服务器redis注册的地方"), + CONNECTION_SERVER("/cs/hosts", "machine", "连接服务器应用注册的路径"), + GATEWAY_SERVER("/gs/hosts", "machine", "连接服务器应用注册的路径"); + + ZKPath(String path, String name, String desc) { + this.path = path; + this.name = name; + } + + private final String path; + private final String name; + + public String getPath() { + return path; + } + + public String getWatchPath() { + return path + ZKPaths.PATH_SEPARATOR + name; + } + + //根据从zk中获取的app的值,拼装全路径 + public String getFullPath(String nodeName) { + return path + ZKPaths.PATH_SEPARATOR + nodeName; + } + + public static void main(String[] args) { + String test = "/cs/%s/kick"; + + System.out.println(String.format(test, "10.1.10.65")); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java index 038c13aa..123ff97d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java @@ -1,67 +1,58 @@ package com.shinemo.mpush.tools.zk.listener; -import java.util.Iterator; -import java.util.Map; - +import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.manage.ServerManage; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Maps; -import com.shinemo.mpush.tools.zk.PathEnum; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener; -import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; -import com.shinemo.mpush.tools.zk.manage.ServerManage; +import java.util.Iterator; +import java.util.Map; public class ListenerDispatcher implements CallBack { - private static final Logger log = LoggerFactory.getLogger(ListenerDispatcher.class); + private static final Logger log = LoggerFactory.getLogger(ListenerDispatcher.class); + + private Map holder = Maps.newTreeMap(); - private Map holder = Maps.newTreeMap(); + public ListenerDispatcher(ServerApp app) { + //所有connection server + //holder.put(ZKPath.CONNECTION_SERVER.getPathByIp(app.getIp()), new ConnPathListener()); + //所有redis + //holder.put(ZKPath.REDIS_SERVER.getPathByIp(app.getIp()), new RedisPathListener()); + //踢人的目录已经交给队列处理了,这里不需要重复处理 + //holder.put(ZKPath.GATEWAY_SERVER.getPathByIp(app.getIp()), new GatewayPathListener()); + } - public ListenerDispatcher(ServerApp app) { - //所有connection server - holder.put(PathEnum.CONNECTION_SERVER.getPathByIp(app.getIp()), new ConnectionPathListener()); - //所有redis - holder.put(PathEnum.REDIS_SERVER.getPathByIp(app.getIp()), new RedisPathListener()); - //踢人的目录已经交给队列处理了,这里不需要重复处理 -// holder.put(PathEnum.CONNECTION_SERVER_KICK.getPathByIp(app.getIp()), new KickPathListener()); - } + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + Iterator> it = holder.entrySet().iterator(); + boolean hasHandler = false; + while (it.hasNext()) { + Map.Entry entry = it.next(); + if (path.startsWith(entry.getKey())) { + hasHandler = true; + entry.getValue().handler(client, event, path); + } + } - Iterator> it = holder.entrySet().iterator(); - boolean hasHandler = false; - while (it.hasNext()) { - Map.Entry entry = it.next(); - if (path.startsWith(entry.getKey())) { - hasHandler = true; - entry.getValue().handler(client, event, path); - } - } - - if(!hasHandler){ - log.warn("ListenerDispatcher other path:" + path + "," + event.getType().name()); - } + if (!hasHandler) { + log.warn("ListenerDispatcher other path:" + path + "," + event.getType().name()); + } - } + } - @Override - public void initData(ServerManage manage) { + @Override + public void initData(ServerManage manage) { - Iterator> it = holder.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = it.next(); - entry.getValue().initData(manage); - } - - } - - public CallBack getListener(PathEnum pathEnum,String ip){ - return holder.get(pathEnum.getPathByIp(ip)); - } + Iterator> it = holder.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + entry.getValue().initData(manage); + } + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java new file mode 100644 index 00000000..d4362d3f --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java @@ -0,0 +1,82 @@ +package com.shinemo.mpush.tools.zk.listener.impl; + +import java.util.List; + +import com.shinemo.mpush.tools.zk.ZKPath; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.listener.CallBack; +import com.shinemo.mpush.tools.zk.manage.ServerAppManage; +import com.shinemo.mpush.tools.zk.manage.ServerManage; + +/** + * 注册的应用的发生变化 + */ +public class ConnPathListener implements CallBack { + + private static final Logger log = LoggerFactory.getLogger(ConnPathListener.class); + + @Override + public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + log.warn("ConnPathListener other path:" + path + "," + event.getType().name() + "," + data); + } + } + + @Override + public void initData(ServerManage manage) { + log.warn("start init app data"); + _initData(); + log.warn("end init app data"); + } + + private void _initData() { + //获取机器列表 + List rawData = ZkUtil.instance.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + for (String raw : rawData) { + String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); + ServerApp app = getServerApp(fullPath); + ServerAppManage.instance.addOrUpdate(fullPath, app); + } + } + + private void dataRemove(ChildData data) { + String path = data.getPath(); + ServerAppManage.instance.remove(path); + } + + private void dataAddOrUpdate(ChildData data) { + String path = data.getPath(); + byte[] rawData = data.getData(); + ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); + ServerAppManage.instance.addOrUpdate(path, serverApp); + } + + private ServerApp getServerApp(String fullPath) { + String rawApp = ZkUtil.instance.get(fullPath); + ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); + return app; + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java deleted file mode 100644 index abbde310..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnectionPathListener.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener.impl; - -import java.util.List; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.zk.PathEnum; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkUtil; -import com.shinemo.mpush.tools.zk.listener.CallBack; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -/** - *注册的应用的发生变化 - * - */ -public class ConnectionPathListener implements CallBack{ - - private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class); - - @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - String data = ""; - if(event.getData()!=null){ - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data); - } - } - - @Override - public void initData(ServerManage manage) { - log.warn("start init app data"); - _initData(); - log.warn("end init app data"); - } - - private void _initData(){ - //获取机器列表 - List rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER.getPath()); - for(String raw:rawData){ - String fullPath = PathEnum.CONNECTION_SERVER.getPathByName(raw); - ServerApp app = getServerApp(fullPath); - ServerAppManage.instance.addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data){ - String path = data.getPath(); - ServerAppManage.instance.remove(path); - } - - private void dataAddOrUpdate(ChildData data){ - String path = data.getPath(); - byte[] rawData = data.getData(); - ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); - ServerAppManage.instance.addOrUpdate(path, serverApp); - } - - private ServerApp getServerApp(String fullPath){ - String rawApp = ZkUtil.instance.get(fullPath); - ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); - return app; - } - - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java similarity index 90% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java index a2726242..9da911d2 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/KickPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java @@ -15,9 +15,9 @@ * 当前应用下踢人的目录发生变化 * */ -public class KickPathListener implements CallBack{ +public class GatewayPathListener implements CallBack{ - private static final Logger log = LoggerFactory.getLogger(KickPathListener.class); + private static final Logger log = LoggerFactory.getLogger(GatewayPathListener.class); @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index 87e6303e..6f0ae20a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -10,27 +10,25 @@ import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.InetAddressUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; -import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkUtil; -import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.manage.ServerManage; /** * 注册的应用的发生变化 */ -public class RedisPathListener implements CallBack { - +public class RedisPathListener implements TreeCacheListener { private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { + public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { String data = ""; if (event.getData() != null) { data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); @@ -42,11 +40,10 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path) } else if (Type.NODE_UPDATED == event.getType()) { dataAddOrUpdate(event.getData()); } else { - log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name() + "," + data); + log.warn("ConnPathListener other path:" + data + "," + event.getType().name() + "," + data); } } - @Override public void initData(ServerManage manage) { log.warn("start init redis data"); _initData(); @@ -55,7 +52,7 @@ public void initData(ServerManage manage) { private void _initData() { //获取redis列表 - List group = getRedisGroup(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress())); + List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); RedisGroupManage.instance.init(group); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index 1b980be5..fbeb9939 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; @@ -27,11 +27,11 @@ public class ServerManage { private final AtomicBoolean startFlag = new AtomicBoolean(false); private final ServerApp app; - private final PathEnum path; + private final ZKPath path; private ListenerDispatcher dispatcher; - public ServerManage(ServerApp app, PathEnum path) { + public ServerManage(ServerApp app, ZKPath path) { this.app = app; this.path = path; } @@ -59,11 +59,11 @@ public void start() { } private void registerApp() { - zkUtil.registerEphemeralSequential(path.getPathByIp(app.getIp()), Jsons.toJson(app)); + zkUtil.registerEphemeralSequential(path.getPath(), Jsons.toJson(app)); } public void unregisterApp() { - zkUtil.remove(path.getPathByIp(app.getIp())); + zkUtil.remove(path.getPath()); } // 注册连接状态监听器 diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index da727922..8feeeb09 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -5,7 +5,7 @@ import com.shinemo.mpush.tools.redis.listener.MessageListener; -import com.shinemo.mpush.tools.zk.PathEnum; +import com.shinemo.mpush.tools.zk.ZKPath; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; @@ -21,7 +21,7 @@ public class RedisGroupManageTest { ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), 3000); - ServerManage manage = new ServerManage(app, PathEnum.REDIS_SERVER); + ServerManage manage = new ServerManage(app, ZKPath.REDIS_SERVER); List groupList = null; RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java index ad5fd05c..66b6d2da 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java @@ -10,7 +10,7 @@ public class DistributedQueueConsumerTest { private ServerApp app = new ServerApp("10.1.10.65", 3000); - private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); @Before public void setup() { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java index 24b61cb0..8ad7a863 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java @@ -16,7 +16,7 @@ public class DistributedQueueProviderTest { private ServerApp app = new ServerApp("10.1.10.64", 3000); - private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); @Before public void setup() { @@ -32,7 +32,7 @@ public void test() throws Exception { while (iterator.hasNext()) { ServerApp app = iterator.next(); if (!app.getIp().equals(this.app.getIp())) { - Provider provider = new Provider<>(PathEnum.GATEWAY_SERVER.getPathByIp(app.getIp()), ServerApp.class); + Provider provider = new Provider<>(ZKPath.GATEWAY_SERVER.getPath(), ServerApp.class); providers.add(provider); provider.start(); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 06f0da77..23250701 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -17,7 +17,7 @@ public class ServerManageTest { private ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), 3000); - private ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); @Test public void testMulThreadRegisterApp() throws InterruptedException { @@ -59,7 +59,7 @@ public void run() { } log.warn("start init " + ip); ServerApp app = new ServerApp(ip, 3000); - ServerManage manage = new ServerManage(app, PathEnum.CONNECTION_SERVER); + ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); manage.start(); try { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index e815c49d..d5e442f1 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -133,14 +133,14 @@ public void testAddRedis() { List groupList = Lists.newArrayList(group1); - zkUtil.registerPersist(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress()), Jsons.toJson(groupList)); + zkUtil.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } @Test public void getRedisTest() { - String value = zkUtil.get(PathEnum.REDIS_SERVER.getPathByIp(InetAddressUtil.getInetAddress())); + String value = zkUtil.get(ZKPath.REDIS_SERVER.getPath()); List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); for (RedisGroup group : newGroupList) { for (RedisNode node : group.getRedisNodeList()) { From e519dcafe49a6b5d820f8086c5ac00e098da2b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 07:10:03 +0000 Subject: [PATCH 115/890] add push client --- .../src/main/java/com/shinemo/mpush/client/PushClient.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 0626e558..82cec037 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -103,7 +103,10 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) } else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) { ServerApp serverApp = servers.remove(path); if (serverApp != null) { - clientMap.remove(serverApp.getIp()); + Client client = clientMap.remove(serverApp.getIp()); + if (client != null) { + client.stop(); + } } } else if (TreeCacheEvent.Type.NODE_UPDATED == event.getType()) { From 65e8949784bf0bcacf82b5930b860a6c33a6306a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 07:17:08 +0000 Subject: [PATCH 116/890] add push client --- .../src/main/java/com/shinemo/mpush/client/PushClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 82cec037..17841ed8 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -65,7 +65,6 @@ public Connection getConnection(String ip) { @Override public void send(String content, Collection userIds, Callback callback) { for (String userId : userIds) { - System.out.println(userId); PushRequest .build(this) .setCallback(callback) From 2e3a3f9badfb622dc43812a41dc23aa3be538f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 09:01:24 +0000 Subject: [PATCH 117/890] body encode empty byte bug fix --- .../java/com/shinemo/mpush/common/message/ByteBufMessage.java | 4 +++- .../com/shinemo/mpush/core/netty/ClientChannelHandler.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java index 232dd801..5ad18876 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java @@ -24,7 +24,9 @@ public void decode(byte[] body) { public byte[] encode() { ByteBuf body = Unpooled.buffer(); encode(body); - return body.array(); + byte[] bytes = new byte[body.readableBytes()]; + body.readBytes(bytes); + return bytes; } public abstract void decode(ByteBuf body); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java index 9823406e..99453b8d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java @@ -120,7 +120,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); startHeartBeat(message.heartbeat, ctx.channel()); - LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, message.serverKey); + LOGGER.info("会话密钥:{},message={}", sessionKey, message); saveToken(message, connection.getSessionContext()); bindUser(); } else if (command == Command.FAST_CONNECT) { From 617d2ba59610a42e98a61a44cfc0512aa32ad41f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 09:53:02 +0000 Subject: [PATCH 118/890] body encode empty byte bug fix --- .../com/shinemo/mpush/core/router/KickRemoteMsg.java | 4 ++-- .../shinemo/mpush/core/router/RouterChangeListener.java | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java index c09142bd..96749d2f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java @@ -6,14 +6,14 @@ public class KickRemoteMsg { public String userId; public String deviceId; - public String srcServer; + public String targetServer; @Override public String toString() { return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + - ", srcServer='" + srcServer + '\'' + + ", targetServer='" + targetServer + '\'' + '}'; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 2df56161..4d16ca2f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -13,7 +13,6 @@ import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.listener.MessageListener; import com.shinemo.mpush.tools.redis.manage.RedisManage; -import com.shinemo.mpush.tools.redis.pubsub.Subscriber; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; @@ -62,18 +61,24 @@ public void operationComplete(ChannelFuture future) throws Exception { public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); + //如果是本机直接忽略 if (location.getHost().equals(MPushUtil.getLocalIp())) { LOGGER.error("kick remote user but router in local, userId={}", userId); return; } KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); - msg.srcServer = location.getHost(); + msg.targetServer = location.getHost(); msg.userId = userId; RedisManage.publish(KICK_CHANNEL, msg); } public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { + //如果目标不是本机,直接忽略 + if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { + LOGGER.error("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + return; + } String userId = msg.userId; LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); LocalRouter router = routerManager.lookup(userId); From 88f3aad13bae26b3a6b2546d1001293fe0b73f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 10:16:05 +0000 Subject: [PATCH 119/890] body encode empty byte bug fix --- .../com/shinemo/mpush/core/handler/GatewayPushHandler.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index 64107190..2a2af3fb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -41,6 +41,12 @@ public void handle(final GatewayPushMessage message) { } else if (router.getRouteType() == Router.RouterType.LOCAL) { //2.如果是本地路由信息,说明用户链接在当前机器,直接把消息下发到客户端 Connection connection = (Connection) router.getRouteValue(); + if (!connection.isConnected()) { + RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); + handle(message);//递归在试一次,看用户是否登陆在远程 + LOGGER.info("gateway push, router in local but disconnect userId={}, connection={}", message.userId, connection); + return; + } PushMessage pushMessage = new PushMessage(message.content, connection); pushMessage.send(new ChannelFutureListener() { @Override From b125ab1be51b629ed6fc678b09f881afb68b4ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 10:17:24 +0000 Subject: [PATCH 120/890] body encode empty byte bug fix --- .../java/com/shinemo/mpush/core/handler/GatewayPushHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index 2a2af3fb..a15df1f2 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -42,9 +42,9 @@ public void handle(final GatewayPushMessage message) { //2.如果是本地路由信息,说明用户链接在当前机器,直接把消息下发到客户端 Connection connection = (Connection) router.getRouteValue(); if (!connection.isConnected()) { + LOGGER.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); handle(message);//递归在试一次,看用户是否登陆在远程 - LOGGER.info("gateway push, router in local but disconnect userId={}, connection={}", message.userId, connection); return; } PushMessage pushMessage = new PushMessage(message.content, connection); From fdbeaa9824dbe699ab7f3b7d67b528e32f24b455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 16:19:49 +0000 Subject: [PATCH 121/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/router/ClientLocation.java | 24 ++-- .../mpush/common/MessageDispatcher.java | 2 +- .../mpush/common/message/PushMessage.java | 2 +- .../router/ConnectionRouterManager.java | 2 +- .../mpush/common/router/RemoteRouter.java | 2 +- .../main/java/com/shinemo/mpush/core/App.java | 4 +- .../mpush/core/handler/BindUserHandler.java | 5 +- .../core/handler/GatewayPushHandler.java | 77 ++++++----- .../mpush/core/handler/HandshakeHandler.java | 4 +- .../mpush/core/router/KickRemoteMsg.java | 2 +- .../mpush/core/router/LocalRouter.java | 2 +- .../mpush/core/router/LocalRouterManager.java | 2 +- .../mpush/core/router/RouterCenter.java | 11 +- .../core/router/RouterChangeListener.java | 2 +- .../mpush/core/server/AdminServer.java | 24 ++++ .../core/server/ServerChannelHandler.java | 2 +- .../core/session/ReusableSessionManager.java | 11 +- .../mpush/netty/codec/PacketDecoder.java | 2 +- .../mpush/netty/codec/PacketEncoder.java | 2 +- .../com/shinemo/mpush/tools/ConfigCenter.java | 6 + .../shinemo/mpush/tools/InetAddressUtil.java | 42 ------ .../com/shinemo/mpush/tools/MPushUtil.java | 38 +++++- .../java/com/shinemo/mpush/tools/Pair.java | 2 +- .../com/shinemo/mpush/tools/RandomUtil.java | 20 --- .../tools/redis/manage/RedisGroupManage.java | 120 +++++++++--------- .../tools/redis/RedisGroupManageTest.java | 4 +- .../mpush/tools/zk/ServerManageTest.java | 4 +- .../shinemo/mpush/tools/zk/ZkUtilTest.java | 8 +- 28 files changed, 229 insertions(+), 197 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java index 39106d01..813d81b9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java @@ -21,28 +21,32 @@ public ClientLocation setDeviceId(String deviceId) { return this; } + + public String getHost() { + return host; + } + + public ClientLocation setHost(String host) { + this.host = host; + return this; + } + public String getOsName() { return osName; } - public void setOsName(String osName) { + public ClientLocation setOsName(String osName) { this.osName = osName; + return this; } public String getClientVersion() { return clientVersion; } - public void setClientVersion(String clientVersion) { + public ClientLocation setClientVersion(String clientVersion) { this.clientVersion = clientVersion; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; + return this; } public static ClientLocation from(SessionContext context) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java index ea6b07eb..643f9887 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java @@ -14,7 +14,7 @@ /** * Created by ohun on 2015/12/22. */ -public class MessageDispatcher implements PacketReceiver { +public final class MessageDispatcher implements PacketReceiver { public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); private final Map handlers = new HashMap<>(); diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java index c67558ee..fe3766d2 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2015/12/30. */ -public class PushMessage extends BaseMessage { +public final class PushMessage extends BaseMessage { public String content; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java index 6753cd43..a031fa55 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2016/1/4. */ -public class ConnectionRouterManager extends RemoteRouterManager { +public final class ConnectionRouterManager extends RemoteRouterManager { public static final ConnectionRouterManager INSTANCE = new ConnectionRouterManager(); // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 private final Cache cache = CacheBuilder diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java index 5b739d0e..eed8dcf6 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java @@ -6,7 +6,7 @@ /** * Created by ohun on 2015/12/23. */ -public class RemoteRouter implements Router { +public final class RemoteRouter implements Router { private final ClientLocation clientLocation; public RemoteRouter(ClientLocation clientLocation) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index d6cd9bb2..db71e436 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -5,7 +5,7 @@ import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.tools.ConfigCenter; -import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; @@ -105,7 +105,7 @@ public void onFailure(String message) { } private void registerServerToZK(int port, ZKPath path) { - ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), port); + ServerApp app = new ServerApp(MPushUtil.getLocalIp(), port); ZkUtil.instance.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); LOGGER.error("mpush app register server:{} to zk success", port); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index e495474c..2f32de22 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -30,15 +30,18 @@ public void handle(BindUserMessage message) { LOGGER.error("bind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } + //1.绑定用户时先看下是否握手成功 SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { + //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { OkMessage.from(message).setData("bind success").send(); LOGGER.warn("bind user success, userId={}, session={}", message.userId, context); } else { - ErrorMessage.from(message).setReason("bind failed").close(); + //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.INSTANCE.unRegister(message.userId); + ErrorMessage.from(message).setReason("bind failed").close(); LOGGER.error("bind user failure, register router failure, userId={}, session={}", message.userId, context); } } else { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index a15df1f2..eef8d418 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -2,14 +2,13 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.router.ClientLocation; import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.common.ErrorCode; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.common.message.PushMessage; import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.core.router.RouterCenter; import com.shinemo.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; @@ -17,6 +16,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.shinemo.mpush.api.router.Router.RouterType.LOCAL; +import static com.shinemo.mpush.common.ErrorCode.*; + /** * Created by ohun on 2015/12/30. */ @@ -30,59 +32,72 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { @Override public void handle(final GatewayPushMessage message) { + //查询路由信息,先查本地,本地不存在,查远程,有可能远程也是本机(在本地被删除的情况下) Router router = RouterCenter.INSTANCE.lookup(message.userId); + if (router == null) { + //1.路由信息不存在说明用户此时不在线 - ErrorMessage - .from(message) - .setErrorCode(ErrorCode.OFFLINE) - .send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + LOGGER.warn("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); - } else if (router.getRouteType() == Router.RouterType.LOCAL) { - //2.如果是本地路由信息,说明用户链接在当前机器,直接把消息下发到客户端 + } else if (router.getRouteType() == LOCAL) { + + //2.如果是本地路由信息,说明用户链接在当前机器,如果链接可用,直接把消息下发到客户端 Connection connection = (Connection) router.getRouteValue(); + if (!connection.isConnected()) { + + //2.1如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 LOGGER.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + + //2.2删除已经失效的本地路由,防止递归死循环 RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); - handle(message);//递归在试一次,看用户是否登陆在远程 + + //2.3递归再试一次,看用户是否登陆在远程其他机器 + this.handle(message); + return; } + + //2.4下发消息到手机客户端 PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) {//推送成功 - OkMessage - .from(message) - .setData(message.userId) - .send(); + if (future.isSuccess()) { + //推送成功 + OkMessage.from(message).setData(message.userId).send(); + LOGGER.info("gateway push message to client success userId={}, content={}", message.userId, message.content); - } else {//推送失败 - ErrorMessage - .from(message) - .setErrorCode(ErrorCode.PUSH_CLIENT_FAILURE) - .send(); + } else { + //推送失败 + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); + LOGGER.error("gateway push message to client failure userId={}, content={}", message.userId, message.content); } } }); + LOGGER.info("gateway push, router in local userId={}, connection={}", message.userId, connection); } else { - RemoteRouter r = (RemoteRouter) router; - if (r.getRouteValue().getHost().equals(MPushUtil.getLocalIp())) { - ErrorMessage - .from(message) - .setErrorCode(ErrorCode.OFFLINE) - .send(); + + //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 + ClientLocation location = (ClientLocation) router.getRouteValue(); + + if (MPushUtil.getLocalIp().equals(location.getHost())) { + //3.1如果查出的远程机器是当前机器,说明本机路由已经失效,此时说明用户已经不在线 + ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + LOGGER.error("gateway push error remote is local, userId={}, router={}", message.userId, router); + return; } - //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 - // 需要通过GatewayClient或ZK把消息推送到另外一台机器上 - ErrorMessage - .from(message) - .setErrorCode(ErrorCode.ROUTER_CHANGE) - .send(); + + //3.2返回给推送服务,路由信息发生更改 + ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + LOGGER.info("gateway push, router in remote userId={}, router={}", message.userId, router); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index cbf051c7..f0ff2cf7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -74,12 +74,14 @@ public void handle(HandshakeMessage message) { //7.更换会话密钥AES(clientKey)=>AES(sessionKey) context.changeCipher(new AesCipher(sessionKey, iv)); - //8.保存client信息 + //8.保存client信息到当前连接 context.setOsName(message.osName) .setOsVersion(message.osVersion) .setClientVersion(message.clientVersion) .setDeviceId(message.deviceId) .setHeartbeat(heartbeat); + + //9.保存可复用session到Redis, 用于快速重连 ReusableSessionManager.INSTANCE.cacheSession(session); //9.触发握手成功事件 diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java index 96749d2f..c8d3cd92 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java @@ -3,7 +3,7 @@ /** * Created by ohun on 2016/1/4. */ -public class KickRemoteMsg { +public final class KickRemoteMsg { public String userId; public String deviceId; public String targetServer; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java index 138c787c..60affbc8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java @@ -6,7 +6,7 @@ /** * Created by ohun on 2015/12/23. */ -public class LocalRouter implements Router { +public final class LocalRouter implements Router { private final Connection connection; public LocalRouter(Connection connection) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 402c858d..1690c379 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -10,7 +10,7 @@ /** * Created by ohun on 2015/12/23. */ -public class LocalRouterManager implements RouterManager { +public final class LocalRouterManager implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); private final Map routerMap = new ConcurrentHashMap<>(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 4eb799a4..e82f59ab 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -14,7 +14,7 @@ /** * Created by ohun on 2015/12/23. */ -public class RouterCenter { +public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); public static final RouterCenter INSTANCE = new RouterCenter(); @@ -30,10 +30,13 @@ public class RouterCenter { * @return */ public boolean register(String userId, Connection connection) { - ClientLocation connConfig = ClientLocation.from(connection.getSessionContext()); - connConfig.setHost(MPushUtil.getLocalIp()); + ClientLocation location = ClientLocation + .from(connection.getSessionContext()) + .setHost(MPushUtil.getLocalIp()); + LocalRouter localRouter = new LocalRouter(connection); - RemoteRouter remoteRouter = new RemoteRouter(connConfig); + RemoteRouter remoteRouter = new RemoteRouter(location); + LocalRouter oldLocalRouter = null; RemoteRouter oldRemoteRouter = null; try { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 4d16ca2f..80c715c3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -21,7 +21,7 @@ /** * Created by ohun on 2016/1/4. */ -public class RouterChangeListener implements MessageListener { +public final class RouterChangeListener implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); public static final String KICK_CHANNEL = "__kick__"; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java new file mode 100644 index 00000000..3e08f30d --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java @@ -0,0 +1,24 @@ +package com.shinemo.mpush.core.server; + +import com.shinemo.mpush.api.Server; + +/** + * Created by ohun on 2016/1/8. + */ +public final class AdminServer implements Server { + + @Override + public void start(Listener listener) { + + } + + @Override + public void stop(Listener listener) { + + } + + @Override + public boolean isRunning() { + return false; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 8c60e19a..0dbe6bcc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -18,7 +18,7 @@ * Created by ohun on 2015/12/19. */ @ChannelHandler.Sharable -public class ServerChannelHandler extends ChannelHandlerAdapter { +public final class ServerChannelHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index 306fd8a6..18fd5fcb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -1,22 +1,20 @@ package com.shinemo.mpush.core.session; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.crypto.MD5Utils; import com.shinemo.mpush.tools.redis.manage.RedisManage; -import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - -import java.util.Map; /** * Created by ohun on 2015/12/25. */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private static final int EXPIRE_TIME = 86400; + private int expiredTime = ConfigCenter.INSTANCE.getSessionExpiredTime(); public boolean cacheSession(ReusableSession session) { - RedisManage.set(session.sessionId, session.encode(), EXPIRE_TIME); + RedisManage.set(session.sessionId, session.encode(), expiredTime); return true; } @@ -37,8 +35,7 @@ public ReusableSession genSession(SessionContext context) { ReusableSession session = new ReusableSession(); session.context = context; session.sessionId = MD5Utils.encrypt(context.deviceId + now); - session.expireTime = now + EXPIRE_TIME * 1000; + session.expireTime = now + expiredTime * 1000; return session; } - } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index 1ade78e8..b531a573 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -15,7 +15,7 @@ * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ -public class PacketDecoder extends ByteToMessageDecoder { +public final class PacketDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java index e0bb066f..cb0e8704 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java @@ -12,7 +12,7 @@ * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ @ChannelHandler.Sharable -public class PacketEncoder extends MessageToByteEncoder { +public final class PacketEncoder extends MessageToByteEncoder { public static final PacketEncoder INSTANCE = new PacketEncoder(); @Override diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java index 1b747fed..374d2bf3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java @@ -14,6 +14,7 @@ public final class ConfigCenter { private int minHeartbeat = 1000 * 10;//10s private int maxHeartbeat = 1000 * 60 * 30;//30min private int maxHBTimeoutTimes = 2; + private int sessionExpiredTime = 86400;//unit second private int rasKeyLength = 1024; private int aesKeyLength = 16; private int connectionServerPort = 3000; @@ -39,6 +40,7 @@ public void init() throws IOException { minHeartbeat = getInt("MIN_HEARTBEAT", minHeartbeat); maxHeartbeat = getInt("MAX_HEARTBEAT", maxHeartbeat); maxHBTimeoutTimes = getInt("MAX_HB_TIMEOUT_TIMES", maxHBTimeoutTimes); + sessionExpiredTime = getInt("SESSION_EXPIRED_TIME", sessionExpiredTime); rasKeyLength = getInt("RAS_KEY_LENGTH", rasKeyLength); aesKeyLength = getInt("AES_KEY_LENGTH", aesKeyLength); maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); @@ -83,6 +85,10 @@ public int getMaxHBTimeoutTimes() { return maxHBTimeoutTimes; } + public int getSessionExpiredTime() { + return sessionExpiredTime; + } + public int getRasKeyLength() { return rasKeyLength; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java deleted file mode 100644 index a4e28058..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/InetAddressUtil.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.shinemo.mpush.tools; - -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.util.Enumeration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class InetAddressUtil { - - private static final Logger log = LoggerFactory.getLogger(InetAddressUtil.class); - - /** - * 获取本机ip - * 只获取第一块网卡绑定的ip地址 - * - * @return - */ - public static String getInetAddress() { - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; - while (interfaces.hasMoreElements()) { - NetworkInterface ni = interfaces.nextElement(); - Enumeration addresses = ni.getInetAddresses(); - while (addresses.hasMoreElements()) { - address = addresses.nextElement(); - if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && address.isSiteLocalAddress()) { - return address.getHostAddress(); - } - } - } - log.warn("[InetAddressUtil] getInetAddress is null"); - return "127.0.0.1"; - } catch (Throwable e) { - log.error("[InetAddressUtil] getInetAddress exception", e); - return "127.0.0.1"; - } - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 003e7350..4c28496a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -1,14 +1,23 @@ package com.shinemo.mpush.tools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; + /** * Created by ohun on 2015/12/25. */ public final class MPushUtil { + private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); + private static String LOCAL_IP; public static String getLocalIp() { if (LOCAL_IP == null) { - LOCAL_IP = InetAddressUtil.getInetAddress(); + LOCAL_IP = getInetAddress(); } return LOCAL_IP; } @@ -20,4 +29,31 @@ public static int getHeartbeat(int min, int max) { ); } + /** + * 获取本机ip + * 只获取第一块网卡绑定的ip地址 + * + * @return + */ + public static String getInetAddress() { + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress address = null; + while (interfaces.hasMoreElements()) { + NetworkInterface ni = interfaces.nextElement(); + Enumeration addresses = ni.getInetAddresses(); + while (addresses.hasMoreElements()) { + address = addresses.nextElement(); + if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && address.isSiteLocalAddress()) { + return address.getHostAddress(); + } + } + } + LOGGER.warn("getInetAddress is null"); + return "127.0.0.1"; + } catch (Throwable e) { + LOGGER.error("getInetAddress exception", e); + return "127.0.0.1"; + } + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java index 32c7f143..7e229aec 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java @@ -3,7 +3,7 @@ /** * Created by ohun on 2015/12/24. */ -public class Pair { +public final class Pair { public final K key; public final V value; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java deleted file mode 100644 index 4e1e3138..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/RandomUtil.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.shinemo.mpush.tools; - -import java.util.Random; - -public class RandomUtil { - - public static int random(int total){ - Random ran = new Random(); - return ran.nextInt(total); - } - - public static void main(String[] args) { - - for(int i = 0;i<20;i++){ - System.out.println(random(3)); - } - - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java index c42d645b..d99959da 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java @@ -9,68 +9,72 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Lists; -import com.shinemo.mpush.tools.RandomUtil; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; public class RedisGroupManage { - - - private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); - - private static List holder = Lists.newArrayList(); - - public static final RedisGroupManage instance = new RedisGroupManage(); - - - private RedisGroupManage() { - } - - public void init(List group){ - holder = group; - printGroupList(); - } - - - public List getGroupList() { - return Collections.unmodifiableList(holder); - } - - private void printGroupList(){ - for(RedisGroup app:holder){ - log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); - } - } - - public int groupSize(){ - return holder.size(); - } - - /** - * 随机获取一个redis 实例 - * @param key - * @return - */ - public RedisNode randomGetRedisNode(String key){ - int i = RandomUtil.random(groupSize()); - RedisGroup group = holder.get(i); - return group.get(key); - } - - /** - * 写操作的时候,获取所有redis 实例 - * @param key - * @return - */ - public List hashSet(String key){ - List nodeList = Lists.newArrayList(); - for(RedisGroup group:holder){ - RedisNode node = group.get(key); - nodeList.add(node); - } - return nodeList; - } - + + private static final Logger LOGGER = LoggerFactory.getLogger(ServerAppManage.class); + + private static List groups = Lists.newArrayList(); + + public static final RedisGroupManage instance = new RedisGroupManage(); + + + private RedisGroupManage() { + } + + public void init(List group) { + if (group == null || group.isEmpty()) { + throw new RuntimeException("init redis client error, redis server is none."); + } + groups = group; + printGroupList(); + } + + + public List getGroupList() { + return Collections.unmodifiableList(groups); + } + + private void printGroupList() { + for (RedisGroup app : groups) { + LOGGER.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + } + } + + public int groupSize() { + return groups.size(); + } + + /** + * 随机获取一个redis 实例 + * + * @param key + * @return + */ + public RedisNode randomGetRedisNode(String key) { + int size = groupSize(); + if (size == 1) return groups.get(0).get(key); + int i = (int) ((Math.random() % size) * size); + RedisGroup group = groups.get(i); + return group.get(key); + } + + /** + * 写操作的时候,获取所有redis 实例 + * + * @param key + * @return + */ + public List hashSet(String key) { + List nodeList = Lists.newArrayList(); + for (RedisGroup group : groups) { + RedisNode node = group.get(key); + nodeList.add(node); + } + return nodeList; + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 8feeeb09..c27f2d7c 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -11,7 +11,7 @@ import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; import com.shinemo.mpush.tools.redis.manage.RedisManage; import com.shinemo.mpush.tools.redis.pubsub.Subscriber; @@ -20,7 +20,7 @@ public class RedisGroupManageTest { - ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), 3000); + ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); ServerManage manage = new ServerManage(app, ZKPath.REDIS_SERVER); List groupList = null; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java index 23250701..0b5eb05e 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java @@ -8,14 +8,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.zk.manage.ServerManage; public class ServerManageTest { private static Executor executor = Executors.newCachedThreadPool(); - private ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(), 3000); + private ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index d5e442f1..09d14e18 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -10,7 +10,7 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.InetAddressUtil; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; @@ -92,13 +92,13 @@ public void testRegister() { @Test public void testLocalIp() { - System.out.println(InetAddressUtil.getInetAddress()); + System.out.println(MPushUtil.getLocalIp()); } @Test public void testRegisterIp() { - String localIp = InetAddressUtil.getInetAddress(); + String localIp = MPushUtil.getInetAddress(); ServerApp app = new ServerApp(localIp, 3000); zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); String value = zkUtil.get("/" + localIp); @@ -112,7 +112,7 @@ public void testRemove() { @Test public void testAddKickOff() { - String localIp = InetAddressUtil.getInetAddress(); + String localIp = MPushUtil.getInetAddress(); String kick = Constants.ZK_KICK; String ip = "10.1.10.65"; zkUtil.registerEphemeral("/" + localIp + "/" + kick, ip); From fb286bdc3bb11b7c7caf8beb773a20f9220b6bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 16:25:28 +0000 Subject: [PATCH 122/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/api/router/ClientLocation.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java index 813d81b9..ba022e7a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java @@ -6,9 +6,25 @@ * Created by ohun on 2015/12/23. */ public final class ClientLocation { + + /** + * 长链接所在的机器IP + */ private String host; + + /** + * 客户端系统类型 + */ private String osName; + + /** + * 客户端版本 + */ private String clientVersion; + + /** + * 客户端设备ID + */ private String deviceId; From de64383d64dacfbd5a1affeed5aa666a9e2fbc91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 8 Jan 2016 16:33:51 +0000 Subject: [PATCH 123/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/security/AesCipher.java | 3 ++- .../mpush/core/handler/FastConnectHandler.java | 17 ++++++++++++++--- .../mpush/core/session/ReusableSession.java | 11 +++++++---- .../core/session/ReusableSessionManager.java | 10 ++-------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index 4eff437d..3f168156 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.common.security; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Cipher; import com.shinemo.mpush.tools.crypto.AESUtils; @@ -44,7 +45,7 @@ public String toString(byte[] a) { public static byte[] toArray(String str) { String[] a = str.split("\\|"); if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { - throw new RuntimeException("decode cipher ex key length invalid"); + return null; } byte[] bytes = new byte[a.length]; for (int i = 0; i < a.length; i++) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 7d94a3cb..7afabdd9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -27,22 +27,33 @@ public FastConnectMessage decode(Packet packet, Connection connection) { @Override public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); + if (session == null) { - ErrorMessage.from(message).setReason("session expire").send(); + + ErrorMessage.from(message).setReason("session expired").send(); + LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); + } else if (!session.context.deviceId.equals(message.deviceId)) { - ErrorMessage.from(message).setReason("error device").send(); - LOGGER.warn("fast connect failure, not same device, deviceId={}, session={}", message.deviceId, session.context); + + ErrorMessage.from(message).setReason("invalid device").send(); + + LOGGER.warn("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); + } else { + int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + session.context.setHeartbeat(heartbeat); message.getConnection().setSessionContext(session.context); + FastConnectOkMessage .from(message) .setServerHost(MPushUtil.getLocalIp()) .setServerTime(System.currentTimeMillis()) .setHeartbeat(heartbeat) .send(); + LOGGER.warn("fast connect success, session={}", message.deviceId, session.context); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java index 9175a420..0a79fb62 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java @@ -13,7 +13,7 @@ public final class ReusableSession { public long expireTime; public SessionContext context; - public String encode() { + public static String encode(SessionContext context) { StringBuffer sb = new StringBuffer(); sb.append(context.osName).append(','); sb.append(context.osVersion).append(','); @@ -23,9 +23,9 @@ public String encode() { return sb.toString(); } - public void decode(String value) throws Exception { + public static ReusableSession decode(String value) { String[] array = value.split(","); - if (array.length != 6) throw new RuntimeException("decode session exception"); + if (array.length != 6) return null; SessionContext context = new SessionContext(); context.osName = array[0]; context.osVersion = array[1]; @@ -33,7 +33,10 @@ public void decode(String value) throws Exception { context.deviceId = array[3]; byte[] key = AesCipher.toArray(array[4]); byte[] iv = AesCipher.toArray(array[5]); + if (key == null || iv == null) return null; context.cipher = new AesCipher(key, iv); - this.context = context; + ReusableSession session = new ReusableSession(); + session.context = context; + return session; } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index 18fd5fcb..c13defed 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -14,20 +14,14 @@ public final class ReusableSessionManager { private int expiredTime = ConfigCenter.INSTANCE.getSessionExpiredTime(); public boolean cacheSession(ReusableSession session) { - RedisManage.set(session.sessionId, session.encode(), expiredTime); + RedisManage.set(session.sessionId, ReusableSession.encode(session.context), expiredTime); return true; } public ReusableSession getSession(String sessionId) { String value = RedisManage.get(sessionId, String.class); if (Strings.isBlank(value)) return null; - ReusableSession session = new ReusableSession(); - try { - session.decode(value); - } catch (Exception e) { - return null; - } - return session; + return ReusableSession.decode(value); } public ReusableSession genSession(SessionContext context) { From c044447dbd651f578d1bb8100193f20fea136f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 9 Jan 2016 14:57:08 +0000 Subject: [PATCH 124/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....java => GatewayClientChannelHandler.java} | 22 +++++++----- .../com/shinemo/mpush/client/PushClient.java | 6 ++-- .../com/shinemo/mpush/common/ErrorCode.java | 12 ++++++- .../core/handler/FastConnectHandler.java | 10 ++++-- .../core/router/RouterChangeListener.java | 35 +++++++++++++++++-- .../core/session/ReusableSessionManager.java | 2 +- 6 files changed, 67 insertions(+), 20 deletions(-) rename mpush-client/src/main/java/com/shinemo/mpush/client/{PushClientChannelHandler.java => GatewayClientChannelHandler.java} (65%) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java b/mpush-client/src/main/java/com/shinemo/mpush/client/GatewayClientChannelHandler.java similarity index 65% rename from mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java rename to mpush-client/src/main/java/com/shinemo/mpush/client/GatewayClientChannelHandler.java index e0059710..1bbb260c 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClientChannelHandler.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/GatewayClientChannelHandler.java @@ -8,11 +8,18 @@ import com.shinemo.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.shinemo.mpush.common.ErrorCode.OFFLINE; +import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; /** * Created by ohun on 2015/12/30. */ -public class PushClientChannelHandler extends ChannelHandlerAdapter { +public class GatewayClientChannelHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); private final Connection connection = new NettyConnection(); @Override @@ -31,23 +38,22 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Packet packet = ((Packet) msg); PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); if (request == null) { + LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); return; } if (packet.cmd == Command.OK.cmd) { request.success(); - } else if (packet.cmd == Command.ERROR.cmd) { + } else { ErrorMessage message = new ErrorMessage(packet, connection); - byte errorCode = message.code; - if (errorCode == ErrorCode.OFFLINE.errorCode) { + if (message.code == OFFLINE.errorCode) { request.offline(); - } else if (errorCode == ErrorCode.PUSH_CLIENT_FAILURE.errorCode) { + } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { request.failure(); - } else if (errorCode == ErrorCode.ROUTER_CHANGE.errorCode) { + } else if (message.code == ROUTER_CHANGE.errorCode) { request.redirect(); } - } else { - + LOGGER.warn("receive an error gateway response, message={}", message); } } } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 17841ed8..23cbb093 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -5,7 +5,6 @@ import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.netty.client.NettyClient; -import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; @@ -26,7 +25,6 @@ public class PushClient implements PushSender { private int defaultTimeout = 3000; - private int port = 4000; private final Map clientMap = new ConcurrentHashMap<>(); private final Map servers = new ConcurrentHashMap<>(); @@ -44,7 +42,7 @@ public void init() throws Exception { private void createClient(final String ip, int port) { Client client = clientMap.get(ip); if (client == null) { - final Client cli = new NettyClient(ip, port, new PushClientChannelHandler()); + final Client cli = new NettyClient(ip, port, new GatewayClientChannelHandler()); ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { @@ -59,7 +57,7 @@ public void run() { public Connection getConnection(String ip) { Client client = clientMap.get(ip); if (client == null) return null; - return ((PushClientChannelHandler) client.getHandler()).getConnection(); + return ((GatewayClientChannelHandler) client.getHandler()).getConnection(); } @Override diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java index 845ebae6..ab4cc1dd 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java @@ -6,7 +6,8 @@ public enum ErrorCode { OFFLINE(1, "user offline"), PUSH_CLIENT_FAILURE(2, "push to client failure"), - ROUTER_CHANGE(3, "router change"); + ROUTER_CHANGE(3, "router change"), + UNKNOWN(-1, "unknown"); ErrorCode(int code, String errorMsg) { this.errorMsg = errorMsg; @@ -15,4 +16,13 @@ public enum ErrorCode { public final byte errorCode; public final String errorMsg; + + public static ErrorCode toEnum(int code) { + for (ErrorCode errorCode : values()) { + if (errorCode.errorCode == code) { + return errorCode; + } + } + return UNKNOWN; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 7afabdd9..86b21bb4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -6,7 +6,6 @@ import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.FastConnectMessage; import com.shinemo.mpush.common.message.FastConnectOkMessage; -import com.shinemo.mpush.core.router.RouterCenter; import com.shinemo.mpush.core.session.ReusableSession; import com.shinemo.mpush.core.session.ReusableSessionManager; import com.shinemo.mpush.tools.MPushUtil; @@ -26,22 +25,27 @@ public FastConnectMessage decode(Packet packet, Connection connection) { @Override public void handle(FastConnectMessage message) { - ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); + //从缓存中心查询session + ReusableSession session = ReusableSessionManager.INSTANCE.querySession(message.sessionId); if (session == null) { + //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { + //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); LOGGER.warn("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); } else { + //3.校验成功,重新计算心跳,完成快速重连 + int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); session.context.setHeartbeat(heartbeat); @@ -54,7 +58,7 @@ public void handle(FastConnectMessage message) { .setHeartbeat(heartbeat) .send(); - LOGGER.warn("fast connect success, session={}", message.deviceId, session.context); + LOGGER.warn("fast connect success, session={}", session.context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 80c715c3..343b5c40 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -23,7 +23,7 @@ */ public final class RouterChangeListener implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); - public static final String KICK_CHANNEL = "__kick__"; + public static final String KICK_CHANNEL = "/mpush/kick"; public RouterChangeListener() { EventBus.INSTANCE.register(this); @@ -41,6 +41,12 @@ void onRouteChangeEvent(RouterChangeEvent event) { } } + /** + * 发送踢人消息到客户端 + * + * @param userId + * @param router + */ public void kickLocal(final String userId, final LocalRouter router) { Connection connection = router.getRouteValue(); SessionContext context = connection.getSessionContext(); @@ -59,13 +65,24 @@ public void operationComplete(ChannelFuture future) throws Exception { }); } + /** + * 广播踢人消息到消息中心(redis). + *

+ * 有可能目标机器是当前机器,所以要做一次过滤 + * 如果client连续2次链接到同一台机器上就有会出现这中情况 + * + * @param userId + * @param router + */ public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); - //如果是本机直接忽略 + //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(MPushUtil.getLocalIp())) { LOGGER.error("kick remote user but router in local, userId={}", userId); return; } + + //2.发送广播 KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); msg.targetServer = location.getHost(); @@ -73,18 +90,30 @@ public void kickRemote(String userId, RemoteRouter router) { RedisManage.publish(KICK_CHANNEL, msg); } + /** + * 处理远程机器发送的踢人广播. + *

+ * 一台机器发送广播所有的机器都能收到, + * 包括发送广播的机器,所有要做一次过滤 + * + * @param msg + */ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { - //如果目标不是本机,直接忽略 + //1.如果当前机器不是目标机器,直接忽略 if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { LOGGER.error("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); return; } + + //2.查询本地路由,找到要被踢下线的链接,并删除该本地路由 String userId = msg.userId; LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); LocalRouter router = routerManager.lookup(userId); if (router != null) { LOGGER.info("receive kick remote msg, msg={}", msg); + //2.1删除本地路由信息 routerManager.unRegister(userId); + //2.2发送踢人消息到客户端 kickLocal(userId, router); } else { LOGGER.warn("no local router find, kick failure, msg={}", msg); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index c13defed..921d95a5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -18,7 +18,7 @@ public boolean cacheSession(ReusableSession session) { return true; } - public ReusableSession getSession(String sessionId) { + public ReusableSession querySession(String sessionId) { String value = RedisManage.get(sessionId, String.class); if (Strings.isBlank(value)) return null; return ReusableSession.decode(value); From 4726639e24c096d39136f179e9447cf40c011fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 9 Jan 2016 16:02:41 +0000 Subject: [PATCH 125/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/client/PushRequest.java | 25 +++++++++++-------- .../shinemo/mpush/client/PushRequestBus.java | 20 +++++++++------ .../core/handler/GatewayPushHandler.java | 3 +++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java index dea52f24..d634fdb4 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java @@ -83,9 +83,10 @@ public void onTimeout(String userId) { } private void submit(int status) { - if (this.status != 0) return; + if (this.status != 0) {//防止重复调用 + return; + } this.status = status; - if (sessionId > 0) PushRequestBus.INSTANCE.remove(sessionId); if (callback != null) { PushRequestBus.INSTANCE.getExecutor().execute(this); } else { @@ -129,36 +130,40 @@ public void failure() { public void offline() { onOffline(userId); + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); } public void send() { this.timeout_ = timeout + System.currentTimeMillis(); - sendToConnectionServer(); + sendToConnServer(); } public void redirect() { this.status = 0; this.timeout_ = timeout + System.currentTimeMillis(); ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - sendToConnectionServer(); + sendToConnServer(); LOGGER.warn("user route has changed, userId={}, content={}", userId, content); } - private void sendToConnectionServer() { + private void sendToConnServer() { + //1.查询用户长连接所在的机器 RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); if (router == null) { + //1.1没有查到说明用户已经下线 this.onOffline(userId); return; } + //2.通过网关连接,把消息发送到所在机器 ClientLocation location = router.getRouteValue(); - Connection connection = pushClient.getConnection(location.getHost()); - if (connection == null || !connection.isConnected()) { + Connection gatewayConn = pushClient.getConnection(location.getHost()); + if (gatewayConn == null || !gatewayConn.isConnected()) { this.onFailure(userId); return; } - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, connection); + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); pushMessage.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { @@ -170,7 +175,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } }); - this.sessionId = pushMessage.getSessionId(); - PushRequestBus.INSTANCE.add(this); + sessionId = pushMessage.getSessionId(); + PushRequestBus.INSTANCE.put(sessionId, this); } } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java index 330d0445..a3cb96a8 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.client; +import java.util.Iterator; import java.util.Map; import java.util.concurrent.*; @@ -13,15 +14,15 @@ public class PushRequestBus implements Runnable { private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test public PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 3, 3, TimeUnit.SECONDS); + scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); } - public void add(PushRequest request) { - requests.put(request.getSessionId(), request); + public void put(int sessionId, PushRequest request) { + requests.put(sessionId, request); } - public PushRequest remove(int reqId) { - return requests.remove(reqId); + public PushRequest remove(int sessionId) { + return requests.remove(sessionId); } public Executor getExecutor() { @@ -31,9 +32,12 @@ public Executor getExecutor() { @Override public void run() { if (requests.isEmpty()) return; - for (PushRequest callback : requests.values()) { - if (callback.isTimeout()) { - callback.timeout(); + Iterator it = requests.values().iterator(); + while (it.hasNext()) { + PushRequest request = it.next(); + if (request.isTimeout()) { + it.remove();//清除超时的请求 + request.timeout(); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index eef8d418..5dbab8c3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -90,6 +90,9 @@ public void operationComplete(ChannelFuture future) throws Exception { //3.1如果查出的远程机器是当前机器,说明本机路由已经失效,此时说明用户已经不在线 ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + //3.2出现这种情况一般是pushClient使用了本地缓存导致的数据不一致,此时应清理下缓存 + RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); + LOGGER.error("gateway push error remote is local, userId={}, router={}", message.userId, router); return; From 803343df72c0a8421831f47a2cc09f8ca123c757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 10 Jan 2016 09:30:36 +0000 Subject: [PATCH 126/890] =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=90=8E=E6=B8=85=E7=90=86=E6=9C=AC=E5=9C=B0=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/connection/Connection.java | 2 - .../mpush/api/event/ConnectionCloseEvent.java | 15 ++ .../mpush/api/event/HandshakeEvent.java | 2 +- .../mpush/api/event/KickUserEvent.java | 2 +- .../mpush/api/event/RouterChangeEvent.java | 2 +- .../core/handler/GatewayPushHandler.java | 145 +++++++++++------- .../mpush/core/router/LocalRouter.java | 2 + .../mpush/core/router/LocalRouterManager.java | 55 ++++++- .../core/router/RouterChangeListener.java | 2 + .../netty/connection/NettyConnection.java | 9 +- .../connection/NettyConnectionManager.java | 2 +- 11 files changed, 168 insertions(+), 70 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java index ca26c63d..e4a06d05 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java @@ -25,8 +25,6 @@ public interface Connection { ChannelFuture send(Packet packet, ChannelFutureListener listener); - Channel channel(); - String getId(); ChannelFuture close(); diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java new file mode 100644 index 00000000..a6d95512 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.api.event; + +import com.shinemo.mpush.api.connection.Connection; + +/** + * Created by ohun on 2016/1/10. + */ +public final class ConnectionCloseEvent implements Event { + public final Connection connection; + + + public ConnectionCloseEvent(Connection connection) { + this.connection = connection; + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java index 8839ed8c..717ad9ff 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java @@ -5,7 +5,7 @@ /** * Created by ohun on 2015/12/29. */ -public class HandshakeEvent implements Event { +public final class HandshakeEvent implements Event { public final Connection connection; public final int heartbeat; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java index 52b6eb9b..8c6091b0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java @@ -3,7 +3,7 @@ /** * Created by ohun on 2015/12/29. */ -public class KickUserEvent implements Event { +public final class KickUserEvent implements Event { public final String userId; public final String deviceId; public final String fromServer; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java index 505c1e3a..f81bd1a8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java @@ -5,7 +5,7 @@ /** * Created by ohun on 2016/1/4. */ -public class RouterChangeEvent implements Event { +public final class RouterChangeEvent implements Event { public final String userId; public final Router router; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index 5dbab8c3..628c26b0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -2,13 +2,13 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.api.router.Router; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.common.message.PushMessage; import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.core.router.LocalRouter; import com.shinemo.mpush.core.router.RouterCenter; import com.shinemo.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; @@ -16,7 +16,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static com.shinemo.mpush.api.router.Router.RouterType.LOCAL; import static com.shinemo.mpush.common.ErrorCode.*; /** @@ -30,78 +29,116 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { return new GatewayPushMessage(packet, connection); } + /** + * 处理PushClient发送过来的Push推送请求 + *

+ * 查推送策略,先查本地路由,本地不存在,查远程,(注意:有可能远程也是本机) + *

+ * 正常情况本地路由应该存在,如果不存在或链接失效,有以下几种情况: + *

+ * 1.客户端重连,并且链接到了其他机器 + * 2.客户端下线,本地路由失效,远程路由还未清除 + * 3.PushClient使用了本地缓存,但缓存数据已经和实际情况不一致了 + *

+ * 对于三种情况的处理方式是, 再检查下远程路由: + * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 + * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 + *

+ * + * @param message + */ @Override - public void handle(final GatewayPushMessage message) { - //查询路由信息,先查本地,本地不存在,查远程,有可能远程也是本机(在本地被删除的情况下) - Router router = RouterCenter.INSTANCE.lookup(message.userId); - - if (router == null) { - - //1.路由信息不存在说明用户此时不在线 - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + public void handle(GatewayPushMessage message) { + if (!checkLocal(message)) { + checkRemote(message); + } + } - LOGGER.warn("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); - } else if (router.getRouteType() == LOCAL) { + /** + * 检查本地路由,如果存在并且链接可用直接推送 + * 否则要检查下远程路由 + * + * @param message + * @return + */ + private boolean checkLocal(final GatewayPushMessage message) { + LocalRouter router = RouterCenter.INSTANCE.getLocalRouterManager().lookup(message.userId); - //2.如果是本地路由信息,说明用户链接在当前机器,如果链接可用,直接把消息下发到客户端 - Connection connection = (Connection) router.getRouteValue(); + //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 + if (router == null) return false; - if (!connection.isConnected()) { + Connection connection = router.getRouteValue(); - //2.1如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 - LOGGER.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 + if (!connection.isConnected()) { - //2.2删除已经失效的本地路由,防止递归死循环 - RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); + LOGGER.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); - //2.3递归再试一次,看用户是否登陆在远程其他机器 - this.handle(message); + //删除已经失效的本地路由 + RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); - return; - } + return false; + } - //2.4下发消息到手机客户端 - PushMessage pushMessage = new PushMessage(message.content, connection); + //3.链接可用,直接下发消息到手机客户端 + PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - //推送成功 - OkMessage.from(message).setData(message.userId).send(); + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + //推送成功 + OkMessage.from(message).setData(message.userId).send(); - LOGGER.info("gateway push message to client success userId={}, content={}", message.userId, message.content); - } else { - //推送失败 - ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); + LOGGER.info("gateway push message to client success userId={}, content={}", message.userId, message.content); + } else { + //推送失败 + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); - LOGGER.error("gateway push message to client failure userId={}, content={}", message.userId, message.content); - } + LOGGER.error("gateway push message to client failure userId={}, content={}", message.userId, message.content); } - }); + } + }); + return true; + } - LOGGER.info("gateway push, router in local userId={}, connection={}", message.userId, connection); - } else { + /** + * 检测远程路由, + * 如果不存在直接返回用户已经下线 + * 如果是本机直接删除路由信息 + * 如果是其他机器让PushClient重推 + * + * @param message + */ + private void checkRemote(GatewayPushMessage message) { + RemoteRouter router = RouterCenter.INSTANCE.getRemoteRouterManager().lookup(message.userId); + + // 1.如果远程路由信息也不存在, 说明用户此时不在线, + if (router == null) { - //3.如果是远程路由,说明此时用户已经跑到另一台机器上了 - ClientLocation location = (ClientLocation) router.getRouteValue(); + ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + + LOGGER.warn("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); - if (MPushUtil.getLocalIp().equals(location.getHost())) { - //3.1如果查出的远程机器是当前机器,说明本机路由已经失效,此时说明用户已经不在线 - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + return; + } - //3.2出现这种情况一般是pushClient使用了本地缓存导致的数据不一致,此时应清理下缓存 - RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); + //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 + if (MPushUtil.getLocalIp().equals(router.getRouteValue().getHost())) { - LOGGER.error("gateway push error remote is local, userId={}, router={}", message.userId, router); + ErrorMessage.from(message).setErrorCode(OFFLINE).send(); - return; - } + //删除失效的远程缓存 + RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); - //3.2返回给推送服务,路由信息发生更改 - ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + LOGGER.error("gateway push error remote is local, userId={}, router={}", message.userId, router); - LOGGER.info("gateway push, router in remote userId={}, router={}", message.userId, router); + return; } + + //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 + ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + + LOGGER.info("gateway push, router in remote userId={}, router={}", message.userId, router); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java index 60affbc8..f63e6862 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java @@ -1,7 +1,9 @@ package com.shinemo.mpush.core.router; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.router.Router; +import com.shinemo.mpush.netty.connection.NettyConnectionManager; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 1690c379..44c69f19 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -1,36 +1,81 @@ package com.shinemo.mpush.core.router; +import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.router.RouterManager; +import com.shinemo.mpush.common.EventBus; +import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * Created by ohun on 2015/12/23. */ public final class LocalRouterManager implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); - private final Map routerMap = new ConcurrentHashMap<>(); + + /** + * 本地路由表 + */ + private final Map routers = new ConcurrentHashMapV8<>(); + + /** + * 反向关系表 + */ + private final Map connIdUserIds = new ConcurrentHashMapV8<>(); + + public LocalRouterManager() { + EventBus.INSTANCE.register(this); + } @Override public LocalRouter register(String userId, LocalRouter router) { LOGGER.debug("register local router success userId={}, router={}", userId, router); - return routerMap.put(userId, router); + connIdUserIds.put(router.getRouteValue().getId(), userId); + return routers.put(userId, router); } @Override public boolean unRegister(String userId) { - LocalRouter router = routerMap.remove(userId); + LocalRouter router = routers.remove(userId); + if (router != null) { + connIdUserIds.remove(router.getRouteValue().getId()); + } LOGGER.info("unRegister local router success userId={}, router={}", userId, router); return true; } @Override public LocalRouter lookup(String userId) { - LocalRouter router = routerMap.get(userId); + LocalRouter router = routers.get(userId); LOGGER.debug("lookup local router userId={}, router={}", userId, router); return router; } + + /** + * 监听链接关闭事件,清理失效的路由 + * + * @param event + */ + @Subscribe + void onConnectionCloseEvent(ConnectionCloseEvent event) { + String id = event.connection.getId(); + + //1.清除反向关系 + String userId = connIdUserIds.remove(id); + if (userId == null) return; + + LocalRouter router = routers.get(userId); + if (router == null) return; + + //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 + if (id.equals(router.getRouteValue().getId())) { + + //3.删除路由 + routers.remove(userId); + LOGGER.warn("clean disconnected local route, userId={}, route={}", userId, router); + } + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 343b5c40..d3531be4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -39,6 +39,8 @@ void onRouteChangeEvent(RouterChangeEvent event) { } else { kickRemote(userId, (RemoteRouter) r); } + + // TODO: 2016/1/10 publish remoter change event to redis } /** diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index ea0d3667..5501d534 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -2,7 +2,9 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.security.CipherBox; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; @@ -68,14 +70,11 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { } } - @Override - public Channel channel() { - return channel; - } - @Override public ChannelFuture close() { + if (status == STATUS_DISCONNECTED) return null; this.status = STATUS_DISCONNECTED; + EventBus.INSTANCE.post(new ConnectionCloseEvent(this)); return this.channel.close(); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 94ca0050..744bfa34 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -57,7 +57,7 @@ public void remove(Channel channel) { } @Subscribe - public void onHandshakeOk(HandshakeEvent event) { + void onHandshakeOk(HandshakeEvent event) { HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); task.startTimeout(); } From b489b0a2a06d4f27e016103259ec0f9881439177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 10 Jan 2016 09:38:10 +0000 Subject: [PATCH 127/890] =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=90=8E=E6=B8=85=E7=90=86=E6=9C=AC=E5=9C=B0=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 0dbe6bcc..34f4ca7b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -41,12 +41,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LOGGER.error("caught an ex, client={}", ctx.channel(), cause); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.warn("a client connect client={}", ctx.channel()); + LOGGER.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -54,7 +54,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LOGGER.warn("a client disconnect client={}", ctx.channel()); + LOGGER.info("client disconnect channel={}", ctx.channel()); connectionManager.remove(ctx.channel()); } } \ No newline at end of file From 0952cfe377c7d5be682002cec0ce84d4859d1ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 10 Jan 2016 09:53:27 +0000 Subject: [PATCH 128/890] =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=90=8E=E6=B8=85=E7=90=86=E6=9C=AC=E5=9C=B0=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/server/ConnectionServer.java | 2 +- .../com/shinemo/mpush/core/server/GatewayServer.java | 2 +- .../shinemo/mpush/core/server/ServerChannelHandler.java | 9 +++++++-- .../shinemo/mpush/netty/connection/NettyConnection.java | 6 ++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 7b99277b..241f4ebd 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -31,7 +31,7 @@ public void init() { receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); connectionManager.init(); - channelHandler = new ServerChannelHandler(connectionManager, receiver); + channelHandler = new ServerChannelHandler(true, connectionManager, receiver); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java index 2b87255a..70df955d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -23,7 +23,7 @@ public void init() { MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); - channelHandler = new ServerChannelHandler(connectionManager, receiver); + channelHandler = new ServerChannelHandler(false, connectionManager, receiver); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 34f4ca7b..048469f8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -5,6 +5,7 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandler; @@ -22,11 +23,15 @@ public final class ServerChannelHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); + /** + * 是否启用加密 + */ + private final boolean security; private final ConnectionManager connectionManager; private final PacketReceiver receiver; - private boolean security = true; - public ServerChannelHandler(ConnectionManager connectionManager, PacketReceiver receiver) { + public ServerChannelHandler(boolean security, ConnectionManager connectionManager, PacketReceiver receiver) { + this.security = security; this.connectionManager = connectionManager; this.receiver = receiver; } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 5501d534..ca2f23de 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -20,7 +20,6 @@ public final class NettyConnection implements Connection, ChannelFutureListener private SessionContext context; private Channel channel; - private boolean security; private volatile int status = STATUS_NEW; private long lastReadTime; private long lastWriteTime; @@ -28,13 +27,12 @@ public final class NettyConnection implements Connection, ChannelFutureListener @Override public void init(Channel channel, boolean security) { this.channel = channel; - this.security = security; this.context = new SessionContext(); + this.lastReadTime = System.currentTimeMillis(); + this.status = STATUS_CONNECTED; if (security) { this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); } - this.lastReadTime = System.currentTimeMillis(); - this.status = STATUS_CONNECTED; } @Override From 1e80fd9e4e5ebb3ac2e8bdd5accf4d12585d1ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 11 Jan 2016 03:28:52 +0000 Subject: [PATCH 129/890] =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=90=8E=E6=B8=85=E7=90=86=E6=9C=AC=E5=9C=B0=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/client/PushRequest.java | 4 ++-- .../mpush/core/router/RouterChangeListener.java | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java index d634fdb4..9a64ebb6 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java @@ -17,10 +17,10 @@ public class PushRequest implements PushSender.Callback, Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); private PushSender.Callback callback; + private PushClient pushClient; private String userId; private String content; private long timeout; - private PushClient pushClient; private int status = 0; private long timeout_; private int sessionId; @@ -129,8 +129,8 @@ public void failure() { } public void offline() { - onOffline(userId); ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + onOffline(userId); } public void send() { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index d3531be4..87134f05 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -23,11 +23,16 @@ */ public final class RouterChangeListener implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); - public static final String KICK_CHANNEL = "/mpush/kick"; + public static final String KICK_CHANNEL_ = "/mpush/kick/"; + private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); public RouterChangeListener() { EventBus.INSTANCE.register(this); - RedisManage.subscribe(this, KICK_CHANNEL); + RedisManage.subscribe(this, getKickChannel()); + } + + public String getKickChannel() { + return kick_channel; } @Subscribe @@ -89,7 +94,7 @@ public void kickRemote(String userId, RemoteRouter router) { msg.deviceId = location.getDeviceId(); msg.targetServer = location.getHost(); msg.userId = userId; - RedisManage.publish(KICK_CHANNEL, msg); + RedisManage.publish(getKickChannel(), msg); } /** @@ -124,7 +129,7 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { @Override public void onMessage(String channel, String message) { - if (KICK_CHANNEL.equals(channel)) { + if (getKickChannel().equals(channel)) { KickRemoteMsg msg = Jsons.fromJson(message, KickRemoteMsg.class); if (msg != null) { onReceiveKickRemoteMsg(msg); From a6fdfe061939e630c56220c962e4ac2ab3b1e33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 14:17:28 +0800 Subject: [PATCH 130/890] add spi --- .../java/com/shinemo/mpush/tools/spi/SPI.java | 14 ++ .../mpush/tools/spi/ServiceContainer.java | 167 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java new file mode 100644 index 00000000..79ae0b1f --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.tools.spi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface SPI { + + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java new file mode 100644 index 00000000..dcf5bec9 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -0,0 +1,167 @@ +package com.shinemo.mpush.tools.spi; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +public class ServiceContainer { + + private static final Logger log = LoggerFactory.getLogger(ServiceContainer.class); + private static final String PREFIX = "META-INF/mpush/services/"; + + private static final ConcurrentMap, Object> objectCacheMap = Maps.newConcurrentMap(); + private final static ConcurrentHashMap, Map>> clazzCacheMap = new ConcurrentHashMap, Map>>(); + private static final ConcurrentMap, ConcurrentMap> objectsCachedMap = Maps.newConcurrentMap(); + + @SuppressWarnings("unchecked") + public static T getInstance(Class clazz){ + T instance = (T)objectCacheMap.get(clazz); + if(instance == null){ + try{ + instance = ServiceLoader.load(clazz,ServiceContainer.class.getClassLoader()).iterator().next(); + objectCacheMap.putIfAbsent(clazz, instance); + return (T) objectCacheMap.get(clazz); + }catch(Throwable e){ + log.warn("can not load "+ clazz,e); + } + }else{ + if(clazz.isAssignableFrom(instance.getClass())){ + return instance; + }else{ + log.warn("[ init service error:]"+clazz); + } + } + return null; + } + + @SuppressWarnings("unchecked") + public static List getInstances(Class clazz){ + List ret = (List)objectCacheMap.get(clazz); + if(ret == null){ + try{ + ret = Lists.newArrayList(); + for(T instance:ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader())){ + ret.add(instance); + } + objectCacheMap.putIfAbsent(clazz, ret); + return (List)objectCacheMap.get(clazz); + }catch(Throwable e){ + log.warn("can not load "+ clazz,e); + } + }else{ + if(List.class.isAssignableFrom(ret.getClass())){ + return ret; + }else{ + log.warn("[ init service error:]"+clazz); + } + } + return null; + } + + @SuppressWarnings("unchecked") + public static T getInstance(Class clazz,String key){ + ConcurrentMap objMap = objectsCachedMap.get(clazz); + if(objMap == null){ + objMap = Maps.newConcurrentMap(); + objectsCachedMap.put(clazz, objMap); + } + objMap = objectsCachedMap.get(clazz); + + T obj = (T) objMap.get(key); + + if(obj != null){ + return obj; + } + + Map> clazzMap = clazzCacheMap.get(clazz); + if(clazzMap == null){ + loadFile(clazz); + } + clazzMap = clazzCacheMap.get(key); + if(clazz != null){ + try{ + Object newObj = clazz.newInstance(); + Object preObj = objMap.putIfAbsent(key, newObj); + return null == preObj?(T)newObj:(T)preObj; + }catch(Exception e){ + log.warn("[ getInstance ] error:"+clazz+","+key,e); + } + } + + return null; + + } + + private static void loadFile(Class type){ + String fileName = PREFIX + type.getName(); + Map> map = Maps.newHashMap(); + try { + Enumeration urls; + ClassLoader classLoader = ServiceContainer.class.getClassLoader(); + if (classLoader != null) { + urls = classLoader.getResources(fileName); + } else { + urls = ClassLoader.getSystemResources(fileName); + } + if (urls != null) { + while (urls.hasMoreElements()) { + java.net.URL url = urls.nextElement(); + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); + try { + String line = null; + while ((line = reader.readLine()) != null) { + final int ci = line.indexOf('#'); + if (ci >= 0) + line = line.substring(0, ci); + line = line.trim(); + if (line.length() > 0) { + try { + String name = null; + int i = line.indexOf('='); + if (i > 0) { + name = line.substring(0, i).trim(); + line = line.substring(i + 1).trim(); + } + if (line.length() > 0) { + Class clazz = Class.forName(line, false, classLoader); + if (!type.isAssignableFrom(clazz)) { + throw new IllegalStateException( + "Error when load extension class(interface: " + type + + ", class line: " + clazz.getName() + "), class " + + clazz.getName() + "is not subtype of interface."); + } + map.put(name, clazz); + } + } catch (Throwable t) { + } + } + } // end of while read lines + } finally { + reader.close(); + } + } catch (Throwable t) { + log.error("", "Exception when load extension class(interface: " + type + ", class file: " + + url + ") in " + url, t); + } + } // end of while urls + } + } catch (Throwable t) { + log.error("", "Exception when load extension class(interface: " + type + ", description file: " + + fileName + ").", t); + } + clazzCacheMap.put(type, map); + } + +} From aa3ca62e935c83729d0d1f45705d248f8066e7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 15:56:52 +0800 Subject: [PATCH 131/890] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88=E7=9A=84spi?= =?UTF-8?q?=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/spi/ServiceContainer.java | 19 ++++++++++++++----- .../shinemo/mpush/tools/spi/TestService.java | 8 ++++++++ .../mpush/tools/spi/TestServiceImpl.java | 14 ++++++++++++++ .../mpush/tools/spi/TestServiceImpl2.java | 16 ++++++++++++++++ .../com/shinemo/mpush/tools/spi/TestSpi.java | 15 +++++++++++++++ .../com.shinemo.mpush.tools.spi.TestService | 2 ++ 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java create mode 100644 mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index dcf5bec9..9eedcfe7 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -6,7 +6,6 @@ import java.util.List; import java.util.Map; import java.util.ServiceLoader; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.slf4j.Logger; @@ -20,8 +19,13 @@ public class ServiceContainer { private static final Logger log = LoggerFactory.getLogger(ServiceContainer.class); private static final String PREFIX = "META-INF/mpush/services/"; + // class -> beanInstance private static final ConcurrentMap, Object> objectCacheMap = Maps.newConcurrentMap(); - private final static ConcurrentHashMap, Map>> clazzCacheMap = new ConcurrentHashMap, Map>>(); + + // class -> ( beanId -> beanClass ) + private static final ConcurrentMap, Map>> clazzCacheMap = Maps.newConcurrentMap(); + + // class -> ( beanId -> beanInstance) private static final ConcurrentMap, ConcurrentMap> objectsCachedMap = Maps.newConcurrentMap(); @SuppressWarnings("unchecked") @@ -88,10 +92,15 @@ public static T getInstance(Class clazz,String key){ if(clazzMap == null){ loadFile(clazz); } - clazzMap = clazzCacheMap.get(key); - if(clazz != null){ + clazzMap = clazzCacheMap.get(clazz); + if (clazzMap == null) { + log.warn("[ getInstance ] clazzMap is null:"+clazz+","+key); + return null; + } + Class realClazz = clazzMap.get(key); + if(realClazz != null){ try{ - Object newObj = clazz.newInstance(); + Object newObj = realClazz.newInstance(); Object preObj = objMap.putIfAbsent(key, newObj); return null == preObj?(T)newObj:(T)preObj; }catch(Exception e){ diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java new file mode 100644 index 00000000..0b81d279 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java @@ -0,0 +1,8 @@ +package com.shinemo.mpush.tools.spi; + + +public interface TestService { + + public void sayHi(String name); + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java new file mode 100644 index 00000000..2310a043 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.tools.spi; + +public class TestServiceImpl implements TestService { + + public TestServiceImpl() { + System.out.println("1111111"); + } + + @Override + public void sayHi(String name) { + System.out.println("TestServiceImpl hi,"+name); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java new file mode 100644 index 00000000..ae9a4c32 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.tools.spi; + + +public class TestServiceImpl2 implements TestService { + + public TestServiceImpl2() { + System.out.println("2222222"); + } + + + @Override + public void sayHi(String name) { + System.out.println("TestServiceImpl2 hi,"+name); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java new file mode 100644 index 00000000..b2115a4f --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.tools.spi; + +public class TestSpi { + + public static void main(String[] args) { + + + TestService testService = ServiceContainer.getInstance(TestService.class,"test2"); + + testService.sayHi("huang"); + + } + +} + diff --git a/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService b/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService new file mode 100644 index 00000000..cdf8099c --- /dev/null +++ b/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService @@ -0,0 +1,2 @@ +test1=com.shinemo.mpush.tools.spi.TestServiceImpl +test2=com.shinemo.mpush.tools.spi.TestServiceImpl2 \ No newline at end of file From d48c76f8e063fbf50285d9ca4abe6cb1e9de24a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 16:21:16 +0800 Subject: [PATCH 132/890] add spi test --- .../java/com/shinemo/mpush/tools/spi/SPI.java | 8 +- .../mpush/tools/spi/ServiceContainer.java | 236 ++++++++---------- .../shinemo/mpush/tools/spi/TestService.java | 1 + .../com/shinemo/mpush/tools/spi/TestSpi.java | 4 +- 4 files changed, 112 insertions(+), 137 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java index 79ae0b1f..4e6d432a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java @@ -6,9 +6,9 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) +@Target({ ElementType.TYPE }) public @interface SPI { - - - + + String value() default ""; + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index 9eedcfe7..6210b2a4 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -3,174 +3,150 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Enumeration; -import java.util.List; import java.util.Map; -import java.util.ServiceLoader; import java.util.concurrent.ConcurrentMap; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; public class ServiceContainer { private static final Logger log = LoggerFactory.getLogger(ServiceContainer.class); private static final String PREFIX = "META-INF/mpush/services/"; - - // class -> beanInstance - private static final ConcurrentMap, Object> objectCacheMap = Maps.newConcurrentMap(); - + // class -> ( beanId -> beanClass ) private static final ConcurrentMap, Map>> clazzCacheMap = Maps.newConcurrentMap(); - + // class -> ( beanId -> beanInstance) private static final ConcurrentMap, ConcurrentMap> objectsCachedMap = Maps.newConcurrentMap(); - - @SuppressWarnings("unchecked") - public static T getInstance(Class clazz){ - T instance = (T)objectCacheMap.get(clazz); - if(instance == null){ - try{ - instance = ServiceLoader.load(clazz,ServiceContainer.class.getClassLoader()).iterator().next(); - objectCacheMap.putIfAbsent(clazz, instance); - return (T) objectCacheMap.get(clazz); - }catch(Throwable e){ - log.warn("can not load "+ clazz,e); - } - }else{ - if(clazz.isAssignableFrom(instance.getClass())){ - return instance; - }else{ - log.warn("[ init service error:]"+clazz); - } + + public static T getInstance(Class clazz) { + + if (clazz == null) + throw new IllegalArgumentException("type == null"); + if (!clazz.isInterface()) { + throw new IllegalArgumentException(" type(" + clazz + ") is not interface!"); } - return null; - } - - @SuppressWarnings("unchecked") - public static List getInstances(Class clazz){ - List ret = (List)objectCacheMap.get(clazz); - if(ret == null){ - try{ - ret = Lists.newArrayList(); - for(T instance:ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader())){ - ret.add(instance); - } - objectCacheMap.putIfAbsent(clazz, ret); - return (List)objectCacheMap.get(clazz); - }catch(Throwable e){ - log.warn("can not load "+ clazz,e); - } - }else{ - if(List.class.isAssignableFrom(ret.getClass())){ - return ret; - }else{ - log.warn("[ init service error:]"+clazz); - } + if (!clazz.isAnnotationPresent(SPI.class)) { + throw new IllegalArgumentException("type(" + clazz + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!"); + } + + SPI spi = clazz.getAnnotation(SPI.class); + String instanceName = spi.value(); + + if(StringUtils.isBlank(instanceName)){ + instanceName = toLowerCaseFirstOne(clazz.getName()); } - return null; + + return getInstance(clazz, instanceName); + } - + @SuppressWarnings("unchecked") - public static T getInstance(Class clazz,String key){ + public static T getInstance(Class clazz, String key) { ConcurrentMap objMap = objectsCachedMap.get(clazz); - if(objMap == null){ + if (objMap == null) { objMap = Maps.newConcurrentMap(); objectsCachedMap.put(clazz, objMap); } objMap = objectsCachedMap.get(clazz); - + T obj = (T) objMap.get(key); - - if(obj != null){ + + if (obj != null) { return obj; } - - Map> clazzMap = clazzCacheMap.get(clazz); - if(clazzMap == null){ + + Map> clazzMap = clazzCacheMap.get(clazz); + if (clazzMap == null) { loadFile(clazz); } clazzMap = clazzCacheMap.get(clazz); - if (clazzMap == null) { - log.warn("[ getInstance ] clazzMap is null:"+clazz+","+key); - return null; - } - Class realClazz = clazzMap.get(key); - if(realClazz != null){ - try{ + if (clazzMap == null) { + log.warn("[ getInstance ] clazzMap is null:" + clazz + "," + key); + throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); + } + Class realClazz = clazzMap.get(key); + if (realClazz != null) { + try { Object newObj = realClazz.newInstance(); Object preObj = objMap.putIfAbsent(key, newObj); - return null == preObj?(T)newObj:(T)preObj; - }catch(Exception e){ - log.warn("[ getInstance ] error:"+clazz+","+key,e); + return null == preObj ? (T) newObj : (T) preObj; + } catch (Exception e) { + log.warn("[ getInstance ] error:" + clazz + "," + key, e); } } - return null; - + throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); + } - - private static void loadFile(Class type){ + + private static void loadFile(Class type) { String fileName = PREFIX + type.getName(); - Map> map = Maps.newHashMap(); - try { - Enumeration urls; - ClassLoader classLoader = ServiceContainer.class.getClassLoader(); - if (classLoader != null) { - urls = classLoader.getResources(fileName); - } else { - urls = ClassLoader.getSystemResources(fileName); - } - if (urls != null) { - while (urls.hasMoreElements()) { - java.net.URL url = urls.nextElement(); - try { - BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); - try { - String line = null; - while ((line = reader.readLine()) != null) { - final int ci = line.indexOf('#'); - if (ci >= 0) - line = line.substring(0, ci); - line = line.trim(); - if (line.length() > 0) { - try { - String name = null; - int i = line.indexOf('='); - if (i > 0) { - name = line.substring(0, i).trim(); - line = line.substring(i + 1).trim(); - } - if (line.length() > 0) { - Class clazz = Class.forName(line, false, classLoader); - if (!type.isAssignableFrom(clazz)) { - throw new IllegalStateException( - "Error when load extension class(interface: " + type - + ", class line: " + clazz.getName() + "), class " - + clazz.getName() + "is not subtype of interface."); - } - map.put(name, clazz); - } - } catch (Throwable t) { - } - } - } // end of while read lines - } finally { - reader.close(); - } - } catch (Throwable t) { - log.error("", "Exception when load extension class(interface: " + type + ", class file: " - + url + ") in " + url, t); - } - } // end of while urls - } - } catch (Throwable t) { - log.error("", "Exception when load extension class(interface: " + type + ", description file: " - + fileName + ").", t); - } - clazzCacheMap.put(type, map); + Map> map = Maps.newHashMap(); + try { + Enumeration urls; + ClassLoader classLoader = ServiceContainer.class.getClassLoader(); + if (classLoader != null) { + urls = classLoader.getResources(fileName); + } else { + urls = ClassLoader.getSystemResources(fileName); + } + if (urls != null) { + while (urls.hasMoreElements()) { + java.net.URL url = urls.nextElement(); + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); + try { + String line = null; + while ((line = reader.readLine()) != null) { + final int ci = line.indexOf('#'); + if (ci >= 0) + line = line.substring(0, ci); + line = line.trim(); + if (line.length() > 0) { + try { + String name = null; + int i = line.indexOf('='); + if (i > 0) { + name = line.substring(0, i).trim(); + line = line.substring(i + 1).trim(); + } + if (line.length() > 0) { + Class clazz = Class.forName(line, false, classLoader); + if (!type.isAssignableFrom(clazz)) { + throw new IllegalStateException("Error when load extension class(interface: " + type + ", class line: " + clazz.getName() + "), class " + + clazz.getName() + "is not subtype of interface."); + } + map.put(name, clazz); + } + } catch (Throwable t) { + } + } + } // end of while read lines + } finally { + reader.close(); + } + } catch (Throwable t) { + log.error("", "Exception when load extension class(interface: " + type + ", class file: " + url + ") in " + url, t); + } + } // end of while urls + } + } catch (Throwable t) { + log.error("", "Exception when load extension class(interface: " + type + ", description file: " + fileName + ").", t); + } + clazzCacheMap.put(type, map); } + public static String toLowerCaseFirstOne(String s) + { + if(Character.isLowerCase(s.charAt(0))) + return s; + else + return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString(); + } + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java index 0b81d279..73a0700a 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.tools.spi; +@SPI("test1") public interface TestService { public void sayHi(String name); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java index b2115a4f..00038d47 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java @@ -4,9 +4,7 @@ public class TestSpi { public static void main(String[] args) { - - TestService testService = ServiceContainer.getInstance(TestService.class,"test2"); - + TestService testService = ServiceContainer.getInstance(TestService.class); testService.sayHi("huang"); } From b720cf46de1cf9747a2f493593e045add8a96e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 20:18:42 +0800 Subject: [PATCH 133/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/spi/ServiceContainer.java | 120 ++++++-- .../mpush/tools/spi/test/TestService.java | 11 + .../mpush/tools/spi/test/TestServiceImpl.java | 19 ++ .../tools/spi/test/TestServiceImpl2.java | 20 ++ .../mpush/tools/zk/KickConnection.java | 5 - .../shinemo/mpush/tools/zk/ZkRegister.java | 37 +++ .../curator/services/ZkRegisterManager.java | 285 ++++++++++++++++++ .../mpush/tools/zk/queue/BaseQueue.java | 53 ---- .../mpush/tools/zk/queue/Consumer.java | 30 -- .../mpush/tools/zk/queue/Provider.java | 37 --- .../com.shinemo.mpush.tools.spi.TestService | 2 + .../shinemo/mpush/tools/spi/TestService.java | 9 - .../mpush/tools/spi/TestServiceImpl.java | 14 - .../mpush/tools/spi/TestServiceImpl2.java | 16 - .../com/shinemo/mpush/tools/spi/TestSpi.java | 13 - 15 files changed, 467 insertions(+), 204 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestService.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java create mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index 6210b2a4..1b4a267d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -3,13 +3,17 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentMap; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; public class ServiceContainer { @@ -36,23 +40,74 @@ public static T getInstance(Class clazz) { SPI spi = clazz.getAnnotation(SPI.class); String instanceName = spi.value(); - - if(StringUtils.isBlank(instanceName)){ + + if (StringUtils.isBlank(instanceName)) { instanceName = toLowerCaseFirstOne(clazz.getName()); } - + return getInstance(clazz, instanceName); } @SuppressWarnings("unchecked") - public static T getInstance(Class clazz, String key) { + public static List getInstances(Class clazz){ ConcurrentMap objMap = objectsCachedMap.get(clazz); if (objMap == null) { objMap = Maps.newConcurrentMap(); objectsCachedMap.put(clazz, objMap); } objMap = objectsCachedMap.get(clazz); + if(!objMap.isEmpty()){ + return Lists.newArrayList((List)objMap.values()); + } + + Map> clazzMap = getClazzMap(clazz); + if (clazzMap == null) { + log.warn("[ getInstance ] getInstances is null:" + clazz); + throw new IllegalStateException("Failed getInstance class(interface: " + clazz); + } + if (!clazzMap.isEmpty()) { //防止一个实例对象被初始化多次 + synchronized (clazz) { + try { + + if(!objMap.isEmpty()){ + return Lists.newArrayList((List)objMap.values()); + } + + Iterator>> iter = clazzMap.entrySet().iterator(); + while (iter.hasNext()) { + Entry> entry = iter.next(); + String key = entry.getKey(); + Class val = entry.getValue(); + + Object newObj = val.newInstance(); + objMap.putIfAbsent(key, newObj); + } + + return Lists.newArrayList((List)objMap.values()); + } catch (Exception e) { + log.warn("[ getInstance ] error:" + clazz, e); + } + } + } + + throw new IllegalStateException("Failed getInstance class(interface: " + clazz); + + } + + @SuppressWarnings("unchecked") + public static T getInstance(Class clazz, String key) { + ConcurrentMap objMap = objectsCachedMap.get(clazz); + if (objMap == null) { + objMap = Maps.newConcurrentMap(); + synchronized (clazz) { + objMap = objectsCachedMap.get(clazz); + if(objMap==null){ + objectsCachedMap.put(clazz, objMap); + } + } + } + objMap = objectsCachedMap.get(clazz); T obj = (T) objMap.get(key); @@ -60,28 +115,40 @@ public static T getInstance(Class clazz, String key) { return obj; } - Map> clazzMap = clazzCacheMap.get(clazz); - if (clazzMap == null) { - loadFile(clazz); - } - clazzMap = clazzCacheMap.get(clazz); + Map> clazzMap = getClazzMap(clazz); if (clazzMap == null) { log.warn("[ getInstance ] clazzMap is null:" + clazz + "," + key); - throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); + throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); } Class realClazz = clazzMap.get(key); - if (realClazz != null) { - try { - Object newObj = realClazz.newInstance(); - Object preObj = objMap.putIfAbsent(key, newObj); - return null == preObj ? (T) newObj : (T) preObj; - } catch (Exception e) { - log.warn("[ getInstance ] error:" + clazz + "," + key, e); + if (realClazz != null) { // 防止一个实例对象被初始化多次 + synchronized (realClazz) { + try { + obj = (T) objMap.get(key); + if (obj != null) { + return obj;// 已经初始化完毕 + } + + Object newObj = realClazz.newInstance(); + Object preObj = objMap.putIfAbsent(key, newObj); + return null == preObj ? (T) newObj : (T) preObj; + } catch (Exception e) { + log.warn("[ getInstance ] error:" + clazz + "," + key, e); + } } } - - throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); + throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); + + } + + private static Map> getClazzMap(Class clazz) { + Map> clazzMap = clazzCacheMap.get(clazz); + if (clazzMap == null) { + loadFile(clazz); + } + clazzMap = clazzCacheMap.get(clazz); + return clazzMap; } private static void loadFile(Class type) { @@ -140,13 +207,12 @@ private static void loadFile(Class type) { } clazzCacheMap.put(type, map); } - - public static String toLowerCaseFirstOne(String s) - { - if(Character.isLowerCase(s.charAt(0))) - return s; - else - return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString(); - } + + public static String toLowerCaseFirstOne(String s) { + if (Character.isLowerCase(s.charAt(0))) + return s; + else + return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString(); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestService.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestService.java new file mode 100644 index 00000000..f5b753a8 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestService.java @@ -0,0 +1,11 @@ +package com.shinemo.mpush.tools.spi.test; + +import com.shinemo.mpush.tools.spi.SPI; + + +@SPI("test1") +public interface TestService { + + public String sayHi(String name); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java new file mode 100644 index 00000000..f69aa6bb --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java @@ -0,0 +1,19 @@ +package com.shinemo.mpush.tools.spi.test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestServiceImpl implements TestService { + + private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); + + public TestServiceImpl() { + log.warn("init"); + } + + @Override + public String sayHi(String name) { + return "TestServiceImpl1 hi,"+name; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java new file mode 100644 index 00000000..dc8be260 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.tools.spi.test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class TestServiceImpl2 implements TestService { + + private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); + + public TestServiceImpl2() { + log.warn("init"); + } + + @Override + public String sayHi(String name) { + return "TestServiceImpl2 hi,"+name; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java deleted file mode 100644 index 1d89324c..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/KickConnection.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -public class KickConnection { - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java new file mode 100644 index 00000000..ebabfb53 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.tools.zk; + +import java.util.List; + +import org.apache.curator.framework.CuratorFramework; + +import com.shinemo.mpush.tools.spi.SPI; + + +@SPI("zkRegister") +public interface ZkRegister { + + public void init(); + + public void close(); + + public void remove(String key); + + public void registerEphemeralSequential(String key); + + public void registerEphemeralSequential(String key, String value); + + public void registerEphemeral(String key, String value); + + public void update(String key, String value); + + public void registerPersist(String key, String value); + + public boolean isExisted(String key); + + public List getChildrenKeys(String key); + + public String get(String key); + + public CuratorFramework getClient(); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java new file mode 100644 index 00000000..e01800a2 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -0,0 +1,285 @@ +package com.shinemo.mpush.tools.zk.curator.services; + +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.CuratorFrameworkFactory.Builder; +import org.apache.curator.framework.api.ACLProvider; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.utils.CloseableUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.zk.ZkConfig; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.ZkUtil; + +public class ZkRegisterManager implements ZkRegister { + + private static final Logger LOGGER = LoggerFactory.getLogger(ZkUtil.class); + + private ZkConfig zkConfig; + + private CuratorFramework client; + private TreeCache cache; + + public ZkConfig getZkConfig() { + return zkConfig; + } + + @Override + public CuratorFramework getClient() { + return client; + } + + /** + * 初始化 + */ + @Override + public void init() { + zkConfig = new ZkConfig(ConfigCenter.INSTANCE.getZkServer(), ConfigCenter.INSTANCE.getZkNamespace()); + LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); + Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); + if (zkConfig.getConnectionTimeout() > 0) { + builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); + } + if (zkConfig.getSessionTimeout() > 0) { + builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); + } + if (StringUtils.isNoneBlank(zkConfig.getDigest())) { + builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { + + @Override + public List getDefaultAcl() { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + + @Override + public List getAclForPath(final String path) { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + }); + } + client = builder.build(); + client.start(); + try { + client.blockUntilConnected(); + cacheData(); + } catch (final Exception ex) { + LOGGER.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); + } + + } + + // 本地缓存 + private void cacheData() throws Exception { + cache = new TreeCache(client, zkConfig.getLocalCachePath()); + cache.start(); + } + + private void waitClose() { + try { + Thread.sleep(600); + } catch (final InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + + /** + * 关闭 + */ + @Override + public void close() { + if (null != cache) { + cache.close(); + } + waitClose(); + CloseableUtils.closeQuietly(client); + } + + /** + * 获取数据,先从本地获取,本地找不到,从远程获取 + * + * @param key + * @return + */ + @Override + public String get(final String key) { + if (null == cache) { + return null; + } + ChildData resultIncache = cache.getCurrentData(key); + if (null != resultIncache) { + return null == resultIncache.getData() ? null : new String(resultIncache.getData(), Charset.forName("UTF-8")); + } + return getFromRemote(key); + } + + /** + * 从远程获取数据 + * + * @param key + * @return + */ + public String getFromRemote(final String key) { + try { + return new String(client.getData().forPath(key), Charset.forName("UTF-8")); + } catch (final Exception ex) { + LOGGER.error("getDirectly" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); + return null; + } + } + + /** + * 获取子节点 + * + * @param key + * @return + */ + @Override + public List getChildrenKeys(final String key) { + try { + List result = client.getChildren().forPath(key); + Collections.sort(result, new Comparator() { + + @Override + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + return result; + } catch (final Exception ex) { + LOGGER.error("getChildrenKeys" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); + return Collections.emptyList(); + } + } + + /** + * 判断路径是否存在 + * + * @param key + * @return + */ + @Override + public boolean isExisted(final String key) { + try { + return null != client.checkExists().forPath(key); + } catch (final Exception ex) { + LOGGER.error("isExisted" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); + return false; + } + } + + /** + * 持久化数据 + * + * @param key + * @param value + */ + @Override + public void registerPersist(final String key, final String value) { + try { + if (!isExisted(key)) { + client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); + } else { + update(key, value); + } + } catch (final Exception ex) { + LOGGER.error("persist" + key + "," + value, ex); + } + } + + /** + * 更新数据 + * + * @param key + * @param value + */ + @Override + public void update(final String key, final String value) { + try { + client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); + } catch (final Exception ex) { + LOGGER.error("update" + key + "," + value, ex); + } + } + + /** + * 注册临时数据 + * + * @param key + * @param value + */ + @Override + public void registerEphemeral(final String key, final String value) { + try { + if (isExisted(key)) { + client.delete().deletingChildrenIfNeeded().forPath(key); + } + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); + } catch (final Exception ex) { + LOGGER.error("persistEphemeral" + key + "," + value, ex); + } + } + + /** + * 注册临时顺序数据 + * + * @param key + */ + @Override + public void registerEphemeralSequential(final String key, final String value) { + try { + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); + } catch (final Exception ex) { + LOGGER.error("persistEphemeralSequential" + key, ex); + } + } + + /** + * 注册临时顺序数据 + * + * @param key + */ + @Override + public void registerEphemeralSequential(final String key) { + try { + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); + } catch (final Exception ex) { + LOGGER.error("persistEphemeralSequential" + key, ex); + } + } + + /** + * 删除数据 + * + * @param key + */ + @Override + public void remove(final String key) { + try { + client.delete().deletingChildrenIfNeeded().forPath(key); + } catch (final Exception ex) { + LOGGER.error("remove" + key, ex); + } + } + + public TreeCache getCache() { + return cache; + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java deleted file mode 100644 index 9b2a5b83..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/BaseQueue.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.shinemo.mpush.tools.zk.queue; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.queue.QueueConsumer; -import org.apache.curator.framework.recipes.queue.QueueSerializer; -import org.apache.curator.framework.state.ConnectionState; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.zk.consumer.ConsumerCallBack; - -public class BaseQueue { - - private static final Logger log = LoggerFactory.getLogger(BaseQueue.class); - - public static QueueSerializer createQueueSerializer(final Class clazz) { - return new QueueSerializer() { - - @Override - public byte[] serialize(T item) { - return Jsons.toJson(item).getBytes(); - } - - @Override - public T deserialize(byte[] bytes) { - return Jsons.fromJson(bytes, clazz); - } - - }; - } - - public static QueueConsumer createQueueConsumer(final Class clazz,final ConsumerCallBack callBack) { - - return new QueueConsumer() { - - @Override - public void consumeMessage(T message) throws Exception { - callBack.handler(message); - log.warn("consume one message:"+message); - } - - @Override - public void stateChanged(CuratorFramework client, ConnectionState newState) { - log.warn("connection new state:"+newState.name()); - } - - }; - - } - - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java deleted file mode 100644 index 22302c25..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Consumer.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.shinemo.mpush.tools.zk.queue; - -import org.apache.curator.framework.recipes.queue.DistributedQueue; -import org.apache.curator.framework.recipes.queue.QueueBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.zk.ZkUtil; -import com.shinemo.mpush.tools.zk.consumer.ConsumerCallBack; - -public class Consumer extends BaseQueue{ - - private static final Logger log = LoggerFactory.getLogger(Consumer.class); - - private DistributedQueue queue = null; - private String path; - - public Consumer(String path,final Class clazz,final ConsumerCallBack callBack){ - QueueBuilder builder = QueueBuilder.builder(ZkUtil.instance.getClient(), - createQueueConsumer(clazz,callBack), createQueueSerializer(clazz), path); - queue = builder.buildQueue(); - this.path = path; - } - - public void start() throws Exception{ - queue.start(); - log.warn("consumer start:"+path); - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java deleted file mode 100644 index 6d5ecf7b..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/queue/Provider.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.shinemo.mpush.tools.zk.queue; - -import org.apache.curator.framework.recipes.queue.DistributedQueue; -import org.apache.curator.framework.recipes.queue.QueueBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.zk.ZkUtil; - -public class Provider extends BaseQueue{ - - private static final Logger log = LoggerFactory.getLogger(Provider.class); - - private DistributedQueue queue = null; - - private String path; - - - public Provider(String path,final Class clazz){ - - QueueBuilder builder = QueueBuilder.builder(ZkUtil.instance.getClient(), - null, createQueueSerializer(clazz), path).lockPath(path); - queue = builder.buildQueue(); - this.path = path; - } - - public void start() throws Exception{ - queue.start(); - log.warn("provider start:"+path); - } - - public void put(T item) throws Exception{ - queue.put(item); - log.warn("provider put:"+item+","+path); - } - -} diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService new file mode 100644 index 00000000..cdf8099c --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService @@ -0,0 +1,2 @@ +test1=com.shinemo.mpush.tools.spi.TestServiceImpl +test2=com.shinemo.mpush.tools.spi.TestServiceImpl2 \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java deleted file mode 100644 index 73a0700a..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestService.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.shinemo.mpush.tools.spi; - - -@SPI("test1") -public interface TestService { - - public void sayHi(String name); - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java deleted file mode 100644 index 2310a043..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.shinemo.mpush.tools.spi; - -public class TestServiceImpl implements TestService { - - public TestServiceImpl() { - System.out.println("1111111"); - } - - @Override - public void sayHi(String name) { - System.out.println("TestServiceImpl hi,"+name); - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java deleted file mode 100644 index ae9a4c32..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestServiceImpl2.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.shinemo.mpush.tools.spi; - - -public class TestServiceImpl2 implements TestService { - - public TestServiceImpl2() { - System.out.println("2222222"); - } - - - @Override - public void sayHi(String name) { - System.out.println("TestServiceImpl2 hi,"+name); - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java deleted file mode 100644 index 00038d47..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/TestSpi.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shinemo.mpush.tools.spi; - -public class TestSpi { - - public static void main(String[] args) { - - TestService testService = ServiceContainer.getInstance(TestService.class); - testService.sayHi("huang"); - - } - -} - From 11d16db610f375112dd555a19dab994622f39918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 20:22:24 +0800 Subject: [PATCH 134/890] bug fix --- .../tools/zk/consumer/ConsumerCallBack.java | 7 --- .../consumer/impl/ConsumerKickListener.java | 20 ------- .../zk/DistributedQueueProviderTest.java | 54 ------------------- 3 files changed, 81 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java deleted file mode 100644 index 8400a252..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/ConsumerCallBack.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.shinemo.mpush.tools.zk.consumer; - -public interface ConsumerCallBack { - - public void handler(T message); - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java deleted file mode 100644 index a90fb472..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/consumer/impl/ConsumerKickListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.shinemo.mpush.tools.zk.consumer.impl; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.zk.KickConnection; -import com.shinemo.mpush.tools.zk.consumer.ConsumerCallBack; - -public class ConsumerKickListener implements ConsumerCallBack{ - - private static final Logger log = LoggerFactory.getLogger(ConsumerKickListener.class); - - @Override - public void handler(KickConnection message) { - //TODO 删除本地持有的引用 - log.warn("consumer kick:"+ToStringBuilder.reflectionToString(message)); - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java deleted file mode 100644 index 8ad7a863..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueProviderTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.util.Iterator; -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.google.common.collect.Lists; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; -import com.shinemo.mpush.tools.zk.manage.ServerManage; -import com.shinemo.mpush.tools.zk.queue.Provider; - -public class DistributedQueueProviderTest { - - private ServerApp app = new ServerApp("10.1.10.64", 3000); - - private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); - - @Before - public void setup() { - manage.start(); - } - - @Test - public void test() throws Exception { - - Iterator iterator = ServerAppManage.instance.getAppList().iterator(); - - List> providers = Lists.newArrayList(); - while (iterator.hasNext()) { - ServerApp app = iterator.next(); - if (!app.getIp().equals(this.app.getIp())) { - Provider provider = new Provider<>(ZKPath.GATEWAY_SERVER.getPath(), ServerApp.class); - providers.add(provider); - provider.start(); - } - } - - for (int i = 0; i < 10; i++) { - providers.get(0).put(new ServerApp("hi" + i, 1000)); - } - - Thread.sleep(20000); - - } - - @After - public void close() { - manage.close(); - } - -} From d9f3dbd7f9ed1305a399f9a5dd90a45ce9cc2485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 20:26:14 +0800 Subject: [PATCH 135/890] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/services/com.shinemo.mpush.tools.spi.TestService | 2 -- .../mpush/services/com.shinemo.mpush.tools.spi.test.TestService | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService create mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService deleted file mode 100644 index cdf8099c..00000000 --- a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService +++ /dev/null @@ -1,2 +0,0 @@ -test1=com.shinemo.mpush.tools.spi.TestServiceImpl -test2=com.shinemo.mpush.tools.spi.TestServiceImpl2 \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService new file mode 100644 index 00000000..e085c84d --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService @@ -0,0 +1,2 @@ +test1=com.shinemo.mpush.tools.spi.test.TestServiceImpl +test2=com.shinemo.mpush.tools.spi.test.TestServiceImpl2 \ No newline at end of file From c6a39fe91ffbe5bd27699acbc6b32ab922d5a3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 12 Jan 2016 21:08:32 +0800 Subject: [PATCH 136/890] add tet --- .../shinemo/mpush/tools/spi/ServiceContainer.java | 4 ++-- .../java/com/shinemo/mpush/tools/spi/SpiTest.java | 13 +++++++++++++ .../com.shinemo.mpush.tools.spi.TestService | 2 -- .../com.shinemo.mpush.tools.spi.test.TestService | 2 ++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java delete mode 100644 mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService create mode 100644 mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index 1b4a267d..7d5fda7f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -26,7 +26,7 @@ public class ServiceContainer { // class -> ( beanId -> beanInstance) private static final ConcurrentMap, ConcurrentMap> objectsCachedMap = Maps.newConcurrentMap(); - + public static T getInstance(Class clazz) { if (clazz == null) @@ -99,10 +99,10 @@ public static List getInstances(Class clazz){ public static T getInstance(Class clazz, String key) { ConcurrentMap objMap = objectsCachedMap.get(clazz); if (objMap == null) { - objMap = Maps.newConcurrentMap(); synchronized (clazz) { objMap = objectsCachedMap.get(clazz); if(objMap==null){ + objMap = Maps.newConcurrentMap(); objectsCachedMap.put(clazz, objMap); } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java new file mode 100644 index 00000000..112fb110 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush.tools.spi; + + +import com.shinemo.mpush.tools.spi.test.TestService; + +public class SpiTest { + + public static void main(String[] args) { + TestService testService = ServiceContainer.getInstance(TestService.class); + System.out.println(testService.sayHi(" huang")); + } + +} diff --git a/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService b/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService deleted file mode 100644 index cdf8099c..00000000 --- a/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.TestService +++ /dev/null @@ -1,2 +0,0 @@ -test1=com.shinemo.mpush.tools.spi.TestServiceImpl -test2=com.shinemo.mpush.tools.spi.TestServiceImpl2 \ No newline at end of file diff --git a/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService b/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService new file mode 100644 index 00000000..e085c84d --- /dev/null +++ b/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService @@ -0,0 +1,2 @@ +test1=com.shinemo.mpush.tools.spi.test.TestServiceImpl +test2=com.shinemo.mpush.tools.spi.test.TestServiceImpl2 \ No newline at end of file From bfb8c5608136d8be573bb0e4632364af0039ec25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 13 Jan 2016 11:06:30 +0800 Subject: [PATCH 137/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20meta-inf=20?= =?UTF-8?q?=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 2 +- mpush-api/pom.xml | 16 +------------ mpush-core/pom.xml | 2 ++ mpush-tools/pom.xml | 57 +++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 9 ------- 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/assembly.xml b/assembly.xml index 4d0cf481..187cc819 100644 --- a/assembly.xml +++ b/assembly.xml @@ -33,4 +33,4 @@ lib - \ No newline at end of file + diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 89d79c1a..f57560de 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -19,19 +19,5 @@ netty-transport - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - UTF-8 - - - - + diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index eb3d75f8..dca91ff8 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -38,6 +38,7 @@ logback-ext-spring + mpush @@ -118,4 +119,5 @@ + diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 6bba15b3..55e7d8c6 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -42,4 +42,61 @@ slf4j-api + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.4.2 + + true + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-tools + + + + + META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService + + + + + + + + + diff --git a/pom.xml b/pom.xml index 84ae0d99..51dab31c 100644 --- a/pom.xml +++ b/pom.xml @@ -182,15 +182,6 @@ - - - src/main/resources - - **/*.xml - - true - - From b91eb9b6a03586531a7e22a0dc5a9f7fe7e0e7e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 13 Jan 2016 11:45:11 +0800 Subject: [PATCH 138/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/spi/ServiceContainer.java | 19 +++++++++--- .../com/shinemo/mpush/tools/spi/SpiTest.java | 31 ++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index 7d5fda7f..c8f207e9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -53,8 +53,13 @@ public static T getInstance(Class clazz) { public static List getInstances(Class clazz){ ConcurrentMap objMap = objectsCachedMap.get(clazz); if (objMap == null) { - objMap = Maps.newConcurrentMap(); - objectsCachedMap.put(clazz, objMap); + synchronized (clazz) { + objMap = objectsCachedMap.get(clazz); + if(objMap == null){ + objMap = Maps.newConcurrentMap(); + objectsCachedMap.put(clazz, objMap); + } + } } objMap = objectsCachedMap.get(clazz); if(!objMap.isEmpty()){ @@ -69,11 +74,9 @@ public static List getInstances(Class clazz){ if (!clazzMap.isEmpty()) { //防止一个实例对象被初始化多次 synchronized (clazz) { try { - if(!objMap.isEmpty()){ return Lists.newArrayList((List)objMap.values()); } - Iterator>> iter = clazzMap.entrySet().iterator(); while (iter.hasNext()) { Entry> entry = iter.next(); @@ -205,7 +208,13 @@ private static void loadFile(Class type) { } catch (Throwable t) { log.error("", "Exception when load extension class(interface: " + type + ", description file: " + fileName + ").", t); } - clazzCacheMap.put(type, map); + synchronized (type) { + Map> oldMap = clazzCacheMap.get(type); + if(oldMap==null){ + clazzCacheMap.put(type, map); + } + } + } public static String toLowerCaseFirstOne(String s) { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java index 112fb110..f4a78f8f 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java @@ -1,13 +1,42 @@ package com.shinemo.mpush.tools.spi; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.junit.Ignore; +import org.junit.Test; + import com.shinemo.mpush.tools.spi.test.TestService; + public class SpiTest { - public static void main(String[] args) { + private static Executor pool = Executors.newCachedThreadPool(); + + @Ignore + @Test + public void baseTest(){ TestService testService = ServiceContainer.getInstance(TestService.class); System.out.println(testService.sayHi(" huang")); } + + @Test + public void mulThreadTest() throws InterruptedException{ + pool.execute(new Worker()); + pool.execute(new Worker()); + Thread.sleep(Integer.MAX_VALUE); + } + + private static final class Worker implements Runnable{ + + @Override + public void run() { + TestService testService = ServiceContainer.getInstance(TestService.class); + System.out.println(testService.sayHi(" huang")); + } + + } + } From 68700f3bc88e5c570c4db65709c7d27682a9d47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 13 Jan 2016 11:47:40 +0800 Subject: [PATCH 139/890] remove test code --- .../java/com/shinemo/mpush/tools/spi/test/TestService.java | 0 .../java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java | 0 .../java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename mpush-tools/src/{main => test}/java/com/shinemo/mpush/tools/spi/test/TestService.java (100%) rename mpush-tools/src/{main => test}/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java (100%) rename mpush-tools/src/{main => test}/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java (100%) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestService.java similarity index 100% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestService.java rename to mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestService.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java similarity index 100% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java rename to mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java similarity index 100% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java rename to mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java From 4b7aa90dd1d36d1c1f92caef0ca0e33e7a8b1eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 13 Jan 2016 12:03:30 +0800 Subject: [PATCH 140/890] =?UTF-8?q?=E5=B9=B6=E5=8F=91=E6=8E=A7=E5=88=B6bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/spi/ServiceContainer.java | 11 +++++--- .../com/shinemo/mpush/tools/spi/SpiTest.java | 27 ++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index c8f207e9..2e628fd8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -2,6 +2,7 @@ import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; @@ -83,11 +84,15 @@ public static List getInstances(Class clazz){ String key = entry.getKey(); Class val = entry.getValue(); - Object newObj = val.newInstance(); - objMap.putIfAbsent(key, newObj); + Object oldObj = objMap.get(key); + if(oldObj==null){ + Object newObj = val.newInstance(); + objMap.putIfAbsent(key, newObj); + } + } - return Lists.newArrayList((List)objMap.values()); + return Lists.newArrayList((Collection)objMap.values()); } catch (Exception e) { log.warn("[ getInstance ] error:" + clazz, e); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java index f4a78f8f..3a572cfb 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java @@ -1,9 +1,11 @@ package com.shinemo.mpush.tools.spi; +import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import org.apache.commons.lang3.builder.ToStringBuilder; import org.junit.Ignore; import org.junit.Test; @@ -21,10 +23,25 @@ public void baseTest(){ System.out.println(testService.sayHi(" huang")); } + + @Test + public void listTest(){ + + List listRet = ServiceContainer.getInstances(TestService.class); + + for(TestService test:listRet){ + System.out.println(ToStringBuilder.reflectionToString(test)); + } + + } + + @Ignore @Test public void mulThreadTest() throws InterruptedException{ pool.execute(new Worker()); pool.execute(new Worker()); + pool.execute(new ListWorker()); + pool.execute(new ListWorker()); Thread.sleep(Integer.MAX_VALUE); } @@ -34,7 +51,15 @@ private static final class Worker implements Runnable{ @Override public void run() { TestService testService = ServiceContainer.getInstance(TestService.class); - System.out.println(testService.sayHi(" huang")); + System.out.println(testService.sayHi(" huang")+","+ToStringBuilder.reflectionToString(testService)); + } + + } + + private static final class ListWorker implements Runnable{ + + @Override + public void run() { } } From ce497b9f69e24840fee55551af656b4e4ac72b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 13 Jan 2016 21:22:13 +0800 Subject: [PATCH 141/890] =?UTF-8?q?add=20spi=20test=20end=E3=80=82?= =?UTF-8?q?=E6=98=8E=E5=A4=A9=E5=BC=80=E5=A7=8B=E9=9B=86=E6=88=90spi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/spi/ServiceContainer.java | 94 +++++++++---------- .../com/shinemo/mpush/tools/spi/SpiTest.java | 14 ++- 2 files changed, 53 insertions(+), 55 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index 2e628fd8..927d2d77 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -64,41 +64,15 @@ public static List getInstances(Class clazz){ } objMap = objectsCachedMap.get(clazz); if(!objMap.isEmpty()){ - return Lists.newArrayList((List)objMap.values()); + return Lists.newArrayList((Collection)objMap.values()); } - Map> clazzMap = getClazzMap(clazz); - if (clazzMap == null) { - log.warn("[ getInstance ] getInstances is null:" + clazz); - throw new IllegalStateException("Failed getInstance class(interface: " + clazz); - } - if (!clazzMap.isEmpty()) { //防止一个实例对象被初始化多次 - synchronized (clazz) { - try { - if(!objMap.isEmpty()){ - return Lists.newArrayList((List)objMap.values()); - } - Iterator>> iter = clazzMap.entrySet().iterator(); - while (iter.hasNext()) { - Entry> entry = iter.next(); - String key = entry.getKey(); - Class val = entry.getValue(); - - Object oldObj = objMap.get(key); - if(oldObj==null){ - Object newObj = val.newInstance(); - objMap.putIfAbsent(key, newObj); - } - - } - - return Lists.newArrayList((Collection)objMap.values()); - } catch (Exception e) { - log.warn("[ getInstance ] error:" + clazz, e); - } - } - } + initClazzInstances(clazz); + objMap = objectsCachedMap.get(clazz); + if(!objMap.isEmpty()){ + return Lists.newArrayList((Collection)objMap.values()); + } throw new IllegalStateException("Failed getInstance class(interface: " + clazz); } @@ -122,32 +96,48 @@ public static T getInstance(Class clazz, String key) { if (obj != null) { return obj; } + + //初始化所有 + initClazzInstances(clazz); + + obj = (T) objMap.get(key); - Map> clazzMap = getClazzMap(clazz); - if (clazzMap == null) { - log.warn("[ getInstance ] clazzMap is null:" + clazz + "," + key); - throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); + if (obj != null) { + return obj; } - Class realClazz = clazzMap.get(key); - if (realClazz != null) { // 防止一个实例对象被初始化多次 - synchronized (realClazz) { - try { - obj = (T) objMap.get(key); - if (obj != null) { - return obj;// 已经初始化完毕 - } + + throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); - Object newObj = realClazz.newInstance(); - Object preObj = objMap.putIfAbsent(key, newObj); - return null == preObj ? (T) newObj : (T) preObj; - } catch (Exception e) { - log.warn("[ getInstance ] error:" + clazz + "," + key, e); + } + + private static void initClazzInstances(Class clazz){ + + Map> clazzMap = getClazzMap(clazz); + ConcurrentMap objMap = objectsCachedMap.get(clazz); + if(objMap.isEmpty()){ + synchronized (clazz) { + objMap = objectsCachedMap.get(clazz); + if(objMap.isEmpty()){ + Iterator>> iter = clazzMap.entrySet().iterator(); + while (iter.hasNext()) { + Entry> entry = iter.next(); + String entryKey = entry.getKey(); + Class val = entry.getValue(); + Object oldObj = objMap.get(entryKey); + if(oldObj==null){ + Object newObj; + try { + newObj = val.newInstance(); + objMap.putIfAbsent(entryKey, newObj); + } catch (Exception e) { + } + } + } + objectsCachedMap.put(clazz, objMap); } } } - - throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); - + } private static Map> getClazzMap(Class clazz) { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java index 3a572cfb..355377f4 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java @@ -16,21 +16,22 @@ public class SpiTest { private static Executor pool = Executors.newCachedThreadPool(); - @Ignore @Test public void baseTest(){ TestService testService = ServiceContainer.getInstance(TestService.class); System.out.println(testService.sayHi(" huang")); + + ServiceContainer.getInstance(TestService.class,"test2"); } - + @Ignore @Test public void listTest(){ List listRet = ServiceContainer.getInstances(TestService.class); for(TestService test:listRet){ - System.out.println(ToStringBuilder.reflectionToString(test)); + System.out.println(ToStringBuilder.reflectionToString(test.sayHi(" huang list"))); } } @@ -60,6 +61,13 @@ private static final class ListWorker implements Runnable{ @Override public void run() { + + List listRet = ServiceContainer.getInstances(TestService.class); + + for(TestService test:listRet){ + System.out.println(test.sayHi(" huang list")+","+Thread.currentThread().getId()+","+ToStringBuilder.reflectionToString(test)); + } + } } From 6b3248d7c631a604c47817f0aa51ee1465964ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 14 Jan 2016 17:53:16 +0800 Subject: [PATCH 142/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-tools/pom.xml | 6 ++++++ .../shinemo/mpush/tools/owner/OwnerTest.java | 20 ++++++++++++++++++ .../mpush/tools/owner/ServerConfig.java | 21 +++++++++++++++++++ .../src/test/resources/config.properties | 18 +++++++++------- .../test/resources/serverconfig.properties | 10 +++++++++ pom.xml | 7 +++++++ 6 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java create mode 100644 mpush-tools/src/test/resources/serverconfig.properties diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 55e7d8c6..883f5d2f 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -41,6 +41,12 @@ org.slf4j slf4j-api + + + org.aeonbits.owner + owner + + diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java new file mode 100644 index 00000000..ce7d67c7 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.tools.owner; + +import org.aeonbits.owner.ConfigFactory; + +public class OwnerTest { + + public static void main(String[] args) { + + ServerConfig cfg = ConfigFactory.create(ServerConfig.class); + + System.out.println(cfg.zkDigest()); + + System.out.println(cfg.zkIp()); + + System.out.println(cfg.hello()); + + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java new file mode 100644 index 00000000..ac9879c5 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.tools.owner; + +import org.aeonbits.owner.Config; +import org.aeonbits.owner.Config.Sources; + + +@Sources({"classpath:serverconfig.properties"}) +public interface ServerConfig extends Config{ + + @Key("zk_ip") + public String zkIp(); + + @Key("zk_digest") + public String zkDigest(); + + @Key("hello") + @DefaultValue("hello world") + public String hello(); + + +} diff --git a/mpush-tools/src/test/resources/config.properties b/mpush-tools/src/test/resources/config.properties index ef2b80aa..e8c461e9 100644 --- a/mpush-tools/src/test/resources/config.properties +++ b/mpush-tools/src/test/resources/config.properties @@ -1,8 +1,10 @@ -ZK_SERVER=10.1.20.74:2181 -MAX_PACKET_SIZE=10240 -COMPRESS_LIMIT=10240 -MIN_HEARTBEAT=10000 -MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 -PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file +zk_ip=10.1.20.74:2181 +zk_namespace=mpush +zk_digest=shinemoIpo +max_packet_size=10240 +compress_limit=10240 +min_heartbeat=10000 +max_heartbeat=1800000 +max_hb_timeout_times=2 +private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-tools/src/test/resources/serverconfig.properties b/mpush-tools/src/test/resources/serverconfig.properties new file mode 100644 index 00000000..e8c461e9 --- /dev/null +++ b/mpush-tools/src/test/resources/serverconfig.properties @@ -0,0 +1,10 @@ +zk_ip=10.1.20.74:2181 +zk_namespace=mpush +zk_digest=shinemoIpo +max_packet_size=10240 +compress_limit=10240 +min_heartbeat=10000 +max_heartbeat=1800000 +max_hb_timeout_times=2 +private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/pom.xml b/pom.xml index 51dab31c..5755f170 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,13 @@ gson 2.5 + + + org.aeonbits.owner + owner + 1.0.9 + + From fc66537b2cc8bfb5cb38065363640b6259533d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 14 Jan 2016 19:49:41 +0800 Subject: [PATCH 143/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/test/ConfigCenterTest.java | 48 ++++++++++++ .../src/main/resources/config.properties | 47 +++++++++-- .../com/shinemo/mpush/tools/ConfigCenter.java | 1 + .../mpush/tools/config/ConfigCenter.java | 77 +++++++++++++++++++ .../shinemo/mpush/tools/owner/OwnerTest.java | 50 +++++++++++- .../mpush/tools/owner/ServerConfig.java | 9 +++ .../src/test/resources/config.properties | 49 +++++++++--- 7 files changed, 262 insertions(+), 19 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java new file mode 100644 index 00000000..9adcd724 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java @@ -0,0 +1,48 @@ +package com.shinemo.mpush.core.test; + +import org.aeonbits.owner.ConfigFactory; + +import com.shinemo.mpush.tools.config.ConfigCenter; + +public class ConfigCenterTest { + + public static void main(String[] args) { + + ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); + + System.out.println(holder.zkIp()); + + System.out.println("aesKeyLength:"+ConfigCenter.holder.aesKeyLength()); + + System.out.println("compressLimit:"+ConfigCenter.holder.compressLimit()); + + System.out.println("connectionServerPort:"+ConfigCenter.holder.connectionServerPort()); + + System.out.println("gatewayServerPort:"+ConfigCenter.holder.gatewayServerPort()); + + System.out.println("maxHBTimeoutTimes:"+ConfigCenter.holder.maxHBTimeoutTimes()); + + System.out.println(ConfigCenter.holder.maxHeartbeat()); + + System.out.println(ConfigCenter.holder.maxPacketSize()); + + System.out.println(ConfigCenter.holder.minHeartbeat()); + + System.out.println(ConfigCenter.holder.privateKey()); + + System.out.println(ConfigCenter.holder.publicKey()); + + System.out.println(ConfigCenter.holder.rasKeyLength()); + + System.out.println(ConfigCenter.holder.redisIp()); + + System.out.println(ConfigCenter.holder.sessionExpiredTime()); + + System.out.println(ConfigCenter.holder.zkDigest()); + + System.out.println(ConfigCenter.holder.zkIp()); + + System.out.println(ConfigCenter.holder.zkNamespace()); + } + +} diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties index ef2b80aa..059d0db8 100644 --- a/mpush-core/src/main/resources/config.properties +++ b/mpush-core/src/main/resources/config.properties @@ -1,8 +1,39 @@ -ZK_SERVER=10.1.20.74:2181 -MAX_PACKET_SIZE=10240 -COMPRESS_LIMIT=10240 -MIN_HEARTBEAT=10000 -MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 -PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file +## +max_packet_size = 10240 +## +compress_limit = 10240 +## +min_heartbeat = 10000 +## +max_heartbeat = 1800000 +## +max_hb_timeout_times = 2 +## +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +## +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +## +gateway_server_port = 4000 +## +connection_server_port = 3000 +## +aes_key_length = 16 +## +ras_key_length = 1024 +## +session_expired_time = 86400 +## +max_hb_timeout_times = 2 +## +max_heartbeat = 1800000 +## +min_heartbeat = 10000 +## +compress_limit = 10240 +## +max_packet_size = 10240 + +## +zk_ip = 10.1.20.74:2181 +zk_namespace = mpush2 +zk_digest = shinemoIpo2 \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java index 374d2bf3..5a5c7de3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java @@ -124,4 +124,5 @@ public String getZkServer() { public String getZkNamespace() { return zkNamespace; } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java new file mode 100644 index 00000000..e0e3e69d --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -0,0 +1,77 @@ +package com.shinemo.mpush.tools.config; + + +import org.aeonbits.owner.Config; +import org.aeonbits.owner.ConfigFactory; +import org.aeonbits.owner.Config.Sources; + +@Sources({"classpath:config.properties"}) +public interface ConfigCenter extends Config{ + + public static ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); + + @Key("max_packet_size") + @DefaultValue("10240") + public int maxPacketSize(); + + @Key("compress_limit") + @DefaultValue("10240") + public int compressLimit(); + + @Key("min_heartbeat") + @DefaultValue("10000") //10s + public int minHeartbeat(); + + @Key("max_heartbeat") + @DefaultValue("1800000") //30min + public int maxHeartbeat(); + + @Key("max_hb_timeout_times") + @DefaultValue("2") + public int maxHBTimeoutTimes(); + + @Key("session_expired_time") + @DefaultValue("86400") //unit second + public int sessionExpiredTime(); + + @Key("ras_key_length") + @DefaultValue("1024") + public int rasKeyLength(); + + @Key("aes_key_length") + @DefaultValue("16") + public int aesKeyLength(); + + @Key("connection_server_port") + @DefaultValue("3000") + public int connectionServerPort(); + + @Key("gateway_server_port") + @DefaultValue("4000") + public int gatewayServerPort(); + + @Key("private_key") + @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") + public String privateKey(); + + @Key("public_key") + @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") + public String publicKey(); + + @Key("redis_ip") + @DefaultValue("127.0.0.1:6379:ShineMoIpo") + public String redisIp(); + + @Key("zk_ip") + @DefaultValue("127.0.0.1:2181") + public String zkIp(); + + @Key("zk_namespace") + @DefaultValue("mpush") + public String zkNamespace(); + + @Key("zk_digest") + @DefaultValue("shinemoIpo") + public String zkDigest(); + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java index ce7d67c7..559fe22d 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java @@ -1,10 +1,14 @@ package com.shinemo.mpush.tools.owner; import org.aeonbits.owner.ConfigFactory; +import org.junit.Test; + +import com.shinemo.mpush.tools.config.ConfigCenter; public class OwnerTest { - public static void main(String[] args) { + @Test + public void test1(){ ServerConfig cfg = ConfigFactory.create(ServerConfig.class); @@ -14,7 +18,51 @@ public static void main(String[] args) { System.out.println(cfg.hello()); + System.out.println(cfg.maxHbTimeoutTimes()); + + System.out.println(cfg.test()); + + Integer tset = cfg.testnotexist(); + if(tset == null){ + System.out.println("not exist"); + }else{ + System.out.println(tset); + } + } + + @Test + public void test2(){ + System.out.println("aesKeyLength:"+ConfigCenter.holder.aesKeyLength()); + + System.out.println("compressLimit:"+ConfigCenter.holder.compressLimit()); + + System.out.println("connectionServerPort:"+ConfigCenter.holder.connectionServerPort()); + + System.out.println("gatewayServerPort:"+ConfigCenter.holder.gatewayServerPort()); + + System.out.println("maxHBTimeoutTimes:"+ConfigCenter.holder.maxHBTimeoutTimes()); + + System.out.println(ConfigCenter.holder.maxHeartbeat()); + + System.out.println(ConfigCenter.holder.maxPacketSize()); + + System.out.println(ConfigCenter.holder.minHeartbeat()); + + System.out.println(ConfigCenter.holder.privateKey()); + + System.out.println(ConfigCenter.holder.publicKey()); + + System.out.println(ConfigCenter.holder.rasKeyLength()); + + System.out.println(ConfigCenter.holder.redisIp()); + + System.out.println(ConfigCenter.holder.sessionExpiredTime()); + + System.out.println(ConfigCenter.holder.zkDigest()); + + System.out.println(ConfigCenter.holder.zkIp()); + System.out.println(ConfigCenter.holder.zkNamespace()); } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java index ac9879c5..08dd8d97 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java @@ -8,6 +8,7 @@ public interface ServerConfig extends Config{ @Key("zk_ip") + @DefaultValue("zkIp") public String zkIp(); @Key("zk_digest") @@ -17,5 +18,13 @@ public interface ServerConfig extends Config{ @DefaultValue("hello world") public String hello(); + @Key("max_hb_timeout_times") + public int maxHbTimeoutTimes(); + + @Key("test") + @DefaultValue("10") + public int test(); + + public Integer testnotexist(); } diff --git a/mpush-tools/src/test/resources/config.properties b/mpush-tools/src/test/resources/config.properties index e8c461e9..059d0db8 100644 --- a/mpush-tools/src/test/resources/config.properties +++ b/mpush-tools/src/test/resources/config.properties @@ -1,10 +1,39 @@ -zk_ip=10.1.20.74:2181 -zk_namespace=mpush -zk_digest=shinemoIpo -max_packet_size=10240 -compress_limit=10240 -min_heartbeat=10000 -max_heartbeat=1800000 -max_hb_timeout_times=2 -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file +## +max_packet_size = 10240 +## +compress_limit = 10240 +## +min_heartbeat = 10000 +## +max_heartbeat = 1800000 +## +max_hb_timeout_times = 2 +## +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +## +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +## +gateway_server_port = 4000 +## +connection_server_port = 3000 +## +aes_key_length = 16 +## +ras_key_length = 1024 +## +session_expired_time = 86400 +## +max_hb_timeout_times = 2 +## +max_heartbeat = 1800000 +## +min_heartbeat = 10000 +## +compress_limit = 10240 +## +max_packet_size = 10240 + +## +zk_ip = 10.1.20.74:2181 +zk_namespace = mpush2 +zk_digest = shinemoIpo2 \ No newline at end of file From 2be81ddea77928a0acb39e1cca8b5e5ba2174537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 14 Jan 2016 20:41:52 +0800 Subject: [PATCH 144/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E4=BD=BF=E7=94=A8owner=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/test/ConfigCenterTest.java | 48 ------------------- .../src/test/resources/config.properties | 8 ---- .../shinemo/mpush/tools/owner/OwnerTest.java | 5 +- .../src/test/resources/config.properties | 39 --------------- 4 files changed, 4 insertions(+), 96 deletions(-) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java delete mode 100644 mpush-core/src/test/resources/config.properties delete mode 100644 mpush-tools/src/test/resources/config.properties diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java b/mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java deleted file mode 100644 index 9adcd724..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/test/ConfigCenterTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.shinemo.mpush.core.test; - -import org.aeonbits.owner.ConfigFactory; - -import com.shinemo.mpush.tools.config.ConfigCenter; - -public class ConfigCenterTest { - - public static void main(String[] args) { - - ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); - - System.out.println(holder.zkIp()); - - System.out.println("aesKeyLength:"+ConfigCenter.holder.aesKeyLength()); - - System.out.println("compressLimit:"+ConfigCenter.holder.compressLimit()); - - System.out.println("connectionServerPort:"+ConfigCenter.holder.connectionServerPort()); - - System.out.println("gatewayServerPort:"+ConfigCenter.holder.gatewayServerPort()); - - System.out.println("maxHBTimeoutTimes:"+ConfigCenter.holder.maxHBTimeoutTimes()); - - System.out.println(ConfigCenter.holder.maxHeartbeat()); - - System.out.println(ConfigCenter.holder.maxPacketSize()); - - System.out.println(ConfigCenter.holder.minHeartbeat()); - - System.out.println(ConfigCenter.holder.privateKey()); - - System.out.println(ConfigCenter.holder.publicKey()); - - System.out.println(ConfigCenter.holder.rasKeyLength()); - - System.out.println(ConfigCenter.holder.redisIp()); - - System.out.println(ConfigCenter.holder.sessionExpiredTime()); - - System.out.println(ConfigCenter.holder.zkDigest()); - - System.out.println(ConfigCenter.holder.zkIp()); - - System.out.println(ConfigCenter.holder.zkNamespace()); - } - -} diff --git a/mpush-core/src/test/resources/config.properties b/mpush-core/src/test/resources/config.properties deleted file mode 100644 index ef2b80aa..00000000 --- a/mpush-core/src/test/resources/config.properties +++ /dev/null @@ -1,8 +0,0 @@ -ZK_SERVER=10.1.20.74:2181 -MAX_PACKET_SIZE=10240 -COMPRESS_LIMIT=10240 -MIN_HEARTBEAT=10000 -MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 -PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java index 559fe22d..8823914b 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java @@ -32,6 +32,9 @@ public void test1(){ @Test public void test2(){ + + System.out.println(ConfigCenter.holder.zkIp()); + System.out.println("aesKeyLength:"+ConfigCenter.holder.aesKeyLength()); System.out.println("compressLimit:"+ConfigCenter.holder.compressLimit()); @@ -60,7 +63,7 @@ public void test2(){ System.out.println(ConfigCenter.holder.zkDigest()); - System.out.println(ConfigCenter.holder.zkIp()); + System.out.println(ConfigCenter.holder.zkNamespace()); } diff --git a/mpush-tools/src/test/resources/config.properties b/mpush-tools/src/test/resources/config.properties deleted file mode 100644 index 059d0db8..00000000 --- a/mpush-tools/src/test/resources/config.properties +++ /dev/null @@ -1,39 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 1800000 -## -max_hb_timeout_times = 2 -## -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port = 4000 -## -connection_server_port = 3000 -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 -## -max_hb_timeout_times = 2 -## -max_heartbeat = 1800000 -## -min_heartbeat = 10000 -## -compress_limit = 10240 -## -max_packet_size = 10240 - -## -zk_ip = 10.1.20.74:2181 -zk_namespace = mpush2 -zk_digest = shinemoIpo2 \ No newline at end of file From c2c1fc3e5022f7bd3fc41a8d869ced36e09e4b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 09:31:04 +0800 Subject: [PATCH 145/890] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84con?= =?UTF-8?q?fig=20center?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/client/PushClient.java | 2 - .../mpush/common/message/BaseMessage.java | 5 +- .../mpush/common/security/CipherBox.java | 8 +- .../main/java/com/shinemo/mpush/core/App.java | 8 +- .../core/session/ReusableSessionManager.java | 4 +- .../mpush/core/netty/NettyClientTest.java | 2 - .../mpush/core/netty/NettyServerTest.java | 2 - .../mpush/netty/codec/PacketDecoder.java | 5 +- .../connection/NettyConnectionManager.java | 9 +- .../com/shinemo/mpush/tools/ConfigCenter.java | 128 ------------------ .../com/shinemo/mpush/tools/MPushUtil.java | 6 +- .../com/shinemo/mpush/tools/zk/ZkUtil.java | 5 +- .../curator/services/ZkRegisterManager.java | 4 +- .../shinemo/mpush/tools/zk/ZkUtilTest.java | 3 - 14 files changed, 30 insertions(+), 161 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 23cbb093..83d55da3 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -5,7 +5,6 @@ import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.netty.client.NettyClient; -import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.ZKPath; @@ -29,7 +28,6 @@ public class PushClient implements PushSender { private final Map servers = new ConcurrentHashMap<>(); public void init() throws Exception { - ConfigCenter.INSTANCE.init(); initRedisClient(); GatewayServerZKListener listener = new GatewayServerZKListener(); Collection nodes = listener.getAllServers(); diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 8b8c8829..8d151aaf 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -5,8 +5,9 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.IOUtils; +import com.shinemo.mpush.tools.config.ConfigCenter; + import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; @@ -52,7 +53,7 @@ protected void encodeBody() { byte[] tmp = encode(); if (tmp != null && tmp.length > 0) { //1.压缩 - if (tmp.length > ConfigCenter.INSTANCE.getCompressLimit()) { + if (tmp.length > ConfigCenter.holder.compressLimit()) { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java index 951d536a..b965cac0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.common.security; -import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.crypto.RSAUtils; import java.security.SecureRandom; @@ -11,7 +11,7 @@ * Created by ohun on 2015/12/24. */ public final class CipherBox { - public int aesKeyLength = ConfigCenter.INSTANCE.getAesKeyLength(); + public int aesKeyLength = ConfigCenter.holder.aesKeyLength(); public static final CipherBox INSTANCE = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; @@ -19,7 +19,7 @@ public final class CipherBox { public RSAPrivateKey getPrivateKey() { if (privateKey == null) { - String key = ConfigCenter.INSTANCE.getPrivateKey(); + String key = ConfigCenter.holder.privateKey(); try { privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(key); } catch (Exception e) { @@ -31,7 +31,7 @@ public RSAPrivateKey getPrivateKey() { public RSAPublicKey getPublicKey() { if (publicKey == null) { - String key = ConfigCenter.INSTANCE.getPublicKey(); + String key = ConfigCenter.holder.publicKey(); try { publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(key); } catch (Exception e) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index db71e436..12a2a814 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -4,9 +4,9 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; -import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; @@ -14,6 +14,7 @@ import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; + import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,7 +51,6 @@ public void run() { } private void init() throws IOException { - ConfigCenter.INSTANCE.init(); LOGGER.error("mpush app config center init success...."); } @@ -58,7 +58,7 @@ public void startConnectionServer() { ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { - final int port = ConfigCenter.INSTANCE.getConnectionServerPort(); + final int port = ConfigCenter.holder.connectionServerPort(); ConnectionServer server = new ConnectionServer(port); server.init(); server.start(new Server.Listener() { @@ -83,7 +83,7 @@ public void startGatewayServer() { ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { - final int port = ConfigCenter.INSTANCE.getGatewayServerPort(); + final int port = ConfigCenter.holder.gatewayServerPort(); GatewayServer server = new GatewayServer(port); server.init(); server.start(new Server.Listener() { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index 921d95a5..b82d2008 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -1,8 +1,8 @@ package com.shinemo.mpush.core.session; import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Strings; +import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.crypto.MD5Utils; import com.shinemo.mpush.tools.redis.manage.RedisManage; @@ -11,7 +11,7 @@ */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private int expiredTime = ConfigCenter.INSTANCE.getSessionExpiredTime(); + private int expiredTime = ConfigCenter.holder.sessionExpiredTime(); public boolean cacheSession(ReusableSession session) { RedisManage.set(session.sessionId, ReusableSession.encode(session.context), expiredTime); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index e5f0c468..4003eb6e 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -3,7 +3,6 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.tools.ConfigCenter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.zk.ZKPath; @@ -23,7 +22,6 @@ public class NettyClientTest { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); public void setUp() throws Exception { - ConfigCenter.INSTANCE.init(); } private List getAllServers() { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java index 4f88fa7d..65170c4b 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java @@ -3,7 +3,6 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.netty.server.NettyServer; -import com.shinemo.mpush.tools.ConfigCenter; import org.junit.Test; /** @@ -12,7 +11,6 @@ public class NettyServerTest { @Test public void testStart() throws Exception { - ConfigCenter.INSTANCE.init(); ConnectionServer server = new ConnectionServer(3000); server.init(); server.start(new Server.Listener() { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index b531a573..3b3e77bf 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -4,7 +4,8 @@ import com.shinemo.mpush.api.exception.DecodeException; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.config.ConfigCenter; + import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -64,7 +65,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { - if (bodyLength > ConfigCenter.INSTANCE.getMaxPacketSize()) { + if (bodyLength > ConfigCenter.holder.maxPacketSize()) { throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); } body = new byte[bodyLength]; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 744bfa34..01148c35 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -2,18 +2,19 @@ import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.config.ConfigCenter; + import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,7 +37,7 @@ public final class NettyConnectionManager implements ConnectionManager { public void init() { //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s - int ticksPerWheel = (int) (ConfigCenter.INSTANCE.getMaxHeartbeat() / tickDuration); + int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); EventBus.INSTANCE.register(this); } @@ -80,7 +81,7 @@ public void startTimeout() { public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) return; if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.INSTANCE.getMaxHBTimeoutTimes()) { + if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { connection.close(); LOGGER.error("connection heartbeat timeout, connection has bean closed"); return; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java deleted file mode 100644 index 5a5c7de3..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/ConfigCenter.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.shinemo.mpush.tools; - -import java.io.IOException; -import java.util.Properties; - -/** - * Created by ohun on 2016/1/5. - */ -public final class ConfigCenter { - public static final ConfigCenter INSTANCE = new ConfigCenter(); - private transient Properties cfg = new Properties(); - private int maxPacketSize = 10240;//10k - private int compressLimit = 10240;//10k - private int minHeartbeat = 1000 * 10;//10s - private int maxHeartbeat = 1000 * 60 * 30;//30min - private int maxHBTimeoutTimes = 2; - private int sessionExpiredTime = 86400;//unit second - private int rasKeyLength = 1024; - private int aesKeyLength = 16; - private int connectionServerPort = 3000; - private int gatewayServerPort = 4000; - private String privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; - private String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; - private String zkNamespace = "mpush"; - private String zkServer = "127.0.0.1:2181"; - private String redisServer = "127.0.0.1:6379:ShineMoIpo"; - - public void init() throws IOException { - cfg.load(this.getClass().getResourceAsStream("/config.properties")); - privateKey = getString("PRIVATE_KEY", privateKey); - publicKey = getString("PUBLIC_KEY", privateKey); - zkNamespace = getString("ZK_NAMESPACE", zkNamespace); - zkServer = getString("ZK_SERVER", zkServer); - redisServer = getString("REDIS_SERVER", redisServer); - - gatewayServerPort = getInt("GATEWAY_SERVER_PORT", gatewayServerPort); - connectionServerPort = getInt("CONNECTION_SERVER_PORT", connectionServerPort); - maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); - compressLimit = getInt("COMPRESS_LIMIT", compressLimit); - minHeartbeat = getInt("MIN_HEARTBEAT", minHeartbeat); - maxHeartbeat = getInt("MAX_HEARTBEAT", maxHeartbeat); - maxHBTimeoutTimes = getInt("MAX_HB_TIMEOUT_TIMES", maxHBTimeoutTimes); - sessionExpiredTime = getInt("SESSION_EXPIRED_TIME", sessionExpiredTime); - rasKeyLength = getInt("RAS_KEY_LENGTH", rasKeyLength); - aesKeyLength = getInt("AES_KEY_LENGTH", aesKeyLength); - maxPacketSize = getInt("MAX_PACKET_SIZE", maxPacketSize); - } - - public String getString(String key, String defaultValue) { - return cfg.getProperty(key, defaultValue); - } - - public boolean getBoolean(String key, boolean defaultValue) { - String value = cfg.getProperty(key); - return value == null ? defaultValue : Boolean.valueOf(value); - } - - public int getInt(String key, int defaultValue) { - String value = cfg.getProperty(key); - return value == null ? defaultValue : Integer.parseInt(value); - } - - public long getLong(String key, long defaultValue) { - String value = cfg.getProperty(key); - return value == null ? defaultValue : Long.parseLong(value); - } - - public int getMaxPacketSize() { - return maxPacketSize; - } - - public int getCompressLimit() { - return compressLimit; - } - - public int getMinHeartbeat() { - return minHeartbeat; - } - - public int getMaxHeartbeat() { - return maxHeartbeat; - } - - public int getMaxHBTimeoutTimes() { - return maxHBTimeoutTimes; - } - - public int getSessionExpiredTime() { - return sessionExpiredTime; - } - - public int getRasKeyLength() { - return rasKeyLength; - } - - public int getAesKeyLength() { - return aesKeyLength; - } - - public int getConnectionServerPort() { - return connectionServerPort; - } - - public int getGatewayServerPort() { - return gatewayServerPort; - } - - public String getPrivateKey() { - return privateKey; - } - - public String getPublicKey() { - return publicKey; - } - - public String getRedisServer() { - return redisServer; - } - - public String getZkServer() { - return zkServer; - } - - public String getZkNamespace() { - return zkNamespace; - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 4c28496a..4970861b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -3,6 +3,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.config.ConfigCenter; + import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; @@ -24,8 +26,8 @@ public static String getLocalIp() { public static int getHeartbeat(int min, int max) { return Math.max( - ConfigCenter.INSTANCE.getMinHeartbeat(), - Math.min(max, ConfigCenter.INSTANCE.getMaxHeartbeat()) + ConfigCenter.holder.minHeartbeat(), + Math.min(max, ConfigCenter.holder.maxHeartbeat()) ); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java index 96cba66b..a0464e3e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -5,7 +5,6 @@ import java.util.Comparator; import java.util.List; -import com.shinemo.mpush.tools.ConfigCenter; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -23,6 +22,8 @@ import org.slf4j.LoggerFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; +import com.shinemo.mpush.tools.config.ConfigCenter; + public class ZkUtil { private static final Logger LOGGER = LoggerFactory.getLogger(ZkUtil.class); @@ -50,7 +51,7 @@ public CuratorFramework getClient() { * 初始化 */ public void init() { - zkConfig = new ZkConfig(ConfigCenter.INSTANCE.getZkServer(), ConfigCenter.INSTANCE.getZkNamespace()); + zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace()); LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index e01800a2..b91df3b6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -22,7 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.ConfigCenter; +import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.ZkUtil; @@ -50,7 +50,7 @@ public CuratorFramework getClient() { */ @Override public void init() { - zkConfig = new ZkConfig(ConfigCenter.INSTANCE.getZkServer(), ConfigCenter.INSTANCE.getZkNamespace()); + zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace()); LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java index 09d14e18..229ba73d 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java @@ -2,7 +2,6 @@ import java.util.List; -import com.shinemo.mpush.tools.ConfigCenter; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; @@ -21,9 +20,7 @@ public class ZkUtilTest { @Before public void setUp() throws Exception { - ConfigCenter.INSTANCE.init(); zkUtil = ZkUtil.instance; - } @Test From 856cf07b8fc8ff073bd8fd8229e32a569d48b084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 10:14:53 +0800 Subject: [PATCH 146/890] =?UTF-8?q?add=20redis=20group=20convert=20use=20o?= =?UTF-8?q?wner=20=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/core/App.java | 14 ++----- .../src/main/resources/config.properties | 12 ++++-- .../mpush/tools/config/ConfigCenter.java | 9 +++++ .../tools/config/RedisGroupConverter.java | 38 +++++++++++++++++++ .../com/shinemo/mpush/tools/zk/ZkConfig.java | 6 ++- .../com/shinemo/mpush/tools/zk/ZkUtil.java | 2 +- 6 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index 12a2a814..c440ab62 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.core; -import com.google.common.collect.Lists; import com.shinemo.mpush.api.Server; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; @@ -8,14 +7,11 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; - -import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -111,12 +107,10 @@ private void registerServerToZK(int port, ZKPath path) { } public void initRedisClient() throws Exception { - Stat stat = ZkUtil.instance.getClient().checkExists().forPath(ZKPath.REDIS_SERVER.getPath()); - if (stat == null) { - RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); - RedisGroup group1 = new RedisGroup(); - group1.addRedisNode(node1); - List groupList = Lists.newArrayList(group1); + + boolean exist = ZkUtil.instance.isExisted(ZKPath.REDIS_SERVER.getPath()); + if (!exist) { + List groupList = ConfigCenter.holder.redisGroups(); ZkUtil.instance.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } RedisPathListener listener = new RedisPathListener(); diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties index 059d0db8..67e2ffaf 100644 --- a/mpush-core/src/main/resources/config.properties +++ b/mpush-core/src/main/resources/config.properties @@ -33,7 +33,11 @@ compress_limit = 10240 ## max_packet_size = 10240 -## -zk_ip = 10.1.20.74:2181 -zk_namespace = mpush2 -zk_digest = shinemoIpo2 \ No newline at end of file +## zk 配置项 +zk_ip = 10.1.10.41:2181 +zk_namespace = mpush +zk_digest = shinemoIpo + +## redis 配置项 redis组以分号分割,每组内的redis以逗号分割 +##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo +redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index e0e3e69d..666b6d5e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -1,10 +1,14 @@ package com.shinemo.mpush.tools.config; +import java.util.List; + import org.aeonbits.owner.Config; import org.aeonbits.owner.ConfigFactory; import org.aeonbits.owner.Config.Sources; +import com.shinemo.mpush.tools.redis.RedisGroup; + @Sources({"classpath:config.properties"}) public interface ConfigCenter extends Config{ @@ -74,4 +78,9 @@ public interface ConfigCenter extends Config{ @DefaultValue("shinemoIpo") public String zkDigest(); + @Separator(";") + @Key("redis_group") + @ConverterClass(RedisGroupConverter.class) + public List redisGroups(); + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java new file mode 100644 index 00000000..9a0a2f2c --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush.tools.config; + +import java.lang.reflect.Method; + +import org.aeonbits.owner.Converter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisNode; + +public class RedisGroupConverter implements Converter{ + + private static final Logger log = LoggerFactory.getLogger(RedisGroupConverter.class); + + @Override + public RedisGroup convert(Method method, String input) { + + log.warn("method:"+method.getName()+","+input); + + + RedisGroup group = new RedisGroup(); + + String[] chunks = input.split(","); + for (String chunk : chunks) { + String[] entry = chunk.split(":"); + String ip = entry[0].trim(); + String port = entry[1].trim(); + String password = entry[2].trim(); + RedisNode node = new RedisNode(ip, Integer.parseInt(port), password); + group.addRedisNode(node); + } + return group; + } + + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java index eb9cdde8..fca7d9eb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java @@ -23,7 +23,11 @@ public class ZkConfig { private final String localCachePath; public ZkConfig(String ipLists, String namespace) { - this(ipLists, namespace, Constants.ZK_MAX_RETRY, Constants.ZK_MIN_TIME, Constants.ZK_MAX_TIME, Constants.ZK_SESSION_TIMEOUT, Constants.ZK_CONNECTION_TIMEOUT,null,Constants.ZK_DEFAULT_CACHE_PATH); + this(ipLists, namespace, null); + } + + public ZkConfig(String ipLists, String namespace,String digest) { + this(ipLists, namespace, Constants.ZK_MAX_RETRY, Constants.ZK_MIN_TIME, Constants.ZK_MAX_TIME, Constants.ZK_SESSION_TIMEOUT, Constants.ZK_CONNECTION_TIMEOUT,digest,Constants.ZK_DEFAULT_CACHE_PATH); } public ZkConfig(String ipLists, String namespace, int maxRetry, int minTime, int maxTime, int sessionTimeout, int connectionTimeout,String digest,String localCachePath) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java index a0464e3e..f18a60b3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java @@ -51,7 +51,7 @@ public CuratorFramework getClient() { * 初始化 */ public void init() { - zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace()); + zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); From 5a470150e2529eb0aeb6d2b19a7274475c18f89a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 10:21:37 +0800 Subject: [PATCH 147/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/tools/config/ConfigCenter.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 666b6d5e..e2c8d961 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -9,6 +9,10 @@ import com.shinemo.mpush.tools.redis.RedisGroup; +/** + * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 + * + */ @Sources({"classpath:config.properties"}) public interface ConfigCenter extends Config{ From 426feea84dd860b117b325728c7d8d18e0ad082d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 10:49:39 +0800 Subject: [PATCH 148/890] =?UTF-8?q?zk=20=20=E8=BF=81=E7=A7=BBspi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/core/App.java | 18 +- .../src/main/resources/config.properties | 2 +- .../mpush/core}/zk/ServerManageTest.java | 4 +- .../com/shinemo/mpush/core/zk/ZkTest.java | 20 ++ .../shinemo/mpush/core}/zk/ZkUtilTest.java | 10 +- .../shinemo/mpush/tools/zk/ZkRegister.java | 5 + .../com/shinemo/mpush/tools/zk/ZkUtil.java | 276 ------------------ .../curator/services/ZkRegisterManager.java | 9 +- .../mpush/tools/zk/manage/ServerManage.java | 9 +- .../com.shinemo.mpush.tools.zk.ZkRegister | 1 + .../com/shinemo/mpush/tools/zk/ClientApp.java | 49 ---- .../shinemo/mpush/tools/zk/CuratorTest.java | 13 - .../zk/DistributedQueueConsumerTest.java | 35 --- .../mpush/tools/zk/InstanceDetails.java | 67 ----- .../shinemo/mpush/tools/zk/ServerAppTest.java | 53 ---- .../mpush/tools/zk/ServiceDiscoverer.java | 71 ----- .../mpush/tools/zk/ServiceRegistrar.java | 50 ---- 17 files changed, 60 insertions(+), 632 deletions(-) rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-core/src/test/java/com/shinemo/mpush/core}/zk/ServerManageTest.java (94%) create mode 100644 mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-core/src/test/java/com/shinemo/mpush/core}/zk/ZkUtilTest.java (92%) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java create mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index c440ab62..d359cda6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -7,11 +7,13 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +28,8 @@ public final class App { private static final App APP = new App(); private ConnectionServer connectionServer; private GatewayServer gatewayServer; + + private ZkRegister zkRegister = null; public static void main(String[] args) throws Exception { LOGGER.error("mpush app start begin...."); @@ -102,19 +106,23 @@ public void onFailure(String message) { private void registerServerToZK(int port, ZKPath path) { ServerApp app = new ServerApp(MPushUtil.getLocalIp(), port); - ZkUtil.instance.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); + zkRegister.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); LOGGER.error("mpush app register server:{} to zk success", port); } + + public void initZkRegister(){ + zkRegister = ServiceContainer.getInstance(ZkRegister.class); + } public void initRedisClient() throws Exception { - boolean exist = ZkUtil.instance.isExisted(ZKPath.REDIS_SERVER.getPath()); + boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); if (!exist) { List groupList = ConfigCenter.holder.redisGroups(); - ZkUtil.instance.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } RedisPathListener listener = new RedisPathListener(); - ZkUtil.instance.getCache().getListenable().addListener(listener); + zkRegister.getCache().getListenable().addListener(listener); listener.initData(null); } } diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties index 67e2ffaf..2b87767d 100644 --- a/mpush-core/src/main/resources/config.properties +++ b/mpush-core/src/main/resources/config.properties @@ -34,7 +34,7 @@ compress_limit = 10240 max_packet_size = 10240 ## zk 配置项 -zk_ip = 10.1.10.41:2181 +zk_ip = 10.1.30.2:2181 zk_namespace = mpush zk_digest = shinemoIpo diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java similarity index 94% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java index 0b5eb05e..db104531 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerManageTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.zk; +package com.shinemo.mpush.core.zk; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; @@ -9,6 +9,8 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.manage.ServerManage; public class ServerManageTest { diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java new file mode 100644 index 00000000..64936c04 --- /dev/null +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.core.zk; + +import org.junit.Test; + +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager; + +public class ZkTest { + + @Test + public void remove(){ + ZkRegister zkRegister = new ZkRegisterManager(); + + zkRegister.init(); + + zkRegister.remove("/"); + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkUtilTest.java similarity index 92% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java rename to mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkUtilTest.java index 229ba73d..a23d99d0 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ZkUtilTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkUtilTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.zk; +package com.shinemo.mpush.core.zk; import java.util.List; @@ -13,14 +13,18 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager; public class ZkUtilTest { - private ZkUtil zkUtil; + private ZkRegister zkUtil; @Before public void setUp() throws Exception { - zkUtil = ZkUtil.instance; + zkUtil = new ZkRegisterManager(); } @Test diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java index ebabfb53..23c1bedc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java @@ -3,6 +3,7 @@ import java.util.List; import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCache; import com.shinemo.mpush.tools.spi.SPI; @@ -33,5 +34,9 @@ public interface ZkRegister { public String get(String key); public CuratorFramework getClient(); + + public ZkConfig getZkConfig(); + + TreeCache getCache(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java deleted file mode 100644 index f18a60b3..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkUtil.java +++ /dev/null @@ -1,276 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.nio.charset.Charset; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.api.ACLProvider; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.CloseableUtils; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.curator.framework.CuratorFrameworkFactory.Builder; - -import com.shinemo.mpush.tools.config.ConfigCenter; - -public class ZkUtil { - - private static final Logger LOGGER = LoggerFactory.getLogger(ZkUtil.class); - - public static final ZkUtil instance = new ZkUtil(); - - private ZkConfig zkConfig; - - private CuratorFramework client; - private TreeCache cache; - - private ZkUtil() { - init(); - } - - public ZkConfig getZkConfig() { - return zkConfig; - } - - public CuratorFramework getClient() { - return client; - } - - /** - * 初始化 - */ - public void init() { - zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); - LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); - Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) - .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); - if (zkConfig.getConnectionTimeout() > 0) { - builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); - } - if (zkConfig.getSessionTimeout() > 0) { - builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); - } - if (StringUtils.isNoneBlank(zkConfig.getDigest())) { - builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { - - @Override - public List getDefaultAcl() { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - - @Override - public List getAclForPath(final String path) { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - }); - } - client = builder.build(); - client.start(); - try { - client.blockUntilConnected(); - cacheData(); - } catch (final Exception ex) { - LOGGER.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); - } - - } - - //本地缓存 - public void cacheData() throws Exception { - cache = new TreeCache(client, zkConfig.getLocalCachePath()); - cache.start(); - } - - private void waitClose() { - try { - Thread.sleep(600); - } catch (final InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - - /** - * 关闭 - */ - public void close() { - if (null != cache) { - cache.close(); - } - waitClose(); - CloseableUtils.closeQuietly(client); - } - - /** - * 获取数据,先从本地获取,本地找不到,从远程获取 - * - * @param key - * @return - */ - public String get(final String key) { - if (null == cache) { - return null; - } - ChildData resultIncache = cache.getCurrentData(key); - if (null != resultIncache) { - return null == resultIncache.getData() ? null : new String(resultIncache.getData(), Charset.forName("UTF-8")); - } - return getFromRemote(key); - } - - /** - * 从远程获取数据 - * - * @param key - * @return - */ - public String getFromRemote(final String key) { - try { - return new String(client.getData().forPath(key), Charset.forName("UTF-8")); - } catch (final Exception ex) { - LOGGER.error("getDirectly" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); - return null; - } - } - - /** - * 获取子节点 - * - * @param key - * @return - */ - public List getChildrenKeys(final String key) { - try { - List result = client.getChildren().forPath(key); - Collections.sort(result, new Comparator() { - - @Override - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); - return result; - } catch (final Exception ex) { - LOGGER.error("getChildrenKeys" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); - return Collections.emptyList(); - } - } - - /** - * 判断路径是否存在 - * - * @param key - * @return - */ - public boolean isExisted(final String key) { - try { - return null != client.checkExists().forPath(key); - } catch (final Exception ex) { - LOGGER.error("isExisted" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); - return false; - } - } - - /** - * 持久化数据 - * - * @param key - * @param value - */ - public void registerPersist(final String key, final String value) { - try { - if (!isExisted(key)) { - client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); - } else { - update(key, value); - } - } catch (final Exception ex) { - LOGGER.error("persist" + key + "," + value, ex); - } - } - - /** - * 更新数据 - * - * @param key - * @param value - */ - public void update(final String key, final String value) { - try { - client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); - } catch (final Exception ex) { - LOGGER.error("update" + key + "," + value, ex); - } - } - - /** - * 注册临时数据 - * - * @param key - * @param value - */ - public void registerEphemeral(final String key, final String value) { - try { - if (isExisted(key)) { - client.delete().deletingChildrenIfNeeded().forPath(key); - } - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); - } catch (final Exception ex) { - LOGGER.error("persistEphemeral" + key + "," + value, ex); - } - } - - /** - * 注册临时顺序数据 - * - * @param key - */ - public void registerEphemeralSequential(final String key, final String value) { - try { - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); - } catch (final Exception ex) { - LOGGER.error("persistEphemeralSequential" + key, ex); - } - } - - /** - * 注册临时顺序数据 - * - * @param key - */ - public void registerEphemeralSequential(final String key) { - try { - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); - } catch (final Exception ex) { - LOGGER.error("persistEphemeralSequential" + key, ex); - } - } - - /** - * 删除数据 - * - * @param key - */ - public void remove(final String key) { - try { - client.delete().deletingChildrenIfNeeded().forPath(key); - } catch (final Exception ex) { - LOGGER.error("remove" + key, ex); - } - } - - public TreeCache getCache() { - return cache; - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index b91df3b6..13ec2daa 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -25,17 +25,17 @@ import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.ZkUtil; public class ZkRegisterManager implements ZkRegister { - private static final Logger LOGGER = LoggerFactory.getLogger(ZkUtil.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ZkRegisterManager.class); private ZkConfig zkConfig; private CuratorFramework client; private TreeCache cache; - + + @Override public ZkConfig getZkConfig() { return zkConfig; } @@ -50,7 +50,7 @@ public CuratorFramework getClient() { */ @Override public void init() { - zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace()); + zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); @@ -278,6 +278,7 @@ public void remove(final String key) { } } + @Override public TreeCache getCache() { return cache; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java index fbeb9939..3af22e6a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java @@ -12,9 +12,10 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; @@ -22,8 +23,8 @@ public class ServerManage { private static final Logger log = LoggerFactory.getLogger(ServerManage.class); - private static ZkUtil zkUtil = ZkUtil.instance; - + private ZkRegister zkUtil = ServiceContainer.getInstance(ZkRegister.class); + private final AtomicBoolean startFlag = new AtomicBoolean(false); private final ServerApp app; @@ -113,7 +114,7 @@ public void close() { zkUtil.close(); } - public ZkUtil getZkUtil() { + public ZkRegister getZkUtil() { return zkUtil; } diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister new file mode 100644 index 00000000..e877b9a6 --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister @@ -0,0 +1 @@ +zkRegister=com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java deleted file mode 100644 index e48b3ae3..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ClientApp.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.api.CuratorEvent; -import org.apache.curator.framework.api.CuratorListener; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.CloseableUtils; -import org.apache.curator.x.discovery.ServiceInstance; - -public class ClientApp { - - public static void main(String[] args) throws Exception { - CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3)); - - client.getCuratorListenable().addListener(new CuratorListener() { - - @Override - public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { - System.out.println("Node data is changed, new data: " + new String(event.getData())); - } - - }); - - client.start(); - ServiceDiscoverer serviceDiscoverer = new ServiceDiscoverer(client,"services"); - - ServiceInstance instance1 = serviceDiscoverer.getInstanceByName("service1"); - - if(instance1!=null){ - System.out.println(instance1.buildUriSpec()); - System.out.println(instance1.getPayload()); - }else{ - System.out.println("instance1 is null"); - } - - ServiceInstance instance2 = serviceDiscoverer.getInstanceByName("service1"); - - if(instance2!=null){ - System.out.println(instance2.buildUriSpec()); - System.out.println(instance2.getPayload()); - }else{ - System.out.println("instance2 is null"); - } - - serviceDiscoverer.close(); - CloseableUtils.closeQuietly(client); - } -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java deleted file mode 100644 index 672ac2d9..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/CuratorTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -public class CuratorTest { - - private static String zkAddress = "127.0.0.1:2081"; - - public static void main(String[] args) { - - - - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java deleted file mode 100644 index 66b6d2da..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/DistributedQueueConsumerTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -public class DistributedQueueConsumerTest { - - private ServerApp app = new ServerApp("10.1.10.65", 3000); - - private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); - - @Before - public void setup() { - manage.start(); - } - - @Test - public void test() throws Exception{ - -// Consumer consumer = ; -// consumer.start(); - - } - - @After - public void close() { - manage.close(); - } - - - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java deleted file mode 100644 index 56be1069..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/InstanceDetails.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import org.codehaus.jackson.map.annotate.JsonRootName; - -@JsonRootName("details") -public class InstanceDetails { - - private String id; - - private String listenAddress; - - private int listenPort; - - private String interfaceName; - - public InstanceDetails(String id, String listenAddress, int listenPort,String interfaceName) { - this.id = id; - this.listenAddress = listenAddress; - this.listenPort = listenPort; - this.interfaceName = interfaceName; - } - - public InstanceDetails() { - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getListenAddress() { - return listenAddress; - } - - public void setListenAddress(String listenAddress) { - this.listenAddress = listenAddress; - } - - public int getListenPort() { - return listenPort; - } - - public void setListenPort(int listenPort) { - this.listenPort = listenPort; - } - - public String getInterfaceName() { - return interfaceName; - } - - public void setInterfaceName(String interfaceName) { - this.interfaceName = interfaceName; - } - - @Override - public String toString() { - return "InstanceDetails{" + - "id='" + id + '\'' + - ", listenAddress='" + listenAddress + '\'' + - ", listenPort=" + listenPort + - ", interfaceName='" + interfaceName + '\'' + - '}'; - } -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java deleted file mode 100644 index 1dab9f3a..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServerAppTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.util.UUID; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.api.CuratorEvent; -import org.apache.curator.framework.api.CuratorListener; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.x.discovery.ServiceInstance; -import org.apache.curator.x.discovery.ServiceType; -import org.apache.curator.x.discovery.UriSpec; - -public class ServerAppTest { - - public static void main(String[] args) throws Exception { - CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(10000, 3)); - - client.getCuratorListenable().addListener(new CuratorListener() { - - @Override - public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { - System.out.println("Node data is changed, new data: " + new String(event.getData())); - } - }); - - client.start(); - final ServiceRegistrar serviceRegistrar = new ServiceRegistrar(client, "services"); - final ServiceInstance instance1 = ServiceInstance. builder().name("service1").port(12345).address("192.168.1.100").serviceType(ServiceType.DYNAMIC) - .payload(new InstanceDetails(UUID.randomUUID().toString(), "192.168.1.100", 12345, "Test.Service1")).uriSpec(new UriSpec("{scheme}://{address}:{port}")).build(); - final ServiceInstance instance2 = ServiceInstance. builder().name("service2").port(12345).address("192.168.1.100").serviceType(ServiceType.DYNAMIC) - .payload(new InstanceDetails(UUID.randomUUID().toString(), "192.168.1.100", 12345, "Test.Service2")).uriSpec(new UriSpec("{scheme}://{address}:{port}")).build(); - serviceRegistrar.registerService(instance1); - System.out.println("register instance1"); - serviceRegistrar.registerService(instance2); - System.out.println("register instance2"); - // 删除 - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - try { - serviceRegistrar.unregisterService(instance1); - System.out.println("unregister instance1"); - serviceRegistrar.unregisterService(instance2); - System.out.println("unregister instance2"); - } catch (Exception e) { - } - } - }); - - Thread.sleep(Integer.MAX_VALUE); - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java deleted file mode 100644 index 0b9dc044..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceDiscoverer.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.io.Closeable; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.utils.CloseableUtils; -import org.apache.curator.x.discovery.ServiceDiscovery; -import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; -import org.apache.curator.x.discovery.ServiceInstance; -import org.apache.curator.x.discovery.ServiceProvider; -import org.apache.curator.x.discovery.details.JsonInstanceSerializer; -import org.apache.curator.x.discovery.strategies.RandomStrategy; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -public class ServiceDiscoverer { - - private ServiceDiscovery serviceDiscovery; - private Map> providers = Maps.newConcurrentMap(); - private List closeableList = Lists.newArrayList(); - private AtomicBoolean closed = new AtomicBoolean(false); - private Object lock = new Object(); - - - public ServiceDiscoverer(CuratorFramework client, String basePath) throws Exception { - JsonInstanceSerializer serializer = new JsonInstanceSerializer(InstanceDetails.class); - serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) - .client(client) - .basePath(basePath) - .serializer(serializer) - .build(); - - serviceDiscovery.start(); - } - - - public ServiceInstance getInstanceByName(String serviceName) throws Exception { - ServiceProvider provider = providers.get(serviceName); - if (provider == null) { - synchronized (lock) { - provider = providers.get(serviceName); - if (provider == null) { - provider = serviceDiscovery.serviceProviderBuilder(). - serviceName(serviceName). - providerStrategy(new RandomStrategy()) - .build(); - provider.start(); - closeableList.add(provider); - providers.put(serviceName, provider); - } - } - } - - return provider.getInstance(); - } - - - public void close() { - Preconditions.checkState(closed.compareAndSet(false, true), "discovery service already closed..."); - - for (Closeable closeable : closeableList) { - CloseableUtils.closeQuietly(closeable); - } - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java deleted file mode 100644 index 9f33a624..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/zk/ServiceRegistrar.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.x.discovery.ServiceDiscovery; -import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; -import org.apache.curator.x.discovery.ServiceInstance; -import org.apache.curator.x.discovery.details.JsonInstanceSerializer; - -import com.google.common.base.Preconditions; - -public class ServiceRegistrar { - - private ServiceDiscovery serviceDiscovery; - private final CuratorFramework client; - private AtomicBoolean closed = new AtomicBoolean(false); - - public ServiceRegistrar(CuratorFramework client,String basePath) throws Exception { - this.client = client; - JsonInstanceSerializer serializer = new JsonInstanceSerializer(InstanceDetails.class); - serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) - .client(client) - .serializer(serializer) - .basePath(basePath) - .build(); - serviceDiscovery.start(); - } - - public void registerService(ServiceInstance serviceInstance) throws Exception { - serviceDiscovery.registerService(serviceInstance); - } - - public void unregisterService(ServiceInstance serviceInstance) throws Exception { - serviceDiscovery.unregisterService(serviceInstance); - - } - - public void updateService(ServiceInstance serviceInstance) throws Exception { - serviceDiscovery.updateService(serviceInstance); - - } - - public void close() throws IOException { - Preconditions.checkState(closed.compareAndSet(false, true), "Registry service already closed..."); - serviceDiscovery.close(); - } - -} From ef3812a08dce18feddff573718a60913ab6c16ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 11:05:45 +0800 Subject: [PATCH 149/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E4=B8=8D=E9=80=9A=E8=BF=87=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/client/PushClient.java | 16 ++++++++++------ .../main/java/com/shinemo/mpush/core/App.java | 1 + .../mpush/core/netty/NettyClientTest.java | 10 +++++++--- .../tools/zk/listener/impl/ConnPathListener.java | 10 +++++++--- .../zk/listener/impl/RedisPathListener.java | 8 ++++++-- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index 83d55da3..a1a3b1a9 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -6,11 +6,13 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; + import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; @@ -26,6 +28,8 @@ public class PushClient implements PushSender { private int defaultTimeout = 3000; private final Map clientMap = new ConcurrentHashMap<>(); private final Map servers = new ConcurrentHashMap<>(); + + private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); public void init() throws Exception { initRedisClient(); @@ -73,14 +77,14 @@ public void send(String content, Collection userIds, Callback callback) public void initRedisClient() { RedisPathListener listener = new RedisPathListener(); - ZkUtil.instance.getCache().getListenable().addListener(listener); + zkRegister.getCache().getListenable().addListener(listener); listener.initData(null); } private class GatewayServerZKListener implements TreeCacheListener { public GatewayServerZKListener() { - ZkUtil.instance.getCache().getListenable().addListener(this); + zkRegister.getCache().getListenable().addListener(this); } @Override @@ -109,17 +113,17 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) } private ServerApp getServer(String path) { - String json = ZkUtil.instance.get(path); + String json = zkRegister.get(path); if (Strings.isNullOrEmpty(json)) return null; return Jsons.fromJson(json, ServerApp.class); } private Collection getAllServers() { - List list = ZkUtil.instance.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); + List list = zkRegister.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; for (String name : list) { String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name); - String json = ZkUtil.instance.get(fullPath); + String json = zkRegister.get(fullPath); if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue; ServerApp server = Jsons.fromJson(json, ServerApp.class); if (server != null) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index d359cda6..e8ec563d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -34,6 +34,7 @@ public final class App { public static void main(String[] args) throws Exception { LOGGER.error("mpush app start begin...."); APP.init(); + APP.initZkRegister(); APP.initRedisClient(); APP.startConnectionServer(); APP.startGatewayServer(); diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 4003eb6e..02cbd59e 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -5,9 +5,11 @@ import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.Strings; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.ZkRegister; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,16 +22,18 @@ */ public class NettyClientTest { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); + + private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); public void setUp() throws Exception { } private List getAllServers() { - List list = ZkUtil.instance.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + List list = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; List servers = new ArrayList<>(); for (String name : list) { - String json = ZkUtil.instance.get(ZKPath.CONNECTION_SERVER.getFullPath(name)); + String json = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(name)); if (Strings.isBlank(json)) continue; ServerApp server = Jsons.fromJson(json, ServerApp.class); if (server != null) servers.add(server); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java index d4362d3f..7e66ce2a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java @@ -2,7 +2,10 @@ import java.util.List; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; + import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; @@ -14,7 +17,6 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkUtil; import com.shinemo.mpush.tools.zk.listener.CallBack; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -25,6 +27,8 @@ public class ConnPathListener implements CallBack { private static final Logger log = LoggerFactory.getLogger(ConnPathListener.class); + + private static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); @Override public void handler(CuratorFramework client, TreeCacheEvent event, String path) { @@ -52,7 +56,7 @@ public void initData(ServerManage manage) { private void _initData() { //获取机器列表 - List rawData = ZkUtil.instance.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + List rawData = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); for (String raw : rawData) { String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); ServerApp app = getServerApp(fullPath); @@ -73,7 +77,7 @@ private void dataAddOrUpdate(ChildData data) { } private ServerApp getServerApp(String fullPath) { - String rawApp = ZkUtil.instance.get(fullPath); + String rawApp = zkRegister.get(fullPath); ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); return app; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index 6f0ae20a..dbec146c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -4,6 +4,7 @@ import java.util.List; import com.google.common.base.Strings; + import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; @@ -17,8 +18,9 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkUtil; +import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.manage.ServerManage; /** @@ -27,6 +29,8 @@ public class RedisPathListener implements TreeCacheListener { private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); + private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + @Override public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { String data = ""; @@ -65,7 +69,7 @@ private void dataAddOrUpdate(ChildData data) { } private List getRedisGroup(String fullPath) { - String rawGroup = ZkUtil.instance.get(fullPath); + String rawGroup = zkRegister.get(fullPath); if (Strings.isNullOrEmpty(rawGroup)) return Collections.EMPTY_LIST; List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); if (group == null) return Collections.EMPTY_LIST; From 84888550cffede737b11c399b47b72f3632b530d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 11:26:45 +0800 Subject: [PATCH 150/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-core/src/main/java/com/shinemo/mpush/core/App.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index e8ec563d..76d515d1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -113,6 +113,7 @@ private void registerServerToZK(int port, ZKPath path) { public void initZkRegister(){ zkRegister = ServiceContainer.getInstance(ZkRegister.class); + zkRegister.init(); } public void initRedisClient() throws Exception { From 9406ef3d88c1dfba881097d88ff170513649e9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 11:55:01 +0800 Subject: [PATCH 151/890] =?UTF-8?q?redis=20=E8=BF=81=E7=A7=BBspi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/redis/RedisRegister.java | 18 ++++++++ .../shinemo/mpush/tools/redis/RedisUtil.java | 5 ++- .../services/JedisRegisterManager.java} | 19 ++++---- .../mpush/tools/redis/manage/RedisManage.java | 43 +++++++++++-------- .../zk/listener/impl/RedisPathListener.java | 6 ++- ...om.shinemo.mpush.tools.redis.RedisRegister | 1 + .../tools/redis/RedisGroupManageTest.java | 8 ++-- 7 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java rename mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/{manage/RedisGroupManage.java => jedis/services/JedisRegisterManager.java} (85%) create mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java new file mode 100644 index 00000000..67853708 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java @@ -0,0 +1,18 @@ +package com.shinemo.mpush.tools.redis; + +import java.util.List; + +import com.shinemo.mpush.tools.spi.SPI; + +@SPI("redisRegister") +public interface RedisRegister { + + public void init(List group); + + public List getGroupList(); + + public RedisNode randomGetRedisNode(String key); + + public List hashSet(String key); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index d4488010..99bd59f3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -6,6 +6,7 @@ import java.util.Set; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,6 +15,7 @@ import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; + import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPubSub; @@ -44,7 +46,8 @@ public static void close(Jedis jedis) { * @param clazz * @return */ - public static T get(RedisNode node, String key, Class clazz) { + @SuppressWarnings("unchecked") + public static T get(RedisNode node, String key, Class clazz) { String value = null; Jedis jedis = null; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java similarity index 85% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java index d99959da..569295ff 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisGroupManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis.manage; +package com.shinemo.mpush.tools.redis.jedis.services; import java.util.Collections; import java.util.List; @@ -11,21 +11,16 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.redis.RedisRegister; import com.shinemo.mpush.tools.zk.manage.ServerAppManage; -public class RedisGroupManage { +public class JedisRegisterManager implements RedisRegister{ - - private static final Logger LOGGER = LoggerFactory.getLogger(ServerAppManage.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ServerAppManage.class); private static List groups = Lists.newArrayList(); - public static final RedisGroupManage instance = new RedisGroupManage(); - - - private RedisGroupManage() { - } - + @Override public void init(List group) { if (group == null || group.isEmpty()) { throw new RuntimeException("init redis client error, redis server is none."); @@ -35,6 +30,7 @@ public void init(List group) { } + @Override public List getGroupList() { return Collections.unmodifiableList(groups); } @@ -55,6 +51,7 @@ public int groupSize() { * @param key * @return */ + @Override public RedisNode randomGetRedisNode(String key) { int size = groupSize(); if (size == 1) return groups.get(0).get(key); @@ -69,6 +66,7 @@ public RedisNode randomGetRedisNode(String key) { * @param key * @return */ + @Override public List hashSet(String key) { List nodeList = Lists.newArrayList(); for (RedisGroup group : groups) { @@ -77,4 +75,5 @@ public List hashSet(String key) { } return nodeList; } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index 420dbdb5..0737ffb7 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -5,26 +5,31 @@ import java.util.Set; import com.shinemo.mpush.tools.redis.listener.MessageListener; + import redis.clients.jedis.JedisPubSub; import com.google.common.collect.Sets; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.redis.RedisRegister; import com.shinemo.mpush.tools.redis.RedisUtil; import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +import com.shinemo.mpush.tools.spi.ServiceContainer; /** * redis 对外封装接口 * */ public class RedisManage { + + private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); /********************* * k v redis start ********************************/ public static T get(String key, Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.get(node, key, clazz); } @@ -44,13 +49,13 @@ public static void set(String key, T value, Integer time) { * @param time seconds */ public static void set(String key, String value, Integer time) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); RedisUtil.set(nodeList, key, value, time); } public static void del(String key) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); RedisUtil.del(nodeList, key); } @@ -63,7 +68,7 @@ public static void del(String key) { ********************************/ public static void hset(String namespace, String key, String value) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); RedisUtil.hset(nodeList, namespace, key, value); } @@ -74,25 +79,25 @@ public static void hset(String namespace, String key, T value) { public static T hget(String namespace, String key, Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + RedisNode node = redisRegister.randomGetRedisNode(namespace); return RedisUtil.hget(node, namespace, key, clazz); } public static void hdel(String namespace, String key) { - List nodeList = RedisGroupManage.instance.hashSet(namespace); + List nodeList = redisRegister.hashSet(namespace); RedisUtil.hdel(nodeList, namespace, key); } public static Map hgetAll(String namespace) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + RedisNode node = redisRegister.randomGetRedisNode(namespace); return RedisUtil.hgetAll(node, namespace); } public static Map hgetAll(String namespace, Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + RedisNode node = redisRegister.randomGetRedisNode(namespace); return RedisUtil.hgetAll(node, namespace, clazz); } @@ -104,7 +109,7 @@ public static Map hgetAll(String namespace, Class clazz) { * @return */ public static Set hkeys(String namespace) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + RedisNode node = redisRegister.randomGetRedisNode(namespace); return RedisUtil.hkeys(node, namespace); } @@ -118,7 +123,7 @@ public static Set hkeys(String namespace) { * @return */ public static List hmget(String namespace, Class clazz, String... key) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(namespace); + RedisNode node = redisRegister.randomGetRedisNode(namespace); return RedisUtil.hmget(node, namespace, clazz, key); } @@ -131,7 +136,7 @@ public static List hmget(String namespace, Class clazz, String... key) * @param time */ public static void hmset(String namespace, Map hash, Integer time) { - List nodeList = RedisGroupManage.instance.hashSet(namespace); + List nodeList = redisRegister.hashSet(namespace); RedisUtil.hmset(nodeList, namespace, hash, time); } @@ -148,7 +153,7 @@ public static void hmset(String namespace, Map hash) { * 从队列的左边入队 */ public static void lpush(String key, String value) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); RedisUtil.lpush(nodeList, key, value); } @@ -160,7 +165,7 @@ public static void lpush(String key, T value) { * 从队列的右边入队 */ public static void rpush(String key, String value) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); RedisUtil.rpush(nodeList, key, value); } @@ -172,7 +177,7 @@ public static void rpush(String key, T value) { * 移除并且返回 key 对应的 list 的第一个元素 */ public static T lpop(String key, Class clazz) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); return RedisUtil.lpop(nodeList, key, clazz); } @@ -180,7 +185,7 @@ public static T lpop(String key, Class clazz) { * 从队列的右边出队一个元素 */ public static T rpop(String key, Class clazz) { - List nodeList = RedisGroupManage.instance.hashSet(key); + List nodeList = redisRegister.hashSet(key); return RedisUtil.rpop(nodeList, key, clazz); } @@ -191,7 +196,7 @@ public static T rpop(String key, Class clazz) { * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ public static List lrange(String key, int start, int end, Class clazz) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.lrange(node, key, start, end, clazz); } @@ -199,13 +204,13 @@ public static List lrange(String key, int start, int end, Class clazz) * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 */ public static long llen(String key) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(key); + RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.llen(node, key); } public static void publish(String channel, T message) { - RedisNode node = RedisGroupManage.instance.randomGetRedisNode(channel); + RedisNode node = redisRegister.randomGetRedisNode(channel); RedisUtil.publish(node, channel, message); } @@ -214,7 +219,7 @@ public static void subscribe(JedisPubSub pubsub, String... channels) { Set set = Sets.newHashSet(); for (String channel : channels) { - List nodeList = RedisGroupManage.instance.hashSet(channel); + List nodeList = redisRegister.hashSet(channel); set.addAll(nodeList); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index dbec146c..bd9c2506 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -17,7 +17,7 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; +import com.shinemo.mpush.tools.redis.RedisRegister; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; @@ -31,6 +31,8 @@ public class RedisPathListener implements TreeCacheListener { private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + @Override public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { String data = ""; @@ -57,7 +59,7 @@ public void initData(ServerManage manage) { private void _initData() { //获取redis列表 List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); - RedisGroupManage.instance.init(group); + redisRegister.init(group); } private void dataRemove(ChildData data) { diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister new file mode 100644 index 00000000..4fa6612a --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister @@ -0,0 +1 @@ +redisRegister=com.shinemo.mpush.tools.redis.jedis.services.JedisRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index c27f2d7c..a3ab0037 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -4,17 +4,17 @@ import java.util.List; import com.shinemo.mpush.tools.redis.listener.MessageListener; - import com.shinemo.mpush.tools.zk.ZKPath; + import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.manage.RedisGroupManage; import com.shinemo.mpush.tools.redis.manage.RedisManage; import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.manage.ServerManage; @@ -26,11 +26,13 @@ public class RedisGroupManageTest { RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); @Before public void init() { manage.start(); - groupList = RedisGroupManage.instance.getGroupList(); + groupList = redisRegister.getGroupList(); } @Test From 51c715ce0243b21a051d2a354d7fcf0f2869337e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 11:57:57 +0800 Subject: [PATCH 152/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20jedis=20register?= =?UTF-8?q?=20manage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/redis/jedis/services/JedisRegisterManager.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java index 569295ff..df435663 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -20,6 +20,9 @@ public class JedisRegisterManager implements RedisRegister{ private static List groups = Lists.newArrayList(); + /** + * zk 启动的时候需要调用这个 + */ @Override public void init(List group) { if (group == null || group.isEmpty()) { From 42d7f23443b3aef4513c444ee17a7207f9d06777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 13:22:21 +0800 Subject: [PATCH 153/890] delete test service --- .../mpush/services/com.shinemo.mpush.tools.spi.test.TestService | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService deleted file mode 100644 index e085c84d..00000000 --- a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService +++ /dev/null @@ -1,2 +0,0 @@ -test1=com.shinemo.mpush.tools.spi.test.TestServiceImpl -test2=com.shinemo.mpush.tools.spi.test.TestServiceImpl2 \ No newline at end of file From e1b129bb720a62c781a046c00d8f21dbf9f87a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 13:34:56 +0800 Subject: [PATCH 154/890] add push server and connection server module --- mpush-cs/pom.xml | 28 ++++++++++++++ .../src/main/java/com/shinemo/mpush/App.java | 13 +++++++ .../test/java/com/shinemo/mpush/AppTest.java | 38 +++++++++++++++++++ mpush-ps/pom.xml | 28 ++++++++++++++ .../src/main/java/com/shinemo/mpush/App.java | 13 +++++++ .../test/java/com/shinemo/mpush/AppTest.java | 38 +++++++++++++++++++ pom.xml | 2 + 7 files changed, 160 insertions(+) create mode 100644 mpush-cs/pom.xml create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/App.java create mode 100644 mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java create mode 100644 mpush-ps/pom.xml create mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/App.java create mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml new file mode 100644 index 00000000..73220dce --- /dev/null +++ b/mpush-cs/pom.xml @@ -0,0 +1,28 @@ + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-cs + jar + + mpush-cs + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/App.java b/mpush-cs/src/main/java/com/shinemo/mpush/App.java new file mode 100644 index 00000000..9cc9cf7b --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/App.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java new file mode 100644 index 00000000..50fff9a1 --- /dev/null +++ b/mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/mpush-ps/pom.xml b/mpush-ps/pom.xml new file mode 100644 index 00000000..8614904e --- /dev/null +++ b/mpush-ps/pom.xml @@ -0,0 +1,28 @@ + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-ps + jar + + mpush-ps + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/App.java b/mpush-ps/src/main/java/com/shinemo/mpush/App.java new file mode 100644 index 00000000..9cc9cf7b --- /dev/null +++ b/mpush-ps/src/main/java/com/shinemo/mpush/App.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java new file mode 100644 index 00000000..50fff9a1 --- /dev/null +++ b/mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/pom.xml b/pom.xml index 5755f170..f0bd3991 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,8 @@ mpush-netty mpush-common mpush-client + mpush-cs + mpush-ps From d5825b6a94fcdd47995f9d9b9eddceef40955407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 15 Jan 2016 17:43:10 +0800 Subject: [PATCH 155/890] =?UTF-8?q?=E6=8A=BD=E5=8F=96=20connection=20serve?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Server.java | 2 + .../com/shinemo/mpush/client/PushClient.java | 7 +- .../shinemo/mpush/client/PushClientTest.java | 100 ++--- .../shinemo/mpush/core/AbstractServer.java | 128 +++++++ .../main/java/com/shinemo/mpush/core/App.java | 260 ++++++------- .../com/shinemo/mpush/core/Application.java | 30 ++ .../mpush/core/server/AdminServer.java | 6 + .../mpush/core/zk/ServerManageTest.java | 160 ++++---- mpush-cs/pom.xml | 7 +- .../mpush/cs/ConnectionServerApplication.java | 15 + .../mpush/cs/ConnectionServerMain.java | 31 ++ .../cs/manage/ConnectionServerManage.java | 17 +- .../mpush/cs/manage/PushServerManage.java | 7 + .../shinemo/mpush/cs/manage/RedisManage.java | 7 + .../impl/ConnectionServerPathListener.java | 90 +++++ .../listener/impl/PushServerPathListener.java | 34 ++ .../zk/listener/impl/RedisPathListener.java | 90 +++++ mpush-ps/pom.xml | 10 +- .../com/shinemo/mpush/tools/GenericsUtil.java | 139 +++++++ .../shinemo/mpush/tools/redis/RedisUtil.java | 3 +- .../jedis/services/JedisRegisterManager.java | 3 +- .../shinemo/mpush/tools/zk/ZkRegister.java | 6 +- .../curator/services/ZkRegisterManager.java | 25 ++ .../mpush/tools/zk/listener/CallBack.java | 24 -- .../tools/zk/listener/DataChangeListener.java | 26 ++ .../tools/zk/listener/ListenerDispatcher.java | 58 --- .../zk/listener/impl/ConnPathListener.java | 86 ----- .../zk/listener/impl/GatewayPathListener.java | 44 --- .../zk/listener/impl/RedisPathListener.java | 80 ---- .../mpush/tools/zk/manage/ServerManage.java | 125 ------- .../tools/redis/RedisGroupManageTest.java | 226 +++++------ .../mpush/tools/redis/RedisUtilTest.java | 354 +++++++++--------- 32 files changed, 1202 insertions(+), 998 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/Application.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java rename mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java => mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java (74%) create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java index a86cbeba..70d731fe 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java @@ -8,6 +8,8 @@ public interface Server { void start(Listener listener); void stop(Listener listener); + + public void init(); boolean isRunning(); diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index a1a3b1a9..ab25c23e 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -11,7 +11,6 @@ import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; @@ -76,9 +75,9 @@ public void send(String content, Collection userIds, Callback callback) } public void initRedisClient() { - RedisPathListener listener = new RedisPathListener(); - zkRegister.getCache().getListenable().addListener(listener); - listener.initData(null); +// RedisPathListener listener = new RedisPathListener(); +// zkRegister.getCache().getListenable().addListener(listener); +// listener.initData(null); } private class GatewayServerZKListener implements TreeCacheListener { diff --git a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java index 2fc93266..743bfbce 100644 --- a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java +++ b/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java @@ -1,50 +1,50 @@ -package com.shinemo.mpush.client; - -import com.shinemo.mpush.api.PushSender; -import org.junit.Test; - -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; - -import static org.junit.Assert.*; - -/** - * Created by ohun on 2016/1/7. - */ -public class PushClientTest { - - @Test - public void testSend() throws Exception { - - } - - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.init(); - Thread.sleep(1000); - client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), - new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - } - ); - LockSupport.park(); - } -} \ No newline at end of file +//package com.shinemo.mpush.client; +// +//import com.shinemo.mpush.api.PushSender; +//import org.junit.Test; +// +//import java.util.Arrays; +//import java.util.concurrent.locks.LockSupport; +// +//import static org.junit.Assert.*; +// +///** +// * Created by ohun on 2016/1/7. +// */ +//public class PushClientTest { +// +// @Test +// public void testSend() throws Exception { +// +// } +// +// public static void main(String[] args) throws Exception { +// PushClient client = new PushClient(); +// client.init(); +// Thread.sleep(1000); +// client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), +// new PushSender.Callback() { +// @Override +// public void onSuccess(String userId) { +// System.err.println("push onSuccess userId=" + userId); +// } +// +// @Override +// public void onFailure(String userId) { +// System.err.println("push onFailure userId=" + userId); +// } +// +// @Override +// public void onOffline(String userId) { +// System.err.println("push onOffline userId=" + userId); +// } +// +// @Override +// public void onTimeout(String userId) { +// System.err.println("push onTimeout userId=" + userId); +// } +// } +// ); +// LockSupport.park(); +// } +//} \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java new file mode 100644 index 00000000..575940be --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -0,0 +1,128 @@ +package com.shinemo.mpush.core; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.api.Server; +import com.shinemo.mpush.core.server.ConnectionServer; +import com.shinemo.mpush.tools.GenericsUtil; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; + +public abstract class AbstractServer { + + private static final Logger log = LoggerFactory.getLogger(AbstractServer.class); + + protected Application application; + + protected List dataChangeListeners = Lists.newArrayList(); + + protected ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + + protected Server server; + + public AbstractServer() { + this.application = getApplication(); + } + + @SuppressWarnings("unchecked") + private Application getApplication() { + try { + return ((Class) GenericsUtil.getSuperClassGenericType(this.getClass(), 0)).newInstance(); + } catch (Exception e) { + log.warn("exception:",e); + throw new RuntimeException(e); + } + } + + public abstract Server getServer(); + + public void registerListener(DataChangeListener listener){ + dataChangeListeners.add(listener); + } + + //step1 启动 zk + public void initZK(){ + zkRegister = ServiceContainer.getInstance(ZkRegister.class); + zkRegister.init(); + } + + //step2 获取redis + public void initRedis(){ + boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); + if (!exist) { + List groupList = ConfigCenter.holder.redisGroups(); + zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } + } + + //step3 初始化 listener data + public void initListenerData(){ + for(DataChangeListener listener:dataChangeListeners){ + listener.initData(); + } + } + + //step4 初始化server + public void initServer(){ + server = getServer(); + } + + //step4 启动 netty server + public void startServer(){ + ThreadPoolUtil.newThread(new Runnable() { + @Override + public void run() { + final int port = ConfigCenter.holder.connectionServerPort(); + ConnectionServer server = new ConnectionServer(port); + server.init(); + server.start(new Server.Listener() { + @Override + public void onSuccess() { + log.error("mpush app start connection server success...."); + } + + @Override + public void onFailure(String message) { + log.error("mpush app start connection server failure, jvm exit with code -1"); + System.exit(-1); + } + }); + } + }, "conn-server", false).start(); + + } + + //step5 注册应用到zk + public void registerServerToZk(){ + ServerApp app = new ServerApp(MPushUtil.getLocalIp(), application.getPort()); + zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(app)); + } + + public void init(){ + initZK(); + initRedis(); + initListenerData(); + startServer(); + registerServerToZk(); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + if (server != null) { + server.stop(null); + } + } + }); + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java index 76d515d1..67672d94 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java @@ -1,130 +1,130 @@ -package com.shinemo.mpush.core; - -import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.core.server.ConnectionServer; -import com.shinemo.mpush.core.server.GatewayServer; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; - -/** - * Created by ohun on 2016/1/5. - */ -public final class App { - private static final Logger LOGGER = LoggerFactory.getLogger(App.class); - private static final App APP = new App(); - private ConnectionServer connectionServer; - private GatewayServer gatewayServer; - - private ZkRegister zkRegister = null; - - public static void main(String[] args) throws Exception { - LOGGER.error("mpush app start begin...."); - APP.init(); - APP.initZkRegister(); - APP.initRedisClient(); - APP.startConnectionServer(); - APP.startGatewayServer(); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - if (APP.connectionServer != null) { - APP.connectionServer.stop(null); - } - if (APP.gatewayServer != null) { - APP.gatewayServer.stop(null); - } - } - }); - LOGGER.error("mpush app start end...."); - } - - private void init() throws IOException { - LOGGER.error("mpush app config center init success...."); - } - - public void startConnectionServer() { - ThreadPoolUtil.newThread(new Runnable() { - @Override - public void run() { - final int port = ConfigCenter.holder.connectionServerPort(); - ConnectionServer server = new ConnectionServer(port); - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess() { - registerServerToZK(port, ZKPath.CONNECTION_SERVER); - LOGGER.error("mpush app start connection server success...."); - } - - @Override - public void onFailure(String message) { - LOGGER.error("mpush app start connection server failure, jvm exit with code -1"); - System.exit(-1); - } - }); - APP.connectionServer = server; - } - }, "conn-server", false).start(); - } - - public void startGatewayServer() { - ThreadPoolUtil.newThread(new Runnable() { - @Override - public void run() { - final int port = ConfigCenter.holder.gatewayServerPort(); - GatewayServer server = new GatewayServer(port); - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess() { - registerServerToZK(port, ZKPath.GATEWAY_SERVER); - LOGGER.error("mpush app start gateway server success...."); - } - - @Override - public void onFailure(String message) { - System.exit(-2); - LOGGER.error("mpush app start gateway server failure, jvm exit with code -2"); - } - }); - APP.gatewayServer = server; - } - }, "gateway-server", false).start(); - } - - private void registerServerToZK(int port, ZKPath path) { - ServerApp app = new ServerApp(MPushUtil.getLocalIp(), port); - zkRegister.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); - LOGGER.error("mpush app register server:{} to zk success", port); - } - - public void initZkRegister(){ - zkRegister = ServiceContainer.getInstance(ZkRegister.class); - zkRegister.init(); - } - - public void initRedisClient() throws Exception { - - boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); - if (!exist) { - List groupList = ConfigCenter.holder.redisGroups(); - zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - RedisPathListener listener = new RedisPathListener(); - zkRegister.getCache().getListenable().addListener(listener); - listener.initData(null); - } -} +//package com.shinemo.mpush.core; +// +//import com.shinemo.mpush.api.Server; +//import com.shinemo.mpush.core.server.ConnectionServer; +//import com.shinemo.mpush.core.server.GatewayServer; +//import com.shinemo.mpush.tools.MPushUtil; +//import com.shinemo.mpush.tools.Jsons; +//import com.shinemo.mpush.tools.config.ConfigCenter; +//import com.shinemo.mpush.tools.redis.RedisGroup; +//import com.shinemo.mpush.tools.spi.ServiceContainer; +//import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +//import com.shinemo.mpush.tools.zk.ZKPath; +//import com.shinemo.mpush.tools.zk.ServerApp; +//import com.shinemo.mpush.tools.zk.ZkRegister; +//import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; +// +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import java.io.IOException; +//import java.util.List; +// +///** +// * Created by ohun on 2016/1/5. +// */ +//public final class App { +// private static final Logger LOGGER = LoggerFactory.getLogger(App.class); +// private static final App APP = new App(); +// private ConnectionServer connectionServer; +// private GatewayServer gatewayServer; +// +// private ZkRegister zkRegister = null; +// +// public static void main(String[] args) throws Exception { +// LOGGER.error("mpush app start begin...."); +// APP.init(); +// APP.initZkRegister(); +// APP.initRedisClient(); +// APP.startConnectionServer(); +// APP.startGatewayServer(); +// Runtime.getRuntime().addShutdownHook(new Thread() { +// public void run() { +// if (APP.connectionServer != null) { +// APP.connectionServer.stop(null); +// } +// if (APP.gatewayServer != null) { +// APP.gatewayServer.stop(null); +// } +// } +// }); +// LOGGER.error("mpush app start end...."); +// } +// +// private void init() throws IOException { +// LOGGER.error("mpush app config center init success...."); +// } +// +// public void startConnectionServer() { +// ThreadPoolUtil.newThread(new Runnable() { +// @Override +// public void run() { +// final int port = ConfigCenter.holder.connectionServerPort(); +// ConnectionServer server = new ConnectionServer(port); +// server.init(); +// server.start(new Server.Listener() { +// @Override +// public void onSuccess() { +// registerServerToZK(port, ZKPath.CONNECTION_SERVER); +// LOGGER.error("mpush app start connection server success...."); +// } +// +// @Override +// public void onFailure(String message) { +// LOGGER.error("mpush app start connection server failure, jvm exit with code -1"); +// System.exit(-1); +// } +// }); +// APP.connectionServer = server; +// } +// }, "conn-server", false).start(); +// } +// +// public void startGatewayServer() { +// ThreadPoolUtil.newThread(new Runnable() { +// @Override +// public void run() { +// final int port = ConfigCenter.holder.gatewayServerPort(); +// GatewayServer server = new GatewayServer(port); +// server.init(); +// server.start(new Server.Listener() { +// @Override +// public void onSuccess() { +// registerServerToZK(port, ZKPath.GATEWAY_SERVER); +// LOGGER.error("mpush app start gateway server success...."); +// } +// +// @Override +// public void onFailure(String message) { +// System.exit(-2); +// LOGGER.error("mpush app start gateway server failure, jvm exit with code -2"); +// } +// }); +// APP.gatewayServer = server; +// } +// }, "gateway-server", false).start(); +// } +// +// private void registerServerToZK(int port, ZKPath path) { +// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), port); +// zkRegister.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); +// LOGGER.error("mpush app register server:{} to zk success", port); +// } +// +// public void initZkRegister(){ +// zkRegister = ServiceContainer.getInstance(ZkRegister.class); +// zkRegister.init(); +// } +// +// public void initRedisClient() throws Exception { +// +// boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); +// if (!exist) { +// List groupList = ConfigCenter.holder.redisGroups(); +// zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); +// } +// RedisPathListener listener = new RedisPathListener(); +// zkRegister.getCache().getListenable().addListener(listener); +// listener.initData(); +// } +//} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java b/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java new file mode 100644 index 00000000..b21837df --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java @@ -0,0 +1,30 @@ +package com.shinemo.mpush.core; + + +/** + * 系统配置 + * + */ +public abstract class Application { + + private int port; + + private String serverRegisterZkPath; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getServerRegisterZkPath() { + return serverRegisterZkPath; + } + + public void setServerRegisterZkPath(String serverRegisterZkPath) { + this.serverRegisterZkPath = serverRegisterZkPath; + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java index 3e08f30d..bc511ddd 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java @@ -21,4 +21,10 @@ public void stop(Listener listener) { public boolean isRunning() { return false; } + + @Override + public void init() { + // TODO Auto-generated method stub + + } } diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java index db104531..a8e86100 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java @@ -1,80 +1,80 @@ -package com.shinemo.mpush.core.zk; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -public class ServerManageTest { - - private static Executor executor = Executors.newCachedThreadPool(); - - private ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); - - private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); - - @Test - public void testMulThreadRegisterApp() throws InterruptedException { - CountDownLatch latch = new CountDownLatch(1); - for (int i = 1; i <= 2; i++) { - executor.execute(new Worker("192.168.1." + i, latch)); - } - latch.countDown(); - - Thread.sleep(Integer.MAX_VALUE); - } - - - @Test - public void testServerManageStart() throws InterruptedException { - manage.start(); - Thread.sleep(Integer.MAX_VALUE); - } - - - private static class Worker implements Runnable { - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - private final String ip; - private final CountDownLatch latch; - - public Worker(String ip, CountDownLatch latch) { - this.ip = ip; - this.latch = latch; - } - - @Override - public void run() { - try { - latch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - log.warn("start init " + ip); - ServerApp app = new ServerApp(ip, 3000); - ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); - manage.start(); - - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - manage.close(); - - log.warn("end init " + ip); - } - - } - -} +//package com.shinemo.mpush.core.zk; +// +//import java.util.concurrent.CountDownLatch; +//import java.util.concurrent.Executor; +//import java.util.concurrent.Executors; +// +//import org.junit.Test; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import com.shinemo.mpush.tools.MPushUtil; +//import com.shinemo.mpush.tools.zk.ServerApp; +//import com.shinemo.mpush.tools.zk.ZKPath; +//import com.shinemo.mpush.tools.zk.manage.ServerManage; +// +//public class ServerManageTest { +// +// private static Executor executor = Executors.newCachedThreadPool(); +// +// private ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); +// +// private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); +// +// @Test +// public void testMulThreadRegisterApp() throws InterruptedException { +// CountDownLatch latch = new CountDownLatch(1); +// for (int i = 1; i <= 2; i++) { +// executor.execute(new Worker("192.168.1." + i, latch)); +// } +// latch.countDown(); +// +// Thread.sleep(Integer.MAX_VALUE); +// } +// +// +// @Test +// public void testServerManageStart() throws InterruptedException { +// manage.start(); +// Thread.sleep(Integer.MAX_VALUE); +// } +// +// +// private static class Worker implements Runnable { +// +// private static final Logger log = LoggerFactory.getLogger(Worker.class); +// +// private final String ip; +// private final CountDownLatch latch; +// +// public Worker(String ip, CountDownLatch latch) { +// this.ip = ip; +// this.latch = latch; +// } +// +// @Override +// public void run() { +// try { +// latch.await(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// log.warn("start init " + ip); +// ServerApp app = new ServerApp(ip, 3000); +// ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); +// manage.start(); +// +// try { +// Thread.sleep(20000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// +// manage.close(); +// +// log.warn("end init " + ip); +// } +// +// } +// +//} diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 73220dce..4c252db7 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -11,7 +11,6 @@ jar mpush-cs - http://maven.apache.org UTF-8 @@ -19,10 +18,8 @@ - junit - junit - 3.8.1 - test + com.shinemo.mpush + mpush-core diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java new file mode 100644 index 00000000..8f2ebba4 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.cs; + +import com.shinemo.mpush.core.Application; +import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.zk.ZKPath; + +public class ConnectionServerApplication extends Application{ + + + public ConnectionServerApplication() { + setPort(ConfigCenter.holder.connectionServerPort()); + setServerRegisterZkPath(ZKPath.CONNECTION_SERVER.getWatchPath()); + } + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java new file mode 100644 index 00000000..4823ecc2 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.cs; + + + +import com.shinemo.mpush.api.Server; +import com.shinemo.mpush.core.AbstractServer; +import com.shinemo.mpush.core.server.ConnectionServer; +import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; +import com.shinemo.mpush.cs.zk.listener.impl.PushServerPathListener; +import com.shinemo.mpush.cs.zk.listener.impl.RedisPathListener; +import com.shinemo.mpush.tools.config.ConfigCenter; + +public class ConnectionServerMain extends AbstractServer{ + + public ConnectionServerMain(){ + + registerListener(new RedisPathListener()); + registerListener(new PushServerPathListener()); + registerListener(new ConnectionServerPathListener()); + + } + + @Override + public Server getServer() { + final int port = ConfigCenter.holder.connectionServerPort(); + ConnectionServer connectionServer = new ConnectionServer(port); + return connectionServer; + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java similarity index 74% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java rename to mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java index 5a64b337..1d4a4027 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerAppManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.zk.manage; +package com.shinemo.mpush.cs.manage; import java.util.Collection; import java.util.Collections; @@ -12,19 +12,15 @@ import com.google.common.collect.Maps; import com.shinemo.mpush.tools.zk.ServerApp; -/** - * 系统中当前可用的app列表 - * - */ -public class ServerAppManage { - - private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); +public class ConnectionServerManage { + + private static final Logger log = LoggerFactory.getLogger(ConnectionServerManage.class); private static Map holder = Maps.newConcurrentMap(); - public static final ServerAppManage instance = new ServerAppManage(); + public static final ConnectionServerManage instance = new ConnectionServerManage(); - private ServerAppManage() { + private ConnectionServerManage() { } public void addOrUpdate(String fullPath,ServerApp app){ @@ -46,4 +42,5 @@ private void printAppList(){ log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); } } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java new file mode 100644 index 00000000..bf560861 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.cs.manage; + +public class PushServerManage { + + + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java new file mode 100644 index 00000000..68f2a025 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.cs.manage; + +public class RedisManage { + + + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java new file mode 100644 index 00000000..1221333b --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -0,0 +1,90 @@ +package com.shinemo.mpush.cs.zk.listener.impl; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.cs.manage.ConnectionServerManage; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ServerApp; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; + +/** + * connection server 应用 监控 + * + */ +public class ConnectionServerPathListener extends DataChangeListener{ + + private static final Logger log = LoggerFactory.getLogger(ConnectionServerPathListener.class); + + private static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + + @Override + public void initData() { + log.warn("start init app data"); + _initData(); + log.warn("end init app data"); + } + + private void _initData() { + // 获取机器列表 + List rawData = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + for (String raw : rawData) { + String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); + ServerApp app = getServerApp(fullPath); + ConnectionServerManage.instance.addOrUpdate(fullPath, app); + } + } + + private void dataRemove(ChildData data) { + String path = data.getPath(); + ConnectionServerManage.instance.remove(path); + } + + private void dataAddOrUpdate(ChildData data) { + String path = data.getPath(); + byte[] rawData = data.getData(); + ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); + ConnectionServerManage.instance.addOrUpdate(path, serverApp); + } + + private ServerApp getServerApp(String fullPath) { + String rawApp = zkRegister.get(fullPath); + ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); + return app; + } + + @Override + public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + log.warn("ConnPathListener other path:" + path + "," + event.getType().name() + "," + data); + } + + } + + @Override + public String listenerPath() { + return null; + } + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java new file mode 100644 index 00000000..04e90702 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java @@ -0,0 +1,34 @@ +package com.shinemo.mpush.cs.zk.listener.impl; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; + +/** + * push server 路径监控 + * + */ +public class PushServerPathListener extends DataChangeListener{ + + private static final Logger log = LoggerFactory.getLogger(PushServerPathListener.class); + + @Override + public void initData() { + + } + + @Override + public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { + + } + + @Override + public String listenerPath() { + return null; + } + + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java new file mode 100644 index 00000000..0217e12d --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java @@ -0,0 +1,90 @@ +package com.shinemo.mpush.cs.zk.listener.impl; + +import java.util.Collections; +import java.util.List; + +import com.google.common.base.Strings; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisRegister; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; + +/** + * redis 监控 + */ +public class RedisPathListener extends DataChangeListener { + private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); + + private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + + private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + + // 获取redis列表 + private void _initData() { + log.warn("start init redis data"); + List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); + redisRegister.init(group); + log.warn("end init redis data"); + } + + private void dataRemove(ChildData data) { + _initData(); + } + + private void dataAddOrUpdate(ChildData data) { + _initData(); + } + + @SuppressWarnings("unchecked") + private List getRedisGroup(String fullPath) { + String rawGroup = zkRegister.get(fullPath); + if (Strings.isNullOrEmpty(rawGroup)) + return Collections.EMPTY_LIST; + List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); + if (group == null) + return Collections.EMPTY_LIST; + return group; + } + + @Override + public void initData() { + _initData(); + } + + @Override + public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { + + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + log.warn("RedisPathListener other path:" + data + "," + event.getType().name() + "," + data); + } + + } + + @Override + public String listenerPath() { + return ZKPath.REDIS_SERVER.getPath(); + } +} diff --git a/mpush-ps/pom.xml b/mpush-ps/pom.xml index 8614904e..6c8f2d5e 100644 --- a/mpush-ps/pom.xml +++ b/mpush-ps/pom.xml @@ -18,11 +18,9 @@ - - junit - junit - 3.8.1 - test - + + com.shinemo.mpush + mpush-core + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java new file mode 100644 index 00000000..e2913435 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java @@ -0,0 +1,139 @@ +package com.shinemo.mpush.tools; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + + +public class GenericsUtil { + + /** + * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * + * @param clazz clazz 需要反射的类,该类必须继承范型父类 + * @param index 泛型参数所在索引,从0开始. + * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getSuperClassGenericType(Class clazz, int index) { + Type genType = clazz.getGenericSuperclass();//得到泛型父类 + //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class + + if (!(genType instanceof ParameterizedType)) { + return Object.class; + } + //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 + Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); + if (index >= params.length || index < 0) { + throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); + } + if (!(params[index] instanceof Class)) { + return Object.class; + } + return (Class) params[index]; + } + /** + * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * + * @param clazz clazz 需要反射的类,该类必须继承泛型父类 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getSuperClassGenericType(Class clazz) { + return getSuperClassGenericType(clazz, 0); + } + /** + * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} + * + * @param method 方法 + * @param index 泛型参数所在索引,从0开始. + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getMethodGenericReturnType(Method method, int index) { + Type returnType = method.getGenericReturnType(); + if(returnType instanceof ParameterizedType){ + ParameterizedType type = (ParameterizedType) returnType; + Type[] typeArguments = type.getActualTypeArguments(); + if (index >= typeArguments.length || index < 0) { + throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class)typeArguments[index]; + } + return Object.class; + } + /** + * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} + * + * @param method 方法 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getMethodGenericReturnType(Method method) { + return getMethodGenericReturnType(method, 0); + } + + /** + * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} + * + * @param method 方法 + * @param index 第几个输入参数 + * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 + */ + public static List> getMethodGenericParameterTypes(Method method, int index) { + List> results = new ArrayList>(); + Type[] genericParameterTypes = method.getGenericParameterTypes(); + if (index >= genericParameterTypes.length ||index < 0) { + throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); + } + Type genericParameterType = genericParameterTypes[index]; + if(genericParameterType instanceof ParameterizedType){ + ParameterizedType aType = (ParameterizedType) genericParameterType; + Type[] parameterArgTypes = aType.getActualTypeArguments(); + for(Type parameterArgType : parameterArgTypes){ + Class parameterArgClass = (Class) parameterArgType; + results.add(parameterArgClass); + } + return results; + } + return results; + } + /** + * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} + * + * @param method 方法 + * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 + */ + public static List> getMethodGenericParameterTypes(Method method) { + return getMethodGenericParameterTypes(method, 0); + } + /** + * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; + * + * @param field 字段 + * @param index 泛型参数所在索引,从0开始. + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getFieldGenericType(Field field, int index) { + Type genericFieldType = field.getGenericType(); + + if(genericFieldType instanceof ParameterizedType){ + ParameterizedType aType = (ParameterizedType) genericFieldType; + Type[] fieldArgTypes = aType.getActualTypeArguments(); + if (index >= fieldArgTypes.length || index < 0) { + throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class)fieldArgTypes[index]; + } + return Object.class; + } + /** + * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; + * + * @param field 字段 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getFieldGenericType(Field field) { + return getFieldGenericType(field, 0); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 99bd59f3..8d6033bb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -14,7 +14,6 @@ import com.google.common.collect.Maps; import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @@ -22,7 +21,7 @@ public class RedisUtil { - private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class); + private static final Logger log = LoggerFactory.getLogger(RedisUtil.class); private static Map holder = Maps.newConcurrentMap(); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java index df435663..b20e6ebd 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -12,11 +12,10 @@ import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.redis.RedisRegister; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; public class JedisRegisterManager implements RedisRegister{ - private static final Logger LOGGER = LoggerFactory.getLogger(ServerAppManage.class); + private static final Logger LOGGER = LoggerFactory.getLogger(JedisRegisterManager.class); private static List groups = Lists.newArrayList(); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java index 23c1bedc..c335076f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java @@ -4,8 +4,8 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCache; - import com.shinemo.mpush.tools.spi.SPI; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; @SPI("zkRegister") @@ -37,6 +37,8 @@ public interface ZkRegister { public ZkConfig getZkConfig(); - TreeCache getCache(); + public TreeCache getCache(); + + public void registerListener(DataChangeListener listener); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 13ec2daa..95097964 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -14,6 +14,8 @@ import org.apache.curator.framework.api.ACLProvider; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.utils.CloseableUtils; import org.apache.zookeeper.CreateMode; @@ -22,9 +24,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; public class ZkRegisterManager implements ZkRegister { @@ -79,11 +83,27 @@ public List getAclForPath(final String path) { try { client.blockUntilConnected(); cacheData(); + registerConnectionLostListener(); } catch (final Exception ex) { LOGGER.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); } } + + // 注册连接状态监听器 + private void registerConnectionLostListener() { + client.getConnectionStateListenable().addListener(new ConnectionStateListener() { + + @Override + public void stateChanged(final CuratorFramework client, final ConnectionState newState) { + if (ConnectionState.LOST == newState) { + LOGGER.warn(MPushUtil.getInetAddress() + ", lost connection"); + } else if (ConnectionState.RECONNECTED == newState) { + LOGGER.warn(MPushUtil.getInetAddress() + ", reconnected"); + } + } + }); + } // 本地缓存 private void cacheData() throws Exception { @@ -277,6 +297,11 @@ public void remove(final String key) { LOGGER.error("remove" + key, ex); } } + + @Override + public void registerListener(DataChangeListener listener){ + cache.getListenable().addListener(listener); + } @Override public TreeCache getCache() { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java deleted file mode 100644 index 74d8c8b6..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/CallBack.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; - -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -public interface CallBack { - - /** - * 处理目录发生变化的事件 - * @param client - * @param event - * @param path - */ - public void handler(CuratorFramework client, TreeCacheEvent event,String path); - - /** - * 应用起来的时候初始化数据 - * @param manage - */ - public void initData(ServerManage manage); - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java new file mode 100644 index 00000000..f8eec80f --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.tools.zk.listener; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; + +public abstract class DataChangeListener implements TreeCacheListener{ + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + String path = null == event.getData() ? "" : event.getData().getPath(); + if (path.isEmpty()) { + return; + } + + if(listenerPath().equals(path)){ + dataChanged(client, event, path); + } + } + + public abstract void initData(); + + public abstract void dataChanged(CuratorFramework client, TreeCacheEvent event,String path) throws Exception; + + public abstract String listenerPath(); +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java deleted file mode 100644 index 123ff97d..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/ListenerDispatcher.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.manage.ServerManage; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Iterator; -import java.util.Map; - -public class ListenerDispatcher implements CallBack { - - private static final Logger log = LoggerFactory.getLogger(ListenerDispatcher.class); - - private Map holder = Maps.newTreeMap(); - - public ListenerDispatcher(ServerApp app) { - //所有connection server - //holder.put(ZKPath.CONNECTION_SERVER.getPathByIp(app.getIp()), new ConnPathListener()); - //所有redis - //holder.put(ZKPath.REDIS_SERVER.getPathByIp(app.getIp()), new RedisPathListener()); - //踢人的目录已经交给队列处理了,这里不需要重复处理 - //holder.put(ZKPath.GATEWAY_SERVER.getPathByIp(app.getIp()), new GatewayPathListener()); - } - - @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - - Iterator> it = holder.entrySet().iterator(); - boolean hasHandler = false; - while (it.hasNext()) { - Map.Entry entry = it.next(); - if (path.startsWith(entry.getKey())) { - hasHandler = true; - entry.getValue().handler(client, event, path); - } - } - - if (!hasHandler) { - log.warn("ListenerDispatcher other path:" + path + "," + event.getType().name()); - } - - } - - @Override - public void initData(ServerManage manage) { - - Iterator> it = holder.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = it.next(); - entry.getValue().initData(manage); - } - - } -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java deleted file mode 100644 index 7e66ce2a..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/ConnPathListener.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener.impl; - -import java.util.List; - -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.listener.CallBack; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -/** - * 注册的应用的发生变化 - */ -public class ConnPathListener implements CallBack { - - private static final Logger log = LoggerFactory.getLogger(ConnPathListener.class); - - private static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn("ConnPathListener other path:" + path + "," + event.getType().name() + "," + data); - } - } - - @Override - public void initData(ServerManage manage) { - log.warn("start init app data"); - _initData(); - log.warn("end init app data"); - } - - private void _initData() { - //获取机器列表 - List rawData = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); - for (String raw : rawData) { - String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); - ServerApp app = getServerApp(fullPath); - ServerAppManage.instance.addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data) { - String path = data.getPath(); - ServerAppManage.instance.remove(path); - } - - private void dataAddOrUpdate(ChildData data) { - String path = data.getPath(); - byte[] rawData = data.getData(); - ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); - ServerAppManage.instance.addOrUpdate(path, serverApp); - } - - private ServerApp getServerApp(String fullPath) { - String rawApp = zkRegister.get(fullPath); - ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); - return app; - } - - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java deleted file mode 100644 index 9da911d2..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/GatewayPathListener.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener.impl; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.zk.listener.CallBack; -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -/** - * 当前应用下踢人的目录发生变化 - * - */ -public class GatewayPathListener implements CallBack{ - - private static final Logger log = LoggerFactory.getLogger(GatewayPathListener.class); - - @Override - public void handler(CuratorFramework client, TreeCacheEvent event, String path) { - String data = ""; - if(event.getData()!=null){ - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - log.warn("path:" + path + ", node Add"+","+data); - } else if (Type.NODE_REMOVED == event.getType()) { - log.warn("path:" + path + ", node Remove"+","+data); - } else if (Type.NODE_UPDATED == event.getType()) { - log.warn("path:" + path + "," + "node update"+","+data); - } else { - log.warn("other path:" + path + "," + event.getType().name()+","+data); - } - } - - @Override - public void initData(ServerManage manage) { - - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java deleted file mode 100644 index bd9c2506..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener.impl; - -import java.util.Collections; -import java.util.List; - -import com.google.common.base.Strings; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisRegister; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -/** - * 注册的应用的发生变化 - */ -public class RedisPathListener implements TreeCacheListener { - private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); - - private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); - - @Override - public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn("ConnPathListener other path:" + data + "," + event.getType().name() + "," + data); - } - } - - public void initData(ServerManage manage) { - log.warn("start init redis data"); - _initData(); - log.warn("end init redis data"); - } - - private void _initData() { - //获取redis列表 - List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); - redisRegister.init(group); - } - - private void dataRemove(ChildData data) { - _initData(); - } - - private void dataAddOrUpdate(ChildData data) { - _initData(); - } - - private List getRedisGroup(String fullPath) { - String rawGroup = zkRegister.get(fullPath); - if (Strings.isNullOrEmpty(rawGroup)) return Collections.EMPTY_LIST; - List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); - if (group == null) return Collections.EMPTY_LIST; - return group; - } -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java deleted file mode 100644 index 3af22e6a..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/manage/ServerManage.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.shinemo.mpush.tools.zk.manage; - -import java.util.concurrent.atomic.AtomicBoolean; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import org.apache.curator.framework.state.ConnectionState; -import org.apache.curator.framework.state.ConnectionStateListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.CallBack; -import com.shinemo.mpush.tools.zk.listener.ListenerDispatcher; - -public class ServerManage { - - private static final Logger log = LoggerFactory.getLogger(ServerManage.class); - - private ZkRegister zkUtil = ServiceContainer.getInstance(ZkRegister.class); - - private final AtomicBoolean startFlag = new AtomicBoolean(false); - - private final ServerApp app; - private final ZKPath path; - - private ListenerDispatcher dispatcher; - - public ServerManage(ServerApp app, ZKPath path) { - this.app = app; - this.path = path; - } - - public void start() { - - if (!startFlag.compareAndSet(false, true)) { - return; - } - - dispatcher = new ListenerDispatcher(app); - - //注册机器到zk中 - registerApp(); - - // 注册连接状态监听器 - registerConnectionLostListener(); - - // 注册节点数据变化 - registerDataChange(dispatcher); - - //获取应用起来的时候的初始化数据 - initAppData(dispatcher); - - } - - private void registerApp() { - zkUtil.registerEphemeralSequential(path.getPath(), Jsons.toJson(app)); - } - - public void unregisterApp() { - zkUtil.remove(path.getPath()); - } - - // 注册连接状态监听器 - private void registerConnectionLostListener() { - zkUtil.getClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { - - @Override - public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - if (ConnectionState.LOST == newState) { - log.warn(app.getIp() + ", lost connection"); - } else if (ConnectionState.RECONNECTED == newState) { - log.warn(app.getIp() + ", reconnected"); - } - } - }); - } - - // 注册节点数据变化 - private void registerDataChange(final CallBack callBack) { - zkUtil.getCache().getListenable().addListener(new TreeCacheListener() { - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - String path = null == event.getData() ? "" : event.getData().getPath(); - if (path.isEmpty()) { - log.warn("registerDataChange empty path:" + path + "," + event.getType().name()); - return; - } - callBack.handler(client, event, path); - } - }); - } - - private void initAppData(final CallBack callBack) { - callBack.initData(this); - } - - public CuratorFramework getClient() { - return zkUtil.getClient(); - } - - public TreeCache getCache() { - return zkUtil.getCache(); - } - - public void close() { - zkUtil.close(); - } - - public ZkRegister getZkUtil() { - return zkUtil; - } - - public ServerApp getServerApp() { - return app; - } - -} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index a3ab0037..60da5d22 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -1,113 +1,113 @@ -package com.shinemo.mpush.tools.redis; - -import java.util.Date; -import java.util.List; - -import com.shinemo.mpush.tools.redis.listener.MessageListener; -import com.shinemo.mpush.tools.zk.ZKPath; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.junit.Before; -import org.junit.Test; - -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -import com.shinemo.mpush.tools.redis.pubsub.Subscriber; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.manage.ServerManage; - -public class RedisGroupManageTest { - - ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); - ServerManage manage = new ServerManage(app, ZKPath.REDIS_SERVER); - List groupList = null; - - RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); - RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); - - @Before - public void init() { - manage.start(); - groupList = redisRegister.getGroupList(); - } - - @Test - public void testGetRedisGroup() { - for (RedisGroup group : groupList) { - for (RedisNode node : group.getRedisNodeList()) { - System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - - @Test - public void testAdd() { - User user = RedisManage.get("huang2", User.class); - if (user == null) { - user = new User("hi", 10, new Date()); - RedisManage.set("huang2", user); - user = RedisManage.get("huang2", User.class); - } - System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); - - User nowUser = RedisUtil.get(node, "huang2", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node2, "huang2", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisManage.del("huang2"); - - nowUser = RedisUtil.get(node2, "huang2", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "huang2", User.class); - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - - } - - @Test - public void testPub() { - for (int i = 0; i < 20; i++) { - User user = new User("pub" + i, 10, new Date()); - RedisManage.publish("channel1", user); - RedisManage.publish("channel2", user); - } - } - - @Test - public void testSub() { - RedisManage.subscribe(new MessageListener() { - @Override - public void onMessage(String channel, String message) { - System.out.printf("on message channel=%s, message=%s%n", channel, message); - } - }, "channel1", "channel2"); - try { - Thread.sleep(Integer.MAX_VALUE); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - @Test - public void testSub2() { - RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); - try { - Thread.sleep(Integer.MAX_VALUE); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - -} +//package com.shinemo.mpush.tools.redis; +// +//import java.util.Date; +//import java.util.List; +// +//import com.shinemo.mpush.tools.redis.listener.MessageListener; +//import com.shinemo.mpush.tools.zk.ZKPath; +// +//import org.apache.commons.lang3.builder.ToStringBuilder; +//import org.apache.commons.lang3.builder.ToStringStyle; +//import org.junit.Before; +//import org.junit.Test; +// +//import com.shinemo.mpush.tools.MPushUtil; +//import com.shinemo.mpush.tools.redis.manage.RedisManage; +//import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +//import com.shinemo.mpush.tools.spi.ServiceContainer; +//import com.shinemo.mpush.tools.zk.ServerApp; +//import com.shinemo.mpush.tools.zk.manage.ServerManage; +// +//public class RedisGroupManageTest { +// +// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); +// ServerManage manage = new ServerManage(app, ZKPath.REDIS_SERVER); +// List groupList = null; +// +// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); +// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); +// +// RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); +// +// @Before +// public void init() { +// manage.start(); +// groupList = redisRegister.getGroupList(); +// } +// +// @Test +// public void testGetRedisGroup() { +// for (RedisGroup group : groupList) { +// for (RedisNode node : group.getRedisNodeList()) { +// System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); +// } +// +// } +// } +// +// @Test +// public void testAdd() { +// User user = RedisManage.get("huang2", User.class); +// if (user == null) { +// user = new User("hi", 10, new Date()); +// RedisManage.set("huang2", user); +// user = RedisManage.get("huang2", User.class); +// } +// System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); +// +// User nowUser = RedisUtil.get(node, "huang2", User.class); +// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); +// +// nowUser = RedisUtil.get(node2, "huang2", User.class); +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// +// RedisManage.del("huang2"); +// +// nowUser = RedisUtil.get(node2, "huang2", User.class); +// if (nowUser == null) { +// System.out.println("node2 nowUser is null"); +// } else { +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// } +// +// nowUser = RedisUtil.get(node, "huang2", User.class); +// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); +// +// } +// +// @Test +// public void testPub() { +// for (int i = 0; i < 20; i++) { +// User user = new User("pub" + i, 10, new Date()); +// RedisManage.publish("channel1", user); +// RedisManage.publish("channel2", user); +// } +// } +// +// @Test +// public void testSub() { +// RedisManage.subscribe(new MessageListener() { +// @Override +// public void onMessage(String channel, String message) { +// System.out.printf("on message channel=%s, message=%s%n", channel, message); +// } +// }, "channel1", "channel2"); +// try { +// Thread.sleep(Integer.MAX_VALUE); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// +// @Test +// public void testSub2() { +// RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); +// try { +// Thread.sleep(Integer.MAX_VALUE); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// +// +//} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java index 5bfc1443..07367fd8 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java @@ -1,177 +1,177 @@ -package com.shinemo.mpush.tools.redis; - -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.junit.Test; - -import com.google.common.collect.Lists; -import com.shinemo.mpush.tools.zk.manage.ServerAppManage; - -import redis.clients.jedis.Jedis; - -public class RedisUtilTest { - - - - RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); - RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - List nodeList = Lists.newArrayList(node, node2); - - @Test - public void testAddAndGetAndDelete() { - Jedis jedis = RedisUtil.getClient(node2); - jedis.set("hi", "huang"); - - String ret = jedis.get("hi"); - System.out.println(ret); - - jedis.del("hi"); - ret = jedis.get("hi"); - if (ret == null) { - System.out.println("ret is null"); - } else { - System.out.println("ret is not null:" + ret); - } - - } - - @Test - public void testJedisPool() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 0; i < 10; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - } - - @Test - public void testJedisPool2() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 1; i <= 8; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - - System.out.println(jedisList.size()); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("first get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - // 关闭一个链接 - RedisUtil.close(jedisList.get(0)); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("second get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - System.out.println(jedisList.size()); - } - - @Test - public void testKV() { - User user = new User("huang", 18, new Date()); - RedisUtil.set(nodeList, "test", user); - - User nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node2, "test", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisUtil.del(nodeList, "test"); - - nowUser = RedisUtil.get(node2, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - RedisUtil.set(nodeList, "test", user, 10); - - try { - Thread.sleep(12000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - nowUser = RedisUtil.get(node2, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void hashTest(){ - - User user = new User("huang", 18, new Date()); - - RedisUtil.hset(nodeList, "hashhuang", "hi", user); - - User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - - Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); - Iterator> iterator = ret.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String key = entry.getKey(); - User val = entry.getValue(); - System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); - } - - RedisUtil.hdel(nodeList, "hashhuang", "hi"); - - nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node2 nowUser is null"); - }else{ - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node nowUser is null"); - }else{ - System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); - } - - } - -} +//package com.shinemo.mpush.tools.redis; +// +//import java.util.Date; +//import java.util.Iterator; +//import java.util.List; +//import java.util.Map; +// +//import org.apache.commons.lang3.builder.ToStringBuilder; +//import org.junit.Test; +// +//import com.google.common.collect.Lists; +//import com.shinemo.mpush.tools.zk.manage.ServerAppManage; +// +//import redis.clients.jedis.Jedis; +// +//public class RedisUtilTest { +// +// +// +// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); +// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); +// +// List nodeList = Lists.newArrayList(node, node2); +// +// @Test +// public void testAddAndGetAndDelete() { +// Jedis jedis = RedisUtil.getClient(node2); +// jedis.set("hi", "huang"); +// +// String ret = jedis.get("hi"); +// System.out.println(ret); +// +// jedis.del("hi"); +// ret = jedis.get("hi"); +// if (ret == null) { +// System.out.println("ret is null"); +// } else { +// System.out.println("ret is not null:" + ret); +// } +// +// } +// +// @Test +// public void testJedisPool() { +// // 最大连接数是8,因此,获取10个链接会抛错误 +// List jedisList = Lists.newArrayList(); +// for (int i = 0; i < 10; i++) { +// Jedis jedis = RedisUtil.getClient(node); +// jedisList.add(jedis); +// } +// } +// +// @Test +// public void testJedisPool2() { +// // 最大连接数是8,因此,获取10个链接会抛错误 +// List jedisList = Lists.newArrayList(); +// for (int i = 1; i <= 8; i++) { +// Jedis jedis = RedisUtil.getClient(node); +// jedisList.add(jedis); +// } +// +// System.out.println(jedisList.size()); +// +// try { +// Jedis jedis = RedisUtil.getClient(node); +// jedisList.add(jedis); +// System.out.println("first get jedis success"); +// } catch (Exception e) { +// System.out.println(e); +// } +// +// // 关闭一个链接 +// RedisUtil.close(jedisList.get(0)); +// +// try { +// Jedis jedis = RedisUtil.getClient(node); +// jedisList.add(jedis); +// System.out.println("second get jedis success"); +// } catch (Exception e) { +// System.out.println(e); +// } +// +// System.out.println(jedisList.size()); +// } +// +// @Test +// public void testKV() { +// User user = new User("huang", 18, new Date()); +// RedisUtil.set(nodeList, "test", user); +// +// User nowUser = RedisUtil.get(node, "test", User.class); +// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); +// +// nowUser = RedisUtil.get(node2, "test", User.class); +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// +// RedisUtil.del(nodeList, "test"); +// +// nowUser = RedisUtil.get(node2, "test", User.class); +// if (nowUser == null) { +// System.out.println("node2 nowUser is null"); +// } else { +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// } +// +// nowUser = RedisUtil.get(node, "test", User.class); +// if (nowUser == null) { +// System.out.println("node nowUser is null"); +// } else { +// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); +// } +// +// RedisUtil.set(nodeList, "test", user, 10); +// +// try { +// Thread.sleep(12000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// +// nowUser = RedisUtil.get(node2, "test", User.class); +// if (nowUser == null) { +// System.out.println("node2 nowUser is null"); +// } else { +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// } +// +// nowUser = RedisUtil.get(node, "test", User.class); +// if (nowUser == null) { +// System.out.println("node nowUser is null"); +// } else { +// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); +// } +// +// } +// +// @Test +// public void hashTest(){ +// +// User user = new User("huang", 18, new Date()); +// +// RedisUtil.hset(nodeList, "hashhuang", "hi", user); +// +// User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); +// System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); +// +// nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); +// System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); +// +// Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); +// Iterator> iterator = ret.entrySet().iterator(); +// while (iterator.hasNext()) { +// Map.Entry entry = iterator.next(); +// String key = entry.getKey(); +// User val = entry.getValue(); +// System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); +// } +// +// RedisUtil.hdel(nodeList, "hashhuang", "hi"); +// +// nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); +// if(nowUser==null){ +// System.out.println("node2 nowUser is null"); +// }else{ +// System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); +// } +// +// nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); +// if(nowUser==null){ +// System.out.println("node nowUser is null"); +// }else{ +// System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); +// } +// +// } +// +//} From 12f03fd58dced38707acd1616aa39b2324bcefe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 11:29:47 +0800 Subject: [PATCH 156/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/AbstractServer.java | 2 +- .../src/main/resources/config.properties | 2 +- .../src/main/java/com/shinemo/mpush/App.java | 13 - .../mpush/cs/ConnectionServerMain.java | 5 + .../listener/impl/PushServerPathListener.java | 8 +- .../tools/zk/listener/DataChangeListener.java | 6 + .../tools/redis/RedisGroupManageTest.java | 222 ++++++----- .../mpush/tools/redis/RedisUtilTest.java | 353 +++++++++--------- 8 files changed, 305 insertions(+), 306 deletions(-) delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/App.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index 575940be..c2e0b8d0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -110,7 +110,7 @@ public void registerServerToZk(){ zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(app)); } - public void init(){ + public void start(){ initZK(); initRedis(); initListenerData(); diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties index 2b87767d..67e2ffaf 100644 --- a/mpush-core/src/main/resources/config.properties +++ b/mpush-core/src/main/resources/config.properties @@ -34,7 +34,7 @@ compress_limit = 10240 max_packet_size = 10240 ## zk 配置项 -zk_ip = 10.1.30.2:2181 +zk_ip = 10.1.10.41:2181 zk_namespace = mpush zk_digest = shinemoIpo diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/App.java b/mpush-cs/src/main/java/com/shinemo/mpush/App.java deleted file mode 100644 index 9cc9cf7b..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shinemo.mpush; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 4823ecc2..528df5f9 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -27,5 +27,10 @@ public Server getServer() { return connectionServer; } + + public static void main(String[] args) { + ConnectionServerMain connectionServerMain = new ConnectionServerMain(); + connectionServerMain.start(); + } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java index 04e90702..57f05010 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.cs.zk.listener.impl; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.slf4j.Logger; @@ -22,7 +24,11 @@ public void initData() { @Override public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + log.warn("ConnPathListener other path:" + path + "," + event.getType().name() + "," + data); } @Override diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java index f8eec80f..c78ecfe3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java @@ -3,9 +3,13 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class DataChangeListener implements TreeCacheListener{ + private static final Logger log = LoggerFactory.getLogger(DataChangeListener.class); + @Override public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { String path = null == event.getData() ? "" : event.getData().getPath(); @@ -13,6 +17,8 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc return; } + log.warn("DataChangeListener:"+path); + if(listenerPath().equals(path)){ dataChanged(client, event, path); } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index 60da5d22..e03e5f70 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -1,113 +1,109 @@ -//package com.shinemo.mpush.tools.redis; -// -//import java.util.Date; -//import java.util.List; -// -//import com.shinemo.mpush.tools.redis.listener.MessageListener; -//import com.shinemo.mpush.tools.zk.ZKPath; -// -//import org.apache.commons.lang3.builder.ToStringBuilder; -//import org.apache.commons.lang3.builder.ToStringStyle; -//import org.junit.Before; -//import org.junit.Test; -// -//import com.shinemo.mpush.tools.MPushUtil; -//import com.shinemo.mpush.tools.redis.manage.RedisManage; -//import com.shinemo.mpush.tools.redis.pubsub.Subscriber; -//import com.shinemo.mpush.tools.spi.ServiceContainer; -//import com.shinemo.mpush.tools.zk.ServerApp; -//import com.shinemo.mpush.tools.zk.manage.ServerManage; -// -//public class RedisGroupManageTest { -// -// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// ServerManage manage = new ServerManage(app, ZKPath.REDIS_SERVER); -// List groupList = null; -// -// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); -// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); -// -// RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); -// -// @Before -// public void init() { -// manage.start(); -// groupList = redisRegister.getGroupList(); -// } -// -// @Test -// public void testGetRedisGroup() { -// for (RedisGroup group : groupList) { -// for (RedisNode node : group.getRedisNodeList()) { -// System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); -// } -// -// } -// } -// -// @Test -// public void testAdd() { -// User user = RedisManage.get("huang2", User.class); -// if (user == null) { -// user = new User("hi", 10, new Date()); -// RedisManage.set("huang2", user); -// user = RedisManage.get("huang2", User.class); -// } -// System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); -// -// User nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// -// RedisManage.del("huang2"); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// if (nowUser == null) { -// System.out.println("node2 nowUser is null"); -// } else { -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); -// -// } -// -// @Test -// public void testPub() { -// for (int i = 0; i < 20; i++) { -// User user = new User("pub" + i, 10, new Date()); -// RedisManage.publish("channel1", user); -// RedisManage.publish("channel2", user); -// } -// } -// -// @Test -// public void testSub() { -// RedisManage.subscribe(new MessageListener() { -// @Override -// public void onMessage(String channel, String message) { -// System.out.printf("on message channel=%s, message=%s%n", channel, message); -// } -// }, "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// @Test -// public void testSub2() { -// RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// -//} +package com.shinemo.mpush.tools.redis; + +import java.util.Date; +import java.util.List; + +import com.shinemo.mpush.tools.redis.listener.MessageListener; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; +import org.junit.Test; + +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ServerApp; + +public class RedisGroupManageTest { + + ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); + List groupList = null; + + RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); + RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + + @Before + public void init() { + groupList = redisRegister.getGroupList(); + } + + @Test + public void testGetRedisGroup() { + for (RedisGroup group : groupList) { + for (RedisNode node : group.getRedisNodeList()) { + System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); + } + + } + } + + @Test + public void testAdd() { + User user = RedisManage.get("huang2", User.class); + if (user == null) { + user = new User("hi", 10, new Date()); + RedisManage.set("huang2", user); + user = RedisManage.get("huang2", User.class); + } + System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); + + User nowUser = RedisUtil.get(node, "huang2", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.get(node2, "huang2", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisManage.del("huang2"); + + nowUser = RedisUtil.get(node2, "huang2", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.get(node, "huang2", User.class); + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + + } + + @Test + public void testPub() { + for (int i = 0; i < 20; i++) { + User user = new User("pub" + i, 10, new Date()); + RedisManage.publish("channel1", user); + RedisManage.publish("channel2", user); + } + } + + @Test + public void testSub() { + RedisManage.subscribe(new MessageListener() { + @Override + public void onMessage(String channel, String message) { + System.out.printf("on message channel=%s, message=%s%n", channel, message); + } + }, "channel1", "channel2"); + try { + Thread.sleep(Integer.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void testSub2() { + RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); + try { + Thread.sleep(Integer.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java index 07367fd8..3edbd454 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java @@ -1,177 +1,176 @@ -//package com.shinemo.mpush.tools.redis; -// -//import java.util.Date; -//import java.util.Iterator; -//import java.util.List; -//import java.util.Map; -// -//import org.apache.commons.lang3.builder.ToStringBuilder; -//import org.junit.Test; -// -//import com.google.common.collect.Lists; -//import com.shinemo.mpush.tools.zk.manage.ServerAppManage; -// -//import redis.clients.jedis.Jedis; -// -//public class RedisUtilTest { -// -// -// -// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); -// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); -// -// List nodeList = Lists.newArrayList(node, node2); -// -// @Test -// public void testAddAndGetAndDelete() { -// Jedis jedis = RedisUtil.getClient(node2); -// jedis.set("hi", "huang"); -// -// String ret = jedis.get("hi"); -// System.out.println(ret); -// -// jedis.del("hi"); -// ret = jedis.get("hi"); -// if (ret == null) { -// System.out.println("ret is null"); -// } else { -// System.out.println("ret is not null:" + ret); -// } -// -// } -// -// @Test -// public void testJedisPool() { -// // 最大连接数是8,因此,获取10个链接会抛错误 -// List jedisList = Lists.newArrayList(); -// for (int i = 0; i < 10; i++) { -// Jedis jedis = RedisUtil.getClient(node); -// jedisList.add(jedis); -// } -// } -// -// @Test -// public void testJedisPool2() { -// // 最大连接数是8,因此,获取10个链接会抛错误 -// List jedisList = Lists.newArrayList(); -// for (int i = 1; i <= 8; i++) { -// Jedis jedis = RedisUtil.getClient(node); -// jedisList.add(jedis); -// } -// -// System.out.println(jedisList.size()); -// -// try { -// Jedis jedis = RedisUtil.getClient(node); -// jedisList.add(jedis); -// System.out.println("first get jedis success"); -// } catch (Exception e) { -// System.out.println(e); -// } -// -// // 关闭一个链接 -// RedisUtil.close(jedisList.get(0)); -// -// try { -// Jedis jedis = RedisUtil.getClient(node); -// jedisList.add(jedis); -// System.out.println("second get jedis success"); -// } catch (Exception e) { -// System.out.println(e); -// } -// -// System.out.println(jedisList.size()); -// } -// -// @Test -// public void testKV() { -// User user = new User("huang", 18, new Date()); -// RedisUtil.set(nodeList, "test", user); -// -// User nowUser = RedisUtil.get(node, "test", User.class); -// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); -// -// nowUser = RedisUtil.get(node2, "test", User.class); -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// -// RedisUtil.del(nodeList, "test"); -// -// nowUser = RedisUtil.get(node2, "test", User.class); -// if (nowUser == null) { -// System.out.println("node2 nowUser is null"); -// } else { -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.get(node, "test", User.class); -// if (nowUser == null) { -// System.out.println("node nowUser is null"); -// } else { -// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// RedisUtil.set(nodeList, "test", user, 10); -// -// try { -// Thread.sleep(12000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// nowUser = RedisUtil.get(node2, "test", User.class); -// if (nowUser == null) { -// System.out.println("node2 nowUser is null"); -// } else { -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.get(node, "test", User.class); -// if (nowUser == null) { -// System.out.println("node nowUser is null"); -// } else { -// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// } -// -// @Test -// public void hashTest(){ -// -// User user = new User("huang", 18, new Date()); -// -// RedisUtil.hset(nodeList, "hashhuang", "hi", user); -// -// User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); -// System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); -// -// nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); -// System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); -// -// Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); -// Iterator> iterator = ret.entrySet().iterator(); -// while (iterator.hasNext()) { -// Map.Entry entry = iterator.next(); -// String key = entry.getKey(); -// User val = entry.getValue(); -// System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); -// } -// -// RedisUtil.hdel(nodeList, "hashhuang", "hi"); -// -// nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); -// if(nowUser==null){ -// System.out.println("node2 nowUser is null"); -// }else{ -// System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); -// if(nowUser==null){ -// System.out.println("node nowUser is null"); -// }else{ -// System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); -// } -// -// } -// -//} +package com.shinemo.mpush.tools.redis; + +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; + +import com.google.common.collect.Lists; + +import redis.clients.jedis.Jedis; + +public class RedisUtilTest { + + + + RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); + RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + + List nodeList = Lists.newArrayList(node, node2); + + @Test + public void testAddAndGetAndDelete() { + Jedis jedis = RedisUtil.getClient(node2); + jedis.set("hi", "huang"); + + String ret = jedis.get("hi"); + System.out.println(ret); + + jedis.del("hi"); + ret = jedis.get("hi"); + if (ret == null) { + System.out.println("ret is null"); + } else { + System.out.println("ret is not null:" + ret); + } + + } + + @Test + public void testJedisPool() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 0; i < 10; i++) { + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + } + } + + @Test + public void testJedisPool2() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 1; i <= 8; i++) { + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + } + + System.out.println(jedisList.size()); + + try { + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + System.out.println("first get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + // 关闭一个链接 + RedisUtil.close(jedisList.get(0)); + + try { + Jedis jedis = RedisUtil.getClient(node); + jedisList.add(jedis); + System.out.println("second get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + System.out.println(jedisList.size()); + } + + @Test + public void testKV() { + User user = new User("huang", 18, new Date()); + RedisUtil.set(nodeList, "test", user); + + User nowUser = RedisUtil.get(node, "test", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.get(node2, "test", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisUtil.del(nodeList, "test"); + + nowUser = RedisUtil.get(node2, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + RedisUtil.set(nodeList, "test", user, 10); + + try { + Thread.sleep(12000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + nowUser = RedisUtil.get(node2, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void hashTest(){ + + User user = new User("huang", 18, new Date()); + + RedisUtil.hset(nodeList, "hashhuang", "hi", user); + + User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); + System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); + System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + + Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); + Iterator> iterator = ret.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + User val = entry.getValue(); + System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); + } + + RedisUtil.hdel(nodeList, "hashhuang", "hi"); + + nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); + if(nowUser==null){ + System.out.println("node2 nowUser is null"); + }else{ + System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); + if(nowUser==null){ + System.out.println("node nowUser is null"); + }else{ + System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); + } + + } + +} From 99f9b9fb275a62890336b947c43e495a5f13b32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 12:54:03 +0800 Subject: [PATCH 157/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/AbstractServer.java | 30 +++++++++++-------- .../impl/ConnectionServerPathListener.java | 6 ++-- .../listener/impl/PushServerPathListener.java | 6 ++-- .../com/shinemo/mpush/tools/zk/ZKPath.java | 7 +++-- .../tools/zk/listener/DataChangeListener.java | 4 +-- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index c2e0b8d0..42900a14 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -7,7 +7,6 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; @@ -53,13 +52,13 @@ public void registerListener(DataChangeListener listener){ } //step1 启动 zk - public void initZK(){ + private void initZK(){ zkRegister = ServiceContainer.getInstance(ZkRegister.class); zkRegister.init(); } //step2 获取redis - public void initRedis(){ + private void initRedis(){ boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); if (!exist) { List groupList = ConfigCenter.holder.redisGroups(); @@ -67,25 +66,30 @@ public void initRedis(){ } } - //step3 初始化 listener data - public void initListenerData(){ + //step3 注册listener + private void registerListeners(){ + for(DataChangeListener listener:dataChangeListeners){ + zkRegister.registerListener(listener); + } + } + + //step4 初始化 listener data + private void initListenerData(){ for(DataChangeListener listener:dataChangeListeners){ listener.initData(); } } - //step4 初始化server - public void initServer(){ + //step5 初始化server + private void initServer(){ server = getServer(); } - //step4 启动 netty server - public void startServer(){ + //step6 启动 netty server + private void startServer(){ ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { - final int port = ConfigCenter.holder.connectionServerPort(); - ConnectionServer server = new ConnectionServer(port); server.init(); server.start(new Server.Listener() { @Override @@ -104,7 +108,7 @@ public void onFailure(String message) { } - //step5 注册应用到zk + //step7 注册应用到zk public void registerServerToZk(){ ServerApp app = new ServerApp(MPushUtil.getLocalIp(), application.getPort()); zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(app)); @@ -113,7 +117,9 @@ public void registerServerToZk(){ public void start(){ initZK(); initRedis(); + registerListeners(); initListenerData(); + initServer(); startServer(); registerServerToZk(); Runtime.getRuntime().addShutdownHook(new Thread() { diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java index 1221333b..aad07d74 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -31,9 +31,9 @@ public class ConnectionServerPathListener extends DataChangeListener{ @Override public void initData() { - log.warn("start init app data"); + log.warn("start init connection server data"); _initData(); - log.warn("end init app data"); + log.warn("end init connection server data"); } private void _initData() { @@ -84,7 +84,7 @@ public void dataChanged(CuratorFramework client, TreeCacheEvent event, String pa @Override public String listenerPath() { - return null; + return ZKPath.CONNECTION_SERVER.getWatchPath(); } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java index 57f05010..454144fc 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java @@ -7,6 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; /** @@ -19,7 +20,8 @@ public class PushServerPathListener extends DataChangeListener{ @Override public void initData() { - + log.warn("start init push server data"); + log.warn("end init push server data"); } @Override @@ -33,7 +35,7 @@ public void dataChanged(CuratorFramework client, TreeCacheEvent event, String pa @Override public String listenerPath() { - return null; + return ZKPath.CONNECTION_SERVER.getWatchPath(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java index baa2a793..e67566a1 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java @@ -4,9 +4,10 @@ import org.apache.curator.utils.ZKPaths; public enum ZKPath { - REDIS_SERVER("/redis", "machine", "连接服务器redis注册的地方"), - CONNECTION_SERVER("/cs/hosts", "machine", "连接服务器应用注册的路径"), - GATEWAY_SERVER("/gs/hosts", "machine", "连接服务器应用注册的路径"); + REDIS_SERVER("/redis", "machine", "redis注册的地方"), + CONNECTION_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), + PUSH_SERVER("/ps/hosts", "machine", "push server服务器应用注册的路径"), + GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"); ZKPath(String path, String name, String desc) { this.path = path; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java index c78ecfe3..cd71dd02 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java @@ -17,9 +17,9 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc return; } - log.warn("DataChangeListener:"+path); + log.warn("DataChangeListener:"+path+",listenerPath:"+listenerPath()); - if(listenerPath().equals(path)){ + if(path.startsWith(listenerPath())){ dataChanged(client, event, path); } } From fd63769027aa6288d8b87f33d2af71df65a84aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 13:08:17 +0800 Subject: [PATCH 158/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81,?= =?UTF-8?q?=E4=BD=BF=E7=94=A8application?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/AbstractServer.java | 6 +-- .../com/shinemo/mpush/core/Application.java | 12 +++++- .../mpush/cs/ConnectionServerApplication.java | 2 + .../test/java/com/shinemo/mpush/AppTest.java | 38 ------------------- .../ConnectionServerApplicationTest.java | 21 ++++++++++ 5 files changed, 35 insertions(+), 44 deletions(-) delete mode 100644 mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java create mode 100644 mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index 42900a14..ec94c1b6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -9,12 +9,10 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; @@ -53,7 +51,6 @@ public void registerListener(DataChangeListener listener){ //step1 启动 zk private void initZK(){ - zkRegister = ServiceContainer.getInstance(ZkRegister.class); zkRegister.init(); } @@ -110,8 +107,7 @@ public void onFailure(String message) { //step7 注册应用到zk public void registerServerToZk(){ - ServerApp app = new ServerApp(MPushUtil.getLocalIp(), application.getPort()); - zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(app)); + zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(application)); } public void start(){ diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java b/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java index b21837df..aed0aab6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java @@ -7,9 +7,11 @@ */ public abstract class Application { + private String ip; + private int port; - private String serverRegisterZkPath; + private transient String serverRegisterZkPath; public int getPort() { return port; @@ -26,5 +28,13 @@ public String getServerRegisterZkPath() { public void setServerRegisterZkPath(String serverRegisterZkPath) { this.serverRegisterZkPath = serverRegisterZkPath; } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index 8f2ebba4..98da8900 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.cs; import com.shinemo.mpush.core.Application; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; @@ -10,6 +11,7 @@ public class ConnectionServerApplication extends Application{ public ConnectionServerApplication() { setPort(ConfigCenter.holder.connectionServerPort()); setServerRegisterZkPath(ZKPath.CONNECTION_SERVER.getWatchPath()); + setIp(MPushUtil.getLocalIp()); } } diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java deleted file mode 100644 index 50fff9a1..00000000 --- a/mpush-cs/src/test/java/com/shinemo/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.shinemo.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java new file mode 100644 index 00000000..f4054582 --- /dev/null +++ b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush; + +import org.junit.Test; + +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.tools.Jsons; + +public class ConnectionServerApplicationTest { + + @Test + public void testJson(){ + + ConnectionServerApplication application = new ConnectionServerApplication(); + + String str = Jsons.toJson(application); + + System.out.println(str); + + } + +} From d15cd4fa7b42551c51eccfaff49dc7ef5f85f908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 13:14:10 +0800 Subject: [PATCH 159/890] =?UTF-8?q?=E6=8A=BD=E5=8F=96servermanage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/cs/GatewayServerApplication.java | 16 ++++++++++++++++ .../com/shinemo/mpush/cs/manage/RedisManage.java | 7 ------- .../{PushServerManage.java => ServerManage.java} | 4 ++-- .../{ => impl}/ConnectionServerManage.java | 10 +++------- .../mpush/cs/manage/impl/PushServerManage.java | 9 +++++++++ .../mpush/cs/manage/impl/RedisServerManage.java | 10 ++++++++++ 6 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java rename mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/{PushServerManage.java => ServerManage.java} (58%) rename mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/{ => impl}/ConnectionServerManage.java (82%) create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java new file mode 100644 index 00000000..55bbf057 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.cs; + +import com.shinemo.mpush.core.Application; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.zk.ZKPath; + +public class GatewayServerApplication extends Application{ + + public GatewayServerApplication() { + setPort(ConfigCenter.holder.gatewayServerPort()); + setServerRegisterZkPath(ZKPath.GATEWAY_SERVER.getWatchPath()); + setIp(MPushUtil.getLocalIp()); + } + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java deleted file mode 100644 index 68f2a025..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/RedisManage.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.shinemo.mpush.cs.manage; - -public class RedisManage { - - - -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java similarity index 58% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java rename to mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java index bf560861..7a7263d0 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/PushServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.cs.manage; -public class PushServerManage { +public interface ServerManage { + - } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java similarity index 82% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java rename to mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java index 1d4a4027..afc057ab 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ConnectionServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs.manage; +package com.shinemo.mpush.cs.manage.impl; import java.util.Collection; import java.util.Collections; @@ -10,19 +10,15 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Maps; +import com.shinemo.mpush.cs.manage.ServerManage; import com.shinemo.mpush.tools.zk.ServerApp; -public class ConnectionServerManage { +public class ConnectionServerManage implements ServerManage{ private static final Logger log = LoggerFactory.getLogger(ConnectionServerManage.class); private static Map holder = Maps.newConcurrentMap(); - public static final ConnectionServerManage instance = new ConnectionServerManage(); - - private ConnectionServerManage() { - } - public void addOrUpdate(String fullPath,ServerApp app){ holder.put(fullPath, app); printAppList(); diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java new file mode 100644 index 00000000..9e181aea --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java @@ -0,0 +1,9 @@ +package com.shinemo.mpush.cs.manage.impl; + +import com.shinemo.mpush.cs.manage.ServerManage; + +public class PushServerManage implements ServerManage{ + + + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java new file mode 100644 index 00000000..27b7708f --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java @@ -0,0 +1,10 @@ +package com.shinemo.mpush.cs.manage.impl; + +import com.shinemo.mpush.cs.manage.ServerManage; + +public class RedisServerManage implements ServerManage{ + + + + +} From dfb18f7ccf3f693a786cf50a802aad7dc8293923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 14:02:23 +0800 Subject: [PATCH 160/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/cs/manage/ServerManage.java | 13 ++- .../manage/impl/ConnectionServerManage.java | 23 ++--- .../cs/manage/impl/GatewayServerManage.java | 45 ++++++++++ .../cs/manage/impl/PushServerManage.java | 21 ++++- .../cs/manage/impl/RedisServerManage.java | 10 --- .../listener/AbstractDataChangeListener.java | 83 +++++++++++++++++++ .../impl/ConnectionServerPathListener.java | 82 +++--------------- .../impl/GatewayServerPathListener.java | 36 ++++++++ .../listener/impl/PushServerPathListener.java | 2 +- .../com.shinemo.mpush.cs.manage.ServiceManage | 3 + 10 files changed, 225 insertions(+), 93 deletions(-) create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java create mode 100644 mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java index 7a7263d0..2f1dde37 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java @@ -1,7 +1,16 @@ package com.shinemo.mpush.cs.manage; -public interface ServerManage { +import java.util.Collection; - +import com.shinemo.mpush.tools.spi.SPI; + +@SPI("redisServerManage") +public interface ServerManage { + + public void addOrUpdate(String fullPath, T application); + + public void remove(String fullPath); + + public Collection getList(); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java index afc057ab..4916137f 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java @@ -10,31 +10,34 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Maps; +import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.cs.manage.ServerManage; -import com.shinemo.mpush.tools.zk.ServerApp; -public class ConnectionServerManage implements ServerManage{ +public class ConnectionServerManage implements ServerManage{ private static final Logger log = LoggerFactory.getLogger(ConnectionServerManage.class); - private static Map holder = Maps.newConcurrentMap(); + private static Map holder = Maps.newConcurrentMap(); - public void addOrUpdate(String fullPath,ServerApp app){ - holder.put(fullPath, app); - printAppList(); + @Override + public void addOrUpdate(String fullPath,ConnectionServerApplication application){ + holder.put(fullPath, application); + printList(); } + @Override public void remove(String fullPath){ holder.remove(fullPath); - printAppList(); + printList(); } - public Collection getAppList() { + @Override + public Collection getList() { return Collections.unmodifiableCollection(holder.values()); } - private void printAppList(){ - for(ServerApp app:holder.values()){ + private void printList(){ + for(ConnectionServerApplication app:holder.values()){ log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java new file mode 100644 index 00000000..985ea98c --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.cs.manage.impl; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.cs.GatewayServerApplication; +import com.shinemo.mpush.cs.manage.ServerManage; + +public class GatewayServerManage implements ServerManage{ + + private static final Logger log = LoggerFactory.getLogger(GatewayServerManage.class); + + private static Map holder = Maps.newConcurrentMap(); + + @Override + public void addOrUpdate(String fullPath,GatewayServerApplication application){ + holder.put(fullPath, application); + printList(); + } + + @Override + public void remove(String fullPath){ + holder.remove(fullPath); + printList(); + } + + @Override + public Collection getList() { + return Collections.unmodifiableCollection(holder.values()); + } + + private void printList(){ + for(GatewayServerApplication app:holder.values()){ + log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + } + } + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java index 9e181aea..7fc5aab1 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java @@ -1,9 +1,28 @@ package com.shinemo.mpush.cs.manage.impl; +import java.util.Collection; + import com.shinemo.mpush.cs.manage.ServerManage; public class PushServerManage implements ServerManage{ - + + @Override + public void addOrUpdate(String fullPath, Object application) { + // TODO Auto-generated method stub + + } + + @Override + public void remove(String fullPath) { + // TODO Auto-generated method stub + + } + + @Override + public Collection getList() { + // TODO Auto-generated method stub + return null; + } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java deleted file mode 100644 index 27b7708f..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/RedisServerManage.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.shinemo.mpush.cs.manage.impl; - -import com.shinemo.mpush.cs.manage.ServerManage; - -public class RedisServerManage implements ServerManage{ - - - - -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java new file mode 100644 index 00000000..d11a8fef --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java @@ -0,0 +1,83 @@ +package com.shinemo.mpush.cs.zk.listener; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.cs.manage.ServerManage; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; + +public abstract class AbstractDataChangeListener extends DataChangeListener { + + protected static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + + private static final Logger log = LoggerFactory.getLogger(AbstractDataChangeListener.class); + + public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData(),null); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData(),null); + } else { + log.warn(this.getClass().getSimpleName()+"other path:" + path + "," + event.getType().name() + "," + data); + } + } + + public void initData() { + log.warn("start init"+ this.getClass().getSimpleName()+"server data"); + _initData(); + log.warn("end init"+ this.getClass().getSimpleName()+"server data"); + } + + public abstract String getRegisterPath(); + + public abstract ServerManage getServerManage(); + + private void _initData() { + // 获取机器列表 + List rawData = zkRegister.getChildrenKeys(getRegisterPath()); + for (String raw : rawData) { + String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); + T app = getServerApplication(fullPath,null); + getServerManage().addOrUpdate(fullPath, app); + } + } + + private void dataRemove(ChildData data) { + String path = data.getPath(); + getServerManage().remove(path); + } + + private void dataAddOrUpdate(ChildData data,Class clazz) { + String path = data.getPath(); + byte[] rawData = data.getData(); + T serverApp = Jsons.fromJson(rawData, clazz); + getServerManage().addOrUpdate(path, serverApp); + } + + private T getServerApplication(String fullPath,Class clazz) { + String rawApp = zkRegister.get(fullPath); + T app = Jsons.fromJson(rawApp,clazz); + return app; + } + + + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java index aad07d74..a06cd85d 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -1,90 +1,34 @@ package com.shinemo.mpush.cs.zk.listener.impl; -import java.util.List; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.cs.manage.ConnectionServerManage; -import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.cs.manage.ServerManage; +import com.shinemo.mpush.cs.zk.listener.AbstractDataChangeListener; import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; /** * connection server 应用 监控 * */ -public class ConnectionServerPathListener extends DataChangeListener{ +public class ConnectionServerPathListener extends AbstractDataChangeListener{ + + @SuppressWarnings("unchecked") + private static ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class, "connectionServerManage"); - private static final Logger log = LoggerFactory.getLogger(ConnectionServerPathListener.class); - - private static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - @Override - public void initData() { - log.warn("start init connection server data"); - _initData(); - log.warn("end init connection server data"); - } - - private void _initData() { - // 获取机器列表 - List rawData = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); - for (String raw : rawData) { - String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); - ServerApp app = getServerApp(fullPath); - ConnectionServerManage.instance.addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data) { - String path = data.getPath(); - ConnectionServerManage.instance.remove(path); - } - - private void dataAddOrUpdate(ChildData data) { - String path = data.getPath(); - byte[] rawData = data.getData(); - ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class); - ConnectionServerManage.instance.addOrUpdate(path, serverApp); - } - - private ServerApp getServerApp(String fullPath) { - String rawApp = zkRegister.get(fullPath); - ServerApp app = Jsons.fromJson(rawApp, ServerApp.class); - return app; + public String listenerPath() { + return ZKPath.CONNECTION_SERVER.getWatchPath(); } @Override - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn("ConnPathListener other path:" + path + "," + event.getType().name() + "," + data); - } - + public String getRegisterPath() { + return ZKPath.CONNECTION_SERVER.getPath(); } @Override - public String listenerPath() { - return ZKPath.CONNECTION_SERVER.getWatchPath(); + public ServerManage getServerManage() { + return connectionServerManage; } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java new file mode 100644 index 00000000..a1acc438 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java @@ -0,0 +1,36 @@ +package com.shinemo.mpush.cs.zk.listener.impl; + +import com.shinemo.mpush.cs.GatewayServerApplication; +import com.shinemo.mpush.cs.manage.ServerManage; +import com.shinemo.mpush.cs.zk.listener.AbstractDataChangeListener; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ZKPath; + +/** + * connection server 应用 监控 + * + */ +public class GatewayServerPathListener extends AbstractDataChangeListener{ + + @SuppressWarnings("unchecked") + private static ServerManage gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); + + + @Override + public String listenerPath() { + return ZKPath.GATEWAY_SERVER.getWatchPath(); + } + + @Override + public ServerManage getServerManage() { + return gatewayServerManage; + } + + @Override + public String getRegisterPath() { + return ZKPath.GATEWAY_SERVER.getPath(); + } + + + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java index 454144fc..66519baa 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java @@ -35,7 +35,7 @@ public void dataChanged(CuratorFramework client, TreeCacheEvent event, String pa @Override public String listenerPath() { - return ZKPath.CONNECTION_SERVER.getWatchPath(); + return ZKPath.PUSH_SERVER.getWatchPath(); } diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage new file mode 100644 index 00000000..3d4f391f --- /dev/null +++ b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage @@ -0,0 +1,3 @@ +gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage +pushServerManage=com.shinemo.mpush.cs.manage.impl.PushServerManage +connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage \ No newline at end of file From 3f34edc6e8052aad98fbedd43a68dbe43da7e0e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 14:06:28 +0800 Subject: [PATCH 161/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zk/listener/AbstractDataChangeListener.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java index d11a8fef..bd4cbb5b 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java @@ -11,18 +11,27 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.core.Application; import com.shinemo.mpush.cs.manage.ServerManage; +import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; -public abstract class AbstractDataChangeListener extends DataChangeListener { +public abstract class AbstractDataChangeListener extends DataChangeListener { protected static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); private static final Logger log = LoggerFactory.getLogger(AbstractDataChangeListener.class); + + private Class clazz; + + @SuppressWarnings("unchecked") + public AbstractDataChangeListener() { + clazz = (Class) GenericsUtil.getSuperClassGenericType(this.getClass(), 0); + } public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { String data = ""; @@ -30,11 +39,11 @@ public void dataChanged(CuratorFramework client, TreeCacheEvent event, String pa data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); } if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData(),null); + dataAddOrUpdate(event.getData(),clazz); } else if (Type.NODE_REMOVED == event.getType()) { dataRemove(event.getData()); } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData(),null); + dataAddOrUpdate(event.getData(),clazz); } else { log.warn(this.getClass().getSimpleName()+"other path:" + path + "," + event.getType().name() + "," + data); } @@ -55,7 +64,7 @@ private void _initData() { List rawData = zkRegister.getChildrenKeys(getRegisterPath()); for (String raw : rawData) { String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); - T app = getServerApplication(fullPath,null); + T app = getServerApplication(fullPath,clazz); getServerManage().addOrUpdate(fullPath, app); } } From d568f9e54f4d1ac901047a07742c559a319dbdc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 14:18:12 +0800 Subject: [PATCH 162/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/core/AbstractServer.java | 13 ++++++------- .../com/shinemo/mpush/cs/ConnectionServerMain.java | 7 ++++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index ec94c1b6..dda52751 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -118,13 +118,12 @@ public void start(){ initServer(); startServer(); registerServerToZk(); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - if (server != null) { - server.stop(null); - } - } - }); + } + + public void stop(){ + if(server!=null){ + server.stop(null); + } } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 528df5f9..7101cc61 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -29,8 +29,13 @@ public Server getServer() { public static void main(String[] args) { - ConnectionServerMain connectionServerMain = new ConnectionServerMain(); + final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); connectionServerMain.start(); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + connectionServerMain.stop(); + } + }); } } From 77fa2d10c31bc837b3fa3cdb2aa4274614db708e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 14:29:07 +0800 Subject: [PATCH 163/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-cs/pom.xml | 56 +++++++++++++++++++ .../impl/ConnectionServerPathListener.java | 2 +- .../impl/GatewayServerPathListener.java | 2 +- .../zk/listener/impl/RedisPathListener.java | 4 +- mpush-tools/pom.xml | 5 +- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 4c252db7..c188428b 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -22,4 +22,60 @@ mpush-core + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.4.2 + + true + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-cs + + + + + META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage + + + + + + + + + diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java index a06cd85d..c4563fa4 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -14,7 +14,7 @@ public class ConnectionServerPathListener extends AbstractDataChangeListener{ @SuppressWarnings("unchecked") - private static ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class, "connectionServerManage"); + private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class, "connectionServerManage"); @Override public String listenerPath() { diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java index a1acc438..73438fae 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java @@ -13,7 +13,7 @@ public class GatewayServerPathListener extends AbstractDataChangeListener{ @SuppressWarnings("unchecked") - private static ServerManage gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); + private ServerManage gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); @Override diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java index 0217e12d..a9f358bf 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java @@ -28,9 +28,9 @@ public class RedisPathListener extends DataChangeListener { private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); - private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + private final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + private final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); // 获取redis列表 private void _initData() { diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 883f5d2f..3fdbb0ae 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -95,7 +95,10 @@ - META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService + META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister + + + META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister From dda4738dbfdfaed4ab87f1acd8ce6184322c38f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 16:00:41 +0800 Subject: [PATCH 164/890] add server manage spi --- mpush-cs/pom.xml | 5 - .../shinemo/mpush/cs/manage/ServerManage.java | 2 +- .../impl/ConnectionServerPathListener.java | 2 +- .../com.shinemo.mpush.cs.manage.ServerManage | 1 + .../com.shinemo.mpush.cs.manage.ServiceManage | 3 - mpush-tools/pom.xml | 8 -- .../mpush/tools/spi/ServiceContainer.java | 95 +++++++++++++------ .../shinemo/mpush/tools/zk/ZkRegister.java | 2 +- .../curator/services/ZkRegisterManager.java | 2 +- 9 files changed, 70 insertions(+), 50 deletions(-) create mode 100644 mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage delete mode 100644 mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index c188428b..65b2f14c 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -66,11 +66,6 @@ com.shinemo.mpush:mpush-cs - - - META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage - - diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java index 2f1dde37..35901298 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java @@ -4,7 +4,7 @@ import com.shinemo.mpush.tools.spi.SPI; -@SPI("redisServerManage") +@SPI("connectionServerManage") public interface ServerManage { public void addOrUpdate(String fullPath, T application); diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java index c4563fa4..fbd0423a 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -14,7 +14,7 @@ public class ConnectionServerPathListener extends AbstractDataChangeListener{ @SuppressWarnings("unchecked") - private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class, "connectionServerManage"); + private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); @Override public String listenerPath() { diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage new file mode 100644 index 00000000..3f7eef2c --- /dev/null +++ b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage @@ -0,0 +1 @@ +connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage \ No newline at end of file diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage deleted file mode 100644 index 3d4f391f..00000000 --- a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServiceManage +++ /dev/null @@ -1,3 +0,0 @@ -gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage -pushServerManage=com.shinemo.mpush.cs.manage.impl.PushServerManage -connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage \ No newline at end of file diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 3fdbb0ae..e7b42be7 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -93,14 +93,6 @@ com.shinemo.mpush:mpush-tools - - - META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister - - - META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister - - diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java index 927d2d77..88c31504 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java @@ -7,6 +7,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.ServiceLoader; import java.util.Map.Entry; import java.util.concurrent.ConcurrentMap; @@ -27,7 +28,33 @@ public class ServiceContainer { // class -> ( beanId -> beanInstance) private static final ConcurrentMap, ConcurrentMap> objectsCachedMap = Maps.newConcurrentMap(); - + + //暂时不使用这个,但是代码保留 + @Deprecated + public static T load(Class clazz) { + try { + T instance = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()).iterator().next(); + return instance; + } catch (Throwable e) { + log.warn("can not load " + clazz, e); + } + return null; + } + + //暂时不使用这个,但是代码保留 + @Deprecated + public static List loadList(Class classType) { + List list = Lists.newArrayList(); + try { + for (T instance : ServiceLoader.load(classType, ServiceContainer.class.getClassLoader())) { + list.add(instance); + } + return list; + } catch (RuntimeException e) { + throw e; + } + } + public static T getInstance(Class clazz) { if (clazz == null) @@ -51,30 +78,30 @@ public static T getInstance(Class clazz) { } @SuppressWarnings("unchecked") - public static List getInstances(Class clazz){ + public static List getInstances(Class clazz) { ConcurrentMap objMap = objectsCachedMap.get(clazz); if (objMap == null) { synchronized (clazz) { objMap = objectsCachedMap.get(clazz); - if(objMap == null){ + if (objMap == null) { objMap = Maps.newConcurrentMap(); objectsCachedMap.put(clazz, objMap); } } } objMap = objectsCachedMap.get(clazz); - if(!objMap.isEmpty()){ - return Lists.newArrayList((Collection)objMap.values()); + if (!objMap.isEmpty()) { + return Lists.newArrayList((Collection) objMap.values()); } - + initClazzInstances(clazz); - + objMap = objectsCachedMap.get(clazz); - if(!objMap.isEmpty()){ - return Lists.newArrayList((Collection)objMap.values()); + if (!objMap.isEmpty()) { + return Lists.newArrayList((Collection) objMap.values()); } throw new IllegalStateException("Failed getInstance class(interface: " + clazz); - + } @SuppressWarnings("unchecked") @@ -83,7 +110,7 @@ public static T getInstance(Class clazz, String key) { if (objMap == null) { synchronized (clazz) { objMap = objectsCachedMap.get(clazz); - if(objMap==null){ + if (objMap == null) { objMap = Maps.newConcurrentMap(); objectsCachedMap.put(clazz, objMap); } @@ -96,48 +123,48 @@ public static T getInstance(Class clazz, String key) { if (obj != null) { return obj; } - - //初始化所有 + + // 初始化所有 initClazzInstances(clazz); - + obj = (T) objMap.get(key); if (obj != null) { return obj; } - + throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); } - - private static void initClazzInstances(Class clazz){ - + + private static void initClazzInstances(Class clazz) { + Map> clazzMap = getClazzMap(clazz); ConcurrentMap objMap = objectsCachedMap.get(clazz); - if(objMap.isEmpty()){ + if (objMap.isEmpty()) { synchronized (clazz) { objMap = objectsCachedMap.get(clazz); - if(objMap.isEmpty()){ + if (objMap.isEmpty()) { Iterator>> iter = clazzMap.entrySet().iterator(); while (iter.hasNext()) { Entry> entry = iter.next(); - String entryKey = entry.getKey(); - Class val = entry.getValue(); - Object oldObj = objMap.get(entryKey); - if(oldObj==null){ - Object newObj; + String entryKey = entry.getKey(); + Class val = entry.getValue(); + Object oldObj = objMap.get(entryKey); + if (oldObj == null) { + Object newObj; try { newObj = val.newInstance(); objMap.putIfAbsent(entryKey, newObj); } catch (Exception e) { } - } + } } objectsCachedMap.put(clazz, objMap); } } } - + } private static Map> getClazzMap(Class clazz) { @@ -154,13 +181,21 @@ private static void loadFile(Class type) { Map> map = Maps.newHashMap(); try { Enumeration urls; - ClassLoader classLoader = ServiceContainer.class.getClassLoader(); + // ClassLoader classLoader = + // ServiceContainer.class.getClassLoader(); + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader != null) { urls = classLoader.getResources(fileName); } else { urls = ClassLoader.getSystemResources(fileName); } if (urls != null) { + while (!urls.hasMoreElements() && classLoader != null) { + classLoader = classLoader.getParent(); + if (classLoader != null) { + urls = classLoader.getResources(fileName); + } + } while (urls.hasMoreElements()) { java.net.URL url = urls.nextElement(); try { @@ -205,11 +240,11 @@ private static void loadFile(Class type) { } synchronized (type) { Map> oldMap = clazzCacheMap.get(type); - if(oldMap==null){ + if (oldMap == null) { clazzCacheMap.put(type, map); } } - + } public static String toLowerCaseFirstOne(String s) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java index c335076f..def87688 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java @@ -9,7 +9,7 @@ @SPI("zkRegister") -public interface ZkRegister { +public interface ZkRegister { public void init(); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 95097964..495c4cd9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -30,7 +30,7 @@ import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; -public class ZkRegisterManager implements ZkRegister { +public class ZkRegisterManager implements ZkRegister { private static final Logger LOGGER = LoggerFactory.getLogger(ZkRegisterManager.class); From 5283ecfe3f5de571f31783e9ae0fc7e6be38f064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 16:10:41 +0800 Subject: [PATCH 165/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/client/PushClient.java | 20 +- .../shinemo/mpush/common}/Application.java | 2 +- .../shinemo/mpush/core/AbstractServer.java | 1 + .../mpush/core/netty/NettyClientTest.java | 134 +++++------ .../mpush/cs/ConnectionServerApplication.java | 12 +- .../mpush/cs/GatewayServerApplication.java | 2 +- .../listener/AbstractDataChangeListener.java | 2 +- .../shinemo/mpush}/zk/ServerManageTest.java | 0 .../java/com/shinemo/mpush}/zk/ZkTest.java | 2 +- .../com/shinemo/mpush}/zk/ZkUtilTest.java | 7 +- .../com/shinemo/mpush/tools/zk/ServerApp.java | 25 -- .../shinemo/mpush/tools/zk/ZkRegister.java | 2 +- .../curator/services/ZkRegisterManager.java | 2 +- .../tools/redis/RedisGroupManageTest.java | 218 +++++++++--------- 14 files changed, 205 insertions(+), 224 deletions(-) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-common/src/main/java/com/shinemo/mpush/common}/Application.java (94%) rename {mpush-core/src/test/java/com/shinemo/mpush/core => mpush-cs/src/test/java/com/shinemo/mpush}/zk/ServerManageTest.java (100%) rename {mpush-core/src/test/java/com/shinemo/mpush/core => mpush-cs/src/test/java/com/shinemo/mpush}/zk/ZkTest.java (89%) rename {mpush-core/src/test/java/com/shinemo/mpush/core => mpush-cs/src/test/java/com/shinemo/mpush}/zk/ZkUtilTest.java (94%) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java index ab25c23e..f65159fc 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java @@ -4,12 +4,12 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.Application; import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZkRegister; import org.apache.curator.framework.CuratorFramework; @@ -26,16 +26,16 @@ public class PushClient implements PushSender { private int defaultTimeout = 3000; private final Map clientMap = new ConcurrentHashMap<>(); - private final Map servers = new ConcurrentHashMap<>(); + private final Map servers = new ConcurrentHashMap<>(); private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); public void init() throws Exception { initRedisClient(); GatewayServerZKListener listener = new GatewayServerZKListener(); - Collection nodes = listener.getAllServers(); + Collection nodes = listener.getAllServers(); if (nodes == null || nodes.isEmpty()) return; - for (ServerApp server : nodes) { + for (Application server : nodes) { createClient(server.getIp(), server.getPort()); } } @@ -93,13 +93,13 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) if (Strings.isNullOrEmpty(path)) return; if (TreeCacheEvent.Type.NODE_ADDED == event.getType()) { - ServerApp serverApp = getServer(path); + Application serverApp = getServer(path); if (serverApp != null) { createClient(serverApp.getIp(), serverApp.getPort()); servers.put(path, serverApp); } } else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) { - ServerApp serverApp = servers.remove(path); + Application serverApp = servers.remove(path); if (serverApp != null) { Client client = clientMap.remove(serverApp.getIp()); if (client != null) { @@ -111,20 +111,20 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) } } - private ServerApp getServer(String path) { + private Application getServer(String path) { String json = zkRegister.get(path); if (Strings.isNullOrEmpty(json)) return null; - return Jsons.fromJson(json, ServerApp.class); + return Jsons.fromJson(json, Application.class); } - private Collection getAllServers() { + private Collection getAllServers() { List list = zkRegister.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; for (String name : list) { String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name); String json = zkRegister.get(fullPath); if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue; - ServerApp server = Jsons.fromJson(json, ServerApp.class); + Application server = Jsons.fromJson(json, Application.class); if (server != null) { servers.put(fullPath, server); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java b/mpush-common/src/main/java/com/shinemo/mpush/common/Application.java similarity index 94% rename from mpush-core/src/main/java/com/shinemo/mpush/core/Application.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/Application.java index aed0aab6..d78bdd5c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/Application.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/Application.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core; +package com.shinemo.mpush.common; /** diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index dda52751..567ecb9e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -7,6 +7,7 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.api.Server; +import com.shinemo.mpush.common.Application; import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.config.ConfigCenter; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java index 02cbd59e..79501ad2 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java @@ -1,67 +1,67 @@ -package com.shinemo.mpush.core.netty; - - -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.Strings; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ServerApp; -import com.shinemo.mpush.tools.zk.ZkRegister; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Created by ohun on 2015/12/24. - */ -public class NettyClientTest { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); - - private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - public void setUp() throws Exception { - } - - private List getAllServers() { - List list = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); - if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; - List servers = new ArrayList<>(); - for (String name : list) { - String json = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(name)); - if (Strings.isBlank(json)) continue; - ServerApp server = Jsons.fromJson(json, ServerApp.class); - if (server != null) servers.add(server); - } - return servers; - } - - public void testClient() throws Exception { - List serverApps = getAllServers(); - if (serverApps == null || serverApps.isEmpty()) return; - int index = (int) ((Math.random() % serverApps.size()) * serverApps.size()); - ServerApp server = serverApps.get(index); - ClientChannelHandler handler = new ClientChannelHandler(); - final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); - client.init(); - Thread t = new Thread(new Runnable() { - @Override - public void run() { - client.start(); - } - }); - t.setDaemon(false); - t.start(); - } - - public static void main(String[] args) throws Exception { - NettyClientTest test = new NettyClientTest(); - test.setUp(); - test.testClient(); - } -} +//package com.shinemo.mpush.core.netty; +// +// +//import com.shinemo.mpush.api.Client; +//import com.shinemo.mpush.netty.client.NettyClientFactory; +//import com.shinemo.mpush.tools.Jsons; +//import com.shinemo.mpush.tools.Strings; +//import com.shinemo.mpush.tools.spi.ServiceContainer; +//import com.shinemo.mpush.tools.zk.ZKPath; +//import com.shinemo.mpush.tools.zk.ServerApp; +//import com.shinemo.mpush.tools.zk.ZkRegister; +// +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import java.util.ArrayList; +//import java.util.Collections; +//import java.util.List; +// +///** +// * Created by ohun on 2015/12/24. +// */ +//public class NettyClientTest { +// private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); +// +// private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); +// +// public void setUp() throws Exception { +// } +// +// private List getAllServers() { +// List list = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); +// if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; +// List servers = new ArrayList<>(); +// for (String name : list) { +// String json = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(name)); +// if (Strings.isBlank(json)) continue; +// ServerApp server = Jsons.fromJson(json, ServerApp.class); +// if (server != null) servers.add(server); +// } +// return servers; +// } +// +// public void testClient() throws Exception { +// List serverApps = getAllServers(); +// if (serverApps == null || serverApps.isEmpty()) return; +// int index = (int) ((Math.random() % serverApps.size()) * serverApps.size()); +// ServerApp server = serverApps.get(index); +// ClientChannelHandler handler = new ClientChannelHandler(); +// final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); +// client.init(); +// Thread t = new Thread(new Runnable() { +// @Override +// public void run() { +// client.start(); +// } +// }); +// t.setDaemon(false); +// t.start(); +// } +// +// public static void main(String[] args) throws Exception { +// NettyClientTest test = new NettyClientTest(); +// test.setUp(); +// test.testClient(); +// } +//} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index 98da8900..f10b5e94 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.cs; -import com.shinemo.mpush.core.Application; +import com.shinemo.mpush.common.Application; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; @@ -9,9 +9,13 @@ public class ConnectionServerApplication extends Application{ public ConnectionServerApplication() { - setPort(ConfigCenter.holder.connectionServerPort()); - setServerRegisterZkPath(ZKPath.CONNECTION_SERVER.getWatchPath()); - setIp(MPushUtil.getLocalIp()); + this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp()); + } + + public ConnectionServerApplication(int port,String path,String ip) { + setPort(port); + setServerRegisterZkPath(path); + setIp(ip); } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java index 55bbf057..551798cb 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.cs; -import com.shinemo.mpush.core.Application; +import com.shinemo.mpush.common.Application; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java index bd4cbb5b..e02801b8 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java @@ -11,7 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.core.Application; +import com.shinemo.mpush.common.Application; import com.shinemo.mpush.cs.manage.ServerManage; import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ServerManageTest.java similarity index 100% rename from mpush-core/src/test/java/com/shinemo/mpush/core/zk/ServerManageTest.java rename to mpush-cs/src/test/java/com/shinemo/mpush/zk/ServerManageTest.java diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java similarity index 89% rename from mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java rename to mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java index 64936c04..70a8923d 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkTest.java +++ b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.zk; +package com.shinemo.mpush.zk; import org.junit.Test; diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkUtilTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java similarity index 94% rename from mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkUtilTest.java rename to mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java index a23d99d0..bb294d57 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/zk/ZkUtilTest.java +++ b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.zk; +package com.shinemo.mpush.zk; import java.util.List; @@ -8,12 +8,13 @@ import org.junit.Test; import com.google.common.collect.Lists; +import com.shinemo.mpush.core.server.ConnectionServer; +import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; -import com.shinemo.mpush.tools.zk.ServerApp; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager; @@ -100,7 +101,7 @@ public void testLocalIp() { @Test public void testRegisterIp() { String localIp = MPushUtil.getInetAddress(); - ServerApp app = new ServerApp(localIp, 3000); + ConnectionServerApplication app = new ConnectionServerApplication(); zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); String value = zkUtil.get("/" + localIp); System.out.println(value); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java deleted file mode 100644 index cb5d03ab..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ServerApp.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.io.Serializable; - -public class ServerApp implements Serializable { - - private static final long serialVersionUID = 5495972321679092837L; - - private final String ip; - private final int port; - - public ServerApp(String ip, int port) { - this.ip = ip; - this.port = port; - } - - public String getIp() { - return ip; - } - - public int getPort() { - return port; - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java index def87688..c335076f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java @@ -9,7 +9,7 @@ @SPI("zkRegister") -public interface ZkRegister { +public interface ZkRegister { public void init(); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 495c4cd9..95097964 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -30,7 +30,7 @@ import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; -public class ZkRegisterManager implements ZkRegister { +public class ZkRegisterManager implements ZkRegister { private static final Logger LOGGER = LoggerFactory.getLogger(ZkRegisterManager.class); diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java index e03e5f70..77981b8b 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java @@ -1,109 +1,109 @@ -package com.shinemo.mpush.tools.redis; - -import java.util.Date; -import java.util.List; - -import com.shinemo.mpush.tools.redis.listener.MessageListener; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.junit.Before; -import org.junit.Test; - -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -import com.shinemo.mpush.tools.redis.pubsub.Subscriber; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ServerApp; - -public class RedisGroupManageTest { - - ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); - List groupList = null; - - RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); - RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); - - @Before - public void init() { - groupList = redisRegister.getGroupList(); - } - - @Test - public void testGetRedisGroup() { - for (RedisGroup group : groupList) { - for (RedisNode node : group.getRedisNodeList()) { - System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - - @Test - public void testAdd() { - User user = RedisManage.get("huang2", User.class); - if (user == null) { - user = new User("hi", 10, new Date()); - RedisManage.set("huang2", user); - user = RedisManage.get("huang2", User.class); - } - System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); - - User nowUser = RedisUtil.get(node, "huang2", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node2, "huang2", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisManage.del("huang2"); - - nowUser = RedisUtil.get(node2, "huang2", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "huang2", User.class); - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - - } - - @Test - public void testPub() { - for (int i = 0; i < 20; i++) { - User user = new User("pub" + i, 10, new Date()); - RedisManage.publish("channel1", user); - RedisManage.publish("channel2", user); - } - } - - @Test - public void testSub() { - RedisManage.subscribe(new MessageListener() { - @Override - public void onMessage(String channel, String message) { - System.out.printf("on message channel=%s, message=%s%n", channel, message); - } - }, "channel1", "channel2"); - try { - Thread.sleep(Integer.MAX_VALUE); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - @Test - public void testSub2() { - RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); - try { - Thread.sleep(Integer.MAX_VALUE); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - -} +//package com.shinemo.mpush.tools.redis; +// +//import java.util.Date; +//import java.util.List; +// +//import com.shinemo.mpush.tools.redis.listener.MessageListener; +// +//import org.apache.commons.lang3.builder.ToStringBuilder; +//import org.apache.commons.lang3.builder.ToStringStyle; +//import org.junit.Before; +//import org.junit.Test; +// +//import com.shinemo.mpush.tools.MPushUtil; +//import com.shinemo.mpush.tools.redis.manage.RedisManage; +//import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +//import com.shinemo.mpush.tools.spi.ServiceContainer; +//import com.shinemo.mpush.tools.zk.ServerApp; +// +//public class RedisGroupManageTest { +// +// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); +// List groupList = null; +// +// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); +// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); +// +// RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); +// +// @Before +// public void init() { +// groupList = redisRegister.getGroupList(); +// } +// +// @Test +// public void testGetRedisGroup() { +// for (RedisGroup group : groupList) { +// for (RedisNode node : group.getRedisNodeList()) { +// System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); +// } +// +// } +// } +// +// @Test +// public void testAdd() { +// User user = RedisManage.get("huang2", User.class); +// if (user == null) { +// user = new User("hi", 10, new Date()); +// RedisManage.set("huang2", user); +// user = RedisManage.get("huang2", User.class); +// } +// System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); +// +// User nowUser = RedisUtil.get(node, "huang2", User.class); +// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); +// +// nowUser = RedisUtil.get(node2, "huang2", User.class); +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// +// RedisManage.del("huang2"); +// +// nowUser = RedisUtil.get(node2, "huang2", User.class); +// if (nowUser == null) { +// System.out.println("node2 nowUser is null"); +// } else { +// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); +// } +// +// nowUser = RedisUtil.get(node, "huang2", User.class); +// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); +// +// } +// +// @Test +// public void testPub() { +// for (int i = 0; i < 20; i++) { +// User user = new User("pub" + i, 10, new Date()); +// RedisManage.publish("channel1", user); +// RedisManage.publish("channel2", user); +// } +// } +// +// @Test +// public void testSub() { +// RedisManage.subscribe(new MessageListener() { +// @Override +// public void onMessage(String channel, String message) { +// System.out.printf("on message channel=%s, message=%s%n", channel, message); +// } +// }, "channel1", "channel2"); +// try { +// Thread.sleep(Integer.MAX_VALUE); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// +// @Test +// public void testSub2() { +// RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); +// try { +// Thread.sleep(Integer.MAX_VALUE); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// +// +//} From bd28c09a56d7843a7276e51f6fe0c3e7c5afc7f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 16:15:02 +0800 Subject: [PATCH 166/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/core/AbstractServer.java | 2 ++ .../main/java/com/shinemo/mpush/cs/ConnectionServerMain.java | 2 +- .../mpush/services/com.shinemo.mpush.cs.manage.ServerManage | 4 +++- .../mpush/tools}/zk/listener/impl/RedisPathListener.java | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-tools/src/main/java/com/shinemo/mpush/tools}/zk/listener/impl/RedisPathListener.java (98%) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index 567ecb9e..17db18d3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -17,6 +17,7 @@ import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; +import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; public abstract class AbstractServer { @@ -32,6 +33,7 @@ public abstract class AbstractServer { public AbstractServer() { this.application = getApplication(); + registerListener(new RedisPathListener()); } @SuppressWarnings("unchecked") diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 7101cc61..443beb1a 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -7,8 +7,8 @@ import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; import com.shinemo.mpush.cs.zk.listener.impl.PushServerPathListener; -import com.shinemo.mpush.cs.zk.listener.impl.RedisPathListener; import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; public class ConnectionServerMain extends AbstractServer{ diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage index 3f7eef2c..7129bddb 100644 --- a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage +++ b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage @@ -1 +1,3 @@ -connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage \ No newline at end of file +connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage +gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage +pushServerManage=com.shinemo.mpush.cs.manage.impl.PushServerManage \ No newline at end of file diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java similarity index 98% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java index a9f358bf..5cc4666a 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/RedisPathListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs.zk.listener.impl; +package com.shinemo.mpush.tools.zk.listener.impl; import java.util.Collections; import java.util.List; From c3b1bfe8be5992d18b344e35e8df12fedd6835a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 17:01:43 +0800 Subject: [PATCH 167/890] =?UTF-8?q?gateway=20server=E4=B8=8E=20connection?= =?UTF-8?q?=20server=20=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/AbstractServer.java | 20 +++++---- .../mpush/cs/ConnectionServerApplication.java | 9 ++++ .../mpush/cs/ConnectionServerMain.java | 43 ++++++++++++------- .../mpush/cs/GatewayServerApplication.java | 18 +++++--- .../main/java/com/shinemo/mpush/cs/Main.java | 15 +++++++ .../cs/manage/impl/PushServerManage.java | 28 ------------ .../listener/impl/PushServerPathListener.java | 42 ------------------ 7 files changed, 75 insertions(+), 100 deletions(-) create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java index 17db18d3..fe2bff52 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java @@ -86,7 +86,7 @@ private void initServer(){ } //step6 启动 netty server - private void startServer(){ + public void startServer(final Server server){ ThreadPoolUtil.newThread(new Runnable() { @Override public void run() { @@ -94,12 +94,12 @@ public void run() { server.start(new Server.Listener() { @Override public void onSuccess() { - log.error("mpush app start connection server success...."); + log.error("mpush app start "+server.getClass().getSimpleName()+" server success...."); } @Override public void onFailure(String message) { - log.error("mpush app start connection server failure, jvm exit with code -1"); + log.error("mpush app start "+server.getClass().getSimpleName()+" server failure, jvm exit with code -1"); System.exit(-1); } }); @@ -109,8 +109,8 @@ public void onFailure(String message) { } //step7 注册应用到zk - public void registerServerToZk(){ - zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(application)); + public void registerServerToZk(String path,String value){ + zkRegister.registerEphemeralSequential(path, value); } public void start(){ @@ -119,14 +119,18 @@ public void start(){ registerListeners(); initListenerData(); initServer(); - startServer(); - registerServerToZk(); + startServer(server); + registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); } - public void stop(){ + public void stopServer(Server server){ if(server!=null){ server.stop(null); } } + public void stop(){ + stopServer(server); + } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index f10b5e94..bbe9fb5a 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -7,6 +7,7 @@ public class ConnectionServerApplication extends Application{ + private transient GatewayServerApplication gatewayServerApplication; public ConnectionServerApplication() { this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp()); @@ -18,4 +19,12 @@ public ConnectionServerApplication(int port,String path,String ip) { setIp(ip); } + public GatewayServerApplication getGatewayServerApplication() { + return gatewayServerApplication; + } + + public void setGatewayServerApplication(GatewayServerApplication gatewayServerApplication) { + this.gatewayServerApplication = gatewayServerApplication; + } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 443beb1a..639c45bb 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -5,37 +5,50 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.core.AbstractServer; import com.shinemo.mpush.core.server.ConnectionServer; +import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; -import com.shinemo.mpush.cs.zk.listener.impl.PushServerPathListener; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; +import com.shinemo.mpush.cs.zk.listener.impl.GatewayServerPathListener; +import com.shinemo.mpush.tools.Jsons; public class ConnectionServerMain extends AbstractServer{ + private Server gatewayServer; + + private ConnectionServerApplication connectionServerApplication; + + private GatewayServerApplication gatewayServerApplication; + public ConnectionServerMain(){ - registerListener(new RedisPathListener()); - registerListener(new PushServerPathListener()); registerListener(new ConnectionServerPathListener()); + registerListener(new GatewayServerPathListener()); + connectionServerApplication = (ConnectionServerApplication)application; + gatewayServerApplication = new GatewayServerApplication(); + connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); + gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); + } + + @Override + public void start() { + super.start(); + startServer(gatewayServer); + registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); } + @Override public Server getServer() { - final int port = ConfigCenter.holder.connectionServerPort(); + final int port = application.getPort(); ConnectionServer connectionServer = new ConnectionServer(port); return connectionServer; } - - public static void main(String[] args) { - final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); - connectionServerMain.start(); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - connectionServerMain.stop(); - } - }); + @Override + public void stop() { + super.stop(); + stopServer(gatewayServer); } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java index 551798cb..b0ac3ec1 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java @@ -1,16 +1,20 @@ package com.shinemo.mpush.cs; -import com.shinemo.mpush.common.Application; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; -public class GatewayServerApplication extends Application{ - + +public class GatewayServerApplication extends ConnectionServerApplication{ + public GatewayServerApplication() { - setPort(ConfigCenter.holder.gatewayServerPort()); - setServerRegisterZkPath(ZKPath.GATEWAY_SERVER.getWatchPath()); - setIp(MPushUtil.getLocalIp()); + this(ConfigCenter.holder.gatewayServerPort(),ZKPath.GATEWAY_SERVER.getWatchPath(),MPushUtil.getLocalIp()); } - + + public GatewayServerApplication(int port,String path,String ip) { + setPort(port); + setServerRegisterZkPath(path); + setIp(ip); + } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java new file mode 100644 index 00000000..7f7116d5 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.cs; + +public class Main { + + public static void main(String[] args) { + final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); + connectionServerMain.start(); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + connectionServerMain.stop(); + } + }); + } + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java deleted file mode 100644 index 7fc5aab1..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/PushServerManage.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.shinemo.mpush.cs.manage.impl; - -import java.util.Collection; - -import com.shinemo.mpush.cs.manage.ServerManage; - -public class PushServerManage implements ServerManage{ - - @Override - public void addOrUpdate(String fullPath, Object application) { - // TODO Auto-generated method stub - - } - - @Override - public void remove(String fullPath) { - // TODO Auto-generated method stub - - } - - @Override - public Collection getList() { - // TODO Auto-generated method stub - return null; - } - - -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java deleted file mode 100644 index 66519baa..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/PushServerPathListener.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.shinemo.mpush.cs.zk.listener.impl; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; - -/** - * push server 路径监控 - * - */ -public class PushServerPathListener extends DataChangeListener{ - - private static final Logger log = LoggerFactory.getLogger(PushServerPathListener.class); - - @Override - public void initData() { - log.warn("start init push server data"); - log.warn("end init push server data"); - } - - @Override - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - log.warn("ConnPathListener other path:" + path + "," + event.getType().name() + "," + data); - } - - @Override - public String listenerPath() { - return ZKPath.PUSH_SERVER.getWatchPath(); - } - - -} From 04a330a5699815883887cda6a3c78ccdf60c7568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 17:02:26 +0800 Subject: [PATCH 168/890] =?UTF-8?q?gateway=20=E4=B8=8Econnect=20=E9=9B=86?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/services/com.shinemo.mpush.cs.manage.ServerManage | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage index 7129bddb..46f79b39 100644 --- a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage +++ b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage @@ -1,3 +1,2 @@ connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage -gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage -pushServerManage=com.shinemo.mpush.cs.manage.impl.PushServerManage \ No newline at end of file +gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage \ No newline at end of file From d3c63520886ee9a581552a8995aeda6f16975b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 17:15:53 +0800 Subject: [PATCH 169/890] change --- .../mpush/cs/zk/listener/AbstractDataChangeListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java index e02801b8..25b7077d 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java @@ -50,9 +50,9 @@ public void dataChanged(CuratorFramework client, TreeCacheEvent event, String pa } public void initData() { - log.warn("start init"+ this.getClass().getSimpleName()+"server data"); + log.warn("start init "+ this.getClass().getSimpleName()+"server data"); _initData(); - log.warn("end init"+ this.getClass().getSimpleName()+"server data"); + log.warn("end init "+ this.getClass().getSimpleName()+"server data"); } public abstract String getRegisterPath(); From 8f7761f762a5d780d7cd2dc3e6604b3fd2df770c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 17:19:05 +0800 Subject: [PATCH 170/890] add --- mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java index 7f7116d5..67dfeb0f 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java @@ -1,13 +1,19 @@ package com.shinemo.mpush.cs; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class Main { + private static final Logger log = LoggerFactory.getLogger(Main.class); + public static void main(String[] args) { final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); connectionServerMain.start(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { connectionServerMain.stop(); + log.warn("connection stop success!"); } }); } From 217c110fc02ae64c8b3dd50b8e40c2202f26a7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sat, 16 Jan 2016 17:35:49 +0800 Subject: [PATCH 171/890] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/core/App.java | 130 ------------------ mpush-ps/pom.xml | 11 +- .../src/main/java/com/shinemo/mpush/App.java | 13 -- .../ps}/GatewayClientChannelHandler.java | 2 +- .../com/shinemo/mpush/ps}/PushClient.java | 2 +- .../com/shinemo/mpush/ps}/PushRequest.java | 2 +- .../com/shinemo/mpush/ps}/PushRequestBus.java | 2 +- mpush-ps/src/main/resources/config.properties | 43 ++++++ mpush-ps/src/main/resources/logback.xml | 30 ++++ .../shinemo/mpush/ps/ConfigCenterTest.java | 16 +++ .../com/shinemo/mpush/ps/PushClientTest.java | 49 +++++++ 11 files changed, 151 insertions(+), 149 deletions(-) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/App.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/App.java rename {mpush-client/src/main/java/com/shinemo/mpush/client => mpush-ps/src/main/java/com/shinemo/mpush/ps}/GatewayClientChannelHandler.java (98%) rename {mpush-client/src/main/java/com/shinemo/mpush/client => mpush-ps/src/main/java/com/shinemo/mpush/ps}/PushClient.java (99%) rename {mpush-client/src/main/java/com/shinemo/mpush/client => mpush-ps/src/main/java/com/shinemo/mpush/ps}/PushRequest.java (99%) rename {mpush-client/src/main/java/com/shinemo/mpush/client => mpush-ps/src/main/java/com/shinemo/mpush/ps}/PushRequestBus.java (97%) create mode 100644 mpush-ps/src/main/resources/config.properties create mode 100644 mpush-ps/src/main/resources/logback.xml create mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java create mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java b/mpush-core/src/main/java/com/shinemo/mpush/core/App.java deleted file mode 100644 index 67672d94..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/App.java +++ /dev/null @@ -1,130 +0,0 @@ -//package com.shinemo.mpush.core; -// -//import com.shinemo.mpush.api.Server; -//import com.shinemo.mpush.core.server.ConnectionServer; -//import com.shinemo.mpush.core.server.GatewayServer; -//import com.shinemo.mpush.tools.MPushUtil; -//import com.shinemo.mpush.tools.Jsons; -//import com.shinemo.mpush.tools.config.ConfigCenter; -//import com.shinemo.mpush.tools.redis.RedisGroup; -//import com.shinemo.mpush.tools.spi.ServiceContainer; -//import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -//import com.shinemo.mpush.tools.zk.ZKPath; -//import com.shinemo.mpush.tools.zk.ServerApp; -//import com.shinemo.mpush.tools.zk.ZkRegister; -//import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; -// -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import java.io.IOException; -//import java.util.List; -// -///** -// * Created by ohun on 2016/1/5. -// */ -//public final class App { -// private static final Logger LOGGER = LoggerFactory.getLogger(App.class); -// private static final App APP = new App(); -// private ConnectionServer connectionServer; -// private GatewayServer gatewayServer; -// -// private ZkRegister zkRegister = null; -// -// public static void main(String[] args) throws Exception { -// LOGGER.error("mpush app start begin...."); -// APP.init(); -// APP.initZkRegister(); -// APP.initRedisClient(); -// APP.startConnectionServer(); -// APP.startGatewayServer(); -// Runtime.getRuntime().addShutdownHook(new Thread() { -// public void run() { -// if (APP.connectionServer != null) { -// APP.connectionServer.stop(null); -// } -// if (APP.gatewayServer != null) { -// APP.gatewayServer.stop(null); -// } -// } -// }); -// LOGGER.error("mpush app start end...."); -// } -// -// private void init() throws IOException { -// LOGGER.error("mpush app config center init success...."); -// } -// -// public void startConnectionServer() { -// ThreadPoolUtil.newThread(new Runnable() { -// @Override -// public void run() { -// final int port = ConfigCenter.holder.connectionServerPort(); -// ConnectionServer server = new ConnectionServer(port); -// server.init(); -// server.start(new Server.Listener() { -// @Override -// public void onSuccess() { -// registerServerToZK(port, ZKPath.CONNECTION_SERVER); -// LOGGER.error("mpush app start connection server success...."); -// } -// -// @Override -// public void onFailure(String message) { -// LOGGER.error("mpush app start connection server failure, jvm exit with code -1"); -// System.exit(-1); -// } -// }); -// APP.connectionServer = server; -// } -// }, "conn-server", false).start(); -// } -// -// public void startGatewayServer() { -// ThreadPoolUtil.newThread(new Runnable() { -// @Override -// public void run() { -// final int port = ConfigCenter.holder.gatewayServerPort(); -// GatewayServer server = new GatewayServer(port); -// server.init(); -// server.start(new Server.Listener() { -// @Override -// public void onSuccess() { -// registerServerToZK(port, ZKPath.GATEWAY_SERVER); -// LOGGER.error("mpush app start gateway server success...."); -// } -// -// @Override -// public void onFailure(String message) { -// System.exit(-2); -// LOGGER.error("mpush app start gateway server failure, jvm exit with code -2"); -// } -// }); -// APP.gatewayServer = server; -// } -// }, "gateway-server", false).start(); -// } -// -// private void registerServerToZK(int port, ZKPath path) { -// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), port); -// zkRegister.registerEphemeralSequential(path.getWatchPath(), Jsons.toJson(app)); -// LOGGER.error("mpush app register server:{} to zk success", port); -// } -// -// public void initZkRegister(){ -// zkRegister = ServiceContainer.getInstance(ZkRegister.class); -// zkRegister.init(); -// } -// -// public void initRedisClient() throws Exception { -// -// boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); -// if (!exist) { -// List groupList = ConfigCenter.holder.redisGroups(); -// zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); -// } -// RedisPathListener listener = new RedisPathListener(); -// zkRegister.getCache().getListenable().addListener(listener); -// listener.initData(); -// } -//} diff --git a/mpush-ps/pom.xml b/mpush-ps/pom.xml index 6c8f2d5e..07dc19ea 100644 --- a/mpush-ps/pom.xml +++ b/mpush-ps/pom.xml @@ -11,7 +11,6 @@ jar mpush-ps - http://maven.apache.org UTF-8 @@ -20,7 +19,15 @@ com.shinemo.mpush - mpush-core + mpush-api + + + com.shinemo.mpush + mpush-netty + + + com.shinemo.mpush + mpush-tools diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/App.java b/mpush-ps/src/main/java/com/shinemo/mpush/App.java deleted file mode 100644 index 9cc9cf7b..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shinemo.mpush; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/GatewayClientChannelHandler.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java similarity index 98% rename from mpush-client/src/main/java/com/shinemo/mpush/client/GatewayClientChannelHandler.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java index 1bbb260c..a603f28e 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/GatewayClientChannelHandler.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.client; +package com.shinemo.mpush.ps; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java similarity index 99% rename from mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java index f65159fc..d9032816 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.client; +package com.shinemo.mpush.ps; import com.google.common.base.Strings; import com.shinemo.mpush.api.Client; diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java similarity index 99% rename from mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java index 9a64ebb6..0c568ccb 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.client; +package com.shinemo.mpush.ps; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; diff --git a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java similarity index 97% rename from mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java index a3cb96a8..97cbcfd7 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/client/PushRequestBus.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.client; +package com.shinemo.mpush.ps; import java.util.Iterator; import java.util.Map; diff --git a/mpush-ps/src/main/resources/config.properties b/mpush-ps/src/main/resources/config.properties new file mode 100644 index 00000000..67e2ffaf --- /dev/null +++ b/mpush-ps/src/main/resources/config.properties @@ -0,0 +1,43 @@ +## +max_packet_size = 10240 +## +compress_limit = 10240 +## +min_heartbeat = 10000 +## +max_heartbeat = 1800000 +## +max_hb_timeout_times = 2 +## +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +## +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +## +gateway_server_port = 4000 +## +connection_server_port = 3000 +## +aes_key_length = 16 +## +ras_key_length = 1024 +## +session_expired_time = 86400 +## +max_hb_timeout_times = 2 +## +max_heartbeat = 1800000 +## +min_heartbeat = 10000 +## +compress_limit = 10240 +## +max_packet_size = 10240 + +## zk 配置项 +zk_ip = 10.1.10.41:2181 +zk_namespace = mpush +zk_digest = shinemoIpo + +## redis 配置项 redis组以分号分割,每组内的redis以逗号分割 +##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo +redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/mpush-ps/src/main/resources/logback.xml b/mpush-ps/src/main/resources/logback.xml new file mode 100644 index 00000000..20979b40 --- /dev/null +++ b/mpush-ps/src/main/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java new file mode 100644 index 00000000..abaf4ca2 --- /dev/null +++ b/mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.ps; + +import org.junit.Test; + +import com.shinemo.mpush.tools.config.ConfigCenter; + +public class ConfigCenterTest { + + @Test + public void test(){ + + System.out.println(ConfigCenter.holder.zkIp()); + + } + +} diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java new file mode 100644 index 00000000..2ac5c779 --- /dev/null +++ b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.ps; + +import com.shinemo.mpush.api.PushSender; +import org.junit.Test; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + + +/** + * Created by ohun on 2016/1/7. + */ +public class PushClientTest { + + @Test + public void testSend() throws Exception { + + } + + public static void main(String[] args) throws Exception { + PushClient client = new PushClient(); + client.init(); + Thread.sleep(1000); + client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), + new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + } + ); + LockSupport.park(); + } +} \ No newline at end of file From 386c6a3ffe1b84a1a6478c9879401d356a15a49d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 17 Jan 2016 04:07:47 +0000 Subject: [PATCH 172/890] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/netty/server/NettyServer.java | 7 ++-- .../mpush/tools/thread/ThreadNameSpace.java | 38 +++++++++---------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 7bb9a971..45abe40e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -58,16 +58,15 @@ public void start(final Listener listener) { /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 - * 在这个例子中我们实现了一个服务端的应用, - * 因此会有2个NioEventLoopGroup会被使用。 + * 在一个服务端的应用会有2个NioEventLoopGroup会被使用。 * 第一个经常被叫做‘boss’,用来接收进来的连接。 * 第二个经常被叫做‘worker’,用来处理已经被接收的连接, * 一旦‘boss’接收到连接,就会把连接信息注册到‘worker’上。 * 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现, * 并且可以通过构造函数来配置他们的关系。 */ - this.bossGroup = new NioEventLoopGroup(0, ThreadPoolUtil.getBossExecutor()); - this.workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), ThreadPoolUtil.getWorkExecutor()); + this.bossGroup = new NioEventLoopGroup(1, ThreadPoolUtil.getBossExecutor()); + this.workerGroup = new NioEventLoopGroup(0, ThreadPoolUtil.getWorkExecutor()); try { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index 8d997d56..6a88488b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -1,24 +1,24 @@ package com.shinemo.mpush.tools.thread; public class ThreadNameSpace { - - /** - * netty boss 线程 - */ - public static final String NETTY_BOSS = "mg-boss"; - - /** - * netty worker 线程 - */ - public static final String NETTY_WORKER = "mg-worker"; - - /** - * connection 定期检测线程 - */ - public static final String NETTY_TIMER = "mg-timer"; - - public static final String getUniqueName(String serviceName){ - return "mg-sn-"+serviceName; - } + + /** + * netty boss 线程 + */ + public static final String NETTY_BOSS = "mp-boss"; + + /** + * netty worker 线程 + */ + public static final String NETTY_WORKER = "mp-worker"; + + /** + * connection 定期检测线程 + */ + public static final String NETTY_TIMER = "mp-timer"; + + public static final String getUniqueName(String serviceName) { + return "mp-sn-" + serviceName; + } } From 12fab5900afcc8bde00eae320d5cbb54d377077a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 12:33:26 +0800 Subject: [PATCH 173/890] add delay queue test --- .../shinemo/mpush/tools/delayqueue/Exam.java | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java new file mode 100644 index 00000000..34135b24 --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java @@ -0,0 +1,155 @@ +package com.shinemo.mpush.tools.delayqueue; + +import java.util.Iterator; +import java.util.Random; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 考试时间为120分钟,30分钟后才可交卷,初始化考生完成试卷时间最小应为30分钟 + * 对于能够在120分钟内交卷的考生,如何实现这些考生交卷 + * 对于120分钟内没有完成考试的考生,在120分钟考试时间到后需要让他们强制交卷 + * 在所有的考生都交完卷后,需要将控制线程关闭 + * + * 实现思想:用DelayQueue存储考生(Student类),每一个考生都有自己的名字和完成试卷的时间, + * Teacher线程对DelayQueue进行监控,收取完成试卷小于120分钟的学生的试卷。 + * 当考试时间120分钟到时,先关闭Teacher线程,然后强制DelayQueue中还存在的考生交卷。 + * 每一个考生交卷都会进行一次countDownLatch.countDown(),当countDownLatch.await()不再阻塞说明所有考生都交完卷了,而后结束考试。 + * + */ + +public class Exam { + + public static void main(String[] args) throws InterruptedException { + int studentNumber = 20; + CountDownLatch countDownLatch = new CountDownLatch(studentNumber+1); + DelayQueue students = new DelayQueue(); + Random random = new Random(); + for (int i = 0; i < studentNumber; i++) { + students.put(new Student("student"+(i+1), 30+random.nextInt(120),countDownLatch)); + } + Thread teacherThread =new Thread(new Teacher(students)); + students.put(new EndExam(students, 120,countDownLatch,teacherThread)); + teacherThread.start(); + countDownLatch.await(); + System.out.println(" 考试时间到,全部交卷!"); + } + +} + +class Student implements Runnable,Delayed{ + + private String name; + private long workTime; //考试时间 + private long submitTime; //交卷时间 + private boolean isForce = false; + private CountDownLatch countDownLatch; + + public Student() { + } + + public Student(String name,long workTime,CountDownLatch countDownLatch){ + this.name = name; + this.workTime = workTime; + this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS)+System.nanoTime(); + this.countDownLatch = countDownLatch; + } + + @Override + public int compareTo(Delayed o) { + if(o == null || ! (o instanceof Student)) return 1; + if(o == this) return 0; + Student s = (Student)o; + if (this.workTime > s.workTime) { + return 1; + }else if (this.workTime == s.workTime) { + return 0; + }else { + return -1; + } + } + + @Override + public long getDelay(TimeUnit unit) { + return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS); + } + + @Override + public void run() { + if (isForce) { + System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 120分钟" ); + }else { + System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 "+workTime +" 分钟"); + } + countDownLatch.countDown(); + } + + public boolean isForce() { + return isForce; + } + + public void setForce(boolean isForce) { + this.isForce = isForce; + } + +} + +class EndExam extends Student{ + + private DelayQueue students; + private CountDownLatch countDownLatch; + private Thread teacherThread; + + public EndExam(DelayQueue students, long workTime, CountDownLatch countDownLatch,Thread teacherThread) { + super("强制收卷", workTime,countDownLatch); + this.students = students; + this.countDownLatch = countDownLatch; + this.teacherThread = teacherThread; + } + + + + @Override + public void run() { + // TODO Auto-generated method stub + + teacherThread.interrupt(); + Student tmpStudent; + for (Iterator iterator2 = students.iterator(); iterator2.hasNext();) { + tmpStudent = iterator2.next(); + tmpStudent.setForce(true); + tmpStudent.run(); + } + countDownLatch.countDown(); + } + +} + + +class Teacher implements Runnable{ + + private static final Logger log = LoggerFactory.getLogger(Teacher.class); + + private DelayQueue students; + + public Teacher(DelayQueue students) { + this.students = students; + } + + @Override + public void run() { + try{ + log.warn(" test start "); + while(!Thread.interrupted()){ + students.take().run(); + } + }catch(Exception e){ + log.warn("exception :",e); + } + } +} From 101e501540573f7c12576da9a3fc19b9b912a753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 13:09:20 +0800 Subject: [PATCH 174/890] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/common}/AbstractServer.java | 4 +- .../mpush/common/{ => app}/Application.java | 2 +- .../app/impl}/GatewayServerApplication.java | 5 +- .../mpush/common}/manage/ServerManage.java | 2 +- .../manage/impl/GatewayServerManage.java | 6 +- .../listener/AbstractDataChangeListener.java | 6 +- .../impl/GatewayServerPathListener.java | 8 +- ...m.shinemo.mpush.common.manage.ServerManage | 1 + .../mpush/cs/ConnectionServerApplication.java | 3 +- .../mpush/cs/ConnectionServerMain.java | 5 +- .../manage/impl/ConnectionServerManage.java | 2 +- .../impl/ConnectionServerPathListener.java | 4 +- ....shinemo.mpush.common.manage.ServerManage} | 1 - .../mpush/ps/GatewayClientChannelHandler.java | 128 +++---- .../shinemo/mpush/ps/GatewayClientMain.java | 33 ++ .../java/com/shinemo/mpush/ps/PushClient.java | 270 ++++++------- .../com/shinemo/mpush/ps/PushRequest.java | 362 +++++++++--------- .../com/shinemo/mpush/ps/PushRequestBus.java | 88 ++--- .../com/shinemo/mpush/ps/PushClientTest.java | 98 ++--- 19 files changed, 532 insertions(+), 496 deletions(-) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-common/src/main/java/com/shinemo/mpush/common}/AbstractServer.java (97%) rename mpush-common/src/main/java/com/shinemo/mpush/common/{ => app}/Application.java (93%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-common/src/main/java/com/shinemo/mpush/common/app/impl}/GatewayServerApplication.java (73%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-common/src/main/java/com/shinemo/mpush/common}/manage/ServerManage.java (86%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-common/src/main/java/com/shinemo/mpush/common}/manage/impl/GatewayServerManage.java (86%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-common/src/main/java/com/shinemo/mpush/common}/zk/listener/AbstractDataChangeListener.java (95%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-common/src/main/java/com/shinemo/mpush/common}/zk/listener/impl/GatewayServerPathListener.java (75%) create mode 100644 mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage rename mpush-cs/src/main/resources/META-INF/mpush/services/{com.shinemo.mpush.cs.manage.ServerManage => com.shinemo.mpush.common.manage.ServerManage} (52%) create mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java similarity index 97% rename from mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index fe2bff52..77b05f88 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core; +package com.shinemo.mpush.common; import java.util.List; @@ -7,7 +7,7 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.common.Application; +import com.shinemo.mpush.common.app.Application; import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.config.ConfigCenter; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/Application.java b/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java similarity index 93% rename from mpush-common/src/main/java/com/shinemo/mpush/common/Application.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java index d78bdd5c..d0daee5b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/Application.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common; +package com.shinemo.mpush.common.app; /** diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java b/mpush-common/src/main/java/com/shinemo/mpush/common/app/impl/GatewayServerApplication.java similarity index 73% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/app/impl/GatewayServerApplication.java index b0ac3ec1..2aec1fd5 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/GatewayServerApplication.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/app/impl/GatewayServerApplication.java @@ -1,11 +1,12 @@ -package com.shinemo.mpush.cs; +package com.shinemo.mpush.common.app.impl; +import com.shinemo.mpush.common.app.Application; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; -public class GatewayServerApplication extends ConnectionServerApplication{ +public class GatewayServerApplication extends Application{ public GatewayServerApplication() { this(ConfigCenter.holder.gatewayServerPort(),ZKPath.GATEWAY_SERVER.getWatchPath(),MPushUtil.getLocalIp()); diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java similarity index 86% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java index 35901298..0821a5c9 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/ServerManage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs.manage; +package com.shinemo.mpush.common.manage; import java.util.Collection; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java similarity index 86% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java index 985ea98c..5ffdf178 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/GatewayServerManage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs.manage.impl; +package com.shinemo.mpush.common.manage.impl; import java.util.Collection; import java.util.Collections; @@ -10,8 +10,8 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Maps; -import com.shinemo.mpush.cs.GatewayServerApplication; -import com.shinemo.mpush.cs.manage.ServerManage; +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.common.manage.ServerManage; public class GatewayServerManage implements ServerManage{ diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java similarity index 95% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java index 25b7077d..ccf7e8cf 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/AbstractDataChangeListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs.zk.listener; +package com.shinemo.mpush.common.zk.listener; import java.util.List; @@ -11,8 +11,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.common.Application; -import com.shinemo.mpush.cs.manage.ServerManage; +import com.shinemo.mpush.common.app.Application; +import com.shinemo.mpush.common.manage.ServerManage; import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.spi.ServiceContainer; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java similarity index 75% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java rename to mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java index 73438fae..8056f9c4 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/GatewayServerPathListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.cs.zk.listener.impl; +package com.shinemo.mpush.common.zk.listener.impl; -import com.shinemo.mpush.cs.GatewayServerApplication; -import com.shinemo.mpush.cs.manage.ServerManage; -import com.shinemo.mpush.cs.zk.listener.AbstractDataChangeListener; +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage new file mode 100644 index 00000000..974df77b --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -0,0 +1 @@ +gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index bbe9fb5a..55d41bf6 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.cs; -import com.shinemo.mpush.common.Application; +import com.shinemo.mpush.common.app.Application; +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 639c45bb..3226b1aa 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -3,11 +3,12 @@ import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.core.AbstractServer; +import com.shinemo.mpush.common.AbstractServer; +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.common.zk.listener.impl.GatewayServerPathListener; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; -import com.shinemo.mpush.cs.zk.listener.impl.GatewayServerPathListener; import com.shinemo.mpush.tools.Jsons; public class ConnectionServerMain extends AbstractServer{ diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java index 4916137f..bdbaef4a 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java @@ -10,8 +10,8 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Maps; +import com.shinemo.mpush.common.manage.ServerManage; import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.cs.manage.ServerManage; public class ConnectionServerManage implements ServerManage{ diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java index fbd0423a..d15af5c4 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -1,9 +1,9 @@ package com.shinemo.mpush.cs.zk.listener.impl; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.cs.manage.ServerManage; -import com.shinemo.mpush.cs.zk.listener.AbstractDataChangeListener; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage similarity index 52% rename from mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage rename to mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage index 46f79b39..afcb1228 100644 --- a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.cs.manage.ServerManage +++ b/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -1,2 +1 @@ connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage -gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java index a603f28e..7abba409 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java @@ -1,64 +1,64 @@ -package com.shinemo.mpush.ps; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.ErrorCode; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.netty.connection.NettyConnection; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.shinemo.mpush.common.ErrorCode.OFFLINE; -import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; - -/** - * Created by ohun on 2015/12/30. - */ -public class GatewayClientChannelHandler extends ChannelHandlerAdapter { - private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); - private final Connection connection = new NettyConnection(); - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - connection.init(ctx.channel(), false); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - connection.close(); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof Packet) { - Packet packet = ((Packet) msg); - PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); - return; - } - - if (packet.cmd == Command.OK.cmd) { - request.success(); - } else { - ErrorMessage message = new ErrorMessage(packet, connection); - if (message.code == OFFLINE.errorCode) { - request.offline(); - } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { - request.failure(); - } else if (message.code == ROUTER_CHANGE.errorCode) { - request.redirect(); - } - LOGGER.warn("receive an error gateway response, message={}", message); - } - } - } - - public Connection getConnection() { - return connection; - } -} +//package com.shinemo.mpush.ps; +// +//import com.shinemo.mpush.api.connection.Connection; +//import com.shinemo.mpush.api.protocol.Command; +//import com.shinemo.mpush.api.protocol.Packet; +//import com.shinemo.mpush.common.ErrorCode; +//import com.shinemo.mpush.common.message.ErrorMessage; +//import com.shinemo.mpush.netty.connection.NettyConnection; +//import io.netty.channel.ChannelHandlerAdapter; +//import io.netty.channel.ChannelHandlerContext; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import static com.shinemo.mpush.common.ErrorCode.OFFLINE; +//import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +//import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; +// +///** +// * Created by ohun on 2015/12/30. +// */ +//public class GatewayClientChannelHandler extends ChannelHandlerAdapter { +// private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); +// private final Connection connection = new NettyConnection(); +// +// @Override +// public void channelActive(ChannelHandlerContext ctx) throws Exception { +// connection.init(ctx.channel(), false); +// } +// +// @Override +// public void channelInactive(ChannelHandlerContext ctx) throws Exception { +// connection.close(); +// } +// +// @Override +// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { +// if (msg instanceof Packet) { +// Packet packet = ((Packet) msg); +// PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); +// if (request == null) { +// LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); +// return; +// } +// +// if (packet.cmd == Command.OK.cmd) { +// request.success(); +// } else { +// ErrorMessage message = new ErrorMessage(packet, connection); +// if (message.code == OFFLINE.errorCode) { +// request.offline(); +// } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { +// request.failure(); +// } else if (message.code == ROUTER_CHANGE.errorCode) { +// request.redirect(); +// } +// LOGGER.warn("receive an error gateway response, message={}", message); +// } +// } +// } +// +// public Connection getConnection() { +// return connection; +// } +//} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java new file mode 100644 index 00000000..3ec86e94 --- /dev/null +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java @@ -0,0 +1,33 @@ +//package com.shinemo.mpush.ps; +// +// +// +//import com.shinemo.mpush.api.Server; +//import com.shinemo.mpush.common.AbstractServer; +//import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +//import com.shinemo.mpush.tools.Jsons; +// +//public class GatewayClientMain extends AbstractServer{ +// +// +// private GatewayServerApplication gatewayServerApplication; +// +// public GatewayClientMain(){ +// +// registerListener(new GatewayServerPathListener()); +// +// connectionServerApplication = (ConnectionServerApplication)application; +// gatewayServerApplication = new GatewayServerApplication(); +// connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); +// gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); +// } +// +// @Override +// public Server getServer() { +// final int port = application.getPort(); +// ConnectionServer connectionServer = new ConnectionServer(port); +// return connectionServer; +// } +// +// +//} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java index d9032816..85819a12 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java @@ -1,135 +1,135 @@ -package com.shinemo.mpush.ps; - -import com.google.common.base.Strings; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.Application; -import com.shinemo.mpush.netty.client.NettyClient; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; - -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Created by ohun on 2015/12/30. - */ -public class PushClient implements PushSender { - - private int defaultTimeout = 3000; - private final Map clientMap = new ConcurrentHashMap<>(); - private final Map servers = new ConcurrentHashMap<>(); - - private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - public void init() throws Exception { - initRedisClient(); - GatewayServerZKListener listener = new GatewayServerZKListener(); - Collection nodes = listener.getAllServers(); - if (nodes == null || nodes.isEmpty()) return; - for (Application server : nodes) { - createClient(server.getIp(), server.getPort()); - } - } - - private void createClient(final String ip, int port) { - Client client = clientMap.get(ip); - if (client == null) { - final Client cli = new NettyClient(ip, port, new GatewayClientChannelHandler()); - ThreadPoolUtil.newThread(new Runnable() { - @Override - public void run() { - cli.init(); - cli.start(); - } - }, "push-client-" + ip).start(); - clientMap.put(ip, cli); - } - } - - public Connection getConnection(String ip) { - Client client = clientMap.get(ip); - if (client == null) return null; - return ((GatewayClientChannelHandler) client.getHandler()).getConnection(); - } - - @Override - public void send(String content, Collection userIds, Callback callback) { - for (String userId : userIds) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(defaultTimeout) - .send(); - } - } - - public void initRedisClient() { -// RedisPathListener listener = new RedisPathListener(); -// zkRegister.getCache().getListenable().addListener(listener); -// listener.initData(null); - } - - private class GatewayServerZKListener implements TreeCacheListener { - - public GatewayServerZKListener() { - zkRegister.getCache().getListenable().addListener(this); - } - - @Override - public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { - if (event.getData() == null) return; - String path = event.getData().getPath(); - if (Strings.isNullOrEmpty(path)) return; - - if (TreeCacheEvent.Type.NODE_ADDED == event.getType()) { - Application serverApp = getServer(path); - if (serverApp != null) { - createClient(serverApp.getIp(), serverApp.getPort()); - servers.put(path, serverApp); - } - } else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) { - Application serverApp = servers.remove(path); - if (serverApp != null) { - Client client = clientMap.remove(serverApp.getIp()); - if (client != null) { - client.stop(); - } - } - } else if (TreeCacheEvent.Type.NODE_UPDATED == event.getType()) { - - } - } - - private Application getServer(String path) { - String json = zkRegister.get(path); - if (Strings.isNullOrEmpty(json)) return null; - return Jsons.fromJson(json, Application.class); - } - - private Collection getAllServers() { - List list = zkRegister.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); - if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; - for (String name : list) { - String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name); - String json = zkRegister.get(fullPath); - if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue; - Application server = Jsons.fromJson(json, Application.class); - if (server != null) { - servers.put(fullPath, server); - } - } - return servers.values(); - } - } -} +//package com.shinemo.mpush.ps; +// +//import com.google.common.base.Strings; +//import com.shinemo.mpush.api.Client; +//import com.shinemo.mpush.api.PushSender; +//import com.shinemo.mpush.api.connection.Connection; +//import com.shinemo.mpush.common.Application; +//import com.shinemo.mpush.netty.client.NettyClient; +//import com.shinemo.mpush.tools.Jsons; +//import com.shinemo.mpush.tools.spi.ServiceContainer; +//import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +//import com.shinemo.mpush.tools.zk.ZKPath; +//import com.shinemo.mpush.tools.zk.ZkRegister; +// +//import org.apache.curator.framework.CuratorFramework; +//import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +//import org.apache.curator.framework.recipes.cache.TreeCacheListener; +// +//import java.util.*; +//import java.util.concurrent.ConcurrentHashMap; +// +///** +// * Created by ohun on 2015/12/30. +// */ +//public class PushClient implements PushSender { +// +// private int defaultTimeout = 3000; +// private final Map clientMap = new ConcurrentHashMap<>(); +// private final Map servers = new ConcurrentHashMap<>(); +// +// private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); +// +// public void init() throws Exception { +// initRedisClient(); +// GatewayServerZKListener listener = new GatewayServerZKListener(); +// Collection nodes = listener.getAllServers(); +// if (nodes == null || nodes.isEmpty()) return; +// for (Application server : nodes) { +// createClient(server.getIp(), server.getPort()); +// } +// } +// +// private void createClient(final String ip, int port) { +// Client client = clientMap.get(ip); +// if (client == null) { +// final Client cli = new NettyClient(ip, port, new GatewayClientChannelHandler()); +// ThreadPoolUtil.newThread(new Runnable() { +// @Override +// public void run() { +// cli.init(); +// cli.start(); +// } +// }, "push-client-" + ip).start(); +// clientMap.put(ip, cli); +// } +// } +// +// public Connection getConnection(String ip) { +// Client client = clientMap.get(ip); +// if (client == null) return null; +// return ((GatewayClientChannelHandler) client.getHandler()).getConnection(); +// } +// +// @Override +// public void send(String content, Collection userIds, Callback callback) { +// for (String userId : userIds) { +// PushRequest +// .build(this) +// .setCallback(callback) +// .setUserId(userId) +// .setContent(content) +// .setTimeout(defaultTimeout) +// .send(); +// } +// } +// +// public void initRedisClient() { +//// RedisPathListener listener = new RedisPathListener(); +//// zkRegister.getCache().getListenable().addListener(listener); +//// listener.initData(null); +// } +// +// private class GatewayServerZKListener implements TreeCacheListener { +// +// public GatewayServerZKListener() { +// zkRegister.getCache().getListenable().addListener(this); +// } +// +// @Override +// public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { +// if (event.getData() == null) return; +// String path = event.getData().getPath(); +// if (Strings.isNullOrEmpty(path)) return; +// +// if (TreeCacheEvent.Type.NODE_ADDED == event.getType()) { +// Application serverApp = getServer(path); +// if (serverApp != null) { +// createClient(serverApp.getIp(), serverApp.getPort()); +// servers.put(path, serverApp); +// } +// } else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) { +// Application serverApp = servers.remove(path); +// if (serverApp != null) { +// Client client = clientMap.remove(serverApp.getIp()); +// if (client != null) { +// client.stop(); +// } +// } +// } else if (TreeCacheEvent.Type.NODE_UPDATED == event.getType()) { +// +// } +// } +// +// private Application getServer(String path) { +// String json = zkRegister.get(path); +// if (Strings.isNullOrEmpty(json)) return null; +// return Jsons.fromJson(json, Application.class); +// } +// +// private Collection getAllServers() { +// List list = zkRegister.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); +// if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; +// for (String name : list) { +// String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name); +// String json = zkRegister.get(fullPath); +// if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue; +// Application server = Jsons.fromJson(json, Application.class); +// if (server != null) { +// servers.put(fullPath, server); +// } +// } +// return servers.values(); +// } +// } +//} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java index 0c568ccb..e1095eb1 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java @@ -1,181 +1,181 @@ -package com.shinemo.mpush.ps; - -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.common.router.ConnectionRouterManager; -import com.shinemo.mpush.common.router.RemoteRouter; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/30. - */ -public class PushRequest implements PushSender.Callback, Runnable { - private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - private PushSender.Callback callback; - private PushClient pushClient; - private String userId; - private String content; - private long timeout; - private int status = 0; - private long timeout_; - private int sessionId; - private long sendTime; - - public PushRequest(PushClient pushClient) { - this.pushClient = pushClient; - } - - public static PushRequest build(PushClient pushClient) { - return new PushRequest(pushClient); - } - - public PushRequest setCallback(PushSender.Callback callback) { - this.callback = callback; - return this; - } - - public PushRequest setUserId(String userId) { - this.userId = userId; - return this; - } - - public PushRequest setContent(String content) { - this.content = content; - return this; - } - - public PushRequest setTimeout(long timeout) { - this.timeout = timeout; - return this; - } - - public void setSessionId(int sessionId) { - this.sessionId = sessionId; - } - - public int getSessionId() { - return sessionId; - } - - @Override - public void onSuccess(String userId) { - submit(1); - } - - @Override - public void onFailure(String userId) { - submit(2); - } - - @Override - public void onOffline(String userId) { - submit(3); - } - - @Override - public void onTimeout(String userId) { - submit(4); - } - - private void submit(int status) { - if (this.status != 0) {//防止重复调用 - return; - } - this.status = status; - if (callback != null) { - PushRequestBus.INSTANCE.getExecutor().execute(this); - } else { - LOGGER.warn("callback is null"); - } - } - - @Override - public void run() { - switch (status) { - case 1: - callback.onSuccess(userId); - break; - case 2: - callback.onFailure(userId); - break; - case 3: - callback.onOffline(userId); - break; - case 4: - callback.onTimeout(userId); - break; - } - } - - public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; - } - - public void timeout() { - onTimeout(userId); - } - - public void success() { - onSuccess(userId); - } - - public void failure() { - onFailure(userId); - } - - public void offline() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - onOffline(userId); - } - - public void send() { - this.timeout_ = timeout + System.currentTimeMillis(); - sendToConnServer(); - } - - public void redirect() { - this.status = 0; - this.timeout_ = timeout + System.currentTimeMillis(); - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - sendToConnServer(); - LOGGER.warn("user route has changed, userId={}, content={}", userId, content); - } - - private void sendToConnServer() { - //1.查询用户长连接所在的机器 - RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); - if (router == null) { - //1.1没有查到说明用户已经下线 - this.onOffline(userId); - return; - } - - //2.通过网关连接,把消息发送到所在机器 - ClientLocation location = router.getRouteValue(); - Connection gatewayConn = pushClient.getConnection(location.getHost()); - if (gatewayConn == null || !gatewayConn.isConnected()) { - this.onFailure(userId); - return; - } - - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - sendTime = System.currentTimeMillis(); - } else { - PushRequest.this.onFailure(userId); - } - } - }); - - sessionId = pushMessage.getSessionId(); - PushRequestBus.INSTANCE.put(sessionId, this); - } -} +//package com.shinemo.mpush.ps; +// +//import com.shinemo.mpush.api.PushSender; +//import com.shinemo.mpush.api.connection.Connection; +//import com.shinemo.mpush.api.router.ClientLocation; +//import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +//import com.shinemo.mpush.common.router.ConnectionRouterManager; +//import com.shinemo.mpush.common.router.RemoteRouter; +//import io.netty.channel.ChannelFuture; +//import io.netty.channel.ChannelFutureListener; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +///** +// * Created by ohun on 2015/12/30. +// */ +//public class PushRequest implements PushSender.Callback, Runnable { +// private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); +// private PushSender.Callback callback; +// private PushClient pushClient; +// private String userId; +// private String content; +// private long timeout; +// private int status = 0; +// private long timeout_; +// private int sessionId; +// private long sendTime; +// +// public PushRequest(PushClient pushClient) { +// this.pushClient = pushClient; +// } +// +// public static PushRequest build(PushClient pushClient) { +// return new PushRequest(pushClient); +// } +// +// public PushRequest setCallback(PushSender.Callback callback) { +// this.callback = callback; +// return this; +// } +// +// public PushRequest setUserId(String userId) { +// this.userId = userId; +// return this; +// } +// +// public PushRequest setContent(String content) { +// this.content = content; +// return this; +// } +// +// public PushRequest setTimeout(long timeout) { +// this.timeout = timeout; +// return this; +// } +// +// public void setSessionId(int sessionId) { +// this.sessionId = sessionId; +// } +// +// public int getSessionId() { +// return sessionId; +// } +// +// @Override +// public void onSuccess(String userId) { +// submit(1); +// } +// +// @Override +// public void onFailure(String userId) { +// submit(2); +// } +// +// @Override +// public void onOffline(String userId) { +// submit(3); +// } +// +// @Override +// public void onTimeout(String userId) { +// submit(4); +// } +// +// private void submit(int status) { +// if (this.status != 0) {//防止重复调用 +// return; +// } +// this.status = status; +// if (callback != null) { +// PushRequestBus.INSTANCE.getExecutor().execute(this); +// } else { +// LOGGER.warn("callback is null"); +// } +// } +// +// @Override +// public void run() { +// switch (status) { +// case 1: +// callback.onSuccess(userId); +// break; +// case 2: +// callback.onFailure(userId); +// break; +// case 3: +// callback.onOffline(userId); +// break; +// case 4: +// callback.onTimeout(userId); +// break; +// } +// } +// +// public boolean isTimeout() { +// return System.currentTimeMillis() > timeout_; +// } +// +// public void timeout() { +// onTimeout(userId); +// } +// +// public void success() { +// onSuccess(userId); +// } +// +// public void failure() { +// onFailure(userId); +// } +// +// public void offline() { +// ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); +// onOffline(userId); +// } +// +// public void send() { +// this.timeout_ = timeout + System.currentTimeMillis(); +// sendToConnServer(); +// } +// +// public void redirect() { +// this.status = 0; +// this.timeout_ = timeout + System.currentTimeMillis(); +// ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); +// sendToConnServer(); +// LOGGER.warn("user route has changed, userId={}, content={}", userId, content); +// } +// +// private void sendToConnServer() { +// //1.查询用户长连接所在的机器 +// RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); +// if (router == null) { +// //1.1没有查到说明用户已经下线 +// this.onOffline(userId); +// return; +// } +// +// //2.通过网关连接,把消息发送到所在机器 +// ClientLocation location = router.getRouteValue(); +// Connection gatewayConn = pushClient.getConnection(location.getHost()); +// if (gatewayConn == null || !gatewayConn.isConnected()) { +// this.onFailure(userId); +// return; +// } +// +// GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); +// pushMessage.send(new ChannelFutureListener() { +// @Override +// public void operationComplete(ChannelFuture future) throws Exception { +// if (future.isSuccess()) { +// sendTime = System.currentTimeMillis(); +// } else { +// PushRequest.this.onFailure(userId); +// } +// } +// }); +// +// sessionId = pushMessage.getSessionId(); +// PushRequestBus.INSTANCE.put(sessionId, this); +// } +//} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java index 97cbcfd7..ce5e61be 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java @@ -1,44 +1,44 @@ -package com.shinemo.mpush.ps; - -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.*; - -/** - * Created by ohun on 2015/12/30. - */ -public class PushRequestBus implements Runnable { - public static final PushRequestBus INSTANCE = new PushRequestBus(); - private Map requests = new ConcurrentHashMap<>(); - private Executor executor = Executors.newFixedThreadPool(5);//test - private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test - - public PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); - } - - public void put(int sessionId, PushRequest request) { - requests.put(sessionId, request); - } - - public PushRequest remove(int sessionId) { - return requests.remove(sessionId); - } - - public Executor getExecutor() { - return executor; - } - - @Override - public void run() { - if (requests.isEmpty()) return; - Iterator it = requests.values().iterator(); - while (it.hasNext()) { - PushRequest request = it.next(); - if (request.isTimeout()) { - it.remove();//清除超时的请求 - request.timeout(); - } - } - } -} +//package com.shinemo.mpush.ps; +// +//import java.util.Iterator; +//import java.util.Map; +//import java.util.concurrent.*; +// +///** +// * Created by ohun on 2015/12/30. +// */ +//public class PushRequestBus implements Runnable { +// public static final PushRequestBus INSTANCE = new PushRequestBus(); +// private Map requests = new ConcurrentHashMap<>(); +// private Executor executor = Executors.newFixedThreadPool(5);//test +// private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test +// +// public PushRequestBus() { +// scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); +// } +// +// public void put(int sessionId, PushRequest request) { +// requests.put(sessionId, request); +// } +// +// public PushRequest remove(int sessionId) { +// return requests.remove(sessionId); +// } +// +// public Executor getExecutor() { +// return executor; +// } +// +// @Override +// public void run() { +// if (requests.isEmpty()) return; +// Iterator it = requests.values().iterator(); +// while (it.hasNext()) { +// PushRequest request = it.next(); +// if (request.isTimeout()) { +// it.remove();//清除超时的请求 +// request.timeout(); +// } +// } +// } +//} diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java index 2ac5c779..eb3d9749 100644 --- a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java +++ b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java @@ -1,49 +1,49 @@ -package com.shinemo.mpush.ps; - -import com.shinemo.mpush.api.PushSender; -import org.junit.Test; - -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; - - -/** - * Created by ohun on 2016/1/7. - */ -public class PushClientTest { - - @Test - public void testSend() throws Exception { - - } - - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.init(); - Thread.sleep(1000); - client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), - new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - } - ); - LockSupport.park(); - } -} \ No newline at end of file +//package com.shinemo.mpush.ps; +// +//import com.shinemo.mpush.api.PushSender; +//import org.junit.Test; +// +//import java.util.Arrays; +//import java.util.concurrent.locks.LockSupport; +// +// +///** +// * Created by ohun on 2016/1/7. +// */ +//public class PushClientTest { +// +// @Test +// public void testSend() throws Exception { +// +// } +// +// public static void main(String[] args) throws Exception { +// PushClient client = new PushClient(); +// client.init(); +// Thread.sleep(1000); +// client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), +// new PushSender.Callback() { +// @Override +// public void onSuccess(String userId) { +// System.err.println("push onSuccess userId=" + userId); +// } +// +// @Override +// public void onFailure(String userId) { +// System.err.println("push onFailure userId=" + userId); +// } +// +// @Override +// public void onOffline(String userId) { +// System.err.println("push onOffline userId=" + userId); +// } +// +// @Override +// public void onTimeout(String userId) { +// System.err.println("push onTimeout userId=" + userId); +// } +// } +// ); +// LockSupport.park(); +// } +//} \ No newline at end of file From 9fe7d6f692b6b21d00db8b051f8623d8029ecd9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 13:14:22 +0800 Subject: [PATCH 175/890] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-common/pom.xml | 53 ++++++++++++++++++- ...m.shinemo.mpush.common.manage.ServerManage | 2 +- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index d0433d21..c78983b4 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -21,4 +21,55 @@ - \ No newline at end of file + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.4.2 + + true + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-common + + + + + + + + + + diff --git a/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage index 974df77b..97ab4667 100644 --- a/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ b/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -1 +1 @@ -gatewayServerManage=com.shinemo.mpush.cs.manage.impl.GatewayServerManage \ No newline at end of file +gatewayServerManage=com.shinemo.mpush.common.manage.impl.GatewayServerManage \ No newline at end of file From 03d36b1e4f6587157d2c660b8e638e998834f49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 14:12:50 +0800 Subject: [PATCH 176/890] =?UTF-8?q?=E8=B0=83=E6=95=B4push=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/common/AbstractClient.java | 66 ++++ .../shinemo/mpush/common/AbstractServer.java | 7 + .../manage/impl/GatewayServerManage.java | 4 + .../netty/client/NettyClientFactory.java | 4 +- .../mpush/ps/GatewayClientChannelHandler.java | 127 +++--- .../shinemo/mpush/ps/GatewayClientMain.java | 56 ++- .../shinemo/mpush/ps/GatewayClientManage.java | 52 +++ .../java/com/shinemo/mpush/ps/PushClient.java | 135 ------- .../com/shinemo/mpush/ps/PushRequest.java | 367 +++++++++--------- .../com/shinemo/mpush/ps/PushRequestBus.java | 88 ++--- ...m.shinemo.mpush.common.manage.ServerManage | 1 + .../com/shinemo/mpush/ps/PushClientTest.java | 49 --- .../java/com/shinemo/mpush/ps/PushTest.java | 49 +++ 13 files changed, 496 insertions(+), 509 deletions(-) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java create mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java create mode 100644 mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage delete mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java create mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java new file mode 100644 index 00000000..1e9f0518 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java @@ -0,0 +1,66 @@ +package com.shinemo.mpush.common; + +import java.util.List; + + +import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; +import com.shinemo.mpush.tools.zk.listener.DataChangeListener; +import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; + +public abstract class AbstractClient { + + protected List dataChangeListeners = Lists.newArrayList(); + + protected ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + + public AbstractClient() { + registerListener(new RedisPathListener()); + } + + + public void registerListener(DataChangeListener listener){ + dataChangeListeners.add(listener); + } + + //step1 启动 zk + private void initZK(){ + zkRegister.init(); + } + + //step2 获取redis + private void initRedis(){ + boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); + if (!exist) { + List groupList = ConfigCenter.holder.redisGroups(); + zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } + } + + //step3 注册listener + private void registerListeners(){ + for(DataChangeListener listener:dataChangeListeners){ + zkRegister.registerListener(listener); + } + } + + //step4 初始化 listener data + private void initListenerData(){ + for(DataChangeListener listener:dataChangeListeners){ + listener.initData(); + } + } + + public void start(){ + initZK(); + initRedis(); + registerListeners(); + initListenerData(); + } + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 77b05f88..da3296c9 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -123,6 +123,13 @@ public void start(){ registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); } + public void startClient(){ + initZK(); + initRedis(); + registerListeners(); + initListenerData(); + } + public void stopServer(Server server){ if(server!=null){ server.stop(null); diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java index 5ffdf178..e608e3ac 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java @@ -42,4 +42,8 @@ private void printList(){ } } + public GatewayServerApplication get(String fullpath){ + return holder.get(fullpath); + } + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index f229ba92..40351588 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -1,8 +1,6 @@ package com.shinemo.mpush.netty.client; import io.netty.channel.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Client; @@ -10,7 +8,7 @@ public class NettyClientFactory extends AbstractNettyClientFactory { public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - Client createClient(String host, int port, ChannelHandler handler) { + public Client createClient(String host, int port, ChannelHandler handler) { return new NettyClient(host, port, handler); } } diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java index 7abba409..ac173d79 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java @@ -1,64 +1,63 @@ -//package com.shinemo.mpush.ps; -// -//import com.shinemo.mpush.api.connection.Connection; -//import com.shinemo.mpush.api.protocol.Command; -//import com.shinemo.mpush.api.protocol.Packet; -//import com.shinemo.mpush.common.ErrorCode; -//import com.shinemo.mpush.common.message.ErrorMessage; -//import com.shinemo.mpush.netty.connection.NettyConnection; -//import io.netty.channel.ChannelHandlerAdapter; -//import io.netty.channel.ChannelHandlerContext; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import static com.shinemo.mpush.common.ErrorCode.OFFLINE; -//import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -//import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; -// -///** -// * Created by ohun on 2015/12/30. -// */ -//public class GatewayClientChannelHandler extends ChannelHandlerAdapter { -// private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); -// private final Connection connection = new NettyConnection(); -// -// @Override -// public void channelActive(ChannelHandlerContext ctx) throws Exception { -// connection.init(ctx.channel(), false); -// } -// -// @Override -// public void channelInactive(ChannelHandlerContext ctx) throws Exception { -// connection.close(); -// } -// -// @Override -// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { -// if (msg instanceof Packet) { -// Packet packet = ((Packet) msg); -// PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); -// if (request == null) { -// LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); -// return; -// } -// -// if (packet.cmd == Command.OK.cmd) { -// request.success(); -// } else { -// ErrorMessage message = new ErrorMessage(packet, connection); -// if (message.code == OFFLINE.errorCode) { -// request.offline(); -// } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { -// request.failure(); -// } else if (message.code == ROUTER_CHANGE.errorCode) { -// request.redirect(); -// } -// LOGGER.warn("receive an error gateway response, message={}", message); -// } -// } -// } -// -// public Connection getConnection() { -// return connection; -// } -//} +package com.shinemo.mpush.ps; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.netty.connection.NettyConnection; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.shinemo.mpush.common.ErrorCode.OFFLINE; +import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; + +/** + * Created by ohun on 2015/12/30. + */ +public class GatewayClientChannelHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); + private final Connection connection = new NettyConnection(); + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + connection.init(ctx.channel(), false); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + if (msg instanceof Packet) { + Packet packet = ((Packet) msg); + PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); + if (request == null) { + LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); + return; + } + + if (packet.cmd == Command.OK.cmd) { + request.success(); + } else { + ErrorMessage message = new ErrorMessage(packet, connection); + if (message.code == OFFLINE.errorCode) { + request.offline(); + } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { + request.failure(); + } else if (message.code == ROUTER_CHANGE.errorCode) { + request.redirect(); + } + LOGGER.warn("receive an error gateway response, message={}", message); + } + } + } + + public Connection getConnection() { + return connection; + } +} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java index 3ec86e94..e064a543 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java @@ -1,33 +1,23 @@ -//package com.shinemo.mpush.ps; -// -// -// -//import com.shinemo.mpush.api.Server; -//import com.shinemo.mpush.common.AbstractServer; -//import com.shinemo.mpush.common.app.impl.GatewayServerApplication; -//import com.shinemo.mpush.tools.Jsons; -// -//public class GatewayClientMain extends AbstractServer{ -// -// -// private GatewayServerApplication gatewayServerApplication; -// -// public GatewayClientMain(){ -// -// registerListener(new GatewayServerPathListener()); -// -// connectionServerApplication = (ConnectionServerApplication)application; -// gatewayServerApplication = new GatewayServerApplication(); -// connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); -// gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); -// } -// -// @Override -// public Server getServer() { -// final int port = application.getPort(); -// ConnectionServer connectionServer = new ConnectionServer(port); -// return connectionServer; -// } -// -// -//} +package com.shinemo.mpush.ps; + +import java.util.Collection; + +import com.shinemo.mpush.api.PushSender.Callback; +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.common.zk.listener.impl.GatewayServerPathListener; + +public class GatewayClientMain extends AbstractClient { + + private static final int defaultTimeout = 3000; + + public GatewayClientMain() { + registerListener(new GatewayServerPathListener()); + } + + public void send(String content, Collection userIds, Callback callback) { + for (String userId : userIds) { + PushRequest.build().setCallback(callback).setUserId(userId).setContent(content).setTimeout(defaultTimeout).send(); + } + } + +} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java new file mode 100644 index 00000000..54b37e49 --- /dev/null +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java @@ -0,0 +1,52 @@ +package com.shinemo.mpush.ps; + +import java.util.Map; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.common.manage.impl.GatewayServerManage; +import com.shinemo.mpush.netty.client.NettyClientFactory; + +public class GatewayClientManage extends GatewayServerManage{ + + private final Map application2Client = Maps.newConcurrentMap(); + + private final Map ip2Client = Maps.newConcurrentMap(); + + @Override + public void addOrUpdate(String fullPath, GatewayServerApplication application) { + super.addOrUpdate(fullPath, application); + Client client = NettyClientFactory.INSTANCE.createClient(application.getIp(), application.getPort(),new GatewayClientChannelHandler()); + application2Client.put(application, client); + ip2Client.put(application.getIp()+":"+application.getPort(), client); + } + + + @Override + public void remove(String fullPath) { + GatewayServerApplication application = super.get(fullPath); + super.remove(fullPath); + + if(application!=null){ + Client client = application2Client.get(application); + if(client!=null){ + client.stop(); + } + } + ip2Client.remove(application.getIp()+":"+application.getPort()); + } + + public Client getClient(GatewayServerApplication application){ + return application2Client.get(application); + } + + public Connection getConnection(String ipAndPort) { + Client client = ip2Client.get(ipAndPort); + if (client == null) return null; + return ((GatewayClientChannelHandler) client.getHandler()).getConnection(); + } + + +} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java deleted file mode 100644 index 85819a12..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushClient.java +++ /dev/null @@ -1,135 +0,0 @@ -//package com.shinemo.mpush.ps; -// -//import com.google.common.base.Strings; -//import com.shinemo.mpush.api.Client; -//import com.shinemo.mpush.api.PushSender; -//import com.shinemo.mpush.api.connection.Connection; -//import com.shinemo.mpush.common.Application; -//import com.shinemo.mpush.netty.client.NettyClient; -//import com.shinemo.mpush.tools.Jsons; -//import com.shinemo.mpush.tools.spi.ServiceContainer; -//import com.shinemo.mpush.tools.thread.ThreadPoolUtil; -//import com.shinemo.mpush.tools.zk.ZKPath; -//import com.shinemo.mpush.tools.zk.ZkRegister; -// -//import org.apache.curator.framework.CuratorFramework; -//import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -//import org.apache.curator.framework.recipes.cache.TreeCacheListener; -// -//import java.util.*; -//import java.util.concurrent.ConcurrentHashMap; -// -///** -// * Created by ohun on 2015/12/30. -// */ -//public class PushClient implements PushSender { -// -// private int defaultTimeout = 3000; -// private final Map clientMap = new ConcurrentHashMap<>(); -// private final Map servers = new ConcurrentHashMap<>(); -// -// private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); -// -// public void init() throws Exception { -// initRedisClient(); -// GatewayServerZKListener listener = new GatewayServerZKListener(); -// Collection nodes = listener.getAllServers(); -// if (nodes == null || nodes.isEmpty()) return; -// for (Application server : nodes) { -// createClient(server.getIp(), server.getPort()); -// } -// } -// -// private void createClient(final String ip, int port) { -// Client client = clientMap.get(ip); -// if (client == null) { -// final Client cli = new NettyClient(ip, port, new GatewayClientChannelHandler()); -// ThreadPoolUtil.newThread(new Runnable() { -// @Override -// public void run() { -// cli.init(); -// cli.start(); -// } -// }, "push-client-" + ip).start(); -// clientMap.put(ip, cli); -// } -// } -// -// public Connection getConnection(String ip) { -// Client client = clientMap.get(ip); -// if (client == null) return null; -// return ((GatewayClientChannelHandler) client.getHandler()).getConnection(); -// } -// -// @Override -// public void send(String content, Collection userIds, Callback callback) { -// for (String userId : userIds) { -// PushRequest -// .build(this) -// .setCallback(callback) -// .setUserId(userId) -// .setContent(content) -// .setTimeout(defaultTimeout) -// .send(); -// } -// } -// -// public void initRedisClient() { -//// RedisPathListener listener = new RedisPathListener(); -//// zkRegister.getCache().getListenable().addListener(listener); -//// listener.initData(null); -// } -// -// private class GatewayServerZKListener implements TreeCacheListener { -// -// public GatewayServerZKListener() { -// zkRegister.getCache().getListenable().addListener(this); -// } -// -// @Override -// public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception { -// if (event.getData() == null) return; -// String path = event.getData().getPath(); -// if (Strings.isNullOrEmpty(path)) return; -// -// if (TreeCacheEvent.Type.NODE_ADDED == event.getType()) { -// Application serverApp = getServer(path); -// if (serverApp != null) { -// createClient(serverApp.getIp(), serverApp.getPort()); -// servers.put(path, serverApp); -// } -// } else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) { -// Application serverApp = servers.remove(path); -// if (serverApp != null) { -// Client client = clientMap.remove(serverApp.getIp()); -// if (client != null) { -// client.stop(); -// } -// } -// } else if (TreeCacheEvent.Type.NODE_UPDATED == event.getType()) { -// -// } -// } -// -// private Application getServer(String path) { -// String json = zkRegister.get(path); -// if (Strings.isNullOrEmpty(json)) return null; -// return Jsons.fromJson(json, Application.class); -// } -// -// private Collection getAllServers() { -// List list = zkRegister.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath()); -// if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; -// for (String name : list) { -// String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name); -// String json = zkRegister.get(fullPath); -// if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue; -// Application server = Jsons.fromJson(json, Application.class); -// if (server != null) { -// servers.put(fullPath, server); -// } -// } -// return servers.values(); -// } -// } -//} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java index e1095eb1..9281aba9 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java @@ -1,181 +1,186 @@ -//package com.shinemo.mpush.ps; -// -//import com.shinemo.mpush.api.PushSender; -//import com.shinemo.mpush.api.connection.Connection; -//import com.shinemo.mpush.api.router.ClientLocation; -//import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -//import com.shinemo.mpush.common.router.ConnectionRouterManager; -//import com.shinemo.mpush.common.router.RemoteRouter; -//import io.netty.channel.ChannelFuture; -//import io.netty.channel.ChannelFutureListener; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -///** -// * Created by ohun on 2015/12/30. -// */ -//public class PushRequest implements PushSender.Callback, Runnable { -// private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); -// private PushSender.Callback callback; -// private PushClient pushClient; -// private String userId; -// private String content; -// private long timeout; -// private int status = 0; -// private long timeout_; -// private int sessionId; -// private long sendTime; -// -// public PushRequest(PushClient pushClient) { -// this.pushClient = pushClient; -// } -// -// public static PushRequest build(PushClient pushClient) { -// return new PushRequest(pushClient); -// } -// -// public PushRequest setCallback(PushSender.Callback callback) { -// this.callback = callback; -// return this; -// } -// -// public PushRequest setUserId(String userId) { -// this.userId = userId; -// return this; -// } -// -// public PushRequest setContent(String content) { -// this.content = content; -// return this; -// } -// -// public PushRequest setTimeout(long timeout) { -// this.timeout = timeout; -// return this; -// } -// -// public void setSessionId(int sessionId) { -// this.sessionId = sessionId; -// } -// -// public int getSessionId() { -// return sessionId; -// } -// -// @Override -// public void onSuccess(String userId) { -// submit(1); -// } -// -// @Override -// public void onFailure(String userId) { -// submit(2); -// } -// -// @Override -// public void onOffline(String userId) { -// submit(3); -// } -// -// @Override -// public void onTimeout(String userId) { -// submit(4); -// } -// -// private void submit(int status) { -// if (this.status != 0) {//防止重复调用 -// return; -// } -// this.status = status; -// if (callback != null) { -// PushRequestBus.INSTANCE.getExecutor().execute(this); -// } else { -// LOGGER.warn("callback is null"); -// } -// } -// -// @Override -// public void run() { -// switch (status) { -// case 1: -// callback.onSuccess(userId); -// break; -// case 2: -// callback.onFailure(userId); -// break; -// case 3: -// callback.onOffline(userId); -// break; -// case 4: -// callback.onTimeout(userId); -// break; -// } -// } -// -// public boolean isTimeout() { -// return System.currentTimeMillis() > timeout_; -// } -// -// public void timeout() { -// onTimeout(userId); -// } -// -// public void success() { -// onSuccess(userId); -// } -// -// public void failure() { -// onFailure(userId); -// } -// -// public void offline() { -// ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); -// onOffline(userId); -// } -// -// public void send() { -// this.timeout_ = timeout + System.currentTimeMillis(); -// sendToConnServer(); -// } -// -// public void redirect() { -// this.status = 0; -// this.timeout_ = timeout + System.currentTimeMillis(); -// ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); -// sendToConnServer(); -// LOGGER.warn("user route has changed, userId={}, content={}", userId, content); -// } -// -// private void sendToConnServer() { -// //1.查询用户长连接所在的机器 -// RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); -// if (router == null) { -// //1.1没有查到说明用户已经下线 -// this.onOffline(userId); -// return; -// } -// -// //2.通过网关连接,把消息发送到所在机器 -// ClientLocation location = router.getRouteValue(); -// Connection gatewayConn = pushClient.getConnection(location.getHost()); -// if (gatewayConn == null || !gatewayConn.isConnected()) { -// this.onFailure(userId); -// return; -// } -// -// GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); -// pushMessage.send(new ChannelFutureListener() { -// @Override -// public void operationComplete(ChannelFuture future) throws Exception { -// if (future.isSuccess()) { -// sendTime = System.currentTimeMillis(); -// } else { -// PushRequest.this.onFailure(userId); -// } -// } -// }); -// -// sessionId = pushMessage.getSessionId(); -// PushRequestBus.INSTANCE.put(sessionId, this); -// } -//} +package com.shinemo.mpush.ps; + +import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.router.ClientLocation; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.common.router.ConnectionRouterManager; +import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.tools.spi.ServiceContainer; + +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushRequest implements PushSender.Callback, Runnable { + + private static GatewayClientManage gatewayServerManage = (GatewayClientManage)ServiceContainer.getInstance(ServerManage.class, "psGatewayServerManage"); + + private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); + private PushSender.Callback callback; + private String userId; + private String content; + private long timeout; + private int status = 0; + private long timeout_; + private int sessionId; + private long sendTime; + + public PushRequest() { + } + + public static PushRequest build() { + return new PushRequest(); + } + + public PushRequest setCallback(PushSender.Callback callback) { + this.callback = callback; + return this; + } + + public PushRequest setUserId(String userId) { + this.userId = userId; + return this; + } + + public PushRequest setContent(String content) { + this.content = content; + return this; + } + + public PushRequest setTimeout(long timeout) { + this.timeout = timeout; + return this; + } + + public void setSessionId(int sessionId) { + this.sessionId = sessionId; + } + + public int getSessionId() { + return sessionId; + } + + @Override + public void onSuccess(String userId) { + submit(1); + } + + @Override + public void onFailure(String userId) { + submit(2); + } + + @Override + public void onOffline(String userId) { + submit(3); + } + + @Override + public void onTimeout(String userId) { + submit(4); + } + + private void submit(int status) { + if (this.status != 0) {//防止重复调用 + return; + } + this.status = status; + if (callback != null) { + PushRequestBus.INSTANCE.getExecutor().execute(this); + } else { + LOGGER.warn("callback is null"); + } + } + + @Override + public void run() { + switch (status) { + case 1: + callback.onSuccess(userId); + break; + case 2: + callback.onFailure(userId); + break; + case 3: + callback.onOffline(userId); + break; + case 4: + callback.onTimeout(userId); + break; + } + } + + public boolean isTimeout() { + return System.currentTimeMillis() > timeout_; + } + + public void timeout() { + onTimeout(userId); + } + + public void success() { + onSuccess(userId); + } + + public void failure() { + onFailure(userId); + } + + public void offline() { + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + onOffline(userId); + } + + public void send() { + this.timeout_ = timeout + System.currentTimeMillis(); + sendToConnServer(); + } + + public void redirect() { + this.status = 0; + this.timeout_ = timeout + System.currentTimeMillis(); + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + sendToConnServer(); + LOGGER.warn("user route has changed, userId={}, content={}", userId, content); + } + + private void sendToConnServer() { + //1.查询用户长连接所在的机器 + RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); + if (router == null) { + //1.1没有查到说明用户已经下线 + this.onOffline(userId); + return; + } + + //2.通过网关连接,把消息发送到所在机器 + ClientLocation location = router.getRouteValue(); + Connection gatewayConn = gatewayServerManage.getConnection(location.getHost()); + if (gatewayConn == null || !gatewayConn.isConnected()) { + this.onFailure(userId); + return; + } + + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + sendTime = System.currentTimeMillis(); + } else { + PushRequest.this.onFailure(userId); + } + } + }); + + sessionId = pushMessage.getSessionId(); + PushRequestBus.INSTANCE.put(sessionId, this); + } +} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java index ce5e61be..97cbcfd7 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java @@ -1,44 +1,44 @@ -//package com.shinemo.mpush.ps; -// -//import java.util.Iterator; -//import java.util.Map; -//import java.util.concurrent.*; -// -///** -// * Created by ohun on 2015/12/30. -// */ -//public class PushRequestBus implements Runnable { -// public static final PushRequestBus INSTANCE = new PushRequestBus(); -// private Map requests = new ConcurrentHashMap<>(); -// private Executor executor = Executors.newFixedThreadPool(5);//test -// private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test -// -// public PushRequestBus() { -// scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); -// } -// -// public void put(int sessionId, PushRequest request) { -// requests.put(sessionId, request); -// } -// -// public PushRequest remove(int sessionId) { -// return requests.remove(sessionId); -// } -// -// public Executor getExecutor() { -// return executor; -// } -// -// @Override -// public void run() { -// if (requests.isEmpty()) return; -// Iterator it = requests.values().iterator(); -// while (it.hasNext()) { -// PushRequest request = it.next(); -// if (request.isTimeout()) { -// it.remove();//清除超时的请求 -// request.timeout(); -// } -// } -// } -//} +package com.shinemo.mpush.ps; + +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.*; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushRequestBus implements Runnable { + public static final PushRequestBus INSTANCE = new PushRequestBus(); + private Map requests = new ConcurrentHashMap<>(); + private Executor executor = Executors.newFixedThreadPool(5);//test + private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test + + public PushRequestBus() { + scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); + } + + public void put(int sessionId, PushRequest request) { + requests.put(sessionId, request); + } + + public PushRequest remove(int sessionId) { + return requests.remove(sessionId); + } + + public Executor getExecutor() { + return executor; + } + + @Override + public void run() { + if (requests.isEmpty()) return; + Iterator it = requests.values().iterator(); + while (it.hasNext()) { + PushRequest request = it.next(); + if (request.isTimeout()) { + it.remove();//清除超时的请求 + request.timeout(); + } + } + } +} diff --git a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage new file mode 100644 index 00000000..06dc19ba --- /dev/null +++ b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -0,0 +1 @@ +psGatewayServerManage=com.shinemo.mpush.common.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java deleted file mode 100644 index eb3d9749..00000000 --- a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushClientTest.java +++ /dev/null @@ -1,49 +0,0 @@ -//package com.shinemo.mpush.ps; -// -//import com.shinemo.mpush.api.PushSender; -//import org.junit.Test; -// -//import java.util.Arrays; -//import java.util.concurrent.locks.LockSupport; -// -// -///** -// * Created by ohun on 2016/1/7. -// */ -//public class PushClientTest { -// -// @Test -// public void testSend() throws Exception { -// -// } -// -// public static void main(String[] args) throws Exception { -// PushClient client = new PushClient(); -// client.init(); -// Thread.sleep(1000); -// client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), -// new PushSender.Callback() { -// @Override -// public void onSuccess(String userId) { -// System.err.println("push onSuccess userId=" + userId); -// } -// -// @Override -// public void onFailure(String userId) { -// System.err.println("push onFailure userId=" + userId); -// } -// -// @Override -// public void onOffline(String userId) { -// System.err.println("push onOffline userId=" + userId); -// } -// -// @Override -// public void onTimeout(String userId) { -// System.err.println("push onTimeout userId=" + userId); -// } -// } -// ); -// LockSupport.park(); -// } -//} \ No newline at end of file diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java new file mode 100644 index 00000000..217427c9 --- /dev/null +++ b/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.ps; + +import com.shinemo.mpush.api.PushSender; +import org.junit.Test; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + + +/** + * Created by ohun on 2016/1/7. + */ +public class PushTest { + + @Test + public void testSend() throws Exception { + + } + + public static void main(String[] args) throws Exception { + GatewayClientMain client = new GatewayClientMain(); + client.start(); + Thread.sleep(1000); + client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), + new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + } + ); + LockSupport.park(); + } +} \ No newline at end of file From e5e8dcb30fa520f3026002c869c84c238d0a7820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 14:56:56 +0800 Subject: [PATCH 177/890] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/zk/listener/AbstractDataChangeListener.java | 5 +++-- .../common/zk/listener/impl/GatewayServerPathListener.java | 5 +++++ .../cs/zk/listener/impl/ConnectionServerPathListener.java | 5 +++++ .../{test => main}/java/com/shinemo/mpush/ps/PushTest.java | 7 ------- 4 files changed, 13 insertions(+), 9 deletions(-) rename mpush-ps/src/{test => main}/java/com/shinemo/mpush/ps/PushTest.java (93%) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java index ccf7e8cf..a8057eba 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java @@ -16,7 +16,6 @@ import com.shinemo.mpush.tools.GenericsUtil; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; @@ -57,13 +56,15 @@ public void initData() { public abstract String getRegisterPath(); + public abstract String getFullPath(String raw); + public abstract ServerManage getServerManage(); private void _initData() { // 获取机器列表 List rawData = zkRegister.getChildrenKeys(getRegisterPath()); for (String raw : rawData) { - String fullPath = ZKPath.CONNECTION_SERVER.getFullPath(raw); + String fullPath = getFullPath(raw); T app = getServerApplication(fullPath,clazz); getServerManage().addOrUpdate(fullPath, app); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java index 8056f9c4..7dcc8196 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java @@ -31,6 +31,11 @@ public String getRegisterPath() { return ZKPath.GATEWAY_SERVER.getPath(); } + @Override + public String getFullPath(String raw) { + return ZKPath.GATEWAY_SERVER.getFullPath(raw); + } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java index d15af5c4..325d96dd 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java @@ -31,4 +31,9 @@ public ServerManage getServerManage() { return connectionServerManage; } + @Override + public String getFullPath(String raw) { + return ZKPath.CONNECTION_SERVER.getFullPath(raw); + } + } diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushTest.java similarity index 93% rename from mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/PushTest.java index 217427c9..53c15b4f 100644 --- a/mpush-ps/src/test/java/com/shinemo/mpush/ps/PushTest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushTest.java @@ -1,22 +1,15 @@ package com.shinemo.mpush.ps; import com.shinemo.mpush.api.PushSender; -import org.junit.Test; import java.util.Arrays; import java.util.concurrent.locks.LockSupport; - /** * Created by ohun on 2016/1/7. */ public class PushTest { - @Test - public void testSend() throws Exception { - - } - public static void main(String[] args) throws Exception { GatewayClientMain client = new GatewayClientMain(); client.start(); From c87721a2bbed768616695106a12b0106d0d352ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 15:50:46 +0800 Subject: [PATCH 178/890] add cs client --- .../mpush/core/netty/NettyClientTest.java | 67 ------------------- .../cs/client}/ClientChannelHandler.java | 2 +- .../mpush/cs/client/ConnectionClientMain.java | 24 +++++++ .../com/shinemo/mpush/cs/client/Main.java | 34 ++++++++++ .../com/shinemo/mpush/ps/PushRequest.java | 4 +- ...m.shinemo.mpush.common.manage.ServerManage | 2 +- 6 files changed, 62 insertions(+), 71 deletions(-) delete mode 100644 mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java rename {mpush-core/src/test/java/com/shinemo/mpush/core/netty => mpush-cs/src/main/java/com/shinemo/mpush/cs/client}/ClientChannelHandler.java (99%) create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java create mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java b/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java deleted file mode 100644 index 79501ad2..00000000 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyClientTest.java +++ /dev/null @@ -1,67 +0,0 @@ -//package com.shinemo.mpush.core.netty; -// -// -//import com.shinemo.mpush.api.Client; -//import com.shinemo.mpush.netty.client.NettyClientFactory; -//import com.shinemo.mpush.tools.Jsons; -//import com.shinemo.mpush.tools.Strings; -//import com.shinemo.mpush.tools.spi.ServiceContainer; -//import com.shinemo.mpush.tools.zk.ZKPath; -//import com.shinemo.mpush.tools.zk.ServerApp; -//import com.shinemo.mpush.tools.zk.ZkRegister; -// -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import java.util.ArrayList; -//import java.util.Collections; -//import java.util.List; -// -///** -// * Created by ohun on 2015/12/24. -// */ -//public class NettyClientTest { -// private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class); -// -// private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); -// -// public void setUp() throws Exception { -// } -// -// private List getAllServers() { -// List list = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); -// if (list == null || list.isEmpty()) return Collections.EMPTY_LIST; -// List servers = new ArrayList<>(); -// for (String name : list) { -// String json = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(name)); -// if (Strings.isBlank(json)) continue; -// ServerApp server = Jsons.fromJson(json, ServerApp.class); -// if (server != null) servers.add(server); -// } -// return servers; -// } -// -// public void testClient() throws Exception { -// List serverApps = getAllServers(); -// if (serverApps == null || serverApps.isEmpty()) return; -// int index = (int) ((Math.random() % serverApps.size()) * serverApps.size()); -// ServerApp server = serverApps.get(index); -// ClientChannelHandler handler = new ClientChannelHandler(); -// final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); -// client.init(); -// Thread t = new Thread(new Runnable() { -// @Override -// public void run() { -// client.start(); -// } -// }); -// t.setDaemon(false); -// t.start(); -// } -// -// public static void main(String[] args) throws Exception { -// NettyClientTest test = new NettyClientTest(); -// test.setUp(); -// test.testClient(); -// } -//} diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java similarity index 99% rename from mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java rename to mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java index 99453b8d..99ce4ad9 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/ClientChannelHandler.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.netty; +package com.shinemo.mpush.cs.client; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java new file mode 100644 index 00000000..7ca90c88 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java @@ -0,0 +1,24 @@ +package com.shinemo.mpush.cs.client; + +import java.util.List; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.cs.manage.impl.ConnectionServerManage; +import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; +import com.shinemo.mpush.tools.spi.ServiceContainer; + +public class ConnectionClientMain extends AbstractClient { + + private static ConnectionServerManage connectionServerManage = ServiceContainer.getInstance(ConnectionServerManage.class); + + public ConnectionClientMain() { + registerListener(new ConnectionServerPathListener()); + } + + public List getApplicationList(){ + return Lists.newArrayList(connectionServerManage.getList()); + } + +} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java new file mode 100644 index 00000000..547a6592 --- /dev/null +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -0,0 +1,34 @@ +package com.shinemo.mpush.cs.client; + +import java.util.List; + +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.netty.client.NettyClientFactory; + +public class Main { + + public static void main(String[] args) { + ConnectionClientMain main = new ConnectionClientMain(); + main.start(); + + List serverList = main.getApplicationList(); + + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ConnectionServerApplication server = serverList.get(index); + ClientChannelHandler handler = new ClientChannelHandler(); + final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); + client.init(); + Thread t = new Thread(new Runnable() { + @Override + public void run() { + client.start(); + } + }); + t.setDaemon(false); + t.start(); + + } + +} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java index 9281aba9..0e61e38f 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java @@ -20,7 +20,7 @@ */ public class PushRequest implements PushSender.Callback, Runnable { - private static GatewayClientManage gatewayServerManage = (GatewayClientManage)ServiceContainer.getInstance(ServerManage.class, "psGatewayServerManage"); + private static GatewayClientManage gatewayClientManage = (GatewayClientManage)ServiceContainer.getInstance(ServerManage.class, "gatewayClientManage"); private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); private PushSender.Callback callback; @@ -162,7 +162,7 @@ private void sendToConnServer() { //2.通过网关连接,把消息发送到所在机器 ClientLocation location = router.getRouteValue(); - Connection gatewayConn = gatewayServerManage.getConnection(location.getHost()); + Connection gatewayConn = gatewayClientManage.getConnection(location.getHost()); if (gatewayConn == null || !gatewayConn.isConnected()) { this.onFailure(userId); return; diff --git a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage index 06dc19ba..09794b03 100644 --- a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -1 +1 @@ -psGatewayServerManage=com.shinemo.mpush.common.manage.impl.GatewayServerManage \ No newline at end of file +gatewayClientManage=com.shinemo.mpush.ps.GatewayClientManage \ No newline at end of file From 89cfff04308e8476ef900d1d54970e78476e6c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 16:03:52 +0800 Subject: [PATCH 179/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/cs/client/ConnectionClientMain.java | 5 +++-- mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java index 7ca90c88..d3f30a2b 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java @@ -4,14 +4,15 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.common.manage.ServerManage; import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.cs.manage.impl.ConnectionServerManage; import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; import com.shinemo.mpush.tools.spi.ServiceContainer; public class ConnectionClientMain extends AbstractClient { - private static ConnectionServerManage connectionServerManage = ServiceContainer.getInstance(ConnectionServerManage.class); + @SuppressWarnings("unchecked") + private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); public ConnectionClientMain() { registerListener(new ConnectionServerPathListener()); diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 547a6592..d1b531da 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.cs.client; import java.util.List; +import java.util.concurrent.locks.LockSupport; import com.shinemo.mpush.api.Client; import com.shinemo.mpush.cs.ConnectionServerApplication; @@ -28,6 +29,8 @@ public void run() { }); t.setDaemon(false); t.start(); + + LockSupport.park(); } From a60bc3027d69ba926ec3cdc819e7c068332b70b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 17 Jan 2016 16:42:00 +0800 Subject: [PATCH 180/890] tset --- .../mpush/netty/client/AbstractNettyClientFactory.java | 1 - .../java/com/shinemo/mpush/netty/client/NettyClient.java | 9 --------- 2 files changed, 10 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index 4e63d9d7..63898d5b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.netty.client; import java.text.MessageFormat; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import io.netty.channel.ChannelHandler; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index 78c03450..62621b60 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.netty.client; import java.net.InetSocketAddress; -import java.util.concurrent.TimeUnit; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; @@ -11,18 +10,10 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; - import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.netty.util.NettySharedHolder; public class NettyClient implements Client { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); From 4981ed42aca6c4015ff54fbc093b7718c5af0461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 18 Jan 2016 16:29:00 +0800 Subject: [PATCH 181/890] remove unused --- .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 1 - .../shinemo/mpush/netty/connection/NettyConnectionManager.java | 3 --- 2 files changed, 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 048469f8..461a8d46 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -5,7 +5,6 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; -import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandler; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 01148c35..99dbb295 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -18,10 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; /** From 5d502156e0f3141f18e90adff8eb35d51f114234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 18 Jan 2016 16:52:40 +0800 Subject: [PATCH 182/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0thread=20name=20space?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/common/AbstractServer.java | 3 ++- .../shinemo/mpush/tools/thread/ThreadNameSpace.java | 12 ++++++++++++ .../shinemo/mpush/tools/thread/ThreadPoolUtil.java | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index da3296c9..0abc6124 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -13,6 +13,7 @@ import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.thread.ThreadNameSpace; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; @@ -104,7 +105,7 @@ public void onFailure(String message) { } }); } - }, "conn-server", false).start(); + }, ThreadNameSpace.getServerName(server.getClass().getSimpleName()), false).start(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index 6a88488b..08cfdf1d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -20,5 +20,17 @@ public class ThreadNameSpace { public static final String getUniqueName(String serviceName) { return "mp-sn-" + serviceName; } + + public static final String THREAD_NAME_PREFIX = "mp-t-"; + public static final String getServerName(String serverName){ + if(serverName.equals("ConnectionServer")){ + return "mp-start-cs"; + }else if(serverName.equals("GatewayServer")){ + return "mp-start-gs"; + }else{ + return "mp-start-unknow"; + } + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java index 58d7ef9c..096bb25a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java @@ -6,7 +6,7 @@ public class ThreadPoolUtil { - public static final String THREAD_NAME_PREFIX = "mp-t-"; + private static final ThreadPoolManager threadPoolManager = new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, Constants.THREAD_QUEUE_SIZE); @@ -31,7 +31,7 @@ public static Executor getWorkExecutor() { public static Thread newThread(Runnable r, String name, boolean daemon) { Thread t = new Thread(r); t.setDaemon(daemon); - t.setName(THREAD_NAME_PREFIX + name); + t.setName(ThreadNameSpace.THREAD_NAME_PREFIX + name); return t; } From 939b965f8168412f368d60390aad56fa69f715dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 18 Jan 2016 18:59:25 +0800 Subject: [PATCH 183/890] remove --- .../src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index 15842347..18b320cd 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -8,7 +8,6 @@ import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.security.*; From 9189912dc070a2594ad3cf4ed4ff45ac982e26a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 19 Jan 2016 09:59:01 +0800 Subject: [PATCH 184/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 6 ++--- mpush-cs/pom.xml | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/assembly.xml b/assembly.xml index 187cc819..2f526c20 100644 --- a/assembly.xml +++ b/assembly.xml @@ -11,17 +11,17 @@ - mpush-core/target/ + mpush-cs/target/ lib/*.jar - mpush-core/target/ + mpush-cs/target/ - mpush.jar + mpush-cs.jar diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 65b2f14c..bcaa90c7 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -24,6 +24,16 @@ + mpush-cs + + + + + src/main/resources + true + + + org.apache.maven.plugins @@ -70,6 +80,58 @@ + + + + maven-dependency-plugin + + + copy + package + + copy-dependencies + + + + ${project.build.directory}/lib + + + + + + + maven-jar-plugin + + + + + false + + + + + true + + lib/ + + com.shinemo.mpush.cs.Main + + + + + + package + + + From 42755f52ba020b3b3d88c858882fbfc669b812dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 19 Jan 2016 11:00:59 +0800 Subject: [PATCH 185/890] remove --- .../com/shinemo/mpush/netty/connection/NettyConnection.java | 4 ++++ .../java/com/shinemo/mpush/netty/util/NettySharedHolder.java | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index ca2f23de..9f7bcb14 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -99,6 +99,10 @@ public void operationComplete(ChannelFuture future) throws Exception { LOGGER.error("send msg error"); } } + + public void updateLastWriteTime(){ + lastWriteTime = System.currentTimeMillis(); + } @Override public String toString() { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java index fbc00f44..1d1ebe65 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java @@ -3,9 +3,6 @@ import com.shinemo.mpush.tools.thread.NamedThreadFactory; import com.shinemo.mpush.tools.thread.ThreadNameSpace; - -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.util.HashedWheelTimer; import io.netty.util.Timer; From 1f651fb266f0dbaa567aadf47dbbf556b97cf573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 19 Jan 2016 11:48:49 +0800 Subject: [PATCH 186/890] tostring --- .../mpush/api/connection/SessionContext.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java index 8acffd8b..211a0ba6 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java @@ -43,14 +43,10 @@ public boolean handshakeOk() { return deviceId != null && deviceId.length() > 0; } - @Override - public String toString() { - return "SessionContext{" + - ", osName='" + osName + '\'' + - ", osVersion='" + osVersion + '\'' + - ", clientVersion='" + clientVersion + '\'' + - ", deviceId='" + deviceId + '\'' + - ", heartbeat=" + heartbeat + - '}'; - } + @Override + public String toString() { + return "SessionContext [osName=" + osName + ", osVersion=" + osVersion + ", clientVersion=" + clientVersion + ", deviceId=" + deviceId + ", heartbeat=" + heartbeat + ", cipher=" + cipher + + "]"; + } + } From c984999162447f1a1f028ff699c305abbcdc4046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 19 Jan 2016 12:52:04 +0800 Subject: [PATCH 187/890] add --- .../com/shinemo/mpush/netty/connection/NettyConnection.java | 5 ++++- .../mpush/netty/connection/NettyConnectionManager.java | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 9f7bcb14..34a87c06 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -6,9 +6,11 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.security.CipherBox; + import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,7 +24,8 @@ public final class NettyConnection implements Connection, ChannelFutureListener private Channel channel; private volatile int status = STATUS_NEW; private long lastReadTime; - private long lastWriteTime; + @SuppressWarnings("unused") + private long lastWriteTime; @Override public void init(Channel channel, boolean security) { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 99dbb295..ef5bab33 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -68,6 +68,8 @@ private class HeartbeatCheckTask implements TimerTask { public HeartbeatCheckTask(int heartbeat, Connection connection) { this.heartbeat = heartbeat; this.connection = connection; + Thread.currentThread().setName("heart-beat-check-task:"+connection.getId()); + LOGGER.warn("heart-beat-check-task:"+connection.getId()+",start"); } public void startTimeout() { From 1b435c92ab9a50b9f652066965a794c42b46afcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 19 Jan 2016 21:20:27 +0800 Subject: [PATCH 188/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=BE=AE=E8=B0=83?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E8=83=BD=E7=BC=96=E8=AF=91=E4=B8=8D=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E3=80=82=E3=80=82=E3=80=82=E6=98=8E=E5=A4=A9=E7=BB=A7?= =?UTF-8?q?=E7=BB=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Client.java | 20 ++- .../mpush/api/connection/Connection.java | 6 + .../api/connection/ConnectionManager.java | 6 + .../mpush/common/security/RsaCipher.java | 6 + .../mpush/core/server/ConnectionServer.java | 11 +- .../core/server/ServerChannelHandler.java | 2 + .../com/shinemo/mpush/cs/client/Main.java | 4 +- .../client/AbstractNettyClientFactory.java | 31 ++-- .../mpush/netty/client/NettyClient.java | 152 ++++++++++-------- .../netty/client/NettyClientFactory.java | 58 ++++++- .../netty/connection/NettyConnection.java | 29 +++- .../connection/NettyConnectionManager.java | 113 +++++++------ .../mpush/netty/server/NettyServer.java | 3 + .../server/ScanAllConnectionTimerTask.java | 67 ++++++++ .../mpush/tools/config/ConfigCenter.java | 4 + 15 files changed, 357 insertions(+), 155 deletions(-) create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index ba722632..6b956c70 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -1,19 +1,25 @@ package com.shinemo.mpush.api; -import io.netty.channel.ChannelHandler; public interface Client { - void init(); - void start(); + boolean isConnected(); - void stop(); + String getHost(); - boolean isConnected(); + int getPort(); + + void close(String cause); + + boolean isEnabled(); + + void resetHbTimes(); + + int inceaseAndGetHbTimes(); - String getUri(); + String getUrl(); - ChannelHandler getHandler(); + void startHeartBeat() throws Exception; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java index e4a06d05..6b5b86d8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java @@ -34,4 +34,10 @@ public interface Connection { boolean heartbeatTimeout(); void updateLastReadTime(); + + int inceaseAndGetHbTimes(); + + void resetHbTimes(); + + long getLastReadTime(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java index 3b475ab4..b76c1f20 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.api.connection; +import java.util.List; + import io.netty.channel.Channel; /** @@ -12,4 +14,8 @@ public interface ConnectionManager { void remove(Channel channel); void add(Connection connection); + + List getConnections(); + + void init(); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java index b97f3e82..c00b4d55 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java @@ -28,4 +28,10 @@ public byte[] decrypt(byte[] data) { public byte[] encrypt(byte[] data) { return RSAUtils.encryptByPublicKey(data, publicKey); } + + @Override + public String toString() { + return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; + } + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 241f4ebd..d3b92876 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -1,5 +1,8 @@ package com.shinemo.mpush.core.server; +import java.util.concurrent.TimeUnit; + +import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; import com.shinemo.mpush.core.handler.BindUserHandler; @@ -8,6 +11,10 @@ import com.shinemo.mpush.core.handler.HeartBeatHandler; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; +import com.shinemo.mpush.netty.server.ScanAllConnectionTimerTask; +import com.shinemo.mpush.netty.util.NettySharedHolder; +import com.shinemo.mpush.tools.config.ConfigCenter; + import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -17,9 +24,12 @@ */ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; + + private ConnectionManager connectionManager = new NettyConnectionManager(); public ConnectionServer(int port) { super(port); + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); } @Override @@ -29,7 +39,6 @@ public void init() { receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - NettyConnectionManager connectionManager = new NettyConnectionManager(); connectionManager.init(); channelHandler = new ServerChannelHandler(true, connectionManager, receiver); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 461a8d46..787d325e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -11,6 +11,7 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +39,7 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); + LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); connection.updateLastReadTime(); receiver.onReceive((Packet) msg, connection); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index d1b531da..abba9f30 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -9,7 +9,7 @@ public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { ConnectionClientMain main = new ConnectionClientMain(); main.start(); @@ -19,7 +19,7 @@ public static void main(String[] args) { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); ClientChannelHandler handler = new ClientChannelHandler(); - final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler); + final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler); client.init(); Thread t = new Thread(new Runnable() { @Override diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index 63898d5b..a9ca28a3 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -1,9 +1,11 @@ package com.shinemo.mpush.netty.client; import java.text.MessageFormat; +import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import io.netty.channel.ChannelHandler; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,21 +50,32 @@ public Client get(final String remoteHost, final int port) { String key = String.format(format, remoteHost, port); return cachedClients.getIfPresent(key); } - - public Client createGet(String remoteHost, int port, ChannelHandler handler) { - Client client = createClient(remoteHost, port, handler); - if (client != null) { - String key = String.format(format, remoteHost, port); - cachedClients.put(key, client); - } - return client; + + public Client get(final String remoteHost,final int port,final ChannelHandler handler) throws Exception{ + String key = String.format(format, remoteHost, port); + Client client = cachedClients.get(key, new Callable() { + @Override + public Client call() throws Exception { + Client client = createClient(remoteHost, port, handler); + if(client!=null){ + client.startHeartBeat(); + } + return client; + } + }); + if(client == null || !client.isConnected()){ + cachedClients.invalidate(key); + return null; + } + return client; } + abstract Client createClient(String remoteHost, int port, ChannelHandler handler); public void remove(Client client) { if (client != null) { - cachedClients.invalidate(client.getUri()); + cachedClients.invalidate(client.getUrl()); LOGGER.warn(MessageFormat.format("[Remoting] {0} is removed", client)); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index 62621b60..dc56237a 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -1,95 +1,109 @@ package com.shinemo.mpush.netty.client; -import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; -import com.shinemo.mpush.netty.codec.PacketDecoder; -import com.shinemo.mpush.netty.codec.PacketEncoder; -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.Timeout; +import io.netty.util.TimerTask; + +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.netty.util.NettySharedHolder; +import com.shinemo.mpush.tools.config.ConfigCenter; public class NettyClient implements Client { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); + + private static final String format = "%s:%s"; + + private static final Logger log = LoggerFactory.getLogger(NettyClient.class); - private final ChannelHandler handler; private final String host; private final int port; private Channel channel; + private int hbTimes; - public NettyClient(final String host, final int port, ChannelHandler handler) { + public NettyClient(final String host, final int port, Channel channel) { this.host = host; this.port = port; - this.handler = handler; + this.channel = channel; } @Override - public void init() { - this.stop(); - EventLoopGroup workerGroup = new NioEventLoopGroup(); - final Bootstrap bootstrap = new Bootstrap(); - bootstrap.group(workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(handler); - } - }); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - channel = future.channel(); - } else { - future.cancel(true); - future.channel().close(); - LOGGER.warn("[remoting] failure to connect:" + host + "," + port); - } - } + public void close(String cause) { + if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { + log.error("close channel:"+cause); + } + this.channel.close(); + } - @Override - public void start() { - if (channel != null) { - try { - channel.closeFuture().sync(); - } catch (InterruptedException e) { - } - } - } + @Override + public boolean isEnabled() { + return channel.isWritable(); + } - @Override - public void stop() { - if (channel != null) { - channel.close(); - } - } + @Override + public boolean isConnected() { + return channel.isActive(); + } - @Override - public boolean isConnected() { - return channel.isActive(); - } + @Override + public void resetHbTimes() { + hbTimes = 0; + } - @Override - public String getUri() { - return host + ":" + port; - } + @Override + public int inceaseAndGetHbTimes() { + return ++hbTimes; + } + + @Override + public void startHeartBeat() throws Exception { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + try { + ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); + } + log.warn("client send msg hb false:" + channel.remoteAddress().toString()); + } else { + log.warn("client send msg hb success:" + channel.remoteAddress().toString()); + } + } + }); + } finally { + if (channel.isActive()) { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); + } + } + } + }, ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); + } + + + @Override + public String getUrl() { + return String.format(format, host, port); + } + + @Override + public String getHost() { + return host; + } + + @Override + public int getPort() { + return port; + } - @Override - public ChannelHandler getHandler() { - return handler; - } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 40351588..19857f0b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -1,14 +1,68 @@ package com.shinemo.mpush.netty.client; +import java.net.InetSocketAddress; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.netty.codec.PacketDecoder; +import com.shinemo.mpush.netty.codec.PacketEncoder; public class NettyClientFactory extends AbstractNettyClientFactory { + + private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - public Client createClient(String host, int port, ChannelHandler handler) { - return new NettyClient(host, port, handler); + public Client createClient(String host, int port, final ChannelHandler handler) { + + EventLoopGroup workerGroup = new NioEventLoopGroup(); + final Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .handler(handler) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(handler); + } + }); + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { + Channel channel = future.channel(); + Client client = new NettyClient(host,port, channel); + return client; + } else { + future.cancel(true); + future.channel().close(); + log.warn("[remoting] failure to connect:" + host+","+port); + } + return null; } + + public Client getClient(final Client client) throws Exception { + return get(client.getHost(),client.getPort()); + } + + public void remove(final Client client) { + super.remove(client); + } + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 34a87c06..fbed0d84 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -24,8 +24,9 @@ public final class NettyConnection implements Connection, ChannelFutureListener private Channel channel; private volatile int status = STATUS_NEW; private long lastReadTime; - @SuppressWarnings("unused") private long lastWriteTime; + + private int hbTimes = 0; @Override public void init(Channel channel, boolean security) { @@ -93,6 +94,11 @@ public boolean heartbeatTimeout() { public void updateLastReadTime() { lastReadTime = System.currentTimeMillis(); } + + @Override + public long getLastReadTime(){ + return lastReadTime; + } @Override public void operationComplete(ChannelFuture future) throws Exception { @@ -106,13 +112,22 @@ public void operationComplete(ChannelFuture future) throws Exception { public void updateLastWriteTime(){ lastWriteTime = System.currentTimeMillis(); } + + @Override + public int inceaseAndGetHbTimes() { + return ++hbTimes; + } @Override - public String toString() { - return "NettyConnection{" + - "context=" + context + - ", channel=" + channel + - ", status=" + status + - '}'; + public void resetHbTimes() { + hbTimes = 0; } + + @Override + public String toString() { + return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes + + "]"; + } + + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index ef5bab33..b24bf6fc 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -1,97 +1,94 @@ package com.shinemo.mpush.netty.connection; -import com.google.common.eventbus.Subscribe; +import com.google.common.collect.Lists; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.ConnectionManager; -import com.shinemo.mpush.api.event.HandshakeEvent; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.channel.Channel; -import io.netty.util.HashedWheelTimer; -import io.netty.util.Timeout; -import io.netty.util.Timer; -import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import java.util.List; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/22. */ public final class NettyConnectionManager implements ConnectionManager { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectionManager.class); //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); - private Timer wheelTimer; +// private Timer wheelTimer; + @Override public void init() { - //每秒钟走一步,一个心跳周期内走一圈 - long tickDuration = 1000;//1s - int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); - this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); - EventBus.INSTANCE.register(this); +// //每秒钟走一步,一个心跳周期内走一圈 +// long tickDuration = 1000;//1s +// int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); +// this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); +// EventBus.INSTANCE.register(this); } + @Override public Connection get(final Channel channel) { return connections.get(channel.id().asLongText()); } + @Override public void add(Connection connection) { connections.putIfAbsent(connection.getId(), connection); } + @Override public void remove(Channel channel) { Connection connection = connections.remove(channel.id().asLongText()); if (connection != null) { connection.close(); } } - - @Subscribe - void onHandshakeOk(HandshakeEvent event) { - HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); - task.startTimeout(); + + @Override + public List getConnections() { + return Lists.newArrayList(connections.values()); } - private class HeartbeatCheckTask implements TimerTask { - private int expiredTimes = 0; - private final int heartbeat; - private final Connection connection; - - public HeartbeatCheckTask(int heartbeat, Connection connection) { - this.heartbeat = heartbeat; - this.connection = connection; - Thread.currentThread().setName("heart-beat-check-task:"+connection.getId()); - LOGGER.warn("heart-beat-check-task:"+connection.getId()+",start"); - } - - public void startTimeout() { - wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); - } - - @Override - public void run(Timeout timeout) throws Exception { - if (!connection.isConnected()) return; - if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { - connection.close(); - LOGGER.error("connection heartbeat timeout, connection has bean closed"); - return; - } else { - LOGGER.error("connection heartbeat timeout, expiredTimes=" + expiredTimes); - } - } else { - expiredTimes = 0; - //LOGGER.info("check heartbeat timeout"); - } - startTimeout(); - } - } +// @Subscribe +// void onHandshakeOk(HandshakeEvent event) { +// HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); +// task.startTimeout(); +// } +// +// private class HeartbeatCheckTask implements TimerTask { +// private int expiredTimes = 0; +// private final int heartbeat; +// private final Connection connection; +// +// public HeartbeatCheckTask(int heartbeat, Connection connection) { +// this.heartbeat = heartbeat; +// this.connection = connection; +// LOGGER.warn("heart-beat-check-task:"+Thread.currentThread().getName()+",start"); +// } +// +// public void startTimeout() { +// wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); +// } +// +// @Override +// public void run(Timeout timeout) throws Exception { +// if (!connection.isConnected()) return; +// if (connection.heartbeatTimeout()) { +// if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { +// connection.close(); +// LOGGER.error("connection heartbeat timeout, connection has bean closed"); +// return; +// } else { +// LOGGER.error("connection heartbeat timeout, expiredTimes=" + expiredTimes); +// } +// } else { +// expiredTimes = 0; +// //LOGGER.info("check heartbeat timeout"); +// } +// startTimeout(); +// } +// } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 45abe40e..9b868f72 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -4,12 +4,14 @@ import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; import com.shinemo.mpush.tools.thread.ThreadPoolUtil; + import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,6 +31,7 @@ public abstract class NettyServer implements Server { public NettyServer(int port) { this.port = port; + } public abstract void init(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java new file mode 100644 index 00000000..3a6bbccd --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java @@ -0,0 +1,67 @@ +package com.shinemo.mpush.netty.server; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.ConnectionManager; +import com.shinemo.mpush.netty.util.NettySharedHolder; +import com.shinemo.mpush.tools.config.ConfigCenter; + +import io.netty.util.Timeout; +import io.netty.util.TimerTask; + +public class ScanAllConnectionTimerTask implements TimerTask{ + + private static final Logger log = LoggerFactory.getLogger(ScanAllConnectionTimerTask.class); + + private ConnectionManager connectionManager; + + private static int maxHBTimeoutTimes = ConfigCenter.holder.maxHBTimeoutTimes(); + + // 180S也许是个不错的选择,微信的心跳时间为300S + private static long scanConnTaskCycle = ConfigCenter.holder.scanConnTaskCycle(); + + public ScanAllConnectionTimerTask(ConnectionManager connectionManager) { + this.connectionManager = connectionManager; + } + + @Override + public void run(Timeout timeout) throws Exception { + log.warn("start deal ScanAllConnectionTimerTask "); + try { + long now = System.currentTimeMillis(); + List connections = connectionManager.getConnections(); + if (connections != null) { + for (final Connection conn : connections) { + if (!conn.isConnected()) { + log.warn("connect is not connected: "+conn); + return; + } + long betwen = now - conn.getLastReadTime(); + if (betwen > scanConnTaskCycle) { + int expiredTimes = conn.inceaseAndGetHbTimes(); + if (expiredTimes > maxHBTimeoutTimes) { + conn.close(); + log.error("connection heartbeat timeout, connection has bean closed:"+conn); + } else { + log.error("connection heartbeat timeout, expiredTimes=" + expiredTimes+","+conn); + } + } else { + conn.resetHbTimes(); + log.warn("connection heartbeat reset, expiredTimes=0,betwen:"+betwen+","+conn+",lastReadTime:"+conn.getLastReadTime()+",now:"+now); + } + } + } + } catch (Throwable e) { + log.error("error during ScanAllConnectionTimerTask", e); + } finally { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, scanConnTaskCycle/1000, TimeUnit.SECONDS); + } + + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index e2c8d961..08113656 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -87,4 +87,8 @@ public interface ConfigCenter extends Config{ @ConverterClass(RedisGroupConverter.class) public List redisGroups(); + @Key("scan_conn_task_cycle") + @DefaultValue("59000") + public long scanConnTaskCycle(); + } From fbe605d8880ee176614bbd479fde383a76926af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 17 Jan 2016 07:18:44 +0000 Subject: [PATCH 189/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/api/protocol/Packet.java | 9 + .../mpush/common/message/BindUserMessage.java | 2 +- .../mpush/common/message/ErrorMessage.java | 4 +- .../common/message/FastConnectMessage.java | 4 +- .../common/message/HandshakeMessage.java | 4 +- .../mpush/common/message/KickUserMessage.java | 4 +- .../mpush/common/message/OkMessage.java | 4 +- .../mpush/common/message/PushMessage.java | 4 +- .../message/gateway/GatewayPushMessage.java | 4 +- .../mpush/netty/codec/PacketDecoder.java | 2 +- .../mpush/tools/config/ConfigCenter.java | 164 +++++++++--------- 11 files changed, 114 insertions(+), 91 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 63b3e8fe..269c5e02 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -31,6 +31,15 @@ public Packet(byte cmd, int sessionId) { this.sessionId = sessionId; } + public Packet(Command cmd) { + this.cmd = cmd.cmd; + } + + public Packet(Command cmd, int sessionId) { + this.cmd = cmd.cmd; + this.sessionId = sessionId; + } + public int getBodyLength() { return body == null ? 0 : body.length; } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java index b510db9f..a40853a7 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java @@ -14,7 +14,7 @@ public final class BindUserMessage extends ByteBufMessage { public String tags; public BindUserMessage(Connection connection) { - super(new Packet(Command.BIND.cmd, genSessionId()), connection); + super(new Packet(Command.BIND, genSessionId()), connection); } public BindUserMessage(Packet message, Connection connection) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index 8d69e35a..c0ac0e75 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -7,6 +7,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; +import static com.shinemo.mpush.api.protocol.Command.ERROR; + /** * Created by ohun on 2015/12/28. */ @@ -39,7 +41,7 @@ public void encode(ByteBuf body) { } public static ErrorMessage from(BaseMessage src) { - return new ErrorMessage(src.packet.cmd, new Packet(Command.ERROR.cmd + return new ErrorMessage(src.packet.cmd, new Packet(ERROR , src.packet.sessionId), src.connection); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java index 64295bf6..c2d0567a 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java @@ -5,6 +5,8 @@ import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import static com.shinemo.mpush.api.protocol.Command.FAST_CONNECT; + /** * Created by ohun on 2015/12/25. */ @@ -15,7 +17,7 @@ public final class FastConnectMessage extends ByteBufMessage { public int maxHeartbeat; public FastConnectMessage(Connection connection) { - super(new Packet(Command.FAST_CONNECT.cmd, genSessionId()), connection); + super(new Packet(FAST_CONNECT, genSessionId()), connection); } public FastConnectMessage(Packet message, Connection connection) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java index 3a1b2172..0840c340 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java @@ -5,6 +5,8 @@ import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import static com.shinemo.mpush.api.protocol.Command.HANDSHAKE; + /** * Created by ohun on 2015/12/24. */ @@ -20,7 +22,7 @@ public final class HandshakeMessage extends ByteBufMessage { public long timestamp; public HandshakeMessage(Connection connection) { - super(new Packet(Command.HANDSHAKE.cmd, genSessionId()), connection); + super(new Packet(HANDSHAKE, genSessionId()), connection); } public HandshakeMessage(Packet message, Connection connection) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java index 07b84c18..7fac42ac 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java @@ -5,6 +5,8 @@ import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import static com.shinemo.mpush.api.protocol.Command.KICK; + /** * Created by ohun on 2015/12/29. */ @@ -13,7 +15,7 @@ public class KickUserMessage extends ByteBufMessage { public String userId; public KickUserMessage(Connection connection) { - super(new Packet(Command.KICK.cmd), connection); + super(new Packet(KICK), connection); } public KickUserMessage(Packet message, Connection connection) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java index e784a40c..5c4ffebd 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java @@ -5,6 +5,8 @@ import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import static com.shinemo.mpush.api.protocol.Command.OK; + /** * Created by ohun on 2015/12/28. */ @@ -37,7 +39,7 @@ public void encode(ByteBuf body) { } public static OkMessage from(BaseMessage src) { - return new OkMessage(src.packet.cmd, new Packet(Command.OK.cmd + return new OkMessage(src.packet.cmd, new Packet(OK , src.packet.sessionId), src.connection); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java index fe3766d2..380a6256 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java @@ -5,6 +5,8 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import static com.shinemo.mpush.api.protocol.Command.PUSH; + /** * Created by ohun on 2015/12/30. */ @@ -17,7 +19,7 @@ public PushMessage(Packet packet, Connection connection) { } public PushMessage(String content, Connection connection) { - super(new Packet(Command.PUSH.cmd, genSessionId()), connection); + super(new Packet(PUSH, genSessionId()), connection); this.content = content; } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java index ad115eff..34c03ed9 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java @@ -6,6 +6,8 @@ import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import static com.shinemo.mpush.api.protocol.Command.GATEWAY_PUSH; + /** * Created by ohun on 2015/12/30. */ @@ -14,7 +16,7 @@ public class GatewayPushMessage extends ByteBufMessage { public String content; public GatewayPushMessage(String userId, String content, Connection connection) { - super(new Packet(Command.GATEWAY_PUSH.cmd, genSessionId()), connection); + super(new Packet(GATEWAY_PUSH, genSessionId()), connection); this.userId = userId; this.content = content; } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index 3b3e77bf..b71e0eff 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -27,7 +27,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET) { - out.add(new Packet(Command.HEARTBEAT.cmd)); + out.add(new Packet(Command.HEARTBEAT)); } else { in.readerIndex(in.readerIndex() - 1); break; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 08113656..a6f68da6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -1,94 +1,94 @@ package com.shinemo.mpush.tools.config; -import java.util.List; - +import com.shinemo.mpush.tools.redis.RedisGroup; import org.aeonbits.owner.Config; -import org.aeonbits.owner.ConfigFactory; import org.aeonbits.owner.Config.Sources; +import org.aeonbits.owner.ConfigFactory; -import com.shinemo.mpush.tools.redis.RedisGroup; +import java.util.List; /** * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 - * */ @Sources({"classpath:config.properties"}) -public interface ConfigCenter extends Config{ - - public static ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); - - @Key("max_packet_size") - @DefaultValue("10240") - public int maxPacketSize(); - - @Key("compress_limit") - @DefaultValue("10240") - public int compressLimit(); - - @Key("min_heartbeat") - @DefaultValue("10000") //10s - public int minHeartbeat(); - - @Key("max_heartbeat") - @DefaultValue("1800000") //30min - public int maxHeartbeat(); - - @Key("max_hb_timeout_times") - @DefaultValue("2") - public int maxHBTimeoutTimes(); - - @Key("session_expired_time") - @DefaultValue("86400") //unit second - public int sessionExpiredTime(); - - @Key("ras_key_length") - @DefaultValue("1024") - public int rasKeyLength(); - - @Key("aes_key_length") - @DefaultValue("16") - public int aesKeyLength(); - - @Key("connection_server_port") - @DefaultValue("3000") - public int connectionServerPort(); - - @Key("gateway_server_port") - @DefaultValue("4000") - public int gatewayServerPort(); - - @Key("private_key") - @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") - public String privateKey(); - - @Key("public_key") - @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") - public String publicKey(); - - @Key("redis_ip") - @DefaultValue("127.0.0.1:6379:ShineMoIpo") - public String redisIp(); - - @Key("zk_ip") - @DefaultValue("127.0.0.1:2181") - public String zkIp(); - - @Key("zk_namespace") - @DefaultValue("mpush") - public String zkNamespace(); - - @Key("zk_digest") - @DefaultValue("shinemoIpo") - public String zkDigest(); - - @Separator(";") - @Key("redis_group") - @ConverterClass(RedisGroupConverter.class) - public List redisGroups(); - - @Key("scan_conn_task_cycle") - @DefaultValue("59000") - public long scanConnTaskCycle(); - +public interface ConfigCenter extends Config { + + ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); + + @Key("max_packet_size") + @DefaultValue("10240") + int maxPacketSize(); + + @Key("compress_limit") + @DefaultValue("10240") + int compressLimit(); + + @Key("min_heartbeat") + @DefaultValue("10000") + //10s + int minHeartbeat(); + + @Key("max_heartbeat") + @DefaultValue("1800000") + //30min + int maxHeartbeat(); + + @Key("max_hb_timeout_times") + @DefaultValue("2") + int maxHBTimeoutTimes(); + + @Key("session_expired_time") + @DefaultValue("86400") + //unit second + int sessionExpiredTime(); + + @Key("ras_key_length") + @DefaultValue("1024") + int rasKeyLength(); + + @Key("aes_key_length") + @DefaultValue("16") + int aesKeyLength(); + + @Key("connection_server_port") + @DefaultValue("3000") + int connectionServerPort(); + + @Key("gateway_server_port") + @DefaultValue("4000") + int gatewayServerPort(); + + @Key("private_key") + @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") + String privateKey(); + + @Key("public_key") + @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") + String publicKey(); + + @Key("redis_ip") + @DefaultValue("127.0.0.1:6379:ShineMoIpo") + String redisIp(); + + @Key("zk_ip") + @DefaultValue("127.0.0.1:2181") + String zkIp(); + + @Key("zk_namespace") + @DefaultValue("mpush") + String zkNamespace(); + + @Key("zk_digest") + @DefaultValue("shinemoIpo") + String zkDigest(); + + @Separator(";") + @Key("redis_group") + @ConverterClass(RedisGroupConverter.class) + List redisGroups(); + + @Key("scan_conn_task_cycle") + @DefaultValue("59000") + long scanConnTaskCycle(); } From 8d492b60c6c910627bbe18435a34d854b24576b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 20 Jan 2016 02:05:52 +0000 Subject: [PATCH 190/890] add netty epoll module --- .../mpush/core/server/ConnectionServer.java | 1 + .../mpush/core/server/GatewayServer.java | 1 + mpush-netty/pom.xml | 6 +- .../mpush/netty/server/NettyServer.java | 58 +++++++++++++------ pom.xml | 7 +++ 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index d3b92876..16880bdf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -34,6 +34,7 @@ public ConnectionServer(int port) { @Override public void init() { + super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java index 70df955d..2d2c7df8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -20,6 +20,7 @@ public GatewayServer(int port) { @Override public void init() { + super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); NettyConnectionManager connectionManager = new NettyConnectionManager(); diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 27db2823..977482ff 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -26,6 +26,10 @@ io.netty netty-codec - + + io.netty + netty-transport-native-epoll + ${os.detected.classifier} + diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 9b868f72..00a1bc88 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -8,6 +8,8 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; +import io.netty.channel.epoll.EpollEventLoopGroup; +import io.netty.channel.epoll.EpollServerSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; @@ -15,7 +17,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; /** * Created by ohun on 2015/12/22. @@ -23,41 +25,50 @@ public abstract class NettyServer implements Server { private static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class); - private final AtomicBoolean startFlag = new AtomicBoolean(false); + + public enum State {Created, Initialized, Starting, Started, Shutdown} + + protected final AtomicReference serverState = new AtomicReference(State.Created); + private final int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; - private boolean hasInit = false; public NettyServer(int port) { this.port = port; } - public abstract void init(); + public void init() { + if (!serverState.compareAndSet(State.Created, State.Initialized)) { + throw new IllegalStateException("Server already init"); + } + } @Override public boolean isRunning() { - return startFlag.get(); + return serverState.get() == State.Started; } @Override public void stop(Listener listener) { - LOGGER.info("netty server stop now"); - this.startFlag.set(false); + if (!serverState.compareAndSet(State.Started, State.Shutdown)) { + throw new IllegalStateException("The server is already shutdown."); + } if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); + LOGGER.warn("netty server stop now"); } @Override public void start(final Listener listener) { - if (!startFlag.compareAndSet(false, true)) { - return; - } - if (!hasInit) { - hasInit = true; - init(); + if (!serverState.compareAndSet(State.Initialized, State.Starting)) { + throw new IllegalStateException("Server already started or have not init"); } + createNioServer(listener); + } + + private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 @@ -68,8 +79,8 @@ public void start(final Listener listener) { * 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现, * 并且可以通过构造函数来配置他们的关系。 */ - this.bossGroup = new NioEventLoopGroup(1, ThreadPoolUtil.getBossExecutor()); - this.workerGroup = new NioEventLoopGroup(0, ThreadPoolUtil.getWorkExecutor()); + this.bossGroup = boss; + this.workerGroup = work; try { @@ -89,7 +100,7 @@ public void start(final Listener listener) { * ServerSocketChannel以NIO的selector为基础进行实现的,用来接收新的连接 * 这里告诉Channel如何获取新的连接. */ - b.channel(NioServerSocketChannel.class); + b.channel(clazz); /*** @@ -127,7 +138,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } } }); - + serverState.set(State.Started); /** * 这里会一直等待,直到socket被关闭 */ @@ -144,6 +155,19 @@ public void operationComplete(ChannelFuture future) throws Exception { } } + private void createNioServer(final Listener listener) { + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolUtil.getBossExecutor()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolUtil.getWorkExecutor()); + createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); + } + + + private void createEpollServer(final Listener listener) { + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolUtil.getBossExecutor()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolUtil.getWorkExecutor()); + createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); + } + protected void initOptions(ServerBootstrap b) { /*** diff --git a/pom.xml b/pom.xml index f0bd3991..fb64703b 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT 5.0.0.Alpha2 + linux-x86_64 @@ -56,6 +57,12 @@ netty-transport ${netty.version} + + io.netty + netty-transport-native-epoll + ${netty.version} + ${os.detected.classifier} + From e143c920df5259e9c68be093847a12b78f2655aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 20 Jan 2016 02:09:06 +0000 Subject: [PATCH 191/890] add netty epoll module --- .../main/java/com/shinemo/mpush/tools/config/ConfigCenter.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index a6f68da6..4f8cd93d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -26,12 +26,10 @@ public interface ConfigCenter extends Config { @Key("min_heartbeat") @DefaultValue("10000") - //10s int minHeartbeat(); @Key("max_heartbeat") @DefaultValue("1800000") - //30min int maxHeartbeat(); @Key("max_hb_timeout_times") @@ -40,7 +38,6 @@ public interface ConfigCenter extends Config { @Key("session_expired_time") @DefaultValue("86400") - //unit second int sessionExpiredTime(); @Key("ras_key_length") From 8fde21e3b76bf17cabdd94341be194cd59db4ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 20 Jan 2016 20:13:38 +0800 Subject: [PATCH 192/890] =?UTF-8?q?client=20=E6=9A=82=E6=97=B6=E8=BF=99?= =?UTF-8?q?=E6=A0=B7=E6=94=B9=E4=BA=86=E3=80=82=E3=80=82=E3=80=82=E6=98=8E?= =?UTF-8?q?=E5=A4=A9=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Client.java | 12 + .../core/client/ClientChannelHandler.java | 192 +++++++++++++++ .../mpush/cs/client/ClientChannelHandler.java | 224 ------------------ .../com/shinemo/mpush/cs/client/Main.java | 22 +- .../client/AbstractNettyClientFactory.java | 32 ++- .../mpush/netty/client/NettyClient.java | 28 ++- .../netty/client/NettyClientFactory.java | 16 +- .../netty/client/SecurityNettyClient.java | 88 +++++++ .../connection/NettyConnectionManager.java | 46 ---- .../shinemo/mpush/ps/GatewayClientManage.java | 13 +- 10 files changed, 378 insertions(+), 295 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index 6b956c70..24f6915b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -1,5 +1,9 @@ package com.shinemo.mpush.api; +import io.netty.channel.Channel; + +import com.shinemo.mpush.api.connection.Connection; + public interface Client { @@ -21,5 +25,13 @@ public interface Client { String getUrl(); void startHeartBeat() throws Exception; + + void stop(); + + Connection getConnection(); + + Channel getChannel(); + + void initConnection(Connection connection); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java new file mode 100644 index 00000000..ce7b2dc3 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -0,0 +1,192 @@ +package com.shinemo.mpush.core.client; + + + +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.common.message.BindUserMessage; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.FastConnectOkMessage; +import com.shinemo.mpush.common.message.HandshakeMessage; +import com.shinemo.mpush.common.message.HandshakeOkMessage; +import com.shinemo.mpush.common.message.KickUserMessage; +import com.shinemo.mpush.common.message.OkMessage; +import com.shinemo.mpush.common.message.PushMessage; +import com.shinemo.mpush.common.security.AesCipher; +import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.netty.client.SecurityNettyClient; +import com.shinemo.mpush.netty.connection.NettyConnection; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/19. + */ +@ChannelHandler.Sharable +public final class ClientChannelHandler extends ChannelHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); + client.getConnection().updateLastReadTime(); + if(client instanceof SecurityNettyClient){ + + SecurityNettyClient securityNettyClient = (SecurityNettyClient)client; + + Connection connection = client.getConnection(); + //加密 + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + Command command = Command.toCMD(packet.cmd); + if (command == Command.HANDSHAKE) { + connection.getSessionContext().changeCipher(new AesCipher(securityNettyClient.getClientKey(), securityNettyClient.getIv())); + HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); + byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); + connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); + client.startHeartBeat(); + LOGGER.info("会话密钥:{},message={}", sessionKey, message); + bindUser(securityNettyClient); + } else if (command == Command.FAST_CONNECT) { + String cipherStr = securityNettyClient.getCipher(); + String[] cs = cipherStr.split(","); + byte[] key = AesCipher.toArray(cs[0]); + byte[] iv = AesCipher.toArray(cs[1]); + connection.getSessionContext().changeCipher(new AesCipher(key, iv)); + + FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); + client.startHeartBeat(); + bindUser(securityNettyClient); + LOGGER.info("fast connect success, message=" + message); + } else if (command == Command.KICK) { + KickUserMessage message = new KickUserMessage(packet, connection); + LOGGER.error("receive kick user userId={}, deviceId={}, message={},",securityNettyClient.getUserId() , securityNettyClient.getDeviceId(), message); + ctx.close(); + } else if (command == Command.ERROR) { + ErrorMessage errorMessage = new ErrorMessage(packet, connection); + LOGGER.error("receive an error packet=" + errorMessage); + } else if (command == Command.BIND) { + OkMessage okMessage = new OkMessage(packet, connection); + LOGGER.info("receive an success packet=" + okMessage); + } else if (command == Command.PUSH) { + PushMessage message = new PushMessage(packet, connection); + LOGGER.info("receive an push message, content=" + message.content); + } + } + + }else{ + //不加密 + } + LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); + NettyClientFactory.INSTANCE.remove(client); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + Connection connection = new NettyConnection(); + Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); + if(client instanceof SecurityNettyClient){ + + tryFastConnect((SecurityNettyClient)client); + }else{ + connection.init(ctx.channel(), false); + } + client.initConnection(connection); + + if(client instanceof SecurityNettyClient){ + connection.init(ctx.channel(), true); + } + + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client disconnect channel={}", ctx.channel()); + Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); + NettyClientFactory.INSTANCE.remove(client); + } + + private void tryFastConnect(SecurityNettyClient securityNettyClient) { + handshake(securityNettyClient); +// if (sessionTickets == null) { +// +// return; +// } +// String sessionId = (String) sessionTickets.get("sessionId"); +// if (sessionId == null) { +// handshake(); +// return; +// } +// String expireTime = (String) sessionTickets.get("expireTime"); +// if (expireTime != null) { +// long exp = Long.parseLong(expireTime); +// if (exp < System.currentTimeMillis()) { +// handshake(); +// return; +// } +// } +// FastConnectMessage message = new FastConnectMessage(connection); +// message.deviceId = deviceId; +// message.sessionId = sessionId; +// message.send(new ChannelFutureListener() { +// @Override +// public void operationComplete(ChannelFuture channelFuture) throws Exception { +// if (!channelFuture.isSuccess()) { +// handshake(); +// } +// } +// }); + } + + private void bindUser(SecurityNettyClient client) { + BindUserMessage message = new BindUserMessage(client.getConnection()); + message.userId = client.getUserId(); + message.send(); + } + +// private void saveToken(HandshakeOkMessage message, SessionContext context) { +// try { +// Map map = new HashMap<>(); +// map.put("sessionId", message.sessionId); +// map.put("serverHost", message.serverHost); +// map.put("expireTime", Long.toString(message.expireTime)); +// map.put("cipher", context.cipher.toString()); +// map.put("deviceId", deviceId); +// map.put("userId", userId); +// String path = this.getClass().getResource("/").getFile(); +// FileOutputStream out = new FileOutputStream(new File(path, "token.dat")); +// out.write(Jsons.toJson(map).getBytes(Constants.UTF_8)); +// out.close(); +// } catch (Exception e) { +// } +// } + + private void handshake(SecurityNettyClient client) { + HandshakeMessage message = new HandshakeMessage(client.getConnection()); + message.clientKey = client.getClientKey(); + message.iv = client.getIv(); + message.clientVersion = client.getClientVersion(); + message.deviceId = client.getDeviceId(); + message.osName = client.getOsName(); + message.osVersion = client.getOsVersion(); + message.timestamp = System.currentTimeMillis(); + message.send(); + } + +} \ No newline at end of file diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java deleted file mode 100644 index 99ce4ad9..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ClientChannelHandler.java +++ /dev/null @@ -1,224 +0,0 @@ -package com.shinemo.mpush.cs.client; - -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.common.message.*; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.security.AesCipher; -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.netty.connection.NettyConnection; -import com.shinemo.mpush.netty.util.NettySharedHolder; -import com.shinemo.mpush.tools.Jsons; - -import io.netty.channel.*; - -import io.netty.util.Timeout; -import io.netty.util.TimerTask; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Random; -import java.util.concurrent.TimeUnit; - -/** - * Created by ohun on 2015/12/24. - */ -public class ClientChannelHandler extends ChannelHandlerAdapter { - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - private byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - private byte[] iv = CipherBox.INSTANCE.randomAESIV(); - private Connection connection = new NettyConnection(); - private String deviceId; - private String userId; - private Map sessionTickets; - - public ClientChannelHandler() { - Map map = getToken(); - if (map != null && map.size() > 0) { - userId = (String) map.get("userId"); - deviceId = (String) map.get("deviceId"); - sessionTickets = map; - } - if (deviceId == null) { - deviceId = "test-device-id-100" + new Random().nextInt(5); - } - if (userId == null) { - userId = "user-" + new Random().nextInt(5); - } - } - - private void handshake() { - HandshakeMessage message = new HandshakeMessage(connection); - message.clientKey = clientKey; - message.iv = iv; - message.clientVersion = "1.0.1"; - message.deviceId = deviceId; - message.osName = "android"; - message.osVersion = "5.0"; - message.timestamp = System.currentTimeMillis(); - message.send(); - } - - private void tryFastConnect() { - if (sessionTickets == null) { - handshake(); - return; - } - String sessionId = (String) sessionTickets.get("sessionId"); - if (sessionId == null) { - handshake(); - return; - } - String expireTime = (String) sessionTickets.get("expireTime"); - if (expireTime != null) { - long exp = Long.parseLong(expireTime); - if (exp < System.currentTimeMillis()) { - handshake(); - return; - } - } - FastConnectMessage message = new FastConnectMessage(connection); - message.deviceId = deviceId; - message.sessionId = sessionId; - message.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) throws Exception { - if (!channelFuture.isSuccess()) { - handshake(); - } - } - }); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client channel Active"); - connection.init(ctx.channel(), true); - tryFastConnect(); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client channel Inactive"); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - //LOGGER.info("client read new packet=" + msg); - if (msg instanceof Packet) { - Packet packet = (Packet) msg; - Command command = Command.toCMD(packet.cmd); - if (command == Command.HANDSHAKE) { - connection.getSessionContext().changeCipher(new AesCipher(clientKey, iv)); - HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, message.serverKey); - connection.getSessionContext().changeCipher(new AesCipher(sessionKey, iv)); - startHeartBeat(message.heartbeat, ctx.channel()); - LOGGER.info("会话密钥:{},message={}", sessionKey, message); - saveToken(message, connection.getSessionContext()); - bindUser(); - } else if (command == Command.FAST_CONNECT) { - String cipherStr = (String) sessionTickets.get("cipher"); - String[] cs = cipherStr.split(","); - byte[] key = AesCipher.toArray(cs[0]); - byte[] iv = AesCipher.toArray(cs[1]); - connection.getSessionContext().changeCipher(new AesCipher(key, iv)); - - FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); - startHeartBeat(message.heartbeat, ctx.channel()); - bindUser(); - LOGGER.info("fast connect success, message=" + message); - } else if (command == Command.KICK) { - KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},", userId, deviceId, message); - ctx.close(); - } else if (command == Command.ERROR) { - ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error packet=" + errorMessage); - } else if (command == Command.BIND) { - OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.info("receive an success packet=" + okMessage); - } else if (command == Command.PUSH) { - PushMessage message = new PushMessage(packet, connection); - LOGGER.info("receive an push message, content=" + message.content); - } - } - } - - private void bindUser() { - BindUserMessage message = new BindUserMessage(connection); - message.userId = userId; - message.send(); - } - - public void startHeartBeat(final int heartbeat, final Channel channel) throws Exception { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - LOGGER.warn("client send hb failed:" + channel + ",channel is not active"); - } else { - LOGGER.warn("client send hb failed:" + channel); - } - } else { - //LOGGER.debug("client send hb success:" + channel); - } - } - }); - if (channel.isActive()) { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); - } - } - }, heartbeat, TimeUnit.MILLISECONDS); - } - - - private void saveToken(HandshakeOkMessage message, SessionContext context) { - try { - Map map = new HashMap<>(); - map.put("sessionId", message.sessionId); - map.put("serverHost", message.serverHost); - map.put("expireTime", Long.toString(message.expireTime)); - map.put("cipher", context.cipher.toString()); - map.put("deviceId", deviceId); - map.put("userId", userId); - String path = this.getClass().getResource("/").getFile(); - FileOutputStream out = new FileOutputStream(new File(path, "token.dat")); - out.write(Jsons.toJson(map).getBytes(Constants.UTF_8)); - out.close(); - } catch (Exception e) { - } - } - - private Map getToken() { - if (true) return Collections.EMPTY_MAP; - try { - InputStream in = this.getClass().getResourceAsStream("/token.dat"); - byte[] bytes = new byte[in.available()]; - if (bytes.length > 0) { - in.read(bytes); - Map map = Jsons.fromJson(bytes, Map.class); - return map; - } - in.close(); - } catch (Exception e) { - } - return Collections.EMPTY_MAP; - } - - public String getLastServerHost() { - return sessionTickets == null ? null : (String) sessionTickets.get("serverHost"); - } -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index abba9f30..4e28b5d8 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -3,7 +3,9 @@ import java.util.List; import java.util.concurrent.locks.LockSupport; +import com.google.common.collect.Lists; import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.core.client.ClientChannelHandler; import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; @@ -18,17 +20,15 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - ClientChannelHandler handler = new ClientChannelHandler(); - final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler); - client.init(); - Thread t = new Thread(new Runnable() { - @Override - public void run() { - client.start(); - } - }); - t.setDaemon(false); - t.start(); + + List clientList = Lists.newArrayList(); + + for(int i = 0;i<100;i++){ + ClientChannelHandler handler = new ClientChannelHandler(); + + final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler, true); + clientList.add(client); + } LockSupport.park(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java index a9ca28a3..0afa7768 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java @@ -4,6 +4,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import org.slf4j.Logger; @@ -37,6 +38,23 @@ public void onRemoval(RemovalNotification notification) { } }) .build(); + + /** + * host:port + */ + private final Cache channelClients = CacheBuilder.newBuilder()// + .maximumSize(2 << 17)// 最大是65535*2 + .expireAfterAccess(2 * 60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力 + .removalListener(new RemovalListener() { + @Override + public void onRemoval(RemovalNotification notification) { + if (notification.getValue().isConnected()) { + notification.getValue().stop(); + LOGGER.warn("[Remoting] removed from cache"); + } + } + }) + .build(); /** * 不存在,则创建 @@ -51,15 +69,16 @@ public Client get(final String remoteHost, final int port) { return cachedClients.getIfPresent(key); } - public Client get(final String remoteHost,final int port,final ChannelHandler handler) throws Exception{ + public Client get(final Channel channel){ + return channelClients.getIfPresent(channel); + } + + public Client get(final String remoteHost,final int port,final ChannelHandler handler,final boolean security) throws Exception{ String key = String.format(format, remoteHost, port); Client client = cachedClients.get(key, new Callable() { @Override public Client call() throws Exception { - Client client = createClient(remoteHost, port, handler); - if(client!=null){ - client.startHeartBeat(); - } + Client client = createClient(remoteHost, port, handler,security); return client; } }); @@ -71,11 +90,12 @@ public Client call() throws Exception { } - abstract Client createClient(String remoteHost, int port, ChannelHandler handler); + abstract Client createClient(String remoteHost, int port, ChannelHandler handler,boolean security); public void remove(Client client) { if (client != null) { cachedClients.invalidate(client.getUrl()); + channelClients.invalidate(client.getChannel()); LOGGER.warn(MessageFormat.format("[Remoting] {0} is removed", client)); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index dc56237a..cc7784b0 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.netty.util.NettySharedHolder; import com.shinemo.mpush.tools.config.ConfigCenter; @@ -25,13 +26,28 @@ public class NettyClient implements Client { private final int port; private Channel channel; private int hbTimes; - + private Connection connection; + public NettyClient(final String host, final int port, Channel channel) { this.host = host; this.port = port; this.channel = channel; } + + @Override + public Channel getChannel() { + return channel; + } + public void setChannel(Channel channel) { + this.channel = channel; + } + + @Override + public void initConnection(Connection connection){ + this.connection = connection; + } + @Override public void close(String cause) { if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { @@ -105,5 +121,15 @@ public String getHost() { public int getPort() { return port; } + + @Override + public void stop(){ + + } + + @Override + public Connection getConnection() { + return connection; + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 19857f0b..9c593c14 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -22,7 +22,7 @@ public class NettyClientFactory extends AbstractNettyClientFactory { public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - public Client createClient(String host, int port, final ChannelHandler handler) { + public Client createClient(String host, int port, final ChannelHandler handler,boolean security) { EventLoopGroup workerGroup = new NioEventLoopGroup(); final Bootstrap bootstrap = new Bootstrap(); @@ -47,8 +47,14 @@ public void initChannel(SocketChannel ch) throws Exception { ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { Channel channel = future.channel(); - Client client = new NettyClient(host,port, channel); - return client; + if(security){ + Client client = new SecurityNettyClient(host,port, channel); + return client; + }else{ + Client client = new NettyClient(host,port, channel); + return client; + } + } else { future.cancel(true); future.channel().close(); @@ -60,6 +66,10 @@ public void initChannel(SocketChannel ch) throws Exception { public Client getClient(final Client client) throws Exception { return get(client.getHost(),client.getPort()); } + + public Client getClient(final Channel channel){ + return getClient(channel); + } public void remove(final Client client) { super.remove(client); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java new file mode 100644 index 00000000..04fce49f --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java @@ -0,0 +1,88 @@ +package com.shinemo.mpush.netty.client; + + +import io.netty.channel.*; + + +public class SecurityNettyClient extends NettyClient { + + private byte[] clientKey; + private byte[] iv; + private String clientVersion; + private String deviceId; + private String osName; + private String osVersion; + + private String userId; + + private String cipher; //快速重连的时候使用 + + public SecurityNettyClient(String host, int port, Channel channel) { + super(host, port, channel); + } + + public byte[] getClientKey() { + return clientKey; + } + + public void setClientKey(byte[] clientKey) { + this.clientKey = clientKey; + } + + public byte[] getIv() { + return iv; + } + + public void setIv(byte[] iv) { + this.iv = iv; + } + + public String getClientVersion() { + return clientVersion; + } + + public void setClientVersion(String clientVersion) { + this.clientVersion = clientVersion; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getOsName() { + return osName; + } + + public void setOsName(String osName) { + this.osName = osName; + } + + public String getOsVersion() { + return osVersion; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public String getCipher() { + return cipher; + } + + public void setCipher(String cipher) { + this.cipher = cipher; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index b24bf6fc..11cb745e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -18,15 +18,8 @@ public final class NettyConnectionManager implements ConnectionManager { //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); -// private Timer wheelTimer; - @Override public void init() { -// //每秒钟走一步,一个心跳周期内走一圈 -// long tickDuration = 1000;//1s -// int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); -// this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); -// EventBus.INSTANCE.register(this); } @Override @@ -52,43 +45,4 @@ public List getConnections() { return Lists.newArrayList(connections.values()); } -// @Subscribe -// void onHandshakeOk(HandshakeEvent event) { -// HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); -// task.startTimeout(); -// } -// -// private class HeartbeatCheckTask implements TimerTask { -// private int expiredTimes = 0; -// private final int heartbeat; -// private final Connection connection; -// -// public HeartbeatCheckTask(int heartbeat, Connection connection) { -// this.heartbeat = heartbeat; -// this.connection = connection; -// LOGGER.warn("heart-beat-check-task:"+Thread.currentThread().getName()+",start"); -// } -// -// public void startTimeout() { -// wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); -// } -// -// @Override -// public void run(Timeout timeout) throws Exception { -// if (!connection.isConnected()) return; -// if (connection.heartbeatTimeout()) { -// if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { -// connection.close(); -// LOGGER.error("connection heartbeat timeout, connection has bean closed"); -// return; -// } else { -// LOGGER.error("connection heartbeat timeout, expiredTimes=" + expiredTimes); -// } -// } else { -// expiredTimes = 0; -// //LOGGER.info("check heartbeat timeout"); -// } -// startTimeout(); -// } -// } } diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java index 54b37e49..26a97574 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java @@ -18,9 +18,14 @@ public class GatewayClientManage extends GatewayServerManage{ @Override public void addOrUpdate(String fullPath, GatewayServerApplication application) { super.addOrUpdate(fullPath, application); - Client client = NettyClientFactory.INSTANCE.createClient(application.getIp(), application.getPort(),new GatewayClientChannelHandler()); - application2Client.put(application, client); - ip2Client.put(application.getIp()+":"+application.getPort(), client); + try{ + Client client = NettyClientFactory.INSTANCE.get(application.getIp(), application.getPort(), new GatewayClientChannelHandler(), false); + application2Client.put(application, client); + ip2Client.put(application.getIp()+":"+application.getPort(), client); + }catch(Exception e){ + + } + } @@ -45,7 +50,7 @@ public Client getClient(GatewayServerApplication application){ public Connection getConnection(String ipAndPort) { Client client = ip2Client.get(ipAndPort); if (client == null) return null; - return ((GatewayClientChannelHandler) client.getHandler()).getConnection(); + return client.getConnection(); } From 38a8b801e9c6260c931cf3e5363b041a7c9fc8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 11:12:07 +0800 Subject: [PATCH 193/890] use new netty client factory --- .../core/client/ClientChannelHandler.java | 12 +-- .../client/AbstractNettyClientFactory.java | 102 ------------------ .../netty/client/NettyClientFactory.java | 63 ++++++----- 3 files changed, 36 insertions(+), 141 deletions(-) delete mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index ce7b2dc3..0cbfd85d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -37,7 +37,7 @@ public final class ClientChannelHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); + Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); client.getConnection().updateLastReadTime(); if(client instanceof SecurityNettyClient){ @@ -91,8 +91,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); - NettyClientFactory.INSTANCE.remove(client); + NettyClientFactory.INSTANCE.remove(ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @@ -100,9 +99,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); - Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); + + Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); if(client instanceof SecurityNettyClient){ - tryFastConnect((SecurityNettyClient)client); }else{ connection.init(ctx.channel(), false); @@ -118,8 +117,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client disconnect channel={}", ctx.channel()); - Client client = NettyClientFactory.INSTANCE.get(ctx.channel()); - NettyClientFactory.INSTANCE.remove(client); + NettyClientFactory.INSTANCE.remove(ctx.channel());; } private void tryFastConnect(SecurityNettyClient securityNettyClient) { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java deleted file mode 100644 index 0afa7768..00000000 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/AbstractNettyClientFactory.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.shinemo.mpush.netty.client; - -import java.text.MessageFormat; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; - -import io.netty.channel.Channel; -import io.netty.channel.ChannelHandler; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; -import com.shinemo.mpush.api.Client; - -public abstract class AbstractNettyClientFactory { - - private static final String format = "%s:%s"; - - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNettyClientFactory.class); - - /** - * host:port - */ - private final Cache cachedClients = CacheBuilder.newBuilder()// - .maximumSize(2 << 17)// 最大是65535*2 - .expireAfterAccess(2 * 60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力 - .removalListener(new RemovalListener() { - @Override - public void onRemoval(RemovalNotification notification) { - if (notification.getValue().isConnected()) { - notification.getValue().stop(); - LOGGER.warn("[Remoting] removed from cache"); - } - } - }) - .build(); - - /** - * host:port - */ - private final Cache channelClients = CacheBuilder.newBuilder()// - .maximumSize(2 << 17)// 最大是65535*2 - .expireAfterAccess(2 * 60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力 - .removalListener(new RemovalListener() { - @Override - public void onRemoval(RemovalNotification notification) { - if (notification.getValue().isConnected()) { - notification.getValue().stop(); - LOGGER.warn("[Remoting] removed from cache"); - } - } - }) - .build(); - - /** - * 不存在,则创建 - * - * @param remoteHost - * @param port - * @return - * @throws Exception - */ - public Client get(final String remoteHost, final int port) { - String key = String.format(format, remoteHost, port); - return cachedClients.getIfPresent(key); - } - - public Client get(final Channel channel){ - return channelClients.getIfPresent(channel); - } - - public Client get(final String remoteHost,final int port,final ChannelHandler handler,final boolean security) throws Exception{ - String key = String.format(format, remoteHost, port); - Client client = cachedClients.get(key, new Callable() { - @Override - public Client call() throws Exception { - Client client = createClient(remoteHost, port, handler,security); - return client; - } - }); - if(client == null || !client.isConnected()){ - cachedClients.invalidate(key); - return null; - } - return client; - } - - - abstract Client createClient(String remoteHost, int port, ChannelHandler handler,boolean security); - - public void remove(Client client) { - if (client != null) { - cachedClients.invalidate(client.getUrl()); - channelClients.invalidate(client.getChannel()); - LOGGER.warn(MessageFormat.format("[Remoting] {0} is removed", client)); - } - } -} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 9c593c14..ad8a9e68 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.netty.client; import java.net.InetSocketAddress; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,29 +13,31 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import com.google.common.collect.Maps; import com.shinemo.mpush.api.Client; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; -public class NettyClientFactory extends AbstractNettyClientFactory { +public class NettyClientFactory { private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); public static final NettyClientFactory INSTANCE = new NettyClientFactory(); + + private final Map channel2Client = Maps.newConcurrentMap(); - public Client createClient(String host, int port, final ChannelHandler handler,boolean security) { - - EventLoopGroup workerGroup = new NioEventLoopGroup(); + public void createClient(String host, int port, final ChannelHandler handler,boolean security) { final Bootstrap bootstrap = new Bootstrap(); + EventLoopGroup workerGroup = new NioEventLoopGroup(); bootstrap.group(workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.TCP_NODELAY, true)// .option(ChannelOption.SO_REUSEADDR, true)// .option(ChannelOption.SO_KEEPALIVE, true)// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// .channel(NioSocketChannel.class) .handler(handler) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - + bootstrap.handler(new ChannelInitializer() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { @@ -44,35 +47,31 @@ public void initChannel(SocketChannel ch) throws Exception { } }); - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - if(security){ - Client client = new SecurityNettyClient(host,port, channel); - return client; + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { + Channel channel = future.channel(); + + Client client = null; + if(security){ + client = new SecurityNettyClient(host,port, channel); }else{ - Client client = new NettyClient(host,port, channel); - return client; + client = new NettyClient(host,port, channel); } - - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + host+","+port); - } - return null; - } - - public Client getClient(final Client client) throws Exception { - return get(client.getHost(),client.getPort()); - } - - public Client getClient(final Channel channel){ - return getClient(channel); + channel2Client.put(channel, client); + } else { + future.cancel(true); + future.channel().close(); + log.warn("[remoting] failure to connect:" + host+","+port); + } } - public void remove(final Client client) { - super.remove(client); - } + public Client getCientByChannel(final Channel channel) { + return channel2Client.get(channel); + } + public void remove(final Channel channel) { + channel2Client.remove(channel); + } + } From 8a849bbca843a5272fee5c121717623de5ef7f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 11:15:09 +0800 Subject: [PATCH 194/890] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84net?= =?UTF-8?q?ty=20client=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/cs/client/Main.java | 6 +----- .../com/shinemo/mpush/netty/client/NettyClientFactory.java | 4 +++- .../main/java/com/shinemo/mpush/ps/GatewayClientManage.java | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 4e28b5d8..4ba91304 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -21,13 +21,9 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - List clientList = Lists.newArrayList(); - for(int i = 0;i<100;i++){ ClientChannelHandler handler = new ClientChannelHandler(); - - final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler, true); - clientList.add(client); + NettyClientFactory.INSTANCE.createClient(server.getIp(), server.getPort(), handler, true); } LockSupport.park(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index ad8a9e68..56efa29f 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -26,7 +26,7 @@ public class NettyClientFactory { private final Map channel2Client = Maps.newConcurrentMap(); - public void createClient(String host, int port, final ChannelHandler handler,boolean security) { + public Client createClient(String host, int port, final ChannelHandler handler,boolean security) { final Bootstrap bootstrap = new Bootstrap(); EventLoopGroup workerGroup = new NioEventLoopGroup(); bootstrap.group(workerGroup)// @@ -59,10 +59,12 @@ public void initChannel(SocketChannel ch) throws Exception { client = new NettyClient(host,port, channel); } channel2Client.put(channel, client); + return client; } else { future.cancel(true); future.channel().close(); log.warn("[remoting] failure to connect:" + host+","+port); + return null; } } diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java index 26a97574..ca97c8e7 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java @@ -19,7 +19,7 @@ public class GatewayClientManage extends GatewayServerManage{ public void addOrUpdate(String fullPath, GatewayServerApplication application) { super.addOrUpdate(fullPath, application); try{ - Client client = NettyClientFactory.INSTANCE.get(application.getIp(), application.getPort(), new GatewayClientChannelHandler(), false); + Client client = NettyClientFactory.INSTANCE.createClient(application.getIp(), application.getPort(), new GatewayClientChannelHandler(), false); application2Client.put(application, client); ip2Client.put(application.getIp()+":"+application.getPort(), client); }catch(Exception e){ From fd303b6d12dea2f7deae672ed801d2407489c9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 14:05:40 +0800 Subject: [PATCH 195/890] =?UTF-8?q?client=20=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Client.java | 1 + .../com/shinemo/mpush/cs/client/Main.java | 13 ++++++-- .../mpush/netty/client/NettyClient.java | 14 ++++++-- .../netty/client/NettyClientFactory.java | 33 +++++++++++++------ .../netty/client/SecurityNettyClient.java | 6 ++-- .../shinemo/mpush/ps/GatewayClientManage.java | 2 +- 6 files changed, 49 insertions(+), 20 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index 24f6915b..c5e25277 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -7,6 +7,7 @@ public interface Client { + void init(Channel channel); boolean isConnected(); diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 4ba91304..43b1b945 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -3,8 +3,6 @@ import java.util.List; import java.util.concurrent.locks.LockSupport; -import com.google.common.collect.Lists; -import com.shinemo.mpush.api.Client; import com.shinemo.mpush.core.client.ClientChannelHandler; import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; @@ -21,9 +19,18 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); + byte[] clientKey = null; + byte[] iv = null; + String clientVersion = null; + String deviceId = null; + String osName = null; + String osVersion = null; + String userId = null; + String cipher = null; + for(int i = 0;i<100;i++){ ClientChannelHandler handler = new ClientChannelHandler(); - NettyClientFactory.INSTANCE.createClient(server.getIp(), server.getPort(), handler, true); + NettyClientFactory.INSTANCE.createSecurityClient(server.getIp(), server.getPort(), handler, clientKey, iv, clientVersion, deviceId, osName, osVersion, userId, cipher); } LockSupport.park(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index cc7784b0..bdc85e96 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -28,10 +28,9 @@ public class NettyClient implements Client { private int hbTimes; private Connection connection; - public NettyClient(final String host, final int port, Channel channel) { + public NettyClient(final String host, final int port) { this.host = host; this.port = port; - this.channel = channel; } @Override @@ -132,4 +131,15 @@ public Connection getConnection() { return connection; } + @Override + public void init(Channel channel) { + this.channel = channel; + } + + @Override + public String toString() { + return "NettyClient [host=" + host + ", port=" + port + ", channel=" + channel + ", hbTimes=" + hbTimes + ", connection=" + connection + "]"; + } + + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 56efa29f..2b35cd81 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -25,8 +25,27 @@ public class NettyClientFactory { public static final NettyClientFactory INSTANCE = new NettyClientFactory(); private final Map channel2Client = Maps.newConcurrentMap(); + + public Client create(String host,int port,final ChannelHandler handler){ + Client client = new NettyClient(host, port); + return init(client, handler); + } + + public Client createSecurityClient(String host,int port,final ChannelHandler handler,byte[] clientKey,byte[] iv,String clientVersion, + String deviceId,String osName,String osVersion,String userId,String cipher){ + SecurityNettyClient client = new SecurityNettyClient(host, port); + client.setClientKey(clientKey); + client.setIv(iv); + client.setClientVersion(clientVersion); + client.setDeviceId(deviceId); + client.setOsName(osName); + client.setOsVersion(osVersion); + client.setUserId(userId); + client.setCipher(cipher); + return init(client, handler); + } - public Client createClient(String host, int port, final ChannelHandler handler,boolean security) { + public Client init(Client client, final ChannelHandler handler) { final Bootstrap bootstrap = new Bootstrap(); EventLoopGroup workerGroup = new NioEventLoopGroup(); bootstrap.group(workerGroup)// @@ -48,22 +67,16 @@ public void initChannel(SocketChannel ch) throws Exception { }); - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { Channel channel = future.channel(); - - Client client = null; - if(security){ - client = new SecurityNettyClient(host,port, channel); - }else{ - client = new NettyClient(host,port, channel); - } + client.init(channel); channel2Client.put(channel, client); return client; } else { future.cancel(true); future.channel().close(); - log.warn("[remoting] failure to connect:" + host+","+port); + log.warn("[remoting] failure to connect:" + client); return null; } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java index 04fce49f..c5b7f2dc 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java @@ -1,8 +1,6 @@ package com.shinemo.mpush.netty.client; -import io.netty.channel.*; - public class SecurityNettyClient extends NettyClient { @@ -17,8 +15,8 @@ public class SecurityNettyClient extends NettyClient { private String cipher; //快速重连的时候使用 - public SecurityNettyClient(String host, int port, Channel channel) { - super(host, port, channel); + public SecurityNettyClient(String host, int port) { + super(host, port); } public byte[] getClientKey() { diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java index ca97c8e7..9d50c4e6 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java @@ -19,7 +19,7 @@ public class GatewayClientManage extends GatewayServerManage{ public void addOrUpdate(String fullPath, GatewayServerApplication application) { super.addOrUpdate(fullPath, application); try{ - Client client = NettyClientFactory.INSTANCE.createClient(application.getIp(), application.getPort(), new GatewayClientChannelHandler(), false); + Client client = NettyClientFactory.INSTANCE.create(application.getIp(), application.getPort(), new GatewayClientChannelHandler()); application2Client.put(application, client); ip2Client.put(application.getIp()+":"+application.getPort(), client); }catch(Exception e){ From 56e5907e20cbc888becab86a5d7a24a294520ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 14:45:13 +0800 Subject: [PATCH 196/890] add --- .../com/shinemo/mpush/cs/client/Main.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 43b1b945..6ecd56fc 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.concurrent.locks.LockSupport; +import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.core.client.ClientChannelHandler; import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; @@ -18,17 +19,16 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - - byte[] clientKey = null; - byte[] iv = null; - String clientVersion = null; - String deviceId = null; - String osName = null; - String osVersion = null; - String userId = null; - String cipher = null; - + for(int i = 0;i<100;i++){ + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "user-"+i; + String deviceId = "test-device-id-"+i; + String cipher = ""; + byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); + byte[] iv = CipherBox.INSTANCE.randomAESIV(); ClientChannelHandler handler = new ClientChannelHandler(); NettyClientFactory.INSTANCE.createSecurityClient(server.getIp(), server.getPort(), handler, clientKey, iv, clientVersion, deviceId, osName, osVersion, userId, cipher); } From 7ce5884910333aae72cdc4ea1443d1eb667297b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 16:09:27 +0800 Subject: [PATCH 197/890] bug fix --- .../shinemo/mpush/core/client/ClientChannelHandler.java | 9 +++------ .../src/main/java/com/shinemo/mpush/cs/client/Main.java | 2 +- .../shinemo/mpush/netty/client/NettyClientFactory.java | 1 - 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 0cbfd85d..5f398bce 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -102,16 +102,13 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); if(client instanceof SecurityNettyClient){ + connection.init(ctx.channel(), true); + client.initConnection(connection); tryFastConnect((SecurityNettyClient)client); }else{ connection.init(ctx.channel(), false); + client.initConnection(connection); } - client.initConnection(connection); - - if(client instanceof SecurityNettyClient){ - connection.init(ctx.channel(), true); - } - } @Override diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 6ecd56fc..336689f6 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<100;i++){ + for(int i = 0;i<1;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 2b35cd81..aab8020b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -54,7 +54,6 @@ public Client init(Client client, final ChannelHandler handler) { .option(ChannelOption.SO_KEEPALIVE, true)// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// .channel(NioSocketChannel.class) - .handler(handler) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); bootstrap.handler(new ChannelInitializer() { // (4) From 7dd3dfaae8ae476c90ca340746e42c921735339e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 17:29:11 +0800 Subject: [PATCH 198/890] =?UTF-8?q?ping=20-=E3=80=8B=20pong?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/connection/SessionContext.java | 3 +- .../common/message/HeartbeatMessage.java | 37 ------------------- .../core/client/ClientChannelHandler.java | 6 +++ 3 files changed, 7 insertions(+), 39 deletions(-) delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java index 211a0ba6..91e76f13 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java @@ -45,8 +45,7 @@ public boolean handshakeOk() { @Override public String toString() { - return "SessionContext [osName=" + osName + ", osVersion=" + osVersion + ", clientVersion=" + clientVersion + ", deviceId=" + deviceId + ", heartbeat=" + heartbeat + ", cipher=" + cipher - + "]"; + return "SessionContext [osName=" + osName + ", osVersion=" + osVersion + ", clientVersion=" + clientVersion + ", deviceId=" + deviceId + ", heartbeat=" + heartbeat + "]"; } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java deleted file mode 100644 index dc1cfe9f..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HeartbeatMessage.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.shinemo.mpush.common.message; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Message; -import com.shinemo.mpush.api.protocol.Packet; -import io.netty.channel.ChannelFutureListener; - -/** - * Created by ohun on 2015/12/28. - */ -public final class HeartbeatMessage implements Message { - private final Connection connection; - - public HeartbeatMessage(Connection connection) { - this.connection = connection; - } - - @Override - public Connection getConnection() { - return connection; - } - - @Override - public void send(ChannelFutureListener listener) { - - } - - @Override - public void sendRaw(ChannelFutureListener listener) { - - } - - @Override - public Packet getPacket() { - return null; - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 5f398bce..847d7abf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -19,6 +19,7 @@ import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; import com.shinemo.mpush.netty.connection.NettyConnection; + import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -80,6 +81,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); LOGGER.info("receive an push message, content=" + message.content); + }else if(command == Command.HEARTBEAT){ + connection.send(packet); // ping -> pong + LOGGER.info("receive an heart beat message"); + }else{ + LOGGER.info("receive an message, type=" + command.cmd+","+packet); } } From 14b28780f03786ed4208454625b39ca9599274f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 17:31:51 +0800 Subject: [PATCH 199/890] bug fix --- .../shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java index 3a6bbccd..cfea6967 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java @@ -31,10 +31,11 @@ public ScanAllConnectionTimerTask(ConnectionManager connectionManager) { @Override public void run(Timeout timeout) throws Exception { - log.warn("start deal ScanAllConnectionTimerTask "); + try { long now = System.currentTimeMillis(); List connections = connectionManager.getConnections(); + log.warn("start deal ScanAllConnectionTimerTask:size,"+connections.size()); if (connections != null) { for (final Connection conn : connections) { if (!conn.isConnected()) { From 2091ecef19944f09d654012979ad25b2dc7b7669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 18:08:32 +0800 Subject: [PATCH 200/890] bugfix --- .../mpush/core/client/ClientChannelHandler.java | 12 +++++++++++- .../main/java/com/shinemo/mpush/cs/client/Main.java | 3 ++- .../src/main/resources/logback.xml | 8 ++++---- .../mpush/netty/client/NettyClientFactory.java | 6 +++--- .../shinemo/mpush/netty/util/NettySharedHolder.java | 4 ++++ .../main/java/com/shinemo/mpush/tools/Constants.java | 2 +- 6 files changed, 25 insertions(+), 10 deletions(-) rename {mpush-core => mpush-cs}/src/main/resources/logback.xml (87%) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 847d7abf..dba73951 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -82,7 +82,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception PushMessage message = new PushMessage(packet, connection); LOGGER.info("receive an push message, content=" + message.content); }else if(command == Command.HEARTBEAT){ - connection.send(packet); // ping -> pong +// connection.send(packet); // ping -> pong LOGGER.info("receive an heart beat message"); }else{ LOGGER.info("receive an message, type=" + command.cmd+","+packet); @@ -107,11 +107,21 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { Connection connection = new NettyConnection(); Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); + + if(client == null){ //可能存在那边还没有插入,导致这边不存在,需要sleep 一下。 + + LOGGER.error("client is null:"+ctx.channel()); + + Thread.sleep(10); + client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); + } + if(client instanceof SecurityNettyClient){ connection.init(ctx.channel(), true); client.initConnection(connection); tryFastConnect((SecurityNettyClient)client); }else{ + LOGGER.error("connection is not support appear hear:"+ client); connection.init(ctx.channel(), false); client.initConnection(connection); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 336689f6..210b38b8 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<1;i++){ + for(int i = 0;i<100;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; @@ -31,6 +31,7 @@ public static void main(String[] args) throws Exception { byte[] iv = CipherBox.INSTANCE.randomAESIV(); ClientChannelHandler handler = new ClientChannelHandler(); NettyClientFactory.INSTANCE.createSecurityClient(server.getIp(), server.getPort(), handler, clientKey, iv, clientVersion, deviceId, osName, osVersion, userId, cipher); + Thread.sleep(10); } LockSupport.park(); diff --git a/mpush-core/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml similarity index 87% rename from mpush-core/src/main/resources/logback.xml rename to mpush-cs/src/main/resources/logback.xml index 20979b40..76594e12 100644 --- a/mpush-core/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -4,7 +4,7 @@ System.out UTF-8 - DEBUG + error %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -15,7 +15,7 @@ System.err UTF-8 - WARN + error %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -24,7 +24,7 @@ - - + + diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index aab8020b..a52b1523 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -9,7 +9,6 @@ import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; @@ -17,6 +16,7 @@ import com.shinemo.mpush.api.Client; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; +import com.shinemo.mpush.netty.util.NettySharedHolder; public class NettyClientFactory { @@ -47,8 +47,7 @@ public Client createSecurityClient(String host,int port,final ChannelHandler han public Client init(Client client, final ChannelHandler handler) { final Bootstrap bootstrap = new Bootstrap(); - EventLoopGroup workerGroup = new NioEventLoopGroup(); - bootstrap.group(workerGroup)// + bootstrap.group(NettySharedHolder.workerGroup)// .option(ChannelOption.TCP_NODELAY, true)// .option(ChannelOption.SO_REUSEADDR, true)// .option(ChannelOption.SO_KEEPALIVE, true)// @@ -71,6 +70,7 @@ public void initChannel(SocketChannel ch) throws Exception { Channel channel = future.channel(); client.init(channel); channel2Client.put(channel, client); + log.error("init channel:"+channel); return client; } else { future.cancel(true); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java index 1d1ebe65..489b2970 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java @@ -3,6 +3,9 @@ import com.shinemo.mpush.tools.thread.NamedThreadFactory; import com.shinemo.mpush.tools.thread.ThreadNameSpace; + +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.HashedWheelTimer; import io.netty.util.Timer; @@ -10,5 +13,6 @@ public class NettySharedHolder { public static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); + public static EventLoopGroup workerGroup = new NioEventLoopGroup(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 5f538792..5d934a13 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -12,7 +12,7 @@ public interface Constants { String JVM_LOG_PATH = "/opt/"; int THREAD_QUEUE_SIZE = 10000; - int MIN_POOL_SIZE = 50; + int MIN_POOL_SIZE = 200; int MAX_POOL_SIZE = 500; int MIN_BOSS_POOL_SIZE = 10; From 02367739b2a09a972d8bfbabb0f2a581a0aabca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 24 Jan 2016 09:52:11 +0800 Subject: [PATCH 201/890] init client --- .../com/shinemo/mpush/common/EventBus.java | 2 +- .../core/client/ClientChannelHandler.java | 27 ++++++------ .../src/main/resources/config.properties | 2 +- .../com/shinemo/mpush/cs/client/Main.java | 18 ++++++-- .../netty/client/ChannelClientHandler.java | 11 +++++ .../netty/client/NettyClientFactory.java | 41 ++++++++++++++++++- 6 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java b/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java index 35970894..b78a8acd 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java @@ -20,7 +20,7 @@ public class EventBus { private final com.google.common.eventbus.EventBus eventBus; public EventBus() { - Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor("event-bus-pool", 4, 4); + Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor("event-bus-pool", 10, 10); eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index dba73951..d346a6bf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -16,6 +16,7 @@ import com.shinemo.mpush.common.message.PushMessage; import com.shinemo.mpush.common.security.AesCipher; import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.netty.client.ChannelClientHandler; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; import com.shinemo.mpush.netty.connection.NettyConnection; @@ -32,10 +33,21 @@ * Created by ohun on 2015/12/19. */ @ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter { +public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - + + private Client client; + + public ClientChannelHandler(Client client) { + this.client = client; + } + + @Override + public Client getClient() { + return client; + } + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); @@ -105,16 +117,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); - - Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); - - if(client == null){ //可能存在那边还没有插入,导致这边不存在,需要sleep 一下。 - - LOGGER.error("client is null:"+ctx.channel()); - - Thread.sleep(10); - client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); - } + NettyClientFactory.INSTANCE.put(ctx.channel(), client); if(client instanceof SecurityNettyClient){ connection.init(ctx.channel(), true); diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties index 67e2ffaf..47481f19 100644 --- a/mpush-core/src/main/resources/config.properties +++ b/mpush-core/src/main/resources/config.properties @@ -34,7 +34,7 @@ compress_limit = 10240 max_packet_size = 10240 ## zk 配置项 -zk_ip = 10.1.10.41:2181 +zk_ip = 127.0.0.1:2181 zk_namespace = mpush zk_digest = shinemoIpo diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 210b38b8..2fbeee4a 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -7,6 +7,7 @@ import com.shinemo.mpush.core.client.ClientChannelHandler; import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.netty.client.SecurityNettyClient; public class Main { @@ -20,7 +21,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<100;i++){ + for(int i = 0;i<500;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; @@ -29,8 +30,19 @@ public static void main(String[] args) throws Exception { String cipher = ""; byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); byte[] iv = CipherBox.INSTANCE.randomAESIV(); - ClientChannelHandler handler = new ClientChannelHandler(); - NettyClientFactory.INSTANCE.createSecurityClient(server.getIp(), server.getPort(), handler, clientKey, iv, clientVersion, deviceId, osName, osVersion, userId, cipher); + + SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); + client.setClientKey(clientKey); + client.setIv(iv); + client.setClientVersion(clientVersion); + client.setDeviceId(deviceId); + client.setOsName(osName); + client.setOsVersion(osVersion); + client.setUserId(userId); + client.setCipher(cipher); + + ClientChannelHandler handler = new ClientChannelHandler(client); + NettyClientFactory.INSTANCE.create(handler); Thread.sleep(10); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java new file mode 100644 index 00000000..54ef263a --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java @@ -0,0 +1,11 @@ +package com.shinemo.mpush.netty.client; + +import com.shinemo.mpush.api.Client; + +import io.netty.channel.ChannelHandler; + +public interface ChannelClientHandler extends ChannelHandler{ + + public Client getClient(); + +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index a52b1523..b4e56f02 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -26,11 +26,47 @@ public class NettyClientFactory { private final Map channel2Client = Maps.newConcurrentMap(); + public Client create(final ChannelClientHandler handler){ + final Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(NettySharedHolder.workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(handler); + } + }); + + Client client = handler.getClient(); + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); + if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { + Channel channel = future.channel(); + log.error("init channel:"+channel); + return client; + } else { + future.cancel(true); + future.channel().close(); + log.warn("[remoting] failure to connect:" + client); + return null; + } + } + + @Deprecated public Client create(String host,int port,final ChannelHandler handler){ Client client = new NettyClient(host, port); return init(client, handler); } + @Deprecated public Client createSecurityClient(String host,int port,final ChannelHandler handler,byte[] clientKey,byte[] iv,String clientVersion, String deviceId,String osName,String osVersion,String userId,String cipher){ SecurityNettyClient client = new SecurityNettyClient(host, port); @@ -69,7 +105,6 @@ public void initChannel(SocketChannel ch) throws Exception { if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { Channel channel = future.channel(); client.init(channel); - channel2Client.put(channel, client); log.error("init channel:"+channel); return client; } else { @@ -88,4 +123,8 @@ public void remove(final Channel channel) { channel2Client.remove(channel); } + public void put(Channel channel,final Client client){ + channel2Client.put(channel, client); + } + } From 62f2f3c24051a8117c747d5d35e4a62cca88fe1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 14:27:53 +0800 Subject: [PATCH 202/890] update --- .../src/main/java/com/shinemo/mpush/tools/Constants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 5d934a13..0148c9c8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -9,7 +9,7 @@ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); byte[] EMPTY_BYTES = new byte[0]; - String JVM_LOG_PATH = "/opt/"; + String JVM_LOG_PATH = "/opt/logs/mpush/"; int THREAD_QUEUE_SIZE = 10000; int MIN_POOL_SIZE = 200; From 0b6fcfa58dddf3a75300d6fafe0e09ea1dc78b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 14:30:51 +0800 Subject: [PATCH 203/890] add mpush monitor and log module --- mpush-log/pom.xml | 15 +++++++++++++++ mpush-monitor/pom.xml | 15 +++++++++++++++ pom.xml | 2 ++ 3 files changed, 32 insertions(+) create mode 100644 mpush-log/pom.xml create mode 100644 mpush-monitor/pom.xml diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml new file mode 100644 index 00000000..11027f09 --- /dev/null +++ b/mpush-log/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-log + + + \ No newline at end of file diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml new file mode 100644 index 00000000..c7a19510 --- /dev/null +++ b/mpush-monitor/pom.xml @@ -0,0 +1,15 @@ + + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-monitor + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index fb64703b..4ebd0ba5 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,8 @@ mpush-client mpush-cs mpush-ps + mpush-monitor + mpush-log From 2e75272344d3bc43bd57dde3fad7ecd359c51af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 14:32:50 +0800 Subject: [PATCH 204/890] add --- mpush-log/pom.xml | 21 ++++++++-- .../src/main/java/com/shinemo/mpush/App.java | 13 +++++++ .../test/java/com/shinemo/mpush/AppTest.java | 38 +++++++++++++++++++ mpush-monitor/pom.xml | 21 ++++++++-- .../src/main/java/com/shinemo/mpush/App.java | 13 +++++++ .../test/java/com/shinemo/mpush/AppTest.java | 38 +++++++++++++++++++ 6 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 mpush-log/src/main/java/com/shinemo/mpush/App.java create mode 100644 mpush-log/src/test/java/com/shinemo/mpush/AppTest.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/App.java create mode 100644 mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml index 11027f09..ec280dd5 100644 --- a/mpush-log/pom.xml +++ b/mpush-log/pom.xml @@ -1,6 +1,4 @@ - - mpush @@ -10,6 +8,21 @@ 4.0.0 mpush-log + jar + mpush-log + http://maven.apache.org - \ No newline at end of file + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/mpush-log/src/main/java/com/shinemo/mpush/App.java b/mpush-log/src/main/java/com/shinemo/mpush/App.java new file mode 100644 index 00000000..9cc9cf7b --- /dev/null +++ b/mpush-log/src/main/java/com/shinemo/mpush/App.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/mpush-log/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-log/src/test/java/com/shinemo/mpush/AppTest.java new file mode 100644 index 00000000..50fff9a1 --- /dev/null +++ b/mpush-log/src/test/java/com/shinemo/mpush/AppTest.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index c7a19510..a2471f9c 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -1,6 +1,4 @@ - - mpush @@ -10,6 +8,21 @@ 4.0.0 mpush-monitor + jar + mpush-monitor + http://maven.apache.org - \ No newline at end of file + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/App.java b/mpush-monitor/src/main/java/com/shinemo/mpush/App.java new file mode 100644 index 00000000..9cc9cf7b --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/App.java @@ -0,0 +1,13 @@ +package com.shinemo.mpush; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java new file mode 100644 index 00000000..50fff9a1 --- /dev/null +++ b/mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} From 6fbe02f9c56b01b493038e73a0b512af42665263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 17:16:00 +0800 Subject: [PATCH 205/890] add monitor --- mpush-cs/pom.xml | 4 + .../main/java/com/shinemo/mpush/cs/Main.java | 5 + mpush-monitor/pom.xml | 24 +- .../mpush/monitor/domain/MonitorData.java | 47 ++++ .../mpush/monitor/mbean/BaseMBean.java | 35 +++ .../shinemo/mpush/monitor/mbean/GCMBean.java | 21 ++ .../mpush/monitor/mbean/MemoryMBean.java | 59 +++++ .../mpush/monitor/mbean/ThreadMBean.java | 14 + .../mpush/monitor/mbean/impl/JVMGC.java | 136 ++++++++++ .../mpush/monitor/mbean/impl/JVMMemory.java | 239 ++++++++++++++++++ .../mpush/monitor/mbean/impl/JVMThread.java | 60 +++++ .../monitor/service/MonitorDataCollector.java | 42 +++ .../monitor/service/MonitorDataService.java | 17 ++ pom.xml | 6 + 14 files changed, 702 insertions(+), 7 deletions(-) create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index bcaa90c7..126f21dc 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -21,6 +21,10 @@ com.shinemo.mpush mpush-core + + com.shinemo.mpush + mpush-monitor + diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java index 67dfeb0f..1ed3b342 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java @@ -3,6 +3,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.shinemo.mpush.monitor.service.MonitorDataCollector; + public class Main { private static final Logger log = LoggerFactory.getLogger(Main.class); @@ -10,6 +12,9 @@ public class Main { public static void main(String[] args) { final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); connectionServerMain.start(); + //开启监控 + MonitorDataCollector.start(); + Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { connectionServerMain.stop(); diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index a2471f9c..de48abb7 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -9,20 +9,30 @@ mpush-monitor jar + 1.0-SNAPSHOT mpush-monitor - http://maven.apache.org UTF-8 - - junit - junit - 3.8.1 - test - + + com.google.code.gson + gson + + + com.google.guava + guava + + + com.shinemo.mpush + mpush-tools + + + org.apache.commons + commons-lang3 + diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java new file mode 100644 index 00000000..77134372 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java @@ -0,0 +1,47 @@ +package com.shinemo.mpush.monitor.domain; + +import java.util.Map; + +public class MonitorData { + + private Long timestamp; + + private Map memoryMap; + + private Map gcMap; + + private Map threadMap; + + public MonitorData() { + this.timestamp = System.currentTimeMillis(); + } + + public Map getMemoryMap() { + return memoryMap; + } + + public void setMemoryMap(Map memoryMap) { + this.memoryMap = memoryMap; + } + + public Map getGcMap() { + return gcMap; + } + + public void setGcMap(Map gcMap) { + this.gcMap = gcMap; + } + + public Map getThreadMap() { + return threadMap; + } + + public void setThreadMap(Map threadMap) { + this.threadMap = threadMap; + } + + public Long getTimestamp() { + return timestamp; + } + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java new file mode 100644 index 00000000..168d8828 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java @@ -0,0 +1,35 @@ +package com.shinemo.mpush.monitor.mbean; + +import java.util.List; +import java.util.Map; + +import com.google.common.collect.Lists; + +public abstract class BaseMBean { + + protected final static List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", + "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", + "Garbage collection optimized for deterministic pausetimes Old Collector"); + + protected final static List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", + "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); + + protected final static List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); + + protected final static List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); + + protected final static List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); + + protected final static List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); + + public static boolean contain(String name, List list) { + for (String str : list) { + if (name.equals(str)) { + return true; + } + } + return false; + } + + public abstract Map toMap(); +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java new file mode 100644 index 00000000..e8284958 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java @@ -0,0 +1,21 @@ +package com.shinemo.mpush.monitor.mbean; + +public interface GCMBean { + + public long yongGcCollectionCount(); + + public long yongGcCollectionTime(); + + public long fullGcCollectionCount(); + + public long fullGcCollectionTime(); + + public long spanYongGcCollectionCount(); + + public long spanYongGcCollectionTime(); + + public long spanFullGcCollectionCount(); + + public long spanFullGcCollectionTime(); + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java new file mode 100644 index 00000000..b2c5c398 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java @@ -0,0 +1,59 @@ +package com.shinemo.mpush.monitor.mbean; + +public interface MemoryMBean { + + // Heap + long heapMemoryCommitted(); + + long heapMemoryInit(); + + long heapMemoryMax(); + + long heapMemoryUsed(); + + // NonHeap + long nonHeapMemoryCommitted(); + + long nonHeapMemoryInit(); + + long nonHeapMemoryMax(); + + long nonHeapMemoryUsed(); + + // PermGen + long permGenCommitted(); + + long permGenInit(); + + long permGenMax(); + + long permGenUsed(); + + // OldGen + long oldGenCommitted(); + + long oldGenInit(); + + long oldGenMax(); + + long oldGenUsed(); + + // EdenSpace + long edenSpaceCommitted(); + + long edenSpaceInit(); + + long edenSpaceMax(); + + long edenSpaceUsed(); + + // Survivor + long survivorCommitted(); + + long survivorInit(); + + long survivorMax(); + + long survivorUsed(); + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java new file mode 100644 index 00000000..81e19681 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.monitor.mbean; + + +public interface ThreadMBean { + + public int daemonThreadCount(); + + public int threadCount(); + + public long totalStartedThreadCount(); + + public int deadLockedThreadCount(); + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java new file mode 100644 index 00000000..111cb3b8 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java @@ -0,0 +1,136 @@ +package com.shinemo.mpush.monitor.mbean.impl; + +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.Map; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.monitor.mbean.BaseMBean; +import com.shinemo.mpush.monitor.mbean.GCMBean; + +public class JVMGC extends BaseMBean implements GCMBean { + + public static final JVMGC instance = new JVMGC(); + + private GarbageCollectorMXBean fullGc; + private GarbageCollectorMXBean yongGc; + + private long lastYoungGcCollectionCount = -1; + private long lastYoungGcCollectionTime = -1; + private long lastFullGcCollectionCount = -1; + private long lastFullGcCollectionTime = -1; + + + private JVMGC() { + for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { + String name = item.getName(); + if (contain(name, youngGcName)) { + yongGc = item; + } else if (contain(name,fullGcName)) { + fullGc = item; + } + } + + } + + @Override + public long yongGcCollectionCount() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionCount(); + } + + @Override + public long yongGcCollectionTime() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionTime(); + } + + @Override + public long fullGcCollectionCount() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionCount(); + } + + @Override + public long fullGcCollectionTime() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionTime(); + } + + @Override + public long spanYongGcCollectionCount() { + + long current = yongGcCollectionCount(); + if (lastYoungGcCollectionCount == -1) { + lastYoungGcCollectionCount = current; + return 0; + } else { + long result = current - lastYoungGcCollectionCount; + lastYoungGcCollectionCount = current; + return result; + } + + } + + @Override + public long spanYongGcCollectionTime() { + long current = yongGcCollectionTime(); + if (lastYoungGcCollectionTime == -1) { + lastYoungGcCollectionTime = current; + return 0; + } else { + long result = current - lastYoungGcCollectionTime; + lastYoungGcCollectionTime = current; + return result; + } + } + + @Override + public long spanFullGcCollectionCount() { + long current = fullGcCollectionCount(); + if (lastFullGcCollectionCount == -1) { + lastFullGcCollectionCount = current; + return 0; + } else { + long result = current - lastFullGcCollectionCount; + lastFullGcCollectionCount = current; + return result; + } + } + + @Override + public long spanFullGcCollectionTime() { + long current = fullGcCollectionTime(); + if (lastFullGcCollectionTime == -1) { + lastFullGcCollectionTime = current; + return 0; + } else { + long result = current - lastFullGcCollectionTime; + lastFullGcCollectionTime = current; + return result; + } + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("yongGcCollectionCount", yongGcCollectionCount()); + map.put("yongGcCollectionTime", yongGcCollectionTime()); + map.put("fullGcCollectionCount", fullGcCollectionCount()); + map.put("fullGcCollectionTime", fullGcCollectionTime()); + map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); + map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); + map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); + map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); + return map; + } + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java new file mode 100644 index 00000000..d915f7e5 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java @@ -0,0 +1,239 @@ +package com.shinemo.mpush.monitor.mbean.impl; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryPoolMXBean; +import java.util.List; +import java.util.Map; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.monitor.mbean.BaseMBean; +import com.shinemo.mpush.monitor.mbean.MemoryMBean; + +public class JVMMemory extends BaseMBean implements MemoryMBean { + + public static final JVMMemory instance = new JVMMemory(); + + private MemoryMXBean memoryMXBean; + + private MemoryPoolMXBean permGenMxBean; + private MemoryPoolMXBean oldGenMxBean; + private MemoryPoolMXBean edenSpaceMxBean; + private MemoryPoolMXBean pSSurvivorSpaceMxBean; + + private JVMMemory() { + memoryMXBean = ManagementFactory.getMemoryMXBean(); + List list = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean item : list) { + String name = item.getName(); + if (contain(name, permGenName)) { + permGenMxBean = item; + } else if (contain(name, oldGenName)) { + oldGenMxBean = item; + } else if (contain(name, edenSpaceName)) { + edenSpaceMxBean = item; + } else if (contain(name, psSurvivorName)) { + pSSurvivorSpaceMxBean = item; + } + } + } + + @Override + public long heapMemoryCommitted() { + return memoryMXBean.getHeapMemoryUsage().getCommitted(); + } + + @Override + public long heapMemoryInit() { + return memoryMXBean.getHeapMemoryUsage().getInit(); + } + + @Override + public long heapMemoryMax() { + return memoryMXBean.getHeapMemoryUsage().getMax(); + } + + @Override + public long heapMemoryUsed() { + return memoryMXBean.getHeapMemoryUsage().getUsed(); + } + + @Override + public long nonHeapMemoryCommitted() { + return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); + } + + @Override + public long nonHeapMemoryInit() { + return memoryMXBean.getNonHeapMemoryUsage().getInit(); + } + + @Override + public long nonHeapMemoryMax() { + return memoryMXBean.getNonHeapMemoryUsage().getMax(); + } + + @Override + public long nonHeapMemoryUsed() { + return memoryMXBean.getNonHeapMemoryUsage().getUsed(); + } + + @Override + public long permGenCommitted() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getCommitted(); + } + + @Override + public long permGenInit() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getInit(); + } + + @Override + public long permGenMax() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getMax(); + } + + @Override + public long permGenUsed() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getUsed(); + } + + @Override + public long oldGenCommitted() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getCommitted(); + } + + @Override + public long oldGenInit() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getInit(); + } + + @Override + public long oldGenMax() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getMax(); + } + + @Override + public long oldGenUsed() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getUsed(); + } + + @Override + public long edenSpaceCommitted() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long edenSpaceInit() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getInit(); + } + + @Override + public long edenSpaceMax() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getMax(); + } + + @Override + public long edenSpaceUsed() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getUsed(); + } + + @Override + public long survivorCommitted() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long survivorInit() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getInit(); + } + + @Override + public long survivorMax() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getMax(); + } + + @Override + public long survivorUsed() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getUsed(); + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("heapMemoryCommitted", heapMemoryCommitted()); + map.put("heapMemoryInit", heapMemoryInit()); + map.put("heapMemoryMax", heapMemoryMax()); + map.put("heapMemoryUsed", heapMemoryUsed()); + map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); + map.put("nonHeapMemoryInit", nonHeapMemoryInit()); + map.put("nonHeapMemoryMax", nonHeapMemoryMax()); + map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); + map.put("permGenCommitted", permGenCommitted()); + map.put("permGenInit", permGenInit()); + map.put("permGenMax", permGenMax()); + map.put("permGenUsed", permGenUsed()); + map.put("oldGenCommitted", oldGenCommitted()); + map.put("oldGenInit", oldGenInit()); + map.put("oldGenMax", oldGenMax()); + map.put("oldGenUsed", oldGenUsed()); + map.put("edenSpaceCommitted", edenSpaceCommitted()); + map.put("edenSpaceInit", edenSpaceInit()); + map.put("edenSpaceMax", edenSpaceMax()); + map.put("edenSpaceUsed", edenSpaceUsed()); + map.put("survivorCommitted", survivorCommitted()); + map.put("survivorInit", survivorInit()); + map.put("survivorMax", survivorMax()); + map.put("survivorUsed", survivorUsed()); + return map; + } + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java new file mode 100644 index 00000000..74831012 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java @@ -0,0 +1,60 @@ +package com.shinemo.mpush.monitor.mbean.impl; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadMXBean; +import java.util.Map; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.monitor.mbean.BaseMBean; +import com.shinemo.mpush.monitor.mbean.ThreadMBean; + +public class JVMThread extends BaseMBean implements ThreadMBean{ + + private ThreadMXBean threadMXBean; + + public static final JVMThread instance = new JVMThread(); + + private JVMThread() { + threadMXBean = ManagementFactory.getThreadMXBean(); + } + + @Override + public int daemonThreadCount() { + return threadMXBean.getDaemonThreadCount(); + } + + @Override + public int threadCount() { + return threadMXBean.getThreadCount(); + } + + @Override + public long totalStartedThreadCount() { + return threadMXBean.getTotalStartedThreadCount(); + } + + @Override + public int deadLockedThreadCount() { + try { + long[] deadLockedThreadIds = threadMXBean.findDeadlockedThreads(); + if (deadLockedThreadIds == null) { + return 0; + } + return deadLockedThreadIds.length; + } catch (Exception e) { + return 0; + } + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("daemonThreadCount", daemonThreadCount()); + map.put("threadCount", threadCount()); + map.put("totalStartedThreadCount", totalStartedThreadCount()); + map.put("deadLockedThreadCount", deadLockedThreadCount()); + return map; + } + + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java new file mode 100644 index 00000000..7e908194 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -0,0 +1,42 @@ +package com.shinemo.mpush.monitor.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.monitor.domain.MonitorData; +import com.shinemo.mpush.monitor.mbean.impl.JVMGC; +import com.shinemo.mpush.monitor.mbean.impl.JVMMemory; +import com.shinemo.mpush.monitor.mbean.impl.JVMThread; +import com.shinemo.mpush.tools.Jsons; + +public class MonitorDataCollector { + + private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); + + public static MonitorData collect(){ + MonitorData data = new MonitorData(); + data.setGcMap(JVMGC.instance.toMap()); + data.setMemoryMap(JVMMemory.instance.toMap()); + data.setThreadMap(JVMThread.instance.toMap()); + return data; + } + + public static void start(){ + new Thread(new Runnable() { + + @Override + public void run() { + while (true) { + MonitorData monitorData = MonitorDataCollector.collect(); + log.warn("monitor data:"+Jsons.toJson(monitorData)); + try { + Thread.sleep(100L); + } catch (InterruptedException e) { + log.warn("monitor data exception",e); + } + } + } + }).start(); + } + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java new file mode 100644 index 00000000..be82f2cb --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.monitor.service; + +import java.lang.management.ManagementFactory; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.management.MBeanServer; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.monitor.mbean.impl.JVMGC; +import com.shinemo.mpush.monitor.mbean.impl.JVMMemory; +import com.shinemo.mpush.monitor.mbean.impl.JVMThread; + +public class MonitorDataService { + + +} diff --git a/pom.xml b/pom.xml index 4ebd0ba5..35a96a32 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT + 1.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT @@ -123,6 +124,11 @@ mpush-client ${mpush-client-version} + + com.shinemo.mpush + mpush-monitor + ${mpush-monitor-version} + From 17ab24fcb4cc89bb9ef1d2c37d17b19c5c4d356f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 17:33:28 +0800 Subject: [PATCH 206/890] =?UTF-8?q?=E9=87=8D=E6=9E=84monitor=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/monitor/domain/MonitorData.java | 10 +++++ .../BaseMBean.java => quota/BaseQuota.java} | 4 +- .../GCMBean.java => quota/GCMQuota.java} | 4 +- .../mpush/monitor/quota/InfoQuota.java | 7 ++++ .../MemoryQuota.java} | 4 +- .../ThreadQuota.java} | 4 +- .../monitor/{mbean => quota}/impl/JVMGC.java | 8 ++-- .../mpush/monitor/quota/impl/JVMInfo.java | 37 +++++++++++++++++++ .../{mbean => quota}/impl/JVMMemory.java | 8 ++-- .../{mbean => quota}/impl/JVMThread.java | 8 ++-- .../monitor/service/MonitorDataCollector.java | 14 ++++--- .../monitor/service/MonitorDataService.java | 17 --------- 12 files changed, 82 insertions(+), 43 deletions(-) rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean/BaseMBean.java => quota/BaseQuota.java} (95%) rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean/GCMBean.java => quota/GCMQuota.java} (82%) create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean/MemoryMBean.java => quota/MemoryQuota.java} (90%) rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean/ThreadMBean.java => quota/ThreadQuota.java} (67%) rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean => quota}/impl/JVMGC.java (93%) create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean => quota}/impl/JVMMemory.java (96%) rename mpush-monitor/src/main/java/com/shinemo/mpush/monitor/{mbean => quota}/impl/JVMThread.java (85%) delete mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java index 77134372..a1157050 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java @@ -11,6 +11,8 @@ public class MonitorData { private Map gcMap; private Map threadMap; + + private Map infoMap; public MonitorData() { this.timestamp = System.currentTimeMillis(); @@ -44,4 +46,12 @@ public Long getTimestamp() { return timestamp; } + public Map getInfoMap() { + return infoMap; + } + + public void setInfoMap(Map infoMap) { + this.infoMap = infoMap; + } + } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/BaseQuota.java similarity index 95% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/BaseQuota.java index 168d8828..7565f3c7 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/BaseMBean.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/BaseQuota.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.monitor.mbean; +package com.shinemo.mpush.monitor.quota; import java.util.List; import java.util.Map; import com.google.common.collect.Lists; -public abstract class BaseMBean { +public abstract class BaseQuota { protected final static List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/GCMQuota.java similarity index 82% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/GCMQuota.java index e8284958..8fb9876f 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/GCMBean.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/GCMQuota.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.monitor.mbean; +package com.shinemo.mpush.monitor.quota; -public interface GCMBean { +public interface GCMQuota { public long yongGcCollectionCount(); diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java new file mode 100644 index 00000000..83fd44a1 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.monitor.quota; + +public interface InfoQuota { + + public String pid(); + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/MemoryQuota.java similarity index 90% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/MemoryQuota.java index b2c5c398..bd526596 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/MemoryMBean.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/MemoryQuota.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.monitor.mbean; +package com.shinemo.mpush.monitor.quota; -public interface MemoryMBean { +public interface MemoryQuota { // Heap long heapMemoryCommitted(); diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadQuota.java similarity index 67% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadQuota.java index 81e19681..2f74b679 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/ThreadMBean.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadQuota.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.monitor.mbean; +package com.shinemo.mpush.monitor.quota; -public interface ThreadMBean { +public interface ThreadQuota { public int daemonThreadCount(); diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMGC.java similarity index 93% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMGC.java index 111cb3b8..9f16fb91 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMGC.java @@ -1,14 +1,14 @@ -package com.shinemo.mpush.monitor.mbean.impl; +package com.shinemo.mpush.monitor.quota.impl; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.mbean.BaseMBean; -import com.shinemo.mpush.monitor.mbean.GCMBean; +import com.shinemo.mpush.monitor.quota.BaseQuota; +import com.shinemo.mpush.monitor.quota.GCMQuota; -public class JVMGC extends BaseMBean implements GCMBean { +public class JVMGC extends BaseQuota implements GCMQuota { public static final JVMGC instance = new JVMGC(); diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java new file mode 100644 index 00000000..d55f614a --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.monitor.quota.impl; + +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; +import java.util.Map; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.monitor.quota.BaseQuota; +import com.shinemo.mpush.monitor.quota.InfoQuota; + +public class JVMInfo extends BaseQuota implements InfoQuota{ + + public static final JVMInfo instance = new JVMInfo(); + + private RuntimeMXBean runtimeMXBean; + private String currentPid; + + private JVMInfo() { + runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + } + + @Override + public String pid() { + if (null == currentPid) { + currentPid = runtimeMXBean.getName().split("@")[0]; + } + return currentPid; + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("pid", pid()); + return map; + } + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMMemory.java similarity index 96% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMMemory.java index d915f7e5..0675c0d3 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMMemory.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.mbean.impl; +package com.shinemo.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; @@ -7,10 +7,10 @@ import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.mbean.BaseMBean; -import com.shinemo.mpush.monitor.mbean.MemoryMBean; +import com.shinemo.mpush.monitor.quota.BaseQuota; +import com.shinemo.mpush.monitor.quota.MemoryQuota; -public class JVMMemory extends BaseMBean implements MemoryMBean { +public class JVMMemory extends BaseQuota implements MemoryQuota { public static final JVMMemory instance = new JVMMemory(); diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThread.java similarity index 85% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java rename to mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThread.java index 74831012..e9285da3 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/mbean/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThread.java @@ -1,14 +1,14 @@ -package com.shinemo.mpush.monitor.mbean.impl; +package com.shinemo.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.mbean.BaseMBean; -import com.shinemo.mpush.monitor.mbean.ThreadMBean; +import com.shinemo.mpush.monitor.quota.BaseQuota; +import com.shinemo.mpush.monitor.quota.ThreadQuota; -public class JVMThread extends BaseMBean implements ThreadMBean{ +public class JVMThread extends BaseQuota implements ThreadQuota{ private ThreadMXBean threadMXBean; diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index 7e908194..344d9eb6 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -4,9 +4,10 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.monitor.domain.MonitorData; -import com.shinemo.mpush.monitor.mbean.impl.JVMGC; -import com.shinemo.mpush.monitor.mbean.impl.JVMMemory; -import com.shinemo.mpush.monitor.mbean.impl.JVMThread; +import com.shinemo.mpush.monitor.quota.impl.JVMGC; +import com.shinemo.mpush.monitor.quota.impl.JVMInfo; +import com.shinemo.mpush.monitor.quota.impl.JVMMemory; +import com.shinemo.mpush.monitor.quota.impl.JVMThread; import com.shinemo.mpush.tools.Jsons; public class MonitorDataCollector { @@ -15,6 +16,7 @@ public class MonitorDataCollector { public static MonitorData collect(){ MonitorData data = new MonitorData(); + data.setInfoMap(JVMInfo.instance.toMap()); data.setGcMap(JVMGC.instance.toMap()); data.setMemoryMap(JVMMemory.instance.toMap()); data.setThreadMap(JVMThread.instance.toMap()); @@ -28,9 +30,9 @@ public static void start(){ public void run() { while (true) { MonitorData monitorData = MonitorDataCollector.collect(); - log.warn("monitor data:"+Jsons.toJson(monitorData)); - try { - Thread.sleep(100L); + log.error("monitor data:"+Jsons.toJson(monitorData)); + try {//10s + Thread.sleep(10000L); } catch (InterruptedException e) { log.warn("monitor data exception",e); } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java deleted file mode 100644 index be82f2cb..00000000 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.shinemo.mpush.monitor.service; - -import java.lang.management.ManagementFactory; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.management.MBeanServer; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.mbean.impl.JVMGC; -import com.shinemo.mpush.monitor.mbean.impl.JVMMemory; -import com.shinemo.mpush.monitor.mbean.impl.JVMThread; - -public class MonitorDataService { - - -} From 51a6fd95c74e49f649fc312f78dc21c1c1825efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 20:45:27 +0800 Subject: [PATCH 207/890] add thread pool --- .../thread/threadpool/IgnoreRunsPolicy.java | 65 +++++++++++++++++++ .../tools/thread/threadpool/ThreadPool.java | 12 ++++ .../thread/threadpool/ThreadPoolContext.java | 38 +++++++++++ .../threadpool/cached/CachedThreadPool.java | 46 +++++++++++++ .../threadpool/fixed/FixedThreadPool.java | 42 ++++++++++++ .../threadpool/limited/LimitedThreadPool.java | 45 +++++++++++++ 6 files changed, 248 insertions(+) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java new file mode 100644 index 00000000..ffb76908 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java @@ -0,0 +1,65 @@ +package com.shinemo.mpush.tools.thread.threadpool; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.tools.Constants; +import com.shinemo.mpush.tools.JVMUtil; + +public class IgnoreRunsPolicy implements RejectedExecutionHandler{ + + private final static Logger log = LoggerFactory.getLogger(IgnoreRunsPolicy.class); + + private volatile boolean dump = false; + + private ThreadPoolContext context; + + public IgnoreRunsPolicy(ThreadPoolContext context) { + this.context = context; + } + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + dumpJVMInfo(); + throw new RejectedExecutionException(); + } + + private void dumpJVMInfo(){ + if (!dump) { + dump = true; + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + String logPath = Constants.JVM_LOG_PATH; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, "jstack.log")); + JVMUtil.jstack(jstackStream); + } catch (FileNotFoundException e) { + log.error("", "Dump JVM cache Error!", e); + } catch (Throwable t) { + log.error("", "Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { + } + } + } + } + }); + + } + } +} + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java new file mode 100644 index 00000000..a7255ac9 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java @@ -0,0 +1,12 @@ +package com.shinemo.mpush.tools.thread.threadpool; + +import java.util.concurrent.Executor; + +import com.shinemo.mpush.tools.spi.SPI; + +@SPI("cachedThreadPool") +public interface ThreadPool { + + public Executor getExecutor(ThreadPoolContext context); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java new file mode 100644 index 00000000..65aa4ab6 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush.tools.thread.threadpool; + +public class ThreadPoolContext { + + private final String name; + private final int cores; + private final int threads; + private final int queues; + private final int alive; + + public ThreadPoolContext(String name, int cores, int threads, int queues, int alive) { + this.name = name; + this.cores = cores; + this.threads = threads; + this.queues = queues; + this.alive = alive; + } + + + public String getName() { + return name; + } + public int getCores() { + return cores; + } + public int getThreads() { + return threads; + } + public int getQueues() { + return queues; + } + public int getAlive() { + return alive; + } + + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java new file mode 100644 index 00000000..9330acdb --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java @@ -0,0 +1,46 @@ +package com.shinemo.mpush.tools.thread.threadpool.cached; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import com.shinemo.mpush.tools.thread.NamedThreadFactory; +import com.shinemo.mpush.tools.thread.threadpool.IgnoreRunsPolicy; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; + +/** + * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 + * + */ +public class CachedThreadPool implements ThreadPool { + + @Override + public Executor getExecutor(ThreadPoolContext context) { + + String name = context.getName(); + int cores = context.getCores(); + int threads = context.getThreads(); + int queues = context.getQueues(); + int alive = context.getAlive(); + + final ThreadFactory threadFactory = new NamedThreadFactory(name); + + BlockingQueue blockingQueue = null; + if(queues == 0){ + blockingQueue = new SynchronousQueue(); + }else if(queues<0){ + blockingQueue = new LinkedBlockingQueue(); + }else{ + blockingQueue = new LinkedBlockingQueue(queues); + } + + return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); + + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java new file mode 100644 index 00000000..bfa9ae60 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java @@ -0,0 +1,42 @@ +package com.shinemo.mpush.tools.thread.threadpool.fixed; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import com.shinemo.mpush.tools.thread.NamedThreadFactory; +import com.shinemo.mpush.tools.thread.threadpool.IgnoreRunsPolicy; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; + +/** + *此线程池启动时即创建固定大小的线程数,不做任何伸缩 + * + */ +public class FixedThreadPool implements ThreadPool { + + @Override + public Executor getExecutor(ThreadPoolContext context) { + String name = context.getName(); + int threads = context.getThreads(); + int queues = context.getQueues(); + + BlockingQueue blockingQueue = null; + if (queues == 0) { + blockingQueue = new SynchronousQueue(); + } else if (queues < 0) { + blockingQueue = new LinkedBlockingQueue(); + } else { + blockingQueue = new LinkedBlockingQueue(queues); + } + + final ThreadFactory threadFactory = new NamedThreadFactory(name); + + return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java new file mode 100644 index 00000000..20786fe4 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java @@ -0,0 +1,45 @@ +package com.shinemo.mpush.tools.thread.threadpool.limited; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import com.shinemo.mpush.tools.thread.NamedThreadFactory; +import com.shinemo.mpush.tools.thread.threadpool.IgnoreRunsPolicy; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; + +/** + * 此线程池一直增长,直到上限,增长后不收缩。 + * + */ +public class LimitedThreadPool implements ThreadPool { + + @Override + public Executor getExecutor(ThreadPoolContext context) { + + String name = context.getName(); + int cores = context.getCores(); + int threads = context.getThreads(); + int queues = context.getQueues(); + + final ThreadFactory threadFactory = new NamedThreadFactory(name); + + BlockingQueue blockingQueue = null; + if (queues == 0) { + blockingQueue = new SynchronousQueue(); + } else if (queues < 0) { + blockingQueue = new LinkedBlockingQueue(); + } else { + blockingQueue = new LinkedBlockingQueue(queues); + } + + return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); + + } + +} From 2f8339cebf255a7fa08c69c76153e13173356186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 25 Jan 2016 20:54:24 +0800 Subject: [PATCH 208/890] thread pool monitor --- .../com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java | 5 +++++ .../shinemo/mpush/monitor/quota/impl/JVMThreadPool.java | 9 +++++++++ 2 files changed, 14 insertions(+) create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java create mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java new file mode 100644 index 00000000..0b64be9d --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java @@ -0,0 +1,5 @@ +package com.shinemo.mpush.monitor.quota; + +public interface ThreadPoolQuota { + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java new file mode 100644 index 00000000..e6a50583 --- /dev/null +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java @@ -0,0 +1,9 @@ +package com.shinemo.mpush.monitor.quota.impl; + +import com.shinemo.mpush.monitor.quota.ThreadPoolQuota; + +public class JVMThreadPool implements ThreadPoolQuota{ + + + +} From 038045a138ea84796593fc9c3b0b1d2b3bf61259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 13:22:56 +0800 Subject: [PATCH 209/890] add jvm log --- .../mpush/monitor/quota/InfoQuota.java | 1 + .../mpush/monitor/quota/impl/JVMInfo.java | 12 +++++++ .../monitor/service/MonitorDataCollector.java | 15 ++++++++ .../java/com/shinemo/mpush/tools/JVMUtil.java | 35 +++++++++++++++++++ .../thread/threadpool/ThreadPoolContext.java | 13 ++++--- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java index 83fd44a1..dea1fb61 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java @@ -4,4 +4,5 @@ public interface InfoQuota { public String pid(); + public double load(); } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java index d55f614a..bc08a2f4 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; +import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.util.Map; @@ -13,10 +14,14 @@ public class JVMInfo extends BaseQuota implements InfoQuota{ public static final JVMInfo instance = new JVMInfo(); private RuntimeMXBean runtimeMXBean; + + private OperatingSystemMXBean systemMXBean; + private String currentPid; private JVMInfo() { runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + systemMXBean = ManagementFactory.getOperatingSystemMXBean(); } @Override @@ -31,7 +36,14 @@ public String pid() { public Map toMap() { Map map = Maps.newHashMap(); map.put("pid", pid()); + map.put("load", load()); return map; } + @Override + public double load() { + double averageLoad = systemMXBean.getSystemLoadAverage(); + return averageLoad; + } + } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index 344d9eb6..b85fb11b 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -8,12 +8,17 @@ import com.shinemo.mpush.monitor.quota.impl.JVMInfo; import com.shinemo.mpush.monitor.quota.impl.JVMMemory; import com.shinemo.mpush.monitor.quota.impl.JVMThread; +import com.shinemo.mpush.tools.JVMUtil; import com.shinemo.mpush.tools.Jsons; public class MonitorDataCollector { private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); + private static volatile boolean dump = false; + + private static String path = "/opt/logs/bops/"; + public static MonitorData collect(){ MonitorData data = new MonitorData(); data.setInfoMap(JVMInfo.instance.toMap()); @@ -31,6 +36,16 @@ public void run() { while (true) { MonitorData monitorData = MonitorDataCollector.collect(); log.error("monitor data:"+Jsons.toJson(monitorData)); + + double load = JVMInfo.instance.load(); + if(load>2){ + if(!dump){ + dump = true; + JVMUtil.dumpJstack(path); + } + + } + try {//10s Thread.sleep(10000L); } catch (InterruptedException e) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java index 2d5f57b1..488453a0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java @@ -1,11 +1,21 @@ package com.shinemo.mpush.tools; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.OutputStream; import java.util.Iterator; import java.util.Map; +import java.util.concurrent.Executors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class JVMUtil { + private static final Logger log = LoggerFactory.getLogger(JVMUtil.class); + public static void jstack(OutputStream stream) throws Exception { try { Map map = Thread.getAllStackTraces(); @@ -27,5 +37,30 @@ public static void jstack(OutputStream stream) throws Exception { throw e; } } + + public static void dumpJstack(final String jvmPath){ + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + String logPath = jvmPath; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, "jstack.log")); + JVMUtil.jstack(jstackStream); + } catch (FileNotFoundException e) { + log.error("", "Dump JVM cache Error!", e); + } catch (Throwable t) { + log.error("", "Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { + } + } + } + } + }); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index 65aa4ab6..6afc36cf 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -2,21 +2,20 @@ public class ThreadPoolContext { - private final String name; - private final int cores; - private final int threads; - private final int queues; - private final int alive; + private final String name;//名字 + private final int cores; //最小线程大小 + private final int threads; //最大线程大小 + private final int queues; // queues > 0,则FIFO队列, + private final int alive;// 存活时间 public ThreadPoolContext(String name, int cores, int threads, int queues, int alive) { this.name = name; this.cores = cores; this.threads = threads; - this.queues = queues; + this.queues = queues; this.alive = alive; } - public String getName() { return name; } From 67ed534b76a5691906e36f8e65c167d332f4fe55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 13:49:20 +0800 Subject: [PATCH 210/890] add jmap --- .../monitor/service/MonitorDataCollector.java | 29 +++++-- .../java/com/shinemo/mpush/tools/JVMUtil.java | 79 ++++++++++++++++++- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index b85fb11b..2fa10034 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -15,9 +15,11 @@ public class MonitorDataCollector { private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); - private static volatile boolean dump = false; + private static volatile boolean dumpJstack = false; - private static String path = "/opt/logs/bops/"; + private static volatile boolean dumpJmap = false; + + private static String currentPath = "/opt/logs/"; public static MonitorData collect(){ MonitorData data = new MonitorData(); @@ -28,6 +30,10 @@ public static MonitorData collect(){ return data; } + public void setPath(String path) { + currentPath = path; + } + public static void start(){ new Thread(new Runnable() { @@ -38,14 +44,21 @@ public void run() { log.error("monitor data:"+Jsons.toJson(monitorData)); double load = JVMInfo.instance.load(); - if(load>2){ - if(!dump){ - dump = true; - JVMUtil.dumpJstack(path); + if(load>4){ + if(!dumpJstack){ + dumpJstack = true; + JVMUtil.dumpJstack(currentPath); } } + if(load>4){ + if(!dumpJmap){ + dumpJmap = true; + JVMUtil.dumpJmap(currentPath); + } + } + try {//10s Thread.sleep(10000L); } catch (InterruptedException e) { @@ -56,4 +69,8 @@ public void run() { }).start(); } + public static void main(String[] args) { + MonitorDataCollector.start(); + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java index 488453a0..8d6bb2a1 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java @@ -5,17 +5,31 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.lang.management.ManagementFactory; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; import java.util.Iterator; import java.util.Map; +import java.util.Set; import java.util.concurrent.Executors; +import javax.management.MBeanServer; +import javax.management.ObjectName; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.sun.management.HotSpotDiagnosticMXBean; + public class JVMUtil { private static final Logger log = LoggerFactory.getLogger(JVMUtil.class); + private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; + private static volatile HotSpotDiagnosticMXBean hotspotMBean; + + private static Object lock = new Object(); + public static void jstack(OutputStream stream) throws Exception { try { Map map = Thread.getAllStackTraces(); @@ -45,7 +59,7 @@ public void run() { String logPath = jvmPath; FileOutputStream jstackStream = null; try { - jstackStream = new FileOutputStream(new File(logPath, "jstack.log")); + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.log")); JVMUtil.jstack(jstackStream); } catch (FileNotFoundException e) { log.error("", "Dump JVM cache Error!", e); @@ -62,5 +76,68 @@ public void run() { } }); } + + private static HotSpotDiagnosticMXBean getHotspotMBean() { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + public HotSpotDiagnosticMXBean run() throws Exception { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + Set s = server.queryNames(new ObjectName(HOTSPOT_BEAN_NAME), null); + Iterator itr = s.iterator(); + if (itr.hasNext()) { + ObjectName name = itr.next(); + HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, + name.toString(), HotSpotDiagnosticMXBean.class); + return bean; + } else { + return null; + } + } + }); + } catch (Exception e) { + log.error("", "getHotspotMBean Error!", e); + return null; + } + } + + private static void initHotspotMBean() throws Exception { + if (hotspotMBean == null) { + synchronized (lock) { + if (hotspotMBean == null) { + hotspotMBean = getHotspotMBean(); + } + } + } + } + + public static void jMap(String fileName, boolean live) { + try { + initHotspotMBean(); + + + + File f = new File(fileName, System.currentTimeMillis()+"-jmap.log"); + String currentFileName = f.getPath(); + if (f.exists()) { + f.delete(); + } + + + hotspotMBean.dumpHeap(currentFileName, live); + } catch (Exception e) { + log.error("", "dumpHeap Error!", e); + } + } + + public static void dumpJmap(final String jvmPath){ + + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + jMap(jvmPath, false); + } + }); + + } } From ee3cc345a48f92b2e6db2a266971ca48e5e17cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 14:25:04 +0800 Subject: [PATCH 211/890] add monitor --- .../monitor/service/MonitorDataCollector.java | 37 ++++++++++++++++--- pom.xml | 4 +- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index 2fa10034..3da6f1d4 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -15,12 +15,24 @@ public class MonitorDataCollector { private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); - private static volatile boolean dumpJstack = false; + private static volatile boolean dumpFirstJstack = false; + + private static volatile boolean dumpSecondJstack = false; + + private static volatile boolean dumpThirdJstack = false; private static volatile boolean dumpJmap = false; private static String currentPath = "/opt/logs/"; + private static int firstJstack = 2; + + private static int secondJstack = 4; + + private static int thirdJstack = 6; + + private static int firstJmap = 4; + public static MonitorData collect(){ MonitorData data = new MonitorData(); data.setInfoMap(JVMInfo.instance.toMap()); @@ -44,15 +56,28 @@ public void run() { log.error("monitor data:"+Jsons.toJson(monitorData)); double load = JVMInfo.instance.load(); - if(load>4){ - if(!dumpJstack){ - dumpJstack = true; + if(load>firstJstack){ + if(!dumpFirstJstack){ + dumpFirstJstack = true; JVMUtil.dumpJstack(currentPath); } - } - if(load>4){ + if(load>secondJstack){ + if(!dumpSecondJstack){ + dumpSecondJstack = true; + JVMUtil.dumpJmap(currentPath); + } + } + + if(load>thirdJstack){ + if(!dumpThirdJstack){ + dumpThirdJstack = true; + JVMUtil.dumpJmap(currentPath); + } + } + + if(load>firstJmap){ if(!dumpJmap){ dumpJmap = true; JVMUtil.dumpJmap(currentPath); diff --git a/pom.xml b/pom.xml index 35a96a32..f7feaa68 100644 --- a/pom.xml +++ b/pom.xml @@ -231,7 +231,7 @@ true - + From 723fc88bb2cd8c26b25c024d8960596be76263a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 15:00:30 +0800 Subject: [PATCH 212/890] update client version --- mpush-monitor/pom.xml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index de48abb7..f66bee0e 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -9,7 +9,7 @@ mpush-monitor jar - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT mpush-monitor diff --git a/pom.xml b/pom.xml index f7feaa68..4ec0a748 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,7 @@ 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT From f6753e72bdcd7d079ac1b29b919617b63e294739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 17:03:40 +0800 Subject: [PATCH 213/890] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor/quota/impl/JVMThreadPool.java | 32 +++++++++++- .../monitor/service/MonitorDataCollector.java | 4 +- .../com/shinemo/mpush/tools/Constants.java | 4 ++ .../mpush/tools/config/ConfigCenter.java | 3 ++ .../mpush/tools/thread/ThreadNameSpace.java | 4 ++ .../thread/threadpool/IgnoreRunsPolicy.java | 37 +++----------- .../thread/threadpool/ThreadPoolContext.java | 20 +++++++- .../thread/threadpool/ThreadPoolManager.java | 51 +++++++++++++++++++ .../threadpool/cached/CachedThreadPool.java | 2 +- .../cached/CachedThreadPoolContext.java | 15 ++++++ .../fixed/FixedThreadPoolContext.java | 15 ++++++ .../threadpool/limited/LimitedThreadPool.java | 45 ---------------- ...o.mpush.tools.thread.threadpool.ThreadPool | 2 + 13 files changed, 154 insertions(+), 80 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java create mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java index e6a50583..77a52e87 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java @@ -1,8 +1,38 @@ package com.shinemo.mpush.monitor.quota.impl; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.monitor.quota.BaseQuota; import com.shinemo.mpush.monitor.quota.ThreadPoolQuota; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; + +public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota{ -public class JVMThreadPool implements ThreadPoolQuota{ + private Map pool = null; + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + if(pool == null){ + pool = ThreadPoolManager.getPool(); + } + + Iterator> ite = pool.entrySet().iterator(); + while (ite.hasNext()) { + Map.Entry entry = ite.next(); + String serviceUniqName = entry.getKey(); + ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue(); + StringBuilder sb = new StringBuilder(); + sb.append("coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:" + + executor.getActiveCount()); + map.put(serviceUniqName, sb.toString()); + } + return map; + } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index 3da6f1d4..74c93567 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -84,8 +84,8 @@ public void run() { } } - try {//10s - Thread.sleep(10000L); + try {//30s + Thread.sleep(30000L); } catch (InterruptedException e) { log.warn("monitor data exception",e); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 0148c9c8..ff85cda6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -20,6 +20,10 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 10; int MAX_WORK_POOL_SIZE = 250; + + int BIZ_POOL_SIZE = 20; + + int EVENT_BUS_POOL_SIZE = 10; //zk int ZK_MAX_RETRY = 3; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 4f8cd93d..29c56fb1 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -88,4 +88,7 @@ public interface ConfigCenter extends Config { @Key("scan_conn_task_cycle") @DefaultValue("59000") long scanConnTaskCycle(); + + @DefaultValue("/opt/shinemo/mpush/") + String logPath(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index 08cfdf1d..effd185a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -11,6 +11,10 @@ public class ThreadNameSpace { * netty worker 线程 */ public static final String NETTY_WORKER = "mp-worker"; + + public static final String EVENT_BUS = "mp-event-bus"; + + public static final String BIZ = "mp-biz"; /** * connection 定期检测线程 diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java index ffb76908..3c30985e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java @@ -1,10 +1,5 @@ package com.shinemo.mpush.tools.thread.threadpool; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; @@ -12,8 +7,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.JVMUtil; +import com.shinemo.mpush.tools.config.ConfigCenter; public class IgnoreRunsPolicy implements RejectedExecutionHandler{ @@ -21,7 +16,9 @@ public class IgnoreRunsPolicy implements RejectedExecutionHandler{ private volatile boolean dump = false; - private ThreadPoolContext context; + private static final String preFixPath = ConfigCenter.holder.logPath(); + + private final ThreadPoolContext context; public IgnoreRunsPolicy(ThreadPoolContext context) { this.context = context; @@ -36,29 +33,9 @@ public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { private void dumpJVMInfo(){ if (!dump) { dump = true; - Executors.newSingleThreadExecutor().execute(new Runnable() { - @Override - public void run() { - String logPath = Constants.JVM_LOG_PATH; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, "jstack.log")); - JVMUtil.jstack(jstackStream); - } catch (FileNotFoundException e) { - log.error("", "Dump JVM cache Error!", e); - } catch (Throwable t) { - log.error("", "Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } - } - } - } - }); - + log.error("start dump jvm info"); + JVMUtil.dumpJstack(preFixPath+"/"+context.getName()); + log.error("end dump jvm info"); } } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index 6afc36cf..3844cc04 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -1,5 +1,11 @@ package com.shinemo.mpush.tools.thread.threadpool; +import com.shinemo.mpush.tools.Constants; +import com.shinemo.mpush.tools.thread.ThreadNameSpace; +import com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; +import com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; + + public class ThreadPoolContext { private final String name;//名字 @@ -8,6 +14,14 @@ public class ThreadPoolContext { private final int queues; // queues > 0,则FIFO队列, private final int alive;// 存活时间 + public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE, 1000*60*5); + + public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 1000*60*5); + + public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); + + public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); + public ThreadPoolContext(String name, int cores, int threads, int queues, int alive) { this.name = name; this.cores = cores; @@ -32,6 +46,10 @@ public int getAlive() { return alive; } - + @Override + public String toString() { + return "ThreadPoolContext [name=" + name + ", cores=" + cores + ", threads=" + threads + ", queues=" + queues + ", alive=" + alive + "]"; + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java new file mode 100644 index 00000000..e9f39c6e --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -0,0 +1,51 @@ +package com.shinemo.mpush.tools.thread.threadpool; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + + +import com.shinemo.mpush.tools.spi.ServiceContainer; + +public class ThreadPoolManager { + + private static final Map poolCache = new HashMap(); + + private static ThreadPool cachedThreadPool = ServiceContainer.getInstance(ThreadPool.class, "cachedThreadPool"); + + private static ThreadPool fixedThreadPool = ServiceContainer.getInstance(ThreadPool.class, "fixedThreadPool"); + + public static Executor bossExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.BOSS_THREAD_POOL); + public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); + public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); + public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); + + static{ + poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); + poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); + poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); + poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); + } + + public static final Map getPool(){ + return poolCache; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("current thread allocation policy:"); + Iterator> ite = poolCache.entrySet().iterator(); + while (ite.hasNext()) { + Map.Entry entry = ite.next(); + String serviceUniqName = entry.getKey(); + ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue(); + sb.append("serviceName[" + serviceUniqName + "]coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:" + + executor.getActiveCount()); + } + + return sb.toString(); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java index 9330acdb..69b30112 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java @@ -42,5 +42,5 @@ public Executor getExecutor(ThreadPoolContext context) { return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); } - + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java new file mode 100644 index 00000000..18af06de --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.tools.thread.threadpool.cached; + +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; + +public class CachedThreadPoolContext extends ThreadPoolContext{ + + public CachedThreadPoolContext(String name, int cores, int threads, int alive) { + super(name, cores, threads, 0, alive); + } + + public static CachedThreadPoolContext create(String name, int cores, int threads, int alive){ + return new CachedThreadPoolContext(name, cores, threads, alive); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java new file mode 100644 index 00000000..26319abd --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.tools.thread.threadpool.fixed; + +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; + +public class FixedThreadPoolContext extends ThreadPoolContext{ + + public FixedThreadPoolContext(String name, int threads) { + super(name, 0, threads, -1, 0); + } + + public static FixedThreadPoolContext create(String name,int threads){ + return new FixedThreadPoolContext(name, threads); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java deleted file mode 100644 index 20786fe4..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/limited/LimitedThreadPool.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.shinemo.mpush.tools.thread.threadpool.limited; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.shinemo.mpush.tools.thread.NamedThreadFactory; -import com.shinemo.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; - -/** - * 此线程池一直增长,直到上限,增长后不收缩。 - * - */ -public class LimitedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - - String name = context.getName(); - int cores = context.getCores(); - int threads = context.getThreads(); - int queues = context.getQueues(); - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - BlockingQueue blockingQueue = null; - if (queues == 0) { - blockingQueue = new SynchronousQueue(); - } else if (queues < 0) { - blockingQueue = new LinkedBlockingQueue(); - } else { - blockingQueue = new LinkedBlockingQueue(queues); - } - - return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - - } - -} diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool new file mode 100644 index 00000000..0badf96e --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool @@ -0,0 +1,2 @@ +cachedThreadPool=com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPool +fixedThreadPool=com.shinemo.mpush.tools.thread.threadpool.cached.FixedThreadPool \ No newline at end of file From 3bb34a75a8802e49a9c4f38a1820b077142dadd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 17:14:59 +0800 Subject: [PATCH 214/890] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/common/AbstractServer.java | 10 ++--- .../com/shinemo/mpush/common/EventBus.java | 5 ++- .../mpush/netty/server/NettyServer.java | 10 ++--- .../shinemo/mpush/tools/redis/RedisUtil.java | 8 ++-- .../mpush/tools/thread/ThreadPoolUtil.java | 41 ------------------- 5 files changed, 17 insertions(+), 57 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 0abc6124..8fdc28fc 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -13,8 +13,7 @@ import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.thread.ThreadNameSpace; -import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; @@ -88,7 +87,7 @@ private void initServer(){ //step6 启动 netty server public void startServer(final Server server){ - ThreadPoolUtil.newThread(new Runnable() { + Runnable runnable = new Runnable() { @Override public void run() { server.init(); @@ -105,8 +104,9 @@ public void onFailure(String message) { } }); } - }, ThreadNameSpace.getServerName(server.getClass().getSimpleName()), false).start(); - + }; + ThreadPoolManager.bizExecutor.execute(runnable); + } //step7 注册应用到zk diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java b/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java index b78a8acd..4cbbb4b1 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java @@ -5,7 +5,8 @@ import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; import com.shinemo.mpush.api.event.Event; -import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,7 +21,7 @@ public class EventBus { private final com.google.common.eventbus.EventBus eventBus; public EventBus() { - Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor("event-bus-pool", 10, 10); + Executor executor = ThreadPoolManager.eventBusExecutor; eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 00a1bc88..757d1a19 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -3,7 +3,7 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.netty.codec.PacketDecoder; import com.shinemo.mpush.netty.codec.PacketEncoder; -import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -156,15 +156,15 @@ public void operationComplete(ChannelFuture future) throws Exception { } private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolUtil.getBossExecutor()); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolUtil.getWorkExecutor()); + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1,ThreadPoolManager.bossExecutor); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } private void createEpollServer(final Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolUtil.getBossExecutor()); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolUtil.getWorkExecutor()); + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 8d6033bb..014ff0e2 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -5,7 +5,7 @@ import java.util.Map; import java.util.Set; -import com.shinemo.mpush.tools.thread.ThreadPoolUtil; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -476,14 +476,14 @@ public static void publish(RedisNode node, String channel, T message) { } public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { - int i = 0; for (final RedisNode node : nodeList) { - ThreadPoolUtil.newThread(new Runnable() { + Runnable runnable = new Runnable() { @Override public void run() { subscribe(node, pubsub, channels); } - }, ("redis-subscribe-" + i++)).start(); + }; + ThreadPoolManager.bizExecutor.execute(runnable); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java deleted file mode 100644 index 096bb25a..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolUtil.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.shinemo.mpush.tools.thread; - -import java.util.concurrent.Executor; - -import com.shinemo.mpush.tools.Constants; - - -public class ThreadPoolUtil { - - private static final ThreadPoolManager threadPoolManager = - new ThreadPoolManager(Constants.MIN_POOL_SIZE, Constants.MAX_POOL_SIZE, Constants.THREAD_QUEUE_SIZE); - - private static Executor bossExecutor = ThreadPoolUtil.getThreadPoolManager() - .getThreadExecutor(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE); - private static Executor workExecutor = ThreadPoolUtil.getThreadPoolManager() - .getThreadExecutor(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE); - - public static ThreadPoolManager getThreadPoolManager() { - return threadPoolManager; - } - - public static Executor getBossExecutor() { - return bossExecutor; - } - - public static Executor getWorkExecutor() { - return workExecutor; - } - - - public static Thread newThread(Runnable r, String name, boolean daemon) { - Thread t = new Thread(r); - t.setDaemon(daemon); - t.setName(ThreadNameSpace.THREAD_NAME_PREFIX + name); - return t; - } - - public static Thread newThread(Runnable r, String name) { - return newThread(r, name, true); - } -} From 964a1e4ac188743af7b748b26718a38fc5f4a776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 26 Jan 2016 17:15:42 +0800 Subject: [PATCH 215/890] remove unused --- .../mpush/tools/thread/ThreadPoolManager.java | 220 ------------------ 1 file changed, 220 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java deleted file mode 100644 index aea701e7..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadPoolManager.java +++ /dev/null @@ -1,220 +0,0 @@ -package com.shinemo.mpush.tools.thread; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.JVMUtil; - - -public class ThreadPoolManager { - - private static final Logger log = LoggerFactory.getLogger(ThreadPoolManager.class); - - private static final long keepAliveTime = 300L; - - private final RejectedExecutionHandler handler = new IgnoreRunsPolicy(); - - private final ThreadPoolExecutor defaultPoolExecutor; - - private final Map poolCache = new HashMap(); - - public ThreadPoolManager(int corePoolSize,int maxPoolSize,int queueSize){ - final BlockingQueue workQueue = new SynchronousQueue(); - final ThreadFactory threadFactory = new NamedThreadFactory(ThreadNameSpace.NETTY_WORKER); - defaultPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, - TimeUnit.SECONDS, workQueue,threadFactory,handler); - } - - - private static class IgnoreRunsPolicy implements RejectedExecutionHandler{ - - private final static Logger log = LoggerFactory.getLogger(IgnoreRunsPolicy.class); - - private volatile boolean dump = false; - - @Override - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - dumpJVMInfo(); - throw new RejectedExecutionException(); - } - - private void dumpJVMInfo(){ - if (!dump) { - dump = true; - Executors.newSingleThreadExecutor().execute(new Runnable() { - @Override - public void run() { - String logPath = Constants.JVM_LOG_PATH; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, "jstack.log")); - JVMUtil.jstack(jstackStream); - } catch (FileNotFoundException e) { - log.error("", "Dump JVM cache Error!", e); - } catch (Throwable t) { - log.error("", "Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } - } - } - } - }); - - } - } - - } - - - public void allocThreadPool(final String serviceUniqueName, int corePoolSize, int maximumPoolSize) - throws Exception { - if (poolCache.containsKey(serviceUniqueName)) { // 对同一个服务重复分配线程池时,抛出异常 - throw new Exception(MessageFormat.format( - "[ThreadPool Manager] Duplicated thread pool allocation request for service [{0}].", - new Object[] { serviceUniqueName })); - } - - if (defaultPoolExecutor == null || defaultPoolExecutor.isShutdown()) { // 线程池已关闭 - throw new Exception(MessageFormat.format( - "[ThreadPool Manager] Can not allocate thread pool for service [{0}].", - new Object[] { serviceUniqueName })); - } - - int balance = defaultPoolExecutor.getMaximumPoolSize(); // 剩余线程数量 - // 剩余线程数量小于申请的线程数量 - if (balance < maximumPoolSize) { - throw new Exception( - MessageFormat - .format("[ThreadPool Manager] Thread pool allocated failed for service [{0}]: balance [{1}] require [{2}].", - new Object[] { serviceUniqueName, balance, maximumPoolSize })); - } - - ThreadFactory threadFactory = new NamedThreadFactory( - ThreadNameSpace.getUniqueName(serviceUniqueName)); - try { - ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, - TimeUnit.SECONDS, new SynchronousQueue(), threadFactory, handler); - poolCache.put(serviceUniqueName, executor); - } catch (Exception e) { - throw new Exception("[ThreadPool Manager] Thread pool allocated failed!", e); - } - - // 重新设置剩余线程数量 - int newBalance = balance - maximumPoolSize; - if (newBalance == 0) { - defaultPoolExecutor.shutdown(); - } else { - if (newBalance < defaultPoolExecutor.getCorePoolSize()) { - defaultPoolExecutor.setCorePoolSize(newBalance); - } - defaultPoolExecutor.setMaximumPoolSize(newBalance); - } - } - - /** - * 不存在,则创建 - * @param serviceUniqueName - * @param corePoolSize - * @param maximumPoolSize - * @return - * @throws Exception - */ - public Executor getThreadExecutor(String serviceUniqueName,int corePoolSize, int maximumPoolSize) { - if (!poolCache.isEmpty()) { - ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); - if (executor != null) { - return executor; - }else{ - try{ - allocThreadPool(serviceUniqueName, corePoolSize, maximumPoolSize); - }catch(Exception e){ - log.error("allocThreadPool exception",e); - } - } - executor = poolCache.get(serviceUniqueName); - if(executor!=null){ - return executor; - } - }else{ - try{ - allocThreadPool(serviceUniqueName, corePoolSize, maximumPoolSize); - }catch(Exception e){ - log.error("allocThreadPool exception",e); - } - ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); - if (executor != null) { - return executor; - } - } - return defaultPoolExecutor; - } - - public Executor getThreadExecutor(String serviceUniqueName) { - if (!poolCache.isEmpty()) { - ThreadPoolExecutor executor = poolCache.get(serviceUniqueName); - if (executor != null) { - return executor; - } - } - return defaultPoolExecutor; - } - - public void shutdown() { - if (defaultPoolExecutor != null && !defaultPoolExecutor.isShutdown()) { - defaultPoolExecutor.shutdown(); - } - - if (!poolCache.isEmpty()) { - Iterator ite = poolCache.values().iterator(); - while (ite.hasNext()) { - ThreadPoolExecutor poolExecutor = ite.next(); - poolExecutor.shutdown(); - } - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("当前线程池分配策略:"); - Iterator> ite = poolCache.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - String serviceUniqName = entry.getKey(); - ThreadPoolExecutor executor = entry.getValue(); - sb.append("服务[" + serviceUniqName + "]核心线程数量:" + executor.getCorePoolSize() + " 最大线程数量:" - + executor.getMaximumPoolSize() + " 活动线程数量:" + executor.getActiveCount()); - } - - if (!defaultPoolExecutor.isShutdown()) { - sb.append("服务默认使用的核心线程数量:" + defaultPoolExecutor.getCorePoolSize() + " 最大线程数量: " - + defaultPoolExecutor.getMaximumPoolSize() + " 活动线程数量:" + defaultPoolExecutor.getActiveCount()); - } - - return sb.toString(); - } - - -} From 709136a9c7a3e1d058b43270ff87c3772c494bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 13:46:26 +0800 Subject: [PATCH 216/890] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-tools/pom.xml | 2 +- .../thread/threadpool/ThreadPoolContext.java | 48 ++++++++++--------- .../thread/threadpool/ThreadPoolManager.java | 17 +------ .../threadpool/cached/CachedThreadPool.java | 16 +++---- .../cached/CachedThreadPoolContext.java | 8 ++-- .../threadpool/fixed/FixedThreadPool.java | 12 ++--- .../fixed/FixedThreadPoolContext.java | 10 +++- .../shinemo/mpush/tools/thread/SyncTest.java | 16 +++++++ pom.xml | 6 +-- 9 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index e7b42be7..f139996b 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -10,7 +10,7 @@ 4.0.0 mpush-tools - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT jar diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index 3844cc04..0364731b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -9,47 +9,51 @@ public class ThreadPoolContext { private final String name;//名字 - private final int cores; //最小线程大小 - private final int threads; //最大线程大小 - private final int queues; // queues > 0,则FIFO队列, - private final int alive;// 存活时间 + private final int corePoolSize; //最小线程大小 + private final int maxPoolSize; //最大线程大小 + private final int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) + private final int keepAliveSeconds;// 存活时间 - public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE, 1000*60*5); + public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE, 60*5); - public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 1000*60*5); + public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5); public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); - public ThreadPoolContext(String name, int cores, int threads, int queues, int alive) { + public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { this.name = name; - this.cores = cores; - this.threads = threads; - this.queues = queues; - this.alive = alive; + this.corePoolSize = corePoolSize; + this.maxPoolSize = maxPoolSize; + this.queueCapacity = queueCapacity; + this.keepAliveSeconds = keepAliveSeconds; } - + public String getName() { return name; } - public int getCores() { - return cores; + + public int getCorePoolSize() { + return corePoolSize; } - public int getThreads() { - return threads; + + public int getMaxPoolSize() { + return maxPoolSize; } - public int getQueues() { - return queues; + + public int getQueueCapacity() { + return queueCapacity; } - public int getAlive() { - return alive; + + public int getKeepAliveSeconds() { + return keepAliveSeconds; } @Override public String toString() { - return "ThreadPoolContext [name=" + name + ", cores=" + cores + ", threads=" + threads + ", queues=" + queues + ", alive=" + alive + "]"; + return "ThreadPoolContext [name=" + name + ", corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity + ", keepAliveSeconds=" + keepAliveSeconds + + "]"; } - } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java index e9f39c6e..78eacb6a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -1,10 +1,8 @@ package com.shinemo.mpush.tools.thread.threadpool; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; import com.shinemo.mpush.tools.spi.ServiceContainer; @@ -33,19 +31,6 @@ public static final Map getPool(){ return poolCache; } - @Override - public String toString() { - StringBuilder sb = new StringBuilder("current thread allocation policy:"); - Iterator> ite = poolCache.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - String serviceUniqName = entry.getKey(); - ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue(); - sb.append("serviceName[" + serviceUniqName + "]coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:" - + executor.getActiveCount()); - } - - return sb.toString(); - } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java index 69b30112..3e8df902 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java @@ -23,23 +23,23 @@ public class CachedThreadPool implements ThreadPool { public Executor getExecutor(ThreadPoolContext context) { String name = context.getName(); - int cores = context.getCores(); - int threads = context.getThreads(); - int queues = context.getQueues(); - int alive = context.getAlive(); + int corePoolSize = context.getCorePoolSize(); + int maxPoolSize = context.getMaxPoolSize(); + int queueCapacity = context.getQueueCapacity(); + int keepAliveSeconds = context.getKeepAliveSeconds(); final ThreadFactory threadFactory = new NamedThreadFactory(name); BlockingQueue blockingQueue = null; - if(queues == 0){ + if(queueCapacity == 0){ blockingQueue = new SynchronousQueue(); - }else if(queues<0){ + }else if(queueCapacity<0){ blockingQueue = new LinkedBlockingQueue(); }else{ - blockingQueue = new LinkedBlockingQueue(queues); + blockingQueue = new LinkedBlockingQueue(queueCapacity); } - return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); + return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java index 18af06de..d5dc4920 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java @@ -4,12 +4,12 @@ public class CachedThreadPoolContext extends ThreadPoolContext{ - public CachedThreadPoolContext(String name, int cores, int threads, int alive) { - super(name, cores, threads, 0, alive); + public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) { + super(name, corePoolSize, maxPoolSize, 0, keepAliveSeconds); } - public static CachedThreadPoolContext create(String name, int cores, int threads, int alive){ - return new CachedThreadPoolContext(name, cores, threads, alive); + public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds){ + return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java index bfa9ae60..136c4f80 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java @@ -22,21 +22,21 @@ public class FixedThreadPool implements ThreadPool { @Override public Executor getExecutor(ThreadPoolContext context) { String name = context.getName(); - int threads = context.getThreads(); - int queues = context.getQueues(); + int corePoolSize = context.getCorePoolSize(); + int queueCapacity = context.getQueueCapacity(); BlockingQueue blockingQueue = null; - if (queues == 0) { + if (queueCapacity == 0) { blockingQueue = new SynchronousQueue(); - } else if (queues < 0) { + } else if (queueCapacity < 0) { blockingQueue = new LinkedBlockingQueue(); } else { - blockingQueue = new LinkedBlockingQueue(queues); + blockingQueue = new LinkedBlockingQueue(queueCapacity); } final ThreadFactory threadFactory = new NamedThreadFactory(name); - return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); + return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java index 26319abd..0bc0f92d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java @@ -4,12 +4,20 @@ public class FixedThreadPoolContext extends ThreadPoolContext{ + public FixedThreadPoolContext(String name, int threads,int queueCapacity) { + super(name, threads, 0, queueCapacity, 0); + } + public FixedThreadPoolContext(String name, int threads) { - super(name, 0, threads, -1, 0); + super(name, threads, 0, -1, 0); } public static FixedThreadPoolContext create(String name,int threads){ return new FixedThreadPoolContext(name, threads); } + + public static FixedThreadPoolContext create(String name,int threads,int queueCapacity){ + return new FixedThreadPoolContext(name, threads,queueCapacity); + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java new file mode 100644 index 00000000..1171fb7a --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.tools.thread; + + +import org.junit.Test; + + +public class SyncTest { + + + + @Test + public void test(){ + + } + +} diff --git a/pom.xml b/pom.xml index 4ec0a748..7ea194d0 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ 1.7 4.0.0.RELEASE 1.0-SNAPSHOT - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT @@ -231,7 +231,7 @@ true - + From 8bcce8078ffea5bb5f8b2cf7a3ec1d7c3e995b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 15:10:54 +0800 Subject: [PATCH 217/890] add thread pool test --- mpush-cs/src/main/resources/logback.xml | 8 +- mpush-log/pom.xml | 27 +++- mpush-tools/pom.xml | 5 +- .../cached/CachedThreadPoolContext.java | 10 +- .../shinemo/mpush/tools/thread/SyncTest.java | 130 +++++++++++++++++- pom.xml | 9 +- 6 files changed, 172 insertions(+), 17 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 76594e12..3e167126 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -4,7 +4,7 @@ System.out UTF-8 - error + INFO %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -15,7 +15,7 @@ System.err UTF-8 - error + WARN %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -24,7 +24,7 @@ - - + + diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml index ec280dd5..6ae2adfb 100644 --- a/mpush-log/pom.xml +++ b/mpush-log/pom.xml @@ -8,6 +8,7 @@ 4.0.0 mpush-log + 1.0-SNAPSHOT jar mpush-log @@ -18,11 +19,25 @@ - - junit - junit - 3.8.1 - test - + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + commons-logging + commons-logging + + + log4j + log4j + + + org.logback-extensions + logback-ext-spring + diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index f139996b..559c7e72 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -41,7 +41,10 @@ org.slf4j slf4j-api - + + com.shinemo.mpush + mpush-log + org.aeonbits.owner owner diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java index d5dc4920..c2119e65 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java @@ -5,11 +5,19 @@ public class CachedThreadPoolContext extends ThreadPoolContext{ public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) { - super(name, corePoolSize, maxPoolSize, 0, keepAliveSeconds); + this(name, corePoolSize, maxPoolSize, keepAliveSeconds, 0); + } + + public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity) { + super(name, corePoolSize, maxPoolSize, queueCapacity, keepAliveSeconds); } public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds){ return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds); } + + public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity){ + return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds,queueCapacity); + } } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java index 1171fb7a..197bede5 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java @@ -1,16 +1,142 @@ package com.shinemo.mpush.tools.thread; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import org.junit.Before; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; +import com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPool; +import com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; +import com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPool; +import com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; public class SyncTest { + final static ThreadPool cachedThreadPool = new CachedThreadPool(); + final static ThreadPool fixedThreadPool = new FixedThreadPool(); + + // 最小线程1,最大线程2,使用sync blockqueue,线程缓存5分钟 + final static ThreadPoolContext cachedThreadPoolContext = CachedThreadPoolContext.create("cached-test", 1, 2, 60*5); + + // 最小线程1,最大线程2,使用LinkedBlockingQueue,线程缓存5分钟 + final static ThreadPoolContext cachedThreadPoolContext2 = CachedThreadPoolContext.create("cached-test2", 1, 2, 60*5,1); + + //线程数1,blockqueue大小为2 + final static ThreadPoolContext fixedThreadPoolContext = FixedThreadPoolContext.create("fix-test",1,2); + + final static Executor cachedExecutor = cachedThreadPool.getExecutor(cachedThreadPoolContext); + + final static Executor fixedExecutor = fixedThreadPool.getExecutor(fixedThreadPoolContext); + + final static Executor cachedExecutor2 = cachedThreadPool.getExecutor(cachedThreadPoolContext2); + + Worker worker1 = new Worker(); + Worker worker2 = new Worker(); + Worker worker3 = new Worker(); + Worker worker4 = new Worker(); + Worker worker5 = new Worker(); + + Monitor monitor = new Monitor(fixedExecutor,cachedExecutor); + + + @Before + public void init(){ + monitor.start(); + } + /** + * 容量为2,因此最多可以接收三个任务。第四个任务开始抛异常 + */ + @Test + public void testFixed(){ + fixedExecutor.execute(worker1); + fixedExecutor.execute(worker2); + fixedExecutor.execute(worker3); + fixedExecutor.execute(worker4); + fixedExecutor.execute(worker5); + } + /** + * 最小线程1,最大线程2,没有缓冲队列。所以,第三个任务提交的时候,就已经抛错了。 + */ @Test - public void test(){ - + public void testCached(){ + cachedExecutor.execute(worker1); + cachedExecutor.execute(worker2); + cachedExecutor.execute(worker3); + cachedExecutor.execute(worker4); + cachedExecutor.execute(worker5); + } + + /** + * 最小线程1,最大线程2,缓冲队列长度为1。所以,第四个任务提交的时候,就已经抛错了。 + */ + @Test + public void testCached2(){ + cachedExecutor2.execute(worker1); + cachedExecutor2.execute(worker2); + cachedExecutor2.execute(worker3); + cachedExecutor2.execute(worker4); + cachedExecutor2.execute(worker5); } + +} + +class Worker implements Runnable{ + + private static final Logger log = LoggerFactory.getLogger(Worker.class); + + @Override + public void run() { + log.warn("start run Worker:"+Thread.currentThread().getName()); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + log.warn("end run Worker:"+Thread.currentThread().getName()); + } +} + +class Monitor extends Thread{ + + private static final Logger log = LoggerFactory.getLogger(Monitor.class); + + private ThreadPoolExecutor fixedExecutor; + private ThreadPoolExecutor cachedExecutor; + + public Monitor(Executor fixedExecutor,Executor cachedExecutor) { + this.fixedExecutor = (ThreadPoolExecutor)fixedExecutor; + this.cachedExecutor = (ThreadPoolExecutor)cachedExecutor; + } + + @Override + public void run() { + + while(true){ + StringBuilder sb = new StringBuilder(); + sb.append("[fixedExecutor]"+"coreThreadNums:" + fixedExecutor.getCorePoolSize() + " maxThreadNums:" + fixedExecutor.getMaximumPoolSize() + " activityThreadNums:" + + fixedExecutor.getActiveCount()); + log.error(sb.toString()); + + StringBuilder sb2 = new StringBuilder(); + sb2.append("[cachedExecutor]"+"coreThreadNums:" + cachedExecutor.getCorePoolSize() + " maxThreadNums:" + cachedExecutor.getMaximumPoolSize() + " activityThreadNums:" + + cachedExecutor.getActiveCount()); + log.error(sb2.toString()); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + + } + + } + } diff --git a/pom.xml b/pom.xml index 7ea194d0..75f62b4f 100644 --- a/pom.xml +++ b/pom.xml @@ -37,9 +37,7 @@ 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0.1-SNAPSHOT - 1.0.0-SNAPSHOT - 1.0.0-SNAPSHOT - 1.0.0-SNAPSHOT + 1.0-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 @@ -129,6 +127,11 @@ mpush-monitor ${mpush-monitor-version} + + com.shinemo.mpush + mpush-log + ${mpush-log-version} + From 3afbd16d1a97914d48a7bde1a7b77fae4dbfc69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 15:36:05 +0800 Subject: [PATCH 218/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/tools/Constants.java | 13 +++++++++---- .../shinemo/mpush/tools/thread/ThreadNameSpace.java | 2 ++ .../tools/thread/threadpool/ThreadPoolContext.java | 6 ++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index ff85cda6..69f2910f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -15,15 +15,20 @@ public interface Constants { int MIN_POOL_SIZE = 200; int MAX_POOL_SIZE = 500; - int MIN_BOSS_POOL_SIZE = 10; - int MAX_BOSS_POLL_SIZE = 50; + int MIN_BOSS_POOL_SIZE = 50; + int MAX_BOSS_POOL_SIZE = 100; + int BOSS_THREAD_QUEUE_SIZE = 10000; - int MIN_WORK_POOL_SIZE = 10; - int MAX_WORK_POOL_SIZE = 250; + int MIN_WORK_POOL_SIZE = 50; + int MAX_WORK_POOL_SIZE = 150; + int WORK_THREAD_QUEUE_SIZE = 10000; int BIZ_POOL_SIZE = 20; int EVENT_BUS_POOL_SIZE = 10; + + int REDIS_POOL_SIZE = 3; + int REDIS_THREAD_QUEUE_SIZE = 10000; //zk int ZK_MAX_RETRY = 3; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index effd185a..d5b6ee5c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -14,6 +14,8 @@ public class ThreadNameSpace { public static final String EVENT_BUS = "mp-event-bus"; + public static final String REDIS = "mp-redis"; + public static final String BIZ = "mp-biz"; /** diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index 0364731b..ae1f5835 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -14,14 +14,16 @@ public class ThreadPoolContext { private final int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) private final int keepAliveSeconds;// 存活时间 - public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE, 60*5); + public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POOL_SIZE, 60*5,Constants.BOSS_THREAD_QUEUE_SIZE); - public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5); + public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5,Constants.WORK_THREAD_QUEUE_SIZE); public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); + public static ThreadPoolContext REDIS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.REDIS, Constants.REDIS_POOL_SIZE,Constants.REDIS_THREAD_QUEUE_SIZE); + public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { this.name = name; this.corePoolSize = corePoolSize; From 0a8bdd466d6135b4347f9a4735fc212bf75a9e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 15:49:58 +0800 Subject: [PATCH 219/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20meta-inf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com.shinemo.mpush.tools.thread.threadpool.ThreadPool | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool index 0badf96e..900e3f9c 100644 --- a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool +++ b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool @@ -1,2 +1,2 @@ cachedThreadPool=com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPool -fixedThreadPool=com.shinemo.mpush.tools.thread.threadpool.cached.FixedThreadPool \ No newline at end of file +fixedThreadPool=com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPool \ No newline at end of file From 514949186319d1fe72984ce215c1034fc6cf01e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 16:26:34 +0800 Subject: [PATCH 220/890] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E6=8A=A5=E5=BC=82=E5=B8=B8=EF=BC=8C=E9=9C=80=E8=A6=81=E6=8E=A5?= =?UTF-8?q?=E4=B8=8B=E6=9D=A5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/message/HandshakeMessage.java | 1 - .../com/shinemo/mpush/cs/client/Main.java | 2 +- .../mpush/tools/crypto/RSAUtilsTest.java | 26 ++++++++++++++++--- .../shinemo/mpush/tools/thread/SyncTest.java | 3 +++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java index 0840c340..4d4e2191 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 2fbeee4a..2751441f 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -21,7 +21,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<500;i++){ + for(int i = 0;i<1;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java index 552df98a..2d0cf94b 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.tools.crypto; import com.shinemo.mpush.tools.Pair; + import org.junit.Before; import org.junit.Test; @@ -8,14 +9,15 @@ import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; -import static org.junit.Assert.*; - /** * Created by ohun on 2015/12/25. */ public class RSAUtilsTest { String publicKey; String privateKey; + + String publicKey2; + String privateKey2; @Before public void setUp() throws Exception { @@ -25,6 +27,10 @@ public void setUp() throws Exception { privateKey = RSAUtils.encodeBase64(pair.value); System.out.println("公钥: \n\r" + publicKey); System.out.println("私钥: \n\r" + privateKey); + + pair = RSAUtils.genKeyPair(); + publicKey2 = RSAUtils.encodeBase64(pair.key); + privateKey2 = RSAUtils.encodeBase64(pair.value); } catch (Exception e) { e.printStackTrace(); } @@ -32,12 +38,24 @@ public void setUp() throws Exception { @Test public void testGetKeys() throws Exception { - + String source = "这是一行测试RSA数字签名的无意义文字"; + byte[] data = source.getBytes(); + byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey); + System.out.println("加密后:\n" + new String(encodedData)); + byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey); + String target = new String(decodedData); + System.out.println("解密后:\n" + target); + + decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey2); + target = new String(decodedData); + System.out.println("解密后2:\n" + target); + } @Test public void testGetPrivateKey() throws Exception { - + System.out.println("公钥: \n\r" + publicKey); + System.out.println("私钥: \n\r" + privateKey); } @Test diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java index 197bede5..43f411b1 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java @@ -18,6 +18,9 @@ public class SyncTest { + //pri MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIvbj8CvXqWcXAeNw7yruM2TZPfIkp2JIbPwpptAMt6t6zoByRLDjPkbkFVbJrmmto5dGLFCQHz9NM24gb5R9U5dotApgCYFabbocIUx+mkUcd+ui+SZ5yTTyXtVhqKBFGqCTEB73S7b5y0xof/r781EZWYA3sh47pNXVYisRh7rAgMBAAECgYARri8NH95qN0sXFV/iUR8qtfB0tqF6UuS0175oMAR+TCRJkAI4YgpHT6m+cKiDncTEWJaPih2W73embiXQxpGpJt0HKegmKF5RiSU8iXjbFQvmlfTRrgo7qLIjgqUxaM0h+ef0p/T3EV+HZ8sk2bHZPd5OzTcAx1UOSgz88VEDEQJBAONTXI88w+cIkeS5uDDCDjV5S5ljQCBmBTlwIp0UCLDZ0KQDFCiOM54ltgcsMrKQFyj2EwTWsevbikTP3KRmXzMCQQCdf78HkzGnGAJUzPchIHvQBC1Q95X002rYPxNrlF86iU7n1fan++xuGDTYnz+eNRKJFEY85SQq0eld0KI57qFpAkAZ9Lu90ydfKthVsGr6jj3HF0ltgyqgSGXSUB5zpwTzBHvRLlTP6KS2KwIkwYQsZU1vrOExDT6VeqTIBJ/h2ZqHAkAW9dKRdiHc7CEa365/Q88I6jL5BL71rAR9deSM4FppnC7GmWiV4KH9AsZhdgW+OJp1JWF/6x+0pllQ9eNQcrtRAkBczJJ5l3BtCE+VToy16DPPQCfyDpb6dmyR4IkTwECWMomz9jnt1fK7nETfBeeP4ySea8jrUCgZdu06iPtoLCAK + //pub MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL24/Ar16lnFwHjcO8q7jNk2T3yJKdiSGz8KabQDLeres6AckSw4z5G5BVWya5praOXRixQkB8/TTNuIG+UfVOXaLQKYAmBWm26HCFMfppFHHfrovkmeck08l7VYaigRRqgkxAe90u2+ctMaH/6+/NRGVmAN7IeO6TV1WIrEYe6wIDAQAB + final static ThreadPool cachedThreadPool = new CachedThreadPool(); final static ThreadPool fixedThreadPool = new FixedThreadPool(); From 8a9230c141cc8d15ed70b26d56868e4a890d9abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 19:50:57 +0800 Subject: [PATCH 221/890] add exception --- .../shinemo/mpush/api/exception/MessageException.java | 11 +++++++++++ .../mpush/common/message/HandshakeMessage.java | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java new file mode 100644 index 00000000..5e92c987 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java @@ -0,0 +1,11 @@ +package com.shinemo.mpush.api.exception; + +public class MessageException extends RuntimeException { + + private static final long serialVersionUID = 8731698346169093329L; + + public MessageException(String message) { + super(message); + } + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java index 4d4e2191..2570b45f 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java @@ -1,9 +1,10 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.exception.MessageException; import com.shinemo.mpush.api.protocol.Packet; -import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBuf; import static com.shinemo.mpush.api.protocol.Command.HANDSHAKE; /** @@ -30,6 +31,9 @@ public HandshakeMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { + if(body.readableBytes()<=0){ + throw new MessageException("body is null"); + } deviceId = decodeString(body); osName = decodeString(body); osVersion = decodeString(body); From 86583f00eb265bd0314e8c41f2722aa49a5f376c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 27 Jan 2016 20:10:58 +0800 Subject: [PATCH 222/890] =?UTF-8?q?=E7=9B=AE=E5=89=8D1000=E6=B5=8B?= =?UTF-8?q?=E8=AF=95ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/common/message/ErrorMessage.java | 1 - .../com/shinemo/mpush/core/client/ClientChannelHandler.java | 1 + mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java | 2 +- .../java/com/shinemo/mpush/netty/client/NettyClientFactory.java | 1 - 4 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index c0ac0e75..eea54b62 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.ErrorCode; import io.netty.buffer.ByteBuf; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index d346a6bf..6748b747 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -122,6 +122,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { if(client instanceof SecurityNettyClient){ connection.init(ctx.channel(), true); client.initConnection(connection); + client.init(ctx.channel()); tryFastConnect((SecurityNettyClient)client); }else{ LOGGER.error("connection is not support appear hear:"+ client); diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 2751441f..17c68377 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -21,7 +21,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<1;i++){ + for(int i = 0;i<1000;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index b4e56f02..a18c26f6 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -104,7 +104,6 @@ public void initChannel(SocketChannel ch) throws Exception { ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { Channel channel = future.channel(); - client.init(channel); log.error("init channel:"+channel); return client; } else { From 4db00d62c1971fb961909fe64c735d4ed337da32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 29 Jan 2016 10:40:34 +0800 Subject: [PATCH 223/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/cs/ConnectionServerMain.java | 4 +- .../com/shinemo/mpush/cs/client/Main.java | 3 +- .../mpush/monitor/domain/MonitorData.java | 10 ++++ .../monitor/quota/impl/JVMThreadPool.java | 5 ++ .../monitor/service/MonitorDataCollector.java | 2 + .../netty/client/NettyClientFactory.java | 1 - .../shinemo/mpush/ps/GatewayClientManage.java | 57 ------------------- .../ps}/manage/impl/GatewayServerManage.java | 36 +++++++++++- .../impl/GatewayServerPathListener.java | 5 +- ...m.shinemo.mpush.common.manage.ServerManage | 2 +- 10 files changed, 58 insertions(+), 67 deletions(-) delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java rename {mpush-common/src/main/java/com/shinemo/mpush/common => mpush-ps/src/main/java/com/shinemo/mpush/ps}/manage/impl/GatewayServerManage.java (50%) rename {mpush-common/src/main/java/com/shinemo/mpush/common => mpush-ps/src/main/java/com/shinemo/mpush/ps}/zk/listener/impl/GatewayServerPathListener.java (91%) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 3226b1aa..9c3a6e82 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -21,8 +21,8 @@ public class ConnectionServerMain extends AbstractServer serverList = main.getApplicationList(); - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<1000;i++){ + for(int i = 0;i<10;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java index a1157050..51af56c9 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java @@ -12,6 +12,8 @@ public class MonitorData { private Map threadMap; + private Map threadPoolMap; + private Map infoMap; public MonitorData() { @@ -54,4 +56,12 @@ public void setInfoMap(Map infoMap) { this.infoMap = infoMap; } + public Map getThreadPoolMap() { + return threadPoolMap; + } + + public void setThreadPoolMap(Map threadPoolMap) { + this.threadPoolMap = threadPoolMap; + } + } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java index 77a52e87..26aa7c0c 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java @@ -12,6 +12,11 @@ public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota{ + public static final JVMThreadPool instance = new JVMThreadPool(); + + private JVMThreadPool() { + } + private Map pool = null; @Override diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index 74c93567..be617dd9 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -8,6 +8,7 @@ import com.shinemo.mpush.monitor.quota.impl.JVMInfo; import com.shinemo.mpush.monitor.quota.impl.JVMMemory; import com.shinemo.mpush.monitor.quota.impl.JVMThread; +import com.shinemo.mpush.monitor.quota.impl.JVMThreadPool; import com.shinemo.mpush.tools.JVMUtil; import com.shinemo.mpush.tools.Jsons; @@ -39,6 +40,7 @@ public static MonitorData collect(){ data.setGcMap(JVMGC.instance.toMap()); data.setMemoryMap(JVMMemory.instance.toMap()); data.setThreadMap(JVMThread.instance.toMap()); + data.setThreadPoolMap(JVMThreadPool.instance.toMap()); return data; } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index a18c26f6..5272b522 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -60,7 +60,6 @@ public void initChannel(SocketChannel ch) throws Exception { } } - @Deprecated public Client create(String host,int port,final ChannelHandler handler){ Client client = new NettyClient(host, port); return init(client, handler); diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java deleted file mode 100644 index 9d50c4e6..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.shinemo.mpush.ps; - -import java.util.Map; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; -import com.shinemo.mpush.common.manage.impl.GatewayServerManage; -import com.shinemo.mpush.netty.client.NettyClientFactory; - -public class GatewayClientManage extends GatewayServerManage{ - - private final Map application2Client = Maps.newConcurrentMap(); - - private final Map ip2Client = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, GatewayServerApplication application) { - super.addOrUpdate(fullPath, application); - try{ - Client client = NettyClientFactory.INSTANCE.create(application.getIp(), application.getPort(), new GatewayClientChannelHandler()); - application2Client.put(application, client); - ip2Client.put(application.getIp()+":"+application.getPort(), client); - }catch(Exception e){ - - } - - } - - - @Override - public void remove(String fullPath) { - GatewayServerApplication application = super.get(fullPath); - super.remove(fullPath); - - if(application!=null){ - Client client = application2Client.get(application); - if(client!=null){ - client.stop(); - } - } - ip2Client.remove(application.getIp()+":"+application.getPort()); - } - - public Client getClient(GatewayServerApplication application){ - return application2Client.get(application); - } - - public Connection getConnection(String ipAndPort) { - Client client = ip2Client.get(ipAndPort); - if (client == null) return null; - return client.getConnection(); - } - - -} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java similarity index 50% rename from mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java index e608e3ac..409fbd83 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/impl/GatewayServerManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common.manage.impl; +package com.shinemo.mpush.ps.manage.impl; import java.util.Collection; import java.util.Collections; @@ -10,8 +10,12 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Maps; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.ps.GatewayClientChannelHandler; public class GatewayServerManage implements ServerManage{ @@ -19,14 +23,33 @@ public class GatewayServerManage implements ServerManage holder = Maps.newConcurrentMap(); + private final Map application2Client = Maps.newConcurrentMap(); + + private final Map ip2Client = Maps.newConcurrentMap(); + @Override public void addOrUpdate(String fullPath,GatewayServerApplication application){ holder.put(fullPath, application); + try{ + Client client = NettyClientFactory.INSTANCE.create(application.getIp(), application.getPort(), new GatewayClientChannelHandler()); + application2Client.put(application, client); + ip2Client.put(application.getIp()+":"+application.getPort(), client); + }catch(Exception e){ + + } printList(); } @Override public void remove(String fullPath){ + GatewayServerApplication application = get(fullPath); + if(application!=null){ + Client client = application2Client.get(application); + if(client!=null){ + client.stop(); + } + } + ip2Client.remove(application.getIp()+":"+application.getPort()); holder.remove(fullPath); printList(); } @@ -45,5 +68,16 @@ private void printList(){ public GatewayServerApplication get(String fullpath){ return holder.get(fullpath); } + + public Client getClient(GatewayServerApplication application){ + return application2Client.get(application); + } + + public Connection getConnection(String ipAndPort) { + Client client = ip2Client.get(ipAndPort); + if (client == null) return null; + return client.getConnection(); + } } + diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java similarity index 91% rename from mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java index 7dcc8196..16b7fad4 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/impl/GatewayServerPathListener.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common.zk.listener.impl; +package com.shinemo.mpush.ps.zk.listener.impl; import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; @@ -7,7 +7,7 @@ import com.shinemo.mpush.tools.zk.ZKPath; /** - * connection server 应用 监控 + * gateway server 应用 监控 * */ public class GatewayServerPathListener extends AbstractDataChangeListener{ @@ -15,7 +15,6 @@ public class GatewayServerPathListener extends AbstractDataChangeListener gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); - @Override public String listenerPath() { return ZKPath.GATEWAY_SERVER.getWatchPath(); diff --git a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage index 09794b03..85817c00 100644 --- a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -1 +1 @@ -gatewayClientManage=com.shinemo.mpush.ps.GatewayClientManage \ No newline at end of file +gatewayServerManage=com.shinemo.mpush.ps.manage.impl.GatewayServerManage \ No newline at end of file From f8c577c43045b0fa3e4c27cd1eacf083b14c0463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 29 Jan 2016 10:44:54 +0800 Subject: [PATCH 224/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9ps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/cs/ConnectionServerMain.java | 2 -- .../src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java | 2 +- mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java | 3 ++- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 9c3a6e82..3e29e1bd 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -5,10 +5,8 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.common.AbstractServer; import com.shinemo.mpush.common.app.impl.GatewayServerApplication; -import com.shinemo.mpush.common.zk.listener.impl.GatewayServerPathListener; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; -import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; import com.shinemo.mpush.tools.Jsons; public class ConnectionServerMain extends AbstractServer{ diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java index e064a543..eae8f82c 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java @@ -4,7 +4,7 @@ import com.shinemo.mpush.api.PushSender.Callback; import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.common.zk.listener.impl.GatewayServerPathListener; +import com.shinemo.mpush.ps.zk.listener.impl.GatewayServerPathListener; public class GatewayClientMain extends AbstractClient { diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java index 0e61e38f..5ac0139a 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java @@ -7,6 +7,7 @@ import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; import com.shinemo.mpush.common.router.ConnectionRouterManager; import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.ps.manage.impl.GatewayServerManage; import com.shinemo.mpush.tools.spi.ServiceContainer; import io.netty.channel.ChannelFuture; @@ -20,7 +21,7 @@ */ public class PushRequest implements PushSender.Callback, Runnable { - private static GatewayClientManage gatewayClientManage = (GatewayClientManage)ServiceContainer.getInstance(ServerManage.class, "gatewayClientManage"); + private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayClientManage"); private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); private PushSender.Callback callback; From 8052fb1e2c7b0faa459c319af2a9dd5547f12418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 29 Jan 2016 11:18:14 +0800 Subject: [PATCH 225/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9ps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/client/ClientChannelHandler.java | 17 ++++- mpush-ps/pom.xml | 4 ++ .../mpush/ps/GatewayClientChannelHandler.java | 63 ------------------- .../mpush/ps/{PushTest.java => Main.java} | 2 +- .../com/shinemo/mpush/ps/PushRequest.java | 2 +- .../ps/manage/impl/GatewayServerManage.java | 9 ++- 6 files changed, 26 insertions(+), 71 deletions(-) delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java rename mpush-ps/src/main/java/com/shinemo/mpush/ps/{PushTest.java => Main.java} (98%) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 6748b747..95e82f8e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -109,7 +109,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - NettyClientFactory.INSTANCE.remove(ctx.channel()); + Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); + if(client instanceof SecurityNettyClient){ + NettyClientFactory.INSTANCE.remove(ctx.channel()); + }else{ + client.close("exception"); + } + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @@ -117,9 +123,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); - NettyClientFactory.INSTANCE.put(ctx.channel(), client); if(client instanceof SecurityNettyClient){ + NettyClientFactory.INSTANCE.put(ctx.channel(), client); connection.init(ctx.channel(), true); client.initConnection(connection); client.init(ctx.channel()); @@ -133,8 +139,13 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { + Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); + if(client instanceof SecurityNettyClient){ + NettyClientFactory.INSTANCE.remove(ctx.channel()); + }else{ + client.close("inactive"); + } LOGGER.info("client disconnect channel={}", ctx.channel()); - NettyClientFactory.INSTANCE.remove(ctx.channel());; } private void tryFastConnect(SecurityNettyClient securityNettyClient) { diff --git a/mpush-ps/pom.xml b/mpush-ps/pom.xml index 07dc19ea..ecde0d19 100644 --- a/mpush-ps/pom.xml +++ b/mpush-ps/pom.xml @@ -29,5 +29,9 @@ com.shinemo.mpush mpush-tools + + com.shinemo.mpush + mpush-core + diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java deleted file mode 100644 index ac173d79..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientChannelHandler.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.shinemo.mpush.ps; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.netty.connection.NettyConnection; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.shinemo.mpush.common.ErrorCode.OFFLINE; -import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; - -/** - * Created by ohun on 2015/12/30. - */ -public class GatewayClientChannelHandler extends ChannelHandlerAdapter { - private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); - private final Connection connection = new NettyConnection(); - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - connection.init(ctx.channel(), false); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - connection.close(); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof Packet) { - Packet packet = ((Packet) msg); - PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); - return; - } - - if (packet.cmd == Command.OK.cmd) { - request.success(); - } else { - ErrorMessage message = new ErrorMessage(packet, connection); - if (message.code == OFFLINE.errorCode) { - request.offline(); - } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { - request.failure(); - } else if (message.code == ROUTER_CHANGE.errorCode) { - request.redirect(); - } - LOGGER.warn("receive an error gateway response, message={}", message); - } - } - } - - public Connection getConnection() { - return connection; - } -} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushTest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java similarity index 98% rename from mpush-ps/src/main/java/com/shinemo/mpush/ps/PushTest.java rename to mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java index 53c15b4f..7d56aac1 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushTest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java @@ -8,7 +8,7 @@ /** * Created by ohun on 2016/1/7. */ -public class PushTest { +public class Main { public static void main(String[] args) throws Exception { GatewayClientMain client = new GatewayClientMain(); diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java index 5ac0139a..20db80d0 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java @@ -21,7 +21,7 @@ */ public class PushRequest implements PushSender.Callback, Runnable { - private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayClientManage"); + private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); private PushSender.Callback callback; diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java index 409fbd83..df3f806f 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java @@ -14,8 +14,9 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.core.client.ClientChannelHandler; +import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.ps.GatewayClientChannelHandler; public class GatewayServerManage implements ServerManage{ @@ -31,9 +32,11 @@ public class GatewayServerManage implements ServerManage Date: Fri, 29 Jan 2016 11:30:06 +0800 Subject: [PATCH 226/890] bug fix --- .../core/client/ClientChannelHandler.java | 10 +- .../mpush/ps/client/ClientChannelHandler.java | 94 +++++++++++++++++++ .../ps/manage/impl/GatewayServerManage.java | 2 +- 3 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 95e82f8e..b826081e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -17,6 +17,7 @@ import com.shinemo.mpush.common.security.AesCipher; import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.netty.client.ChannelClientHandler; +import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; import com.shinemo.mpush.netty.connection.NettyConnection; @@ -50,12 +51,9 @@ public Client getClient() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); client.getConnection().updateLastReadTime(); if(client instanceof SecurityNettyClient){ - SecurityNettyClient securityNettyClient = (SecurityNettyClient)client; - Connection connection = client.getConnection(); //加密 if (msg instanceof Packet) { @@ -101,15 +99,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } - }else{ - //不加密 + }else if(client instanceof NettyClient){//不加密 + } LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); if(client instanceof SecurityNettyClient){ NettyClientFactory.INSTANCE.remove(ctx.channel()); }else{ @@ -139,7 +136,6 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - Client client = NettyClientFactory.INSTANCE.getCientByChannel(ctx.channel()); if(client instanceof SecurityNettyClient){ NettyClientFactory.INSTANCE.remove(ctx.channel()); }else{ diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java new file mode 100644 index 00000000..3dd8ebff --- /dev/null +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java @@ -0,0 +1,94 @@ +package com.shinemo.mpush.ps.client; + + + +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.netty.client.ChannelClientHandler; +import com.shinemo.mpush.netty.connection.NettyConnection; +import com.shinemo.mpush.ps.PushRequest; +import com.shinemo.mpush.ps.PushRequestBus; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import static com.shinemo.mpush.common.ErrorCode.OFFLINE; +import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; + +/** + * Created by ohun on 2015/12/19. + */ +@ChannelHandler.Sharable +public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); + + private Client client; + + public ClientChannelHandler(Client client) { + this.client = client; + } + + @Override + public Client getClient() { + return client; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + client.getConnection().updateLastReadTime(); + if (msg instanceof Packet) { + Packet packet = ((Packet) msg); + PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); + if (request == null) { + LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); + return; + } + + if (packet.cmd == Command.OK.cmd) { + request.success(); + } else { + ErrorMessage message = new ErrorMessage(packet, client.getConnection()); + if (message.code == OFFLINE.errorCode) { + request.offline(); + } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { + request.failure(); + } else if (message.code == ROUTER_CHANGE.errorCode) { + request.redirect(); + } + LOGGER.warn("receive an error gateway response, message={}", message); + } + } + LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + client.close("exception"); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + Connection connection = new NettyConnection(); + connection.init(ctx.channel(), false); + client.initConnection(connection); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + client.close("inactive"); + LOGGER.info("client disconnect channel={}", ctx.channel()); + } + + +} \ No newline at end of file diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java index df3f806f..e796e964 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java @@ -14,9 +14,9 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.core.client.ClientChannelHandler; import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.ps.client.ClientChannelHandler; public class GatewayServerManage implements ServerManage{ From 79e6f49fec8c3eb3182c51a687c8e0a59fef48d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 05:32:01 +0000 Subject: [PATCH 227/890] del key --- mpush-ps/src/main/resources/config.properties | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/mpush-ps/src/main/resources/config.properties b/mpush-ps/src/main/resources/config.properties index 67e2ffaf..3a6f0466 100644 --- a/mpush-ps/src/main/resources/config.properties +++ b/mpush-ps/src/main/resources/config.properties @@ -22,16 +22,6 @@ aes_key_length = 16 ras_key_length = 1024 ## session_expired_time = 86400 -## -max_hb_timeout_times = 2 -## -max_heartbeat = 1800000 -## -min_heartbeat = 10000 -## -compress_limit = 10240 -## -max_packet_size = 10240 ## zk 配置项 zk_ip = 10.1.10.41:2181 From a625ef078db5662853a2f117d102786851fe3bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 07:16:00 +0000 Subject: [PATCH 228/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 7 ++ config.properties | 41 ++++++++-- .../src/test/resources/config.properties | 8 -- mpush-common/pom.xml | 10 --- mpush-core/pom.xml | 82 ------------------- .../src/main/resources/config.properties | 43 ---------- mpush-cs/pom.xml | 41 ++++------ mpush-ps/src/main/resources/config.properties | 33 -------- mpush-tools/pom.xml | 23 ------ .../mpush/tools/config/ConfigCenter.java | 7 +- 10 files changed, 62 insertions(+), 233 deletions(-) delete mode 100644 mpush-client/src/test/resources/config.properties delete mode 100644 mpush-core/src/main/resources/config.properties delete mode 100644 mpush-ps/src/main/resources/config.properties diff --git a/assembly.xml b/assembly.xml index 2f526c20..ff0e73f3 100644 --- a/assembly.xml +++ b/assembly.xml @@ -10,6 +10,13 @@ tar.gz + + / + + + config.properties + + mpush-cs/target/ diff --git a/config.properties b/config.properties index ef2b80aa..04d27b9e 100644 --- a/config.properties +++ b/config.properties @@ -1,8 +1,33 @@ -ZK_SERVER=10.1.20.74:2181 -MAX_PACKET_SIZE=10240 -COMPRESS_LIMIT=10240 -MIN_HEARTBEAT=10000 -MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 -PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file +## +max_packet_size = 10240 +## +compress_limit = 10240 +## +min_heartbeat = 10000 +## +max_heartbeat = 1800000 +## +max_hb_timeout_times = 2 +## +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +## +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +## +gateway_server_port = 4000 +## +connection_server_port = 3000 +## +aes_key_length = 16 +## +ras_key_length = 1024 +## +session_expired_time = 86400 + +## zk +zk_ip = 10.1.80.57:2181 +zk_namespace = mpush +zk_digest = shinemoIpo + +## redis redisԷֺŷָÿڵredisԶŷָ +##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo +redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/mpush-client/src/test/resources/config.properties b/mpush-client/src/test/resources/config.properties deleted file mode 100644 index ef2b80aa..00000000 --- a/mpush-client/src/test/resources/config.properties +++ /dev/null @@ -1,8 +0,0 @@ -ZK_SERVER=10.1.20.74:2181 -MAX_PACKET_SIZE=10240 -COMPRESS_LIMIT=10240 -MIN_HEARTBEAT=10000 -MAX_HEARTBEAT=1800000 -MAX_HB_TIMEOUT_TIMES=2 -PRIVATE_KEY=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -PUBLIC_KEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index c78983b4..a8ddc52a 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -24,16 +24,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - UTF-8 - - org.apache.maven.plugins maven-resources-plugin diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index dca91ff8..ac6798f1 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -38,86 +38,4 @@ logback-ext-spring - - - - mpush - - - - src/main/resources - true - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - UTF-8 - - - - org.apache.maven.plugins - maven-resources-plugin - 2.6 - - - - maven-dependency-plugin - - - copy - package - - copy-dependencies - - - - ${project.build.directory}/lib - - - - - - - maven-jar-plugin - - - - - false - - - - - true - - lib/ - - com.shinemo.mpush.core.App - - - - - - package - - - - - - diff --git a/mpush-core/src/main/resources/config.properties b/mpush-core/src/main/resources/config.properties deleted file mode 100644 index 47481f19..00000000 --- a/mpush-core/src/main/resources/config.properties +++ /dev/null @@ -1,43 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 1800000 -## -max_hb_timeout_times = 2 -## -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port = 4000 -## -connection_server_port = 3000 -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 -## -max_hb_timeout_times = 2 -## -max_heartbeat = 1800000 -## -min_heartbeat = 10000 -## -compress_limit = 10240 -## -max_packet_size = 10240 - -## zk 配置项 -zk_ip = 127.0.0.1:2181 -zk_namespace = mpush -zk_digest = shinemoIpo - -## redis 配置项 redis组以分号分割,每组内的redis以逗号分割 -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 126f21dc..57b6f32f 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -39,29 +39,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - UTF-8 - - - - org.apache.maven.plugins - maven-resources-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - - true - - org.apache.maven.plugins maven-shade-plugin @@ -84,7 +61,23 @@ - + + maven-dependency-plugin + + + copy + package + + copy-dependencies + + + + ${project.build.directory}/lib + + + + + maven-dependency-plugin diff --git a/mpush-ps/src/main/resources/config.properties b/mpush-ps/src/main/resources/config.properties deleted file mode 100644 index 3a6f0466..00000000 --- a/mpush-ps/src/main/resources/config.properties +++ /dev/null @@ -1,33 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 1800000 -## -max_hb_timeout_times = 2 -## -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port = 4000 -## -connection_server_port = 3000 -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 - -## zk 配置项 -zk_ip = 10.1.10.41:2181 -zk_namespace = mpush -zk_digest = shinemoIpo - -## redis 配置项 redis组以分号分割,每组内的redis以逗号分割 -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 559c7e72..29c13338 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -55,29 +55,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - UTF-8 - - - - org.apache.maven.plugins - maven-resources-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - - true - - org.apache.maven.plugins maven-shade-plugin diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 29c56fb1..aedac7e0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -11,7 +11,10 @@ /** * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 */ -@Sources({"classpath:config.properties"}) +@Sources({ + "classpath:config.properties", + "file:/${user.dir}/config.properties" +}) public interface ConfigCenter extends Config { ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); @@ -88,7 +91,7 @@ public interface ConfigCenter extends Config { @Key("scan_conn_task_cycle") @DefaultValue("59000") long scanConnTaskCycle(); - + @DefaultValue("/opt/shinemo/mpush/") String logPath(); } From a68abbbb59b235bc9104d2ff71b43b06cbf30446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 07:16:25 +0000 Subject: [PATCH 229/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-common/pom.xml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index a8ddc52a..5427cdb3 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -24,19 +24,6 @@ - - org.apache.maven.plugins - maven-resources-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - - true - - org.apache.maven.plugins maven-shade-plugin From b649b29cef344b024d8133ba8708d86a96f6b9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 07:23:53 +0000 Subject: [PATCH 230/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assembly.xml b/assembly.xml index ff0e73f3..58d8cdd5 100644 --- a/assembly.xml +++ b/assembly.xml @@ -11,7 +11,7 @@ - / + ./ config.properties From 1e3122bba23391b23efc8c7a3c0c1193e7ccb241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 07:36:24 +0000 Subject: [PATCH 231/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/common/message/HandshakeMessage.java | 6 +----- .../java/com/shinemo/mpush/tools/crypto/AESUtils.java | 4 ++-- .../java/com/shinemo/mpush/tools/crypto/RSAUtils.java | 8 ++++---- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java index 2570b45f..4d4e2191 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java @@ -1,10 +1,9 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.exception.MessageException; import com.shinemo.mpush.api.protocol.Packet; - import io.netty.buffer.ByteBuf; + import static com.shinemo.mpush.api.protocol.Command.HANDSHAKE; /** @@ -31,9 +30,6 @@ public HandshakeMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { - if(body.readableBytes()<=0){ - throw new MessageException("body is null"); - } deviceId = decodeString(body); osName = decodeString(body); osVersion = decodeString(body); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java index fb53d2b0..ac6914dd 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java @@ -26,8 +26,8 @@ public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { return cipher.doFinal(data); } catch (Exception e) { LOGGER.error("encrypt ex, decryptKey=" + encryptKey, e); + throw new RuntimeException("AES encrypt ex", e); } - return Constants.EMPTY_BYTES; } public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { @@ -39,7 +39,7 @@ public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { return cipher.doFinal(data); } catch (Exception e) { LOGGER.error("decrypt ex, decryptKey=" + decryptKey, e); + throw new RuntimeException("AES decrypt ex", e); } - return Constants.EMPTY_BYTES; } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index 18b320cd..8b1805fc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -169,8 +169,8 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { LOGGER.error("getPublicKey ex modulus={}, exponent={}", modulus, exponent, e); + throw new RuntimeException("Get PublicKey ex", e); } - return null; } /** @@ -192,7 +192,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { LOGGER.error("getPrivateKey ex modulus={}, exponent={}", modulus, exponent, e); - return null; + throw new RuntimeException("Get PrivateKey ex", e); } } @@ -215,8 +215,8 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { return doFinal(cipher, data, key_len - 11); } catch (Exception e) { LOGGER.error("encryptByPublicKey ex", e); + throw new RuntimeException("RSA encrypt ex", e); } - return Constants.EMPTY_BYTES; } /** @@ -237,8 +237,8 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) return doFinal(cipher, data, key_len); } catch (Exception e) { LOGGER.error("decryptByPrivateKey ex", e); + throw new RuntimeException("RSA encrypt ex", e); } - return Constants.EMPTY_BYTES; } /** From e39d3e94543d7c87543c3ad13f3ec2dd5ee8c549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 07:47:31 +0000 Subject: [PATCH 232/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml => mpush-cs/assembly.xml | 4 +- .../config.properties | 0 mpush-cs/pom.xml | 58 ++++++++++++------- pom.xml | 19 +----- 4 files changed, 41 insertions(+), 40 deletions(-) rename assembly.xml => mpush-cs/assembly.xml (92%) rename config.properties => mpush-cs/config.properties (100%) diff --git a/assembly.xml b/mpush-cs/assembly.xml similarity index 92% rename from assembly.xml rename to mpush-cs/assembly.xml index 58d8cdd5..cf361b56 100644 --- a/assembly.xml +++ b/mpush-cs/assembly.xml @@ -18,14 +18,14 @@ - mpush-cs/target/ + ./target/ lib/*.jar - mpush-cs/target/ + ./target/ mpush-cs.jar diff --git a/config.properties b/mpush-cs/config.properties similarity index 100% rename from config.properties rename to mpush-cs/config.properties diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 57b6f32f..eedbc5d4 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -40,26 +40,44 @@ - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-cs - - - - - + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-cs + + + + + + + + maven-assembly-plugin + 2.6 + + mpush + + assembly.xml + + + + + package + + single + + + maven-dependency-plugin diff --git a/pom.xml b/pom.xml index 75f62b4f..823ad7e1 100644 --- a/pom.xml +++ b/pom.xml @@ -234,24 +234,7 @@ true - - maven-assembly-plugin - 2.6 - - mpush - - assembly.xml - - - - - package - - single - - - - + From 4c80f0fc05f1c74eb3daec3ed5752f8737c8e866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 08:13:10 +0000 Subject: [PATCH 233/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-cs/assembly.xml => assembly.xml | 4 +- .../config.properties => config.properties | 0 mpush-api/pom.xml | 2 +- mpush-client/pom.xml | 2 +- mpush-common/pom.xml | 1 + mpush-core/pom.xml | 2 +- mpush-cs/pom.xml | 24 +-------- mpush-log/pom.xml | 2 +- mpush-monitor/pom.xml | 6 +-- mpush-netty/pom.xml | 2 +- mpush-ps/pom.xml | 37 ++++++------- mpush-tools/pom.xml | 52 +++++++++---------- pom.xml | 33 +++++++++--- 13 files changed, 78 insertions(+), 89 deletions(-) rename mpush-cs/assembly.xml => assembly.xml (92%) rename mpush-cs/config.properties => config.properties (100%) diff --git a/mpush-cs/assembly.xml b/assembly.xml similarity index 92% rename from mpush-cs/assembly.xml rename to assembly.xml index cf361b56..58d8cdd5 100644 --- a/mpush-cs/assembly.xml +++ b/assembly.xml @@ -18,14 +18,14 @@ - ./target/ + mpush-cs/target/ lib/*.jar - ./target/ + mpush-cs/target/ mpush-cs.jar diff --git a/mpush-cs/config.properties b/config.properties similarity index 100% rename from mpush-cs/config.properties rename to config.properties diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index f57560de..0ed22fd5 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -10,7 +10,7 @@ 4.0.0 mpush-api - 1.0-SNAPSHOT + ${mpush-api-version} jar diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 9c5b81cf..d2cce769 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -10,7 +10,7 @@ 4.0.0 mpush-client - 1.0-SNAPSHOT + ${mpush-client-version} jar diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 5427cdb3..2f5c2a33 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -10,6 +10,7 @@ 4.0.0 mpush-common + ${mpush-common-version} com.shinemo.mpush diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index ac6798f1..83eb7a35 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -10,7 +10,7 @@ 4.0.0 mpush-core - 1.0-SNAPSHOT + ${mpush-core-version} jar diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index eedbc5d4..e659adf6 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -6,16 +6,12 @@ 1.0-SNAPSHOT 4.0.0 - + ${mpush-cs-version} mpush-cs jar mpush-cs - - UTF-8 - - com.shinemo.mpush @@ -61,24 +57,6 @@ - - maven-assembly-plugin - 2.6 - - mpush - - assembly.xml - - - - - package - - single - - - - maven-dependency-plugin diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml index 6ae2adfb..50a54473 100644 --- a/mpush-log/pom.xml +++ b/mpush-log/pom.xml @@ -8,7 +8,7 @@ 4.0.0 mpush-log - 1.0-SNAPSHOT + ${mpush-log-version} jar mpush-log diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index f66bee0e..bad2d960 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -9,14 +9,10 @@ mpush-monitor jar - 1.0.1-SNAPSHOT + ${mpush-monitor-version} mpush-monitor - - UTF-8 - - com.google.code.gson diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 977482ff..9530505d 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -10,7 +10,7 @@ 4.0.0 mpush-netty - 1.0-SNAPSHOT + ${mpush-netty-version} jar diff --git a/mpush-ps/pom.xml b/mpush-ps/pom.xml index ecde0d19..e74ec6d7 100644 --- a/mpush-ps/pom.xml +++ b/mpush-ps/pom.xml @@ -9,29 +9,26 @@ mpush-ps jar - + ${mpush-ps-version} mpush-ps - - UTF-8 - - - com.shinemo.mpush - mpush-api - - - com.shinemo.mpush - mpush-netty - - - com.shinemo.mpush - mpush-tools - - - com.shinemo.mpush - mpush-core - + + com.shinemo.mpush + mpush-api + + + com.shinemo.mpush + mpush-netty + + + com.shinemo.mpush + mpush-tools + + + com.shinemo.mpush + mpush-core + diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 29c13338..dae24b03 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -10,7 +10,7 @@ 4.0.0 mpush-tools - 1.0.1-SNAPSHOT + ${mpush-tools-version} jar @@ -26,12 +26,12 @@ commons-lang3 - org.apache.curator - curator-recipes + org.apache.curator + curator-recipes - org.apache.curator - curator-x-discovery + org.apache.curator + curator-x-discovery redis.clients @@ -49,35 +49,33 @@ org.aeonbits.owner owner - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-tools - - - - - + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-tools + + + + + - diff --git a/pom.xml b/pom.xml index 823ad7e1..117fadb7 100644 --- a/pom.xml +++ b/pom.xml @@ -29,15 +29,16 @@ 1.7 - 4.0.0.RELEASE - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT 1.0.1-SNAPSHOT - 1.0-SNAPSHOT - 1.0-SNAPSHOT - 1.0-SNAPSHOT - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT + 1.0.1-SNAPSHOT + 1.0.1-SNAPSHOT + 1.0.1-SNAPSHOT 1.0.1-SNAPSHOT - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT + 1.0.1-SNAPSHOT + 1.0.1-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 @@ -235,6 +236,24 @@ + + maven-assembly-plugin + 2.6 + + mpush + + assembly.xml + + + + + package + + single + + + + From 76e9a16baf685cca42fbb291651fedf721802bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 1 Feb 2016 08:28:23 +0000 Subject: [PATCH 234/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 117fadb7..bb394b1e 100644 --- a/pom.xml +++ b/pom.xml @@ -29,16 +29,16 @@ 1.7 - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT - 1.0.1-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 From 10d179bc1ad7cb7ac093768391e73a922a1bf663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 11:03:06 +0800 Subject: [PATCH 235/890] bug fix for redis --- .../main/java/com/shinemo/mpush/common/AbstractServer.java | 4 +++- .../java/com/shinemo/mpush/tools/config/ConfigCenter.java | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 8fdc28fc..d8a0d769 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.shinemo.mpush.api.Server; import com.shinemo.mpush.common.app.Application; @@ -60,7 +61,8 @@ private void initZK(){ //step2 获取redis private void initRedis(){ boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); - if (!exist) { + String rawGroup = zkRegister.get(ZKPath.REDIS_SERVER.getPath()); + if (!exist||Strings.isNullOrEmpty(rawGroup)) { List groupList = ConfigCenter.holder.redisGroups(); zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index aedac7e0..b6efe57a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -87,6 +87,9 @@ public interface ConfigCenter extends Config { @Key("redis_group") @ConverterClass(RedisGroupConverter.class) List redisGroups(); + + @Key("force_write_redis_group_info") + boolean forceWriteRedisGroupInfo(); @Key("scan_conn_task_cycle") @DefaultValue("59000") @@ -94,4 +97,6 @@ public interface ConfigCenter extends Config { @DefaultValue("/opt/shinemo/mpush/") String logPath(); + + } From 825a1d1839ee99af70d7e4c705ef0bcf36db1235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 11:07:15 +0800 Subject: [PATCH 236/890] =?UTF-8?q?redis=20=E5=BC=BA=E5=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.properties | 5 +++-- .../main/java/com/shinemo/mpush/common/AbstractServer.java | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/config.properties b/config.properties index 04d27b9e..185d6af1 100644 --- a/config.properties +++ b/config.properties @@ -23,11 +23,12 @@ ras_key_length = 1024 ## session_expired_time = 86400 -## zk +## zk ������ zk_ip = 10.1.80.57:2181 zk_namespace = mpush zk_digest = shinemoIpo -## redis redisԷֺŷָÿڵredisԶŷָ +## redis ������ redis���Էֺŷָÿ���ڵ�redis�Զ��ŷָ� ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = 10.1.80.57:6379:shinemoIpo +force_write_redis_group_info = true diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index d8a0d769..eb4a2381 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -66,6 +66,11 @@ private void initRedis(){ List groupList = ConfigCenter.holder.redisGroups(); zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } + //强刷 + if(ConfigCenter.holder.forceWriteRedisGroupInfo()){ + List groupList = ConfigCenter.holder.redisGroups(); + zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } } //step3 注册listener From 678fcc3c63f6109a03fd909811da4ea3121a6ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 11:42:37 +0800 Subject: [PATCH 237/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=93=E5=8C=85?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-daily.properties | 24 +------- conf-online.properties | 22 +------- conf-pre.properties | 22 +------- mpush-cs/pom.xml | 16 ++++-- mpush-cs/src/main/resources/logback.xml | 73 +++++++++++++++++-------- 5 files changed, 67 insertions(+), 90 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 8279c9b1..a6e4f17f 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,25 +1,3 @@ -#主库 -jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://111.1.57.148:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 -jdbc.user=root -jdbc.password=kYCAxx6s9qe90HWnIPxWMQ -#备库 -jdbc.url.second=jdbc:mysql://111.1.57.148:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 -jdbc.user.second=root -jdbc.password.second=kYCAxx6s9qe90HWnIPxWMQ - #日志根目录 -#catalina.base=/opt/logs/mydb -catalina.base=/opt/logs/mpush +log.home=/opt/logs/mpush loglevel=DEBUG - -dubbo.zookeeper.registry.address=127.0.0.1:2181 -medical.consumer.version=1.0.0.daily -datacenterdubbo.provider.port=20882 -medicaldubbo.provider.port=20881 -dubbo.provider.version=1.0.0.datacenter.daily - - - -dc.task.host=localhost -#end diff --git a/conf-online.properties b/conf-online.properties index 8732a9a7..6f4485a7 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -1,21 +1,3 @@ -#主库 -jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://10.161.223.238:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 -jdbc.user=yimi -jdbc.password=0HXu5asddX6 - -#备库 -jdbc.url.second=jdbc:mysql://10.161.155.50:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 -jdbc.user.second=readonlyuser -jdbc.password.second=Hello2015 - #日志根目录 -catalina.base=/opt/logs/mpush -loglevel=WARN -dubbo.zookeeper.registry.address=10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -medical.consumer.version=1.0.0 -datacenterdubbo.provider.port=20882 -medicaldubbo.provider.port=20881 -dubbo.provider.version=1.0.0.datacenter.online - -dc.task.host=html5-1 +log.home=/opt/logs/mpush +loglevel=INFO diff --git a/conf-pre.properties b/conf-pre.properties index d7a6a01e..6f4485a7 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -1,21 +1,3 @@ -#主库 -jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://10.161.223.238:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 -jdbc.user=yimi -jdbc.password=0HXu5asddX6 -#备库 -jdbc.url.second=jdbc:mysql://10.161.155.50:3306/datacenter?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF8 -jdbc.user.second=readonlyuser -jdbc.password.second=Hello2015 - #日志根目录 -catalina.base=/opt/logs/mpush -loglevel=DEBUG - -dubbo.zookeeper.registry.address=10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -medical.consumer.version=1.0.0.pre -datacenterdubbo.provider.port=20882 -medicaldubbo.provider.port=20881 -dubbo.provider.version=1.0.0.datacenter.pre - -dc.task.host=html5-1 +log.home=/opt/logs/mpush +loglevel=INFO diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index e659adf6..bd25b3cf 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -26,12 +26,18 @@ mpush-cs - + + ../conf-${deploy.env}.properties + + - - src/main/resources - true - + + src/main/resources + + **/* + + true + diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 3e167126..3e5ff9c7 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -1,30 +1,59 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + + ${log.home}/mpush-warn.log + + WARN + + true + + ${log.home}/mpush-warn.log.%d{yyyy-MM-dd} + 2 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + + + ${log.home}/mpush-info.log + + INFO + + true + + ${log.home}/mpush-info.log.%d{yyyy-MM-dd} + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/mpush-error.log + + error + + true + + ${log.home}/mpush-error.log.%d{yyyy-MM-dd} + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + - + + - - + + + + From 2dd46f0b7e88594afc02e008940e0ffaae452366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 13:40:45 +0800 Subject: [PATCH 238/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 2 +- conf-daily.properties | 2 ++ conf-online.properties | 2 ++ conf-pre.properties | 2 ++ mpush-cs/pom.xml | 17 ----------------- .../src/main/resources/config.properties | 8 ++++---- 6 files changed, 11 insertions(+), 22 deletions(-) rename config.properties => mpush-cs/src/main/resources/config.properties (85%) diff --git a/assembly.xml b/assembly.xml index 58d8cdd5..e94710d3 100644 --- a/assembly.xml +++ b/assembly.xml @@ -11,7 +11,7 @@ - ./ + mpush-cs/ config.properties diff --git a/conf-daily.properties b/conf-daily.properties index a6e4f17f..85292e5b 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,3 +1,5 @@ #日志根目录 log.home=/opt/logs/mpush loglevel=DEBUG +zk_ip = 10.1.80.57:2181 +redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/conf-online.properties b/conf-online.properties index 6f4485a7..052a5272 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -1,3 +1,5 @@ #日志根目录 log.home=/opt/logs/mpush loglevel=INFO +zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 +redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/conf-pre.properties b/conf-pre.properties index 6f4485a7..052a5272 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -1,3 +1,5 @@ #日志根目录 log.home=/opt/logs/mpush loglevel=INFO +zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 +redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index bd25b3cf..3c46c3cd 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -63,23 +63,6 @@ - - maven-dependency-plugin - - - copy - package - - copy-dependencies - - - - ${project.build.directory}/lib - - - - - maven-dependency-plugin diff --git a/config.properties b/mpush-cs/src/main/resources/config.properties similarity index 85% rename from config.properties rename to mpush-cs/src/main/resources/config.properties index 185d6af1..5a622575 100644 --- a/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -23,12 +23,12 @@ ras_key_length = 1024 ## session_expired_time = 86400 -## zk ������ -zk_ip = 10.1.80.57:2181 +## zk ip +zk_ip = ${zk_ip} zk_namespace = mpush zk_digest = shinemoIpo -## redis ������ redis���Էֺŷָÿ���ڵ�redis�Զ��ŷָ� + ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 10.1.80.57:6379:shinemoIpo +redis_group = ${redis_group} force_write_redis_group_info = true From cc5a553dcbbcca6eab4c65373636ebfc40e73d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 14:39:05 +0800 Subject: [PATCH 239/890] add debug.sh --- debug.sh | 8 ++ mpush-cs/src/main/resources/logback.xml | 113 +++++++++++++++--------- start.sh | 0 3 files changed, 79 insertions(+), 42 deletions(-) create mode 100755 debug.sh create mode 100644 start.sh diff --git a/debug.sh b/debug.sh new file mode 100755 index 00000000..aa749503 --- /dev/null +++ b/debug.sh @@ -0,0 +1,8 @@ +#!/bin/sh +echo "start assembly lib..." +mvn assembly:assembly +tar -xzvf ./target/mpush-jar-with-dependency.tar.gz +echo "start start mpush..." +java -jar ./target/mpush/mpush-cs.jar -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n & + +echo "end start mpush..." diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 3e5ff9c7..38a54a1a 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -1,59 +1,88 @@ - - ${log.home}/mpush-warn.log - - WARN - - true - - ${log.home}/mpush-warn.log.%d{yyyy-MM-dd} - 2 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + ${log.home}/mpush-warn.log + + WARN + + true + + ${log.home}/mpush-warn.log.%d{yyyy-MM-dd} + + 2 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - + %logger{35} - %msg%n + - - ${log.home}/mpush-info.log - - INFO - - true - - ${log.home}/mpush-info.log.%d{yyyy-MM-dd} - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + ${log.home}/mpush-info.log + + INFO + + true + + ${log.home}/mpush-info.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - + %logger{35} - %msg%n + - - ${log.home}/mpush-error.log - - error - - true - - ${log.home}/mpush-error.log.%d{yyyy-MM-dd} - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + ${log.home}/mpush-error.log + + error + + true + + ${log.home}/mpush-error.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - + %logger{35} - %msg%n + + + + + + + ${log.home}/mpush-monitor.log + + info + + true + + ${log.home}/mpush-monitor.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - + %logger{35} - %msg%n + - - - + + + diff --git a/start.sh b/start.sh new file mode 100644 index 00000000..e69de29b From ac68bc065fa59f772663848196bd8692110dadff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 14:44:45 +0800 Subject: [PATCH 240/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-cs/src/main/resources/logback.xml | 63 +++++-------------------- 1 file changed, 12 insertions(+), 51 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 38a54a1a..94dc83a6 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -1,65 +1,26 @@ - - - ${log.home}/mpush-warn.log - - WARN - - true - - ${log.home}/mpush-warn.log.%d{yyyy-MM-dd} - - 2 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - - %logger{35} - %msg%n - - - - - - ${log.home}/mpush-info.log - - INFO - - true - - ${log.home}/mpush-info.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - - %logger{35} - %msg%n - - - - + - ${log.home}/mpush-error.log + ${log.home}/mpush.log error true - ${log.home}/mpush-error.log.%d{yyyy-MM-dd} + ${log.home}/mpush.log.%d{yyyy-MM-dd} 10 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + ${log.home}/mpush-monitor.log @@ -72,17 +33,17 @@ 5 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + + + - - - + From ba43c6221ddc3d1feba23d6e4225a498061f836d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 15:24:04 +0800 Subject: [PATCH 241/890] add --- debug.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/debug.sh b/debug.sh index aa749503..964b13a5 100755 --- a/debug.sh +++ b/debug.sh @@ -1,8 +1,17 @@ #!/bin/sh + +ENV=daily + +base_dir=`pwd` + echo "start assembly lib..." -mvn assembly:assembly -tar -xzvf ./target/mpush-jar-with-dependency.tar.gz +mvn clean assembly:assembly -P $ENV + +echo "start tar mpush..." +cd $base_dir/target +tar -xzvf ./mpush-jar-with-dependency.tar.gz echo "start start mpush..." -java -jar ./target/mpush/mpush-cs.jar -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n & + +java -jar $base_dir/target/mpush/mpush-cs.jar -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n & echo "end start mpush..." From f78c5506f776baa26a007a869b83b55c4ccc50cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 2 Feb 2016 15:41:41 +0800 Subject: [PATCH 242/890] log --- conf-daily.properties | 2 +- debug.sh | 2 +- mpush-cs/src/main/resources/logback.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 85292e5b..0beca187 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,5 +1,5 @@ #日志根目录 log.home=/opt/logs/mpush -loglevel=DEBUG +loglevel=warn zk_ip = 10.1.80.57:2181 redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/debug.sh b/debug.sh index 964b13a5..c206a69e 100755 --- a/debug.sh +++ b/debug.sh @@ -12,6 +12,6 @@ cd $base_dir/target tar -xzvf ./mpush-jar-with-dependency.tar.gz echo "start start mpush..." -java -jar $base_dir/target/mpush/mpush-cs.jar -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n & +java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -jar $base_dir/target/mpush/mpush-cs.jar & echo "end start mpush..." diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 94dc83a6..7ed58acb 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -6,7 +6,7 @@ class="ch.qos.logback.core.rolling.RollingFileAppender"> ${log.home}/mpush.log - error + warn true From ddfd567b994d678949743905cc4bbdcb5cdbd1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 3 Feb 2016 06:08:43 +0000 Subject: [PATCH 243/890] add push test --- .../mpush/common/message/BaseMessage.java | 10 +++- .../mpush/core/handler/UnbindUserHandler.java | 50 +++++++++++++++++++ .../core/router/RouterChangeListener.java | 1 + .../mpush/core/server/ConnectionServer.java | 10 ++-- .../core/server/ServerChannelHandler.java | 2 +- .../shinemo/mpush/ps/GatewayClientMain.java | 24 ++++++--- .../main/java/com/shinemo/mpush/ps/Main.java | 45 ++++++++++------- .../com/shinemo/mpush/ps/PushContent.java | 14 ++++++ mpush-ps/src/main/resources/config.properties | 33 ++++++++++++ 9 files changed, 153 insertions(+), 36 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java create mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java create mode 100644 mpush-ps/src/main/resources/config.properties diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 8d151aaf..4c473bc2 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -44,6 +44,9 @@ protected void decodeBody() { tmp = result; } } + if (tmp.length == 0) { + throw new RuntimeException("message decode ex"); + } packet.body = tmp; decode(packet.body); } @@ -64,8 +67,11 @@ protected void encodeBody() { //2.加密 SessionContext context = connection.getSessionContext(); if (context.cipher != null) { - tmp = context.cipher.encrypt(tmp); - packet.setFlag(Constants.CRYPTO_FLAG); + byte[] result = context.cipher.encrypt(tmp); + if (result.length > 0) { + tmp = result; + packet.setFlag(Constants.CRYPTO_FLAG); + } } packet.body = tmp; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java new file mode 100644 index 00000000..be88f6a4 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java @@ -0,0 +1,50 @@ +package com.shinemo.mpush.core.handler; + +import com.google.common.base.Strings; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.handler.BaseMessageHandler; +import com.shinemo.mpush.common.message.BindUserMessage; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.OkMessage; +import com.shinemo.mpush.core.router.RouterCenter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/23. + */ +public final class UnbindUserHandler extends BaseMessageHandler { + public static final Logger LOGGER = LoggerFactory.getLogger(UnbindUserHandler.class); + + @Override + public BindUserMessage decode(Packet packet, Connection connection) { + return new BindUserMessage(packet, connection); + } + + @Override + public void handle(BindUserMessage message) { + if (Strings.isNullOrEmpty(message.userId)) { + ErrorMessage.from(message).setReason("invalid param").close(); + LOGGER.error("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); + return; + } + //1.绑定用户时先看下是否握手成功 + SessionContext context = message.getConnection().getSessionContext(); + if (context.handshakeOk()) { + //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 + boolean success = RouterCenter.INSTANCE.unRegister(message.userId); + if (success) { + OkMessage.from(message).setData("unbind success").send(); + LOGGER.warn("unbind user success, userId={}, session={}", message.userId, context); + } else { + ErrorMessage.from(message).setReason("unbind failed").close(); + LOGGER.error("unbind user failure, register router failure, userId={}, session={}", message.userId, context); + } + } else { + ErrorMessage.from(message).setReason("not handshake").close(); + LOGGER.error("unbind user failure not handshake, userId={}, session={}", message.userId, context); + } + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 87134f05..d56d7c19 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -63,6 +63,7 @@ public void kickLocal(final String userId, final LocalRouter router) { message.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { + future.channel().close(); if (future.isSuccess()) { LOGGER.info("kick local connection success, userId={}, router={}", userId, router); } else { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 16880bdf..7635286e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -5,10 +5,7 @@ import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.core.handler.BindUserHandler; -import com.shinemo.mpush.core.handler.FastConnectHandler; -import com.shinemo.mpush.core.handler.HandshakeHandler; -import com.shinemo.mpush.core.handler.HeartBeatHandler; +import com.shinemo.mpush.core.handler.*; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; import com.shinemo.mpush.netty.server.ScanAllConnectionTimerTask; @@ -24,12 +21,12 @@ */ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; - + private ConnectionManager connectionManager = new NettyConnectionManager(); public ConnectionServer(int port) { super(port); - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle() / 1000, TimeUnit.SECONDS); } @Override @@ -39,6 +36,7 @@ public void init() { receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.BIND, new BindUserHandler()); + receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); connectionManager.init(); channelHandler = new ServerChannelHandler(true, connectionManager, receiver); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 787d325e..3efab5df 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -39,7 +39,7 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); - LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); + LOGGER.debug("update currentTime:" + ctx.channel() + "," + msg); connection.updateLastReadTime(); receiver.onReceive((Packet) msg, connection); } diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java index eae8f82c..a95bb049 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java @@ -2,6 +2,7 @@ import java.util.Collection; +import com.google.common.base.Strings; import com.shinemo.mpush.api.PushSender.Callback; import com.shinemo.mpush.common.AbstractClient; import com.shinemo.mpush.ps.zk.listener.impl.GatewayServerPathListener; @@ -10,14 +11,21 @@ public class GatewayClientMain extends AbstractClient { private static final int defaultTimeout = 3000; - public GatewayClientMain() { - registerListener(new GatewayServerPathListener()); - } + public GatewayClientMain() { + registerListener(new GatewayServerPathListener()); + } - public void send(String content, Collection userIds, Callback callback) { - for (String userId : userIds) { - PushRequest.build().setCallback(callback).setUserId(userId).setContent(content).setTimeout(defaultTimeout).send(); - } - } + public void send(String content, Collection userIds, Callback callback) { + if (Strings.isNullOrEmpty(content)) return; + for (String userId : userIds) { + PushRequest + .build() + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(defaultTimeout) + .send(); + } + } } diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java index 7d56aac1..db377bb9 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.ps; import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.tools.Jsons; import java.util.Arrays; import java.util.concurrent.locks.LockSupport; @@ -14,29 +15,35 @@ public static void main(String[] args) throws Exception { GatewayClientMain client = new GatewayClientMain(); client.start(); Thread.sleep(1000); - client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), - new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } + for (int i = 0; i < 100; i++) { + PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } + client.send(Jsons.toJson(content), Arrays.asList("43", "8"), + new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } } - } - ); + ); + Thread.sleep(10000); + } LockSupport.park(); } + } \ No newline at end of file diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java new file mode 100644 index 00000000..0dbfdb91 --- /dev/null +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.ps; + +public class PushContent { + public String msgId; + public String title; + public String content; + public int msgType; + + public PushContent(String msgId, String title, String content) { + this.msgId = msgId; + this.title = title; + this.content = content; + } +} \ No newline at end of file diff --git a/mpush-ps/src/main/resources/config.properties b/mpush-ps/src/main/resources/config.properties new file mode 100644 index 00000000..04d27b9e --- /dev/null +++ b/mpush-ps/src/main/resources/config.properties @@ -0,0 +1,33 @@ +## +max_packet_size = 10240 +## +compress_limit = 10240 +## +min_heartbeat = 10000 +## +max_heartbeat = 1800000 +## +max_hb_timeout_times = 2 +## +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +## +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +## +gateway_server_port = 4000 +## +connection_server_port = 3000 +## +aes_key_length = 16 +## +ras_key_length = 1024 +## +session_expired_time = 86400 + +## zk +zk_ip = 10.1.80.57:2181 +zk_namespace = mpush +zk_digest = shinemoIpo + +## redis redisԷֺŷָÿڵredisԶŷָ +##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo +redis_group = 10.1.80.57:6379:shinemoIpo From 8713f1f34b56850577612dc4ece008bd82d7da1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 3 Feb 2016 06:16:23 +0000 Subject: [PATCH 244/890] add push test --- assembly.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assembly.xml b/assembly.xml index e94710d3..6cdaef0d 100644 --- a/assembly.xml +++ b/assembly.xml @@ -11,7 +11,7 @@ - mpush-cs/ + mpush-cs/target/classes/ config.properties From 76bf23b2ea20f767f521ca8d5c390d23f2710adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 3 Feb 2016 06:25:48 +0000 Subject: [PATCH 245/890] modify log dir --- conf-daily.properties | 2 +- .../monitor/service/MonitorDataCollector.java | 150 +++++++++--------- 2 files changed, 76 insertions(+), 76 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 0beca187..76c58b66 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,5 +1,5 @@ #日志根目录 -log.home=/opt/logs/mpush +log.home=/tmp/logs/mpush loglevel=warn zk_ip = 10.1.80.57:2181 redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index be617dd9..724dd057 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -13,91 +13,91 @@ import com.shinemo.mpush.tools.Jsons; public class MonitorDataCollector { - - private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); - - private static volatile boolean dumpFirstJstack = false; - - private static volatile boolean dumpSecondJstack = false; - - private static volatile boolean dumpThirdJstack = false; - - private static volatile boolean dumpJmap = false; - - private static String currentPath = "/opt/logs/"; - - private static int firstJstack = 2; - - private static int secondJstack = 4; - - private static int thirdJstack = 6; - - private static int firstJmap = 4; - - public static MonitorData collect(){ - MonitorData data = new MonitorData(); - data.setInfoMap(JVMInfo.instance.toMap()); - data.setGcMap(JVMGC.instance.toMap()); - data.setMemoryMap(JVMMemory.instance.toMap()); - data.setThreadMap(JVMThread.instance.toMap()); - data.setThreadPoolMap(JVMThreadPool.instance.toMap()); - return data; - } - - public void setPath(String path) { - currentPath = path; + + private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); + + private static volatile boolean dumpFirstJstack = false; + + private static volatile boolean dumpSecondJstack = false; + + private static volatile boolean dumpThirdJstack = false; + + private static volatile boolean dumpJmap = false; + + private static String currentPath = "/tmp/logs/mpush/"; + + private static int firstJstack = 2; + + private static int secondJstack = 4; + + private static int thirdJstack = 6; + + private static int firstJmap = 4; + + public static MonitorData collect() { + MonitorData data = new MonitorData(); + data.setInfoMap(JVMInfo.instance.toMap()); + data.setGcMap(JVMGC.instance.toMap()); + data.setMemoryMap(JVMMemory.instance.toMap()); + data.setThreadMap(JVMThread.instance.toMap()); + data.setThreadPoolMap(JVMThreadPool.instance.toMap()); + return data; } - - public static void start(){ - new Thread(new Runnable() { + + public void setPath(String path) { + currentPath = path; + } + + public static void start() { + new Thread(new Runnable() { @Override public void run() { while (true) { - MonitorData monitorData = MonitorDataCollector.collect(); - log.error("monitor data:"+Jsons.toJson(monitorData)); - - double load = JVMInfo.instance.load(); - if(load>firstJstack){ - if(!dumpFirstJstack){ - dumpFirstJstack = true; - JVMUtil.dumpJstack(currentPath); - } - } - - if(load>secondJstack){ - if(!dumpSecondJstack){ - dumpSecondJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if(load>thirdJstack){ - if(!dumpThirdJstack){ - dumpThirdJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if(load>firstJmap){ - if(!dumpJmap){ - dumpJmap = true; - JVMUtil.dumpJmap(currentPath); - } - } - + MonitorData monitorData = MonitorDataCollector.collect(); + log.error("monitor data:" + Jsons.toJson(monitorData)); + + double load = JVMInfo.instance.load(); + if (load > firstJstack) { + if (!dumpFirstJstack) { + dumpFirstJstack = true; + JVMUtil.dumpJstack(currentPath); + } + } + + if (load > secondJstack) { + if (!dumpSecondJstack) { + dumpSecondJstack = true; + JVMUtil.dumpJmap(currentPath); + } + } + + if (load > thirdJstack) { + if (!dumpThirdJstack) { + dumpThirdJstack = true; + JVMUtil.dumpJmap(currentPath); + } + } + + if (load > firstJmap) { + if (!dumpJmap) { + dumpJmap = true; + JVMUtil.dumpJmap(currentPath); + } + } + try {//30s Thread.sleep(30000L); } catch (InterruptedException e) { - log.warn("monitor data exception",e); + log.warn("monitor data exception", e); } } } }).start(); - } - - public static void main(String[] args) { - MonitorDataCollector.start(); - } - + } + + public static void main(String[] args) { + MonitorDataCollector.start(); + } + } From 577dfb5c2793e7076ebb60a472f437a17e022200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 3 Feb 2016 15:46:36 +0800 Subject: [PATCH 246/890] add log --- .../mpush/core/router/LocalRouter.java | 2 - .../core/server/ServerChannelHandler.java | 7 ++- .../com/shinemo/mpush/cs/client/Main.java | 2 +- mpush-cs/src/main/resources/logback.xml | 46 +++++++++++++++++++ .../java/com/shinemo/mpush/log/LogType.java | 6 +++ .../com/shinemo/mpush/log/LoggerManage.java | 40 ++++++++++++++++ 6 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java create mode 100644 mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java index f63e6862..60affbc8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java @@ -1,9 +1,7 @@ package com.shinemo.mpush.core.router; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.netty.connection.NettyConnectionManager; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 787d325e..67d5f420 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -5,6 +5,7 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandler; @@ -39,6 +40,7 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); + connection.close(); LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); connection.updateLastReadTime(); receiver.onReceive((Packet) msg, connection); @@ -47,12 +49,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); + LoggerManage.log(security, "client exceptionCaught channel=", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); + LoggerManage.log(security, "client connect channel=%s", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -60,7 +63,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client disconnect channel={}", ctx.channel()); + LoggerManage.log(security, "client disconnect channel=", ctx.channel()); connectionManager.remove(ctx.channel()); } } \ No newline at end of file diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 305353f2..fb8ed475 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<10;i++){ + for(int i = 0;i<1;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 7ed58acb..19c2d42d 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -41,6 +41,52 @@ + + + + + ${log.home}/mpush-connection.log + + info + + true + + ${log.home}/mpush-connection.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + + + + ${log.home}/mpush-push.log + + info + + true + + ${log.home}/mpush-push.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java new file mode 100644 index 00000000..29a352ce --- /dev/null +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java @@ -0,0 +1,6 @@ +package com.shinemo.mpush.log; + +public enum LogType { + CONNECTION,PUSH + +} diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java new file mode 100644 index 00000000..132b53ac --- /dev/null +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LoggerManage { + + private static final Logger connectionLog = LoggerFactory.getLogger("connectionLog"); + private static final Logger pushLog = LoggerFactory.getLogger("pushLog"); + + public static void log(LogType type,String format,Object... arguments){ + String ret = String.format(format, arguments); + if(type.equals(LogType.CONNECTION)){ + connectionLog.info(ret); + }else if(type.equals(LogType.PUSH)){ + pushLog.info(ret); + } + } + + /** + * security的log 为 connectionLog的log + * @param security + * @param format + * @param arguments + */ + public static void log(boolean security,String format,Object... arguments){ + String ret = String.format(format, arguments); + if(security){ + connectionLog.info(ret); + }else{ + pushLog.info(ret); + } + } + + public static void main(String[] args) { + String format = "client connect channel=%s"; + System.out.println(String.format(format, "hi")); + } + +} From 7830bd45da662c92324c7d83b21473b7b3a1bc81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 3 Feb 2016 16:58:01 +0800 Subject: [PATCH 247/890] add close test --- .../java/com/shinemo/mpush/core/handler/HandshakeHandler.java | 1 + .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index f0ff2cf7..ae3f07c6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -31,6 +31,7 @@ public HandshakeMessage decode(Packet packet, Connection connection) { @Override public void handle(HandshakeMessage message) { + byte[] iv = message.iv;//AES密钥向量16位 byte[] clientKey = message.clientKey;//客户端随机数16位 byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数16位 diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index fe318379..051993fb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -38,7 +38,6 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); - connection.close(); LOGGER.debug("update currentTime:" + ctx.channel() + "," + msg); connection.updateLastReadTime(); receiver.onReceive((Packet) msg, connection); @@ -47,7 +46,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LoggerManage.log(security, "client exceptionCaught channel=", ctx.channel()); + LoggerManage.log(security, "client exceptionCaught channel=%s", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } From b5fc3fbce580479587beb8d5f1138b9c54793058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 3 Feb 2016 17:07:55 +0800 Subject: [PATCH 248/890] log bugfix --- .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 051993fb..9abe0f19 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -60,7 +60,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, "client disconnect channel=", ctx.channel()); + LoggerManage.log(security, "client disconnect channel=%s", ctx.channel()); connectionManager.remove(ctx.channel()); } } \ No newline at end of file From f25cbf9960b1dd03ad1153ca566caa849d9d7bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 10:44:01 +0800 Subject: [PATCH 249/890] =?UTF-8?q?log=20=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/core/handler/BindUserHandler.java | 5 ++++- .../com/shinemo/mpush/core/handler/HandshakeHandler.java | 5 ++++- .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 2f32de22..2b6ff9db 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -9,6 +9,9 @@ import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.core.router.RouterCenter; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +40,7 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { OkMessage.from(message).setData("bind success").send(); - LOGGER.warn("bind user success, userId={}, session={}", message.userId, context); + LoggerManage.log(LogType.CONNECTION, "bind user success, userId=%s, session=%s", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.INSTANCE.unRegister(message.userId); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index ae3f07c6..796a3ced 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -14,7 +14,10 @@ import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.core.session.ReusableSession; import com.shinemo.mpush.core.session.ReusableSessionManager; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.MPushUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -87,6 +90,6 @@ public void handle(HandshakeMessage message) { //9.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); - LOGGER.warn("handshake success, session={}", context); + LoggerManage.log(LogType.CONNECTION, "client handshake success:%s", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 9abe0f19..8b1d3e0c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -11,6 +11,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; From f65c68a36d3a308d001ebb0fc6f04985136eebfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 11:27:18 +0800 Subject: [PATCH 250/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0test=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/HandshakeHandler.java | 2 +- .../mpush/cs/client/ConnectionClientMain.java | 25 --- .../com/shinemo/mpush/cs/client/Main.java | 52 ----- mpush-ps/pom.xml | 34 ---- .../shinemo/mpush/ps/GatewayClientMain.java | 31 --- .../main/java/com/shinemo/mpush/ps/Main.java | 49 ----- .../com/shinemo/mpush/ps/PushContent.java | 14 -- .../com/shinemo/mpush/ps/PushRequest.java | 187 ------------------ .../com/shinemo/mpush/ps/PushRequestBus.java | 44 ----- .../mpush/ps/client/ClientChannelHandler.java | 94 --------- .../ps/manage/impl/GatewayServerManage.java | 86 -------- .../impl/GatewayServerPathListener.java | 40 ---- ...m.shinemo.mpush.common.manage.ServerManage | 1 - mpush-ps/src/main/resources/config.properties | 33 ---- mpush-ps/src/main/resources/logback.xml | 30 --- .../test/java/com/shinemo/mpush/AppTest.java | 38 ---- .../shinemo/mpush/ps/ConfigCenterTest.java | 16 -- pom.xml | 14 +- 18 files changed, 13 insertions(+), 777 deletions(-) delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java delete mode 100644 mpush-ps/pom.xml delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java delete mode 100644 mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java delete mode 100644 mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage delete mode 100644 mpush-ps/src/main/resources/config.properties delete mode 100644 mpush-ps/src/main/resources/logback.xml delete mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java delete mode 100644 mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 796a3ced..dd46b9b4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -88,7 +88,7 @@ public void handle(HandshakeMessage message) { //9.保存可复用session到Redis, 用于快速重连 ReusableSessionManager.INSTANCE.cacheSession(session); - //9.触发握手成功事件 + //10.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); LoggerManage.log(LogType.CONNECTION, "client handshake success:%s", context); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java deleted file mode 100644 index d3f30a2b..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/ConnectionClientMain.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.shinemo.mpush.cs.client; - -import java.util.List; - -import com.google.common.collect.Lists; -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; -import com.shinemo.mpush.tools.spi.ServiceContainer; - -public class ConnectionClientMain extends AbstractClient { - - @SuppressWarnings("unchecked") - private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); - - public ConnectionClientMain() { - registerListener(new ConnectionServerPathListener()); - } - - public List getApplicationList(){ - return Lists.newArrayList(connectionServerManage.getList()); - } - -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java deleted file mode 100644 index fb8ed475..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.shinemo.mpush.cs.client; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.core.client.ClientChannelHandler; -import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.netty.client.SecurityNettyClient; - -public class Main { - - public static void main(String[] args) throws Exception { - ConnectionClientMain main = new ConnectionClientMain(); - main.start(); - - List serverList = main.getApplicationList(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ConnectionServerApplication server = serverList.get(index); - - for(int i = 0;i<1;i++){ - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "user-"+i; - String deviceId = "test-device-id-"+i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(10); - } - - LockSupport.park(); - - } - -} diff --git a/mpush-ps/pom.xml b/mpush-ps/pom.xml deleted file mode 100644 index e74ec6d7..00000000 --- a/mpush-ps/pom.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - - 4.0.0 - - mpush-ps - jar - ${mpush-ps-version} - mpush-ps - - - - - com.shinemo.mpush - mpush-api - - - com.shinemo.mpush - mpush-netty - - - com.shinemo.mpush - mpush-tools - - - com.shinemo.mpush - mpush-core - - - diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java deleted file mode 100644 index a95bb049..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientMain.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.shinemo.mpush.ps; - -import java.util.Collection; - -import com.google.common.base.Strings; -import com.shinemo.mpush.api.PushSender.Callback; -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.ps.zk.listener.impl.GatewayServerPathListener; - -public class GatewayClientMain extends AbstractClient { - - private static final int defaultTimeout = 3000; - - public GatewayClientMain() { - registerListener(new GatewayServerPathListener()); - } - - public void send(String content, Collection userIds, Callback callback) { - if (Strings.isNullOrEmpty(content)) return; - for (String userId : userIds) { - PushRequest - .build() - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(defaultTimeout) - .send(); - } - } - -} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java deleted file mode 100644 index db377bb9..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/Main.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.shinemo.mpush.ps; - -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.tools.Jsons; - -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; - -/** - * Created by ohun on 2016/1/7. - */ -public class Main { - - public static void main(String[] args) throws Exception { - GatewayClientMain client = new GatewayClientMain(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - - client.send(Jsons.toJson(content), Arrays.asList("43", "8"), - new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - } - ); - Thread.sleep(10000); - } - LockSupport.park(); - } - -} \ No newline at end of file diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java deleted file mode 100644 index 0dbfdb91..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushContent.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.shinemo.mpush.ps; - -public class PushContent { - public String msgId; - public String title; - public String content; - public int msgType; - - public PushContent(String msgId, String title, String content) { - this.msgId = msgId; - this.title = title; - this.content = content; - } -} \ No newline at end of file diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java deleted file mode 100644 index 20db80d0..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequest.java +++ /dev/null @@ -1,187 +0,0 @@ -package com.shinemo.mpush.ps; - -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.common.router.ConnectionRouterManager; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.ps.manage.impl.GatewayServerManage; -import com.shinemo.mpush.tools.spi.ServiceContainer; - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/30. - */ -public class PushRequest implements PushSender.Callback, Runnable { - - private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); - - private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - private PushSender.Callback callback; - private String userId; - private String content; - private long timeout; - private int status = 0; - private long timeout_; - private int sessionId; - private long sendTime; - - public PushRequest() { - } - - public static PushRequest build() { - return new PushRequest(); - } - - public PushRequest setCallback(PushSender.Callback callback) { - this.callback = callback; - return this; - } - - public PushRequest setUserId(String userId) { - this.userId = userId; - return this; - } - - public PushRequest setContent(String content) { - this.content = content; - return this; - } - - public PushRequest setTimeout(long timeout) { - this.timeout = timeout; - return this; - } - - public void setSessionId(int sessionId) { - this.sessionId = sessionId; - } - - public int getSessionId() { - return sessionId; - } - - @Override - public void onSuccess(String userId) { - submit(1); - } - - @Override - public void onFailure(String userId) { - submit(2); - } - - @Override - public void onOffline(String userId) { - submit(3); - } - - @Override - public void onTimeout(String userId) { - submit(4); - } - - private void submit(int status) { - if (this.status != 0) {//防止重复调用 - return; - } - this.status = status; - if (callback != null) { - PushRequestBus.INSTANCE.getExecutor().execute(this); - } else { - LOGGER.warn("callback is null"); - } - } - - @Override - public void run() { - switch (status) { - case 1: - callback.onSuccess(userId); - break; - case 2: - callback.onFailure(userId); - break; - case 3: - callback.onOffline(userId); - break; - case 4: - callback.onTimeout(userId); - break; - } - } - - public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; - } - - public void timeout() { - onTimeout(userId); - } - - public void success() { - onSuccess(userId); - } - - public void failure() { - onFailure(userId); - } - - public void offline() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - onOffline(userId); - } - - public void send() { - this.timeout_ = timeout + System.currentTimeMillis(); - sendToConnServer(); - } - - public void redirect() { - this.status = 0; - this.timeout_ = timeout + System.currentTimeMillis(); - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - sendToConnServer(); - LOGGER.warn("user route has changed, userId={}, content={}", userId, content); - } - - private void sendToConnServer() { - //1.查询用户长连接所在的机器 - RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); - if (router == null) { - //1.1没有查到说明用户已经下线 - this.onOffline(userId); - return; - } - - //2.通过网关连接,把消息发送到所在机器 - ClientLocation location = router.getRouteValue(); - Connection gatewayConn = gatewayClientManage.getConnection(location.getHost()); - if (gatewayConn == null || !gatewayConn.isConnected()) { - this.onFailure(userId); - return; - } - - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - sendTime = System.currentTimeMillis(); - } else { - PushRequest.this.onFailure(userId); - } - } - }); - - sessionId = pushMessage.getSessionId(); - PushRequestBus.INSTANCE.put(sessionId, this); - } -} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java deleted file mode 100644 index 97cbcfd7..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/PushRequestBus.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.shinemo.mpush.ps; - -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.*; - -/** - * Created by ohun on 2015/12/30. - */ -public class PushRequestBus implements Runnable { - public static final PushRequestBus INSTANCE = new PushRequestBus(); - private Map requests = new ConcurrentHashMap<>(); - private Executor executor = Executors.newFixedThreadPool(5);//test - private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test - - public PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); - } - - public void put(int sessionId, PushRequest request) { - requests.put(sessionId, request); - } - - public PushRequest remove(int sessionId) { - return requests.remove(sessionId); - } - - public Executor getExecutor() { - return executor; - } - - @Override - public void run() { - if (requests.isEmpty()) return; - Iterator it = requests.values().iterator(); - while (it.hasNext()) { - PushRequest request = it.next(); - if (request.isTimeout()) { - it.remove();//清除超时的请求 - request.timeout(); - } - } - } -} diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java deleted file mode 100644 index 3dd8ebff..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/client/ClientChannelHandler.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.shinemo.mpush.ps.client; - - - -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.netty.client.ChannelClientHandler; -import com.shinemo.mpush.netty.connection.NettyConnection; -import com.shinemo.mpush.ps.PushRequest; -import com.shinemo.mpush.ps.PushRequestBus; - -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static com.shinemo.mpush.common.ErrorCode.OFFLINE; -import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; - -/** - * Created by ohun on 2015/12/19. - */ -@ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); - if (msg instanceof Packet) { - Packet packet = ((Packet) msg); - PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); - return; - } - - if (packet.cmd == Command.OK.cmd) { - request.success(); - } else { - ErrorMessage message = new ErrorMessage(packet, client.getConnection()); - if (message.code == OFFLINE.errorCode) { - request.offline(); - } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { - request.failure(); - } else if (message.code == ROUTER_CHANGE.errorCode) { - request.redirect(); - } - LOGGER.warn("receive an error gateway response, message={}", message); - } - } - LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - client.close("exception"); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); - connection.init(ctx.channel(), false); - client.initConnection(connection); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - client.close("inactive"); - LOGGER.info("client disconnect channel={}", ctx.channel()); - } - - -} \ No newline at end of file diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java deleted file mode 100644 index e796e964..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/manage/impl/GatewayServerManage.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.shinemo.mpush.ps.manage.impl; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.netty.client.NettyClient; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.ps.client.ClientChannelHandler; - -public class GatewayServerManage implements ServerManage{ - - private static final Logger log = LoggerFactory.getLogger(GatewayServerManage.class); - - private static Map holder = Maps.newConcurrentMap(); - - private final Map application2Client = Maps.newConcurrentMap(); - - private final Map ip2Client = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath,GatewayServerApplication application){ - holder.put(fullPath, application); - try{ - Client client = new NettyClient(application.getIp(), application.getPort()); - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - application2Client.put(application, client); - ip2Client.put(application.getIp(), client); - }catch(Exception e){ - - } - printList(); - } - - @Override - public void remove(String fullPath){ - GatewayServerApplication application = get(fullPath); - if(application!=null){ - Client client = application2Client.get(application); - if(client!=null){ - client.stop(); - } - } - ip2Client.remove(application.getIp()+":"+application.getPort()); - holder.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(holder.values()); - } - - private void printList(){ - for(GatewayServerApplication app:holder.values()){ - log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); - } - } - - public GatewayServerApplication get(String fullpath){ - return holder.get(fullpath); - } - - public Client getClient(GatewayServerApplication application){ - return application2Client.get(application); - } - - public Connection getConnection(String ipAndPort) { - Client client = ip2Client.get(ipAndPort); - if (client == null) return null; - return client.getConnection(); - } - -} - diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java deleted file mode 100644 index 16b7fad4..00000000 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/zk/listener/impl/GatewayServerPathListener.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.shinemo.mpush.ps.zk.listener.impl; - -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; - -/** - * gateway server 应用 监控 - * - */ -public class GatewayServerPathListener extends AbstractDataChangeListener{ - - @SuppressWarnings("unchecked") - private ServerManage gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); - - @Override - public String listenerPath() { - return ZKPath.GATEWAY_SERVER.getWatchPath(); - } - - @Override - public ServerManage getServerManage() { - return gatewayServerManage; - } - - @Override - public String getRegisterPath() { - return ZKPath.GATEWAY_SERVER.getPath(); - } - - @Override - public String getFullPath(String raw) { - return ZKPath.GATEWAY_SERVER.getFullPath(raw); - } - - - -} diff --git a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage deleted file mode 100644 index 85817c00..00000000 --- a/mpush-ps/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ /dev/null @@ -1 +0,0 @@ -gatewayServerManage=com.shinemo.mpush.ps.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-ps/src/main/resources/config.properties b/mpush-ps/src/main/resources/config.properties deleted file mode 100644 index 04d27b9e..00000000 --- a/mpush-ps/src/main/resources/config.properties +++ /dev/null @@ -1,33 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 1800000 -## -max_hb_timeout_times = 2 -## -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port = 4000 -## -connection_server_port = 3000 -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 - -## zk -zk_ip = 10.1.80.57:2181 -zk_namespace = mpush -zk_digest = shinemoIpo - -## redis redisԷֺŷָÿڵredisԶŷָ -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/mpush-ps/src/main/resources/logback.xml b/mpush-ps/src/main/resources/logback.xml deleted file mode 100644 index 20979b40..00000000 --- a/mpush-ps/src/main/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java deleted file mode 100644 index 50fff9a1..00000000 --- a/mpush-ps/src/test/java/com/shinemo/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.shinemo.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java b/mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java deleted file mode 100644 index abaf4ca2..00000000 --- a/mpush-ps/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.shinemo.mpush.ps; - -import org.junit.Test; - -import com.shinemo.mpush.tools.config.ConfigCenter; - -public class ConfigCenterTest { - - @Test - public void test(){ - - System.out.println(ConfigCenter.holder.zkIp()); - - } - -} diff --git a/pom.xml b/pom.xml index bb394b1e..cf962651 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ mpush-common mpush-client mpush-cs - mpush-ps + mpush-test mpush-monitor mpush-log @@ -38,7 +38,7 @@ 1.0.2-SNAPSHOT 1.0.2-SNAPSHOT 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT + 1.0.2-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 @@ -133,6 +133,16 @@ mpush-log ${mpush-log-version} + + com.shinemo.mpush + mpush-test + ${mpush-test-version} + + + com.shinemo.mpush + mpush-cs + ${mpush-cs-version} + From d42aba7bcc6b5a78b59ed6ca22671c2d3ca0ee01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 11:30:48 +0800 Subject: [PATCH 251/890] add test module --- mpush-test/pom.xml | 38 ++++ .../client/ConnectionClientMain.java | 25 +++ .../mpush/test/connection/client/Main.java | 52 +++++ .../mpush/test/push/GatewayClientMain.java | 31 +++ .../com/shinemo/mpush/test/push/Main.java | 49 +++++ .../shinemo/mpush/test/push/PushContent.java | 14 ++ .../shinemo/mpush/test/push/PushRequest.java | 187 ++++++++++++++++++ .../mpush/test/push/PushRequestBus.java | 44 +++++ .../push/client/ClientChannelHandler.java | 95 +++++++++ .../push/manage/impl/GatewayServerManage.java | 86 ++++++++ .../impl/GatewayServerPathListener.java | 40 ++++ ...m.shinemo.mpush.common.manage.ServerManage | 1 + .../src/main/resources/config.properties | 33 ++++ mpush-test/src/main/resources/logback.xml | 30 +++ .../test/java/com/shinemo/mpush/AppTest.java | 38 ++++ .../shinemo/mpush/ps/ConfigCenterTest.java | 16 ++ 16 files changed, 779 insertions(+) create mode 100644 mpush-test/pom.xml create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/Main.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/GatewayClientMain.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/Main.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/PushContent.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequest.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequestBus.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java create mode 100644 mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage create mode 100644 mpush-test/src/main/resources/config.properties create mode 100644 mpush-test/src/main/resources/logback.xml create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/AppTest.java create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml new file mode 100644 index 00000000..c4cfc1ce --- /dev/null +++ b/mpush-test/pom.xml @@ -0,0 +1,38 @@ + + + mpush + com.shinemo.mpush + 1.0-SNAPSHOT + + 4.0.0 + + mpush-test + jar + ${mpush-test-version} + mpush-test + + + + + com.shinemo.mpush + mpush-api + + + com.shinemo.mpush + mpush-netty + + + com.shinemo.mpush + mpush-tools + + + com.shinemo.mpush + mpush-core + + + com.shinemo.mpush + mpush-cs + + + diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java b/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java new file mode 100644 index 00000000..d6a554ae --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java @@ -0,0 +1,25 @@ +package com.shinemo.mpush.test.connection.client; + +import java.util.List; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; +import com.shinemo.mpush.tools.spi.ServiceContainer; + +public class ConnectionClientMain extends AbstractClient { + + @SuppressWarnings("unchecked") + private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); + + public ConnectionClientMain() { + registerListener(new ConnectionServerPathListener()); + } + + public List getApplicationList(){ + return Lists.newArrayList(connectionServerManage.getList()); + } + +} diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/Main.java new file mode 100644 index 00000000..6c0121f0 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/Main.java @@ -0,0 +1,52 @@ +package com.shinemo.mpush.test.connection.client; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.core.client.ClientChannelHandler; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.netty.client.SecurityNettyClient; + +public class Main { + + public static void main(String[] args) throws Exception { + ConnectionClientMain main = new ConnectionClientMain(); + main.start(); + + List serverList = main.getApplicationList(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ConnectionServerApplication server = serverList.get(index); + + for(int i = 0;i<1;i++){ + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "user-"+i; + String deviceId = "test-device-id-"+i; + String cipher = ""; + byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); + byte[] iv = CipherBox.INSTANCE.randomAESIV(); + + SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); + client.setClientKey(clientKey); + client.setIv(iv); + client.setClientVersion(clientVersion); + client.setDeviceId(deviceId); + client.setOsName(osName); + client.setOsVersion(osVersion); + client.setUserId(userId); + client.setCipher(cipher); + + ClientChannelHandler handler = new ClientChannelHandler(client); + NettyClientFactory.INSTANCE.create(handler); + Thread.sleep(10); + } + + LockSupport.park(); + + } + +} diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/GatewayClientMain.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/GatewayClientMain.java new file mode 100644 index 00000000..7f9a80b7 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/GatewayClientMain.java @@ -0,0 +1,31 @@ +package com.shinemo.mpush.test.push; + +import java.util.Collection; + +import com.google.common.base.Strings; +import com.shinemo.mpush.api.PushSender.Callback; +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.test.push.zk.listener.impl.GatewayServerPathListener; + +public class GatewayClientMain extends AbstractClient { + + private static final int defaultTimeout = 3000; + + public GatewayClientMain() { + registerListener(new GatewayServerPathListener()); + } + + public void send(String content, Collection userIds, Callback callback) { + if (Strings.isNullOrEmpty(content)) return; + for (String userId : userIds) { + PushRequest + .build() + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(defaultTimeout) + .send(); + } + } + +} diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/Main.java new file mode 100644 index 00000000..418e20ec --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/Main.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.test.push; + +import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.tools.Jsons; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + +/** + * Created by ohun on 2016/1/7. + */ +public class Main { + + public static void main(String[] args) throws Exception { + GatewayClientMain client = new GatewayClientMain(); + client.start(); + Thread.sleep(1000); + for (int i = 0; i < 100; i++) { + PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); + + client.send(Jsons.toJson(content), Arrays.asList("43", "8"), + new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + } + ); + Thread.sleep(10000); + } + LockSupport.park(); + } + +} \ No newline at end of file diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushContent.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushContent.java new file mode 100644 index 00000000..7c39ea87 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushContent.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.test.push; + +public class PushContent { + public String msgId; + public String title; + public String content; + public int msgType; + + public PushContent(String msgId, String title, String content) { + this.msgId = msgId; + this.title = title; + this.content = content; + } +} \ No newline at end of file diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequest.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequest.java new file mode 100644 index 00000000..d990959d --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequest.java @@ -0,0 +1,187 @@ +package com.shinemo.mpush.test.push; + +import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.router.ClientLocation; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; +import com.shinemo.mpush.common.router.ConnectionRouterManager; +import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.test.push.manage.impl.GatewayServerManage; +import com.shinemo.mpush.tools.spi.ServiceContainer; + +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushRequest implements PushSender.Callback, Runnable { + + private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); + + private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); + private PushSender.Callback callback; + private String userId; + private String content; + private long timeout; + private int status = 0; + private long timeout_; + private int sessionId; + private long sendTime; + + public PushRequest() { + } + + public static PushRequest build() { + return new PushRequest(); + } + + public PushRequest setCallback(PushSender.Callback callback) { + this.callback = callback; + return this; + } + + public PushRequest setUserId(String userId) { + this.userId = userId; + return this; + } + + public PushRequest setContent(String content) { + this.content = content; + return this; + } + + public PushRequest setTimeout(long timeout) { + this.timeout = timeout; + return this; + } + + public void setSessionId(int sessionId) { + this.sessionId = sessionId; + } + + public int getSessionId() { + return sessionId; + } + + @Override + public void onSuccess(String userId) { + submit(1); + } + + @Override + public void onFailure(String userId) { + submit(2); + } + + @Override + public void onOffline(String userId) { + submit(3); + } + + @Override + public void onTimeout(String userId) { + submit(4); + } + + private void submit(int status) { + if (this.status != 0) {//防止重复调用 + return; + } + this.status = status; + if (callback != null) { + PushRequestBus.INSTANCE.getExecutor().execute(this); + } else { + LOGGER.warn("callback is null"); + } + } + + @Override + public void run() { + switch (status) { + case 1: + callback.onSuccess(userId); + break; + case 2: + callback.onFailure(userId); + break; + case 3: + callback.onOffline(userId); + break; + case 4: + callback.onTimeout(userId); + break; + } + } + + public boolean isTimeout() { + return System.currentTimeMillis() > timeout_; + } + + public void timeout() { + onTimeout(userId); + } + + public void success() { + onSuccess(userId); + } + + public void failure() { + onFailure(userId); + } + + public void offline() { + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + onOffline(userId); + } + + public void send() { + this.timeout_ = timeout + System.currentTimeMillis(); + sendToConnServer(); + } + + public void redirect() { + this.status = 0; + this.timeout_ = timeout + System.currentTimeMillis(); + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + sendToConnServer(); + LOGGER.warn("user route has changed, userId={}, content={}", userId, content); + } + + private void sendToConnServer() { + //1.查询用户长连接所在的机器 + RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); + if (router == null) { + //1.1没有查到说明用户已经下线 + this.onOffline(userId); + return; + } + + //2.通过网关连接,把消息发送到所在机器 + ClientLocation location = router.getRouteValue(); + Connection gatewayConn = gatewayClientManage.getConnection(location.getHost()); + if (gatewayConn == null || !gatewayConn.isConnected()) { + this.onFailure(userId); + return; + } + + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); + pushMessage.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + sendTime = System.currentTimeMillis(); + } else { + PushRequest.this.onFailure(userId); + } + } + }); + + sessionId = pushMessage.getSessionId(); + PushRequestBus.INSTANCE.put(sessionId, this); + } +} diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequestBus.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequestBus.java new file mode 100644 index 00000000..d2687bc0 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequestBus.java @@ -0,0 +1,44 @@ +package com.shinemo.mpush.test.push; + +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.*; + +/** + * Created by ohun on 2015/12/30. + */ +public class PushRequestBus implements Runnable { + public static final PushRequestBus INSTANCE = new PushRequestBus(); + private Map requests = new ConcurrentHashMap<>(); + private Executor executor = Executors.newFixedThreadPool(5);//test + private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test + + public PushRequestBus() { + scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); + } + + public void put(int sessionId, PushRequest request) { + requests.put(sessionId, request); + } + + public PushRequest remove(int sessionId) { + return requests.remove(sessionId); + } + + public Executor getExecutor() { + return executor; + } + + @Override + public void run() { + if (requests.isEmpty()) return; + Iterator it = requests.values().iterator(); + while (it.hasNext()) { + PushRequest request = it.next(); + if (request.isTimeout()) { + it.remove();//清除超时的请求 + request.timeout(); + } + } + } +} diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java new file mode 100644 index 00000000..bb917502 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java @@ -0,0 +1,95 @@ +package com.shinemo.mpush.test.push.client; + + + +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.netty.client.ChannelClientHandler; +import com.shinemo.mpush.netty.connection.NettyConnection; +import com.shinemo.mpush.test.push.PushRequest; +import com.shinemo.mpush.test.push.PushRequestBus; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.shinemo.mpush.common.ErrorCode.OFFLINE; +import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; + +/** + * Created by ohun on 2015/12/19. + */ +@ChannelHandler.Sharable +public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); + + private Client client; + + public ClientChannelHandler(Client client) { + this.client = client; + } + + @Override + public Client getClient() { + return client; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + client.getConnection().updateLastReadTime(); + if (msg instanceof Packet) { + Packet packet = ((Packet) msg); + PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); + if (request == null) { + LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); + return; + } + + if (packet.cmd == Command.OK.cmd) { + request.success(); + } else { + ErrorMessage message = new ErrorMessage(packet, client.getConnection()); + if (message.code == OFFLINE.errorCode) { + request.offline(); + } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { + request.failure(); + } else if (message.code == ROUTER_CHANGE.errorCode) { + request.redirect(); + } + LOGGER.warn("receive an error gateway response, message={}", message); + } + } + LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + client.close("exception"); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + Connection connection = new NettyConnection(); + connection.init(ctx.channel(), false); + client.initConnection(connection); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + client.close("inactive"); + LOGGER.info("client disconnect channel={}", ctx.channel()); + } + + +} \ No newline at end of file diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java new file mode 100644 index 00000000..7942e639 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java @@ -0,0 +1,86 @@ +package com.shinemo.mpush.test.push.manage.impl; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.netty.client.NettyClient; +import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.test.push.client.ClientChannelHandler; + +public class GatewayServerManage implements ServerManage{ + + private static final Logger log = LoggerFactory.getLogger(GatewayServerManage.class); + + private static Map holder = Maps.newConcurrentMap(); + + private final Map application2Client = Maps.newConcurrentMap(); + + private final Map ip2Client = Maps.newConcurrentMap(); + + @Override + public void addOrUpdate(String fullPath,GatewayServerApplication application){ + holder.put(fullPath, application); + try{ + Client client = new NettyClient(application.getIp(), application.getPort()); + ClientChannelHandler handler = new ClientChannelHandler(client); + NettyClientFactory.INSTANCE.create(handler); + application2Client.put(application, client); + ip2Client.put(application.getIp(), client); + }catch(Exception e){ + + } + printList(); + } + + @Override + public void remove(String fullPath){ + GatewayServerApplication application = get(fullPath); + if(application!=null){ + Client client = application2Client.get(application); + if(client!=null){ + client.stop(); + } + } + ip2Client.remove(application.getIp()+":"+application.getPort()); + holder.remove(fullPath); + printList(); + } + + @Override + public Collection getList() { + return Collections.unmodifiableCollection(holder.values()); + } + + private void printList(){ + for(GatewayServerApplication app:holder.values()){ + log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + } + } + + public GatewayServerApplication get(String fullpath){ + return holder.get(fullpath); + } + + public Client getClient(GatewayServerApplication application){ + return application2Client.get(application); + } + + public Connection getConnection(String ipAndPort) { + Client client = ip2Client.get(ipAndPort); + if (client == null) return null; + return client.getConnection(); + } + +} + diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java b/mpush-test/src/main/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java new file mode 100644 index 00000000..d4ba4ee1 --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.test.push.zk.listener.impl; + +import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ZKPath; + +/** + * gateway server 应用 监控 + * + */ +public class GatewayServerPathListener extends AbstractDataChangeListener{ + + @SuppressWarnings("unchecked") + private ServerManage gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); + + @Override + public String listenerPath() { + return ZKPath.GATEWAY_SERVER.getWatchPath(); + } + + @Override + public ServerManage getServerManage() { + return gatewayServerManage; + } + + @Override + public String getRegisterPath() { + return ZKPath.GATEWAY_SERVER.getPath(); + } + + @Override + public String getFullPath(String raw) { + return ZKPath.GATEWAY_SERVER.getFullPath(raw); + } + + + +} diff --git a/mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage new file mode 100644 index 00000000..85817c00 --- /dev/null +++ b/mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -0,0 +1 @@ +gatewayServerManage=com.shinemo.mpush.ps.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-test/src/main/resources/config.properties b/mpush-test/src/main/resources/config.properties new file mode 100644 index 00000000..04d27b9e --- /dev/null +++ b/mpush-test/src/main/resources/config.properties @@ -0,0 +1,33 @@ +## +max_packet_size = 10240 +## +compress_limit = 10240 +## +min_heartbeat = 10000 +## +max_heartbeat = 1800000 +## +max_hb_timeout_times = 2 +## +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +## +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +## +gateway_server_port = 4000 +## +connection_server_port = 3000 +## +aes_key_length = 16 +## +ras_key_length = 1024 +## +session_expired_time = 86400 + +## zk +zk_ip = 10.1.80.57:2181 +zk_namespace = mpush +zk_digest = shinemoIpo + +## redis redisԷֺŷָÿڵredisԶŷָ +##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo +redis_group = 10.1.80.57:6379:shinemoIpo diff --git a/mpush-test/src/main/resources/logback.xml b/mpush-test/src/main/resources/logback.xml new file mode 100644 index 00000000..20979b40 --- /dev/null +++ b/mpush-test/src/main/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-test/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-test/src/test/java/com/shinemo/mpush/AppTest.java new file mode 100644 index 00000000..50fff9a1 --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/AppTest.java @@ -0,0 +1,38 @@ +package com.shinemo.mpush; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java b/mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java new file mode 100644 index 00000000..abaf4ca2 --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java @@ -0,0 +1,16 @@ +package com.shinemo.mpush.ps; + +import org.junit.Test; + +import com.shinemo.mpush.tools.config.ConfigCenter; + +public class ConfigCenterTest { + + @Test + public void test(){ + + System.out.println(ConfigCenter.holder.zkIp()); + + } + +} From c86ea9633365b708b9aa33834e5e03eb29b063b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 11:38:32 +0800 Subject: [PATCH 252/890] =?UTF-8?q?=E6=8B=86=E5=87=BAtest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-daily.properties | 4 ++-- mpush-test/src/main/resources/config.properties | 6 ++---- mpush-test/src/main/resources/logback.xml | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 76c58b66..7653074c 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,5 +1,5 @@ #日志根目录 log.home=/tmp/logs/mpush loglevel=warn -zk_ip = 10.1.80.57:2181 -redis_group = 10.1.80.57:6379:shinemoIpo +zk_ip = 127.0.0.1:2181 +redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/mpush-test/src/main/resources/config.properties b/mpush-test/src/main/resources/config.properties index 04d27b9e..a3158cec 100644 --- a/mpush-test/src/main/resources/config.properties +++ b/mpush-test/src/main/resources/config.properties @@ -23,11 +23,9 @@ ras_key_length = 1024 ## session_expired_time = 86400 -## zk -zk_ip = 10.1.80.57:2181 +zk_ip = 127.0.0.1:2181 zk_namespace = mpush zk_digest = shinemoIpo -## redis redisԷֺŷָÿڵredisԶŷָ ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 10.1.80.57:6379:shinemoIpo +redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/mpush-test/src/main/resources/logback.xml b/mpush-test/src/main/resources/logback.xml index 20979b40..a3f29d83 100644 --- a/mpush-test/src/main/resources/logback.xml +++ b/mpush-test/src/main/resources/logback.xml @@ -24,7 +24,7 @@ - + From dedce9e17937224777f56bb9cfd13bd2e869649a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 11:54:23 +0800 Subject: [PATCH 253/890] heart beat log --- .../mpush/api/connection/Connection.java | 3 +++ .../mpush/core/handler/BindUserHandler.java | 10 +++----- .../mpush/core/handler/HandshakeHandler.java | 3 ++- .../mpush/core/handler/HeartBeatHandler.java | 5 +++- mpush-cs/src/main/resources/logback.xml | 23 +++++++++++++++++++ .../java/com/shinemo/mpush/log/LogType.java | 2 +- .../com/shinemo/mpush/log/LoggerManage.java | 4 ++++ .../netty/connection/NettyConnection.java | 5 ++++ 8 files changed, 45 insertions(+), 10 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java index 6b5b86d8..1ae118e4 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java @@ -40,4 +40,7 @@ public interface Connection { void resetHbTimes(); long getLastReadTime(); + + Channel getChannel(); + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 2b6ff9db..5e9dbd43 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -12,14 +12,10 @@ import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Created by ohun on 2015/12/23. */ public final class BindUserHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(BindUserHandler.class); @Override public BindUserMessage decode(Packet packet, Connection connection) { @@ -30,7 +26,7 @@ public BindUserMessage decode(Packet packet, Connection connection) { public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - LOGGER.error("bind user failure invalid param, session={}", message.getConnection().getSessionContext()); + LoggerManage.log(LogType.CONNECTION, "bind user failure for invalid param, session=%s", message.getConnection().getSessionContext()); return; } //1.绑定用户时先看下是否握手成功 @@ -45,11 +41,11 @@ public void handle(BindUserMessage message) { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.INSTANCE.unRegister(message.userId); ErrorMessage.from(message).setReason("bind failed").close(); - LOGGER.error("bind user failure, register router failure, userId={}, session={}", message.userId, context); + LoggerManage.log(LogType.CONNECTION, "bind user failure, userId=%s, session=%s", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - LOGGER.error("bind user failure not handshake, userId={}, session={}", message.userId, context); + LoggerManage.log(LogType.CONNECTION, "bind user failure for not handshake, userId=%s, session=%s", message.userId, context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index dd46b9b4..0a065c18 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -45,13 +45,14 @@ public void handle(HandshakeMessage message) { || iv.length != CipherBox.INSTANCE.getAesKeyLength() || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); + LoggerManage.log(LogType.CONNECTION, "client handshake false:%s", message.getConnection()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - ErrorMessage.from(message).setReason("Repeat handshake").close(); + LoggerManage.log(LogType.CONNECTION, "client handshake false for repeat handshake:%s", message.getConnection().getSessionContext()); return; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 3212f4ef..d2b030cf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -3,6 +3,8 @@ import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; /** * Created by ohun on 2015/12/22. @@ -10,6 +12,7 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { - connection.send(packet);//ping -> pong + connection.send(packet);//ping -> pong + LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:%s", connection.getChannel()); } } diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 19c2d42d..49b45d32 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -87,6 +87,29 @@ + + + + + ${log.home}/mpush-heartbeat.log + + info + + true + + ${log.home}/mpush-heartbeat.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java index 29a352ce..0e155a31 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.log; public enum LogType { - CONNECTION,PUSH + CONNECTION,PUSH,HEARTBEAT } diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index 132b53ac..ca7eb145 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -7,6 +7,8 @@ public class LoggerManage { private static final Logger connectionLog = LoggerFactory.getLogger("connectionLog"); private static final Logger pushLog = LoggerFactory.getLogger("pushLog"); + private static final Logger heartBeatLog = LoggerFactory.getLogger("heartBeatLog"); + public static void log(LogType type,String format,Object... arguments){ String ret = String.format(format, arguments); @@ -14,6 +16,8 @@ public static void log(LogType type,String format,Object... arguments){ connectionLog.info(ret); }else if(type.equals(LogType.PUSH)){ pushLog.info(ret); + }else if(type.equals(LogType.HEARTBEAT)){ + heartBeatLog.info(ret); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index fbed0d84..3b7c4dff 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -128,6 +128,11 @@ public String toString() { return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes + "]"; } + + @Override + public Channel getChannel() { + return channel; + } } From 9c92747a2d16c6e456a1061198e3d1c2afc04754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 12:53:27 +0800 Subject: [PATCH 254/890] heart beat log --- .../java/com/shinemo/mpush/core/handler/HeartBeatHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index d2b030cf..35cd5663 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -13,6 +13,6 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong - LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:%s", connection.getChannel()); + LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); } } From 2472d110ccfaa87ee4a17c12ead917b6de6bc0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 14:14:30 +0800 Subject: [PATCH 255/890] =?UTF-8?q?zk=20redis=20log=20=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/AbstractEventContainer.java | 9 +++ .../common/router/RemoteRouterManager.java | 3 +- .../mpush/core/router/LocalRouterManager.java | 19 +++--- .../mpush/core/router/RouterCenter.java | 6 +- .../core/router/RouterChangeListener.java | 15 +++-- mpush-cs/src/main/resources/logback.xml | 46 +++++++++++++++ .../java/com/shinemo/mpush/log/LogType.java | 2 +- .../com/shinemo/mpush/log/LoggerManage.java | 48 +++++++++++++-- .../shinemo/mpush/tools/redis/RedisUtil.java | 59 ++++++++++--------- .../jedis/services/JedisRegisterManager.java | 6 +- .../mpush/tools/redis/pubsub/Subscriber.java | 27 +++++---- .../curator/services/ZkRegisterManager.java | 35 +++++------ .../tools/zk/listener/DataChangeListener.java | 11 ++-- 13 files changed, 192 insertions(+), 94 deletions(-) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java new file mode 100644 index 00000000..ff11636c --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java @@ -0,0 +1,9 @@ +package com.shinemo.mpush.common; + +public abstract class AbstractEventContainer { + + public AbstractEventContainer() { + EventBus.INSTANCE.register(this); + } + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index ba8e7779..3933ccdc 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -13,6 +13,7 @@ public class RemoteRouterManager implements RouterManager { @Override public RemoteRouter register(String userId, RemoteRouter router) { + LOGGER.info("register remote router success userId={}, router={}", userId, router); RemoteRouter old = RedisManage.get(userId, RemoteRouter.class); if (old != null) { RedisManage.del(userId); @@ -24,7 +25,7 @@ public RemoteRouter register(String userId, RemoteRouter router) { @Override public boolean unRegister(String userId) { RedisManage.del(userId); - LOGGER.info("unRegister local router success userId={}", userId); + LOGGER.info("unRegister remote router success userId={}", userId); return true; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 44c69f19..c53c674a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -3,8 +3,10 @@ import com.google.common.eventbus.Subscribe; import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.router.RouterManager; -import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.AbstractEventContainer; + import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,7 +15,7 @@ /** * Created by ohun on 2015/12/23. */ -public final class LocalRouterManager implements RouterManager { +public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); /** @@ -26,13 +28,9 @@ public final class LocalRouterManager implements RouterManager { */ private final Map connIdUserIds = new ConcurrentHashMapV8<>(); - public LocalRouterManager() { - EventBus.INSTANCE.register(this); - } - @Override public LocalRouter register(String userId, LocalRouter router) { - LOGGER.debug("register local router success userId={}, router={}", userId, router); + LOGGER.info("register local router success userId={}, router={}", userId, router); connIdUserIds.put(router.getRouteValue().getId(), userId); return routers.put(userId, router); } @@ -50,7 +48,7 @@ public boolean unRegister(String userId) { @Override public LocalRouter lookup(String userId) { LocalRouter router = routers.get(userId); - LOGGER.debug("lookup local router userId={}, router={}", userId, router); + LOGGER.info("lookup local router userId={}, router={}", userId, router); return router; } @@ -72,10 +70,11 @@ void onConnectionCloseEvent(ConnectionCloseEvent event) { //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 if (id.equals(router.getRouteValue().getId())) { - //3.删除路由 routers.remove(userId); - LOGGER.warn("clean disconnected local route, userId={}, route={}", userId, router); + LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); + }else{ //如果不相等,则log一下 + LOGGER.info("clean disconnected local route, not clean:userId={}, route={}",userId,router); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index e82f59ab..1dc14348 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -43,17 +43,17 @@ public boolean register(String userId, Connection connection) { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); } catch (Exception e) { - LOGGER.warn("register router ex, userId={}, connection={}", userId, connection, e); + LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); } if (oldLocalRouter != null) { EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); - LOGGER.warn("register router success, find old local router={}, userId={}", oldLocalRouter, userId); + LOGGER.info("register router success, find old local router={}, userId={}", oldLocalRouter, userId); } if (oldRemoteRouter != null) { EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldRemoteRouter)); - LOGGER.warn("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); + LOGGER.info("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); } return true; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index d56d7c19..1d027f57 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -6,40 +6,45 @@ import com.shinemo.mpush.api.event.RouterChangeEvent; import com.shinemo.mpush.api.router.ClientLocation; import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.AbstractEventContainer; import com.shinemo.mpush.common.message.KickUserMessage; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.listener.MessageListener; import com.shinemo.mpush.tools.redis.manage.RedisManage; + import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by ohun on 2016/1/4. */ -public final class RouterChangeListener implements MessageListener { +public final class RouterChangeListener extends AbstractEventContainer implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); public static final String KICK_CHANNEL_ = "/mpush/kick/"; private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); public RouterChangeListener() { - EventBus.INSTANCE.register(this); RedisManage.subscribe(this, getKickChannel()); } public String getKickChannel() { return kick_channel; } + + public String getKickChannel(String remoteIp){ + return KICK_CHANNEL_ + remoteIp; + } @Subscribe void onRouteChangeEvent(RouterChangeEvent event) { String userId = event.userId; Router r = event.router; - if (r.getRouteType() == Router.RouterType.LOCAL) { + if (r.getRouteType().equals(Router.RouterType.LOCAL)) { kickLocal(userId, (LocalRouter) r); } else { kickRemote(userId, (RemoteRouter) r); @@ -95,7 +100,7 @@ public void kickRemote(String userId, RemoteRouter router) { msg.deviceId = location.getDeviceId(); msg.targetServer = location.getHost(); msg.userId = userId; - RedisManage.publish(getKickChannel(), msg); + RedisManage.publish(getKickChannel(msg.targetServer), msg); } /** diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 49b45d32..f3aef748 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -110,6 +110,52 @@ + + + + + ${log.home}/mpush-redis.log + + info + + true + + ${log.home}/mpush-redis.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + + + + ${log.home}/mpush-zk.log + + info + + true + + ${log.home}/mpush-zk.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java index 0e155a31..2618706f 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.log; public enum LogType { - CONNECTION,PUSH,HEARTBEAT + CONNECTION,PUSH,HEARTBEAT,REDIS,ZK } diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index ca7eb145..9efb6b1f 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -1,5 +1,8 @@ package com.shinemo.mpush.log; +import java.util.HashMap; +import java.util.Map; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,16 +11,49 @@ public class LoggerManage { private static final Logger connectionLog = LoggerFactory.getLogger("connectionLog"); private static final Logger pushLog = LoggerFactory.getLogger("pushLog"); private static final Logger heartBeatLog = LoggerFactory.getLogger("heartBeatLog"); + private static final Logger redisLog = LoggerFactory.getLogger("redisLog"); + private static final Logger zkLog = LoggerFactory.getLogger("zkLog"); + + private static final Logger defaultLog = LoggerFactory.getLogger(LoggerManage.class); + + private static final Map map = new HashMap<>(); + + static{ + map.put(LogType.CONNECTION, connectionLog); + map.put(LogType.PUSH, pushLog); + map.put(LogType.HEARTBEAT, heartBeatLog); + map.put(LogType.REDIS, redisLog); + map.put(LogType.ZK, zkLog); + } public static void log(LogType type,String format,Object... arguments){ + log(type, null, format, arguments); + } + + public static Logger getLog(LogType type){ + Logger log = map.get(type); + if(log == null){ + log = defaultLog; + } + return log; + } + + public static void log(LogType type,Throwable ex,String format,Object... arguments){ String ret = String.format(format, arguments); - if(type.equals(LogType.CONNECTION)){ - connectionLog.info(ret); - }else if(type.equals(LogType.PUSH)){ - pushLog.info(ret); - }else if(type.equals(LogType.HEARTBEAT)){ - heartBeatLog.info(ret); + Logger log = map.get(type); + if(ex!=null){ + if(log!=null){ + log.info(ret,ex); + }else{ + defaultLog.info(ret,ex); + } + }else{ + if(log!=null){ + log.info(ret); + }else{ + defaultLog.info(ret); + } } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 014ff0e2..35dcb024 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -5,11 +5,9 @@ import java.util.Map; import java.util.Set; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.shinemo.mpush.tools.Constants; @@ -20,9 +18,7 @@ import redis.clients.jedis.JedisPubSub; public class RedisUtil { - - private static final Logger log = LoggerFactory.getLogger(RedisUtil.class); - + private static Map holder = Maps.newConcurrentMap(); public static Jedis getClient(RedisNode node) { @@ -54,7 +50,7 @@ public static T get(RedisNode node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - log.warn("redis get exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis get exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -85,16 +81,19 @@ public static void set(List nodeList, String key, T value, Intege * @param time seconds */ public static void set(List nodeList, String key, String value, Integer time) { + if(time == null){ + time = -1; + } for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.set(key, value); - if (time != null) { + if (time>0) { jedis.expire(key, time); } } catch (Exception e) { - log.warn("redis set exception:" + key + "," + value + "," + time, e); + LoggerManage.log(LogType.REDIS, e, "redis set exception:%s,%s,%s,%s",key,value,time,node); } finally { // 返还到连接池 close(jedis); @@ -110,7 +109,7 @@ public static void del(List nodeList, String key) { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - log.warn("redis del exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis del exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -131,7 +130,7 @@ public static void hset(List nodeList, String namespace, String key, jedis = getClient(node); jedis.hset(namespace, key, value); } catch (Exception e) { - log.warn("redis hset exception:" + namespace + "," + key + "," + value, e); + LoggerManage.log(LogType.REDIS, e, "redis hset exception:%s,%s,%s,%s",namespace,key,value,node); } finally { // 返还到连接池 close(jedis); @@ -151,7 +150,7 @@ public static T hget(RedisNode node, String namespace, String key, Class jedis = getClient(node); value = jedis.hget(namespace, key); } catch (Exception e) { - log.warn("redis hget exception:" + namespace + "," + key, e); + LoggerManage.log(LogType.REDIS, e, "redis hget exception:%s,%s",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -168,7 +167,7 @@ public static void hdel(List nodeList, String namespace, String key) jedis = getClient(node); jedis.hdel(namespace, key); } catch (Exception e) { - log.warn("redis hdel exception:" + namespace + "," + key, e); + LoggerManage.log(LogType.REDIS, e, "redis hdel exception:%s,%s,%s",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -183,7 +182,7 @@ public static Map hgetAll(RedisNode node, String namespace) { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - log.warn("redis hgetAll exception:" + namespace, e); + LoggerManage.log(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); } finally { // 返还到连接池 close(jedis); @@ -198,7 +197,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - log.warn("redis hgetAll exception:" + namespace, e); + LoggerManage.log(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); } finally { // 返还到连接池 close(jedis); @@ -233,7 +232,7 @@ public static Set hkeys(RedisNode node, String key) { jedis = getClient(node); result = jedis.hkeys(key); } catch (Exception e) { - log.warn("redis hkeys exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis hkeys exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -259,7 +258,7 @@ public static List hmget(RedisNode node, String namespace, Class clazz jedis = getClient(node); value = jedis.hmget(namespace, key); } catch (Exception e) { - log.warn("redis lpopList exception:" + namespace + "," + key, e); + LoggerManage.log(LogType.REDIS, e, "redis hmget exception:%s,%s,%s",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -286,17 +285,20 @@ public static List hmget(RedisNode node, String namespace, Class clazz */ public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + if(time == null){ + time = -1; + } for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.hmset(namespace, hash); - if (time != null) { + if (time>0) { jedis.expire(namespace, time); } } catch (Exception e) { - log.warn("redis hmset exception:" + namespace, e); + LoggerManage.log(LogType.REDIS, e, "redis hmset exception:%s,%s,%s",namespace,time,node); } finally { // 返还到连接池 close(jedis); @@ -323,7 +325,7 @@ public static void lpush(List nodeList, String key, String value) { jedis = getClient(node); jedis.lpush(key, value); } catch (Exception e) { - log.warn("redis lpush exception:" + key + "," + value, e); + LoggerManage.log(LogType.REDIS, e, "redis lpush exception:%s,%s,%s",key,value,node); } finally { // 返还到连接池 close(jedis); @@ -349,7 +351,7 @@ public static void rpush(List nodeList, String key, String value) { jedis = getClient(node); jedis.rpush(key, value); } catch (Exception e) { - log.warn("redis rpush exception:" + key + "," + value, e); + LoggerManage.log(LogType.REDIS, e, "redis rpush exception:%s,%s,%s",key,value,node); } finally { // 返还到连接池 close(jedis); @@ -374,7 +376,7 @@ public static T lpop(List nodeList, String key, Class clazz) { vaule = jedis.lpop(key); retValue = vaule; } catch (Exception e) { - log.warn("redis lpop exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis lpop exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -397,7 +399,7 @@ public static T rpop(List nodeList, String key, Class clazz) { vaule = jedis.rpop(key); retValue = vaule; } catch (Exception e) { - log.warn("redis lpop exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis rpop exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -419,7 +421,7 @@ public static List lrange(RedisNode node, String key, int start, int end, jedis = getClient(node); value = jedis.lrange(key, start, end); } catch (Exception e) { - log.warn("redis lrange exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis lrange exception:%s,%s,%s,%s",key,start,end,node); } finally { // 返还到连接池 close(jedis); @@ -446,7 +448,7 @@ public static long llen(RedisNode node, String key) { jedis = getClient(node); jedis.llen(key); } catch (Exception e) { - log.warn("redis llen exception:" + key, e); + LoggerManage.log(LogType.REDIS, e, "redis llen exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -468,13 +470,14 @@ public static void publish(RedisNode node, String channel, T message) { jedis = getClient(node); jedis.publish(channel, value); } catch (Exception e) { - log.warn("redis pub exception:" + channel + "," + value, e); + LoggerManage.log(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,node,channel); } finally { // 返还到连接池 close(jedis); } } + //TODO 使用别的线程 public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { for (final RedisNode node : nodeList) { Runnable runnable = new Runnable() { @@ -494,7 +497,7 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann jedis = getClient(node); jedis.subscribe(pubsub, channel); } catch (Exception e) { - log.warn("redis subscribe exception:" + channel, e); + LoggerManage.log(LogType.REDIS, e, "redis subscribe exception:%s,%s",node,channel); } finally { // 返还到连接池 close(jedis); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java index b20e6ebd..91bc88c0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -9,6 +9,9 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.Lists; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.RedisGroup; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.redis.RedisRegister; @@ -25,6 +28,7 @@ public class JedisRegisterManager implements RedisRegister{ @Override public void init(List group) { if (group == null || group.isEmpty()) { + LoggerManage.log(LogType.REDIS, "init redis client error, redis server is none."); throw new RuntimeException("init redis client error, redis server is none."); } groups = group; @@ -39,7 +43,7 @@ public List getGroupList() { private void printGroupList() { for (RedisGroup app : groups) { - LOGGER.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); + LoggerManage.log(LogType.REDIS,Jsons.toJson(app)); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 7d3b5099..942839f8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -1,8 +1,10 @@ package com.shinemo.mpush.tools.redis.pubsub; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.redis.listener.MessageListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import org.apache.commons.lang3.builder.ToStringBuilder; import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; @@ -10,61 +12,60 @@ public class Subscriber extends JedisPubSub { - private static final Logger log = LoggerFactory.getLogger(Subscriber.class); - private ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; - + @Override public void onMessage(String channel, String message) { - log.warn("onMessage channel:" + channel + "," + message); + LoggerManage.log(LogType.REDIS, "onMessage:%s,%s", channel,message); dispatcher.onMessage(channel, message); super.onMessage(channel, message); } @Override public void onPMessage(String pattern, String channel, String message) { - log.warn("onPMessage:" + pattern + "," + channel + "," + message); + LoggerManage.log(LogType.REDIS, "onPMessage:%s,%s,%s",pattern,channel,message); super.onPMessage(pattern, channel, message); } @Override public void onPSubscribe(String pattern, int subscribedChannels) { - log.warn("onPSubscribe:" + pattern + "," + subscribedChannels); + LoggerManage.log(LogType.REDIS, "onPSubscribe:%s,%s",pattern,subscribedChannels); super.onPSubscribe(pattern, subscribedChannels); } @Override public void onPUnsubscribe(String pattern, int subscribedChannels) { - log.warn("onPUnsubscribe:" + pattern + "," + subscribedChannels); + LoggerManage.log(LogType.REDIS, "onPUnsubscribe:%s,%s",pattern,subscribedChannels); super.onPUnsubscribe(pattern, subscribedChannels); } @Override public void onSubscribe(String channel, int subscribedChannels) { - log.warn("onSubscribe:" + channel + "," + subscribedChannels); + LoggerManage.log(LogType.REDIS, "onSubscribe:%s,%s",channel,subscribedChannels); super.onSubscribe(channel, subscribedChannels); } @Override public void onUnsubscribe(String channel, int subscribedChannels) { - log.warn("onUnsubscribe:" + channel + "," + subscribedChannels); + LoggerManage.log(LogType.REDIS, "onUnsubscribe:%s,%s",channel,subscribedChannels); super.onUnsubscribe(channel, subscribedChannels); } @Override public void unsubscribe() { - log.warn("unsubscribe:"); + LoggerManage.log(LogType.REDIS, "unsubscribe"); super.unsubscribe(); } @Override public void unsubscribe(String... channels) { - log.warn("unsubscribe:" + channels); + LoggerManage.log(LogType.REDIS, "unsubscribe:%s",ToStringBuilder.reflectionToString(channels)); super.unsubscribe(channels); } public void subscribe(MessageListener listener, String... channels) { + LoggerManage.log(LogType.REDIS, "subscribe:%s",ToStringBuilder.reflectionToString(channels)); for (String channel : channels) { dispatcher.subscribe(channel, listener); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 95097964..2d31ef3b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -6,8 +6,6 @@ import java.util.List; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; @@ -21,9 +19,10 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZkConfig; @@ -32,8 +31,6 @@ public class ZkRegisterManager implements ZkRegister { - private static final Logger LOGGER = LoggerFactory.getLogger(ZkRegisterManager.class); - private ZkConfig zkConfig; private CuratorFramework client; @@ -55,7 +52,7 @@ public CuratorFramework getClient() { @Override public void init() { zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); - LOGGER.warn("start registry zk, server lists is: {}.", zkConfig.getIpLists()); + LoggerManage.log(LogType.ZK, "start registry zk, server lists is:%s", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); if (zkConfig.getConnectionTimeout() > 0) { @@ -85,7 +82,7 @@ public List getAclForPath(final String path) { cacheData(); registerConnectionLostListener(); } catch (final Exception ex) { - LOGGER.error("zk connection error" + ToStringBuilder.reflectionToString(zkConfig, ToStringStyle.DEFAULT_STYLE)); + LoggerManage.log(LogType.ZK,ex,"zk connection error:%s", Jsons.toJson(zkConfig)); } } @@ -97,9 +94,9 @@ private void registerConnectionLostListener() { @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { if (ConnectionState.LOST == newState) { - LOGGER.warn(MPushUtil.getInetAddress() + ", lost connection"); + LoggerManage.log(LogType.ZK, "%s lost connection", MPushUtil.getInetAddress()); } else if (ConnectionState.RECONNECTED == newState) { - LOGGER.warn(MPushUtil.getInetAddress() + ", reconnected"); + LoggerManage.log(LogType.ZK, "%s reconnected", MPushUtil.getInetAddress()); } } }); @@ -159,7 +156,7 @@ public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Charset.forName("UTF-8")); } catch (final Exception ex) { - LOGGER.error("getDirectly" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); + LoggerManage.log(LogType.ZK, ex, "getFromRemote:%s", key); return null; } } @@ -183,7 +180,7 @@ public int compare(final String o1, final String o2) { }); return result; } catch (final Exception ex) { - LOGGER.error("getChildrenKeys" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); + LoggerManage.log(LogType.ZK, ex, "getChildrenKeys:%s", key); return Collections.emptyList(); } } @@ -199,7 +196,7 @@ public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); } catch (final Exception ex) { - LOGGER.error("isExisted" + ToStringBuilder.reflectionToString(key, ToStringStyle.DEFAULT_STYLE), ex); + LoggerManage.log(LogType.ZK, ex, "isExisted:%s", key); return false; } } @@ -219,7 +216,7 @@ public void registerPersist(final String key, final String value) { update(key, value); } } catch (final Exception ex) { - LOGGER.error("persist" + key + "," + value, ex); + LoggerManage.log(LogType.ZK, ex, "persist:%s,%s", key,value); } } @@ -234,7 +231,7 @@ public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); } catch (final Exception ex) { - LOGGER.error("update" + key + "," + value, ex); + LoggerManage.log(LogType.ZK, ex, "update:%s,%s", key,value); } } @@ -252,7 +249,7 @@ public void registerEphemeral(final String key, final String value) { } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); } catch (final Exception ex) { - LOGGER.error("persistEphemeral" + key + "," + value, ex); + LoggerManage.log(LogType.ZK, ex, "persistEphemeral:%s,%s", key,value); } } @@ -266,7 +263,7 @@ public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); } catch (final Exception ex) { - LOGGER.error("persistEphemeralSequential" + key, ex); + LoggerManage.log(LogType.ZK, ex, "persistEphemeralSequential:%s,%s", key,value); } } @@ -280,7 +277,7 @@ public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { - LOGGER.error("persistEphemeralSequential" + key, ex); + LoggerManage.log(LogType.ZK, ex, "persistEphemeralSequential:%s", key); } } @@ -294,7 +291,7 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - LOGGER.error("remove" + key, ex); + LoggerManage.log(LogType.ZK, ex, "remove:%s", key); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java index cd71dd02..0fbf058f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java @@ -3,22 +3,19 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; public abstract class DataChangeListener implements TreeCacheListener{ - private static final Logger log = LoggerFactory.getLogger(DataChangeListener.class); - @Override public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { String path = null == event.getData() ? "" : event.getData().getPath(); if (path.isEmpty()) { return; } - - log.warn("DataChangeListener:"+path+",listenerPath:"+listenerPath()); - + LoggerManage.log(LogType.ZK, "DataChangeListener:%s,%s", path,listenerPath()); if(path.startsWith(listenerPath())){ dataChanged(client, event, path); } From 652a67d08824b782bc248c8525e04bed1e617f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 14:51:43 +0800 Subject: [PATCH 256/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0log=20level?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/server/ServerChannelHandler.java | 7 +- .../java/com/shinemo/mpush/log/LogLevel.java | 5 ++ .../com/shinemo/mpush/log/LoggerManage.java | 71 +++++++++++++++---- .../shinemo/mpush/tools/redis/RedisUtil.java | 38 +++++----- .../mpush/tools/redis/pubsub/Subscriber.java | 7 +- .../curator/services/ZkRegisterManager.java | 20 +++--- 6 files changed, 100 insertions(+), 48 deletions(-) create mode 100644 mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 8b1d3e0c..15679806 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -5,6 +5,7 @@ import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.log.LogLevel; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.connection.NettyConnection; @@ -47,13 +48,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LoggerManage.log(security, "client exceptionCaught channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO,"client exceptionCaught channel=%s", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, "client connect channel=%s", ctx.channel()); + LoggerManage.log(security,LogLevel.INFO,"client connect channel=%s", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -61,7 +62,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, "client disconnect channel=%s", ctx.channel()); + LoggerManage.log(security,LogLevel.INFO,"client disconnect channel=%s", ctx.channel()); connectionManager.remove(ctx.channel()); } } \ No newline at end of file diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java new file mode 100644 index 00000000..f93ad51e --- /dev/null +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java @@ -0,0 +1,5 @@ +package com.shinemo.mpush.log; + +public enum LogLevel { + DEBUG,WARN,INFO,ERROR +} diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index 9efb6b1f..a21ade57 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -28,7 +28,23 @@ public class LoggerManage { public static void log(LogType type,String format,Object... arguments){ - log(type, null, format, arguments); + warn(type, format, arguments); + } + + public static void warn(LogType type,String format,Object... arguments){ + log(type, LogLevel.WARN, null, format, arguments); + } + + public static void info(LogType type,String format,Object... arguments){ + log(type, LogLevel.INFO, null, format, arguments); + } + + public static void error(LogType type,String format,Object... arguments){ + log(type, LogLevel.ERROR, null, format, arguments); + } + + public static void debug(LogType type,String format,Object... arguments){ + log(type, LogLevel.DEBUG, null, format, arguments); } public static Logger getLog(LogType type){ @@ -39,39 +55,70 @@ public static Logger getLog(LogType type){ return log; } - public static void log(LogType type,Throwable ex,String format,Object... arguments){ + public static void execption(LogType type,Throwable ex,String format,Object... arguments){ + log(type, LogLevel.ERROR, ex, format, arguments); + } + + /** + * 默认 level 为warn + * @param type + * @param level + * @param ex + * @param format + * @param arguments + */ + public static void log(LogType type,LogLevel level,Throwable ex,String format,Object... arguments){ String ret = String.format(format, arguments); + if(level == null){ + level = LogLevel.WARN; + } Logger log = map.get(type); if(ex!=null){ if(log!=null){ - log.info(ret,ex); + if(level.equals(LogLevel.WARN)){ + log.warn(ret,ex); + }else if(level.equals(LogLevel.INFO)){ + log.info(ret,ex); + }else if(level.equals(LogLevel.DEBUG)){ + log.debug(ret,ex); + }else if(level.equals(LogLevel.ERROR)){ + log.error(ret,ex); + } }else{ - defaultLog.info(ret,ex); + defaultLog.warn(ret,ex); } }else{ if(log!=null){ - log.info(ret); + if(level.equals(LogLevel.WARN)){ + log.warn(ret); + }else if(level.equals(LogLevel.INFO)){ + log.info(ret); + }else if(level.equals(LogLevel.DEBUG)){ + log.debug(ret); + }else if(level.equals(LogLevel.ERROR)){ + log.error(ret); + } }else{ - defaultLog.info(ret); + defaultLog.warn(ret); } } } + /** * security的log 为 connectionLog的log * @param security + * @param level * @param format * @param arguments */ - public static void log(boolean security,String format,Object... arguments){ - String ret = String.format(format, arguments); + public static void log(boolean security,LogLevel level,String format,Object... arguments){ if(security){ - connectionLog.info(ret); + log(LogType.CONNECTION, level, null, format, arguments); }else{ - pushLog.info(ret); + log(LogType.PUSH, level, null, format, arguments); } - } - + } public static void main(String[] args) { String format = "client connect channel=%s"; System.out.println(String.format(format, "hi")); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 35dcb024..47753fb4 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -50,7 +50,7 @@ public static T get(RedisNode node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis get exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis get exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -93,7 +93,7 @@ public static void set(List nodeList, String key, String value, Integ jedis.expire(key, time); } } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis set exception:%s,%s,%s,%s",key,value,time,node); + LoggerManage.execption(LogType.REDIS, e, "redis set exception:%s,%s,%s,%s",key,value,time,node); } finally { // 返还到连接池 close(jedis); @@ -109,7 +109,7 @@ public static void del(List nodeList, String key) { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis del exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis del exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -130,7 +130,7 @@ public static void hset(List nodeList, String namespace, String key, jedis = getClient(node); jedis.hset(namespace, key, value); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hset exception:%s,%s,%s,%s",namespace,key,value,node); + LoggerManage.execption(LogType.REDIS, e, "redis hset exception:%s,%s,%s,%s",namespace,key,value,node); } finally { // 返还到连接池 close(jedis); @@ -150,7 +150,7 @@ public static T hget(RedisNode node, String namespace, String key, Class jedis = getClient(node); value = jedis.hget(namespace, key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hget exception:%s,%s",namespace,key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hget exception:%s,%s",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -167,7 +167,7 @@ public static void hdel(List nodeList, String namespace, String key) jedis = getClient(node); jedis.hdel(namespace, key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hdel exception:%s,%s,%s",namespace,key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hdel exception:%s,%s,%s",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -182,7 +182,7 @@ public static Map hgetAll(RedisNode node, String namespace) { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); + LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); } finally { // 返还到连接池 close(jedis); @@ -197,7 +197,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); + LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); } finally { // 返还到连接池 close(jedis); @@ -232,7 +232,7 @@ public static Set hkeys(RedisNode node, String key) { jedis = getClient(node); result = jedis.hkeys(key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hkeys exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hkeys exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -258,7 +258,7 @@ public static List hmget(RedisNode node, String namespace, Class clazz jedis = getClient(node); value = jedis.hmget(namespace, key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis hmget exception:%s,%s,%s",namespace,key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hmget exception:%s,%s,%s",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -298,7 +298,7 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String key, String value) { jedis = getClient(node); jedis.lpush(key, value); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis lpush exception:%s,%s,%s",key,value,node); + LoggerManage.execption(LogType.REDIS, e, "redis lpush exception:%s,%s,%s",key,value,node); } finally { // 返还到连接池 close(jedis); @@ -351,7 +351,7 @@ public static void rpush(List nodeList, String key, String value) { jedis = getClient(node); jedis.rpush(key, value); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis rpush exception:%s,%s,%s",key,value,node); + LoggerManage.execption(LogType.REDIS, e, "redis rpush exception:%s,%s,%s",key,value,node); } finally { // 返还到连接池 close(jedis); @@ -376,7 +376,7 @@ public static T lpop(List nodeList, String key, Class clazz) { vaule = jedis.lpop(key); retValue = vaule; } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis lpop exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis lpop exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -399,7 +399,7 @@ public static T rpop(List nodeList, String key, Class clazz) { vaule = jedis.rpop(key); retValue = vaule; } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis rpop exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis rpop exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -421,7 +421,7 @@ public static List lrange(RedisNode node, String key, int start, int end, jedis = getClient(node); value = jedis.lrange(key, start, end); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis lrange exception:%s,%s,%s,%s",key,start,end,node); + LoggerManage.execption(LogType.REDIS, e, "redis lrange exception:%s,%s,%s,%s",key,start,end,node); } finally { // 返还到连接池 close(jedis); @@ -448,7 +448,7 @@ public static long llen(RedisNode node, String key) { jedis = getClient(node); jedis.llen(key); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis llen exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis llen exception:%s,%s",key,node); } finally { // 返还到连接池 close(jedis); @@ -470,7 +470,7 @@ public static void publish(RedisNode node, String channel, T message) { jedis = getClient(node); jedis.publish(channel, value); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,node,channel); + LoggerManage.execption(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,node,channel); } finally { // 返还到连接池 close(jedis); @@ -497,7 +497,7 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann jedis = getClient(node); jedis.subscribe(pubsub, channel); } catch (Exception e) { - LoggerManage.log(LogType.REDIS, e, "redis subscribe exception:%s,%s",node,channel); + LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:%s,%s",node,channel); } finally { // 返还到连接池 close(jedis); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 942839f8..502785ec 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -2,10 +2,9 @@ import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.redis.listener.MessageListener; -import org.apache.commons.lang3.builder.ToStringBuilder; - import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; import redis.clients.jedis.JedisPubSub; @@ -60,12 +59,12 @@ public void unsubscribe() { @Override public void unsubscribe(String... channels) { - LoggerManage.log(LogType.REDIS, "unsubscribe:%s",ToStringBuilder.reflectionToString(channels)); + LoggerManage.log(LogType.REDIS, "unsubscribe:%s",Jsons.toJson(channels)); super.unsubscribe(channels); } public void subscribe(MessageListener listener, String... channels) { - LoggerManage.log(LogType.REDIS, "subscribe:%s",ToStringBuilder.reflectionToString(channels)); + LoggerManage.log(LogType.REDIS, "subscribe:%s",Jsons.toJson(channels)); for (String channel : channels) { dispatcher.subscribe(channel, listener); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 2d31ef3b..d7c10659 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -82,7 +82,7 @@ public List getAclForPath(final String path) { cacheData(); registerConnectionLostListener(); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK,ex,"zk connection error:%s", Jsons.toJson(zkConfig)); + LoggerManage.execption(LogType.ZK,ex,"zk connection error:%s", Jsons.toJson(zkConfig)); } } @@ -156,7 +156,7 @@ public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Charset.forName("UTF-8")); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "getFromRemote:%s", key); + LoggerManage.execption(LogType.ZK, ex, "getFromRemote:%s", key); return null; } } @@ -180,7 +180,7 @@ public int compare(final String o1, final String o2) { }); return result; } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "getChildrenKeys:%s", key); + LoggerManage.execption(LogType.ZK, ex, "getChildrenKeys:%s", key); return Collections.emptyList(); } } @@ -196,7 +196,7 @@ public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "isExisted:%s", key); + LoggerManage.execption(LogType.ZK, ex, "isExisted:%s", key); return false; } } @@ -216,7 +216,7 @@ public void registerPersist(final String key, final String value) { update(key, value); } } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "persist:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "persist:%s,%s", key,value); } } @@ -231,7 +231,7 @@ public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "update:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "update:%s,%s", key,value); } } @@ -249,7 +249,7 @@ public void registerEphemeral(final String key, final String value) { } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "persistEphemeral:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:%s,%s", key,value); } } @@ -263,7 +263,7 @@ public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "persistEphemeralSequential:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:%s,%s", key,value); } } @@ -277,7 +277,7 @@ public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "persistEphemeralSequential:%s", key); + LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:%s", key); } } @@ -291,7 +291,7 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - LoggerManage.log(LogType.ZK, ex, "remove:%s", key); + LoggerManage.execption(LogType.ZK, ex, "remove:%s", key); } } From 0f3d3a3712537ebb210239998f60a991a7a091bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 14:54:32 +0800 Subject: [PATCH 257/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java | 2 +- .../mpush/tools/zk/curator/services/ZkRegisterManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index a21ade57..704fd8f9 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -28,7 +28,7 @@ public class LoggerManage { public static void log(LogType type,String format,Object... arguments){ - warn(type, format, arguments); + info(type, format, arguments); } public static void warn(LogType type,String format,Object... arguments){ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index d7c10659..77589607 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -52,7 +52,7 @@ public CuratorFramework getClient() { @Override public void init() { zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); - LoggerManage.log(LogType.ZK, "start registry zk, server lists is:%s", zkConfig.getIpLists()); + LoggerManage.info(LogType.ZK, "start registry zk, server lists is:%s", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); if (zkConfig.getConnectionTimeout() > 0) { From 21ab4d9ce67ff9bfb82dcd5f097e0973e5b22761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 15:03:34 +0800 Subject: [PATCH 258/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9zk=E7=9A=84=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/tools/Constants.java | 3 +++ .../java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java | 2 ++ .../mpush/tools/thread/threadpool/ThreadPoolContext.java | 2 ++ .../mpush/tools/thread/threadpool/ThreadPoolManager.java | 2 ++ .../mpush/tools/zk/curator/services/ZkRegisterManager.java | 3 ++- 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java index 69f2910f..6a2c3126 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java @@ -29,6 +29,9 @@ public interface Constants { int REDIS_POOL_SIZE = 3; int REDIS_THREAD_QUEUE_SIZE = 10000; + + int ZK_POOL_SIZE = 3; + int ZK_THREAD_QUEUE_SIZE = 10000; //zk int ZK_MAX_RETRY = 3; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index d5b6ee5c..8445b850 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -16,6 +16,8 @@ public class ThreadNameSpace { public static final String REDIS = "mp-redis"; + public static final String ZK = "mp-zk"; + public static final String BIZ = "mp-biz"; /** diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index ae1f5835..67a5d1a1 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -24,6 +24,8 @@ public class ThreadPoolContext { public static ThreadPoolContext REDIS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.REDIS, Constants.REDIS_POOL_SIZE,Constants.REDIS_THREAD_QUEUE_SIZE); + public static ThreadPoolContext ZK_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.ZK, Constants.ZK_POOL_SIZE,Constants.ZK_THREAD_QUEUE_SIZE); + public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { this.name = name; this.corePoolSize = corePoolSize; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java index 78eacb6a..e62680df 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -19,12 +19,14 @@ public class ThreadPoolManager { public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); + public static Executor zkExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.ZK_THREAD_POOL); static{ poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); + poolCache.put(ThreadPoolContext.ZK_THREAD_POOL.getName(), zkExecutor); } public static final Map getPool(){ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 77589607..73c947b8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -25,6 +25,7 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; @@ -99,7 +100,7 @@ public void stateChanged(final CuratorFramework client, final ConnectionState ne LoggerManage.log(LogType.ZK, "%s reconnected", MPushUtil.getInetAddress()); } } - }); + },ThreadPoolManager.zkExecutor); } // 本地缓存 From 33ee39519615c4857bf617b303f8447a0a5dfbc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 15:12:37 +0800 Subject: [PATCH 259/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/router/RouterChangeListener.java | 1 + .../mpush/tools/zk/curator/services/ZkRegisterManager.java | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 1d027f57..8f27970b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -96,6 +96,7 @@ public void kickRemote(String userId, RemoteRouter router) { } //2.发送广播 + //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); msg.targetServer = location.getHost(); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index 73c947b8..fa5e010e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -25,7 +25,6 @@ import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; import com.shinemo.mpush.tools.zk.ZkConfig; import com.shinemo.mpush.tools.zk.ZkRegister; import com.shinemo.mpush.tools.zk.listener.DataChangeListener; @@ -62,6 +61,7 @@ public void init() { if (zkConfig.getSessionTimeout() > 0) { builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); } + if (StringUtils.isNoneBlank(zkConfig.getDigest())) { builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { @@ -100,7 +100,7 @@ public void stateChanged(final CuratorFramework client, final ConnectionState ne LoggerManage.log(LogType.ZK, "%s reconnected", MPushUtil.getInetAddress()); } } - },ThreadPoolManager.zkExecutor); + }); } // 本地缓存 From 38e3ac6de9f4fa6cf2811747ccb86c3278f9831e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 15:26:31 +0800 Subject: [PATCH 260/890] add session redis key --- .../java/com/shinemo/mpush/api/RedisKey.java | 17 +++++++++++++++++ .../common/router/RemoteRouterManager.java | 15 ++++++++++----- .../mpush/core/router/KickRemoteMsg.java | 18 +++++++----------- .../mpush/core/session/ReusableSession.java | 2 -- .../core/session/ReusableSessionManager.java | 7 +++++-- 5 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java new file mode 100644 index 00000000..7bb32759 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.api; + +public class RedisKey { + + private static final String USER_PREFIX = "mp_u_"; + + private static final String SESSION_PREFIX = "mp_s_"; + + public static final String getUserKey(String userId){ + return USER_PREFIX+userId; + } + + public static final String getSessionKey(String sessionId){ + return SESSION_PREFIX + sessionId; + } + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java index 3933ccdc..30df5025 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java @@ -1,7 +1,9 @@ package com.shinemo.mpush.common.router; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.router.RouterManager; import com.shinemo.mpush.tools.redis.manage.RedisManage; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,23 +16,26 @@ public class RemoteRouterManager implements RouterManager { @Override public RemoteRouter register(String userId, RemoteRouter router) { LOGGER.info("register remote router success userId={}, router={}", userId, router); - RemoteRouter old = RedisManage.get(userId, RemoteRouter.class); + String key = RedisKey.getUserKey(userId); + RemoteRouter old = RedisManage.get(key, RemoteRouter.class); if (old != null) { - RedisManage.del(userId); + RedisManage.del(key); } - RedisManage.set(userId, router); + RedisManage.set(key, router); return old; } @Override public boolean unRegister(String userId) { - RedisManage.del(userId); + String key = RedisKey.getUserKey(userId); + RedisManage.del(key); LOGGER.info("unRegister remote router success userId={}", userId); return true; } @Override public RemoteRouter lookup(String userId) { - return RedisManage.get(userId, RemoteRouter.class); + String key = RedisKey.getUserKey(userId); + return RedisManage.get(key, RemoteRouter.class); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java index c8d3cd92..f666712e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java @@ -4,16 +4,12 @@ * Created by ohun on 2016/1/4. */ public final class KickRemoteMsg { - public String userId; - public String deviceId; - public String targetServer; + public String userId; + public String deviceId; + public String targetServer; - @Override - public String toString() { - return "KickRemoteMsg{" + - "userId='" + userId + '\'' + - ", deviceId='" + deviceId + '\'' + - ", targetServer='" + targetServer + '\'' + - '}'; - } + @Override + public String toString() { + return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + ", targetServer='" + targetServer + '\'' + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java index 0a79fb62..989ebf50 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java @@ -2,8 +2,6 @@ import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.common.security.AesCipher; -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.common.security.RsaCipher; /** * Created by ohun on 2015/12/25. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java index b82d2008..b7d77398 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.core.session; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.tools.Strings; import com.shinemo.mpush.tools.config.ConfigCenter; @@ -14,12 +15,14 @@ public final class ReusableSessionManager { private int expiredTime = ConfigCenter.holder.sessionExpiredTime(); public boolean cacheSession(ReusableSession session) { - RedisManage.set(session.sessionId, ReusableSession.encode(session.context), expiredTime); + String key = RedisKey.getSessionKey(session.sessionId); + RedisManage.set(key, ReusableSession.encode(session.context), expiredTime); return true; } public ReusableSession querySession(String sessionId) { - String value = RedisManage.get(sessionId, String.class); + String key = RedisKey.getSessionKey(sessionId); + String value = RedisManage.get(key, String.class); if (Strings.isBlank(value)) return null; return ReusableSession.decode(value); } From 6ac6d189f8d8a22c55531e9223fb01b2c90b9e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 15:48:38 +0800 Subject: [PATCH 261/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9redis=20sub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/router/RouterChangeListener.java | 4 +++- .../com/shinemo/mpush/tools/redis/RedisUtil.java | 15 +++++++-------- .../tools/redis/listener/ListenerDispatcher.java | 8 ++++++-- .../mpush/tools/redis/manage/RedisManage.java | 9 --------- .../mpush/tools/redis/pubsub/Subscriber.java | 7 ------- .../thread/threadpool/ThreadPoolContext.java | 2 -- .../thread/threadpool/ThreadPoolManager.java | 9 +++++++-- 7 files changed, 23 insertions(+), 31 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 8f27970b..eb6a3aea 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -11,6 +11,7 @@ import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; import com.shinemo.mpush.tools.redis.listener.MessageListener; import com.shinemo.mpush.tools.redis.manage.RedisManage; @@ -29,7 +30,8 @@ public final class RouterChangeListener extends AbstractEventContainer implement private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); public RouterChangeListener() { - RedisManage.subscribe(this, getKickChannel()); + ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); +// RedisManage.subscribe(this, getKickChannel()); } public String getKickChannel() { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 47753fb4..77655e3e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -477,16 +477,15 @@ public static void publish(RedisNode node, String channel, T message) { } } - //TODO 使用别的线程 public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { for (final RedisNode node : nodeList) { - Runnable runnable = new Runnable() { - @Override - public void run() { - subscribe(node, pubsub, channels); - } - }; - ThreadPoolManager.bizExecutor.execute(runnable); + String name = node.getIp()+"_"+Jsons.toJson(channels); + ThreadPoolManager.newThread(name, new Runnable() { + @Override + public void run() { + subscribe(node, pubsub, channels); + } + }); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java index a409bb0e..115515d2 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java @@ -3,10 +3,12 @@ import java.util.List; import java.util.Map; import java.util.concurrent.Executor; -import java.util.concurrent.Executors; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; public class ListenerDispatcher implements MessageListener { @@ -14,7 +16,7 @@ public class ListenerDispatcher implements MessageListener { private Map> subscribes = Maps.newTreeMap(); - private Executor executor = Executors.newFixedThreadPool(5); + private Executor executor = ThreadPoolManager.redisExecutor; @Override public void onMessage(final String channel, final String message) { @@ -39,5 +41,7 @@ public void subscribe(String channel, MessageListener listener) { subscribes.put(channel, listeners); } listeners.add(listener); + Subscriber subscriber = new Subscriber(); + RedisManage.subscribe(subscriber, channel); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index 0737ffb7..b4d0848c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -4,7 +4,6 @@ import java.util.Map; import java.util.Set; -import com.shinemo.mpush.tools.redis.listener.MessageListener; import redis.clients.jedis.JedisPubSub; @@ -13,7 +12,6 @@ import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.redis.RedisRegister; import com.shinemo.mpush.tools.redis.RedisUtil; -import com.shinemo.mpush.tools.redis.pubsub.Subscriber; import com.shinemo.mpush.tools.spi.ServiceContainer; /** @@ -225,11 +223,4 @@ public static void subscribe(JedisPubSub pubsub, String... channels) { RedisUtil.subscribe(set, pubsub, channels); } - - public static void subscribe(MessageListener listener, String... channel) { - Subscriber subscriber = new Subscriber(); - subscriber.subscribe(listener, channel); - subscribe(subscriber, channel); - } - } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 502785ec..4811a888 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -63,11 +63,4 @@ public void unsubscribe(String... channels) { super.unsubscribe(channels); } - public void subscribe(MessageListener listener, String... channels) { - LoggerManage.log(LogType.REDIS, "subscribe:%s",Jsons.toJson(channels)); - for (String channel : channels) { - dispatcher.subscribe(channel, listener); - } - } - } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index 67a5d1a1..ae1f5835 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -24,8 +24,6 @@ public class ThreadPoolContext { public static ThreadPoolContext REDIS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.REDIS, Constants.REDIS_POOL_SIZE,Constants.REDIS_THREAD_QUEUE_SIZE); - public static ThreadPoolContext ZK_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.ZK, Constants.ZK_POOL_SIZE,Constants.ZK_THREAD_QUEUE_SIZE); - public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { this.name = name; this.corePoolSize = corePoolSize; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java index e62680df..0c937fb2 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -19,19 +19,24 @@ public class ThreadPoolManager { public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); - public static Executor zkExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.ZK_THREAD_POOL); + public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); static{ poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); - poolCache.put(ThreadPoolContext.ZK_THREAD_POOL.getName(), zkExecutor); + poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); } public static final Map getPool(){ return poolCache; } + + public static final Thread newThread(String name,Runnable target){ + Thread thread = new Thread(target, name); + return thread; + } From d8277e087830102cb2174665f7456414caacc9fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 15:54:54 +0800 Subject: [PATCH 262/890] pub sub --- .../com/shinemo/mpush/core/router/RouterChangeListener.java | 1 - .../java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index eb6a3aea..66dc342b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -31,7 +31,6 @@ public final class RouterChangeListener extends AbstractEventContainer implement public RouterChangeListener() { ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); -// RedisManage.subscribe(this, getKickChannel()); } public String getKickChannel() { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 4811a888..3047b29e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -3,15 +3,13 @@ import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.listener.MessageListener; - import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; import redis.clients.jedis.JedisPubSub; public class Subscriber extends JedisPubSub { - private ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; + private static ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; @Override public void onMessage(String channel, String message) { From c18470731c2c020836dd26a9856db6a5cafb01ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 15:58:50 +0800 Subject: [PATCH 263/890] jvm util --- .../src/main/java/com/shinemo/mpush/tools/JVMUtil.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java index 8d6bb2a1..2a6831e2 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.tools; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -61,10 +60,8 @@ public void run() { try { jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.log")); JVMUtil.jstack(jstackStream); - } catch (FileNotFoundException e) { - log.error("", "Dump JVM cache Error!", e); } catch (Throwable t) { - log.error("", "Dump JVM cache Error!", t); + log.error("Dump JVM cache Error!", t); } finally { if (jstackStream != null) { try { @@ -95,7 +92,7 @@ public HotSpotDiagnosticMXBean run() throws Exception { } }); } catch (Exception e) { - log.error("", "getHotspotMBean Error!", e); + log.error("getHotspotMBean Error!", e); return null; } } @@ -125,7 +122,7 @@ public static void jMap(String fileName, boolean live) { hotspotMBean.dumpHeap(currentFileName, live); } catch (Exception e) { - log.error("", "dumpHeap Error!", e); + log.error("dumpHeap Error!", e); } } From 9e3f11cc0df95d9b40a1dcb1545e4b9cb09b03d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 16:43:26 +0800 Subject: [PATCH 264/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9redis=20manage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/test}/redis/ConsistentHashTest.java | 2 +- .../shinemo/mpush/test/redis/PubSubTest.java | 40 +++++++++++++++++++ .../mpush/test}/redis/RedisClusterTest.java | 3 +- .../test}/redis/RedisGroupManageTest.java | 0 .../mpush/test}/redis/RedisUtilTest.java | 4 +- .../com/shinemo/mpush/test}/redis/User.java | 2 +- .../shinemo/mpush/tools/redis/RedisNode.java | 4 ++ .../shinemo/mpush/tools/redis/RedisUtil.java | 6 +-- .../jedis/services/JedisRegisterManager.java | 7 ---- .../redis/listener/ListenerDispatcher.java | 8 +++- .../mpush/tools/redis/pubsub/Subscriber.java | 4 ++ 11 files changed, 64 insertions(+), 16 deletions(-) rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-test/src/main/java/com/shinemo/mpush/test}/redis/ConsistentHashTest.java (98%) create mode 100644 mpush-test/src/main/java/com/shinemo/mpush/test/redis/PubSubTest.java rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-test/src/main/java/com/shinemo/mpush/test}/redis/RedisClusterTest.java (93%) rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-test/src/main/java/com/shinemo/mpush/test}/redis/RedisGroupManageTest.java (100%) rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-test/src/main/java/com/shinemo/mpush/test}/redis/RedisUtilTest.java (97%) rename {mpush-tools/src/test/java/com/shinemo/mpush/tools => mpush-test/src/main/java/com/shinemo/mpush/test}/redis/User.java (94%) diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java similarity index 98% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java rename to mpush-test/src/main/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java index 3a02124a..b85f9499 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/ConsistentHashTest.java +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis; +package com.shinemo.mpush.test.redis; import java.util.ArrayList; import java.util.Collections; diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/PubSubTest.java b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/PubSubTest.java new file mode 100644 index 00000000..2814d2dc --- /dev/null +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/PubSubTest.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.test.redis; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.redis.RedisGroup; +import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.redis.RedisRegister; +import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.shinemo.mpush.tools.redis.pubsub.Subscriber; +import com.shinemo.mpush.tools.spi.ServiceContainer; + +public class PubSubTest { + + private RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + + @Before + public void init(){ + RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); + RedisGroup group = new RedisGroup(); + group.addRedisNode(node); + List listGroup = Lists.newArrayList(group); + redisRegister.init(listGroup); + } + + @Test + public void pubSubTest(){ + + RedisManage.subscribe(Subscriber.holder, "/hello/123"); + + RedisManage.subscribe(Subscriber.holder, "/hello/124"); + + RedisManage.publish("/hello/123", "123"); + RedisManage.publish("/hello/124", "124"); + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisClusterTest.java similarity index 93% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java rename to mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisClusterTest.java index ddace43c..95f95891 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisClusterTest.java +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisClusterTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis; +package com.shinemo.mpush.test.redis; import java.util.Date; import java.util.HashSet; @@ -10,6 +10,7 @@ import org.junit.Test; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.redis.RedisPoolConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java similarity index 100% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisGroupManageTest.java rename to mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisUtilTest.java similarity index 97% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java rename to mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisUtilTest.java index 3edbd454..c96f76ed 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/RedisUtilTest.java +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisUtilTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis; +package com.shinemo.mpush.test.redis; import java.util.Date; import java.util.Iterator; @@ -9,6 +9,8 @@ import org.junit.Test; import com.google.common.collect.Lists; +import com.shinemo.mpush.tools.redis.RedisNode; +import com.shinemo.mpush.tools.redis.RedisUtil; import redis.clients.jedis.Jedis; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/User.java similarity index 94% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java rename to mpush-test/src/main/java/com/shinemo/mpush/test/redis/User.java index 1156dffa..2fddc275 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/redis/User.java +++ b/mpush-test/src/main/java/com/shinemo/mpush/test/redis/User.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis; +package com.shinemo.mpush.test.redis; import java.io.Serializable; import java.util.Date; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java index 0b88cba8..a3bf80b9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java @@ -33,5 +33,9 @@ public RedisNode(String ip, int port, String password) { this.port = port; this.password = password; } + @Override + public String toString() { + return "RedisNode [ip=" + ip + ", port=" + port + ", password=" + password + "]"; + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 77655e3e..22ff03b3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -470,7 +470,7 @@ public static void publish(RedisNode node, String channel, T message) { jedis = getClient(node); jedis.publish(channel, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,node,channel); + LoggerManage.execption(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,Jsons.toJson(node),Jsons.toJson(channel)); } finally { // 返还到连接池 close(jedis); @@ -485,7 +485,7 @@ public static void subscribe(Set nodeList, final JedisPubSub pubsub, public void run() { subscribe(node, pubsub, channels); } - }); + }).start(); } } @@ -496,7 +496,7 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann jedis = getClient(node); jedis.subscribe(pubsub, channel); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:%s,%s",node,channel); + LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:%s,%s",Jsons.toJson(node),Jsons.toJson(channel)); } finally { // 返还到连接池 close(jedis); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java index 91bc88c0..57086e73 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -3,11 +3,6 @@ import java.util.Collections; import java.util.List; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.common.collect.Lists; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; @@ -18,8 +13,6 @@ public class JedisRegisterManager implements RedisRegister{ - private static final Logger LOGGER = LoggerFactory.getLogger(JedisRegisterManager.class); - private static List groups = Lists.newArrayList(); /** diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java index 115515d2..28926c37 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java @@ -6,6 +6,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.redis.manage.RedisManage; import com.shinemo.mpush.tools.redis.pubsub.Subscriber; import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; @@ -15,6 +17,8 @@ public class ListenerDispatcher implements MessageListener { public static final ListenerDispatcher INSTANCE = new ListenerDispatcher(); private Map> subscribes = Maps.newTreeMap(); + + private ListenerDispatcher(){} private Executor executor = ThreadPoolManager.redisExecutor; @@ -22,6 +26,7 @@ public class ListenerDispatcher implements MessageListener { public void onMessage(final String channel, final String message) { List listeners = subscribes.get(channel); if (listeners == null) { + LoggerManage.info(LogType.REDIS, "cannot find listener:%s,%s", channel,message); return; } for (final MessageListener listener : listeners) { @@ -41,7 +46,6 @@ public void subscribe(String channel, MessageListener listener) { subscribes.put(channel, listeners); } listeners.add(listener); - Subscriber subscriber = new Subscriber(); - RedisManage.subscribe(subscriber, channel); + RedisManage.subscribe(Subscriber.holder, channel); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 3047b29e..9b6f5556 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -11,6 +11,10 @@ public class Subscriber extends JedisPubSub { private static ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; + public static Subscriber holder = new Subscriber(); + + private Subscriber(){} + @Override public void onMessage(String channel, String message) { LoggerManage.log(LogType.REDIS, "onMessage:%s,%s", channel,message); From 24cb2458dd23f685205d12ca1ae5d930e2e79f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 16:49:49 +0800 Subject: [PATCH 265/890] =?UTF-8?q?=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/pom.xml | 4 ++++ .../mpush/test/connection/client/ConnectionClientMain.java | 0 .../java/com/shinemo/mpush/test/connection/client/Main.java | 0 .../java/com/shinemo/mpush/test/push/GatewayClientMain.java | 0 .../{main => test}/java/com/shinemo/mpush/test/push/Main.java | 0 .../java/com/shinemo/mpush/test/push/PushContent.java | 0 .../java/com/shinemo/mpush/test/push/PushRequest.java | 0 .../java/com/shinemo/mpush/test/push/PushRequestBus.java | 0 .../shinemo/mpush/test/push/client/ClientChannelHandler.java | 0 .../mpush/test/push/manage/impl/GatewayServerManage.java | 0 .../test/push/zk/listener/impl/GatewayServerPathListener.java | 0 .../java/com/shinemo/mpush/test/redis/ConsistentHashTest.java | 0 .../java/com/shinemo/mpush/test/redis/PubSubTest.java | 0 .../java/com/shinemo/mpush/test/redis/RedisClusterTest.java | 0 .../com/shinemo/mpush/test/redis/RedisGroupManageTest.java | 0 .../java/com/shinemo/mpush/test/redis/RedisUtilTest.java | 0 .../java/com/shinemo/mpush/test/redis/User.java | 0 17 files changed, 4 insertions(+) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/connection/client/Main.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/GatewayClientMain.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/Main.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/PushContent.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/PushRequest.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/PushRequestBus.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/redis/PubSubTest.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/redis/RedisClusterTest.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/redis/RedisUtilTest.java (100%) rename mpush-test/src/{main => test}/java/com/shinemo/mpush/test/redis/User.java (100%) diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index c4cfc1ce..a16ca8d5 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -34,5 +34,9 @@ com.shinemo.mpush mpush-cs + + com.shinemo.mpush + mpush-log + diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/connection/client/Main.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/GatewayClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/GatewayClientMain.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/GatewayClientMain.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/GatewayClientMain.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/Main.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushContent.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushContent.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/PushContent.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/PushContent.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequestBus.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/PushRequestBus.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/redis/PubSubTest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisClusterTest.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisClusterTest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisClusterTest.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/redis/RedisUtilTest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java diff --git a/mpush-test/src/main/java/com/shinemo/mpush/test/redis/User.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/User.java similarity index 100% rename from mpush-test/src/main/java/com/shinemo/mpush/test/redis/User.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/redis/User.java From e283edfba4d3c02bb71b574da2574f854ffdb506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 17:12:32 +0800 Subject: [PATCH 266/890] =?UTF-8?q?route=20change=20=E5=B0=8F=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/router/RouterChangeListener.java | 22 +++++++++---------- .../java/com/shinemo/mpush/tools/JVMUtil.java | 10 +++------ start.sh | 17 ++++++++++++++ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 66dc342b..874a7faf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -9,6 +9,8 @@ import com.shinemo.mpush.common.AbstractEventContainer; import com.shinemo.mpush.common.message.KickUserMessage; import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; @@ -18,14 +20,10 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Created by ohun on 2016/1/4. */ public final class RouterChangeListener extends AbstractEventContainer implements MessageListener { - private static final Logger LOGGER = LoggerFactory.getLogger(RouterChangeListener.class); public static final String KICK_CHANNEL_ = "/mpush/kick/"; private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); @@ -71,9 +69,9 @@ public void kickLocal(final String userId, final LocalRouter router) { public void operationComplete(ChannelFuture future) throws Exception { future.channel().close(); if (future.isSuccess()) { - LOGGER.info("kick local connection success, userId={}, router={}", userId, router); + LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId=%s, router=%s", userId, router); } else { - LOGGER.error("kick local connection failure, userId={}, router={}", userId, router); + LoggerManage.info(LogType.CONNECTION, "kick local connection failure, userId=%s, router=%s", userId, router); } } }); @@ -92,7 +90,7 @@ public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(MPushUtil.getLocalIp())) { - LOGGER.error("kick remote user but router in local, userId={}", userId); + LoggerManage.info(LogType.CONNECTION, "kick remote user but router in local, userId=%s", userId); return; } @@ -116,7 +114,7 @@ public void kickRemote(String userId, RemoteRouter router) { public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - LOGGER.error("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, target server error, localIp=%s, msg=%s", MPushUtil.getLocalIp(), msg); return; } @@ -125,13 +123,13 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); LocalRouter router = routerManager.lookup(userId); if (router != null) { - LOGGER.info("receive kick remote msg, msg={}", msg); + LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, msg=%s", msg); //2.1删除本地路由信息 routerManager.unRegister(userId); //2.2发送踢人消息到客户端 kickLocal(userId, router); } else { - LOGGER.warn("no local router find, kick failure, msg={}", msg); + LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg=%s", msg); } } @@ -142,10 +140,10 @@ public void onMessage(String channel, String message) { if (msg != null) { onReceiveKickRemoteMsg(msg); } else { - LOGGER.warn("receive an error kick message={}", message); + LoggerManage.info(LogType.CONNECTION, "receive an error kick message=%s", message); } } else { - LOGGER.warn("receive an error redis channel={}", channel); + LoggerManage.info(LogType.CONNECTION, "receive an error redis channel=%s",channel); } } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java index 2a6831e2..f95e4801 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java @@ -108,21 +108,17 @@ private static void initHotspotMBean() throws Exception { } public static void jMap(String fileName, boolean live) { + File f = new File(fileName, System.currentTimeMillis()+"-jmap.log"); + String currentFileName = f.getPath(); try { initHotspotMBean(); - - - - File f = new File(fileName, System.currentTimeMillis()+"-jmap.log"); - String currentFileName = f.getPath(); if (f.exists()) { f.delete(); } - hotspotMBean.dumpHeap(currentFileName, live); } catch (Exception e) { - log.error("dumpHeap Error!", e); + log.error("dumpHeap Error!"+currentFileName, e); } } diff --git a/start.sh b/start.sh index e69de29b..95df5328 100644 --- a/start.sh +++ b/start.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +ENV=daily + +base_dir=`pwd` + +echo "start assembly lib..." +mvn clean assembly:assembly -P $ENV + +echo "start tar mpush..." +cd $base_dir/target +tar -xzvf ./mpush-jar-with-dependency.tar.gz +echo "start start mpush..." + +java -jar $base_dir/target/mpush/mpush-cs.jar & + +echo "end start mpush..." From 73b755d3603a5712a8f83f65ebccb16a1a67b827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 17:16:51 +0800 Subject: [PATCH 267/890] =?UTF-8?q?log=20=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/UnbindUserHandler.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java index be88f6a4..b0083a2a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java @@ -9,14 +9,13 @@ import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.core.router.RouterCenter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; /** * Created by ohun on 2015/12/23. */ public final class UnbindUserHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(UnbindUserHandler.class); @Override public BindUserMessage decode(Packet packet, Connection connection) { @@ -27,7 +26,7 @@ public BindUserMessage decode(Packet packet, Connection connection) { public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - LOGGER.error("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); + LoggerManage.info(LogType.CONNECTION, "unbind user failure invalid param, session=%s", message.getConnection().getSessionContext()); return; } //1.绑定用户时先看下是否握手成功 @@ -37,14 +36,14 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.unRegister(message.userId); if (success) { OkMessage.from(message).setData("unbind success").send(); - LOGGER.warn("unbind user success, userId={}, session={}", message.userId, context); + LoggerManage.info(LogType.CONNECTION, "unbind user success, userId=%s, session=%s", message.userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").close(); - LOGGER.error("unbind user failure, register router failure, userId={}, session={}", message.userId, context); + LoggerManage.info(LogType.CONNECTION, "unbind user failure, register router failure, userId=%s, session=%s", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - LOGGER.error("unbind user failure not handshake, userId={}, session={}", message.userId, context); + LoggerManage.info(LogType.CONNECTION, "unbind user failure not handshake, userId=%s, session=%s", message.userId, context); } } } From d7f7be92fd78b03e32d457b0b20e1b9488bec4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 17:38:04 +0800 Subject: [PATCH 268/890] =?UTF-8?q?=E5=BF=AB=E9=80=9F=E9=87=8D=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/RedisKey.java | 7 ++ .../common/message/FastConnectMessage.java | 1 - .../core/client/ClientChannelHandler.java | 98 ++++++++++--------- .../core/handler/FastConnectHandler.java | 18 ++-- 4 files changed, 65 insertions(+), 59 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java index 7bb32759..d60793af 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -6,6 +6,8 @@ public class RedisKey { private static final String SESSION_PREFIX = "mp_s_"; + private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp_f_c_d_"; + public static final String getUserKey(String userId){ return USER_PREFIX+userId; } @@ -13,5 +15,10 @@ public static final String getUserKey(String userId){ public static final String getSessionKey(String sessionId){ return SESSION_PREFIX + sessionId; } + + //for fast connection test + public static final String getDeviceIdKey(String deviceId){ + return FAST_CONNECTION_DEVICE_PREFIX+deviceId; + } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java index c2d0567a..648e333b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index b826081e..5c065b11 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -2,12 +2,17 @@ +import java.util.Map; + +import com.google.common.collect.Maps; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Client; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.common.message.BindUserMessage; import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.common.message.FastConnectMessage; import com.shinemo.mpush.common.message.FastConnectOkMessage; import com.shinemo.mpush.common.message.HandshakeMessage; import com.shinemo.mpush.common.message.HandshakeOkMessage; @@ -21,7 +26,10 @@ import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; import com.shinemo.mpush.netty.connection.NettyConnection; +import com.shinemo.mpush.tools.redis.manage.RedisManage; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -67,6 +75,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception client.startHeartBeat(); LOGGER.info("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); + saveToRedisForFastConnection(securityNettyClient.getDeviceId(), message.sessionId, message.expireTime); } else if (command == Command.FAST_CONNECT) { String cipherStr = securityNettyClient.getCipher(); String[] cs = cipherStr.split(","); @@ -92,7 +101,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception PushMessage message = new PushMessage(packet, connection); LOGGER.info("receive an push message, content=" + message.content); }else if(command == Command.HEARTBEAT){ -// connection.send(packet); // ping -> pong LOGGER.info("receive an heart beat message"); }else{ LOGGER.info("receive an message, type=" + command.cmd+","+packet); @@ -144,36 +152,38 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client disconnect channel={}", ctx.channel()); } - private void tryFastConnect(SecurityNettyClient securityNettyClient) { + private void tryFastConnect(final SecurityNettyClient securityNettyClient) { handshake(securityNettyClient); -// if (sessionTickets == null) { -// -// return; -// } -// String sessionId = (String) sessionTickets.get("sessionId"); -// if (sessionId == null) { -// handshake(); -// return; -// } -// String expireTime = (String) sessionTickets.get("expireTime"); -// if (expireTime != null) { -// long exp = Long.parseLong(expireTime); -// if (exp < System.currentTimeMillis()) { -// handshake(); -// return; -// } -// } -// FastConnectMessage message = new FastConnectMessage(connection); -// message.deviceId = deviceId; -// message.sessionId = sessionId; -// message.send(new ChannelFutureListener() { -// @Override -// public void operationComplete(ChannelFuture channelFuture) throws Exception { -// if (!channelFuture.isSuccess()) { -// handshake(); -// } -// } -// }); + + Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); + + if (sessionTickets == null) { + return; + } + String sessionId = (String) sessionTickets.get("sessionId"); + if (sessionId == null) { + handshake(securityNettyClient); + return; + } + String expireTime = (String) sessionTickets.get("expireTime"); + if (expireTime != null) { + long exp = Long.parseLong(expireTime); + if (exp < System.currentTimeMillis()) { + handshake(securityNettyClient); + return; + } + } + FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); + message.deviceId = securityNettyClient.getDeviceId(); + message.sessionId = sessionId; + message.send(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + if (!channelFuture.isSuccess()) { + handshake(securityNettyClient); + } + } + }); } private void bindUser(SecurityNettyClient client) { @@ -182,22 +192,18 @@ private void bindUser(SecurityNettyClient client) { message.send(); } -// private void saveToken(HandshakeOkMessage message, SessionContext context) { -// try { -// Map map = new HashMap<>(); -// map.put("sessionId", message.sessionId); -// map.put("serverHost", message.serverHost); -// map.put("expireTime", Long.toString(message.expireTime)); -// map.put("cipher", context.cipher.toString()); -// map.put("deviceId", deviceId); -// map.put("userId", userId); -// String path = this.getClass().getResource("/").getFile(); -// FileOutputStream out = new FileOutputStream(new File(path, "token.dat")); -// out.write(Jsons.toJson(map).getBytes(Constants.UTF_8)); -// out.close(); -// } catch (Exception e) { -// } -// } + private void saveToRedisForFastConnection(String deviceId,String sessionId,Long expireTime){ + Map map = Maps.newHashMap(); + map.put("sessionId", sessionId); + map.put("expireTime", expireTime+""); + String key = RedisKey.getDeviceIdKey(deviceId); + RedisManage.set(key, map,60*5); //5分钟 + } + + private Map getFastConnectionInfo(String deviceId){ + String key = RedisKey.getDeviceIdKey(deviceId); + return RedisManage.get(key, Map.class); + } private void handshake(SecurityNettyClient client) { HandshakeMessage message = new HandshakeMessage(client.getConnection()); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 86b21bb4..758d0f90 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -8,7 +8,10 @@ import com.shinemo.mpush.common.message.FastConnectOkMessage; import com.shinemo.mpush.core.session.ReusableSession; import com.shinemo.mpush.core.session.ReusableSessionManager; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.MPushUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,23 +32,15 @@ public void handle(FastConnectMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.querySession(message.sessionId); if (session == null) { - //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - - LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); - + LoggerManage.info(LogType.CONNECTION, "fast connect failure, session is expired, sessionId=%s, deviceId=%s", message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { - //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - - LOGGER.warn("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); - + LoggerManage.info(LogType.CONNECTION, "fast connect failure, not the same device, deviceId=%s, session=%s", message.deviceId, session.context); } else { - //3.校验成功,重新计算心跳,完成快速重连 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); session.context.setHeartbeat(heartbeat); @@ -57,8 +52,7 @@ public void handle(FastConnectMessage message) { .setServerTime(System.currentTimeMillis()) .setHeartbeat(heartbeat) .send(); - - LOGGER.warn("fast connect success, session={}", session.context); + LoggerManage.info(LogType.CONNECTION, "fast connect success, session=%s", session.context); } } } From 41de42690de527a98e004389db19a0103dc3cbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 17:53:54 +0800 Subject: [PATCH 269/890] =?UTF-8?q?=E5=BF=AB=E9=80=9F=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/client/ClientChannelHandler.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 5c065b11..e5913251 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -75,7 +75,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception client.startHeartBeat(); LOGGER.info("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); - saveToRedisForFastConnection(securityNettyClient.getDeviceId(), message.sessionId, message.expireTime); + saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime,sessionKey); } else if (command == Command.FAST_CONNECT) { String cipherStr = securityNettyClient.getCipher(); String[] cs = cipherStr.split(","); @@ -153,11 +153,11 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { } private void tryFastConnect(final SecurityNettyClient securityNettyClient) { - handshake(securityNettyClient); Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); if (sessionTickets == null) { + handshake(securityNettyClient); return; } String sessionId = (String) sessionTickets.get("sessionId"); @@ -173,9 +173,13 @@ private void tryFastConnect(final SecurityNettyClient securityNettyClient) { return; } } + + String cipher = sessionTickets.get("cipherStr"); + FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); message.deviceId = securityNettyClient.getDeviceId(); message.sessionId = sessionId; + securityNettyClient.setCipher(cipher); message.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { @@ -192,11 +196,12 @@ private void bindUser(SecurityNettyClient client) { message.send(); } - private void saveToRedisForFastConnection(String deviceId,String sessionId,Long expireTime){ + private void saveToRedisForFastConnection(SecurityNettyClient client,String sessionId,Long expireTime,byte[] sessionKey){ Map map = Maps.newHashMap(); map.put("sessionId", sessionId); map.put("expireTime", expireTime+""); - String key = RedisKey.getDeviceIdKey(deviceId); + map.put("cipherStr", new String(sessionKey)+","+new String(client.getIv())); + String key = RedisKey.getDeviceIdKey(client.getDeviceId()); RedisManage.set(key, map,60*5); //5分钟 } From d7c25e5edbc6f0ae32b3614d726d7b469f863966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 4 Feb 2016 18:57:14 +0800 Subject: [PATCH 270/890] =?UTF-8?q?=E5=BF=AB=E9=80=9F=E9=87=8D=E8=BF=9Eok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/common/security/AesCipher.java | 2 -- .../com/shinemo/mpush/core/client/ClientChannelHandler.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index 3f168156..e94378eb 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -1,10 +1,8 @@ package com.shinemo.mpush.common.security; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Cipher; import com.shinemo.mpush.tools.crypto.AESUtils; -import java.util.Arrays; /** * Created by ohun on 2015/12/28. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index e5913251..2e6b81e0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -200,7 +200,7 @@ private void saveToRedisForFastConnection(SecurityNettyClient client,String sess Map map = Maps.newHashMap(); map.put("sessionId", sessionId); map.put("expireTime", expireTime+""); - map.put("cipherStr", new String(sessionKey)+","+new String(client.getIv())); + map.put("cipherStr", client.getConnection().getSessionContext().cipher.toString()); String key = RedisKey.getDeviceIdKey(client.getDeviceId()); RedisManage.set(key, map,60*5); //5分钟 } From 0319173b1ccda077bec2a9c21cbe8a6044a189f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 09:29:16 +0800 Subject: [PATCH 271/890] serverManage --- ...m.shinemo.mpush.common.manage.ServerManage | 1 - .../com/shinemo/mpush/test/push/Main.java | 66 +++++++++---------- ...m.shinemo.mpush.common.manage.ServerManage | 1 + 3 files changed, 33 insertions(+), 35 deletions(-) delete mode 100644 mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage create mode 100644 mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage diff --git a/mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage deleted file mode 100644 index 85817c00..00000000 --- a/mpush-test/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ /dev/null @@ -1 +0,0 @@ -gatewayServerManage=com.shinemo.mpush.ps.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index 418e20ec..2c43455e 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -11,39 +11,37 @@ */ public class Main { - public static void main(String[] args) throws Exception { - GatewayClientMain client = new GatewayClientMain(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - - client.send(Jsons.toJson(content), Arrays.asList("43", "8"), - new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - } - ); - Thread.sleep(10000); - } - LockSupport.park(); - } + public static void main(String[] args) throws Exception { + GatewayClientMain client = new GatewayClientMain(); + client.start(); + Thread.sleep(1000); + for (int i = 0; i < 100; i++) { + PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); + + client.send(Jsons.toJson(content), Arrays.asList("43", "8"), new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + }); + Thread.sleep(10000); + } + LockSupport.park(); + } } \ No newline at end of file diff --git a/mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage new file mode 100644 index 00000000..71f2823a --- /dev/null +++ b/mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -0,0 +1 @@ +gatewayServerManage=com.shinemo.mpush.test.push.manage.impl.GatewayServerManage \ No newline at end of file From cca2cfc13fd208c6c9783cb962b6244af0f09c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 11:02:13 +0800 Subject: [PATCH 272/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/client/NettyClientFactory.java | 53 ------------------- mpush-test/pom.xml | 28 ++++++++++ .../com/shinemo/mpush/test/push/Main.java | 2 +- 3 files changed, 29 insertions(+), 54 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index 5272b522..74809a3c 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -60,59 +60,6 @@ public void initChannel(SocketChannel ch) throws Exception { } } - public Client create(String host,int port,final ChannelHandler handler){ - Client client = new NettyClient(host, port); - return init(client, handler); - } - - @Deprecated - public Client createSecurityClient(String host,int port,final ChannelHandler handler,byte[] clientKey,byte[] iv,String clientVersion, - String deviceId,String osName,String osVersion,String userId,String cipher){ - SecurityNettyClient client = new SecurityNettyClient(host, port); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - return init(client, handler); - } - - public Client init(Client client, final ChannelHandler handler) { - final Bootstrap bootstrap = new Bootstrap(); - bootstrap.group(NettySharedHolder.workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(handler); - } - }); - - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - log.error("init channel:"+channel); - return client; - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + client); - return null; - } - } - public Client getCientByChannel(final Channel channel) { return channel2Client.get(channel); } diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index a16ca8d5..2d790e9a 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -39,4 +39,32 @@ mpush-log + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-test + + + + + + + + diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index 2c43455e..e6e339af 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -18,7 +18,7 @@ public static void main(String[] args) throws Exception { for (int i = 0; i < 100; i++) { PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - client.send(Jsons.toJson(content), Arrays.asList("43", "8"), new PushSender.Callback() { + client.send(Jsons.toJson(content), Arrays.asList("user-0", "8"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); From 3a99bf7f224c81c14c146e02772c4b69d641b8b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 11:47:32 +0800 Subject: [PATCH 273/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/message/PushMessage.java | 1 - .../core/handler/GatewayPushHandler.java | 27 ++++++++++--------- .../com/shinemo/mpush/test/push/Main.java | 1 - .../shinemo/mpush/test/push/PushRequest.java | 6 +++++ .../mpush/test/push/PushRequestBus.java | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java index 380a6256..7943358c 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java @@ -2,7 +2,6 @@ import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import static com.shinemo.mpush.api.protocol.Command.PUSH; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index 628c26b0..b7c3582e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -10,11 +10,13 @@ import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.core.router.LocalRouter; import com.shinemo.mpush.core.router.RouterCenter; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.MPushUtil; + import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + import static com.shinemo.mpush.common.ErrorCode.*; @@ -22,7 +24,6 @@ * Created by ohun on 2015/12/30. */ public final class GatewayPushHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(GatewayPushHandler.class); @Override public GatewayPushMessage decode(Packet packet, Connection connection) { @@ -72,8 +73,8 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - LOGGER.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); - + LoggerManage.info(LogType.PUSH, "gateway push, router in local but disconnect, userId=%s, connection=%s", message.userId, connection); + //删除已经失效的本地路由 RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); @@ -90,12 +91,13 @@ public void operationComplete(ChannelFuture future) throws Exception { //推送成功 OkMessage.from(message).setData(message.userId).send(); - LOGGER.info("gateway push message to client success userId={}, content={}", message.userId, message.content); + LoggerManage.info(LogType.PUSH, "gateway push message to client success userId=%s, content=%s", message.userId, message.content); + } else { //推送失败 ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); - - LOGGER.error("gateway push message to client failure userId={}, content={}", message.userId, message.content); + + LoggerManage.info(LogType.PUSH, "gateway push message to client failure userId=%s, content=%s", message.userId, message.content); } } }); @@ -118,7 +120,7 @@ private void checkRemote(GatewayPushMessage message) { ErrorMessage.from(message).setErrorCode(OFFLINE).send(); - LOGGER.warn("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); + LoggerManage.info(LogType.PUSH, "gateway push, router not exists user offline userId=%s, content=%s", message.userId, message.content); return; } @@ -130,15 +132,16 @@ private void checkRemote(GatewayPushMessage message) { //删除失效的远程缓存 RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); - - LOGGER.error("gateway push error remote is local, userId={}, router={}", message.userId, router); + + LoggerManage.info(LogType.PUSH, "gateway push error remote is local, userId=%s, router=%s", message.userId, router); return; } //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + + LoggerManage.info(LogType.PUSH, "gateway push, router in remote userId=%s, router=%s", message.userId, router); - LOGGER.info("gateway push, router in remote userId={}, router={}", message.userId, router); } } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index e6e339af..58a0d81b 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -10,7 +10,6 @@ * Created by ohun on 2016/1/7. */ public class Main { - public static void main(String[] args) throws Exception { GatewayClientMain client = new GatewayClientMain(); client.start(); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java index d990959d..74eb71f0 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java @@ -184,4 +184,10 @@ public void operationComplete(ChannelFuture future) throws Exception { sessionId = pushMessage.getSessionId(); PushRequestBus.INSTANCE.put(sessionId, this); } + + public long getSendTime() { + return sendTime; + } + + } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java index d2687bc0..00d4ccc5 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java @@ -13,7 +13,7 @@ public class PushRequestBus implements Runnable { private Executor executor = Executors.newFixedThreadPool(5);//test private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test - public PushRequestBus() { + private PushRequestBus() { scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); } From 49e4db7f89b8aa33f54e234fcc0933335a2c2b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 15:22:01 +0800 Subject: [PATCH 274/890] =?UTF-8?q?push=20client=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- conf-daily.properties | 7 +++ conf-online.properties | 7 +++ conf-pre.properties | 7 +++ mpush-client/pom.xml | 28 +++++++++++ .../java/com/shinemo/mpush/push/Main.java | 46 +++++++++++++++++++ .../com/shinemo/mpush/push/PushClient.java | 10 ++-- .../com/shinemo/mpush}/push/PushContent.java | 2 +- .../com/shinemo/mpush}/push/PushRequest.java | 4 +- .../shinemo/mpush}/push/PushRequestBus.java | 2 +- .../push/client/ClientChannelHandler.java | 6 +-- .../push/manage/impl/GatewayServerManage.java | 4 +- .../impl/GatewayServerPathListener.java | 2 +- ...m.shinemo.mpush.common.manage.ServerManage | 1 + .../message/gateway/GatewayPushMessage.java | 1 - .../listener/AbstractDataChangeListener.java | 4 +- .../core/client/ClientChannelHandler.java | 12 +++-- .../mpush/core/client/GatewayClient.java | 8 ---- mpush-cs/src/main/resources/config.properties | 14 +++--- mpush-test/pom.xml | 4 ++ .../com/shinemo/mpush/test/push/Main.java | 4 +- .../resources/config.properties | 4 +- .../src/{main => test}/resources/logback.xml | 0 .../tools/zk/listener/DataChangeListener.java | 2 +- 24 files changed, 139 insertions(+), 43 deletions(-) create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/Main.java rename mpush-test/src/test/java/com/shinemo/mpush/test/push/GatewayClientMain.java => mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java (72%) rename {mpush-test/src/test/java/com/shinemo/mpush/test => mpush-client/src/main/java/com/shinemo/mpush}/push/PushContent.java (88%) rename {mpush-test/src/test/java/com/shinemo/mpush/test => mpush-client/src/main/java/com/shinemo/mpush}/push/PushRequest.java (97%) rename {mpush-test/src/test/java/com/shinemo/mpush/test => mpush-client/src/main/java/com/shinemo/mpush}/push/PushRequestBus.java (97%) rename {mpush-test/src/test/java/com/shinemo/mpush/test => mpush-client/src/main/java/com/shinemo/mpush}/push/client/ClientChannelHandler.java (95%) rename {mpush-test/src/test/java/com/shinemo/mpush/test => mpush-client/src/main/java/com/shinemo/mpush}/push/manage/impl/GatewayServerManage.java (95%) rename {mpush-test/src/test/java/com/shinemo/mpush/test => mpush-client/src/main/java/com/shinemo/mpush}/push/zk/listener/impl/GatewayServerPathListener.java (94%) create mode 100644 mpush-client/src/main/resources/mpush/services/com.shinemo.mpush.common.manage.ServerManage delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java rename mpush-test/src/{main => test}/resources/config.properties (95%) rename mpush-test/src/{main => test}/resources/logback.xml (100%) diff --git a/.gitignore b/.gitignore index 99cbf641..7009d5a8 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ *.jar *.war *.ear -*.iml \ No newline at end of file +*.iml +./target/* diff --git a/conf-daily.properties b/conf-daily.properties index 7653074c..1fcded38 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -2,4 +2,11 @@ log.home=/tmp/logs/mpush loglevel=warn zk_ip = 127.0.0.1:2181 +zk_digest = shinemoIpo +zk_namespace = mpush-daily redis_group = 127.0.0.1:6379:shinemoIpo +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +force_write_redis_group_info = true +connection_server_port = 20882 +gateway_server_port = 4000 diff --git a/conf-online.properties b/conf-online.properties index 052a5272..bb633fb2 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -2,4 +2,11 @@ log.home=/opt/logs/mpush loglevel=INFO zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 +zk_digest = shinemoIpo +zk_namespace = mpush-online redis_group = 10.1.80.57:6379:shinemoIpo +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +force_write_redis_group_info = false +connection_server_port = 3000 +gateway_server_port = 4000 diff --git a/conf-pre.properties b/conf-pre.properties index 052a5272..d9ff0381 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -2,4 +2,11 @@ log.home=/opt/logs/mpush loglevel=INFO zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 +zk_digest = shinemoIpo +zk_namespace = mpush-pre redis_group = 10.1.80.57:6379:shinemoIpo +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +force_write_redis_group_info = false +connection_server_port = 3000 +gateway_server_port = 4000 diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index d2cce769..17507657 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -22,4 +22,32 @@ mpush-netty + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-test + + + + + + + + diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java b/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java new file mode 100644 index 00000000..9deb82c4 --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java @@ -0,0 +1,46 @@ +package com.shinemo.mpush.push; + +import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.tools.Jsons; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + +/** + * Created by ohun on 2016/1/7. + */ +public class Main { + public static void main(String[] args) throws Exception { + PushClient client = new PushClient(); + client.start(); + Thread.sleep(1000); + for (int i = 0; i < 100; i++) { + PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); + + client.send(Jsons.toJson(content), Arrays.asList("user-0", "8"), new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + }); + Thread.sleep(10000); + } + LockSupport.park(); + } + +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/GatewayClientMain.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java similarity index 72% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/GatewayClientMain.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java index 7f9a80b7..1767ebc1 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/GatewayClientMain.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java @@ -1,17 +1,17 @@ -package com.shinemo.mpush.test.push; +package com.shinemo.mpush.push; import java.util.Collection; import com.google.common.base.Strings; -import com.shinemo.mpush.api.PushSender.Callback; +import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.test.push.zk.listener.impl.GatewayServerPathListener; +import com.shinemo.mpush.push.zk.listener.impl.GatewayServerPathListener; -public class GatewayClientMain extends AbstractClient { +public class PushClient extends AbstractClient implements PushSender{ private static final int defaultTimeout = 3000; - public GatewayClientMain() { + public PushClient() { registerListener(new GatewayServerPathListener()); } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushContent.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java similarity index 88% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/PushContent.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java index 7c39ea87..3b496dac 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushContent.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.push; +package com.shinemo.mpush.push; public class PushContent { public String msgId; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java similarity index 97% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index 74eb71f0..a5a6d31b 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.push; +package com.shinemo.mpush.push; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; @@ -7,7 +7,7 @@ import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; import com.shinemo.mpush.common.router.ConnectionRouterManager; import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.test.push.manage.impl.GatewayServerManage; +import com.shinemo.mpush.push.manage.impl.GatewayServerManage; import com.shinemo.mpush.tools.spi.ServiceContainer; import io.netty.channel.ChannelFuture; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java similarity index 97% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java index 00d4ccc5..ad423927 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.push; +package com.shinemo.mpush.push; import java.util.Iterator; import java.util.Map; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/shinemo/mpush/push/client/ClientChannelHandler.java similarity index 95% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/client/ClientChannelHandler.java index bb917502..f576941c 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/client/ClientChannelHandler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.push.client; +package com.shinemo.mpush.push.client; @@ -9,8 +9,8 @@ import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.netty.client.ChannelClientHandler; import com.shinemo.mpush.netty.connection.NettyConnection; -import com.shinemo.mpush.test.push.PushRequest; -import com.shinemo.mpush.test.push.PushRequestBus; +import com.shinemo.mpush.push.PushRequest; +import com.shinemo.mpush.push.PushRequestBus; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java similarity index 95% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java index 7942e639..d11b53d6 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/manage/impl/GatewayServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.push.manage.impl; +package com.shinemo.mpush.push.manage.impl; import java.util.Collection; import java.util.Collections; @@ -16,7 +16,7 @@ import com.shinemo.mpush.common.manage.ServerManage; import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.test.push.client.ClientChannelHandler; +import com.shinemo.mpush.push.client.ClientChannelHandler; public class GatewayServerManage implements ServerManage{ diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java similarity index 94% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java index d4ba4ee1..20f98477 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/zk/listener/impl/GatewayServerPathListener.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.push.zk.listener.impl; +package com.shinemo.mpush.push.zk.listener.impl; import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; diff --git a/mpush-client/src/main/resources/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-client/src/main/resources/mpush/services/com.shinemo.mpush.common.manage.ServerManage new file mode 100644 index 00000000..f15779a3 --- /dev/null +++ b/mpush-client/src/main/resources/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -0,0 +1 @@ +gatewayServerManage=com.shinemo.mpush.push.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java index 34c03ed9..88dce714 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java @@ -2,7 +2,6 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.common.message.ByteBufMessage; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java index a8057eba..b28c7124 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java @@ -49,9 +49,9 @@ public void dataChanged(CuratorFramework client, TreeCacheEvent event, String pa } public void initData() { - log.warn("start init "+ this.getClass().getSimpleName()+"server data"); + log.warn(zkRegister.getClient().getNamespace()+" start init "+ this.getClass().getSimpleName()+" server data"); _initData(); - log.warn("end init "+ this.getClass().getSimpleName()+"server data"); + log.warn(zkRegister.getClient().getNamespace()+" end init "+ this.getClass().getSimpleName()+" server data"); } public abstract String getRegisterPath(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 2e6b81e0..664fc485 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -174,17 +174,19 @@ private void tryFastConnect(final SecurityNettyClient securityNettyClient) { } } - String cipher = sessionTickets.get("cipherStr"); + final String cipher = sessionTickets.get("cipherStr"); FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); message.deviceId = securityNettyClient.getDeviceId(); message.sessionId = sessionId; - securityNettyClient.setCipher(cipher); - message.send(new ChannelFutureListener() { + + message.sendRaw(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { - if (!channelFuture.isSuccess()) { - handshake(securityNettyClient); + if (channelFuture.isSuccess()) { + securityNettyClient.setCipher(cipher); + }else{ + handshake(securityNettyClient); } } }); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java deleted file mode 100644 index f9fc8a85..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/GatewayClient.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.shinemo.mpush.core.client; - -/** - * Created by ohun on 2015/12/30. - */ -public class GatewayClient { - -} diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-cs/src/main/resources/config.properties index 5a622575..958fd5d2 100644 --- a/mpush-cs/src/main/resources/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -9,13 +9,13 @@ max_heartbeat = 1800000 ## max_hb_timeout_times = 2 ## -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +private_key = ${private_key} ## -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +public_key = ${public_key} ## -gateway_server_port = 4000 +gateway_server_port = ${gateway_server_port} ## -connection_server_port = 3000 +connection_server_port = ${connection_server_port} ## aes_key_length = 16 ## @@ -25,10 +25,10 @@ session_expired_time = 86400 ## zk ip zk_ip = ${zk_ip} -zk_namespace = mpush -zk_digest = shinemoIpo +zk_namespace = ${zk_namespace} +zk_digest = ${zk_digest} ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = ${redis_group} -force_write_redis_group_info = true +force_write_redis_group_info = ${force_write_redis_group_info} diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 2d790e9a..a1051bb7 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -38,6 +38,10 @@ com.shinemo.mpush mpush-log + + com.shinemo.mpush + mpush-client + diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index 58a0d81b..e321996e 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -1,6 +1,8 @@ package com.shinemo.mpush.test.push; import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.push.PushClient; +import com.shinemo.mpush.push.PushContent; import com.shinemo.mpush.tools.Jsons; import java.util.Arrays; @@ -11,7 +13,7 @@ */ public class Main { public static void main(String[] args) throws Exception { - GatewayClientMain client = new GatewayClientMain(); + PushClient client = new PushClient(); client.start(); Thread.sleep(1000); for (int i = 0; i < 100; i++) { diff --git a/mpush-test/src/main/resources/config.properties b/mpush-test/src/test/resources/config.properties similarity index 95% rename from mpush-test/src/main/resources/config.properties rename to mpush-test/src/test/resources/config.properties index a3158cec..8bffba27 100644 --- a/mpush-test/src/main/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -13,7 +13,7 @@ private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO ## public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB ## -gateway_server_port = 4000 +gateway_server_port = 20882 ## connection_server_port = 3000 ## @@ -24,7 +24,7 @@ ras_key_length = 1024 session_expired_time = 86400 zk_ip = 127.0.0.1:2181 -zk_namespace = mpush +zk_namespace = mpush-daily zk_digest = shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo diff --git a/mpush-test/src/main/resources/logback.xml b/mpush-test/src/test/resources/logback.xml similarity index 100% rename from mpush-test/src/main/resources/logback.xml rename to mpush-test/src/test/resources/logback.xml diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java index 0fbf058f..822cb664 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java @@ -15,7 +15,7 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc if (path.isEmpty()) { return; } - LoggerManage.log(LogType.ZK, "DataChangeListener:%s,%s", path,listenerPath()); + LoggerManage.log(LogType.ZK, "DataChangeListener:%s,%s,namespace:%s", path,listenerPath(),client.getNamespace()); if(path.startsWith(listenerPath())){ dataChanged(client, event, path); } From e2884c1df6922059703ac03aebbe350e2a5c3402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 15:56:11 +0800 Subject: [PATCH 275/890] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/server/ConnectionServer.java | 6 +- .../connection/NettyConnectionManager.java | 59 ++++++++++++++++++- .../mpush/netty/server/NettyServer.java | 5 +- .../shinemo/mpush/test/redis/PubSubTest.java | 29 +++++++-- mpush-test/src/test/resources/logback.xml | 4 +- 5 files changed, 88 insertions(+), 15 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 7635286e..d91c161c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.core.server; -import java.util.concurrent.TimeUnit; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Command; @@ -8,9 +7,6 @@ import com.shinemo.mpush.core.handler.*; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; -import com.shinemo.mpush.netty.server.ScanAllConnectionTimerTask; -import com.shinemo.mpush.netty.util.NettySharedHolder; -import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; @@ -26,7 +22,7 @@ public final class ConnectionServer extends NettyServer { public ConnectionServer(int port) { super(port); - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle() / 1000, TimeUnit.SECONDS); +// NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle() / 1000, TimeUnit.SECONDS); } @Override diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 11cb745e..f8084f89 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -2,24 +2,42 @@ import com.google.common.collect.Lists; +import com.google.common.eventbus.Subscribe; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.ConnectionManager; +import com.shinemo.mpush.api.event.HandshakeEvent; +import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; +import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.channel.Channel; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timeout; +import io.netty.util.Timer; +import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import java.util.List; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/22. */ -public final class NettyConnectionManager implements ConnectionManager { +public final class NettyConnectionManager implements ConnectionManager { //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); + private Timer wheelTimer; + @Override public void init() { + //每秒钟走一步,一个心跳周期内走一圈 + long tickDuration = 1000;//1s + int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); + this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); + EventBus.INSTANCE.register(this); } @Override @@ -44,5 +62,44 @@ public void remove(Channel channel) { public List getConnections() { return Lists.newArrayList(connections.values()); } + + @Subscribe + void onHandshakeOk(HandshakeEvent event) { + HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); + task.startTimeout(); + } + + private class HeartbeatCheckTask implements TimerTask { + private int expiredTimes = 0; + private final int heartbeat; + private final Connection connection; + + public HeartbeatCheckTask(int heartbeat, Connection connection) { + this.heartbeat = heartbeat; + this.connection = connection; + } + + public void startTimeout() { + wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + } + + @Override + public void run(Timeout timeout) throws Exception { + if (!connection.isConnected()) return; + if (connection.heartbeatTimeout()) { + if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { + connection.close(); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); + return; + } else { + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + } + } else { + expiredTimes = 0; + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + } + startTimeout(); + } + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 757d1a19..575260d8 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -28,7 +28,7 @@ public abstract class NettyServer implements Server { public enum State {Created, Initialized, Starting, Started, Shutdown} - protected final AtomicReference serverState = new AtomicReference(State.Created); + protected final AtomicReference serverState = new AtomicReference<>(State.Created); private final int port; private EventLoopGroup bossGroup; @@ -162,7 +162,8 @@ private void createNioServer(final Listener listener) { } - private void createEpollServer(final Listener listener) { + @SuppressWarnings("unused") + private void createEpollServer(final Listener listener) { EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java index 2814d2dc..9536130c 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.test.redis; import java.util.List; +import java.util.concurrent.locks.LockSupport; import org.junit.Before; import org.junit.Test; @@ -27,14 +28,32 @@ public void init(){ } @Test - public void pubSubTest(){ - + public void subpubTest(){ RedisManage.subscribe(Subscriber.holder, "/hello/123"); - - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - + RedisManage.subscribe(Subscriber.holder, "/hello/124"); RedisManage.publish("/hello/123", "123"); RedisManage.publish("/hello/124", "124"); } + @Test + public void pubsubTest(){ + RedisManage.publish("/hello/123", "123"); + RedisManage.publish("/hello/124", "124"); + RedisManage.subscribe(Subscriber.holder, "/hello/123"); + RedisManage.subscribe(Subscriber.holder, "/hello/124"); + } + + @Test + public void pubTest(){ + RedisManage.publish("/hello/123", "123"); + RedisManage.publish("/hello/124", "124"); + } + + @Test + public void subTest(){ + RedisManage.subscribe(Subscriber.holder, "/hello/123"); + RedisManage.subscribe(Subscriber.holder, "/hello/124"); + LockSupport.park(); + } + } diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index a3f29d83..f45215a7 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -15,7 +15,7 @@ System.err UTF-8 - WARN + debug %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -24,7 +24,7 @@ - + From 1524a4c51945f8c408c49f5efd03be85e93512fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 16:19:20 +0800 Subject: [PATCH 276/890] log --- mpush-cs/src/main/resources/config.properties | 3 +- .../java/com/shinemo/mpush/log/LogType.java | 2 +- .../connection/NettyConnectionManager.java | 31 ++++++++++++------- .../mpush/test/connection/client/Main.java | 2 +- .../src/test/resources/config.properties | 2 +- mpush-test/src/test/resources/logback.xml | 2 +- .../mpush/tools/config/ConfigCenter.java | 3 +- .../test/resources/serverconfig.properties | 2 +- 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-cs/src/main/resources/config.properties index 958fd5d2..48e4494b 100644 --- a/mpush-cs/src/main/resources/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -5,7 +5,7 @@ compress_limit = 10240 ## min_heartbeat = 10000 ## -max_heartbeat = 1800000 +max_heartbeat = 180000 ## max_hb_timeout_times = 2 ## @@ -32,3 +32,4 @@ zk_digest = ${zk_digest} ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = ${redis_group} force_write_redis_group_info = ${force_write_redis_group_info} +jvm_log_path = /opt/shinemo/mpush/ diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java index 2618706f..62337eb8 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.log; public enum LogType { - CONNECTION,PUSH,HEARTBEAT,REDIS,ZK + CONNECTION,PUSH,HEARTBEAT,REDIS,ZK,DEFAULT } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index f8084f89..fb1a4dea 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -22,6 +22,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; + /** * Created by ohun on 2015/12/22. */ @@ -70,6 +71,7 @@ void onHandshakeOk(HandshakeEvent event) { } private class HeartbeatCheckTask implements TimerTask { + private int expiredTimes = 0; private final int heartbeat; private final Connection connection; @@ -85,20 +87,25 @@ public void startTimeout() { @Override public void run(Timeout timeout) throws Exception { - if (!connection.isConnected()) return; - if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { - connection.close(); - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); - return; + try{ + if (!connection.isConnected()) return; + if (connection.heartbeatTimeout()) { + if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { + connection.close(); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); + return; + } else { + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + } } else { - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + expiredTimes = 0; + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); } - } else { - expiredTimes = 0; - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); - } - startTimeout(); + }catch(Throwable e){ + LoggerManage.execption(LogType.DEFAULT, e, "HeartbeatCheckTask error"); + }finally{ + startTimeout(); + } } } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java index 6c0121f0..6afff5f8 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<1;i++){ + for(int i = 0;i<10;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index 8bffba27..cced99e9 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -5,7 +5,7 @@ compress_limit = 10240 ## min_heartbeat = 10000 ## -max_heartbeat = 1800000 +max_heartbeat = 180000 ## max_hb_timeout_times = 2 ## diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index f45215a7..ea3f95d0 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -24,7 +24,7 @@ - + diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index b6efe57a..c01c84a5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -32,7 +32,7 @@ public interface ConfigCenter extends Config { int minHeartbeat(); @Key("max_heartbeat") - @DefaultValue("1800000") + @DefaultValue("180000") //180秒 int maxHeartbeat(); @Key("max_hb_timeout_times") @@ -95,6 +95,7 @@ public interface ConfigCenter extends Config { @DefaultValue("59000") long scanConnTaskCycle(); + @Key("jvm_log_path") @DefaultValue("/opt/shinemo/mpush/") String logPath(); diff --git a/mpush-tools/src/test/resources/serverconfig.properties b/mpush-tools/src/test/resources/serverconfig.properties index e8c461e9..50ba973a 100644 --- a/mpush-tools/src/test/resources/serverconfig.properties +++ b/mpush-tools/src/test/resources/serverconfig.properties @@ -4,7 +4,7 @@ zk_digest=shinemoIpo max_packet_size=10240 compress_limit=10240 min_heartbeat=10000 -max_heartbeat=1800000 +max_heartbeat=180000 max_hb_timeout_times=2 private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file From 74137d653f9a76d6ca429fa60a80af7d511e42a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 17:23:26 +0800 Subject: [PATCH 277/890] heart beat time --- .../java/com/shinemo/mpush/api/Client.java | 2 + .../core/client/ClientChannelHandler.java | 4 +- .../mpush/netty/client/NettyClient.java | 58 ++++++++++--------- .../netty/connection/NettyConnection.java | 3 +- .../connection/NettyConnectionManager.java | 8 ++- 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java index c5e25277..2c1eb1d0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java @@ -27,6 +27,8 @@ public interface Client { void startHeartBeat() throws Exception; + void startHeartBeat(final int heartbeat) throws Exception; + void stop(); Connection getConnection(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 664fc485..ed911ba5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -72,7 +72,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); - client.startHeartBeat(); + client.startHeartBeat(message.heartbeat); LOGGER.info("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime,sessionKey); @@ -84,7 +84,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().changeCipher(new AesCipher(key, iv)); FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); - client.startHeartBeat(); + client.startHeartBeat(message.heartbeat); bindUser(securityNettyClient); LOGGER.info("fast connect success, message=" + message); } else if (command == Command.KICK) { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index bdc85e96..2852b7c3 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -77,32 +77,7 @@ public int inceaseAndGetHbTimes() { @Override public void startHeartBeat() throws Exception { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); - } - log.warn("client send msg hb false:" + channel.remoteAddress().toString()); - } else { - log.warn("client send msg hb success:" + channel.remoteAddress().toString()); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); - } - } - } - }, ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); + startHeartBeat((int)ConfigCenter.holder.scanConnTaskCycle()/1000); } @@ -141,5 +116,36 @@ public String toString() { return "NettyClient [host=" + host + ", port=" + port + ", channel=" + channel + ", hbTimes=" + hbTimes + ", connection=" + connection + "]"; } + @Override + public void startHeartBeat(final int heartbeat) throws Exception { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + try { + ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); + } + log.warn("client send msg hb false:" + channel.remoteAddress().toString()); + } else { + log.warn("client send msg hb success:" + channel.remoteAddress().toString()); + } + } + }); + } finally { + if (channel.isActive()) { + NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + } + } + } + }, heartbeat, TimeUnit.MILLISECONDS); + + } + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 3b7c4dff..2ffe7dc0 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -87,7 +87,8 @@ public boolean isConnected() { @Override public boolean heartbeatTimeout() { - return context.heartbeat > 0 && System.currentTimeMillis() - lastReadTime > context.heartbeat; + long between = System.currentTimeMillis() - lastReadTime; + return context.heartbeat > 0 && between > context.heartbeat; } @Override diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index fb1a4dea..f9956f19 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -88,7 +88,10 @@ public void startTimeout() { @Override public void run(Timeout timeout) throws Exception { try{ - if (!connection.isConnected()) return; + if (!connection.isConnected()){ + LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + return; + } if (connection.heartbeatTimeout()) { if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { connection.close(); @@ -103,9 +106,8 @@ public void run(Timeout timeout) throws Exception { } }catch(Throwable e){ LoggerManage.execption(LogType.DEFAULT, e, "HeartbeatCheckTask error"); - }finally{ - startTimeout(); } + startTimeout(); } } From 7f8462dd03bd9411671cd5ad4776d6786fc5f79f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 17:28:13 +0800 Subject: [PATCH 278/890] =?UTF-8?q?mpush=20daily=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-daily.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 1fcded38..62828ee9 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,10 +1,10 @@ #日志根目录 -log.home=/tmp/logs/mpush +log.home=/opt/logs/mpush loglevel=warn -zk_ip = 127.0.0.1:2181 +zk_ip = 115.29.169.109:5666 zk_digest = shinemoIpo zk_namespace = mpush-daily -redis_group = 127.0.0.1:6379:shinemoIpo +redis_group = 111.1.57.148:6379:ShineMoIpo private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info = true From 5381a255e16594404291eaf5fa8c1b170fd55243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 17:32:56 +0800 Subject: [PATCH 279/890] monid start --- start.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/start.sh b/start.sh index 95df5328..b6e94066 100644 --- a/start.sh +++ b/start.sh @@ -14,4 +14,5 @@ echo "start start mpush..." java -jar $base_dir/target/mpush/mpush-cs.jar & + echo "end start mpush..." From 92f1e3777005df38608acf23a2348d6f7ec71d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 18:00:27 +0800 Subject: [PATCH 280/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 12 ++++++++ debug.sh | 2 +- mpush-client/pom.xml | 2 +- ...m.shinemo.mpush.common.manage.ServerManage | 0 mpush-common/pom.xml | 28 ------------------- ...m.shinemo.mpush.common.manage.ServerManage | 1 - mpush-test/pom.xml | 28 +------------------ .../mpush/test/connection/client/Main.java | 2 +- ...m.shinemo.mpush.common.manage.ServerManage | 1 - pom.xml | 6 ++++ 10 files changed, 22 insertions(+), 60 deletions(-) create mode 100644 conf-dev.properties rename mpush-client/src/main/resources/{ => META-INF}/mpush/services/com.shinemo.mpush.common.manage.ServerManage (100%) delete mode 100644 mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage delete mode 100644 mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage diff --git a/conf-dev.properties b/conf-dev.properties new file mode 100644 index 00000000..3633f605 --- /dev/null +++ b/conf-dev.properties @@ -0,0 +1,12 @@ +#日志根目录 +log.home=/opt/logs/mpush +loglevel=warn +zk_ip = 127.0.0.1:2081 +zk_digest = shinemoIpo +zk_namespace = mpush-daily +redis_group = 127.0.0.1:6379:shinemoIpo +private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +force_write_redis_group_info = true +connection_server_port = 20882 +gateway_server_port = 4000 diff --git a/debug.sh b/debug.sh index c206a69e..dea7e89f 100755 --- a/debug.sh +++ b/debug.sh @@ -1,6 +1,6 @@ #!/bin/sh -ENV=daily +ENV=dev base_dir=`pwd` diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 17507657..5a93b22c 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -41,7 +41,7 @@ true - com.shinemo.mpush:mpush-test + com.shinemo.mpush:mpush-client diff --git a/mpush-client/src/main/resources/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage similarity index 100% rename from mpush-client/src/main/resources/mpush/services/com.shinemo.mpush.common.manage.ServerManage rename to mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 2f5c2a33..ceef2259 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -22,32 +22,4 @@ - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-common - - - - - - - - - diff --git a/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage deleted file mode 100644 index 97ab4667..00000000 --- a/mpush-common/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ /dev/null @@ -1 +0,0 @@ -gatewayServerManage=com.shinemo.mpush.common.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index a1051bb7..4dc68087 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -44,31 +44,5 @@ - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-test - - - - - - - - + diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java index 6afff5f8..44b226e1 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<10;i++){ + for(int i = 0;i<3;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage deleted file mode 100644 index 71f2823a..00000000 --- a/mpush-test/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ /dev/null @@ -1 +0,0 @@ -gatewayServerManage=com.shinemo.mpush.test.push.manage.impl.GatewayServerManage \ No newline at end of file diff --git a/pom.xml b/pom.xml index cf962651..db96e936 100644 --- a/pom.xml +++ b/pom.xml @@ -270,6 +270,12 @@ daily + + daily + + + + dev true From fa13590847193a8c4ef837b035a8ea52524acbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 18:22:49 +0800 Subject: [PATCH 281/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9zk=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 6 +++--- pom.xml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conf-dev.properties b/conf-dev.properties index 3633f605..5d1a49ea 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -1,7 +1,7 @@ #日志根目录 -log.home=/opt/logs/mpush -loglevel=warn -zk_ip = 127.0.0.1:2081 +log.home=/tmp/logs/mpush +loglevel=debug +zk_ip = 127.0.0.1:2181 zk_digest = shinemoIpo zk_namespace = mpush-daily redis_group = 127.0.0.1:6379:shinemoIpo diff --git a/pom.xml b/pom.xml index db96e936..70aae37a 100644 --- a/pom.xml +++ b/pom.xml @@ -280,7 +280,7 @@ true - daily + dev From 3fd9168d3871a437e48511228b60c973ad97cf51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Sun, 14 Feb 2016 18:29:09 +0800 Subject: [PATCH 282/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=BC=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/src/test/resources/config.properties | 1 + .../main/java/com/shinemo/mpush/tools/config/ConfigCenter.java | 1 + 2 files changed, 2 insertions(+) diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index cced99e9..643a4c1f 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -29,3 +29,4 @@ zk_digest = shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = 127.0.0.1:6379:shinemoIpo +force_write_redis_group_info = true diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index c01c84a5..22d1c8c5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -89,6 +89,7 @@ public interface ConfigCenter extends Config { List redisGroups(); @Key("force_write_redis_group_info") + @DefaultValue("true") boolean forceWriteRedisGroupInfo(); @Key("scan_conn_task_cycle") From f785261098c63a207573866b618940ef8fadb830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 14 Feb 2016 10:24:59 +0000 Subject: [PATCH 283/890] test --- .../java/com/shinemo/mpush/push/Main.java | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java b/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java index 9deb82c4..32143068 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java @@ -10,37 +10,37 @@ * Created by ohun on 2016/1/7. */ public class Main { - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - - client.send(Jsons.toJson(content), Arrays.asList("user-0", "8"), new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - }); - Thread.sleep(10000); - } - LockSupport.park(); - } + public static void main(String[] args) throws Exception { + PushClient client = new PushClient(); + client.start(); + Thread.sleep(1000); + for (int i = 0; i < 100; i++) { + PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); + + client.send(Jsons.toJson(content), Arrays.asList("user-0", "8", "43"), new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + }); + Thread.sleep(10000); + } + LockSupport.park(); + } } \ No newline at end of file From 8cb762f1f45f4849fe2070dc7917d2d8dae9d256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 14 Feb 2016 10:48:00 +0000 Subject: [PATCH 284/890] test --- .../java/com/shinemo/mpush/push/Main.java | 46 ------------- .../com/shinemo/mpush/test/push/Main.java | 64 +++++++++---------- 2 files changed, 32 insertions(+), 78 deletions(-) delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/Main.java diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java b/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java deleted file mode 100644 index 32143068..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/Main.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.shinemo.mpush.push; - -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.tools.Jsons; - -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; - -/** - * Created by ohun on 2016/1/7. - */ -public class Main { - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - - client.send(Jsons.toJson(content), Arrays.asList("user-0", "8", "43"), new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - }); - Thread.sleep(10000); - } - LockSupport.park(); - } - -} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index e321996e..2a38b18a 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -12,37 +12,37 @@ * Created by ohun on 2016/1/7. */ public class Main { - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); - - client.send(Jsons.toJson(content), Arrays.asList("user-0", "8"), new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - }); - Thread.sleep(10000); - } - LockSupport.park(); - } + public static void main(String[] args) throws Exception { + PushClient client = new PushClient(); + client.start(); + Thread.sleep(1000); + for (int i = 0; i < 100; i++) { + PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); + + client.send(Jsons.toJson(content), Arrays.asList("user-0", "8", "43"), new PushSender.Callback() { + @Override + public void onSuccess(String userId) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId) { + System.err.println("push onTimeout userId=" + userId); + } + }); + Thread.sleep(10000); + } + LockSupport.park(); + } } \ No newline at end of file From 876a429b639193a4e534fa24b24d41998bd4190b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 15 Feb 2016 09:19:13 +0800 Subject: [PATCH 285/890] =?UTF-8?q?connection=20server=20=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E7=BB=93=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/common/AbstractServer.java | 8 ++++---- .../mpush/{ps => test/configcenter}/ConfigCenterTest.java | 4 ++-- mpush-test/src/test/resources/config.properties | 2 +- .../java/com/shinemo/mpush/tools/config/ConfigCenter.java | 1 - 4 files changed, 7 insertions(+), 8 deletions(-) rename mpush-test/src/test/java/com/shinemo/mpush/{ps => test/configcenter}/ConfigCenterTest.java (56%) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index eb4a2381..04e3fe52 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -42,7 +42,7 @@ private Application getApplication() { try { return ((Class) GenericsUtil.getSuperClassGenericType(this.getClass(), 0)).newInstance(); } catch (Exception e) { - log.warn("exception:",e); + log.error("exception:",e); throw new RuntimeException(e); } } @@ -67,7 +67,8 @@ private void initRedis(){ zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } //强刷 - if(ConfigCenter.holder.forceWriteRedisGroupInfo()){ + boolean forceWriteRedisGroupInfo = ConfigCenter.holder.forceWriteRedisGroupInfo(); + if(forceWriteRedisGroupInfo){ List groupList = ConfigCenter.holder.redisGroups(); zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } @@ -112,8 +113,7 @@ public void onFailure(String message) { }); } }; - ThreadPoolManager.bizExecutor.execute(runnable); - + ThreadPoolManager.newThread(server.getClass().getSimpleName(), runnable).start(); } //step7 注册应用到zk diff --git a/mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java similarity index 56% rename from mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java rename to mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java index abaf4ca2..281a4ca7 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/ps/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.ps; +package com.shinemo.mpush.test.configcenter; import org.junit.Test; @@ -9,7 +9,7 @@ public class ConfigCenterTest { @Test public void test(){ - System.out.println(ConfigCenter.holder.zkIp()); + System.out.println(ConfigCenter.holder.forceWriteRedisGroupInfo()); } diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index 643a4c1f..79452096 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -29,4 +29,4 @@ zk_digest = shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = 127.0.0.1:6379:shinemoIpo -force_write_redis_group_info = true +force_write_redis_group_info = false diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 22d1c8c5..c01c84a5 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -89,7 +89,6 @@ public interface ConfigCenter extends Config { List redisGroups(); @Key("force_write_redis_group_info") - @DefaultValue("true") boolean forceWriteRedisGroupInfo(); @Key("scan_conn_task_cycle") From 9271be13fda5dcb264e4b7fc13aa2e5ca1ecf0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 15 Feb 2016 09:23:00 +0800 Subject: [PATCH 286/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9ip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf-dev.properties b/conf-dev.properties index 5d1a49ea..46f51cd7 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -1,10 +1,10 @@ #日志根目录 log.home=/tmp/logs/mpush loglevel=debug -zk_ip = 127.0.0.1:2181 +zk_ip = 10.1.80.57:2181 zk_digest = shinemoIpo zk_namespace = mpush-daily -redis_group = 127.0.0.1:6379:shinemoIpo +redis_group = 10.1.80.57:6379:shinemoIpo private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info = true From f5edcaf220190d6762836c31939917a9f6f29bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 15 Feb 2016 16:56:50 +0800 Subject: [PATCH 287/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/ConnectionClientMain.java | 25 +++++++++ .../client/ConnectionClientMain.java | 2 +- .../connection/mpns/ConnectionClientMain.java | 17 ++++++ .../mpush/test/connection/mpns/Main.java | 52 +++++++++++++++++++ .../src/test/resources/config.properties | 4 +- 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java diff --git a/mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java b/mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java new file mode 100644 index 00000000..d6a554ae --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java @@ -0,0 +1,25 @@ +package com.shinemo.mpush.test.connection.client; + +import java.util.List; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; +import com.shinemo.mpush.tools.spi.ServiceContainer; + +public class ConnectionClientMain extends AbstractClient { + + @SuppressWarnings("unchecked") + private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); + + public ConnectionClientMain() { + registerListener(new ConnectionServerPathListener()); + } + + public List getApplicationList(){ + return Lists.newArrayList(connectionServerManage.getList()); + } + +} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java index d6a554ae..aede62ea 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java @@ -22,4 +22,4 @@ public List getApplicationList(){ return Lists.newArrayList(connectionServerManage.getList()); } -} +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java new file mode 100644 index 00000000..51e83af3 --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java @@ -0,0 +1,17 @@ +package com.shinemo.mpush.test.connection.mpns; + +import java.util.List; + +import com.google.common.collect.Lists; +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.cs.ConnectionServerApplication; + +public class ConnectionClientMain extends AbstractClient { + + + private static final List applicationLists = Lists.newArrayList(new ConnectionServerApplication(20882,"","111.1.57.148")); + + public List getApplicationList(){ + return Lists.newArrayList(applicationLists); + } +} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java new file mode 100644 index 00000000..579e5932 --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java @@ -0,0 +1,52 @@ +package com.shinemo.mpush.test.connection.mpns; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.core.client.ClientChannelHandler; +import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.netty.client.NettyClientFactory; +import com.shinemo.mpush.netty.client.SecurityNettyClient; + +public class Main { + + public static void main(String[] args) throws InterruptedException { + + ConnectionClientMain main = new ConnectionClientMain(); + main.start(); + + List serverList = main.getApplicationList(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ConnectionServerApplication server = serverList.get(index); + + for(int i = 0;i<3;i++){ + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "user-"+i; + String deviceId = "test-device-id-"+i; + String cipher = ""; + byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); + byte[] iv = CipherBox.INSTANCE.randomAESIV(); + + SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); + client.setClientKey(clientKey); + client.setIv(iv); + client.setClientVersion(clientVersion); + client.setDeviceId(deviceId); + client.setOsName(osName); + client.setOsVersion(osVersion); + client.setUserId(userId); + client.setCipher(cipher); + + ClientChannelHandler handler = new ClientChannelHandler(client); + NettyClientFactory.INSTANCE.create(handler); + Thread.sleep(10); + } + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index 79452096..a40b7165 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -23,10 +23,10 @@ ras_key_length = 1024 ## session_expired_time = 86400 -zk_ip = 127.0.0.1:2181 +zk_ip = 115.29.169.109:5666 zk_namespace = mpush-daily zk_digest = shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 127.0.0.1:6379:shinemoIpo +redis_group = 111.1.57.148:6379:ShineMoIpo force_write_redis_group_info = false From ba999c071a3423133b0d48c93ffdd61b9d00d52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 15 Feb 2016 17:16:50 +0800 Subject: [PATCH 288/890] =?UTF-8?q?=E6=9A=82=E6=97=B6=E4=B8=8D=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/ConnectionClientMain.java | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java diff --git a/mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java b/mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java deleted file mode 100644 index d6a554ae..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/connection/client/ConnectionClientMain.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.shinemo.mpush.test.connection.client; - -import java.util.List; - -import com.google.common.collect.Lists; -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; -import com.shinemo.mpush.tools.spi.ServiceContainer; - -public class ConnectionClientMain extends AbstractClient { - - @SuppressWarnings("unchecked") - private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); - - public ConnectionClientMain() { - registerListener(new ConnectionServerPathListener()); - } - - public List getApplicationList(){ - return Lists.newArrayList(connectionServerManage.getList()); - } - -} From d898c907140d01a37f8e635f94599c21a3868cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 16 Feb 2016 14:47:37 +0800 Subject: [PATCH 289/890] redis uril --- debug.sh | 2 +- .../com/shinemo/mpush/api/PushContent.java | 49 +++++++++++++++++++ .../com/shinemo/mpush/push/PushContent.java | 14 ------ .../mpush/test/connection/mpns/Main.java | 4 +- .../com/shinemo/mpush/test/push/Main.java | 7 +-- .../shinemo/mpush/tools/redis/RedisUtil.java | 22 +++++++++ .../mpush/tools/redis/manage/RedisManage.java | 5 ++ 7 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java diff --git a/debug.sh b/debug.sh index dea7e89f..177b7c6c 100755 --- a/debug.sh +++ b/debug.sh @@ -12,6 +12,6 @@ cd $base_dir/target tar -xzvf ./mpush-jar-with-dependency.tar.gz echo "start start mpush..." -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -jar $base_dir/target/mpush/mpush-cs.jar & +java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -jar $base_dir/target/mpush/mpush-cs.jar & echo "end start mpush..." diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java new file mode 100644 index 00000000..250dfe1c --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.api; + +public final class PushContent { + public String msgId; //返回使用 + public String title; + public String content; //content + public int msgType; //type + + public PushContent(int msgType) { + this.msgType = msgType; + } + + public static PushContent build(int msgType){ + PushContent pushContent = new PushContent(msgType); + return pushContent; + } + + public String getMsgId() { + return msgId; + } + + public String getTitle() { + return title; + } + + public String getContent() { + return content; + } + + public int getMsgType() { + return msgType; + } + + public PushContent setTitle(String title) { + this.title = title; + return this; + } + + public PushContent setContent(String content) { + this.content = content; + return this; + } + + public void setMsgId(String msgId) { + this.msgId = msgId; + } + + +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java deleted file mode 100644 index 3b496dac..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushContent.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.shinemo.mpush.push; - -public class PushContent { - public String msgId; - public String title; - public String content; - public int msgType; - - public PushContent(String msgId, String title, String content) { - this.msgId = msgId; - this.title = title; - this.content = content; - } -} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java index 579e5932..d7aadec4 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java @@ -21,11 +21,11 @@ public static void main(String[] args) throws InterruptedException { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<3;i++){ + for(int i = 0;i<10000;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; - String userId = "user-"+i; + String userId = "uh-"+i; String deviceId = "test-device-id-"+i; String cipher = ""; byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index 2a38b18a..d868234c 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -1,8 +1,8 @@ package com.shinemo.mpush.test.push; +import com.shinemo.mpush.api.PushContent; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.push.PushClient; -import com.shinemo.mpush.push.PushContent; import com.shinemo.mpush.tools.Jsons; import java.util.Arrays; @@ -17,9 +17,10 @@ public static void main(String[] args) throws Exception { client.start(); Thread.sleep(1000); for (int i = 0; i < 100; i++) { - PushContent content = new PushContent("msgId_" + (i % 2), "MPush", "this a first push." + i); + PushContent content = PushContent.build(1).setContent("this a first push." + i).setTitle("MPush"); + content.setMsgId("msgId_" + (i % 2)); - client.send(Jsons.toJson(content), Arrays.asList("user-0", "8", "43"), new PushSender.Callback() { + client.send(Jsons.toJson(content), Arrays.asList("huang1", "huang2", "huang"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 22ff03b3..c98b9572 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -33,6 +33,28 @@ public static Jedis getClient(RedisNode node) { public static void close(Jedis jedis) { jedis.close(); } + + public static long incr(List nodeList,String key,Integer time){ + long incrRet = -1; + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + long ret = jedis.incr(key); + if(ret == 1 && time!=null){ + jedis.expire(key, time); + } + incrRet = ret; + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis incr exception:%s,%s,%s,%s",key,time,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + return incrRet; + + } /********************* k v redis start ********************************/ /** diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index b4d0848c..c274a758 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -22,6 +22,11 @@ public class RedisManage { private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + public static long incr(String key,Integer time){ + List nodeList = redisRegister.hashSet(key); + return RedisUtil.incr(nodeList, key, time); + } + /********************* * k v redis start ********************************/ From 88e125055c466f611e32242c1b5e9f5d4f04d42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 16 Feb 2016 15:01:27 +0800 Subject: [PATCH 290/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=8F=91?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/api/PushSender.java | 2 ++ .../main/java/com/shinemo/mpush/push/PushClient.java | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java index b4ec1bb9..71bf48d5 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java @@ -7,6 +7,8 @@ */ public interface PushSender { void send(String content, Collection userIds, Callback callback); + + void send(String content, String userId, Callback callback); interface Callback { void onSuccess(String userId); diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java index 1767ebc1..950170df 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java @@ -28,4 +28,15 @@ public void send(String content, Collection userIds, Callback callback) } } + @Override + public void send(String content, String userId, Callback callback) { + PushRequest + .build() + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(defaultTimeout) + .send(); + } + } From fb724d2708abf2f6559e7bb293cd01d09809c306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 16 Feb 2016 15:50:22 +0800 Subject: [PATCH 291/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/src/test/resources/config.properties | 2 ++ pom.xml | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index a40b7165..9cecd8f3 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -24,9 +24,11 @@ ras_key_length = 1024 session_expired_time = 86400 zk_ip = 115.29.169.109:5666 +#zk_ip = 127.0.0.1:2181 zk_namespace = mpush-daily zk_digest = shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = 111.1.57.148:6379:ShineMoIpo +#redis_group = 127.0.0.1:6379:shinemoIpo force_write_redis_group_info = false diff --git a/pom.xml b/pom.xml index 70aae37a..f5a0f588 100644 --- a/pom.xml +++ b/pom.xml @@ -246,7 +246,7 @@ - + From 3915b32a01790c637972f13d6cb945cb2c9967e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 16 Feb 2016 09:02:13 +0000 Subject: [PATCH 292/890] add http proxy --- .../shinemo/mpush/api/protocol/Command.java | 2 +- .../com/shinemo/mpush/common/DnsMapping.java | 39 +++ .../common/message/HttpRequestMessage.java | 94 ++++++ .../common/message/HttpResponseMessage.java | 57 ++++ .../core/client/ClientChannelHandler.java | 163 +++++----- .../mpush/core/handler/HttpProxyHandler.java | 169 ++++++++++ .../mpush/core/server/ConnectionServer.java | 1 + mpush-netty/pom.xml | 4 + .../mpush/netty/client/HttpCallback.java | 20 ++ .../mpush/netty/client/HttpClient.java | 15 + .../mpush/netty/client/NettyHttpClient.java | 289 ++++++++++++++++++ .../mpush/netty/client/RequestInfo.java | 49 +++ .../mpush/netty/server/NettyServer.java | 14 +- .../mpush/tools/config/ConfigCenter.java | 13 +- pom.xml | 5 + 15 files changed, 837 insertions(+), 97 deletions(-) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java create mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java index 6024a2e0..d1c34b98 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java @@ -15,7 +15,7 @@ public enum Command { RESUME(9), ERROR(10), OK(11), - API(12), + HTTP_PROXY(12), KICK(13), GATEWAY_KICK(14), PUSH(15), diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java b/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java new file mode 100644 index 00000000..ce9c88ed --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java @@ -0,0 +1,39 @@ +package com.shinemo.mpush.common; + +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.collect.ArrayListMultimap; +import com.shinemo.mpush.tools.config.ConfigCenter; + +import java.util.List; +import java.util.Map; + +/** + * Created by ohun on 2016/2/16. + */ +public final class DnsMapping { + private final ArrayListMultimap mappings = ArrayListMultimap.create(); + + public DnsMapping() { + String dnsString = ConfigCenter.holder.dnsMapping(); + if (Strings.isNullOrEmpty(dnsString)) return; + + Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); + Splitter vsp = Splitter.on(','); + for (Map.Entry entry : map.entrySet()) { + String value = entry.getValue(); + if (Strings.isNullOrEmpty(value)) continue; + Iterable it = vsp.split(entry.getValue()); + mappings.putAll(entry.getKey(), it); + } + } + + public String translate(String origin) { + if (mappings.isEmpty()) return null; + List list = mappings.get(origin); + if (list == null || list.isEmpty()) return null; + int L = list.size(); + if (L == 1) return list.get(0); + return list.get((int) (Math.random() * L % L)); + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java new file mode 100644 index 00000000..8e7168f2 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java @@ -0,0 +1,94 @@ +package com.shinemo.mpush.common.message; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by ohun on 2016/2/15. + */ +public class HttpRequestMessage extends ByteBufMessage { + public byte method; + public String uri; + public Map headers; + public byte[] body; + + public HttpRequestMessage(String uri, Connection connection) { + super(new Packet(Command.HTTP_PROXY, genSessionId()), connection); + this.uri = uri; + } + + public HttpRequestMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + method = decodeByte(body); + uri = decodeString(body); + headers = headerFromString(decodeString(body)); + this.body = decodeBytes(body); + } + + @Override + public void encode(ByteBuf body) { + encodeByte(body, method); + encodeString(body, uri); + encodeString(body, headerToString(headers)); + encodeBytes(body, this.body); + } + + public static String headerToString(Map headers) { + if (headers != null && headers.size() > 0) { + StringBuilder sb = new StringBuilder(headers.size() * 64); + for (Map.Entry entry : headers.entrySet()) { + sb.append(entry.getKey()) + .append(':') + .append(entry.getValue()).append('\n'); + } + return sb.toString(); + } + return null; + } + + + public static Map headerFromString(String headersString) { + if (headersString == null) return null; + Map headers = new HashMap<>(); + int L = headersString.length(); + String name, value = null; + for (int i = 0, start = 0; i < L; i++) { + char c = headersString.charAt(i); + if (c != '\n') continue; + if (start >= L - 1) break; + String header = headersString.substring(start, i); + start = i + 1; + int index = header.indexOf(':'); + if (index <= 0) continue; + name = header.substring(0, index); + if (index < header.length() - 1) { + value = header.substring(index + 1); + } + headers.put(name, value); + } + return headers; + } + + public String getMethod() { + switch (method) { + case 0: + return "GET"; + case 1: + return "POST"; + case 2: + return "PUT"; + case 3: + return "DELETE"; + } + return "GET"; + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java new file mode 100644 index 00000000..96031703 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java @@ -0,0 +1,57 @@ +package com.shinemo.mpush.common.message; + +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Command; +import com.shinemo.mpush.api.protocol.Packet; +import io.netty.buffer.ByteBuf; + +import java.util.HashMap; +import java.util.Map; + +import static com.shinemo.mpush.common.message.HttpRequestMessage.headerFromString; +import static com.shinemo.mpush.common.message.HttpRequestMessage.headerToString; + +/** + * Created by ohun on 2016/2/15. + */ +public class HttpResponseMessage extends ByteBufMessage { + public int statusCode; + public String reasonPhrase; + public Map headers = new HashMap<>(); + public byte[] body; + + public HttpResponseMessage(Packet message, Connection connection) { + super(message, connection); + } + + @Override + public void decode(ByteBuf body) { + headers = headerFromString(decodeString(body)); + this.body = decodeBytes(body); + } + + @Override + public void encode(ByteBuf body) { + encodeString(body, headerToString(headers)); + encodeBytes(body, this.body); + } + + public static HttpResponseMessage from(HttpRequestMessage src) { + return new HttpResponseMessage(src.createResponse(), src.connection); + } + + public HttpResponseMessage setStatusCode(int statusCode) { + this.statusCode = statusCode; + return this; + } + + public HttpResponseMessage setReasonPhrase(String reasonPhrase) { + this.reasonPhrase = reasonPhrase; + return this; + } + + public HttpResponseMessage addHeader(String name, String value) { + this.headers.put(name, value); + return this; + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index ed911ba5..1af87d26 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.core.client; - import java.util.Map; import com.google.common.collect.Maps; @@ -10,15 +9,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.common.message.BindUserMessage; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.FastConnectMessage; -import com.shinemo.mpush.common.message.FastConnectOkMessage; -import com.shinemo.mpush.common.message.HandshakeMessage; -import com.shinemo.mpush.common.message.HandshakeOkMessage; -import com.shinemo.mpush.common.message.KickUserMessage; -import com.shinemo.mpush.common.message.OkMessage; -import com.shinemo.mpush.common.message.PushMessage; +import com.shinemo.mpush.common.message.*; import com.shinemo.mpush.common.security.AesCipher; import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.netty.client.ChannelClientHandler; @@ -45,26 +36,26 @@ public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - + private Client client; - + public ClientChannelHandler(Client client) { - this.client = client; - } - + this.client = client; + } + @Override public Client getClient() { - return client; + return client; } - + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { client.getConnection().updateLastReadTime(); - if(client instanceof SecurityNettyClient){ - SecurityNettyClient securityNettyClient = (SecurityNettyClient)client; - Connection connection = client.getConnection(); - //加密 - if (msg instanceof Packet) { + if (client instanceof SecurityNettyClient) { + SecurityNettyClient securityNettyClient = (SecurityNettyClient) client; + Connection connection = client.getConnection(); + //加密 + if (msg instanceof Packet) { Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); if (command == Command.HANDSHAKE) { @@ -75,7 +66,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception client.startHeartBeat(message.heartbeat); LOGGER.info("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); - saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime,sessionKey); + saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); } else if (command == Command.FAST_CONNECT) { String cipherStr = securityNettyClient.getCipher(); String[] cs = cipherStr.split(","); @@ -89,7 +80,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.info("fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},",securityNettyClient.getUserId() , securityNettyClient.getDeviceId(), message); + LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); ctx.close(); } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); @@ -97,29 +88,31 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); LOGGER.info("receive an success packet=" + okMessage); + HttpRequestMessage message = new HttpRequestMessage("http://baidu.com", connection); + message.send(); } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); LOGGER.info("receive an push message, content=" + message.content); - }else if(command == Command.HEARTBEAT){ - LOGGER.info("receive an heart beat message"); - }else{ - LOGGER.info("receive an message, type=" + command.cmd+","+packet); + } else if (command == Command.HEARTBEAT) { + LOGGER.info("receive a heartbeat pong..."); + } else { + LOGGER.info("receive a message, type=" + command + "," + packet); } } - - }else if(client instanceof NettyClient){//不加密 - + + } else if (client instanceof NettyClient) {//不加密 + } - LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); + LOGGER.warn("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if(client instanceof SecurityNettyClient){ + if (client instanceof SecurityNettyClient) { NettyClientFactory.INSTANCE.remove(ctx.channel()); - }else{ - client.close("exception"); - } + } else { + client.close("exception"); + } LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @@ -128,41 +121,41 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); - - if(client instanceof SecurityNettyClient){ - NettyClientFactory.INSTANCE.put(ctx.channel(), client); - connection.init(ctx.channel(), true); + + if (client instanceof SecurityNettyClient) { + NettyClientFactory.INSTANCE.put(ctx.channel(), client); + connection.init(ctx.channel(), true); client.initConnection(connection); client.init(ctx.channel()); - tryFastConnect((SecurityNettyClient)client); - }else{ - LOGGER.error("connection is not support appear hear:"+ client); - connection.init(ctx.channel(), false); + tryFastConnect((SecurityNettyClient) client); + } else { + LOGGER.error("connection is not support appear hear:" + client); + connection.init(ctx.channel(), false); client.initConnection(connection); } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - if(client instanceof SecurityNettyClient){ - NettyClientFactory.INSTANCE.remove(ctx.channel()); - }else{ - client.close("inactive"); - } + if (client instanceof SecurityNettyClient) { + NettyClientFactory.INSTANCE.remove(ctx.channel()); + } else { + client.close("inactive"); + } LOGGER.info("client disconnect channel={}", ctx.channel()); } - + private void tryFastConnect(final SecurityNettyClient securityNettyClient) { - - Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); - + + Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); + if (sessionTickets == null) { - handshake(securityNettyClient); + handshake(securityNettyClient); return; } String sessionId = (String) sessionTickets.get("sessionId"); if (sessionId == null) { - handshake(securityNettyClient); + handshake(securityNettyClient); return; } String expireTime = (String) sessionTickets.get("expireTime"); @@ -173,55 +166,55 @@ private void tryFastConnect(final SecurityNettyClient securityNettyClient) { return; } } - + final String cipher = sessionTickets.get("cipherStr"); - + FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); message.deviceId = securityNettyClient.getDeviceId(); message.sessionId = sessionId; - + message.sendRaw(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { - securityNettyClient.setCipher(cipher); - }else{ - handshake(securityNettyClient); + securityNettyClient.setCipher(cipher); + } else { + handshake(securityNettyClient); } } }); } - + private void bindUser(SecurityNettyClient client) { BindUserMessage message = new BindUserMessage(client.getConnection()); message.userId = client.getUserId(); message.send(); } - private void saveToRedisForFastConnection(SecurityNettyClient client,String sessionId,Long expireTime,byte[] sessionKey){ - Map map = Maps.newHashMap(); - map.put("sessionId", sessionId); - map.put("expireTime", expireTime+""); - map.put("cipherStr", client.getConnection().getSessionContext().cipher.toString()); - String key = RedisKey.getDeviceIdKey(client.getDeviceId()); - RedisManage.set(key, map,60*5); //5分钟 + private void saveToRedisForFastConnection(SecurityNettyClient client, String sessionId, Long expireTime, byte[] sessionKey) { + Map map = Maps.newHashMap(); + map.put("sessionId", sessionId); + map.put("expireTime", expireTime + ""); + map.put("cipherStr", client.getConnection().getSessionContext().cipher.toString()); + String key = RedisKey.getDeviceIdKey(client.getDeviceId()); + RedisManage.set(key, map, 60 * 5); //5分钟 } - - private Map getFastConnectionInfo(String deviceId){ - String key = RedisKey.getDeviceIdKey(deviceId); - return RedisManage.get(key, Map.class); + + private Map getFastConnectionInfo(String deviceId) { + String key = RedisKey.getDeviceIdKey(deviceId); + return RedisManage.get(key, Map.class); } - + private void handshake(SecurityNettyClient client) { - HandshakeMessage message = new HandshakeMessage(client.getConnection()); - message.clientKey = client.getClientKey(); - message.iv = client.getIv(); - message.clientVersion = client.getClientVersion(); - message.deviceId = client.getDeviceId(); - message.osName = client.getOsName(); - message.osVersion = client.getOsVersion(); - message.timestamp = System.currentTimeMillis(); - message.send(); - } - + HandshakeMessage message = new HandshakeMessage(client.getConnection()); + message.clientKey = client.getClientKey(); + message.iv = client.getIv(); + message.clientVersion = client.getClientVersion(); + message.deviceId = client.getDeviceId(); + message.osName = client.getOsName(); + message.osVersion = client.getOsVersion(); + message.timestamp = System.currentTimeMillis(); + message.send(); + } + } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java new file mode 100644 index 00000000..0ec483d1 --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -0,0 +1,169 @@ +package com.shinemo.mpush.core.handler; + +import com.google.common.base.Strings; +import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.DnsMapping; +import com.shinemo.mpush.common.handler.BaseMessageHandler; +import com.shinemo.mpush.common.message.HttpRequestMessage; +import com.shinemo.mpush.common.message.HttpResponseMessage; +import com.shinemo.mpush.netty.client.HttpCallback; +import com.shinemo.mpush.netty.client.NettyHttpClient; +import com.shinemo.mpush.netty.client.RequestInfo; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.config.ConfigCenter; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.http.*; + +import java.net.InetSocketAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; + +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +/** + * Created by ohun on 2016/2/15. + */ +public class HttpProxyHandler extends BaseMessageHandler { + private final NettyHttpClient httpClient; + private final DnsMapping dnsMapping; + + public HttpProxyHandler() { + this.httpClient = new NettyHttpClient(); + this.dnsMapping = new DnsMapping(); + } + + @Override + public HttpRequestMessage decode(Packet packet, Connection connection) { + return new HttpRequestMessage(packet, connection); + } + + @Override + public void handle(HttpRequestMessage message) { + String method = message.getMethod(); + String uri = message.uri; + if (Strings.isNullOrEmpty(uri)) { + HttpResponseMessage + .from(message) + .setStatusCode(400) + .setReasonPhrase("Bad Request") + .sendRaw(); + } + + uri = doDnsMapping(uri); + FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); + setHeaders(request, message); + setBody(request, message); + + try { + httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); + } catch (Exception e) { + HttpResponseMessage + .from(message) + .setStatusCode(500) + .setReasonPhrase("Internal Server Error") + .sendRaw(); + } + } + + private static class DefaultHttpCallback implements HttpCallback { + private final HttpRequestMessage message; + private int redirectCount; + + private DefaultHttpCallback(HttpRequestMessage message) { + this.message = message; + } + + @Override + public void onResponse(HttpResponse httpResponse) { + HttpResponseMessage response = HttpResponseMessage + .from(message) + .setStatusCode(httpResponse.status().code()) + .setReasonPhrase(httpResponse.status().reasonPhrase().toString()); + for (Map.Entry entry : httpResponse.headers()) { + response.addHeader(entry.getKey().toString(), entry.getValue().toString()); + } + + if (httpResponse instanceof FullHttpResponse) { + ByteBuf body = ((FullHttpResponse) httpResponse).content(); + if (body != null && body.readableBytes() > 0) { + byte[] buffer = new byte[body.readableBytes()]; + body.readBytes(buffer); + response.body = buffer; + response.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), + Integer.toString(response.body.length)); + } + } + response.send(); + } + + @Override + public void onFailure(int statusCode, String reasonPhrase) { + HttpResponseMessage + .from(message) + .setStatusCode(statusCode) + .setReasonPhrase(reasonPhrase) + .sendRaw(); + } + + @Override + public void onException(Throwable throwable) { + HttpResponseMessage + .from(message) + .setStatusCode(500) + .setReasonPhrase("Internal Server Error") + .sendRaw(); + } + + @Override + public void onTimeout() { + HttpResponseMessage + .from(message) + .setStatusCode(408) + .setReasonPhrase("Request Timeout") + .sendRaw(); + } + + @Override + public boolean onRedirect(HttpResponse response) { + return redirectCount++ < 5; + } + } + + + private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { + Map headers = message.headers; + if (headers != null) { + HttpHeaders httpHeaders = request.headers(); + for (Map.Entry entry : headers.entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } + } + InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); + request.headers().add("x-forwarded-for", remoteAddress.getHostName() + "," + MPushUtil.getLocalIp()); + request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); + } + + private void setBody(FullHttpRequest request, HttpRequestMessage message) { + byte[] body = message.body; + if (body != null && body.length > 0) { + request.content().writeBytes(body); + request.headers().add(HttpHeaderNames.CONTENT_LENGTH, + Integer.toString(body.length)); + } + } + + private String doDnsMapping(String url) { + URI uri = null; + try { + uri = new URI(url); + } catch (URISyntaxException e) { + } + if (uri == null) return url; + String host = uri.getHost(); + String localHost = dnsMapping.translate(host); + if (localHost == null) return url; + return url.replace(host, localHost); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index d91c161c..6bda280e 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -34,6 +34,7 @@ public void init() { receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); + receiver.register(Command.HTTP_PROXY, new HttpProxyHandler()); connectionManager.init(); channelHandler = new ServerChannelHandler(true, connectionManager, receiver); } diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 9530505d..3a4f6731 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -31,5 +31,9 @@ netty-transport-native-epoll ${os.detected.classifier} + + io.netty + netty-codec-http + diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java new file mode 100644 index 00000000..379cf2f5 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java @@ -0,0 +1,20 @@ +package com.shinemo.mpush.netty.client; + + +import io.netty.handler.codec.http.HttpResponse; + +/** + * Created by ohun on 2016/2/15. + */ +public interface HttpCallback { + + void onResponse(HttpResponse response); + + void onFailure(int statusCode, String reasonPhrase); + + void onException(Throwable throwable); + + void onTimeout(); + + boolean onRedirect(HttpResponse response); +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java new file mode 100644 index 00000000..ae5f46f3 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java @@ -0,0 +1,15 @@ +package com.shinemo.mpush.netty.client; + +import io.netty.handler.codec.http.HttpRequest; + +/** + * Created by ohun on 2016/2/15. + */ +public interface HttpClient { + + void start(); + + void stop(); + + void request(RequestInfo requestInfo) throws Exception; +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java new file mode 100644 index 00000000..25cd1e21 --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -0,0 +1,289 @@ +package com.shinemo.mpush.netty.client; + +import com.google.common.collect.ArrayListMultimap; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.tools.thread.NamedThreadFactory; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.http.*; +import io.netty.util.AttributeKey; +import io.netty.util.HashedWheelTimer; +import io.netty.util.IllegalReferenceCountException; +import io.netty.util.Timer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.net.URL; +import java.net.URLDecoder; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +/** + * Created by ohun on 2016/2/15. + */ +public class NettyHttpClient implements HttpClient { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private Bootstrap b; + private EventLoopGroup workerGroup; + private Timer timer; + private final AttributeKey key = AttributeKey.newInstance("requestInfo"); + private final ArrayListMultimap channelPool = ArrayListMultimap.create(); + + @Override + public void start() { // TODO: 2016/2/15 yxx 配置线程池 + workerGroup = new NioEventLoopGroup(); + b = new Bootstrap(); + b.group(workerGroup); + b.channel(NioSocketChannel.class); + b.option(ChannelOption.SO_KEEPALIVE, true); + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast("decoder", new HttpResponseDecoder()); + ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 20)); + ch.pipeline().addLast("encoder", new HttpRequestEncoder()); + ch.pipeline().addLast("handler", new HttpClientHandler()); + } + }); + timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), + 1, TimeUnit.SECONDS, 64); + } + + @Override + public void stop() { + for (Channel channel : channelPool.values()) { + channel.close(); + } + channelPool.clear(); + workerGroup.shutdownGracefully(); + timer.stop(); + } + + @Override + public void request(final RequestInfo info) throws Exception { + HttpRequest request = info.request; + String url = request.uri(); + URI uri = new URI(url); + String host = uri.getHost(); + int port = uri.getPort() == -1 ? 80 : uri.getPort(); + info.host = host; + request.headers().set(HttpHeaderNames.HOST, host); + request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); + timer.newTimeout(info, info.timeout, TimeUnit.MILLISECONDS); + Channel channel = tryAcquire(host); + if (channel == null) { + LOGGER.debug("create new channel,host=" + host); + ChannelFuture f = b.connect(host, port); + f.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + writeRequest(future.channel(), info); + } else { + info.cancel(); + info.callback.onFailure(504, "Gateway Timeout"); + } + } + }); + } else { + writeRequest(channel, info); + } + } + + private synchronized Channel tryAcquire(String host) { + List channels = channelPool.get(host); + if (channels == null || channels.isEmpty()) return null; + Iterator it = channels.iterator(); + while (it.hasNext()) { + Channel channel = it.next(); + channelPool.remove(host, channel); + if (channel.isActive()) { + LOGGER.debug("tryAcquire channel success ,host=" + host); + return channel; + } + } + return null; + } + + private synchronized void tryRelease(Channel channel) { + String host = channel.attr(key).getAndRemove().host; + List channels = channelPool.get(host); + if (channels == null || channels.size() < 5) { + LOGGER.debug("tryRelease channel success ,host=" + host); + channelPool.put(host, channel); + } else { + LOGGER.debug("tryRelease channel failure ,host=" + host); + channel.close(); + } + } + + private void writeRequest(Channel channel, RequestInfo info) { + channel.attr(key).set(info); + channel.writeAndFlush(info.request).addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + RequestInfo requestInfo = future.channel().attr(key).get(); + requestInfo.cancel(); + requestInfo.callback.onFailure(503, "Service Unavailable"); + tryRelease(future.channel()); + } + } + }); + } + + + private class HttpClientHandler extends ChannelHandlerAdapter { + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + LOGGER.error("http client caught an error,", cause); + try { + RequestInfo info = ctx.channel().attr(key).get(); + if (info.cancel()) { + info.callback.onException(cause); + } + } finally { + tryRelease(ctx.channel()); + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + RequestInfo info = ctx.channel().attr(key).get(); + if (info == null) return; + try { + if (info.cancel()) { + HttpCallback callback = info.callback; + HttpRequest request = info.request; + HttpResponse response = (HttpResponse) msg; + if (isRedirect(response)) { + if (callback.onRedirect(response)) { + CharSequence location = getRedirectLocation(request, response); + if (location != null && location.length() > 0) { + info.cancelled.set(false); + info.request = copy(location.toString(), request); + request(info); + return; + } + } + } + callback.onResponse(response); + } + } finally { + tryRelease(ctx.channel()); + } + } + + private boolean isRedirect(HttpResponse response) { + HttpResponseStatus status = response.status(); + switch (status.code()) { + case 300: + case 301: + case 302: + case 303: + case 305: + case 307: + return true; + default: + return false; + } + } + + private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { + String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); + if (hdr != null) { + if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { + return hdr; + } else { + URL orig = new URL(request.uri()); + String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); + if (hdr.startsWith("/")) { + pth = hdr; + } else if (pth.endsWith("/")) { + pth += hdr; + } else { + pth += "/" + hdr; + } + StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); + sb.append("://").append(orig.getHost()); + if (orig.getPort() > 0) { + sb.append(":").append(orig.getPort()); + } + if (pth.charAt(0) != '/') { + sb.append('/'); + } + sb.append(pth); + return sb.toString(); + } + } + return null; + } + + + private HttpRequest copy(String uri, HttpRequest request) { + HttpRequest nue = request; + if (request instanceof DefaultFullHttpRequest) { + DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; + FullHttpRequest rq; + try { + rq = dfrq.copy(); + } catch (IllegalReferenceCountException e) { // Empty bytebuf + rq = dfrq; + } + rq.setUri(uri); + } else { + DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); + dfr.headers().set(request.headers()); + nue = dfr; + } + return nue; + } + + } + + + public static void main(String[] args) throws Exception { + NettyHttpClient client = new NettyHttpClient(); + client.start(); + for (int i = 0; i < 100; i++) { + LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)); + HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://baidu.com/"); + client.request(new RequestInfo(request, new HttpCallback() { + @Override + public void onResponse(HttpResponse response) { + System.out.println("response=" + response); + //System.out.println("content=" + ((FullHttpResponse) response).content().toString(Constants.UTF_8)); + } + + @Override + public void onFailure(int statusCode, String reasonPhrase) { + System.out.println("reasonPhrase=" + reasonPhrase); + } + + @Override + public void onException(Throwable throwable) { + throwable.printStackTrace(); + } + + @Override + public void onTimeout() { + System.out.println("onTimeout"); + } + + @Override + public boolean onRedirect(HttpResponse response) { + return true; + } + })); + } + LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10)); + client.stop(); + } +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java new file mode 100644 index 00000000..408d4efa --- /dev/null +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.netty.client; + +import io.netty.handler.codec.http.HttpRequest; +import io.netty.util.Timeout; +import io.netty.util.TimerTask; + +import java.util.concurrent.atomic.AtomicBoolean; + +public class RequestInfo implements TimerTask { + final AtomicBoolean cancelled = new AtomicBoolean(false); + HttpCallback callback; + HttpRequest request; + String host; + int timeout = 5000;//5s + + public RequestInfo(HttpRequest request, HttpCallback callback) { + this.callback = callback; + this.request = request; + } + + public HttpCallback getCallback() { + return callback; + } + + public HttpRequest getRequest() { + return request; + } + + public int getTimeout() { + return timeout; + } + + public RequestInfo setTimeout(int timeout) { + this.timeout = timeout; + return this; + } + + @Override + public void run(Timeout timeout) throws Exception { + if (!cancelled.get()) { + cancelled.set(true); + callback.onTimeout(); + } + } + + public boolean cancel() { + return cancelled.compareAndSet(false, true); + } +} \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 575260d8..853229bf 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -28,7 +28,7 @@ public abstract class NettyServer implements Server { public enum State {Created, Initialized, Starting, Started, Shutdown} - protected final AtomicReference serverState = new AtomicReference<>(State.Created); + protected final AtomicReference serverState = new AtomicReference<>(State.Created); private final int port; private EventLoopGroup bossGroup; @@ -36,7 +36,7 @@ public enum State {Created, Initialized, Starting, Started, Shutdown} public NettyServer(int port) { this.port = port; - + } public void init() { @@ -115,9 +115,9 @@ private void createServer(final Listener listener, EventLoopGroup boss, EventLoo b.childHandler(new ChannelInitializer() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(getChannelHandler()); + ch.pipeline().addLast("decoder", new PacketDecoder()); + ch.pipeline().addLast("encoder", PacketEncoder.INSTANCE); + ch.pipeline().addLast("handler", getChannelHandler()); } }); @@ -156,14 +156,14 @@ public void operationComplete(ChannelFuture future) throws Exception { } private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1,ThreadPoolManager.bossExecutor); + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolManager.bossExecutor); NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } @SuppressWarnings("unused") - private void createEpollServer(final Listener listener) { + private void createEpollServer(final Listener listener) { EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index c01c84a5..6da2782d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -7,6 +7,7 @@ import org.aeonbits.owner.ConfigFactory; import java.util.List; +import java.util.Map; /** * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 @@ -32,7 +33,8 @@ public interface ConfigCenter extends Config { int minHeartbeat(); @Key("max_heartbeat") - @DefaultValue("180000") //180秒 + @DefaultValue("180000") + //180秒 int maxHeartbeat(); @Key("max_hb_timeout_times") @@ -87,7 +89,7 @@ public interface ConfigCenter extends Config { @Key("redis_group") @ConverterClass(RedisGroupConverter.class) List redisGroups(); - + @Key("force_write_redis_group_info") boolean forceWriteRedisGroupInfo(); @@ -98,6 +100,9 @@ public interface ConfigCenter extends Config { @Key("jvm_log_path") @DefaultValue("/opt/shinemo/mpush/") String logPath(); - - + + @Key("dns_mapping") + String dnsMapping(); + + } diff --git a/pom.xml b/pom.xml index f5a0f588..095c1366 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,11 @@ ${netty.version} ${os.detected.classifier} + + io.netty + netty-codec-http + ${netty.version} + From d2e6041447604e4e69aab4e87ef8706aa8da7ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 16 Feb 2016 17:49:01 +0800 Subject: [PATCH 293/890] pom --- pom.xml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 095c1366..d78aff06 100644 --- a/pom.xml +++ b/pom.xml @@ -29,16 +29,16 @@ 1.7 - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT - 1.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT + 0.0.2-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 @@ -251,7 +251,7 @@ - + From 269f7bb2f8a6543657ab6dd0b6c2bb1301c41a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 16 Feb 2016 19:44:14 +0800 Subject: [PATCH 294/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/api/PushContent.java | 7 +++++-- start.sh | 0 2 files changed, 5 insertions(+), 2 deletions(-) mode change 100644 => 100755 start.sh diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java index 250dfe1c..d4d5d825 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java @@ -1,7 +1,10 @@ package com.shinemo.mpush.api; -public final class PushContent { - public String msgId; //返回使用 +import java.io.Serializable; + +public final class PushContent implements Serializable{ + private static final long serialVersionUID = -1805329333995385960L; + public String msgId; //返回使用 public String title; public String content; //content public int msgType; //type diff --git a/start.sh b/start.sh old mode 100644 new mode 100755 From 2d0685d14daf5a10369f3518ca2d9e280c5b813c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 16 Feb 2016 22:44:44 +0800 Subject: [PATCH 295/890] =?UTF-8?q?=E5=BC=BA=E5=88=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/exception/CryptoException.java | 14 +++++++++++++ .../common/handler/BaseMessageHandler.java | 20 +++++++++++++++---- .../mpush/core/handler/BindUserHandler.java | 4 +++- .../core/handler/FastConnectHandler.java | 5 +++++ .../core/handler/GatewayPushHandler.java | 5 +++++ .../mpush/core/handler/HandshakeHandler.java | 5 ++++- .../mpush/core/handler/HeartBeatHandler.java | 1 + .../mpush/core/handler/HttpProxyHandler.java | 2 +- 8 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java new file mode 100644 index 00000000..965b4e4f --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.api.exception; + +/** + * Created by ohun on 2015/12/23. + */ +public class CryptoException extends RuntimeException { + + private static final long serialVersionUID = 368277451733324220L; + + public CryptoException(String message) { + super(message); + } + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java index b40cc3ff..f9b2deed 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java @@ -1,9 +1,11 @@ package com.shinemo.mpush.common.handler; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.exception.CryptoException; import com.shinemo.mpush.api.protocol.Packet; /** @@ -15,9 +17,19 @@ public abstract class BaseMessageHandler implements MessageHa public abstract void handle(T message); public void handle(Packet packet, Connection connection) { - T t = decode(packet, connection); - if (t != null) { - handle(t); - } + if(checkCrypto(packet)){ + T t = decode(packet, connection); + if (t != null) { + handle(t); + } + } + } + + public boolean checkCrypto(Packet packet){ + if(packet.hasFlag(Constants.CRYPTO_FLAG)){ + return true; + }else{ + throw new CryptoException("need crypto"); + } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 5e9dbd43..16bea2c2 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -1,8 +1,10 @@ package com.shinemo.mpush.core.handler; import com.google.common.base.Strings; +import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.exception.CryptoException; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.BindUserMessage; @@ -19,7 +21,7 @@ public final class BindUserHandler extends BaseMessageHandler { @Override public BindUserMessage decode(Packet packet, Connection connection) { - return new BindUserMessage(packet, connection); + return new BindUserMessage(packet, connection); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 758d0f90..6f698c54 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -25,6 +25,11 @@ public final class FastConnectHandler extends BaseMessageHandler @Override public HandshakeMessage decode(Packet packet, Connection connection) { - return new HandshakeMessage(packet, connection); + return new HandshakeMessage(packet, connection); } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 35cd5663..59ce7ba3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -15,4 +15,5 @@ public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); } + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 0ec483d1..ba0617ba 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -33,7 +33,7 @@ public HttpProxyHandler() { this.httpClient = new NettyHttpClient(); this.dnsMapping = new DnsMapping(); } - + @Override public HttpRequestMessage decode(Packet packet, Connection connection) { return new HttpRequestMessage(packet, connection); From 6c780105b0d552924bb1dbdb5ee53dbf4832a7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 16 Feb 2016 15:23:53 +0000 Subject: [PATCH 296/890] add http proxy --- .../common/message/HttpRequestMessage.java | 43 ++----------------- .../common/message/HttpResponseMessage.java | 13 +++--- .../core/client/ClientChannelHandler.java | 3 +- .../mpush/core/handler/HttpProxyHandler.java | 1 - .../com/shinemo/mpush/tools/MPushUtil.java | 38 ++++++++++++++++ 5 files changed, 51 insertions(+), 47 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java index 8e7168f2..b017152e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java @@ -3,9 +3,9 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; -import java.util.HashMap; import java.util.Map; /** @@ -17,9 +17,8 @@ public class HttpRequestMessage extends ByteBufMessage { public Map headers; public byte[] body; - public HttpRequestMessage(String uri, Connection connection) { + public HttpRequestMessage(Connection connection) { super(new Packet(Command.HTTP_PROXY, genSessionId()), connection); - this.uri = uri; } public HttpRequestMessage(Packet message, Connection connection) { @@ -30,7 +29,7 @@ public HttpRequestMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { method = decodeByte(body); uri = decodeString(body); - headers = headerFromString(decodeString(body)); + headers = MPushUtil.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -38,45 +37,11 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeByte(body, method); encodeString(body, uri); - encodeString(body, headerToString(headers)); + encodeString(body, MPushUtil.headerToString(headers)); encodeBytes(body, this.body); } - public static String headerToString(Map headers) { - if (headers != null && headers.size() > 0) { - StringBuilder sb = new StringBuilder(headers.size() * 64); - for (Map.Entry entry : headers.entrySet()) { - sb.append(entry.getKey()) - .append(':') - .append(entry.getValue()).append('\n'); - } - return sb.toString(); - } - return null; - } - - public static Map headerFromString(String headersString) { - if (headersString == null) return null; - Map headers = new HashMap<>(); - int L = headersString.length(); - String name, value = null; - for (int i = 0, start = 0; i < L; i++) { - char c = headersString.charAt(i); - if (c != '\n') continue; - if (start >= L - 1) break; - String header = headersString.substring(start, i); - start = i + 1; - int index = header.indexOf(':'); - if (index <= 0) continue; - name = header.substring(0, index); - if (index < header.length() - 1) { - value = header.substring(index + 1); - } - headers.put(name, value); - } - return headers; - } public String getMethod() { switch (method) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java index 96031703..99d85d7b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java @@ -1,16 +1,13 @@ package com.shinemo.mpush.common.message; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; import java.util.HashMap; import java.util.Map; -import static com.shinemo.mpush.common.message.HttpRequestMessage.headerFromString; -import static com.shinemo.mpush.common.message.HttpRequestMessage.headerToString; - /** * Created by ohun on 2016/2/15. */ @@ -26,13 +23,17 @@ public HttpResponseMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { - headers = headerFromString(decodeString(body)); + statusCode = decodeInt(body); + reasonPhrase = decodeString(body); + headers = MPushUtil.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @Override public void encode(ByteBuf body) { - encodeString(body, headerToString(headers)); + encodeInt(body, statusCode); + encodeString(body, reasonPhrase); + encodeString(body, MPushUtil.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java index 1af87d26..f3eb55e1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java @@ -88,7 +88,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); LOGGER.info("receive an success packet=" + okMessage); - HttpRequestMessage message = new HttpRequestMessage("http://baidu.com", connection); + HttpRequestMessage message = new HttpRequestMessage(connection); + message.uri = "http://baidu.com"; message.send(); } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index ba0617ba..344d5fc8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -11,7 +11,6 @@ import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.client.RequestInfo; import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 4970861b..4c30eef9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -8,6 +8,8 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; /** * Created by ohun on 2015/12/25. @@ -58,4 +60,40 @@ public static String getInetAddress() { return "127.0.0.1"; } } + + public static String headerToString(Map headers) { + if (headers != null && headers.size() > 0) { + StringBuilder sb = new StringBuilder(headers.size() * 64); + for (Map.Entry entry : headers.entrySet()) { + sb.append(entry.getKey()) + .append(':') + .append(entry.getValue()).append('\n'); + } + return sb.toString(); + } + return null; + } + + + public static Map headerFromString(String headersString) { + if (headersString == null) return null; + Map headers = new HashMap<>(); + int L = headersString.length(); + String name, value = null; + for (int i = 0, start = 0; i < L; i++) { + char c = headersString.charAt(i); + if (c != '\n') continue; + if (start >= L - 1) break; + String header = headersString.substring(start, i); + start = i + 1; + int index = header.indexOf(':'); + if (index <= 0) continue; + name = header.substring(0, index); + if (index < header.length() - 1) { + value = header.substring(index + 1); + } + headers.put(name, value); + } + return headers; + } } From 8485e7fba4a0d4a762e750433987fba4c4bf43fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 16 Feb 2016 15:41:46 +0000 Subject: [PATCH 297/890] add http proxy --- .../common/handler/BaseMessageHandler.java | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java index f9b2deed..b40cc3ff 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java @@ -1,11 +1,9 @@ package com.shinemo.mpush.common.handler; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.exception.CryptoException; import com.shinemo.mpush.api.protocol.Packet; /** @@ -17,19 +15,9 @@ public abstract class BaseMessageHandler implements MessageHa public abstract void handle(T message); public void handle(Packet packet, Connection connection) { - if(checkCrypto(packet)){ - T t = decode(packet, connection); - if (t != null) { - handle(t); - } - } - } - - public boolean checkCrypto(Packet packet){ - if(packet.hasFlag(Constants.CRYPTO_FLAG)){ - return true; - }else{ - throw new CryptoException("need crypto"); - } + T t = decode(packet, connection); + if (t != null) { + handle(t); + } } } From c234e584e1bbb65ffc72e04d200ceed244a394b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 16 Feb 2016 15:45:21 +0000 Subject: [PATCH 298/890] add http proxy --- .../com/shinemo/mpush/core/handler/FastConnectHandler.java | 5 ----- .../com/shinemo/mpush/core/handler/GatewayPushHandler.java | 5 ----- 2 files changed, 10 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 6f698c54..104a207b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -26,11 +26,6 @@ public FastConnectMessage decode(Packet packet, Connection connection) { return new FastConnectMessage(packet, connection); } - @Override - public boolean checkCrypto(Packet packet) { - return true; - } - @Override public void handle(FastConnectMessage message) { //从缓存中心查询session diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index de8e0285..1b91cb52 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -30,11 +30,6 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { return new GatewayPushMessage(packet, connection); } - @Override - public boolean checkCrypto(Packet packet) { - return true; - } - /** * 处理PushClient发送过来的Push推送请求 *

From a5cd93a879b58bc599ddea197338de8dc8d5eeb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 02:20:28 +0000 Subject: [PATCH 299/890] add http proxy --- .../common/message/HttpRequestMessage.java | 13 +++++++++- .../common/message/HttpResponseMessage.java | 12 +++++++++ .../mpush/core/handler/HttpProxyHandler.java | 25 +++++++++++++------ .../mpush/netty/client/NettyHttpClient.java | 4 +-- .../mpush/netty/client/RequestInfo.java | 10 ++++++++ 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java index b017152e..6ba54a32 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java @@ -3,9 +3,11 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; +import java.util.Arrays; import java.util.Map; /** @@ -42,7 +44,6 @@ public void encode(ByteBuf body) { } - public String getMethod() { switch (method) { case 0: @@ -56,4 +57,14 @@ public String getMethod() { } return "GET"; } + + @Override + public String toString() { + return "HttpRequestMessage{" + + "method=" + method + + ", uri='" + uri + '\'' + + ", headers=" + headers + + ", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) + + '}'; + } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java index 99d85d7b..6ba56c0e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java @@ -2,9 +2,11 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -55,4 +57,14 @@ public HttpResponseMessage addHeader(String name, String value) { this.headers.put(name, value); return this; } + + @Override + public String toString() { + return "HttpResponseMessage{" + + "statusCode=" + statusCode + + ", reasonPhrase='" + reasonPhrase + '\'' + + ", headers=" + headers + + ", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) + + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 344d5fc8..5c2588a1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -13,6 +13,8 @@ import com.shinemo.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; import java.net.URI; @@ -25,6 +27,7 @@ * Created by ohun on 2016/2/15. */ public class HttpProxyHandler extends BaseMessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyHandler.class); private final NettyHttpClient httpClient; private final DnsMapping dnsMapping; @@ -32,7 +35,7 @@ public HttpProxyHandler() { this.httpClient = new NettyHttpClient(); this.dnsMapping = new DnsMapping(); } - + @Override public HttpRequestMessage decode(Packet packet, Connection connection) { return new HttpRequestMessage(packet, connection); @@ -48,6 +51,7 @@ public void handle(HttpRequestMessage message) { .setStatusCode(400) .setReasonPhrase("Bad Request") .sendRaw(); + LOGGER.warn("request url is empty!"); } uri = doDnsMapping(uri); @@ -63,21 +67,22 @@ public void handle(HttpRequestMessage message) { .setStatusCode(500) .setReasonPhrase("Internal Server Error") .sendRaw(); + LOGGER.error("send request ex, message=" + message, e); } } private static class DefaultHttpCallback implements HttpCallback { - private final HttpRequestMessage message; + private final HttpRequestMessage request; private int redirectCount; - private DefaultHttpCallback(HttpRequestMessage message) { - this.message = message; + private DefaultHttpCallback(HttpRequestMessage request) { + this.request = request; } @Override public void onResponse(HttpResponse httpResponse) { HttpResponseMessage response = HttpResponseMessage - .from(message) + .from(request) .setStatusCode(httpResponse.status().code()) .setReasonPhrase(httpResponse.status().reasonPhrase().toString()); for (Map.Entry entry : httpResponse.headers()) { @@ -95,33 +100,37 @@ public void onResponse(HttpResponse httpResponse) { } } response.send(); + LOGGER.debug("callback success request={}, response={}", request, response); } @Override public void onFailure(int statusCode, String reasonPhrase) { HttpResponseMessage - .from(message) + .from(request) .setStatusCode(statusCode) .setReasonPhrase(reasonPhrase) .sendRaw(); + LOGGER.warn("callback failure request={}, response={}", request, statusCode + ":" + reasonPhrase); } @Override public void onException(Throwable throwable) { HttpResponseMessage - .from(message) + .from(request) .setStatusCode(500) .setReasonPhrase("Internal Server Error") .sendRaw(); + LOGGER.error("callback exception request={}, response={}", request, 500, throwable); } @Override public void onTimeout() { HttpResponseMessage - .from(message) + .from(request) .setStatusCode(408) .setReasonPhrase("Request Timeout") .sendRaw(); + LOGGER.warn("callback timeout request={}, response={}", request, 408); } @Override diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 25cd1e21..7ee798fe 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -143,9 +143,9 @@ private class HttpClientHandler extends ChannelHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - LOGGER.error("http client caught an error,", cause); + RequestInfo info = ctx.channel().attr(key).get(); + LOGGER.error("http client caught an error, info=" + info, cause); try { - RequestInfo info = ctx.channel().attr(key).get(); if (info.cancel()) { info.callback.onException(cause); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index 408d4efa..0ceb9717 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -46,4 +46,14 @@ public void run(Timeout timeout) throws Exception { public boolean cancel() { return cancelled.compareAndSet(false, true); } + + @Override + public String toString() { + return "RequestInfo{" + + "cancelled=" + cancelled + + ", request=" + request + + ", host='" + host + '\'' + + ", timeout=" + timeout + + '}'; + } } \ No newline at end of file From 683bc661dd2b1f7069bb4ded91d4a21528955920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 02:28:04 +0000 Subject: [PATCH 300/890] add http proxy --- .../java/com/shinemo/mpush/core/handler/HttpProxyHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 5c2588a1..2bda1f23 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -34,6 +34,7 @@ public class HttpProxyHandler extends BaseMessageHandler { public HttpProxyHandler() { this.httpClient = new NettyHttpClient(); this.dnsMapping = new DnsMapping(); + this.httpClient.start(); } @Override From fecc9de750d88c3567f127bef53a7c725d8abf86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 10:44:39 +0800 Subject: [PATCH 301/890] remove netty --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index d78aff06..c91eabf5 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,12 @@ org.apache.curator curator-recipes 2.9.1 + + + netty + io.netty + + org.apache.curator From ae518f41480a7520972ef896d2568c86ea7fbd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 02:59:40 +0000 Subject: [PATCH 302/890] add http proxy --- .../mpush/netty/client/NettyHttpClient.java | 43 +------------------ 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 7ee798fe..cdaed07c 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.netty.client; import com.google.common.collect.ArrayListMultimap; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.tools.thread.NamedThreadFactory; import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; @@ -22,7 +21,6 @@ import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.LockSupport; /** * Created by ohun on 2016/2/15. @@ -102,7 +100,7 @@ private synchronized Channel tryAcquire(String host) { Iterator it = channels.iterator(); while (it.hasNext()) { Channel channel = it.next(); - channelPool.remove(host, channel); + it.remove(); if (channel.isActive()) { LOGGER.debug("tryAcquire channel success ,host=" + host); return channel; @@ -247,43 +245,4 @@ private HttpRequest copy(String uri, HttpRequest request) { } } - - - public static void main(String[] args) throws Exception { - NettyHttpClient client = new NettyHttpClient(); - client.start(); - for (int i = 0; i < 100; i++) { - LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)); - HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://baidu.com/"); - client.request(new RequestInfo(request, new HttpCallback() { - @Override - public void onResponse(HttpResponse response) { - System.out.println("response=" + response); - //System.out.println("content=" + ((FullHttpResponse) response).content().toString(Constants.UTF_8)); - } - - @Override - public void onFailure(int statusCode, String reasonPhrase) { - System.out.println("reasonPhrase=" + reasonPhrase); - } - - @Override - public void onException(Throwable throwable) { - throwable.printStackTrace(); - } - - @Override - public void onTimeout() { - System.out.println("onTimeout"); - } - - @Override - public boolean onRedirect(HttpResponse response) { - return true; - } - })); - } - LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10)); - client.stop(); - } } From 2a3c5a96f3bcf1484fd4a816cf4bd02cd64c2986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 11:41:15 +0800 Subject: [PATCH 303/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9log=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-daily.properties | 2 +- debug.sh | 6 ++++-- start.sh | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 62828ee9..6ef02744 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -1,6 +1,6 @@ #日志根目录 log.home=/opt/logs/mpush -loglevel=warn +loglevel=debug zk_ip = 115.29.169.109:5666 zk_digest = shinemoIpo zk_namespace = mpush-daily diff --git a/debug.sh b/debug.sh index 177b7c6c..aa595e1f 100755 --- a/debug.sh +++ b/debug.sh @@ -5,13 +5,15 @@ ENV=dev base_dir=`pwd` echo "start assembly lib..." -mvn clean assembly:assembly -P $ENV +mvn clean install -P $ENV echo "start tar mpush..." cd $base_dir/target tar -xzvf ./mpush-jar-with-dependency.tar.gz echo "start start mpush..." -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -jar $base_dir/target/mpush/mpush-cs.jar & +cd mpush/lib + +java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -jar $base_dir/target/mpush/mpush-cs.jar & echo "end start mpush..." diff --git a/start.sh b/start.sh index b6e94066..e214f71f 100755 --- a/start.sh +++ b/start.sh @@ -5,7 +5,7 @@ ENV=daily base_dir=`pwd` echo "start assembly lib..." -mvn clean assembly:assembly -P $ENV +mvn clean install -P $ENV echo "start tar mpush..." cd $base_dir/target From e7749a7c74745e159cc6fe6d0923a2263a42d3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:04:22 +0000 Subject: [PATCH 304/890] add http proxy --- conf-daily.properties | 1 + conf-dev.properties | 1 + conf-online.properties | 1 + conf-pre.properties | 1 + mpush-cs/src/main/resources/config.properties | 1 + .../shinemo/mpush/netty/client/NettyHttpClient.java | 13 ++++++++----- .../com/shinemo/mpush/netty/client/RequestInfo.java | 11 +++++++---- 7 files changed, 20 insertions(+), 9 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 6ef02744..1aa1a2c3 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 +dns_mapping=111.1.57.148=127.0.0.1 \ No newline at end of file diff --git a/conf-dev.properties b/conf-dev.properties index 46f51cd7..e689ed74 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 +dns_mapping=127.0.0.1=127.0.0.1 \ No newline at end of file diff --git a/conf-online.properties b/conf-online.properties index bb633fb2..d55b1358 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 +dns_mapping=127.0.0.1=127.0.0.1 \ No newline at end of file diff --git a/conf-pre.properties b/conf-pre.properties index d9ff0381..0d33492a 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 +dns_mapping=127.0.0.1=127.0.0.1 \ No newline at end of file diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-cs/src/main/resources/config.properties index 48e4494b..f903dc30 100644 --- a/mpush-cs/src/main/resources/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -33,3 +33,4 @@ zk_digest = ${zk_digest} redis_group = ${redis_group} force_write_redis_group_info = ${force_write_redis_group_info} jvm_log_path = /opt/shinemo/mpush/ +dns_mapping=${dns_mapping} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index cdaed07c..a86924f1 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -84,8 +84,9 @@ public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { writeRequest(future.channel(), info); } else { - info.cancel(); + info.tryDone(); info.callback.onFailure(504, "Gateway Timeout"); + LOGGER.debug("request failure request=%s", info); } } }); @@ -128,8 +129,9 @@ private void writeRequest(Channel channel, RequestInfo info) { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { RequestInfo requestInfo = future.channel().attr(key).get(); - requestInfo.cancel(); + requestInfo.tryDone(); requestInfo.callback.onFailure(503, "Service Unavailable"); + LOGGER.debug("request failure request=%s", requestInfo); tryRelease(future.channel()); } } @@ -142,9 +144,9 @@ private class HttpClientHandler extends ChannelHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { RequestInfo info = ctx.channel().attr(key).get(); - LOGGER.error("http client caught an error, info=" + info, cause); + LOGGER.error("http client caught an error, info=%s", info, cause); try { - if (info.cancel()) { + if (info.tryDone()) { info.callback.onException(cause); } } finally { @@ -157,7 +159,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception RequestInfo info = ctx.channel().attr(key).get(); if (info == null) return; try { - if (info.cancel()) { + if (info.tryDone()) { HttpCallback callback = info.callback; HttpRequest request = info.request; HttpResponse response = (HttpResponse) msg; @@ -173,6 +175,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } callback.onResponse(response); + LOGGER.debug("request done request=%s", info); } } finally { tryRelease(ctx.channel()); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index 0ceb9717..0f591bcf 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -11,7 +11,9 @@ public class RequestInfo implements TimerTask { HttpCallback callback; HttpRequest request; String host; - int timeout = 5000;//5s + int timeout = 10000;//5s + long startTime = System.currentTimeMillis(); + long endTime = System.currentTimeMillis(); public RequestInfo(HttpRequest request, HttpCallback callback) { this.callback = callback; @@ -37,13 +39,13 @@ public RequestInfo setTimeout(int timeout) { @Override public void run(Timeout timeout) throws Exception { - if (!cancelled.get()) { - cancelled.set(true); + if (tryDone()) { callback.onTimeout(); } } - public boolean cancel() { + public boolean tryDone() { + endTime = System.currentTimeMillis(); return cancelled.compareAndSet(false, true); } @@ -54,6 +56,7 @@ public String toString() { ", request=" + request + ", host='" + host + '\'' + ", timeout=" + timeout + + ", costTime=" + (startTime - endTime) + '}'; } } \ No newline at end of file From 0e40583cf22b9911d1b0c5ed42851909301c7417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:09:49 +0000 Subject: [PATCH 305/890] add http proxy --- conf-dev.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf-dev.properties b/conf-dev.properties index e689ed74..eec3faae 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -10,4 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 -dns_mapping=127.0.0.1=127.0.0.1 \ No newline at end of file +dns_mapping=111.1.57.148=127.0.0.1 \ No newline at end of file From b0adf3420ce34684892b6ca533318c78b89fd929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:15:32 +0000 Subject: [PATCH 306/890] change log level --- mpush-cs/src/main/resources/logback.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index f3aef748..e8187b28 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -6,7 +6,7 @@ class="ch.qos.logback.core.rolling.RollingFileAppender"> ${log.home}/mpush.log - warn + debug true From af5f98302d6ed314b8a38f44e8e5aab3ca67050c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:31:31 +0000 Subject: [PATCH 307/890] change log level --- .../shinemo/mpush/common/message/HttpRequestMessage.java | 2 +- .../com/shinemo/mpush/netty/client/NettyHttpClient.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java index 6ba54a32..192a9765 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java @@ -64,7 +64,7 @@ public String toString() { "method=" + method + ", uri='" + uri + '\'' + ", headers=" + headers + - ", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) + + ", body=" + (body == null ? "" : body.length) + '}'; } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index a86924f1..8f19524d 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -131,7 +131,7 @@ public void operationComplete(ChannelFuture future) throws Exception { RequestInfo requestInfo = future.channel().attr(key).get(); requestInfo.tryDone(); requestInfo.callback.onFailure(503, "Service Unavailable"); - LOGGER.debug("request failure request=%s", requestInfo); + LOGGER.debug("request failure request={}", requestInfo); tryRelease(future.channel()); } } @@ -144,7 +144,7 @@ private class HttpClientHandler extends ChannelHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { RequestInfo info = ctx.channel().attr(key).get(); - LOGGER.error("http client caught an error, info=%s", info, cause); + LOGGER.error("http client caught an error, info={}", info, cause); try { if (info.tryDone()) { info.callback.onException(cause); @@ -175,7 +175,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } callback.onResponse(response); - LOGGER.debug("request done request=%s", info); + LOGGER.debug("request done request={}", info); } } finally { tryRelease(ctx.channel()); From ab1c92ce9cf30b63efd0ce45313ff37354959235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:31:49 +0000 Subject: [PATCH 308/890] change log level --- .../com/shinemo/mpush/common/message/HttpResponseMessage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java index 6ba56c0e..806c848b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java @@ -64,7 +64,7 @@ public String toString() { "statusCode=" + statusCode + ", reasonPhrase='" + reasonPhrase + '\'' + ", headers=" + headers + - ", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) + + ", body=" + (body == null ? "" : body.length) + '}'; } } From e70249873f548676d85c80eb6a6191fd92b8eb01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:47:26 +0000 Subject: [PATCH 309/890] change log level --- .../main/java/com/shinemo/mpush/netty/client/RequestInfo.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index 0f591bcf..54c9416a 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -53,10 +53,9 @@ public boolean tryDone() { public String toString() { return "RequestInfo{" + "cancelled=" + cancelled + - ", request=" + request + ", host='" + host + '\'' + ", timeout=" + timeout + - ", costTime=" + (startTime - endTime) + + ", costTime=" + (endTime - startTime) + '}'; } } \ No newline at end of file From 840e2f7431e0c61ef3028edf702e76ccc584100e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 17 Feb 2016 05:56:50 +0000 Subject: [PATCH 310/890] change log level --- .../main/java/com/shinemo/mpush/netty/client/RequestInfo.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index 54c9416a..47d04cfe 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -53,6 +53,7 @@ public boolean tryDone() { public String toString() { return "RequestInfo{" + "cancelled=" + cancelled + + ", uri='" + request.uri() + '\'' + ", host='" + host + '\'' + ", timeout=" + timeout + ", costTime=" + (endTime - startTime) + From 5933279a3d852c90c9d3153ff66452216a1877dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 13:58:22 +0800 Subject: [PATCH 311/890] =?UTF-8?q?log=20=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-cs/src/main/resources/logback.xml | 32 ++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index e8187b28..a9f8bb80 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -38,16 +38,16 @@ - + - + ${log.home}/mpush-connection.log - info + warn true @@ -61,16 +61,16 @@ - + - + ${log.home}/mpush-push.log - info + warn true @@ -84,16 +84,16 @@ - + - + ${log.home}/mpush-heartbeat.log - info + warn true @@ -107,16 +107,16 @@ - + - + ${log.home}/mpush-redis.log - info + warn true @@ -130,16 +130,16 @@ - + - + ${log.home}/mpush-zk.log - info + warn true @@ -153,7 +153,7 @@ - + From f5478a1712029528dd2973bb26a311a8f11134ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 14:07:07 +0800 Subject: [PATCH 312/890] debug --- mpush-cs/src/main/resources/logback.xml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index a9f8bb80..6b852c40 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -24,7 +24,7 @@ ${log.home}/mpush-monitor.log - info + debug true @@ -38,7 +38,7 @@ - + @@ -47,7 +47,7 @@ ${log.home}/mpush-connection.log - warn + debug true @@ -61,7 +61,7 @@ - + @@ -70,7 +70,7 @@ ${log.home}/mpush-push.log - warn + debug true @@ -84,7 +84,7 @@ - + @@ -93,7 +93,7 @@ ${log.home}/mpush-heartbeat.log - warn + debug true @@ -107,7 +107,7 @@ - + @@ -116,7 +116,7 @@ ${log.home}/mpush-redis.log - warn + debug true @@ -130,7 +130,7 @@ - + @@ -139,7 +139,7 @@ ${log.home}/mpush-zk.log - warn + debug true @@ -153,7 +153,7 @@ - + From f8d0cfa4dd44d4d4b93357ac719e8760f88e1818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 14:21:34 +0800 Subject: [PATCH 313/890] log back --- mpush-cs/src/main/resources/logback.xml | 27 ++++++------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index 6b852c40..ea1a64cf 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -1,12 +1,15 @@ - ${log.home}/mpush.log - - debug + + + message.contains("org.apache.zookeeper.ClientCnxn") + + NEUTRAL + DENY true @@ -23,9 +26,6 @@ ${log.home}/mpush-monitor.log - - debug - true ${log.home}/mpush-monitor.log.%d{yyyy-MM-dd} @@ -46,9 +46,6 @@ ${log.home}/mpush-connection.log - - debug - true ${log.home}/mpush-connection.log.%d{yyyy-MM-dd} @@ -69,9 +66,6 @@ ${log.home}/mpush-push.log - - debug - true ${log.home}/mpush-push.log.%d{yyyy-MM-dd} @@ -92,9 +86,6 @@ ${log.home}/mpush-heartbeat.log - - debug - true ${log.home}/mpush-heartbeat.log.%d{yyyy-MM-dd} @@ -115,9 +106,6 @@ ${log.home}/mpush-redis.log - - debug - true ${log.home}/mpush-redis.log.%d{yyyy-MM-dd} @@ -138,9 +126,6 @@ ${log.home}/mpush-zk.log - - debug - true ${log.home}/mpush-zk.log.%d{yyyy-MM-dd} From 36b56382263d3e7884aad3dd39f284f025ef144b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 14:41:46 +0800 Subject: [PATCH 314/890] deny zk log --- mpush-cs/src/main/resources/logback.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index ea1a64cf..bb055864 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -6,9 +6,9 @@ ${log.home}/mpush.log - message.contains("org.apache.zookeeper.ClientCnxn") + logger.getName().contains("Got ping response") - NEUTRAL + ACCEPT DENY true From d326362b0abdc5d7089d7df4d814a2bebbc1c250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 14:44:59 +0800 Subject: [PATCH 315/890] add janino --- mpush-cs/pom.xml | 4 ++++ pom.xml | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 3c46c3cd..fffd8a66 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -21,6 +21,10 @@ com.shinemo.mpush mpush-monitor + + org.codehaus.janino + janino + diff --git a/pom.xml b/pom.xml index c91eabf5..b6a05cb2 100644 --- a/pom.xml +++ b/pom.xml @@ -213,6 +213,12 @@ 1.0.9 + + org.codehaus.janino + janino + 2.7.8 + + From 35fe0729110450fe3aa8866eebf18de4c23942a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 14:57:20 +0800 Subject: [PATCH 316/890] log --- mpush-cs/src/main/resources/logback.xml | 54 +++++++++++++++++++++---- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index bb055864..f2994f26 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -1,16 +1,49 @@ + + + + ${log.home}/mpush.error.log + + error + ACCEPT + DENY + + true + + ${log.home}/mpush.error.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + ${log.home}/mpush.info.log + + info + ACCEPT + DENY + + true + + ${log.home}/mpush.info.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + ${log.home}/mpush.log - - - logger.getName().contains("Got ping response") - - ACCEPT - DENY - true ${log.home}/mpush.log.%d{yyyy-MM-dd} @@ -82,6 +115,11 @@ + + + + + @@ -145,5 +183,7 @@ + + From ec5cbb04d04b1db967f55a051401ef687e7fe3a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 17 Feb 2016 21:21:45 +0800 Subject: [PATCH 317/890] add payload --- .../com/shinemo/mpush/api/PushContent.java | 47 +++++++------ .../mpush/api/payload/BasePayload.java | 64 ++++++++++++++++++ .../mpush/api/payload/CustomPushPayload.java | 26 +++++++ .../api/payload/NotificationPushPayload.java | 40 +++++++++++ .../shinemo/mpush/api/payload/Payload.java | 7 ++ .../mpush/api/payload/PayloadFactory.java | 39 +++++++++++ .../mpush/api/payload/PushContent.java | 67 +++++++++++++++++++ .../com/shinemo/mpush/test/gson/GsonTest.java | 63 +++++++++++++++++ .../mpush/test/payload/PayloadTest.java | 42 ++++++++++++ .../com/shinemo/mpush/test/push/Main.java | 2 +- start.sh | 5 +- 11 files changed, 378 insertions(+), 24 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java index d4d5d825..c62d7960 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java @@ -2,19 +2,33 @@ import java.io.Serializable; + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + * + * + */ public final class PushContent implements Serializable{ private static final long serialVersionUID = -1805329333995385960L; - public String msgId; //返回使用 - public String title; - public String content; //content - public int msgType; //type + private String msgId; //返回使用 + private String content; //content + private int msgType; //type public PushContent(int msgType) { this.msgType = msgType; } - public static PushContent build(int msgType){ + public static PushContent build(int msgType,String content){ PushContent pushContent = new PushContent(msgType); + pushContent.setContent(content); return pushContent; } @@ -22,31 +36,20 @@ public String getMsgId() { return msgId; } - public String getTitle() { - return title; - } - - public String getContent() { - return content; - } - public int getMsgType() { return msgType; } - public PushContent setTitle(String title) { - this.title = title; - return this; + public void setMsgId(String msgId) { + this.msgId = msgId; } - public PushContent setContent(String content) { - this.content = content; - return this; + public String getContent() { + return content; } - public void setMsgId(String msgId) { - this.msgId = msgId; + public void setContent(String content) { + this.content = content; } - } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java new file mode 100644 index 00000000..06e2b8cb --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java @@ -0,0 +1,64 @@ +package com.shinemo.mpush.api.payload; + + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + * + * + */ +@SuppressWarnings("unchecked") +public class BasePayload implements Payload{ + + private static final long serialVersionUID = 2367820805720853376L; + /*****以下非必填**/ + private Long nid; //主要用于聚合通知,非必填 + private Byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯 + private String largeIcon; // 大图标 + private String ticker; //和title一样 + private Integer number; // + public Long getNid() { + return nid; + } + public T setNid(Long nid) { + this.nid = nid; + return (T)this; + } + public Byte getFlags() { + return flags; + } + public T setFlags(Byte flags) { + this.flags = flags; + return (T)this; + } + public String getLargeIcon() { + return largeIcon; + } + public T setLargeIcon(String largeIcon) { + this.largeIcon = largeIcon; + return (T)this; + } + public String getTicker() { + return ticker; + } + public T setTicker(String ticker) { + this.ticker = ticker; + return (T)this; + } + public Integer getNumber() { + return number; + } + public T setNumber(Integer number) { + this.number = number; + return (T)this; + } + +} + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java new file mode 100644 index 00000000..531d36e1 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.api.payload; + +import java.util.HashMap; + + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + * + * + */ +public class CustomPushPayload extends HashMap implements Payload { + + private static final long serialVersionUID = 6076731078625705602L; + + + +} + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java new file mode 100644 index 00000000..361e1b41 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.api.payload; + + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + * + * + */ +public class NotificationPushPayload extends BasePayload implements Payload{ + + private static final long serialVersionUID = 4363667286689742483L; + private String title; + private String content; + public String getTitle() { + return title; + } + public NotificationPushPayload setTitle(String title) { + this.title = title; + return this; + } + public String getContent() { + return content; + } + public NotificationPushPayload setContent(String content) { + this.content = content; + return this; + } + + + +} + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java new file mode 100644 index 00000000..e3a99305 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java @@ -0,0 +1,7 @@ +package com.shinemo.mpush.api.payload; + +import java.io.Serializable; + +public interface Payload extends Serializable{ + +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java new file mode 100644 index 00000000..9a97728f --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java @@ -0,0 +1,39 @@ +package com.shinemo.mpush.api.payload; + + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + * + * + */ +@SuppressWarnings("unchecked") +public class PayloadFactory{ + + public static T create(MessageType messageType){ + + Payload payload = null; + if(messageType.equals(MessageType.NOTIFICATION)){ + payload = new NotificationPushPayload(); + }else if(messageType.equals(MessageType.CUSTOM)){ + payload = new CustomPushPayload(); + } + if(payload!=null){ + return (T)payload; + } + return null; + } + + public enum MessageType{ + NOTIFICATION,CUSTOM; + } + +} + diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java new file mode 100644 index 00000000..bd07837e --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java @@ -0,0 +1,67 @@ +//package com.shinemo.mpush.api; +// +//import java.io.Serializable; +// +///** +// * msgId、msgType 必填 +// * msgType=1 :nofication,提醒。 +// * 必填:title,content。没有title,则为应用名称。 +// * 非必填。nid 通知id,主要用于聚合通知。 +// * content 为push message。附加的一些业务属性,都在里边。json格式 +// * msgType=2 :非通知消息。不在通知栏展示。 +// * 必填:content。 +// * msgType=3 :消息+提醒 +// * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 +// * +// * +// */ +//public final class PushContent implements Serializable{ +// private static final long serialVersionUID = -1805329333995385960L; +// private String msgId; //返回使用 +// private String title; //没有title,则为应用名称。 +// private String content; //content +// private int msgType; //type +// +// +// +// public PushContent(int msgType) { +// this.msgType = msgType; +// } +// +// public static PushContent build(int msgType){ +// PushContent pushContent = new PushContent(msgType); +// return pushContent; +// } +// +// public String getMsgId() { +// return msgId; +// } +// +// public String getTitle() { +// return title; +// } +// +// public String getContent() { +// return content; +// } +// +// public int getMsgType() { +// return msgType; +// } +// +// public PushContent setTitle(String title) { +// this.title = title; +// return this; +// } +// +// public PushContent setContent(String content) { +// this.content = content; +// return this; +// } +// +// public void setMsgId(String msgId) { +// this.msgId = msgId; +// } +// +// +//} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java new file mode 100644 index 00000000..f488153d --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java @@ -0,0 +1,63 @@ +package com.shinemo.mpush.test.gson; + +import java.util.Map; + +import org.junit.Test; +import com.google.common.collect.Maps; +import com.google.gson.JsonObject; +import com.shinemo.mpush.api.PushContent; +import com.shinemo.mpush.tools.Jsons; + +public class GsonTest { + + @Test + public void test(){ + Map map = Maps.newHashMap(); + map.put("key1", 1121+""); + map.put("key2", "value2"); + + PushContent content = PushContent.build(1,Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + } + + @Test + public void test2(){ + ValueMap map = new ValueMap("1122", "value2"); + + PushContent content = PushContent.build(1,Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); + + System.out.println(newContetn.getContent()); + + + } + + private static class ValueMap{ + + private String key1; + private String key2; + + public ValueMap(String key1, String key2) { + this.key1 = key1; + this.key2 = key2; + } + + public String getKey1() { + return key1; + } + + public String getKey2() { + return key2; + } + + + } + +} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java new file mode 100644 index 00000000..bb521590 --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java @@ -0,0 +1,42 @@ +package com.shinemo.mpush.test.payload; + +import java.util.Map; + +import org.junit.Test; + +import com.google.common.collect.Maps; +import com.shinemo.mpush.api.PushContent; +import com.shinemo.mpush.api.payload.CustomPushPayload; +import com.shinemo.mpush.api.payload.NotificationPushPayload; +import com.shinemo.mpush.api.payload.PayloadFactory; +import com.shinemo.mpush.api.payload.PayloadFactory.MessageType; +import com.shinemo.mpush.tools.Jsons; + +public class PayloadTest { + + @Test + public void customTest(){ + + Map map = Maps.newHashMap(); + map.put("key1", "value1"); + map.put("key2", "value2"); + + CustomPushPayload customPayload = PayloadFactory.create(MessageType.CUSTOM); + customPayload.putAll(map); + + PushContent pushContent = PushContent.build(1, Jsons.toJson(customPayload)); + + System.out.println(Jsons.toJson(pushContent)); + } + + @Test + public void notificationTest(){ + NotificationPushPayload payload = PayloadFactory.create(MessageType.NOTIFICATION); + payload.setContent("content"); + payload.setTitle("title"); + PushContent pushContent = PushContent.build(2, Jsons.toJson(payload)); + + System.out.println(Jsons.toJson(pushContent)); + } + +} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index d868234c..e90875bf 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -17,7 +17,7 @@ public static void main(String[] args) throws Exception { client.start(); Thread.sleep(1000); for (int i = 0; i < 100; i++) { - PushContent content = PushContent.build(1).setContent("this a first push." + i).setTitle("MPush"); + PushContent content = PushContent.build(1,"this a first push." + i); content.setMsgId("msgId_" + (i % 2)); client.send(Jsons.toJson(content), Arrays.asList("huang1", "huang2", "huang"), new PushSender.Callback() { diff --git a/start.sh b/start.sh index e214f71f..c97a1a5e 100755 --- a/start.sh +++ b/start.sh @@ -5,7 +5,10 @@ ENV=daily base_dir=`pwd` echo "start assembly lib..." -mvn clean install -P $ENV + +rm -rf $base_dir/target + +mvn clean install assembly:assembly -P $ENV echo "start tar mpush..." cd $base_dir/target From 65275d9498cafe237fddb646311c0fb34955b5ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 18 Feb 2016 01:09:29 +0000 Subject: [PATCH 318/890] chang message filed encode length --- .../com/shinemo/mpush/common/message/ByteBufMessage.java | 7 ++++++- .../com/shinemo/mpush/netty/client/NettyHttpClient.java | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java index 5ad18876..e84b0377 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java @@ -56,8 +56,10 @@ public void encodeLong(ByteBuf body, long field) { public void encodeBytes(ByteBuf body, byte[] field) { if (field == null || field.length == 0) { body.writeShort(0); - } else { + } else if (field.length < Short.MAX_VALUE) { body.writeShort(field.length).writeBytes(field); + } else { + body.writeShort(Short.MAX_VALUE).writeInt(field.length - Short.MAX_VALUE).writeBytes(field); } } @@ -70,6 +72,9 @@ public String decodeString(ByteBuf body) { public byte[] decodeBytes(ByteBuf body) { int fieldLength = body.readShort(); if (fieldLength == 0) return null; + if (fieldLength == Short.MAX_VALUE) { + fieldLength += body.readInt(); + } byte[] bytes = new byte[fieldLength]; body.readBytes(bytes); return bytes; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 8f19524d..3377bfa1 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -76,7 +76,7 @@ public void request(final RequestInfo info) throws Exception { timer.newTimeout(info, info.timeout, TimeUnit.MILLISECONDS); Channel channel = tryAcquire(host); if (channel == null) { - LOGGER.debug("create new channel,host=" + host); + LOGGER.debug("create new channel, host={}", host); ChannelFuture f = b.connect(host, port); f.addListener(new ChannelFutureListener() { @Override From a44e05919afb1496d742c4044101cdc884b09ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 18 Feb 2016 16:15:28 +0800 Subject: [PATCH 319/890] =?UTF-8?q?push=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/api/PushContent.java | 25 ++++- .../mpush/api/payload/BasePayload.java | 64 ------------- .../api/payload/NotificationPushPayload.java | 91 ++++++++++++++++++- .../com/shinemo/mpush/test/gson/GsonTest.java | 6 +- .../mpush/test/payload/PayloadTest.java | 5 +- .../com/shinemo/mpush/test/push/Main.java | 3 +- .../shinemo/mpush/tools/crypto/AESUtils.java | 24 ++++- pom.xml | 20 ++-- 8 files changed, 155 insertions(+), 83 deletions(-) delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java index c62d7960..d1e5e142 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java @@ -26,8 +26,8 @@ public PushContent(int msgType) { this.msgType = msgType; } - public static PushContent build(int msgType,String content){ - PushContent pushContent = new PushContent(msgType); + public static PushContent build(PushType msgType,String content){ + PushContent pushContent = new PushContent(msgType.getValue()); pushContent.setContent(content); return pushContent; } @@ -52,4 +52,25 @@ public void setContent(String content) { this.content = content; } + public enum PushType{ + NOTIFICATION("提醒",1), + MESSAGE("消息",2), + NOTIFICATIONANDMESSAGE("提醒+消息",3); + + PushType(String desc, int value) { + this.desc = desc; + this.value = value; + } + + private final String desc; + private final int value; + + public String getDesc() { + return desc; + } + public int getValue() { + return value; + } + } + } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java deleted file mode 100644 index 06e2b8cb..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/BasePayload.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.shinemo.mpush.api.payload; - - -/** - * msgId、msgType 必填 - * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 - * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 - * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * - */ -@SuppressWarnings("unchecked") -public class BasePayload implements Payload{ - - private static final long serialVersionUID = 2367820805720853376L; - /*****以下非必填**/ - private Long nid; //主要用于聚合通知,非必填 - private Byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯 - private String largeIcon; // 大图标 - private String ticker; //和title一样 - private Integer number; // - public Long getNid() { - return nid; - } - public T setNid(Long nid) { - this.nid = nid; - return (T)this; - } - public Byte getFlags() { - return flags; - } - public T setFlags(Byte flags) { - this.flags = flags; - return (T)this; - } - public String getLargeIcon() { - return largeIcon; - } - public T setLargeIcon(String largeIcon) { - this.largeIcon = largeIcon; - return (T)this; - } - public String getTicker() { - return ticker; - } - public T setTicker(String ticker) { - this.ticker = ticker; - return (T)this; - } - public Integer getNumber() { - return number; - } - public T setNumber(Integer number) { - this.number = number; - return (T)this; - } - -} - diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java index 361e1b41..fa69faa3 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java @@ -1,5 +1,10 @@ package com.shinemo.mpush.api.payload; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + + /** * msgId、msgType 必填 @@ -14,11 +19,14 @@ * * */ -public class NotificationPushPayload extends BasePayload implements Payload{ +public class NotificationPushPayload implements Payload{ private static final long serialVersionUID = 4363667286689742483L; private String title; private String content; + + private Map extras; + public String getTitle() { return title; } @@ -34,6 +42,87 @@ public NotificationPushPayload setContent(String content) { return this; } + /*****以下非必填**/ + private Long nid; //主要用于聚合通知,非必填 + private byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯 + private String largeIcon; // 大图标 + private String ticker; //和title一样 + private Integer number; // + public Long getNid() { + return nid; + } + public NotificationPushPayload setNid(Long nid) { + this.nid = nid; + return this; + } + public Byte getFlags() { + return flags; + } + public NotificationPushPayload setFlags(Byte flags) { + this.flags = flags; + return this; + } + public String getLargeIcon() { + return largeIcon; + } + public NotificationPushPayload setLargeIcon(String largeIcon) { + this.largeIcon = largeIcon; + return this; + } + public String getTicker() { + return ticker; + } + public NotificationPushPayload setTicker(String ticker) { + this.ticker = ticker; + return this; + } + public Integer getNumber() { + return number; + } + public NotificationPushPayload setNumber(Integer number) { + this.number = number; + return this; + } + + public NotificationPushPayload setFlag(Flag flag) { + this.flags |= flag.getValue(); + return this; + } + + public boolean hasFlag(Flag flag) { + return (this.flags & flag.getValue()) != 0; + } + + public Map getExtras() { + return extras; + } + + public NotificationPushPayload setExtras(Map extras) { + this.extras = new HashMap<>(extras); + return this; + } + + public enum Flag { + VOICE("声音",0x01), + SHOCK("震动",0x02), + LIGHT("闪灯",0x03); + + Flag(String desc, int value) { + this.desc = desc; + this.value = value; + } + + private final String desc; + private final Integer value; + + public String getDesc() { + return desc; + } + public byte getValue() { + return value.byteValue(); + } + + } } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java index f488153d..21b4d9ac 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java @@ -3,9 +3,11 @@ import java.util.Map; import org.junit.Test; + import com.google.common.collect.Maps; import com.google.gson.JsonObject; import com.shinemo.mpush.api.PushContent; +import com.shinemo.mpush.api.PushContent.PushType; import com.shinemo.mpush.tools.Jsons; public class GsonTest { @@ -16,7 +18,7 @@ public void test(){ map.put("key1", 1121+""); map.put("key2", "value2"); - PushContent content = PushContent.build(1,Jsons.toJson(map)); + PushContent content = PushContent.build(PushType.MESSAGE,Jsons.toJson(map)); System.out.println(Jsons.toJson(content)); @@ -27,7 +29,7 @@ public void test(){ public void test2(){ ValueMap map = new ValueMap("1122", "value2"); - PushContent content = PushContent.build(1,Jsons.toJson(map)); + PushContent content = PushContent.build(PushType.MESSAGE,Jsons.toJson(map)); System.out.println(Jsons.toJson(content)); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java index bb521590..110b8793 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java @@ -6,6 +6,7 @@ import com.google.common.collect.Maps; import com.shinemo.mpush.api.PushContent; +import com.shinemo.mpush.api.PushContent.PushType; import com.shinemo.mpush.api.payload.CustomPushPayload; import com.shinemo.mpush.api.payload.NotificationPushPayload; import com.shinemo.mpush.api.payload.PayloadFactory; @@ -24,7 +25,7 @@ public void customTest(){ CustomPushPayload customPayload = PayloadFactory.create(MessageType.CUSTOM); customPayload.putAll(map); - PushContent pushContent = PushContent.build(1, Jsons.toJson(customPayload)); + PushContent pushContent = PushContent.build(PushType.MESSAGE, Jsons.toJson(customPayload)); System.out.println(Jsons.toJson(pushContent)); } @@ -34,7 +35,7 @@ public void notificationTest(){ NotificationPushPayload payload = PayloadFactory.create(MessageType.NOTIFICATION); payload.setContent("content"); payload.setTitle("title"); - PushContent pushContent = PushContent.build(2, Jsons.toJson(payload)); + PushContent pushContent = PushContent.build(PushType.NOTIFICATION, Jsons.toJson(payload)); System.out.println(Jsons.toJson(pushContent)); } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java index e90875bf..10db6d38 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java @@ -2,6 +2,7 @@ import com.shinemo.mpush.api.PushContent; import com.shinemo.mpush.api.PushSender; +import com.shinemo.mpush.api.PushContent.PushType; import com.shinemo.mpush.push.PushClient; import com.shinemo.mpush.tools.Jsons; @@ -17,7 +18,7 @@ public static void main(String[] args) throws Exception { client.start(); Thread.sleep(1000); for (int i = 0; i < 100; i++) { - PushContent content = PushContent.build(1,"this a first push." + i); + PushContent content = PushContent.build(PushType.MESSAGE,"this a first push." + i); content.setMsgId("msgId_" + (i % 2)); client.send(Jsons.toJson(content), Arrays.asList("huang1", "huang2", "huang"), new PushSender.Callback() { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java index ac6914dd..9a13cc29 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java @@ -1,10 +1,15 @@ package com.shinemo.mpush.tools.crypto; +import java.security.SecureRandom; + import com.shinemo.mpush.tools.Constants; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; @@ -16,7 +21,22 @@ public final class AESUtils { public static final String KEY_ALGORITHM = "AES"; public static final String KEY_ALGORITHM_PADDING = "AES/CBC/PKCS5Padding"; - + /** + *

+ * 生成密钥 + *

+ * + * @param seed 密钥种子 + * @return + * @throws Exception + */ + public static SecretKey getSecretKey(byte[] seed) throws Exception { + SecureRandom secureRandom = new SecureRandom(seed); + KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); + keyGenerator.init(secureRandom); + return keyGenerator.generateKey(); + } + public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); @@ -29,6 +49,8 @@ public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { throw new RuntimeException("AES encrypt ex", e); } } + + public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); diff --git a/pom.xml b/pom.xml index b6a05cb2..f8054633 100644 --- a/pom.xml +++ b/pom.xml @@ -29,16 +29,16 @@ 1.7 - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT - 0.0.2-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT + 0.0.3-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 From 3efd60978ff5c028aad8004b37c47f54a1776fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 18 Feb 2016 16:30:44 +0800 Subject: [PATCH 320/890] add construtor --- .../com/shinemo/mpush/api/payload/CustomPushPayload.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java index 531d36e1..a47c3166 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.api.payload; import java.util.HashMap; +import java.util.Map; /** @@ -20,7 +21,13 @@ public class CustomPushPayload extends HashMap implements Payloa private static final long serialVersionUID = 6076731078625705602L; + public CustomPushPayload(Map extras) { + this.putAll(extras); + } + public static CustomPushPayload build(Map extras){ + return new CustomPushPayload(extras); + } } From 3d6e473c1f03d8e44a3bdb30e3651e23f461472d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 18 Feb 2016 17:03:09 +0800 Subject: [PATCH 321/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/api/payload/NotificationPushPayload.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java index fa69faa3..744d38d9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.api.payload; -import java.io.Serializable; import java.util.HashMap; import java.util.Map; @@ -25,7 +24,7 @@ public class NotificationPushPayload implements Payload{ private String title; private String content; - private Map extras; + private Map extras; public String getTitle() { return title; @@ -93,11 +92,11 @@ public boolean hasFlag(Flag flag) { return (this.flags & flag.getValue()) != 0; } - public Map getExtras() { + public Map getExtras() { return extras; } - public NotificationPushPayload setExtras(Map extras) { + public NotificationPushPayload setExtras(Map extras) { this.extras = new HashMap<>(extras); return this; } From 491afd1f0f5d3b8e5140f86ae7a56732b1a053d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 18 Feb 2016 17:05:15 +0800 Subject: [PATCH 322/890] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/payload/CustomPushPayload.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java index a47c3166..4c240094 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java @@ -25,6 +25,9 @@ public CustomPushPayload(Map extras) { this.putAll(extras); } + public CustomPushPayload() { + } + public static CustomPushPayload build(Map extras){ return new CustomPushPayload(extras); } From 79d330f0974f8f817731b00bfa82d3dec2f89df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 18 Feb 2016 09:05:00 +0000 Subject: [PATCH 323/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/connection/ConnectionManager.java | 2 + .../shinemo/mpush/api/protocol/Packet.java | 2 +- .../mpush/core/handler/HttpProxyHandler.java | 7 +- .../mpush/core/server/ConnectionServer.java | 15 +++- .../mpush/netty/client/HttpClient.java | 2 - .../mpush/netty/client/NettyHttpClient.java | 73 ++++++++-------- .../mpush/netty/client/RequestInfo.java | 86 ++++++++++++++----- .../connection/NettyConnectionManager.java | 55 +++++++----- .../mpush/tools/config/ConfigCenter.java | 4 + 9 files changed, 158 insertions(+), 88 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java index b76c1f20..59f56f2d 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java @@ -18,4 +18,6 @@ public interface ConnectionManager { List getConnections(); void init(); + + void destroy(); } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 269c5e02..c31f01ec 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -64,7 +64,7 @@ public String toString() { ", flags=" + flags + ", sessionId=" + sessionId + ", lrc=" + lrc + - ", body=" + Arrays.toString(body) + + ", body=" + (body == null ? 0 : body.length) + '}'; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 2bda1f23..c8bde0cc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -8,6 +8,7 @@ import com.shinemo.mpush.common.message.HttpRequestMessage; import com.shinemo.mpush.common.message.HttpResponseMessage; import com.shinemo.mpush.netty.client.HttpCallback; +import com.shinemo.mpush.netty.client.HttpClient; import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.client.RequestInfo; import com.shinemo.mpush.tools.MPushUtil; @@ -28,12 +29,12 @@ */ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyHandler.class); - private final NettyHttpClient httpClient; + private final HttpClient httpClient; private final DnsMapping dnsMapping; - public HttpProxyHandler() { - this.httpClient = new NettyHttpClient(); + public HttpProxyHandler(HttpClient httpClient) { this.dnsMapping = new DnsMapping(); + this.httpClient = httpClient; this.httpClient.start(); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 6bda280e..77b31092 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -5,9 +5,10 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; import com.shinemo.mpush.core.handler.*; +import com.shinemo.mpush.netty.client.HttpClient; +import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; - import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -19,26 +20,32 @@ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; private ConnectionManager connectionManager = new NettyConnectionManager(); + private HttpClient httpClient = new NettyHttpClient(); public ConnectionServer(int port) { super(port); -// NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle() / 1000, TimeUnit.SECONDS); } @Override public void init() { super.init(); + connectionManager.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - receiver.register(Command.HTTP_PROXY, new HttpProxyHandler()); - connectionManager.init(); + receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); channelHandler = new ServerChannelHandler(true, connectionManager, receiver); } + @Override + public void stop(Listener listener) { + super.stop(listener); + httpClient.stop(); + connectionManager.destroy(); + } @Override protected void initOptions(ServerBootstrap b) { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java index ae5f46f3..ead3ee91 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java @@ -1,7 +1,5 @@ package com.shinemo.mpush.netty.client; -import io.netty.handler.codec.http.HttpRequest; - /** * Created by ohun on 2016/2/15. */ diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 3377bfa1..e6162d4b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -1,8 +1,10 @@ package com.shinemo.mpush.netty.client; import com.google.common.collect.ArrayListMultimap; +import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.thread.NamedThreadFactory; import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; @@ -20,6 +22,7 @@ import java.net.URLDecoder; import java.util.Iterator; import java.util.List; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** @@ -30,16 +33,23 @@ public class NettyHttpClient implements HttpClient { private Bootstrap b; private EventLoopGroup workerGroup; private Timer timer; - private final AttributeKey key = AttributeKey.newInstance("requestInfo"); + private final AttributeKey requestKey = AttributeKey.newInstance("requestInfo"); + private final AttributeKey hostKey = AttributeKey.newInstance("host"); private final ArrayListMultimap channelPool = ArrayListMultimap.create(); + private final int maxConnPerHost = ConfigCenter.holder.maxHttpConnCountPerHost(); @Override public void start() { // TODO: 2016/2/15 yxx 配置线程池 - workerGroup = new NioEventLoopGroup(); + workerGroup = new NioEventLoopGroup(0, Executors.newCachedThreadPool()); b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); + b.option(ChannelOption.TCP_NODELAY, true); + b.option(ChannelOption.SO_REUSEADDR, true); + b.option(ChannelOption.SO_KEEPALIVE, true); + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.handler(new ChannelInitializer() { @Override public void initChannel(SocketChannel ch) throws Exception { @@ -65,15 +75,12 @@ public void stop() { @Override public void request(final RequestInfo info) throws Exception { - HttpRequest request = info.request; - String url = request.uri(); - URI uri = new URI(url); - String host = uri.getHost(); + URI uri = new URI(info.request.uri()); + String host = info.host = uri.getHost(); int port = uri.getPort() == -1 ? 80 : uri.getPort(); - info.host = host; - request.headers().set(HttpHeaderNames.HOST, host); - request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); - timer.newTimeout(info, info.timeout, TimeUnit.MILLISECONDS); + info.request.headers().set(HttpHeaderNames.HOST, host); + info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); + timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); Channel channel = tryAcquire(host); if (channel == null) { LOGGER.debug("create new channel, host={}", host); @@ -85,8 +92,8 @@ public void operationComplete(ChannelFuture future) throws Exception { writeRequest(future.channel(), info); } else { info.tryDone(); - info.callback.onFailure(504, "Gateway Timeout"); - LOGGER.debug("request failure request=%s", info); + info.onFailure(504, "Gateway Timeout"); + LOGGER.debug("request failure request={}", info); } } }); @@ -103,7 +110,8 @@ private synchronized Channel tryAcquire(String host) { Channel channel = it.next(); it.remove(); if (channel.isActive()) { - LOGGER.debug("tryAcquire channel success ,host=" + host); + LOGGER.debug("tryAcquire channel success ,host={}", host); + channel.attr(hostKey).set(host); return channel; } } @@ -111,43 +119,44 @@ private synchronized Channel tryAcquire(String host) { } private synchronized void tryRelease(Channel channel) { - String host = channel.attr(key).getAndRemove().host; + String host = channel.attr(hostKey).getAndRemove(); List channels = channelPool.get(host); - if (channels == null || channels.size() < 5) { - LOGGER.debug("tryRelease channel success ,host=" + host); + if (channels == null || channels.size() < maxConnPerHost) { + LOGGER.debug("tryRelease channel success, host={}", host); channelPool.put(host, channel); } else { - LOGGER.debug("tryRelease channel failure ,host=" + host); + LOGGER.debug("tryRelease channel over limit={}, host={}, channel closed.", maxConnPerHost, host); channel.close(); } } private void writeRequest(Channel channel, RequestInfo info) { - channel.attr(key).set(info); + channel.attr(requestKey).set(info); + channel.attr(hostKey).set(info.host); channel.writeAndFlush(info.request).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { - RequestInfo requestInfo = future.channel().attr(key).get(); - requestInfo.tryDone(); - requestInfo.callback.onFailure(503, "Service Unavailable"); - LOGGER.debug("request failure request={}", requestInfo); + RequestInfo info = future.channel().attr(requestKey).getAndRemove(); + info.tryDone(); + info.onFailure(503, "Service Unavailable"); + LOGGER.debug("request failure request={}", info); tryRelease(future.channel()); } } }); } - + @ChannelHandler.Sharable private class HttpClientHandler extends ChannelHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - RequestInfo info = ctx.channel().attr(key).get(); + RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); LOGGER.error("http client caught an error, info={}", info, cause); try { if (info.tryDone()) { - info.callback.onException(cause); + info.onException(cause); } } finally { tryRelease(ctx.channel()); @@ -156,25 +165,23 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - RequestInfo info = ctx.channel().attr(key).get(); + RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); if (info == null) return; try { if (info.tryDone()) { - HttpCallback callback = info.callback; - HttpRequest request = info.request; HttpResponse response = (HttpResponse) msg; if (isRedirect(response)) { - if (callback.onRedirect(response)) { - CharSequence location = getRedirectLocation(request, response); + if (info.onRedirect(response)) { + String location = getRedirectLocation(info.request, response); if (location != null && location.length() > 0) { info.cancelled.set(false); - info.request = copy(location.toString(), request); + info.request = info.request.copy().setUri(location); request(info); return; } } } - callback.onResponse(response); + info.onResponse(response); LOGGER.debug("request done request={}", info); } } finally { @@ -227,7 +234,6 @@ private String getRedirectLocation(HttpRequest request, HttpResponse response) t return null; } - private HttpRequest copy(String uri, HttpRequest request) { HttpRequest nue = request; if (request instanceof DefaultFullHttpRequest) { @@ -246,6 +252,5 @@ private HttpRequest copy(String uri, HttpRequest request) { } return nue; } - } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index 47d04cfe..e6024e7d 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -1,51 +1,56 @@ package com.shinemo.mpush.netty.client; +import com.google.common.primitives.Ints; +import com.google.common.primitives.Primitives; +import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Timeout; import io.netty.util.TimerTask; import java.util.concurrent.atomic.AtomicBoolean; -public class RequestInfo implements TimerTask { +public class RequestInfo implements TimerTask, HttpCallback { + private static final String HEAD_READ_TIMEOUT = "readTimeout"; final AtomicBoolean cancelled = new AtomicBoolean(false); - HttpCallback callback; - HttpRequest request; + private HttpCallback callback; + FullHttpRequest request; + String uri; String host; - int timeout = 10000;//5s + int readTimeout = 10000; long startTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis(); - public RequestInfo(HttpRequest request, HttpCallback callback) { + public RequestInfo(FullHttpRequest request, HttpCallback callback) { this.callback = callback; this.request = request; + this.uri = request.uri(); + String timeout = request.headers().getAndRemoveAndConvert(HEAD_READ_TIMEOUT); + if (timeout != null) { + Integer integer = Ints.tryParse(timeout); + if (integer != null && integer > 0) readTimeout = integer; + } } - public HttpCallback getCallback() { - return callback; - } - - public HttpRequest getRequest() { - return request; - } - - public int getTimeout() { - return timeout; + public int getReadTimeout() { + return readTimeout; } - public RequestInfo setTimeout(int timeout) { - this.timeout = timeout; + public RequestInfo setReadTimeout(int timeout) { + this.readTimeout = timeout; return this; } @Override public void run(Timeout timeout) throws Exception { if (tryDone()) { - callback.onTimeout(); + if (callback != null) { + callback.onTimeout(); + } } } public boolean tryDone() { - endTime = System.currentTimeMillis(); return cancelled.compareAndSet(false, true); } @@ -53,10 +58,49 @@ public boolean tryDone() { public String toString() { return "RequestInfo{" + "cancelled=" + cancelled + - ", uri='" + request.uri() + '\'' + + ", uri='" + uri + '\'' + ", host='" + host + '\'' + - ", timeout=" + timeout + + ", readTimeout=" + readTimeout + ", costTime=" + (endTime - startTime) + '}'; } + + @Override + public void onResponse(HttpResponse response) { + callback.onResponse(response); + endTime = System.currentTimeMillis(); + destroy(); + } + + @Override + public void onFailure(int statusCode, String reasonPhrase) { + callback.onFailure(statusCode, reasonPhrase); + endTime = System.currentTimeMillis(); + destroy(); + } + + @Override + public void onException(Throwable throwable) { + callback.onException(throwable); + endTime = System.currentTimeMillis(); + destroy(); + } + + @Override + public void onTimeout() { + callback.onTimeout(); + endTime = System.currentTimeMillis(); + destroy(); + } + + @Override + public boolean onRedirect(HttpResponse response) { + endTime = System.currentTimeMillis(); + return callback.onRedirect(response); + } + + private void destroy() { + request = null; + callback = null; + } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index f9956f19..637b49ef 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -26,21 +26,30 @@ /** * Created by ohun on 2015/12/22. */ -public final class NettyConnectionManager implements ConnectionManager { +public final class NettyConnectionManager implements ConnectionManager { //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); - private Timer wheelTimer; - + private Timer timer; + @Override public void init() { - //每秒钟走一步,一个心跳周期内走一圈 + //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); - this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); + this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); EventBus.INSTANCE.register(this); } + @Override + public void destroy() { + if (timer != null) timer.stop(); + for (Connection connection : connections.values()) { + connection.close(); + } + connections.clear(); + } + @Override public Connection get(final Channel channel) { return connections.get(channel.id().asLongText()); @@ -58,12 +67,12 @@ public void remove(Channel channel) { connection.close(); } } - + @Override public List getConnections() { - return Lists.newArrayList(connections.values()); + return Lists.newArrayList(connections.values()); } - + @Subscribe void onHandshakeOk(HandshakeEvent event) { HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); @@ -71,7 +80,7 @@ void onHandshakeOk(HandshakeEvent event) { } private class HeartbeatCheckTask implements TimerTask { - + private int expiredTimes = 0; private final int heartbeat; private final Connection connection; @@ -82,32 +91,32 @@ public HeartbeatCheckTask(int heartbeat, Connection connection) { } public void startTimeout() { - wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + timer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); } @Override public void run(Timeout timeout) throws Exception { - try{ - if (!connection.isConnected()){ - LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); - return; - } + try { + if (!connection.isConnected()) { + LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:%s,%s", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + return; + } if (connection.heartbeatTimeout()) { if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { connection.close(); - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(), connection.getSessionContext().deviceId); return; } else { - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } } else { - expiredTimes = 0; - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId); + expiredTimes = 0; + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } - }catch(Throwable e){ - LoggerManage.execption(LogType.DEFAULT, e, "HeartbeatCheckTask error"); - } - startTimeout(); + } catch (Throwable e) { + LoggerManage.execption(LogType.DEFAULT, e, "HeartbeatCheckTask error"); + } + startTimeout(); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 6da2782d..5b209541 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -104,5 +104,9 @@ public interface ConfigCenter extends Config { @Key("dns_mapping") String dnsMapping(); + @Key("max_http_client_conn_count_per_host") + @DefaultValue("5") + int maxHttpConnCountPerHost(); + } From 401e651dbd82e159c7c1b7bbdb29f78ce6bcb6e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 18 Feb 2016 09:11:49 +0000 Subject: [PATCH 324/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/api/Constants.java | 2 ++ .../shinemo/mpush/netty/client/RequestInfo.java | 14 ++++++++------ .../shinemo/mpush/tools/config/ConfigCenter.java | 6 +++++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index e3898b81..70cf7b54 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -11,4 +11,6 @@ public interface Constants { byte CRYPTO_FLAG = 0x01; byte COMPRESS_FLAG = 0x02; + String HTTP_HEAD_READ_TIMEOUT = "readTimeout"; + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index e6024e7d..63072bd5 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -2,6 +2,8 @@ import com.google.common.primitives.Ints; import com.google.common.primitives.Primitives; +import com.shinemo.mpush.api.Constants; +import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; @@ -11,21 +13,21 @@ import java.util.concurrent.atomic.AtomicBoolean; public class RequestInfo implements TimerTask, HttpCallback { - private static final String HEAD_READ_TIMEOUT = "readTimeout"; + private static final int TIMEOUT = ConfigCenter.holder.httpDefaultReadTimeout(); final AtomicBoolean cancelled = new AtomicBoolean(false); + final long startTime = System.currentTimeMillis(); + long endTime = startTime; + int readTimeout = TIMEOUT; + private String uri; private HttpCallback callback; FullHttpRequest request; - String uri; String host; - int readTimeout = 10000; - long startTime = System.currentTimeMillis(); - long endTime = System.currentTimeMillis(); public RequestInfo(FullHttpRequest request, HttpCallback callback) { this.callback = callback; this.request = request; this.uri = request.uri(); - String timeout = request.headers().getAndRemoveAndConvert(HEAD_READ_TIMEOUT); + String timeout = request.headers().getAndRemoveAndConvert(Constants.HTTP_HEAD_READ_TIMEOUT); if (timeout != null) { Integer integer = Ints.tryParse(timeout); if (integer != null && integer > 0) readTimeout = integer; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 5b209541..d5ddcabc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -32,9 +32,9 @@ public interface ConfigCenter extends Config { @DefaultValue("10000") int minHeartbeat(); + //180秒 @Key("max_heartbeat") @DefaultValue("180000") - //180秒 int maxHeartbeat(); @Key("max_hb_timeout_times") @@ -108,5 +108,9 @@ public interface ConfigCenter extends Config { @DefaultValue("5") int maxHttpConnCountPerHost(); + //10s + @Key("http_default_read_timeout") + @DefaultValue("10000") + int httpDefaultReadTimeout(); } From 5c4fac1720b401178fb2fbebfedf16a5853af743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 18 Feb 2016 17:58:20 +0800 Subject: [PATCH 325/890] =?UTF-8?q?=E5=8D=87=E7=BA=A7pom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index f8054633..42595607 100644 --- a/pom.xml +++ b/pom.xml @@ -29,16 +29,16 @@ 1.7 - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT - 0.0.3-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT + 0.0.4-SNAPSHOT 5.0.0.Alpha2 linux-x86_64 From 8749831aa1bc7e7bcd993ecfa6fdac7fcfb00949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 18 Feb 2016 10:07:23 +0000 Subject: [PATCH 326/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/handler/HttpProxyHandler.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index c8bde0cc..f1a3b5c2 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -66,8 +66,8 @@ public void handle(HttpRequestMessage message) { } catch (Exception e) { HttpResponseMessage .from(message) - .setStatusCode(500) - .setReasonPhrase("Internal Server Error") + .setStatusCode(502) + .setReasonPhrase("Bad Gateway") .sendRaw(); LOGGER.error("send request ex, message=" + message, e); } @@ -119,10 +119,10 @@ public void onFailure(int statusCode, String reasonPhrase) { public void onException(Throwable throwable) { HttpResponseMessage .from(request) - .setStatusCode(500) - .setReasonPhrase("Internal Server Error") + .setStatusCode(502) + .setReasonPhrase("Bad Gateway") .sendRaw(); - LOGGER.error("callback exception request={}, response={}", request, 500, throwable); + LOGGER.error("callback exception request={}, response={}", request, 502, throwable); } @Override From ac3167e03732ce1553c7a611121ecfa2d3baa653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 18 Feb 2016 14:17:11 +0000 Subject: [PATCH 327/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/push/PushRequest.java | 49 ++++++++----------- .../shinemo/mpush/push/PushRequestBus.java | 4 +- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index a5a6d31b..55b995b3 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -16,22 +16,24 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.concurrent.atomic.AtomicInteger; + /** * Created by ohun on 2015/12/30. */ public class PushRequest implements PushSender.Callback, Runnable { - - private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); - private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); + + private final static GatewayServerManage gatewayClientManage = (GatewayServerManage) ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); + private PushSender.Callback callback; private String userId; private String content; private long timeout; - private int status = 0; private long timeout_; private int sessionId; private long sendTime; + private AtomicInteger status = new AtomicInteger(0); public PushRequest() { } @@ -60,14 +62,6 @@ public PushRequest setTimeout(long timeout) { return this; } - public void setSessionId(int sessionId) { - this.sessionId = sessionId; - } - - public int getSessionId() { - return sessionId; - } - @Override public void onSuccess(String userId) { submit(1); @@ -89,20 +83,18 @@ public void onTimeout(String userId) { } private void submit(int status) { - if (this.status != 0) {//防止重复调用 - return; - } - this.status = status; - if (callback != null) { - PushRequestBus.INSTANCE.getExecutor().execute(this); - } else { - LOGGER.warn("callback is null"); + if (this.status.compareAndSet(0, status)) {//防止重复调用 + if (callback != null) { + PushRequestBus.INSTANCE.getExecutor().execute(this); + } else { + LOGGER.warn("callback is null"); + } } } @Override public void run() { - switch (status) { + switch (status.get()) { case 1: callback.onSuccess(userId); break; @@ -145,11 +137,11 @@ public void send() { } public void redirect() { - this.status = 0; - this.timeout_ = timeout + System.currentTimeMillis(); ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - sendToConnServer(); LOGGER.warn("user route has changed, userId={}, content={}", userId, content); + if (status.get() == 0) { + send(); + } } private void sendToConnServer() { @@ -185,9 +177,8 @@ public void operationComplete(ChannelFuture future) throws Exception { PushRequestBus.INSTANCE.put(sessionId, this); } - public long getSendTime() { - return sendTime; - } - - + public long getSendTime() { + return sendTime; + } + } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java index ad423927..b47905e4 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java @@ -1,5 +1,7 @@ package com.shinemo.mpush.push; +import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + import java.util.Iterator; import java.util.Map; import java.util.concurrent.*; @@ -9,7 +11,7 @@ */ public class PushRequestBus implements Runnable { public static final PushRequestBus INSTANCE = new PushRequestBus(); - private Map requests = new ConcurrentHashMap<>(); + private Map requests = new ConcurrentHashMapV8<>(1024); private Executor executor = Executors.newFixedThreadPool(5);//test private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test From 50f1636a5c143648fabc88f35678613d18fc4cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 19 Feb 2016 09:06:50 +0800 Subject: [PATCH 328/890] =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= =?UTF-8?q?=E6=8E=92=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debug.sh | 2 +- start.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/debug.sh b/debug.sh index aa595e1f..2349b531 100755 --- a/debug.sh +++ b/debug.sh @@ -14,6 +14,6 @@ echo "start start mpush..." cd mpush/lib -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -jar $base_dir/target/mpush/mpush-cs.jar & +java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -Dio.netty.leakDetectionLevel=advanced -jar $base_dir/target/mpush/mpush-cs.jar & echo "end start mpush..." diff --git a/start.sh b/start.sh index c97a1a5e..47407d39 100755 --- a/start.sh +++ b/start.sh @@ -15,7 +15,7 @@ cd $base_dir/target tar -xzvf ./mpush-jar-with-dependency.tar.gz echo "start start mpush..." -java -jar $base_dir/target/mpush/mpush-cs.jar & +java -Dio.netty.leakDetectionLevel=advanced -jar $base_dir/target/mpush/mpush-cs.jar & echo "end start mpush..." From c00727ca9fb7deef61d4bd833365be7f5e00029b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 19 Feb 2016 10:11:33 +0800 Subject: [PATCH 329/890] add log --- .../src/main/java/com/shinemo/mpush/push/PushRequest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index a5a6d31b..bb897d36 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -169,12 +169,13 @@ private void sendToConnServer() { return; } - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); + final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); pushMessage.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { sendTime = System.currentTimeMillis(); + LOGGER.warn("pushMessage packet:"+pushMessage.getPacket()); } else { PushRequest.this.onFailure(userId); } From 7d8b7e84328770e179eb35a7767c86fff77c8484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 19 Feb 2016 02:13:44 +0000 Subject: [PATCH 330/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/message/gateway/GatewayPushMessage.java | 8 ++++++++ .../shinemo/mpush/core/server/ServerChannelHandler.java | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java index 88dce714..dc49ba5e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java @@ -35,4 +35,12 @@ public void encode(ByteBuf body) { encodeString(body, userId); encodeString(body, content); } + + @Override + public String toString() { + return "GatewayPushMessage{" + + "userId='" + userId + '\'' + + ", content='" + content + '\'' + + '}'; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 15679806..c0fa999f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -40,7 +40,7 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("update currentTime:" + ctx.channel() + "," + msg); + LOGGER.debug("channelRead channel={}, packet={}", ctx.channel(), msg); connection.updateLastReadTime(); receiver.onReceive((Packet) msg, connection); } @@ -48,13 +48,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LoggerManage.log(security, LogLevel.INFO,"client exceptionCaught channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO, "client exceptionCaught channel=%s", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security,LogLevel.INFO,"client connect channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO, "client connect channel=%s", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -62,7 +62,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security,LogLevel.INFO,"client disconnect channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO, "client disconnect channel=%s", ctx.channel()); connectionManager.remove(ctx.channel()); } } \ No newline at end of file From 4e3e2ea9b280c588ae466ed75df03a6594dc963d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 19 Feb 2016 02:41:27 +0000 Subject: [PATCH 331/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/common/message/ByteBufMessage.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java index e84b0377..b7576ef8 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java @@ -34,11 +34,7 @@ public byte[] encode() { public abstract void encode(ByteBuf body); public void encodeString(ByteBuf body, String field) { - if (field == null) { - body.writeShort(0); - } else { - body.writeShort(field.length()).writeBytes(field.getBytes(Constants.UTF_8)); - } + encodeBytes(body, field == null ? null : field.getBytes(Constants.UTF_8)); } public void encodeByte(ByteBuf body, byte field) { From 75acf2cd811f62e8767bb5ab95c03c18772ff528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 19 Feb 2016 03:11:59 +0000 Subject: [PATCH 332/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/push/PushRequest.java | 4 ++-- .../common/message/gateway/GatewayPushMessage.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index f153a10d..1ad092e4 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -162,12 +162,12 @@ private void sendToConnServer() { } final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); - pushMessage.send(new ChannelFutureListener() { + pushMessage.sendRaw(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { sendTime = System.currentTimeMillis(); - LOGGER.warn("pushMessage packet:"+pushMessage.getPacket()); + LOGGER.warn("pushMessage packet:" + pushMessage.getPacket()); } else { PushRequest.this.onFailure(userId); } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java index dc49ba5e..477d4435 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.common.message.ByteBufMessage; import com.shinemo.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFutureListener; import static com.shinemo.mpush.api.protocol.Command.GATEWAY_PUSH; @@ -36,6 +37,16 @@ public void encode(ByteBuf body) { encodeString(body, content); } + @Override + public void send() { + super.sendRaw(); + } + + @Override + public void send(ChannelFutureListener listener) { + super.sendRaw(listener); + } + @Override public String toString() { return "GatewayPushMessage{" + From bbbec0bab7ba13b48bf48ba010eda5d462fbfa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 19 Feb 2016 14:16:00 +0800 Subject: [PATCH 333/890] add log --- .../api/payload/NotificationPushPayload.java | 8 ++- .../mpush/api/payload/PushContent.java | 67 ------------------- .../push/manage/impl/GatewayServerManage.java | 2 +- .../mpush/test/connection/client/Main.java | 2 +- 4 files changed, 8 insertions(+), 71 deletions(-) delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java index 744d38d9..52a30cae 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java @@ -43,7 +43,7 @@ public NotificationPushPayload setContent(String content) { /*****以下非必填**/ private Long nid; //主要用于聚合通知,非必填 - private byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯 + private Byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯 private String largeIcon; // 大图标 private String ticker; //和title一样 private Integer number; // @@ -84,7 +84,11 @@ public NotificationPushPayload setNumber(Integer number) { } public NotificationPushPayload setFlag(Flag flag) { - this.flags |= flag.getValue(); + if(this.flags == null){ + this.flags = flag.getValue(); + }else{ + this.flags = (byte) (this.flags.byteValue()|flag.getValue()); + } return this; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java deleted file mode 100644 index bd07837e..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PushContent.java +++ /dev/null @@ -1,67 +0,0 @@ -//package com.shinemo.mpush.api; -// -//import java.io.Serializable; -// -///** -// * msgId、msgType 必填 -// * msgType=1 :nofication,提醒。 -// * 必填:title,content。没有title,则为应用名称。 -// * 非必填。nid 通知id,主要用于聚合通知。 -// * content 为push message。附加的一些业务属性,都在里边。json格式 -// * msgType=2 :非通知消息。不在通知栏展示。 -// * 必填:content。 -// * msgType=3 :消息+提醒 -// * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 -// * -// * -// */ -//public final class PushContent implements Serializable{ -// private static final long serialVersionUID = -1805329333995385960L; -// private String msgId; //返回使用 -// private String title; //没有title,则为应用名称。 -// private String content; //content -// private int msgType; //type -// -// -// -// public PushContent(int msgType) { -// this.msgType = msgType; -// } -// -// public static PushContent build(int msgType){ -// PushContent pushContent = new PushContent(msgType); -// return pushContent; -// } -// -// public String getMsgId() { -// return msgId; -// } -// -// public String getTitle() { -// return title; -// } -// -// public String getContent() { -// return content; -// } -// -// public int getMsgType() { -// return msgType; -// } -// -// public PushContent setTitle(String title) { -// this.title = title; -// return this; -// } -// -// public PushContent setContent(String content) { -// this.content = content; -// return this; -// } -// -// public void setMsgId(String msgId) { -// this.msgId = msgId; -// } -// -// -//} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java index d11b53d6..cb4c2a21 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java @@ -38,7 +38,7 @@ public void addOrUpdate(String fullPath,GatewayServerApplication application){ application2Client.put(application, client); ip2Client.put(application.getIp(), client); }catch(Exception e){ - + log.error("addOrUpdate:{},{}",fullPath,application,e); } printList(); } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java index 44b226e1..6c0121f0 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - for(int i = 0;i<3;i++){ + for(int i = 0;i<1;i++){ String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; From 4812fb1271d6a8da209113ea68dc28998a708abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 19 Feb 2016 10:03:27 +0000 Subject: [PATCH 334/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/handler/HttpProxyHandler.java | 1 - .../com/shinemo/mpush/core/server/GatewayServer.java | 10 +++++++++- .../shinemo/mpush/netty/client/NettyHttpClient.java | 6 ++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index f1a3b5c2..4d55716d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -9,7 +9,6 @@ import com.shinemo.mpush.common.message.HttpResponseMessage; import com.shinemo.mpush.netty.client.HttpCallback; import com.shinemo.mpush.netty.client.HttpClient; -import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.client.RequestInfo; import com.shinemo.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java index 2d2c7df8..912d406f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.core.server; +import com.shinemo.mpush.api.Server; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; import com.shinemo.mpush.core.handler.*; @@ -13,6 +14,7 @@ public final class GatewayServer extends NettyServer { private ServerChannelHandler channelHandler; + private NettyConnectionManager connectionManager; public GatewayServer(int port) { super(port); @@ -23,10 +25,16 @@ public void init() { super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); - NettyConnectionManager connectionManager = new NettyConnectionManager(); + connectionManager = new NettyConnectionManager(); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); } + @Override + public void stop(Listener listener) { + super.stop(listener); + connectionManager.destroy(); + } + @Override public ChannelHandler getChannelHandler() { return channelHandler; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index e6162d4b..5906fdbb 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -10,10 +10,7 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; -import io.netty.util.AttributeKey; -import io.netty.util.HashedWheelTimer; -import io.netty.util.IllegalReferenceCountException; -import io.netty.util.Timer; +import io.netty.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -186,6 +183,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } finally { tryRelease(ctx.channel()); + ReferenceCountUtil.release(msg); } } From 7f39c615569309d28b41f46682f56b3295de273c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 09:34:21 +0800 Subject: [PATCH 335/890] netty client bug fix --- .../mpush/netty/client/NettyClient.java | 7 ++- .../mpush/test/payload/PayloadTest.java | 43 ------------------- 2 files changed, 5 insertions(+), 45 deletions(-) delete mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java index 2852b7c3..cd9441af 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java @@ -52,7 +52,10 @@ public void close(String cause) { if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { log.error("close channel:"+cause); } - this.channel.close(); + if(this.channel!=null){ + this.channel.close(); + } + } @Override @@ -98,7 +101,7 @@ public int getPort() { @Override public void stop(){ - + this.close("stop"); } @Override diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java deleted file mode 100644 index 110b8793..00000000 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/payload/PayloadTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.shinemo.mpush.test.payload; - -import java.util.Map; - -import org.junit.Test; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.api.PushContent; -import com.shinemo.mpush.api.PushContent.PushType; -import com.shinemo.mpush.api.payload.CustomPushPayload; -import com.shinemo.mpush.api.payload.NotificationPushPayload; -import com.shinemo.mpush.api.payload.PayloadFactory; -import com.shinemo.mpush.api.payload.PayloadFactory.MessageType; -import com.shinemo.mpush.tools.Jsons; - -public class PayloadTest { - - @Test - public void customTest(){ - - Map map = Maps.newHashMap(); - map.put("key1", "value1"); - map.put("key2", "value2"); - - CustomPushPayload customPayload = PayloadFactory.create(MessageType.CUSTOM); - customPayload.putAll(map); - - PushContent pushContent = PushContent.build(PushType.MESSAGE, Jsons.toJson(customPayload)); - - System.out.println(Jsons.toJson(pushContent)); - } - - @Test - public void notificationTest(){ - NotificationPushPayload payload = PayloadFactory.create(MessageType.NOTIFICATION); - payload.setContent("content"); - payload.setTitle("title"); - PushContent pushContent = PushContent.build(PushType.NOTIFICATION, Jsons.toJson(payload)); - - System.out.println(Jsons.toJson(pushContent)); - } - -} From b74572b0dea7d241d2d612b25b3aabe5a3dc726e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 09:36:22 +0800 Subject: [PATCH 336/890] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=AD=A3=E5=BC=8F?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 42595607..76442ff1 100644 --- a/pom.xml +++ b/pom.xml @@ -29,16 +29,16 @@ 1.7 - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT - 0.0.4-SNAPSHOT + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 5.0.0.Alpha2 linux-x86_64 From 6a01cfc42c5389125893f4f5b959eeeb01b65fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 10:38:45 +0800 Subject: [PATCH 337/890] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/conn}/client/ClientChannelHandler.java | 2 +- .../shinemo/mpush/conn/client}/GatewayServerApplication.java | 2 +- .../shinemo/mpush/push/manage/impl/GatewayServerManage.java | 2 +- .../push/zk/listener/impl/GatewayServerPathListener.java | 2 +- mpush-core/pom.xml | 4 ++++ mpush-cs/pom.xml | 3 +++ .../com/shinemo/mpush/cs/ConnectionServerApplication.java | 2 +- .../main/java/com/shinemo/mpush/cs/ConnectionServerMain.java | 2 +- .../java/com/shinemo/mpush/test/connection/client/Main.java | 2 +- .../java/com/shinemo/mpush/test/connection/mpns/Main.java | 2 +- 10 files changed, 15 insertions(+), 8 deletions(-) rename {mpush-core/src/main/java/com/shinemo/mpush/core => mpush-client/src/main/java/com/shinemo/mpush/conn}/client/ClientChannelHandler.java (99%) rename {mpush-common/src/main/java/com/shinemo/mpush/common/app/impl => mpush-client/src/main/java/com/shinemo/mpush/conn/client}/GatewayServerApplication.java (92%) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ClientChannelHandler.java similarity index 99% rename from mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java rename to mpush-client/src/main/java/com/shinemo/mpush/conn/client/ClientChannelHandler.java index f3eb55e1..7d1548e4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ClientChannelHandler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.client; +package com.shinemo.mpush.conn.client; import java.util.Map; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/app/impl/GatewayServerApplication.java b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java similarity index 92% rename from mpush-common/src/main/java/com/shinemo/mpush/common/app/impl/GatewayServerApplication.java rename to mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java index 2aec1fd5..98cb23df 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/app/impl/GatewayServerApplication.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common.app.impl; +package com.shinemo.mpush.conn.client; import com.shinemo.mpush.common.app.Application; import com.shinemo.mpush.tools.MPushUtil; diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java index cb4c2a21..345bb4b9 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java @@ -12,8 +12,8 @@ import com.google.common.collect.Maps; import com.shinemo.mpush.api.Client; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; +import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.netty.client.NettyClient; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.push.client.ClientChannelHandler; diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java index 20f98477..642bbf18 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java @@ -1,8 +1,8 @@ package com.shinemo.mpush.push.zk.listener.impl; -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; import com.shinemo.mpush.common.manage.ServerManage; import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; +import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 83eb7a35..9d73a0a6 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -17,6 +17,10 @@ com.shinemo.mpush mpush-netty + + com.shinemo.mpush + mpush-client + org.slf4j jcl-over-slf4j diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index fffd8a66..ecaca99b 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -118,6 +118,9 @@ + + + diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index 55d41bf6..a2bdd4b7 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.cs; import com.shinemo.mpush.common.app.Application; -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 3e29e1bd..937604bc 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -4,7 +4,7 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.common.AbstractServer; -import com.shinemo.mpush.common.app.impl.GatewayServerApplication; +import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.tools.Jsons; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java index 6c0121f0..1ef7112f 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java @@ -4,7 +4,7 @@ import java.util.concurrent.locks.LockSupport; import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.core.client.ClientChannelHandler; +import com.shinemo.mpush.conn.client.ClientChannelHandler; import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java index d7aadec4..9ac642de 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java @@ -4,7 +4,7 @@ import java.util.concurrent.locks.LockSupport; import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.core.client.ClientChannelHandler; +import com.shinemo.mpush.conn.client.ClientChannelHandler; import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; From 15e5ca88c1169b896c959536b87cfc9358ae02e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 10:38:57 +0800 Subject: [PATCH 338/890] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conf-dev.properties b/conf-dev.properties index eec3faae..a15302ea 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -1,13 +1,13 @@ #日志根目录 log.home=/tmp/logs/mpush loglevel=debug -zk_ip = 10.1.80.57:2181 +zk_ip = 127.0.0.1:2181 zk_digest = shinemoIpo zk_namespace = mpush-daily -redis_group = 10.1.80.57:6379:shinemoIpo +redis_group = 127.0.0.1:6379:shinemoIpo private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 \ No newline at end of file +dns_mapping=111.1.57.148=127.0.0.1 From 1836e87510ce5c2e5a789f8d1ed902d59b3bf15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 11:40:04 +0800 Subject: [PATCH 339/890] add sleep --- .../com/shinemo/mpush/push/manage/impl/GatewayServerManage.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java index 345bb4b9..d311b6d3 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java @@ -34,6 +34,7 @@ public void addOrUpdate(String fullPath,GatewayServerApplication application){ try{ Client client = new NettyClient(application.getIp(), application.getPort()); ClientChannelHandler handler = new ClientChannelHandler(client); + Thread.sleep(30); NettyClientFactory.INSTANCE.create(handler); application2Client.put(application, client); ip2Client.put(application.getIp(), client); From 6e8553585dfb98da8359baf773fa207b7e72ae4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 12:44:26 +0800 Subject: [PATCH 340/890] add sleep --- .../com/shinemo/mpush/push/manage/impl/GatewayServerManage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java index d311b6d3..94c820f8 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java @@ -34,7 +34,7 @@ public void addOrUpdate(String fullPath,GatewayServerApplication application){ try{ Client client = new NettyClient(application.getIp(), application.getPort()); ClientChannelHandler handler = new ClientChannelHandler(client); - Thread.sleep(30); + Thread.sleep(250); NettyClientFactory.INSTANCE.create(handler); application2Client.put(application, client); ip2Client.put(application.getIp(), client); From 45dd42afe340654c8ee5d51b95fb74b7625c4610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 13:18:49 +0800 Subject: [PATCH 341/890] bug fix for mpns server start --- .../push/manage/impl/GatewayServerManage.java | 1 - .../com/shinemo/mpush/common/AbstractServer.java | 10 ++++++++++ .../shinemo/mpush/netty/server/NettyServer.java | 14 +++++++++----- mpush-test/src/test/resources/config.properties | 8 ++++---- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java index 94c820f8..345bb4b9 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java @@ -34,7 +34,6 @@ public void addOrUpdate(String fullPath,GatewayServerApplication application){ try{ Client client = new NettyClient(application.getIp(), application.getPort()); ClientChannelHandler handler = new ClientChannelHandler(client); - Thread.sleep(250); NettyClientFactory.INSTANCE.create(handler); application2Client.put(application, client); ip2Client.put(application.getIp(), client); diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 04e3fe52..89e26db0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -114,6 +114,16 @@ public void onFailure(String message) { } }; ThreadPoolManager.newThread(server.getClass().getSimpleName(), runnable).start(); + //TODO sleep for server start + while(!server.isRunning()){ + log.error("server is not started,wait for {} start",server.getClass().getSimpleName()); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + log.error("server started {}",server.getClass().getSimpleName()); + } //step7 注册应用到zk diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 853229bf..c3e55118 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -138,11 +138,15 @@ public void operationComplete(ChannelFuture future) throws Exception { } } }); - serverState.set(State.Started); - /** - * 这里会一直等待,直到socket被关闭 - */ - f.channel().closeFuture().sync(); + f.await(); + if (f.isSuccess()) { + serverState.set(State.Started); + /** + * 这里会一直等待,直到socket被关闭 + */ + f.channel().closeFuture().sync(); + } + } catch (Exception e) { LOGGER.error("server start exception", e); if (listener != null) listener.onFailure("start server ex=" + e.getMessage()); diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index 9cecd8f3..b053a273 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -23,12 +23,12 @@ ras_key_length = 1024 ## session_expired_time = 86400 -zk_ip = 115.29.169.109:5666 -#zk_ip = 127.0.0.1:2181 +#zk_ip = 115.29.169.109:5666 +zk_ip = 127.0.0.1:2181 zk_namespace = mpush-daily zk_digest = shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -redis_group = 111.1.57.148:6379:ShineMoIpo -#redis_group = 127.0.0.1:6379:shinemoIpo +#redis_group = 111.1.57.148:6379:ShineMoIpo +redis_group = 127.0.0.1:6379:shinemoIpo force_write_redis_group_info = false From 71eb5000057e978cfa57e4cb583b0fca0b68ed1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 13:26:51 +0800 Subject: [PATCH 342/890] bug fix --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index 76442ff1..6dbccca9 100644 --- a/pom.xml +++ b/pom.xml @@ -281,6 +281,8 @@ + + From 4bb172ad66b8ec00444700747698c8c15e32e482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 17:44:28 +0800 Subject: [PATCH 343/890] add online offline message --- .../java/com/shinemo/mpush/api/RedisKey.java | 12 ++ .../mpush/common/manage/user/UserManager.java | 48 ++++++ .../common/router/UserChangeListener.java | 54 +++++++ .../mpush/core/handler/BindUserHandler.java | 10 +- .../core/handler/FastConnectHandler.java | 6 +- .../core/handler/GatewayPushHandler.java | 12 +- .../mpush/core/handler/HandshakeHandler.java | 9 +- .../mpush/core/handler/HeartBeatHandler.java | 2 +- .../mpush/core/handler/UnbindUserHandler.java | 8 +- .../mpush/core/router/LocalRouterManager.java | 2 + .../mpush/core/router/RouterCenter.java | 7 + .../mpush/tools/config/ConfigCenter.java | 5 +- .../shinemo/mpush/tools/redis/RedisUtil.java | 139 +++++++++++++++--- .../mpush/tools/redis/manage/RedisManage.java | 24 +++ 14 files changed, 290 insertions(+), 48 deletions(-) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java index d60793af..ed04fb48 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -8,6 +8,10 @@ public class RedisKey { private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp_f_c_d_"; + private static final String USER_ONLINE_KEY = "mp_u_ol"; + + private static final String USER_OFFLINE_KEY = "mp_u_ofl"; + public static final String getUserKey(String userId){ return USER_PREFIX+userId; } @@ -20,5 +24,13 @@ public static final String getSessionKey(String sessionId){ public static final String getDeviceIdKey(String deviceId){ return FAST_CONNECTION_DEVICE_PREFIX+deviceId; } + + public static final String getUserOnlineKey(){ + return USER_ONLINE_KEY; + } + + public static final String getUserOfflineKey(){ + return USER_OFFLINE_KEY; + } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java new file mode 100644 index 00000000..10211af3 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -0,0 +1,48 @@ +package com.shinemo.mpush.common.manage.user; + +import java.util.List; + +import com.shinemo.mpush.api.RedisKey; +import com.shinemo.mpush.tools.redis.manage.RedisManage; + +//查询使用 +public class UserManager { + + public void userOnline(String userId) { + String onlineKey = RedisKey.getUserOnlineKey(); + RedisManage.sAdd(onlineKey, userId); + String offlineKey = RedisKey.getUserOfflineKey(); + RedisManage.sRem(offlineKey, userId); + } + + public void userOffline(String userId) { + String onlineKey = RedisKey.getUserOnlineKey(); + RedisManage.sRem(onlineKey, userId); + String offlineKey = RedisKey.getUserOfflineKey(); + RedisManage.sAdd(offlineKey, userId); + } + + //在线用户 + public long onlineUserNum(){ + String onlineKey = RedisKey.getUserOnlineKey(); + return RedisManage.sCard(onlineKey); + } + //离线用户 + public long offlineUserNum(){ + String offlineKey = RedisKey.getUserOfflineKey(); + return RedisManage.sCard(offlineKey); + } + + //在线用户列表 + public List onlineUserList(int start){ + String onlineKey = RedisKey.getUserOnlineKey(); + return RedisManage.sScan(onlineKey, start, String.class); + } + + //离线用户 + public List offlineUserList(int start){ + String offlineKey = RedisKey.getUserOfflineKey(); + return RedisManage.sScan(offlineKey, start, String.class); + } + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java new file mode 100644 index 00000000..4268bcda --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java @@ -0,0 +1,54 @@ +package com.shinemo.mpush.common.router; + +import com.shinemo.mpush.common.AbstractEventContainer; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; +import com.shinemo.mpush.tools.redis.listener.MessageListener; +import com.shinemo.mpush.tools.redis.manage.RedisManage; + +/** + * Created by ohun on 2016/1/4. + */ +public final class UserChangeListener extends AbstractEventContainer implements MessageListener { + public static final String ONLINE_CHANNEL = "/mpush/online/"; + + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; + + //只需要一台机器注册online、offline 消息通道 + public UserChangeListener() { + if(ConfigCenter.holder.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ + ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); + } + } + + public String getOnlineChannel() { + return ONLINE_CHANNEL; + } + + public String getOfflineChannel() { + return OFFLINE_CHANNEL; + } + + public void userOnline(String userId) { + RedisManage.publish(getOnlineChannel(), userId); + } + + public void userOffline(String userId){ + RedisManage.publish(getOnlineChannel(), userId); + } + + @Override + public void onMessage(String channel, String message) { +// if (getKickChannel().equals(channel)) { +// KickRemoteMsg msg = Jsons.fromJson(message, KickRemoteMsg.class); +// if (msg != null) { +// onReceiveKickRemoteMsg(msg); +// } else { +// LoggerManage.info(LogType.CONNECTION, "receive an error kick message=%s", message); +// } +// } else { +// LoggerManage.info(LogType.CONNECTION, "receive an error redis channel=%s",channel); +// } + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 16bea2c2..22b0f1cd 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -1,10 +1,8 @@ package com.shinemo.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.exception.CryptoException; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.BindUserMessage; @@ -28,7 +26,7 @@ public BindUserMessage decode(Packet packet, Connection connection) { public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - LoggerManage.log(LogType.CONNECTION, "bind user failure for invalid param, session=%s", message.getConnection().getSessionContext()); + LoggerManage.log(LogType.CONNECTION, "bind user failure for invalid param, session={}", message.getConnection().getSessionContext()); return; } //1.绑定用户时先看下是否握手成功 @@ -38,16 +36,16 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { OkMessage.from(message).setData("bind success").send(); - LoggerManage.log(LogType.CONNECTION, "bind user success, userId=%s, session=%s", message.userId, context); + LoggerManage.log(LogType.CONNECTION, "bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.INSTANCE.unRegister(message.userId); ErrorMessage.from(message).setReason("bind failed").close(); - LoggerManage.log(LogType.CONNECTION, "bind user failure, userId=%s, session=%s", message.userId, context); + LoggerManage.log(LogType.CONNECTION, "bind user failure, userId={}, session={}", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - LoggerManage.log(LogType.CONNECTION, "bind user failure for not handshake, userId=%s, session=%s", message.userId, context); + LoggerManage.log(LogType.CONNECTION, "bind user failure for not handshake, userId={}, session={}", message.userId, context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 104a207b..7259f60a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -34,11 +34,11 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - LoggerManage.info(LogType.CONNECTION, "fast connect failure, session is expired, sessionId=%s, deviceId=%s", message.sessionId, message.deviceId); + LoggerManage.info(LogType.CONNECTION, "fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - LoggerManage.info(LogType.CONNECTION, "fast connect failure, not the same device, deviceId=%s, session=%s", message.deviceId, session.context); + LoggerManage.info(LogType.CONNECTION, "fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); } else { //3.校验成功,重新计算心跳,完成快速重连 int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); @@ -52,7 +52,7 @@ public void handle(FastConnectMessage message) { .setServerTime(System.currentTimeMillis()) .setHeartbeat(heartbeat) .send(); - LoggerManage.info(LogType.CONNECTION, "fast connect success, session=%s", session.context); + LoggerManage.info(LogType.CONNECTION, "fast connect success, session={}", session.context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java index 1b91cb52..1d267570 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java @@ -73,7 +73,7 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - LoggerManage.info(LogType.PUSH, "gateway push, router in local but disconnect, userId=%s, connection=%s", message.userId, connection); + LoggerManage.info(LogType.PUSH, "gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); //删除已经失效的本地路由 RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); @@ -91,13 +91,13 @@ public void operationComplete(ChannelFuture future) throws Exception { //推送成功 OkMessage.from(message).setData(message.userId).send(); - LoggerManage.info(LogType.PUSH, "gateway push message to client success userId=%s, content=%s", message.userId, message.content); + LoggerManage.info(LogType.PUSH, "gateway push message to client success userId={}, content={}", message.userId, message.content); } else { //推送失败 ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); - LoggerManage.info(LogType.PUSH, "gateway push message to client failure userId=%s, content=%s", message.userId, message.content); + LoggerManage.info(LogType.PUSH, "gateway push message to client failure userId={}, content={}", message.userId, message.content); } } }); @@ -120,7 +120,7 @@ private void checkRemote(GatewayPushMessage message) { ErrorMessage.from(message).setErrorCode(OFFLINE).send(); - LoggerManage.info(LogType.PUSH, "gateway push, router not exists user offline userId=%s, content=%s", message.userId, message.content); + LoggerManage.info(LogType.PUSH, "gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); return; } @@ -133,7 +133,7 @@ private void checkRemote(GatewayPushMessage message) { //删除失效的远程缓存 RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); - LoggerManage.info(LogType.PUSH, "gateway push error remote is local, userId=%s, router=%s", message.userId, router); + LoggerManage.info(LogType.PUSH, "gateway push error remote is local, userId={}, router={}", message.userId, router); return; } @@ -141,7 +141,7 @@ private void checkRemote(GatewayPushMessage message) { //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); - LoggerManage.info(LogType.PUSH, "gateway push, router in remote userId=%s, router=%s", message.userId, router); + LoggerManage.info(LogType.PUSH, "gateway push, router in remote userId={}, router={}", message.userId, router); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 1ee84498..63113678 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -1,15 +1,12 @@ package com.shinemo.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.event.HandshakeEvent; -import com.shinemo.mpush.api.exception.CryptoException; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.BindUserMessage; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.HandshakeMessage; import com.shinemo.mpush.common.message.HandshakeOkMessage; @@ -48,14 +45,14 @@ public void handle(HandshakeMessage message) { || iv.length != CipherBox.INSTANCE.getAesKeyLength() || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - LoggerManage.log(LogType.CONNECTION, "client handshake false:%s", message.getConnection()); + LoggerManage.log(LogType.CONNECTION, "client handshake false:{}", message.getConnection()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - LoggerManage.log(LogType.CONNECTION, "client handshake false for repeat handshake:%s", message.getConnection().getSessionContext()); + LoggerManage.log(LogType.CONNECTION, "client handshake false for repeat handshake:{}", message.getConnection().getSessionContext()); return; } @@ -94,6 +91,6 @@ public void handle(HandshakeMessage message) { //10.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); - LoggerManage.log(LogType.CONNECTION, "client handshake success:%s", context); + LoggerManage.log(LogType.CONNECTION, "client handshake success:{}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 59ce7ba3..00f28010 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -13,7 +13,7 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong - LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId); + LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:{},{}", connection.getChannel(),connection.getSessionContext().deviceId); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java index b0083a2a..035fa2e1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java @@ -26,7 +26,7 @@ public BindUserMessage decode(Packet packet, Connection connection) { public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - LoggerManage.info(LogType.CONNECTION, "unbind user failure invalid param, session=%s", message.getConnection().getSessionContext()); + LoggerManage.info(LogType.CONNECTION, "unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } //1.绑定用户时先看下是否握手成功 @@ -36,14 +36,14 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.unRegister(message.userId); if (success) { OkMessage.from(message).setData("unbind success").send(); - LoggerManage.info(LogType.CONNECTION, "unbind user success, userId=%s, session=%s", message.userId, context); + LoggerManage.info(LogType.CONNECTION, "unbind user success, userId={}, session={}", message.userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").close(); - LoggerManage.info(LogType.CONNECTION, "unbind user failure, register router failure, userId=%s, session=%s", message.userId, context); + LoggerManage.info(LogType.CONNECTION, "unbind user failure, register router failure, userId={}, session={}", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - LoggerManage.info(LogType.CONNECTION, "unbind user failure not handshake, userId=%s, session=%s", message.userId, context); + LoggerManage.info(LogType.CONNECTION, "unbind user failure not handshake, userId={}, session={}", message.userId, context); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index c53c674a..d47b1164 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -32,6 +32,8 @@ public final class LocalRouterManager extends AbstractEventContainer implements public LocalRouter register(String userId, LocalRouter router) { LOGGER.info("register local router success userId={}, router={}", userId, router); connIdUserIds.put(router.getRouteValue().getId(), userId); + + //add online userId return routers.put(userId, router); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 1dc14348..38fea170 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -5,9 +5,11 @@ import com.shinemo.mpush.api.router.Router; import com.shinemo.mpush.api.router.ClientLocation; import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.manage.user.UserManager; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; import com.shinemo.mpush.tools.MPushUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,6 +23,8 @@ public final class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); private final RouterChangeListener routerChangeListener = new RouterChangeListener(); + private final UserManager userManager = new UserManager(); + /** * 注册用户和链接 @@ -42,6 +46,8 @@ public boolean register(String userId, Connection connection) { try { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); + userManager.userOnline(userId); + } catch (Exception e) { LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); } @@ -61,6 +67,7 @@ public boolean register(String userId, Connection connection) { public boolean unRegister(String userId) { localRouterManager.unRegister(userId); remoteRouterManager.unRegister(userId); + userManager.userOffline(userId); return true; } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index d5ddcabc..cc00cfda 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -7,7 +7,6 @@ import org.aeonbits.owner.ConfigFactory; import java.util.List; -import java.util.Map; /** * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 @@ -112,5 +111,9 @@ public interface ConfigCenter extends Config { @Key("http_default_read_timeout") @DefaultValue("10000") int httpDefaultReadTimeout(); + + @Key("online_and_offline_listener_ip") + @DefaultValue("127.0.0.1") + String onlineAndOfflineListenerIp(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index c98b9572..4e720e85 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -16,6 +16,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.ScanResult; public class RedisUtil { @@ -46,7 +47,7 @@ public static long incr(List nodeList,String key,Integer time){ } incrRet = ret; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis incr exception:%s,%s,%s,%s",key,time,node); + LoggerManage.execption(LogType.REDIS, e, "redis incr exception:{},{},{},{}",key,time,node); } finally { // 返还到连接池 close(jedis); @@ -72,7 +73,7 @@ public static T get(RedisNode node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis get exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis get exception:{},{}",key,node); } finally { // 返还到连接池 close(jedis); @@ -115,7 +116,7 @@ public static void set(List nodeList, String key, String value, Integ jedis.expire(key, time); } } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis set exception:%s,%s,%s,%s",key,value,time,node); + LoggerManage.execption(LogType.REDIS, e, "redis set exception:{},{},{},{}",key,value,time,node); } finally { // 返还到连接池 close(jedis); @@ -131,7 +132,7 @@ public static void del(List nodeList, String key) { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis del exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis del exception:{},{}",key,node); } finally { // 返还到连接池 close(jedis); @@ -152,7 +153,7 @@ public static void hset(List nodeList, String namespace, String key, jedis = getClient(node); jedis.hset(namespace, key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hset exception:%s,%s,%s,%s",namespace,key,value,node); + LoggerManage.execption(LogType.REDIS, e, "redis hset exception:{},{},{},{}",namespace,key,value,node); } finally { // 返还到连接池 close(jedis); @@ -172,7 +173,7 @@ public static T hget(RedisNode node, String namespace, String key, Class jedis = getClient(node); value = jedis.hget(namespace, key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hget exception:%s,%s",namespace,key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hget exception:{},{}",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -189,7 +190,7 @@ public static void hdel(List nodeList, String namespace, String key) jedis = getClient(node); jedis.hdel(namespace, key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hdel exception:%s,%s,%s",namespace,key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hdel exception:{},{},{}",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -204,7 +205,7 @@ public static Map hgetAll(RedisNode node, String namespace) { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); + LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:{},{}",namespace,node); } finally { // 返还到连接池 close(jedis); @@ -219,7 +220,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:%s,%s",namespace,node); + LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:{},{}",namespace,node); } finally { // 返还到连接池 close(jedis); @@ -254,7 +255,7 @@ public static Set hkeys(RedisNode node, String key) { jedis = getClient(node); result = jedis.hkeys(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hkeys exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hkeys exception:{},{}",key,node); } finally { // 返还到连接池 close(jedis); @@ -280,7 +281,7 @@ public static List hmget(RedisNode node, String namespace, Class clazz jedis = getClient(node); value = jedis.hmget(namespace, key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hmget exception:%s,%s,%s",namespace,key,node); + LoggerManage.execption(LogType.REDIS, e, "redis hmget exception:{},{},{}",namespace,key,node); } finally { // 返还到连接池 close(jedis); @@ -320,7 +321,7 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String key, String value) { jedis = getClient(node); jedis.lpush(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lpush exception:%s,%s,%s",key,value,node); + LoggerManage.execption(LogType.REDIS, e, "redis lpush exception:{},{},{}",key,value,node); } finally { // 返还到连接池 close(jedis); @@ -373,7 +374,7 @@ public static void rpush(List nodeList, String key, String value) { jedis = getClient(node); jedis.rpush(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis rpush exception:%s,%s,%s",key,value,node); + LoggerManage.execption(LogType.REDIS, e, "redis rpush exception:{},{},{}",key,value,node); } finally { // 返还到连接池 close(jedis); @@ -398,7 +399,7 @@ public static T lpop(List nodeList, String key, Class clazz) { vaule = jedis.lpop(key); retValue = vaule; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lpop exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis lpop exception:{},{}",key,node); } finally { // 返还到连接池 close(jedis); @@ -421,7 +422,7 @@ public static T rpop(List nodeList, String key, Class clazz) { vaule = jedis.rpop(key); retValue = vaule; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis rpop exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis rpop exception:{},{}",key,node); } finally { // 返还到连接池 close(jedis); @@ -443,7 +444,7 @@ public static List lrange(RedisNode node, String key, int start, int end, jedis = getClient(node); value = jedis.lrange(key, start, end); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lrange exception:%s,%s,%s,%s",key,start,end,node); + LoggerManage.execption(LogType.REDIS, e, "redis lrange exception:{},{},{},{}",key,start,end,node); } finally { // 返还到连接池 close(jedis); @@ -470,7 +471,7 @@ public static long llen(RedisNode node, String key) { jedis = getClient(node); jedis.llen(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis llen exception:%s,%s",key,node); + LoggerManage.execption(LogType.REDIS, e, "redis llen exception:{},{}",key,node); } finally { // 返还到连接池 close(jedis); @@ -492,7 +493,7 @@ public static void publish(RedisNode node, String channel, T message) { jedis = getClient(node); jedis.publish(channel, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,Jsons.toJson(node),Jsons.toJson(channel)); + LoggerManage.execption(LogType.REDIS, e, "redis publish exception:{},{},{}",value,Jsons.toJson(node),Jsons.toJson(channel)); } finally { // 返还到连接池 close(jedis); @@ -518,7 +519,7 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann jedis = getClient(node); jedis.subscribe(pubsub, channel); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:%s,%s",Jsons.toJson(node),Jsons.toJson(channel)); + LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:{},{}",Jsons.toJson(node),Jsons.toJson(channel)); } finally { // 返还到连接池 close(jedis); @@ -527,6 +528,102 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann } /********************* - * pubsub redis end + * set redis start ********************************/ + /** + * @param nodeList + * @param key + * @param value + * @param time seconds + */ + public static void sAdd(List nodeList, String key, String value) { + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.sadd(key, value); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis sadd exception:{},{},{}",key,value,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + } + + /** + * @param node 返回个数 + * @param key + * @param clazz + * @return + */ + public static Long sCard(RedisNode node, String key) { + + Long value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.scard(key); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis scard exception:{},{}",key,node); + } finally { + // 返还到连接池 + close(jedis); + } + return value; + } + + public static void sRem(List nodeList, String key,String value) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.srem(key, value); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis srem exception:{},{},{}",key,value,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + + } + + /** + * 默认使用每页10个 + * + * @param node + * @param key + * @param clazz + * @param fields + * @return + */ + public static List sScan(RedisNode node, String key, Class clazz, int start) { + + List value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + ScanResult sscanResult = jedis.sscan(key, start+""); + if(sscanResult!=null&&sscanResult.getResult()!=null){ + value = sscanResult.getResult(); + } + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis hmget exception:{},{},{}",key,start,node); + } finally { + // 返还到连接池 + close(jedis); + } + if (value != null) { + List newValue = Lists.newArrayList(); + for (String temp : value) { + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index c274a758..cefb0e46 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -228,4 +228,28 @@ public static void subscribe(JedisPubSub pubsub, String... channels) { RedisUtil.subscribe(set, pubsub, channels); } + + public static void sAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); + RedisUtil.sAdd(nodeList, key, jsonValue); + } + + public static Long sCard(String key) { + RedisNode node = redisRegister.randomGetRedisNode(key); + return RedisUtil.sCard(node, key); + } + + public static void sRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); + RedisUtil.sRem(nodeList, key, jsonValue); + } + + public static List sScan(String key,int start, Class clazz) { + RedisNode node = redisRegister.randomGetRedisNode(key); + return RedisUtil.sScan(node, key, clazz, start); + } + + } From 2df530519f4118c5fb261f1905b1664994a39b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 17:58:06 +0800 Subject: [PATCH 344/890] remove final --- .../mpush/common/router/UserChangeListener.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java index 4268bcda..e4a29def 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java @@ -10,7 +10,7 @@ /** * Created by ohun on 2016/1/4. */ -public final class UserChangeListener extends AbstractEventContainer implements MessageListener { +public class UserChangeListener extends AbstractEventContainer implements MessageListener { public static final String ONLINE_CHANNEL = "/mpush/online/"; public static final String OFFLINE_CHANNEL = "/mpush/offline/"; @@ -19,6 +19,7 @@ public final class UserChangeListener extends AbstractEventContainer implements public UserChangeListener() { if(ConfigCenter.holder.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); + ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this); } } @@ -40,15 +41,5 @@ public void userOffline(String userId){ @Override public void onMessage(String channel, String message) { -// if (getKickChannel().equals(channel)) { -// KickRemoteMsg msg = Jsons.fromJson(message, KickRemoteMsg.class); -// if (msg != null) { -// onReceiveKickRemoteMsg(msg); -// } else { -// LoggerManage.info(LogType.CONNECTION, "receive an error kick message=%s", message); -// } -// } else { -// LoggerManage.info(LogType.CONNECTION, "receive an error redis channel=%s",channel); -// } } } From f621c3201edca625e4bc2d9ad4f2f68600c1d0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 22 Feb 2016 22:27:06 +0800 Subject: [PATCH 345/890] size 10 --- .../shinemo/mpush/core/router/LocalRouterManager.java | 11 ++++++++++- .../com/shinemo/mpush/core/router/RouterCenter.java | 6 +++++- .../java/com/shinemo/mpush/tools/redis/RedisUtil.java | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index d47b1164..ce0a4a97 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -18,6 +18,12 @@ public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); + private RouterCenter center; + + public LocalRouterManager(RouterCenter center) { + this.center = center; + } + /** * 本地路由表 */ @@ -66,7 +72,10 @@ void onConnectionCloseEvent(ConnectionCloseEvent event) { //1.清除反向关系 String userId = connIdUserIds.remove(id); if (userId == null) return; - + + //TODO 用户下线。可能会出现问题。用户会先上线,然后老的链接下线的。 + center.getUserManager().userOffline(userId); + LocalRouter router = routers.get(userId); if (router == null) return; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 38fea170..31349196 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -20,7 +20,7 @@ public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); public static final RouterCenter INSTANCE = new RouterCenter(); - private final LocalRouterManager localRouterManager = new LocalRouterManager(); + private final LocalRouterManager localRouterManager = new LocalRouterManager(this); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); private final RouterChangeListener routerChangeListener = new RouterChangeListener(); private final UserManager userManager = new UserManager(); @@ -90,4 +90,8 @@ public RemoteRouterManager getRemoteRouterManager() { public RouterChangeListener getRouterChangeListener() { return routerChangeListener; } + + public UserManager getUserManager(){ + return userManager; + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 4e720e85..d62ed349 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -16,6 +16,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; public class RedisUtil { @@ -605,12 +606,12 @@ public static List sScan(RedisNode node, String key, Class clazz, int Jedis jedis = null; try { jedis = getClient(node); - ScanResult sscanResult = jedis.sscan(key, start+""); + ScanResult sscanResult = jedis.sscan(key, start+"",new ScanParams().count(10)); if(sscanResult!=null&&sscanResult.getResult()!=null){ value = sscanResult.getResult(); } } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hmget exception:{},{},{}",key,start,node); + LoggerManage.execption(LogType.REDIS, e, "redis sscan exception:{},{},{}",key,start,node); } finally { // 返还到连接池 close(jedis); From 9fd0a9d90f19fb9325f4dcf1474f427046a4297a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 09:23:29 +0800 Subject: [PATCH 346/890] add stat --- .../mpush/common/manage/user/UserManager.java | 26 +++++++++------ .../mpush/test/redis/RedisUtilTest.java | 32 +++++++++++++------ .../shinemo/mpush/tools/redis/RedisUtil.java | 23 +++++++++++++ .../mpush/tools/redis/manage/RedisManage.java | 6 ++++ 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index 10211af3..08379a27 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -10,39 +10,45 @@ public class UserManager { public void userOnline(String userId) { String onlineKey = RedisKey.getUserOnlineKey(); - RedisManage.sAdd(onlineKey, userId); + RedisManage.rpush(onlineKey, userId); String offlineKey = RedisKey.getUserOfflineKey(); - RedisManage.sRem(offlineKey, userId); + RedisManage.lrem(offlineKey, userId); } public void userOffline(String userId) { String onlineKey = RedisKey.getUserOnlineKey(); - RedisManage.sRem(onlineKey, userId); + RedisManage.lrem(onlineKey, userId); String offlineKey = RedisKey.getUserOfflineKey(); - RedisManage.sAdd(offlineKey, userId); + RedisManage.rpush(offlineKey, userId); } //在线用户 public long onlineUserNum(){ String onlineKey = RedisKey.getUserOnlineKey(); - return RedisManage.sCard(onlineKey); + return RedisManage.llen(onlineKey); } //离线用户 public long offlineUserNum(){ String offlineKey = RedisKey.getUserOfflineKey(); - return RedisManage.sCard(offlineKey); + return RedisManage.llen(offlineKey); } //在线用户列表 - public List onlineUserList(int start){ + public List onlineUserList(int start,int size){ + if(size<=10){ + size = 10; + } String onlineKey = RedisKey.getUserOnlineKey(); - return RedisManage.sScan(onlineKey, start, String.class); + return RedisManage.lrange(onlineKey, start, size-1, String.class); } //离线用户 - public List offlineUserList(int start){ + public List offlineUserList(int start,int size){ + if(size<=10){ + size = 10; + } String offlineKey = RedisKey.getUserOfflineKey(); - return RedisManage.sScan(offlineKey, start, String.class); + return RedisManage.lrange(offlineKey, start, size-1, String.class); } } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java index c96f76ed..46c8f582 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import com.google.common.collect.Lists; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.tools.redis.RedisNode; import com.shinemo.mpush.tools.redis.RedisUtil; @@ -18,14 +19,13 @@ public class RedisUtilTest { - RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); - RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); + RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - List nodeList = Lists.newArrayList(node, node2); + List nodeList = Lists.newArrayList(node); @Test public void testAddAndGetAndDelete() { - Jedis jedis = RedisUtil.getClient(node2); + Jedis jedis = RedisUtil.getClient(node); jedis.set("hi", "huang"); String ret = jedis.get("hi"); @@ -92,12 +92,12 @@ public void testKV() { User nowUser = RedisUtil.get(node, "test", User.class); System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - nowUser = RedisUtil.get(node2, "test", User.class); + nowUser = RedisUtil.get(node, "test", User.class); System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); RedisUtil.del(nodeList, "test"); - nowUser = RedisUtil.get(node2, "test", User.class); + nowUser = RedisUtil.get(node, "test", User.class); if (nowUser == null) { System.out.println("node2 nowUser is null"); } else { @@ -119,7 +119,7 @@ public void testKV() { e.printStackTrace(); } - nowUser = RedisUtil.get(node2, "test", User.class); + nowUser = RedisUtil.get(node, "test", User.class); if (nowUser == null) { System.out.println("node2 nowUser is null"); } else { @@ -145,7 +145,7 @@ public void hashTest(){ User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); - nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); + nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); @@ -159,7 +159,7 @@ public void hashTest(){ RedisUtil.hdel(nodeList, "hashhuang", "hi"); - nowUser = RedisUtil.hget(node2, "hashhuang", "hi", User.class); + nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); if(nowUser==null){ System.out.println("node2 nowUser is null"); }else{ @@ -174,5 +174,19 @@ public void hashTest(){ } } + + @Test + public void testSet(){ + System.out.println(RedisUtil.sCard(node, RedisKey.getUserOnlineKey())); + + List onlineUserIdList = RedisUtil.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); + System.out.println(onlineUserIdList.size()); + + } + + @Test + public void testlist(){ + + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index d62ed349..2b33b459 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -479,6 +479,29 @@ public static long llen(RedisNode node, String key) { } return len; } + + /** + * 移除表中所有与 value 相等的值 + * @param nodeList + * @param key + * @param value + */ + public static void lRem(List nodeList, String key,String value) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.lrem(key, 0, value); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis lrem exception:{},{},{}",key,value,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + + } /********************* list redis end ********************************/ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index cefb0e46..21dce6bb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -210,6 +210,12 @@ public static long llen(String key) { RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.llen(node, key); } + + public static void lrem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); + RedisUtil.lRem(nodeList, key, jsonValue); + } public static void publish(String channel, T message) { From 9e71911205791b8878afaab8d45676e8b8148d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 09:54:44 +0800 Subject: [PATCH 347/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dllen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java | 2 +- .../src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java index 46c8f582..619481a5 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java @@ -186,7 +186,7 @@ public void testSet(){ @Test public void testlist(){ - + RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 2b33b459..8c1cf042 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -470,7 +470,7 @@ public static long llen(RedisNode node, String key) { Jedis jedis = null; try { jedis = getClient(node); - jedis.llen(key); + len = jedis.llen(key); } catch (Exception e) { LoggerManage.execption(LogType.REDIS, e, "redis llen exception:{},{}",key,node); } finally { From 7f0a7dec6b5ebe84e5e10895e04f1b6c9e4d9a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 10:10:56 +0800 Subject: [PATCH 348/890] add sorted set --- .../mpush/common/manage/user/UserManager.java | 20 ++--- .../shinemo/mpush/tools/redis/RedisUtil.java | 90 +++++++++++++++++++ .../mpush/tools/redis/manage/RedisManage.java | 21 +++++ 3 files changed, 121 insertions(+), 10 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index 08379a27..f75285aa 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -10,45 +10,45 @@ public class UserManager { public void userOnline(String userId) { String onlineKey = RedisKey.getUserOnlineKey(); - RedisManage.rpush(onlineKey, userId); + RedisManage.zAdd(onlineKey, userId); String offlineKey = RedisKey.getUserOfflineKey(); - RedisManage.lrem(offlineKey, userId); + RedisManage.zRem(offlineKey, userId); } public void userOffline(String userId) { String onlineKey = RedisKey.getUserOnlineKey(); - RedisManage.lrem(onlineKey, userId); + RedisManage.zRem(onlineKey, userId); String offlineKey = RedisKey.getUserOfflineKey(); - RedisManage.rpush(offlineKey, userId); + RedisManage.zAdd(offlineKey, userId); } //在线用户 public long onlineUserNum(){ String onlineKey = RedisKey.getUserOnlineKey(); - return RedisManage.llen(onlineKey); + return RedisManage.zCard(onlineKey); } //离线用户 public long offlineUserNum(){ String offlineKey = RedisKey.getUserOfflineKey(); - return RedisManage.llen(offlineKey); + return RedisManage.zCard(offlineKey); } //在线用户列表 public List onlineUserList(int start,int size){ - if(size<=10){ + if(size<10){ size = 10; } String onlineKey = RedisKey.getUserOnlineKey(); - return RedisManage.lrange(onlineKey, start, size-1, String.class); + return RedisManage.zrange(onlineKey, start, size-1, String.class); } //离线用户 public List offlineUserList(int start,int size){ - if(size<=10){ + if(size<10){ size = 10; } String offlineKey = RedisKey.getUserOfflineKey(); - return RedisManage.lrange(offlineKey, start, size-1, String.class); + return RedisManage.zrange(offlineKey, start, size-1, String.class); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index 8c1cf042..b03b8677 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -650,4 +650,94 @@ public static List sScan(RedisNode node, String key, Class clazz, int } + /********************* + * sorted set + ********************************/ + /** + * @param nodeList + * @param key + * @param value + * @param time seconds + */ + public static void zAdd(List nodeList, String key, String value) { + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.zadd(key, 0, value); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis zadd exception:{},{},{}",key,value,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + } + + /** + * @param node 返回个数 + * @param key + * @param clazz + * @return + */ + public static Long zCard(RedisNode node, String key) { + + Long value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.zcard(key); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis zcard exception:{},{}",key,node); + } finally { + // 返还到连接池 + close(jedis); + } + return value; + } + + public static void zRem(List nodeList, String key,String value) { + + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + jedis.zrem(key, value); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis srem exception:{},{},{}",key,value,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + + } + + /** + * 从列表中获取指定返回的元素 start 和 end + * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public static List zrange(RedisNode node, String key, int start, int end, Class clazz) { + Set value = null; + Jedis jedis = null; + try { + jedis = getClient(node); + value = jedis.zrange(key, start, end); + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis zrange exception:{},{},{},{}",key,start,end,node); + } finally { + // 返还到连接池 + close(jedis); + } + if (value != null) { + List newValue = Lists.newArrayList(); + for (String temp : value) { + newValue.add(Jsons.fromJson(temp, clazz)); + } + return newValue; + } + return null; + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index 21dce6bb..a445bcb9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -257,5 +257,26 @@ public static List sScan(String key,int start, Class clazz) { return RedisUtil.sScan(node, key, clazz, start); } + public static void zAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); + RedisUtil.zAdd(nodeList, key, jsonValue); + } + + public static Long zCard(String key) { + RedisNode node = redisRegister.randomGetRedisNode(key); + return RedisUtil.zCard(node, key); + } + + public static void zRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); + RedisUtil.zRem(nodeList, key, jsonValue); + } + + public static List zrange(String key, int start, int end, Class clazz) { + RedisNode node = redisRegister.randomGetRedisNode(key); + return RedisUtil.zrange(node, key, start, end, clazz); + } } From 8825bbb2862ef34f2ee5d67527d04dfdd3fea6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 10:37:10 +0800 Subject: [PATCH 349/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BB=9F=E8=AE=A1bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/router/LocalRouterManager.java | 8 ++++++-- .../java/com/shinemo/mpush/test/redis/RedisUtilTest.java | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index ce0a4a97..575fafdd 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.router.RouterManager; import com.shinemo.mpush.common.AbstractEventContainer; +import com.shinemo.mpush.common.router.RemoteRouter; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; @@ -73,8 +74,11 @@ void onConnectionCloseEvent(ConnectionCloseEvent event) { String userId = connIdUserIds.remove(id); if (userId == null) return; - //TODO 用户下线。可能会出现问题。用户会先上线,然后老的链接下线的。 - center.getUserManager().userOffline(userId); + // 用户下线。可能会出现问题。用户会先上线,然后老的链接下线的。所以,如果远程不存在,则该用户肯定下线。 + RemoteRouter remoteRouter = center.getRemoteRouterManager().lookup(userId); + if(remoteRouter==null){ + center.getUserManager().userOffline(userId); + } LocalRouter router = routers.get(userId); if (router == null) return; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java index 619481a5..a0151360 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java @@ -188,5 +188,13 @@ public void testSet(){ public void testlist(){ RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); } + + @Test + public void testsortedset(){ + RedisUtil.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); + + long len =RedisUtil.zCard(node, RedisKey.getUserOnlineKey()); + System.out.println(len); + } } From cb9b25f21c596a2b7e03a2394b6e51d4a45f2ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 10:47:20 +0800 Subject: [PATCH 350/890] =?UTF-8?q?=E5=9C=A8=E7=BA=BFlog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/common/manage/user/UserManager.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index f75285aa..1e60a492 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -2,17 +2,23 @@ import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.tools.redis.manage.RedisManage; //查询使用 public class UserManager { + private static final Logger log = LoggerFactory.getLogger(UserManager.class); + public void userOnline(String userId) { String onlineKey = RedisKey.getUserOnlineKey(); RedisManage.zAdd(onlineKey, userId); String offlineKey = RedisKey.getUserOfflineKey(); RedisManage.zRem(offlineKey, userId); + log.info("user online {}",userId); } public void userOffline(String userId) { @@ -20,6 +26,7 @@ public void userOffline(String userId) { RedisManage.zRem(onlineKey, userId); String offlineKey = RedisKey.getUserOfflineKey(); RedisManage.zAdd(offlineKey, userId); + log.info("user offline {}",userId); } //在线用户 From 89d1ecf7900d0ad83b8099e9ae704c486843008a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 11:46:46 +0800 Subject: [PATCH 351/890] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=8B=E7=BA=BF=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/event/UserOfflineEvent.java | 30 ++++++++++++++ .../mpush/api/event/UserOnlineEvent.java | 26 ++++++++++++ .../mpush/common/manage/user/UserManager.java | 1 + .../mpush/core/handler/BindUserHandler.java | 5 +++ .../mpush/core/handler/UnbindUserHandler.java | 6 +++ .../mpush/core/router/LocalRouterManager.java | 19 +++------ .../mpush/core/router/RouterCenter.java | 4 +- .../mpush/core/router/UserManager.java | 40 +++++++++++++++++++ .../connection/NettyConnectionManager.java | 5 +++ 9 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java create mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java new file mode 100644 index 00000000..260b3af2 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java @@ -0,0 +1,30 @@ +package com.shinemo.mpush.api.event; + +import com.shinemo.mpush.api.connection.Connection; + +/** + * 链接超时,用户解绑的时候,才会触发该事件 + * + */ +public final class UserOfflineEvent implements Event { + + private final Connection connection; + private final String userId; + + public UserOfflineEvent(Connection connection, String userId) { + this.connection = connection; + this.userId = userId; + } + + public UserOfflineEvent(Connection connection) { + this.connection = connection; + this.userId = null; + } + + public Connection getConnection() { + return connection; + } + public String getUserId() { + return userId; + } +} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java new file mode 100644 index 00000000..4da6f450 --- /dev/null +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java @@ -0,0 +1,26 @@ +package com.shinemo.mpush.api.event; + +import com.shinemo.mpush.api.connection.Connection; + +/** + * 绑定用户的时候才会触发该事件 + */ +public final class UserOnlineEvent implements Event { + + private final Connection connection; + private final String userId; + + public UserOnlineEvent(Connection connection, String userId) { + this.connection = connection; + this.userId = userId; + } + + public Connection getConnection() { + return connection; + } + public String getUserId() { + return userId; + } + + +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index 1e60a492..fdecf413 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -34,6 +34,7 @@ public long onlineUserNum(){ String onlineKey = RedisKey.getUserOnlineKey(); return RedisManage.zCard(onlineKey); } + //离线用户 public long offlineUserNum(){ String offlineKey = RedisKey.getUserOfflineKey(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java index 22b0f1cd..9c623381 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java @@ -3,7 +3,9 @@ import com.google.common.base.Strings; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.BindUserMessage; import com.shinemo.mpush.common.message.ErrorMessage; @@ -35,6 +37,9 @@ public void handle(BindUserMessage message) { //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { + + EventBus.INSTANCE.post(new UserOnlineEvent( message.getConnection(),message.userId)); + OkMessage.from(message).setData("bind success").send(); LoggerManage.log(LogType.CONNECTION, "bind user success, userId={}, session={}", message.userId, context); } else { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java index 035fa2e1..7801925a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java @@ -3,7 +3,10 @@ import com.google.common.base.Strings; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; +import com.shinemo.mpush.api.event.UserOfflineEvent; +import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.BindUserMessage; import com.shinemo.mpush.common.message.ErrorMessage; @@ -35,6 +38,9 @@ public void handle(BindUserMessage message) { //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 boolean success = RouterCenter.INSTANCE.unRegister(message.userId); if (success) { + + EventBus.INSTANCE.post(new UserOfflineEvent( message.getConnection(),message.userId)); + OkMessage.from(message).setData("unbind success").send(); LoggerManage.info(LogType.CONNECTION, "unbind user success, userId={}, session={}", message.userId, context); } else { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 575fafdd..3cfd28a7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -19,12 +19,6 @@ public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); - private RouterCenter center; - - public LocalRouterManager(RouterCenter center) { - this.center = center; - } - /** * 本地路由表 */ @@ -60,6 +54,10 @@ public LocalRouter lookup(String userId) { LOGGER.info("lookup local router userId={}, router={}", userId, router); return router; } + + public String getUserIdByConnId(String connId){ + return connIdUserIds.get(connId); + } /** * 监听链接关闭事件,清理失效的路由 @@ -74,20 +72,15 @@ void onConnectionCloseEvent(ConnectionCloseEvent event) { String userId = connIdUserIds.remove(id); if (userId == null) return; - // 用户下线。可能会出现问题。用户会先上线,然后老的链接下线的。所以,如果远程不存在,则该用户肯定下线。 - RemoteRouter remoteRouter = center.getRemoteRouterManager().lookup(userId); - if(remoteRouter==null){ - center.getUserManager().userOffline(userId); - } - LocalRouter router = routers.get(userId); if (router == null) return; - + //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 if (id.equals(router.getRouteValue().getId())) { //3.删除路由 routers.remove(userId); LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); + }else{ //如果不相等,则log一下 LOGGER.info("clean disconnected local route, not clean:userId={}, route={}",userId,router); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 31349196..afb39de0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -20,7 +20,7 @@ public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); public static final RouterCenter INSTANCE = new RouterCenter(); - private final LocalRouterManager localRouterManager = new LocalRouterManager(this); + private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); private final RouterChangeListener routerChangeListener = new RouterChangeListener(); private final UserManager userManager = new UserManager(); @@ -46,7 +46,6 @@ public boolean register(String userId, Connection connection) { try { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); - userManager.userOnline(userId); } catch (Exception e) { LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); @@ -67,7 +66,6 @@ public boolean register(String userId, Connection connection) { public boolean unRegister(String userId) { localRouterManager.unRegister(userId); remoteRouterManager.unRegister(userId); - userManager.userOffline(userId); return true; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java new file mode 100644 index 00000000..b387498c --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java @@ -0,0 +1,40 @@ +package com.shinemo.mpush.core.router; + +import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.event.UserOfflineEvent; +import com.shinemo.mpush.api.event.UserOnlineEvent; +import com.shinemo.mpush.common.EventBus; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/23. + */ +public final class UserManager extends com.shinemo.mpush.common.manage.user.UserManager{ + public static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); + + public UserManager() { + EventBus.INSTANCE.register(this); + } + + @Subscribe + void handlerUserOnlineEvent(UserOnlineEvent event) { + userOnline(event.getUserId()); + } + + @Subscribe + void handlerUserOfflineEvent(UserOfflineEvent event) { + if(event.getUserId()==null){//链接超时 + String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); + if(StringUtils.isNotBlank(userId)){ + userOffline(event.getUserId()); + } + }else{ //解绑用户 + userOffline(event.getUserId()); + } + + } + +} diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 637b49ef..2e225e6c 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -6,6 +6,8 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; +import com.shinemo.mpush.api.event.UserOfflineEvent; +import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; @@ -103,6 +105,9 @@ public void run(Timeout timeout) throws Exception { } if (connection.heartbeatTimeout()) { if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { + + EventBus.INSTANCE.post(new UserOfflineEvent(connection)); + connection.close(); LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(), connection.getSessionContext().deviceId); return; From 1cacd9f53d647cef76018f9c0ccfaef72d5c282d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 13:05:06 +0800 Subject: [PATCH 352/890] =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E5=8F=91=E8=B5=B7=E5=85=B3=E9=97=AD=E9=93=BE=E6=8E=A5=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/event/UserOfflineEvent.java | 2 +- .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java index 260b3af2..8c029282 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java @@ -3,7 +3,7 @@ import com.shinemo.mpush.api.connection.Connection; /** - * 链接超时,用户解绑的时候,才会触发该事件 + * 链接超时,用户解绑的时候,用户主动关闭链接,才会触发该事件 * */ public final class UserOfflineEvent implements Event { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index c0fa999f..d0839176 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -4,7 +4,9 @@ import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; +import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.PacketReceiver; +import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.log.LogLevel; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.connection.NettyConnection; @@ -63,6 +65,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { LoggerManage.log(security, LogLevel.INFO, "client disconnect channel=%s", ctx.channel()); + Connection connection = connectionManager.get(ctx.channel()); + EventBus.INSTANCE.post(new UserOfflineEvent(connection)); connectionManager.remove(ctx.channel()); } } \ No newline at end of file From e582979242bbe7dfb83138c7eb32fa3bb34100b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 13:27:23 +0800 Subject: [PATCH 353/890] log bug fix --- .../com/shinemo/mpush/log/LoggerManage.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index 704fd8f9..39799912 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -68,7 +68,6 @@ public static void execption(LogType type,Throwable ex,String format,Object... a * @param arguments */ public static void log(LogType type,LogLevel level,Throwable ex,String format,Object... arguments){ - String ret = String.format(format, arguments); if(level == null){ level = LogLevel.WARN; } @@ -76,30 +75,30 @@ public static void log(LogType type,LogLevel level,Throwable ex,String format,Ob if(ex!=null){ if(log!=null){ if(level.equals(LogLevel.WARN)){ - log.warn(ret,ex); + log.warn(format,ex); }else if(level.equals(LogLevel.INFO)){ - log.info(ret,ex); + log.info(format,ex); }else if(level.equals(LogLevel.DEBUG)){ - log.debug(ret,ex); + log.debug(format,ex); }else if(level.equals(LogLevel.ERROR)){ - log.error(ret,ex); + log.error(format,ex); } }else{ - defaultLog.warn(ret,ex); + defaultLog.warn(format,ex); } }else{ if(log!=null){ if(level.equals(LogLevel.WARN)){ - log.warn(ret); + log.warn(format); }else if(level.equals(LogLevel.INFO)){ - log.info(ret); + log.info(format); }else if(level.equals(LogLevel.DEBUG)){ - log.debug(ret); + log.debug(format); }else if(level.equals(LogLevel.ERROR)){ - log.error(ret); + log.error(format); } }else{ - defaultLog.warn(ret); + defaultLog.warn(format); } } } From df57a4996465488372a0c0347c82b6e36f689091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 13:36:09 +0800 Subject: [PATCH 354/890] bugfix for log --- .../core/server/ServerChannelHandler.java | 6 +++--- .../com/shinemo/mpush/log/LoggerManage.java | 20 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index d0839176..789e84bb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -50,13 +50,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LoggerManage.log(security, LogLevel.INFO, "client exceptionCaught channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO, "client exceptionCaught channel={}", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, LogLevel.INFO, "client connect channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO, "client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -64,7 +64,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, LogLevel.INFO, "client disconnect channel=%s", ctx.channel()); + LoggerManage.log(security, LogLevel.INFO, "client disconnect channel={}", ctx.channel()); Connection connection = connectionManager.get(ctx.channel()); EventBus.INSTANCE.post(new UserOfflineEvent(connection)); connectionManager.remove(ctx.channel()); diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index 39799912..85634742 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -75,30 +75,30 @@ public static void log(LogType type,LogLevel level,Throwable ex,String format,Ob if(ex!=null){ if(log!=null){ if(level.equals(LogLevel.WARN)){ - log.warn(format,ex); + log.warn(format,arguments,ex); }else if(level.equals(LogLevel.INFO)){ - log.info(format,ex); + log.info(format,arguments,ex); }else if(level.equals(LogLevel.DEBUG)){ - log.debug(format,ex); + log.debug(format,arguments,ex); }else if(level.equals(LogLevel.ERROR)){ - log.error(format,ex); + log.error(format,arguments,ex); } }else{ - defaultLog.warn(format,ex); + defaultLog.warn(format,arguments,ex); } }else{ if(log!=null){ if(level.equals(LogLevel.WARN)){ - log.warn(format); + log.warn(format,arguments); }else if(level.equals(LogLevel.INFO)){ - log.info(format); + log.info(format,arguments); }else if(level.equals(LogLevel.DEBUG)){ - log.debug(format); + log.debug(format,arguments); }else if(level.equals(LogLevel.ERROR)){ - log.error(format); + log.error(format,arguments); } }else{ - defaultLog.warn(format); + defaultLog.warn(format,arguments); } } } From 8dc407260df466e4375ca7dd2b494e91896d4a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 13:50:21 +0800 Subject: [PATCH 355/890] add --- .../shinemo/mpush/core/router/RouterCenter.java | 1 - .../mpush/core/router/RouterChangeListener.java | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index afb39de0..585095d9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -5,7 +5,6 @@ import com.shinemo.mpush.api.router.Router; import com.shinemo.mpush.api.router.ClientLocation; import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.manage.user.UserManager; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; import com.shinemo.mpush.tools.MPushUtil; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 874a7faf..da8fb7b5 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -69,9 +69,9 @@ public void kickLocal(final String userId, final LocalRouter router) { public void operationComplete(ChannelFuture future) throws Exception { future.channel().close(); if (future.isSuccess()) { - LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId=%s, router=%s", userId, router); + LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId={}, router={}", userId, router); } else { - LoggerManage.info(LogType.CONNECTION, "kick local connection failure, userId=%s, router=%s", userId, router); + LoggerManage.info(LogType.CONNECTION, "kick local connection failure, userId={}, router={}", userId, router); } } }); @@ -90,7 +90,7 @@ public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(MPushUtil.getLocalIp())) { - LoggerManage.info(LogType.CONNECTION, "kick remote user but router in local, userId=%s", userId); + LoggerManage.info(LogType.CONNECTION, "kick remote user but router in local, userId={}", userId); return; } @@ -114,7 +114,7 @@ public void kickRemote(String userId, RemoteRouter router) { public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, target server error, localIp=%s, msg=%s", MPushUtil.getLocalIp(), msg); + LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); return; } @@ -129,7 +129,7 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //2.2发送踢人消息到客户端 kickLocal(userId, router); } else { - LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg=%s", msg); + LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg={}", msg); } } @@ -140,10 +140,10 @@ public void onMessage(String channel, String message) { if (msg != null) { onReceiveKickRemoteMsg(msg); } else { - LoggerManage.info(LogType.CONNECTION, "receive an error kick message=%s", message); + LoggerManage.info(LogType.CONNECTION, "receive an error kick message={}", message); } } else { - LoggerManage.info(LogType.CONNECTION, "receive an error redis channel=%s",channel); + LoggerManage.info(LogType.CONNECTION, "receive an error redis channel={}",channel); } } } From 15aa67c2847a66baf3ae62adfd713420b29f0e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 14:02:01 +0800 Subject: [PATCH 356/890] =?UTF-8?q?=E4=BC=98=E5=85=88=E4=BD=BF=E7=94=A8loc?= =?UTF-8?q?al=20userId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/shinemo/mpush/core/router/UserManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java index b387498c..a3e14f10 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java @@ -29,7 +29,7 @@ void handlerUserOfflineEvent(UserOfflineEvent event) { if(event.getUserId()==null){//链接超时 String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); if(StringUtils.isNotBlank(userId)){ - userOffline(event.getUserId()); + userOffline(userId); } }else{ //解绑用户 userOffline(event.getUserId()); From e5d268925fd7e1d5bd612bde1928dcc40a29d6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 15:11:38 +0800 Subject: [PATCH 357/890] pub sub user online offline --- .../java/com/shinemo/mpush/core/router/UserManager.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java index a3e14f10..220b3184 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.tools.redis.manage.RedisManage; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -15,6 +16,10 @@ public final class UserManager extends com.shinemo.mpush.common.manage.user.UserManager{ public static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); + public static final String ONLINE_CHANNEL = "/mpush/online/"; + + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; + public UserManager() { EventBus.INSTANCE.register(this); } @@ -22,6 +27,7 @@ public UserManager() { @Subscribe void handlerUserOnlineEvent(UserOnlineEvent event) { userOnline(event.getUserId()); + RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe @@ -30,6 +36,7 @@ void handlerUserOfflineEvent(UserOfflineEvent event) { String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); if(StringUtils.isNotBlank(userId)){ userOffline(userId); + RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); } }else{ //解绑用户 userOffline(event.getUserId()); From 281f2fe2888be99e6d4d4cc2129bac2c13a1362d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 17:09:56 +0800 Subject: [PATCH 358/890] modified debug --- debug.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/debug.sh b/debug.sh index 2349b531..22c56f31 100755 --- a/debug.sh +++ b/debug.sh @@ -5,7 +5,10 @@ ENV=dev base_dir=`pwd` echo "start assembly lib..." -mvn clean install -P $ENV + +rm -rf $base_dir/target + +mvn clean install assembly:assembly -P $ENV echo "start tar mpush..." cd $base_dir/target From 1c16393dd37445257ca109901dae488d015bfe14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 17:15:20 +0800 Subject: [PATCH 359/890] add log --- .../shinemo/mpush/common/router/UserChangeListener.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java index e4a29def..7ef38392 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java @@ -1,5 +1,8 @@ package com.shinemo.mpush.common.router; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.shinemo.mpush.common.AbstractEventContainer; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; @@ -11,6 +14,9 @@ * Created by ohun on 2016/1/4. */ public class UserChangeListener extends AbstractEventContainer implements MessageListener { + + private static final Logger log = LoggerFactory.getLogger(UserChangeListener.class); + public static final String ONLINE_CHANNEL = "/mpush/online/"; public static final String OFFLINE_CHANNEL = "/mpush/offline/"; @@ -20,6 +26,8 @@ public UserChangeListener() { if(ConfigCenter.holder.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this); + }else{ + log.error("UserChangeListener is not localhost,required:{},but:{}",ConfigCenter.holder.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp()); } } From 7c214b5a7b5b17004854378bcf6f7523c7179496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 18:08:41 +0800 Subject: [PATCH 360/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=A4=96=E7=BD=91ip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/cs/ConnectionServerApplication.java | 17 +++++++++++--- .../ConnectionServerApplicationTest.java | 2 +- .../java/com/shinemo/mpush/zk/ZkUtilTest.java | 2 +- .../connection/mpns/ConnectionClientMain.java | 2 +- .../mpush/test/redis/MPushUtilTest.java | 14 ++++++++++++ .../com/shinemo/mpush/tools/MPushUtil.java | 22 +++++++++++++++++++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index a2bdd4b7..468cde40 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -10,14 +10,17 @@ public class ConnectionServerApplication extends Application{ private transient GatewayServerApplication gatewayServerApplication; - public ConnectionServerApplication() { - this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp()); + private String extranetIp; + + public ConnectionServerApplication() throws Exception { + this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp(),MPushUtil.getExtranetAddress()); } - public ConnectionServerApplication(int port,String path,String ip) { + public ConnectionServerApplication(int port,String path,String ip,String extranetIp) { setPort(port); setServerRegisterZkPath(path); setIp(ip); + setExtranetIp(extranetIp); } public GatewayServerApplication getGatewayServerApplication() { @@ -28,4 +31,12 @@ public void setGatewayServerApplication(GatewayServerApplication gatewayServerAp this.gatewayServerApplication = gatewayServerApplication; } + public String getExtranetIp() { + return extranetIp; + } + + public void setExtranetIp(String extranetIp) { + this.extranetIp = extranetIp; + } + } diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java index f4054582..ede5fed3 100644 --- a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java +++ b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java @@ -8,7 +8,7 @@ public class ConnectionServerApplicationTest { @Test - public void testJson(){ + public void testJson() throws Exception{ ConnectionServerApplication application = new ConnectionServerApplication(); diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java index bb294d57..b479bb4a 100644 --- a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java +++ b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java @@ -99,7 +99,7 @@ public void testLocalIp() { } @Test - public void testRegisterIp() { + public void testRegisterIp() throws Exception { String localIp = MPushUtil.getInetAddress(); ConnectionServerApplication app = new ConnectionServerApplication(); zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java index 51e83af3..7fe12006 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java @@ -9,7 +9,7 @@ public class ConnectionClientMain extends AbstractClient { - private static final List applicationLists = Lists.newArrayList(new ConnectionServerApplication(20882,"","111.1.57.148")); + private static final List applicationLists = Lists.newArrayList(new ConnectionServerApplication(20882,"","111.1.57.148","111.1.57.148")); public List getApplicationList(){ return Lists.newArrayList(applicationLists); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java new file mode 100644 index 00000000..09ff7c4d --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java @@ -0,0 +1,14 @@ +package com.shinemo.mpush.test.redis; + +import org.junit.Test; + +import com.shinemo.mpush.tools.MPushUtil; + +public class MPushUtilTest { + + @Test + public void getIp() throws Exception{ + System.out.println(MPushUtil.getExtranetAddress()); + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 4c30eef9..07a66042 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -60,6 +60,28 @@ public static String getInetAddress() { return "127.0.0.1"; } } + + public static String getExtranetAddress() throws Exception{ + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress address = null; + while (interfaces.hasMoreElements()) { + NetworkInterface ni = interfaces.nextElement(); + Enumeration addresses = ni.getInetAddresses(); + while (addresses.hasMoreElements()) { + address = addresses.nextElement(); + if(!address.isSiteLocalAddress()){ + return address.getHostAddress(); + } + } + } + LOGGER.warn("getExtranetAddress is null"); + return null; + } catch (Throwable e) { + LOGGER.error("getExtranetAddress exception", e); + throw new Exception(e); + } + } public static String headerToString(Map headers) { if (headers != null && headers.size() > 0) { From a96d926d16caa07956a1fb79beb462a753dddee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 18:14:47 +0800 Subject: [PATCH 361/890] =?UTF-8?q?add=20=E5=A4=96=E7=BD=91ip=20=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/tools/MPushUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 07a66042..f4c7ed84 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -70,7 +70,7 @@ public static String getExtranetAddress() throws Exception{ Enumeration addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { address = addresses.nextElement(); - if(!address.isSiteLocalAddress()){ + if(!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && !address.isSiteLocalAddress()){ return address.getHostAddress(); } } From a3202e73aa005fe7bf930a168c64a539370ae21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:08:22 +0800 Subject: [PATCH 362/890] add ip --- .../java/com/shinemo/mpush/common/app/Application.java | 10 ++++++++++ .../shinemo/mpush/cs/ConnectionServerApplication.java | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java b/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java index d0daee5b..a5b89619 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java @@ -12,6 +12,8 @@ public abstract class Application { private int port; private transient String serverRegisterZkPath; + + private String extranetIp; public int getPort() { return port; @@ -36,5 +38,13 @@ public String getIp() { public void setIp(String ip) { this.ip = ip; } + + public String getExtranetIp() { + return extranetIp; + } + + public void setExtranetIp(String extranetIp) { + this.extranetIp = extranetIp; + } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java index 468cde40..fccaa29b 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java @@ -10,8 +10,6 @@ public class ConnectionServerApplication extends Application{ private transient GatewayServerApplication gatewayServerApplication; - private String extranetIp; - public ConnectionServerApplication() throws Exception { this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp(),MPushUtil.getExtranetAddress()); } @@ -31,12 +29,4 @@ public void setGatewayServerApplication(GatewayServerApplication gatewayServerAp this.gatewayServerApplication = gatewayServerApplication; } - public String getExtranetIp() { - return extranetIp; - } - - public void setExtranetIp(String extranetIp) { - this.extranetIp = extranetIp; - } - } From 41084c016eff5b2bf7c154f0e49d42e9c2a2edd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:20:17 +0800 Subject: [PATCH 363/890] add log --- .../com/shinemo/mpush/tools/zk/listener/DataChangeListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java index 822cb664..f94b1566 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java @@ -15,7 +15,7 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc if (path.isEmpty()) { return; } - LoggerManage.log(LogType.ZK, "DataChangeListener:%s,%s,namespace:%s", path,listenerPath(),client.getNamespace()); + LoggerManage.log(LogType.ZK, "DataChangeListener:{},{},namespace:{}", path,listenerPath(),client.getNamespace()); if(path.startsWith(listenerPath())){ dataChanged(client, event, path); } From 0164beaf44eb0b43e2ae8e31411d84b7d0c1313e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:21:44 +0800 Subject: [PATCH 364/890] add log --- .../src/main/java/com/shinemo/mpush/common/AbstractServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 89e26db0..034b6163 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -129,6 +129,7 @@ public void onFailure(String message) { //step7 注册应用到zk public void registerServerToZk(String path,String value){ zkRegister.registerEphemeralSequential(path, value); + log.error("register server to zk:{},{}",path,value); } public void start(){ From e59649e76d8ad0437f2541113a2135d30fc6046e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:42:58 +0800 Subject: [PATCH 365/890] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/conn/client}/ConnectionServerApplication.java | 2 +- .../mpush/push}/manage/impl/ConnectionServerManage.java | 4 ++-- .../push}/zk/listener/impl/ConnectionServerPathListener.java | 4 ++-- .../services/com.shinemo.mpush.common.manage.ServerManage | 3 ++- .../main/java/com/shinemo/mpush/cs/ConnectionServerMain.java | 1 + .../com/shinemo/mpush/ConnectionServerApplicationTest.java | 2 +- mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java | 2 +- .../mpush/test/connection/client/ConnectionClientMain.java | 4 ++-- .../java/com/shinemo/mpush/test/connection/client/Main.java | 2 +- .../mpush/test/connection/mpns/ConnectionClientMain.java | 2 +- .../java/com/shinemo/mpush/test/connection/mpns/Main.java | 2 +- 11 files changed, 15 insertions(+), 13 deletions(-) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-client/src/main/java/com/shinemo/mpush/conn/client}/ConnectionServerApplication.java (96%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-client/src/main/java/com/shinemo/mpush/push}/manage/impl/ConnectionServerManage.java (91%) rename {mpush-cs/src/main/java/com/shinemo/mpush/cs => mpush-client/src/main/java/com/shinemo/mpush/push}/zk/listener/impl/ConnectionServerPathListener.java (89%) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java similarity index 96% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java rename to mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java index fccaa29b..07b72f7d 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerApplication.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs; +package com.shinemo.mpush.conn.client; import com.shinemo.mpush.common.app.Application; import com.shinemo.mpush.conn.client.GatewayServerApplication; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java similarity index 91% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java index bdbaef4a..087707bb 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/manage/impl/ConnectionServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.cs.manage.impl; +package com.shinemo.mpush.push.manage.impl; import java.util.Collection; import java.util.Collections; @@ -11,7 +11,7 @@ import com.google.common.collect.Maps; import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; public class ConnectionServerManage implements ServerManage{ diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java similarity index 89% rename from mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java rename to mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java index 325d96dd..9fa1f8d8 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/zk/listener/impl/ConnectionServerPathListener.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.cs.zk.listener.impl; +package com.shinemo.mpush.push.zk.listener.impl; import com.shinemo.mpush.common.manage.ServerManage; import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; -import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.tools.spi.ServiceContainer; import com.shinemo.mpush.tools.zk.ZKPath; diff --git a/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage index f15779a3..c23f617e 100644 --- a/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ b/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage @@ -1 +1,2 @@ -gatewayServerManage=com.shinemo.mpush.push.manage.impl.GatewayServerManage \ No newline at end of file +gatewayServerManage=com.shinemo.mpush.push.manage.impl.GatewayServerManage +connectionServerManage=com.shinemo.mpush.push.manage.impl.ConnectionServerManage diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 937604bc..e694d188 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.Server; import com.shinemo.mpush.common.AbstractServer; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java index ede5fed3..b08722be 100644 --- a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java +++ b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java @@ -2,7 +2,7 @@ import org.junit.Test; -import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.tools.Jsons; public class ConnectionServerApplicationTest { diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java index b479bb4a..f25c9acd 100644 --- a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java +++ b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java @@ -8,8 +8,8 @@ import org.junit.Test; import com.google.common.collect.Lists; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.core.server.ConnectionServer; -import com.shinemo.mpush.cs.ConnectionServerApplication; import com.shinemo.mpush.tools.Constants; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Jsons; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java index aede62ea..145588ef 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java @@ -5,8 +5,8 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.common.AbstractClient; import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.cs.ConnectionServerApplication; -import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; +import com.shinemo.mpush.push.zk.listener.impl.ConnectionServerPathListener; import com.shinemo.mpush.tools.spi.ServiceContainer; public class ConnectionClientMain extends AbstractClient { diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java index 1ef7112f..b8c7070c 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java @@ -5,7 +5,7 @@ import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.conn.client.ClientChannelHandler; -import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java index 7fe12006..ad72e897 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java @@ -4,7 +4,7 @@ import com.google.common.collect.Lists; import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; public class ConnectionClientMain extends AbstractClient { diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java index 9ac642de..30dfbfba 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java @@ -5,7 +5,7 @@ import com.shinemo.mpush.common.security.CipherBox; import com.shinemo.mpush.conn.client.ClientChannelHandler; -import com.shinemo.mpush.cs.ConnectionServerApplication; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.netty.client.NettyClientFactory; import com.shinemo.mpush.netty.client.SecurityNettyClient; From 977e7cd76ff7795cb83dc0e95248d54f2cd535c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:43:47 +0800 Subject: [PATCH 366/890] =?UTF-8?q?remove=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-cs/pom.xml | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index ecaca99b..fcc8c779 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -44,29 +44,6 @@ - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-cs - - - - - - maven-dependency-plugin From 5511ed10090ef74aab8ee2dddc866170cd395ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:45:40 +0800 Subject: [PATCH 367/890] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-cs/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index fcc8c779..0cec0c04 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -44,6 +44,7 @@ + maven-dependency-plugin From 0e316aea288881895eb25115492327a95b6a2fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 23 Feb 2016 19:53:46 +0800 Subject: [PATCH 368/890] add conn client --- .../main/java/com/shinemo/mpush/push/ConnClient.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java b/mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java new file mode 100644 index 00000000..971051fe --- /dev/null +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java @@ -0,0 +1,12 @@ +package com.shinemo.mpush.push; + +import com.shinemo.mpush.common.AbstractClient; +import com.shinemo.mpush.push.zk.listener.impl.ConnectionServerPathListener; + +public class ConnClient extends AbstractClient{ + + public ConnClient() { + registerListener(new ConnectionServerPathListener()); + } + +} From cea655b59a88bb8e5abfca832ef597b2e467bf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 09:19:23 +0800 Subject: [PATCH 369/890] log bug fix --- .../mpush/tools/redis/pubsub/Subscriber.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java index 9b6f5556..b4235af3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java @@ -17,38 +17,38 @@ private Subscriber(){} @Override public void onMessage(String channel, String message) { - LoggerManage.log(LogType.REDIS, "onMessage:%s,%s", channel,message); + LoggerManage.log(LogType.REDIS, "onMessage:{},{}", channel,message); dispatcher.onMessage(channel, message); super.onMessage(channel, message); } @Override public void onPMessage(String pattern, String channel, String message) { - LoggerManage.log(LogType.REDIS, "onPMessage:%s,%s,%s",pattern,channel,message); + LoggerManage.log(LogType.REDIS, "onPMessage:{},{},{}",pattern,channel,message); super.onPMessage(pattern, channel, message); } @Override public void onPSubscribe(String pattern, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onPSubscribe:%s,%s",pattern,subscribedChannels); + LoggerManage.log(LogType.REDIS, "onPSubscribe:{},{}",pattern,subscribedChannels); super.onPSubscribe(pattern, subscribedChannels); } @Override public void onPUnsubscribe(String pattern, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onPUnsubscribe:%s,%s",pattern,subscribedChannels); + LoggerManage.log(LogType.REDIS, "onPUnsubscribe:{},{}",pattern,subscribedChannels); super.onPUnsubscribe(pattern, subscribedChannels); } @Override public void onSubscribe(String channel, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onSubscribe:%s,%s",channel,subscribedChannels); + LoggerManage.log(LogType.REDIS, "onSubscribe:{},{}",channel,subscribedChannels); super.onSubscribe(channel, subscribedChannels); } @Override public void onUnsubscribe(String channel, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onUnsubscribe:%s,%s",channel,subscribedChannels); + LoggerManage.log(LogType.REDIS, "onUnsubscribe:{},{}",channel,subscribedChannels); super.onUnsubscribe(channel, subscribedChannels); } @@ -61,7 +61,7 @@ public void unsubscribe() { @Override public void unsubscribe(String... channels) { - LoggerManage.log(LogType.REDIS, "unsubscribe:%s",Jsons.toJson(channels)); + LoggerManage.log(LogType.REDIS, "unsubscribe:{}",Jsons.toJson(channels)); super.unsubscribe(channels); } From 49efff971649fb18cf818765511b65342f41fa0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 10:52:13 +0800 Subject: [PATCH 370/890] bug fix --- .../main/java/com/shinemo/mpush/tools/redis/RedisUtil.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index b03b8677..e08871b8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -512,7 +512,12 @@ public static void lRem(List nodeList, String key,String value) { public static void publish(RedisNode node, String channel, T message) { Jedis jedis = null; - String value = Jsons.toJson(message); + String value = null; + if(message instanceof String){ + value = (String) message; + }else{ + value = Jsons.toJson(message); + } try { jedis = getClient(node); jedis.publish(channel, value); From 31294093e9bb386f91da72c638f468c173f470c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 13:04:21 +0800 Subject: [PATCH 371/890] add time out log --- .../src/main/java/com/shinemo/mpush/push/PushRequest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index 1ad092e4..f8d3f0e8 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -111,7 +111,9 @@ public void run() { } public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; + long delt = System.currentTimeMillis() - timeout_; + LOGGER.info("delt:{},content:{}",delt,content); + return delt > 0 ? true :false; } public void timeout() { From 2b847623a4874673e9349097d77aa7da35152401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 13:28:08 +0800 Subject: [PATCH 372/890] add log --- .../src/main/java/com/shinemo/mpush/push/PushRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index f8d3f0e8..c0d42af6 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -112,7 +112,7 @@ public void run() { public boolean isTimeout() { long delt = System.currentTimeMillis() - timeout_; - LOGGER.info("delt:{},content:{}",delt,content); + LOGGER.info("delt:{},{},content:{}",delt,timeout_,content); return delt > 0 ? true :false; } @@ -169,7 +169,7 @@ private void sendToConnServer() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { sendTime = System.currentTimeMillis(); - LOGGER.warn("pushMessage packet:" + pushMessage.getPacket()); + LOGGER.info("push Message send success:{},{}",sendTime,content); } else { PushRequest.this.onFailure(userId); } From da4b502975bb4204aff89378cb3b8e4c1791366f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 14:34:33 +0800 Subject: [PATCH 373/890] add log --- .../com/shinemo/mpush/push/PushRequest.java | 19 +++++++++++++++++++ .../shinemo/mpush/push/PushRequestBus.java | 1 + 2 files changed, 20 insertions(+) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index c0d42af6..bf00b194 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -1,5 +1,6 @@ package com.shinemo.mpush.push; +import com.google.common.collect.Maps; import com.shinemo.mpush.api.PushSender; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.router.ClientLocation; @@ -8,6 +9,7 @@ import com.shinemo.mpush.common.router.ConnectionRouterManager; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.push.manage.impl.GatewayServerManage; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.spi.ServiceContainer; import io.netty.channel.ChannelFuture; @@ -16,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; /** @@ -34,6 +37,7 @@ public class PushRequest implements PushSender.Callback, Runnable { private int sessionId; private long sendTime; private AtomicInteger status = new AtomicInteger(0); + private Map times = Maps.newHashMap(); public PushRequest() { } @@ -79,6 +83,8 @@ public void onOffline(String userId) { @Override public void onTimeout(String userId) { + putTime("timeout"); + LOGGER.info("timeout,{},{},{}",sessionId,Jsons.toJson(times),content); submit(4); } @@ -135,6 +141,7 @@ public void offline() { public void send() { this.timeout_ = timeout + System.currentTimeMillis(); + putTime("startsend"); sendToConnServer(); } @@ -163,12 +170,15 @@ private void sendToConnServer() { return; } + times.put("sendtoconnserver", System.currentTimeMillis()); + final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); pushMessage.sendRaw(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { sendTime = System.currentTimeMillis(); + putTime("sendsuccess"); LOGGER.info("push Message send success:{},{}",sendTime,content); } else { PushRequest.this.onFailure(userId); @@ -177,6 +187,7 @@ public void operationComplete(ChannelFuture future) throws Exception { }); sessionId = pushMessage.getSessionId(); + putTime("putrequestbus"); PushRequestBus.INSTANCE.put(sessionId, this); } @@ -184,4 +195,12 @@ public long getSendTime() { return sendTime; } + public Map getTimes() { + return times; + } + + public void putTime(String key) { + this.times.put(key, System.currentTimeMillis()); + } + } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java index b47905e4..0bac05bb 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java @@ -10,6 +10,7 @@ * Created by ohun on 2015/12/30. */ public class PushRequestBus implements Runnable { + public static final PushRequestBus INSTANCE = new PushRequestBus(); private Map requests = new ConcurrentHashMapV8<>(1024); private Executor executor = Executors.newFixedThreadPool(5);//test From bb12281f5945b7eeb25a00b653093571f8ede2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 15:03:37 +0800 Subject: [PATCH 374/890] add time step --- .../java/com/shinemo/mpush/push/PushRequest.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java index bf00b194..12a835c9 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java @@ -68,23 +68,29 @@ public PushRequest setTimeout(long timeout) { @Override public void onSuccess(String userId) { + putTime("success"); + LOGGER.info("success,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); submit(1); } @Override public void onFailure(String userId) { + putTime("failure"); + LOGGER.info("failure,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); submit(2); } @Override public void onOffline(String userId) { + putTime("offline"); + LOGGER.info("offline,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); submit(3); } @Override public void onTimeout(String userId) { putTime("timeout"); - LOGGER.info("timeout,{},{},{}",sessionId,Jsons.toJson(times),content); + LOGGER.info("timeout,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); submit(4); } @@ -118,7 +124,6 @@ public void run() { public boolean isTimeout() { long delt = System.currentTimeMillis() - timeout_; - LOGGER.info("delt:{},{},content:{}",delt,timeout_,content); return delt > 0 ? true :false; } @@ -170,16 +175,14 @@ private void sendToConnServer() { return; } - times.put("sendtoconnserver", System.currentTimeMillis()); + putTime("sendtoconnserver"); final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); pushMessage.sendRaw(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - sendTime = System.currentTimeMillis(); putTime("sendsuccess"); - LOGGER.info("push Message send success:{},{}",sendTime,content); } else { PushRequest.this.onFailure(userId); } From 483e31214f8dd725c7a5ab5b5a74fb5cff4c6764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 16:26:34 +0800 Subject: [PATCH 375/890] bug fix --- .../connection/NettyConnectionManager.java | 8 +++--- .../curator/services/ZkRegisterManager.java | 26 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 2e225e6c..c689d79b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -100,7 +100,7 @@ public void startTimeout() { public void run(Timeout timeout) throws Exception { try { if (!connection.isConnected()) { - LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:%s,%s", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); return; } if (connection.heartbeatTimeout()) { @@ -109,14 +109,14 @@ public void run(Timeout timeout) throws Exception { EventBus.INSTANCE.post(new UserOfflineEvent(connection)); connection.close(); - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(), connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); return; } else { - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } } else { expiredTimes = 0; - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } } catch (Throwable e) { LoggerManage.execption(LogType.DEFAULT, e, "HeartbeatCheckTask error"); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java index fa5e010e..cb18561f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java @@ -52,7 +52,7 @@ public CuratorFramework getClient() { @Override public void init() { zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); - LoggerManage.info(LogType.ZK, "start registry zk, server lists is:%s", zkConfig.getIpLists()); + LoggerManage.info(LogType.ZK, "start registry zk, server lists is:{}", zkConfig.getIpLists()); Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); if (zkConfig.getConnectionTimeout() > 0) { @@ -83,7 +83,7 @@ public List getAclForPath(final String path) { cacheData(); registerConnectionLostListener(); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK,ex,"zk connection error:%s", Jsons.toJson(zkConfig)); + LoggerManage.execption(LogType.ZK,ex,"zk connection error:{}", Jsons.toJson(zkConfig)); } } @@ -95,9 +95,9 @@ private void registerConnectionLostListener() { @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { if (ConnectionState.LOST == newState) { - LoggerManage.log(LogType.ZK, "%s lost connection", MPushUtil.getInetAddress()); + LoggerManage.log(LogType.ZK, "{} lost connection", MPushUtil.getInetAddress()); } else if (ConnectionState.RECONNECTED == newState) { - LoggerManage.log(LogType.ZK, "%s reconnected", MPushUtil.getInetAddress()); + LoggerManage.log(LogType.ZK, "{} reconnected", MPushUtil.getInetAddress()); } } }); @@ -157,7 +157,7 @@ public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Charset.forName("UTF-8")); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "getFromRemote:%s", key); + LoggerManage.execption(LogType.ZK, ex, "getFromRemote:{}", key); return null; } } @@ -181,7 +181,7 @@ public int compare(final String o1, final String o2) { }); return result; } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "getChildrenKeys:%s", key); + LoggerManage.execption(LogType.ZK, ex, "getChildrenKeys:{}", key); return Collections.emptyList(); } } @@ -197,7 +197,7 @@ public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "isExisted:%s", key); + LoggerManage.execption(LogType.ZK, ex, "isExisted:{}", key); return false; } } @@ -217,7 +217,7 @@ public void registerPersist(final String key, final String value) { update(key, value); } } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persist:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "persist:{},{}", key,value); } } @@ -232,7 +232,7 @@ public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "update:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "update:{},{}", key,value); } } @@ -250,7 +250,7 @@ public void registerEphemeral(final String key, final String value) { } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:{},{}", key,value); } } @@ -264,7 +264,7 @@ public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:%s,%s", key,value); + LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{},{}", key,value); } } @@ -278,7 +278,7 @@ public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:%s", key); + LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{}", key); } } @@ -292,7 +292,7 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "remove:%s", key); + LoggerManage.execption(LogType.ZK, ex, "remove:{}", key); } } From fb9fc30b068682e811cafc45677f460e2bb12ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 17:03:41 +0800 Subject: [PATCH 376/890] log --- conf-daily.properties | 5 +- conf-dev.properties | 1 + conf-online.properties | 3 +- conf-pre.properties | 3 +- .../mpush/core/handler/HttpProxyHandler.java | 7 ++- .../main/java/com/shinemo/mpush/cs/Main.java | 3 +- mpush-cs/src/main/resources/config.properties | 1 + mpush-cs/src/main/resources/logback.xml | 20 ++++++++ .../java/com/shinemo/mpush/log/LogType.java | 2 +- .../com/shinemo/mpush/log/LoggerManage.java | 2 + .../monitor/service/MonitorDataCollector.java | 47 ++++++++++--------- .../connection/NettyConnectionManager.java | 1 - .../mpush/tools/config/ConfigCenter.java | 4 ++ 13 files changed, 68 insertions(+), 31 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 1aa1a2c3..94d98648 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -8,6 +8,7 @@ redis_group = 111.1.57.148:6379:ShineMoIpo private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info = true -connection_server_port = 20882 +connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 \ No newline at end of file +dns_mapping=111.1.57.148=127.0.0.1 +skip_dump=true diff --git a/conf-dev.properties b/conf-dev.properties index a15302ea..b704c379 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -11,3 +11,4 @@ force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 dns_mapping=111.1.57.148=127.0.0.1 +skip_dump=true diff --git a/conf-online.properties b/conf-online.properties index d55b1358..44ef31bc 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,4 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=127.0.0.1=127.0.0.1 \ No newline at end of file +dns_mapping=127.0.0.1=127.0.0.1 +skip_dump=false diff --git a/conf-pre.properties b/conf-pre.properties index 0d33492a..fc46bb83 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -10,4 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=127.0.0.1=127.0.0.1 \ No newline at end of file +dns_mapping=127.0.0.1=127.0.0.1 +skip_dump=false diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 4d55716d..f376a907 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -7,14 +7,17 @@ import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.HttpRequestMessage; import com.shinemo.mpush.common.message.HttpResponseMessage; +import com.shinemo.mpush.log.LogType; +import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.client.HttpCallback; import com.shinemo.mpush.netty.client.HttpClient; import com.shinemo.mpush.netty.client.RequestInfo; import com.shinemo.mpush.tools.MPushUtil; + import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; + import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; import java.net.URI; @@ -27,7 +30,7 @@ * Created by ohun on 2016/2/15. */ public class HttpProxyHandler extends BaseMessageHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyHandler.class); + private static final Logger LOGGER = LoggerManage.getLog(LogType.HTTP); private final HttpClient httpClient; private final DnsMapping dnsMapping; diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java index 1ed3b342..cd4f5d99 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java @@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.monitor.service.MonitorDataCollector; +import com.shinemo.mpush.tools.config.ConfigCenter; public class Main { @@ -13,7 +14,7 @@ public static void main(String[] args) { final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); connectionServerMain.start(); //开启监控 - MonitorDataCollector.start(); + MonitorDataCollector.start(ConfigCenter.holder.skipDump()); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-cs/src/main/resources/config.properties index f903dc30..b774c3d9 100644 --- a/mpush-cs/src/main/resources/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -34,3 +34,4 @@ redis_group = ${redis_group} force_write_redis_group_info = ${force_write_redis_group_info} jvm_log_path = /opt/shinemo/mpush/ dns_mapping=${dns_mapping} +skip_dump=${skip_dump} diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-cs/src/main/resources/logback.xml index f2994f26..ce77c389 100644 --- a/mpush-cs/src/main/resources/logback.xml +++ b/mpush-cs/src/main/resources/logback.xml @@ -160,6 +160,26 @@ + + + + ${log.home}/mpush-http.log + true + + ${log.home}/mpush-http.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java index 62337eb8..f77fa348 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java @@ -1,6 +1,6 @@ package com.shinemo.mpush.log; public enum LogType { - CONNECTION,PUSH,HEARTBEAT,REDIS,ZK,DEFAULT + CONNECTION,PUSH,HEARTBEAT,REDIS,ZK,DEFAULT,HTTP } diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java index 85634742..82775f03 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java @@ -13,6 +13,7 @@ public class LoggerManage { private static final Logger heartBeatLog = LoggerFactory.getLogger("heartBeatLog"); private static final Logger redisLog = LoggerFactory.getLogger("redisLog"); private static final Logger zkLog = LoggerFactory.getLogger("zkLog"); + private static final Logger httpLog = LoggerFactory.getLogger("httpLog"); private static final Logger defaultLog = LoggerFactory.getLogger(LoggerManage.class); @@ -24,6 +25,7 @@ public class LoggerManage { map.put(LogType.HEARTBEAT, heartBeatLog); map.put(LogType.REDIS, redisLog); map.put(LogType.ZK, zkLog); + map.put(LogType.HTTP, httpLog); } diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java index 724dd057..0ae94459 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java @@ -48,7 +48,7 @@ public void setPath(String path) { currentPath = path; } - public static void start() { + public static void start(final boolean skipdump) { new Thread(new Runnable() { @Override @@ -57,34 +57,37 @@ public void run() { MonitorData monitorData = MonitorDataCollector.collect(); log.error("monitor data:" + Jsons.toJson(monitorData)); - double load = JVMInfo.instance.load(); - if (load > firstJstack) { - if (!dumpFirstJstack) { - dumpFirstJstack = true; - JVMUtil.dumpJstack(currentPath); + if(!skipdump){ + double load = JVMInfo.instance.load(); + if (load > firstJstack) { + if (!dumpFirstJstack) { + dumpFirstJstack = true; + JVMUtil.dumpJstack(currentPath); + } } - } - if (load > secondJstack) { - if (!dumpSecondJstack) { - dumpSecondJstack = true; - JVMUtil.dumpJmap(currentPath); + if (load > secondJstack) { + if (!dumpSecondJstack) { + dumpSecondJstack = true; + JVMUtil.dumpJmap(currentPath); + } } - } - if (load > thirdJstack) { - if (!dumpThirdJstack) { - dumpThirdJstack = true; - JVMUtil.dumpJmap(currentPath); + if (load > thirdJstack) { + if (!dumpThirdJstack) { + dumpThirdJstack = true; + JVMUtil.dumpJmap(currentPath); + } } - } - if (load > firstJmap) { - if (!dumpJmap) { - dumpJmap = true; - JVMUtil.dumpJmap(currentPath); + if (load > firstJmap) { + if (!dumpJmap) { + dumpJmap = true; + JVMUtil.dumpJmap(currentPath); + } } } + try {//30s Thread.sleep(30000L); @@ -97,7 +100,7 @@ public void run() { } public static void main(String[] args) { - MonitorDataCollector.start(); + MonitorDataCollector.start(true); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index c689d79b..5bfa5082 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -7,7 +7,6 @@ import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; import com.shinemo.mpush.api.event.UserOfflineEvent; -import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index cc00cfda..3e5c0d62 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -115,5 +115,9 @@ public interface ConfigCenter extends Config { @Key("online_and_offline_listener_ip") @DefaultValue("127.0.0.1") String onlineAndOfflineListenerIp(); + + @Key("skip_dump") + @DefaultValue("true") + boolean skipDump(); } From b01c0b118c76a671f1564d7ccffa6c58f994568b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 17:44:15 +0800 Subject: [PATCH 377/890] add bash --- pub-hive.sh | 19 +++++++++++++++++++ test.sh | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 pub-hive.sh create mode 100755 test.sh diff --git a/pub-hive.sh b/pub-hive.sh new file mode 100644 index 00000000..22f49500 --- /dev/null +++ b/pub-hive.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +ENV=dev + +base_dir=`pwd` + +echo "start assembly lib..." + +rm -rf $base_dir/target + +mvn clean install assembly:assembly -P $ENV + +echo "start scp mpush..." + +cd $base_dir/target + +scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive1_host:~/mpush + +scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive2_host:~/mpush diff --git a/test.sh b/test.sh new file mode 100755 index 00000000..6d2aa540 --- /dev/null +++ b/test.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +ENV=dev + +base_dir=`pwd` + +DIR=`dirname $0` + +PROG=`basename $0` + +echo $DIR From 57faea449c2e3b87fcbea5aa3e4b74e495de85d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 17:51:13 +0800 Subject: [PATCH 378/890] chmod --- pub-hive.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 pub-hive.sh diff --git a/pub-hive.sh b/pub-hive.sh old mode 100644 new mode 100755 From aed42d7feeb51a5ebbffc89e05f374e32895d355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 24 Feb 2016 17:57:58 +0800 Subject: [PATCH 379/890] shell --- pub-hive.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pub-hive.sh b/pub-hive.sh index 22f49500..29de189c 100755 --- a/pub-hive.sh +++ b/pub-hive.sh @@ -1,6 +1,6 @@ #!/bin/sh -ENV=dev +ENV=daily base_dir=`pwd` @@ -16,4 +16,6 @@ cd $base_dir/target scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive1_host:~/mpush + + scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive2_host:~/mpush From 878cfe9787cfc6293a74776011a1c47162d06208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 03:08:43 +0000 Subject: [PATCH 380/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/Constants.java | 3 - .../shinemo/mpush/api/protocol/Packet.java | 57 ++++++++++++++----- .../mpush/common/message/BaseMessage.java | 9 ++- .../mpush/core/handler/HeartBeatHandler.java | 7 ++- .../mpush/core/server/GatewayServer.java | 3 +- .../mpush/netty/codec/PacketDecoder.java | 8 +-- .../mpush/netty/codec/PacketEncoder.java | 2 +- .../connection/NettyConnectionManager.java | 47 +++++++-------- 8 files changed, 78 insertions(+), 58 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java index 70cf7b54..a29c52a3 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java @@ -7,10 +7,7 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); - int HEADER_LEN = 13; - byte CRYPTO_FLAG = 0x01; - byte COMPRESS_FLAG = 0x02; String HTTP_HEAD_READ_TIMEOUT = "readTimeout"; } diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index c31f01ec..85c6bac2 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -4,22 +4,24 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import java.io.Serializable; - -import java.util.Arrays; - /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) */ -public final class Packet implements Serializable { - public static final byte HB_PACKET = '\n'; - private static final long serialVersionUID = -2725825199998223372L; +public final class Packet { + public static final int HEADER_LEN = 13; + public static final byte FLAG_CRYPTO = 0x01; + public static final byte FLAG_COMPRESS = 0x02; + + public static final byte HB_PACKET_BYTE = '\n'; + public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; + public static final Packet HB_PACKET = new Packet(Command.HEARTBEAT); + public byte cmd; //命令 public short cc; //校验码 暂时没有用到 public byte flags; //特性,如是否加密,是否压缩等 public int sessionId; // 会话id。客户端生成。 - public byte lrc; // 校验,纵向冗余校验。只校验body + public byte lrc; // 校验,纵向冗余校验。只校验head public byte[] body; public Packet(byte cmd) { @@ -44,10 +46,6 @@ public int getBodyLength() { return body == null ? 0 : body.length; } - public String getStringBody() { - return body == null ? "" : new String(body, Constants.UTF_8); - } - public void setFlag(byte flag) { this.flags |= flag; } @@ -56,6 +54,39 @@ public boolean hasFlag(byte flag) { return (flags & flag) != 0; } + public short calcCheckCode() { + short checkCode = 0; + if (body != null) { + for (int i = 0; i < body.length; i++) { + checkCode += (body[i] & 0x0ff); + } + } + return checkCode; + } + + public byte calcLrc() { + byte[] data = Unpooled.buffer(HEADER_LEN - 1) + .writeInt(getBodyLength()) + .writeByte(cmd) + .writeShort(cc) + .writeByte(flags) + .writeInt(sessionId) + .array(); + byte lrc = 0; + for (int i = 0; i < data.length; i++) { + lrc ^= data[i]; + } + return lrc; + } + + public boolean vaildCheckCode() { + return calcCheckCode() == cc; + } + + public boolean validLrc() { + return (lrc ^ calcLrc()) == 0; + } + @Override public String toString() { return "Packet{" + @@ -69,6 +100,6 @@ public String toString() { } public static ByteBuf getHBPacket() { - return Unpooled.buffer(1).writeByte(HB_PACKET); + return Unpooled.wrappedBuffer(HB_PACKET_BYTES); } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 4c473bc2..eabb6e10 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.common.message; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.Message; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; @@ -30,7 +29,7 @@ protected void decodeBody() { if (packet.body != null && packet.body.length > 0) { //1.解密 byte[] tmp = packet.body; - if (packet.hasFlag(Constants.CRYPTO_FLAG)) { + if (packet.hasFlag(Packet.FLAG_CRYPTO)) { SessionContext info = connection.getSessionContext(); if (info.cipher != null) { tmp = info.cipher.decrypt(tmp); @@ -38,7 +37,7 @@ protected void decodeBody() { } //2.解压 - if (packet.hasFlag(Constants.COMPRESS_FLAG)) { + if (packet.hasFlag(Packet.FLAG_COMPRESS)) { byte[] result = IOUtils.uncompress(tmp); if (result.length > 0) { tmp = result; @@ -60,7 +59,7 @@ protected void encodeBody() { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Constants.COMPRESS_FLAG); + packet.setFlag(Packet.FLAG_COMPRESS); } } @@ -70,7 +69,7 @@ protected void encodeBody() { byte[] result = context.cipher.encrypt(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Constants.CRYPTO_FLAG); + packet.setFlag(Packet.FLAG_CRYPTO); } } packet.body = tmp; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index 00f28010..bb9e8d17 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -12,8 +12,9 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { - connection.send(packet);//ping -> pong - LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:{},{}", connection.getChannel(),connection.getSessionContext().deviceId); + connection.send(packet);//ping -> pong + LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:{}, {}", + connection.getChannel(), connection.getSessionContext().deviceId); } - + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java index 912d406f..8f82446a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java @@ -1,9 +1,8 @@ package com.shinemo.mpush.core.server; -import com.shinemo.mpush.api.Server; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.core.handler.*; +import com.shinemo.mpush.core.handler.GatewayPushHandler; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java index b71e0eff..71782340 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java @@ -1,11 +1,9 @@ package com.shinemo.mpush.netty.codec; -import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.api.exception.DecodeException; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.tools.config.ConfigCenter; - import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -26,7 +24,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { - if (in.readByte() == Packet.HB_PACKET) { + if (in.readByte() == Packet.HB_PACKET_BYTE) { out.add(new Packet(Command.HEARTBEAT)); } else { in.readerIndex(in.readerIndex() - 1); @@ -37,7 +35,7 @@ private void decodeHeartbeat(ByteBuf in, List out) { private void decodeFrames(ByteBuf in, List out) throws Exception { try { - while (in.readableBytes() >= Constants.HEADER_LEN) { + while (in.readableBytes() >= Packet.HEADER_LEN) { //1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 in.markReaderIndex(); out.add(decodeFrame(in)); @@ -51,7 +49,7 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { private Packet decodeFrame(ByteBuf in) throws Exception { int bufferSize = in.readableBytes(); int bodyLength = in.readInt(); - if (bufferSize < (bodyLength + Constants.HEADER_LEN)) { + if (bufferSize < (bodyLength + Packet.HEADER_LEN)) { throw new DecodeException("invalid frame"); } return readPacket(in, bodyLength); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java index cb0e8704..42232eb2 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java @@ -18,7 +18,7 @@ public final class PacketEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { if (packet.cmd == Command.HEARTBEAT.cmd) { - out.writeByte(Packet.HB_PACKET); + out.writeByte(Packet.HB_PACKET_BYTE); } else { out.writeInt(packet.getBodyLength()); out.writeByte(packet.cmd); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index 5bfa5082..c1efb191 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -11,7 +11,6 @@ import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.tools.config.ConfigCenter; - import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; @@ -19,6 +18,7 @@ import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; +import java.nio.ByteBuffer; import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; @@ -76,52 +76,47 @@ public List getConnections() { @Subscribe void onHandshakeOk(HandshakeEvent event) { - HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection); + HeartbeatCheckTask task = new HeartbeatCheckTask(event.connection); task.startTimeout(); } private class HeartbeatCheckTask implements TimerTask { private int expiredTimes = 0; - private final int heartbeat; private final Connection connection; - public HeartbeatCheckTask(int heartbeat, Connection connection) { - this.heartbeat = heartbeat; + public HeartbeatCheckTask(Connection connection) { this.connection = connection; } public void startTimeout() { - timer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + int timeout = connection.getSessionContext().heartbeat; + timer.newTimeout(this, timeout > 0 ? timeout : ConfigCenter.holder.minHeartbeat(), TimeUnit.MILLISECONDS); } @Override public void run(Timeout timeout) throws Exception { - try { - if (!connection.isConnected()) { - LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + if (!connection.isConnected()) { + LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + return; + } + if (connection.heartbeatTimeout()) { + if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { + + EventBus.INSTANCE.post(new UserOfflineEvent(connection)); + + connection.close(); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); return; - } - if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { - - EventBus.INSTANCE.post(new UserOfflineEvent(connection)); - - connection.close(); - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); - return; - } else { - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); - } } else { - expiredTimes = 0; - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } - } catch (Throwable e) { - LoggerManage.execption(LogType.DEFAULT, e, "HeartbeatCheckTask error"); + } else { + expiredTimes = 0; + LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } + startTimeout(); } } - } From b5b71346abf204ef09bce643665a9cfe80a97239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 03:27:45 +0000 Subject: [PATCH 381/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/core/handler/HeartBeatHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java index bb9e8d17..30814281 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java @@ -10,11 +10,11 @@ * Created by ohun on 2015/12/22. */ public final class HeartBeatHandler implements MessageHandler { + @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:{}, {}", connection.getChannel(), connection.getSessionContext().deviceId); } - } From cc1bc3c5f7edecd995136859e3cad3593b7c4e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 05:59:47 +0000 Subject: [PATCH 382/890] =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=B8=A2=E4=BA=BA?= =?UTF-8?q?=E6=97=B6=E4=B8=BB=E5=8A=A8=E5=85=B3=E9=97=AD=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=20=E6=94=B9=E7=94=B1=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E8=87=AA=E5=B7=B1=E5=85=B3=E9=97=AD=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/router/RouterChangeListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index da8fb7b5..fca1d298 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -67,7 +67,7 @@ public void kickLocal(final String userId, final LocalRouter router) { message.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - future.channel().close(); + //future.channel().close(); if (future.isSuccess()) { LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId={}, router={}", userId, router); } else { From 1c37e1c430da6862afb20a4bbf7a097d120f9ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 06:52:31 +0000 Subject: [PATCH 383/890] add http proxy --- conf-daily.properties | 2 +- conf-dev.properties | 2 +- conf-online.properties | 2 +- conf-pre.properties | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 94d98648..8a83431f 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 skip_dump=true diff --git a/conf-dev.properties b/conf-dev.properties index b704c379..f5d45bd7 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 skip_dump=true diff --git a/conf-online.properties b/conf-online.properties index 44ef31bc..d34a70fb 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=127.0.0.1=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 skip_dump=false diff --git a/conf-pre.properties b/conf-pre.properties index fc46bb83..1615010f 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=127.0.0.1=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 skip_dump=false From 29a4e61e1fdd9abe1294af0e1722d52e8de74e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 06:59:19 +0000 Subject: [PATCH 384/890] add http proxy --- conf-daily.properties | 2 +- conf-dev.properties | 2 +- conf-online.properties | 2 +- conf-pre.properties | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 8a83431f..a0bc5d6a 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=true diff --git a/conf-dev.properties b/conf-dev.properties index f5d45bd7..d4407db1 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=true diff --git a/conf-online.properties b/conf-online.properties index d34a70fb..3a65047b 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=false diff --git a/conf-pre.properties b/conf-pre.properties index 1615010f..4f537587 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -10,5 +10,5 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1,120.27.196.68=127.0.0.1,120.27.198.172=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=false From c052d4e1294412b80437dce3fbbbfd10c41e648f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 25 Feb 2016 15:28:10 +0800 Subject: [PATCH 385/890] delete --- .../mpush/api/payload/CustomPushPayload.java | 36 ----- .../api/payload/NotificationPushPayload.java | 132 ------------------ .../shinemo/mpush/api/payload/Payload.java | 7 - .../mpush/api/payload/PayloadFactory.java | 39 ------ 4 files changed, 214 deletions(-) delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java delete mode 100644 mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java deleted file mode 100644 index 4c240094..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/CustomPushPayload.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.shinemo.mpush.api.payload; - -import java.util.HashMap; -import java.util.Map; - - -/** - * msgId、msgType 必填 - * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 - * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 - * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * - */ -public class CustomPushPayload extends HashMap implements Payload { - - private static final long serialVersionUID = 6076731078625705602L; - - public CustomPushPayload(Map extras) { - this.putAll(extras); - } - - public CustomPushPayload() { - } - - public static CustomPushPayload build(Map extras){ - return new CustomPushPayload(extras); - } - -} - diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java deleted file mode 100644 index 52a30cae..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/NotificationPushPayload.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.shinemo.mpush.api.payload; - -import java.util.HashMap; -import java.util.Map; - - - -/** - * msgId、msgType 必填 - * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 - * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 - * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * - */ -public class NotificationPushPayload implements Payload{ - - private static final long serialVersionUID = 4363667286689742483L; - private String title; - private String content; - - private Map extras; - - public String getTitle() { - return title; - } - public NotificationPushPayload setTitle(String title) { - this.title = title; - return this; - } - public String getContent() { - return content; - } - public NotificationPushPayload setContent(String content) { - this.content = content; - return this; - } - - /*****以下非必填**/ - private Long nid; //主要用于聚合通知,非必填 - private Byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯 - private String largeIcon; // 大图标 - private String ticker; //和title一样 - private Integer number; // - public Long getNid() { - return nid; - } - public NotificationPushPayload setNid(Long nid) { - this.nid = nid; - return this; - } - public Byte getFlags() { - return flags; - } - public NotificationPushPayload setFlags(Byte flags) { - this.flags = flags; - return this; - } - public String getLargeIcon() { - return largeIcon; - } - public NotificationPushPayload setLargeIcon(String largeIcon) { - this.largeIcon = largeIcon; - return this; - } - public String getTicker() { - return ticker; - } - public NotificationPushPayload setTicker(String ticker) { - this.ticker = ticker; - return this; - } - public Integer getNumber() { - return number; - } - public NotificationPushPayload setNumber(Integer number) { - this.number = number; - return this; - } - - public NotificationPushPayload setFlag(Flag flag) { - if(this.flags == null){ - this.flags = flag.getValue(); - }else{ - this.flags = (byte) (this.flags.byteValue()|flag.getValue()); - } - return this; - } - - public boolean hasFlag(Flag flag) { - return (this.flags & flag.getValue()) != 0; - } - - public Map getExtras() { - return extras; - } - - public NotificationPushPayload setExtras(Map extras) { - this.extras = new HashMap<>(extras); - return this; - } - - public enum Flag { - VOICE("声音",0x01), - SHOCK("震动",0x02), - LIGHT("闪灯",0x03); - - Flag(String desc, int value) { - this.desc = desc; - this.value = value; - } - - private final String desc; - private final Integer value; - - public String getDesc() { - return desc; - } - public byte getValue() { - return value.byteValue(); - } - - } - - -} - diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java deleted file mode 100644 index e3a99305..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/Payload.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.shinemo.mpush.api.payload; - -import java.io.Serializable; - -public interface Payload extends Serializable{ - -} diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java b/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java deleted file mode 100644 index 9a97728f..00000000 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/payload/PayloadFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.shinemo.mpush.api.payload; - - -/** - * msgId、msgType 必填 - * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 - * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 - * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * - */ -@SuppressWarnings("unchecked") -public class PayloadFactory{ - - public static T create(MessageType messageType){ - - Payload payload = null; - if(messageType.equals(MessageType.NOTIFICATION)){ - payload = new NotificationPushPayload(); - }else if(messageType.equals(MessageType.CUSTOM)){ - payload = new CustomPushPayload(); - } - if(payload!=null){ - return (T)payload; - } - return null; - } - - public enum MessageType{ - NOTIFICATION,CUSTOM; - } - -} - From 0a3de97793cdecce8ce532d0ad88fc3b1d44f374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 07:54:01 +0000 Subject: [PATCH 386/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A3=E7=BB=91?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/UnbindUserHandler.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java index 7801925a..21f7fb2d 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java @@ -11,6 +11,8 @@ import com.shinemo.mpush.common.message.BindUserMessage; import com.shinemo.mpush.common.message.ErrorMessage; import com.shinemo.mpush.common.message.OkMessage; +import com.shinemo.mpush.common.router.RemoteRouter; +import com.shinemo.mpush.common.router.RemoteRouterManager; import com.shinemo.mpush.core.router.RouterCenter; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; @@ -32,19 +34,28 @@ public void handle(BindUserMessage message) { LoggerManage.info(LogType.CONNECTION, "unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } - //1.绑定用户时先看下是否握手成功 + //1.解绑用户时先看下是否握手成功 SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { - //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 - boolean success = RouterCenter.INSTANCE.unRegister(message.userId); + //2.先删除远程路由, 必须是同一个设备才允许解绑 + RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); + RemoteRouter remoteRouter = remoteRouterManager.lookup(message.userId); + if (remoteRouter != null) { + String deviceId = remoteRouter.getRouteValue().getDeviceId(); + if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 + remoteRouterManager.unRegister(message.userId); + } + } + //3.删除本地路由信息 + boolean success = RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); if (success) { - - EventBus.INSTANCE.post(new UserOfflineEvent( message.getConnection(),message.userId)); - + + EventBus.INSTANCE.post(new UserOfflineEvent(message.getConnection(), message.userId)); + OkMessage.from(message).setData("unbind success").send(); LoggerManage.info(LogType.CONNECTION, "unbind user success, userId={}, session={}", message.userId, context); } else { - ErrorMessage.from(message).setReason("unbind failed").close(); + ErrorMessage.from(message).setReason("unbind failed").send(); LoggerManage.info(LogType.CONNECTION, "unbind user failure, register router failure, userId={}, session={}", message.userId, context); } } else { From d185d6714e7a76c211ff0d4cc5e21218cecdb328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 08:24:56 +0000 Subject: [PATCH 387/890] =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/event/UserOfflineEvent.java | 37 ++++++++---------- .../mpush/core/router/LocalRouterManager.java | 20 +++++----- .../mpush/core/router/UserManager.java | 38 +++++++++---------- .../core/server/ServerChannelHandler.java | 3 +- .../netty/connection/NettyConnection.java | 1 - .../connection/NettyConnectionManager.java | 3 -- 6 files changed, 47 insertions(+), 55 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java index 8c029282..a024a907 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java @@ -3,28 +3,23 @@ import com.shinemo.mpush.api.connection.Connection; /** - * 链接超时,用户解绑的时候,用户主动关闭链接,才会触发该事件 - * + * 链接超时,用户解绑的时候,用户主动关闭链接,才会触发该事件 */ public final class UserOfflineEvent implements Event { - - private final Connection connection; + + private final Connection connection; private final String userId; - - public UserOfflineEvent(Connection connection, String userId) { - this.connection = connection; - this.userId = userId; - } - - public UserOfflineEvent(Connection connection) { - this.connection = connection; - this.userId = null; - } - - public Connection getConnection() { - return connection; - } - public String getUserId() { - return userId; - } + + public UserOfflineEvent(Connection connection, String userId) { + this.connection = connection; + this.userId = userId; + } + + public Connection getConnection() { + return connection; + } + + public String getUserId() { + return userId; + } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 3cfd28a7..24d128fd 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -2,8 +2,10 @@ import com.google.common.eventbus.Subscribe; import com.shinemo.mpush.api.event.ConnectionCloseEvent; +import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.router.RouterManager; import com.shinemo.mpush.common.AbstractEventContainer; +import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.router.RemoteRouter; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; @@ -33,7 +35,7 @@ public final class LocalRouterManager extends AbstractEventContainer implements public LocalRouter register(String userId, LocalRouter router) { LOGGER.info("register local router success userId={}, router={}", userId, router); connIdUserIds.put(router.getRouteValue().getId(), userId); - + //add online userId return routers.put(userId, router); } @@ -54,9 +56,9 @@ public LocalRouter lookup(String userId) { LOGGER.info("lookup local router userId={}, router={}", userId, router); return router; } - - public String getUserIdByConnId(String connId){ - return connIdUserIds.get(connId); + + public String getUserIdByConnId(String connId) { + return connIdUserIds.get(connId); } /** @@ -71,18 +73,18 @@ void onConnectionCloseEvent(ConnectionCloseEvent event) { //1.清除反向关系 String userId = connIdUserIds.remove(id); if (userId == null) return; - + EventBus.INSTANCE.post(new UserOfflineEvent(event.connection, userId)); LocalRouter router = routers.get(userId); if (router == null) return; - + //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 if (id.equals(router.getRouteValue().getId())) { //3.删除路由 routers.remove(userId); LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); - - }else{ //如果不相等,则log一下 - LOGGER.info("clean disconnected local route, not clean:userId={}, route={}",userId,router); + + } else { //如果不相等,则log一下 + LOGGER.info("clean disconnected local route, not clean:userId={}, route={}", userId, router); } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java index 220b3184..a68ead61 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java @@ -13,35 +13,33 @@ /** * Created by ohun on 2015/12/23. */ -public final class UserManager extends com.shinemo.mpush.common.manage.user.UserManager{ +public final class UserManager extends com.shinemo.mpush.common.manage.user.UserManager { public static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); public static final String ONLINE_CHANNEL = "/mpush/online/"; - + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; - + public UserManager() { - EventBus.INSTANCE.register(this); - } - + EventBus.INSTANCE.register(this); + } + @Subscribe void handlerUserOnlineEvent(UserOnlineEvent event) { - userOnline(event.getUserId()); - RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); + userOnline(event.getUserId()); + RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); } - + @Subscribe void handlerUserOfflineEvent(UserOfflineEvent event) { - if(event.getUserId()==null){//链接超时 - String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); - if(StringUtils.isNotBlank(userId)){ - userOffline(userId); - RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); - } - }else{ //解绑用户 - userOffline(event.getUserId()); - } - + if (event.getUserId() == null) {//链接超时 + String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); + if (StringUtils.isNotBlank(userId)) { + userOffline(userId); + RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); + } + } else { //解绑用户 + userOffline(event.getUserId()); + } } - } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 789e84bb..48e67e61 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -2,6 +2,7 @@ import com.shinemo.mpush.api.connection.ConnectionManager; +import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.event.UserOfflineEvent; @@ -66,7 +67,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception { LoggerManage.log(security, LogLevel.INFO, "client disconnect channel={}", ctx.channel()); Connection connection = connectionManager.get(ctx.channel()); - EventBus.INSTANCE.post(new UserOfflineEvent(connection)); + EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); connectionManager.remove(ctx.channel()); } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 2ffe7dc0..56948f52 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -76,7 +76,6 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { public ChannelFuture close() { if (status == STATUS_DISCONNECTED) return null; this.status = STATUS_DISCONNECTED; - EventBus.INSTANCE.post(new ConnectionCloseEvent(this)); return this.channel.close(); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index c1efb191..f6dbf581 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -102,9 +102,6 @@ public void run(Timeout timeout) throws Exception { } if (connection.heartbeatTimeout()) { if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { - - EventBus.INSTANCE.post(new UserOfflineEvent(connection)); - connection.close(); LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); return; From fde8548ae76b9c8779558f61d42ba3634dd56bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 25 Feb 2016 16:31:52 +0800 Subject: [PATCH 388/890] add --- .../src/main/java/com/shinemo/mpush/api/RedisKey.java | 7 +++++++ .../src/main/java/com/shinemo/mpush/tools/MPushUtil.java | 5 ++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java index ed04fb48..38c6b9b8 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -12,6 +12,8 @@ public class RedisKey { private static final String USER_OFFLINE_KEY = "mp_u_ofl"; + private static final String CONN_NUM_ = "mp_cn_"; + public static final String getUserKey(String userId){ return USER_PREFIX+userId; } @@ -32,5 +34,10 @@ public static final String getUserOnlineKey(){ public static final String getUserOfflineKey(){ return USER_OFFLINE_KEY; } + + public static final String getConnNum(String extranetAddress){ + return CONN_NUM_+extranetAddress; + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index f4c7ed84..3b092315 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -61,7 +61,7 @@ public static String getInetAddress() { } } - public static String getExtranetAddress() throws Exception{ + public static String getExtranetAddress() { try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); InetAddress address = null; @@ -76,11 +76,10 @@ public static String getExtranetAddress() throws Exception{ } } LOGGER.warn("getExtranetAddress is null"); - return null; } catch (Throwable e) { LOGGER.error("getExtranetAddress exception", e); - throw new Exception(e); } + return getInetAddress(); } public static String headerToString(Map headers) { From 5ec1f80fdbe795b1f4f246af8a5502e49729ab13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 25 Feb 2016 17:32:43 +0800 Subject: [PATCH 389/890] add num --- .../mpush/core/router/UserManager.java | 34 +++++++++++++------ .../shinemo/mpush/tools/redis/RedisUtil.java | 19 +++++++++++ .../mpush/tools/redis/manage/RedisManage.java | 5 +++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java index a68ead61..3b18c314 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java @@ -1,12 +1,12 @@ package com.shinemo.mpush.core.router; import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.manage.RedisManage; - -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,6 +19,10 @@ public final class UserManager extends com.shinemo.mpush.common.manage.user.User public static final String ONLINE_CHANNEL = "/mpush/online/"; public static final String OFFLINE_CHANNEL = "/mpush/offline/"; + + private static final String EXTRANET_ADDRESS = MPushUtil.getExtranetAddress(); + + private static final String ONLINE_KEY = RedisKey.getConnNum(EXTRANET_ADDRESS); public UserManager() { EventBus.INSTANCE.register(this); @@ -28,18 +32,26 @@ public UserManager() { void handlerUserOnlineEvent(UserOnlineEvent event) { userOnline(event.getUserId()); RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); + RedisManage.incrBy(ONLINE_KEY, 1); } @Subscribe void handlerUserOfflineEvent(UserOfflineEvent event) { - if (event.getUserId() == null) {//链接超时 - String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); - if (StringUtils.isNotBlank(userId)) { - userOffline(userId); - RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); - } - } else { //解绑用户 - userOffline(event.getUserId()); - } +// if (event.getUserId() == null) {//链接超时 +// String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); +// if (StringUtils.isNotBlank(userId)) { +// userOffline(userId); +// RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); +// } +// } else { //解绑用户 +// userOffline(event.getUserId()); +// RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); +// } + if(event.getUserId()==null){ + return; + } + userOffline(event.getUserId()); + RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); + RedisManage.incrBy(ONLINE_KEY, -1); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java index e08871b8..f3836a5f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java @@ -57,6 +57,25 @@ public static long incr(List nodeList,String key,Integer time){ return incrRet; } + + public static long incrBy(List nodeList,String key,long delt){ + long incrRet = -1; + for (RedisNode node : nodeList) { + Jedis jedis = null; + try { + jedis = getClient(node); + long ret = jedis.incrBy(key, delt); + incrRet = ret; + } catch (Exception e) { + LoggerManage.execption(LogType.REDIS, e, "redis incr exception:{},{},{},{}",key,delt,node); + } finally { + // 返还到连接池 + close(jedis); + } + } + return incrRet; + + } /********************* k v redis start ********************************/ /** diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java index a445bcb9..fbb688ce 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java @@ -27,6 +27,11 @@ public static long incr(String key,Integer time){ return RedisUtil.incr(nodeList, key, time); } + public static long incrBy(String key,long delt){ + List nodeList = redisRegister.hashSet(key); + return RedisUtil.incrBy(nodeList, key, delt); + } + /********************* * k v redis start ********************************/ From 635f44b553ce5b063d14711bc8398845998422f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 25 Feb 2016 21:54:17 +0800 Subject: [PATCH 390/890] =?UTF-8?q?add=20null=20=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/router/LocalRouterManager.java | 3 +++ .../test/java/com/shinemo/mpush/test/gson/GsonTest.java | 1 - .../src/main/java/com/shinemo/mpush/tools/Jsons.java | 8 +++----- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java index 24d128fd..4885ad09 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.core.router; import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.router.RouterManager; @@ -68,6 +69,8 @@ public String getUserIdByConnId(String connId) { */ @Subscribe void onConnectionCloseEvent(ConnectionCloseEvent event) { + Connection connection = event.connection; + if(connection == null) return; String id = event.connection.getId(); //1.清除反向关系 diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java index 21b4d9ac..5280f4ee 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java @@ -5,7 +5,6 @@ import org.junit.Test; import com.google.common.collect.Maps; -import com.google.gson.JsonObject; import com.shinemo.mpush.api.PushContent; import com.shinemo.mpush.api.PushContent.PushType; import com.shinemo.mpush.tools.Jsons; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java index 2dc1da7d..cc3659f4 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java @@ -100,10 +100,8 @@ private static void append(Map.Entry entry, StringBuilder sb) { } public static void main(String[] args) { - String test = "test"; - String ret = Jsons.toJson(test); - String ret2 = Jsons.fromJson(ret, String.class); - System.out.println(ret); - System.out.println(ret2); + String tet = "7"; + Long l = Jsons.fromJson(tet, Long.class); + System.out.println(l); } } From 2b0c79fa5bced9c441db5cdbecb044fbf1ee9f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 25 Feb 2016 14:54:49 +0000 Subject: [PATCH 391/890] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/message/FastConnectOkMessage.java | 17 ----------------- .../common/message/HandshakeOkMessage.java | 16 ---------------- .../mpush/core/handler/FastConnectHandler.java | 2 -- .../mpush/core/handler/HandshakeHandler.java | 2 -- 4 files changed, 37 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java index ea6b48bc..7c94b26e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java @@ -8,8 +8,6 @@ * Created by ohun on 2015/12/28. */ public final class FastConnectOkMessage extends ByteBufMessage { - public String serverHost; - public long serverTime; public int heartbeat; public FastConnectOkMessage(Packet message, Connection connection) { @@ -22,29 +20,14 @@ public static FastConnectOkMessage from(BaseMessage src) { @Override public void decode(ByteBuf body) { - serverHost = decodeString(body); - serverTime = decodeLong(body); heartbeat = decodeInt(body); } @Override public void encode(ByteBuf body) { - encodeString(body, serverHost); - encodeLong(body, serverTime); encodeInt(body, heartbeat); } - - public FastConnectOkMessage setServerHost(String serverHost) { - this.serverHost = serverHost; - return this; - } - - public FastConnectOkMessage setServerTime(long serverTime) { - this.serverTime = serverTime; - return this; - } - public FastConnectOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java index d09fa641..a451ee17 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java @@ -9,8 +9,6 @@ */ public final class HandshakeOkMessage extends ByteBufMessage { public byte[] serverKey; - public String serverHost; - public long serverTime; public int heartbeat; public String sessionId; public long expireTime; @@ -22,8 +20,6 @@ public HandshakeOkMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { serverKey = decodeBytes(body); - serverHost = decodeString(body); - serverTime = decodeLong(body); heartbeat = decodeInt(body); sessionId = decodeString(body); expireTime = decodeLong(body); @@ -32,8 +28,6 @@ public void decode(ByteBuf body) { @Override public void encode(ByteBuf body) { encodeBytes(body, serverKey); - encodeString(body, serverHost); - encodeLong(body, serverTime); encodeInt(body, heartbeat); encodeString(body, sessionId); encodeLong(body, expireTime); @@ -48,16 +42,6 @@ public HandshakeOkMessage setServerKey(byte[] serverKey) { return this; } - public HandshakeOkMessage setServerHost(String serverHost) { - this.serverHost = serverHost; - return this; - } - - public HandshakeOkMessage setServerTime(long serverTime) { - this.serverTime = serverTime; - return this; - } - public HandshakeOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java index 7259f60a..fe420dc8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java @@ -48,8 +48,6 @@ public void handle(FastConnectMessage message) { FastConnectOkMessage .from(message) - .setServerHost(MPushUtil.getLocalIp()) - .setServerTime(System.currentTimeMillis()) .setHeartbeat(heartbeat) .send(); LoggerManage.info(LogType.CONNECTION, "fast connect success, session={}", session.context); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java index 63113678..7603aedc 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java @@ -69,8 +69,6 @@ public void handle(HandshakeMessage message) { HandshakeOkMessage .from(message) .setServerKey(serverKey) - .setServerHost(MPushUtil.getLocalIp()) - .setServerTime(System.currentTimeMillis()) .setHeartbeat(heartbeat) .setSessionId(session.sessionId) .setExpireTime(session.expireTime) From f591031f47b561acdd75aa8335ad83d9fe3e0006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 26 Feb 2016 05:59:01 +0000 Subject: [PATCH 392/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E5=AD=97=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/api/protocol/Packet.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 85c6bac2..6481d0c7 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -13,9 +13,8 @@ public final class Packet { public static final byte FLAG_CRYPTO = 0x01; public static final byte FLAG_COMPRESS = 0x02; - public static final byte HB_PACKET_BYTE = '\n'; + public static final byte HB_PACKET_BYTE = 0; public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; - public static final Packet HB_PACKET = new Packet(Command.HEARTBEAT); public byte cmd; //命令 public short cc; //校验码 暂时没有用到 From 3604d42fdd40f5dc37aa385e6b7f947a485b5eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 26 Feb 2016 06:09:08 +0000 Subject: [PATCH 393/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E5=AD=97=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/api/protocol/Packet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 6481d0c7..3188b960 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -13,7 +13,7 @@ public final class Packet { public static final byte FLAG_CRYPTO = 0x01; public static final byte FLAG_COMPRESS = 0x02; - public static final byte HB_PACKET_BYTE = 0; + public static final byte HB_PACKET_BYTE = -33; public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; public byte cmd; //命令 From a07cd4968d1ad297181ad6853364cdb028e0b0a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 26 Feb 2016 14:25:09 +0800 Subject: [PATCH 394/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/common/AbstractServer.java | 21 ++++++++++--------- .../mpush/netty/server/NettyServer.java | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 034b6163..b2ecd2bb 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -103,6 +103,7 @@ public void run() { @Override public void onSuccess() { log.error("mpush app start "+server.getClass().getSimpleName()+" server success...."); + registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); } @Override @@ -114,15 +115,15 @@ public void onFailure(String message) { } }; ThreadPoolManager.newThread(server.getClass().getSimpleName(), runnable).start(); - //TODO sleep for server start - while(!server.isRunning()){ - log.error("server is not started,wait for {} start",server.getClass().getSimpleName()); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - } - } - log.error("server started {}",server.getClass().getSimpleName()); +// //TODO sleep for server start +// while(!server.isRunning()){ +// log.error("server is not started,wait for {} start",server.getClass().getSimpleName()); +// try { +// Thread.sleep(100); +// } catch (InterruptedException e) { +// } +// } +// log.error("server started {}",server.getClass().getSimpleName()); } @@ -139,7 +140,7 @@ public void start(){ initListenerData(); initServer(); startServer(server); - registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); +// registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); } public void startClient(){ diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index c3e55118..67d1c890 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -138,7 +138,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } } }); - f.await(); +// f.await(); if (f.isSuccess()) { serverState.set(State.Started); /** From 8e36086a7547eeb936e74814d5760a72488ba79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 26 Feb 2016 15:21:39 +0800 Subject: [PATCH 395/890] bug fix --- .../main/java/com/shinemo/mpush/common/AbstractServer.java | 6 +++--- .../java/com/shinemo/mpush/cs/ConnectionServerMain.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index b2ecd2bb..f9ffeac8 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -94,7 +94,7 @@ private void initServer(){ } //step6 启动 netty server - public void startServer(final Server server){ + public void startServer(final Server server,final String path, final String value){ Runnable runnable = new Runnable() { @Override public void run() { @@ -103,7 +103,7 @@ public void run() { @Override public void onSuccess() { log.error("mpush app start "+server.getClass().getSimpleName()+" server success...."); - registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); + registerServerToZk(path,value); } @Override @@ -139,7 +139,7 @@ public void start(){ registerListeners(); initListenerData(); initServer(); - startServer(server); + startServer(server,application.getServerRegisterZkPath(),Jsons.toJson(application)); // registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index e694d188..e4730ba6 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -32,8 +32,8 @@ public ConnectionServerMain(){ @Override public void start() { super.start(); - startServer(gatewayServer); - registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); + startServer(gatewayServer,gatewayServerApplication.getServerRegisterZkPath(),Jsons.toJson(gatewayServerApplication)); +// registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); } From e2037bb0d04d56e1984144dd4e7c882405a082e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 26 Feb 2016 17:08:12 +0800 Subject: [PATCH 396/890] add admin server --- conf-daily.properties | 1 + conf-dev.properties | 2 + conf-online.properties | 1 + conf-pre.properties | 1 + .../shinemo/mpush/common/AbstractServer.java | 9 +- .../mpush/core/handler/AdminHandler.java | 43 +++++++ .../mpush/core/server/AdminServer.java | 120 +++++++++++++++--- .../mpush/cs/ConnectionServerMain.java | 7 + .../main/java/com/shinemo/mpush/cs/Main.java | 1 + mpush-cs/src/main/resources/config.properties | 1 + .../com/shinemo/mpush/tools/MPushUtil.java | 11 ++ .../mpush/tools/config/ConfigCenter.java | 4 + 12 files changed, 183 insertions(+), 18 deletions(-) create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java diff --git a/conf-daily.properties b/conf-daily.properties index a0bc5d6a..f7bc1dfc 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -12,3 +12,4 @@ connection_server_port = 3000 gateway_server_port = 4000 dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=true +admin_port=4001 diff --git a/conf-dev.properties b/conf-dev.properties index d4407db1..01ce6b99 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -12,3 +12,5 @@ connection_server_port = 20882 gateway_server_port = 4000 dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=true + +admin_port=4001 diff --git a/conf-online.properties b/conf-online.properties index 3a65047b..04293547 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -12,3 +12,4 @@ connection_server_port = 3000 gateway_server_port = 4000 dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=false +admin_port=4001 diff --git a/conf-pre.properties b/conf-pre.properties index 4f537587..ce9c28f8 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -12,3 +12,4 @@ connection_server_port = 3000 gateway_server_port = 4000 dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 skip_dump=false +admin_port=4001 diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index f9ffeac8..1ec54be1 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -2,6 +2,7 @@ import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,6 +94,10 @@ private void initServer(){ server = getServer(); } + public void startServer(final Server server){ + this.startServer(server, null, null); + } + //step6 启动 netty server public void startServer(final Server server,final String path, final String value){ Runnable runnable = new Runnable() { @@ -103,7 +108,9 @@ public void run() { @Override public void onSuccess() { log.error("mpush app start "+server.getClass().getSimpleName()+" server success...."); - registerServerToZk(path,value); + if(StringUtils.isNoneBlank(path)&&StringUtils.isNoneBlank(value)){ + registerServerToZk(path,value); + } } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java new file mode 100644 index 00000000..ba681e9a --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java @@ -0,0 +1,43 @@ +package com.shinemo.mpush.core.handler; + +import java.util.Date; + +import com.shinemo.mpush.tools.MPushUtil; + +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + +public final class AdminHandler extends SimpleChannelInboundHandler { + + @Override + protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { + String response; + boolean close = false; + if(request.isEmpty()){ + response = "please type something.\r\n"; + }else if("quit".equals(request.toLowerCase())){ + response = "have a good day! \r\n"; + close = true; + }else { + response = "did you say " + request +" ? \r\n"; + } + ChannelFuture future = ctx.write(response); + if (close) { + future.addListener(ChannelFutureListener.CLOSE); + } + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + ctx.write("welcome to "+MPushUtil.getInetAddress()+ "!\r\n"); + ctx.write("It is " + new Date() + " now.\r\n"); + ctx.flush(); + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + ctx.flush(); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java index bc511ddd..dc6730b8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java @@ -1,30 +1,116 @@ package com.shinemo.mpush.core.server; -import com.shinemo.mpush.api.Server; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.ServerChannel; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.DelimiterBasedFrameDecoder; +import io.netty.handler.codec.Delimiters; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; -/** - * Created by ohun on 2016/1/8. - */ -public final class AdminServer implements Server { +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; - @Override - public void start(Listener listener) { +import com.shinemo.mpush.core.handler.AdminHandler; +import com.shinemo.mpush.netty.server.NettyServer; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; - } +public final class AdminServer extends NettyServer { - @Override - public void stop(Listener listener) { + private static final Logger log = LoggerFactory.getLogger(AdminServer.class); - } + private EventLoopGroup bossGroup; + private EventLoopGroup workerGroup; + + private final int port; + + private AdminHandler adminHandler = new AdminHandler(); + + public AdminServer(int port) { + super(port); + this.port = port; + } - @Override - public boolean isRunning() { - return false; - } + @Override + public void start(Listener listener) { + if (!serverState.compareAndSet(State.Initialized, State.Starting)) { + throw new IllegalStateException("Server already started or have not init"); + } + createNioServer(listener); + } @Override - public void init() { - // TODO Auto-generated method stub + public ChannelHandler getChannelHandler() { + return adminHandler; + } + + private void createNioServer(final Listener listener) { + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolManager.bossExecutor); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); + createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } + + private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { + this.bossGroup = boss; + this.workerGroup = work; + try { + + ServerBootstrap b = new ServerBootstrap(); + b.group(bossGroup, workerGroup) + .channel(clazz) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); + ch.pipeline().addLast("decoder", new StringDecoder()); + ch.pipeline().addLast("encoder", new StringEncoder()); + ch.pipeline().addLast("handler", getChannelHandler()); + } + }); + + + /*** + * 绑定端口并启动去接收进来的连接 + */ + ChannelFuture f = b.bind(port).sync().addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + log.info("server start success on:" + port); + if (listener != null) listener.onSuccess(); + } else { + log.error("server start failure on:" + port); + if (listener != null) listener.onFailure("start server failure"); + } + } + }); +// f.await(); + if (f.isSuccess()) { + serverState.set(State.Started); + /** + * 这里会一直等待,直到socket被关闭 + */ + f.channel().closeFuture().sync(); + } + + } catch (Exception e) { + log.error("server start exception", e); + if (listener != null) listener.onFailure("start server ex=" + e.getMessage()); + throw new RuntimeException("server start exception, port=" + port, e); + } finally { + /*** + * 优雅关闭 + */ + stop(null); + } + } + } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index e4730ba6..b7a30893 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -6,14 +6,18 @@ import com.shinemo.mpush.common.AbstractServer; import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.conn.client.GatewayServerApplication; +import com.shinemo.mpush.core.server.AdminServer; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.config.ConfigCenter; public class ConnectionServerMain extends AbstractServer{ private Server gatewayServer; + private Server adminServer; + private ConnectionServerApplication connectionServerApplication; private GatewayServerApplication gatewayServerApplication; @@ -27,6 +31,7 @@ public ConnectionServerMain(){ gatewayServerApplication = new GatewayServerApplication(); connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); + adminServer = new AdminServer(ConfigCenter.holder.adminPort()); } @Override @@ -34,6 +39,7 @@ public void start() { super.start(); startServer(gatewayServer,gatewayServerApplication.getServerRegisterZkPath(),Jsons.toJson(gatewayServerApplication)); // registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); + startServer(adminServer); } @@ -48,6 +54,7 @@ public Server getServer() { public void stop() { super.stop(); stopServer(gatewayServer); + stopServer(adminServer); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java index cd4f5d99..8ac6268e 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java @@ -13,6 +13,7 @@ public class Main { public static void main(String[] args) { final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); connectionServerMain.start(); + //开启监控 MonitorDataCollector.start(ConfigCenter.holder.skipDump()); diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-cs/src/main/resources/config.properties index b774c3d9..2729c8c3 100644 --- a/mpush-cs/src/main/resources/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -35,3 +35,4 @@ force_write_redis_group_info = ${force_write_redis_group_info} jvm_log_path = /opt/shinemo/mpush/ dns_mapping=${dns_mapping} skip_dump=${skip_dump} +admin_port=${admin_port} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 3b092315..acf5fb6c 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -10,6 +10,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; /** * Created by ohun on 2015/12/25. @@ -18,7 +19,17 @@ public final class MPushUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); private static String LOCAL_IP; + + private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$"); + public static boolean isLocalHost(String host) { + return host == null + || host.length() == 0 + || host.equalsIgnoreCase("localhost") + || host.equals("0.0.0.0") + || (LOCAL_IP_PATTERN.matcher(host).matches()); + } + public static String getLocalIp() { if (LOCAL_IP == null) { LOCAL_IP = getInetAddress(); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 3e5c0d62..38452bbc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -119,5 +119,9 @@ public interface ConfigCenter extends Config { @Key("skip_dump") @DefaultValue("true") boolean skipDump(); + + @Key("admin_port") + @DefaultValue("4001") + int adminPort(); } From f34fa3be07496c0e95ada4f27895afc28040ddc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 26 Feb 2016 18:10:11 +0800 Subject: [PATCH 397/890] add admin handler --- .../mpush/core/handler/AdminHandler.java | 87 ++++++++++++++++--- 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java index ba681e9a..4df03885 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java @@ -2,31 +2,28 @@ import java.util.Date; +import org.apache.commons.lang3.StringUtils; + +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.redis.manage.RedisManage; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; +@ChannelHandler.Sharable public final class AdminHandler extends SimpleChannelInboundHandler { @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { - String response; - boolean close = false; - if(request.isEmpty()){ - response = "please type something.\r\n"; - }else if("quit".equals(request.toLowerCase())){ - response = "have a good day! \r\n"; - close = true; - }else { - response = "did you say " + request +" ? \r\n"; + Command command = Command.getCommand(request); + ChannelFuture future = ctx.write(command.handler(request)); + if(command.equals(Command.QUIT)){ + future.addListener(ChannelFutureListener.CLOSE); } - ChannelFuture future = ctx.write(response); - if (close) { - future.addListener(ChannelFutureListener.CLOSE); - } } @Override @@ -40,4 +37,68 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } + + public static enum Command{ + HELP("help"){ + @Override + public String handler(String request) { + StringBuilder buf = new StringBuilder(); + buf.append("Command:\r\n"); + buf.append("help:display all command.\r\n"); + buf.append("quit:exit telnet.\r\n"); + buf.append("r1:statistics conn num.\r\n"); + buf.append("r2:remove connection server zk info.\r\n"); + buf.append("r3:stop connection server.\r\n"); + return buf.toString(); + } + }, + QUIT("quit"){ + @Override + public String handler(String request) { + return "have a good day! \r\n"; + } + }, + R1("r1"){ + @Override + public String handler(String request) { + Long value = RedisManage.get(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), Long.class); + if(value == null){ + value = 0L; + } + return value.toString()+".\r\n"; + } + }, + R2("r2"){ + @Override + public String handler(String request) { + return "not support now.\r\n"; + } + }, + R3("r3"){ + @Override + public String handler(String request) { + return "not support now.\r\n"; + } + }; + private final String cmd; + public abstract String handler(String request); + private Command(String cmd) { + this.cmd = cmd; + } + public String getCmd() { + return cmd; + } + + public static Command getCommand(String request){ + if(StringUtils.isNoneEmpty(request)){ + for(Command command: Command.values()){ + if(command.getCmd().equals(request)){ + return command; + } + } + } + return HELP; + } + } + } From 967a0e988743fb6e6cf43aa4447e09a3b9a6d562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 26 Feb 2016 06:11:15 +0000 Subject: [PATCH 398/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E5=AD=97=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/api/protocol/Packet.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java index 3188b960..31068646 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java @@ -1,6 +1,5 @@ package com.shinemo.mpush.api.protocol; -import com.shinemo.mpush.api.Constants; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; From 6c1fc3102a4436aaca03a13d95e26b267102eea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 29 Feb 2016 01:32:27 +0000 Subject: [PATCH 399/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E5=AD=97=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/src/main/java/com/shinemo/mpush/api/Server.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java index 70d731fe..9f976416 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java @@ -8,8 +8,8 @@ public interface Server { void start(Listener listener); void stop(Listener listener); - - public void init(); + + void init(); boolean isRunning(); From cb971245454a144b99adae5195e7cc0eb1e818dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 29 Feb 2016 02:13:07 +0000 Subject: [PATCH 400/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A3=E7=BB=91?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=AE=BE=E5=A4=87=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/UnbindUserHandler.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java index 21f7fb2d..e9ef79d9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java @@ -13,6 +13,8 @@ import com.shinemo.mpush.common.message.OkMessage; import com.shinemo.mpush.common.router.RemoteRouter; import com.shinemo.mpush.common.router.RemoteRouterManager; +import com.shinemo.mpush.core.router.LocalRouter; +import com.shinemo.mpush.core.router.LocalRouterManager; import com.shinemo.mpush.core.router.RouterCenter; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; @@ -27,6 +29,12 @@ public BindUserMessage decode(Packet packet, Connection connection) { return new BindUserMessage(packet, connection); } + /** + * 目前是以用户维度来存储路由信息的,所以在删除路由信息时要判断下是否是同一个设备 + * 后续可以修改为按设备来存储路由信息。 + * + * @param message + */ @Override public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { @@ -34,24 +42,34 @@ public void handle(BindUserMessage message) { LoggerManage.info(LogType.CONNECTION, "unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } + //1.解绑用户时先看下是否握手成功 SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { //2.先删除远程路由, 必须是同一个设备才允许解绑 + boolean unRegisterSuccess = true; RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); RemoteRouter remoteRouter = remoteRouterManager.lookup(message.userId); if (remoteRouter != null) { String deviceId = remoteRouter.getRouteValue().getDeviceId(); if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - remoteRouterManager.unRegister(message.userId); + unRegisterSuccess = remoteRouterManager.unRegister(message.userId); } } + //3.删除本地路由信息 - boolean success = RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); - if (success) { + LocalRouterManager localRouterManager = RouterCenter.INSTANCE.getLocalRouterManager(); + LocalRouter localRouter = localRouterManager.lookup(message.userId); + if (localRouter != null) { + String deviceId = localRouter.getRouteValue().getSessionContext().deviceId; + if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 + unRegisterSuccess = localRouterManager.unRegister(message.userId) && unRegisterSuccess; + } + } + //4.路由删除成功,广播用户下线事件 + if (unRegisterSuccess) { EventBus.INSTANCE.post(new UserOfflineEvent(message.getConnection(), message.userId)); - OkMessage.from(message).setData("unbind success").send(); LoggerManage.info(LogType.CONNECTION, "unbind user success, userId={}, session={}", message.userId, context); } else { From 1f9a1b94d7455a35fdb42802ae16d70a5a338768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 10:18:28 +0800 Subject: [PATCH 401/890] add admin remove --- .../mpush/core/handler/AdminHandler.java | 67 ++++++++++++++----- .../core/server/ServerChannelHandler.java | 1 - mpush-test/pom.xml | 6 ++ pom.xml | 6 ++ 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java index 4df03885..a2db6c1f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java @@ -1,12 +1,20 @@ package com.shinemo.mpush.core.handler; import java.util.Date; +import java.util.List; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.RedisKey; +import com.shinemo.mpush.conn.client.ConnectionServerApplication; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.shinemo.mpush.tools.zk.ZKPath; +import com.shinemo.mpush.tools.zk.ZkRegister; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -16,20 +24,29 @@ @ChannelHandler.Sharable public final class AdminHandler extends SimpleChannelInboundHandler { + + private static final Logger log = LoggerFactory.getLogger(AdminHandler.class); + private static final String DOUBLE_END = "\r\n\r\n"; + + private static final String ONE_END = "\r\n"; + + protected static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); + @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { Command command = Command.getCommand(request); - ChannelFuture future = ctx.write(command.handler(request)); + ChannelFuture future = ctx.write(command.handler(request)+DOUBLE_END); if(command.equals(Command.QUIT)){ future.addListener(ChannelFutureListener.CLOSE); } + } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - ctx.write("welcome to "+MPushUtil.getInetAddress()+ "!\r\n"); - ctx.write("It is " + new Date() + " now.\r\n"); + ctx.write("welcome to "+MPushUtil.getInetAddress()+ "!"+ONE_END); + ctx.write("It is " + new Date() + " now."+DOUBLE_END); ctx.flush(); } @@ -43,41 +60,59 @@ public static enum Command{ @Override public String handler(String request) { StringBuilder buf = new StringBuilder(); - buf.append("Command:\r\n"); - buf.append("help:display all command.\r\n"); - buf.append("quit:exit telnet.\r\n"); - buf.append("r1:statistics conn num.\r\n"); - buf.append("r2:remove connection server zk info.\r\n"); - buf.append("r3:stop connection server.\r\n"); + buf.append("Command:"+ONE_END); + buf.append("help:display all command."+ONE_END); + buf.append("quit:exit telnet."+ONE_END); + buf.append("scn:statistics conn num."+ONE_END); + buf.append("rcs:remove connection server zk info."+ONE_END); + buf.append("scs:stop connection server."); return buf.toString(); } }, QUIT("quit"){ @Override public String handler(String request) { - return "have a good day! \r\n"; + return "have a good day!"; } }, - R1("r1"){ + SCN("scn"){ @Override public String handler(String request) { Long value = RedisManage.get(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), Long.class); if(value == null){ value = 0L; } - return value.toString()+".\r\n"; + return value.toString()+"."; } }, - R2("r2"){ + RCS("rcs"){ @Override public String handler(String request) { - return "not support now.\r\n"; + + List rawData = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + boolean removeSuccess = false; + for (String raw : rawData) { + String data = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + ConnectionServerApplication application = Jsons.fromJson(data, ConnectionServerApplication.class); + if(application.getIp().equals(MPushUtil.getInetAddress())){ + zkRegister.remove(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + log.info("delete connection server success:{}",data); + removeSuccess = true; + }else{ + log.info("delete connection server failed: required ip:{}, but:{}",application.getIp(),MPushUtil.getInetAddress()); + } + } + if(removeSuccess){ + return "remove success."; + }else{ + return "remove false."; + } } }, - R3("r3"){ + SCS("scs"){ @Override public String handler(String request) { - return "not support now.\r\n"; + return "not support now."; } }; private final String cmd; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 48e67e61..842b6069 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -5,7 +5,6 @@ import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.log.LogLevel; diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 4dc68087..8e089fca 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -42,6 +42,12 @@ com.shinemo.mpush mpush-client + + + args4j + args4j + + diff --git a/pom.xml b/pom.xml index 6dbccca9..752ca3ef 100644 --- a/pom.xml +++ b/pom.xml @@ -219,6 +219,12 @@ 2.7.8 + + args4j + args4j + 2.33 + + From 8f70a80ee906f31fa60cefc66dca3812337b5f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 10:33:54 +0800 Subject: [PATCH 402/890] =?UTF-8?q?=E9=87=8D=E7=BD=AE=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/core/server/ConnectionServer.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 77b31092..f953c386 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.core.server; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; @@ -9,6 +10,9 @@ import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.redis.manage.RedisManage; + import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -30,6 +34,8 @@ public ConnectionServer(int port) { public void init() { super.init(); connectionManager.init(); + //重置在线数 + RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); From dc343f3192c20b6761844fe8a3e5f2df50292d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 15:34:43 +0800 Subject: [PATCH 403/890] add python --- pub-hive.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pub-hive.sh b/pub-hive.sh index 29de189c..eaf495cb 100755 --- a/pub-hive.sh +++ b/pub-hive.sh @@ -14,8 +14,19 @@ echo "start scp mpush..." cd $base_dir/target + + +##远程备份 + + scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive1_host:~/mpush +## 杀进程 + +##启进程 + + + scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive2_host:~/mpush From 4619c5733a5b9e2bc943ab2741bc1329f4317e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 15:35:45 +0800 Subject: [PATCH 404/890] add --- pub-python.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pub-python.py diff --git a/pub-python.py b/pub-python.py new file mode 100644 index 00000000..12bbd672 --- /dev/null +++ b/pub-python.py @@ -0,0 +1,52 @@ +#! /usr/bin/python +# coding=utf8 + +import paramiko + +class SSH(): + def __init__(self): + self.client = None + + def connect(self,host,port=22,username='root',password=None): + self.client = paramiko.SSHClient() + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.connect(host, port, username=username, password=password, timeout=10) + return self + + def exe(self,cmd,isprint=True): + if not cmd: + return + stdin, stdout, stderr = self.client.exec_command(cmd) + if isprint: + for std in stdout.readlines(): + print std, + return stdin, stdout, stderr + + + def close(self): + if self.client: + self.client.close() + +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'shinemo' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'shinemo' + } +] + + +def main(): + for item in HOSTS: + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + ssh.exe('ls -l') + ssh.close() + + +if __name__ == "__main__": + main() From 3f945ad1e2bb9b29353fddbe7d2c679b040c0757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 16:06:02 +0800 Subject: [PATCH 405/890] chmod --- pub-python.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 pub-python.py diff --git a/pub-python.py b/pub-python.py old mode 100644 new mode 100755 From 50c3eec5f9c1a441ed0669fee51192db260ae3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 16:09:58 +0800 Subject: [PATCH 406/890] change user --- pub-python.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pub-python.py b/pub-python.py index 12bbd672..962fe5d7 100755 --- a/pub-python.py +++ b/pub-python.py @@ -1,4 +1,3 @@ -#! /usr/bin/python # coding=utf8 import paramiko @@ -31,12 +30,12 @@ def close(self): { 'HOST':'hive1_host', 'PORT':9092, - 'USER':'shinemo' + 'USER':'root' }, { 'HOST':'hive2_host', 'PORT':9092, - 'USER':'shinemo' + 'USER':'root' } ] From daa86a672dc067aaf8cd3e377ab4aebf80fe49cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 16:11:42 +0800 Subject: [PATCH 407/890] add color --- pub-python.py | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/pub-python.py b/pub-python.py index 962fe5d7..b642fc5d 100755 --- a/pub-python.py +++ b/pub-python.py @@ -2,6 +2,19 @@ import paramiko +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'root' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'root' + } +] + class SSH(): def __init__(self): self.client = None @@ -26,18 +39,27 @@ def close(self): if self.client: self.client.close() -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] + + +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s def main(): From 8abc1e0f7cb161d0fb3a6cbb070144e94993142f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 16:19:17 +0800 Subject: [PATCH 408/890] add back --- pub-python.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index b642fc5d..9665b11b 100755 --- a/pub-python.py +++ b/pub-python.py @@ -1,6 +1,7 @@ # coding=utf8 import paramiko +import datetime HOSTS = [ { @@ -65,7 +66,10 @@ def yellowText(s): def main(): for item in HOSTS: ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - ssh.exe('ls -l') + ##back + base = '/root/mpush/mpush-jar-with-dependency.tar.gz' + to = '/root/mpush/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ssh.exe('cp %s %s '%(base,to)) ssh.close() From fa67db475859ed1418fa8760b33109c138666c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 16:42:28 +0800 Subject: [PATCH 409/890] =?UTF-8?q?add=20python=20=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pub-python.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pub-python.py b/pub-python.py index 9665b11b..f65a8e2c 100755 --- a/pub-python.py +++ b/pub-python.py @@ -2,6 +2,7 @@ import paramiko import datetime +import telnetlib HOSTS = [ { @@ -16,6 +17,10 @@ } ] +BASEPATH = '/root/mpush' + + + class SSH(): def __init__(self): self.client = None @@ -41,7 +46,6 @@ def close(self): self.client.close() - def showText(s, typ): if typ == 'RED': return redText(s) @@ -66,10 +70,23 @@ def yellowText(s): def main(): for item in HOSTS: ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - ##back - base = '/root/mpush/mpush-jar-with-dependency.tar.gz' - to = '/root/mpush/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + + ##backup + base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' + to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') ssh.exe('cp %s %s '%(base,to)) + print greenText('backup mpush ok') + + ##telnet remove zk info + #ssh.exe('telent 127.0.0.1 4001') + #ssh.exe('') + + ## kill process + ssh.exe('ps aux|grep mpush-cs.jar') + + ## start process + # ssh.exe('') + ssh.close() From b74dd99edab5c0c393f089ebdb10669345446df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 16:44:44 +0800 Subject: [PATCH 410/890] add python --- pub-python.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index f65a8e2c..5c4506e6 100755 --- a/pub-python.py +++ b/pub-python.py @@ -20,7 +20,6 @@ BASEPATH = '/root/mpush' - class SSH(): def __init__(self): self.client = None @@ -37,7 +36,7 @@ def exe(self,cmd,isprint=True): stdin, stdout, stderr = self.client.exec_command(cmd) if isprint: for std in stdout.readlines(): - print std, + print `std`, return stdin, stdout, stderr From 71f683820d3b95d9166b81fc203e1f4e143cc53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 17:20:53 +0800 Subject: [PATCH 411/890] add python --- pub-python.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/pub-python.py b/pub-python.py index 5c4506e6..9c42353b 100755 --- a/pub-python.py +++ b/pub-python.py @@ -19,6 +19,10 @@ BASEPATH = '/root/mpush' +STARTPROCESS = 'java -jar mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' + class SSH(): def __init__(self): @@ -36,7 +40,7 @@ def exe(self,cmd,isprint=True): stdin, stdout, stderr = self.client.exec_command(cmd) if isprint: for std in stdout.readlines(): - print `std`, + print std, return stdin, stdout, stderr @@ -44,6 +48,17 @@ def close(self): if self.client: self.client.close() +def getPid(ssh): + pids = [] + stdin, stdout, stderr = ssh.exe('ps aux|grep "java -jar mpush-cs.jar" |grep -v "grep"') + for std in stdout.readlines(): + x = std.split(' ') + if len(x) < 10: + continue + if x.index(STARTPROCESS) < 0 : + continue + pid = filter(lambda ch: ch!='', std.split(' '))[1] + pids.append(pid) def showText(s, typ): if typ == 'RED': @@ -66,11 +81,12 @@ def yellowText(s): return "\033[1;33m%s\033[0m" % s + def main(): for item in HOSTS: ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - ##backup + ##1 backup base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') ssh.exe('cp %s %s '%(base,to)) @@ -80,11 +96,24 @@ def main(): #ssh.exe('telent 127.0.0.1 4001') #ssh.exe('') - ## kill process - ssh.exe('ps aux|grep mpush-cs.jar') + ##2 kill process + pids = getPid(ssh) + if len(pids) > 1: + print redText('mpush-cs server has more than one process.pls kill process by you self') + elif len(pids) == 0: + print yellowText('there is no mpush-cs process to kill') + elif len(pids) == 1: + ssh.exe('kill -9 %s'%(pids[0])) + + ##3 scp + scp -P item['PORT'] GITLABPATH item['HOST']:BASEPATH+'/mpush' + + + ##4 tar package + ssh.exe('tar -xzvf '+BASEPATH+'/mpush/mpush-jar-with-dependency.tar.gz') - ## start process - # ssh.exe('') + ##5 start process + ssh.exe('java -jar ' +BASEPATH +'/mpush/mpush-cs.jar &') ssh.close() From 2d1e3afb199e2be3e45af361a9b827513cc67694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 17:24:54 +0800 Subject: [PATCH 412/890] bug fix for python --- pub-python.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pub-python.py b/pub-python.py index 9c42353b..bd5425b1 100755 --- a/pub-python.py +++ b/pub-python.py @@ -80,7 +80,9 @@ def greenText(s): def yellowText(s): return "\033[1;33m%s\033[0m" % s - +def runShell(c): + print c + os.system(c) def main(): for item in HOSTS: @@ -106,8 +108,7 @@ def main(): ssh.exe('kill -9 %s'%(pids[0])) ##3 scp - scp -P item['PORT'] GITLABPATH item['HOST']:BASEPATH+'/mpush' - + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH+'/mpush') ##4 tar package ssh.exe('tar -xzvf '+BASEPATH+'/mpush/mpush-jar-with-dependency.tar.gz') From 256c9be3c45f0f261a563e5176c5068f348b10f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 17:27:10 +0800 Subject: [PATCH 413/890] bug fix --- pub-python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index bd5425b1..d97b6524 100755 --- a/pub-python.py +++ b/pub-python.py @@ -111,10 +111,10 @@ def main(): runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH+'/mpush') ##4 tar package - ssh.exe('tar -xzvf '+BASEPATH+'/mpush/mpush-jar-with-dependency.tar.gz') + ssh.exe('tar -xzvf %s/mpush/mpush-jar-with-dependency.tar.gz'%(BASEPATH)) ##5 start process - ssh.exe('java -jar ' +BASEPATH +'/mpush/mpush-cs.jar &') + ssh.exe('java -jar %s/mpush/mpush-cs.jar %'%(BASEPATH)) ssh.close() From da062e3a36f5ad85c495d94cf77daa905a3a8807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 17:29:08 +0800 Subject: [PATCH 414/890] bug fix --- pub-python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index d97b6524..e5825daa 100755 --- a/pub-python.py +++ b/pub-python.py @@ -111,10 +111,10 @@ def main(): runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH+'/mpush') ##4 tar package - ssh.exe('tar -xzvf %s/mpush/mpush-jar-with-dependency.tar.gz'%(BASEPATH)) + ssh.exe('tar -xzvf %s/mpush/mpush-jar-with-dependency.tar.gz'%BASEPATH) ##5 start process - ssh.exe('java -jar %s/mpush/mpush-cs.jar %'%(BASEPATH)) + ssh.exe('java -jar %s/mpush/mpush-cs.jar &'%BASEPATH) ssh.close() From 661de64eb82c9b918b81dffd31a6f7c323f5bd5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 17:33:26 +0800 Subject: [PATCH 415/890] bug fix --- pub-python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index e5825daa..15d55b81 100755 --- a/pub-python.py +++ b/pub-python.py @@ -111,10 +111,10 @@ def main(): runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH+'/mpush') ##4 tar package - ssh.exe('tar -xzvf %s/mpush/mpush-jar-with-dependency.tar.gz'%BASEPATH) + ssh.exe('tar -xzvf /root/mpush/mpush/mpush-jar-with-dependency.tar.gz') ##5 start process - ssh.exe('java -jar %s/mpush/mpush-cs.jar &'%BASEPATH) + ssh.exe('java -jar /root/mpush/mpush/mpush-cs.jar &') ssh.close() From 7d64e621b175d2e0e3ae6eacdb11fac650eec860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 18:02:57 +0800 Subject: [PATCH 416/890] bug fix --- pub-python.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/pub-python.py b/pub-python.py index 15d55b81..fa1622c0 100755 --- a/pub-python.py +++ b/pub-python.py @@ -3,6 +3,8 @@ import paramiko import datetime import telnetlib +import os +import sys HOSTS = [ { @@ -37,6 +39,7 @@ def connect(self,host,port=22,username='root',password=None): def exe(self,cmd,isprint=True): if not cmd: return + print greenText(cmd) stdin, stdout, stderr = self.client.exec_command(cmd) if isprint: for std in stdout.readlines(): @@ -49,17 +52,8 @@ def close(self): self.client.close() def getPid(ssh): - pids = [] - stdin, stdout, stderr = ssh.exe('ps aux|grep "java -jar mpush-cs.jar" |grep -v "grep"') - for std in stdout.readlines(): - x = std.split(' ') - if len(x) < 10: - continue - if x.index(STARTPROCESS) < 0 : - continue - pid = filter(lambda ch: ch!='', std.split(' '))[1] - pids.append(pid) - + stdin, stdout, stderr = ssh.exe(''' ps aux|grep "java -jar mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) + return stdout.read().strip() def showText(s, typ): if typ == 'RED': return redText(s) @@ -99,19 +93,19 @@ def main(): #ssh.exe('') ##2 kill process - pids = getPid(ssh) - if len(pids) > 1: - print redText('mpush-cs server has more than one process.pls kill process by you self') - elif len(pids) == 0: + pid = getPid(ssh) + print '=============',pid + + if pid : + ssh.exe('kill -9 %s'%pid) + else: print yellowText('there is no mpush-cs process to kill') - elif len(pids) == 1: - ssh.exe('kill -9 %s'%(pids[0])) ##3 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH+'/mpush') + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush/mpush')) ##4 tar package - ssh.exe('tar -xzvf /root/mpush/mpush/mpush-jar-with-dependency.tar.gz') + ssh.exe('cd /root/mpush/mpush & tar -xzvf ./mpush-jar-with-dependency.tar.gz') ##5 start process ssh.exe('java -jar /root/mpush/mpush/mpush-cs.jar &') From e3033611d95a3917c60a9d8e0c86de81fe470ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 18:07:28 +0800 Subject: [PATCH 417/890] bug ix --- pub-python.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index fa1622c0..0f8cdb06 100755 --- a/pub-python.py +++ b/pub-python.py @@ -82,6 +82,9 @@ def main(): for item in HOSTS: ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + ##0 assembly + + ##1 backup base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') @@ -105,7 +108,7 @@ def main(): runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush/mpush')) ##4 tar package - ssh.exe('cd /root/mpush/mpush & tar -xzvf ./mpush-jar-with-dependency.tar.gz') + ssh.exe('cd /root/mpush/mpush && tar -xzvf ./mpush-jar-with-dependency.tar.gz') ##5 start process ssh.exe('java -jar /root/mpush/mpush/mpush-cs.jar &') From 4b976d26526453ed86f0d6188fa3b8a0303a1de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 18:09:45 +0800 Subject: [PATCH 418/890] bug fix --- pub-python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index 0f8cdb06..068c1625 100755 --- a/pub-python.py +++ b/pub-python.py @@ -105,10 +105,10 @@ def main(): print yellowText('there is no mpush-cs process to kill') ##3 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush/mpush')) + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) ##4 tar package - ssh.exe('cd /root/mpush/mpush && tar -xzvf ./mpush-jar-with-dependency.tar.gz') + ssh.exe('cd /root/mpush && tar -xzvf ./mpush-jar-with-dependency.tar.gz') ##5 start process ssh.exe('java -jar /root/mpush/mpush/mpush-cs.jar &') From fb6cbe0481adaf3ea5da51db0d5e34009fdf1e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:40:25 +0800 Subject: [PATCH 419/890] add python --- pub-python.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/pub-python.py b/pub-python.py index 068c1625..120b5709 100755 --- a/pub-python.py +++ b/pub-python.py @@ -25,6 +25,8 @@ GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' +ENV=daily + class SSH(): def __init__(self): @@ -44,6 +46,7 @@ def exe(self,cmd,isprint=True): if isprint: for std in stdout.readlines(): print std, + print stderr.read() return stdin, stdout, stderr @@ -52,7 +55,7 @@ def close(self): self.client.close() def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "java -jar mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) + stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) return stdout.read().strip() def showText(s, typ): if typ == 'RED': @@ -83,13 +86,14 @@ def main(): ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) ##0 assembly - + runShell('mvn clean install assembly:assembly -P %s'%DEV)) + print showText('assembly success','greenText') ##1 backup base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('cp %s %s '%(base,to)) - print greenText('backup mpush ok') + ssh.exe('mv %s %s '%(base,to)) + print showText('backup mpush ok','greenText') ##telnet remove zk info #ssh.exe('telent 127.0.0.1 4001') @@ -97,21 +101,23 @@ def main(): ##2 kill process pid = getPid(ssh) - print '=============',pid if pid : ssh.exe('kill -9 %s'%pid) else: - print yellowText('there is no mpush-cs process to kill') + print showText('there is no mpush-cs process to kill','YELLOW') ##3 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush'),False) + print showText('scp success','greenText') ##4 tar package - ssh.exe('cd /root/mpush && tar -xzvf ./mpush-jar-with-dependency.tar.gz') + ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz') + print showText('tar success','greenText') ##5 start process - ssh.exe('java -jar /root/mpush/mpush/mpush-cs.jar &') + ssh.exe('/opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar &') + print showText('start process success','greenText') ssh.close() From 40b9eaeb77fa188326e9c4fc0c0e0c1d6cd3afab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:42:07 +0800 Subject: [PATCH 420/890] python --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index 120b5709..7d0a50ce 100755 --- a/pub-python.py +++ b/pub-python.py @@ -86,7 +86,7 @@ def main(): ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) ##0 assembly - runShell('mvn clean install assembly:assembly -P %s'%DEV)) + runShell('mvn clean install assembly:assembly -P %s',%DEV)) print showText('assembly success','greenText') ##1 backup From e47dc549f489f8055d0c7f7f99d3730780a41879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:42:51 +0800 Subject: [PATCH 421/890] bug fix --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index 7d0a50ce..814bcb0c 100755 --- a/pub-python.py +++ b/pub-python.py @@ -86,7 +86,7 @@ def main(): ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) ##0 assembly - runShell('mvn clean install assembly:assembly -P %s',%DEV)) + runShell('mvn clean install assembly:assembly -P %s'%DEV) print showText('assembly success','greenText') ##1 backup From 6759c5e870cf579458ecebd53b5c2e0503b1ebae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:43:32 +0800 Subject: [PATCH 422/890] bug ifx --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index 814bcb0c..c6d8e234 100755 --- a/pub-python.py +++ b/pub-python.py @@ -25,7 +25,7 @@ GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' -ENV=daily +ENV= 'daily' class SSH(): From 607a3d40b26a01b316d65199cf59cd71781d0637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:44:18 +0800 Subject: [PATCH 423/890] bug fi --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index c6d8e234..fdb2e67f 100755 --- a/pub-python.py +++ b/pub-python.py @@ -86,7 +86,7 @@ def main(): ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) ##0 assembly - runShell('mvn clean install assembly:assembly -P %s'%DEV) + runShell('mvn clean install assembly:assembly -P %s'%ENV) print showText('assembly success','greenText') ##1 backup From d8ef480901c15b4db865471ad1a970f655e23af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:46:38 +0800 Subject: [PATCH 424/890] bug fix --- pub-python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index fdb2e67f..1c199f1a 100755 --- a/pub-python.py +++ b/pub-python.py @@ -108,11 +108,11 @@ def main(): print showText('there is no mpush-cs process to kill','YELLOW') ##3 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush'),False) + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) print showText('scp success','greenText') ##4 tar package - ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz') + ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) print showText('tar success','greenText') ##5 start process From 3e0289dd52b970164a4578995f2de061bc17f291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 20:59:20 +0800 Subject: [PATCH 425/890] add timeout --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index 1c199f1a..c462872d 100755 --- a/pub-python.py +++ b/pub-python.py @@ -35,7 +35,7 @@ def __init__(self): def connect(self,host,port=22,username='root',password=None): self.client = paramiko.SSHClient() self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=10) + self.client.connect(host, port, username=username, password=password, timeout=30) return self def exe(self,cmd,isprint=True): From baf48d27cc5ea3be405925fdd035dcebe06aa1f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 21:08:30 +0800 Subject: [PATCH 426/890] add timeout --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index c462872d..698a83f4 100755 --- a/pub-python.py +++ b/pub-python.py @@ -35,7 +35,7 @@ def __init__(self): def connect(self,host,port=22,username='root',password=None): self.client = paramiko.SSHClient() self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=30) + self.client.connect(host, port, username=username, password=password, timeout=120) return self def exe(self,cmd,isprint=True): From f91f33366c1cffeb9d362df186ee0e977d30e42e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 29 Feb 2016 21:11:52 +0800 Subject: [PATCH 427/890] bug fix --- pub-python.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pub-python.py b/pub-python.py index 698a83f4..c191aa81 100755 --- a/pub-python.py +++ b/pub-python.py @@ -82,13 +82,14 @@ def runShell(c): os.system(c) def main(): + + ##0 assembly + runShell('mvn clean install assembly:assembly -P %s'%ENV) + print showText('assembly success','greenText') + for item in HOSTS: ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - ##0 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) - print showText('assembly success','greenText') - ##1 backup base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') From 94d4964076d4328fd3667aa3e7bc052b9b017d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:22:18 +0800 Subject: [PATCH 428/890] bug fix --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index c191aa81..022e49fe 100755 --- a/pub-python.py +++ b/pub-python.py @@ -117,7 +117,7 @@ def main(): print showText('tar success','greenText') ##5 start process - ssh.exe('/opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar &') + ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') print showText('start process success','greenText') ssh.close() From b054924052789677e01dd9d6dd88e2873345b0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:34:06 +0800 Subject: [PATCH 429/890] =?UTF-8?q?=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pub-python.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pub-python.py b/pub-python.py index 022e49fe..9a688a21 100755 --- a/pub-python.py +++ b/pub-python.py @@ -87,7 +87,17 @@ def main(): runShell('mvn clean install assembly:assembly -P %s'%ENV) print showText('assembly success','greenText') + confirmPub = raw_input("确认发布Y/N:") + + if confirmPub != 'Y' + return; + for item in HOSTS: + + pubHost = raw_input("发布%sY/N:"%item['HOST']) + if pubHost != 'Y' + return; + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) ##1 backup From e67d860faee4fcebdab08b0b1fdf8e60fb7e591d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:36:47 +0800 Subject: [PATCH 430/890] add --- pub-python.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pub-python.py b/pub-python.py index 9a688a21..75543715 100755 --- a/pub-python.py +++ b/pub-python.py @@ -89,14 +89,14 @@ def main(): confirmPub = raw_input("确认发布Y/N:") - if confirmPub != 'Y' - return; + if confirmPub != 'Y': + return for item in HOSTS: pubHost = raw_input("发布%sY/N:"%item['HOST']) - if pubHost != 'Y' - return; + if pubHost != 'Y': + return ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) From 28dadb0a07c808c8cb5fb95a801663221c30dafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:41:39 +0800 Subject: [PATCH 431/890] pub --- pub-python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-python.py b/pub-python.py index 75543715..3caf114e 100755 --- a/pub-python.py +++ b/pub-python.py @@ -87,14 +87,14 @@ def main(): runShell('mvn clean install assembly:assembly -P %s'%ENV) print showText('assembly success','greenText') - confirmPub = raw_input("确认发布Y/N:") + confirmPub = raw_input("确认发布(Y/N):") if confirmPub != 'Y': return for item in HOSTS: - pubHost = raw_input("发布%sY/N:"%item['HOST']) + pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) if pubHost != 'Y': return From 4818901570866bec1246390e05485d47b8ebd179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:46:40 +0800 Subject: [PATCH 432/890] bug ifx --- pub-python.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pub-python.py b/pub-python.py index 3caf114e..630b3a6d 100755 --- a/pub-python.py +++ b/pub-python.py @@ -87,6 +87,9 @@ def main(): runShell('mvn clean install assembly:assembly -P %s'%ENV) print showText('assembly success','greenText') + ##包创建时间 + runShell('stat -c "%y" %s'%GITLABPATH) + confirmPub = raw_input("确认发布(Y/N):") if confirmPub != 'Y': From b8445d4768be1c26cd707d8db92d43db1249fe5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:48:45 +0800 Subject: [PATCH 433/890] bug ifx --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index 630b3a6d..0623e76f 100755 --- a/pub-python.py +++ b/pub-python.py @@ -88,7 +88,7 @@ def main(): print showText('assembly success','greenText') ##包创建时间 - runShell('stat -c "%y" %s'%GITLABPATH) + runShell(''' stat -c "%y" %s '''%GITLABPATH) confirmPub = raw_input("确认发布(Y/N):") From 7e64092db3b370920880f0fdca980301fac83d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 09:51:15 +0800 Subject: [PATCH 434/890] bug fix --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index 0623e76f..ad668a20 100755 --- a/pub-python.py +++ b/pub-python.py @@ -88,7 +88,7 @@ def main(): print showText('assembly success','greenText') ##包创建时间 - runShell(''' stat -c "%y" %s '''%GITLABPATH) + runShell(''' stat -c "%y" /data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz ''') confirmPub = raw_input("确认发布(Y/N):") From b46cc0e01eca593a4f431be8296158d697938b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 10:10:34 +0800 Subject: [PATCH 435/890] bugfix --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index ad668a20..b07e4ae7 100755 --- a/pub-python.py +++ b/pub-python.py @@ -88,7 +88,7 @@ def main(): print showText('assembly success','greenText') ##包创建时间 - runShell(''' stat -c "%y" /data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz ''') + runShell("stat -c '%y' %s"%GITLABPATH) confirmPub = raw_input("确认发布(Y/N):") From d932a1f314a828bf29cc01ee9d9bed9f01ce6730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 10:14:45 +0800 Subject: [PATCH 436/890] pub --- pub-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-python.py b/pub-python.py index b07e4ae7..e9c52a07 100755 --- a/pub-python.py +++ b/pub-python.py @@ -88,7 +88,7 @@ def main(): print showText('assembly success','greenText') ##包创建时间 - runShell("stat -c '%y' %s"%GITLABPATH) + runShell('stat -c "%%y" %s'%GITLABPATH) confirmPub = raw_input("确认发布(Y/N):") From 4eaf7b9b5ea3826e3f7e127eb373037ae2a00c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 10:21:57 +0800 Subject: [PATCH 437/890] bug fix --- pub-python.py | 140 -------------------------------------------------- 1 file changed, 140 deletions(-) delete mode 100755 pub-python.py diff --git a/pub-python.py b/pub-python.py deleted file mode 100755 index e9c52a07..00000000 --- a/pub-python.py +++ /dev/null @@ -1,140 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -STARTPROCESS = 'java -jar mpush-cs.jar' - -GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' - -ENV= 'daily' - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) - print showText('assembly success','greenText') - - ##包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(Y/N):") - - if confirmPub != 'Y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) - if pubHost != 'Y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##1 backup - base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' - to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ##telnet remove zk info - #ssh.exe('telent 127.0.0.1 4001') - #ssh.exe('') - - ##2 kill process - pid = getPid(ssh) - - if pid : - ssh.exe('kill -9 %s'%pid) - else: - print showText('there is no mpush-cs process to kill','YELLOW') - - ##3 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) - print showText('scp success','greenText') - - ##4 tar package - ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) - print showText('tar success','greenText') - - ##5 start process - ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') - print showText('start process success','greenText') - - ssh.close() - - -if __name__ == "__main__": - main() From 3a998d6d9c2da1264dada49fec84ade902aa06c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 10:40:12 +0800 Subject: [PATCH 438/890] add --- pub-daily.py | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ pub-dev.py | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++ pub-online.py | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++ pub-pre.py | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++ test.py | 9 ++++ 5 files changed, 572 insertions(+) create mode 100755 pub-daily.py create mode 100644 pub-dev.py create mode 100644 pub-online.py create mode 100644 pub-pre.py create mode 100755 test.py diff --git a/pub-daily.py b/pub-daily.py new file mode 100755 index 00000000..42451212 --- /dev/null +++ b/pub-daily.py @@ -0,0 +1,140 @@ +# coding=utf8 + +import paramiko +import datetime +import telnetlib +import os +import sys + +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'root' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'root' + } +] + +BASEPATH = '/root/mpush' + +MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz' + +PROCESS_KEY_WORD = 'mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME + +JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' + +ENV= 'daily' + + +class SSH(): + def __init__(self): + self.client = None + + def connect(self,host,port=22,username='root',password=None): + self.client = paramiko.SSHClient() + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.connect(host, port, username=username, password=password, timeout=120) + return self + + def exe(self,cmd,isprint=True,get_pty=True): + if not cmd: + return + print greenText(cmd) + stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=get_pty) + if isprint: + for std in stdout.readlines(): + print std, + print stderr.read() + return stdin, stdout, stderr + + + def close(self): + if self.client: + self.client.close() + +def getPid(ssh): + stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD,False) + return stdout.read().strip() +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s + +def runShell(c): + print c + os.system(c) + +def main(): + + ##0 assembly + runShell('mvn clean install assembly:assembly -P %s'%ENV) + print showText('assembly success','greenText') + + ##1 包创建时间 + runShell('stat -c "%%y" %s'%GITLABPATH) + + confirmPub = raw_input("确认发布(Y/N):") + + if confirmPub != 'Y': + return + + for item in HOSTS: + + pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) + if pubHost != 'Y': + return + + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + + ##2 backup + base = BASEPATH+MPUSH_TAR_NAME + to = BASEPATH+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ssh.exe('mv %s %s '%(base,to)) + print showText('backup mpush ok','greenText') + + ##3 kill process + pid = getPid(ssh) + if pid : + ssh.exe('kill -9 %s'%pid) + else: + print showText('there is no process to kill','YELLOW') + + ##4 scp + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + print showText('scp success','greenText') + + ##5 tar package + ssh.exe('cd %s && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) + print showText('tar success','greenText') + + ##6 start process + ssh.exe('nohup %s -jar %s/mpush/mpush-cs.jar >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,BASEPATH)) + print showText('start process success','greenText') + + + ssh.close() + + +if __name__ == "__main__": + main() diff --git a/pub-dev.py b/pub-dev.py new file mode 100644 index 00000000..d75a3fc3 --- /dev/null +++ b/pub-dev.py @@ -0,0 +1,141 @@ +# coding=utf8 + +import paramiko +import datetime +import telnetlib +import os +import sys + +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'root' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'root' + } +] + +BASEPATH = '/root/mpush' + +STARTPROCESS = 'java -jar mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' + +ENV= 'daily' + + +class SSH(): + def __init__(self): + self.client = None + + def connect(self,host,port=22,username='root',password=None): + self.client = paramiko.SSHClient() + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.connect(host, port, username=username, password=password, timeout=120) + return self + + def exe(self,cmd,isprint=True): + if not cmd: + return + print greenText(cmd) + stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) + if isprint: + for std in stdout.readlines(): + print std, + print stderr.read() + return stdin, stdout, stderr + + + def close(self): + if self.client: + self.client.close() + +def getPid(ssh): + stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) + return stdout.read().strip() +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s + +def runShell(c): + print c + os.system(c) + +def main(): + + ##0 assembly + runShell('mvn clean install assembly:assembly -P %s'%ENV) + print showText('assembly success','greenText') + + ##1 包创建时间 + runShell('stat -c "%%y" %s'%GITLABPATH) + + confirmPub = raw_input("确认发布(Y/N):") + + if confirmPub != 'Y': + return + + for item in HOSTS: + + pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) + if pubHost != 'Y': + return + + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + + ##2 backup + base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' + to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ssh.exe('mv %s %s '%(base,to)) + print showText('backup mpush ok','greenText') + + ##telnet remove zk info + #ssh.exe('telent 127.0.0.1 4001') + #ssh.exe('') + + ##3 kill process + pid = getPid(ssh) + + if pid : + ssh.exe('kill -9 %s'%pid) + else: + print showText('there is no mpush-cs process to kill','YELLOW') + + ##4 scp + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + print showText('scp success','greenText') + + ##5 tar package + ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) + print showText('tar success','greenText') + + ##6 start process + ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') + print showText('start process success','greenText') + + + ssh.close() + + +if __name__ == "__main__": + main() diff --git a/pub-online.py b/pub-online.py new file mode 100644 index 00000000..d75a3fc3 --- /dev/null +++ b/pub-online.py @@ -0,0 +1,141 @@ +# coding=utf8 + +import paramiko +import datetime +import telnetlib +import os +import sys + +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'root' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'root' + } +] + +BASEPATH = '/root/mpush' + +STARTPROCESS = 'java -jar mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' + +ENV= 'daily' + + +class SSH(): + def __init__(self): + self.client = None + + def connect(self,host,port=22,username='root',password=None): + self.client = paramiko.SSHClient() + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.connect(host, port, username=username, password=password, timeout=120) + return self + + def exe(self,cmd,isprint=True): + if not cmd: + return + print greenText(cmd) + stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) + if isprint: + for std in stdout.readlines(): + print std, + print stderr.read() + return stdin, stdout, stderr + + + def close(self): + if self.client: + self.client.close() + +def getPid(ssh): + stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) + return stdout.read().strip() +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s + +def runShell(c): + print c + os.system(c) + +def main(): + + ##0 assembly + runShell('mvn clean install assembly:assembly -P %s'%ENV) + print showText('assembly success','greenText') + + ##1 包创建时间 + runShell('stat -c "%%y" %s'%GITLABPATH) + + confirmPub = raw_input("确认发布(Y/N):") + + if confirmPub != 'Y': + return + + for item in HOSTS: + + pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) + if pubHost != 'Y': + return + + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + + ##2 backup + base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' + to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ssh.exe('mv %s %s '%(base,to)) + print showText('backup mpush ok','greenText') + + ##telnet remove zk info + #ssh.exe('telent 127.0.0.1 4001') + #ssh.exe('') + + ##3 kill process + pid = getPid(ssh) + + if pid : + ssh.exe('kill -9 %s'%pid) + else: + print showText('there is no mpush-cs process to kill','YELLOW') + + ##4 scp + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + print showText('scp success','greenText') + + ##5 tar package + ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) + print showText('tar success','greenText') + + ##6 start process + ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') + print showText('start process success','greenText') + + + ssh.close() + + +if __name__ == "__main__": + main() diff --git a/pub-pre.py b/pub-pre.py new file mode 100644 index 00000000..d75a3fc3 --- /dev/null +++ b/pub-pre.py @@ -0,0 +1,141 @@ +# coding=utf8 + +import paramiko +import datetime +import telnetlib +import os +import sys + +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'root' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'root' + } +] + +BASEPATH = '/root/mpush' + +STARTPROCESS = 'java -jar mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' + +ENV= 'daily' + + +class SSH(): + def __init__(self): + self.client = None + + def connect(self,host,port=22,username='root',password=None): + self.client = paramiko.SSHClient() + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.connect(host, port, username=username, password=password, timeout=120) + return self + + def exe(self,cmd,isprint=True): + if not cmd: + return + print greenText(cmd) + stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) + if isprint: + for std in stdout.readlines(): + print std, + print stderr.read() + return stdin, stdout, stderr + + + def close(self): + if self.client: + self.client.close() + +def getPid(ssh): + stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) + return stdout.read().strip() +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s + +def runShell(c): + print c + os.system(c) + +def main(): + + ##0 assembly + runShell('mvn clean install assembly:assembly -P %s'%ENV) + print showText('assembly success','greenText') + + ##1 包创建时间 + runShell('stat -c "%%y" %s'%GITLABPATH) + + confirmPub = raw_input("确认发布(Y/N):") + + if confirmPub != 'Y': + return + + for item in HOSTS: + + pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) + if pubHost != 'Y': + return + + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + + ##2 backup + base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' + to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ssh.exe('mv %s %s '%(base,to)) + print showText('backup mpush ok','greenText') + + ##telnet remove zk info + #ssh.exe('telent 127.0.0.1 4001') + #ssh.exe('') + + ##3 kill process + pid = getPid(ssh) + + if pid : + ssh.exe('kill -9 %s'%pid) + else: + print showText('there is no mpush-cs process to kill','YELLOW') + + ##4 scp + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + print showText('scp success','greenText') + + ##5 tar package + ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) + print showText('tar success','greenText') + + ##6 start process + ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') + print showText('start process success','greenText') + + + ssh.close() + + +if __name__ == "__main__": + main() diff --git a/test.py b/test.py new file mode 100755 index 00000000..a9dd11e2 --- /dev/null +++ b/test.py @@ -0,0 +1,9 @@ +#!/usr/bin/python +# coding=utf8 + +import datetime +import os +import sys + +str = raw_input("请输入:"); +print "你输入的内容是: ", str From c5fc4584c6cc20dedfb458a606eb3ca9b06b8244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 10:45:55 +0800 Subject: [PATCH 439/890] bug fix --- pub-daily.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 42451212..11162c77 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -108,8 +108,8 @@ def main(): ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) ##2 backup - base = BASEPATH+MPUSH_TAR_NAME - to = BASEPATH+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + base = BASEPATH+'/'+MPUSH_TAR_NAME + to = BASEPATH+'/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') ssh.exe('mv %s %s '%(base,to)) print showText('backup mpush ok','greenText') From e046217cde38749b97279d219b69c2a7c7efad3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 10:56:49 +0800 Subject: [PATCH 440/890] bug fix --- pub-daily.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 11162c77..70e8a8e1 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -109,7 +109,7 @@ def main(): ##2 backup base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') ssh.exe('mv %s %s '%(base,to)) print showText('backup mpush ok','greenText') @@ -121,7 +121,7 @@ def main(): print showText('there is no process to kill','YELLOW') ##4 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) print showText('scp success','greenText') ##5 tar package From b577cb380aa0a54f2305e63118b0212ec73bfd73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 11:02:37 +0800 Subject: [PATCH 441/890] bug fix --- pub-daily.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 70e8a8e1..5c375e3b 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -125,11 +125,11 @@ def main(): print showText('scp success','greenText') ##5 tar package - ssh.exe('cd %s && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) + ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) print showText('tar success','greenText') ##6 start process - ssh.exe('nohup %s -jar %s/mpush/mpush-cs.jar >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,BASEPATH)) + ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) print showText('start process success','greenText') From 0ea956909ec48a6c7639235d35c720023604ec23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 11:41:24 +0800 Subject: [PATCH 442/890] bug fix --- pub-daily.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 5c375e3b..f0b014b2 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -42,11 +42,11 @@ def connect(self,host,port=22,username='root',password=None): self.client.connect(host, port, username=username, password=password, timeout=120) return self - def exe(self,cmd,isprint=True,get_pty=True): + def exe(self,cmd,isprint=True): if not cmd: return print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=get_pty) + stdin, stdout, stderr = self.client.exec_command(cmd) if isprint: for std in stdout.readlines(): print std, From 9b03845dcbfbc116ccc6b3ed3771f7311051b2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 11:51:43 +0800 Subject: [PATCH 443/890] bug fix --- pub-daily.py | 22 ++++---- pub-dev.py | 141 --------------------------------------------------- pub-hive.sh | 32 ------------ 3 files changed, 13 insertions(+), 182 deletions(-) delete mode 100644 pub-dev.py delete mode 100755 pub-hive.sh diff --git a/pub-daily.py b/pub-daily.py index f0b014b2..5957dfe6 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -87,48 +87,52 @@ def runShell(c): def main(): - ##0 assembly + ##0 git pull + runShell('git pull origin master') + print showText('git pull master success','greenText') + + ##1 assembly runShell('mvn clean install assembly:assembly -P %s'%ENV) print showText('assembly success','greenText') - ##1 包创建时间 + ##2 包创建时间 runShell('stat -c "%%y" %s'%GITLABPATH) confirmPub = raw_input("确认发布(Y/N):") - if confirmPub != 'Y': + if confirmPub != 'Y' or confirmPub != 'y': return for item in HOSTS: pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) - if pubHost != 'Y': + if pubHost != 'Y' or pubHost != 'y': return ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - ##2 backup + ##3 backup base = BASEPATH+'/'+MPUSH_TAR_NAME to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') ssh.exe('mv %s %s '%(base,to)) print showText('backup mpush ok','greenText') - ##3 kill process + ##4 kill process pid = getPid(ssh) if pid : ssh.exe('kill -9 %s'%pid) else: print showText('there is no process to kill','YELLOW') - ##4 scp + ##5 scp runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) print showText('scp success','greenText') - ##5 tar package + ##6 tar package ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) print showText('tar success','greenText') - ##6 start process + ##7 start process ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) print showText('start process success','greenText') diff --git a/pub-dev.py b/pub-dev.py deleted file mode 100644 index d75a3fc3..00000000 --- a/pub-dev.py +++ /dev/null @@ -1,141 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -STARTPROCESS = 'java -jar mpush-cs.jar' - -GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' - -ENV= 'daily' - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) - print showText('assembly success','greenText') - - ##1 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(Y/N):") - - if confirmPub != 'Y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) - if pubHost != 'Y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##2 backup - base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' - to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ##telnet remove zk info - #ssh.exe('telent 127.0.0.1 4001') - #ssh.exe('') - - ##3 kill process - pid = getPid(ssh) - - if pid : - ssh.exe('kill -9 %s'%pid) - else: - print showText('there is no mpush-cs process to kill','YELLOW') - - ##4 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) - print showText('scp success','greenText') - - ##5 tar package - ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) - print showText('tar success','greenText') - - ##6 start process - ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/pub-hive.sh b/pub-hive.sh deleted file mode 100755 index eaf495cb..00000000 --- a/pub-hive.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh - -ENV=daily - -base_dir=`pwd` - -echo "start assembly lib..." - -rm -rf $base_dir/target - -mvn clean install assembly:assembly -P $ENV - -echo "start scp mpush..." - -cd $base_dir/target - - - -##远程备份 - - -scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive1_host:~/mpush - -## 杀进程 - -##启进程 - - - - - -scp -P 9092 ./mpush-jar-with-dependency.tar.gz hive2_host:~/mpush From 6e6320bd96fe1d6904be164c754d2eb981525e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 11:54:33 +0800 Subject: [PATCH 444/890] bug fix --- pub-daily.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 5957dfe6..69f6f0ea 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -98,15 +98,15 @@ def main(): ##2 包创建时间 runShell('stat -c "%%y" %s'%GITLABPATH) - confirmPub = raw_input("确认发布(Y/N):") + confirmPub = raw_input("确认发布(y/n):") - if confirmPub != 'Y' or confirmPub != 'y': + if confirmPub != 'y': return for item in HOSTS: - pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) - if pubHost != 'Y' or pubHost != 'y': + pubHost = raw_input("发布 %s (y/n):"%item['HOST']) + if pubHost != 'y': return ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) From 542246516523fda37cca2b75bc5d04cb39818310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:26:00 +0800 Subject: [PATCH 445/890] bug ifx --- daily-pub.py | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++ pub-daily.py | 3 + 2 files changed, 200 insertions(+) create mode 100644 daily-pub.py diff --git a/daily-pub.py b/daily-pub.py new file mode 100644 index 00000000..07dd1bf7 --- /dev/null +++ b/daily-pub.py @@ -0,0 +1,197 @@ +# coding=utf8 + +import paramiko +import datetime +import telnetlib +import os +import sys + +HOSTS = [ + { + 'HOST':'hive1_host', + 'PORT':9092, + 'USER':'root' + }, + { + 'HOST':'hive2_host', + 'PORT':9092, + 'USER':'root' + } +] + +BASEPATH = '/root/mpush' + +MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz' + +PROCESS_KEY_WORD = 'mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME + +JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' + +ENV= 'daily' + + +class SSH(object): + def __init__(self): + self.client = None + self.chan = None + self.shell = None + + def __enter__(self): + return self + + def __exit__(self, typ, value, trace): + self.close() + + def connect(self, host, port=22, username='root', password=None): + self.client = paramiko.SSHClient() + self.client.load_system_host_keys() + self.client.connect(host, port, username=username, password=password, timeout=10) + return self + + def close(self): + if self.client: + self.client.close() + + def exec_command(self, cmd, isprint=True): + """执行命令,每次执行都是新的session""" + if not cmd: + return + print_cmd(cmd) + + stdin, stdout, stderr = self.client.exec_command(cmd) + out = stdout.read() + if isprint: + print_out(out) + + err = stderr.read() + if err: + print_out(err) + return out, err + + def _invoke_shell(self): + """创建一个shell""" + self.shell = self.client.invoke_shell(width=200) + is_recv = False + while True: + if self.shell.recv_ready(): + print_out_stream(self.shell.recv(1024)) + is_recv = True + else: + if is_recv: + return + else: + time.sleep(0.1) + + def shell_exec(self, cmd): + """在shell中执行命令,使用的是同一个session""" + if not cmd: + return + + if not self.shell: + self._invoke_shell() + + self.shell.send(cmd + "\n") + + out = '' + is_recv = False + while True: + if self.shell.recv_ready(): + tmp = self.shell.recv(1024) + out += tmp + print_out_stream(tmp) + is_recv = True + else: + if is_recv: + return out + else: + time.sleep(0.1) + +def getPid(ssh): + stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD,False) + return stdout.read().strip() +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s + +def runShell(c): + print c + os.system(c) + +def main(): + + ##0 git pull + runShell('git pull origin master') + print showText('git pull master success','greenText') + + ##1 assembly + runShell('mvn clean install assembly:assembly -P %s'%ENV) + print showText('assembly success','greenText') + + ##2 包创建时间 + runShell('stat -c "%%y" %s'%GITLABPATH) + + confirmPub = raw_input("确认发布(y/n):") + + if confirmPub != 'y': + return + + for item in HOSTS: + + pubHost = raw_input("发布 %s (y/n):"%item['HOST']) + if pubHost != 'y': + return + + ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + + ##3 backup + base = BASEPATH+'/'+MPUSH_TAR_NAME + to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ssh.shell_exec('mv %s %s '%(base,to)) + print showText('backup mpush ok','greenText') + + ## remove zk info + + + ##4 kill process + pid = getPid(ssh) + if pid : + ssh.shell_exec('kill -9 %s'%pid) + else: + print showText('there is no process to kill','YELLOW') + + ##5 scp + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) + print showText('scp success','greenText') + + ##6 tar package + ssh.shell_exec('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) + print showText('tar success','greenText') + + ##7 start process + ssh.shell_exec('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) + print showText('start process success','greenText') + + + ssh.close() + + +if __name__ == "__main__": + main() diff --git a/pub-daily.py b/pub-daily.py index 69f6f0ea..5adeca64 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -117,6 +117,9 @@ def main(): ssh.exe('mv %s %s '%(base,to)) print showText('backup mpush ok','greenText') + ## remove zk info + + ##4 kill process pid = getPid(ssh) if pid : From 6b49e12e0298e7c7c68ec6672e87e7ea260c71b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:28:09 +0800 Subject: [PATCH 446/890] bug ifx --- daily-pub.py | 1 + 1 file changed, 1 insertion(+) diff --git a/daily-pub.py b/daily-pub.py index 07dd1bf7..eedd780c 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -5,6 +5,7 @@ import telnetlib import os import sys +import time HOSTS = [ { From 90cea99fba7447bef34365bab300c398c74fa29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:34:41 +0800 Subject: [PATCH 447/890] bug fix --- daily-pub.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/daily-pub.py b/daily-pub.py index eedd780c..4090b918 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -47,7 +47,8 @@ def __exit__(self, typ, value, trace): def connect(self, host, port=22, username='root', password=None): self.client = paramiko.SSHClient() - self.client.load_system_host_keys() + ##self.client.load_system_host_keys() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client.connect(host, port, username=username, password=password, timeout=10) return self @@ -160,7 +161,7 @@ def main(): if pubHost != 'y': return - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) + ssh = SSH().connect(item['HOST'],item['PORT'],item['USER']) ##3 backup base = BASEPATH+'/'+MPUSH_TAR_NAME From 7d9cb7c05ad8adcbfdfee264a4ac17d9c0908a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:35:17 +0800 Subject: [PATCH 448/890] bug ifx --- daily-pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daily-pub.py b/daily-pub.py index 4090b918..52630a3e 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -48,7 +48,7 @@ def __exit__(self, typ, value, trace): def connect(self, host, port=22, username='root', password=None): self.client = paramiko.SSHClient() ##self.client.load_system_host_keys() - client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client.connect(host, port, username=username, password=password, timeout=10) return self From 97b2198014f61e2c702903bc1076202780022a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:37:25 +0800 Subject: [PATCH 449/890] bug fix --- daily-pub.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/daily-pub.py b/daily-pub.py index 52630a3e..1e528099 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -133,6 +133,20 @@ def greenText(s): def yellowText(s): return "\033[1;33m%s\033[0m" % s +def print_cmd(s): + """打印执行的命令""" + print yellowText(s) + + +def print_out(s): + """打印执行命令的结果""" + print greenText(s) + + +def print_out_stream(s): + """打印执行命令的结果""" + sys.stdout.write(greenText(s)) + def runShell(c): print c os.system(c) From fa4cd8475359ae58537f2a01952bc341d79a01b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:39:11 +0800 Subject: [PATCH 450/890] bug fix --- daily-pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daily-pub.py b/daily-pub.py index 1e528099..05a09246 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -111,7 +111,7 @@ def shell_exec(self, cmd): time.sleep(0.1) def getPid(ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD,False) + stdin, stdout, stderr = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD,False) return stdout.read().strip() def showText(s, typ): if typ == 'RED': From 51b26714b86a30d82dbc40a5fbb337a2ea552932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:40:51 +0800 Subject: [PATCH 451/890] bug fix --- daily-pub.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/daily-pub.py b/daily-pub.py index 05a09246..ba1527d5 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -111,7 +111,7 @@ def shell_exec(self, cmd): time.sleep(0.1) def getPid(ssh): - stdin, stdout, stderr = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD,False) + stdin, stdout, stderr = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) return stdout.read().strip() def showText(s, typ): if typ == 'RED': @@ -198,7 +198,7 @@ def main(): print showText('scp success','greenText') ##6 tar package - ssh.shell_exec('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) + ssh.shell_exec('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME)) print showText('tar success','greenText') ##7 start process From 1e09fa9ed1bcdb26a833a58a32b69fdfa891118f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:43:50 +0800 Subject: [PATCH 452/890] bug fix --- daily-pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daily-pub.py b/daily-pub.py index ba1527d5..71347977 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -111,7 +111,7 @@ def shell_exec(self, cmd): time.sleep(0.1) def getPid(ssh): - stdin, stdout, stderr = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) + stdout = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) return stdout.read().strip() def showText(s, typ): if typ == 'RED': From dc42b22fa2cee18ba3a576723b951341f2d4955a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 15:46:00 +0800 Subject: [PATCH 453/890] bug fix --- daily-pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daily-pub.py b/daily-pub.py index 71347977..0e771267 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -112,7 +112,7 @@ def shell_exec(self, cmd): def getPid(ssh): stdout = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) - return stdout.read().strip() + return stdout def showText(s, typ): if typ == 'RED': return redText(s) From 58b9b94c2e05f7d138117a9073a765819fae2caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 16:56:54 +0800 Subject: [PATCH 454/890] bug fix --- pub-daily.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 5adeca64..bee53c9b 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -5,6 +5,7 @@ import telnetlib import os import sys +import time HOSTS = [ { @@ -118,14 +119,19 @@ def main(): print showText('backup mpush ok','greenText') ## remove zk info - - ##4 kill process + + ##4 kill process 先kill执行。等待5秒后,如果进程还是没有杀掉,则执行kill -9 pid = getPid(ssh) if pid : - ssh.exe('kill -9 %s'%pid) + ssh.exe('kill %s'%pid) + time.sleep(5) else: print showText('there is no process to kill','YELLOW') + pid = getPid(ssh) + if pid: + ssh.exe('kill -9 %s'%pid) + ##5 scp runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) From b40cd59eade7534ff65e0a6f6858ed6a1aff0c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 17:00:55 +0800 Subject: [PATCH 455/890] bug fix --- pub-daily.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index bee53c9b..a02491b0 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -121,11 +121,11 @@ def main(): ## remove zk info - ##4 kill process 先kill执行。等待5秒后,如果进程还是没有杀掉,则执行kill -9 + ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 pid = getPid(ssh) if pid : ssh.exe('kill %s'%pid) - time.sleep(5) + time.sleep(15) else: print showText('there is no process to kill','YELLOW') pid = getPid(ssh) From 6084f381862fccc08d2e38411ce879e0328ad9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 17:35:50 +0800 Subject: [PATCH 456/890] add telnet --- pub-daily.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/pub-daily.py b/pub-daily.py index a02491b0..650431db 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -32,6 +32,39 @@ ENV= 'daily' +class Telnet(object): + def __init__(self, chan): + self.chan = chan + + def send(self, cmd): + self.chan.send(cmd+'\n') + print_cmd(cmd) + out = '' + is_recv = False + is_recv_err = False + while True: + # 结束 + if self.chan.recv_stderr_ready(): + tmp = self.chan.recv_stderr(1024) + print_out_stream(tmp) + out += tmp + is_recv_err = True + else: + if is_recv_err: + return out + else: + time.sleep(0.1) + + if self.chan.recv_ready(): + tmp = self.chan.recv(1024) + print_out_stream(tmp) + out += tmp + is_recv = True + else: + if is_recv: + return out + else: + time.sleep(0.1) class SSH(): def __init__(self): @@ -54,6 +87,24 @@ def exe(self,cmd,isprint=True): print stderr.read() return stdin, stdout, stderr + def telnet(self, cmd): + chan = self.client.get_transport().open_session(timeout=10) + chan.exec_command(cmd) + + t = Telnet(chan) + + is_recv = False + + while True: + if chan.recv_ready(): + print_out_stream(chan.recv(1024)) + is_recv = True + else: + if is_recv: + return t + else: + time.sleep(0.1) + def close(self): if self.client: @@ -119,7 +170,10 @@ def main(): print showText('backup mpush ok','greenText') ## remove zk info - + telnet = ssh.telnet('telnet 127.0.0.1 4001') + telnet.send(' ') + telnet.send('rcs') ## 删除zk + telnet.send('quit') ## 关闭连接 ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 pid = getPid(ssh) From 0eff757fe0fdf7c73e93fe2c96eeebe2b4dcdca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 17:37:54 +0800 Subject: [PATCH 457/890] bug fix --- pub-daily.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pub-daily.py b/pub-daily.py index 650431db..3b0a8921 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -133,6 +133,20 @@ def greenText(s): def yellowText(s): return "\033[1;33m%s\033[0m" % s +def print_cmd(s): + """打印执行的命令""" + print yellowText(s) + + +def print_out(s): + """打印执行命令的结果""" + print greenText(s) + + +def print_out_stream(s): + """打印执行命令的结果""" + sys.stdout.write(greenText(s)) + def runShell(c): print c os.system(c) From 7f324c901001a2fc8b58149a7b7806f6754b5074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 17:42:43 +0800 Subject: [PATCH 458/890] bug fix --- pub-daily.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index 3b0a8921..af3d0b17 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -36,7 +36,7 @@ class Telnet(object): def __init__(self, chan): self.chan = chan - def send(self, cmd): + def send(self, cmd,isprint=True): self.chan.send(cmd+'\n') print_cmd(cmd) out = '' @@ -46,7 +46,8 @@ def send(self, cmd): # 结束 if self.chan.recv_stderr_ready(): tmp = self.chan.recv_stderr(1024) - print_out_stream(tmp) + if isprint: + print_out_stream(tmp) out += tmp is_recv_err = True else: @@ -57,7 +58,8 @@ def send(self, cmd): if self.chan.recv_ready(): tmp = self.chan.recv(1024) - print_out_stream(tmp) + if isprint: + print_out_stream(tmp) out += tmp is_recv = True else: @@ -185,10 +187,14 @@ def main(): ## remove zk info telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ') + telnet.send(' ',False) telnet.send('rcs') ## 删除zk telnet.send('quit') ## 关闭连接 + + time.sleep(30) + print showText('start kill process','greenText') + ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 pid = getPid(ssh) if pid : From a89369aed240cf120bf51272b15f1ba94cff75a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 17:51:56 +0800 Subject: [PATCH 459/890] bug fix --- pub-daily.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index af3d0b17..b06a9e1b 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -89,7 +89,7 @@ def exe(self,cmd,isprint=True): print stderr.read() return stdin, stdout, stderr - def telnet(self, cmd): + def telnet(self, cmd,isprint=True): chan = self.client.get_transport().open_session(timeout=10) chan.exec_command(cmd) @@ -99,7 +99,8 @@ def telnet(self, cmd): while True: if chan.recv_ready(): - print_out_stream(chan.recv(1024)) + if isprint: + print_out_stream(chan.recv(1024)) is_recv = True else: if is_recv: @@ -112,8 +113,8 @@ def close(self): if self.client: self.client.close() -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD,False) +def getPid(keyword,ssh): + stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) return stdout.read().strip() def showText(s, typ): if typ == 'RED': @@ -186,23 +187,25 @@ def main(): print showText('backup mpush ok','greenText') ## remove zk info - telnet = ssh.telnet('telnet 127.0.0.1 4001') + telnet = ssh.telnet('telnet 127.0.0.1 4001',False) telnet.send(' ',False) telnet.send('rcs') ## 删除zk telnet.send('quit') ## 关闭连接 + pid = getPid('telnet',ssh) + if pid : + ssh.exe('kill -9 %s'%pid) - time.sleep(30) print showText('start kill process','greenText') ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(ssh) + pid = getPid(PROCESS_KEY_WORD,ssh) if pid : ssh.exe('kill %s'%pid) time.sleep(15) else: print showText('there is no process to kill','YELLOW') - pid = getPid(ssh) + pid = getPid(PROCESS_KEY_WORD,ssh) if pid: ssh.exe('kill -9 %s'%pid) From 7e6912b0acbdb85baf03b716e3cfedb10336b938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 18:09:03 +0800 Subject: [PATCH 460/890] bug fix --- pub-daily.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index b06a9e1b..305063fa 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -187,13 +187,16 @@ def main(): print showText('backup mpush ok','greenText') ## remove zk info - telnet = ssh.telnet('telnet 127.0.0.1 4001',False) - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - pid = getPid('telnet',ssh) - if pid : - ssh.exe('kill -9 %s'%pid) + try: + telnet = ssh.telnet('telnet 127.0.0.1 4001') + telnet.send(' ',False) + telnet.send('rcs') ## 删除zk + telnet.send('quit') ## 关闭连接 + pid = getPid('telnet',ssh) + if pid : + ssh.exe('kill -9 %s'%pid) + except: + print showText('telnet exception','redText') print showText('start kill process','greenText') From 6dbba10224c1bab5b095dacbf64fc2de97313a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 18:11:54 +0800 Subject: [PATCH 461/890] bug fix --- pub-daily.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pub-daily.py b/pub-daily.py index 305063fa..ec956d20 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -188,7 +188,7 @@ def main(): ## remove zk info try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') + telnet = ssh.telnet('telnet 127.0.0.1 4001',False) telnet.send(' ',False) telnet.send('rcs') ## 删除zk telnet.send('quit') ## 关闭连接 From f3b0c42d830ff4f9cf0952bdee8e21ea7d7ce21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 18:13:57 +0800 Subject: [PATCH 462/890] bug fix --- pub-daily.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index ec956d20..f18eb721 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -188,10 +188,10 @@ def main(): ## remove zk info try: - telnet = ssh.telnet('telnet 127.0.0.1 4001',False) + telnet = ssh.telnet('telnet 127.0.0.1 4001') telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 + telnet.send('rcs',False) ## 删除zk + telnet.send('quit',False) ## 关闭连接 pid = getPid('telnet',ssh) if pid : ssh.exe('kill -9 %s'%pid) From 0957cb7ebb8e9d6aaf35f91e7457eeab54f63562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 1 Mar 2016 19:02:22 +0800 Subject: [PATCH 463/890] bug fix --- pub-daily.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pub-daily.py b/pub-daily.py index f18eb721..dedf28ee 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -190,11 +190,8 @@ def main(): try: telnet = ssh.telnet('telnet 127.0.0.1 4001') telnet.send(' ',False) - telnet.send('rcs',False) ## 删除zk - telnet.send('quit',False) ## 关闭连接 - pid = getPid('telnet',ssh) - if pid : - ssh.exe('kill -9 %s'%pid) + telnet.send('rcs') ## 删除zk + telnet.send('quit') ## 关闭连接 except: print showText('telnet exception','redText') From bd60d73d6ca10f643194f1f9aa67faaffa2007ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 2 Mar 2016 09:14:24 +0800 Subject: [PATCH 464/890] add sleep --- pub-daily.py | 10 +++++++++- test.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pub-daily.py b/pub-daily.py index dedf28ee..0e163036 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -150,6 +150,14 @@ def print_out_stream(s): """打印执行命令的结果""" sys.stdout.write(greenText(s)) +def sleep(checkCount): + while(checkCount>1): + checkCount = checkCount - 1 + sys.stdout.write(greenText(' . ')) + sys.stdout.flush() + time.sleep(1) + print greenText(' . ') + def runShell(c): print c os.system(c) @@ -202,7 +210,7 @@ def main(): pid = getPid(PROCESS_KEY_WORD,ssh) if pid : ssh.exe('kill %s'%pid) - time.sleep(15) + sleep(15) else: print showText('there is no process to kill','YELLOW') pid = getPid(PROCESS_KEY_WORD,ssh) diff --git a/test.py b/test.py index a9dd11e2..92bc5e04 100755 --- a/test.py +++ b/test.py @@ -4,6 +4,27 @@ import datetime import os import sys +import time str = raw_input("请输入:"); print "你输入的内容是: ", str + +def redText(s): + return "\033[1;31m%s\033[0m" % s + +def greenText(s): + return "\033[1;32m%s\033[0m" % s + + +def yellowText(s): + return "\033[1;33m%s\033[0m" % s + +def sleep(checkCount): + while(checkCount>1): + checkCount = checkCount - 1 + sys.stdout.write(greenText(' . ')) + sys.stdout.flush() + time.sleep(1) + print greenText(' . ') + +sleep(3) From 0cf982029a905e020e8313e25cd99c898c875efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 2 Mar 2016 13:50:50 +0800 Subject: [PATCH 465/890] bug fix --- .../mpush/common/manage/user/UserManager.java | 41 +++++++------------ .../mpush/core/router/UserManager.java | 9 +--- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index fdecf413..3920e2dc 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -6,39 +6,36 @@ import org.slf4j.LoggerFactory; import com.shinemo.mpush.api.RedisKey; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.manage.RedisManage; //查询使用 public class UserManager { + private static final String EXTRANET_ADDRESS = MPushUtil.getExtranetAddress(); + + private static final String ONLINE_KEY = RedisKey.getConnNum(EXTRANET_ADDRESS); + private static final Logger log = LoggerFactory.getLogger(UserManager.class); + public void init(){ + RedisManage.del(ONLINE_KEY); + log.info("init redis key:{}"+ONLINE_KEY); + } + public void userOnline(String userId) { - String onlineKey = RedisKey.getUserOnlineKey(); - RedisManage.zAdd(onlineKey, userId); - String offlineKey = RedisKey.getUserOfflineKey(); - RedisManage.zRem(offlineKey, userId); + RedisManage.zAdd(ONLINE_KEY, userId); log.info("user online {}",userId); } public void userOffline(String userId) { - String onlineKey = RedisKey.getUserOnlineKey(); - RedisManage.zRem(onlineKey, userId); - String offlineKey = RedisKey.getUserOfflineKey(); - RedisManage.zAdd(offlineKey, userId); + RedisManage.zRem(ONLINE_KEY, userId); log.info("user offline {}",userId); } //在线用户 public long onlineUserNum(){ - String onlineKey = RedisKey.getUserOnlineKey(); - return RedisManage.zCard(onlineKey); - } - - //离线用户 - public long offlineUserNum(){ - String offlineKey = RedisKey.getUserOfflineKey(); - return RedisManage.zCard(offlineKey); + return RedisManage.zCard(ONLINE_KEY); } //在线用户列表 @@ -46,17 +43,7 @@ public List onlineUserList(int start,int size){ if(size<10){ size = 10; } - String onlineKey = RedisKey.getUserOnlineKey(); - return RedisManage.zrange(onlineKey, start, size-1, String.class); + return RedisManage.zrange(ONLINE_KEY, start, size-1, String.class); } - //离线用户 - public List offlineUserList(int start,int size){ - if(size<10){ - size = 10; - } - String offlineKey = RedisKey.getUserOfflineKey(); - return RedisManage.zrange(offlineKey, start, size-1, String.class); - } - } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java index 3b18c314..73304ab1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java @@ -1,11 +1,9 @@ package com.shinemo.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.api.event.UserOnlineEvent; import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.redis.manage.RedisManage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,19 +18,15 @@ public final class UserManager extends com.shinemo.mpush.common.manage.user.User public static final String OFFLINE_CHANNEL = "/mpush/offline/"; - private static final String EXTRANET_ADDRESS = MPushUtil.getExtranetAddress(); - - private static final String ONLINE_KEY = RedisKey.getConnNum(EXTRANET_ADDRESS); - public UserManager() { EventBus.INSTANCE.register(this); + init(); } @Subscribe void handlerUserOnlineEvent(UserOnlineEvent event) { userOnline(event.getUserId()); RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); - RedisManage.incrBy(ONLINE_KEY, 1); } @Subscribe @@ -52,6 +46,5 @@ void handlerUserOfflineEvent(UserOfflineEvent event) { } userOffline(event.getUserId()); RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); - RedisManage.incrBy(ONLINE_KEY, -1); } } From f4ce4c423edee9075d0767305337d87c61813571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 2 Mar 2016 14:38:50 +0800 Subject: [PATCH 466/890] bug fix --- .../main/java/com/shinemo/mpush/api/RedisKey.java | 12 +++--------- .../mpush/common/manage/user/UserManager.java | 2 +- .../shinemo/mpush/test/redis/RedisUtilTest.java | 14 +++++++------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java index 38c6b9b8..515afb95 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -8,9 +8,7 @@ public class RedisKey { private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp_f_c_d_"; - private static final String USER_ONLINE_KEY = "mp_u_ol"; - - private static final String USER_OFFLINE_KEY = "mp_u_ofl"; + private static final String USER_ONLINE_KEY = "mp_u_ol_"; private static final String CONN_NUM_ = "mp_cn_"; @@ -27,12 +25,8 @@ public static final String getDeviceIdKey(String deviceId){ return FAST_CONNECTION_DEVICE_PREFIX+deviceId; } - public static final String getUserOnlineKey(){ - return USER_ONLINE_KEY; - } - - public static final String getUserOfflineKey(){ - return USER_OFFLINE_KEY; + public static final String getUserOnlineKey(String extranetAddress){ + return USER_ONLINE_KEY+extranetAddress; } public static final String getConnNum(String extranetAddress){ diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index 3920e2dc..7f17f3f5 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -14,7 +14,7 @@ public class UserManager { private static final String EXTRANET_ADDRESS = MPushUtil.getExtranetAddress(); - private static final String ONLINE_KEY = RedisKey.getConnNum(EXTRANET_ADDRESS); + private static final String ONLINE_KEY = RedisKey.getUserOnlineKey(EXTRANET_ADDRESS); private static final Logger log = LoggerFactory.getLogger(UserManager.class); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java index a0151360..6a22adcf 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java @@ -177,24 +177,24 @@ public void hashTest(){ @Test public void testSet(){ - System.out.println(RedisUtil.sCard(node, RedisKey.getUserOnlineKey())); +// System.out.println(RedisUtil.sCard(node, RedisKey.getUserOnlineKey())); - List onlineUserIdList = RedisUtil.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); - System.out.println(onlineUserIdList.size()); +// List onlineUserIdList = RedisUtil.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); +// System.out.println(onlineUserIdList.size()); } @Test public void testlist(){ - RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); +// RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); } @Test public void testsortedset(){ - RedisUtil.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); +// RedisUtil.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); - long len =RedisUtil.zCard(node, RedisKey.getUserOnlineKey()); - System.out.println(len); +// long len =RedisUtil.zCard(node, RedisKey.getUserOnlineKey()); +// System.out.println(len); } } From 787d19dc9ef638b4d0c486f092b5f10147548a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 2 Mar 2016 20:25:17 +0800 Subject: [PATCH 467/890] bug fix --- .../mpush/push/manage/impl/ConnectionServerManage.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java index 087707bb..a26d02e8 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.slf4j.Logger; @@ -21,7 +22,12 @@ public class ConnectionServerManage implements ServerManage Date: Thu, 3 Mar 2016 09:51:55 +0800 Subject: [PATCH 468/890] bug fix --- test.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/test.py b/test.py index 92bc5e04..8a624cd8 100755 --- a/test.py +++ b/test.py @@ -1,13 +1,47 @@ -#!/usr/bin/python # coding=utf8 +import paramiko import datetime +import telnetlib import os import sys -import time -str = raw_input("请输入:"); -print "你输入的内容是: ", str + +class SSH(): + def __init__(self): + self.client = None + + def connect(self,host,port=9092,username='shinemo',password=None): + self.client = paramiko.SSHClient() + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.connect(host, port, username=username, password=password, timeout=120) + return self + + def exe(self,cmd,isprint=True): + if not cmd: + return + print greenText(cmd) + stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) + if isprint: + for std in stdout.readlines(): + print std, + print stderr.read() + return stdin, stdout, stderr + + + def close(self): + if self.client: + self.client.close() + +def showText(s, typ): + if typ == 'RED': + return redText(s) + elif typ == 'GREEN': + return greenText(s) + elif typ == 'YELLOW': + return yellowText(s) + else: + return s def redText(s): return "\033[1;31m%s\033[0m" % s @@ -19,12 +53,17 @@ def greenText(s): def yellowText(s): return "\033[1;33m%s\033[0m" % s -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') +def runShell(c): + print c + os.system(c) + +def main(): + + + confirmPub = raw_input("") + + print(confirmPub) + -sleep(3) +if __name__ == "__main__": + main() From 1f66f81498bd318d562050930b38f9d6b2ed34ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 11:00:13 +0800 Subject: [PATCH 469/890] =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-pre.properties | 6 +- .../netty/connection/NettyConnection.java | 3 +- .../connection/NettyConnectionManager.java | 3 - .../mpush/netty/server/NettyServer.java | 12 ++-- .../server/ScanAllConnectionTimerTask.java | 68 ------------------- 5 files changed, 9 insertions(+), 83 deletions(-) delete mode 100644 mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java diff --git a/conf-pre.properties b/conf-pre.properties index ce9c28f8..1bf56628 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -1,15 +1,15 @@ #日志根目录 log.home=/opt/logs/mpush -loglevel=INFO +loglevel=DEBUG zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 zk_digest = shinemoIpo zk_namespace = mpush-pre redis_group = 10.1.80.57:6379:shinemoIpo private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info = false +force_write_redis_group_info = true connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 +dns_mapping=115.29.230.2=127.0.0.1 skip_dump=false admin_port=4001 diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java index 56948f52..9026d679 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java @@ -2,9 +2,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.event.ConnectionCloseEvent; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.security.CipherBox; import io.netty.channel.Channel; @@ -69,6 +67,7 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { } } else { return this.close(); + } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java index f6dbf581..13a5e224 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java @@ -6,7 +6,6 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.event.HandshakeEvent; -import com.shinemo.mpush.api.event.UserOfflineEvent; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; @@ -17,8 +16,6 @@ import io.netty.util.Timer; import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - -import java.nio.ByteBuffer; import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java index 67d1c890..7a76da40 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java @@ -24,7 +24,7 @@ */ public abstract class NettyServer implements Server { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class); + private static final Logger log = LoggerFactory.getLogger(NettyServer.class); public enum State {Created, Initialized, Starting, Started, Shutdown} @@ -36,7 +36,6 @@ public enum State {Created, Initialized, Starting, Started, Shutdown} public NettyServer(int port) { this.port = port; - } public void init() { @@ -57,7 +56,7 @@ public void stop(Listener listener) { } if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); - LOGGER.warn("netty server stop now"); + log.error("netty server stop now"); } @Override @@ -130,15 +129,14 @@ public void initChannel(SocketChannel ch) throws Exception { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - LOGGER.info("server start success on:" + port); + log.error("server start success on:" + port); if (listener != null) listener.onSuccess(); } else { - LOGGER.error("server start failure on:" + port); + log.error("server start failure on:" + port); if (listener != null) listener.onFailure("start server failure"); } } }); -// f.await(); if (f.isSuccess()) { serverState.set(State.Started); /** @@ -148,7 +146,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } } catch (Exception e) { - LOGGER.error("server start exception", e); + log.error("server start exception", e); if (listener != null) listener.onFailure("start server ex=" + e.getMessage()); throw new RuntimeException("server start exception, port=" + port, e); } finally { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java deleted file mode 100644 index cfea6967..00000000 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/ScanAllConnectionTimerTask.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.shinemo.mpush.netty.server; - -import java.util.List; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.ConnectionManager; -import com.shinemo.mpush.netty.util.NettySharedHolder; -import com.shinemo.mpush.tools.config.ConfigCenter; - -import io.netty.util.Timeout; -import io.netty.util.TimerTask; - -public class ScanAllConnectionTimerTask implements TimerTask{ - - private static final Logger log = LoggerFactory.getLogger(ScanAllConnectionTimerTask.class); - - private ConnectionManager connectionManager; - - private static int maxHBTimeoutTimes = ConfigCenter.holder.maxHBTimeoutTimes(); - - // 180S也许是个不错的选择,微信的心跳时间为300S - private static long scanConnTaskCycle = ConfigCenter.holder.scanConnTaskCycle(); - - public ScanAllConnectionTimerTask(ConnectionManager connectionManager) { - this.connectionManager = connectionManager; - } - - @Override - public void run(Timeout timeout) throws Exception { - - try { - long now = System.currentTimeMillis(); - List connections = connectionManager.getConnections(); - log.warn("start deal ScanAllConnectionTimerTask:size,"+connections.size()); - if (connections != null) { - for (final Connection conn : connections) { - if (!conn.isConnected()) { - log.warn("connect is not connected: "+conn); - return; - } - long betwen = now - conn.getLastReadTime(); - if (betwen > scanConnTaskCycle) { - int expiredTimes = conn.inceaseAndGetHbTimes(); - if (expiredTimes > maxHBTimeoutTimes) { - conn.close(); - log.error("connection heartbeat timeout, connection has bean closed:"+conn); - } else { - log.error("connection heartbeat timeout, expiredTimes=" + expiredTimes+","+conn); - } - } else { - conn.resetHbTimes(); - log.warn("connection heartbeat reset, expiredTimes=0,betwen:"+betwen+","+conn+",lastReadTime:"+conn.getLastReadTime()+",now:"+now); - } - } - } - } catch (Throwable e) { - log.error("error during ScanAllConnectionTimerTask", e); - } finally { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, scanConnTaskCycle/1000, TimeUnit.SECONDS); - } - - } - -} From 0143d4e51e3ab6d21afa9f914eaf5004f09d7cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 11:04:54 +0800 Subject: [PATCH 470/890] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=A2=84=E5=8F=91?= =?UTF-8?q?=E4=B8=8E=E7=BA=BF=E4=B8=8A=E7=9A=84=E7=A7=81=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-online.properties | 4 ++-- conf-pre.properties | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conf-online.properties b/conf-online.properties index 04293547..0c2cd8d5 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -5,8 +5,8 @@ zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 zk_digest = shinemoIpo zk_namespace = mpush-online redis_group = 10.1.80.57:6379:shinemoIpo -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +private_key = MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g= +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 diff --git a/conf-pre.properties b/conf-pre.properties index 1bf56628..4db481d3 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -5,8 +5,8 @@ zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 zk_digest = shinemoIpo zk_namespace = mpush-pre redis_group = 10.1.80.57:6379:shinemoIpo -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +private_key = MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA== +public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB force_write_redis_group_info = true connection_server_port = 3000 gateway_server_port = 4000 From c1194cc38ab67f6976915a6670d2eb03e07cb920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 11:09:46 +0800 Subject: [PATCH 471/890] bug fix --- conf-daily.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index f7bc1dfc..379dab97 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -8,8 +8,8 @@ redis_group = 111.1.57.148:6379:ShineMoIpo private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info = true -connection_server_port = 3000 +connection_server_port = 20882 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1 skip_dump=true admin_port=4001 From ec7cf5a03549c65813791d4f3ecfa5dfb34824b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 13:35:12 +0800 Subject: [PATCH 472/890] add pre py --- pub-pre.py | 61 +++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 51 deletions(-) diff --git a/pub-pre.py b/pub-pre.py index d75a3fc3..06b81589 100644 --- a/pub-pre.py +++ b/pub-pre.py @@ -6,18 +6,6 @@ import os import sys -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] BASEPATH = '/root/mpush' @@ -25,7 +13,7 @@ GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' -ENV= 'daily' +ENV= 'pre' class SSH(): @@ -95,46 +83,17 @@ def main(): if confirmPub != 'Y': return - for item in HOSTS: + ##4 cp + runShell('cp %s %s'%(GITLABPATH,BASEPATH)) + print showText('cp success','greenText') - pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) - if pubHost != 'Y': - return + ##5 tar package + runShell('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz') + print showText('tar success','greenText') - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##2 backup - base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' - to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ##telnet remove zk info - #ssh.exe('telent 127.0.0.1 4001') - #ssh.exe('') - - ##3 kill process - pid = getPid(ssh) - - if pid : - ssh.exe('kill -9 %s'%pid) - else: - print showText('there is no mpush-cs process to kill','YELLOW') - - ##4 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) - print showText('scp success','greenText') - - ##5 tar package - ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) - print showText('tar success','greenText') - - ##6 start process - ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') - print showText('start process success','greenText') - - - ssh.close() + ##6 start process + runShell('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') + print showText('start process success','greenText') if __name__ == "__main__": From c21d396c667090a5a22a2d0e05a449d314023d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 13:44:30 +0800 Subject: [PATCH 473/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9zk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-online.properties | 4 ++-- conf-pre.properties | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/conf-online.properties b/conf-online.properties index 0c2cd8d5..c69b1d56 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -1,10 +1,10 @@ #日志根目录 log.home=/opt/logs/mpush -loglevel=INFO +loglevel=debug zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 zk_digest = shinemoIpo zk_namespace = mpush-online -redis_group = 10.1.80.57:6379:shinemoIpo +redis_group = 10.161.223.238:6379:ShineMoIpo private_key = MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g= public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB force_write_redis_group_info = false diff --git a/conf-pre.properties b/conf-pre.properties index 4db481d3..c022613c 100644 --- a/conf-pre.properties +++ b/conf-pre.properties @@ -4,7 +4,7 @@ loglevel=DEBUG zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 zk_digest = shinemoIpo zk_namespace = mpush-pre -redis_group = 10.1.80.57:6379:shinemoIpo +redis_group = 10.161.223.238:6379:ShineMoIpo private_key = MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA== public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB force_write_redis_group_info = true From 935845427652873981a047a2c8dbcf7b609310a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 14:30:03 +0800 Subject: [PATCH 474/890] bug fix --- pub-online.py | 159 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 31 deletions(-) diff --git a/pub-online.py b/pub-online.py index d75a3fc3..4404c4f5 100644 --- a/pub-online.py +++ b/pub-online.py @@ -5,6 +5,7 @@ import telnetlib import os import sys +import time HOSTS = [ { @@ -21,12 +22,51 @@ BASEPATH = '/root/mpush' -STARTPROCESS = 'java -jar mpush-cs.jar' - -GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' - -ENV= 'daily' - +MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz' + +PROCESS_KEY_WORD = 'mpush-cs.jar' + +GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME + +JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' + +ENV= 'online' + +class Telnet(object): + def __init__(self, chan): + self.chan = chan + + def send(self, cmd,isprint=True): + self.chan.send(cmd+'\n') + print_cmd(cmd) + out = '' + is_recv = False + is_recv_err = False + while True: + # 结束 + if self.chan.recv_stderr_ready(): + tmp = self.chan.recv_stderr(1024) + if isprint: + print_out_stream(tmp) + out += tmp + is_recv_err = True + else: + if is_recv_err: + return out + else: + time.sleep(0.1) + + if self.chan.recv_ready(): + tmp = self.chan.recv(1024) + if isprint: + print_out_stream(tmp) + out += tmp + is_recv = True + else: + if is_recv: + return out + else: + time.sleep(0.1) class SSH(): def __init__(self): @@ -42,20 +82,39 @@ def exe(self,cmd,isprint=True): if not cmd: return print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) + stdin, stdout, stderr = self.client.exec_command(cmd) if isprint: for std in stdout.readlines(): print std, print stderr.read() return stdin, stdout, stderr + def telnet(self, cmd,isprint=True): + chan = self.client.get_transport().open_session(timeout=10) + chan.exec_command(cmd) + + t = Telnet(chan) + + is_recv = False + + while True: + if chan.recv_ready(): + if isprint: + print_out_stream(chan.recv(1024)) + is_recv = True + else: + if is_recv: + return t + else: + time.sleep(0.1) + def close(self): if self.client: self.client.close() -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) +def getPid(keyword,ssh): + stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) return stdout.read().strip() def showText(s, typ): if typ == 'RED': @@ -77,60 +136,98 @@ def greenText(s): def yellowText(s): return "\033[1;33m%s\033[0m" % s +def print_cmd(s): + """打印执行的命令""" + print yellowText(s) + + +def print_out(s): + """打印执行命令的结果""" + print greenText(s) + + +def print_out_stream(s): + """打印执行命令的结果""" + sys.stdout.write(greenText(s)) + +def sleep(checkCount): + while(checkCount>1): + checkCount = checkCount - 1 + sys.stdout.write(greenText(' . ')) + sys.stdout.flush() + time.sleep(1) + print greenText(' . ') + def runShell(c): print c os.system(c) def main(): - ##0 assembly + ##0 git pull + runShell('git pull origin master') + print showText('git pull master success','greenText') + + ##1 assembly runShell('mvn clean install assembly:assembly -P %s'%ENV) print showText('assembly success','greenText') - ##1 包创建时间 + ##2 包创建时间 runShell('stat -c "%%y" %s'%GITLABPATH) - confirmPub = raw_input("确认发布(Y/N):") + confirmPub = raw_input("确认发布(y/n):") - if confirmPub != 'Y': + if confirmPub != 'y': return for item in HOSTS: - pubHost = raw_input("发布 %s (Y/N):"%item['HOST']) - if pubHost != 'Y': + pubHost = raw_input("发布 %s (y/n):"%item['HOST']) + if pubHost != 'y': return ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - ##2 backup - base = BASEPATH+'/mpush-jar-with-dependency.tar.gz' - to = BASEPATH+'/back/mpush-jar-with-dependency.tar.gz.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ##3 backup + base = BASEPATH+'/'+MPUSH_TAR_NAME + to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') ssh.exe('mv %s %s '%(base,to)) print showText('backup mpush ok','greenText') - ##telnet remove zk info - #ssh.exe('telent 127.0.0.1 4001') - #ssh.exe('') + ## remove zk info + try: + telnet = ssh.telnet('telnet 127.0.0.1 4001') + telnet.send(' ',False) + telnet.send('rcs') ## 删除zk + telnet.send('quit') ## 关闭连接 + except: + print showText('telnet exception','redText') - ##3 kill process - pid = getPid(ssh) + print showText('start kill process','greenText') + + ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 + pid = getPid(PROCESS_KEY_WORD,ssh) if pid : - ssh.exe('kill -9 %s'%pid) + ssh.exe('kill %s'%pid) + sleep(15) else: - print showText('there is no mpush-cs process to kill','YELLOW') + print showText('there is no process to kill','YELLOW') + pid = getPid(PROCESS_KEY_WORD,ssh) + if pid: + ssh.exe('kill -9 %s'%pid) + - ##4 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],'/root/mpush')) + ##5 scp + runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) print showText('scp success','greenText') - ##5 tar package - ssh.exe('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz',False) + ##6 tar package + ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) print showText('tar success','greenText') - ##6 start process - ssh.exe('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') + ##7 start process + ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) print showText('start process success','greenText') From 89905734b4af5eebf59a441d135752b8fe07560a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 4 Mar 2016 14:30:50 +0800 Subject: [PATCH 475/890] bug fix --- pub-online.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 pub-online.py diff --git a/pub-online.py b/pub-online.py old mode 100644 new mode 100755 From 53da431b7cf792cb4de05d3fa7967e581284c7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 6 Mar 2016 02:45:44 +0000 Subject: [PATCH 476/890] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/api/RedisKey.java | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java index 515afb95..ba46e0c0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -1,37 +1,37 @@ package com.shinemo.mpush.api; -public class RedisKey { - - private static final String USER_PREFIX = "mp_u_"; - - private static final String SESSION_PREFIX = "mp_s_"; - - private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp_f_c_d_"; - - private static final String USER_ONLINE_KEY = "mp_u_ol_"; - - private static final String CONN_NUM_ = "mp_cn_"; - - public static final String getUserKey(String userId){ - return USER_PREFIX+userId; - } - - public static final String getSessionKey(String sessionId){ - return SESSION_PREFIX + sessionId; - } - - //for fast connection test - public static final String getDeviceIdKey(String deviceId){ - return FAST_CONNECTION_DEVICE_PREFIX+deviceId; - } - - public static final String getUserOnlineKey(String extranetAddress){ - return USER_ONLINE_KEY+extranetAddress; - } - - public static final String getConnNum(String extranetAddress){ - return CONN_NUM_+extranetAddress; - } - +public final class RedisKey { + + private static final String USER_PREFIX = "mp_u_"; + + private static final String SESSION_PREFIX = "mp_s_"; + + private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp_f_c_d_"; + + private static final String USER_ONLINE_KEY = "mp_u_ol_"; + + private static final String CONN_NUM_ = "mp_cn_"; + + public static final String getUserKey(String userId) { + return USER_PREFIX + userId; + } + + public static final String getSessionKey(String sessionId) { + return SESSION_PREFIX + sessionId; + } + + //for fast connection test + public static final String getDeviceIdKey(String deviceId) { + return FAST_CONNECTION_DEVICE_PREFIX + deviceId; + } + + public static final String getUserOnlineKey(String extranetAddress) { + return USER_ONLINE_KEY + extranetAddress; + } + + public static final String getConnNum(String extranetAddress) { + return CONN_NUM_ + extranetAddress; + } + } From be092b6fb0e3b7f1e37df56add7cbd9d1d915144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 7 Mar 2016 10:57:48 +0800 Subject: [PATCH 477/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 7 ++++--- mpush-client/pom.xml | 13 +++++++------ mpush-common/pom.xml | 15 +++++++++------ mpush-core/pom.xml | 13 +++++++------ mpush-cs/pom.xml | 15 +++++++-------- mpush-log/pom.xml | 14 ++++++-------- mpush-monitor/pom.xml | 15 ++++++++------- mpush-netty/pom.xml | 13 +++++++------ mpush-test/pom.xml | 12 ++++++------ mpush-tools/pom.xml | 13 +++++++------ pom.xml | 42 +++++++++++++++++++++--------------------- 11 files changed, 89 insertions(+), 83 deletions(-) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 0ed22fd5..c4bb6f8e 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -5,12 +5,13 @@ mpush com.shinemo.mpush - 1.0-SNAPSHOT + 0.0.0.2 + ../pom.xml 4.0.0 mpush-api - ${mpush-api-version} + mpush-api jar @@ -19,5 +20,5 @@ netty-transport - + diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 5a93b22c..d41af302 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -2,15 +2,16 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-client - ${mpush-client-version} + mpush-client jar diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index ceef2259..9288b544 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -2,15 +2,18 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-common - ${mpush-common-version} + mpush-common + jar + com.shinemo.mpush diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 9d73a0a6..e2002c48 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -2,15 +2,16 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-core - ${mpush-core-version} + mpush-core jar diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml index 0cec0c04..d8de1009 100644 --- a/mpush-cs/pom.xml +++ b/mpush-cs/pom.xml @@ -1,16 +1,15 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 - ${mpush-cs-version} mpush-cs - jar - mpush-cs + jar diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml index 50a54473..6328df79 100644 --- a/mpush-log/pom.xml +++ b/mpush-log/pom.xml @@ -1,18 +1,16 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-log - ${mpush-log-version} jar - mpush-log - http://maven.apache.org UTF-8 diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index bad2d960..2fa7ce7e 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -1,18 +1,19 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-monitor jar - ${mpush-monitor-version} - mpush-monitor + + com.google.code.gson diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 3a4f6731..05f1ffdd 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -2,16 +2,17 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-netty - ${mpush-netty-version} jar + mpush-netty diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 8e089fca..d143cc6f 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -1,15 +1,15 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-test jar - ${mpush-test-version} mpush-test diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index dae24b03..77c4d16e 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -2,16 +2,17 @@ - - mpush - com.shinemo.mpush - 1.0-SNAPSHOT - + + mpush + com.shinemo.mpush + 0.0.0.2 + ../pom.xml + 4.0.0 mpush-tools - ${mpush-tools-version} jar + mpush-tools com.google.code.gson diff --git a/pom.xml b/pom.xml index 752ca3ef..3aa4bfd8 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.shinemo.mpush mpush pom - 1.0-SNAPSHOT + 0.0.0.2 mpush-api mpush-core @@ -105,54 +105,54 @@ - com.shinemo.mpush + ${pom.groupId} mpush-api - ${mpush-api-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-tools - ${mpush-tools-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-common - ${mpush-common-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-netty - ${mpush-netty-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-core - ${mpush-core-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-client - ${mpush-client-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-monitor - ${mpush-monitor-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-log - ${mpush-log-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-test - ${mpush-test-version} + ${project.version} - com.shinemo.mpush + ${pom.groupId} mpush-cs - ${mpush-cs-version} + ${project.version} From 089c1156518a3eafb3a01895d5373af4624af081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 7 Mar 2016 15:04:53 +0800 Subject: [PATCH 478/890] bug fix --- .../com/shinemo/mpush/core/router/RouterChangeListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index fca1d298..3d1dc5c7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -123,7 +123,7 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); LocalRouter router = routerManager.lookup(userId); if (router != null) { - LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, msg=%s", msg); + LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, msg={}", msg); //2.1删除本地路由信息 routerManager.unRegister(userId); //2.2发送踢人消息到客户端 From 2081541c90f0e7cae47e5434b499f7d7e7602855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Mon, 7 Mar 2016 15:17:12 +0800 Subject: [PATCH 479/890] add lead tack --- pub-online.py | 2 +- pub-pre.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pub-online.py b/pub-online.py index 4404c4f5..56454b6f 100755 --- a/pub-online.py +++ b/pub-online.py @@ -227,7 +227,7 @@ def main(): print showText('tar success','greenText') ##7 start process - ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) + ssh.exe('nohup %s -Dio.netty.leakDetectionLevel=advanced -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) print showText('start process success','greenText') diff --git a/pub-pre.py b/pub-pre.py index 06b81589..ca51ecdc 100644 --- a/pub-pre.py +++ b/pub-pre.py @@ -92,7 +92,7 @@ def main(): print showText('tar success','greenText') ##6 start process - runShell('nohup /opt/shinemo/jdk1.7.0_40/bin/java -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') + runShell('nohup /opt/shinemo/jdk1.7.0_40/bin/java -Dio.netty.leakDetectionLevel=advanced -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') print showText('start process success','greenText') From 478b8c6fba807708da37e83ad03b96c15ebdd473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 11:22:34 +0800 Subject: [PATCH 480/890] add body log --- .../main/java/com/shinemo/mpush/common/MessageDispatcher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java index 643f9887..bdf6dc98 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java @@ -31,7 +31,7 @@ public void onReceive(Packet packet, Connection connection) { handler.handle(packet, connection); } } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={},conn={}", packet, connection, throwable); + LOGGER.error("dispatch packet ex, packet={},conn={},body={}", packet, connection, packet.body); connection.close(); } } From efd6cff147b797c239a270129c37b43469925d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 13:12:19 +0800 Subject: [PATCH 481/890] add handshake message --- .../mpush/test/connection/body/BodyTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java new file mode 100644 index 00000000..0edd8670 --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java @@ -0,0 +1,74 @@ +package com.shinemo.mpush.test.connection.body; + +import java.security.interfaces.RSAPrivateKey; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.junit.Before; +import org.junit.Test; + +import com.shinemo.mpush.common.message.HandshakeMessage; +import com.shinemo.mpush.tools.crypto.RSAUtils; + +public class BodyTest { + + private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; + private String daily_allocServer = "http://111.1.57.148/mpns/push/server/"; + private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; + private RSAPrivateKey dailyPrivateKey = null; + + private String pre_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB"; + private String pre_allocServer = "http://115.29.230.2/mpns/push/server/"; + private String pre_privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA=="; + private RSAPrivateKey prePrivateKey = null; + + private String online_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB"; + private String online_allocServer = "http://allot.mangguoyisheng.com/"; + private String online_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g="; + private RSAPrivateKey onlinePrivateKey = null; + + String rawData = "3, 37, -3, -15, 22, -68, 4, 61, -95, -108, 94, 36, -104, 6, -76, 34, 107, -76, 63, 127, -114, -97, 97, -80, -74, 122, 84, 90, -67, 7, -116, 16, -75, 95, 126, 119, 125, -71, -106, 121, -100, -121, -76, 28, 63, -2, 105, 84, -123, 113, 18, -93, 108, -18, 73, -1, 40, 65, -67, -17, -79, -44, 9, -85, 97, -99, -30, 45, -19, -62, -6, -48, 66, -101, 6, 34, -87, -71, -80, 74, -82, -49, -71, -98, -121, -24, -21, -93, 15, 100, -101, 106, -48, -22, -35, -15, -61, -102, 123, -117, 48, -45, 41, -109, -57, -79, 87, -24, -11, -18, -53, -126, -115, -24, 0, 19, 38, -99, 107, 118, -100, -37, -56, -105, 8, 91, 2, -83"; + + byte[]data = null; + + @Before + public void init(){ + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); + onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); + String[] temp = rawData.split(","); + data = new byte[temp.length]; + for(int i = 0;i Date: Tue, 8 Mar 2016 13:51:45 +0800 Subject: [PATCH 482/890] add profile dump --- .../com/shinemo/mpush/common/Profiler.java | 499 ++++++++++++++++++ .../common/handler/BaseMessageHandler.java | 8 +- .../mpush/common/message/BaseMessage.java | 17 +- .../mpush/common/security/RsaCipher.java | 16 +- .../core/server/ServerChannelHandler.java | 20 +- .../mpush/test/connection/body/BodyTest.java | 2 +- 6 files changed, 549 insertions(+), 13 deletions(-) create mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java new file mode 100644 index 00000000..571efdb4 --- /dev/null +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java @@ -0,0 +1,499 @@ +package com.shinemo.mpush.common; + + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; + +/** + * 用来测试并统计线程执行时间的工具。 + */ +@SuppressWarnings(value={"rawtypes","unchecked"}) +public class Profiler { + + private static final ThreadLocal entryStack = new ThreadLocal(); + public static final String EMPTY_STRING = ""; + + /** + * 开始计时。 + */ + public static void start() { + start((String) null); + } + + /** + * 开始计时。 + * + * @param message 第一个entry的信息 + */ + + public static void start(String message) { + entryStack.set(new Entry(message, null, null)); + } + + /** + * 开始计时。 + * + * @param message 第一个entry的信息 + */ + public static void start(Message message) { + entryStack.set(new Entry(message, null, null)); + } + + /** + * 清除计时器。 + *

+ *

+ * 清除以后必须再次调用start方可重新计时。 + *

+ */ + public static void reset() { + entryStack.set(null); + } + + /** + * 开始一个新的entry,并计时。 + * + * @param message 新entry的信息 + */ + public static void enter(String message) { + Entry currentEntry = getCurrentEntry(); + + if (currentEntry != null) { + currentEntry.enterSubEntry(message); + } + } + + /** + * 开始一个新的entry,并计时。 + * + * @param message 新entry的信息 + */ + public static void enter(Message message) { + Entry currentEntry = getCurrentEntry(); + + if (currentEntry != null) { + currentEntry.enterSubEntry(message); + } + } + + /** + * 结束最近的一个entry,记录结束时间。 + */ + public static void release() { + Entry currentEntry = getCurrentEntry(); + + if (currentEntry != null) { + currentEntry.release(); + } + } + + /** + * 取得耗费的总时间。 + * + * @return 耗费的总时间,如果未开始计时,则返回-1 + */ + public static long getDuration() { + Entry entry = (Entry) entryStack.get(); + + if (entry != null) { + return entry.getDuration(); + } else { + return -1; + } + } + + /** + * 列出所有的entry。 + * + * @return 列出所有entry,并统计各自所占用的时间 + */ + public static String dump() { + return dump("", ""); + } + + /** + * 列出所有的entry。 + * + * @param prefix 前缀 + * @return 列出所有entry,并统计各自所占用的时间 + */ + public static String dump(String prefix) { + return dump(prefix, prefix); + } + + /** + * 列出所有的entry。 + * + * @param prefix1 首行前缀 + * @param prefix2 后续行前缀 + * @return 列出所有entry,并统计各自所占用的时间 + */ + public static String dump(String prefix1, String prefix2) { + Entry entry = (Entry) entryStack.get(); + + if (entry != null) { + return entry.toString(prefix1, prefix2); + } else { + return EMPTY_STRING; + } + } + + /** + * 取得第一个entry。 + * + * @return 第一个entry,如果不存在,则返回null + */ + public static Entry getEntry() { + return (Entry) entryStack.get(); + } + + /** + * 取得最近的一个entry。 + * + * @return 最近的一个entry,如果不存在,则返回null + */ + private static Entry getCurrentEntry() { + Entry subEntry = (Entry) entryStack.get(); + Entry entry = null; + + if (subEntry != null) { + do { + entry = subEntry; + subEntry = entry.getUnreleasedEntry(); + } while (subEntry != null); + } + + return entry; + } + + /** + * 代表一个计时单元。 + */ + public static final class Entry { + private final List subEntries = new ArrayList(4); + private final Object message; + private final Entry parentEntry; + private final Entry firstEntry; + private final long baseTime; + private final long startTime; + private long endTime; + + /** + * 创建一个新的entry。 + * + * @param message entry的信息,可以是null + * @param parentEntry 父entry,可以是null + * @param firstEntry 第一个entry,可以是null + */ + private Entry(Object message, Entry parentEntry, Entry firstEntry) { + this.message = message; + this.startTime = System.currentTimeMillis(); + this.parentEntry = parentEntry; + this.firstEntry = (Entry) firstEntry == null ? this : firstEntry; + this.baseTime = (firstEntry == null) ? 0 + : firstEntry.startTime; + } + + /** + * 取得entry的信息。 + */ + public String getMessage() { + String messageString = null; + + if (message instanceof String) { + messageString = (String) message; + } else if (message instanceof Message) { + Message messageObject = (Message) message; + MessageLevel level = MessageLevel.BRIEF_MESSAGE; + + if (isReleased()) { + level = messageObject.getMessageLevel(this); + } + + if (level == MessageLevel.DETAILED_MESSAGE) { + messageString = messageObject.getDetailedMessage(); + } else { + messageString = messageObject.getBriefMessage(); + } + } + + return StringUtils.defaultIfEmpty(messageString, null); + } + + /** + * 取得entry相对于第一个entry的起始时间。 + * + * @return 相对起始时间 + */ + public long getStartTime() { + return (baseTime > 0) ? (startTime - baseTime) + : 0; + } + + /** + * 取得entry相对于第一个entry的结束时间。 + * + * @return 相对结束时间,如果entry还未结束,则返回-1 + */ + public long getEndTime() { + if (endTime < baseTime) { + return -1; + } else { + return endTime - baseTime; + } + } + + /** + * 取得entry持续的时间。 + * + * @return entry持续的时间,如果entry还未结束,则返回-1 + */ + public long getDuration() { + if (endTime < startTime) { + return -1; + } else { + return endTime - startTime; + } + } + + /** + * 取得entry自身所用的时间,即总时间减去所有子entry所用的时间。 + * + * @return entry自身所用的时间,如果entry还未结束,则返回-1 + */ + public long getDurationOfSelf() { + long duration = getDuration(); + + if (duration < 0) { + return -1; + } else if (subEntries.isEmpty()) { + return duration; + } else { + for (int i = 0; i < subEntries.size(); i++) { + Entry subEntry = (Entry) subEntries.get(i); + + duration -= subEntry.getDuration(); + } + + if (duration < 0) { + return -1; + } else { + return duration; + } + } + } + + /** + * 取得当前entry在父entry中所占的时间百分比。 + * + * @return 百分比 + */ + public double getPecentage() { + double parentDuration = 0; + double duration = getDuration(); + + if ((parentEntry != null) && parentEntry.isReleased()) { + parentDuration = parentEntry.getDuration(); + } + + if ((duration > 0) && (parentDuration > 0)) { + return duration / parentDuration; + } else { + return 0; + } + } + + /** + * 取得当前entry在第一个entry中所占的时间百分比。 + * + * @return 百分比 + */ + public double getPecentageOfAll() { + double firstDuration = 0; + double duration = getDuration(); + + if ((firstEntry != null) && firstEntry.isReleased()) { + firstDuration = firstEntry.getDuration(); + } + + if ((duration > 0) && (firstDuration > 0)) { + return duration / firstDuration; + } else { + return 0; + } + } + + /** + * 取得所有子entries。 + * + * @return 所有子entries的列表(不可更改) + */ + public List getSubEntries() { + return Collections.unmodifiableList(subEntries); + } + + /** + * 结束当前entry,并记录结束时间。 + */ + private void release() { + endTime = System.currentTimeMillis(); + } + + /** + * 判断当前entry是否结束。 + * + * @return 如果entry已经结束,则返回true + */ + private boolean isReleased() { + return endTime > 0; + } + + /** + * 创建一个新的子entry。 + * + * @param message 子entry的信息 + */ + private void enterSubEntry(Object message) { + Entry subEntry = new Entry(message, this, firstEntry); + + subEntries.add(subEntry); + } + + /** + * 取得未结束的子entry。 + * + * @return 未结束的子entry,如果没有子entry,或所有entry均已结束,则返回null + */ + private Entry getUnreleasedEntry() { + Entry subEntry = null; + + if (!subEntries.isEmpty()) { + subEntry = (Entry) subEntries.get(subEntries.size() - 1); + + if (subEntry.isReleased()) { + subEntry = null; + } + } + + return subEntry; + } + + /** + * 将entry转换成字符串的表示。 + * + * @return 字符串表示的entry + */ + public String toString() { + return toString("", ""); + } + + /** + * 将entry转换成字符串的表示。 + * + * @param prefix1 首行前缀 + * @param prefix2 后续行前缀 + * @return 字符串表示的entry + */ + private String toString(String prefix1, String prefix2) { + StringBuffer buffer = new StringBuffer(); + + toString(buffer, prefix1, prefix2); + + return buffer.toString(); + } + + /** + * 将entry转换成字符串的表示。 + * + * @param buffer 字符串buffer + * @param prefix1 首行前缀 + * @param prefix2 后续行前缀 + */ + private void toString(StringBuffer buffer, String prefix1, String prefix2) { + buffer.append(prefix1); + + String message = getMessage(); + long startTime = getStartTime(); + long duration = getDuration(); + long durationOfSelf = getDurationOfSelf(); + double percent = getPecentage(); + double percentOfAll = getPecentageOfAll(); + + Object[] params = new Object[]{ + message, // {0} - entry信息 + new Long(startTime), // {1} - 起始时间 + new Long(duration), // {2} - 持续总时间 + new Long(durationOfSelf), // {3} - 自身消耗的时间 + new Double(percent), // {4} - 在父entry中所占的时间比例 + new Double(percentOfAll) // {5} - 在总时间中所旧的时间比例 + }; + + StringBuffer pattern = new StringBuffer("{1,number} "); + + if (isReleased()) { + pattern.append("[{2,number}ms"); + + if ((durationOfSelf > 0) && (durationOfSelf != duration)) { + pattern.append(" ({3,number}ms)"); + } + + if (percent > 0) { + pattern.append(", {4,number,##%}"); + } + + if (percentOfAll > 0) { + pattern.append(", {5,number,##%}"); + } + + pattern.append("]"); + } else { + pattern.append("[UNRELEASED]"); + } + + if (message != null) { + pattern.append(" - {0}"); + } + + buffer.append(MessageFormat.format(pattern.toString(), params)); + + for (int i = 0; i < subEntries.size(); i++) { + Entry subEntry = (Entry) subEntries.get(i); + + buffer.append('\n'); + + if (i == (subEntries.size() - 1)) { + subEntry.toString(buffer, prefix2 + "`---", prefix2 + " "); // 最后一项 + } else if (i == 0) { + subEntry.toString(buffer, prefix2 + "+---", prefix2 + "| "); // 第一项 + } else { + subEntry.toString(buffer, prefix2 + "+---", prefix2 + "| "); // 中间项 + } + } + } + } + + /** + * 显示消息的级别。 + */ + public enum MessageLevel { + NO_MESSAGE, BRIEF_MESSAGE, DETAILED_MESSAGE; + } + + + /** + * 代表一个profiler entry的详细信息。 + */ + public interface Message { + MessageLevel getMessageLevel(Entry entry); + + String getBriefMessage(); + + String getDetailedMessage(); + } +} + + diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java index b40cc3ff..9278b277 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java @@ -5,6 +5,7 @@ import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.Profiler; /** * Created by ohun on 2015/12/22. @@ -17,7 +18,12 @@ public abstract class BaseMessageHandler implements MessageHa public void handle(Packet packet, Connection connection) { T t = decode(packet, connection); if (t != null) { - handle(t); + try{ + Profiler.enter("start handle"); + handle(t); + }finally{ + Profiler.release(); + } } } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index eabb6e10..97b367f7 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.tools.IOUtils; import com.shinemo.mpush.tools.config.ConfigCenter; @@ -20,22 +21,27 @@ public abstract class BaseMessage implements Message { protected final Connection connection; public BaseMessage(Packet packet, Connection connection) { - this.packet = packet; - this.connection = connection; - this.decodeBody(); + Profiler.enter("start decode message"); + try{ + this.packet = packet; + this.connection = connection; + this.decodeBody(); + }finally{ + Profiler.release(); + } } protected void decodeBody() { + if (packet.body != null && packet.body.length > 0) { //1.解密 byte[] tmp = packet.body; if (packet.hasFlag(Packet.FLAG_CRYPTO)) { SessionContext info = connection.getSessionContext(); if (info.cipher != null) { - tmp = info.cipher.decrypt(tmp); + tmp = info.cipher.decrypt(tmp); } } - //2.解压 if (packet.hasFlag(Packet.FLAG_COMPRESS)) { byte[] result = IOUtils.uncompress(tmp); @@ -49,6 +55,7 @@ protected void decodeBody() { packet.body = tmp; decode(packet.body); } + } protected void encodeBody() { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java index c00b4d55..0d99f3bd 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.common.security; import com.shinemo.mpush.api.connection.Cipher; +import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; @@ -21,12 +22,23 @@ public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @Override public byte[] decrypt(byte[] data) { - return RSAUtils.decryptByPrivateKey(data, privateKey); + try{ + Profiler.enter("start decrypt"); + return RSAUtils.decryptByPrivateKey(data, privateKey); + }finally{ + Profiler.release(); + } + } @Override public byte[] encrypt(byte[] data) { - return RSAUtils.encryptByPublicKey(data, publicKey); + try{ + Profiler.enter("start encrypt"); + return RSAUtils.encryptByPublicKey(data, publicKey); + }finally{ + Profiler.release(); + } } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 842b6069..2ddb9eb1 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -7,6 +7,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.log.LogLevel; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.connection.NettyConnection; @@ -41,10 +42,21 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("channelRead channel={}, packet={}", ctx.channel(), msg); - connection.updateLastReadTime(); - receiver.onReceive((Packet) msg, connection); + try{ + Profiler.start("end channel read:"); + Connection connection = connectionManager.get(ctx.channel()); + LOGGER.debug("channelRead channel={}, packet={}", ctx.channel(), msg); + connection.updateLastReadTime(); + receiver.onReceive((Packet) msg, connection); + }finally{ + Profiler.release(); + long duration = Profiler.getDuration(); + if(duration>100){ + LOGGER.error("end channel read:"+Profiler.dump()); + } + Profiler.reset(); + } + } @Override diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java index 0edd8670..9a13aca6 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java @@ -27,7 +27,7 @@ public class BodyTest { private String online_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g="; private RSAPrivateKey onlinePrivateKey = null; - String rawData = "3, 37, -3, -15, 22, -68, 4, 61, -95, -108, 94, 36, -104, 6, -76, 34, 107, -76, 63, 127, -114, -97, 97, -80, -74, 122, 84, 90, -67, 7, -116, 16, -75, 95, 126, 119, 125, -71, -106, 121, -100, -121, -76, 28, 63, -2, 105, 84, -123, 113, 18, -93, 108, -18, 73, -1, 40, 65, -67, -17, -79, -44, 9, -85, 97, -99, -30, 45, -19, -62, -6, -48, 66, -101, 6, 34, -87, -71, -80, 74, -82, -49, -71, -98, -121, -24, -21, -93, 15, 100, -101, 106, -48, -22, -35, -15, -61, -102, 123, -117, 48, -45, 41, -109, -57, -79, 87, -24, -11, -18, -53, -126, -115, -24, 0, 19, 38, -99, 107, 118, -100, -37, -56, -105, 8, 91, 2, -83"; + String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; byte[]data = null; From 4a728bb6158d61d85aa92d5f7f3d9a2faf44d13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 8 Mar 2016 06:46:33 +0000 Subject: [PATCH 483/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/message/BaseMessage.java | 30 ++++----- .../java/com/shinemo/mpush/tools/IOUtils.java | 17 +++--- .../java/com/shinemo/mpush/tools/Strings.java | 2 +- .../mpush/tools/config/ConfigCenter.java | 2 +- .../shinemo/mpush/tools/crypto/AESUtils.java | 6 +- .../shinemo/mpush/tools/crypto/MD5Utils.java | 6 +- .../shinemo/mpush/tools/crypto/RSAUtils.java | 12 +++- .../com/shinemo/mpush/tools/IOUtilsTest.java | 61 +++++++++++++++++++ .../mpush/tools/crypto/RSAUtilsTest.java | 16 ++--- 9 files changed, 106 insertions(+), 46 deletions(-) create mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 97b367f7..2423ef17 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -21,41 +21,37 @@ public abstract class BaseMessage implements Message { protected final Connection connection; public BaseMessage(Packet packet, Connection connection) { - Profiler.enter("start decode message"); - try{ - this.packet = packet; - this.connection = connection; - this.decodeBody(); - }finally{ - Profiler.release(); - } + this.packet = packet; + this.connection = connection; + Profiler.enter("start decode message"); + try { + decodeBody(); + } finally { + Profiler.release(); + } } protected void decodeBody() { - if (packet.body != null && packet.body.length > 0) { //1.解密 byte[] tmp = packet.body; if (packet.hasFlag(Packet.FLAG_CRYPTO)) { - SessionContext info = connection.getSessionContext(); - if (info.cipher != null) { - tmp = info.cipher.decrypt(tmp); + if (connection.getSessionContext().cipher != null) { + tmp = connection.getSessionContext().cipher.decrypt(tmp); } } //2.解压 if (packet.hasFlag(Packet.FLAG_COMPRESS)) { - byte[] result = IOUtils.uncompress(tmp); - if (result.length > 0) { - tmp = result; - } + tmp = IOUtils.uncompress(tmp); } + if (tmp.length == 0) { throw new RuntimeException("message decode ex"); } + packet.body = tmp; decode(packet.body); } - } protected void encodeBody() { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java index 90569e35..e2ce0c4f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java @@ -26,10 +26,9 @@ public static void close(Closeable closeable) { } } - public static byte[] compress(byte[] data) { - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - DeflaterOutputStream zipOut = new DeflaterOutputStream(byteStream); + ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); + DeflaterOutputStream zipOut = new DeflaterOutputStream(out); try { zipOut.write(data); zipOut.finish(); @@ -40,23 +39,23 @@ public static byte[] compress(byte[] data) { } finally { close(zipOut); } - return byteStream.toByteArray(); + return out.toByteArray(); } public static byte[] uncompress(byte[] data) { - InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(data)); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[data.length * 2]; + InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data)); + ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4); + byte[] buffer = new byte[1024]; int length; try { - while ((length = in.read(buffer)) != -1) { + while ((length = zipIn.read(buffer)) != -1) { out.write(buffer, 0, length); } } catch (IOException e) { LOGGER.error("uncompress ex", e); return Constants.EMPTY_BYTES; } finally { - close(in); + close(zipIn); } return out.toByteArray(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java index 335160a3..7a40e206 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java @@ -3,7 +3,7 @@ /** * Created by ohun on 2015/12/23. */ -public class Strings { +public final class Strings { public static final String EMPTY = ""; public static boolean isBlank(CharSequence text) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 38452bbc..b622e697 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -24,7 +24,7 @@ public interface ConfigCenter extends Config { int maxPacketSize(); @Key("compress_limit") - @DefaultValue("10240") + @DefaultValue("1024") int compressLimit(); @Key("min_heartbeat") diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java index 9a13cc29..3c519cbe 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java @@ -25,7 +25,7 @@ public final class AESUtils { *

* 生成密钥 *

- * + * * @param seed 密钥种子 * @return * @throws Exception @@ -36,7 +36,7 @@ public static SecretKey getSecretKey(byte[] seed) throws Exception { keyGenerator.init(secureRandom); return keyGenerator.generateKey(); } - + public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); @@ -49,8 +49,6 @@ public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { throw new RuntimeException("AES encrypt ex", e); } } - - public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java index 8a68fa2a..b3b5dc97 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java @@ -14,13 +14,13 @@ /** * Created by ohun on 2015/12/25. */ -public class MD5Utils { +public final class MD5Utils { public static String encrypt(File file) { InputStream in = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); - byte[] buffer = new byte[10240];//10k + byte[] buffer = new byte[1024];//10k int readLen; while ((readLen = in.read(buffer)) != -1) { digest.update(buffer, 0, readLen); @@ -55,7 +55,7 @@ public static String encrypt(byte[] bytes) { } private static String toHex(byte[] bytes) { - StringBuffer buffer = new StringBuffer(bytes.length * 2); + StringBuilder buffer = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; ++i) { buffer.append(Character.forDigit((bytes[i] & 240) >> 4, 16)); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java index 8b1805fc..3b1698cc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java @@ -25,7 +25,7 @@ * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,
* 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 */ -public class RSAUtils { +public final class RSAUtils { private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class); /** @@ -237,7 +237,7 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) return doFinal(cipher, data, key_len); } catch (Exception e) { LOGGER.error("decryptByPrivateKey ex", e); - throw new RuntimeException("RSA encrypt ex", e); + throw new RuntimeException("RSA decrypt ex", e); } } @@ -253,9 +253,9 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) * @throws IllegalBlockSizeException */ private static byte[] doFinal(Cipher cipher, byte[] data, int key_len) throws BadPaddingException, IllegalBlockSizeException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); int inputLen = data.length, offSet = 0; byte[] tmp; + ByteArrayOutputStream out = new ByteArrayOutputStream(getTmpArrayLength(inputLen)); while (inputLen > 0) { tmp = cipher.doFinal(data, offSet, Math.min(key_len, inputLen)); out.write(tmp, 0, tmp.length); @@ -265,6 +265,12 @@ private static byte[] doFinal(Cipher cipher, byte[] data, int key_len) throws Ba return out.toByteArray(); } + private static int getTmpArrayLength(int L) { + int S = MAX_DECRYPT_BLOCK; + while (S < L) S <<= 1; + return S; + } + /** * 私钥解密 * diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java new file mode 100644 index 00000000..37c1468e --- /dev/null +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java @@ -0,0 +1,61 @@ +package com.shinemo.mpush.tools; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by ohun on 2016/3/8. + */ +public class IOUtilsTest { + + @Test + public void testCompress() throws Exception { + byte[] s = ("士大夫士大大啊实打实大苏打撒" + + "阿斯顿撒大苏打实打实的苏打似的啊实" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "打实大苏打实打实大苏打水水" + + "水水水水夫").getBytes(); + long t1 = System.currentTimeMillis(); + for (int i = 0; i < 100; i++) { + IOUtils.compress(s); + } + System.out.println((System.currentTimeMillis()-t1)); + System.out.println("src:" + s.length); + byte[] out = IOUtils.compress(s); + System.out.println("compress:" + out.length); + byte[] ss = IOUtils.uncompress(out); + System.out.println("uncompress:" + ss.length); + } + + @Test + public void testUncompress() throws Exception { + + } +} \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java index 2d0cf94b..2ad9fb79 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java @@ -15,7 +15,7 @@ public class RSAUtilsTest { String publicKey; String privateKey; - + String publicKey2; String privateKey2; @@ -27,7 +27,7 @@ public void setUp() throws Exception { privateKey = RSAUtils.encodeBase64(pair.value); System.out.println("公钥: \n\r" + publicKey); System.out.println("私钥: \n\r" + privateKey); - + pair = RSAUtils.genKeyPair(); publicKey2 = RSAUtils.encodeBase64(pair.key); privateKey2 = RSAUtils.encodeBase64(pair.value); @@ -38,23 +38,23 @@ public void setUp() throws Exception { @Test public void testGetKeys() throws Exception { - String source = "这是一行测试RSA数字签名的无意义文字"; - byte[] data = source.getBytes(); + String source = "这是一行测试RSA数字签名的无意义文字"; + byte[] data = source.getBytes(); byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey); System.out.println("加密后:\n" + new String(encodedData)); byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey); String target = new String(decodedData); System.out.println("解密后:\n" + target); - + decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey2); target = new String(decodedData); System.out.println("解密后2:\n" + target); - + } @Test public void testGetPrivateKey() throws Exception { - System.out.println("公钥: \n\r" + publicKey); + System.out.println("公钥: \n\r" + publicKey); System.out.println("私钥: \n\r" + privateKey); } @@ -91,7 +91,7 @@ public void test1() throws Exception { System.out.println("加密后文字:\r\n" + new String(encodedData)); byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey); String target = new String(decodedData); - System.out.println("解密后文字: \r\n" + target); + System.out.println("解密后文字: \r\n" + target + ", 明文长度:" + data.length + ", 密文长度:" + encodedData.length); } @Test From 0ed6f6c869750841c84d8eec1ef2e46b043b0e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 8 Mar 2016 06:46:51 +0000 Subject: [PATCH 484/890] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/common/ErrorCode.java | 1 + .../java/com/shinemo/mpush/common/MessageDispatcher.java | 8 ++++++-- .../com/shinemo/mpush/common/message/ErrorMessage.java | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java index ab4cc1dd..8520a11f 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java @@ -7,6 +7,7 @@ public enum ErrorCode { OFFLINE(1, "user offline"), PUSH_CLIENT_FAILURE(2, "push to client failure"), ROUTER_CHANGE(3, "router change"), + DISPATCH_ERROR(100, "handle message error"), UNKNOWN(-1, "unknown"); ErrorCode(int code, String errorMsg) { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java index bdf6dc98..a222498e 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java @@ -5,6 +5,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; +import com.shinemo.mpush.common.message.ErrorMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,8 +32,11 @@ public void onReceive(Packet packet, Connection connection) { handler.handle(packet, connection); } } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={},conn={},body={}", packet, connection, packet.body); - connection.close(); + LOGGER.error("dispatch packet ex, packet={}, conn={}", packet, connection, throwable); + ErrorMessage + .from(packet, connection) + .setErrorCode(ErrorCode.DISPATCH_ERROR) + .close(); } } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java index eea54b62..ac23e4ec 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java @@ -44,6 +44,11 @@ public static ErrorMessage from(BaseMessage src) { , src.packet.sessionId), src.connection); } + public static ErrorMessage from(Packet src, Connection connection) { + return new ErrorMessage(src.cmd, new Packet(ERROR, src.sessionId), connection); + } + + public ErrorMessage setReason(String reason) { this.reason = reason; return this; From f1cce8ad9275985fff7ed182e5e72a2ebcca3029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 14:57:55 +0800 Subject: [PATCH 485/890] conf edit --- conf-online.properties | 2 +- .../shinemo/mpush/common/AbstractServer.java | 9 ----- .../mpush/netty/client/NettyHttpClient.java | 6 ++-- .../mpush/netty/client/RequestInfo.java | 2 -- .../mpush/test/connection/body/BodyTest.java | 1 + .../mpush/test/gson/DnsMappingTest.java | 34 +++++++++++++++++++ .../mpush/tools/thread/ThreadNameSpace.java | 2 ++ .../thread/threadpool/ThreadPoolContext.java | 2 ++ .../thread/threadpool/ThreadPoolManager.java | 3 ++ 9 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java diff --git a/conf-online.properties b/conf-online.properties index c69b1d56..ed8e635b 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,6 +10,6 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0 force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 +dns_mapping=api.server.mangguoyisheng.com=10.161.215.146,10.161.158.135 skip_dump=false admin_port=4001 diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java index 1ec54be1..6d888501 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java @@ -122,15 +122,6 @@ public void onFailure(String message) { } }; ThreadPoolManager.newThread(server.getClass().getSimpleName(), runnable).start(); -// //TODO sleep for server start -// while(!server.isRunning()){ -// log.error("server is not started,wait for {} start",server.getClass().getSimpleName()); -// try { -// Thread.sleep(100); -// } catch (InterruptedException e) { -// } -// } -// log.error("server started {}",server.getClass().getSimpleName()); } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 5906fdbb..77ba80da 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -3,6 +3,8 @@ import com.google.common.collect.ArrayListMultimap; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.thread.NamedThreadFactory; +import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; + import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -11,6 +13,7 @@ import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.util.*; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +22,6 @@ import java.net.URLDecoder; import java.util.Iterator; import java.util.List; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** @@ -37,7 +39,7 @@ public class NettyHttpClient implements HttpClient { @Override public void start() { // TODO: 2016/2/15 yxx 配置线程池 - workerGroup = new NioEventLoopGroup(0, Executors.newCachedThreadPool()); + workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.httpExecutor); b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java index 63072bd5..7e98c553 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java @@ -1,11 +1,9 @@ package com.shinemo.mpush.netty.client; import com.google.common.primitives.Ints; -import com.google.common.primitives.Primitives; import com.shinemo.mpush.api.Constants; import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Timeout; import io.netty.util.TimerTask; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java index 9a13aca6..d0cf2844 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java @@ -22,6 +22,7 @@ public class BodyTest { private String pre_privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA=="; private RSAPrivateKey prePrivateKey = null; + //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB private String online_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB"; private String online_allocServer = "http://allot.mangguoyisheng.com/"; private String online_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g="; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java new file mode 100644 index 00000000..1f60025b --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java @@ -0,0 +1,34 @@ +package com.shinemo.mpush.test.gson; + +import java.util.Map; + +import org.junit.Test; + +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.collect.ArrayListMultimap; + +public class DnsMappingTest { + + @Test + public void test(){ + + ArrayListMultimap mappings = ArrayListMultimap.create(); + + String dnsString = "111.1.57.148=127.0.0.1,127.0.0.2;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1"; + if (Strings.isNullOrEmpty(dnsString)) return; + + Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); + Splitter vsp = Splitter.on(','); + for (Map.Entry entry : map.entrySet()) { + String value = entry.getValue(); + if (Strings.isNullOrEmpty(value)) continue; + Iterable it = vsp.split(entry.getValue()); + mappings.putAll(entry.getKey(), it); + } + + System.out.println(mappings); + + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java index 8445b850..1ed21225 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java @@ -12,6 +12,8 @@ public class ThreadNameSpace { */ public static final String NETTY_WORKER = "mp-worker"; + public static final String NETTY_HTTP = "mp-http"; + public static final String EVENT_BUS = "mp-event-bus"; public static final String REDIS = "mp-redis"; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java index ae1f5835..3557dc1b 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -18,6 +18,8 @@ public class ThreadPoolContext { public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5,Constants.WORK_THREAD_QUEUE_SIZE); + public static ThreadPoolContext HTTP_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.NETTY_HTTP, Constants.BIZ_POOL_SIZE); + public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java index 0c937fb2..3edce224 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -21,12 +21,15 @@ public class ThreadPoolManager { public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); + public static Executor httpExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); + static{ poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); + poolCache.put(ThreadPoolContext.HTTP_THREAD_POOL.getName(), httpExecutor); } public static final Map getPool(){ From 466ffaa94c7d6add692e82f351bab52335b7864e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 14:58:27 +0800 Subject: [PATCH 486/890] conf edit --- conf-dev.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf-dev.properties b/conf-dev.properties index 01ce6b99..2bbc1564 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -10,7 +10,7 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi force_write_redis_group_info = true connection_server_port = 20882 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1 +dns_mapping=111.1.57.148=127.0.0.1 skip_dump=true admin_port=4001 From 7261a27eb702b4e8d4d53efe42bd151cfa638f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 15:11:28 +0800 Subject: [PATCH 487/890] bug fix --- .../mpush/tools/thread/threadpool/ThreadPoolManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java index 3edce224..041f5559 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -21,7 +21,7 @@ public class ThreadPoolManager { public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); - public static Executor httpExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); + public static Executor httpExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); static{ poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); From 654cd59235bf6ae1ce221e2f43652e08a1f5617b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 16:09:42 +0800 Subject: [PATCH 488/890] add test --- .../shinemo/mpush/test/crypto/RsaTest.java | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java new file mode 100644 index 00000000..9272452b --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java @@ -0,0 +1,157 @@ +package com.shinemo.mpush.test.crypto; + +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.junit.Before; +import org.junit.Test; + +import com.shinemo.mpush.common.message.HandshakeMessage; +import com.shinemo.mpush.common.security.CipherBox; +import com.shinemo.mpush.tools.crypto.RSAUtils; + +public class RsaTest { + + private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; + private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; + private static RSAPrivateKey dailyPrivateKey = null; + private static RSAPublicKey dailyPublicKey = null; + + @Before + public void init(){ + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + dailyPublicKey = (RSAPublicKey)RSAUtils.decodePublicKey(daily_publicKey); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test(){ + + HandshakeMessage message = new HandshakeMessage(null); + message.clientKey = CipherBox.INSTANCE.randomAESKey(); + message.iv = CipherBox.INSTANCE.randomAESIV(); + message.clientVersion = "1.1.0"; + message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; + message.osName = "android"; + message.osVersion = "1.2.1"; + message.timestamp = System.currentTimeMillis(); + + byte[] temp = message.encode(); + + long startencode = System.currentTimeMillis(); + byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); + long encodeTime = System.currentTimeMillis() - startencode; + + long startdecode = System.currentTimeMillis(); + byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); + long decodeTime = System.currentTimeMillis() - startdecode; + decode(temp2); + System.out.println(encodeTime+","+decodeTime); + + } + + @Test + public void mulTest(){ + + Executor pool = Executors.newFixedThreadPool(20); + + CountDownLatch encodeLatch = new CountDownLatch(1); + CountDownLatch decodeLatch = new CountDownLatch(1); + + for(int i = 0;i<18;i++){ + pool.execute(new Worker(i, encodeLatch, decodeLatch)); + } + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + encodeLatch.countDown(); + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + decodeLatch.countDown(); + + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println("end"); + + } + + private static void decode(byte[] message){ + HandshakeMessage handshakeMessage = new HandshakeMessage(null); + handshakeMessage.decode(message); +// System.out.println(ToStringBuilder.reflectionToString(handshakeMessage, ToStringStyle.MULTI_LINE_STYLE)); + } + + public static class Worker implements Runnable{ + + private int i; + private CountDownLatch encodeLatch; + private CountDownLatch decodeLatch; + + public Worker(int i,CountDownLatch encodeLatch,CountDownLatch decodeLatch) { + this.i = i; + this.encodeLatch = encodeLatch; + this.decodeLatch = decodeLatch; + } + + @Override + public void run() { + HandshakeMessage message = new HandshakeMessage(null); + message.clientKey = CipherBox.INSTANCE.randomAESKey(); + message.iv = CipherBox.INSTANCE.randomAESIV(); + message.clientVersion = "1.1.0"+i; + message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; + message.osName = "android"; + message.osVersion = "1.2.1"; + message.timestamp = System.currentTimeMillis(); + + byte[] temp = message.encode(); + System.out.println(i+":wait encode"); + try { + encodeLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long startencode = System.currentTimeMillis(); + byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); + long encodeTime = System.currentTimeMillis() - startencode; + + + + System.out.println(i+":wait decode"); + + try { + decodeLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long startdecode = System.currentTimeMillis(); + byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); + long decodeTime = System.currentTimeMillis() - startdecode; + decode(temp2); + System.out.println(i+":"+encodeTime+","+decodeTime); + + } + + } + +} From 7c5838caf4a95a2f53b4be3bff53f422d24ac819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 16:18:00 +0800 Subject: [PATCH 489/890] add redis remove --- .../java/com/shinemo/mpush/core/server/ConnectionServer.java | 3 +++ .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index f953c386..b426d9bb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -36,6 +36,9 @@ public void init() { connectionManager.init(); //重置在线数 RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); + //删除已经存在的数据 + RedisManage.del(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); + MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 2ddb9eb1..e967a8d7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -52,7 +52,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Profiler.release(); long duration = Profiler.getDuration(); if(duration>100){ - LOGGER.error("end channel read:"+Profiler.dump()); + LOGGER.error("end channel read:"+duration+","+Profiler.dump()); } Profiler.reset(); } From 1fa808fb0661b3b3ef9fbed1b1f310697b3ce8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 16:34:04 +0800 Subject: [PATCH 490/890] bug fix --- mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java | 6 +++--- .../java/com/shinemo/mpush/core/handler/AdminHandler.java | 2 +- .../com/shinemo/mpush/core/server/ConnectionServer.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java index ba46e0c0..0a704ddd 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java @@ -29,9 +29,9 @@ public static final String getUserOnlineKey(String extranetAddress) { return USER_ONLINE_KEY + extranetAddress; } - public static final String getConnNum(String extranetAddress) { - return CONN_NUM_ + extranetAddress; - } +// public static final String getConnNum(String extranetAddress) { +// return CONN_NUM_ + extranetAddress; +// } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java index a2db6c1f..2ced1c58 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java @@ -78,7 +78,7 @@ public String handler(String request) { SCN("scn"){ @Override public String handler(String request) { - Long value = RedisManage.get(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), Long.class); + Long value = RedisManage.get(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()), Long.class); if(value == null){ value = 0L; } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index b426d9bb..0ab799b3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -35,7 +35,7 @@ public void init() { super.init(); connectionManager.init(); //重置在线数 - RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); +// RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); //删除已经存在的数据 RedisManage.del(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); From 470dcfdee6a7479e0b65274d2b43c81566022881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 17:07:42 +0800 Subject: [PATCH 491/890] bug fix --- .../main/java/com/shinemo/mpush/core/handler/AdminHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java index 2ced1c58..f33004c9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java @@ -78,7 +78,7 @@ public String handler(String request) { SCN("scn"){ @Override public String handler(String request) { - Long value = RedisManage.get(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()), Long.class); + Long value = RedisManage.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); if(value == null){ value = 0L; } From 858eec28042a191a33a87ead30dec4a4a8b214f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Tue, 8 Mar 2016 20:28:14 +0800 Subject: [PATCH 492/890] bug fix --- .../shinemo/mpush/core/router/RouterChangeListener.java | 6 ++++++ .../src/main/java/com/shinemo/mpush/tools/MPushUtil.java | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 3d1dc5c7..5b5512ce 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.core.router; import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.event.RouterChangeEvent; @@ -128,6 +129,7 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { routerManager.unRegister(userId); //2.2发送踢人消息到客户端 kickLocal(userId, router); + remStatUser(userId); } else { LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg={}", msg); } @@ -146,4 +148,8 @@ public void onMessage(String channel, String message) { LoggerManage.info(LogType.CONNECTION, "receive an error redis channel={}",channel); } } + + private void remStatUser(String userId){ + RedisManage.zRem(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp()), userId); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index acf5fb6c..64e2a872 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -21,6 +21,8 @@ public final class MPushUtil { private static String LOCAL_IP; private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$"); + + private static String EXTRANET_IP; public static boolean isLocalHost(String host) { return host == null @@ -72,6 +74,13 @@ public static String getInetAddress() { } } + public static String getExtranetIp(){ + if(EXTRANET_IP == null){ + EXTRANET_IP = getExtranetAddress(); + } + return EXTRANET_IP; + } + public static String getExtranetAddress() { try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); From 945dc015c8d5df70326fadfb7cac352bffe104e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 10:19:44 +0800 Subject: [PATCH 493/890] bug fix --- .../java/com/shinemo/mpush/common/MessageDispatcher.java | 7 ++++++- .../shinemo/mpush/core/server/ServerChannelHandler.java | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java index a222498e..411170cf 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java @@ -26,9 +26,10 @@ public void register(Command command, MessageHandler handler) { @Override public void onReceive(Packet packet, Connection connection) { + MessageHandler handler = handlers.get(packet.cmd); try { - MessageHandler handler = handlers.get(packet.cmd); if (handler != null) { + Profiler.enter("start handle:"+handler.getClass().getSimpleName()); handler.handle(packet, connection); } } catch (Throwable throwable) { @@ -37,6 +38,10 @@ public void onReceive(Packet packet, Connection connection) { .from(packet, connection) .setErrorCode(ErrorCode.DISPATCH_ERROR) .close(); + }finally{ + if(handler!=null){ + Profiler.release(); + } } } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index e967a8d7..a1cbe4db 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -68,7 +68,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, LogLevel.INFO, "client connect channel={}", ctx.channel()); + LoggerManage.log(false, LogLevel.INFO, "client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -76,7 +76,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(security, LogLevel.INFO, "client disconnect channel={}", ctx.channel()); + LoggerManage.log(false, LogLevel.INFO, "client disconnect channel={}", ctx.channel()); Connection connection = connectionManager.get(ctx.channel()); EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); connectionManager.remove(ctx.channel()); From 777b4bba86d19cbdbf38ff1a52d64ceffa440858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 10:51:45 +0800 Subject: [PATCH 494/890] log --- .../shinemo/mpush/core/server/ServerChannelHandler.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index a1cbe4db..7630e977 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -8,7 +8,7 @@ import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.common.EventBus; import com.shinemo.mpush.common.Profiler; -import com.shinemo.mpush.log.LogLevel; +import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.connection.NettyConnection; @@ -62,13 +62,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LoggerManage.log(security, LogLevel.INFO, "client exceptionCaught channel={}", ctx.channel()); + LoggerManage.info(LogType.CONNECTION, "client exceptionCaught channel={}", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(false, LogLevel.INFO, "client connect channel={}", ctx.channel()); + LoggerManage.info(LogType.CONNECTION, "client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -76,7 +76,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.log(false, LogLevel.INFO, "client disconnect channel={}", ctx.channel()); + LoggerManage.info(LogType.CONNECTION, "client disconnect channel={}", ctx.channel()); Connection connection = connectionManager.get(ctx.channel()); EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); connectionManager.remove(ctx.channel()); From c94b7b308d1ad8d25f05a4028b123400922e5a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 11:26:35 +0800 Subject: [PATCH 495/890] =?UTF-8?q?add=20h5=20=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-online.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf-online.properties b/conf-online.properties index ed8e635b..1428a531 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,6 +10,6 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0 force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146,10.161.158.135 +dns_mapping=api.server.mangguoyisheng.com=10.161.215.146,10.161.158.135;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 skip_dump=false admin_port=4001 From 500538d6ffa3be135acfb4e284b8348f30b3dc48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 14:45:53 +0800 Subject: [PATCH 496/890] add log --- .../java/com/shinemo/mpush/core/handler/HttpProxyHandler.java | 4 ++++ .../java/com/shinemo/mpush/netty/client/NettyHttpClient.java | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index f376a907..d6f004ec 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -4,6 +4,7 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.DnsMapping; +import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.HttpRequestMessage; import com.shinemo.mpush.common.message.HttpResponseMessage; @@ -64,6 +65,7 @@ public void handle(HttpRequestMessage message) { setBody(request, message); try { + Profiler.enter("start http proxy handler"); httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage @@ -72,6 +74,8 @@ public void handle(HttpRequestMessage message) { .setReasonPhrase("Bad Gateway") .sendRaw(); LOGGER.error("send request ex, message=" + message, e); + }finally{ + Profiler.release(); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 77ba80da..366a850b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -82,12 +82,14 @@ public void request(final RequestInfo info) throws Exception { timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); Channel channel = tryAcquire(host); if (channel == null) { + final long startConnectChannel = System.currentTimeMillis(); LOGGER.debug("create new channel, host={}", host); ChannelFuture f = b.connect(host, port); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { + LOGGER.debug("create new channel cost:"+(System.currentTimeMillis()-startConnectChannel)); + if (future.isSuccess()) { writeRequest(future.channel(), info); } else { info.tryDone(); From 4eccafa5ba71fe4fdfc12b50a885e0727af4e5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 9 Mar 2016 06:42:57 +0000 Subject: [PATCH 497/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/manage/user/UserManager.java | 57 ++++++++++--------- .../mpush/core/router/RouterCenter.java | 14 +---- .../core/router/RouterChangeListener.java | 35 ++++++------ .../mpush/core/router/UserManager.java | 50 ---------------- .../router/UserOnlineOfflineListener.java | 37 ++++++++++++ .../mpush/core/server/ConnectionServer.java | 9 --- 6 files changed, 85 insertions(+), 117 deletions(-) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java create mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index 7f17f3f5..385f1e78 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -10,40 +10,41 @@ import com.shinemo.mpush.tools.redis.manage.RedisManage; //查询使用 -public class UserManager { - - private static final String EXTRANET_ADDRESS = MPushUtil.getExtranetAddress(); - - private static final String ONLINE_KEY = RedisKey.getUserOnlineKey(EXTRANET_ADDRESS); - - private static final Logger log = LoggerFactory.getLogger(UserManager.class); - - public void init(){ - RedisManage.del(ONLINE_KEY); - log.info("init redis key:{}"+ONLINE_KEY); - } - - public void userOnline(String userId) { - RedisManage.zAdd(ONLINE_KEY, userId); - log.info("user online {}",userId); +public final class UserManager { + public static final UserManager INSTANCE = new UserManager(); + private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); + + private static final Logger log = LoggerFactory.getLogger(UserManager.class); + + public UserManager() { + //重置在线数 +// RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); + //删除已经存在的数据 + RedisManage.del(ONLINE_KEY); + log.info("init redis key:{}" + ONLINE_KEY); + } + + public void userOnline(String userId) { + RedisManage.zAdd(ONLINE_KEY, userId); + log.info("user online {}", userId); } public void userOffline(String userId) { - RedisManage.zRem(ONLINE_KEY, userId); - log.info("user offline {}",userId); + RedisManage.zRem(ONLINE_KEY, userId); + log.info("user offline {}", userId); } - + //在线用户 - public long onlineUserNum(){ - return RedisManage.zCard(ONLINE_KEY); + public long onlineUserNum() { + return RedisManage.zCard(ONLINE_KEY); } - + //在线用户列表 - public List onlineUserList(int start,int size){ - if(size<10){ - size = 10; - } - return RedisManage.zrange(ONLINE_KEY, start, size-1, String.class); + public List onlineUserList(int start, int size) { + if (size < 10) { + size = 10; + } + return RedisManage.zrange(ONLINE_KEY, start, size - 1, String.class); } - + } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java index 585095d9..d2c359a4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java @@ -22,8 +22,8 @@ public final class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); private final RouterChangeListener routerChangeListener = new RouterChangeListener(); - private final UserManager userManager = new UserManager(); - + private final UserOnlineOfflineListener userOnlineOfflineListener = new UserOnlineOfflineListener(); + /** * 注册用户和链接 @@ -45,7 +45,7 @@ public boolean register(String userId, Connection connection) { try { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); - + } catch (Exception e) { LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); } @@ -83,12 +83,4 @@ public LocalRouterManager getLocalRouterManager() { public RemoteRouterManager getRemoteRouterManager() { return remoteRouterManager; } - - public RouterChangeListener getRouterChangeListener() { - return routerChangeListener; - } - - public UserManager getUserManager(){ - return userManager; - } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java index 5b5512ce..9d5cfe2c 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java @@ -29,19 +29,19 @@ public final class RouterChangeListener extends AbstractEventContainer implement private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); public RouterChangeListener() { - ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); + ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); } public String getKickChannel() { return kick_channel; } - - public String getKickChannel(String remoteIp){ - return KICK_CHANNEL_ + remoteIp; + + public String getKickChannel(String remoteIp) { + return KICK_CHANNEL_ + remoteIp; } @Subscribe - void onRouteChangeEvent(RouterChangeEvent event) { + void onRouteChange(RouterChangeEvent event) { String userId = event.userId; Router r = event.router; if (r.getRouteType().equals(Router.RouterType.LOCAL)) { @@ -49,8 +49,6 @@ void onRouteChangeEvent(RouterChangeEvent event) { } else { kickRemote(userId, (RemoteRouter) r); } - - // TODO: 2016/1/10 publish remoter change event to redis } /** @@ -68,11 +66,10 @@ public void kickLocal(final String userId, final LocalRouter router) { message.send(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - //future.channel().close(); if (future.isSuccess()) { - LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId={}, router={}", userId, router); + LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId={}, router={}", userId, router); } else { - LoggerManage.info(LogType.CONNECTION, "kick local connection failure, userId={}, router={}", userId, router); + LoggerManage.info(LogType.CONNECTION, "kick local connection failure, userId={}, router={}", userId, router); } } }); @@ -91,7 +88,7 @@ public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(MPushUtil.getLocalIp())) { - LoggerManage.info(LogType.CONNECTION, "kick remote user but router in local, userId={}", userId); + LoggerManage.info(LogType.CONNECTION, "kick remote user but router in local, userId={}", userId); return; } @@ -115,7 +112,7 @@ public void kickRemote(String userId, RemoteRouter router) { public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); return; } @@ -124,14 +121,14 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); LocalRouter router = routerManager.lookup(userId); if (router != null) { - LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, msg={}", msg); + LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, msg={}", msg); //2.1删除本地路由信息 routerManager.unRegister(userId); //2.2发送踢人消息到客户端 kickLocal(userId, router); remStatUser(userId); } else { - LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg={}", msg); + LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg={}", msg); } } @@ -142,14 +139,14 @@ public void onMessage(String channel, String message) { if (msg != null) { onReceiveKickRemoteMsg(msg); } else { - LoggerManage.info(LogType.CONNECTION, "receive an error kick message={}", message); + LoggerManage.info(LogType.CONNECTION, "receive an error kick message={}", message); } } else { - LoggerManage.info(LogType.CONNECTION, "receive an error redis channel={}",channel); + LoggerManage.info(LogType.CONNECTION, "receive an error redis channel={}", channel); } } - - private void remStatUser(String userId){ - RedisManage.zRem(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp()), userId); + + private void remStatUser(String userId) { + RedisManage.zRem(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp()), userId); } } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java deleted file mode 100644 index 73304ab1..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserManager.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shinemo.mpush.core.router; - -import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.event.UserOfflineEvent; -import com.shinemo.mpush.api.event.UserOnlineEvent; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/23. - */ -public final class UserManager extends com.shinemo.mpush.common.manage.user.UserManager { - public static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); - - public static final String ONLINE_CHANNEL = "/mpush/online/"; - - public static final String OFFLINE_CHANNEL = "/mpush/offline/"; - - public UserManager() { - EventBus.INSTANCE.register(this); - init(); - } - - @Subscribe - void handlerUserOnlineEvent(UserOnlineEvent event) { - userOnline(event.getUserId()); - RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); - } - - @Subscribe - void handlerUserOfflineEvent(UserOfflineEvent event) { -// if (event.getUserId() == null) {//链接超时 -// String userId = RouterCenter.INSTANCE.getLocalRouterManager().getUserIdByConnId(event.getConnection().getId()); -// if (StringUtils.isNotBlank(userId)) { -// userOffline(userId); -// RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); -// } -// } else { //解绑用户 -// userOffline(event.getUserId()); -// RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); -// } - if(event.getUserId()==null){ - return; - } - userOffline(event.getUserId()); - RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); - } -} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java new file mode 100644 index 00000000..77d6836e --- /dev/null +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java @@ -0,0 +1,37 @@ +package com.shinemo.mpush.core.router; + +import com.google.common.eventbus.Subscribe; +import com.shinemo.mpush.api.event.UserOfflineEvent; +import com.shinemo.mpush.api.event.UserOnlineEvent; +import com.shinemo.mpush.common.EventBus; +import com.shinemo.mpush.common.manage.user.UserManager; +import com.shinemo.mpush.tools.redis.manage.RedisManage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/23. + */ +public final class UserOnlineOfflineListener { + public static final Logger LOGGER = LoggerFactory.getLogger(UserOnlineOfflineListener.class); + + public static final String ONLINE_CHANNEL = "/mpush/online/"; + + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; + + public UserOnlineOfflineListener() { + EventBus.INSTANCE.register(this); + } + + @Subscribe + void onUserOnline(UserOnlineEvent event) { + UserManager.INSTANCE.userOnline(event.getUserId()); + RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); + } + + @Subscribe + void onUserOffline(UserOfflineEvent event) { + UserManager.INSTANCE.userOffline(event.getUserId()); + RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); + } +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 0ab799b3..77b31092 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -1,7 +1,6 @@ package com.shinemo.mpush.core.server; -import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.common.MessageDispatcher; @@ -10,9 +9,6 @@ import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.manage.RedisManage; - import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -34,11 +30,6 @@ public ConnectionServer(int port) { public void init() { super.init(); connectionManager.init(); - //重置在线数 -// RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); - //删除已经存在的数据 - RedisManage.del(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); - MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); From 90cf62df3eb2c3c74848b162d7908794ba6c10b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 9 Mar 2016 06:58:54 +0000 Subject: [PATCH 498/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/manage/user/UserManager.java | 21 +++++++++---------- .../router/UserOnlineOfflineListener.java | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java index 385f1e78..2363982f 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java @@ -11,40 +11,39 @@ //查询使用 public final class UserManager { + private static final Logger log = LoggerFactory.getLogger(UserManager.class); public static final UserManager INSTANCE = new UserManager(); - private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); - private static final Logger log = LoggerFactory.getLogger(UserManager.class); + private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); public UserManager() { - //重置在线数 -// RedisManage.set(RedisKey.getConnNum(MPushUtil.getExtranetAddress()), 0); - //删除已经存在的数据 + clearUserOnlineData(); + } + + public void clearUserOnlineData() { RedisManage.del(ONLINE_KEY); - log.info("init redis key:{}" + ONLINE_KEY); } - public void userOnline(String userId) { + public void recordUserOnline(String userId) { RedisManage.zAdd(ONLINE_KEY, userId); log.info("user online {}", userId); } - public void userOffline(String userId) { + public void recordUserOffline(String userId) { RedisManage.zRem(ONLINE_KEY, userId); log.info("user offline {}", userId); } //在线用户 - public long onlineUserNum() { + public long getOnlineUserNum() { return RedisManage.zCard(ONLINE_KEY); } //在线用户列表 - public List onlineUserList(int start, int size) { + public List getOnlineUserList(int start, int size) { if (size < 10) { size = 10; } return RedisManage.zrange(ONLINE_KEY, start, size - 1, String.class); } - } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java index 77d6836e..68047515 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java @@ -25,13 +25,13 @@ public UserOnlineOfflineListener() { @Subscribe void onUserOnline(UserOnlineEvent event) { - UserManager.INSTANCE.userOnline(event.getUserId()); + UserManager.INSTANCE.recordUserOnline(event.getUserId()); RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe void onUserOffline(UserOfflineEvent event) { - UserManager.INSTANCE.userOffline(event.getUserId()); + UserManager.INSTANCE.recordUserOffline(event.getUserId()); RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); } } From 39f10f7837e71c573fe25d7a3760306d7df670a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 15:03:50 +0800 Subject: [PATCH 499/890] =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=9F=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/common/security/AesCipher.java | 15 ++++++++-- .../mpush/core/handler/HttpProxyHandler.java | 30 +++++++++---------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index e94378eb..ac44286f 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -1,6 +1,7 @@ package com.shinemo.mpush.common.security; import com.shinemo.mpush.api.connection.Cipher; +import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.tools.crypto.AESUtils; @@ -18,12 +19,22 @@ public AesCipher(byte[] key, byte[] iv) { @Override public byte[] decrypt(byte[] data) { - return AESUtils.decrypt(data, key, iv); + try{ + Profiler.enter("start aes decrypt"); + return AESUtils.decrypt(data, key, iv); + }finally{ + Profiler.release(); + } } @Override public byte[] encrypt(byte[] data) { - return AESUtils.encrypt(data, key, iv); + try{ + Profiler.enter("start encrypt"); + return AESUtils.encrypt(data, key, iv); + }finally{ + Profiler.release(); + } } @Override diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index d6f004ec..b1984899 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -48,24 +48,24 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { @Override public void handle(HttpRequestMessage message) { - String method = message.getMethod(); - String uri = message.uri; - if (Strings.isNullOrEmpty(uri)) { - HttpResponseMessage - .from(message) - .setStatusCode(400) - .setReasonPhrase("Bad Request") - .sendRaw(); - LOGGER.warn("request url is empty!"); - } - - uri = doDnsMapping(uri); - FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); - setHeaders(request, message); - setBody(request, message); try { Profiler.enter("start http proxy handler"); + String method = message.getMethod(); + String uri = message.uri; + if (Strings.isNullOrEmpty(uri)) { + HttpResponseMessage + .from(message) + .setStatusCode(400) + .setReasonPhrase("Bad Request") + .sendRaw(); + LOGGER.warn("request url is empty!"); + } + + uri = doDnsMapping(uri); + FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); + setHeaders(request, message); + setBody(request, message); httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage From d8e3702943e5c3ec25d366796fe2f2f6a140744e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 15:13:27 +0800 Subject: [PATCH 500/890] bug fix --- .../java/com/shinemo/mpush/core/handler/HttpProxyHandler.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index b1984899..4ff0cabf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -66,7 +66,10 @@ public void handle(HttpRequestMessage message) { FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); setHeaders(request, message); setBody(request, message); + + Profiler.enter("start http proxy request"); httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); + Profiler.release(); } catch (Exception e) { HttpResponseMessage .from(message) From f589c5fcf37b31cf9e453ef19f2a1bbd7b420a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 15:25:48 +0800 Subject: [PATCH 501/890] bug fix --- .../java/com/shinemo/mpush/core/handler/HttpProxyHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 4ff0cabf..309462de 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -64,8 +64,12 @@ public void handle(HttpRequestMessage message) { uri = doDnsMapping(uri); FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); + Profiler.enter("start set full http headers"); setHeaders(request, message); + Profiler.release(); + Profiler.enter("start set full http body"); setBody(request, message); + Profiler.release(); Profiler.enter("start http proxy request"); httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); From 69b3e3e80078a40c929571224afd58f87aa06eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 9 Mar 2016 07:33:55 +0000 Subject: [PATCH 502/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/netty/client/NettyHttpClient.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 366a850b..42b9b9b8 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -82,14 +82,14 @@ public void request(final RequestInfo info) throws Exception { timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); Channel channel = tryAcquire(host); if (channel == null) { - final long startConnectChannel = System.currentTimeMillis(); + final long startConnectChannel = System.currentTimeMillis(); LOGGER.debug("create new channel, host={}", host); ChannelFuture f = b.connect(host, port); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - LOGGER.debug("create new channel cost:"+(System.currentTimeMillis()-startConnectChannel)); - if (future.isSuccess()) { + LOGGER.debug("create new channel cost:" + (System.currentTimeMillis() - startConnectChannel)); + if (future.isSuccess()) { writeRequest(future.channel(), info); } else { info.tryDone(); @@ -154,22 +154,21 @@ private class HttpClientHandler extends ChannelHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - LOGGER.error("http client caught an error, info={}", info, cause); try { - if (info.tryDone()) { + if (info != null && info.tryDone()) { info.onException(cause); } } finally { tryRelease(ctx.channel()); } + LOGGER.error("http client caught an error, info={}", info, cause); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - if (info == null) return; try { - if (info.tryDone()) { + if (info != null && info.tryDone()) { HttpResponse response = (HttpResponse) msg; if (isRedirect(response)) { if (info.onRedirect(response)) { From 90894f21fa0343a1f2f8e04df3cb221e3fa014d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 15:34:56 +0800 Subject: [PATCH 503/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/common/MessageDispatcher.java | 2 ++ .../shinemo/mpush/common/handler/BaseMessageHandler.java | 2 +- .../java/com/shinemo/mpush/common/message/BaseMessage.java | 2 +- .../java/com/shinemo/mpush/common/security/AesCipher.java | 2 +- .../java/com/shinemo/mpush/common/security/RsaCipher.java | 6 +++--- .../com/shinemo/mpush/core/handler/HttpProxyHandler.java | 2 +- .../com/shinemo/mpush/core/server/ServerChannelHandler.java | 2 +- .../src/main/java/com/shinemo/mpush/tools/MPushUtil.java | 3 +++ .../src/main/java/com/shinemo/mpush/tools}/Profiler.java | 2 +- 9 files changed, 14 insertions(+), 9 deletions(-) rename {mpush-common/src/main/java/com/shinemo/mpush/common => mpush-tools/src/main/java/com/shinemo/mpush/tools}/Profiler.java (99%) diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java index 411170cf..45ad18b3 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java @@ -6,6 +6,8 @@ import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.message.ErrorMessage; +import com.shinemo.mpush.tools.Profiler; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java index 9278b277..b45df761 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java @@ -5,7 +5,7 @@ import com.shinemo.mpush.api.MessageHandler; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.Profiler; +import com.shinemo.mpush.tools.Profiler; /** * Created by ohun on 2015/12/22. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java index 2423ef17..2362e8cd 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java @@ -4,8 +4,8 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.SessionContext; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.tools.IOUtils; +import com.shinemo.mpush.tools.Profiler; import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java index ac44286f..40b9e8f2 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.common.security; import com.shinemo.mpush.api.connection.Cipher; -import com.shinemo.mpush.common.Profiler; +import com.shinemo.mpush.tools.Profiler; import com.shinemo.mpush.tools.crypto.AESUtils; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java index 0d99f3bd..0b587b66 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java @@ -1,7 +1,7 @@ package com.shinemo.mpush.common.security; import com.shinemo.mpush.api.connection.Cipher; -import com.shinemo.mpush.common.Profiler; +import com.shinemo.mpush.tools.Profiler; import com.shinemo.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; @@ -23,7 +23,7 @@ public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @Override public byte[] decrypt(byte[] data) { try{ - Profiler.enter("start decrypt"); + Profiler.enter("start rsa decrypt"); return RSAUtils.decryptByPrivateKey(data, privateKey); }finally{ Profiler.release(); @@ -34,7 +34,7 @@ public byte[] decrypt(byte[] data) { @Override public byte[] encrypt(byte[] data) { try{ - Profiler.enter("start encrypt"); + Profiler.enter("start rsa encrypt"); return RSAUtils.encryptByPublicKey(data, publicKey); }finally{ Profiler.release(); diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 309462de..68834b5a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -4,7 +4,6 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; import com.shinemo.mpush.common.DnsMapping; -import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.HttpRequestMessage; import com.shinemo.mpush.common.message.HttpResponseMessage; @@ -14,6 +13,7 @@ import com.shinemo.mpush.netty.client.HttpClient; import com.shinemo.mpush.netty.client.RequestInfo; import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.Profiler; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 7630e977..7f420b87 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -7,10 +7,10 @@ import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.PacketReceiver; import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.Profiler; import com.shinemo.mpush.log.LogType; import com.shinemo.mpush.log.LoggerManage; import com.shinemo.mpush.netty.connection.NettyConnection; +import com.shinemo.mpush.tools.Profiler; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 64e2a872..de595d60 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -54,6 +54,7 @@ public static int getHeartbeat(int min, int max) { */ public static String getInetAddress() { try { + com.shinemo.mpush.tools.Profiler.enter("start get inet addresss"); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); InetAddress address = null; while (interfaces.hasMoreElements()) { @@ -71,6 +72,8 @@ public static String getInetAddress() { } catch (Throwable e) { LOGGER.error("getInetAddress exception", e); return "127.0.0.1"; + }finally{ + com.shinemo.mpush.tools.Profiler.release(); } } diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Profiler.java similarity index 99% rename from mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java rename to mpush-tools/src/main/java/com/shinemo/mpush/tools/Profiler.java index 571efdb4..1658c3e6 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/Profiler.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/Profiler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common; +package com.shinemo.mpush.tools; import java.text.MessageFormat; From 757593e42020ed36d8dbb4583539edfdb4a567c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 15:42:05 +0800 Subject: [PATCH 504/890] add log --- .../java/com/shinemo/mpush/core/handler/HttpProxyHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 68834b5a..784e4c9a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -156,6 +156,7 @@ public boolean onRedirect(HttpResponse response) { private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { + Profiler.enter("start headers fill"); Map headers = message.headers; if (headers != null) { HttpHeaders httpHeaders = request.headers(); @@ -163,7 +164,10 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { httpHeaders.add(entry.getKey(), entry.getValue()); } } + Profiler.release(); + Profiler.enter("start get remoteAddress"); InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); + Profiler.release(); request.headers().add("x-forwarded-for", remoteAddress.getHostName() + "," + MPushUtil.getLocalIp()); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } From d807bbae461721cbae2d07fa23902433503a0c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 15:45:53 +0800 Subject: [PATCH 505/890] add log --- .../java/com/shinemo/mpush/core/handler/HttpProxyHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 784e4c9a..37c1615f 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -168,8 +168,12 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { Profiler.enter("start get remoteAddress"); InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); Profiler.release(); + Profiler.enter("start set x-forwarded-for"); request.headers().add("x-forwarded-for", remoteAddress.getHostName() + "," + MPushUtil.getLocalIp()); + Profiler.release(); + Profiler.enter("start set x-forwarded-port"); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); + Profiler.release(); } private void setBody(FullHttpRequest request, HttpRequestMessage message) { From a9fec7d8b9f8d818378c485a9ef722ff1893325f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 16:14:54 +0800 Subject: [PATCH 506/890] add log --- .../shinemo/mpush/core/handler/HttpProxyHandler.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 37c1615f..47b3bb97 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -156,7 +156,6 @@ public boolean onRedirect(HttpResponse response) { private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { - Profiler.enter("start headers fill"); Map headers = message.headers; if (headers != null) { HttpHeaders httpHeaders = request.headers(); @@ -164,16 +163,14 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { httpHeaders.add(entry.getKey(), entry.getValue()); } } - Profiler.release(); - Profiler.enter("start get remoteAddress"); InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); - Profiler.release(); Profiler.enter("start set x-forwarded-for"); - request.headers().add("x-forwarded-for", remoteAddress.getHostName() + "," + MPushUtil.getLocalIp()); + String remoteHostName = remoteAddress.getHostName(); + LOGGER.error("http request remoteHostName:"+remoteHostName); + request.headers().add("x-forwarded-for", remoteHostName); +// request.headers().add("x-forwarded-for", remoteAddress.getAddress().getHostAddress() + "," + MPushUtil.getLocalIp()); Profiler.release(); - Profiler.enter("start set x-forwarded-port"); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); - Profiler.release(); } private void setBody(FullHttpRequest request, HttpRequestMessage message) { From fe4d53333a01117fcf473a462de0e2c3ed829832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 16:20:36 +0800 Subject: [PATCH 507/890] bug fix --- .../com/shinemo/mpush/core/handler/HttpProxyHandler.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 47b3bb97..f30123d6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -165,10 +165,10 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { } InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); Profiler.enter("start set x-forwarded-for"); - String remoteHostName = remoteAddress.getHostName(); - LOGGER.error("http request remoteHostName:"+remoteHostName); - request.headers().add("x-forwarded-for", remoteHostName); -// request.headers().add("x-forwarded-for", remoteAddress.getAddress().getHostAddress() + "," + MPushUtil.getLocalIp()); +// String remoteHostName = remoteAddress.getHostName(); +// LOGGER.error("http request remoteHostName:"+remoteHostName); +// request.headers().add("x-forwarded-for", remoteHostName); + request.headers().add("x-forwarded-for", remoteAddress.getAddress().getHostAddress() + "," + MPushUtil.getLocalIp()); Profiler.release(); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } From 83edaa29a01613144b4f27c9c41c284e1d2b12b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 16:36:19 +0800 Subject: [PATCH 508/890] bug fix --- .../com/shinemo/mpush/core/handler/HttpProxyHandler.java | 7 +++---- .../shinemo/mpush/core/server/ServerChannelHandler.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index f30123d6..a56902a7 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -165,10 +165,9 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { } InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); Profiler.enter("start set x-forwarded-for"); -// String remoteHostName = remoteAddress.getHostName(); -// LOGGER.error("http request remoteHostName:"+remoteHostName); -// request.headers().add("x-forwarded-for", remoteHostName); - request.headers().add("x-forwarded-for", remoteAddress.getAddress().getHostAddress() + "," + MPushUtil.getLocalIp()); + String remoteIp = remoteAddress.getAddress().getHostAddress(); + request.headers().add("x-forwarded-for", remoteIp); +// request.headers().add("x-forwarded-for", remoteIp + "," + MPushUtil.getLocalIp()); Profiler.release(); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java index 7f420b87..4315e5ae 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java @@ -51,7 +51,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception }finally{ Profiler.release(); long duration = Profiler.getDuration(); - if(duration>100){ + if(duration>80){ LOGGER.error("end channel read:"+duration+","+Profiler.dump()); } Profiler.reset(); From c844800351bcd62c40c14fa7ddef0d817aee9896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 17:00:11 +0800 Subject: [PATCH 509/890] add else --- .../java/com/shinemo/mpush/netty/client/NettyHttpClient.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 42b9b9b8..ecc9efb8 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -114,6 +114,8 @@ private synchronized Channel tryAcquire(String host) { LOGGER.debug("tryAcquire channel success ,host={}", host); channel.attr(hostKey).set(host); return channel; + }else{ + LOGGER.error("tryAcquire channel false channel is not active,host={}",host); } } return null; From 70434f861a063650cf2429f3aa1f69fdaa4b5524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 9 Mar 2016 09:03:09 +0000 Subject: [PATCH 510/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/netty/client/NettyHttpClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index ecc9efb8..6fdd3e7e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -114,8 +114,8 @@ private synchronized Channel tryAcquire(String host) { LOGGER.debug("tryAcquire channel success ,host={}", host); channel.attr(hostKey).set(host); return channel; - }else{ - LOGGER.error("tryAcquire channel false channel is not active,host={}",host); + } else {//链接由于意外情况不可用了,keepAlive_timeout + LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); } } return null; From fbd3b231ec123f3395ba57793da871cfc33cca5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 9 Mar 2016 09:18:43 +0000 Subject: [PATCH 511/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shinemo/mpush/netty/client/NettyHttpClient.java | 2 +- .../src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 6fdd3e7e..b855bb64 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -111,7 +111,7 @@ private synchronized Channel tryAcquire(String host) { Channel channel = it.next(); it.remove(); if (channel.isActive()) { - LOGGER.debug("tryAcquire channel success ,host={}", host); + LOGGER.debug("tryAcquire channel success, host={}", host); channel.attr(hostKey).set(host); return channel; } else {//链接由于意外情况不可用了,keepAlive_timeout diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java index 37c1468e..475810d8 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java @@ -46,7 +46,7 @@ public void testCompress() throws Exception { for (int i = 0; i < 100; i++) { IOUtils.compress(s); } - System.out.println((System.currentTimeMillis()-t1)); + System.out.println((System.currentTimeMillis()-t1)/100); System.out.println("src:" + s.length); byte[] out = IOUtils.compress(s); System.out.println("compress:" + out.length); From 6d80fba345d37b8281b921ba91cc87ec3e2e5cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 17:46:03 +0800 Subject: [PATCH 512/890] =?UTF-8?q?add=20press=20=E5=9F=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/tools/IOUtils.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java index e2ce0c4f..b742ec05 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java @@ -27,6 +27,9 @@ public static void close(Closeable closeable) { } public static byte[] compress(byte[] data) { + + Profiler.enter("start compress"); + ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); DeflaterOutputStream zipOut = new DeflaterOutputStream(out); try { @@ -38,11 +41,13 @@ public static byte[] compress(byte[] data) { return Constants.EMPTY_BYTES; } finally { close(zipOut); + Profiler.release(); } return out.toByteArray(); } public static byte[] uncompress(byte[] data) { + Profiler.enter("start uncompress"); InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4); byte[] buffer = new byte[1024]; @@ -56,6 +61,7 @@ public static byte[] uncompress(byte[] data) { return Constants.EMPTY_BYTES; } finally { close(zipIn); + Profiler.release(); } return out.toByteArray(); } From b315479011ac2fbf49596c35db6cf74d00997da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Wed, 9 Mar 2016 20:24:47 +0800 Subject: [PATCH 513/890] bug fix --- .../java/com/shinemo/mpush/netty/client/NettyHttpClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index b855bb64..d5a88e27 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -76,7 +76,7 @@ public void stop() { public void request(final RequestInfo info) throws Exception { URI uri = new URI(info.request.uri()); String host = info.host = uri.getHost(); - int port = uri.getPort() == -1 ? 80 : uri.getPort(); + int port = uri.getPort() == -1 ? 81 : uri.getPort(); info.request.headers().set(HttpHeaderNames.HOST, host); info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); From 993154e81c69ee1fb7f888d8f879c67b4110d784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 9 Mar 2016 14:51:04 +0000 Subject: [PATCH 514/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/HttpProxyHandler.java | 4 ++-- .../mpush/netty/client/NettyHttpClient.java | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index a56902a7..cc720761 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -23,6 +23,7 @@ import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.util.Map; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; @@ -167,7 +168,6 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { Profiler.enter("start set x-forwarded-for"); String remoteIp = remoteAddress.getAddress().getHostAddress(); request.headers().add("x-forwarded-for", remoteIp); -// request.headers().add("x-forwarded-for", remoteIp + "," + MPushUtil.getLocalIp()); Profiler.release(); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } @@ -191,6 +191,6 @@ private String doDnsMapping(String url) { String host = uri.getHost(); String localHost = dnsMapping.translate(host); if (localHost == null) return url; - return url.replace(host, localHost); + return url.replaceFirst(host, localHost); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index d5a88e27..0a489e7e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -29,16 +29,18 @@ */ public class NettyHttpClient implements HttpClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); - private Bootstrap b; - private EventLoopGroup workerGroup; - private Timer timer; + + private final int maxConnPerHost = ConfigCenter.holder.maxHttpConnCountPerHost(); private final AttributeKey requestKey = AttributeKey.newInstance("requestInfo"); private final AttributeKey hostKey = AttributeKey.newInstance("host"); private final ArrayListMultimap channelPool = ArrayListMultimap.create(); - private final int maxConnPerHost = ConfigCenter.holder.maxHttpConnCountPerHost(); + + private Bootstrap b; + private EventLoopGroup workerGroup; + private Timer timer; @Override - public void start() { // TODO: 2016/2/15 yxx 配置线程池 + public void start() { workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.httpExecutor); b = new Bootstrap(); b.group(workerGroup); @@ -46,14 +48,13 @@ public void start() { // TODO: 2016/2/15 yxx 配置线程池 b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_REUSEADDR, true); - b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.handler(new ChannelInitializer() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new HttpResponseDecoder()); - ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 20)); + ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 5));//5M ch.pipeline().addLast("encoder", new HttpRequestEncoder()); ch.pipeline().addLast("handler", new HttpClientHandler()); } @@ -128,7 +129,7 @@ private synchronized void tryRelease(Channel channel) { LOGGER.debug("tryRelease channel success, host={}", host); channelPool.put(host, channel); } else { - LOGGER.debug("tryRelease channel over limit={}, host={}, channel closed.", maxConnPerHost, host); + LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); channel.close(); } } From 03d75a70a2a86a7ea2c437da63da3914c11bb103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 10 Mar 2016 02:20:08 +0000 Subject: [PATCH 515/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shinemo/mpush/core/handler/HttpProxyHandler.java | 12 ++++-------- .../shinemo/mpush/netty/client/NettyHttpClient.java | 4 ++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index cc720761..e72cfef4 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -12,18 +12,14 @@ import com.shinemo.mpush.netty.client.HttpCallback; import com.shinemo.mpush.netty.client.HttpClient; import com.shinemo.mpush.netty.client.RequestInfo; -import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Profiler; - import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; - import org.slf4j.Logger; import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.Map; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; @@ -51,7 +47,7 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { public void handle(HttpRequestMessage message) { try { - Profiler.enter("start http proxy handler"); + Profiler.enter("start http proxy handler"); String method = message.getMethod(); String uri = message.uri; if (Strings.isNullOrEmpty(uri)) { @@ -71,7 +67,7 @@ public void handle(HttpRequestMessage message) { Profiler.enter("start set full http body"); setBody(request, message); Profiler.release(); - + Profiler.enter("start http proxy request"); httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); Profiler.release(); @@ -82,8 +78,8 @@ public void handle(HttpRequestMessage message) { .setReasonPhrase("Bad Gateway") .sendRaw(); LOGGER.error("send request ex, message=" + message, e); - }finally{ - Profiler.release(); + } finally { + Profiler.release(); } } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 0a489e7e..c07f967d 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -31,7 +31,7 @@ public class NettyHttpClient implements HttpClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); private final int maxConnPerHost = ConfigCenter.holder.maxHttpConnCountPerHost(); - private final AttributeKey requestKey = AttributeKey.newInstance("requestInfo"); + private final AttributeKey requestKey = AttributeKey.newInstance("request"); private final AttributeKey hostKey = AttributeKey.newInstance("host"); private final ArrayListMultimap channelPool = ArrayListMultimap.create(); @@ -77,7 +77,7 @@ public void stop() { public void request(final RequestInfo info) throws Exception { URI uri = new URI(info.request.uri()); String host = info.host = uri.getHost(); - int port = uri.getPort() == -1 ? 81 : uri.getPort(); + int port = uri.getPort() == -1 ? 80 : uri.getPort(); info.request.headers().set(HttpHeaderNames.HOST, host); info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); From 8be157ab2d5abc318d1e6fb1371142eda0f96640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 10 Mar 2016 11:58:38 +0800 Subject: [PATCH 516/890] add dnsmapping --- .../com/shinemo/mpush/common/DnsMapping.java | 78 +++++++-------- .../mpush/core/handler/HttpProxyHandler.java | 13 +-- .../mpush/cs/ConnectionServerMain.java | 8 ++ .../mpush/netty/client/NettyHttpClient.java | 2 +- .../test/configcenter/ConfigCenterTest.java | 13 +++ .../shinemo/mpush/test/util/TelnetTest.java | 32 ++++++ .../src/test/resources/config.properties | 2 + mpush-tools/pom.xml | 4 + .../com/shinemo/mpush/tools/MPushUtil.java | 20 ++++ .../mpush/tools/config/ConfigCenter.java | 6 +- .../tools/config/DnsMappingConverter.java | 49 +++++++++ .../shinemo/mpush/tools/dns/DnsMapping.java | 25 +++++ .../tools/dns/manage/DnsMappingManage.java | 99 +++++++++++++++++++ pom.xml | 6 ++ 14 files changed, 308 insertions(+), 49 deletions(-) create mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java b/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java index ce9c88ed..72f01775 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java +++ b/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java @@ -1,39 +1,39 @@ -package com.shinemo.mpush.common; - -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import com.google.common.collect.ArrayListMultimap; -import com.shinemo.mpush.tools.config.ConfigCenter; - -import java.util.List; -import java.util.Map; - -/** - * Created by ohun on 2016/2/16. - */ -public final class DnsMapping { - private final ArrayListMultimap mappings = ArrayListMultimap.create(); - - public DnsMapping() { - String dnsString = ConfigCenter.holder.dnsMapping(); - if (Strings.isNullOrEmpty(dnsString)) return; - - Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); - Splitter vsp = Splitter.on(','); - for (Map.Entry entry : map.entrySet()) { - String value = entry.getValue(); - if (Strings.isNullOrEmpty(value)) continue; - Iterable it = vsp.split(entry.getValue()); - mappings.putAll(entry.getKey(), it); - } - } - - public String translate(String origin) { - if (mappings.isEmpty()) return null; - List list = mappings.get(origin); - if (list == null || list.isEmpty()) return null; - int L = list.size(); - if (L == 1) return list.get(0); - return list.get((int) (Math.random() * L % L)); - } -} +//package com.shinemo.mpush.common; +// +//import com.google.common.base.Splitter; +//import com.google.common.base.Strings; +//import com.google.common.collect.ArrayListMultimap; +//import com.shinemo.mpush.tools.config.ConfigCenter; +// +//import java.util.List; +//import java.util.Map; +// +///** +// * Created by ohun on 2016/2/16. +// */ +//public final class DnsMapping { +// private final ArrayListMultimap mappings = ArrayListMultimap.create(); +// +// public DnsMapping() { +// String dnsString = ConfigCenter.holder.dnsMapping(); +// if (Strings.isNullOrEmpty(dnsString)) return; +// +// Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); +// Splitter vsp = Splitter.on(','); +// for (Map.Entry entry : map.entrySet()) { +// String value = entry.getValue(); +// if (Strings.isNullOrEmpty(value)) continue; +// Iterable it = vsp.split(entry.getValue()); +// mappings.putAll(entry.getKey(), it); +// } +// } +// +// public String translate(String origin) { +// if (mappings.isEmpty()) return null; +// List list = mappings.get(origin); +// if (list == null || list.isEmpty()) return null; +// int L = list.size(); +// if (L == 1) return list.get(0); +// return list.get((int) (Math.random() * L % L)); +// } +//} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index cc720761..07576c32 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -3,7 +3,6 @@ import com.google.common.base.Strings; import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.DnsMapping; import com.shinemo.mpush.common.handler.BaseMessageHandler; import com.shinemo.mpush.common.message.HttpRequestMessage; import com.shinemo.mpush.common.message.HttpResponseMessage; @@ -12,8 +11,9 @@ import com.shinemo.mpush.netty.client.HttpCallback; import com.shinemo.mpush.netty.client.HttpClient; import com.shinemo.mpush.netty.client.RequestInfo; -import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.Profiler; +import com.shinemo.mpush.tools.dns.DnsMapping; +import com.shinemo.mpush.tools.dns.manage.DnsMappingManage; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; @@ -23,7 +23,6 @@ import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.Map; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; @@ -34,10 +33,8 @@ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = LoggerManage.getLog(LogType.HTTP); private final HttpClient httpClient; - private final DnsMapping dnsMapping; public HttpProxyHandler(HttpClient httpClient) { - this.dnsMapping = new DnsMapping(); this.httpClient = httpClient; this.httpClient.start(); } @@ -189,8 +186,8 @@ private String doDnsMapping(String url) { } if (uri == null) return url; String host = uri.getHost(); - String localHost = dnsMapping.translate(host); - if (localHost == null) return url; - return url.replaceFirst(host, localHost); + DnsMapping mapping = DnsMappingManage.holder.translate(host); + if (mapping == null) return url; + return url.replaceFirst(host, mapping.toString()); } } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index b7a30893..f49adbb6 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -2,6 +2,7 @@ +import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.Server; import com.shinemo.mpush.common.AbstractServer; import com.shinemo.mpush.conn.client.ConnectionServerApplication; @@ -10,7 +11,10 @@ import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.dns.manage.DnsMappingManage; +import com.shinemo.mpush.tools.redis.manage.RedisManage; public class ConnectionServerMain extends AbstractServer{ @@ -40,6 +44,10 @@ public void start() { startServer(gatewayServer,gatewayServerApplication.getServerRegisterZkPath(),Jsons.toJson(gatewayServerApplication)); // registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); startServer(adminServer); + + RedisManage.del(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp())); + DnsMappingManage.holder.init(); + } diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java index 0a489e7e..23fc9f6a 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java @@ -77,7 +77,7 @@ public void stop() { public void request(final RequestInfo info) throws Exception { URI uri = new URI(info.request.uri()); String host = info.host = uri.getHost(); - int port = uri.getPort() == -1 ? 81 : uri.getPort(); + int port = uri.getPort() == -1 ? 80 : uri.getPort(); info.request.headers().set(HttpHeaderNames.HOST, host); info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java index 281a4ca7..3ee4a336 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java @@ -1,8 +1,13 @@ package com.shinemo.mpush.test.configcenter; +import java.util.List; +import java.util.Map; + import org.junit.Test; +import com.shinemo.mpush.tools.Jsons; import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.dns.DnsMapping; public class ConfigCenterTest { @@ -12,5 +17,13 @@ public void test(){ System.out.println(ConfigCenter.holder.forceWriteRedisGroupInfo()); } + + @Test + public void testDnsMapping(){ + Map> ret = ConfigCenter.holder.dnsMapping(); + + System.out.println(Jsons.toJson(ret)); + + } } diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java new file mode 100644 index 00000000..3d82812e --- /dev/null +++ b/mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java @@ -0,0 +1,32 @@ +package com.shinemo.mpush.test.util; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.Test; + +import com.shinemo.mpush.tools.MPushUtil; + +public class TelnetTest { + + @Test + public void test(){ + boolean ret = MPushUtil.telnet("120.27.196.68", 82); + System.out.println(ret); + } + + @Test + public void test2(){ + boolean ret = MPushUtil.telnet("120.27.196.68", 80); + System.out.println(ret); + } + + @Test + public void uriTest() throws URISyntaxException{ + String url = "http://127.0.0.1"; + URI uri = new URI(url); + System.out.println(uri.getPort()); + } + + +} diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index b053a273..d2776999 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -32,3 +32,5 @@ zk_digest = shinemoIpo #redis_group = 111.1.57.148:6379:ShineMoIpo redis_group = 127.0.0.1:6379:shinemoIpo force_write_redis_group_info = false +dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 + diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 77c4d16e..22ddc3ef 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -50,6 +50,10 @@ org.aeonbits.owner owner
+ + commons-net + commons-net +
diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index de595d60..8865e721 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -1,10 +1,12 @@ package com.shinemo.mpush.tools; +import org.apache.commons.net.telnet.TelnetClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.shinemo.mpush.tools.config.ConfigCenter; +import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; @@ -140,4 +142,22 @@ public static Map headerFromString(String headersString) { } return headers; } + + public static boolean telnet(String ip,int port){ + TelnetClient client = new TelnetClient(); + try{ + client.connect(ip, port); + return true; + }catch(Exception e){ + return false; + }finally{ + try { + if(client.isConnected()){ + client.disconnect(); + } + } catch (IOException e) { + LOGGER.error("disconnect error",e); + } + } + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index b622e697..93b14401 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -1,12 +1,15 @@ package com.shinemo.mpush.tools.config; +import com.shinemo.mpush.tools.dns.DnsMapping; import com.shinemo.mpush.tools.redis.RedisGroup; + import org.aeonbits.owner.Config; import org.aeonbits.owner.Config.Sources; import org.aeonbits.owner.ConfigFactory; import java.util.List; +import java.util.Map; /** * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 @@ -101,7 +104,8 @@ public interface ConfigCenter extends Config { String logPath(); @Key("dns_mapping") - String dnsMapping(); + @ConverterClass(DnsMappingConverter.class) + Map> dnsMapping(); @Key("max_http_client_conn_count_per_host") @DefaultValue("5") diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java new file mode 100644 index 00000000..ea04f2f3 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java @@ -0,0 +1,49 @@ +package com.shinemo.mpush.tools.config; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.aeonbits.owner.Converter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Function; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.primitives.Ints; +import com.shinemo.mpush.tools.dns.DnsMapping; + +public class DnsMappingConverter implements Converter>>{ + +private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); + + @Override + public Map> convert(Method method, String input) { + + log.warn("method:"+method.getName()+","+input); + Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); + Map> result = Maps.newConcurrentMap(); + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + List dnsMappings = Lists.transform(Arrays.asList(value.split(",")), new Function() { + @Override + public DnsMapping apply(String ipAndPort) { + if(ipAndPort.contains(":")){ + String[] temp = ipAndPort.split(":"); + return new DnsMapping(temp[0], Ints.tryParse(temp[1])); + }else{ + return new DnsMapping(ipAndPort, 80); + } + } + }); + result.put(key, dnsMappings); + } + return result; + + } + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java new file mode 100644 index 00000000..0b44457d --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java @@ -0,0 +1,25 @@ +package com.shinemo.mpush.tools.dns; + + +public class DnsMapping { + + private String ip; + private int port; + + public DnsMapping(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public String getIp() { + return ip; + } + public int getPort() { + return port; + } + + @Override + public String toString() { + return ip+":"+port; + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java new file mode 100644 index 00000000..6c27dbd9 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java @@ -0,0 +1,99 @@ +package com.shinemo.mpush.tools.dns.manage; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.shinemo.mpush.tools.Jsons; +import com.shinemo.mpush.tools.MPushUtil; +import com.shinemo.mpush.tools.config.ConfigCenter; +import com.shinemo.mpush.tools.dns.DnsMapping; + +public class DnsMappingManage { + + private static final Logger LOG = LoggerFactory.getLogger(DnsMappingManage.class); + + private DnsMappingManage() { + } + + public static DnsMappingManage holder = new DnsMappingManage(); + + private Map> all = Maps.newConcurrentMap(); + private Map> available = Maps.newConcurrentMap(); + + private Worker worker = new Worker(); + + private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); + + public void init() { + LOG.error("start init dnsmapping"); + all.putAll(ConfigCenter.holder.dnsMapping()); + available.putAll(ConfigCenter.holder.dnsMapping()); + pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + LOG.error("end init dnsmapping"); + } + + public void update(Map> nowAvailable) { + available = nowAvailable; + } + + public Map> getAll() { + return all; + } + + public DnsMapping translate(String origin) { + if (available.isEmpty()) + return null; + List list = available.get(origin); + if (list == null || list.isEmpty()) + return null; + int L = list.size(); + if (L == 1) + return list.get(0); + return list.get((int) (Math.random() * L % L)); + } + + public void shutdown() { + pool.shutdown(); + } + + public static class Worker implements Runnable { + + private static final Logger log = LoggerFactory.getLogger(Worker.class); + + @Override + public void run() { + + log.debug("start dns mapping telnet"); + + Map> all = DnsMappingManage.holder.getAll(); + + Map> available = Maps.newConcurrentMap(); + + for (Map.Entry> entry : all.entrySet()) { + String key = entry.getKey(); + List value = entry.getValue(); + List nowValue = Lists.newArrayList(); + for(DnsMapping temp:value){ + boolean canTelnet = MPushUtil.telnet(temp.getIp(), temp.getPort()); + if(canTelnet){ + nowValue.add(temp); + }else{ + log.error("dns can not reachable:"+Jsons.toJson(temp)); + } + } + available.put(key, nowValue); + } + + } + + } + +} diff --git a/pom.xml b/pom.xml index 3aa4bfd8..a1e04e75 100644 --- a/pom.xml +++ b/pom.xml @@ -225,6 +225,12 @@ 2.33
+ + commons-net + commons-net + 3.4 + +
From 6fcfd5830ea1d6c962ec13b7ed6568bcdd5da1b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 10 Mar 2016 13:39:09 +0800 Subject: [PATCH 517/890] use 81 port --- conf-online.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf-online.properties b/conf-online.properties index 1428a531..a5e35416 100644 --- a/conf-online.properties +++ b/conf-online.properties @@ -10,6 +10,6 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0 force_write_redis_group_info = false connection_server_port = 3000 gateway_server_port = 4000 -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146,10.161.158.135;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 +dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 skip_dump=false admin_port=4001 From 0f1b2fd3a0c43843e7ce5d5dd10675274582196d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Thu, 10 Mar 2016 13:47:11 +0800 Subject: [PATCH 518/890] update --- .../com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java index 6c27dbd9..ecd25bdb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java @@ -91,6 +91,8 @@ public void run() { } available.put(key, nowValue); } + + DnsMappingManage.holder.update(available); } From 0a58f14218c53414b4d6002710246770e2252dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 10 Mar 2016 05:39:46 +0000 Subject: [PATCH 519/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/cs/ConnectionServerMain.java | 91 +++++----- .../tools/dns/manage/DnsMappingManage.java | 158 +++++++++--------- 2 files changed, 120 insertions(+), 129 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index f49adbb6..63269523 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -1,69 +1,62 @@ package com.shinemo.mpush.cs; - -import com.shinemo.mpush.api.RedisKey; import com.shinemo.mpush.api.Server; import com.shinemo.mpush.common.AbstractServer; +import com.shinemo.mpush.common.manage.user.UserManager; import com.shinemo.mpush.conn.client.ConnectionServerApplication; import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.core.server.AdminServer; import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.GatewayServer; import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.dns.manage.DnsMappingManage; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -public class ConnectionServerMain extends AbstractServer{ - - private Server gatewayServer; - - private Server adminServer; - - private ConnectionServerApplication connectionServerApplication; - - private GatewayServerApplication gatewayServerApplication; - - public ConnectionServerMain(){ - +public class ConnectionServerMain extends AbstractServer { + + private Server gatewayServer; + + private Server adminServer; + + private ConnectionServerApplication connectionServerApplication; + + private GatewayServerApplication gatewayServerApplication; + + public ConnectionServerMain() { + // registerListener(new ConnectionServerPathListener()); // registerListener(new GatewayServerPathListener()); - - connectionServerApplication = (ConnectionServerApplication)application; - gatewayServerApplication = new GatewayServerApplication(); - connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); - gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); - adminServer = new AdminServer(ConfigCenter.holder.adminPort()); - } - - @Override - public void start() { - super.start(); - startServer(gatewayServer,gatewayServerApplication.getServerRegisterZkPath(),Jsons.toJson(gatewayServerApplication)); + + connectionServerApplication = (ConnectionServerApplication) application; + gatewayServerApplication = new GatewayServerApplication(); + connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); + gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); + adminServer = new AdminServer(ConfigCenter.holder.adminPort()); + } + + @Override + public void start() { + super.start(); + startServer(gatewayServer, gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); // registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); - startServer(adminServer); - - RedisManage.del(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp())); - DnsMappingManage.holder.init(); - - } - + startServer(adminServer); + + UserManager.INSTANCE.clearUserOnlineData(); + DnsMappingManage.holder.init(); + } + - @Override - public Server getServer() { - final int port = application.getPort(); - ConnectionServer connectionServer = new ConnectionServer(port); - return connectionServer; - } - - @Override - public void stop() { - super.stop(); - stopServer(gatewayServer); - stopServer(adminServer); - } - + @Override + public Server getServer() { + int port = application.getPort(); + return new ConnectionServer(port); + } + @Override + public void stop() { + super.stop(); + stopServer(gatewayServer); + stopServer(adminServer); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java index ecd25bdb..ca2443d4 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java @@ -17,85 +17,83 @@ import com.shinemo.mpush.tools.dns.DnsMapping; public class DnsMappingManage { - - private static final Logger LOG = LoggerFactory.getLogger(DnsMappingManage.class); - - private DnsMappingManage() { - } - - public static DnsMappingManage holder = new DnsMappingManage(); - - private Map> all = Maps.newConcurrentMap(); - private Map> available = Maps.newConcurrentMap(); - - private Worker worker = new Worker(); - - private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); - - public void init() { - LOG.error("start init dnsmapping"); - all.putAll(ConfigCenter.holder.dnsMapping()); - available.putAll(ConfigCenter.holder.dnsMapping()); - pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns - LOG.error("end init dnsmapping"); - } - - public void update(Map> nowAvailable) { - available = nowAvailable; - } - - public Map> getAll() { - return all; - } - - public DnsMapping translate(String origin) { - if (available.isEmpty()) - return null; - List list = available.get(origin); - if (list == null || list.isEmpty()) - return null; - int L = list.size(); - if (L == 1) - return list.get(0); - return list.get((int) (Math.random() * L % L)); - } - - public void shutdown() { - pool.shutdown(); - } - - public static class Worker implements Runnable { - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - - log.debug("start dns mapping telnet"); - - Map> all = DnsMappingManage.holder.getAll(); - - Map> available = Maps.newConcurrentMap(); - - for (Map.Entry> entry : all.entrySet()) { - String key = entry.getKey(); - List value = entry.getValue(); - List nowValue = Lists.newArrayList(); - for(DnsMapping temp:value){ - boolean canTelnet = MPushUtil.telnet(temp.getIp(), temp.getPort()); - if(canTelnet){ - nowValue.add(temp); - }else{ - log.error("dns can not reachable:"+Jsons.toJson(temp)); - } - } - available.put(key, nowValue); - } - - DnsMappingManage.holder.update(available); - - } - - } + private static final Logger LOG = LoggerFactory.getLogger(DnsMappingManage.class); + + private DnsMappingManage() { + } + + public static final DnsMappingManage holder = new DnsMappingManage(); + + private Map> all = Maps.newConcurrentMap(); + private Map> available = Maps.newConcurrentMap(); + + private Worker worker = new Worker(); + + private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); + + public void init() { + LOG.error("start init dnsmapping"); + all.putAll(ConfigCenter.holder.dnsMapping()); + available.putAll(ConfigCenter.holder.dnsMapping()); + pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + LOG.error("end init dnsmapping"); + } + + public void update(Map> nowAvailable) { + available = nowAvailable; + } + + public Map> getAll() { + return all; + } + + public DnsMapping translate(String origin) { + if (available.isEmpty()) + return null; + List list = available.get(origin); + if (list == null || list.isEmpty()) + return null; + int L = list.size(); + if (L == 1) + return list.get(0); + return list.get((int) (Math.random() * L % L)); + } + + public void shutdown() { + pool.shutdown(); + } + + public static class Worker implements Runnable { + + private static final Logger log = LoggerFactory.getLogger(Worker.class); + + @Override + public void run() { + + log.debug("start dns mapping telnet"); + + Map> all = DnsMappingManage.holder.getAll(); + + Map> available = Maps.newConcurrentMap(); + + for (Map.Entry> entry : all.entrySet()) { + String key = entry.getKey(); + List value = entry.getValue(); + List nowValue = Lists.newArrayList(); + for (DnsMapping temp : value) { + boolean canTelnet = MPushUtil.telnet(temp.getIp(), temp.getPort()); + if (canTelnet) { + nowValue.add(temp); + } else { + log.error("dns can not reachable:" + Jsons.toJson(temp)); + } + } + available.put(key, nowValue); + } + + DnsMappingManage.holder.update(available); + + } + } } From 77fee90e6b71fcce0a58f2e276e51d4e6799e1fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 10 Mar 2016 05:56:46 +0000 Subject: [PATCH 520/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/HttpProxyHandler.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java index 23e27eef..d5ca8226 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java @@ -23,6 +23,7 @@ import java.net.URISyntaxException; import java.util.Map; +import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; /** @@ -44,7 +45,6 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { @Override public void handle(HttpRequestMessage message) { - try { Profiler.enter("start http proxy handler"); String method = message.getMethod(); @@ -101,13 +101,12 @@ public void onResponse(HttpResponse httpResponse) { } if (httpResponse instanceof FullHttpResponse) { - ByteBuf body = ((FullHttpResponse) httpResponse).content(); - if (body != null && body.readableBytes() > 0) { - byte[] buffer = new byte[body.readableBytes()]; - body.readBytes(buffer); - response.body = buffer; - response.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), - Integer.toString(response.body.length)); + ByteBuf content = ((FullHttpResponse) httpResponse).content(); + if (content != null && content.readableBytes() > 0) { + byte[] body = new byte[content.readableBytes()]; + content.readBytes(body); + response.body = body; + response.addHeader(CONTENT_LENGTH.toString(), Integer.toString(response.body.length)); } } response.send(); @@ -171,8 +170,7 @@ private void setBody(FullHttpRequest request, HttpRequestMessage message) { byte[] body = message.body; if (body != null && body.length > 0) { request.content().writeBytes(body); - request.headers().add(HttpHeaderNames.CONTENT_LENGTH, - Integer.toString(body.length)); + request.headers().add(CONTENT_LENGTH, Integer.toString(body.length)); } } From dfbc8f4513e0117f3d34218d02cc4a4676aca734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 10 Mar 2016 06:00:25 +0000 Subject: [PATCH 521/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java index ca2443d4..a07482ae 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java @@ -33,11 +33,11 @@ private DnsMappingManage() { private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); public void init() { - LOG.error("start init dnsmapping"); + LOG.error("start init dnsMapping"); all.putAll(ConfigCenter.holder.dnsMapping()); available.putAll(ConfigCenter.holder.dnsMapping()); pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns - LOG.error("end init dnsmapping"); + LOG.error("end init dnsMapping"); } public void update(Map> nowAvailable) { From a9712e702de267e2304c0ce5f4db0287fd95023d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 29 Apr 2016 12:10:58 +0200 Subject: [PATCH 522/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 12 +- .../mpush/core/server/ConnectionServer.java | 11 +- .../mpush/cs/ConnectionServerMain.java | 5 +- .../mpush/tools/config/ConfigCenter.java | 120 ++++++++++++++++-- .../shinemo/mpush/tools/owner/OwnerTest.java | 2 +- pom.xml | 28 +++- 6 files changed, 156 insertions(+), 22 deletions(-) diff --git a/conf-dev.properties b/conf-dev.properties index 2bbc1564..1a44bb8f 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -1,16 +1,24 @@ #日志根目录 log.home=/tmp/logs/mpush +#日志级别 loglevel=debug +#zk配置 zk_ip = 127.0.0.1:2181 zk_digest = shinemoIpo zk_namespace = mpush-daily +#redis配置多个用,隔开 redis_group = 127.0.0.1:6379:shinemoIpo +#私钥 private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +#公钥 public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info = true +#长连服务端口 connection_server_port = 20882 +#网关服务端口 gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 +#控制台服务端口 +admin_port=4001 skip_dump=true +dns_mapping=111.1.57.148=127.0.0.1 -admin_port=4001 diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java index 77b31092..a67efdd8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java @@ -9,6 +9,7 @@ import com.shinemo.mpush.netty.client.NettyHttpClient; import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.server.NettyServer; +import com.shinemo.mpush.tools.config.ConfigCenter; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -20,7 +21,7 @@ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; private ConnectionManager connectionManager = new NettyConnectionManager(); - private HttpClient httpClient = new NettyHttpClient(); + private HttpClient httpClient; public ConnectionServer(int port) { super(port); @@ -36,14 +37,18 @@ public void init() { receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); + + if (ConfigCenter.holder.httpProxyEnable()) { + httpClient = new NettyHttpClient(); + receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); + } channelHandler = new ServerChannelHandler(true, connectionManager, receiver); } @Override public void stop(Listener listener) { super.stop(listener); - httpClient.stop(); + if (httpClient != null) httpClient.stop(); connectionManager.destroy(); } diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java index 63269523..bf2a37e7 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java @@ -43,7 +43,10 @@ public void start() { startServer(adminServer); UserManager.INSTANCE.clearUserOnlineData(); - DnsMappingManage.holder.init(); + + if (ConfigCenter.holder.httpProxyEnable()) { + DnsMappingManage.holder.init(); + } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index 93b14401..b046b62f 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -3,7 +3,6 @@ import com.shinemo.mpush.tools.dns.DnsMapping; import com.shinemo.mpush.tools.redis.RedisGroup; - import org.aeonbits.owner.Config; import org.aeonbits.owner.Config.Sources; import org.aeonbits.owner.ConfigFactory; @@ -22,76 +21,178 @@ public interface ConfigCenter extends Config { ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); + /** + * 最大包长度 + * + * @return + */ @Key("max_packet_size") @DefaultValue("10240") int maxPacketSize(); + /** + * 包启用压缩特性阈值 + * + * @return + */ @Key("compress_limit") @DefaultValue("1024") int compressLimit(); + /** + * 最小心跳间隔 10s + * + * @return + */ @Key("min_heartbeat") @DefaultValue("10000") int minHeartbeat(); - //180秒 + /** + * 最大心跳间隔 10s + * + * @return + */ @Key("max_heartbeat") @DefaultValue("180000") int maxHeartbeat(); + /** + * 最大心跳超时次数 + * + * @return + */ @Key("max_hb_timeout_times") @DefaultValue("2") int maxHBTimeoutTimes(); + /** + * 快速重连session超时时间 + * + * @return + */ @Key("session_expired_time") @DefaultValue("86400") int sessionExpiredTime(); + /** + * RSA密钥长度 + * + * @return + */ @Key("ras_key_length") @DefaultValue("1024") - int rasKeyLength(); + int rsaKeyLength(); + /** + * AES密钥长度 + * + * @return + */ @Key("aes_key_length") @DefaultValue("16") int aesKeyLength(); + /** + * 长连接服务端口 + * + * @return + */ @Key("connection_server_port") @DefaultValue("3000") int connectionServerPort(); + /** + * 网关服务端口 + * + * @return + */ @Key("gateway_server_port") @DefaultValue("4000") int gatewayServerPort(); + + /** + * 控制台服务端口 + * + * @return + */ + @Key("admin_port") + @DefaultValue("4001") + int adminPort(); + + /** + * RSA私钥 + * + * @return + */ @Key("private_key") @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") String privateKey(); + /** + * RSA公钥 + * + * @return + */ @Key("public_key") @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") String publicKey(); + /** + * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd + * 多台机器用“,”分割 + * + * @return + */ + @Deprecated @Key("redis_ip") @DefaultValue("127.0.0.1:6379:ShineMoIpo") String redisIp(); + /** + * zookeeper机器,格式ip:port + * + * @return + */ @Key("zk_ip") @DefaultValue("127.0.0.1:2181") String zkIp(); + /** + * zookeeper 空间 + * + * @return + */ @Key("zk_namespace") @DefaultValue("mpush") String zkNamespace(); + /** + * zookeeper 权限密码 + * + * @return + */ @Key("zk_digest") @DefaultValue("shinemoIpo") String zkDigest(); + /** + * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd + * 多台机器用“;”分割 + * + * @return + */ @Separator(";") @Key("redis_group") @ConverterClass(RedisGroupConverter.class) List redisGroups(); + /** + * 自动把配置的redis机器集群写入到zk + * + * @return + */ @Key("force_write_redis_group_info") boolean forceWriteRedisGroupInfo(); @@ -103,6 +204,10 @@ public interface ConfigCenter extends Config { @DefaultValue("/opt/shinemo/mpush/") String logPath(); + @Key("http_proxy_enable") + @DefaultValue("false") + boolean httpProxyEnable(); + @Key("dns_mapping") @ConverterClass(DnsMappingConverter.class) Map> dnsMapping(); @@ -115,17 +220,14 @@ public interface ConfigCenter extends Config { @Key("http_default_read_timeout") @DefaultValue("10000") int httpDefaultReadTimeout(); - + @Key("online_and_offline_listener_ip") @DefaultValue("127.0.0.1") String onlineAndOfflineListenerIp(); - + @Key("skip_dump") @DefaultValue("true") boolean skipDump(); - - @Key("admin_port") - @DefaultValue("4001") - int adminPort(); + } diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java index 8823914b..ee1d8993 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java +++ b/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java @@ -55,7 +55,7 @@ public void test2(){ System.out.println(ConfigCenter.holder.publicKey()); - System.out.println(ConfigCenter.holder.rasKeyLength()); + System.out.println(ConfigCenter.holder.rsaKeyLength()); System.out.println(ConfigCenter.holder.redisIp()); diff --git a/pom.xml b/pom.xml index a1e04e75..a0466cda 100644 --- a/pom.xml +++ b/pom.xml @@ -4,16 +4,32 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - - com.shinemo - parent - 1.0.0 - - com.shinemo.mpush mpush pom 0.0.0.2 + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + master + git@github.com:mpusher/mpush.git + scm:git@github.com:mpusher/mpush.git + scm:ggit@github.com:mpusher/mpush.git + + + + ohun + ohun@live.cm + mpusher + + + mpush-api mpush-core From 92dbbe2009401075d23ac6d34413d3ad56d9f8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 12 May 2016 05:49:58 +0200 Subject: [PATCH 523/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=AC=E6=9C=BAIP?= =?UTF-8?q?=E5=88=B0=E5=A4=96=E7=BD=91IP=E7=9A=84=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 20 +-- .../com/shinemo/mpush/tools/MPushUtil.java | 124 ++++++++++-------- .../mpush/tools/config/ConfigCenter.java | 8 ++ .../tools/config/DnsMappingConverter.java | 19 ++- .../mpush/tools/config/MapConverter.java | 27 ++++ 5 files changed, 123 insertions(+), 75 deletions(-) create mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java diff --git a/conf-dev.properties b/conf-dev.properties index 1a44bb8f..5135928f 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -3,22 +3,24 @@ log.home=/tmp/logs/mpush #日志级别 loglevel=debug #zk配置 -zk_ip = 127.0.0.1:2181 -zk_digest = shinemoIpo -zk_namespace = mpush-daily +zk_ip=127.0.0.1:2181 +zk_digest=shinemoIpo +zk_namespace=mpush-daily #redis配置多个用,隔开 -redis_group = 127.0.0.1:6379:shinemoIpo +redis_group=127.0.0.1:6379:shinemoIpo #私钥 -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= #公钥 -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info = true +public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +force_write_redis_group_info=true #长连服务端口 -connection_server_port = 20882 +connection_server_port=20882 #网关服务端口 -gateway_server_port = 4000 +gateway_server_port=4000 #控制台服务端口 admin_port=4001 skip_dump=true dns_mapping=111.1.57.148=127.0.0.1 +#本机Ip和外网Ip的映射 +remote_ip_mapping=127.0.0.1:111.1.57.148,127.0.0.1:111.1.57.148 diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java index 8865e721..dbd71650 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java @@ -1,11 +1,10 @@ package com.shinemo.mpush.tools; +import com.shinemo.mpush.tools.config.ConfigCenter; import org.apache.commons.net.telnet.TelnetClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.config.ConfigCenter; - import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; @@ -21,19 +20,19 @@ public final class MPushUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); private static String LOCAL_IP; - + private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$"); - + private static String EXTRANET_IP; public static boolean isLocalHost(String host) { - return host == null - || host.length() == 0 - || host.equalsIgnoreCase("localhost") - || host.equals("0.0.0.0") - || (LOCAL_IP_PATTERN.matcher(host).matches()); + return host == null + || host.length() == 0 + || host.equalsIgnoreCase("localhost") + || host.equals("0.0.0.0") + || (LOCAL_IP_PATTERN.matcher(host).matches()); } - + public static String getLocalIp() { if (LOCAL_IP == null) { LOCAL_IP = getInetAddress(); @@ -43,7 +42,7 @@ public static String getLocalIp() { public static int getHeartbeat(int min, int max) { return Math.max( - ConfigCenter.holder.minHeartbeat(), + ConfigCenter.holder.minHeartbeat(), Math.min(max, ConfigCenter.holder.maxHeartbeat()) ); } @@ -56,7 +55,7 @@ public static int getHeartbeat(int min, int max) { */ public static String getInetAddress() { try { - com.shinemo.mpush.tools.Profiler.enter("start get inet addresss"); + com.shinemo.mpush.tools.Profiler.enter("start get inet addresss"); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); InetAddress address = null; while (interfaces.hasMoreElements()) { @@ -74,37 +73,50 @@ public static String getInetAddress() { } catch (Throwable e) { LOGGER.error("getInetAddress exception", e); return "127.0.0.1"; - }finally{ - com.shinemo.mpush.tools.Profiler.release(); + } finally { + com.shinemo.mpush.tools.Profiler.release(); } } - - public static String getExtranetIp(){ - if(EXTRANET_IP == null){ - EXTRANET_IP = getExtranetAddress(); - } - return EXTRANET_IP; + + public static String getExtranetIp() { + if (EXTRANET_IP == null) { + EXTRANET_IP = getExtranetAddress(); + } + return EXTRANET_IP; } - + public static String getExtranetAddress() { - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; - while (interfaces.hasMoreElements()) { - NetworkInterface ni = interfaces.nextElement(); - Enumeration addresses = ni.getInetAddresses(); - while (addresses.hasMoreElements()) { - address = addresses.nextElement(); - if(!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && !address.isSiteLocalAddress()){ - return address.getHostAddress(); - } - } - } - LOGGER.warn("getExtranetAddress is null"); - } catch (Throwable e) { - LOGGER.error("getExtranetAddress exception", e); - } - return getInetAddress(); + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress address = null; + while (interfaces.hasMoreElements()) { + NetworkInterface ni = interfaces.nextElement(); + Enumeration addresses = ni.getInetAddresses(); + while (addresses.hasMoreElements()) { + address = addresses.nextElement(); + if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && !address.isSiteLocalAddress()) { + return address.getHostAddress(); + } + } + } + LOGGER.warn("getExtranetAddress is null"); + } catch (Throwable e) { + LOGGER.error("getExtranetAddress exception", e); + } + + + String localIp = getInetAddress(); + String remoteIp = null; + Map mapping = ConfigCenter.holder.remoteIpMapping(); + if (mapping != null) { + remoteIp = mapping.get(localIp); + } + + if (remoteIp == null) { + remoteIp = localIp; + } + + return remoteIp; } public static String headerToString(Map headers) { @@ -142,22 +154,22 @@ public static Map headerFromString(String headersString) { } return headers; } - - public static boolean telnet(String ip,int port){ - TelnetClient client = new TelnetClient(); - try{ - client.connect(ip, port); - return true; - }catch(Exception e){ - return false; - }finally{ - try { - if(client.isConnected()){ - client.disconnect(); - } - } catch (IOException e) { - LOGGER.error("disconnect error",e); - } - } + + public static boolean telnet(String ip, int port) { + TelnetClient client = new TelnetClient(); + try { + client.connect(ip, port); + return true; + } catch (Exception e) { + return false; + } finally { + try { + if (client.isConnected()) { + client.disconnect(); + } + } catch (IOException e) { + LOGGER.error("disconnect error", e); + } + } } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java index b046b62f..fd778509 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java @@ -229,5 +229,13 @@ public interface ConfigCenter extends Config { @DefaultValue("true") boolean skipDump(); + /** + * 本机IP到外网Ip的映射 格式localIp:remoteIp,localIp:remoteIp + * + * @return + */ + @Key("remote_ip_mapping") + @ConverterClass(MapConverter.class) + Map remoteIpMapping(); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java index ea04f2f3..3d534343 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java @@ -1,24 +1,23 @@ package com.shinemo.mpush.tools.config; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.primitives.Ints; import com.shinemo.mpush.tools.dns.DnsMapping; +import org.aeonbits.owner.Converter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Map; public class DnsMappingConverter implements Converter>>{ -private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); + private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); @Override public Map> convert(Method method, String input) { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java new file mode 100644 index 00000000..a4fae2f1 --- /dev/null +++ b/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java @@ -0,0 +1,27 @@ +package com.shinemo.mpush.tools.config; + +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import org.aeonbits.owner.Converter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.Map; + +/** + * Created by yxx on 2016/5/12. + * + * @author ohun@live.cn + */ +public class MapConverter implements Converter> { + private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); + + @Override + public Map convert(Method method, String input) { + log.warn("method:" + method.getName() + "," + input); + if (Strings.isNullOrEmpty(input)) return Collections.emptyMap(); + return Splitter.on(',').withKeyValueSeparator(':').split(input); + } +} From 2851c08011a2e2880d9032f8aaab819338eb8ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 13 May 2016 10:27:30 +0200 Subject: [PATCH 524/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=AC=E6=9C=BAIP?= =?UTF-8?q?=E5=88=B0=E5=A4=96=E7=BD=91IP=E7=9A=84=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-daily.properties | 1 + conf-dev.properties | 2 +- .../client/ConnectionServerApplication.java | 41 +++++++++---------- mpush-cs/src/main/resources/config.properties | 1 + 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/conf-daily.properties b/conf-daily.properties index 379dab97..92ca7c8f 100644 --- a/conf-daily.properties +++ b/conf-daily.properties @@ -13,3 +13,4 @@ gateway_server_port = 4000 dns_mapping=111.1.57.148=127.0.0.1 skip_dump=true admin_port=4001 +remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/conf-dev.properties b/conf-dev.properties index 5135928f..907bfd87 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -22,5 +22,5 @@ admin_port=4001 skip_dump=true dns_mapping=111.1.57.148=127.0.0.1 #本机Ip和外网Ip的映射 -remote_ip_mapping=127.0.0.1:111.1.57.148,127.0.0.1:111.1.57.148 +remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java index 07b72f7d..5e7e47f4 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java +++ b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java @@ -1,32 +1,31 @@ package com.shinemo.mpush.conn.client; import com.shinemo.mpush.common.app.Application; -import com.shinemo.mpush.conn.client.GatewayServerApplication; import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.zk.ZKPath; +import org.slf4j.Logger; -public class ConnectionServerApplication extends Application{ - - private transient GatewayServerApplication gatewayServerApplication; - - public ConnectionServerApplication() throws Exception { - this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp(),MPushUtil.getExtranetAddress()); - } - - public ConnectionServerApplication(int port,String path,String ip,String extranetIp) { - setPort(port); - setServerRegisterZkPath(path); - setIp(ip); - setExtranetIp(extranetIp); - } +public class ConnectionServerApplication extends Application { + private transient GatewayServerApplication gatewayServerApplication; - public GatewayServerApplication getGatewayServerApplication() { - return gatewayServerApplication; - } + public ConnectionServerApplication() throws Exception { + this(ConfigCenter.holder.connectionServerPort(), ZKPath.CONNECTION_SERVER.getWatchPath(), MPushUtil.getLocalIp(), MPushUtil.getExtranetAddress()); + } - public void setGatewayServerApplication(GatewayServerApplication gatewayServerApplication) { - this.gatewayServerApplication = gatewayServerApplication; - } + public ConnectionServerApplication(int port, String path, String ip, String extranetIp) { + setPort(port); + setServerRegisterZkPath(path); + setIp(ip); + setExtranetIp(extranetIp); + } + + public GatewayServerApplication getGatewayServerApplication() { + return gatewayServerApplication; + } + + public void setGatewayServerApplication(GatewayServerApplication gatewayServerApplication) { + this.gatewayServerApplication = gatewayServerApplication; + } } diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-cs/src/main/resources/config.properties index 2729c8c3..6e1c5d8b 100644 --- a/mpush-cs/src/main/resources/config.properties +++ b/mpush-cs/src/main/resources/config.properties @@ -36,3 +36,4 @@ jvm_log_path = /opt/shinemo/mpush/ dns_mapping=${dns_mapping} skip_dump=${skip_dump} admin_port=${admin_port} +remote_ip_mapping=${remote_ip_mapping} From 6eb02d4e7b1b3f41198670ec9a0fe0da98927a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 15 May 2016 07:34:46 +0200 Subject: [PATCH 525/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-dev.properties | 4 +- daily-pub.py | 11 +- debug.sh | 20 +- mpush-api/pom.xml | 7 +- .../com/{shinemo => }/mpush/api/Client.java | 5 +- .../{shinemo => }/mpush/api/Constants.java | 2 +- .../com/{shinemo => }/mpush/api/Message.java | 6 +- .../mpush/api/MessageHandler.java | 6 +- .../mpush/api/PacketReceiver.java | 6 +- .../{shinemo => }/mpush/api/PushContent.java | 2 +- .../{shinemo => }/mpush/api/PushSender.java | 2 +- .../com/{shinemo => }/mpush/api/RedisKey.java | 2 +- .../com/{shinemo => }/mpush/api/Server.java | 2 +- .../mpush/api/connection/Cipher.java | 2 +- .../mpush/api/connection/Connection.java | 4 +- .../api/connection/ConnectionManager.java | 2 +- .../mpush/api/connection/SessionContext.java | 2 +- .../mpush/api/event/ConnectionCloseEvent.java | 4 +- .../{shinemo => }/mpush/api/event/Event.java | 2 +- .../mpush/api/event/HandshakeEvent.java | 4 +- .../mpush/api/event/KickUserEvent.java | 2 +- .../mpush/api/event/RouterChangeEvent.java | 4 +- .../mpush/api/event/UserOfflineEvent.java | 4 +- .../mpush/api/event/UserOnlineEvent.java | 4 +- .../mpush/api/exception/CryptoException.java | 2 +- .../mpush/api/exception/DecodeException.java | 2 +- .../mpush/api/exception/MessageException.java | 2 +- .../api/exception/SendMessageException.java | 2 +- .../mpush/api/protocol/Command.java | 2 +- .../mpush/api/protocol/Packet.java | 2 +- .../mpush/api/router/ClientLocation.java | 4 +- .../mpush/api/router/Router.java | 2 +- .../mpush/api/router/RouterManager.java | 2 +- assembly.xml => mpush-boot/assembly.xml | 15 +- mpush-boot/pom.xml | 93 ++++++ mpush-boot/src/main/java/com/mpush/Main.java | 19 ++ .../main/java/com/mpush/ServerLauncher.java | 47 +++ .../main/java/com/mpush/boot/BootChain.java | 31 ++ .../src/main/java/com/mpush/boot/BootJob.java | 23 ++ .../src/main/java/com/mpush/boot/EndBoot.java | 22 ++ .../main/java/com/mpush/boot/RedisBoot.java | 37 +++ .../main/java/com/mpush/boot/ServerBoot.java | 59 ++++ .../src/main/java/com/mpush/boot/ZKBoot.java | 45 +++ ...m.shinemo.mpush.common.manage.ServerManage | 0 .../src/main/resources/config.properties | 0 .../src/main/resources/logback.xml | 0 .../ConnectionServerApplicationTest.java | 13 + .../java/com}/mpush/zk/ServerManageTest.java | 10 +- .../src/test/java/com/mpush/zk/ZkTest.java | 19 ++ .../test/java/com}/mpush/zk/ZkUtilTest.java | 30 +- mpush-client/pom.xml | 67 ++-- .../conn/client/ClientChannelHandler.java | 30 +- .../java/com/mpush/push/ConnectClient.java | 12 + .../main/java/com/mpush/push/PushClient.java | 47 +++ .../{shinemo => }/mpush/push/PushRequest.java | 69 ++-- .../mpush/push/PushRequestBus.java | 2 +- .../push/client/ClientChannelHandler.java | 26 +- .../push/zk/listener/ConnectZKListener.java | 35 ++ .../push/zk/listener/GatewayZKListener.java | 35 ++ .../push/zk/manager/ConnectZKNodeManager.java | 47 +++ .../push/zk/manager/GatewayZKNodeManager.java | 83 +++++ .../client/ConnectionServerApplication.java | 31 -- .../conn/client/GatewayServerApplication.java | 21 -- .../com/shinemo/mpush/push/ConnClient.java | 12 - .../com/shinemo/mpush/push/PushClient.java | 42 --- .../manage/impl/ConnectionServerManage.java | 51 --- .../push/manage/impl/GatewayServerManage.java | 86 ----- .../impl/ConnectionServerPathListener.java | 39 --- .../impl/GatewayServerPathListener.java | 40 --- ...m.shinemo.mpush.common.manage.ServerManage | 2 - .../services/com.mpush.zk.ZKNodeManager | 2 + .../mpush/client/PushClientTest.java | 4 +- mpush-common/pom.xml | 16 +- .../java/com/mpush/common/AbstractClient.java | 64 ++++ .../mpush/common/AbstractEventContainer.java | 2 +- .../{shinemo => }/mpush/common/ErrorCode.java | 2 +- .../{shinemo => }/mpush/common/EventBus.java | 6 +- .../mpush/common/MessageDispatcher.java | 16 +- .../common/handler/BaseMessageHandler.java | 12 +- .../common/handler/ErrorMessageHandler.java | 8 +- .../common/handler/OkMessageHandler.java | 8 +- .../mpush/common/manage/user/UserManager.java | 8 +- .../mpush/common/message/BaseMessage.java | 18 +- .../mpush/common/message/BindUserMessage.java | 8 +- .../mpush/common/message/ByteBufMessage.java | 8 +- .../mpush/common/message/ErrorMessage.java | 10 +- .../common/message/FastConnectMessage.java | 8 +- .../common/message/FastConnectOkMessage.java | 6 +- .../common/message/HandshakeMessage.java | 8 +- .../common/message/HandshakeOkMessage.java | 6 +- .../common/message/HttpRequestMessage.java | 12 +- .../common/message/HttpResponseMessage.java | 10 +- .../mpush/common/message/KickUserMessage.java | 9 +- .../mpush/common/message/OkMessage.java | 9 +- .../mpush/common/message/PushMessage.java | 10 +- .../message/gateway/GatewayPushMessage.java | 10 +- .../router/ConnectionRouterManager.java | 2 +- .../mpush/common/router/RemoteRouter.java | 6 +- .../common/router/RemoteRouterManager.java | 8 +- .../common/router/UserChangeListener.java | 14 +- .../mpush/common/security/AesCipher.java | 8 +- .../mpush/common/security/CipherBox.java | 6 +- .../mpush/common/security/RsaCipher.java | 8 +- .../shinemo/mpush/common/AbstractClient.java | 66 ---- .../shinemo/mpush/common/AbstractServer.java | 161 --------- .../com/shinemo/mpush/common/DnsMapping.java | 39 --- .../shinemo/mpush/common/app/Application.java | 50 --- .../mpush/common/manage/ServerManage.java | 16 - .../listener/AbstractDataChangeListener.java | 93 ------ mpush-core/pom.xml | 12 +- .../com/mpush/core/handler/AdminHandler.java | 135 ++++++++ .../mpush/core/handler/BindUserHandler.java | 26 +- .../core/handler/FastConnectHandler.java | 24 +- .../core/handler/GatewayPushHandler.java | 35 +- .../mpush/core/handler/HandshakeHandler.java | 34 +- .../mpush/core/handler/HeartBeatHandler.java | 12 +- .../mpush/core/handler/HttpProxyHandler.java | 28 +- .../mpush/core/handler/UnbindUserHandler.java | 36 +- .../mpush/core/router/KickRemoteMsg.java | 2 +- .../mpush/core/router/LocalRouter.java | 6 +- .../mpush/core/router/LocalRouterManager.java | 16 +- .../mpush/core/router/RouterCenter.java | 20 +- .../core/router/RouterChangeListener.java | 34 +- .../router/UserOnlineOfflineListener.java | 12 +- .../mpush/core/server/AdminServer.java | 8 +- .../mpush/core/server/ConnectionServer.java | 20 +- .../mpush/core/server/GatewayServer.java | 12 +- .../core/server/ServerChannelHandler.java | 22 +- .../mpush/core/session/ReusableSession.java | 6 +- .../core/session/ReusableSessionManager.java | 14 +- .../mpush/core/handler/AdminHandler.java | 139 -------- .../mpush/core/netty/NettyServerTest.java | 7 +- .../mpush/core/security/CipherBoxTest.java | 4 +- mpush-cs/pom.xml | 104 ------ .../mpush/cs/ConnectionServerMain.java | 65 ---- .../main/java/com/shinemo/mpush/cs/Main.java | 28 -- .../ConnectionServerApplicationTest.java | 21 -- .../java/com/shinemo/mpush/zk/ZkTest.java | 20 -- mpush-log/pom.xml | 54 +-- .../java/com/{shinemo => }/mpush/App.java | 2 +- .../com/{shinemo => }/mpush/log/LogLevel.java | 2 +- .../com/{shinemo => }/mpush/log/LogType.java | 2 +- .../{shinemo => }/mpush/log/LoggerManage.java | 2 +- .../java/com/{shinemo => }/mpush/AppTest.java | 2 +- mpush-monitor/pom.xml | 23 +- .../java/com/{shinemo => }/mpush/App.java | 2 +- .../mpush/monitor/domain/MonitorData.java | 2 +- .../mpush/monitor/quota/BaseQuota.java | 2 +- .../mpush/monitor/quota/GCMQuota.java | 2 +- .../mpush/monitor/quota/InfoQuota.java | 2 +- .../mpush/monitor/quota/MemoryQuota.java | 2 +- .../mpush/monitor/quota/ThreadPoolQuota.java | 5 + .../mpush/monitor/quota/ThreadQuota.java | 2 +- .../mpush/monitor/quota/impl/JVMGC.java | 6 +- .../mpush/monitor/quota/impl/JVMInfo.java | 8 +- .../mpush/monitor/quota/impl/JVMMemory.java | 6 +- .../mpush/monitor/quota/impl/JVMThread.java | 6 +- .../monitor/quota/impl/JVMThreadPool.java | 8 +- .../monitor/service/MonitorDataCollector.java | 18 +- .../mpush/monitor/quota/ThreadPoolQuota.java | 5 - .../src/test/java/com}/mpush/AppTest.java | 2 +- mpush-netty/pom.xml | 15 +- .../netty/client/ChannelClientHandler.java | 4 +- .../mpush/netty/client/HttpCallback.java | 2 +- .../mpush/netty/client/HttpClient.java | 2 +- .../mpush/netty/client/NettyClient.java | 12 +- .../netty/client/NettyClientFactory.java | 10 +- .../mpush/netty/client/NettyHttpClient.java | 8 +- .../mpush/netty/client/RequestInfo.java | 6 +- .../netty/client/SecurityNettyClient.java | 2 +- .../mpush/netty/codec/PacketDecoder.java | 10 +- .../mpush/netty/codec/PacketEncoder.java | 6 +- .../netty/connection/NettyConnection.java | 10 +- .../connection/NettyConnectionManager.java | 16 +- .../mpush/netty/server/NettyServer.java | 10 +- .../mpush/netty/util/NettySharedHolder.java | 6 +- mpush-test/pom.xml | 35 +- .../src/test/java/com}/mpush/AppTest.java | 2 +- .../test/configcenter/ConfigCenterTest.java | 8 +- .../mpush/test/connection/body/BodyTest.java | 7 +- .../connection/client/ConnectTestClient.java | 25 ++ .../mpush/test/connection/client/Main.java | 20 +- .../connection/mpns/ConnectTestClient.java | 17 + .../com/mpush/test/connection/mpns/Main.java | 52 +++ .../mpush/test/crypto/RsaTest.java | 9 +- .../mpush/test/gson/DnsMappingTest.java | 2 +- .../mpush/test/gson/GsonTest.java | 10 +- .../{shinemo => }/mpush/test/push/Main.java | 12 +- .../mpush/test/redis/ConsistentHashTest.java | 7 +- .../mpush/test/redis/MPushUtilTest.java | 5 +- .../mpush/test/redis/PubSubTest.java | 16 +- .../mpush/test/redis/RedisClusterTest.java | 8 +- .../test/redis/RedisGroupManageTest.java | 14 +- .../mpush/test/redis/RedisUtilTest.java | 7 +- .../{shinemo => }/mpush/test/redis/User.java | 2 +- .../mpush/test/util/TelnetTest.java | 5 +- .../client/ConnectionClientMain.java | 25 -- .../connection/mpns/ConnectionClientMain.java | 17 - .../mpush/test/connection/mpns/Main.java | 52 --- mpush-tools/pom.xml | 29 +- .../{shinemo => }/mpush/tools/Constants.java | 2 +- .../mpush/tools/GenericsUtil.java | 2 +- .../{shinemo => }/mpush/tools/IOUtils.java | 2 +- .../{shinemo => }/mpush/tools/JVMUtil.java | 2 +- .../com/{shinemo => }/mpush/tools/Jsons.java | 2 +- .../{shinemo => }/mpush/tools/MPushUtil.java | 8 +- .../com/{shinemo => }/mpush/tools/Pair.java | 2 +- .../{shinemo => }/mpush/tools/Profiler.java | 2 +- .../{shinemo => }/mpush/tools/Strings.java | 2 +- .../mpush/tools/config/ConfigCenter.java | 6 +- .../tools/config/DnsMappingConverter.java | 4 +- .../mpush/tools/config/MapConverter.java | 2 +- .../tools/config/RedisGroupConverter.java | 6 +- .../mpush/tools/crypto/AESUtils.java | 4 +- .../mpush/tools/crypto/Base64.java | 2 +- .../mpush/tools/crypto/Base64Utils.java | 4 +- .../mpush/tools/crypto/MD5Utils.java | 8 +- .../mpush/tools/crypto/RSAUtils.java | 6 +- .../mpush/tools/dns/DnsMapping.java | 2 +- .../tools/dns/manage/DnsMappingManage.java | 10 +- .../mpush/tools/event/Event.java | 2 +- .../mpush/tools/event/EventDispatcher.java | 2 +- .../mpush/tools/event/EventListener.java | 2 +- .../java/com/mpush/tools/event/EventType.java | 5 + .../mpush/tools/exception/ZKException.java | 24 ++ .../mpush/tools/redis/RedisGroup.java | 2 +- .../mpush/tools/redis/RedisNode.java | 2 +- .../mpush/tools/redis/RedisPoolConfig.java | 4 +- .../com/mpush/tools/redis/RedisRegister.java | 17 + .../mpush/tools/redis/RedisUtil.java | 12 +- .../redis/consistenthash/ConsistentHash.java | 2 +- .../tools/redis/consistenthash/Node.java | 2 +- .../jedis/services/JedisRegisterManager.java | 26 +- .../redis/listener/ListenerDispatcher.java | 12 +- .../tools/redis/listener/MessageListener.java | 2 +- .../mpush/tools/redis/manage/RedisManage.java | 136 ++++---- .../mpush/tools/redis/pubsub/Subscriber.java | 12 +- .../{shinemo => }/mpush/tools/spi/SPI.java | 2 +- .../com/mpush/tools/spi/ServiceContainer.java | 42 +++ .../tools/thread/NamedThreadFactory.java | 2 +- .../mpush/tools/thread/ThreadNameSpace.java | 2 +- .../thread/threadpool/IgnoreRunsPolicy.java | 6 +- .../tools/thread/threadpool/ThreadPool.java | 4 +- .../thread/threadpool/ThreadPoolContext.java | 10 +- .../thread/threadpool/ThreadPoolManager.java | 45 +++ .../threadpool/cached/CachedThreadPool.java | 10 +- .../cached/CachedThreadPoolContext.java | 6 +- .../threadpool/fixed/FixedThreadPool.java | 10 +- .../fixed/FixedThreadPoolContext.java | 6 +- .../shinemo/mpush/tools/event/EventType.java | 5 - .../mpush/tools/redis/RedisRegister.java | 18 - .../mpush/tools/spi/ServiceContainer.java | 257 --------------- .../thread/threadpool/ThreadPoolManager.java | 46 --- .../com/shinemo/mpush/tools/zk/ZkConfig.java | 81 ----- .../shinemo/mpush/tools/zk/ZkRegister.java | 44 --- .../curator/services/ZkRegisterManager.java | 309 ------------------ .../tools/zk/listener/DataChangeListener.java | 29 -- .../zk/listener/impl/RedisPathListener.java | 90 ----- ...om.shinemo.mpush.tools.redis.RedisRegister | 1 - ...o.mpush.tools.thread.threadpool.ThreadPool | 2 - .../com.shinemo.mpush.tools.zk.ZkRegister | 1 - .../com.mpush.tools.redis.RedisRegister | 1 + .../mpush/tools/IOUtilsTest.java | 4 +- .../mpush/tools/crypto/AESUtilsTest.java | 5 +- .../mpush/tools/crypto/RSAUtilsTest.java | 4 +- .../mpush/tools/delayqueue/Exam.java | 2 +- .../mpush/tools/owner/OwnerTest.java | 5 +- .../mpush/tools/owner/ServerConfig.java | 2 +- .../java/com/mpush/tools/spi/SpiTest.java | 60 ++++ .../mpush/tools/spi/test/TestService.java | 4 +- .../mpush/tools/spi/test/TestServiceImpl.java | 2 +- .../tools/spi/test/TestServiceImpl2.java | 2 +- .../mpush/tools/thread/SyncTest.java | 14 +- .../com/shinemo/mpush/tools/spi/SpiTest.java | 75 ----- ...m.shinemo.mpush.tools.spi.test.TestService | 2 - .../com.mpush.tools.spi.test.TestService | 2 + mpush-zk/pom.xml | 31 ++ .../src/main/java/com/mpush/zk/ZKClient.java | 295 +++++++++++++++++ .../src/main/java/com/mpush/zk/ZKConfig.java | 113 +++++++ .../main/java/com/mpush/zk/ZKNodeManager.java | 16 + .../src/main/java/com/mpush}/zk/ZKPath.java | 8 +- .../main/java/com/mpush/zk/ZKServerNode.java | 108 ++++++ .../zk/listener/ZKDataChangeListener.java | 28 ++ .../zk/listener/ZKRedisNodeListener.java | 85 +++++ .../zk/listener/ZKServerNodeListener.java | 79 +++++ pom.xml | 146 ++++----- pub-daily.py | 9 +- pub-online.py | 9 +- pub-pre.py | 7 +- start.sh | 9 +- 290 files changed, 2936 insertions(+), 3259 deletions(-) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/Client.java (85%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/Constants.java (86%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/Message.java (68%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/MessageHandler.java (51%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/PacketReceiver.java (51%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/PushContent.java (98%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/PushSender.java (93%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/RedisKey.java (96%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/Server.java (90%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/connection/Cipher.java (76%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/connection/Connection.java (90%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/connection/ConnectionManager.java (88%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/connection/SessionContext.java (96%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/ConnectionCloseEvent.java (73%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/Event.java (65%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/HandshakeEvent.java (78%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/KickUserEvent.java (91%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/RouterChangeEvent.java (78%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/UserOfflineEvent.java (85%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/event/UserOnlineEvent.java (82%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/exception/CryptoException.java (85%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/exception/DecodeException.java (81%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/exception/MessageException.java (83%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/exception/SendMessageException.java (72%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/protocol/Command.java (94%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/protocol/Packet.java (98%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/router/ClientLocation.java (94%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/router/Router.java (82%) rename mpush-api/src/main/java/com/{shinemo => }/mpush/api/router/RouterManager.java (92%) rename assembly.xml => mpush-boot/assembly.xml (73%) create mode 100644 mpush-boot/pom.xml create mode 100644 mpush-boot/src/main/java/com/mpush/Main.java create mode 100644 mpush-boot/src/main/java/com/mpush/ServerLauncher.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/BootChain.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/BootJob.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/EndBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java rename {mpush-cs/src/main/resources/META-INF/mpush => mpush-boot/src/main/resources/META-INF}/services/com.shinemo.mpush.common.manage.ServerManage (100%) rename {mpush-cs => mpush-boot}/src/main/resources/config.properties (100%) rename {mpush-cs => mpush-boot}/src/main/resources/logback.xml (100%) create mode 100644 mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java rename {mpush-cs/src/test/java/com/shinemo => mpush-boot/src/test/java/com}/mpush/zk/ServerManageTest.java (90%) create mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkTest.java rename {mpush-cs/src/test/java/com/shinemo => mpush-boot/src/test/java/com}/mpush/zk/ZkUtilTest.java (83%) rename mpush-client/src/main/java/com/{shinemo => }/mpush/conn/client/ClientChannelHandler.java (92%) create mode 100644 mpush-client/src/main/java/com/mpush/push/ConnectClient.java create mode 100644 mpush-client/src/main/java/com/mpush/push/PushClient.java rename mpush-client/src/main/java/com/{shinemo => }/mpush/push/PushRequest.java (71%) rename mpush-client/src/main/java/com/{shinemo => }/mpush/push/PushRequestBus.java (97%) rename mpush-client/src/main/java/com/{shinemo => }/mpush/push/client/ClientChannelHandler.java (79%) create mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java create mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java create mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java create mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java delete mode 100644 mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java delete mode 100644 mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage create mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager rename mpush-client/src/test/java/com/{shinemo => }/mpush/client/PushClientTest.java (94%) create mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractClient.java rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/AbstractEventContainer.java (78%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/ErrorCode.java (95%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/EventBus.java (89%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/MessageDispatcher.java (77%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/handler/BaseMessageHandler.java (66%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/handler/ErrorMessageHandler.java (63%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/handler/OkMessageHandler.java (62%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/manage/user/UserManager.java (86%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/BaseMessage.java (90%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/BindUserMessage.java (80%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/ByteBufMessage.java (92%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/ErrorMessage.java (88%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/FastConnectMessage.java (81%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/FastConnectOkMessage.java (84%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/HandshakeMessage.java (87%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/HandshakeOkMessage.java (91%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/HttpRequestMessage.java (84%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/HttpResponseMessage.java (87%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/KickUserMessage.java (78%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/OkMessage.java (84%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/PushMessage.java (72%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/message/gateway/GatewayPushMessage.java (81%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/router/ConnectionRouterManager.java (96%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/router/RemoteRouter.java (80%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/router/RemoteRouterManager.java (85%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/router/UserChangeListener.java (78%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/security/AesCipher.java (88%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/security/CipherBox.java (93%) rename mpush-common/src/main/java/com/{shinemo => }/mpush/common/security/RsaCipher.java (85%) delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java delete mode 100644 mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java create mode 100644 mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/BindUserHandler.java (76%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/FastConnectHandler.java (75%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/GatewayPushHandler.java (87%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/HandshakeHandler.java (78%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/HeartBeatHandler.java (60%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/HttpProxyHandler.java (90%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/handler/UnbindUserHandler.java (77%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/router/KickRemoteMsg.java (89%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/router/LocalRouter.java (79%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/router/LocalRouterManager.java (87%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/router/RouterCenter.java (85%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/router/RouterChangeListener.java (85%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/router/UserOnlineOfflineListener.java (75%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/server/AdminServer.java (94%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/server/ConnectionServer.java (82%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/server/GatewayServer.java (74%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/server/ServerChannelHandler.java (82%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/session/ReusableSession.java (89%) rename mpush-core/src/main/java/com/{shinemo => }/mpush/core/session/ReusableSessionManager.java (77%) delete mode 100644 mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java rename mpush-core/src/test/java/com/{shinemo => }/mpush/core/netty/NettyServerTest.java (72%) rename mpush-core/src/test/java/com/{shinemo => }/mpush/core/security/CipherBoxTest.java (89%) delete mode 100644 mpush-cs/pom.xml delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java delete mode 100644 mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java delete mode 100644 mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java delete mode 100644 mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java rename mpush-log/src/main/java/com/{shinemo => }/mpush/App.java (85%) rename mpush-log/src/main/java/com/{shinemo => }/mpush/log/LogLevel.java (61%) rename mpush-log/src/main/java/com/{shinemo => }/mpush/log/LogType.java (70%) rename mpush-log/src/main/java/com/{shinemo => }/mpush/log/LoggerManage.java (99%) rename mpush-log/src/test/java/com/{shinemo => }/mpush/AppTest.java (95%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/App.java (85%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/domain/MonitorData.java (96%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/BaseQuota.java (97%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/GCMQuota.java (89%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/InfoQuota.java (66%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/MemoryQuota.java (94%) create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/ThreadQuota.java (81%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/impl/JVMGC.java (95%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/impl/JVMInfo.java (82%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/impl/JVMMemory.java (97%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/impl/JVMThread.java (90%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/quota/impl/JVMThreadPool.java (83%) rename mpush-monitor/src/main/java/com/{shinemo => }/mpush/monitor/service/MonitorDataCollector.java (87%) delete mode 100644 mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java rename {mpush-test/src/test/java/com/shinemo => mpush-monitor/src/test/java/com}/mpush/AppTest.java (95%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/ChannelClientHandler.java (64%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/HttpCallback.java (89%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/HttpClient.java (81%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/NettyClient.java (92%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/NettyClientFactory.java (91%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/NettyHttpClient.java (97%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/RequestInfo.java (95%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/client/SecurityNettyClient.java (97%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/codec/PacketDecoder.java (91%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/codec/PacketEncoder.java (88%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/connection/NettyConnection.java (93%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/connection/NettyConnectionManager.java (90%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/server/NettyServer.java (96%) rename mpush-netty/src/main/java/com/{shinemo => }/mpush/netty/util/NettySharedHolder.java (72%) rename {mpush-monitor/src/test/java/com/shinemo => mpush-test/src/test/java/com}/mpush/AppTest.java (95%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/configcenter/ConfigCenterTest.java (67%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/connection/body/BodyTest.java (97%) create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/connection/client/Main.java (67%) create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/crypto/RsaTest.java (96%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/gson/DnsMappingTest.java (96%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/gson/GsonTest.java (79%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/push/Main.java (84%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/ConsistentHashTest.java (91%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/MPushUtilTest.java (66%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/PubSubTest.java (73%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/RedisClusterTest.java (85%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/RedisGroupManageTest.java (90%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/RedisUtilTest.java (96%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/redis/User.java (94%) rename mpush-test/src/test/java/com/{shinemo => }/mpush/test/util/TelnetTest.java (86%) delete mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java delete mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java delete mode 100644 mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/Constants.java (98%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/GenericsUtil.java (99%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/IOUtils.java (98%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/JVMUtil.java (99%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/Jsons.java (98%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/MPushUtil.java (96%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/Pair.java (92%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/Profiler.java (99%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/Strings.java (95%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/config/ConfigCenter.java (97%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/config/DnsMappingConverter.java (94%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/config/MapConverter.java (95%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/config/RedisGroupConverter.java (86%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/crypto/AESUtils.java (96%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/crypto/Base64.java (99%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/crypto/Base64Utils.java (97%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/crypto/MD5Utils.java (94%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/crypto/RSAUtils.java (99%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/dns/DnsMapping.java (89%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/dns/manage/DnsMappingManage.java (92%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/event/Event.java (90%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/event/EventDispatcher.java (90%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/event/EventListener.java (65%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventType.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/RedisGroup.java (95%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/RedisNode.java (94%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/RedisPoolConfig.java (96%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/RedisUtil.java (98%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/consistenthash/ConsistentHash.java (96%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/consistenthash/Node.java (87%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/jedis/services/JedisRegisterManager.java (74%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/listener/ListenerDispatcher.java (82%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/listener/MessageListener.java (65%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/manage/RedisManage.java (74%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/redis/pubsub/Subscriber.java (86%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/spi/SPI.java (88%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/NamedThreadFactory.java (96%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/ThreadNameSpace.java (96%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java (87%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/ThreadPool.java (63%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/ThreadPoolContext.java (87%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/cached/CachedThreadPool.java (80%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java (81%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java (79%) rename mpush-tools/src/main/java/com/{shinemo => }/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java (73%) delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java delete mode 100644 mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java delete mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister delete mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool delete mode 100644 mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister create mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/IOUtilsTest.java (97%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/crypto/AESUtilsTest.java (84%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/crypto/RSAUtilsTest.java (98%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/delayqueue/Exam.java (99%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/owner/OwnerTest.java (94%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/owner/ServerConfig.java (92%) create mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/spi/test/TestService.java (52%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/spi/test/TestServiceImpl.java (89%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/spi/test/TestServiceImpl2.java (89%) rename mpush-tools/src/test/java/com/{shinemo => }/mpush/tools/thread/SyncTest.java (91%) delete mode 100644 mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java delete mode 100644 mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService create mode 100644 mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService create mode 100644 mpush-zk/pom.xml create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKClient.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java rename {mpush-tools/src/main/java/com/shinemo/mpush/tools => mpush-zk/src/main/java/com/mpush}/zk/ZKPath.java (82%) create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java diff --git a/conf-dev.properties b/conf-dev.properties index 907bfd87..6d29e48a 100644 --- a/conf-dev.properties +++ b/conf-dev.properties @@ -7,14 +7,14 @@ zk_ip=127.0.0.1:2181 zk_digest=shinemoIpo zk_namespace=mpush-daily #redis配置多个用,隔开 -redis_group=127.0.0.1:6379:shinemoIpo +redis_group=111.1.57.148:6379:ShineMoIpo #私钥 private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= #公钥 public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB force_write_redis_group_info=true #长连服务端口 -connection_server_port=20882 +connection_server_port=3000 #网关服务端口 gateway_server_port=4000 #控制台服务端口 diff --git a/daily-pub.py b/daily-pub.py index 0e771267..a5810bd0 100644 --- a/daily-pub.py +++ b/daily-pub.py @@ -22,11 +22,11 @@ BASEPATH = '/root/mpush' -MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz' +MPUSH_TAR_NAME = 'mpush-release.tar.gz' -PROCESS_KEY_WORD = 'mpush-cs.jar' +PROCESS_KEY_WORD = 'boot.jar' -GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME +GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' @@ -158,8 +158,9 @@ def main(): print showText('git pull master success','greenText') ##1 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) - print showText('assembly success','greenText') + runShell('mvn clean) + runShell('mvn package -P %s'%ENV) + print showText('package success','greenText') ##2 包创建时间 runShell('stat -c "%%y" %s'%GITLABPATH) diff --git a/debug.sh b/debug.sh index 22c56f31..41d177ff 100755 --- a/debug.sh +++ b/debug.sh @@ -1,22 +1,18 @@ #!/bin/sh ENV=dev - base_dir=`pwd` -echo "start assembly lib..." - -rm -rf $base_dir/target - -mvn clean install assembly:assembly -P $ENV +echo "start package project..." +mvn clean +mvn package -P $ENV echo "start tar mpush..." -cd $base_dir/target -tar -xzvf ./mpush-jar-with-dependency.tar.gz -echo "start start mpush..." - -cd mpush/lib +cd $base_dir/mpush-boot/target +tar -xzvf ./mpush-release.tar.gz -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -Dio.netty.leakDetectionLevel=advanced -jar $base_dir/target/mpush/mpush-cs.jar & +echo "start start mpush..." +cd mpush +java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -Dio.netty.leakDetectionLevel=advanced -jar boot.jar & echo "end start mpush..." diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index c4bb6f8e..141fb582 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -4,13 +4,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml + com.mpush + 1.0 4.0.0 + ${mpush.groupId} mpush-api + ${mpush-api-version} mpush-api jar diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java b/mpush-api/src/main/java/com/mpush/api/Client.java similarity index 85% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Client.java rename to mpush-api/src/main/java/com/mpush/api/Client.java index 2c1eb1d0..f63a0ca0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/mpush/api/Client.java @@ -1,9 +1,8 @@ -package com.shinemo.mpush.api; +package com.mpush.api; +import com.mpush.api.connection.Connection; import io.netty.channel.Channel; -import com.shinemo.mpush.api.connection.Connection; - public interface Client { diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java similarity index 86% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java rename to mpush-api/src/main/java/com/mpush/api/Constants.java index a29c52a3..d399d042 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.mpush.api; import java.nio.charset.Charset; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java similarity index 68% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Message.java rename to mpush-api/src/main/java/com/mpush/api/Message.java index 93f1f09d..90de5c06 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.api; +package com.mpush.api; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; import io.netty.channel.ChannelFutureListener; /** diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java similarity index 51% rename from mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java rename to mpush-api/src/main/java/com/mpush/api/MessageHandler.java index ec64e2cb..7bf9fb65 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.api; +package com.mpush.api; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java similarity index 51% rename from mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java rename to mpush-api/src/main/java/com/mpush/api/PacketReceiver.java index 7bd7511a..f0165b9a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.api; +package com.mpush.api; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java b/mpush-api/src/main/java/com/mpush/api/PushContent.java similarity index 98% rename from mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java rename to mpush-api/src/main/java/com/mpush/api/PushContent.java index d1e5e142..7abe5307 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java +++ b/mpush-api/src/main/java/com/mpush/api/PushContent.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.mpush.api; import java.io.Serializable; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java b/mpush-api/src/main/java/com/mpush/api/PushSender.java similarity index 93% rename from mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java rename to mpush-api/src/main/java/com/mpush/api/PushSender.java index 71bf48d5..7beca2e2 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/PushSender.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.mpush.api; import java.util.Collection; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java b/mpush-api/src/main/java/com/mpush/api/RedisKey.java similarity index 96% rename from mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java rename to mpush-api/src/main/java/com/mpush/api/RedisKey.java index 0a704ddd..0077c4d1 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/RedisKey.java +++ b/mpush-api/src/main/java/com/mpush/api/RedisKey.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.mpush.api; public final class RedisKey { diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/Server.java similarity index 90% rename from mpush-api/src/main/java/com/shinemo/mpush/api/Server.java rename to mpush-api/src/main/java/com/mpush/api/Server.java index 9f976416..1130b921 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/mpush/api/Server.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api; +package com.mpush.api; /** * Created by ohun on 2015/12/24. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Cipher.java b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java similarity index 76% rename from mpush-api/src/main/java/com/shinemo/mpush/api/connection/Cipher.java rename to mpush-api/src/main/java/com/mpush/api/connection/Cipher.java index 9f958e15..91732e2b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Cipher.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.connection; +package com.mpush.api.connection; /** * Created by ohun on 2015/12/28. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java similarity index 90% rename from mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java rename to mpush-api/src/main/java/com/mpush/api/connection/Connection.java index 1ae118e4..f1ea56aa 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.api.connection; +package com.mpush.api.connection; +import com.mpush.api.protocol.Packet; import io.netty.channel.Channel; -import com.shinemo.mpush.api.protocol.Packet; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java similarity index 88% rename from mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java rename to mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index 59f56f2d..554486e9 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.connection; +package com.mpush.api.connection; import java.util.List; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java similarity index 96% rename from mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java rename to mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index 91e76f13..6313a8d3 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.connection; +package com.mpush.api.connection; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java similarity index 73% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java rename to mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java index a6d95512..2eaf18e7 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/ConnectionCloseEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; -import com.shinemo.mpush.api.connection.Connection; +import com.mpush.api.connection.Connection; /** * Created by ohun on 2016/1/10. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java b/mpush-api/src/main/java/com/mpush/api/event/Event.java similarity index 65% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java rename to mpush-api/src/main/java/com/mpush/api/event/Event.java index b9ac7669..08c72cbf 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java +++ b/mpush-api/src/main/java/com/mpush/api/event/Event.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; /** * Created by ohun on 2015/12/29. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java similarity index 78% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java rename to mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java index 717ad9ff..17cf2bb7 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; -import com.shinemo.mpush.api.connection.Connection; +import com.mpush.api.connection.Connection; /** * Created by ohun on 2015/12/29. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java similarity index 91% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java rename to mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java index 8c6091b0..eb33f83e 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/KickUserEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; /** * Created by ohun on 2015/12/29. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java similarity index 78% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java rename to mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java index f81bd1a8..add6cdaa 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/RouterChangeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; -import com.shinemo.mpush.api.router.Router; +import com.mpush.api.router.Router; /** * Created by ohun on 2016/1/4. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java similarity index 85% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java rename to mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java index a024a907..a5087299 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOfflineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; -import com.shinemo.mpush.api.connection.Connection; +import com.mpush.api.connection.Connection; /** * 链接超时,用户解绑的时候,用户主动关闭链接,才会触发该事件 diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java similarity index 82% rename from mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java rename to mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java index 4da6f450..6ecb3596 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/event/UserOnlineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.event; +package com.mpush.api.event; -import com.shinemo.mpush.api.connection.Connection; +import com.mpush.api.connection.Connection; /** * 绑定用户的时候才会触发该事件 diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java similarity index 85% rename from mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java rename to mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java index 965b4e4f..39170d30 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/CryptoException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.exception; +package com.mpush.api.exception; /** * Created by ohun on 2015/12/23. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java similarity index 81% rename from mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java rename to mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java index 543768df..fc28a125 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.exception; +package com.mpush.api.exception; /** * Created by ohun on 2015/12/23. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java similarity index 83% rename from mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java rename to mpush-api/src/main/java/com/mpush/api/exception/MessageException.java index 5e92c987..d2dd1a3b 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/MessageException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.exception; +package com.mpush.api.exception; public class MessageException extends RuntimeException { diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java similarity index 72% rename from mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java rename to mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java index ba7c3c40..fe30b371 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/exception/SendMessageException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.exception; +package com.mpush.api.exception; /** * Created by ohun on 2015/12/30. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java similarity index 94% rename from mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java rename to mpush-api/src/main/java/com/mpush/api/protocol/Command.java index d1c34b98..7e2dae4d 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.protocol; +package com.mpush.api.protocol; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java similarity index 98% rename from mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java rename to mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 31068646..c6c5fcb0 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.protocol; +package com.mpush.api.protocol; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java similarity index 94% rename from mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java rename to mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index ba022e7a..b0652758 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.api.router; +package com.mpush.api.router; -import com.shinemo.mpush.api.connection.SessionContext; +import com.mpush.api.connection.SessionContext; /** * Created by ohun on 2015/12/23. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/Router.java b/mpush-api/src/main/java/com/mpush/api/router/Router.java similarity index 82% rename from mpush-api/src/main/java/com/shinemo/mpush/api/router/Router.java rename to mpush-api/src/main/java/com/mpush/api/router/Router.java index 03af6152..ef3c6629 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/Router.java +++ b/mpush-api/src/main/java/com/mpush/api/router/Router.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.router; +package com.mpush.api.router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java similarity index 92% rename from mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java rename to mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index 46114fea..c7eebb4a 100644 --- a/mpush-api/src/main/java/com/shinemo/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.api.router; +package com.mpush.api.router; /** * Created by ohun on 2015/12/23. diff --git a/assembly.xml b/mpush-boot/assembly.xml similarity index 73% rename from assembly.xml rename to mpush-boot/assembly.xml index 6cdaef0d..8d71b814 100644 --- a/assembly.xml +++ b/mpush-boot/assembly.xml @@ -3,7 +3,7 @@ xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> - jar-with-dependency + release mpush true @@ -11,24 +11,17 @@ - mpush-cs/target/classes/ + target/classes/ config.properties - mpush-cs/target/ + target/ - lib/*.jar - - - - mpush-cs/target/ - - - mpush-cs.jar + boot.jar diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml new file mode 100644 index 00000000..1b65ce50 --- /dev/null +++ b/mpush-boot/pom.xml @@ -0,0 +1,93 @@ + + + mpush + com.mpush + 1.0 + + 4.0.0 + ${mpush.groupId} + mpush-boot + ${mpush-cs-version} + mpush-boot + jar + + + + ${mpush.groupId} + mpush-core + + + ${mpush.groupId} + mpush-monitor + + + ${mpush.groupId} + mpush-zk + + + org.codehaus.janino + janino + + + + + boot + + ../conf-${deploy.env}.properties + + + + src/main/resources + + **/* + + true + + + + + maven-jar-plugin + + + + false + + + + true + + lib/ + + com.mpush.Main + + + + + + package + + + + + maven-assembly-plugin + 2.6 + + mpush + + assembly.xml + + + + + package + + single + + + + + + + + diff --git a/mpush-boot/src/main/java/com/mpush/Main.java b/mpush-boot/src/main/java/com/mpush/Main.java new file mode 100644 index 00000000..82f2bff7 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/Main.java @@ -0,0 +1,19 @@ +package com.mpush; + +public class Main { + + public static void main(String[] args) { + ServerLauncher launcher = new ServerLauncher(); + launcher.start(); + addHook(launcher); + } + + private static void addHook(final ServerLauncher serverBoot) { + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + serverBoot.stop(); + } + }); + } + +} diff --git a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java new file mode 100644 index 00000000..b8c3972f --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java @@ -0,0 +1,47 @@ +package com.mpush; + + +import com.mpush.boot.*; +import com.mpush.zk.ZKServerNode; +import com.mpush.api.Server; +import com.mpush.core.server.AdminServer; +import com.mpush.core.server.ConnectionServer; +import com.mpush.core.server.GatewayServer; +import com.mpush.tools.config.ConfigCenter; + +public class ServerLauncher { + private final ZKServerNode csNode = ZKServerNode.csNode(); + + private final ZKServerNode gsNode = ZKServerNode.gsNode(); + + private final Server connectServer = new ConnectionServer(csNode.getPort()); + + private final Server gatewayServer = new GatewayServer(gsNode.getPort()); + + private final Server adminServer = new AdminServer(ConfigCenter.holder.adminPort()); + + + public void start() { + BootChain chain = BootChain.chain(); + chain.boot() + .setNext(new RedisBoot()) + .setNext(new ZKBoot()) + .setNext(new ServerBoot(connectServer, csNode)) + .setNext(new ServerBoot(gatewayServer, gsNode)) + .setNext(new ServerBoot(adminServer, null)) + .setNext(new EndBoot()); + chain.run(); + } + + public void stop() { + stopServer(gatewayServer); + stopServer(gatewayServer); + stopServer(adminServer); + } + + private void stopServer(Server server) { + if (server != null) { + server.stop(null); + } + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java b/mpush-boot/src/main/java/com/mpush/boot/BootChain.java new file mode 100644 index 00000000..3ffdfde9 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/BootChain.java @@ -0,0 +1,31 @@ +package com.mpush.boot; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class BootChain { + private BootJob first = first(); + + public void run() { + first.run(); + } + + public static BootChain chain() { + return new BootChain(); + } + + private BootJob first() { + return new BootJob() { + @Override + public void run() { + next(); + } + }; + } + + public BootJob boot() { + return first; + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java b/mpush-boot/src/main/java/com/mpush/boot/BootJob.java new file mode 100644 index 00000000..f2c8272c --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/BootJob.java @@ -0,0 +1,23 @@ +package com.mpush.boot; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public abstract class BootJob { + private BootJob next; + + abstract void run(); + + public void next() { + if (next != null) { + next.run(); + } + } + + public BootJob setNext(BootJob next) { + this.next = next; + return next; + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/EndBoot.java b/mpush-boot/src/main/java/com/mpush/boot/EndBoot.java new file mode 100644 index 00000000..fbaa81c4 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/EndBoot.java @@ -0,0 +1,22 @@ +package com.mpush.boot; + +import com.mpush.common.manage.user.UserManager; +import com.mpush.monitor.service.MonitorDataCollector; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.dns.manage.DnsMappingManage; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class EndBoot extends BootJob { + @Override + public void run() { + UserManager.INSTANCE.clearUserOnlineData(); + if (ConfigCenter.holder.httpProxyEnable()) { + DnsMappingManage.holder.init(); + } + MonitorDataCollector.start(ConfigCenter.holder.skipDump()); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java new file mode 100644 index 00000000..32bfefae --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java @@ -0,0 +1,37 @@ +package com.mpush.boot; + +import com.google.common.base.Strings; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.redis.manage.RedisManage; + +import java.util.List; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class RedisBoot extends BootJob { + + @Override + public void run() { + List groupList = ConfigCenter.holder.redisGroups(); + if (groupList.isEmpty()) throw new RuntimeException("init redis sever ex"); + boolean exist = ZKClient.I.isExisted(ZKPath.REDIS_SERVER.getPath()); + String rawGroup = ZKClient.I.get(ZKPath.REDIS_SERVER.getPath()); + if (!exist || Strings.isNullOrEmpty(rawGroup)) { + ZKClient.I.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } + //强刷 + boolean forceWriteRedisGroupInfo = ConfigCenter.holder.forceWriteRedisGroupInfo(); + if (forceWriteRedisGroupInfo) { + ZKClient.I.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } + RedisManage.test(groupList); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java new file mode 100644 index 00000000..5fce0514 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java @@ -0,0 +1,59 @@ +package com.mpush.boot; + +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKServerNode; +import com.mpush.api.Server; +import com.mpush.tools.Jsons; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ServerBoot extends BootJob { + private final Logger log = LoggerFactory.getLogger(ServerBoot.class); + + private final Server server; + private final ZKServerNode node; + + public ServerBoot(Server server, ZKServerNode node) { + this.server = server; + this.node = node; + } + + @Override + public void run() { + ThreadPoolManager.newThread(server.getClass().getSimpleName(), new Runnable() { + @Override + public void run() { + server.init(); + server.start(new Server.Listener() { + @Override + public void onSuccess() { + log.error("mpush app start " + server.getClass().getSimpleName() + " server success...."); + if (node != null) { + registerServerToZk(node.getZkPath(), Jsons.toJson(node)); + } + next(); + } + + @Override + public void onFailure(String message) { + log.error("mpush app start " + server.getClass().getSimpleName() + + " server failure, jvm exit with code -1"); + System.exit(-1); + } + }); + } + }).start(); + } + + //step7 注册应用到zk + public void registerServerToZk(String path, String value) { + ZKClient.I.registerEphemeralSequential(path, value); + log.error("register server to zk:{},{}", path, value); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java new file mode 100644 index 00000000..dbdb2360 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java @@ -0,0 +1,45 @@ +package com.mpush.boot; + +import com.google.common.collect.Lists; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKDataChangeListener; +import com.mpush.zk.listener.ZKRedisNodeListener; + +import java.util.List; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKBoot extends BootJob { + private List dataChangeListeners = Lists.newArrayList(); + + public ZKBoot() { + registerListener(new ZKRedisNodeListener()); + } + + public void registerListener(ZKDataChangeListener listener) { + dataChangeListeners.add(listener); + } + + private void registerListeners() { + for (ZKDataChangeListener listener : dataChangeListeners) { + ZKClient.I.registerListener(listener); + } + } + + private void initListenerData() { + for (ZKDataChangeListener listener : dataChangeListeners) { + listener.initData(); + } + } + + + @Override + public void run() { + registerListeners(); + initListenerData(); + next(); + } +} diff --git a/mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-boot/src/main/resources/META-INF/services/com.shinemo.mpush.common.manage.ServerManage similarity index 100% rename from mpush-cs/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage rename to mpush-boot/src/main/resources/META-INF/services/com.shinemo.mpush.common.manage.ServerManage diff --git a/mpush-cs/src/main/resources/config.properties b/mpush-boot/src/main/resources/config.properties similarity index 100% rename from mpush-cs/src/main/resources/config.properties rename to mpush-boot/src/main/resources/config.properties diff --git a/mpush-cs/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml similarity index 100% rename from mpush-cs/src/main/resources/logback.xml rename to mpush-boot/src/main/resources/logback.xml diff --git a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java b/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java new file mode 100644 index 00000000..2a2d6d5e --- /dev/null +++ b/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java @@ -0,0 +1,13 @@ +package com.mpush; + +import org.junit.Test; + +public class ConnectionServerApplicationTest { + + @Test + public void testJson() throws Exception{ + + + } + +} diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ServerManageTest.java b/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java similarity index 90% rename from mpush-cs/src/test/java/com/shinemo/mpush/zk/ServerManageTest.java rename to mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java index a8e86100..d4c66e7b 100644 --- a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ServerManageTest.java +++ b/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java @@ -1,4 +1,4 @@ -//package com.shinemo.mpush.core.zk; +//package com.mpush.core.zk; // //import java.util.concurrent.CountDownLatch; //import java.util.concurrent.Executor; @@ -8,10 +8,10 @@ //import org.slf4j.Logger; //import org.slf4j.LoggerFactory; // -//import com.shinemo.mpush.tools.MPushUtil; -//import com.shinemo.mpush.tools.zk.ServerApp; -//import com.shinemo.mpush.tools.zk.ZKPath; -//import com.shinemo.mpush.tools.zk.manage.ServerManage; +//import MPushUtil; +//import com.mpush.tools.zk.ServerApp; +//import com.mpush.tools.zk.ZKPath; +//import com.mpush.tools.zk.manage.ServerManage; // //public class ServerManageTest { // diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java new file mode 100644 index 00000000..330948c6 --- /dev/null +++ b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java @@ -0,0 +1,19 @@ +package com.mpush.zk; + +import com.mpush.zk.ZKClient; +import org.junit.Test; + + +public class ZkTest { + + @Test + public void remove() { + ZKClient zkRegister = new ZKClient(); + + zkRegister.init(); + + zkRegister.remove("/"); + + } + +} diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java similarity index 83% rename from mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java rename to mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java index f25c9acd..98fad0b3 100644 --- a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkUtilTest.java +++ b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java @@ -1,31 +1,25 @@ -package com.shinemo.mpush.zk; - -import java.util.List; +package com.mpush.zk; +import com.google.common.collect.Lists; +import com.mpush.tools.Constants; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.Jsons; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.redis.RedisNode; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; -import com.google.common.collect.Lists; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.core.server.ConnectionServer; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisNode; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager; +import java.util.List; public class ZkUtilTest { - private ZkRegister zkUtil; + private ZKClient zkUtil = ZKClient.I; @Before public void setUp() throws Exception { - zkUtil = new ZkRegisterManager(); + zkUtil = new ZKClient(); } @Test @@ -62,7 +56,7 @@ public void getTest() { @Test public void testRegister() { - String path = "/" + zkUtil.getZkConfig().getNamespace(); + String path = "/" + zkUtil.getZKConfig().getNamespace(); String prefix = Constants.ZK_REGISTER_PREFIX_NAME; @@ -101,7 +95,7 @@ public void testLocalIp() { @Test public void testRegisterIp() throws Exception { String localIp = MPushUtil.getInetAddress(); - ConnectionServerApplication app = new ConnectionServerApplication(); + ZKServerNode app = new ZKServerNode(); zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); String value = zkUtil.get("/" + localIp); System.out.println(value); diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index d41af302..2ac6b833 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -2,52 +2,61 @@ - - mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml - - 4.0.0 + + mpush + com.mpush + 1.0 + + 4.0.0 + ${mpush.groupId} mpush-client + ${mpush-client-version} mpush-client jar - com.shinemo.mpush + ${mpush.groupId} mpush-api - com.shinemo.mpush + ${mpush.groupId} mpush-netty + + ${mpush.groupId} + mpush-zk + + + com.mpush + mpush-common + - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-client - - - - - + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + true + true + + + com.shinemo.mpush:mpush-client + + + + + diff --git a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java similarity index 92% rename from mpush-client/src/main/java/com/shinemo/mpush/conn/client/ClientChannelHandler.java rename to mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java index 7d1548e4..79602b36 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java @@ -1,23 +1,23 @@ -package com.shinemo.mpush.conn.client; +package com.mpush.conn.client; import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.common.message.*; -import com.shinemo.mpush.common.security.AesCipher; -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.netty.client.ChannelClientHandler; -import com.shinemo.mpush.netty.client.NettyClient; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.netty.client.SecurityNettyClient; -import com.shinemo.mpush.netty.connection.NettyConnection; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.Client; +import com.mpush.api.RedisKey; +import com.mpush.common.message.*; +import com.mpush.common.security.AesCipher; +import com.mpush.common.security.CipherBox; +import com.mpush.netty.client.NettyClientFactory; +import com.mpush.netty.client.ChannelClientHandler; +import com.mpush.netty.client.NettyClient; +import com.mpush.netty.client.SecurityNettyClient; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.redis.manage.RedisManage; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java b/mpush-client/src/main/java/com/mpush/push/ConnectClient.java new file mode 100644 index 00000000..65ef68b9 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/push/ConnectClient.java @@ -0,0 +1,12 @@ +package com.mpush.push; + +import com.mpush.common.AbstractClient; +import com.mpush.push.zk.listener.ConnectZKListener; + +public class ConnectClient extends AbstractClient{ + + public ConnectClient() { + registerListener(new ConnectZKListener()); + } + +} diff --git a/mpush-client/src/main/java/com/mpush/push/PushClient.java b/mpush-client/src/main/java/com/mpush/push/PushClient.java new file mode 100644 index 00000000..b4225861 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/push/PushClient.java @@ -0,0 +1,47 @@ +package com.mpush.push; + +import com.google.common.base.Strings; +import com.mpush.api.PushSender; +import com.mpush.api.connection.Connection; +import com.mpush.common.AbstractClient; +import com.mpush.push.zk.listener.GatewayZKListener; + +import java.util.Collection; + +public class PushClient extends AbstractClient implements PushSender { + private static final int DEFAULT_TIMEOUT = 3000; + private GatewayZKListener listener = new GatewayZKListener(); + + public PushClient() { + registerListener(listener); + } + + public void send(String content, Collection userIds, Callback callback) { + if (Strings.isNullOrEmpty(content)) return; + for (String userId : userIds) { + PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(); + } + } + + @Override + public void send(String content, String userId, Callback callback) { + PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(); + } + + public Connection getGatewayConnection(String ip) { + return listener.getManager().getConnection(ip); + } + +} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/push/PushRequest.java similarity index 71% rename from mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java rename to mpush-client/src/main/java/com/mpush/push/PushRequest.java index 12a835c9..baa71a7d 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/push/PushRequest.java @@ -1,20 +1,15 @@ -package com.shinemo.mpush.push; +package com.mpush.push; import com.google.common.collect.Maps; -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.common.router.ConnectionRouterManager; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.push.manage.impl.GatewayServerManage; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.spi.ServiceContainer; - +import com.mpush.api.PushSender; +import com.mpush.api.connection.Connection; +import com.mpush.api.router.ClientLocation; +import com.mpush.tools.Jsons; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.RemoteRouter; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +22,7 @@ public class PushRequest implements PushSender.Callback, Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - private final static GatewayServerManage gatewayClientManage = (GatewayServerManage) ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); + private final PushClient client; private PushSender.Callback callback; private String userId; @@ -37,13 +32,14 @@ public class PushRequest implements PushSender.Callback, Runnable { private int sessionId; private long sendTime; private AtomicInteger status = new AtomicInteger(0); - private Map times = Maps.newHashMap(); + private Map times = Maps.newHashMap(); - public PushRequest() { + public PushRequest(PushClient client) { + this.client = client; } - public static PushRequest build() { - return new PushRequest(); + public static PushRequest build(PushClient client) { + return new PushRequest(client); } public PushRequest setCallback(PushSender.Callback callback) { @@ -68,29 +64,29 @@ public PushRequest setTimeout(long timeout) { @Override public void onSuccess(String userId) { - putTime("success"); - LOGGER.info("success,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); + putTime("success"); + LOGGER.info("success,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); submit(1); } @Override public void onFailure(String userId) { - putTime("failure"); - LOGGER.info("failure,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); + putTime("failure"); + LOGGER.info("failure,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); submit(2); } @Override public void onOffline(String userId) { - putTime("offline"); - LOGGER.info("offline,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); + putTime("offline"); + LOGGER.info("offline,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); submit(3); } @Override public void onTimeout(String userId) { - putTime("timeout"); - LOGGER.info("timeout,sessionId:{},times:{},content:{}",sessionId,Jsons.toJson(times),content); + putTime("timeout"); + LOGGER.info("timeout,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); submit(4); } @@ -123,8 +119,7 @@ public void run() { } public boolean isTimeout() { - long delt = System.currentTimeMillis() - timeout_; - return delt > 0 ? true :false; + return System.currentTimeMillis() > timeout_; } public void timeout() { @@ -169,14 +164,14 @@ private void sendToConnServer() { //2.通过网关连接,把消息发送到所在机器 ClientLocation location = router.getRouteValue(); - Connection gatewayConn = gatewayClientManage.getConnection(location.getHost()); + Connection gatewayConn = client.getGatewayConnection(location.getHost()); if (gatewayConn == null || !gatewayConn.isConnected()) { this.onFailure(userId); return; } putTime("sendtoconnserver"); - + final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); pushMessage.sendRaw(new ChannelFutureListener() { @Override @@ -198,12 +193,12 @@ public long getSendTime() { return sendTime; } - public Map getTimes() { - return times; - } + public Map getTimes() { + return times; + } + + public void putTime(String key) { + this.times.put(key, System.currentTimeMillis()); + } - public void putTime(String key) { - this.times.put(key, System.currentTimeMillis()); - } - } diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java similarity index 97% rename from mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java rename to mpush-client/src/main/java/com/mpush/push/PushRequestBus.java index 0bac05bb..8460343c 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.push; +package com.mpush.push; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java similarity index 79% rename from mpush-client/src/main/java/com/shinemo/mpush/push/client/ClientChannelHandler.java rename to mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java index f576941c..e2a9ad25 100644 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java @@ -1,16 +1,16 @@ -package com.shinemo.mpush.push.client; +package com.mpush.push.client; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.netty.client.ChannelClientHandler; -import com.shinemo.mpush.netty.connection.NettyConnection; -import com.shinemo.mpush.push.PushRequest; -import com.shinemo.mpush.push.PushRequestBus; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.Client; +import com.mpush.common.message.ErrorMessage; +import com.mpush.netty.client.ChannelClientHandler; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.push.PushRequest; +import com.mpush.push.PushRequestBus; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; @@ -20,9 +20,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static com.shinemo.mpush.common.ErrorCode.OFFLINE; -import static com.shinemo.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.shinemo.mpush.common.ErrorCode.ROUTER_CHANGE; +import static com.mpush.common.ErrorCode.OFFLINE; +import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.mpush.common.ErrorCode.ROUTER_CHANGE; /** * Created by ohun on 2015/12/19. diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java new file mode 100644 index 00000000..98091e43 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java @@ -0,0 +1,35 @@ +package com.mpush.push.zk.listener; + + +import com.mpush.zk.ZKPath; +import com.mpush.zk.listener.ZKServerNodeListener; +import com.mpush.push.zk.manager.ConnectZKNodeManager; + +/** + * connection server 应用 监控 + */ +public class ConnectZKListener extends ZKServerNodeListener { + + private final ConnectZKNodeManager manager = new ConnectZKNodeManager(); + + @Override + public String listenerPath() { + return ZKPath.CONNECTION_SERVER.getWatchPath(); + } + + @Override + public String getRegisterPath() { + return ZKPath.CONNECTION_SERVER.getPath(); + } + + @Override + public ConnectZKNodeManager getManager() { + return manager; + } + + @Override + public String getFullPath(String raw) { + return ZKPath.CONNECTION_SERVER.getFullPath(raw); + } + +} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java new file mode 100644 index 00000000..17f90570 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java @@ -0,0 +1,35 @@ +package com.mpush.push.zk.listener; + +import com.mpush.push.zk.manager.GatewayZKNodeManager; +import com.mpush.zk.ZKPath; +import com.mpush.zk.listener.ZKServerNodeListener; + +/** + * gateway server 应用 监控 + */ +public class GatewayZKListener extends ZKServerNodeListener { + + private final GatewayZKNodeManager manager = new GatewayZKNodeManager(); + + @Override + public String listenerPath() { + return ZKPath.GATEWAY_SERVER.getWatchPath(); + } + + @Override + public GatewayZKNodeManager getManager() { + return manager; + } + + @Override + public String getRegisterPath() { + return ZKPath.GATEWAY_SERVER.getPath(); + } + + @Override + public String getFullPath(String raw) { + return ZKPath.GATEWAY_SERVER.getFullPath(raw); + } + + +} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java new file mode 100644 index 00000000..b7915dc7 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java @@ -0,0 +1,47 @@ +package com.mpush.push.zk.manager; + +import com.google.common.collect.Maps; +import com.mpush.zk.ZKServerNode; +import com.mpush.zk.ZKNodeManager; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +public class ConnectZKNodeManager implements ZKNodeManager { + + private final Logger log = LoggerFactory.getLogger(ConnectZKNodeManager.class); + + private Map cache = Maps.newConcurrentMap(); + + @Override + public void addOrUpdate(String fullPath, ZKServerNode node) { + if (StringUtils.isNotBlank(fullPath) && node != null) { + cache.put(fullPath, node); + } else { + log.error("fullPath is null or application is null"); + } + printList(); + } + + @Override + public void remove(String fullPath) { + cache.remove(fullPath); + printList(); + } + + @Override + public Collection getList() { + return Collections.unmodifiableCollection(cache.values()); + } + + private void printList() { + for (ZKServerNode app : cache.values()) { + log.warn(app.toString()); + } + } + +} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java new file mode 100644 index 00000000..66648c52 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java @@ -0,0 +1,83 @@ +package com.mpush.push.zk.manager; + +import com.google.common.collect.Maps; +import com.mpush.netty.client.NettyClientFactory; +import com.mpush.zk.ZKNodeManager; +import com.mpush.zk.ZKServerNode; +import com.mpush.api.Client; +import com.mpush.api.connection.Connection; +import com.mpush.netty.client.NettyClient; +import com.mpush.push.client.ClientChannelHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +public class GatewayZKNodeManager implements ZKNodeManager { + + private final Logger log = LoggerFactory.getLogger(GatewayZKNodeManager.class); + + private static Map holder = Maps.newConcurrentMap(); + + private final Map application2Client = Maps.newConcurrentMap(); + + private final Map ip2Client = Maps.newConcurrentMap(); + + @Override + public void addOrUpdate(String fullPath, ZKServerNode application) { + holder.put(fullPath, application); + try { + Client client = new NettyClient(application.getIp(), application.getPort()); + ClientChannelHandler handler = new ClientChannelHandler(client); + NettyClientFactory.INSTANCE.create(handler); + application2Client.put(application, client); + ip2Client.put(application.getIp(), client); + } catch (Exception e) { + log.error("addOrUpdate:{},{}", fullPath, application, e); + } + printList(); + } + + @Override + public void remove(String fullPath) { + ZKServerNode application = get(fullPath); + if (application != null) { + Client client = application2Client.get(application); + if (client != null) { + client.stop(); + } + } + ip2Client.remove(application.getIp() + ":" + application.getPort()); + holder.remove(fullPath); + printList(); + } + + @Override + public Collection getList() { + return Collections.unmodifiableCollection(holder.values()); + } + + private void printList() { + for (ZKServerNode app : holder.values()) { + log.warn(app.toString()); + } + } + + public ZKServerNode get(String fullpath) { + return holder.get(fullpath); + } + + public Client getClient(ZKServerNode application) { + return application2Client.get(application); + } + + public Connection getConnection(String ipAndPort) { + Client client = ip2Client.get(ipAndPort); + if (client == null) return null; + return client.getConnection(); + } + +} + diff --git a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java deleted file mode 100644 index 5e7e47f4..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/ConnectionServerApplication.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.shinemo.mpush.conn.client; - -import com.shinemo.mpush.common.app.Application; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.zk.ZKPath; -import org.slf4j.Logger; - -public class ConnectionServerApplication extends Application { - private transient GatewayServerApplication gatewayServerApplication; - - public ConnectionServerApplication() throws Exception { - this(ConfigCenter.holder.connectionServerPort(), ZKPath.CONNECTION_SERVER.getWatchPath(), MPushUtil.getLocalIp(), MPushUtil.getExtranetAddress()); - } - - public ConnectionServerApplication(int port, String path, String ip, String extranetIp) { - setPort(port); - setServerRegisterZkPath(path); - setIp(ip); - setExtranetIp(extranetIp); - } - - public GatewayServerApplication getGatewayServerApplication() { - return gatewayServerApplication; - } - - public void setGatewayServerApplication(GatewayServerApplication gatewayServerApplication) { - this.gatewayServerApplication = gatewayServerApplication; - } - -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java b/mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java deleted file mode 100644 index 98cb23df..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/conn/client/GatewayServerApplication.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.shinemo.mpush.conn.client; - -import com.shinemo.mpush.common.app.Application; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.zk.ZKPath; - - -public class GatewayServerApplication extends Application{ - - public GatewayServerApplication() { - this(ConfigCenter.holder.gatewayServerPort(),ZKPath.GATEWAY_SERVER.getWatchPath(),MPushUtil.getLocalIp()); - } - - public GatewayServerApplication(int port,String path,String ip) { - setPort(port); - setServerRegisterZkPath(path); - setIp(ip); - } - -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java b/mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java deleted file mode 100644 index 971051fe..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/ConnClient.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.shinemo.mpush.push; - -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.push.zk.listener.impl.ConnectionServerPathListener; - -public class ConnClient extends AbstractClient{ - - public ConnClient() { - registerListener(new ConnectionServerPathListener()); - } - -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java b/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java deleted file mode 100644 index 950170df..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/PushClient.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.shinemo.mpush.push; - -import java.util.Collection; - -import com.google.common.base.Strings; -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.push.zk.listener.impl.GatewayServerPathListener; - -public class PushClient extends AbstractClient implements PushSender{ - - private static final int defaultTimeout = 3000; - - public PushClient() { - registerListener(new GatewayServerPathListener()); - } - - public void send(String content, Collection userIds, Callback callback) { - if (Strings.isNullOrEmpty(content)) return; - for (String userId : userIds) { - PushRequest - .build() - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(defaultTimeout) - .send(); - } - } - - @Override - public void send(String content, String userId, Callback callback) { - PushRequest - .build() - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(defaultTimeout) - .send(); - } - -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java deleted file mode 100644 index a26d02e8..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/ConnectionServerManage.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.shinemo.mpush.push.manage.impl; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; - -public class ConnectionServerManage implements ServerManage{ - - private static final Logger log = LoggerFactory.getLogger(ConnectionServerManage.class); - - private static Map holder = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath,ConnectionServerApplication application){ - if(StringUtils.isNotBlank(fullPath)&&application!=null){ - holder.put(fullPath, application); - }else{ - log.error("fullPath is null or application is null"+fullPath==null?"fullpath is null":fullPath+","+application==null?"application is null":"application is not null"); - } - - printList(); - } - - @Override - public void remove(String fullPath){ - holder.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(holder.values()); - } - - private void printList(){ - for(ConnectionServerApplication app:holder.values()){ - log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); - } - } - -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java b/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java deleted file mode 100644 index 345bb4b9..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/manage/impl/GatewayServerManage.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.shinemo.mpush.push.manage.impl; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Maps; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.conn.client.GatewayServerApplication; -import com.shinemo.mpush.netty.client.NettyClient; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.push.client.ClientChannelHandler; - -public class GatewayServerManage implements ServerManage{ - - private static final Logger log = LoggerFactory.getLogger(GatewayServerManage.class); - - private static Map holder = Maps.newConcurrentMap(); - - private final Map application2Client = Maps.newConcurrentMap(); - - private final Map ip2Client = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath,GatewayServerApplication application){ - holder.put(fullPath, application); - try{ - Client client = new NettyClient(application.getIp(), application.getPort()); - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - application2Client.put(application, client); - ip2Client.put(application.getIp(), client); - }catch(Exception e){ - log.error("addOrUpdate:{},{}",fullPath,application,e); - } - printList(); - } - - @Override - public void remove(String fullPath){ - GatewayServerApplication application = get(fullPath); - if(application!=null){ - Client client = application2Client.get(application); - if(client!=null){ - client.stop(); - } - } - ip2Client.remove(application.getIp()+":"+application.getPort()); - holder.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(holder.values()); - } - - private void printList(){ - for(GatewayServerApplication app:holder.values()){ - log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE)); - } - } - - public GatewayServerApplication get(String fullpath){ - return holder.get(fullpath); - } - - public Client getClient(GatewayServerApplication application){ - return application2Client.get(application); - } - - public Connection getConnection(String ipAndPort) { - Client client = ip2Client.get(ipAndPort); - if (client == null) return null; - return client.getConnection(); - } - -} - diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java deleted file mode 100644 index 9fa1f8d8..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/ConnectionServerPathListener.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.shinemo.mpush.push.zk.listener.impl; - - -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; - -/** - * connection server 应用 监控 - * - */ -public class ConnectionServerPathListener extends AbstractDataChangeListener{ - - @SuppressWarnings("unchecked") - private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); - - @Override - public String listenerPath() { - return ZKPath.CONNECTION_SERVER.getWatchPath(); - } - - @Override - public String getRegisterPath() { - return ZKPath.CONNECTION_SERVER.getPath(); - } - - @Override - public ServerManage getServerManage() { - return connectionServerManage; - } - - @Override - public String getFullPath(String raw) { - return ZKPath.CONNECTION_SERVER.getFullPath(raw); - } - -} diff --git a/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java b/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java deleted file mode 100644 index 642bbf18..00000000 --- a/mpush-client/src/main/java/com/shinemo/mpush/push/zk/listener/impl/GatewayServerPathListener.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.shinemo.mpush.push.zk.listener.impl; - -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.common.zk.listener.AbstractDataChangeListener; -import com.shinemo.mpush.conn.client.GatewayServerApplication; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; - -/** - * gateway server 应用 监控 - * - */ -public class GatewayServerPathListener extends AbstractDataChangeListener{ - - @SuppressWarnings("unchecked") - private ServerManage gatewayServerManage = ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage"); - - @Override - public String listenerPath() { - return ZKPath.GATEWAY_SERVER.getWatchPath(); - } - - @Override - public ServerManage getServerManage() { - return gatewayServerManage; - } - - @Override - public String getRegisterPath() { - return ZKPath.GATEWAY_SERVER.getPath(); - } - - @Override - public String getFullPath(String raw) { - return ZKPath.GATEWAY_SERVER.getFullPath(raw); - } - - - -} diff --git a/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage deleted file mode 100644 index c23f617e..00000000 --- a/mpush-client/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.common.manage.ServerManage +++ /dev/null @@ -1,2 +0,0 @@ -gatewayServerManage=com.shinemo.mpush.push.manage.impl.GatewayServerManage -connectionServerManage=com.shinemo.mpush.push.manage.impl.ConnectionServerManage diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager new file mode 100644 index 00000000..c68945f8 --- /dev/null +++ b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager @@ -0,0 +1,2 @@ +com.mpush.push.zk.manager.GatewayZKNodeManager +com.shinemo.mpush.push.manage.impl.ConnectZKNodeManager diff --git a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/mpush/client/PushClientTest.java similarity index 94% rename from mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java rename to mpush-client/src/test/java/com/mpush/client/PushClientTest.java index 743bfbce..cd9c4979 100644 --- a/mpush-client/src/test/java/com/shinemo/mpush/client/PushClientTest.java +++ b/mpush-client/src/test/java/com/mpush/client/PushClientTest.java @@ -1,6 +1,6 @@ -//package com.shinemo.mpush.client; +//package com.mpush.client; // -//import com.shinemo.mpush.api.PushSender; +//import PushSender; //import org.junit.Test; // //import java.util.Arrays; diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 9288b544..e3a9fa1c 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -4,25 +4,31 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml + com.mpush + 1.0 4.0.0 + ${mpush.groupId} mpush-common + ${mpush-common-version} mpush-common jar - com.shinemo.mpush + ${mpush.groupId} mpush-api - com.shinemo.mpush + ${mpush.groupId} mpush-tools + + ${mpush.groupId} + mpush-zk + + diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java new file mode 100644 index 00000000..fa84d09c --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java @@ -0,0 +1,64 @@ +package com.mpush.common; + +import java.util.List; + + +import com.google.common.collect.Lists; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.zk.listener.ZKDataChangeListener; +import com.mpush.zk.listener.ZKRedisNodeListener; + +public abstract class AbstractClient { + + protected List dataChangeListeners = Lists.newArrayList(); + + protected ZKClient zkClient = ZKClient.I; + + public AbstractClient() { + registerListener(new ZKRedisNodeListener()); + } + + + public void registerListener(ZKDataChangeListener listener){ + dataChangeListeners.add(listener); + } + + //step1 启动 zk + private void initZK(){ + zkClient.init(); + } + + //step2 获取redis + private void initRedis(){ + boolean exist = zkClient.isExisted(ZKPath.REDIS_SERVER.getPath()); + if (!exist) { + List groupList = ConfigCenter.holder.redisGroups(); + zkClient.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + } + } + + //step3 注册listener + private void registerListeners(){ + for(ZKDataChangeListener listener:dataChangeListeners){ + zkClient.registerListener(listener); + } + } + + //step4 初始化 listener data + private void initListenerData(){ + for(ZKDataChangeListener listener:dataChangeListeners){ + listener.initData(); + } + } + + public void start(){ + initZK(); + initRedis(); + registerListeners(); + initListenerData(); + } +} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java b/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java similarity index 78% rename from mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java rename to mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java index ff11636c..a9081df0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractEventContainer.java +++ b/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common; +package com.mpush.common; public abstract class AbstractEventContainer { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java similarity index 95% rename from mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java rename to mpush-common/src/main/java/com/mpush/common/ErrorCode.java index 8520a11f..f93f19d5 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common; +package com.mpush.common; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java b/mpush-common/src/main/java/com/mpush/common/EventBus.java similarity index 89% rename from mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java rename to mpush-common/src/main/java/com/mpush/common/EventBus.java index 4cbbb4b1..1291da39 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/EventBus.java +++ b/mpush-common/src/main/java/com/mpush/common/EventBus.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.common; +package com.mpush.common; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; -import com.shinemo.mpush.api.event.Event; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.api.event.Event; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java similarity index 77% rename from mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java rename to mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 45ad18b3..df218503 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -1,12 +1,12 @@ -package com.shinemo.mpush.common; +package com.mpush.common; -import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.PacketReceiver; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.tools.Profiler; +import com.mpush.api.MessageHandler; +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.Profiler; +import com.mpush.common.message.ErrorMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java similarity index 66% rename from mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java rename to mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index b45df761..72522de9 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.common.handler; +package com.mpush.common.handler; -import com.shinemo.mpush.api.Message; -import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.Profiler; +import com.mpush.api.Message; +import com.mpush.api.MessageHandler; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.Profiler; /** * Created by ohun on 2015/12/22. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java similarity index 63% rename from mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java rename to mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java index 9f96837f..2e01ddc1 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/ErrorMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.handler; +package com.mpush.common.handler; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.common.message.ErrorMessage; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java similarity index 62% rename from mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java rename to mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java index b7be18ec..ad383984 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/handler/OkMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.handler; +package com.mpush.common.handler; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.message.OkMessage; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.common.message.OkMessage; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java similarity index 86% rename from mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java rename to mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java index 2363982f..1269273b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.common.manage.user; +package com.mpush.common.manage.user; import java.util.List; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.redis.manage.RedisManage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.api.RedisKey; //查询使用 public final class UserManager { diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java similarity index 90% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 2362e8cd..d5beeec0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -1,12 +1,12 @@ -package com.shinemo.mpush.common.message; - -import com.shinemo.mpush.api.Message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.IOUtils; -import com.shinemo.mpush.tools.Profiler; -import com.shinemo.mpush.tools.config.ConfigCenter; +package com.mpush.common.message; + +import com.mpush.api.Message; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.IOUtils; +import com.mpush.tools.Profiler; +import com.mpush.tools.config.ConfigCenter; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java similarity index 80% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index a40853a7..4f162cb0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; /** diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java similarity index 92% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index b7576ef8..1a233a9d 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.Constants; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java similarity index 88% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index ac23e4ec..3a70a087 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -1,12 +1,12 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.ErrorCode; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.common.ErrorCode; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; -import static com.shinemo.mpush.api.protocol.Command.ERROR; +import static com.mpush.api.protocol.Command.ERROR; /** * Created by ohun on 2015/12/28. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java similarity index 81% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java index 648e333b..e1bce4c3 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; -import static com.shinemo.mpush.api.protocol.Command.FAST_CONNECT; +import static com.mpush.api.protocol.Command.FAST_CONNECT; /** * Created by ohun on 2015/12/25. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java similarity index 84% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index 7c94b26e..6a0a29d0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; /** diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java similarity index 87% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 4d4e2191..0851e3b0 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; -import static com.shinemo.mpush.api.protocol.Command.HANDSHAKE; +import static com.mpush.api.protocol.Command.HANDSHAKE; /** * Created by ohun on 2015/12/24. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java similarity index 91% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index a451ee17..e0c75ab7 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; /** diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java similarity index 84% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index 192a9765..01a7eea8 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -1,13 +1,11 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.MPushUtil; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; -import java.util.Arrays; import java.util.Map; /** diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java similarity index 87% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 806c848b..7f9007ac 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -1,12 +1,10 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.MPushUtil; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.MPushUtil; import io.netty.buffer.ByteBuf; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java similarity index 78% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java index 7fac42ac..2436b8cf 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java @@ -1,11 +1,10 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; -import static com.shinemo.mpush.api.protocol.Command.KICK; +import static com.mpush.api.protocol.Command.KICK; /** * Created by ohun on 2015/12/29. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java similarity index 84% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index 5c4ffebd..cc4fbfe7 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -1,11 +1,10 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; -import static com.shinemo.mpush.api.protocol.Command.OK; +import static com.mpush.api.protocol.Command.OK; /** * Created by ohun on 2015/12/28. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java similarity index 72% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index 7943358c..65ac3236 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.common.message; +package com.mpush.common.message; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.Constants; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; -import static com.shinemo.mpush.api.protocol.Command.PUSH; +import static com.mpush.api.protocol.Command.PUSH; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java similarity index 81% rename from mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java rename to mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 477d4435..95717b9f 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -1,12 +1,12 @@ -package com.shinemo.mpush.common.message.gateway; +package com.mpush.common.message.gateway; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.common.message.ByteBufMessage; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.common.message.ByteBufMessage; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; -import static com.shinemo.mpush.api.protocol.Command.GATEWAY_PUSH; +import static com.mpush.api.protocol.Command.GATEWAY_PUSH; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java similarity index 96% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java rename to mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java index a031fa55..4c548316 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.common.router; +package com.mpush.common.router; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java similarity index 80% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java rename to mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index eed8dcf6..a254ab8b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.common.router; +package com.mpush.common.router; -import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.api.router.ClientLocation; +import com.mpush.api.router.Router; +import com.mpush.api.router.ClientLocation; /** * Created by ohun on 2015/12/23. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java similarity index 85% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java rename to mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 30df5025..35acb754 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.router; +package com.mpush.common.router; -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.api.router.RouterManager; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.api.RedisKey; +import com.mpush.api.router.RouterManager; +import com.mpush.tools.redis.manage.RedisManage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java similarity index 78% rename from mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java rename to mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index 7ef38392..7f9c9253 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -1,14 +1,14 @@ -package com.shinemo.mpush.common.router; +package com.mpush.common.router; +import com.mpush.tools.redis.listener.MessageListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.common.AbstractEventContainer; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; -import com.shinemo.mpush.tools.redis.listener.MessageListener; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.common.AbstractEventContainer; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.redis.listener.ListenerDispatcher; +import com.mpush.tools.redis.manage.RedisManage; /** * Created by ohun on 2016/1/4. diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java similarity index 88% rename from mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java rename to mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 40b9e8f2..6e4c40b4 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.security; +package com.mpush.common.security; -import com.shinemo.mpush.api.connection.Cipher; -import com.shinemo.mpush.tools.Profiler; -import com.shinemo.mpush.tools.crypto.AESUtils; +import com.mpush.api.connection.Cipher; +import com.mpush.tools.Profiler; +import com.mpush.tools.crypto.AESUtils; /** diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java similarity index 93% rename from mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java rename to mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index b965cac0..c6caf310 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.common.security; +package com.mpush.common.security; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.crypto.RSAUtils; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.crypto.RSAUtils; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java similarity index 85% rename from mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java rename to mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index 0b587b66..6117f65b 100644 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.common.security; +package com.mpush.common.security; -import com.shinemo.mpush.api.connection.Cipher; -import com.shinemo.mpush.tools.Profiler; -import com.shinemo.mpush.tools.crypto.RSAUtils; +import com.mpush.api.connection.Cipher; +import com.mpush.tools.Profiler; +import com.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java deleted file mode 100644 index 1e9f0518..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractClient.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.shinemo.mpush.common; - -import java.util.List; - - -import com.google.common.collect.Lists; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; -import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; - -public abstract class AbstractClient { - - protected List dataChangeListeners = Lists.newArrayList(); - - protected ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - public AbstractClient() { - registerListener(new RedisPathListener()); - } - - - public void registerListener(DataChangeListener listener){ - dataChangeListeners.add(listener); - } - - //step1 启动 zk - private void initZK(){ - zkRegister.init(); - } - - //step2 获取redis - private void initRedis(){ - boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); - if (!exist) { - List groupList = ConfigCenter.holder.redisGroups(); - zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - } - - //step3 注册listener - private void registerListeners(){ - for(DataChangeListener listener:dataChangeListeners){ - zkRegister.registerListener(listener); - } - } - - //step4 初始化 listener data - private void initListenerData(){ - for(DataChangeListener listener:dataChangeListeners){ - listener.initData(); - } - } - - public void start(){ - initZK(); - initRedis(); - registerListeners(); - initListenerData(); - } - -} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java b/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java deleted file mode 100644 index 6d888501..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/AbstractServer.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.shinemo.mpush.common; - -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Strings; -import com.google.common.collect.Lists; -import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.common.app.Application; -import com.shinemo.mpush.tools.GenericsUtil; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; -import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener; - -public abstract class AbstractServer { - - private static final Logger log = LoggerFactory.getLogger(AbstractServer.class); - - protected Application application; - - protected List dataChangeListeners = Lists.newArrayList(); - - protected ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - protected Server server; - - public AbstractServer() { - this.application = getApplication(); - registerListener(new RedisPathListener()); - } - - @SuppressWarnings("unchecked") - private Application getApplication() { - try { - return ((Class) GenericsUtil.getSuperClassGenericType(this.getClass(), 0)).newInstance(); - } catch (Exception e) { - log.error("exception:",e); - throw new RuntimeException(e); - } - } - - public abstract Server getServer(); - - public void registerListener(DataChangeListener listener){ - dataChangeListeners.add(listener); - } - - //step1 启动 zk - private void initZK(){ - zkRegister.init(); - } - - //step2 获取redis - private void initRedis(){ - boolean exist = zkRegister.isExisted(ZKPath.REDIS_SERVER.getPath()); - String rawGroup = zkRegister.get(ZKPath.REDIS_SERVER.getPath()); - if (!exist||Strings.isNullOrEmpty(rawGroup)) { - List groupList = ConfigCenter.holder.redisGroups(); - zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - //强刷 - boolean forceWriteRedisGroupInfo = ConfigCenter.holder.forceWriteRedisGroupInfo(); - if(forceWriteRedisGroupInfo){ - List groupList = ConfigCenter.holder.redisGroups(); - zkRegister.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - } - - //step3 注册listener - private void registerListeners(){ - for(DataChangeListener listener:dataChangeListeners){ - zkRegister.registerListener(listener); - } - } - - //step4 初始化 listener data - private void initListenerData(){ - for(DataChangeListener listener:dataChangeListeners){ - listener.initData(); - } - } - - //step5 初始化server - private void initServer(){ - server = getServer(); - } - - public void startServer(final Server server){ - this.startServer(server, null, null); - } - - //step6 启动 netty server - public void startServer(final Server server,final String path, final String value){ - Runnable runnable = new Runnable() { - @Override - public void run() { - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess() { - log.error("mpush app start "+server.getClass().getSimpleName()+" server success...."); - if(StringUtils.isNoneBlank(path)&&StringUtils.isNoneBlank(value)){ - registerServerToZk(path,value); - } - } - - @Override - public void onFailure(String message) { - log.error("mpush app start "+server.getClass().getSimpleName()+" server failure, jvm exit with code -1"); - System.exit(-1); - } - }); - } - }; - ThreadPoolManager.newThread(server.getClass().getSimpleName(), runnable).start(); - - } - - //step7 注册应用到zk - public void registerServerToZk(String path,String value){ - zkRegister.registerEphemeralSequential(path, value); - log.error("register server to zk:{},{}",path,value); - } - - public void start(){ - initZK(); - initRedis(); - registerListeners(); - initListenerData(); - initServer(); - startServer(server,application.getServerRegisterZkPath(),Jsons.toJson(application)); -// registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application)); - } - - public void startClient(){ - initZK(); - initRedis(); - registerListeners(); - initListenerData(); - } - - public void stopServer(Server server){ - if(server!=null){ - server.stop(null); - } - } - - public void stop(){ - stopServer(server); - } - -} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java b/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java deleted file mode 100644 index 72f01775..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/DnsMapping.java +++ /dev/null @@ -1,39 +0,0 @@ -//package com.shinemo.mpush.common; -// -//import com.google.common.base.Splitter; -//import com.google.common.base.Strings; -//import com.google.common.collect.ArrayListMultimap; -//import com.shinemo.mpush.tools.config.ConfigCenter; -// -//import java.util.List; -//import java.util.Map; -// -///** -// * Created by ohun on 2016/2/16. -// */ -//public final class DnsMapping { -// private final ArrayListMultimap mappings = ArrayListMultimap.create(); -// -// public DnsMapping() { -// String dnsString = ConfigCenter.holder.dnsMapping(); -// if (Strings.isNullOrEmpty(dnsString)) return; -// -// Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); -// Splitter vsp = Splitter.on(','); -// for (Map.Entry entry : map.entrySet()) { -// String value = entry.getValue(); -// if (Strings.isNullOrEmpty(value)) continue; -// Iterable it = vsp.split(entry.getValue()); -// mappings.putAll(entry.getKey(), it); -// } -// } -// -// public String translate(String origin) { -// if (mappings.isEmpty()) return null; -// List list = mappings.get(origin); -// if (list == null || list.isEmpty()) return null; -// int L = list.size(); -// if (L == 1) return list.get(0); -// return list.get((int) (Math.random() * L % L)); -// } -//} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java b/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java deleted file mode 100644 index a5b89619..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/app/Application.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shinemo.mpush.common.app; - - -/** - * 系统配置 - * - */ -public abstract class Application { - - private String ip; - - private int port; - - private transient String serverRegisterZkPath; - - private String extranetIp; - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public String getServerRegisterZkPath() { - return serverRegisterZkPath; - } - - public void setServerRegisterZkPath(String serverRegisterZkPath) { - this.serverRegisterZkPath = serverRegisterZkPath; - } - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public String getExtranetIp() { - return extranetIp; - } - - public void setExtranetIp(String extranetIp) { - this.extranetIp = extranetIp; - } - -} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java b/mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java deleted file mode 100644 index 0821a5c9..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/manage/ServerManage.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.shinemo.mpush.common.manage; - -import java.util.Collection; - -import com.shinemo.mpush.tools.spi.SPI; - -@SPI("connectionServerManage") -public interface ServerManage { - - public void addOrUpdate(String fullPath, T application); - - public void remove(String fullPath); - - public Collection getList(); - -} diff --git a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java b/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java deleted file mode 100644 index b28c7124..00000000 --- a/mpush-common/src/main/java/com/shinemo/mpush/common/zk/listener/AbstractDataChangeListener.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.shinemo.mpush.common.zk.listener; - -import java.util.List; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.common.app.Application; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.tools.GenericsUtil; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; - -public abstract class AbstractDataChangeListener extends DataChangeListener { - - protected static ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - private static final Logger log = LoggerFactory.getLogger(AbstractDataChangeListener.class); - - private Class clazz; - - @SuppressWarnings("unchecked") - public AbstractDataChangeListener() { - clazz = (Class) GenericsUtil.getSuperClassGenericType(this.getClass(), 0); - } - - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData(),clazz); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData(),clazz); - } else { - log.warn(this.getClass().getSimpleName()+"other path:" + path + "," + event.getType().name() + "," + data); - } - } - - public void initData() { - log.warn(zkRegister.getClient().getNamespace()+" start init "+ this.getClass().getSimpleName()+" server data"); - _initData(); - log.warn(zkRegister.getClient().getNamespace()+" end init "+ this.getClass().getSimpleName()+" server data"); - } - - public abstract String getRegisterPath(); - - public abstract String getFullPath(String raw); - - public abstract ServerManage getServerManage(); - - private void _initData() { - // 获取机器列表 - List rawData = zkRegister.getChildrenKeys(getRegisterPath()); - for (String raw : rawData) { - String fullPath = getFullPath(raw); - T app = getServerApplication(fullPath,clazz); - getServerManage().addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data) { - String path = data.getPath(); - getServerManage().remove(path); - } - - private void dataAddOrUpdate(ChildData data,Class clazz) { - String path = data.getPath(); - byte[] rawData = data.getData(); - T serverApp = Jsons.fromJson(rawData, clazz); - getServerManage().addOrUpdate(path, serverApp); - } - - private T getServerApplication(String fullPath,Class clazz) { - String rawApp = zkRegister.get(fullPath); - T app = Jsons.fromJson(rawApp,clazz); - return app; - } - - - -} diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index e2002c48..de05f1c5 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -4,22 +4,22 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml + com.mpush + 1.0 4.0.0 - + ${mpush.groupId} mpush-core + ${mpush-core-version} mpush-core jar - com.shinemo.mpush + ${mpush.groupId} mpush-netty - com.shinemo.mpush + ${mpush.groupId} mpush-client diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java new file mode 100644 index 00000000..82df0f24 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -0,0 +1,135 @@ +package com.mpush.core.handler; + +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.ZKServerNode; +import com.mpush.api.RedisKey; +import com.mpush.tools.Jsons; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.redis.manage.RedisManage; +import io.netty.channel.*; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.List; + +@ChannelHandler.Sharable +public final class AdminHandler extends SimpleChannelInboundHandler { + + private static final Logger log = LoggerFactory.getLogger(AdminHandler.class); + + private static final String DOUBLE_END = "\r\n\r\n"; + + private static final String ONE_END = "\r\n"; + + protected static final ZKClient zkClient = ZKClient.I; + + @Override + protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { + Command command = Command.getCommand(request); + ChannelFuture future = ctx.write(command.handler(request) + DOUBLE_END); + if (command.equals(Command.QUIT)) { + future.addListener(ChannelFutureListener.CLOSE); + } + + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + ONE_END); + ctx.write("It is " + new Date() + " now." + DOUBLE_END); + ctx.flush(); + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + ctx.flush(); + } + + public enum Command { + HELP("help") { + @Override + public String handler(String request) { + StringBuilder buf = new StringBuilder(); + buf.append("Command:" + ONE_END); + buf.append("help:display all command." + ONE_END); + buf.append("quit:exit telnet." + ONE_END); + buf.append("scn:statistics conn num." + ONE_END); + buf.append("rcs:remove connection server zk info." + ONE_END); + buf.append("scs:stop connection server."); + return buf.toString(); + } + }, + QUIT("quit") { + @Override + public String handler(String request) { + return "have a good day!"; + } + }, + SCN("scn") { + @Override + public String handler(String request) { + Long value = RedisManage.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); + if (value == null) { + value = 0L; + } + return value.toString() + "."; + } + }, + RCS("rcs") { + @Override + public String handler(String request) { + + List rawData = zkClient.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + boolean removeSuccess = false; + for (String raw : rawData) { + String data = zkClient.get(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + ZKServerNode serverNode = Jsons.fromJson(data, ZKServerNode.class); + if (serverNode.getIp().equals(MPushUtil.getInetAddress())) { + zkClient.remove(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + log.info("delete connection server success:{}", data); + removeSuccess = true; + } else { + log.info("delete connection server failed: required ip:{}, but:{}", serverNode.getIp(), MPushUtil.getInetAddress()); + } + } + if (removeSuccess) { + return "remove success."; + } else { + return "remove false."; + } + } + }, + SCS("scs") { + @Override + public String handler(String request) { + return "not support now."; + } + }; + private final String cmd; + + public abstract String handler(String request); + + private Command(String cmd) { + this.cmd = cmd; + } + + public String getCmd() { + return cmd; + } + + public static Command getCommand(String request) { + if (StringUtils.isNoneEmpty(request)) { + for (Command command : Command.values()) { + if (command.getCmd().equals(request)) { + return command; + } + } + } + return HELP; + } + } + +} diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java similarity index 76% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 9c623381..4b45c0bf 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -1,18 +1,18 @@ -package com.shinemo.mpush.core.handler; +package com.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.event.UserOnlineEvent; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.BindUserMessage; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.OkMessage; -import com.shinemo.mpush.core.router.RouterCenter; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; +import com.mpush.core.router.RouterCenter; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.UserOnlineEvent; +import com.mpush.api.protocol.Packet; +import com.mpush.common.EventBus; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.BindUserMessage; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java similarity index 75% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index fe420dc8..9467e57b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -1,16 +1,16 @@ -package com.shinemo.mpush.core.handler; +package com.mpush.core.handler; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.FastConnectMessage; -import com.shinemo.mpush.common.message.FastConnectOkMessage; -import com.shinemo.mpush.core.session.ReusableSession; -import com.shinemo.mpush.core.session.ReusableSessionManager; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.MPushUtil; +import com.mpush.core.session.ReusableSession; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.FastConnectMessage; +import com.mpush.common.message.FastConnectOkMessage; +import com.mpush.core.session.ReusableSessionManager; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.MPushUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java similarity index 87% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 1d267570..705d46a3 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -1,24 +1,25 @@ -package com.shinemo.mpush.core.handler; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.OkMessage; -import com.shinemo.mpush.common.message.PushMessage; -import com.shinemo.mpush.common.message.gateway.GatewayPushMessage; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.core.router.LocalRouter; -import com.shinemo.mpush.core.router.RouterCenter; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.MPushUtil; +package com.mpush.core.handler; + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.common.message.PushMessage; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.common.router.RemoteRouter; +import com.mpush.core.router.LocalRouter; +import com.mpush.core.router.RouterCenter; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; - -import static com.shinemo.mpush.common.ErrorCode.*; +import static com.mpush.common.ErrorCode.OFFLINE; +import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.mpush.common.ErrorCode.ROUTER_CHANGE; /** * Created by ohun on 2015/12/30. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java similarity index 78% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 7603aedc..72737f32 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -1,22 +1,22 @@ -package com.shinemo.mpush.core.handler; +package com.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.event.HandshakeEvent; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.HandshakeMessage; -import com.shinemo.mpush.common.message.HandshakeOkMessage; -import com.shinemo.mpush.common.security.AesCipher; -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.core.session.ReusableSession; -import com.shinemo.mpush.core.session.ReusableSessionManager; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.MPushUtil; +import com.mpush.core.session.ReusableSession; +import com.mpush.core.session.ReusableSessionManager; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.HandshakeEvent; +import com.mpush.api.protocol.Packet; +import com.mpush.common.EventBus; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.HandshakeMessage; +import com.mpush.common.message.HandshakeOkMessage; +import com.mpush.common.security.AesCipher; +import com.mpush.common.security.CipherBox; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.MPushUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java similarity index 60% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 30814281..99331f8b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.core.handler; +package com.mpush.core.handler; -import com.shinemo.mpush.api.MessageHandler; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; +import com.mpush.api.MessageHandler; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; /** * Created by ohun on 2015/12/22. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java similarity index 90% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index d5ca8226..338eb527 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -1,19 +1,19 @@ -package com.shinemo.mpush.core.handler; +package com.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.HttpRequestMessage; -import com.shinemo.mpush.common.message.HttpResponseMessage; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.netty.client.HttpCallback; -import com.shinemo.mpush.netty.client.HttpClient; -import com.shinemo.mpush.netty.client.RequestInfo; -import com.shinemo.mpush.tools.Profiler; -import com.shinemo.mpush.tools.dns.DnsMapping; -import com.shinemo.mpush.tools.dns.manage.DnsMappingManage; +import com.mpush.netty.client.HttpCallback; +import com.mpush.netty.client.RequestInfo; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.HttpRequestMessage; +import com.mpush.common.message.HttpResponseMessage; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.netty.client.HttpClient; +import com.mpush.tools.Profiler; +import com.mpush.tools.dns.DnsMapping; +import com.mpush.tools.dns.manage.DnsMappingManage; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; import org.slf4j.Logger; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java similarity index 77% rename from mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java rename to mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index e9ef79d9..0d427a29 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -1,23 +1,23 @@ -package com.shinemo.mpush.core.handler; +package com.mpush.core.handler; import com.google.common.base.Strings; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.event.UserOfflineEvent; -import com.shinemo.mpush.api.event.UserOnlineEvent; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.handler.BaseMessageHandler; -import com.shinemo.mpush.common.message.BindUserMessage; -import com.shinemo.mpush.common.message.ErrorMessage; -import com.shinemo.mpush.common.message.OkMessage; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.common.router.RemoteRouterManager; -import com.shinemo.mpush.core.router.LocalRouter; -import com.shinemo.mpush.core.router.LocalRouterManager; -import com.shinemo.mpush.core.router.RouterCenter; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; +import com.mpush.core.router.LocalRouterManager; +import com.mpush.core.router.RouterCenter; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.UserOfflineEvent; +import com.mpush.api.event.UserOnlineEvent; +import com.mpush.api.protocol.Packet; +import com.mpush.common.EventBus; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.BindUserMessage; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.common.router.RemoteRouter; +import com.mpush.common.router.RemoteRouterManager; +import com.mpush.core.router.LocalRouter; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java similarity index 89% rename from mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java rename to mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index f666712e..e876d58a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.core.router; +package com.mpush.core.router; /** * Created by ohun on 2016/1/4. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java similarity index 79% rename from mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java rename to mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index 60affbc8..dbb4a2d9 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.core.router; +package com.mpush.core.router; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.router.Router; +import com.mpush.api.connection.Connection; +import com.mpush.api.router.Router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java similarity index 87% rename from mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java rename to mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 4885ad09..598c69b6 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.core.router; +package com.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.event.ConnectionCloseEvent; -import com.shinemo.mpush.api.event.UserOfflineEvent; -import com.shinemo.mpush.api.router.RouterManager; -import com.shinemo.mpush.common.AbstractEventContainer; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.router.RemoteRouter; +import com.mpush.api.connection.Connection; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.event.UserOfflineEvent; +import com.mpush.api.router.RouterManager; +import com.mpush.common.AbstractEventContainer; +import com.mpush.common.EventBus; +import com.mpush.common.router.RemoteRouter; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java similarity index 85% rename from mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java rename to mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index d2c359a4..5ca9d808 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.core.router; - -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.event.RouterChangeEvent; -import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.common.router.RemoteRouterManager; -import com.shinemo.mpush.tools.MPushUtil; +package com.mpush.core.router; + +import com.mpush.api.connection.Connection; +import com.mpush.api.event.RouterChangeEvent; +import com.mpush.api.router.Router; +import com.mpush.api.router.ClientLocation; +import com.mpush.common.EventBus; +import com.mpush.common.router.RemoteRouter; +import com.mpush.common.router.RemoteRouterManager; +import com.mpush.tools.MPushUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java similarity index 85% rename from mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java rename to mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index 9d5cfe2c..81933204 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -1,22 +1,22 @@ -package com.shinemo.mpush.core.router; +package com.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.event.RouterChangeEvent; -import com.shinemo.mpush.api.router.ClientLocation; -import com.shinemo.mpush.api.router.Router; -import com.shinemo.mpush.common.AbstractEventContainer; -import com.shinemo.mpush.common.message.KickUserMessage; -import com.shinemo.mpush.common.router.RemoteRouter; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; -import com.shinemo.mpush.tools.redis.listener.MessageListener; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.api.RedisKey; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.RouterChangeEvent; +import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.Router; +import com.mpush.common.AbstractEventContainer; +import com.mpush.common.message.KickUserMessage; +import com.mpush.common.router.RemoteRouter; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.Jsons; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.redis.listener.ListenerDispatcher; +import com.mpush.tools.redis.listener.MessageListener; +import com.mpush.tools.redis.manage.RedisManage; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java similarity index 75% rename from mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java rename to mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 68047515..44a325f8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.core.router; +package com.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.event.UserOfflineEvent; -import com.shinemo.mpush.api.event.UserOnlineEvent; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.common.manage.user.UserManager; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.api.event.UserOfflineEvent; +import com.mpush.api.event.UserOnlineEvent; +import com.mpush.common.EventBus; +import com.mpush.common.manage.user.UserManager; +import com.mpush.tools.redis.manage.RedisManage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java similarity index 94% rename from mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java rename to mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index dc6730b8..565b055a 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -1,5 +1,7 @@ -package com.shinemo.mpush.core.server; +package com.mpush.core.server; +import com.mpush.core.handler.AdminHandler; +import com.mpush.netty.server.NettyServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -18,9 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.core.handler.AdminHandler; -import com.shinemo.mpush.netty.server.NettyServer; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; public final class AdminServer extends NettyServer { diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java similarity index 82% rename from mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java rename to mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index a67efdd8..8e692541 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -1,15 +1,15 @@ -package com.shinemo.mpush.core.server; +package com.mpush.core.server; -import com.shinemo.mpush.api.connection.ConnectionManager; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.core.handler.*; -import com.shinemo.mpush.netty.client.HttpClient; -import com.shinemo.mpush.netty.client.NettyHttpClient; -import com.shinemo.mpush.netty.connection.NettyConnectionManager; -import com.shinemo.mpush.netty.server.NettyServer; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.core.handler.*; +import com.mpush.netty.client.HttpClient; +import com.mpush.netty.client.NettyHttpClient; +import com.mpush.netty.connection.NettyConnectionManager; +import com.mpush.netty.server.NettyServer; +import com.mpush.api.connection.ConnectionManager; +import com.mpush.api.protocol.Command; +import com.mpush.common.MessageDispatcher; +import com.mpush.tools.config.ConfigCenter; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java similarity index 74% rename from mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java rename to mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 8f82446a..7fe3ebf8 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.core.server; +package com.mpush.core.server; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.common.MessageDispatcher; -import com.shinemo.mpush.core.handler.GatewayPushHandler; -import com.shinemo.mpush.netty.connection.NettyConnectionManager; -import com.shinemo.mpush.netty.server.NettyServer; +import com.mpush.core.handler.GatewayPushHandler; +import com.mpush.netty.connection.NettyConnectionManager; +import com.mpush.netty.server.NettyServer; +import com.mpush.api.protocol.Command; +import com.mpush.common.MessageDispatcher; import io.netty.channel.ChannelHandler; /** diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java similarity index 82% rename from mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java rename to mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 4315e5ae..ad84b60b 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -1,16 +1,16 @@ -package com.shinemo.mpush.core.server; +package com.mpush.core.server; -import com.shinemo.mpush.api.connection.ConnectionManager; -import com.shinemo.mpush.api.event.ConnectionCloseEvent; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.PacketReceiver; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.netty.connection.NettyConnection; -import com.shinemo.mpush.tools.Profiler; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.api.connection.ConnectionManager; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.protocol.Packet; +import com.mpush.api.connection.Connection; +import com.mpush.api.PacketReceiver; +import com.mpush.common.EventBus; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.Profiler; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java similarity index 89% rename from mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java rename to mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java index 989ebf50..98bdecfb 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.core.session; +package com.mpush.core.session; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.common.security.AesCipher; +import com.mpush.api.connection.SessionContext; +import com.mpush.common.security.AesCipher; /** * Created by ohun on 2015/12/25. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java similarity index 77% rename from mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java rename to mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index b7d77398..3d87b0c0 100644 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.core.session; +package com.mpush.core.session; -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.tools.Strings; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.crypto.MD5Utils; -import com.shinemo.mpush.tools.redis.manage.RedisManage; +import com.mpush.api.RedisKey; +import com.mpush.api.connection.SessionContext; +import com.mpush.tools.Strings; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.crypto.MD5Utils; +import com.mpush.tools.redis.manage.RedisManage; /** * Created by ohun on 2015/12/25. diff --git a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java deleted file mode 100644 index f33004c9..00000000 --- a/mpush-core/src/main/java/com/shinemo/mpush/core/handler/AdminHandler.java +++ /dev/null @@ -1,139 +0,0 @@ -package com.shinemo.mpush.core.handler; - -import java.util.Date; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.SimpleChannelInboundHandler; - -@ChannelHandler.Sharable -public final class AdminHandler extends SimpleChannelInboundHandler { - - private static final Logger log = LoggerFactory.getLogger(AdminHandler.class); - - private static final String DOUBLE_END = "\r\n\r\n"; - - private static final String ONE_END = "\r\n"; - - protected static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - @Override - protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { - Command command = Command.getCommand(request); - ChannelFuture future = ctx.write(command.handler(request)+DOUBLE_END); - if(command.equals(Command.QUIT)){ - future.addListener(ChannelFutureListener.CLOSE); - } - - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - ctx.write("welcome to "+MPushUtil.getInetAddress()+ "!"+ONE_END); - ctx.write("It is " + new Date() + " now."+DOUBLE_END); - ctx.flush(); - } - - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - ctx.flush(); - } - - public static enum Command{ - HELP("help"){ - @Override - public String handler(String request) { - StringBuilder buf = new StringBuilder(); - buf.append("Command:"+ONE_END); - buf.append("help:display all command."+ONE_END); - buf.append("quit:exit telnet."+ONE_END); - buf.append("scn:statistics conn num."+ONE_END); - buf.append("rcs:remove connection server zk info."+ONE_END); - buf.append("scs:stop connection server."); - return buf.toString(); - } - }, - QUIT("quit"){ - @Override - public String handler(String request) { - return "have a good day!"; - } - }, - SCN("scn"){ - @Override - public String handler(String request) { - Long value = RedisManage.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); - if(value == null){ - value = 0L; - } - return value.toString()+"."; - } - }, - RCS("rcs"){ - @Override - public String handler(String request) { - - List rawData = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); - boolean removeSuccess = false; - for (String raw : rawData) { - String data = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(raw)); - ConnectionServerApplication application = Jsons.fromJson(data, ConnectionServerApplication.class); - if(application.getIp().equals(MPushUtil.getInetAddress())){ - zkRegister.remove(ZKPath.CONNECTION_SERVER.getFullPath(raw)); - log.info("delete connection server success:{}",data); - removeSuccess = true; - }else{ - log.info("delete connection server failed: required ip:{}, but:{}",application.getIp(),MPushUtil.getInetAddress()); - } - } - if(removeSuccess){ - return "remove success."; - }else{ - return "remove false."; - } - } - }, - SCS("scs"){ - @Override - public String handler(String request) { - return "not support now."; - } - }; - private final String cmd; - public abstract String handler(String request); - private Command(String cmd) { - this.cmd = cmd; - } - public String getCmd() { - return cmd; - } - - public static Command getCommand(String request){ - if(StringUtils.isNoneEmpty(request)){ - for(Command command: Command.values()){ - if(command.getCmd().equals(request)){ - return command; - } - } - } - return HELP; - } - } - -} diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java similarity index 72% rename from mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java rename to mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java index 65170c4b..93640774 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java @@ -1,8 +1,7 @@ -package com.shinemo.mpush.core.netty; +package com.mpush.core.netty; -import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.core.server.ConnectionServer; -import com.shinemo.mpush.netty.server.NettyServer; +import com.mpush.api.Server; +import com.mpush.core.server.ConnectionServer; import org.junit.Test; /** diff --git a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java similarity index 89% rename from mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java rename to mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java index f518f4ae..7810ae22 100644 --- a/mpush-core/src/test/java/com/shinemo/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.core.security; +package com.mpush.core.security; -import com.shinemo.mpush.common.security.CipherBox; +import com.mpush.common.security.CipherBox; import org.junit.Test; import java.util.Arrays; diff --git a/mpush-cs/pom.xml b/mpush-cs/pom.xml deleted file mode 100644 index d8de1009..00000000 --- a/mpush-cs/pom.xml +++ /dev/null @@ -1,104 +0,0 @@ - - - mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml - - 4.0.0 - mpush-cs - mpush-cs - jar - - - - com.shinemo.mpush - mpush-core - - - com.shinemo.mpush - mpush-monitor - - - org.codehaus.janino - janino - - - - - mpush-cs - - - ../conf-${deploy.env}.properties - - - - - src/main/resources - - **/* - - true - - - - - - - maven-dependency-plugin - - - copy - package - - copy-dependencies - - - - ${project.build.directory}/lib - - - - - - - maven-jar-plugin - - - - - false - - - - - true - - lib/ - - com.shinemo.mpush.cs.Main - - - - - - package - - - - - - - - - - diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java deleted file mode 100644 index bf2a37e7..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/ConnectionServerMain.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.shinemo.mpush.cs; - - -import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.common.AbstractServer; -import com.shinemo.mpush.common.manage.user.UserManager; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.conn.client.GatewayServerApplication; -import com.shinemo.mpush.core.server.AdminServer; -import com.shinemo.mpush.core.server.ConnectionServer; -import com.shinemo.mpush.core.server.GatewayServer; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.dns.manage.DnsMappingManage; - -public class ConnectionServerMain extends AbstractServer { - - private Server gatewayServer; - - private Server adminServer; - - private ConnectionServerApplication connectionServerApplication; - - private GatewayServerApplication gatewayServerApplication; - - public ConnectionServerMain() { - -// registerListener(new ConnectionServerPathListener()); -// registerListener(new GatewayServerPathListener()); - - connectionServerApplication = (ConnectionServerApplication) application; - gatewayServerApplication = new GatewayServerApplication(); - connectionServerApplication.setGatewayServerApplication(gatewayServerApplication); - gatewayServer = new GatewayServer(gatewayServerApplication.getPort()); - adminServer = new AdminServer(ConfigCenter.holder.adminPort()); - } - - @Override - public void start() { - super.start(); - startServer(gatewayServer, gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); -// registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication)); - startServer(adminServer); - - UserManager.INSTANCE.clearUserOnlineData(); - - if (ConfigCenter.holder.httpProxyEnable()) { - DnsMappingManage.holder.init(); - } - } - - - @Override - public Server getServer() { - int port = application.getPort(); - return new ConnectionServer(port); - } - - @Override - public void stop() { - super.stop(); - stopServer(gatewayServer); - stopServer(adminServer); - } -} diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java deleted file mode 100644 index 8ac6268e..00000000 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.shinemo.mpush.cs; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.monitor.service.MonitorDataCollector; -import com.shinemo.mpush.tools.config.ConfigCenter; - -public class Main { - - private static final Logger log = LoggerFactory.getLogger(Main.class); - - public static void main(String[] args) { - final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); - connectionServerMain.start(); - - //开启监控 - MonitorDataCollector.start(ConfigCenter.holder.skipDump()); - - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - connectionServerMain.stop(); - log.warn("connection stop success!"); - } - }); - } - -} diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java deleted file mode 100644 index b08722be..00000000 --- a/mpush-cs/src/test/java/com/shinemo/mpush/ConnectionServerApplicationTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.shinemo.mpush; - -import org.junit.Test; - -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.tools.Jsons; - -public class ConnectionServerApplicationTest { - - @Test - public void testJson() throws Exception{ - - ConnectionServerApplication application = new ConnectionServerApplication(); - - String str = Jsons.toJson(application); - - System.out.println(str); - - } - -} diff --git a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java b/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java deleted file mode 100644 index 70a8923d..00000000 --- a/mpush-cs/src/test/java/com/shinemo/mpush/zk/ZkTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.shinemo.mpush.zk; - -import org.junit.Test; - -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager; - -public class ZkTest { - - @Test - public void remove(){ - ZkRegister zkRegister = new ZkRegisterManager(); - - zkRegister.init(); - - zkRegister.remove("/"); - - } - -} diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml index 6328df79..f8f5595e 100644 --- a/mpush-log/pom.xml +++ b/mpush-log/pom.xml @@ -1,14 +1,14 @@ - - mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml - + + mpush + com.mpush + 1.0 + 4.0.0 - + ${mpush.groupId} mpush-log + ${mpush-log-version} jar mpush-log @@ -17,25 +17,25 @@
- - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring - + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + commons-logging + commons-logging + + + log4j + log4j + + + org.logback-extensions + logback-ext-spring +
diff --git a/mpush-log/src/main/java/com/shinemo/mpush/App.java b/mpush-log/src/main/java/com/mpush/App.java similarity index 85% rename from mpush-log/src/main/java/com/shinemo/mpush/App.java rename to mpush-log/src/main/java/com/mpush/App.java index 9cc9cf7b..0a7102ea 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/App.java +++ b/mpush-log/src/main/java/com/mpush/App.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush; +package com.mpush; /** * Hello world! diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java b/mpush-log/src/main/java/com/mpush/log/LogLevel.java similarity index 61% rename from mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java rename to mpush-log/src/main/java/com/mpush/log/LogLevel.java index f93ad51e..03e64ae1 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LogLevel.java +++ b/mpush-log/src/main/java/com/mpush/log/LogLevel.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.log; +package com.mpush.log; public enum LogLevel { DEBUG,WARN,INFO,ERROR diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java b/mpush-log/src/main/java/com/mpush/log/LogType.java similarity index 70% rename from mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java rename to mpush-log/src/main/java/com/mpush/log/LogType.java index f77fa348..cb01804d 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LogType.java +++ b/mpush-log/src/main/java/com/mpush/log/LogType.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.log; +package com.mpush.log; public enum LogType { CONNECTION,PUSH,HEARTBEAT,REDIS,ZK,DEFAULT,HTTP diff --git a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/mpush/log/LoggerManage.java similarity index 99% rename from mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java rename to mpush-log/src/main/java/com/mpush/log/LoggerManage.java index 82775f03..0c017360 100644 --- a/mpush-log/src/main/java/com/shinemo/mpush/log/LoggerManage.java +++ b/mpush-log/src/main/java/com/mpush/log/LoggerManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.log; +package com.mpush.log; import java.util.HashMap; import java.util.Map; diff --git a/mpush-log/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-log/src/test/java/com/mpush/AppTest.java similarity index 95% rename from mpush-log/src/test/java/com/shinemo/mpush/AppTest.java rename to mpush-log/src/test/java/com/mpush/AppTest.java index 50fff9a1..796b8466 100644 --- a/mpush-log/src/test/java/com/shinemo/mpush/AppTest.java +++ b/mpush-log/src/test/java/com/mpush/AppTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush; +package com.mpush; import junit.framework.Test; import junit.framework.TestCase; diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 2fa7ce7e..6da74f1c 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -2,34 +2,21 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml + com.mpush + 1.0 4.0.0 + ${mpush.groupId} mpush-monitor + ${mpush-monitor-version} jar mpush-monitor - - - com.google.code.gson - gson - - - com.google.guava - guava - - - com.shinemo.mpush + ${mpush.groupId} mpush-tools - - org.apache.commons - commons-lang3 -
diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/App.java b/mpush-monitor/src/main/java/com/mpush/App.java similarity index 85% rename from mpush-monitor/src/main/java/com/shinemo/mpush/App.java rename to mpush-monitor/src/main/java/com/mpush/App.java index 9cc9cf7b..0a7102ea 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/App.java +++ b/mpush-monitor/src/main/java/com/mpush/App.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush; +package com.mpush; /** * Hello world! diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java similarity index 96% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java rename to mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java index 51af56c9..9ae26d87 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/domain/MonitorData.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.domain; +package com.mpush.monitor.domain; import java.util.Map; diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/BaseQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java similarity index 97% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/BaseQuota.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java index 7565f3c7..2c7ad2e7 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/BaseQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota; +package com.mpush.monitor.quota; import java.util.List; import java.util.Map; diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/GCMQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java similarity index 89% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/GCMQuota.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java index 8fb9876f..5dca627d 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/GCMQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota; +package com.mpush.monitor.quota; public interface GCMQuota { diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java similarity index 66% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java index dea1fb61..23a4b810 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota; +package com.mpush.monitor.quota; public interface InfoQuota { diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/MemoryQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java similarity index 94% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/MemoryQuota.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java index bd526596..3a368af2 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/MemoryQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota; +package com.mpush.monitor.quota; public interface MemoryQuota { diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java new file mode 100644 index 00000000..2396a0e6 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java @@ -0,0 +1,5 @@ +package com.mpush.monitor.quota; + +public interface ThreadPoolQuota { + +} diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java similarity index 81% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadQuota.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java index 2f74b679..8d000cdf 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota; +package com.mpush.monitor.quota; public interface ThreadQuota { diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMGC.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java similarity index 95% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMGC.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java index 9f16fb91..05c1f177 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java @@ -1,12 +1,12 @@ -package com.shinemo.mpush.monitor.quota.impl; +package com.mpush.monitor.quota.impl; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.quota.BaseQuota; -import com.shinemo.mpush.monitor.quota.GCMQuota; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.GCMQuota; public class JVMGC extends BaseQuota implements GCMQuota { diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java similarity index 82% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index bc08a2f4..42e2bd04 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota.impl; +package com.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; @@ -6,10 +6,10 @@ import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.quota.BaseQuota; -import com.shinemo.mpush.monitor.quota.InfoQuota; +import com.mpush.monitor.quota.InfoQuota; +import com.mpush.monitor.quota.BaseQuota; -public class JVMInfo extends BaseQuota implements InfoQuota{ +public class JVMInfo extends BaseQuota implements InfoQuota { public static final JVMInfo instance = new JVMInfo(); diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java similarity index 97% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMMemory.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java index 0675c0d3..2ccb5f19 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota.impl; +package com.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; @@ -7,8 +7,8 @@ import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.quota.BaseQuota; -import com.shinemo.mpush.monitor.quota.MemoryQuota; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.MemoryQuota; public class JVMMemory extends BaseQuota implements MemoryQuota { diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java similarity index 90% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThread.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index e9285da3..a2c7ef03 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -1,12 +1,12 @@ -package com.shinemo.mpush.monitor.quota.impl; +package com.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Map; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.quota.BaseQuota; -import com.shinemo.mpush.monitor.quota.ThreadQuota; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.ThreadQuota; public class JVMThread extends BaseQuota implements ThreadQuota{ diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java similarity index 83% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index 26aa7c0c..c9c77e7f 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.monitor.quota.impl; +package com.mpush.monitor.quota.impl; import java.util.Iterator; import java.util.Map; @@ -6,9 +6,9 @@ import java.util.concurrent.ThreadPoolExecutor; import com.google.common.collect.Maps; -import com.shinemo.mpush.monitor.quota.BaseQuota; -import com.shinemo.mpush.monitor.quota.ThreadPoolQuota; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.ThreadPoolQuota; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota{ diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java similarity index 87% rename from mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java rename to mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java index 0ae94459..f299ed5c 100644 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/service/MonitorDataCollector.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java @@ -1,16 +1,16 @@ -package com.shinemo.mpush.monitor.service; +package com.mpush.monitor.service; +import com.mpush.tools.JVMUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.monitor.domain.MonitorData; -import com.shinemo.mpush.monitor.quota.impl.JVMGC; -import com.shinemo.mpush.monitor.quota.impl.JVMInfo; -import com.shinemo.mpush.monitor.quota.impl.JVMMemory; -import com.shinemo.mpush.monitor.quota.impl.JVMThread; -import com.shinemo.mpush.monitor.quota.impl.JVMThreadPool; -import com.shinemo.mpush.tools.JVMUtil; -import com.shinemo.mpush.tools.Jsons; +import com.mpush.monitor.domain.MonitorData; +import com.mpush.monitor.quota.impl.JVMGC; +import com.mpush.monitor.quota.impl.JVMInfo; +import com.mpush.monitor.quota.impl.JVMMemory; +import com.mpush.monitor.quota.impl.JVMThread; +import com.mpush.monitor.quota.impl.JVMThreadPool; +import com.mpush.tools.Jsons; public class MonitorDataCollector { diff --git a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java deleted file mode 100644 index 0b64be9d..00000000 --- a/mpush-monitor/src/main/java/com/shinemo/mpush/monitor/quota/ThreadPoolQuota.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.shinemo.mpush.monitor.quota; - -public interface ThreadPoolQuota { - -} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-monitor/src/test/java/com/mpush/AppTest.java similarity index 95% rename from mpush-test/src/test/java/com/shinemo/mpush/AppTest.java rename to mpush-monitor/src/test/java/com/mpush/AppTest.java index 50fff9a1..796b8466 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/AppTest.java +++ b/mpush-monitor/src/test/java/com/mpush/AppTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush; +package com.mpush; import junit.framework.Test; import junit.framework.TestCase; diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 05f1ffdd..b0bcbe3f 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -2,21 +2,22 @@ - - mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml - + + mpush + com.mpush + 1.0 + 4.0.0 + ${mpush.groupId} mpush-netty + ${mpush-netty-version} jar mpush-netty - com.shinemo.mpush + ${mpush.groupId} mpush-common diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java similarity index 64% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java rename to mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java index 54ef263a..5aa506c2 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/ChannelClientHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; -import com.shinemo.mpush.api.Client; +import com.mpush.api.Client; import io.netty.channel.ChannelHandler; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java similarity index 89% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java rename to mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java index 379cf2f5..eb4d0532 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpCallback.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; import io.netty.handler.codec.http.HttpResponse; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java similarity index 81% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java rename to mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java index ead3ee91..d9aa0707 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/HttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; /** * Created by ohun on 2016/2/15. diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java similarity index 92% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java rename to mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index cd9441af..ed567ded 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -1,7 +1,8 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; import java.util.concurrent.TimeUnit; +import com.mpush.netty.util.NettySharedHolder; import io.netty.channel.*; import io.netty.util.Timeout; import io.netty.util.TimerTask; @@ -10,11 +11,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.netty.util.NettySharedHolder; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.api.Client; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.config.ConfigCenter; public class NettyClient implements Client { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java similarity index 91% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java rename to mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java index 74809a3c..d3cce35e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java @@ -1,8 +1,9 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; import java.net.InetSocketAddress; import java.util.Map; +import com.mpush.netty.codec.PacketDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,10 +14,9 @@ import io.netty.channel.socket.nio.NioSocketChannel; import com.google.common.collect.Maps; -import com.shinemo.mpush.api.Client; -import com.shinemo.mpush.netty.codec.PacketDecoder; -import com.shinemo.mpush.netty.codec.PacketEncoder; -import com.shinemo.mpush.netty.util.NettySharedHolder; +import com.mpush.api.Client; +import com.mpush.netty.codec.PacketEncoder; +import com.mpush.netty.util.NettySharedHolder; public class NettyClientFactory { diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java similarity index 97% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java rename to mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java index c07f967d..772f419f 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; import com.google.common.collect.ArrayListMultimap; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.thread.NamedThreadFactory; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.config.ConfigCenter; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java similarity index 95% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java rename to mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java index 7e98c553..e7efb94e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; import com.google.common.primitives.Ints; -import com.shinemo.mpush.api.Constants; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.api.Constants; +import com.mpush.tools.config.ConfigCenter; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Timeout; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java similarity index 97% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java rename to mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java index c5b7f2dc..6e55531b 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/SecurityNettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.netty.client; +package com.mpush.netty.client; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java similarity index 91% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java rename to mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 71782340..a4ba8cae 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.netty.codec; +package com.mpush.netty.codec; -import com.shinemo.mpush.api.exception.DecodeException; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.api.exception.DecodeException; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.tools.config.ConfigCenter; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java similarity index 88% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java rename to mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index 42232eb2..641120de 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.netty.codec; +package com.mpush.netty.codec; -import com.shinemo.mpush.api.protocol.Command; -import com.shinemo.mpush.api.protocol.Packet; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java similarity index 93% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java rename to mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 9026d679..ebc0fb9e 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.netty.connection; +package com.mpush.netty.connection; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.SessionContext; -import com.shinemo.mpush.api.protocol.Packet; -import com.shinemo.mpush.common.security.CipherBox; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.protocol.Packet; +import com.mpush.common.security.CipherBox; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java similarity index 90% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java rename to mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java index 13a5e224..2982fb82 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java @@ -1,15 +1,15 @@ -package com.shinemo.mpush.netty.connection; +package com.mpush.netty.connection; import com.google.common.collect.Lists; import com.google.common.eventbus.Subscribe; -import com.shinemo.mpush.api.connection.Connection; -import com.shinemo.mpush.api.connection.ConnectionManager; -import com.shinemo.mpush.api.event.HandshakeEvent; -import com.shinemo.mpush.common.EventBus; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.ConnectionManager; +import com.mpush.api.event.HandshakeEvent; +import com.mpush.common.EventBus; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.config.ConfigCenter; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java similarity index 96% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java rename to mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 7a76da40..f0412028 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.netty.server; +package com.mpush.netty.server; -import com.shinemo.mpush.api.Server; -import com.shinemo.mpush.netty.codec.PacketDecoder; -import com.shinemo.mpush.netty.codec.PacketEncoder; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.api.Server; +import com.mpush.netty.codec.PacketDecoder; +import com.mpush.netty.codec.PacketEncoder; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java similarity index 72% rename from mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java rename to mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java index 489b2970..34874d67 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/util/NettySharedHolder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.netty.util; +package com.mpush.netty.util; -import com.shinemo.mpush.tools.thread.NamedThreadFactory; -import com.shinemo.mpush.tools.thread.ThreadNameSpace; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.ThreadNameSpace; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index d143cc6f..2c830035 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -1,53 +1,52 @@ - - mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml - + + mpush + com.mpush + 1.0 + 4.0.0 + ${mpush.groupId} mpush-test + ${mpush-test-version} jar mpush-test - - com.shinemo.mpush + ${mpush.groupId} mpush-api - com.shinemo.mpush + ${mpush.groupId} mpush-netty - com.shinemo.mpush + ${mpush.groupId} mpush-tools - com.shinemo.mpush + ${mpush.groupId} mpush-core - com.shinemo.mpush - mpush-cs + ${mpush.groupId} + mpush-boot - com.shinemo.mpush + ${mpush.groupId} mpush-log - com.shinemo.mpush + ${mpush.groupId} mpush-client - args4j - args4j + args4j + args4j - diff --git a/mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java b/mpush-test/src/test/java/com/mpush/AppTest.java similarity index 95% rename from mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java rename to mpush-test/src/test/java/com/mpush/AppTest.java index 50fff9a1..796b8466 100644 --- a/mpush-monitor/src/test/java/com/shinemo/mpush/AppTest.java +++ b/mpush-test/src/test/java/com/mpush/AppTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush; +package com.mpush; import junit.framework.Test; import junit.framework.TestCase; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java similarity index 67% rename from mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java rename to mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 3ee4a336..8ef2504d 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.test.configcenter; +package com.mpush.test.configcenter; import java.util.List; import java.util.Map; import org.junit.Test; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.dns.DnsMapping; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.dns.DnsMapping; public class ConfigCenterTest { diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java similarity index 97% rename from mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java rename to mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java index d0cf2844..2a0ed803 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java @@ -1,15 +1,14 @@ -package com.shinemo.mpush.test.connection.body; +package com.mpush.test.connection.body; import java.security.interfaces.RSAPrivateKey; +import com.mpush.common.message.HandshakeMessage; +import com.mpush.tools.crypto.RSAUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.common.message.HandshakeMessage; -import com.shinemo.mpush.tools.crypto.RSAUtils; - public class BodyTest { private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; diff --git a/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java new file mode 100644 index 00000000..b99faeb3 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java @@ -0,0 +1,25 @@ +package com.mpush.test.connection.client; + +import com.google.common.collect.Lists; +import com.mpush.common.AbstractClient; +import com.mpush.zk.ZKNodeManager; +import com.mpush.zk.ZKServerNode; +import com.mpush.push.zk.listener.ConnectZKListener; +import com.mpush.tools.spi.ServiceContainer; + +import java.util.List; + +public class ConnectTestClient extends AbstractClient { + + @SuppressWarnings("unchecked") + private ZKNodeManager connectionServerManage = ServiceContainer.load(ZKNodeManager.class); + + public ConnectTestClient() { + registerListener(new ConnectZKListener()); + } + + public List getServers() { + return Lists.newArrayList(connectionServerManage.getList()); + } + +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java similarity index 67% rename from mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java rename to mpush-test/src/test/java/com/mpush/test/connection/client/Main.java index b8c7070c..e0a0aea7 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java @@ -1,24 +1,24 @@ -package com.shinemo.mpush.test.connection.client; +package com.mpush.test.connection.client; + +import com.mpush.conn.client.ClientChannelHandler; +import com.mpush.netty.client.NettyClientFactory; +import com.mpush.zk.ZKServerNode; +import com.mpush.common.security.CipherBox; +import com.mpush.netty.client.SecurityNettyClient; import java.util.List; import java.util.concurrent.locks.LockSupport; -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.conn.client.ClientChannelHandler; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.netty.client.SecurityNettyClient; - public class Main { public static void main(String[] args) throws Exception { - ConnectionClientMain main = new ConnectionClientMain(); + ConnectTestClient main = new ConnectTestClient(); main.start(); - List serverList = main.getApplicationList(); + List serverList = main.getServers(); int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ConnectionServerApplication server = serverList.get(index); + ZKServerNode server = serverList.get(index); for(int i = 0;i<1;i++){ String clientVersion = "1.0." + i; diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java new file mode 100644 index 00000000..625541a3 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java @@ -0,0 +1,17 @@ +package com.mpush.test.connection.mpns; + +import com.google.common.collect.Lists; +import com.mpush.common.AbstractClient; +import com.mpush.zk.ZKServerNode; + +import java.util.List; + +public class ConnectTestClient extends AbstractClient { + + private final List applicationLists = Lists.newArrayList( + new ZKServerNode("111.1.57.148", 20882, "111.1.57.148", "")); + + public List getServers() { + return Lists.newArrayList(applicationLists); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java new file mode 100644 index 00000000..319fa02b --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java @@ -0,0 +1,52 @@ +package com.mpush.test.connection.mpns; + +import com.mpush.common.security.CipherBox; +import com.mpush.conn.client.ClientChannelHandler; +import com.mpush.netty.client.NettyClientFactory; +import com.mpush.netty.client.SecurityNettyClient; +import com.mpush.zk.ZKServerNode; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +public class Main { + + public static void main(String[] args) throws InterruptedException { + + ConnectTestClient main = new ConnectTestClient(); + main.start(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + + for (int i = 0; i < 10000; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "uh-" + i; + String deviceId = "test-device-id-" + i; + String cipher = ""; + byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); + byte[] iv = CipherBox.INSTANCE.randomAESIV(); + + SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); + client.setClientKey(clientKey); + client.setIv(iv); + client.setClientVersion(clientVersion); + client.setDeviceId(deviceId); + client.setOsName(osName); + client.setOsVersion(osVersion); + client.setUserId(userId); + client.setCipher(cipher); + + ClientChannelHandler handler = new ClientChannelHandler(client); + NettyClientFactory.INSTANCE.create(handler); + Thread.sleep(10); + } + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java similarity index 96% rename from mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java rename to mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java index 9272452b..5ff50659 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/crypto/RsaTest.java +++ b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.crypto; +package com.mpush.test.crypto; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; @@ -6,13 +6,12 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import com.mpush.common.message.HandshakeMessage; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.crypto.RSAUtils; import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.common.message.HandshakeMessage; -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.tools.crypto.RSAUtils; - public class RsaTest { private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java similarity index 96% rename from mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java rename to mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java index 1f60025b..4e67ed76 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/DnsMappingTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.gson; +package com.mpush.test.gson; import java.util.Map; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java similarity index 79% rename from mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java rename to mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 5280f4ee..7406913d 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.test.gson; +package com.mpush.test.gson; import java.util.Map; +import com.mpush.tools.Jsons; import org.junit.Test; import com.google.common.collect.Maps; -import com.shinemo.mpush.api.PushContent; -import com.shinemo.mpush.api.PushContent.PushType; -import com.shinemo.mpush.tools.Jsons; +import com.mpush.api.PushContent; +import com.mpush.api.PushContent.PushType; public class GsonTest { @@ -17,7 +17,7 @@ public void test(){ map.put("key1", 1121+""); map.put("key2", "value2"); - PushContent content = PushContent.build(PushType.MESSAGE,Jsons.toJson(map)); + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); System.out.println(Jsons.toJson(content)); diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/Main.java similarity index 84% rename from mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java rename to mpush-test/src/test/java/com/mpush/test/push/Main.java index 10db6d38..dd5a5acb 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/push/Main.java @@ -1,10 +1,10 @@ -package com.shinemo.mpush.test.push; +package com.mpush.test.push; -import com.shinemo.mpush.api.PushContent; -import com.shinemo.mpush.api.PushSender; -import com.shinemo.mpush.api.PushContent.PushType; -import com.shinemo.mpush.push.PushClient; -import com.shinemo.mpush.tools.Jsons; +import com.mpush.api.PushContent; +import com.mpush.api.PushSender; +import com.mpush.api.PushContent.PushType; +import com.mpush.push.PushClient; +import com.mpush.tools.Jsons; import java.util.Arrays; import java.util.concurrent.locks.LockSupport; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java similarity index 91% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java rename to mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java index b85f9499..fe122a9f 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/ConsistentHashTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.redis; +package com.mpush.test.redis; import java.util.ArrayList; import java.util.Collections; @@ -7,14 +7,13 @@ import java.util.Map; import java.util.UUID; +import com.mpush.tools.redis.consistenthash.ConsistentHash; +import com.mpush.tools.redis.consistenthash.Node; import org.junit.Test; import redis.clients.util.Hashing; import redis.clients.util.MurmurHash; -import com.shinemo.mpush.tools.redis.consistenthash.ConsistentHash; -import com.shinemo.mpush.tools.redis.consistenthash.Node; - public class ConsistentHashTest { private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java similarity index 66% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java rename to mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java index 09ff7c4d..c52c091c 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/MPushUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java @@ -1,9 +1,8 @@ -package com.shinemo.mpush.test.redis; +package com.mpush.test.redis; +import com.mpush.tools.MPushUtil; import org.junit.Test; -import com.shinemo.mpush.tools.MPushUtil; - public class MPushUtilTest { @Test diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java similarity index 73% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java rename to mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java index 9536130c..a2913fa5 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java @@ -1,22 +1,22 @@ -package com.shinemo.mpush.test.redis; +package com.mpush.test.redis; import java.util.List; import java.util.concurrent.locks.LockSupport; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.redis.RedisNode; +import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.tools.redis.pubsub.Subscriber; +import com.mpush.tools.redis.RedisRegister; import org.junit.Before; import org.junit.Test; import com.google.common.collect.Lists; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisNode; -import com.shinemo.mpush.tools.redis.RedisRegister; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -import com.shinemo.mpush.tools.redis.pubsub.Subscriber; -import com.shinemo.mpush.tools.spi.ServiceContainer; +import com.mpush.tools.spi.ServiceContainer; public class PubSubTest { - private RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); + private RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); @Before public void init(){ diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java similarity index 85% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisClusterTest.java rename to mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java index 95f95891..13052f07 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisClusterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java @@ -1,16 +1,16 @@ -package com.shinemo.mpush.test.redis; +package com.mpush.test.redis; import java.util.Date; import java.util.HashSet; import java.util.Set; +import com.mpush.tools.redis.RedisPoolConfig; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.RedisPoolConfig; +import com.mpush.tools.Jsons; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; @@ -29,7 +29,7 @@ public void init() { jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); - cluster = new JedisCluster(jedisClusterNodes,RedisPoolConfig.config); + cluster = new JedisCluster(jedisClusterNodes, RedisPoolConfig.config); } @Test diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java similarity index 90% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java rename to mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java index 77981b8b..2d376e59 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisGroupManageTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java @@ -1,20 +1,20 @@ -//package com.shinemo.mpush.tools.redis; +//package com.mpush.tools.redis; // //import java.util.Date; //import java.util.List; // -//import com.shinemo.mpush.tools.redis.listener.MessageListener; +//import MessageListener; // //import org.apache.commons.lang3.builder.ToStringBuilder; //import org.apache.commons.lang3.builder.ToStringStyle; //import org.junit.Before; //import org.junit.Test; // -//import com.shinemo.mpush.tools.MPushUtil; -//import com.shinemo.mpush.tools.redis.manage.RedisManage; -//import com.shinemo.mpush.tools.redis.pubsub.Subscriber; -//import com.shinemo.mpush.tools.spi.ServiceContainer; -//import com.shinemo.mpush.tools.zk.ServerApp; +//import MPushUtil; +//import RedisManage; +//import Subscriber; +//import ServiceContainer; +//import com.mpush.tools.zk.ServerApp; // //public class RedisGroupManageTest { // diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java similarity index 96% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java rename to mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index 6a22adcf..c8c4a45f 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.redis; +package com.mpush.test.redis; import java.util.Date; import java.util.Iterator; @@ -9,9 +9,8 @@ import org.junit.Test; import com.google.common.collect.Lists; -import com.shinemo.mpush.api.RedisKey; -import com.shinemo.mpush.tools.redis.RedisNode; -import com.shinemo.mpush.tools.redis.RedisUtil; +import com.mpush.tools.redis.RedisNode; +import com.mpush.tools.redis.RedisUtil; import redis.clients.jedis.Jedis; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/User.java b/mpush-test/src/test/java/com/mpush/test/redis/User.java similarity index 94% rename from mpush-test/src/test/java/com/shinemo/mpush/test/redis/User.java rename to mpush-test/src/test/java/com/mpush/test/redis/User.java index 2fddc275..180ebf1e 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/redis/User.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/User.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.test.redis; +package com.mpush.test.redis; import java.io.Serializable; import java.util.Date; diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java similarity index 86% rename from mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java rename to mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java index 3d82812e..5965f67d 100644 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/util/TelnetTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java @@ -1,12 +1,11 @@ -package com.shinemo.mpush.test.util; +package com.mpush.test.util; import java.net.URI; import java.net.URISyntaxException; +import com.mpush.tools.MPushUtil; import org.junit.Test; -import com.shinemo.mpush.tools.MPushUtil; - public class TelnetTest { @Test diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java deleted file mode 100644 index 145588ef..00000000 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/client/ConnectionClientMain.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.shinemo.mpush.test.connection.client; - -import java.util.List; - -import com.google.common.collect.Lists; -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.common.manage.ServerManage; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.push.zk.listener.impl.ConnectionServerPathListener; -import com.shinemo.mpush.tools.spi.ServiceContainer; - -public class ConnectionClientMain extends AbstractClient { - - @SuppressWarnings("unchecked") - private ServerManage connectionServerManage = ServiceContainer.getInstance(ServerManage.class); - - public ConnectionClientMain() { - registerListener(new ConnectionServerPathListener()); - } - - public List getApplicationList(){ - return Lists.newArrayList(connectionServerManage.getList()); - } - -} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java deleted file mode 100644 index ad72e897..00000000 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/ConnectionClientMain.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.shinemo.mpush.test.connection.mpns; - -import java.util.List; - -import com.google.common.collect.Lists; -import com.shinemo.mpush.common.AbstractClient; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; - -public class ConnectionClientMain extends AbstractClient { - - - private static final List applicationLists = Lists.newArrayList(new ConnectionServerApplication(20882,"","111.1.57.148","111.1.57.148")); - - public List getApplicationList(){ - return Lists.newArrayList(applicationLists); - } -} diff --git a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java deleted file mode 100644 index 30dfbfba..00000000 --- a/mpush-test/src/test/java/com/shinemo/mpush/test/connection/mpns/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.shinemo.mpush.test.connection.mpns; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -import com.shinemo.mpush.common.security.CipherBox; -import com.shinemo.mpush.conn.client.ClientChannelHandler; -import com.shinemo.mpush.conn.client.ConnectionServerApplication; -import com.shinemo.mpush.netty.client.NettyClientFactory; -import com.shinemo.mpush.netty.client.SecurityNettyClient; - -public class Main { - - public static void main(String[] args) throws InterruptedException { - - ConnectionClientMain main = new ConnectionClientMain(); - main.start(); - - List serverList = main.getApplicationList(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ConnectionServerApplication server = serverList.get(index); - - for(int i = 0;i<10000;i++){ - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "uh-"+i; - String deviceId = "test-device-id-"+i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(10); - } - - LockSupport.park(); - } - -} diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 22ddc3ef..218927c0 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -2,17 +2,19 @@ - - mpush - com.shinemo.mpush - 0.0.0.2 - ../pom.xml - + + mpush + com.mpush + 1.0 + 4.0.0 + ${mpush.groupId} mpush-tools + ${mpush-tools-version} jar mpush-tools + com.google.code.gson @@ -26,14 +28,7 @@ org.apache.commons commons-lang3 - - org.apache.curator - curator-recipes - - - org.apache.curator - curator-x-discovery - + redis.clients jedis @@ -43,7 +38,7 @@ slf4j-api - com.shinemo.mpush + ${mpush.groupId} mpush-log @@ -51,8 +46,8 @@ owner - commons-net - commons-net + commons-net + commons-net diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/mpush/tools/Constants.java similarity index 98% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java rename to mpush-tools/src/main/java/com/mpush/tools/Constants.java index 6a2c3126..1afef995 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Constants.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import java.nio.charset.Charset; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java similarity index 99% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java index e2913435..342c312a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/GenericsUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import java.lang.reflect.Field; import java.lang.reflect.Method; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java similarity index 98% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java rename to mpush-tools/src/main/java/com/mpush/tools/IOUtils.java index b742ec05..78c57ebe 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java similarity index 99% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java index f95e4801..1bffb0b0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import java.io.File; import java.io.FileOutputStream; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java similarity index 98% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java rename to mpush-tools/src/main/java/com/mpush/tools/Jsons.java index cc3659f4..5b7e726e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import com.google.gson.Gson; import com.google.gson.GsonBuilder; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java similarity index 96% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java index dbd71650..e30020fc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.ConfigCenter; import org.apache.commons.net.telnet.TelnetClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,7 +55,7 @@ public static int getHeartbeat(int min, int max) { */ public static String getInetAddress() { try { - com.shinemo.mpush.tools.Profiler.enter("start get inet addresss"); + Profiler.enter("start get inet addresss"); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); InetAddress address = null; while (interfaces.hasMoreElements()) { @@ -74,7 +74,7 @@ public static String getInetAddress() { LOGGER.error("getInetAddress exception", e); return "127.0.0.1"; } finally { - com.shinemo.mpush.tools.Profiler.release(); + Profiler.release(); } } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/Pair.java similarity index 92% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java rename to mpush-tools/src/main/java/com/mpush/tools/Pair.java index 7e229aec..a255ca57 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Pair.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Pair.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; /** * Created by ohun on 2015/12/24. diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/Profiler.java similarity index 99% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/Profiler.java rename to mpush-tools/src/main/java/com/mpush/tools/Profiler.java index 1658c3e6..d4a3d1fb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Profiler.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import java.text.MessageFormat; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/mpush/tools/Strings.java similarity index 95% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java rename to mpush-tools/src/main/java/com/mpush/tools/Strings.java index 7a40e206..5b52ec17 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Strings.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; /** * Created by ohun on 2015/12/23. diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java similarity index 97% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java rename to mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java index fd778509..e5369001 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.tools.config; +package com.mpush.tools.config; -import com.shinemo.mpush.tools.dns.DnsMapping; -import com.shinemo.mpush.tools.redis.RedisGroup; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.dns.DnsMapping; import org.aeonbits.owner.Config; import org.aeonbits.owner.Config.Sources; import org.aeonbits.owner.ConfigFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java similarity index 94% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java rename to mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java index 3d534343..7696bbbb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/DnsMappingConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java @@ -1,11 +1,11 @@ -package com.shinemo.mpush.tools.config; +package com.mpush.tools.config; import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.primitives.Ints; -import com.shinemo.mpush.tools.dns.DnsMapping; +import com.mpush.tools.dns.DnsMapping; import org.aeonbits.owner.Converter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java similarity index 95% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java rename to mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java index a4fae2f1..f16e05a2 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/MapConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.config; +package com.mpush.tools.config; import com.google.common.base.Splitter; import com.google.common.base.Strings; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java similarity index 86% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java rename to mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java index 9a0a2f2c..424c2580 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/config/RedisGroupConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java @@ -1,13 +1,13 @@ -package com.shinemo.mpush.tools.config; +package com.mpush.tools.config; import java.lang.reflect.Method; +import com.mpush.tools.redis.RedisGroup; import org.aeonbits.owner.Converter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisNode; +import com.mpush.tools.redis.RedisNode; public class RedisGroupConverter implements Converter{ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java similarity index 96% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java rename to mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index 3c519cbe..0279c503 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -1,9 +1,7 @@ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; import java.security.SecureRandom; -import com.shinemo.mpush.tools.Constants; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java similarity index 99% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java rename to mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java index f68d1b86..207fd55e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java @@ -23,7 +23,7 @@ * */ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; import java.io.FilterOutputStream; import java.io.InputStream; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java similarity index 97% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java rename to mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index b6fa6dc5..c7280a07 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; -import com.shinemo.mpush.tools.Constants; +import com.mpush.tools.Constants; import java.io.*; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java similarity index 94% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java rename to mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index b3b5dc97..225f3986 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.IOUtils; -import com.shinemo.mpush.tools.Strings; +import com.mpush.tools.Constants; +import com.mpush.tools.IOUtils; +import com.mpush.tools.Strings; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java similarity index 99% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java rename to mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index 3b1698cc..e3201180 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -1,7 +1,7 @@ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.Pair; +import com.mpush.tools.Constants; +import com.mpush.tools.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java similarity index 89% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java rename to mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java index 0b44457d..6f541b3a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/DnsMapping.java +++ b/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.dns; +package com.mpush.tools.dns; public class DnsMapping { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java similarity index 92% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java rename to mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java index a07482ae..783d2958 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/dns/manage/DnsMappingManage.java +++ b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.dns.manage; +package com.mpush.tools.dns.manage; import java.util.List; import java.util.Map; @@ -11,10 +11,10 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.dns.DnsMapping; +import com.mpush.tools.Jsons; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.dns.DnsMapping; public class DnsMappingManage { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java b/mpush-tools/src/main/java/com/mpush/tools/event/Event.java similarity index 90% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java rename to mpush-tools/src/main/java/com/mpush/tools/event/Event.java index 416222a8..afabfdee 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/Event.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/Event.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.event; +package com.mpush.tools.event; public class Event { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java similarity index 90% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java rename to mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java index ea528cba..fbcb19ee 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventDispatcher.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.event; +package com.mpush.tools.event; import java.util.ArrayList; import java.util.List; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java similarity index 65% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java rename to mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java index ada094cb..5a9b6d82 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventListener.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.event; +package com.mpush.tools.event; public interface EventListener { diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java new file mode 100644 index 00000000..ccd77b3f --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java @@ -0,0 +1,5 @@ +package com.mpush.tools.event; + +public enum EventType { + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java b/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java new file mode 100644 index 00000000..c5326f53 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java @@ -0,0 +1,24 @@ +package com.mpush.tools.exception; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKException extends RuntimeException { + + public ZKException() { + } + + public ZKException(String message) { + super(message); + } + + public ZKException(String message, Throwable cause) { + super(message, cause); + } + + public ZKException(Throwable cause) { + super(cause); + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java similarity index 95% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java index b165e27c..73b69ec3 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisGroup.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis; +package com.mpush.tools.redis; import java.util.List; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java similarity index 94% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java index a3bf80b9..82f3de80 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisNode.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis; +package com.mpush.tools.redis; /** * redis 相关的配置信息 diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java similarity index 96% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java index fc24eacc..976bd1c8 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisPoolConfig.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.tools.redis; +package com.mpush.tools.redis; -import com.shinemo.mpush.tools.Constants; +import com.mpush.tools.Constants; import redis.clients.jedis.JedisPoolConfig; diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java new file mode 100644 index 00000000..834babfa --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java @@ -0,0 +1,17 @@ +package com.mpush.tools.redis; + +import com.mpush.tools.spi.SPI; + +import java.util.List; + +@SPI("redisRegister") +public interface RedisRegister { + + void init(List group); + + List getGroupList(); + + RedisNode randomGetRedisNode(String key); + + List hashSet(String key); +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java similarity index 98% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java index f3836a5f..a8063e8a 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java @@ -1,17 +1,17 @@ -package com.shinemo.mpush.tools.redis; +package com.mpush.tools.redis; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.Constants; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.Jsons; +import com.mpush.tools.Jsons; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java similarity index 96% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java index f5ced20d..c50d3eac 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/ConsistentHash.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis.consistenthash; +package com.mpush.tools.redis.consistenthash; import java.util.Collection; import java.util.SortedMap; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java similarity index 87% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java index 0c2a7616..59beaacb 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/consistenthash/Node.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis.consistenthash; +package com.mpush.tools.redis.consistenthash; public class Node { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java similarity index 74% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java index 57086e73..775992d9 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -1,17 +1,17 @@ -package com.shinemo.mpush.tools.redis.jedis.services; +package com.mpush.tools.redis.jedis.services; + +import com.google.common.collect.Lists; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.redis.RedisNode; +import com.mpush.tools.Jsons; +import com.mpush.tools.redis.RedisRegister; import java.util.Collections; import java.util.List; -import com.google.common.collect.Lists; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisNode; -import com.shinemo.mpush.tools.redis.RedisRegister; - -public class JedisRegisterManager implements RedisRegister{ +public class JedisRegisterManager implements RedisRegister { private static List groups = Lists.newArrayList(); @@ -21,7 +21,7 @@ public class JedisRegisterManager implements RedisRegister{ @Override public void init(List group) { if (group == null || group.isEmpty()) { - LoggerManage.log(LogType.REDIS, "init redis client error, redis server is none."); + LoggerManage.log(LogType.REDIS, "init redis client error, redis server is none."); throw new RuntimeException("init redis client error, redis server is none."); } groups = group; @@ -36,7 +36,7 @@ public List getGroupList() { private void printGroupList() { for (RedisGroup app : groups) { - LoggerManage.log(LogType.REDIS,Jsons.toJson(app)); + LoggerManage.log(LogType.REDIS, Jsons.toJson(app)); } } @@ -74,5 +74,5 @@ public List hashSet(String key) { } return nodeList; } - + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java similarity index 82% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java index 28926c37..ae2361b6 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis.listener; +package com.mpush.tools.redis.listener; import java.util.List; import java.util.Map; @@ -6,11 +6,11 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.redis.manage.RedisManage; -import com.shinemo.mpush.tools.redis.pubsub.Subscriber; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.tools.redis.pubsub.Subscriber; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; public class ListenerDispatcher implements MessageListener { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java similarity index 65% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java index f5967a39..627bfefc 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/listener/MessageListener.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.redis.listener; +package com.mpush.tools.redis.listener; public interface MessageListener { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java similarity index 74% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java index fbb688ce..cc0c9790 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/manage/RedisManage.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java @@ -1,37 +1,36 @@ -package com.shinemo.mpush.tools.redis.manage; +package com.mpush.tools.redis.manage; + +import com.google.common.collect.Sets; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.Jsons; +import com.mpush.tools.redis.RedisNode; +import com.mpush.tools.redis.RedisRegister; +import com.mpush.tools.redis.RedisUtil; +import com.mpush.tools.spi.ServiceContainer; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPubSub; import java.util.List; import java.util.Map; import java.util.Set; - -import redis.clients.jedis.JedisPubSub; - -import com.google.common.collect.Sets; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.RedisNode; -import com.shinemo.mpush.tools.redis.RedisRegister; -import com.shinemo.mpush.tools.redis.RedisUtil; -import com.shinemo.mpush.tools.spi.ServiceContainer; - /** * redis 对外封装接口 - * */ public class RedisManage { - - private static final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); - - public static long incr(String key,Integer time){ - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incr(nodeList, key, time); - } - - public static long incrBy(String key,long delt){ - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incrBy(nodeList, key, delt); - } - + + private static final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); + + public static long incr(String key, Integer time) { + List nodeList = redisRegister.hashSet(key); + return RedisUtil.incr(nodeList, key, time); + } + + public static long incrBy(String key, long delt) { + List nodeList = redisRegister.hashSet(key); + return RedisUtil.incrBy(nodeList, key, delt); + } + /********************* * k v redis start ********************************/ @@ -51,10 +50,9 @@ public static void set(String key, T value, Integer time) { } /** - * @param nodeList * @param key * @param value - * @param time seconds + * @param time seconds */ public static void set(String key, String value, Integer time) { List nodeList = redisRegister.hashSet(key); @@ -112,8 +110,6 @@ public static Map hgetAll(String namespace, Class clazz) { /** * 返回 key 指定的哈希集中所有字段的名字。 * - * @param node - * @param key * @return */ public static Set hkeys(String namespace) { @@ -124,10 +120,8 @@ public static Set hkeys(String namespace) { /** * 返回 key 指定的哈希集中指定字段的值 * - * @param node * @param key * @param clazz - * @param fields * @return */ public static List hmget(String namespace, Class clazz, String... key) { @@ -138,8 +132,6 @@ public static List hmget(String namespace, Class clazz, String... key) /** * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 * - * @param nodeList - * @param key * @param hash * @param time */ @@ -215,10 +207,10 @@ public static long llen(String key) { RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.llen(node, key); } - + public static void lrem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); RedisUtil.lRem(nodeList, key, jsonValue); } @@ -239,49 +231,59 @@ public static void subscribe(JedisPubSub pubsub, String... channels) { RedisUtil.subscribe(set, pubsub, channels); } - + public static void sAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sAdd(nodeList, key, jsonValue); + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); + RedisUtil.sAdd(nodeList, key, jsonValue); } - + public static Long sCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); + RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.sCard(node, key); } - + public static void sRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); RedisUtil.sRem(nodeList, key, jsonValue); } - - public static List sScan(String key,int start, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); + + public static List sScan(String key, int start, Class clazz) { + RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.sScan(node, key, clazz, start); } - + public static void zAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); RedisUtil.zAdd(nodeList, key, jsonValue); - } - - public static Long zCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); + } + + public static Long zCard(String key) { + RedisNode node = redisRegister.randomGetRedisNode(key); return RedisUtil.zCard(node, key); - } - - public static void zRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); + } + + public static void zRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = redisRegister.hashSet(key); RedisUtil.zRem(nodeList, key, jsonValue); - } - - public static List zrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zrange(node, key, start, end, clazz); - } - + } + + public static List zrange(String key, int start, int end, Class clazz) { + RedisNode node = redisRegister.randomGetRedisNode(key); + return RedisUtil.zrange(node, key, start, end, clazz); + } + + public static void test(List groupList) { + for (RedisGroup group : groupList) { + for (RedisNode node : group.getRedisNodeList()) { + Jedis jedis = RedisUtil.getClient(node); + if (jedis == null) throw new RuntimeException("init redis sever error."); + jedis.close(); + } + } + } + } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java similarity index 86% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java rename to mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java index b4235af3..b0b2c7f7 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.tools.redis.pubsub; +package com.mpush.tools.redis.pubsub; -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.Jsons; +import com.mpush.tools.redis.listener.ListenerDispatcher; import redis.clients.jedis.JedisPubSub; @@ -61,7 +61,7 @@ public void unsubscribe() { @Override public void unsubscribe(String... channels) { - LoggerManage.log(LogType.REDIS, "unsubscribe:{}",Jsons.toJson(channels)); + LoggerManage.log(LogType.REDIS, "unsubscribe:{}", Jsons.toJson(channels)); super.unsubscribe(channels); } diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java b/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java similarity index 88% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java rename to mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java index 4e6d432a..e212649e 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/SPI.java +++ b/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.spi; +package com.mpush.tools.spi; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java new file mode 100644 index 00000000..9436c75d --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java @@ -0,0 +1,42 @@ +package com.mpush.tools.spi; + +import java.util.Map; +import java.util.ServiceLoader; +import java.util.concurrent.ConcurrentHashMap; + +public class ServiceContainer { + private static final Map cache = new ConcurrentHashMap<>(); + + public static T load(Class clazz) { + String name = clazz.getName(); + Object o = cache.get(name); + if (o == null) { + T t = load0(clazz); + if (t != null) { + cache.put(name, t); + return t; + } + } else if (clazz.isInstance(o)) { + return (T) o; + } + + return load0(clazz); + } + + public static T load0(Class clazz) { + ServiceLoader factories = ServiceLoader.load(clazz); + if (factories.iterator().hasNext()) { + return factories.iterator().next(); + } else { + // By default ServiceLoader.load uses the TCCL, this may not be enough in environment deading with + // classloaders differently such as OSGi. So we should try to use the classloader having loaded this + // class. In OSGi it would be the bundle exposing vert.x and so have access to all its classes. + factories = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()); + if (factories.iterator().hasNext()) { + return factories.iterator().next(); + } else { + throw new IllegalStateException("Cannot find META-INF/services/" + clazz.getName() + " on classpath"); + } + } + } +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java similarity index 96% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/NamedThreadFactory.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 1338dc9c..39fa5f51 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.thread; +package com.mpush.tools.thread; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java similarity index 96% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java index 1ed21225..63fe7d72 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/ThreadNameSpace.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.thread; +package com.mpush.tools.thread; public class ThreadNameSpace { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java similarity index 87% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java index 3c30985e..e56b5863 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java @@ -1,14 +1,14 @@ -package com.shinemo.mpush.tools.thread.threadpool; +package com.mpush.tools.thread.threadpool; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; +import com.mpush.tools.JVMUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.JVMUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.ConfigCenter; public class IgnoreRunsPolicy implements RejectedExecutionHandler{ diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java similarity index 63% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java index a7255ac9..da079304 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPool.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.tools.thread.threadpool; +package com.mpush.tools.thread.threadpool; import java.util.concurrent.Executor; -import com.shinemo.mpush.tools.spi.SPI; +import com.mpush.tools.spi.SPI; @SPI("cachedThreadPool") public interface ThreadPool { diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java similarity index 87% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java index 3557dc1b..5823bad0 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java @@ -1,9 +1,9 @@ -package com.shinemo.mpush.tools.thread.threadpool; +package com.mpush.tools.thread.threadpool; -import com.shinemo.mpush.tools.Constants; -import com.shinemo.mpush.tools.thread.ThreadNameSpace; -import com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; +import com.mpush.tools.Constants; +import com.mpush.tools.thread.ThreadNameSpace; +import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; +import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; public class ThreadPoolContext { diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java new file mode 100644 index 00000000..db2b1d79 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java @@ -0,0 +1,45 @@ +package com.mpush.tools.thread.threadpool; + +import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; +import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; + +public class ThreadPoolManager { + + private static final Map poolCache = new HashMap(); + + private static ThreadPool cachedThreadPool = new CachedThreadPool(); + + private static ThreadPool fixedThreadPool = new FixedThreadPool(); + + public static Executor bossExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.BOSS_THREAD_POOL); + public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); + public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); + public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); + public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); + + public static Executor httpExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); + + static { + poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); + poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); + poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); + poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); + poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); + poolCache.put(ThreadPoolContext.HTTP_THREAD_POOL.getName(), httpExecutor); + } + + public static final Map getPool() { + return poolCache; + } + + public static final Thread newThread(String name, Runnable target) { + Thread thread = new Thread(target, name); + return thread; + } + + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java similarity index 80% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java index 3e8df902..e432d46d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPool.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.thread.threadpool.cached; +package com.mpush.tools.thread.threadpool.cached; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; @@ -8,10 +8,10 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import com.shinemo.mpush.tools.thread.NamedThreadFactory; -import com.shinemo.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; +import com.mpush.tools.thread.threadpool.ThreadPoolContext; +import com.mpush.tools.thread.threadpool.ThreadPool; /** * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java similarity index 81% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java index c2119e65..def69d45 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.tools.thread.threadpool.cached; +package com.mpush.tools.thread.threadpool.cached; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; +import com.mpush.tools.thread.threadpool.ThreadPoolContext; -public class CachedThreadPoolContext extends ThreadPoolContext{ +public class CachedThreadPoolContext extends ThreadPoolContext { public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) { this(name, corePoolSize, maxPoolSize, keepAliveSeconds, 0); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java similarity index 79% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java index 136c4f80..743ec71d 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.thread.threadpool.fixed; +package com.mpush.tools.thread.threadpool.fixed; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; @@ -8,10 +8,10 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import com.shinemo.mpush.tools.thread.NamedThreadFactory; -import com.shinemo.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; +import com.mpush.tools.thread.threadpool.ThreadPool; +import com.mpush.tools.thread.threadpool.ThreadPoolContext; /** *此线程池启动时即创建固定大小的线程数,不做任何伸缩 diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java similarity index 73% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java index 0bc0f92d..09cc9305 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java @@ -1,8 +1,8 @@ -package com.shinemo.mpush.tools.thread.threadpool.fixed; +package com.mpush.tools.thread.threadpool.fixed; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; +import com.mpush.tools.thread.threadpool.ThreadPoolContext; -public class FixedThreadPoolContext extends ThreadPoolContext{ +public class FixedThreadPoolContext extends ThreadPoolContext { public FixedThreadPoolContext(String name, int threads,int queueCapacity) { super(name, threads, 0, queueCapacity, 0); diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java deleted file mode 100644 index b5c9b0f4..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/event/EventType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.shinemo.mpush.tools.event; - -public enum EventType { - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java deleted file mode 100644 index 67853708..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/redis/RedisRegister.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.shinemo.mpush.tools.redis; - -import java.util.List; - -import com.shinemo.mpush.tools.spi.SPI; - -@SPI("redisRegister") -public interface RedisRegister { - - public void init(List group); - - public List getGroupList(); - - public RedisNode randomGetRedisNode(String key); - - public List hashSet(String key); - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java deleted file mode 100644 index 88c31504..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/spi/ServiceContainer.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.shinemo.mpush.tools.spi; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentMap; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -public class ServiceContainer { - - private static final Logger log = LoggerFactory.getLogger(ServiceContainer.class); - private static final String PREFIX = "META-INF/mpush/services/"; - - // class -> ( beanId -> beanClass ) - private static final ConcurrentMap, Map>> clazzCacheMap = Maps.newConcurrentMap(); - - // class -> ( beanId -> beanInstance) - private static final ConcurrentMap, ConcurrentMap> objectsCachedMap = Maps.newConcurrentMap(); - - //暂时不使用这个,但是代码保留 - @Deprecated - public static T load(Class clazz) { - try { - T instance = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()).iterator().next(); - return instance; - } catch (Throwable e) { - log.warn("can not load " + clazz, e); - } - return null; - } - - //暂时不使用这个,但是代码保留 - @Deprecated - public static List loadList(Class classType) { - List list = Lists.newArrayList(); - try { - for (T instance : ServiceLoader.load(classType, ServiceContainer.class.getClassLoader())) { - list.add(instance); - } - return list; - } catch (RuntimeException e) { - throw e; - } - } - - public static T getInstance(Class clazz) { - - if (clazz == null) - throw new IllegalArgumentException("type == null"); - if (!clazz.isInterface()) { - throw new IllegalArgumentException(" type(" + clazz + ") is not interface!"); - } - if (!clazz.isAnnotationPresent(SPI.class)) { - throw new IllegalArgumentException("type(" + clazz + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!"); - } - - SPI spi = clazz.getAnnotation(SPI.class); - String instanceName = spi.value(); - - if (StringUtils.isBlank(instanceName)) { - instanceName = toLowerCaseFirstOne(clazz.getName()); - } - - return getInstance(clazz, instanceName); - - } - - @SuppressWarnings("unchecked") - public static List getInstances(Class clazz) { - ConcurrentMap objMap = objectsCachedMap.get(clazz); - if (objMap == null) { - synchronized (clazz) { - objMap = objectsCachedMap.get(clazz); - if (objMap == null) { - objMap = Maps.newConcurrentMap(); - objectsCachedMap.put(clazz, objMap); - } - } - } - objMap = objectsCachedMap.get(clazz); - if (!objMap.isEmpty()) { - return Lists.newArrayList((Collection) objMap.values()); - } - - initClazzInstances(clazz); - - objMap = objectsCachedMap.get(clazz); - if (!objMap.isEmpty()) { - return Lists.newArrayList((Collection) objMap.values()); - } - throw new IllegalStateException("Failed getInstance class(interface: " + clazz); - - } - - @SuppressWarnings("unchecked") - public static T getInstance(Class clazz, String key) { - ConcurrentMap objMap = objectsCachedMap.get(clazz); - if (objMap == null) { - synchronized (clazz) { - objMap = objectsCachedMap.get(clazz); - if (objMap == null) { - objMap = Maps.newConcurrentMap(); - objectsCachedMap.put(clazz, objMap); - } - } - } - objMap = objectsCachedMap.get(clazz); - - T obj = (T) objMap.get(key); - - if (obj != null) { - return obj; - } - - // 初始化所有 - initClazzInstances(clazz); - - obj = (T) objMap.get(key); - - if (obj != null) { - return obj; - } - - throw new IllegalStateException("Failed getInstance class(interface: " + clazz + ", key: " + key + ")"); - - } - - private static void initClazzInstances(Class clazz) { - - Map> clazzMap = getClazzMap(clazz); - ConcurrentMap objMap = objectsCachedMap.get(clazz); - if (objMap.isEmpty()) { - synchronized (clazz) { - objMap = objectsCachedMap.get(clazz); - if (objMap.isEmpty()) { - Iterator>> iter = clazzMap.entrySet().iterator(); - while (iter.hasNext()) { - Entry> entry = iter.next(); - String entryKey = entry.getKey(); - Class val = entry.getValue(); - Object oldObj = objMap.get(entryKey); - if (oldObj == null) { - Object newObj; - try { - newObj = val.newInstance(); - objMap.putIfAbsent(entryKey, newObj); - } catch (Exception e) { - } - } - } - objectsCachedMap.put(clazz, objMap); - } - } - } - - } - - private static Map> getClazzMap(Class clazz) { - Map> clazzMap = clazzCacheMap.get(clazz); - if (clazzMap == null) { - loadFile(clazz); - } - clazzMap = clazzCacheMap.get(clazz); - return clazzMap; - } - - private static void loadFile(Class type) { - String fileName = PREFIX + type.getName(); - Map> map = Maps.newHashMap(); - try { - Enumeration urls; - // ClassLoader classLoader = - // ServiceContainer.class.getClassLoader(); - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader != null) { - urls = classLoader.getResources(fileName); - } else { - urls = ClassLoader.getSystemResources(fileName); - } - if (urls != null) { - while (!urls.hasMoreElements() && classLoader != null) { - classLoader = classLoader.getParent(); - if (classLoader != null) { - urls = classLoader.getResources(fileName); - } - } - while (urls.hasMoreElements()) { - java.net.URL url = urls.nextElement(); - try { - BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); - try { - String line = null; - while ((line = reader.readLine()) != null) { - final int ci = line.indexOf('#'); - if (ci >= 0) - line = line.substring(0, ci); - line = line.trim(); - if (line.length() > 0) { - try { - String name = null; - int i = line.indexOf('='); - if (i > 0) { - name = line.substring(0, i).trim(); - line = line.substring(i + 1).trim(); - } - if (line.length() > 0) { - Class clazz = Class.forName(line, false, classLoader); - if (!type.isAssignableFrom(clazz)) { - throw new IllegalStateException("Error when load extension class(interface: " + type + ", class line: " + clazz.getName() + "), class " - + clazz.getName() + "is not subtype of interface."); - } - map.put(name, clazz); - } - } catch (Throwable t) { - } - } - } // end of while read lines - } finally { - reader.close(); - } - } catch (Throwable t) { - log.error("", "Exception when load extension class(interface: " + type + ", class file: " + url + ") in " + url, t); - } - } // end of while urls - } - } catch (Throwable t) { - log.error("", "Exception when load extension class(interface: " + type + ", description file: " + fileName + ").", t); - } - synchronized (type) { - Map> oldMap = clazzCacheMap.get(type); - if (oldMap == null) { - clazzCacheMap.put(type, map); - } - } - - } - - public static String toLowerCaseFirstOne(String s) { - if (Character.isLowerCase(s.charAt(0))) - return s; - else - return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString(); - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java deleted file mode 100644 index 041f5559..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.shinemo.mpush.tools.thread.threadpool; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Executor; - - -import com.shinemo.mpush.tools.spi.ServiceContainer; - -public class ThreadPoolManager { - - private static final Map poolCache = new HashMap(); - - private static ThreadPool cachedThreadPool = ServiceContainer.getInstance(ThreadPool.class, "cachedThreadPool"); - - private static ThreadPool fixedThreadPool = ServiceContainer.getInstance(ThreadPool.class, "fixedThreadPool"); - - public static Executor bossExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.BOSS_THREAD_POOL); - public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); - public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); - public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); - public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); - - public static Executor httpExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); - - static{ - poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); - poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); - poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); - poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); - poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); - poolCache.put(ThreadPoolContext.HTTP_THREAD_POOL.getName(), httpExecutor); - } - - public static final Map getPool(){ - return poolCache; - } - - public static final Thread newThread(String name,Runnable target){ - Thread thread = new Thread(target, name); - return thread; - } - - - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java deleted file mode 100644 index fca7d9eb..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkConfig.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.shinemo.mpush.tools.zk; -import com.shinemo.mpush.tools.Constants; - - -public class ZkConfig { - - private final String ipLists; - - private final String namespace; - - private final int maxRetry; - - private final int minTime; - - private final int maxTime; - - private final int sessionTimeout; - - private final int connectionTimeout; - - private final String digest; - - private final String localCachePath; - - public ZkConfig(String ipLists, String namespace) { - this(ipLists, namespace, null); - } - - public ZkConfig(String ipLists, String namespace,String digest) { - this(ipLists, namespace, Constants.ZK_MAX_RETRY, Constants.ZK_MIN_TIME, Constants.ZK_MAX_TIME, Constants.ZK_SESSION_TIMEOUT, Constants.ZK_CONNECTION_TIMEOUT,digest,Constants.ZK_DEFAULT_CACHE_PATH); - } - - public ZkConfig(String ipLists, String namespace, int maxRetry, int minTime, int maxTime, int sessionTimeout, int connectionTimeout,String digest,String localCachePath) { - this.ipLists = ipLists; - this.namespace = namespace; - this.maxRetry = maxRetry; - this.minTime = minTime; - this.maxTime = maxTime; - this.sessionTimeout = sessionTimeout; - this.connectionTimeout = connectionTimeout; - this.digest = digest; - this.localCachePath = localCachePath; - } - - public String getIpLists() { - return ipLists; - } - - public String getNamespace() { - return namespace; - } - - public int getMaxRetry() { - return maxRetry; - } - - public int getMinTime() { - return minTime; - } - - public int getMaxTime() { - return maxTime; - } - - public int getSessionTimeout() { - return sessionTimeout; - } - - public int getConnectionTimeout() { - return connectionTimeout; - } - - public String getDigest() { - return digest; - } - - public String getLocalCachePath() { - return localCachePath; - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java deleted file mode 100644 index c335076f..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZkRegister.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.shinemo.mpush.tools.zk; - -import java.util.List; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCache; -import com.shinemo.mpush.tools.spi.SPI; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; - - -@SPI("zkRegister") -public interface ZkRegister { - - public void init(); - - public void close(); - - public void remove(String key); - - public void registerEphemeralSequential(String key); - - public void registerEphemeralSequential(String key, String value); - - public void registerEphemeral(String key, String value); - - public void update(String key, String value); - - public void registerPersist(String key, String value); - - public boolean isExisted(String key); - - public List getChildrenKeys(String key); - - public String get(String key); - - public CuratorFramework getClient(); - - public ZkConfig getZkConfig(); - - public TreeCache getCache(); - - public void registerListener(DataChangeListener listener); - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java deleted file mode 100644 index cb18561f..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/curator/services/ZkRegisterManager.java +++ /dev/null @@ -1,309 +0,0 @@ -package com.shinemo.mpush.tools.zk.curator.services; - -import java.nio.charset.Charset; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.CuratorFrameworkFactory.Builder; -import org.apache.curator.framework.api.ACLProvider; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.state.ConnectionState; -import org.apache.curator.framework.state.ConnectionStateListener; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.CloseableUtils; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.data.ACL; - -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.MPushUtil; -import com.shinemo.mpush.tools.config.ConfigCenter; -import com.shinemo.mpush.tools.zk.ZkConfig; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; - -public class ZkRegisterManager implements ZkRegister { - - private ZkConfig zkConfig; - - private CuratorFramework client; - private TreeCache cache; - - @Override - public ZkConfig getZkConfig() { - return zkConfig; - } - - @Override - public CuratorFramework getClient() { - return client; - } - - /** - * 初始化 - */ - @Override - public void init() { - zkConfig = new ZkConfig(ConfigCenter.holder.zkIp(), ConfigCenter.holder.zkNamespace(),ConfigCenter.holder.zkDigest()); - LoggerManage.info(LogType.ZK, "start registry zk, server lists is:{}", zkConfig.getIpLists()); - Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getIpLists()) - .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())).namespace(zkConfig.getNamespace()); - if (zkConfig.getConnectionTimeout() > 0) { - builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); - } - if (zkConfig.getSessionTimeout() > 0) { - builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); - } - - if (StringUtils.isNoneBlank(zkConfig.getDigest())) { - builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() { - - @Override - public List getDefaultAcl() { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - - @Override - public List getAclForPath(final String path) { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - }); - } - client = builder.build(); - client.start(); - try { - client.blockUntilConnected(); - cacheData(); - registerConnectionLostListener(); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK,ex,"zk connection error:{}", Jsons.toJson(zkConfig)); - } - - } - - // 注册连接状态监听器 - private void registerConnectionLostListener() { - client.getConnectionStateListenable().addListener(new ConnectionStateListener() { - - @Override - public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - if (ConnectionState.LOST == newState) { - LoggerManage.log(LogType.ZK, "{} lost connection", MPushUtil.getInetAddress()); - } else if (ConnectionState.RECONNECTED == newState) { - LoggerManage.log(LogType.ZK, "{} reconnected", MPushUtil.getInetAddress()); - } - } - }); - } - - // 本地缓存 - private void cacheData() throws Exception { - cache = new TreeCache(client, zkConfig.getLocalCachePath()); - cache.start(); - } - - private void waitClose() { - try { - Thread.sleep(600); - } catch (final InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - - /** - * 关闭 - */ - @Override - public void close() { - if (null != cache) { - cache.close(); - } - waitClose(); - CloseableUtils.closeQuietly(client); - } - - /** - * 获取数据,先从本地获取,本地找不到,从远程获取 - * - * @param key - * @return - */ - @Override - public String get(final String key) { - if (null == cache) { - return null; - } - ChildData resultIncache = cache.getCurrentData(key); - if (null != resultIncache) { - return null == resultIncache.getData() ? null : new String(resultIncache.getData(), Charset.forName("UTF-8")); - } - return getFromRemote(key); - } - - /** - * 从远程获取数据 - * - * @param key - * @return - */ - public String getFromRemote(final String key) { - try { - return new String(client.getData().forPath(key), Charset.forName("UTF-8")); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "getFromRemote:{}", key); - return null; - } - } - - /** - * 获取子节点 - * - * @param key - * @return - */ - @Override - public List getChildrenKeys(final String key) { - try { - List result = client.getChildren().forPath(key); - Collections.sort(result, new Comparator() { - - @Override - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); - return result; - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "getChildrenKeys:{}", key); - return Collections.emptyList(); - } - } - - /** - * 判断路径是否存在 - * - * @param key - * @return - */ - @Override - public boolean isExisted(final String key) { - try { - return null != client.checkExists().forPath(key); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "isExisted:{}", key); - return false; - } - } - - /** - * 持久化数据 - * - * @param key - * @param value - */ - @Override - public void registerPersist(final String key, final String value) { - try { - if (!isExisted(key)) { - client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); - } else { - update(key, value); - } - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persist:{},{}", key,value); - } - } - - /** - * 更新数据 - * - * @param key - * @param value - */ - @Override - public void update(final String key, final String value) { - try { - client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charset.forName("UTF-8"))).and().commit(); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "update:{},{}", key,value); - } - } - - /** - * 注册临时数据 - * - * @param key - * @param value - */ - @Override - public void registerEphemeral(final String key, final String value) { - try { - if (isExisted(key)) { - client.delete().deletingChildrenIfNeeded().forPath(key); - } - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charset.forName("UTF-8"))); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:{},{}", key,value); - } - } - - /** - * 注册临时顺序数据 - * - * @param key - */ - @Override - public void registerEphemeralSequential(final String key, final String value) { - try { - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{},{}", key,value); - } - } - - /** - * 注册临时顺序数据 - * - * @param key - */ - @Override - public void registerEphemeralSequential(final String key) { - try { - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{}", key); - } - } - - /** - * 删除数据 - * - * @param key - */ - @Override - public void remove(final String key) { - try { - client.delete().deletingChildrenIfNeeded().forPath(key); - } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "remove:{}", key); - } - } - - @Override - public void registerListener(DataChangeListener listener){ - cache.getListenable().addListener(listener); - } - - @Override - public TreeCache getCache() { - return cache; - } - -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java deleted file mode 100644 index f94b1566..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/DataChangeListener.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; - -import com.shinemo.mpush.log.LogType; -import com.shinemo.mpush.log.LoggerManage; - -public abstract class DataChangeListener implements TreeCacheListener{ - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - String path = null == event.getData() ? "" : event.getData().getPath(); - if (path.isEmpty()) { - return; - } - LoggerManage.log(LogType.ZK, "DataChangeListener:{},{},namespace:{}", path,listenerPath(),client.getNamespace()); - if(path.startsWith(listenerPath())){ - dataChanged(client, event, path); - } - } - - public abstract void initData(); - - public abstract void dataChanged(CuratorFramework client, TreeCacheEvent event,String path) throws Exception; - - public abstract String listenerPath(); -} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java b/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java deleted file mode 100644 index 5cc4666a..00000000 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/listener/impl/RedisPathListener.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.shinemo.mpush.tools.zk.listener.impl; - -import java.util.Collections; -import java.util.List; - -import com.google.common.base.Strings; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.shinemo.mpush.tools.Jsons; -import com.shinemo.mpush.tools.redis.RedisGroup; -import com.shinemo.mpush.tools.redis.RedisRegister; -import com.shinemo.mpush.tools.spi.ServiceContainer; -import com.shinemo.mpush.tools.zk.ZKPath; -import com.shinemo.mpush.tools.zk.ZkRegister; -import com.shinemo.mpush.tools.zk.listener.DataChangeListener; - -/** - * redis 监控 - */ -public class RedisPathListener extends DataChangeListener { - private static final Logger log = LoggerFactory.getLogger(RedisPathListener.class); - - private final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class); - - private final RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); - - // 获取redis列表 - private void _initData() { - log.warn("start init redis data"); - List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); - redisRegister.init(group); - log.warn("end init redis data"); - } - - private void dataRemove(ChildData data) { - _initData(); - } - - private void dataAddOrUpdate(ChildData data) { - _initData(); - } - - @SuppressWarnings("unchecked") - private List getRedisGroup(String fullPath) { - String rawGroup = zkRegister.get(fullPath); - if (Strings.isNullOrEmpty(rawGroup)) - return Collections.EMPTY_LIST; - List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); - if (group == null) - return Collections.EMPTY_LIST; - return group; - } - - @Override - public void initData() { - _initData(); - } - - @Override - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn("RedisPathListener other path:" + data + "," + event.getType().name() + "," + data); - } - - } - - @Override - public String listenerPath() { - return ZKPath.REDIS_SERVER.getPath(); - } -} diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister deleted file mode 100644 index 4fa6612a..00000000 --- a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.redis.RedisRegister +++ /dev/null @@ -1 +0,0 @@ -redisRegister=com.shinemo.mpush.tools.redis.jedis.services.JedisRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool deleted file mode 100644 index 900e3f9c..00000000 --- a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.thread.threadpool.ThreadPool +++ /dev/null @@ -1,2 +0,0 @@ -cachedThreadPool=com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPool -fixedThreadPool=com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPool \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister b/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister deleted file mode 100644 index e877b9a6..00000000 --- a/mpush-tools/src/main/resources/META-INF/mpush/services/com.shinemo.mpush.tools.zk.ZkRegister +++ /dev/null @@ -1 +0,0 @@ -zkRegister=com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister b/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister new file mode 100644 index 00000000..ff33af00 --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister @@ -0,0 +1 @@ +com.mpush.tools.redis.jedis.services.JedisRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java similarity index 97% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java rename to mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java index 475810d8..a1acb4af 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java @@ -1,9 +1,7 @@ -package com.shinemo.mpush.tools; +package com.mpush.tools; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by ohun on 2016/3/8. */ diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java similarity index 84% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java rename to mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java index 48bc40e7..36d4b0d6 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/AESUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java @@ -1,7 +1,6 @@ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; -import com.shinemo.mpush.tools.Constants; -import org.apache.commons.lang3.RandomStringUtils; +import com.mpush.tools.Constants; import org.junit.Test; import java.util.Random; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java similarity index 98% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java rename to mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java index 2ad9fb79..0e5f4e44 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.tools.crypto; +package com.mpush.tools.crypto; -import com.shinemo.mpush.tools.Pair; +import com.mpush.tools.Pair; import org.junit.Before; import org.junit.Test; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java similarity index 99% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java rename to mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java index 34135b24..45d7a82f 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/delayqueue/Exam.java +++ b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.delayqueue; +package com.mpush.tools.delayqueue; import java.util.Iterator; import java.util.Random; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java similarity index 94% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java rename to mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java index ee1d8993..74f74f12 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/OwnerTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java @@ -1,10 +1,9 @@ -package com.shinemo.mpush.tools.owner; +package com.mpush.tools.owner; +import com.mpush.tools.config.ConfigCenter; import org.aeonbits.owner.ConfigFactory; import org.junit.Test; -import com.shinemo.mpush.tools.config.ConfigCenter; - public class OwnerTest { @Test diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java b/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java similarity index 92% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java rename to mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java index 08dd8d97..e3060630 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/owner/ServerConfig.java +++ b/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.owner; +package com.mpush.tools.owner; import org.aeonbits.owner.Config; import org.aeonbits.owner.Config.Sources; diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java new file mode 100644 index 00000000..9cf9a807 --- /dev/null +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java @@ -0,0 +1,60 @@ +package com.mpush.tools.spi; + + +import com.mpush.tools.spi.test.TestService; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + + +public class SpiTest { + + private static Executor pool = Executors.newCachedThreadPool(); + + @Test + public void baseTest() { + TestService testService = ServiceContainer.load(TestService.class); + System.out.println(testService.sayHi(" huang")); + + ServiceContainer.load(TestService.class); + } + + @Ignore + @Test + public void listTest() { + + + } + + @Ignore + @Test + public void mulThreadTest() throws InterruptedException { + pool.execute(new Worker()); + pool.execute(new Worker()); + pool.execute(new ListWorker()); + pool.execute(new ListWorker()); + Thread.sleep(Integer.MAX_VALUE); + } + + + private static final class Worker implements Runnable { + + @Override + public void run() { + } + + } + + private static final class ListWorker implements Runnable { + + @Override + public void run() { + + + } + + } + +} diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java similarity index 52% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestService.java rename to mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java index f5b753a8..8460ca51 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestService.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java @@ -1,6 +1,6 @@ -package com.shinemo.mpush.tools.spi.test; +package com.mpush.tools.spi.test; -import com.shinemo.mpush.tools.spi.SPI; +import com.mpush.tools.spi.SPI; @SPI("test1") diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java similarity index 89% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java rename to mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java index f69aa6bb..a23cf509 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.spi.test; +package com.mpush.tools.spi.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java similarity index 89% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java rename to mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java index dc8be260..575e4316 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/test/TestServiceImpl2.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.spi.test; +package com.mpush.tools.spi.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java similarity index 91% rename from mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java rename to mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java index 43f411b1..1e69324d 100644 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/thread/SyncTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java @@ -1,20 +1,20 @@ -package com.shinemo.mpush.tools.thread; +package com.mpush.tools.thread; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; +import com.mpush.tools.thread.threadpool.ThreadPoolContext; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPool; -import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolContext; -import com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.shinemo.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPool; -import com.shinemo.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; +import com.mpush.tools.thread.threadpool.ThreadPool; +import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; +import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; +import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; +import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; public class SyncTest { diff --git a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java deleted file mode 100644 index 355377f4..00000000 --- a/mpush-tools/src/test/java/com/shinemo/mpush/tools/spi/SpiTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.shinemo.mpush.tools.spi; - - -import java.util.List; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.junit.Ignore; -import org.junit.Test; - -import com.shinemo.mpush.tools.spi.test.TestService; - - -public class SpiTest { - - private static Executor pool = Executors.newCachedThreadPool(); - - @Test - public void baseTest(){ - TestService testService = ServiceContainer.getInstance(TestService.class); - System.out.println(testService.sayHi(" huang")); - - ServiceContainer.getInstance(TestService.class,"test2"); - } - - @Ignore - @Test - public void listTest(){ - - List listRet = ServiceContainer.getInstances(TestService.class); - - for(TestService test:listRet){ - System.out.println(ToStringBuilder.reflectionToString(test.sayHi(" huang list"))); - } - - } - - @Ignore - @Test - public void mulThreadTest() throws InterruptedException{ - pool.execute(new Worker()); - pool.execute(new Worker()); - pool.execute(new ListWorker()); - pool.execute(new ListWorker()); - Thread.sleep(Integer.MAX_VALUE); - } - - - private static final class Worker implements Runnable{ - - @Override - public void run() { - TestService testService = ServiceContainer.getInstance(TestService.class); - System.out.println(testService.sayHi(" huang")+","+ToStringBuilder.reflectionToString(testService)); - } - - } - - private static final class ListWorker implements Runnable{ - - @Override - public void run() { - - List listRet = ServiceContainer.getInstances(TestService.class); - - for(TestService test:listRet){ - System.out.println(test.sayHi(" huang list")+","+Thread.currentThread().getId()+","+ToStringBuilder.reflectionToString(test)); - } - - } - - } - -} diff --git a/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService b/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService deleted file mode 100644 index e085c84d..00000000 --- a/mpush-tools/src/test/resources/META-INF/mpush/services/com.shinemo.mpush.tools.spi.test.TestService +++ /dev/null @@ -1,2 +0,0 @@ -test1=com.shinemo.mpush.tools.spi.test.TestServiceImpl -test2=com.shinemo.mpush.tools.spi.test.TestServiceImpl2 \ No newline at end of file diff --git a/mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService b/mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService new file mode 100644 index 00000000..d914afd4 --- /dev/null +++ b/mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService @@ -0,0 +1,2 @@ +test1=com.mpush.tools.spi.test.TestServiceImpl +test2=com.mpush.tools.spi.test.TestServiceImpl2 \ No newline at end of file diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml new file mode 100644 index 00000000..aa427ea6 --- /dev/null +++ b/mpush-zk/pom.xml @@ -0,0 +1,31 @@ + + + + mpush + com.mpush + 1.0 + + 4.0.0 + + ${mpush.groupId} + mpush-zk + ${mpush-zk-version} + + + + ${mpush.groupId} + mpush-tools + + + org.apache.curator + curator-recipes + + + org.apache.curator + curator-x-discovery + + + + \ No newline at end of file diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java new file mode 100644 index 00000000..bc9b5481 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -0,0 +1,295 @@ +package com.mpush.zk; + +import com.mpush.zk.listener.ZKDataChangeListener; +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import com.mpush.tools.Constants; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.exception.ZKException; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.CuratorFrameworkFactory.Builder; +import org.apache.curator.framework.api.ACLProvider; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.utils.CloseableUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class ZKClient { + public static final ZKClient I = I(); + private ZKConfig zkConfig; + private CuratorFramework client; + private TreeCache cache; + + private synchronized static ZKClient I() { + if (I == null) return new ZKClient(); + else return I; + } + + public ZKClient() { + init(); + } + + /** + * 初始化 + */ + public void init() { + zkConfig = ZKConfig.build(ConfigCenter.holder.zkIp()) + .setDigest(ConfigCenter.holder.zkNamespace()) + .setNamespace(ConfigCenter.holder.zkNamespace()); + Builder builder = CuratorFrameworkFactory + .builder() + .connectString(zkConfig.getHosts()) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())) + .namespace(zkConfig.getNamespace()); + + if (zkConfig.getConnectionTimeout() > 0) { + builder.connectionTimeoutMs(zkConfig.getConnectionTimeout()); + } + if (zkConfig.getSessionTimeout() > 0) { + builder.sessionTimeoutMs(zkConfig.getSessionTimeout()); + } + + if (zkConfig.getDigest() != null) { + builder.authorization("digest", zkConfig.getDigest().getBytes(Constants.UTF_8)) + .aclProvider(new ACLProvider() { + + @Override + public List getDefaultAcl() { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + + @Override + public List getAclForPath(final String path) { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + }); + } + client = builder.build(); + client.start(); + try { + client.blockUntilConnected(); + initLocalCache(zkConfig.getLocalCachePath()); + registerConnectionLostListener(); + } catch (Exception ex) { + throw new ZKException("init zk error, config=" + zkConfig, ex); + } + LoggerManage.info(LogType.ZK, "zk client start success, server lists is:{}", zkConfig.getHosts()); + } + + // 注册连接状态监听器 + private void registerConnectionLostListener() { + client.getConnectionStateListenable().addListener(new ConnectionStateListener() { + + @Override + public void stateChanged(final CuratorFramework client, final ConnectionState newState) { + if (ConnectionState.LOST == newState) { + LoggerManage.log(LogType.ZK, "{} lost connection", MPushUtil.getInetAddress()); + } else if (ConnectionState.RECONNECTED == newState) { + LoggerManage.log(LogType.ZK, "{} reconnected", MPushUtil.getInetAddress()); + } + } + }); + } + + // 本地缓存 + private void initLocalCache(String cachePath) throws Exception { + cache = new TreeCache(client, cachePath); + cache.start(); + } + + private void waitClose() { + try { + Thread.sleep(600); + } catch (final InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + + /** + * 关闭 + */ + public void close() { + if (null != cache) { + cache.close(); + } + waitClose(); + CloseableUtils.closeQuietly(client); + } + + /** + * 获取数据,先从本地获取,本地找不到,从远程获取 + * + * @param key + * @return + */ + public String get(final String key) { + if (null == cache) { + return null; + } + ChildData data = cache.getCurrentData(key); + if (null != data) { + return null == data.getData() ? null : new String(data.getData(), Constants.UTF_8); + } + return getFromRemote(key); + } + + /** + * 从远程获取数据 + * + * @param key + * @return + */ + public String getFromRemote(final String key) { + try { + return new String(client.getData().forPath(key), Constants.UTF_8); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "getFromRemote:{}", key); + return null; + } + } + + /** + * 获取子节点 + * + * @param key + * @return + */ + public List getChildrenKeys(final String key) { + try { + List result = client.getChildren().forPath(key); + Collections.sort(result, new Comparator() { + + @Override + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + return result; + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "getChildrenKeys:{}", key); + return Collections.emptyList(); + } + } + + /** + * 判断路径是否存在 + * + * @param key + * @return + */ + public boolean isExisted(final String key) { + try { + return null != client.checkExists().forPath(key); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "isExisted:{}", key); + return false; + } + } + + /** + * 持久化数据 + * + * @param key + * @param value + */ + public void registerPersist(final String key, final String value) { + try { + if (!isExisted(key)) { + client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); + } else { + update(key, value); + } + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "persist:{},{}", key, value); + } + } + + /** + * 更新数据 + * + * @param key + * @param value + */ + public void update(final String key, final String value) { + try { + client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Constants.UTF_8)).and().commit(); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "update:{},{}", key, value); + } + } + + /** + * 注册临时数据 + * + * @param key + * @param value + */ + public void registerEphemeral(final String key, final String value) { + try { + if (isExisted(key)) { + client.delete().deletingChildrenIfNeeded().forPath(key); + } + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Constants.UTF_8)); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:{},{}", key, value); + } + } + + /** + * 注册临时顺序数据 + * + * @param key + */ + public void registerEphemeralSequential(final String key, final String value) { + try { + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{},{}", key, value); + } + } + + /** + * 注册临时顺序数据 + * + * @param key + */ + public void registerEphemeralSequential(final String key) { + try { + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{}", key); + } + } + + /** + * 删除数据 + * + * @param key + */ + public void remove(final String key) { + try { + client.delete().deletingChildrenIfNeeded().forPath(key); + } catch (final Exception ex) { + LoggerManage.execption(LogType.ZK, ex, "remove:{}", key); + } + } + + public void registerListener(ZKDataChangeListener listener) { + cache.getListenable().addListener(listener); + } + + public ZKConfig getZKConfig() { + return zkConfig; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java new file mode 100644 index 00000000..27cf8912 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -0,0 +1,113 @@ +package com.mpush.zk; + +import com.mpush.tools.Constants; + +public class ZKConfig { + + private String hosts; + + private String digest; + + private String namespace; + + private int maxRetry = Constants.ZK_MAX_RETRY; + + private int minTime = Constants.ZK_MIN_TIME; + + private int maxTime = Constants.ZK_MAX_TIME; + + private int sessionTimeout = Constants.ZK_SESSION_TIMEOUT; + + private int connectionTimeout = Constants.ZK_CONNECTION_TIMEOUT; + + private String localCachePath = Constants.ZK_DEFAULT_CACHE_PATH; + + public ZKConfig(String hosts) { + this.hosts = hosts; + } + + public static ZKConfig build(String hosts) { + return new ZKConfig(hosts); + } + + public String getHosts() { + return hosts; + } + + public ZKConfig setHosts(String hosts) { + this.hosts = hosts; + return this; + } + + public String getNamespace() { + return namespace; + } + + public ZKConfig setNamespace(String namespace) { + this.namespace = namespace; + return this; + } + + public int getMaxRetry() { + return maxRetry; + } + + public ZKConfig setMaxRetry(int maxRetry) { + this.maxRetry = maxRetry; + return this; + } + + public int getMinTime() { + return minTime; + } + + public ZKConfig setMinTime(int minTime) { + this.minTime = minTime; + return this; + } + + public int getMaxTime() { + return maxTime; + } + + public ZKConfig setMaxTime(int maxTime) { + this.maxTime = maxTime; + return this; + } + + public int getSessionTimeout() { + return sessionTimeout; + } + + public ZKConfig setSessionTimeout(int sessionTimeout) { + this.sessionTimeout = sessionTimeout; + return this; + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public ZKConfig setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + return this; + } + + public String getDigest() { + return digest; + } + + public ZKConfig setDigest(String digest) { + this.digest = digest; + return this; + } + + public String getLocalCachePath() { + return localCachePath; + } + + public ZKConfig setLocalCachePath(String localCachePath) { + this.localCachePath = localCachePath; + return this; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java b/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java new file mode 100644 index 00000000..1040b0cb --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java @@ -0,0 +1,16 @@ +package com.mpush.zk; + +import com.mpush.tools.spi.SPI; + +import java.util.Collection; + +@SPI("connectionServerManage") +public interface ZKNodeManager { + + void addOrUpdate(String fullPath, T application); + + void remove(String fullPath); + + Collection getList(); + +} diff --git a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java similarity index 82% rename from mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java rename to mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index e67566a1..c13f1a08 100644 --- a/mpush-tools/src/main/java/com/shinemo/mpush/tools/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -1,4 +1,4 @@ -package com.shinemo.mpush.tools.zk; +package com.mpush.zk; import org.apache.curator.utils.ZKPaths; @@ -30,10 +30,4 @@ public String getFullPath(String nodeName) { return path + ZKPaths.PATH_SEPARATOR + nodeName; } - public static void main(String[] args) { - String test = "/cs/%s/kick"; - - System.out.println(String.format(test, "10.1.10.65")); - } - } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java new file mode 100644 index 00000000..2da8f629 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java @@ -0,0 +1,108 @@ +package com.mpush.zk; + + +import com.mpush.tools.MPushUtil; +import com.mpush.tools.config.ConfigCenter; + +/** + * 系统配置 + */ +public class ZKServerNode { + + private String ip; + + private int port; + + private String extranetIp; + + private transient String zkPath; + + public ZKServerNode() { + } + + public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { + this.ip = ip; + this.port = port; + this.extranetIp = extranetIp; + this.zkPath = zkPath; + } + + public static ZKServerNode csNode() { + return new ZKServerNode(MPushUtil.getLocalIp(), + ConfigCenter.holder.connectionServerPort(), + ZKPath.CONNECTION_SERVER.getWatchPath(), + MPushUtil.getExtranetAddress()); + } + + public static ZKServerNode gsNode() { + return new ZKServerNode(MPushUtil.getLocalIp(), + ConfigCenter.holder.gatewayServerPort(), + ZKPath.GATEWAY_SERVER.getWatchPath(), + null); + } + + public String getIp() { + return ip; + } + + public ZKServerNode setIp(String ip) { + this.ip = ip; + return this; + } + + public int getPort() { + return port; + } + + public ZKServerNode setPort(int port) { + this.port = port; + return this; + } + + public String getExtranetIp() { + return extranetIp; + } + + public ZKServerNode setExtranetIp(String extranetIp) { + this.extranetIp = extranetIp; + return this; + } + + public String getZkPath() { + return zkPath; + } + + public ZKServerNode setZkPath(String zkPath) { + this.zkPath = zkPath; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ZKServerNode that = (ZKServerNode) o; + + if (port != that.port) return false; + return ip != null ? ip.equals(that.ip) : that.ip == null; + + } + + @Override + public int hashCode() { + int result = ip != null ? ip.hashCode() : 0; + result = 31 * result + port; + return result; + } + + @Override + public String toString() { + return "ZKServerNode{" + + "ip='" + ip + '\'' + + ", port=" + port + + ", extranetIp='" + extranetIp + '\'' + + ", zkPath='" + zkPath + '\'' + + '}'; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java new file mode 100644 index 00000000..83ef0f7c --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java @@ -0,0 +1,28 @@ +package com.mpush.zk.listener; + +import com.mpush.log.LogType; +import com.mpush.log.LoggerManage; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; + +public abstract class ZKDataChangeListener implements TreeCacheListener { + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + String path = null == event.getData() ? "" : event.getData().getPath(); + if (path.isEmpty()) { + return; + } + LoggerManage.log(LogType.ZK, "DataChangeListener:{},{},namespace:{}", path, listenerPath(), client.getNamespace()); + if (path.startsWith(listenerPath())) { + dataChanged(client, event, path); + } + } + + public abstract void initData(); + + public abstract void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception; + + public abstract String listenerPath(); +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java new file mode 100644 index 00000000..d7266ad4 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java @@ -0,0 +1,85 @@ +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.tools.Jsons; +import com.mpush.tools.redis.RedisGroup; +import com.mpush.tools.redis.RedisRegister; +import com.mpush.tools.spi.ServiceContainer; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; + +/** + * redis 监控 + */ +public class ZKRedisNodeListener extends ZKDataChangeListener { + private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeListener.class); + + private final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); + + // 获取redis列表 + private void _initData() { + logger.warn("start init redis data"); + List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); + redisRegister.init(group); + logger.warn("end init redis data"); + } + + private void dataRemove(ChildData data) { + _initData(); + } + + private void dataAddOrUpdate(ChildData data) { + _initData(); + } + + @SuppressWarnings("unchecked") + private List getRedisGroup(String fullPath) { + String rawGroup = ZKClient.I.get(fullPath); + if (Strings.isNullOrEmpty(rawGroup)) + return Collections.EMPTY_LIST; + List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); + if (group == null) + return Collections.EMPTY_LIST; + return group; + } + + @Override + public void initData() { + _initData(); + } + + @Override + public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { + + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + logger.warn("RedisPathListener other path:" + data + "," + event.getType().name() + "," + data); + } + + } + + @Override + public String listenerPath() { + return ZKPath.REDIS_SERVER.getPath(); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java new file mode 100644 index 00000000..8bc0a15b --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java @@ -0,0 +1,79 @@ +package com.mpush.zk.listener; + +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKNodeManager; +import com.mpush.zk.ZKServerNode; +import com.mpush.tools.Jsons; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +public abstract class ZKServerNodeListener> extends ZKDataChangeListener { + + private final Logger log = LoggerFactory.getLogger(ZKServerNodeListener.class); + + public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { + String data = ""; + if (event.getData() != null) { + data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); + } + if (Type.NODE_ADDED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else if (Type.NODE_REMOVED == event.getType()) { + dataRemove(event.getData()); + } else if (Type.NODE_UPDATED == event.getType()) { + dataAddOrUpdate(event.getData()); + } else { + log.warn(this.getClass().getSimpleName() + "other path:" + path + "," + event.getType().name() + "," + data); + } + } + + public void initData() { + log.warn(" start init " + this.getClass().getSimpleName() + " server data"); + initData0(); + log.warn(" end init " + this.getClass().getSimpleName() + " server data"); + } + + public abstract String getRegisterPath(); + + public abstract String getFullPath(String raw); + + public abstract T getManager(); + + private void initData0() { + // 获取机器列表 + List rawData = ZKClient.I.getChildrenKeys(getRegisterPath()); + for (String raw : rawData) { + String fullPath = getFullPath(raw); + ZKServerNode app = getServerNode(fullPath); + getManager().addOrUpdate(fullPath, app); + } + } + + private void dataRemove(ChildData data) { + String path = data.getPath(); + getManager().remove(path); + } + + private void dataAddOrUpdate(ChildData data) { + String path = data.getPath(); + byte[] rawData = data.getData(); + ZKServerNode serverApp = Jsons.fromJson(rawData, ZKServerNode.class); + getManager().addOrUpdate(path, serverApp); + } + + private ZKServerNode getServerNode(String fullPath) { + String rawApp = ZKClient.I.get(fullPath); + ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); + return app; + } + + +} diff --git a/pom.xml b/pom.xml index a0466cda..9713384e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,10 +4,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.shinemo.mpush + com.mpush mpush pom - 0.0.0.2 + 1.0 @@ -37,24 +37,31 @@ mpush-netty mpush-common mpush-client - mpush-cs + mpush-boot mpush-test mpush-monitor mpush-log + mpush-zk - 1.7 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 - 0.0.0.1 + UTF-8 + UTF-8 + UTF-8 + 1.8 + com.mpush + 0.0.0.3 + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} + ${mpush-version} 5.0.0.Alpha2 linux-x86_64 @@ -105,10 +112,10 @@ curator-recipes 2.9.1 - - netty - io.netty - + + netty + io.netty + @@ -121,54 +128,59 @@ - ${pom.groupId} + ${mpush.groupId} mpush-api - ${project.version} + ${mpush-api-version} - ${pom.groupId} + ${mpush.groupId} mpush-tools - ${project.version} + ${mpush-tools-version} - ${pom.groupId} + ${mpush.groupId} mpush-common - ${project.version} + ${mpush-common-version} - ${pom.groupId} + ${mpush.groupId} mpush-netty - ${project.version} + ${mpush-netty-version} - ${pom.groupId} + ${mpush.groupId} mpush-core - ${project.version} + ${mpush-core-version} - ${pom.groupId} + ${mpush.groupId} mpush-client - ${project.version} + ${mpush-client-version} - ${pom.groupId} + ${mpush.groupId} mpush-monitor - ${project.version} + ${mpush-monitor-version} - ${pom.groupId} + ${mpush.groupId} mpush-log - ${project.version} + ${mpush-log-version} - ${pom.groupId} + ${mpush.groupId} mpush-test - ${project.version} + ${mpush-test-version} - ${pom.groupId} - mpush-cs - ${project.version} + ${mpush.groupId} + mpush-boot + ${mpush-cs-version} + + + ${mpush.groupId} + mpush-zk + ${mpush-zk-version} @@ -230,21 +242,21 @@ - org.codehaus.janino - janino - 2.7.8 + org.codehaus.janino + janino + 2.7.8 - args4j - args4j - 2.33 + args4j + args4j + 2.33 - commons-net - commons-net - 3.4 + commons-net + commons-net + 3.4 @@ -265,52 +277,28 @@ - - org.apache.maven.plugins maven-compiler-plugin - 3.1 - 1.7 - 1.7 - UTF-8 + ${java.version} + ${java.version} + ${java.encoding} - org.apache.maven.plugins maven-resources-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - true + ${java.encoding} - - maven-assembly-plugin - 2.6 + org.apache.maven.plugins + maven-surefire-plugin - mpush - - assembly.xml - + true - - - package - - single - - - - - diff --git a/pub-daily.py b/pub-daily.py index 0e163036..9344ed27 100755 --- a/pub-daily.py +++ b/pub-daily.py @@ -22,11 +22,11 @@ BASEPATH = '/root/mpush' -MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz' +MPUSH_TAR_NAME = 'mpush-release.tar.gz' -PROCESS_KEY_WORD = 'mpush-cs.jar' +PROCESS_KEY_WORD = 'boot.jar' -GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME +GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' @@ -169,7 +169,8 @@ def main(): print showText('git pull master success','greenText') ##1 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) + runShell('mvn clean') + runShell('mvn package -P %s'%ENV) print showText('assembly success','greenText') ##2 包创建时间 diff --git a/pub-online.py b/pub-online.py index 56454b6f..1d6739ec 100755 --- a/pub-online.py +++ b/pub-online.py @@ -22,11 +22,11 @@ BASEPATH = '/root/mpush' -MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz' +MPUSH_TAR_NAME = 'mpush-release.tar.gz' -PROCESS_KEY_WORD = 'mpush-cs.jar' +PROCESS_KEY_WORD = 'boot.jar' -GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME +GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' @@ -169,7 +169,8 @@ def main(): print showText('git pull master success','greenText') ##1 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) + runShell('mvn clean') + runShell('mvn package -P %s'%ENV) print showText('assembly success','greenText') ##2 包创建时间 diff --git a/pub-pre.py b/pub-pre.py index ca51ecdc..e3bc8ca8 100644 --- a/pub-pre.py +++ b/pub-pre.py @@ -9,9 +9,9 @@ BASEPATH = '/root/mpush' -STARTPROCESS = 'java -jar mpush-cs.jar' +STARTPROCESS = 'java -jar boot.jar' -GITLABPATH = '/data/localgit/mpush/mpush/target/mpush-jar-with-dependency.tar.gz' +GITLABPATH = '/data/localgit/mpush/mpush-boot/target/mpush-release.tar.gz' ENV= 'pre' @@ -72,7 +72,8 @@ def runShell(c): def main(): ##0 assembly - runShell('mvn clean install assembly:assembly -P %s'%ENV) + runShell('mvn clean') + runShell('mvn package -P %s'%ENV) print showText('assembly success','greenText') ##1 包创建时间 diff --git a/start.sh b/start.sh index 47407d39..86399213 100755 --- a/start.sh +++ b/start.sh @@ -6,16 +6,15 @@ base_dir=`pwd` echo "start assembly lib..." -rm -rf $base_dir/target - -mvn clean install assembly:assembly -P $ENV +mvn clean +mvn package -P $ENV echo "start tar mpush..." cd $base_dir/target -tar -xzvf ./mpush-jar-with-dependency.tar.gz +tar -xzvf ./mpush-release.tar.gz echo "start start mpush..." -java -Dio.netty.leakDetectionLevel=advanced -jar $base_dir/target/mpush/mpush-cs.jar & +java -Dio.netty.leakDetectionLevel=advanced -jar $base_dir/target/mpush/boot.jar & echo "end start mpush..." From fcc913e52e01f0d7bca6d56babc88c38d99d77d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 15 May 2016 17:44:00 +0200 Subject: [PATCH 526/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daily-pub.py => bin/daily-pub.py | 0 debug.sh => bin/debug.sh | 7 +- pub-daily.py => bin/pub-daily.py | 0 pub-online.py => bin/pub-online.py | 0 pub-pre.py => bin/pub-pre.py | 0 bin/run.sh | 20 +++ bin/start.sh | 23 ++++ test.py => bin/test.py | 0 test.sh => bin/test.sh | 0 .../conf-daily.properties | 0 .../conf-dev.properties | 0 .../conf-online.properties | 0 .../conf-pre.properties | 0 .../main/java/com/mpush/api/Constants.java | 2 + .../src/main/java/com/mpush/api/Message.java | 4 +- .../java/com/mpush/api/MessageHandler.java | 2 + .../java/com/mpush/api/PacketReceiver.java | 2 + .../main/java/com/mpush/api/PushSender.java | 2 + .../src/main/java/com/mpush/api/Server.java | 6 +- .../java/com/mpush/api/connection/Cipher.java | 2 + .../mpush/api/connection/SessionContext.java | 2 + .../main/java/com/mpush/api/event/Event.java | 2 + .../com/mpush/api/event/HandshakeEvent.java | 2 + .../com/mpush/api/event/KickUserEvent.java | 2 + .../mpush/api/event/RouterChangeEvent.java | 2 + .../mpush/api/exception/CryptoException.java | 10 +- .../mpush/api/exception/DecodeException.java | 2 + .../api/exception/SendMessageException.java | 2 + .../java/com/mpush/api/protocol/Packet.java | 2 + .../com/mpush/api/router/ClientLocation.java | 2 + .../java/com/mpush/api/router/Router.java | 2 + .../com/mpush/api/router/RouterManager.java | 2 + mpush-boot/pom.xml | 5 +- mpush-boot/src/main/java/com/mpush/Main.java | 4 + .../main/java/com/mpush/ServerLauncher.java | 15 ++- .../main/java/com/mpush/boot/BootChain.java | 3 + .../src/main/java/com/mpush/boot/BootJob.java | 3 + .../src/main/java/com/mpush/boot/EndBoot.java | 22 ---- .../java/com/mpush/boot/HttpProxyBoot.java | 19 +++ .../main/java/com/mpush/boot/LastBoot.java | 18 +++ .../main/java/com/mpush/boot/MonitorBoot.java | 17 +++ .../main/java/com/mpush/boot/RedisBoot.java | 35 ++++-- .../main/java/com/mpush/boot/ServerBoot.java | 27 ++-- ...m.shinemo.mpush.common.manage.ServerManage | 1 - .../src/main/resources/config.properties | 2 - .../src/test/java/com/mpush/zk/ZkTest.java | 5 +- .../test/java/com/mpush/zk/ZkUtilTest.java | 8 +- mpush-client/pom.xml | 2 +- .../conn/client/ClientChannelHandler.java | 2 + .../main/java/com/mpush/push/PushRequest.java | 4 +- .../java/com/mpush/push/PushRequestBus.java | 2 + .../push/client/ClientChannelHandler.java | 2 + .../services/com.mpush.zk.ZKNodeManager | 3 +- mpush-common/pom.xml | 11 +- .../java/com/mpush/common/AbstractClient.java | 86 ++++++------- .../main/java/com/mpush/common/ErrorCode.java | 2 + .../main/java/com/mpush/common/EventBus.java | 3 +- .../com/mpush/common/MessageDispatcher.java | 2 + .../common/handler/BaseMessageHandler.java | 14 ++- .../common/handler/ErrorMessageHandler.java | 4 +- .../common/handler/OkMessageHandler.java | 2 + .../com/mpush/common/message/BaseMessage.java | 5 +- .../mpush/common/message/BindUserMessage.java | 2 + .../mpush/common/message/ByteBufMessage.java | 2 + .../mpush/common/message/ErrorMessage.java | 2 + .../common/message/FastConnectMessage.java | 2 + .../common/message/FastConnectOkMessage.java | 2 + .../common/message/HandshakeMessage.java | 2 + .../common/message/HandshakeOkMessage.java | 2 + .../common/message/HttpRequestMessage.java | 2 + .../common/message/HttpResponseMessage.java | 2 + .../mpush/common/message/KickUserMessage.java | 2 + .../com/mpush/common/message/OkMessage.java | 2 + .../com/mpush/common/message/PushMessage.java | 2 + .../message/gateway/GatewayPushMessage.java | 4 +- .../com/mpush/common/router/RemoteRouter.java | 2 + .../common/router/RemoteRouterManager.java | 2 + .../common/router/UserChangeListener.java | 6 +- .../com/mpush/common/security/AesCipher.java | 26 ++-- .../com/mpush/common/security/CipherBox.java | 8 +- .../com/mpush/common/security/RsaCipher.java | 2 + .../mpush/core/handler/BindUserHandler.java | 2 + .../core/handler/FastConnectHandler.java | 7 +- .../core/handler/GatewayPushHandler.java | 21 ++-- .../mpush/core/handler/HandshakeHandler.java | 2 + .../mpush/core/handler/HeartBeatHandler.java | 2 + .../mpush/core/handler/HttpProxyHandler.java | 2 + .../mpush/core/handler/UnbindUserHandler.java | 2 + .../com/mpush/core/router/KickRemoteMsg.java | 2 + .../com/mpush/core/router/LocalRouter.java | 2 + .../mpush/core/router/LocalRouterManager.java | 2 + .../com/mpush/core/router/RouterCenter.java | 2 + .../core/router/RouterChangeListener.java | 3 +- .../router/UserOnlineOfflineListener.java | 5 +- .../com/mpush/core/server/AdminServer.java | 117 ++++-------------- .../mpush/core/server/ConnectionServer.java | 2 +- .../com/mpush/core/server/GatewayServer.java | 6 +- .../core/server/ServerChannelHandler.java | 2 + .../mpush/core/session/ReusableSession.java | 2 + .../core/session/ReusableSessionManager.java | 4 +- .../com/mpush/core/netty/NettyServerTest.java | 8 +- .../com/mpush/netty/client/HttpCallback.java | 2 + .../com/mpush/netty/client/HttpClient.java | 2 + .../com/mpush/netty/client/NettyClient.java | 2 +- .../mpush/netty/client/NettyHttpClient.java | 4 +- .../com/mpush/netty/client/RequestInfo.java | 2 +- .../com/mpush/netty/codec/PacketDecoder.java | 4 +- .../com/mpush/netty/codec/PacketEncoder.java | 2 + .../netty/connection/NettyConnection.java | 46 +++---- .../connection/NettyConnectionManager.java | 8 +- .../com/mpush/netty/server/NettyServer.java | 55 +++++--- .../test/configcenter/ConfigCenterTest.java | 4 +- .../test/java/com/mpush/test/push/Main.java | 8 +- .../main/java/com/mpush/tools/ConsoleLog.java | 32 +++++ .../main/java/com/mpush/tools/Constants.java | 48 ++----- .../main/java/com/mpush/tools/IOUtils.java | 2 + .../main/java/com/mpush/tools/MPushUtil.java | 8 +- .../src/main/java/com/mpush/tools/Pair.java | 2 + .../main/java/com/mpush/tools/Strings.java | 2 + .../com/mpush/tools/config/ConfigCenter.java | 2 +- .../java/com/mpush/tools/crypto/AESUtils.java | 5 +- .../java/com/mpush/tools/crypto/MD5Utils.java | 2 + .../tools/dns/manage/DnsMappingManage.java | 4 +- .../mpush/tools/redis/RedisPoolConfig.java | 14 +-- .../com/mpush/tools/spi/ServiceContainer.java | 56 ++++++--- .../thread/threadpool/IgnoreRunsPolicy.java | 2 +- .../java/com/mpush/tools/IOUtilsTest.java | 2 + .../com/mpush/tools/crypto/RSAUtilsTest.java | 2 + .../java/com/mpush/tools/owner/OwnerTest.java | 32 ++--- .../src/main/java/com/mpush/zk/ZKClient.java | 42 ++++--- .../src/main/java/com/mpush/zk/ZKConfig.java | 35 ++++-- .../main/java/com/mpush/zk/ZKServerNode.java | 12 +- pom.xml | 10 +- start.sh | 20 --- 134 files changed, 699 insertions(+), 473 deletions(-) rename daily-pub.py => bin/daily-pub.py (100%) rename debug.sh => bin/debug.sh (85%) mode change 100755 => 100644 rename pub-daily.py => bin/pub-daily.py (100%) mode change 100755 => 100644 rename pub-online.py => bin/pub-online.py (100%) mode change 100755 => 100644 rename pub-pre.py => bin/pub-pre.py (100%) create mode 100644 bin/run.sh create mode 100644 bin/start.sh rename test.py => bin/test.py (100%) mode change 100755 => 100644 rename test.sh => bin/test.sh (100%) mode change 100755 => 100644 rename conf-daily.properties => conf/conf-daily.properties (100%) rename conf-dev.properties => conf/conf-dev.properties (100%) rename conf-online.properties => conf/conf-online.properties (100%) rename conf-pre.properties => conf/conf-pre.properties (100%) delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/EndBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/LastBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java delete mode 100644 mpush-boot/src/main/resources/META-INF/services/com.shinemo.mpush.common.manage.ServerManage create mode 100644 mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java delete mode 100755 start.sh diff --git a/daily-pub.py b/bin/daily-pub.py similarity index 100% rename from daily-pub.py rename to bin/daily-pub.py diff --git a/debug.sh b/bin/debug.sh old mode 100755 new mode 100644 similarity index 85% rename from debug.sh rename to bin/debug.sh index 41d177ff..82a5fc7d --- a/debug.sh +++ b/bin/debug.sh @@ -1,14 +1,15 @@ #!/bin/sh -ENV=dev -base_dir=`pwd` +ENV=daily +cd `dirname $0` +cd .. echo "start package project..." mvn clean mvn package -P $ENV echo "start tar mpush..." -cd $base_dir/mpush-boot/target +cd ./mpush-boot/target tar -xzvf ./mpush-release.tar.gz echo "start start mpush..." diff --git a/pub-daily.py b/bin/pub-daily.py old mode 100755 new mode 100644 similarity index 100% rename from pub-daily.py rename to bin/pub-daily.py diff --git a/pub-online.py b/bin/pub-online.py old mode 100755 new mode 100644 similarity index 100% rename from pub-online.py rename to bin/pub-online.py diff --git a/pub-pre.py b/bin/pub-pre.py similarity index 100% rename from pub-pre.py rename to bin/pub-pre.py diff --git a/bin/run.sh b/bin/run.sh new file mode 100644 index 00000000..d5f5ab08 --- /dev/null +++ b/bin/run.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +ENV=dev + +cd `dirname $0` +cd .. + +echo "start tar mpush..." +cd ./mpush-boot/target + +rm -rf mpush +tar -xzvf ./mpush-release.tar.gz + +echo "start start mpush..." + +cd mpush +java \ +-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ +-Dio.netty.leakDetectionLevel=advanced \ +-jar boot.jar diff --git a/bin/start.sh b/bin/start.sh new file mode 100644 index 00000000..0f807d5a --- /dev/null +++ b/bin/start.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +ENV=dev + +cd `dirname $0` +cd .. + +echo "start package project..." +mvn clean +mvn package -P $ENV + +echo "start tar mpush..." +cd ./mpush-boot/target +tar -xzvf ./mpush-release.tar.gz + +echo "start start mpush..." +cd mpush +nohup java \ +-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ +-Dio.netty.leakDetectionLevel=advanced \ +-jar boot.jar >/dev/null 2>&1 & + +echo "end start mpush..." \ No newline at end of file diff --git a/test.py b/bin/test.py old mode 100755 new mode 100644 similarity index 100% rename from test.py rename to bin/test.py diff --git a/test.sh b/bin/test.sh old mode 100755 new mode 100644 similarity index 100% rename from test.sh rename to bin/test.sh diff --git a/conf-daily.properties b/conf/conf-daily.properties similarity index 100% rename from conf-daily.properties rename to conf/conf-daily.properties diff --git a/conf-dev.properties b/conf/conf-dev.properties similarity index 100% rename from conf-dev.properties rename to conf/conf-dev.properties diff --git a/conf-online.properties b/conf/conf-online.properties similarity index 100% rename from conf-online.properties rename to conf/conf-online.properties diff --git a/conf-pre.properties b/conf/conf-pre.properties similarity index 100% rename from conf-pre.properties rename to conf/conf-pre.properties diff --git a/mpush-api/src/main/java/com/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java index d399d042..11c15dbd 100644 --- a/mpush-api/src/main/java/com/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -4,6 +4,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); diff --git a/mpush-api/src/main/java/com/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java index 90de5c06..cbf37d56 100644 --- a/mpush-api/src/main/java/com/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -1,11 +1,13 @@ package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public interface Message { diff --git a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java index 7bf9fb65..e444949a 100644 --- a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java @@ -5,6 +5,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public interface MessageHandler { void handle(Packet packet, Connection connection); diff --git a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java index f0165b9a..da0bffe6 100644 --- a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java @@ -5,6 +5,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public interface PacketReceiver { void onReceive(Packet packet, Connection connection); diff --git a/mpush-api/src/main/java/com/mpush/api/PushSender.java b/mpush-api/src/main/java/com/mpush/api/PushSender.java index 7beca2e2..83b1d519 100644 --- a/mpush-api/src/main/java/com/mpush/api/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/PushSender.java @@ -4,6 +4,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public interface PushSender { void send(String content, Collection userIds, Callback callback); diff --git a/mpush-api/src/main/java/com/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/Server.java index 1130b921..f9785345 100644 --- a/mpush-api/src/main/java/com/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/mpush/api/Server.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn */ public interface Server { @@ -14,8 +16,8 @@ public interface Server { boolean isRunning(); interface Listener { - void onSuccess(); + void onSuccess(int port); - void onFailure(String message); + void onFailure(Throwable cause); } } diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java index 91732e2b..e365f656 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public interface Cipher { diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index 6313a8d3..aa94e9bd 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public final class SessionContext { public String osName; diff --git a/mpush-api/src/main/java/com/mpush/api/event/Event.java b/mpush-api/src/main/java/com/mpush/api/event/Event.java index 08c72cbf..e1043d2d 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/Event.java +++ b/mpush-api/src/main/java/com/mpush/api/event/Event.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/29. + * + * @author ohun@live.cn */ public interface Event { } diff --git a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java index 17cf2bb7..d7cafa10 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java @@ -4,6 +4,8 @@ /** * Created by ohun on 2015/12/29. + * + * @author ohun@live.cn */ public final class HandshakeEvent implements Event { public final Connection connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java index eb33f83e..bcb2608b 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/29. + * + * @author ohun@live.cn */ public final class KickUserEvent implements Event { public final String userId; diff --git a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java index add6cdaa..926bb71d 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java @@ -4,6 +4,8 @@ /** * Created by ohun on 2016/1/4. + * + * @author ohun@live.cn */ public final class RouterChangeEvent implements Event { public final String userId; diff --git a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java index 39170d30..add6460c 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java @@ -2,13 +2,15 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public class CryptoException extends RuntimeException { - - private static final long serialVersionUID = 368277451733324220L; - public CryptoException(String message) { + private static final long serialVersionUID = 368277451733324220L; + + public CryptoException(String message) { super(message); } - + } diff --git a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java index fc28a125..ce7f8435 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public class DecodeException extends RuntimeException { public DecodeException(String message) { diff --git a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java index fe30b371..62080127 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public class SendMessageException extends RuntimeException { } diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index c6c5fcb0..b800417c 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -6,6 +6,8 @@ /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) + * + * @author ohun@live.cn */ public final class Packet { public static final int HEADER_LEN = 13; diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index b0652758..c7267088 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -4,6 +4,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class ClientLocation { diff --git a/mpush-api/src/main/java/com/mpush/api/router/Router.java b/mpush-api/src/main/java/com/mpush/api/router/Router.java index ef3c6629..f5b1582c 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/Router.java +++ b/mpush-api/src/main/java/com/mpush/api/router/Router.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public interface Router { diff --git a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index c7eebb4a..70dd37ae 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public interface RouterManager { diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 1b65ce50..c5eb3d3f 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -8,7 +8,7 @@ 4.0.0 ${mpush.groupId} mpush-boot - ${mpush-cs-version} + ${mpush-boot-version} mpush-boot jar @@ -34,7 +34,7 @@ boot - ../conf-${deploy.env}.properties + ../conf/conf-${deploy.env}.properties @@ -89,5 +89,4 @@ - diff --git a/mpush-boot/src/main/java/com/mpush/Main.java b/mpush-boot/src/main/java/com/mpush/Main.java index 82f2bff7..295480e4 100644 --- a/mpush-boot/src/main/java/com/mpush/Main.java +++ b/mpush-boot/src/main/java/com/mpush/Main.java @@ -1,8 +1,11 @@ package com.mpush; +import com.mpush.tools.ConsoleLog; + public class Main { public static void main(String[] args) { + ConsoleLog.i("launch app..."); ServerLauncher launcher = new ServerLauncher(); launcher.start(); addHook(launcher); @@ -12,6 +15,7 @@ private static void addHook(final ServerLauncher serverBoot) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { serverBoot.stop(); + ConsoleLog.i("jvm exit all server stopped..."); } }); } diff --git a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java index b8c3972f..1c246c5e 100644 --- a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java @@ -1,14 +1,19 @@ package com.mpush; -import com.mpush.boot.*; -import com.mpush.zk.ZKServerNode; import com.mpush.api.Server; +import com.mpush.boot.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.GatewayServer; import com.mpush.tools.config.ConfigCenter; +import com.mpush.zk.ZKServerNode; +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ public class ServerLauncher { private final ZKServerNode csNode = ZKServerNode.csNode(); @@ -18,7 +23,7 @@ public class ServerLauncher { private final Server gatewayServer = new GatewayServer(gsNode.getPort()); - private final Server adminServer = new AdminServer(ConfigCenter.holder.adminPort()); + private final Server adminServer = new AdminServer(ConfigCenter.I.adminPort()); public void start() { @@ -29,7 +34,9 @@ public void start() { .setNext(new ServerBoot(connectServer, csNode)) .setNext(new ServerBoot(gatewayServer, gsNode)) .setNext(new ServerBoot(adminServer, null)) - .setNext(new EndBoot()); + .setNext(new HttpProxyBoot()) + .setNext(new MonitorBoot()) + .setNext(new LastBoot()); chain.run(); } diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java b/mpush-boot/src/main/java/com/mpush/boot/BootChain.java index 3ffdfde9..fb4183f4 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/boot/BootChain.java @@ -1,5 +1,7 @@ package com.mpush.boot; +import com.mpush.tools.ConsoleLog; + /** * Created by yxx on 2016/5/15. * @@ -20,6 +22,7 @@ private BootJob first() { return new BootJob() { @Override public void run() { + ConsoleLog.i("begin run boot chain..."); next(); } }; diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java b/mpush-boot/src/main/java/com/mpush/boot/BootJob.java index f2c8272c..4273651d 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/boot/BootJob.java @@ -1,5 +1,7 @@ package com.mpush.boot; +import com.mpush.tools.ConsoleLog; + /** * Created by yxx on 2016/5/14. * @@ -12,6 +14,7 @@ public abstract class BootJob { public void next() { if (next != null) { + ConsoleLog.i("run next boot [" + next.getClass().getSimpleName() + "]"); next.run(); } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/EndBoot.java b/mpush-boot/src/main/java/com/mpush/boot/EndBoot.java deleted file mode 100644 index fbaa81c4..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/EndBoot.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.mpush.boot; - -import com.mpush.common.manage.user.UserManager; -import com.mpush.monitor.service.MonitorDataCollector; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.manage.DnsMappingManage; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class EndBoot extends BootJob { - @Override - public void run() { - UserManager.INSTANCE.clearUserOnlineData(); - if (ConfigCenter.holder.httpProxyEnable()) { - DnsMappingManage.holder.init(); - } - MonitorDataCollector.start(ConfigCenter.holder.skipDump()); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java new file mode 100644 index 00000000..850df861 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java @@ -0,0 +1,19 @@ +package com.mpush.boot; + +import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.dns.manage.DnsMappingManage; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class HttpProxyBoot extends BootJob { + @Override + void run() { + if (ConfigCenter.I.httpProxyEnable()) { + DnsMappingManage.holder.init(); + } + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java b/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java new file mode 100644 index 00000000..4be3db0c --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java @@ -0,0 +1,18 @@ +package com.mpush.boot; + +import com.mpush.common.manage.user.UserManager; +import com.mpush.tools.ConsoleLog; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class LastBoot extends BootJob { + @Override + public void run() { + UserManager.INSTANCE.clearUserOnlineData(); + ConsoleLog.i("end run boot chain..."); + ConsoleLog.i("app start success..."); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java new file mode 100644 index 00000000..be85f539 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java @@ -0,0 +1,17 @@ +package com.mpush.boot; + +import com.mpush.monitor.service.MonitorDataCollector; +import com.mpush.tools.config.ConfigCenter; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class MonitorBoot extends BootJob { + @Override + void run() { + MonitorDataCollector.start(ConfigCenter.I.skipDump()); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java index 32bfefae..c13efa35 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java @@ -1,15 +1,17 @@ package com.mpush.boot; import com.google.common.base.Strings; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; +import com.mpush.tools.ConsoleLog; import com.mpush.tools.Jsons; import com.mpush.tools.config.ConfigCenter; import com.mpush.tools.redis.RedisGroup; import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.zk.ZKClient; import java.util.List; +import static com.mpush.zk.ZKPath.REDIS_SERVER; + /** * Created by yxx on 2016/5/14. * @@ -19,19 +21,26 @@ public class RedisBoot extends BootJob { @Override public void run() { - List groupList = ConfigCenter.holder.redisGroups(); - if (groupList.isEmpty()) throw new RuntimeException("init redis sever ex"); - boolean exist = ZKClient.I.isExisted(ZKPath.REDIS_SERVER.getPath()); - String rawGroup = ZKClient.I.get(ZKPath.REDIS_SERVER.getPath()); - if (!exist || Strings.isNullOrEmpty(rawGroup)) { - ZKClient.I.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - //强刷 - boolean forceWriteRedisGroupInfo = ConfigCenter.holder.forceWriteRedisGroupInfo(); - if (forceWriteRedisGroupInfo) { - ZKClient.I.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); + List groupList = ConfigCenter.I.redisGroups(); + if (groupList.size() > 0) { + if (ConfigCenter.I.forceWriteRedisGroupInfo()) { + register(groupList); + } else if (!ZKClient.I.isExisted(REDIS_SERVER.getPath())) { + register(groupList); + } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getPath()))) { + register(groupList); + } + } else { + throw new RuntimeException("init redis sever fail groupList is null"); } + RedisManage.test(groupList); next(); } + + private void register(List groupList) { + String data = Jsons.toJson(groupList); + ZKClient.I.registerPersist(REDIS_SERVER.getPath(), data); + ConsoleLog.i("register redis server group success, group=" + data); + } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java index 5fce0514..ff80675d 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java @@ -1,10 +1,11 @@ package com.mpush.boot; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKServerNode; import com.mpush.api.Server; +import com.mpush.tools.ConsoleLog; import com.mpush.tools.Jsons; import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKServerNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +15,7 @@ * @author ohun@live.cn */ public class ServerBoot extends BootJob { - private final Logger log = LoggerFactory.getLogger(ServerBoot.class); + private final Logger logger = LoggerFactory.getLogger(ServerBoot.class); private final Server server; private final ZKServerNode node; @@ -26,14 +27,17 @@ public ServerBoot(Server server, ZKServerNode node) { @Override public void run() { - ThreadPoolManager.newThread(server.getClass().getSimpleName(), new Runnable() { + final String serverName = server.getClass().getSimpleName(); + ThreadPoolManager.newThread(serverName, new Runnable() { @Override public void run() { server.init(); server.start(new Server.Listener() { @Override - public void onSuccess() { - log.error("mpush app start " + server.getClass().getSimpleName() + " server success...."); + public void onSuccess(int port) { + String msg = "start " + serverName + " success listen:" + port; + logger.error(msg); + ConsoleLog.i(msg); if (node != null) { registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } @@ -41,9 +45,10 @@ public void onSuccess() { } @Override - public void onFailure(String message) { - log.error("mpush app start " + server.getClass().getSimpleName() - + " server failure, jvm exit with code -1"); + public void onFailure(Throwable cause) { + String msg = "start " + serverName + " failure, jvm exit with code -1"; + logger.error(msg); + ConsoleLog.e(cause, msg); System.exit(-1); } }); @@ -54,6 +59,8 @@ public void onFailure(String message) { //step7 注册应用到zk public void registerServerToZk(String path, String value) { ZKClient.I.registerEphemeralSequential(path, value); - log.error("register server to zk:{},{}", path, value); + String msg = "register server node=" + value + " to zk path=" + path; + logger.error(msg); + ConsoleLog.i(msg); } } diff --git a/mpush-boot/src/main/resources/META-INF/services/com.shinemo.mpush.common.manage.ServerManage b/mpush-boot/src/main/resources/META-INF/services/com.shinemo.mpush.common.manage.ServerManage deleted file mode 100644 index afcb1228..00000000 --- a/mpush-boot/src/main/resources/META-INF/services/com.shinemo.mpush.common.manage.ServerManage +++ /dev/null @@ -1 +0,0 @@ -connectionServerManage=com.shinemo.mpush.cs.manage.impl.ConnectionServerManage diff --git a/mpush-boot/src/main/resources/config.properties b/mpush-boot/src/main/resources/config.properties index 6e1c5d8b..27237da8 100644 --- a/mpush-boot/src/main/resources/config.properties +++ b/mpush-boot/src/main/resources/config.properties @@ -28,8 +28,6 @@ zk_ip = ${zk_ip} zk_namespace = ${zk_namespace} zk_digest = ${zk_digest} - -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo redis_group = ${redis_group} force_write_redis_group_info = ${force_write_redis_group_info} jvm_log_path = /opt/shinemo/mpush/ diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java index 330948c6..52c93350 100644 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java +++ b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java @@ -1,6 +1,5 @@ package com.mpush.zk; -import com.mpush.zk.ZKClient; import org.junit.Test; @@ -8,9 +7,7 @@ public class ZkTest { @Test public void remove() { - ZKClient zkRegister = new ZKClient(); - - zkRegister.init(); + ZKClient zkRegister = ZKClient.I; zkRegister.remove("/"); diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java index 98fad0b3..8f49408a 100644 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java +++ b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java @@ -1,10 +1,9 @@ package com.mpush.zk; import com.google.common.collect.Lists; -import com.mpush.tools.Constants; -import com.mpush.tools.redis.RedisGroup; import com.mpush.tools.Jsons; import com.mpush.tools.MPushUtil; +import com.mpush.tools.redis.RedisGroup; import com.mpush.tools.redis.RedisNode; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -19,7 +18,6 @@ public class ZkUtilTest { @Before public void setUp() throws Exception { - zkUtil = new ZKClient(); } @Test @@ -58,7 +56,7 @@ public void testRegister() { String path = "/" + zkUtil.getZKConfig().getNamespace(); - String prefix = Constants.ZK_REGISTER_PREFIX_NAME; + String prefix = "machine"; List hosts = zkUtil.getChildrenKeys(path); @@ -109,7 +107,7 @@ public void testRemove() { @Test public void testAddKickOff() { String localIp = MPushUtil.getInetAddress(); - String kick = Constants.ZK_KICK; + String kick = "kickoff"; String ip = "10.1.10.65"; zkUtil.registerEphemeral("/" + localIp + "/" + kick, ip); diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 2ac6b833..54440364 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -28,7 +28,7 @@ mpush-zk
- com.mpush + ${mpush.groupId} mpush-common
diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java index 79602b36..697d70bc 100644 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java @@ -31,6 +31,8 @@ /** * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn */ @ChannelHandler.Sharable public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/push/PushRequest.java index baa71a7d..cda87e8b 100644 --- a/mpush-client/src/main/java/com/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/push/PushRequest.java @@ -4,10 +4,10 @@ import com.mpush.api.PushSender; import com.mpush.api.connection.Connection; import com.mpush.api.router.ClientLocation; -import com.mpush.tools.Jsons; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.common.router.ConnectionRouterManager; import com.mpush.common.router.RemoteRouter; +import com.mpush.tools.Jsons; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; @@ -18,6 +18,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public class PushRequest implements PushSender.Callback, Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java index 8460343c..3bfc3b8e 100644 --- a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public class PushRequestBus implements Runnable { diff --git a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java index e2a9ad25..c91d3486 100644 --- a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java @@ -26,6 +26,8 @@ /** * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn */ @ChannelHandler.Sharable public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager index c68945f8..257ea937 100644 --- a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager +++ b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager @@ -1,2 +1 @@ -com.mpush.push.zk.manager.GatewayZKNodeManager -com.shinemo.mpush.push.manage.impl.ConnectZKNodeManager +com.mpush.push.zk.manager.ConnectZKNodeManager diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index e3a9fa1c..867dbf8e 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -2,11 +2,11 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} @@ -28,7 +28,6 @@ ${mpush.groupId} mpush-zk
-
diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java index fa84d09c..1c5e2a5f 100644 --- a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java +++ b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java @@ -1,64 +1,56 @@ package com.mpush.common; -import java.util.List; - - import com.google.common.collect.Lists; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.ConfigCenter; import com.mpush.tools.redis.RedisGroup; import com.mpush.zk.ZKClient; import com.mpush.zk.ZKPath; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; import com.mpush.zk.listener.ZKDataChangeListener; import com.mpush.zk.listener.ZKRedisNodeListener; +import java.util.List; + public abstract class AbstractClient { - + protected List dataChangeListeners = Lists.newArrayList(); - + protected ZKClient zkClient = ZKClient.I; - - public AbstractClient() { - registerListener(new ZKRedisNodeListener()); - } - - - public void registerListener(ZKDataChangeListener listener){ - dataChangeListeners.add(listener); - } - //step1 启动 zk - private void initZK(){ - zkClient.init(); - } - - //step2 获取redis - private void initRedis(){ - boolean exist = zkClient.isExisted(ZKPath.REDIS_SERVER.getPath()); + public AbstractClient() { + registerListener(new ZKRedisNodeListener()); + } + + public void registerListener(ZKDataChangeListener listener) { + dataChangeListeners.add(listener); + } + + //step2 获取redis + private void initRedis() { + boolean exist = zkClient.isExisted(ZKPath.REDIS_SERVER.getPath()); if (!exist) { - List groupList = ConfigCenter.holder.redisGroups(); + List groupList = ConfigCenter.I.redisGroups(); zkClient.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); } - } - - //step3 注册listener - private void registerListeners(){ - for(ZKDataChangeListener listener:dataChangeListeners){ - zkClient.registerListener(listener); - } - } - - //step4 初始化 listener data - private void initListenerData(){ - for(ZKDataChangeListener listener:dataChangeListeners){ - listener.initData(); - } - } - - public void start(){ - initZK(); - initRedis(); - registerListeners(); - initListenerData(); - } + } + + //step3 注册listener + private void registerListeners() { + for (ZKDataChangeListener listener : dataChangeListeners) { + zkClient.registerListener(listener); + } + } + + //step4 初始化 listener data + private void initListenerData() { + for (ZKDataChangeListener listener : dataChangeListeners) { + listener.initData(); + } + } + + public void start() { + initRedis(); + registerListeners(); + initListenerData(); + } } diff --git a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java index f93f19d5..cd881c08 100644 --- a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public enum ErrorCode { OFFLINE(1, "user offline"), diff --git a/mpush-common/src/main/java/com/mpush/common/EventBus.java b/mpush-common/src/main/java/com/mpush/common/EventBus.java index 1291da39..e27184c4 100644 --- a/mpush-common/src/main/java/com/mpush/common/EventBus.java +++ b/mpush-common/src/main/java/com/mpush/common/EventBus.java @@ -6,7 +6,6 @@ import com.google.common.eventbus.SubscriberExceptionHandler; import com.mpush.api.event.Event; import com.mpush.tools.thread.threadpool.ThreadPoolManager; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,6 +13,8 @@ /** * Created by ohun on 2015/12/29. + * + * @author ohun@live.cn */ public class EventBus { private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index df218503..14b0eaf3 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -16,6 +16,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public final class MessageDispatcher implements PacketReceiver { public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index 72522de9..bbd346a0 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -9,6 +9,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public abstract class BaseMessageHandler implements MessageHandler { public abstract T decode(Packet packet, Connection connection); @@ -18,12 +20,12 @@ public abstract class BaseMessageHandler implements MessageHa public void handle(Packet packet, Connection connection) { T t = decode(packet, connection); if (t != null) { - try{ - Profiler.enter("start handle"); - handle(t); - }finally{ - Profiler.release(); - } + try { + Profiler.enter("start handle"); + handle(t); + } finally { + Profiler.release(); + } } } } diff --git a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java index 2e01ddc1..b6c0c5e9 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java @@ -1,11 +1,13 @@ package com.mpush.common.handler; import com.mpush.api.connection.Connection; -import com.mpush.common.message.ErrorMessage; import com.mpush.api.protocol.Packet; +import com.mpush.common.message.ErrorMessage; /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public class ErrorMessageHandler extends BaseMessageHandler { @Override diff --git a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java index ad383984..6271a2cf 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java @@ -6,6 +6,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public class OkMessageHandler extends BaseMessageHandler { @Override diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index d5beeec0..7c056e6e 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -7,13 +7,14 @@ import com.mpush.tools.IOUtils; import com.mpush.tools.Profiler; import com.mpush.tools.config.ConfigCenter; - import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public abstract class BaseMessage implements Message { private static final AtomicInteger ID_SEQ = new AtomicInteger(); @@ -58,7 +59,7 @@ protected void encodeBody() { byte[] tmp = encode(); if (tmp != null && tmp.length > 0) { //1.压缩 - if (tmp.length > ConfigCenter.holder.compressLimit()) { + if (tmp.length > ConfigCenter.I.compressLimit()) { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; diff --git a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index 4f162cb0..4638e36a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -7,6 +7,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public final class BindUserMessage extends ByteBufMessage { public String userId; diff --git a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index 1a233a9d..81e7674a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public abstract class ByteBufMessage extends BaseMessage { diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index 3a70a087..435bb0ad 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -10,6 +10,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public final class ErrorMessage extends ByteBufMessage { public byte cmd; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java index e1bce4c3..e60bd0f7 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class FastConnectMessage extends ByteBufMessage { public String sessionId; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index 6a0a29d0..2e4982c6 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -6,6 +6,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public final class FastConnectOkMessage extends ByteBufMessage { public int heartbeat; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 0851e3b0..6b67aea7 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn */ public final class HandshakeMessage extends ByteBufMessage { public String deviceId; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index e0c75ab7..990cad06 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -6,6 +6,8 @@ /** * Created by ohun on 2015/12/27. + * + * @author ohun@live.cn */ public final class HandshakeOkMessage extends ByteBufMessage { public byte[] serverKey; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index 01a7eea8..bc1edcdd 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -10,6 +10,8 @@ /** * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn */ public class HttpRequestMessage extends ByteBufMessage { public byte method; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 7f9007ac..5b53aa40 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -10,6 +10,8 @@ /** * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn */ public class HttpResponseMessage extends ByteBufMessage { public int statusCode; diff --git a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java index 2436b8cf..3c2293df 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/29. + * + * @author ohun@live.cn */ public class KickUserMessage extends ByteBufMessage { public String deviceId; diff --git a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index cc4fbfe7..b48fbc9b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public final class OkMessage extends ByteBufMessage { public byte cmd; diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index 65ac3236..c8b3b608 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public final class PushMessage extends BaseMessage { diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 95717b9f..328eace3 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -1,8 +1,8 @@ package com.mpush.common.message.gateway; import com.mpush.api.connection.Connection; -import com.mpush.common.message.ByteBufMessage; import com.mpush.api.protocol.Packet; +import com.mpush.common.message.ByteBufMessage; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; @@ -10,6 +10,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public class GatewayPushMessage extends ByteBufMessage { public String userId; diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index a254ab8b..5f1f78f1 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -5,6 +5,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class RemoteRouter implements Router { private final ClientLocation clientLocation; diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 35acb754..510372b5 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -9,6 +9,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public class RemoteRouterManager implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(RemoteRouterManager.class); diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index 7f9c9253..dd1a16db 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -12,6 +12,8 @@ /** * Created by ohun on 2016/1/4. + * + * @author ohun@live.cn */ public class UserChangeListener extends AbstractEventContainer implements MessageListener { @@ -23,11 +25,11 @@ public class UserChangeListener extends AbstractEventContainer implements Messag //只需要一台机器注册online、offline 消息通道 public UserChangeListener() { - if(ConfigCenter.holder.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ + if(ConfigCenter.I.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this); }else{ - log.error("UserChangeListener is not localhost,required:{},but:{}",ConfigCenter.holder.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp()); + log.error("UserChangeListener is not localhost,required:{},but:{}",ConfigCenter.I.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp()); } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 6e4c40b4..2bf17464 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -7,6 +7,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public final class AesCipher implements Cipher { public final byte[] key; @@ -19,22 +21,22 @@ public AesCipher(byte[] key, byte[] iv) { @Override public byte[] decrypt(byte[] data) { - try{ - Profiler.enter("start aes decrypt"); - return AESUtils.decrypt(data, key, iv); - }finally{ - Profiler.release(); - } + try { + Profiler.enter("start aes decrypt"); + return AESUtils.decrypt(data, key, iv); + } finally { + Profiler.release(); + } } @Override public byte[] encrypt(byte[] data) { - try{ - Profiler.enter("start encrypt"); - return AESUtils.encrypt(data, key, iv); - }finally{ - Profiler.release(); - } + try { + Profiler.enter("start encrypt"); + return AESUtils.encrypt(data, key, iv); + } finally { + Profiler.release(); + } } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index c6caf310..d8585cdb 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -9,9 +9,11 @@ /** * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn */ public final class CipherBox { - public int aesKeyLength = ConfigCenter.holder.aesKeyLength(); + public int aesKeyLength = ConfigCenter.I.aesKeyLength(); public static final CipherBox INSTANCE = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; @@ -19,7 +21,7 @@ public final class CipherBox { public RSAPrivateKey getPrivateKey() { if (privateKey == null) { - String key = ConfigCenter.holder.privateKey(); + String key = ConfigCenter.I.privateKey(); try { privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(key); } catch (Exception e) { @@ -31,7 +33,7 @@ public RSAPrivateKey getPrivateKey() { public RSAPublicKey getPublicKey() { if (publicKey == null) { - String key = ConfigCenter.holder.publicKey(); + String key = ConfigCenter.I.publicKey(); try { publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(key); } catch (Exception e) { diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index 6117f65b..2afce590 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -9,6 +9,8 @@ /** * Created by ohun on 2015/12/28. + * + * @author ohun@live.cn */ public final class RsaCipher implements Cipher { private final RSAPrivateKey privateKey; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 4b45c0bf..de400d14 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -16,6 +16,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class BindUserHandler extends BaseMessageHandler { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 9467e57b..95fa895f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -1,22 +1,23 @@ package com.mpush.core.handler; -import com.mpush.core.session.ReusableSession; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.FastConnectMessage; import com.mpush.common.message.FastConnectOkMessage; +import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; import com.mpush.log.LogType; import com.mpush.log.LoggerManage; import com.mpush.tools.MPushUtil; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class FastConnectHandler extends BaseMessageHandler { public static final Logger LOGGER = LoggerFactory.getLogger(FastConnectHandler.class); @@ -25,7 +26,7 @@ public final class FastConnectHandler extends BaseMessageHandler { @@ -30,7 +29,7 @@ public final class GatewayPushHandler extends BaseMessageHandler @@ -74,8 +73,8 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - LoggerManage.info(LogType.PUSH, "gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); - + LoggerManage.info(LogType.PUSH, "gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + //删除已经失效的本地路由 RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); @@ -93,11 +92,11 @@ public void operationComplete(ChannelFuture future) throws Exception { OkMessage.from(message).setData(message.userId).send(); LoggerManage.info(LogType.PUSH, "gateway push message to client success userId={}, content={}", message.userId, message.content); - + } else { //推送失败 ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); - + LoggerManage.info(LogType.PUSH, "gateway push message to client failure userId={}, content={}", message.userId, message.content); } } @@ -133,7 +132,7 @@ private void checkRemote(GatewayPushMessage message) { //删除失效的远程缓存 RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); - + LoggerManage.info(LogType.PUSH, "gateway push error remote is local, userId={}, router={}", message.userId, router); return; @@ -141,7 +140,7 @@ private void checkRemote(GatewayPushMessage message) { //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); - + LoggerManage.info(LogType.PUSH, "gateway push, router in remote userId={}, router={}", message.userId, router); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 72737f32..58f35040 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -23,6 +23,8 @@ /** * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn */ public final class HandshakeHandler extends BaseMessageHandler { public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 99331f8b..cee592f8 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -8,6 +8,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public final class HeartBeatHandler implements MessageHandler { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 338eb527..5ad960d2 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -28,6 +28,8 @@ /** * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn */ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = LoggerManage.getLog(LogType.HTTP); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index 0d427a29..41074ced 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -21,6 +21,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class UnbindUserHandler extends BaseMessageHandler { diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index e876d58a..71f635e9 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2016/1/4. + * + * @author ohun@live.cn */ public final class KickRemoteMsg { public String userId; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index dbb4a2d9..c078ec3f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -5,6 +5,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class LocalRouter implements Router { private final Connection connection; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 598c69b6..391bb377 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -18,6 +18,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index 5ca9d808..b072bb28 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -14,6 +14,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index 81933204..d24e4e06 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -17,12 +17,13 @@ import com.mpush.tools.redis.listener.ListenerDispatcher; import com.mpush.tools.redis.listener.MessageListener; import com.mpush.tools.redis.manage.RedisManage; - import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2016/1/4. + * + * @author ohun@live.cn */ public final class RouterChangeListener extends AbstractEventContainer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 44a325f8..1b514a5d 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -6,14 +6,13 @@ import com.mpush.common.EventBus; import com.mpush.common.manage.user.UserManager; import com.mpush.tools.redis.manage.RedisManage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class UserOnlineOfflineListener { - public static final Logger LOGGER = LoggerFactory.getLogger(UserOnlineOfflineListener.class); public static final String ONLINE_CHANNEL = "/mpush/online/"; diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index 565b055a..f4fbf926 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -2,115 +2,40 @@ import com.mpush.core.handler.AdminHandler; import com.mpush.netty.server.NettyServer; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.ServerChannel; -import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.thread.threadpool.ThreadPoolManager; - public final class AdminServer extends NettyServer { - private static final Logger log = LoggerFactory.getLogger(AdminServer.class); - - private EventLoopGroup bossGroup; - private EventLoopGroup workerGroup; - - private final int port; - - private AdminHandler adminHandler = new AdminHandler(); - - public AdminServer(int port) { - super(port); - this.port = port; - } - - @Override - public void start(Listener listener) { - if (!serverState.compareAndSet(State.Initialized, State.Starting)) { - throw new IllegalStateException("Server already started or have not init"); - } - createNioServer(listener); - } - - @Override - public ChannelHandler getChannelHandler() { - return adminHandler; - } + private final AdminHandler adminHandler = new AdminHandler(); - private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolManager.bossExecutor); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); - - createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); - } - - private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { - this.bossGroup = boss; - this.workerGroup = work; - try { - - ServerBootstrap b = new ServerBootstrap(); - b.group(bossGroup, workerGroup) - .channel(clazz) - .childHandler(new ChannelInitializer() { - @Override - protected void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); - ch.pipeline().addLast("decoder", new StringDecoder()); - ch.pipeline().addLast("encoder", new StringEncoder()); - ch.pipeline().addLast("handler", getChannelHandler()); - } - }); + public AdminServer(int port) { + super(port); + } + @Override + protected void initPipeline(ChannelPipeline pipeline) { + pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); + super.initPipeline(pipeline); + } - /*** - * 绑定端口并启动去接收进来的连接 - */ - ChannelFuture f = b.bind(port).sync().addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - log.info("server start success on:" + port); - if (listener != null) listener.onSuccess(); - } else { - log.error("server start failure on:" + port); - if (listener != null) listener.onFailure("start server failure"); - } - } - }); -// f.await(); - if (f.isSuccess()) { - serverState.set(State.Started); - /** - * 这里会一直等待,直到socket被关闭 - */ - f.channel().closeFuture().sync(); - } + @Override + public ChannelHandler getChannelHandler() { + return adminHandler; + } - } catch (Exception e) { - log.error("server start exception", e); - if (listener != null) listener.onFailure("start server ex=" + e.getMessage()); - throw new RuntimeException("server start exception, port=" + port, e); - } finally { - /*** - * 优雅关闭 - */ - stop(null); - } + @Override + protected ChannelHandler getDecoder() { + return new StringDecoder(); } + @Override + protected ChannelHandler getEncoder() { + return new StringEncoder(); + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 8e692541..dd5a5398 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -38,7 +38,7 @@ public void init() { receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - if (ConfigCenter.holder.httpProxyEnable()) { + if (ConfigCenter.I.httpProxyEnable()) { httpClient = new NettyHttpClient(); receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 7fe3ebf8..6ec0ce7f 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -1,14 +1,16 @@ package com.mpush.core.server; +import com.mpush.api.protocol.Command; +import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; import com.mpush.netty.connection.NettyConnectionManager; import com.mpush.netty.server.NettyServer; -import com.mpush.api.protocol.Command; -import com.mpush.common.MessageDispatcher; import io.netty.channel.ChannelHandler; /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn */ public final class GatewayServer extends NettyServer { diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index ad84b60b..a9dd523a 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -21,6 +21,8 @@ /** * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn */ @ChannelHandler.Sharable public final class ServerChannelHandler extends ChannelHandlerAdapter { diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java index 98bdecfb..7a8e2f72 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java @@ -5,6 +5,8 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class ReusableSession { public String sessionId; diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index 3d87b0c0..1fbf2c78 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -9,10 +9,12 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private int expiredTime = ConfigCenter.holder.sessionExpiredTime(); + private int expiredTime = ConfigCenter.I.sessionExpiredTime(); public boolean cacheSession(ReusableSession session) { String key = RedisKey.getSessionKey(session.sessionId); diff --git a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java index 93640774..f51ae52f 100644 --- a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java @@ -6,6 +6,8 @@ /** * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn */ public class NettyServerTest { @Test @@ -14,13 +16,11 @@ public void testStart() throws Exception { server.init(); server.start(new Server.Listener() { @Override - public void onSuccess() { - + public void onSuccess(int port) { } @Override - public void onFailure(String message) { - + public void onFailure(Throwable cause) { } }); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java index eb4d0532..43b27000 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java @@ -5,6 +5,8 @@ /** * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn */ public interface HttpCallback { diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java index d9aa0707..014e0ba0 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn */ public interface HttpClient { diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index ed567ded..7929ff42 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -80,7 +80,7 @@ public int inceaseAndGetHbTimes() { @Override public void startHeartBeat() throws Exception { - startHeartBeat((int)ConfigCenter.holder.scanConnTaskCycle()/1000); + startHeartBeat((int)ConfigCenter.I.scanConnTaskCycle()/1000); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java index 772f419f..d7677c43 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java @@ -26,11 +26,13 @@ /** * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn */ public class NettyHttpClient implements HttpClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); - private final int maxConnPerHost = ConfigCenter.holder.maxHttpConnCountPerHost(); + private final int maxConnPerHost = ConfigCenter.I.maxHttpConnCountPerHost(); private final AttributeKey requestKey = AttributeKey.newInstance("request"); private final AttributeKey hostKey = AttributeKey.newInstance("host"); private final ArrayListMultimap channelPool = ArrayListMultimap.create(); diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java index e7efb94e..28c61ee4 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java @@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class RequestInfo implements TimerTask, HttpCallback { - private static final int TIMEOUT = ConfigCenter.holder.httpDefaultReadTimeout(); + private static final int TIMEOUT = ConfigCenter.I.httpDefaultReadTimeout(); final AtomicBoolean cancelled = new AtomicBoolean(false); final long startTime = System.currentTimeMillis(); long endTime = startTime; diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index a4ba8cae..764bf760 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -13,6 +13,8 @@ /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) + * + * @author ohun@live.cn */ public final class PacketDecoder extends ByteToMessageDecoder { @@ -63,7 +65,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { - if (bodyLength > ConfigCenter.holder.maxPacketSize()) { + if (bodyLength > ConfigCenter.I.maxPacketSize()) { throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); } body = new byte[bodyLength]; diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index 641120de..57a887a7 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -10,6 +10,8 @@ /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) + * + * @author ohun@live.cn */ @ChannelHandler.Sharable public final class PacketEncoder extends MessageToByteEncoder { diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index ebc0fb9e..0b1a12e2 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -4,16 +4,16 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; import com.mpush.common.security.CipherBox; - import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public final class NettyConnection implements Connection, ChannelFutureListener { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); @@ -22,8 +22,8 @@ public final class NettyConnection implements Connection, ChannelFutureListener private Channel channel; private volatile int status = STATUS_NEW; private long lastReadTime; - private long lastWriteTime; - + private long lastWriteTime; + private int hbTimes = 0; @Override @@ -67,7 +67,7 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { } } else { return this.close(); - + } } @@ -85,7 +85,7 @@ public boolean isConnected() { @Override public boolean heartbeatTimeout() { - long between = System.currentTimeMillis() - lastReadTime; + long between = System.currentTimeMillis() - lastReadTime; return context.heartbeat > 0 && between > context.heartbeat; } @@ -93,10 +93,10 @@ public boolean heartbeatTimeout() { public void updateLastReadTime() { lastReadTime = System.currentTimeMillis(); } - + @Override - public long getLastReadTime(){ - return lastReadTime; + public long getLastReadTime() { + return lastReadTime; } @Override @@ -107,11 +107,11 @@ public void operationComplete(ChannelFuture future) throws Exception { LOGGER.error("send msg error"); } } - - public void updateLastWriteTime(){ - lastWriteTime = System.currentTimeMillis(); + + public void updateLastWriteTime() { + lastWriteTime = System.currentTimeMillis(); } - + @Override public int inceaseAndGetHbTimes() { return ++hbTimes; @@ -122,16 +122,16 @@ public void resetHbTimes() { hbTimes = 0; } - @Override - public String toString() { - return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes - + "]"; - } + @Override + public String toString() { + return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes + + "]"; + } + + @Override + public Channel getChannel() { + return channel; + } - @Override - public Channel getChannel() { - return channel; - } - } diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java index 2982fb82..691268d0 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java @@ -23,6 +23,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public final class NettyConnectionManager implements ConnectionManager { //可能会有20w的链接数 @@ -34,7 +36,7 @@ public final class NettyConnectionManager implements ConnectionManager { public void init() { //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s - int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration); + int ticksPerWheel = (int) (ConfigCenter.I.maxHeartbeat() / tickDuration); this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); EventBus.INSTANCE.register(this); } @@ -88,7 +90,7 @@ public HeartbeatCheckTask(Connection connection) { public void startTimeout() { int timeout = connection.getSessionContext().heartbeat; - timer.newTimeout(this, timeout > 0 ? timeout : ConfigCenter.holder.minHeartbeat(), TimeUnit.MILLISECONDS); + timer.newTimeout(this, timeout > 0 ? timeout : ConfigCenter.I.minHeartbeat(), TimeUnit.MILLISECONDS); } @Override @@ -98,7 +100,7 @@ public void run(Timeout timeout) throws Exception { return; } if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) { + if (++expiredTimes > ConfigCenter.I.maxHBTimeoutTimes()) { connection.close(); LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); return; diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index f0412028..db065085 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -4,7 +4,6 @@ import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; import com.mpush.tools.thread.threadpool.ThreadPoolManager; - import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -13,7 +12,6 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,18 +19,20 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn */ public abstract class NettyServer implements Server { - private static final Logger log = LoggerFactory.getLogger(NettyServer.class); + private final Logger logger = LoggerFactory.getLogger(this.getClass()); public enum State {Created, Initialized, Starting, Started, Shutdown} protected final AtomicReference serverState = new AtomicReference<>(State.Created); - private final int port; - private EventLoopGroup bossGroup; - private EventLoopGroup workerGroup; + protected final int port; + protected EventLoopGroup bossGroup; + protected EventLoopGroup workerGroup; public NettyServer(int port) { this.port = port; @@ -52,11 +52,16 @@ public boolean isRunning() { @Override public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { - throw new IllegalStateException("The server is already shutdown."); + IllegalStateException e = new IllegalStateException("server was already shutdown."); + if (listener != null) listener.onFailure(e); + throw e; } if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); - log.error("netty server stop now"); + logger.error("netty server stop now"); + if (listener != null) { + listener.onSuccess(port); + } } @Override @@ -82,12 +87,10 @@ private void createServer(final Listener listener, EventLoopGroup boss, EventLoo this.workerGroup = work; try { - /** * ServerBootstrap 是一个启动NIO服务的辅助启动类 * 你可以在这个服务中直接使用Channel */ - ServerBootstrap b = new ServerBootstrap(); /** @@ -114,9 +117,7 @@ private void createServer(final Listener listener, EventLoopGroup boss, EventLoo b.childHandler(new ChannelInitializer() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast("decoder", new PacketDecoder()); - ch.pipeline().addLast("encoder", PacketEncoder.INSTANCE); - ch.pipeline().addLast("handler", getChannelHandler()); + initPipeline(ch.pipeline()); } }); @@ -129,16 +130,16 @@ public void initChannel(SocketChannel ch) throws Exception { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - log.error("server start success on:" + port); - if (listener != null) listener.onSuccess(); + logger.error("server start success on:" + port); + if (listener != null) listener.onSuccess(port); } else { - log.error("server start failure on:" + port); - if (listener != null) listener.onFailure("start server failure"); + logger.error("server start failure on:" + port, future.cause()); + if (listener != null) listener.onFailure(future.cause()); } } }); if (f.isSuccess()) { - serverState.set(State.Started); + serverState.set(State.Started); /** * 这里会一直等待,直到socket被关闭 */ @@ -146,8 +147,8 @@ public void operationComplete(ChannelFuture future) throws Exception { } } catch (Exception e) { - log.error("server start exception", e); - if (listener != null) listener.onFailure("start server ex=" + e.getMessage()); + logger.error("server start exception", e); + if (listener != null) listener.onFailure(e); throw new RuntimeException("server start exception, port=" + port, e); } finally { /*** @@ -193,4 +194,18 @@ protected void initOptions(ServerBootstrap b) { public abstract ChannelHandler getChannelHandler(); + + protected ChannelHandler getDecoder() { + return new PacketDecoder(); + } + + protected ChannelHandler getEncoder() { + return PacketEncoder.INSTANCE; + } + + protected void initPipeline(ChannelPipeline pipeline) { + pipeline.addLast("decoder", getDecoder()); + pipeline.addLast("encoder", getEncoder()); + pipeline.addLast("handler", getChannelHandler()); + } } diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 8ef2504d..61ae1c36 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -14,13 +14,13 @@ public class ConfigCenterTest { @Test public void test(){ - System.out.println(ConfigCenter.holder.forceWriteRedisGroupInfo()); + System.out.println(ConfigCenter.I.forceWriteRedisGroupInfo()); } @Test public void testDnsMapping(){ - Map> ret = ConfigCenter.holder.dnsMapping(); + Map> ret = ConfigCenter.I.dnsMapping(); System.out.println(Jsons.toJson(ret)); diff --git a/mpush-test/src/test/java/com/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/Main.java index dd5a5acb..e5398b2a 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/push/Main.java @@ -1,8 +1,8 @@ package com.mpush.test.push; import com.mpush.api.PushContent; -import com.mpush.api.PushSender; import com.mpush.api.PushContent.PushType; +import com.mpush.api.PushSender; import com.mpush.push.PushClient; import com.mpush.tools.Jsons; @@ -11,6 +11,8 @@ /** * Created by ohun on 2016/1/7. + * + * @author ohun@live.cn */ public class Main { public static void main(String[] args) throws Exception { @@ -18,8 +20,8 @@ public static void main(String[] args) throws Exception { client.start(); Thread.sleep(1000); for (int i = 0; i < 100; i++) { - PushContent content = PushContent.build(PushType.MESSAGE,"this a first push." + i); - content.setMsgId("msgId_" + (i % 2)); + PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); + content.setMsgId("msgId_" + (i % 2)); client.send(Jsons.toJson(content), Arrays.asList("huang1", "huang2", "huang"), new PushSender.Callback() { @Override diff --git a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java b/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java new file mode 100644 index 00000000..27497d15 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java @@ -0,0 +1,32 @@ +package com.mpush.tools; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public final class ConsoleLog { + private static final String TAG = "[mpush] "; + private static final DateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); + + public static void d(String s, Object... args) { + System.out.printf(format.format(new Date()) + " [D] " + TAG + s + '\n', args); + } + + public static void i(String s, Object... args) { + System.out.printf(format.format(new Date()) + " [I] " + TAG + s + '\n', args); + } + + public static void w(String s, Object... args) { + System.err.printf(format.format(new Date()) + " [W] " + TAG + s + '\n', args); + } + + public static void e(Throwable e, String s, Object... args) { + System.err.printf(format.format(new Date()) + " [E] " + TAG + s + '\n', args); + e.printStackTrace(); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/mpush/tools/Constants.java index 1afef995..458c6068 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Constants.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Constants.java @@ -4,17 +4,13 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); byte[] EMPTY_BYTES = new byte[0]; - String JVM_LOG_PATH = "/opt/logs/mpush/"; - - int THREAD_QUEUE_SIZE = 10000; - int MIN_POOL_SIZE = 200; - int MAX_POOL_SIZE = 500; - int MIN_BOSS_POOL_SIZE = 50; int MAX_BOSS_POOL_SIZE = 100; int BOSS_THREAD_QUEUE_SIZE = 10000; @@ -22,42 +18,24 @@ public interface Constants { int MIN_WORK_POOL_SIZE = 50; int MAX_WORK_POOL_SIZE = 150; int WORK_THREAD_QUEUE_SIZE = 10000; - + int BIZ_POOL_SIZE = 20; - + int EVENT_BUS_POOL_SIZE = 10; - + int REDIS_POOL_SIZE = 3; int REDIS_THREAD_QUEUE_SIZE = 10000; - - int ZK_POOL_SIZE = 3; - int ZK_THREAD_QUEUE_SIZE = 10000; - - //zk - int ZK_MAX_RETRY = 3; - int ZK_MIN_TIME = 5000; - int ZK_MAX_TIME = 5000; - int ZK_SESSION_TIMEOUT = 5000; - int ZK_CONNECTION_TIMEOUT = 5000; - String ZK_DEFAULT_CACHE_PATH = "/"; - String ZK_DEFAULT_DIGEST = "shinemo"; - - //zk cs - //所有机器启动的时候注册ip的地方 - String ZK_REGISTER_HOST = "/allhost"; - String ZK_REGISTER_PREFIX_NAME = "machine"; - String ZK_KICK = "kickoff"; - + //redis int REDIS_TIMEOUT = 2000; int REDIS_MAX_TOTAL = 8; int REDIS_MAX_IDLE = 4; int REDIS_MIN_IDLE = 1; - int REDIS_MAX_WAITMILLIS = 5000; - int REDIS_MIN_EVICTABLEIDLETIMEMILLIS = 300000; - int REDIS_NUMTESTSPEREVICTIONRUN = 3; - int REDIS_TIMEBETWEENEVICTIONRUNMILLIS = 60000; - boolean REDIS_TESTONBORROW = false; - boolean REDIS_TESTONRETURN = false; - boolean REDIS_TESTWHILEIDLE = false; + int REDIS_MAX_WAIT_MILLIS = 5000; + int REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 300000; + int REDIS_NUM_TESTS_PER_EVICTION_RUN = 3; + int REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; + boolean REDIS_TEST_ON_BORROW = false; + boolean REDIS_TEST_ON_RETURN = false; + boolean REDIS_TEST_WHILE_IDLE = false; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java index 78c57ebe..f322e8f8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java @@ -12,6 +12,8 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class IOUtils { private static final Logger LOGGER = LoggerFactory.getLogger(IOUtils.class); diff --git a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java index e30020fc..5e846c06 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java @@ -15,6 +15,8 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class MPushUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); @@ -42,8 +44,8 @@ public static String getLocalIp() { public static int getHeartbeat(int min, int max) { return Math.max( - ConfigCenter.holder.minHeartbeat(), - Math.min(max, ConfigCenter.holder.maxHeartbeat()) + ConfigCenter.I.minHeartbeat(), + Math.min(max, ConfigCenter.I.maxHeartbeat()) ); } @@ -107,7 +109,7 @@ public static String getExtranetAddress() { String localIp = getInetAddress(); String remoteIp = null; - Map mapping = ConfigCenter.holder.remoteIpMapping(); + Map mapping = ConfigCenter.I.remoteIpMapping(); if (mapping != null) { remoteIp = mapping.get(localIp); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/Pair.java index a255ca57..23019945 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Pair.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Pair.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn */ public final class Pair { public final K key; diff --git a/mpush-tools/src/main/java/com/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/mpush/tools/Strings.java index 5b52ec17..c81fc531 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Strings.java @@ -2,6 +2,8 @@ /** * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn */ public final class Strings { public static final String EMPTY = ""; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java index e5369001..29d7588a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java @@ -19,7 +19,7 @@ }) public interface ConfigCenter extends Config { - ConfigCenter holder = ConfigFactory.create(ConfigCenter.class); + ConfigCenter I = ConfigFactory.create(ConfigCenter.class); /** * 最大包长度 diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index 0279c503..bc9eb644 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -1,7 +1,5 @@ package com.mpush.tools.crypto; -import java.security.SecureRandom; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,9 +8,12 @@ import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import java.security.SecureRandom; /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class AESUtils { private static final Logger LOGGER = LoggerFactory.getLogger(AESUtils.class); diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index 225f3986..02b2ff17 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -13,6 +13,8 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public final class MD5Utils { public static String encrypt(File file) { diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java index 783d2958..905f4e96 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java +++ b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java @@ -34,8 +34,8 @@ private DnsMappingManage() { public void init() { LOG.error("start init dnsMapping"); - all.putAll(ConfigCenter.holder.dnsMapping()); - available.putAll(ConfigCenter.holder.dnsMapping()); + all.putAll(ConfigCenter.I.dnsMapping()); + available.putAll(ConfigCenter.I.dnsMapping()); pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns LOG.error("end init dnsMapping"); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java index 976bd1c8..b1a1e383 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java @@ -16,19 +16,19 @@ public class RedisPoolConfig { //连接池中最少空闲的连接数 config.setMinIdle(Constants.REDIS_MIN_IDLE); //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait - config.setMaxWaitMillis(Constants.REDIS_MAX_WAITMILLIS); + config.setMaxWaitMillis(Constants.REDIS_MAX_WAIT_MILLIS); //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 - config.setMinEvictableIdleTimeMillis(Constants.REDIS_MIN_EVICTABLEIDLETIMEMILLIS); + config.setMinEvictableIdleTimeMillis(Constants.REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS); //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 - config.setNumTestsPerEvictionRun(Constants.REDIS_NUMTESTSPEREVICTIONRUN); + config.setNumTestsPerEvictionRun(Constants.REDIS_NUM_TESTS_PER_EVICTION_RUN); //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 - config.setTimeBetweenEvictionRunsMillis(Constants.REDIS_TIMEBETWEENEVICTIONRUNMILLIS); + config.setTimeBetweenEvictionRunsMillis(Constants.REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. - config.setTestOnBorrow(Constants.REDIS_TESTONBORROW); + config.setTestOnBorrow(Constants.REDIS_TEST_ON_BORROW); //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 - config.setTestOnReturn(Constants.REDIS_TESTONRETURN); + config.setTestOnReturn(Constants.REDIS_TEST_ON_RETURN); //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. - config.setTestWhileIdle(Constants.REDIS_TESTWHILEIDLE); + config.setTestWhileIdle(Constants.REDIS_TEST_WHILE_IDLE); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java index 9436c75d..83d0db82 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java @@ -1,42 +1,62 @@ package com.mpush.tools.spi; +import java.util.Iterator; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; public class ServiceContainer { - private static final Map cache = new ConcurrentHashMap<>(); + private static final Map CACHE = new ConcurrentHashMap<>(); public static T load(Class clazz) { - String name = clazz.getName(); - Object o = cache.get(name); + return load(clazz, null); + } + + public static T load(Class clazz, String name) { + String key = clazz.getName(); + Object o = CACHE.get(key); if (o == null) { - T t = load0(clazz); + T t = load0(clazz, name); if (t != null) { - cache.put(name, t); + CACHE.put(key, t); return t; } } else if (clazz.isInstance(o)) { return (T) o; } - - return load0(clazz); + return load0(clazz, name); } - public static T load0(Class clazz) { + public static T load0(Class clazz, String name) { ServiceLoader factories = ServiceLoader.load(clazz); - if (factories.iterator().hasNext()) { - return factories.iterator().next(); - } else { - // By default ServiceLoader.load uses the TCCL, this may not be enough in environment deading with - // classloaders differently such as OSGi. So we should try to use the classloader having loaded this - // class. In OSGi it would be the bundle exposing vert.x and so have access to all its classes. + T t = filterByName(factories, name); + + if (t == null) { factories = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()); - if (factories.iterator().hasNext()) { - return factories.iterator().next(); - } else { - throw new IllegalStateException("Cannot find META-INF/services/" + clazz.getName() + " on classpath"); + t = filterByName(factories, name); + } + + if (t != null) { + return t; + } else { + throw new IllegalStateException("Cannot find META-INF/services/" + clazz.getName() + " on classpath"); + } + } + + private static T filterByName(ServiceLoader factories, String name) { + Iterator it = factories.iterator(); + if (name == null) { + if (it.hasNext()) { + return it.next(); + } + } else { + while (it.hasNext()) { + T t = factories.iterator().next(); + if (name.equals(t.getClass().getSimpleName())) { + return t; + } } } + return null; } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java index e56b5863..88255500 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java @@ -16,7 +16,7 @@ public class IgnoreRunsPolicy implements RejectedExecutionHandler{ private volatile boolean dump = false; - private static final String preFixPath = ConfigCenter.holder.logPath(); + private static final String preFixPath = ConfigCenter.I.logPath(); private final ThreadPoolContext context; diff --git a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java index a1acb4af..cdab4c60 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java @@ -4,6 +4,8 @@ /** * Created by ohun on 2016/3/8. + * + * @author ohun@live.cn */ public class IOUtilsTest { diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java index 0e5f4e44..84c7eec6 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java @@ -11,6 +11,8 @@ /** * Created by ohun on 2015/12/25. + * + * @author ohun@live.cn */ public class RSAUtilsTest { String publicKey; diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java index 74f74f12..f9907f95 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java @@ -32,39 +32,39 @@ public void test1(){ @Test public void test2(){ - System.out.println(ConfigCenter.holder.zkIp()); + System.out.println(ConfigCenter.I.zkIp()); - System.out.println("aesKeyLength:"+ConfigCenter.holder.aesKeyLength()); + System.out.println("aesKeyLength:"+ConfigCenter.I.aesKeyLength()); - System.out.println("compressLimit:"+ConfigCenter.holder.compressLimit()); + System.out.println("compressLimit:"+ConfigCenter.I.compressLimit()); - System.out.println("connectionServerPort:"+ConfigCenter.holder.connectionServerPort()); + System.out.println("connectionServerPort:"+ConfigCenter.I.connectionServerPort()); - System.out.println("gatewayServerPort:"+ConfigCenter.holder.gatewayServerPort()); + System.out.println("gatewayServerPort:"+ConfigCenter.I.gatewayServerPort()); - System.out.println("maxHBTimeoutTimes:"+ConfigCenter.holder.maxHBTimeoutTimes()); + System.out.println("maxHBTimeoutTimes:"+ConfigCenter.I.maxHBTimeoutTimes()); - System.out.println(ConfigCenter.holder.maxHeartbeat()); + System.out.println(ConfigCenter.I.maxHeartbeat()); - System.out.println(ConfigCenter.holder.maxPacketSize()); + System.out.println(ConfigCenter.I.maxPacketSize()); - System.out.println(ConfigCenter.holder.minHeartbeat()); + System.out.println(ConfigCenter.I.minHeartbeat()); - System.out.println(ConfigCenter.holder.privateKey()); + System.out.println(ConfigCenter.I.privateKey()); - System.out.println(ConfigCenter.holder.publicKey()); + System.out.println(ConfigCenter.I.publicKey()); - System.out.println(ConfigCenter.holder.rsaKeyLength()); + System.out.println(ConfigCenter.I.rsaKeyLength()); - System.out.println(ConfigCenter.holder.redisIp()); + System.out.println(ConfigCenter.I.redisIp()); - System.out.println(ConfigCenter.holder.sessionExpiredTime()); + System.out.println(ConfigCenter.I.sessionExpiredTime()); - System.out.println(ConfigCenter.holder.zkDigest()); + System.out.println(ConfigCenter.I.zkDigest()); - System.out.println(ConfigCenter.holder.zkNamespace()); + System.out.println(ConfigCenter.I.zkNamespace()); } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index bc9b5481..f867d36e 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -1,12 +1,13 @@ package com.mpush.zk; -import com.mpush.zk.listener.ZKDataChangeListener; import com.mpush.log.LogType; import com.mpush.log.LoggerManage; +import com.mpush.tools.ConsoleLog; import com.mpush.tools.Constants; import com.mpush.tools.MPushUtil; import com.mpush.tools.config.ConfigCenter; import com.mpush.tools.exception.ZKException; +import com.mpush.zk.listener.ZKDataChangeListener; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; @@ -24,6 +25,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.concurrent.TimeUnit; public class ZKClient { public static final ZKClient I = I(); @@ -36,17 +38,22 @@ private synchronized static ZKClient I() { else return I; } - public ZKClient() { - init(); + private ZKClient() { + try { + init(); + } catch (Exception e) { + throw new ZKException("init zk error, config=" + zkConfig, e); + } } /** * 初始化 */ - public void init() { - zkConfig = ZKConfig.build(ConfigCenter.holder.zkIp()) - .setDigest(ConfigCenter.holder.zkNamespace()) - .setNamespace(ConfigCenter.holder.zkNamespace()); + private void init() throws Exception { + zkConfig = ZKConfig.build(ConfigCenter.I.zkIp()) + .setDigest(ConfigCenter.I.zkDigest()) + .setNamespace(ConfigCenter.I.zkNamespace()); + ConsoleLog.i("init zk client, config=" + zkConfig); Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) @@ -77,20 +84,21 @@ public List getAclForPath(final String path) { } client = builder.build(); client.start(); - try { - client.blockUntilConnected(); - initLocalCache(zkConfig.getLocalCachePath()); - registerConnectionLostListener(); - } catch (Exception ex) { - throw new ZKException("init zk error, config=" + zkConfig, ex); + ConsoleLog.i("init zk client waiting for connected..."); + if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { + throw new ZKException("init zk error, config=" + zkConfig); } + initLocalCache(zkConfig.getLocalCachePath()); + registerConnectionLostListener(); LoggerManage.info(LogType.ZK, "zk client start success, server lists is:{}", zkConfig.getHosts()); + + ConsoleLog.i("init zk client success..."); } // 注册连接状态监听器 private void registerConnectionLostListener() { client.getConnectionStateListenable().addListener(new ConnectionStateListener() { - + //TODO need close jvm? @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { if (ConnectionState.LOST == newState) { @@ -212,6 +220,7 @@ public void registerPersist(final String key, final String value) { } } catch (final Exception ex) { LoggerManage.execption(LogType.ZK, ex, "persist:{},{}", key, value); + throw new ZKException(ex); } } @@ -226,6 +235,7 @@ public void update(final String key, final String value) { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Constants.UTF_8)).and().commit(); } catch (final Exception ex) { LoggerManage.execption(LogType.ZK, ex, "update:{},{}", key, value); + throw new ZKException(ex); } } @@ -243,6 +253,7 @@ public void registerEphemeral(final String key, final String value) { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Constants.UTF_8)); } catch (final Exception ex) { LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:{},{}", key, value); + throw new ZKException(ex); } } @@ -256,6 +267,7 @@ public void registerEphemeralSequential(final String key, final String value) { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); } catch (final Exception ex) { LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{},{}", key, value); + throw new ZKException(ex); } } @@ -269,6 +281,7 @@ public void registerEphemeralSequential(final String key) { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{}", key); + throw new ZKException(ex); } } @@ -282,6 +295,7 @@ public void remove(final String key) { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { LoggerManage.execption(LogType.ZK, ex, "remove:{}", key); + throw new ZKException(ex); } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java index 27cf8912..efa19e4d 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -1,8 +1,14 @@ package com.mpush.zk; -import com.mpush.tools.Constants; +import com.mpush.tools.config.ConfigCenter; public class ZKConfig { + public static final int ZK_MAX_RETRY = 3; + public static final int ZK_MIN_TIME = 5000; + public static final int ZK_MAX_TIME = 5000; + public static final int ZK_SESSION_TIMEOUT = 5000; + public static final int ZK_CONNECTION_TIMEOUT = 5000; + public static final String ZK_DEFAULT_CACHE_PATH = "/"; private String hosts; @@ -10,17 +16,17 @@ public class ZKConfig { private String namespace; - private int maxRetry = Constants.ZK_MAX_RETRY; + private int maxRetry = ZK_MAX_RETRY; - private int minTime = Constants.ZK_MIN_TIME; + private int minTime = ZK_MIN_TIME; - private int maxTime = Constants.ZK_MAX_TIME; + private int maxTime = ZK_MAX_TIME; - private int sessionTimeout = Constants.ZK_SESSION_TIMEOUT; + private int sessionTimeout = ZK_SESSION_TIMEOUT; - private int connectionTimeout = Constants.ZK_CONNECTION_TIMEOUT; + private int connectionTimeout = ZK_CONNECTION_TIMEOUT; - private String localCachePath = Constants.ZK_DEFAULT_CACHE_PATH; + private String localCachePath = ZK_DEFAULT_CACHE_PATH; public ZKConfig(String hosts) { this.hosts = hosts; @@ -110,4 +116,19 @@ public ZKConfig setLocalCachePath(String localCachePath) { this.localCachePath = localCachePath; return this; } + + @Override + public String toString() { + return "ZKConfig{" + + "hosts='" + hosts + '\'' + + ", digest='" + digest + '\'' + + ", namespace='" + namespace + '\'' + + ", maxRetry=" + maxRetry + + ", minTime=" + minTime + + ", maxTime=" + maxTime + + ", sessionTimeout=" + sessionTimeout + + ", connectionTimeout=" + connectionTimeout + + ", localCachePath='" + localCachePath + '\'' + + '}'; + } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java index 2da8f629..b56dba59 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java @@ -29,16 +29,16 @@ public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { public static ZKServerNode csNode() { return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.holder.connectionServerPort(), - ZKPath.CONNECTION_SERVER.getWatchPath(), - MPushUtil.getExtranetAddress()); + ConfigCenter.I.connectionServerPort(), + MPushUtil.getExtranetAddress(), + ZKPath.CONNECTION_SERVER.getWatchPath()); } public static ZKServerNode gsNode() { return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.holder.gatewayServerPort(), - ZKPath.GATEWAY_SERVER.getWatchPath(), - null); + ConfigCenter.I.gatewayServerPort(), + null, + ZKPath.GATEWAY_SERVER.getWatchPath()); } public String getIp() { diff --git a/pom.xml b/pom.xml index 9713384e..da4d2d61 100644 --- a/pom.xml +++ b/pom.xml @@ -32,12 +32,12 @@ mpush-api + mpush-boot mpush-core mpush-tools mpush-netty mpush-common mpush-client - mpush-boot mpush-test mpush-monitor mpush-log @@ -48,9 +48,9 @@ UTF-8 UTF-8 UTF-8 - 1.8 + 1.7 com.mpush - 0.0.0.3 + 0.0.1 ${mpush-version} ${mpush-version} ${mpush-version} @@ -59,7 +59,7 @@ ${mpush-version} ${mpush-version} ${mpush-version} - ${mpush-version} + ${mpush-version} ${mpush-version} ${mpush-version} 5.0.0.Alpha2 @@ -175,7 +175,7 @@ ${mpush.groupId} mpush-boot - ${mpush-cs-version} + ${mpush-boot-version} ${mpush.groupId} diff --git a/start.sh b/start.sh deleted file mode 100755 index 86399213..00000000 --- a/start.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -ENV=daily - -base_dir=`pwd` - -echo "start assembly lib..." - -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd $base_dir/target -tar -xzvf ./mpush-release.tar.gz -echo "start start mpush..." - -java -Dio.netty.leakDetectionLevel=advanced -jar $base_dir/target/mpush/boot.jar & - - -echo "end start mpush..." From 385d5696cdaa3f65e70205ee9368cb3151f62230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 15 May 2016 17:49:46 +0200 Subject: [PATCH 527/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/META-INF/services/com.mpush.zk.ZKNodeManager | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager index 257ea937..d9e4979b 100644 --- a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager +++ b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager @@ -1 +1,2 @@ com.mpush.push.zk.manager.ConnectZKNodeManager +com.mpush.push.zk.manager.GatewayZKNodeManager From b9005cffdf8bd397d8f6728be5704d48fe91732c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 15 May 2016 18:06:47 +0200 Subject: [PATCH 528/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-client/src/main/java/com/mpush/push/PushClient.java | 2 +- mpush-test/src/test/java/com/mpush/test/push/Main.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/push/PushClient.java b/mpush-client/src/main/java/com/mpush/push/PushClient.java index b4225861..953b81c5 100644 --- a/mpush-client/src/main/java/com/mpush/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/push/PushClient.java @@ -10,7 +10,7 @@ public class PushClient extends AbstractClient implements PushSender { private static final int DEFAULT_TIMEOUT = 3000; - private GatewayZKListener listener = new GatewayZKListener(); + private final GatewayZKListener listener = new GatewayZKListener(); public PushClient() { registerListener(listener); diff --git a/mpush-test/src/test/java/com/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/Main.java index e5398b2a..0a6bd320 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/push/Main.java @@ -23,7 +23,7 @@ public static void main(String[] args) throws Exception { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); - client.send(Jsons.toJson(content), Arrays.asList("huang1", "huang2", "huang"), new PushSender.Callback() { + client.send(Jsons.toJson(content), Arrays.asList("doctor43test", "huang2", "huang"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); From 945207fee58e46b3762dbc294c785f2f94961904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 04:54:59 +0200 Subject: [PATCH 529/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../conn/client/ClientChannelHandler.java | 12 +++--- .../push/zk/manager/ConnectZKNodeManager.java | 2 +- .../connection/client/ConnectTestClient.java | 13 ++----- .../mpush/test/connection/severs/Main.java | 12 ++++++ .../test/java/com/mpush/test/push/Main.java | 2 +- .../src/test/resources/config.properties | 39 +++++++++---------- 6 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java index 697d70bc..238d12ad 100644 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java @@ -66,7 +66,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); client.startHeartBeat(message.heartbeat); - LOGGER.info("会话密钥:{},message={}", sessionKey, message); + LOGGER.warn("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); } else if (command == Command.FAST_CONNECT) { @@ -79,7 +79,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); client.startHeartBeat(message.heartbeat); bindUser(securityNettyClient); - LOGGER.info("fast connect success, message=" + message); + LOGGER.warn("fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); @@ -89,17 +89,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.error("receive an error packet=" + errorMessage); } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.info("receive an success packet=" + okMessage); + LOGGER.warn("receive an success packet=" + okMessage); HttpRequestMessage message = new HttpRequestMessage(connection); message.uri = "http://baidu.com"; message.send(); } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); - LOGGER.info("receive an push message, content=" + message.content); + LOGGER.warn("receive an push message, content=" + message.content); } else if (command == Command.HEARTBEAT) { - LOGGER.info("receive a heartbeat pong..."); + LOGGER.warn("receive a heartbeat pong..."); } else { - LOGGER.info("receive a message, type=" + command + "," + packet); + LOGGER.warn("receive a message, type=" + command + "," + packet); } } diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java index b7915dc7..df9c939a 100644 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java +++ b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java @@ -15,7 +15,7 @@ public class ConnectZKNodeManager implements ZKNodeManager { private final Logger log = LoggerFactory.getLogger(ConnectZKNodeManager.class); - private Map cache = Maps.newConcurrentMap(); + private final Map cache = Maps.newConcurrentMap(); @Override public void addOrUpdate(String fullPath, ZKServerNode node) { diff --git a/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java index b99faeb3..ca5b9c0d 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java @@ -2,24 +2,19 @@ import com.google.common.collect.Lists; import com.mpush.common.AbstractClient; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; import com.mpush.push.zk.listener.ConnectZKListener; -import com.mpush.tools.spi.ServiceContainer; +import com.mpush.zk.ZKServerNode; import java.util.List; public class ConnectTestClient extends AbstractClient { - - @SuppressWarnings("unchecked") - private ZKNodeManager connectionServerManage = ServiceContainer.load(ZKNodeManager.class); + private final ConnectZKListener listener = new ConnectZKListener(); public ConnectTestClient() { - registerListener(new ConnectZKListener()); + registerListener(listener); } public List getServers() { - return Lists.newArrayList(connectionServerManage.getList()); + return Lists.newArrayList(listener.getManager().getList()); } - } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java new file mode 100644 index 00000000..778a4778 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java @@ -0,0 +1,12 @@ +package com.mpush.test.connection.severs; + +/** + * Created by yxx on 2016/5/16. + * + * @author ohun@live.cn + */ +public class Main { + public static void main(String[] args) { + com.mpush.Main.main(args); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/Main.java index 0a6bd320..38b6b120 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/push/Main.java @@ -23,7 +23,7 @@ public static void main(String[] args) throws Exception { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); - client.send(Jsons.toJson(content), Arrays.asList("doctor43test", "huang2", "huang"), new PushSender.Callback() { + client.send(Jsons.toJson(content), Arrays.asList("doctor43test", "user-0", "huang"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index d2776999..1a1e412b 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -1,36 +1,35 @@ ## -max_packet_size = 10240 +max_packet_size=10240 ## -compress_limit = 10240 +compress_limit=10240 ## -min_heartbeat = 10000 +min_heartbeat=10000 ## -max_heartbeat = 180000 +max_heartbeat=180000 ## -max_hb_timeout_times = 2 +max_hb_timeout_times=2 ## -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= ## -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB +public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv6Ggb2XAeZhlLD28fHwIDAQAB ## -gateway_server_port = 20882 +gateway_server_port=20882 ## -connection_server_port = 3000 +connection_server_port=3000 ## -aes_key_length = 16 +aes_key_length=16 ## -ras_key_length = 1024 +ras_key_length=1024 ## -session_expired_time = 86400 - +session_expired_time=86400 #zk_ip = 115.29.169.109:5666 -zk_ip = 127.0.0.1:2181 -zk_namespace = mpush-daily -zk_digest = shinemoIpo - +zk_ip=127.0.0.1:2181 +zk_namespace=mpush-daily +zk_digest=shinemoIpo ##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo #redis_group = 111.1.57.148:6379:ShineMoIpo -redis_group = 127.0.0.1:6379:shinemoIpo -force_write_redis_group_info = false +redis_group=111.1.57.148:6379:ShineMoIpo +force_write_redis_group_info=false dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 - +#IpIpӳ +remote_ip_mapping=127.0.0.1:111.1.57.148 From 8dc68944fd930e8ea8761f8f1fa0be9b3c3c60fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 05:16:26 +0200 Subject: [PATCH 530/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/src/test/resources/config.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties index 1a1e412b..23e53825 100644 --- a/mpush-test/src/test/resources/config.properties +++ b/mpush-test/src/test/resources/config.properties @@ -11,7 +11,7 @@ max_hb_timeout_times=2 ## private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= ## -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv6Ggb2XAeZhlLD28fHwIDAQAB +public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB ## gateway_server_port=20882 ## From 42f49e3557079e13b413b911a5b00b982687f55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 05:28:25 +0200 Subject: [PATCH 531/890] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/test/connection/mpns/ConnectTestClient.java | 9 ++++++--- .../test/java/com/mpush/test/connection/mpns/Main.java | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java index 625541a3..1126476d 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java @@ -2,16 +2,19 @@ import com.google.common.collect.Lists; import com.mpush.common.AbstractClient; +import com.mpush.push.zk.listener.ConnectZKListener; import com.mpush.zk.ZKServerNode; import java.util.List; public class ConnectTestClient extends AbstractClient { + private final ConnectZKListener listener = new ConnectZKListener(); - private final List applicationLists = Lists.newArrayList( - new ZKServerNode("111.1.57.148", 20882, "111.1.57.148", "")); + public ConnectTestClient() { + registerListener(listener); + } public List getServers() { - return Lists.newArrayList(applicationLists); + return Lists.newArrayList(listener.getManager().getList()); } } diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java index 319fa02b..9de9a50e 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java @@ -21,7 +21,7 @@ public static void main(String[] args) throws InterruptedException { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ZKServerNode server = serverList.get(index); - for (int i = 0; i < 10000; i++) { + for (int i = 0; i < 1000; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; @@ -43,7 +43,7 @@ public static void main(String[] args) throws InterruptedException { ClientChannelHandler handler = new ClientChannelHandler(client); NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(10); + Thread.sleep(100); } LockSupport.park(); From 243781d804e9b9ff08d6aa5b90ef9e6c0883dd29 Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Mon, 16 May 2016 11:57:03 +0800 Subject: [PATCH 532/890] cap with redis server without passwd --- .../java/com/mpush/tools/config/RedisGroupConverter.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java index 424c2580..f43ae3ea 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java @@ -26,7 +26,10 @@ public RedisGroup convert(Method method, String input) { String[] entry = chunk.split(":"); String ip = entry[0].trim(); String port = entry[1].trim(); - String password = entry[2].trim(); + String password = null; + if(entry.length >=3){ + password = entry[2].trim(); + } RedisNode node = new RedisNode(ip, Integer.parseInt(port), password); group.addRedisNode(node); } From 935168f055f3729c16c02ac89394f797fae53529 Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Mon, 16 May 2016 12:02:22 +0800 Subject: [PATCH 533/890] add comment --- .../main/java/com/mpush/tools/config/RedisGroupConverter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java index f43ae3ea..7ca15bc0 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java @@ -26,7 +26,9 @@ public RedisGroup convert(Method method, String input) { String[] entry = chunk.split(":"); String ip = entry[0].trim(); String port = entry[1].trim(); - String password = null; + // 如果配置了redis密码(redis_group = 111.1.57.148:6379:ShineMoIpo)才设置密码 + // 否则密码为空,JedisPool可以兼容两种情况 + String password = null; if(entry.length >=3){ password = entry[2].trim(); } From 9fec49d79ec69460cf4c0cadbb713bbeb6789cf3 Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Mon, 16 May 2016 15:20:19 +0800 Subject: [PATCH 534/890] gateway server add extranet ip address --- mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java index b56dba59..f7ad9ea2 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java @@ -37,7 +37,7 @@ public static ZKServerNode csNode() { public static ZKServerNode gsNode() { return new ZKServerNode(MPushUtil.getLocalIp(), ConfigCenter.I.gatewayServerPort(), - null, + MPushUtil.getExtranetAddress(), ZKPath.GATEWAY_SERVER.getWatchPath()); } From 955f919e3eb9ad667b848371cad417243efecb55 Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Mon, 16 May 2016 15:31:50 +0800 Subject: [PATCH 535/890] gateway server should not open external ip port! gateway server should run within same network with connection server --- mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java index f7ad9ea2..b56dba59 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java @@ -37,7 +37,7 @@ public static ZKServerNode csNode() { public static ZKServerNode gsNode() { return new ZKServerNode(MPushUtil.getLocalIp(), ConfigCenter.I.gatewayServerPort(), - MPushUtil.getExtranetAddress(), + null, ZKPath.GATEWAY_SERVER.getWatchPath()); } From b701d9e3a6b49bb5fd50a263c89fd530ca05eca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 11:33:06 +0200 Subject: [PATCH 536/890] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/ServerLauncher.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java index 1c246c5e..87315101 100644 --- a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java @@ -29,14 +29,14 @@ public class ServerLauncher { public void start() { BootChain chain = BootChain.chain(); chain.boot() - .setNext(new RedisBoot()) - .setNext(new ZKBoot()) - .setNext(new ServerBoot(connectServer, csNode)) - .setNext(new ServerBoot(gatewayServer, gsNode)) - .setNext(new ServerBoot(adminServer, null)) - .setNext(new HttpProxyBoot()) - .setNext(new MonitorBoot()) - .setNext(new LastBoot()); + .setNext(new RedisBoot())//1.注册redis sever 到ZK + .setNext(new ZKBoot())//2.启动ZK节点数据变化监听 + .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 + .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 + .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 + .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns + .setNext(new MonitorBoot())//7.启动监控 + .setNext(new LastBoot());//8.启动结束 chain.run(); } From 181f200f75ce85a4e2258215b8e2052da4d2ee2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 11:33:42 +0200 Subject: [PATCH 537/890] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E7=B1=BBbug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/tools/spi/ServiceContainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java b/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java index 83d0db82..3e8cc3d1 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java @@ -51,7 +51,7 @@ private static T filterByName(ServiceLoader factories, String name) { } } else { while (it.hasNext()) { - T t = factories.iterator().next(); + T t = it.next(); if (name.equals(t.getClass().getSimpleName())) { return t; } From 481473a19eb3482295f74e9726f4ba4907e83325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 12:29:11 +0200 Subject: [PATCH 538/890] =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/logback.xml | 12 +- .../mpush/core/handler/BindUserHandler.java | 21 +- .../core/handler/FastConnectHandler.java | 9 +- .../core/handler/GatewayPushHandler.java | 15 +- .../mpush/core/handler/HandshakeHandler.java | 14 +- .../mpush/core/handler/HeartBeatHandler.java | 5 +- .../mpush/core/handler/HttpProxyHandler.java | 9 +- .../mpush/core/handler/UnbindUserHandler.java | 13 +- .../core/router/RouterChangeListener.java | 20 +- .../core/server/ServerChannelHandler.java | 10 +- .../src/main/java/com/mpush/log/LogLevel.java | 5 - .../src/main/java/com/mpush/log/LogType.java | 6 - .../main/java/com/mpush/log/LoggerManage.java | 128 ------- .../src/main/java/com/mpush/log/Logs.java | 326 ++++++++++++++++++ .../connection/NettyConnectionManager.java | 12 +- .../java/com/mpush/tools/redis/RedisUtil.java | 188 +++++----- .../jedis/services/JedisRegisterManager.java | 8 +- .../redis/listener/ListenerDispatcher.java | 6 +- .../mpush/tools/redis/pubsub/Subscriber.java | 20 +- .../src/main/java/com/mpush/zk/ZKClient.java | 27 +- .../zk/listener/ZKDataChangeListener.java | 6 +- 21 files changed, 516 insertions(+), 344 deletions(-) delete mode 100644 mpush-log/src/main/java/com/mpush/log/LogLevel.java delete mode 100644 mpush-log/src/main/java/com/mpush/log/LogType.java delete mode 100644 mpush-log/src/main/java/com/mpush/log/LoggerManage.java create mode 100644 mpush-log/src/main/java/com/mpush/log/Logs.java diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml index ce77c389..17dc0dee 100644 --- a/mpush-boot/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -86,7 +86,7 @@ 30 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n @@ -106,7 +106,7 @@ 30 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n @@ -131,7 +131,7 @@ 30 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n @@ -151,7 +151,7 @@ 5 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n @@ -171,7 +171,7 @@ 5 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n @@ -191,7 +191,7 @@ 10 - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index de400d14..30c25115 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -1,7 +1,6 @@ package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.core.router.RouterCenter; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOnlineEvent; @@ -11,8 +10,8 @@ import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.OkMessage; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.core.router.RouterCenter; +import com.mpush.log.Logs; /** * Created by ohun on 2015/12/23. @@ -23,14 +22,14 @@ public final class BindUserHandler extends BaseMessageHandler { @Override public BindUserMessage decode(Packet packet, Connection connection) { - return new BindUserMessage(packet, connection); + return new BindUserMessage(packet, connection); } @Override public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - LoggerManage.log(LogType.CONNECTION, "bind user failure for invalid param, session={}", message.getConnection().getSessionContext()); + Logs.Conn.info("bind user failure for invalid param, session={}", message.getConnection().getSessionContext()); return; } //1.绑定用户时先看下是否握手成功 @@ -39,20 +38,20 @@ public void handle(BindUserMessage message) { //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { - - EventBus.INSTANCE.post(new UserOnlineEvent( message.getConnection(),message.userId)); - + + EventBus.INSTANCE.post(new UserOnlineEvent(message.getConnection(), message.userId)); + OkMessage.from(message).setData("bind success").send(); - LoggerManage.log(LogType.CONNECTION, "bind user success, userId={}, session={}", message.userId, context); + Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.INSTANCE.unRegister(message.userId); ErrorMessage.from(message).setReason("bind failed").close(); - LoggerManage.log(LogType.CONNECTION, "bind user failure, userId={}, session={}", message.userId, context); + Logs.Conn.info("bind user failure, userId={}, session={}", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - LoggerManage.log(LogType.CONNECTION, "bind user failure for not handshake, userId={}, session={}", message.userId, context); + Logs.Conn.info("bind user failure for not handshake, userId={}, session={}", message.userId, context); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 95fa895f..de0c9e40 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -8,8 +8,7 @@ import com.mpush.common.message.FastConnectOkMessage; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; import com.mpush.tools.MPushUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,11 +34,11 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - LoggerManage.info(LogType.CONNECTION, "fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); + Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - LoggerManage.info(LogType.CONNECTION, "fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); + Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); } else { //3.校验成功,重新计算心跳,完成快速重连 int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); @@ -51,7 +50,7 @@ public void handle(FastConnectMessage message) { .from(message) .setHeartbeat(heartbeat) .send(); - LoggerManage.info(LogType.CONNECTION, "fast connect success, session={}", session.context); + Logs.Conn.info("fast connect success, session={}", session.context); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 108be503..69acb209 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -10,8 +10,7 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.core.router.LocalRouter; import com.mpush.core.router.RouterCenter; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; import com.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -73,7 +72,7 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - LoggerManage.info(LogType.PUSH, "gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + Logs.PUSH.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); //删除已经失效的本地路由 RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); @@ -91,13 +90,13 @@ public void operationComplete(ChannelFuture future) throws Exception { //推送成功 OkMessage.from(message).setData(message.userId).send(); - LoggerManage.info(LogType.PUSH, "gateway push message to client success userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push message to client success userId={}, content={}", message.userId, message.content); } else { //推送失败 ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); - LoggerManage.info(LogType.PUSH, "gateway push message to client failure userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push message to client failure userId={}, content={}", message.userId, message.content); } } }); @@ -120,7 +119,7 @@ private void checkRemote(GatewayPushMessage message) { ErrorMessage.from(message).setErrorCode(OFFLINE).send(); - LoggerManage.info(LogType.PUSH, "gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); return; } @@ -133,7 +132,7 @@ private void checkRemote(GatewayPushMessage message) { //删除失效的远程缓存 RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); - LoggerManage.info(LogType.PUSH, "gateway push error remote is local, userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push error remote is local, userId={}, router={}", message.userId, router); return; } @@ -141,7 +140,7 @@ private void checkRemote(GatewayPushMessage message) { //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); - LoggerManage.info(LogType.PUSH, "gateway push, router in remote userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push, router in remote userId={}, router={}", message.userId, router); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 58f35040..30f3400b 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -1,8 +1,6 @@ package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.core.session.ReusableSession; -import com.mpush.core.session.ReusableSessionManager; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.HandshakeEvent; @@ -14,10 +12,10 @@ import com.mpush.common.message.HandshakeOkMessage; import com.mpush.common.security.AesCipher; import com.mpush.common.security.CipherBox; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.core.session.ReusableSession; +import com.mpush.core.session.ReusableSessionManager; +import com.mpush.log.Logs; import com.mpush.tools.MPushUtil; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,14 +45,14 @@ public void handle(HandshakeMessage message) { || iv.length != CipherBox.INSTANCE.getAesKeyLength() || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - LoggerManage.log(LogType.CONNECTION, "client handshake false:{}", message.getConnection()); + Logs.Conn.info("client handshake false:{}", message.getConnection()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - LoggerManage.log(LogType.CONNECTION, "client handshake false for repeat handshake:{}", message.getConnection().getSessionContext()); + Logs.Conn.info("client handshake false for repeat handshake:{}", message.getConnection().getSessionContext()); return; } @@ -91,6 +89,6 @@ public void handle(HandshakeMessage message) { //10.触发握手成功事件 EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); - LoggerManage.log(LogType.CONNECTION, "client handshake success:{}", context); + Logs.Conn.info("client handshake success:{}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index cee592f8..5feea063 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -3,8 +3,7 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; /** * Created by ohun on 2015/12/22. @@ -16,7 +15,7 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong - LoggerManage.log(LogType.HEARTBEAT, "response client heartbeat:{}, {}", + Logs.HB.info("response client heartbeat:{}, {}", connection.getChannel(), connection.getSessionContext().deviceId); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 5ad960d2..4cc42f1d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -1,16 +1,15 @@ package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.netty.client.HttpCallback; -import com.mpush.netty.client.RequestInfo; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; +import com.mpush.netty.client.HttpCallback; import com.mpush.netty.client.HttpClient; +import com.mpush.netty.client.RequestInfo; import com.mpush.tools.Profiler; import com.mpush.tools.dns.DnsMapping; import com.mpush.tools.dns.manage.DnsMappingManage; @@ -32,7 +31,7 @@ * @author ohun@live.cn */ public class HttpProxyHandler extends BaseMessageHandler { - private static final Logger LOGGER = LoggerManage.getLog(LogType.HTTP); + private static final Logger LOGGER = Logs.HTTP; private final HttpClient httpClient; public HttpProxyHandler(HttpClient httpClient) { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index 41074ced..8b339af8 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -6,7 +6,6 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOfflineEvent; -import com.mpush.api.event.UserOnlineEvent; import com.mpush.api.protocol.Packet; import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; @@ -16,8 +15,8 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.core.router.LocalRouter; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + /** * Created by ohun on 2015/12/23. @@ -41,7 +40,7 @@ public BindUserMessage decode(Packet packet, Connection connection) { public void handle(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - LoggerManage.info(LogType.CONNECTION, "unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); + Logs.Conn.info("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } @@ -73,14 +72,14 @@ public void handle(BindUserMessage message) { if (unRegisterSuccess) { EventBus.INSTANCE.post(new UserOfflineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("unbind success").send(); - LoggerManage.info(LogType.CONNECTION, "unbind user success, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user success, userId={}, session={}", message.userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").send(); - LoggerManage.info(LogType.CONNECTION, "unbind user failure, register router failure, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - LoggerManage.info(LogType.CONNECTION, "unbind user failure not handshake, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user failure not handshake, userId={}, session={}", message.userId, context); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index d24e4e06..e218cbee 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -10,8 +10,8 @@ import com.mpush.common.AbstractEventContainer; import com.mpush.common.message.KickUserMessage; import com.mpush.common.router.RemoteRouter; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import com.mpush.tools.Jsons; import com.mpush.tools.MPushUtil; import com.mpush.tools.redis.listener.ListenerDispatcher; @@ -68,9 +68,9 @@ public void kickLocal(final String userId, final LocalRouter router) { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - LoggerManage.info(LogType.CONNECTION, "kick local connection success, userId={}, router={}", userId, router); + Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); } else { - LoggerManage.info(LogType.CONNECTION, "kick local connection failure, userId={}, router={}", userId, router); + Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); } } }); @@ -89,7 +89,7 @@ public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(MPushUtil.getLocalIp())) { - LoggerManage.info(LogType.CONNECTION, "kick remote user but router in local, userId={}", userId); + Logs.Conn.info("kick remote user but router in local, userId={}", userId); return; } @@ -113,7 +113,7 @@ public void kickRemote(String userId, RemoteRouter router) { public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); return; } @@ -122,14 +122,14 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); LocalRouter router = routerManager.lookup(userId); if (router != null) { - LoggerManage.info(LogType.CONNECTION, "receive kick remote msg, msg={}", msg); + Logs.Conn.info("receive kick remote msg, msg={}", msg); //2.1删除本地路由信息 routerManager.unRegister(userId); //2.2发送踢人消息到客户端 kickLocal(userId, router); remStatUser(userId); } else { - LoggerManage.info(LogType.CONNECTION, "no local router find, kick failure, msg={}", msg); + Logs.Conn.info("no local router find, kick failure, msg={}", msg); } } @@ -140,10 +140,10 @@ public void onMessage(String channel, String message) { if (msg != null) { onReceiveKickRemoteMsg(msg); } else { - LoggerManage.info(LogType.CONNECTION, "receive an error kick message={}", message); + Logs.Conn.info("receive an error kick message={}", message); } } else { - LoggerManage.info(LogType.CONNECTION, "receive an error redis channel={}", channel); + Logs.Conn.info("receive an error redis channel={}", channel); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index a9dd523a..73a8d6eb 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -8,8 +8,8 @@ import com.mpush.api.connection.Connection; import com.mpush.api.PacketReceiver; import com.mpush.common.EventBus; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import com.mpush.tools.Profiler; import io.netty.channel.ChannelHandler; @@ -64,13 +64,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connectionManager.remove(ctx.channel()); - LoggerManage.info(LogType.CONNECTION, "client exceptionCaught channel={}", ctx.channel()); + Logs.Conn.info("client exceptionCaught channel={}", ctx.channel()); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.info(LogType.CONNECTION, "client connect channel={}", ctx.channel()); + Logs.Conn.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -78,7 +78,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - LoggerManage.info(LogType.CONNECTION, "client disconnect channel={}", ctx.channel()); + Logs.Conn.info("client disconnect channel={}", ctx.channel()); Connection connection = connectionManager.get(ctx.channel()); EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); connectionManager.remove(ctx.channel()); diff --git a/mpush-log/src/main/java/com/mpush/log/LogLevel.java b/mpush-log/src/main/java/com/mpush/log/LogLevel.java deleted file mode 100644 index 03e64ae1..00000000 --- a/mpush-log/src/main/java/com/mpush/log/LogLevel.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.mpush.log; - -public enum LogLevel { - DEBUG,WARN,INFO,ERROR -} diff --git a/mpush-log/src/main/java/com/mpush/log/LogType.java b/mpush-log/src/main/java/com/mpush/log/LogType.java deleted file mode 100644 index cb01804d..00000000 --- a/mpush-log/src/main/java/com/mpush/log/LogType.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.mpush.log; - -public enum LogType { - CONNECTION,PUSH,HEARTBEAT,REDIS,ZK,DEFAULT,HTTP - -} diff --git a/mpush-log/src/main/java/com/mpush/log/LoggerManage.java b/mpush-log/src/main/java/com/mpush/log/LoggerManage.java deleted file mode 100644 index 0c017360..00000000 --- a/mpush-log/src/main/java/com/mpush/log/LoggerManage.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.mpush.log; - -import java.util.HashMap; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class LoggerManage { - - private static final Logger connectionLog = LoggerFactory.getLogger("connectionLog"); - private static final Logger pushLog = LoggerFactory.getLogger("pushLog"); - private static final Logger heartBeatLog = LoggerFactory.getLogger("heartBeatLog"); - private static final Logger redisLog = LoggerFactory.getLogger("redisLog"); - private static final Logger zkLog = LoggerFactory.getLogger("zkLog"); - private static final Logger httpLog = LoggerFactory.getLogger("httpLog"); - - private static final Logger defaultLog = LoggerFactory.getLogger(LoggerManage.class); - - private static final Map map = new HashMap<>(); - - static{ - map.put(LogType.CONNECTION, connectionLog); - map.put(LogType.PUSH, pushLog); - map.put(LogType.HEARTBEAT, heartBeatLog); - map.put(LogType.REDIS, redisLog); - map.put(LogType.ZK, zkLog); - map.put(LogType.HTTP, httpLog); - } - - - public static void log(LogType type,String format,Object... arguments){ - info(type, format, arguments); - } - - public static void warn(LogType type,String format,Object... arguments){ - log(type, LogLevel.WARN, null, format, arguments); - } - - public static void info(LogType type,String format,Object... arguments){ - log(type, LogLevel.INFO, null, format, arguments); - } - - public static void error(LogType type,String format,Object... arguments){ - log(type, LogLevel.ERROR, null, format, arguments); - } - - public static void debug(LogType type,String format,Object... arguments){ - log(type, LogLevel.DEBUG, null, format, arguments); - } - - public static Logger getLog(LogType type){ - Logger log = map.get(type); - if(log == null){ - log = defaultLog; - } - return log; - } - - public static void execption(LogType type,Throwable ex,String format,Object... arguments){ - log(type, LogLevel.ERROR, ex, format, arguments); - } - - /** - * 默认 level 为warn - * @param type - * @param level - * @param ex - * @param format - * @param arguments - */ - public static void log(LogType type,LogLevel level,Throwable ex,String format,Object... arguments){ - if(level == null){ - level = LogLevel.WARN; - } - Logger log = map.get(type); - if(ex!=null){ - if(log!=null){ - if(level.equals(LogLevel.WARN)){ - log.warn(format,arguments,ex); - }else if(level.equals(LogLevel.INFO)){ - log.info(format,arguments,ex); - }else if(level.equals(LogLevel.DEBUG)){ - log.debug(format,arguments,ex); - }else if(level.equals(LogLevel.ERROR)){ - log.error(format,arguments,ex); - } - }else{ - defaultLog.warn(format,arguments,ex); - } - }else{ - if(log!=null){ - if(level.equals(LogLevel.WARN)){ - log.warn(format,arguments); - }else if(level.equals(LogLevel.INFO)){ - log.info(format,arguments); - }else if(level.equals(LogLevel.DEBUG)){ - log.debug(format,arguments); - }else if(level.equals(LogLevel.ERROR)){ - log.error(format,arguments); - } - }else{ - defaultLog.warn(format,arguments); - } - } - } - - - /** - * security的log 为 connectionLog的log - * @param security - * @param level - * @param format - * @param arguments - */ - public static void log(boolean security,LogLevel level,String format,Object... arguments){ - if(security){ - log(LogType.CONNECTION, level, null, format, arguments); - }else{ - log(LogType.PUSH, level, null, format, arguments); - } - } - public static void main(String[] args) { - String format = "client connect channel=%s"; - System.out.println(String.format(format, "hi")); - } - -} diff --git a/mpush-log/src/main/java/com/mpush/log/Logs.java b/mpush-log/src/main/java/com/mpush/log/Logs.java new file mode 100644 index 00000000..04a5d949 --- /dev/null +++ b/mpush-log/src/main/java/com/mpush/log/Logs.java @@ -0,0 +1,326 @@ +package com.mpush.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.Marker; + +public enum Logs implements Logger { + Conn("connectionLog"), + PUSH("pushLog"), + HB("heartBeatLog"), + REDIS("redisLog"), + ZK("zkLog"), + HTTP("httpLog"); + + private final Logger logger; + + Logs(String loggerName) { + this.logger = LoggerFactory.getLogger(loggerName); + } + + @Override + public String getName() { + return logger.getName(); + } + + @Override + public boolean isTraceEnabled() { + return logger.isTraceEnabled(); + } + + @Override + public void trace(String s) { + logger.trace(s); + } + + @Override + public void trace(String s, Object o) { + logger.trace(s, o); + } + + @Override + public void trace(String s, Object o, Object o1) { + logger.trace(s, o, o1); + } + + @Override + public void trace(String s, Object... objects) { + logger.trace(s, objects); + } + + @Override + public void trace(String s, Throwable throwable) { + logger.trace(s, throwable); + } + + @Override + public boolean isTraceEnabled(Marker marker) { + return isTraceEnabled(marker); + } + + @Override + public void trace(Marker marker, String s) { + logger.trace(marker, s); + } + + @Override + public void trace(Marker marker, String s, Object o) { + logger.trace(marker, s, o); + } + + @Override + public void trace(Marker marker, String s, Object o, Object o1) { + logger.trace(marker, s, o, o1); + } + + @Override + public void trace(Marker marker, String s, Object... objects) { + logger.trace(marker, s, objects); + } + + @Override + public void trace(Marker marker, String s, Throwable throwable) { + logger.trace(marker, s, throwable); + } + + @Override + public boolean isDebugEnabled() { + return isErrorEnabled(); + } + + @Override + public void debug(String s) { + logger.debug(s); + } + + @Override + public void debug(String s, Object o) { + logger.debug(s, o); + } + + @Override + public void debug(String s, Object o, Object o1) { + logger.debug(s, o, o1); + } + + @Override + public void debug(String s, Object... objects) { + logger.debug(s, objects); + } + + @Override + public void debug(String s, Throwable throwable) { + logger.debug(s, throwable); + } + + @Override + public boolean isDebugEnabled(Marker marker) { + return logger.isDebugEnabled(marker); + } + + @Override + public void debug(Marker marker, String s) { + logger.debug(marker, s); + } + + @Override + public void debug(Marker marker, String s, Object o) { + logger.debug(marker, s, o); + } + + @Override + public void debug(Marker marker, String s, Object o, Object o1) { + logger.debug(marker, s, o, o1); + } + + @Override + public void debug(Marker marker, String s, Object... objects) { + logger.debug(marker, s, objects); + } + + @Override + public void debug(Marker marker, String s, Throwable throwable) { + logger.debug(marker, s, throwable); + } + + @Override + public boolean isInfoEnabled() { + return logger.isInfoEnabled(); + } + + @Override + public void info(String s) { + logger.info(s); + } + + @Override + public void info(String s, Object o) { + logger.info(s, o); + } + + @Override + public void info(String s, Object o, Object o1) { + logger.info(s, o, o1); + } + + @Override + public void info(String s, Object... objects) { + logger.info(s, objects); + } + + @Override + public void info(String s, Throwable throwable) { + logger.info(s, throwable); + } + + @Override + public boolean isInfoEnabled(Marker marker) { + return logger.isInfoEnabled(marker); + } + + @Override + public void info(Marker marker, String s) { + logger.info(marker, s); + } + + @Override + public void info(Marker marker, String s, Object o) { + logger.info(marker, s, o); + } + + @Override + public void info(Marker marker, String s, Object o, Object o1) { + logger.info(marker, s, o, o1); + } + + @Override + public void info(Marker marker, String s, Object... objects) { + logger.info(marker, s, objects); + } + + @Override + public void info(Marker marker, String s, Throwable throwable) { + logger.info(marker, s, throwable); + } + + @Override + public boolean isWarnEnabled() { + return logger.isWarnEnabled(); + } + + @Override + public void warn(String s) { + logger.warn(s); + } + + @Override + public void warn(String s, Object o) { + logger.warn(s, o); + } + + @Override + public void warn(String s, Object... objects) { + logger.warn(s, objects); + } + + @Override + public void warn(String s, Object o, Object o1) { + logger.warn(s, o, o1); + } + + @Override + public void warn(String s, Throwable throwable) { + logger.warn(s, throwable); + } + + @Override + public boolean isWarnEnabled(Marker marker) { + return isWarnEnabled(marker); + } + + @Override + public void warn(Marker marker, String s) { + logger.warn(marker, s); + } + + @Override + public void warn(Marker marker, String s, Object o) { + logger.warn(marker, s, o); + } + + @Override + public void warn(Marker marker, String s, Object o, Object o1) { + logger.warn(marker, s, o, o1); + } + + @Override + public void warn(Marker marker, String s, Object... objects) { + logger.warn(marker, s, objects); + } + + @Override + public void warn(Marker marker, String s, Throwable throwable) { + logger.warn(marker, s, throwable); + } + + @Override + public boolean isErrorEnabled() { + return isErrorEnabled(); + } + + @Override + public void error(String s) { + logger.error(s); + } + + @Override + public void error(String s, Object o) { + logger.error(s, o); + } + + @Override + public void error(String s, Object o, Object o1) { + logger.error(s, o, o1); + } + + @Override + public void error(String s, Object... objects) { + logger.error(s, objects); + } + + @Override + public void error(String s, Throwable throwable) { + logger.error(s, throwable); + } + + @Override + public boolean isErrorEnabled(Marker marker) { + return isErrorEnabled(marker); + } + + @Override + public void error(Marker marker, String s) { + logger.error(marker, s); + } + + @Override + public void error(Marker marker, String s, Object o) { + logger.error(marker, s, o); + } + + @Override + public void error(Marker marker, String s, Object o, Object o1) { + logger.error(marker, s, o, o1); + } + + @Override + public void error(Marker marker, String s, Object... objects) { + logger.error(marker, s, objects); + } + + @Override + public void error(Marker marker, String s, Throwable throwable) { + logger.error(marker, s, throwable); + } + +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java index 691268d0..43d14a39 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java @@ -7,8 +7,8 @@ import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.HandshakeEvent; import com.mpush.common.EventBus; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import com.mpush.tools.config.ConfigCenter; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; @@ -96,20 +96,20 @@ public void startTimeout() { @Override public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) { - LoggerManage.info(LogType.HEARTBEAT, "connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); return; } if (connection.heartbeatTimeout()) { if (++expiredTimes > ConfigCenter.I.maxHBTimeoutTimes()) { connection.close(); - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); return; } else { - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } } else { expiredTimes = 0; - LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); } startTimeout(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java index a8063e8a..6c160b30 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java @@ -1,26 +1,20 @@ package com.mpush.tools.redis; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; -import com.mpush.tools.Constants; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.mpush.log.Logs; +import com.mpush.tools.Constants; import com.mpush.tools.Jsons; +import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import redis.clients.jedis.*; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPubSub; -import redis.clients.jedis.ScanParams; -import redis.clients.jedis.ScanResult; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; public class RedisUtil { - + private static Map holder = Maps.newConcurrentMap(); public static Jedis getClient(RedisNode node) { @@ -35,46 +29,46 @@ public static Jedis getClient(RedisNode node) { public static void close(Jedis jedis) { jedis.close(); } - - public static long incr(List nodeList,String key,Integer time){ - long incrRet = -1; - for (RedisNode node : nodeList) { + + public static long incr(List nodeList, String key, Integer time) { + long incrRet = -1; + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); long ret = jedis.incr(key); - if(ret == 1 && time!=null){ - jedis.expire(key, time); + if (ret == 1 && time != null) { + jedis.expire(key, time); } incrRet = ret; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis incr exception:{},{},{},{}",key,time,node); + Logs.REDIS.error("redis incr exception:{},{},{},{}", key, time, node, e); } finally { // 返还到连接池 close(jedis); } } - return incrRet; - + return incrRet; + } - - public static long incrBy(List nodeList,String key,long delt){ - long incrRet = -1; - for (RedisNode node : nodeList) { + + public static long incrBy(List nodeList, String key, long delt) { + long incrRet = -1; + for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); long ret = jedis.incrBy(key, delt); incrRet = ret; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis incr exception:{},{},{},{}",key,delt,node); + Logs.REDIS.error("redis incr exception:{},{},{},{}", key, delt, node, e); } finally { // 返还到连接池 close(jedis); } } - return incrRet; - + return incrRet; + } /********************* k v redis start ********************************/ @@ -85,7 +79,7 @@ public static long incrBy(List nodeList,String key,long delt){ * @return */ @SuppressWarnings("unchecked") - public static T get(RedisNode node, String key, Class clazz) { + public static T get(RedisNode node, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -93,7 +87,7 @@ public static T get(RedisNode node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis get exception:{},{}",key,node); + Logs.REDIS.error("redis get exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -124,19 +118,19 @@ public static void set(List nodeList, String key, T value, Intege * @param time seconds */ public static void set(List nodeList, String key, String value, Integer time) { - if(time == null){ - time = -1; - } + if (time == null) { + time = -1; + } for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.set(key, value); - if (time>0) { + if (time > 0) { jedis.expire(key, time); } } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis set exception:{},{},{},{}",key,value,time,node); + Logs.REDIS.error("redis set exception:{},{},{},{}", key, value, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -152,7 +146,7 @@ public static void del(List nodeList, String key) { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis del exception:{},{}",key,node); + Logs.REDIS.error("redis del exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -173,7 +167,7 @@ public static void hset(List nodeList, String namespace, String key, jedis = getClient(node); jedis.hset(namespace, key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hset exception:{},{},{},{}",namespace,key,value,node); + Logs.REDIS.error("redis hset exception:{},{},{},{}", namespace, key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -193,7 +187,7 @@ public static T hget(RedisNode node, String namespace, String key, Class jedis = getClient(node); value = jedis.hget(namespace, key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hget exception:{},{}",namespace,key,node); + Logs.REDIS.error("redis hget exception:{},{}", namespace, key, node, e); } finally { // 返还到连接池 close(jedis); @@ -210,7 +204,7 @@ public static void hdel(List nodeList, String namespace, String key) jedis = getClient(node); jedis.hdel(namespace, key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hdel exception:{},{},{}",namespace,key,node); + Logs.REDIS.error("redis hdel exception:{},{},{}", namespace, key, node, e); } finally { // 返还到连接池 close(jedis); @@ -225,7 +219,7 @@ public static Map hgetAll(RedisNode node, String namespace) { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:{},{}",namespace,node); + Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); } finally { // 返还到连接池 close(jedis); @@ -240,7 +234,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hgetAll exception:{},{}",namespace,node); + Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); } finally { // 返还到连接池 close(jedis); @@ -275,7 +269,7 @@ public static Set hkeys(RedisNode node, String key) { jedis = getClient(node); result = jedis.hkeys(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hkeys exception:{},{}",key,node); + Logs.REDIS.error("redis hkeys exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -301,7 +295,7 @@ public static List hmget(RedisNode node, String namespace, Class clazz jedis = getClient(node); value = jedis.hmget(namespace, key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hmget exception:{},{},{}",namespace,key,node); + Logs.REDIS.error("redis hmget exception:{},{},{}", namespace, key, node, e); } finally { // 返还到连接池 close(jedis); @@ -328,20 +322,20 @@ public static List hmget(RedisNode node, String namespace, Class clazz */ public static void hmset(List nodeList, String namespace, Map hash, Integer time) { - if(time == null){ - time = -1; - } + if (time == null) { + time = -1; + } for (RedisNode node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.hmset(namespace, hash); - if (time>0) { + if (time > 0) { jedis.expire(namespace, time); } } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis hmset exception:{},{},{}",namespace,time,node); + Logs.REDIS.error("redis hmset exception:{},{},{}", namespace, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -368,7 +362,7 @@ public static void lpush(List nodeList, String key, String value) { jedis = getClient(node); jedis.lpush(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lpush exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis lpush exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -394,7 +388,7 @@ public static void rpush(List nodeList, String key, String value) { jedis = getClient(node); jedis.rpush(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis rpush exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis rpush exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -419,7 +413,7 @@ public static T lpop(List nodeList, String key, Class clazz) { vaule = jedis.lpop(key); retValue = vaule; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lpop exception:{},{}",key,node); + Logs.REDIS.error("redis lpop exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -442,7 +436,7 @@ public static T rpop(List nodeList, String key, Class clazz) { vaule = jedis.rpop(key); retValue = vaule; } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis rpop exception:{},{}",key,node); + Logs.REDIS.error("redis rpop exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -464,7 +458,7 @@ public static List lrange(RedisNode node, String key, int start, int end, jedis = getClient(node); value = jedis.lrange(key, start, end); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lrange exception:{},{},{},{}",key,start,end,node); + Logs.REDIS.error("redis lrange exception:{},{},{},{}", key, start, end, node, e); } finally { // 返还到连接池 close(jedis); @@ -491,21 +485,22 @@ public static long llen(RedisNode node, String key) { jedis = getClient(node); len = jedis.llen(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis llen exception:{},{}",key,node); + Logs.REDIS.error("redis llen exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); } return len; } - + /** * 移除表中所有与 value 相等的值 + * * @param nodeList * @param key * @param value */ - public static void lRem(List nodeList, String key,String value) { + public static void lRem(List nodeList, String key, String value) { for (RedisNode node : nodeList) { Jedis jedis = null; @@ -513,7 +508,7 @@ public static void lRem(List nodeList, String key,String value) { jedis = getClient(node); jedis.lrem(key, 0, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis lrem exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis lrem exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -532,16 +527,16 @@ public static void lRem(List nodeList, String key,String value) { public static void publish(RedisNode node, String channel, T message) { Jedis jedis = null; String value = null; - if(message instanceof String){ - value = (String) message; - }else{ - value = Jsons.toJson(message); + if (message instanceof String) { + value = (String) message; + } else { + value = Jsons.toJson(message); } try { jedis = getClient(node); jedis.publish(channel, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis publish exception:{},{},{}",value,Jsons.toJson(node),Jsons.toJson(channel)); + Logs.REDIS.error("redis publish exception:{},{},{}", value, Jsons.toJson(node), Jsons.toJson(channel), e); } finally { // 返还到连接池 close(jedis); @@ -550,13 +545,13 @@ public static void publish(RedisNode node, String channel, T message) { public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { for (final RedisNode node : nodeList) { - String name = node.getIp()+"_"+Jsons.toJson(channels); - ThreadPoolManager.newThread(name, new Runnable() { - @Override - public void run() { - subscribe(node, pubsub, channels); - } - }).start(); + String name = node.getIp() + "_" + Jsons.toJson(channels); + ThreadPoolManager.newThread(name, new Runnable() { + @Override + public void run() { + subscribe(node, pubsub, channels); + } + }).start(); } } @@ -567,7 +562,7 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann jedis = getClient(node); jedis.subscribe(pubsub, channel); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:{},{}",Jsons.toJson(node),Jsons.toJson(channel)); + Logs.REDIS.error("redis subscribe exception:{},{}", Jsons.toJson(node), Jsons.toJson(channel), e); } finally { // 返还到连接池 close(jedis); @@ -591,21 +586,21 @@ public static void sAdd(List nodeList, String key, String value) { jedis = getClient(node); jedis.sadd(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis sadd exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis sadd exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); } } } - + /** * @param node 返回个数 * @param key * @param clazz * @return */ - public static Long sCard(RedisNode node, String key) { + public static Long sCard(RedisNode node, String key) { Long value = null; Jedis jedis = null; @@ -613,15 +608,15 @@ public static Long sCard(RedisNode node, String key) { jedis = getClient(node); value = jedis.scard(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis scard exception:{},{}",key,node); + Logs.REDIS.error("redis scard exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); } return value; } - - public static void sRem(List nodeList, String key,String value) { + + public static void sRem(List nodeList, String key, String value) { for (RedisNode node : nodeList) { Jedis jedis = null; @@ -629,7 +624,7 @@ public static void sRem(List nodeList, String key,String value) { jedis = getClient(node); jedis.srem(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis srem exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis srem exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -637,7 +632,7 @@ public static void sRem(List nodeList, String key,String value) { } } - + /** * 默认使用每页10个 * @@ -653,12 +648,12 @@ public static List sScan(RedisNode node, String key, Class clazz, int Jedis jedis = null; try { jedis = getClient(node); - ScanResult sscanResult = jedis.sscan(key, start+"",new ScanParams().count(10)); - if(sscanResult!=null&&sscanResult.getResult()!=null){ - value = sscanResult.getResult(); + ScanResult sscanResult = jedis.sscan(key, start + "", new ScanParams().count(10)); + if (sscanResult != null && sscanResult.getResult() != null) { + value = sscanResult.getResult(); } } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis sscan exception:{},{},{}",key,start,node); + Logs.REDIS.error("redis sscan exception:{},{},{}", key, start, node, e); } finally { // 返还到连接池 close(jedis); @@ -673,7 +668,7 @@ public static List sScan(RedisNode node, String key, Class clazz, int return null; } - + /********************* * sorted set ********************************/ @@ -681,7 +676,6 @@ public static List sScan(RedisNode node, String key, Class clazz, int * @param nodeList * @param key * @param value - * @param time seconds */ public static void zAdd(List nodeList, String key, String value) { for (RedisNode node : nodeList) { @@ -690,21 +684,21 @@ public static void zAdd(List nodeList, String key, String value) { jedis = getClient(node); jedis.zadd(key, 0, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis zadd exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis zadd exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); } } } - + /** * @param node 返回个数 * @param key * @param clazz * @return */ - public static Long zCard(RedisNode node, String key) { + public static Long zCard(RedisNode node, String key) { Long value = null; Jedis jedis = null; @@ -712,15 +706,15 @@ public static Long zCard(RedisNode node, String key) { jedis = getClient(node); value = jedis.zcard(key); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis zcard exception:{},{}",key,node); + Logs.REDIS.error("redis zcard exception:{},{}", key, node, e); } finally { // 返还到连接池 close(jedis); } return value; } - - public static void zRem(List nodeList, String key,String value) { + + public static void zRem(List nodeList, String key, String value) { for (RedisNode node : nodeList) { Jedis jedis = null; @@ -728,7 +722,7 @@ public static void zRem(List nodeList, String key,String value) { jedis = getClient(node); jedis.zrem(key, value); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis srem exception:{},{},{}",key,value,node); + Logs.REDIS.error("redis srem exception:{},{},{}", key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -736,7 +730,7 @@ public static void zRem(List nodeList, String key,String value) { } } - + /** * 从列表中获取指定返回的元素 start 和 end * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 @@ -749,7 +743,7 @@ public static List zrange(RedisNode node, String key, int start, int end, jedis = getClient(node); value = jedis.zrange(key, start, end); } catch (Exception e) { - LoggerManage.execption(LogType.REDIS, e, "redis zrange exception:{},{},{},{}",key,start,end,node); + Logs.REDIS.error("redis zrange exception:{},{},{},{}", key, start, end, node, e); } finally { // 返还到连接池 close(jedis); @@ -763,5 +757,5 @@ public static List zrange(RedisNode node, String key, int start, int end, } return null; } - + } diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java index 775992d9..71a5765d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java @@ -1,8 +1,8 @@ package com.mpush.tools.redis.jedis.services; import com.google.common.collect.Lists; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import com.mpush.tools.redis.RedisGroup; import com.mpush.tools.redis.RedisNode; import com.mpush.tools.Jsons; @@ -21,7 +21,7 @@ public class JedisRegisterManager implements RedisRegister { @Override public void init(List group) { if (group == null || group.isEmpty()) { - LoggerManage.log(LogType.REDIS, "init redis client error, redis server is none."); + Logs.REDIS.info("init redis client error, redis server is none."); throw new RuntimeException("init redis client error, redis server is none."); } groups = group; @@ -36,7 +36,7 @@ public List getGroupList() { private void printGroupList() { for (RedisGroup app : groups) { - LoggerManage.log(LogType.REDIS, Jsons.toJson(app)); + Logs.REDIS.info(Jsons.toJson(app)); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java index ae2361b6..428bf120 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java @@ -6,8 +6,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import com.mpush.tools.redis.manage.RedisManage; import com.mpush.tools.redis.pubsub.Subscriber; import com.mpush.tools.thread.threadpool.ThreadPoolManager; @@ -26,7 +26,7 @@ private ListenerDispatcher(){} public void onMessage(final String channel, final String message) { List listeners = subscribes.get(channel); if (listeners == null) { - LoggerManage.info(LogType.REDIS, "cannot find listener:%s,%s", channel,message); + Logs.REDIS.info("cannot find listener:%s,%s", channel,message); return; } for (final MessageListener listener : listeners) { diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java index b0b2c7f7..54534522 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java @@ -1,7 +1,7 @@ package com.mpush.tools.redis.pubsub; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import com.mpush.tools.Jsons; import com.mpush.tools.redis.listener.ListenerDispatcher; @@ -17,51 +17,51 @@ private Subscriber(){} @Override public void onMessage(String channel, String message) { - LoggerManage.log(LogType.REDIS, "onMessage:{},{}", channel,message); + Logs.REDIS.info("onMessage:{},{}", channel,message); dispatcher.onMessage(channel, message); super.onMessage(channel, message); } @Override public void onPMessage(String pattern, String channel, String message) { - LoggerManage.log(LogType.REDIS, "onPMessage:{},{},{}",pattern,channel,message); + Logs.REDIS.info("onPMessage:{},{},{}",pattern,channel,message); super.onPMessage(pattern, channel, message); } @Override public void onPSubscribe(String pattern, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onPSubscribe:{},{}",pattern,subscribedChannels); + Logs.REDIS.info("onPSubscribe:{},{}",pattern,subscribedChannels); super.onPSubscribe(pattern, subscribedChannels); } @Override public void onPUnsubscribe(String pattern, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onPUnsubscribe:{},{}",pattern,subscribedChannels); + Logs.REDIS.info("onPUnsubscribe:{},{}",pattern,subscribedChannels); super.onPUnsubscribe(pattern, subscribedChannels); } @Override public void onSubscribe(String channel, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onSubscribe:{},{}",channel,subscribedChannels); + Logs.REDIS.info("onSubscribe:{},{}",channel,subscribedChannels); super.onSubscribe(channel, subscribedChannels); } @Override public void onUnsubscribe(String channel, int subscribedChannels) { - LoggerManage.log(LogType.REDIS, "onUnsubscribe:{},{}",channel,subscribedChannels); + Logs.REDIS.info("onUnsubscribe:{},{}",channel,subscribedChannels); super.onUnsubscribe(channel, subscribedChannels); } @Override public void unsubscribe() { - LoggerManage.log(LogType.REDIS, "unsubscribe"); + Logs.REDIS.info("unsubscribe"); super.unsubscribe(); } @Override public void unsubscribe(String... channels) { - LoggerManage.log(LogType.REDIS, "unsubscribe:{}", Jsons.toJson(channels)); + Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); super.unsubscribe(channels); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index f867d36e..d727e523 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -1,7 +1,6 @@ package com.mpush.zk; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; import com.mpush.tools.ConsoleLog; import com.mpush.tools.Constants; import com.mpush.tools.MPushUtil; @@ -90,7 +89,7 @@ public List getAclForPath(final String path) { } initLocalCache(zkConfig.getLocalCachePath()); registerConnectionLostListener(); - LoggerManage.info(LogType.ZK, "zk client start success, server lists is:{}", zkConfig.getHosts()); + Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); ConsoleLog.i("init zk client success..."); } @@ -102,9 +101,9 @@ private void registerConnectionLostListener() { @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { if (ConnectionState.LOST == newState) { - LoggerManage.log(LogType.ZK, "{} lost connection", MPushUtil.getInetAddress()); + Logs.ZK.info("{} lost connection", MPushUtil.getInetAddress()); } else if (ConnectionState.RECONNECTED == newState) { - LoggerManage.log(LogType.ZK, "{} reconnected", MPushUtil.getInetAddress()); + Logs.ZK.info("{} reconnected", MPushUtil.getInetAddress()); } } }); @@ -162,7 +161,7 @@ public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Constants.UTF_8); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "getFromRemote:{}", key); + Logs.ZK.error("getFromRemote:{}", key, ex); return null; } } @@ -185,7 +184,7 @@ public int compare(final String o1, final String o2) { }); return result; } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "getChildrenKeys:{}", key); + Logs.ZK.error("getChildrenKeys:{}", key, ex); return Collections.emptyList(); } } @@ -200,7 +199,7 @@ public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "isExisted:{}", key); + Logs.ZK.error("isExisted:{}", key, ex); return false; } } @@ -219,7 +218,7 @@ public void registerPersist(final String key, final String value) { update(key, value); } } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persist:{},{}", key, value); + Logs.ZK.error("persist:{},{}", key, value, ex); throw new ZKException(ex); } } @@ -234,7 +233,7 @@ public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Constants.UTF_8)).and().commit(); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "update:{},{}", key, value); + Logs.ZK.error("update:{},{}", key, value, ex); throw new ZKException(ex); } } @@ -252,7 +251,7 @@ public void registerEphemeral(final String key, final String value) { } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Constants.UTF_8)); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeral:{},{}", key, value); + Logs.ZK.error("persistEphemeral:{},{}", key, value, ex); throw new ZKException(ex); } } @@ -266,7 +265,7 @@ public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{},{}", key, value); + Logs.ZK.error("persistEphemeralSequential:{},{}", key, value, ex); throw new ZKException(ex); } } @@ -280,7 +279,7 @@ public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "persistEphemeralSequential:{}", key); + Logs.ZK.error("persistEphemeralSequential:{}", key, ex); throw new ZKException(ex); } } @@ -294,7 +293,7 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - LoggerManage.execption(LogType.ZK, ex, "remove:{}", key); + Logs.ZK.error("remove:{}", key, ex); throw new ZKException(ex); } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java index 83ef0f7c..2945a837 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java @@ -1,7 +1,7 @@ package com.mpush.zk.listener; -import com.mpush.log.LogType; -import com.mpush.log.LoggerManage; +import com.mpush.log.Logs; + import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; @@ -14,7 +14,7 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc if (path.isEmpty()) { return; } - LoggerManage.log(LogType.ZK, "DataChangeListener:{},{},namespace:{}", path, listenerPath(), client.getNamespace()); + Logs.ZK.info("DataChangeListener:{},{},namespace:{}", path, listenerPath(), client.getNamespace()); if (path.startsWith(listenerPath())) { dataChanged(client, event, path); } From b82a0fc7a8d5a2221182e3739d2c75b5a7495a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 16 May 2016 12:32:10 +0200 Subject: [PATCH 539/890] =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-log/src/main/java/com/mpush/log/Logs.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mpush-log/src/main/java/com/mpush/log/Logs.java b/mpush-log/src/main/java/com/mpush/log/Logs.java index 04a5d949..d9504ab2 100644 --- a/mpush-log/src/main/java/com/mpush/log/Logs.java +++ b/mpush-log/src/main/java/com/mpush/log/Logs.java @@ -4,6 +4,11 @@ import org.slf4j.LoggerFactory; import org.slf4j.Marker; +/** + * Created by ohun on 2016/5/16. + * + * @author ohun@live.cn + */ public enum Logs implements Logger { Conn("connectionLog"), PUSH("pushLog"), From 4bcce87602c9dc06ad3830212e8f56fc1746e5a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 17 May 2016 03:54:13 +0200 Subject: [PATCH 540/890] =?UTF-8?q?log=20=E6=A8=A1=E5=9D=97=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-log/src/main/java/com/mpush/App.java | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 mpush-log/src/main/java/com/mpush/App.java diff --git a/mpush-log/src/main/java/com/mpush/App.java b/mpush-log/src/main/java/com/mpush/App.java deleted file mode 100644 index 0a7102ea..00000000 --- a/mpush-log/src/main/java/com/mpush/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mpush; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} From c77f433d814c17b6fecfb854218935bc50c015ce Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Mon, 16 May 2016 21:46:26 +0800 Subject: [PATCH 541/890] set log from warn to debug --- .../com/mpush/conn/client/ClientChannelHandler.java | 10 +++++----- .../main/java/com/mpush/netty/client/NettyClient.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java index 238d12ad..4200b5ad 100644 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java @@ -66,7 +66,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); client.startHeartBeat(message.heartbeat); - LOGGER.warn("会话密钥:{},message={}", sessionKey, message); + LOGGER.debug("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); } else if (command == Command.FAST_CONNECT) { @@ -79,7 +79,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); client.startHeartBeat(message.heartbeat); bindUser(securityNettyClient); - LOGGER.warn("fast connect success, message=" + message); + LOGGER.debug("fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); @@ -89,7 +89,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.error("receive an error packet=" + errorMessage); } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.warn("receive an success packet=" + okMessage); + LOGGER.debug("receive an success packet=" + okMessage); HttpRequestMessage message = new HttpRequestMessage(connection); message.uri = "http://baidu.com"; message.send(); @@ -97,7 +97,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception PushMessage message = new PushMessage(packet, connection); LOGGER.warn("receive an push message, content=" + message.content); } else if (command == Command.HEARTBEAT) { - LOGGER.warn("receive a heartbeat pong..."); + LOGGER.debug("receive a heartbeat pong..."); } else { LOGGER.warn("receive a message, type=" + command + "," + packet); } @@ -106,7 +106,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (client instanceof NettyClient) {//不加密 } - LOGGER.warn("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); + LOGGER.debug("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); } @Override diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 7929ff42..0a214a21 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -136,7 +136,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } log.warn("client send msg hb false:" + channel.remoteAddress().toString()); } else { - log.warn("client send msg hb success:" + channel.remoteAddress().toString()); + log.debug("client send msg hb success:" + channel.remoteAddress().toString()); } } }); From b9026177168e96099a14930b23ed38ee5aa7261f Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Mon, 16 May 2016 21:46:26 +0800 Subject: [PATCH 542/890] set log from warn to debug --- .../com/mpush/conn/client/ClientChannelHandler.java | 10 +++++----- .../main/java/com/mpush/netty/client/NettyClient.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java index 238d12ad..4200b5ad 100644 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java @@ -66,7 +66,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); client.startHeartBeat(message.heartbeat); - LOGGER.warn("会话密钥:{},message={}", sessionKey, message); + LOGGER.debug("会话密钥:{},message={}", sessionKey, message); bindUser(securityNettyClient); saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); } else if (command == Command.FAST_CONNECT) { @@ -79,7 +79,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); client.startHeartBeat(message.heartbeat); bindUser(securityNettyClient); - LOGGER.warn("fast connect success, message=" + message); + LOGGER.debug("fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); @@ -89,7 +89,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.error("receive an error packet=" + errorMessage); } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.warn("receive an success packet=" + okMessage); + LOGGER.debug("receive an success packet=" + okMessage); HttpRequestMessage message = new HttpRequestMessage(connection); message.uri = "http://baidu.com"; message.send(); @@ -97,7 +97,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception PushMessage message = new PushMessage(packet, connection); LOGGER.warn("receive an push message, content=" + message.content); } else if (command == Command.HEARTBEAT) { - LOGGER.warn("receive a heartbeat pong..."); + LOGGER.debug("receive a heartbeat pong..."); } else { LOGGER.warn("receive a message, type=" + command + "," + packet); } @@ -106,7 +106,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (client instanceof NettyClient) {//不加密 } - LOGGER.warn("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); + LOGGER.debug("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); } @Override diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 7929ff42..0a214a21 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -136,7 +136,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } log.warn("client send msg hb false:" + channel.remoteAddress().toString()); } else { - log.warn("client send msg hb success:" + channel.remoteAddress().toString()); + log.debug("client send msg hb success:" + channel.remoteAddress().toString()); } } }); From f72563ad532848a4734b9cdf3140ae573be9c7c0 Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Tue, 17 May 2016 16:18:14 +0800 Subject: [PATCH 543/890] ignore --- .gitignore | 20 +++++++ conf/conf-dev.properties | 26 ---------- mpush-api/.gitignore | 1 + mpush-boot/.gitignore | 1 + mpush-client/.gitignore | 1 + mpush-common/.gitignore | 1 + mpush-core/.gitignore | 1 + mpush-log/.gitignore | 1 + mpush-monitor/.gitignore | 1 + mpush-netty/.gitignore | 1 + mpush-test/.gitignore | 1 + .../mpush/test/connection/client/Main.java | 52 ------------------- .../com/mpush/test/connection/mpns/Main.java | 52 ------------------- .../test/java/com/mpush/test/push/Main.java | 52 ------------------- .../src/test/resources/config.properties | 35 ------------- mpush-tools/.gitignore | 1 + mpush-zk/.gitignore | 1 + 17 files changed, 31 insertions(+), 217 deletions(-) delete mode 100644 conf/conf-dev.properties create mode 100644 mpush-api/.gitignore create mode 100644 mpush-boot/.gitignore create mode 100644 mpush-common/.gitignore create mode 100644 mpush-core/.gitignore create mode 100644 mpush-log/.gitignore create mode 100644 mpush-monitor/.gitignore create mode 100644 mpush-netty/.gitignore create mode 100644 mpush-test/.gitignore delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/Main.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/push/Main.java delete mode 100644 mpush-test/src/test/resources/config.properties create mode 100644 mpush-tools/.gitignore create mode 100644 mpush-zk/.gitignore diff --git a/.gitignore b/.gitignore index a0043cc4..3c74f152 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,23 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +mpush-test/src/test/resources/config.properties + +build.sh + +*.prefs + +conf/conf-dev.properties + +*.properties + +mpush-test/src/test/java/com/mpush/test/connection/client/Main.java + +mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java + +mpush-test/src/test/java/com/mpush/test/push/Main.java + +*.properties + +conf/conf-dev.properties diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties deleted file mode 100644 index 6d29e48a..00000000 --- a/conf/conf-dev.properties +++ /dev/null @@ -1,26 +0,0 @@ -#日志根目录 -log.home=/tmp/logs/mpush -#日志级别 -loglevel=debug -#zk配置 -zk_ip=127.0.0.1:2181 -zk_digest=shinemoIpo -zk_namespace=mpush-daily -#redis配置多个用,隔开 -redis_group=111.1.57.148:6379:ShineMoIpo -#私钥 -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -#公钥 -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info=true -#长连服务端口 -connection_server_port=3000 -#网关服务端口 -gateway_server_port=4000 -#控制台服务端口 -admin_port=4001 -skip_dump=true -dns_mapping=111.1.57.148=127.0.0.1 -#本机Ip和外网Ip的映射 -remote_ip_mapping=127.0.0.1:111.1.57.148 - diff --git a/mpush-api/.gitignore b/mpush-api/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-api/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-boot/.gitignore b/mpush-boot/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-boot/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-client/.gitignore b/mpush-client/.gitignore index ae3c1726..09e3bc9b 100644 --- a/mpush-client/.gitignore +++ b/mpush-client/.gitignore @@ -1 +1,2 @@ /bin/ +/target/ diff --git a/mpush-common/.gitignore b/mpush-common/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-common/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-core/.gitignore b/mpush-core/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-core/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-log/.gitignore b/mpush-log/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-log/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-monitor/.gitignore b/mpush-monitor/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-monitor/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-netty/.gitignore b/mpush-netty/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-netty/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-test/.gitignore b/mpush-test/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-test/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java deleted file mode 100644 index e0a0aea7..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.connection.client; - -import com.mpush.conn.client.ClientChannelHandler; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.zk.ZKServerNode; -import com.mpush.common.security.CipherBox; -import com.mpush.netty.client.SecurityNettyClient; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class Main { - - public static void main(String[] args) throws Exception { - ConnectTestClient main = new ConnectTestClient(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for(int i = 0;i<1;i++){ - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "user-"+i; - String deviceId = "test-device-id-"+i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(10); - } - - LockSupport.park(); - - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java deleted file mode 100644 index 9de9a50e..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.connection.mpns; - -import com.mpush.common.security.CipherBox; -import com.mpush.conn.client.ClientChannelHandler; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.netty.client.SecurityNettyClient; -import com.mpush.zk.ZKServerNode; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class Main { - - public static void main(String[] args) throws InterruptedException { - - ConnectTestClient main = new ConnectTestClient(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for (int i = 0; i < 1000; i++) { - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "uh-" + i; - String deviceId = "test-device-id-" + i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(100); - } - - LockSupport.park(); - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/Main.java deleted file mode 100644 index 38b6b120..00000000 --- a/mpush-test/src/test/java/com/mpush/test/push/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.push; - -import com.mpush.api.PushContent; -import com.mpush.api.PushContent.PushType; -import com.mpush.api.PushSender; -import com.mpush.push.PushClient; -import com.mpush.tools.Jsons; - -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; - -/** - * Created by ohun on 2016/1/7. - * - * @author ohun@live.cn - */ -public class Main { - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); - content.setMsgId("msgId_" + (i % 2)); - - client.send(Jsons.toJson(content), Arrays.asList("doctor43test", "user-0", "huang"), new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - }); - Thread.sleep(10000); - } - LockSupport.park(); - } - -} \ No newline at end of file diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties deleted file mode 100644 index 23e53825..00000000 --- a/mpush-test/src/test/resources/config.properties +++ /dev/null @@ -1,35 +0,0 @@ -## -max_packet_size=10240 -## -compress_limit=10240 -## -min_heartbeat=10000 -## -max_heartbeat=180000 -## -max_hb_timeout_times=2 -## -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port=20882 -## -connection_server_port=3000 -## -aes_key_length=16 -## -ras_key_length=1024 -## -session_expired_time=86400 -#zk_ip = 115.29.169.109:5666 -zk_ip=127.0.0.1:2181 -zk_namespace=mpush-daily -zk_digest=shinemoIpo -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -#redis_group = 111.1.57.148:6379:ShineMoIpo -redis_group=111.1.57.148:6379:ShineMoIpo -force_write_redis_group_info=false -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 -#IpIpӳ -remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/mpush-tools/.gitignore b/mpush-tools/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-tools/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-zk/.gitignore b/mpush-zk/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-zk/.gitignore @@ -0,0 +1 @@ +/target/ From 19b98cfe5b8ae4a839ade1448f600b5b4d19eb52 Mon Sep 17 00:00:00 2001 From: maksim <154536744@qq.com> Date: Tue, 17 May 2016 16:18:14 +0800 Subject: [PATCH 544/890] ignore --- .gitignore | 20 +++++++ conf/conf-dev.properties | 26 ---------- mpush-api/.gitignore | 1 + mpush-boot/.gitignore | 1 + mpush-client/.gitignore | 1 + mpush-common/.gitignore | 1 + mpush-core/.gitignore | 1 + mpush-log/.gitignore | 1 + mpush-monitor/.gitignore | 1 + mpush-netty/.gitignore | 1 + mpush-test/.gitignore | 1 + .../mpush/test/connection/client/Main.java | 52 ------------------- .../com/mpush/test/connection/mpns/Main.java | 52 ------------------- .../test/java/com/mpush/test/push/Main.java | 52 ------------------- .../src/test/resources/config.properties | 35 ------------- mpush-tools/.gitignore | 1 + mpush-zk/.gitignore | 1 + 17 files changed, 31 insertions(+), 217 deletions(-) delete mode 100644 conf/conf-dev.properties create mode 100644 mpush-api/.gitignore create mode 100644 mpush-boot/.gitignore create mode 100644 mpush-common/.gitignore create mode 100644 mpush-core/.gitignore create mode 100644 mpush-log/.gitignore create mode 100644 mpush-monitor/.gitignore create mode 100644 mpush-netty/.gitignore create mode 100644 mpush-test/.gitignore delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/Main.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/push/Main.java delete mode 100644 mpush-test/src/test/resources/config.properties create mode 100644 mpush-tools/.gitignore create mode 100644 mpush-zk/.gitignore diff --git a/.gitignore b/.gitignore index a0043cc4..3c74f152 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,23 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +mpush-test/src/test/resources/config.properties + +build.sh + +*.prefs + +conf/conf-dev.properties + +*.properties + +mpush-test/src/test/java/com/mpush/test/connection/client/Main.java + +mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java + +mpush-test/src/test/java/com/mpush/test/push/Main.java + +*.properties + +conf/conf-dev.properties diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties deleted file mode 100644 index 6d29e48a..00000000 --- a/conf/conf-dev.properties +++ /dev/null @@ -1,26 +0,0 @@ -#日志根目录 -log.home=/tmp/logs/mpush -#日志级别 -loglevel=debug -#zk配置 -zk_ip=127.0.0.1:2181 -zk_digest=shinemoIpo -zk_namespace=mpush-daily -#redis配置多个用,隔开 -redis_group=111.1.57.148:6379:ShineMoIpo -#私钥 -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -#公钥 -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info=true -#长连服务端口 -connection_server_port=3000 -#网关服务端口 -gateway_server_port=4000 -#控制台服务端口 -admin_port=4001 -skip_dump=true -dns_mapping=111.1.57.148=127.0.0.1 -#本机Ip和外网Ip的映射 -remote_ip_mapping=127.0.0.1:111.1.57.148 - diff --git a/mpush-api/.gitignore b/mpush-api/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-api/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-boot/.gitignore b/mpush-boot/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-boot/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-client/.gitignore b/mpush-client/.gitignore index ae3c1726..09e3bc9b 100644 --- a/mpush-client/.gitignore +++ b/mpush-client/.gitignore @@ -1 +1,2 @@ /bin/ +/target/ diff --git a/mpush-common/.gitignore b/mpush-common/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-common/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-core/.gitignore b/mpush-core/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-core/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-log/.gitignore b/mpush-log/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-log/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-monitor/.gitignore b/mpush-monitor/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-monitor/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-netty/.gitignore b/mpush-netty/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-netty/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-test/.gitignore b/mpush-test/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-test/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java deleted file mode 100644 index e0a0aea7..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.connection.client; - -import com.mpush.conn.client.ClientChannelHandler; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.zk.ZKServerNode; -import com.mpush.common.security.CipherBox; -import com.mpush.netty.client.SecurityNettyClient; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class Main { - - public static void main(String[] args) throws Exception { - ConnectTestClient main = new ConnectTestClient(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for(int i = 0;i<1;i++){ - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "user-"+i; - String deviceId = "test-device-id-"+i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(10); - } - - LockSupport.park(); - - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java deleted file mode 100644 index 9de9a50e..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.connection.mpns; - -import com.mpush.common.security.CipherBox; -import com.mpush.conn.client.ClientChannelHandler; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.netty.client.SecurityNettyClient; -import com.mpush.zk.ZKServerNode; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class Main { - - public static void main(String[] args) throws InterruptedException { - - ConnectTestClient main = new ConnectTestClient(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for (int i = 0; i < 1000; i++) { - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "uh-" + i; - String deviceId = "test-device-id-" + i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(100); - } - - LockSupport.park(); - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/Main.java deleted file mode 100644 index 38b6b120..00000000 --- a/mpush-test/src/test/java/com/mpush/test/push/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.push; - -import com.mpush.api.PushContent; -import com.mpush.api.PushContent.PushType; -import com.mpush.api.PushSender; -import com.mpush.push.PushClient; -import com.mpush.tools.Jsons; - -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; - -/** - * Created by ohun on 2016/1/7. - * - * @author ohun@live.cn - */ -public class Main { - public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { - PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); - content.setMsgId("msgId_" + (i % 2)); - - client.send(Jsons.toJson(content), Arrays.asList("doctor43test", "user-0", "huang"), new PushSender.Callback() { - @Override - public void onSuccess(String userId) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId) { - System.err.println("push onOffline userId=" + userId); - } - - @Override - public void onTimeout(String userId) { - System.err.println("push onTimeout userId=" + userId); - } - }); - Thread.sleep(10000); - } - LockSupport.park(); - } - -} \ No newline at end of file diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties deleted file mode 100644 index 23e53825..00000000 --- a/mpush-test/src/test/resources/config.properties +++ /dev/null @@ -1,35 +0,0 @@ -## -max_packet_size=10240 -## -compress_limit=10240 -## -min_heartbeat=10000 -## -max_heartbeat=180000 -## -max_hb_timeout_times=2 -## -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port=20882 -## -connection_server_port=3000 -## -aes_key_length=16 -## -ras_key_length=1024 -## -session_expired_time=86400 -#zk_ip = 115.29.169.109:5666 -zk_ip=127.0.0.1:2181 -zk_namespace=mpush-daily -zk_digest=shinemoIpo -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -#redis_group = 111.1.57.148:6379:ShineMoIpo -redis_group=111.1.57.148:6379:ShineMoIpo -force_write_redis_group_info=false -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 -#IpIpӳ -remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/mpush-tools/.gitignore b/mpush-tools/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-tools/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mpush-zk/.gitignore b/mpush-zk/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/mpush-zk/.gitignore @@ -0,0 +1 @@ +/target/ From 1ba5e70d10820ffe576600e2670d6a0497009105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 22 May 2016 15:57:40 +0200 Subject: [PATCH 545/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/daily-pub.py | 214 -------- bin/debug.sh | 19 - bin/mp-env.cmd | 49 ++ bin/mp-env.sh | 122 +++++ bin/mp.cmd | 24 + bin/mp.sh | 229 +++++++++ bin/pub-daily.py | 239 --------- bin/pub-online.py | 239 --------- bin/pub-pre.py | 101 ---- bin/run.sh | 20 - bin/start.sh | 23 - bin/test.py | 69 --- bin/test.sh | 11 - conf/conf-daily.properties | 16 - conf/conf-pre.properties | 15 - ...-online.properties => conf-pub.properties} | 0 conf/reference.conf | 142 ++++++ .../main/java/com/mpush/api/BaseService.java | 30 ++ .../src/main/java/com/mpush/api/Client.java | 36 +- .../main/java/com/mpush/api/Constants.java | 1 + .../src/main/java/com/mpush/api/Server.java | 15 +- .../src/main/java/com/mpush/api/Service.java | 23 + .../com/mpush/api/connection/Connection.java | 9 +- .../mpush/api/exception/BootException.java | 12 + .../com/mpush/api/{ => push}/PushContent.java | 2 +- .../com/mpush/api/{ => push}/PushSender.java | 16 +- .../main/java/com/mpush/api/spi/Factory.java | 10 + .../java/com/mpush/api/spi/SpiLoader.java | 6 +- .../mpush/api/spi/client/PusherFactory.java | 13 + .../api/spi/common/ThreadPoolFactory.java | 20 + .../com/mpush/api/spi/core/CipherFactory.java | 13 + mpush-boot/assembly.xml | 32 +- mpush-boot/pom.xml | 6 +- .../java/com/mpush/boot/HttpProxyBoot.java | 19 - .../main/java/com/mpush/{ => boot}/Main.java | 8 +- .../main/java/com/mpush/boot/MonitorBoot.java | 17 - .../main/java/com/mpush/boot/RedisBoot.java | 46 -- .../com/mpush/{ => boot}/ServerLauncher.java | 14 +- .../src/main/java/com/mpush/boot/ZKBoot.java | 45 -- .../com/mpush/boot/{ => job}/BootChain.java | 6 +- .../com/mpush/boot/{ => job}/BootJob.java | 6 +- .../com/mpush/boot/job/HttpProxyBoot.java | 20 + .../com/mpush/boot/{ => job}/LastBoot.java | 10 +- .../java/com/mpush/boot/job/MonitorBoot.java | 20 + .../java/com/mpush/boot/job/RedisBoot.java | 17 + .../com/mpush/boot/{ => job}/ServerBoot.java | 28 +- .../main/java/com/mpush/boot/job/ZKBoot.java | 21 + .../src/main/resources/config.properties | 37 -- mpush-boot/src/main/resources/logback.xml | 209 -------- mpush-boot/src/main/resources/mpush.conf | 2 + .../ConnectionServerApplicationTest.java | 13 - .../java/com/mpush/zk/ServerManageTest.java | 80 --- .../src/test/java/com/mpush/zk/ZkTest.java | 16 - .../test/java/com/mpush/zk/ZkUtilTest.java | 148 ------ mpush-cache/pom.xml | 27 + .../com/mpush/cache/redis/RedisClient.java | 190 ++++--- .../com/mpush/cache/redis/RedisGroup.java | 50 ++ .../java/com/mpush/cache/redis}/RedisKey.java | 2 +- .../com/mpush/cache/redis/RedisServer.java | 11 + .../cache/redis/hash}/ConsistentHash.java | 2 +- .../com/mpush/cache/redis/hash}/Node.java | 2 +- .../redis/listener/ListenerDispatcher.java | 22 +- .../redis/listener/MessageListener.java | 2 +- .../redis/manager/RedisClusterManager.java | 17 + .../cache/redis/manager/RedisManager.java | 299 +++++++++++ .../redis/manager/ZKRedisClusterManager.java | 109 +++++ .../com/mpush/cache/redis/mq}/Subscriber.java | 10 +- mpush-client/pom.xml | 34 +- .../mpush/client/connect/ClientConfig.java | 79 +++ .../connect/ConnClientChannelHandler.java | 225 +++++++++ .../mpush/client/connect/ConnectClient.java | 20 + .../mpush/client/gateway/GatewayClient.java | 27 + .../client/gateway/GatewayClientBoot.java | 20 + .../gateway/GatewayClientChannelHandler.java} | 53 +- .../client/gateway/GatewayClientFactory.java | 86 ++++ .../com/mpush/client/push/PushClient.java | 70 +++ .../mpush/client/push/PushClientFactory.java | 23 + .../mpush/{ => client}/push/PushRequest.java | 4 +- .../{ => client}/push/PushRequestBus.java | 2 +- .../conn/client/ClientChannelHandler.java | 223 --------- .../java/com/mpush/push/ConnectClient.java | 12 - .../main/java/com/mpush/push/PushClient.java | 47 -- .../push/zk/listener/ConnectZKListener.java | 35 -- .../push/zk/listener/GatewayZKListener.java | 35 -- .../push/zk/manager/ConnectZKNodeManager.java | 47 -- .../push/zk/manager/GatewayZKNodeManager.java | 83 ---- .../com.mpush.api.spi.client.PusherFactory | 1 + .../services/com.mpush.zk.ZKNodeManager | 2 - .../java/com/mpush/client/PushClientTest.java | 50 -- mpush-client/src/test/resources/logback.xml | 30 -- mpush-common/pom.xml | 6 +- .../java/com/mpush/common/AbstractClient.java | 56 --- .../mpush/common/AbstractEventContainer.java | 9 - .../com/mpush/common/MessageDispatcher.java | 4 +- .../com/mpush/common/message/BaseMessage.java | 4 +- .../mpush/common/net/DnsMappingManager.java | 101 ++++ .../common/router/RemoteRouterManager.java | 14 +- .../common/router/UserChangeListener.java | 46 +- .../com/mpush/common/security/AesCipher.java | 2 +- .../com/mpush/common/security/CipherBox.java | 14 +- .../com/mpush/common/security/RsaCipher.java | 40 +- .../common/security/RsaCipherFactory.java | 18 + .../common/{manage => }/user/UserManager.java | 22 +- .../com.mpush.api.spi.core.CipherFactory | 1 + mpush-core/pom.xml | 32 +- .../com/mpush/core/handler/AdminHandler.java | 45 +- .../mpush/core/handler/BindUserHandler.java | 6 +- .../core/handler/FastConnectHandler.java | 6 +- .../core/handler/GatewayPushHandler.java | 2 +- .../mpush/core/handler/HandshakeHandler.java | 22 +- .../mpush/core/handler/HeartBeatHandler.java | 2 +- .../mpush/core/handler/HttpProxyHandler.java | 14 +- .../mpush/core/handler/UnbindUserHandler.java | 6 +- .../mpush/core/router/LocalRouterManager.java | 9 +- .../com/mpush/core/router/RouterCenter.java | 6 +- .../core/router/RouterChangeListener.java | 22 +- .../router/UserOnlineOfflineListener.java | 12 +- .../mpush/core/server/ConnectionServer.java | 15 +- .../com/mpush/core/server/GatewayServer.java | 5 +- .../core/server/ServerChannelHandler.java | 6 +- .../core/server/ServerConnectionManager.java | 20 +- .../core/session/ReusableSessionManager.java | 16 +- .../com/mpush/core/netty/NettyServerTest.java | 2 +- .../mpush/core/security/CipherBoxTest.java | 6 +- mpush-log/pom.xml | 41 -- .../src/main/java/com/mpush/log/Logs.java | 331 ------------- .../src/test/java/com/mpush/AppTest.java | 38 -- mpush-monitor/pom.xml | 22 +- .../MonitorResult.java} | 6 +- .../mpush/monitor/data/ResultCollector.java | 22 + .../com/mpush/monitor/quota/BaseQuota.java | 29 +- .../com/mpush/monitor/quota/impl/JVMGC.java | 261 +++++----- .../com/mpush/monitor/quota/impl/JVMInfo.java | 2 +- .../mpush/monitor/quota/impl/JVMMemory.java | 463 +++++++++--------- .../mpush/monitor/quota/impl/JVMThread.java | 2 +- .../monitor/quota/impl/JVMThreadPool.java | 60 +-- .../monitor/service/MonitorDataCollector.java | 106 ---- .../mpush/monitor/service/MonitorService.java | 105 ++++ mpush-netty/pom.xml | 6 +- .../netty/client/ChannelClientHandler.java | 11 - .../com/mpush/netty/client/NettyClient.java | 199 +++----- .../netty/client/NettyClientFactory.java | 75 --- .../netty/client/SecurityNettyClient.java | 86 ---- .../com/mpush/netty/codec/PacketDecoder.java | 5 +- .../netty/connection/NettyConnection.java | 26 +- .../netty/{client => http}/HttpCallback.java | 2 +- .../netty/{client => http}/HttpClient.java | 2 +- .../{client => http}/NettyHttpClient.java | 16 +- .../netty/{client => http}/RequestInfo.java | 6 +- .../com/mpush/netty/server/NettyServer.java | 12 +- .../mpush/netty/util/NettySharedHolder.java | 18 - mpush-test/pom.xml | 10 - .../src/test/java/com/mpush/AppTest.java | 38 -- .../mpush/test/client/ConnClientTestMain.java | 53 ++ .../mpush/test/client/ConnectClientBoot.java | 24 + .../test/configcenter/ConfigCenterTest.java | 105 +++- .../connection/client/ConnectTestClient.java | 20 - .../mpush/test/connection/client/Main.java | 52 -- .../connection/mpns/ConnClientTestMain.java | 50 ++ .../connection/mpns/ConnectTestClient.java | 20 - .../mpns/ConnectTestClientBoot.java | 19 + .../com/mpush/test/connection/mpns/Main.java | 52 -- .../java/com/mpush/test/crypto/RsaTest.java | 8 +- .../java/com/mpush/test/gson/GsonTest.java | 4 +- .../{Main.java => PushClientTestMain.java} | 17 +- .../mpush/test/redis/ConsistentHashTest.java | 4 +- .../java/com/mpush/test/redis/PubSubTest.java | 93 ++-- .../mpush/test/redis/RedisClusterTest.java | 64 ++- .../test/redis/RedisGroupManageTest.java | 109 ----- .../com/mpush/test/redis/RedisUtilTest.java | 367 +++++++------- .../Main.java => sever/ServerTestMain.java} | 8 +- .../java/com/mpush/test/util/TelnetTest.java | 4 +- .../src/test/resources/application.conf | 3 + .../src/test/resources/config.properties | 35 -- mpush-test/src/test/resources/logback.xml | 30 -- .../services/com.mpush.api.spi.PusherFactory | 1 + mpush-tools/pom.xml | 76 +-- .../main/java/com/mpush/tools/ConsoleLog.java | 32 -- .../main/java/com/mpush/tools/Constants.java | 41 -- .../main/java/com/mpush/tools/IOUtils.java | 1 + .../main/java/com/mpush/tools/JVMUtil.java | 31 +- .../src/main/java/com/mpush/tools/Jsons.java | 2 +- .../main/java/com/mpush/tools/MPushUtil.java | 43 +- .../main/java/com/mpush/tools/config/CC.java | 263 ++++++++++ .../com/mpush/tools/config/ConfigCenter.java | 13 +- .../com/mpush/tools/config/ConfigManager.java | 36 ++ .../tools/config/DnsMappingConverter.java | 48 -- .../tools/config/RedisGroupConverter.java | 43 -- .../config/converter/DnsMappingConverter.java | 45 ++ .../config/{ => converter}/MapConverter.java | 8 +- .../config/converter/RedisGroupConverter.java | 35 ++ .../mpush/tools/config/data/DnsMapping.java | 37 ++ .../mpush/tools/config/data/RedisGroup.java | 26 + .../mpush/tools/config/data/RedisServer.java | 52 ++ .../com/mpush/tools/crypto/Base64Utils.java | 3 +- .../java/com/mpush/tools/crypto/MD5Utils.java | 2 +- .../java/com/mpush/tools/crypto/RSAUtils.java | 2 +- .../java/com/mpush/tools/dns/DnsMapping.java | 25 - .../tools/dns/manage/DnsMappingManage.java | 99 ---- .../java/com/mpush/tools/event/Event.java | 21 - .../java/com/mpush/tools/event}/EventBus.java | 14 +- .../com/mpush/tools/event/EventConsumer.java | 9 + .../mpush/tools/event/EventDispatcher.java | 19 - .../com/mpush/tools/event/EventListener.java | 7 - .../java/com/mpush/tools/event/EventType.java | 5 - .../main/java/com/mpush/tools/log/Logs.java | 41 ++ .../com/mpush/tools/redis/RedisGroup.java | 49 -- .../java/com/mpush/tools/redis/RedisNode.java | 41 -- .../mpush/tools/redis/RedisPoolConfig.java | 35 -- .../com/mpush/tools/redis/RedisRegister.java | 17 - .../jedis/services/JedisRegisterManager.java | 78 --- .../mpush/tools/redis/manage/RedisManage.java | 289 ----------- .../main/java/com/mpush/tools/spi/SPI.java | 14 - .../tools/thread/NamedThreadFactory.java | 70 ++- .../mpush/tools/thread/PoolThreadFactory.java | 42 ++ .../mpush/tools/thread/ThreadNameSpace.java | 46 -- .../com/mpush/tools/thread/ThreadNames.java | 32 ++ .../thread/pool/DefaultThreadPoolFactory.java | 88 ++++ .../pool/DumpThreadRejectedHandler.java | 40 ++ .../tools/thread/pool/ThreadPoolConfig.java | 104 ++++ .../tools/thread/pool/ThreadPoolManager.java | 93 ++++ .../thread/threadpool/IgnoreRunsPolicy.java | 42 -- .../tools/thread/threadpool/ThreadPool.java | 12 - .../thread/threadpool/ThreadPoolContext.java | 63 --- .../thread/threadpool/ThreadPoolManager.java | 45 -- .../threadpool/cached/CachedThreadPool.java | 46 -- .../cached/CachedThreadPoolContext.java | 23 - .../threadpool/fixed/FixedThreadPool.java | 42 -- .../fixed/FixedThreadPoolContext.java | 23 - ...com.mpush.api.spi.common.ThreadPoolFactory | 1 + .../com.mpush.tools.redis.RedisRegister | 1 - mpush-tools/src/main/resources/logback.xml | 220 +++++++++ .../com/mpush/tools/crypto/AESUtilsTest.java | 2 +- .../java/com/mpush/tools/owner/OwnerTest.java | 70 --- .../com/mpush/tools/owner/ServerConfig.java | 30 -- .../java/com/mpush/tools/spi/SpiTest.java | 60 --- .../com/mpush/tools/spi/test/TestService.java | 5 +- .../java/com/mpush/tools/thread/SyncTest.java | 145 ------ .../src/main/java/com/mpush/zk/ZKClient.java | 49 +- .../src/main/java/com/mpush/zk/ZKConfig.java | 51 +- .../main/java/com/mpush/zk/ZKNodeManager.java | 16 - .../src/main/java/com/mpush/zk/ZKPath.java | 20 +- .../java/com/mpush/zk/cache/ZKNodeCache.java | 20 + .../com/mpush/zk/cache/ZKRedisNodeCache.java | 43 ++ .../com/mpush/zk/cache/ZKServerNodeCache.java | 62 +++ .../zk/listener/ZKDataChangeListener.java | 28 -- .../mpush/zk/listener/ZKNodeCacheWatcher.java | 57 +++ .../zk/listener/ZKRedisNodeListener.java | 85 ---- .../mpush/zk/listener/ZKRedisNodeWatcher.java | 60 +++ .../zk/listener/ZKServerNodeListener.java | 79 --- .../zk/listener/ZKServerNodeWatcher.java | 90 ++++ .../main/java/com/mpush/zk/node/ZKNode.java | 10 + .../java/com/mpush/zk/node/ZKRedisNode.java | 26 + .../com/mpush/zk/{ => node}/ZKServerNode.java | 26 +- pom.xml | 104 ++-- 255 files changed, 5307 insertions(+), 6708 deletions(-) delete mode 100644 bin/daily-pub.py delete mode 100644 bin/debug.sh create mode 100644 bin/mp-env.cmd create mode 100644 bin/mp-env.sh create mode 100644 bin/mp.cmd create mode 100644 bin/mp.sh delete mode 100644 bin/pub-daily.py delete mode 100644 bin/pub-online.py delete mode 100644 bin/pub-pre.py delete mode 100644 bin/run.sh delete mode 100644 bin/start.sh delete mode 100644 bin/test.py delete mode 100644 bin/test.sh delete mode 100644 conf/conf-daily.properties delete mode 100644 conf/conf-pre.properties rename conf/{conf-online.properties => conf-pub.properties} (100%) create mode 100644 conf/reference.conf create mode 100644 mpush-api/src/main/java/com/mpush/api/BaseService.java create mode 100644 mpush-api/src/main/java/com/mpush/api/Service.java create mode 100644 mpush-api/src/main/java/com/mpush/api/exception/BootException.java rename mpush-api/src/main/java/com/mpush/api/{ => push}/PushContent.java (98%) rename mpush-api/src/main/java/com/mpush/api/{ => push}/PushSender.java (59%) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/Factory.java rename mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java => mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java (91%) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java rename mpush-boot/src/main/java/com/mpush/{ => boot}/Main.java (70%) delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java rename mpush-boot/src/main/java/com/mpush/{ => boot}/ServerLauncher.java (80%) delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java rename mpush-boot/src/main/java/com/mpush/boot/{ => job}/BootChain.java (79%) rename mpush-boot/src/main/java/com/mpush/boot/{ => job}/BootJob.java (69%) create mode 100644 mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java rename mpush-boot/src/main/java/com/mpush/boot/{ => job}/LastBoot.java (50%) create mode 100644 mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java rename mpush-boot/src/main/java/com/mpush/boot/{ => job}/ServerBoot.java (58%) create mode 100644 mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java delete mode 100644 mpush-boot/src/main/resources/config.properties delete mode 100644 mpush-boot/src/main/resources/logback.xml create mode 100644 mpush-boot/src/main/resources/mpush.conf delete mode 100644 mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java create mode 100644 mpush-cache/pom.xml rename mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java => mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java (71%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java rename {mpush-api/src/main/java/com/mpush/api => mpush-cache/src/main/java/com/mpush/cache/redis}/RedisKey.java (96%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java rename {mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash => mpush-cache/src/main/java/com/mpush/cache/redis/hash}/ConsistentHash.java (96%) rename {mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash => mpush-cache/src/main/java/com/mpush/cache/redis/hash}/Node.java (88%) rename {mpush-tools/src/main/java/com/mpush/tools => mpush-cache/src/main/java/com/mpush/cache}/redis/listener/ListenerDispatcher.java (68%) rename {mpush-tools/src/main/java/com/mpush/tools => mpush-cache/src/main/java/com/mpush/cache}/redis/listener/MessageListener.java (69%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java rename {mpush-tools/src/main/java/com/mpush/tools/redis/pubsub => mpush-cache/src/main/java/com/mpush/cache/redis/mq}/Subscriber.java (93%) create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java rename mpush-client/src/main/java/com/mpush/{push/client/ClientChannelHandler.java => client/gateway/GatewayClientChannelHandler.java} (67%) create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java rename mpush-client/src/main/java/com/mpush/{ => client}/push/PushRequest.java (98%) rename mpush-client/src/main/java/com/mpush/{ => client}/push/PushRequestBus.java (97%) delete mode 100644 mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/ConnectClient.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushClient.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java create mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory delete mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager delete mode 100644 mpush-client/src/test/java/com/mpush/client/PushClientTest.java delete mode 100644 mpush-client/src/test/resources/logback.xml delete mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractClient.java delete mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java create mode 100644 mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java create mode 100644 mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java rename mpush-common/src/main/java/com/mpush/common/{manage => }/user/UserManager.java (57%) create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory rename mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java => mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java (86%) delete mode 100644 mpush-log/pom.xml delete mode 100644 mpush-log/src/main/java/com/mpush/log/Logs.java delete mode 100644 mpush-log/src/test/java/com/mpush/AppTest.java rename mpush-monitor/src/main/java/com/mpush/monitor/{domain/MonitorData.java => data/MonitorResult.java} (93%) create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java rename mpush-netty/src/main/java/com/mpush/netty/{client => http}/HttpCallback.java (92%) rename mpush-netty/src/main/java/com/mpush/netty/{client => http}/HttpClient.java (86%) rename mpush-netty/src/main/java/com/mpush/netty/{client => http}/NettyHttpClient.java (96%) rename mpush-netty/src/main/java/com/mpush/netty/{client => http}/RequestInfo.java (94%) delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java delete mode 100644 mpush-test/src/test/java/com/mpush/AppTest.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/Main.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java rename mpush-test/src/test/java/com/mpush/test/push/{Main.java => PushClientTestMain.java} (78%) delete mode 100644 mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java rename mpush-test/src/test/java/com/mpush/test/{connection/severs/Main.java => sever/ServerTestMain.java} (51%) create mode 100644 mpush-test/src/test/resources/application.conf delete mode 100644 mpush-test/src/test/resources/config.properties delete mode 100644 mpush-test/src/test/resources/logback.xml create mode 100644 mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/Constants.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/CC.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java rename mpush-tools/src/main/java/com/mpush/tools/config/{ => converter}/MapConverter.java (70%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/Event.java rename {mpush-common/src/main/java/com/mpush/common => mpush-tools/src/main/java/com/mpush/tools/event}/EventBus.java (72%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventType.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/log/Logs.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java create mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory delete mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister create mode 100644 mpush-tools/src/main/resources/logback.xml delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java rename mpush-zk/src/main/java/com/mpush/zk/{ => node}/ZKServerNode.java (77%) diff --git a/bin/daily-pub.py b/bin/daily-pub.py deleted file mode 100644 index a5810bd0..00000000 --- a/bin/daily-pub.py +++ /dev/null @@ -1,214 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'daily' - - -class SSH(object): - def __init__(self): - self.client = None - self.chan = None - self.shell = None - - def __enter__(self): - return self - - def __exit__(self, typ, value, trace): - self.close() - - def connect(self, host, port=22, username='root', password=None): - self.client = paramiko.SSHClient() - ##self.client.load_system_host_keys() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=10) - return self - - def close(self): - if self.client: - self.client.close() - - def exec_command(self, cmd, isprint=True): - """执行命令,每次执行都是新的session""" - if not cmd: - return - print_cmd(cmd) - - stdin, stdout, stderr = self.client.exec_command(cmd) - out = stdout.read() - if isprint: - print_out(out) - - err = stderr.read() - if err: - print_out(err) - return out, err - - def _invoke_shell(self): - """创建一个shell""" - self.shell = self.client.invoke_shell(width=200) - is_recv = False - while True: - if self.shell.recv_ready(): - print_out_stream(self.shell.recv(1024)) - is_recv = True - else: - if is_recv: - return - else: - time.sleep(0.1) - - def shell_exec(self, cmd): - """在shell中执行命令,使用的是同一个session""" - if not cmd: - return - - if not self.shell: - self._invoke_shell() - - self.shell.send(cmd + "\n") - - out = '' - is_recv = False - while True: - if self.shell.recv_ready(): - tmp = self.shell.recv(1024) - out += tmp - print_out_stream(tmp) - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -def getPid(ssh): - stdout = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) - return stdout -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean) - runShell('mvn package -P %s'%ENV) - print showText('package success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.shell_exec('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - - - ##4 kill process - pid = getPid(ssh) - if pid : - ssh.shell_exec('kill -9 %s'%pid) - else: - print showText('there is no process to kill','YELLOW') - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.shell_exec('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME)) - print showText('tar success','greenText') - - ##7 start process - ssh.shell_exec('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/debug.sh b/bin/debug.sh deleted file mode 100644 index 82a5fc7d..00000000 --- a/bin/debug.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -ENV=daily -cd `dirname $0` -cd .. - -echo "start package project..." -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd ./mpush-boot/target -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." -cd mpush -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -Dio.netty.leakDetectionLevel=advanced -jar boot.jar & - -echo "end start mpush..." diff --git a/bin/mp-env.cmd b/bin/mp-env.cmd new file mode 100644 index 00000000..fa39e14c --- /dev/null +++ b/bin/mp-env.cmd @@ -0,0 +1,49 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +set MPCFGDIR=%~dp0%..\conf +set MP_LOG_DIR=%~dp0%.. +set MP_LOG4J_PROP=INFO,CONSOLE + +REM for sanity sake assume Java 1.6 +REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html + +REM add the mpcfg dir to classpath +set CLASSPATH=%MPCFGDIR% + +REM make it work in the release +SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% + +REM make it work for developers +SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% + +set MPCFG=%MPCFGDIR%\zoo.cfg + +@REM setup java environment variables + +if not defined JAVA_HOME ( + echo Error: JAVA_HOME is not set. + goto :eof +) + +set JAVA_HOME=%JAVA_HOME:"=% + +if not exist "%JAVA_HOME%"\bin\java.exe ( + echo Error: JAVA_HOME is incorrectly set. + goto :eof +) + +set JAVA="%JAVA_HOME%"\bin\java diff --git a/bin/mp-env.sh b/bin/mp-env.sh new file mode 100644 index 00000000..618dee65 --- /dev/null +++ b/bin/mp-env.sh @@ -0,0 +1,122 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script should be sourced into other mpush +# scripts to setup the env variables + +# We use MPCFGDIR if defined, +# otherwise we use /etc/mp +# or the conf directory that is +# a sibling of this script's directory + +MPBINDIR="${MPBINDIR:-/usr/bin}" +MPUSH_PREFIX="${MPBINDIR}/.." + +if [ "x$MPCFGDIR" = "x" ] +then + if [ -e "${MPUSH_PREFIX}/conf" ]; then + MPCFGDIR="$MPBINDIR/../conf" + else + MPCFGDIR="$MPBINDIR/../etc/mpush" + fi +fi + +if [ -f "${MPBINDIR}/set-env.sh" ]; then + . "${MPBINDIR}/set-env.sh" +fi + +if [ "x$MPCFG" = "x" ] +then + MPCFG="mpush.conf" +fi + +MPCFG="$MPCFGDIR/$MPCFG" + +if [ -f "$MPBINDIR/java.env" ] +then + . "$MPBINDIR/java.env" +fi + +if [ "x${MP_DATADIR}" = "x" ] +then + MP_DATADIR="${MPUSH_PREFIX}/tmp" +fi + +if [ "x${MP_LOG_DIR}" = "x" ] +then + MP_LOG_DIR="${MPUSH_PREFIX}/logs" +fi + +if [ "x${MP_LOG4J_PROP}" = "x" ] +then + MP_LOG4J_PROP="INFO,CONSOLE" +fi + +if [ "$JAVA_HOME" != "" ]; then + JAVA="$JAVA_HOME/bin/java" +else + JAVA=java +fi + + +#add the conf dir to classpath +CLASSPATH="$MPCFGDIR:$CLASSPATH" + +for i in "$MPBINDIR"/../src/java/lib/*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work in the binary package +#(use array for LIBPATH to account for spaces within wildcard expansion) +if [ -e "${MPUSH_PREFIX}"/share/mpush/mpush-*.jar ]; then + LIBPATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) +else + #release tarball format + for i in "$MPBINDIR"/../mpush-*.jar + do + CLASSPATH="$i:$CLASSPATH" + done + LIBPATH=("${MPBINDIR}"/../lib/*.jar) +fi + +for i in "${LIBPATH[@]}" +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work for developers +for d in "$MPBINDIR"/../build/lib/*.jar +do + CLASSPATH="$d:$CLASSPATH" +done + +#make it work for developers +CLASSPATH="$MPBINDIR/../build/classes:$CLASSPATH" + + +case "`uname`" in + CYGWIN*) cygwin=true ;; + *) cygwin=false ;; +esac + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + +#echo "CLASSPATH=$CLASSPATH" \ No newline at end of file diff --git a/bin/mp.cmd b/bin/mp.cmd new file mode 100644 index 00000000..f578f4e0 --- /dev/null +++ b/bin/mp.cmd @@ -0,0 +1,24 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0mpEnv.cmd" + +set MPMAIN="-jar ../boot.jar" +echo on +call %JAVA% "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% "%MPCFG%" %* + +endlocal diff --git a/bin/mp.sh b/bin/mp.sh new file mode 100644 index 00000000..ca02e9fe --- /dev/null +++ b/bin/mp.sh @@ -0,0 +1,229 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + + + +# use POSTIX interface, symlink is followed automatically +MPBIN="${BASH_SOURCE-$0}" +MPBIN="$(dirname "${MPBIN}")" +MPBINDIR="$(cd "${MPBIN}"; pwd)" + +if [ -e "$MPBIN/../libexec/mp-env.sh" ]; then + . "$MPBINDIR/../libexec/mp-env.sh" +else + . "$MPBINDIR/mp-env.sh" +fi + +# See the following page for extensive details on setting +# up the JVM to accept JMX remote management: +# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html +# by default we allow local JMX connections +if [ "x$JMXLOCALONLY" = "x" ] +then + JMXLOCALONLY=false +fi + +if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ] +then + echo "MPush JMX enabled by default" >&2 + if [ "x$JMXPORT" = "x" ] + then + # for some reason these two options are necessary on jdk6 on Ubuntu + # accord to the docs they are not necessary, but otw jconsole cannot + # do a local attach + MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" + else + if [ "x$JMXAUTH" = "x" ] + then + JMXAUTH=false + fi + if [ "x$JMXSSL" = "x" ] + then + JMXSSL=false + fi + if [ "x$JMXLOG4J" = "x" ] + then + JMXLOG4J=true + fi + echo "MPush remote JMX Port set to $JMXPORT" >&2 + echo "MPush remote JMX authenticate set to $JMXAUTH" >&2 + echo "MPush remote JMX ssl set to $JMXSSL" >&2 + echo "MPush remote JMX log4j set to $JMXLOG4J" >&2 + MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" + fi +else + echo "JMX disabled by user request" >&2 + MPMAIN="" +fi + +MPMAIN="$MPMAIN -jar $MPUSH_PREFIX/boot.jar" + +if [ "x$SERVER_JVMFLAGS" != "x" ] +then + JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" +fi + +if [ "x$2" != "x" ] +then + MPCFG="$MPCFGDIR/$2" +fi + +# if we give a more complicated path to the config, don't screw around in $MPCFGDIR +if [ "x$(dirname "$MPCFG")" != "x$MPCFGDIR" ] +then + MPCFG="$2" +fi + +if $cygwin +then + MPCFG=`cygpath -wp "$MPCFG"` + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +echo "Using config: $MPCFG" >&2 + +case "$OSTYPE" in +*solaris*) + GREP=/usr/xpg4/bin/grep + ;; +*) + GREP=grep + ;; +esac +if [ -z "$MPPIDFILE" ]; then +# MP_DATADIR="$($GREP "^[[:space:]]*dataDir" "$MPCFG" | sed -e 's/.*=//')" + if [ ! -d "$MP_DATADIR" ]; then + mkdir -p "$MP_DATADIR" + fi + MPPIDFILE="$MP_DATADIR/mpush_server.pid" +else + # ensure it exists, otw stop will fail + mkdir -p "$(dirname "$MPPIDFILE")" +fi + +if [ ! -w "$MP_LOG_DIR" ] ; then +echo $MP_LOG_DIR +mkdir -p "$MP_LOG_DIR" +fi + +_MP_DAEMON_OUT="$MP_LOG_DIR/mpush.out" + +case $1 in +start) + echo -n "Starting mpush ... " + if [ -f "$MPPIDFILE" ]; then + if kill -0 `cat "$MPPIDFILE"` > /dev/null 2>&1; then + echo $command already running as process `cat "$MPPIDFILE"`. + exit 0 + fi + fi + nohup "$JAVA" "-Dmp.conf=$MPCFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $MPMAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & + if [ $? -eq 0 ] + then + case "$OSTYPE" in + *solaris*) + /bin/echo "${!}\\c" > "$MPPIDFILE" + ;; + *) + /bin/echo -n $! > "$MPPIDFILE" + ;; + esac + if [ $? -eq 0 ]; + then + sleep 1 + echo STARTED + else + echo FAILED TO WRITE PID + exit 1 + fi + else + echo SERVER DID NOT START + exit 1 + fi + ;; +start-foreground) + MP_CMD=(exec "$JAVA") + if [ "${MP_NOEXEC}" != "" ]; then + MP_CMD=("$JAVA") + fi + "${MP_CMD[@]}" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $MPMAIN "-Dmp.conf=$MPCFG" + ;; +print-cmd) + echo "\"$JAVA\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" + echo -cp \"$CLASSPATH\" $JVMFLAGS $MPMAIN \"-Dmp.conf=$MPCFG\" > \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" + ;; +stop) + echo -n "Stopping mpush ... " + if [ ! -f "$MPPIDFILE" ] + then + echo "no mpush to stop (could not find file $MPPIDFILE)" + else + $KILL -9 $(cat "$MPPIDFILE") + rm "$MPPIDFILE" + echo STOPPED + fi + exit 0 + ;; +upgrade) + shift + echo "upgrading the servers to 3.*" + "$JAVA" "-Dmpush.log.dir=${MP_LOG_DIR}" "-Dmpush.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS com.mpush.tools.upgrade.UpgradeMain ${@} + echo "Upgrading ... " + ;; +restart) + shift + "$0" stop ${@} + sleep 3 + "$0" start ${@} + ;; +status) + # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + if ! [ $clientPortAddress ] + then + clientPortAddress="localhost" + fi + clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + STAT=`"$JAVA" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS org.apache.mpush.client.FourLetterWordMain \ + $clientPortAddress $clientPort srvr 2> /dev/null \ + | $GREP Mode` + if [ "x$STAT" = "x" ] + then + echo "Error contacting service. It is probably not running." + exit 1 + else + echo $STAT + exit 0 + fi + ;; +*) + echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2 + +esac diff --git a/bin/pub-daily.py b/bin/pub-daily.py deleted file mode 100644 index 9344ed27..00000000 --- a/bin/pub-daily.py +++ /dev/null @@ -1,239 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'daily' - -class Telnet(object): - def __init__(self, chan): - self.chan = chan - - def send(self, cmd,isprint=True): - self.chan.send(cmd+'\n') - print_cmd(cmd) - out = '' - is_recv = False - is_recv_err = False - while True: - # 结束 - if self.chan.recv_stderr_ready(): - tmp = self.chan.recv_stderr(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv_err = True - else: - if is_recv_err: - return out - else: - time.sleep(0.1) - - if self.chan.recv_ready(): - tmp = self.chan.recv(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - def telnet(self, cmd,isprint=True): - chan = self.client.get_transport().open_session(timeout=10) - chan.exec_command(cmd) - - t = Telnet(chan) - - is_recv = False - - while True: - if chan.recv_ready(): - if isprint: - print_out_stream(chan.recv(1024)) - is_recv = True - else: - if is_recv: - return t - else: - time.sleep(0.1) - - - def close(self): - if self.client: - self.client.close() - -def getPid(keyword,ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - except: - print showText('telnet exception','redText') - - - print showText('start kill process','greenText') - - ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid : - ssh.exe('kill %s'%pid) - sleep(15) - else: - print showText('there is no process to kill','YELLOW') - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid: - ssh.exe('kill -9 %s'%pid) - - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) - print showText('tar success','greenText') - - ##7 start process - ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/pub-online.py b/bin/pub-online.py deleted file mode 100644 index 1d6739ec..00000000 --- a/bin/pub-online.py +++ /dev/null @@ -1,239 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'online' - -class Telnet(object): - def __init__(self, chan): - self.chan = chan - - def send(self, cmd,isprint=True): - self.chan.send(cmd+'\n') - print_cmd(cmd) - out = '' - is_recv = False - is_recv_err = False - while True: - # 结束 - if self.chan.recv_stderr_ready(): - tmp = self.chan.recv_stderr(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv_err = True - else: - if is_recv_err: - return out - else: - time.sleep(0.1) - - if self.chan.recv_ready(): - tmp = self.chan.recv(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - def telnet(self, cmd,isprint=True): - chan = self.client.get_transport().open_session(timeout=10) - chan.exec_command(cmd) - - t = Telnet(chan) - - is_recv = False - - while True: - if chan.recv_ready(): - if isprint: - print_out_stream(chan.recv(1024)) - is_recv = True - else: - if is_recv: - return t - else: - time.sleep(0.1) - - - def close(self): - if self.client: - self.client.close() - -def getPid(keyword,ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - except: - print showText('telnet exception','redText') - - - print showText('start kill process','greenText') - - ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid : - ssh.exe('kill %s'%pid) - sleep(15) - else: - print showText('there is no process to kill','YELLOW') - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid: - ssh.exe('kill -9 %s'%pid) - - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) - print showText('tar success','greenText') - - ##7 start process - ssh.exe('nohup %s -Dio.netty.leakDetectionLevel=advanced -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/pub-pre.py b/bin/pub-pre.py deleted file mode 100644 index e3bc8ca8..00000000 --- a/bin/pub-pre.py +++ /dev/null @@ -1,101 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - - -BASEPATH = '/root/mpush' - -STARTPROCESS = 'java -jar boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/mpush-release.tar.gz' - -ENV= 'pre' - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##1 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(Y/N):") - - if confirmPub != 'Y': - return - - ##4 cp - runShell('cp %s %s'%(GITLABPATH,BASEPATH)) - print showText('cp success','greenText') - - ##5 tar package - runShell('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz') - print showText('tar success','greenText') - - ##6 start process - runShell('nohup /opt/shinemo/jdk1.7.0_40/bin/java -Dio.netty.leakDetectionLevel=advanced -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') - print showText('start process success','greenText') - - -if __name__ == "__main__": - main() diff --git a/bin/run.sh b/bin/run.sh deleted file mode 100644 index d5f5ab08..00000000 --- a/bin/run.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -ENV=dev - -cd `dirname $0` -cd .. - -echo "start tar mpush..." -cd ./mpush-boot/target - -rm -rf mpush -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." - -cd mpush -java \ --Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ --Dio.netty.leakDetectionLevel=advanced \ --jar boot.jar diff --git a/bin/start.sh b/bin/start.sh deleted file mode 100644 index 0f807d5a..00000000 --- a/bin/start.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -ENV=dev - -cd `dirname $0` -cd .. - -echo "start package project..." -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd ./mpush-boot/target -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." -cd mpush -nohup java \ --Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ --Dio.netty.leakDetectionLevel=advanced \ --jar boot.jar >/dev/null 2>&1 & - -echo "end start mpush..." \ No newline at end of file diff --git a/bin/test.py b/bin/test.py deleted file mode 100644 index 8a624cd8..00000000 --- a/bin/test.py +++ /dev/null @@ -1,69 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=9092,username='shinemo',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - - confirmPub = raw_input("") - - print(confirmPub) - - -if __name__ == "__main__": - main() diff --git a/bin/test.sh b/bin/test.sh deleted file mode 100644 index 6d2aa540..00000000 --- a/bin/test.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -ENV=dev - -base_dir=`pwd` - -DIR=`dirname $0` - -PROG=`basename $0` - -echo $DIR diff --git a/conf/conf-daily.properties b/conf/conf-daily.properties deleted file mode 100644 index 92ca7c8f..00000000 --- a/conf/conf-daily.properties +++ /dev/null @@ -1,16 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=debug -zk_ip = 115.29.169.109:5666 -zk_digest = shinemoIpo -zk_namespace = mpush-daily -redis_group = 111.1.57.148:6379:ShineMoIpo -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info = true -connection_server_port = 20882 -gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 -skip_dump=true -admin_port=4001 -remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/conf/conf-pre.properties b/conf/conf-pre.properties deleted file mode 100644 index c022613c..00000000 --- a/conf/conf-pre.properties +++ /dev/null @@ -1,15 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=DEBUG -zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -zk_digest = shinemoIpo -zk_namespace = mpush-pre -redis_group = 10.161.223.238:6379:ShineMoIpo -private_key = MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA== -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB -force_write_redis_group_info = true -connection_server_port = 3000 -gateway_server_port = 4000 -dns_mapping=115.29.230.2=127.0.0.1 -skip_dump=false -admin_port=4001 diff --git a/conf/conf-online.properties b/conf/conf-pub.properties similarity index 100% rename from conf/conf-online.properties rename to conf/conf-pub.properties diff --git a/conf/reference.conf b/conf/reference.conf new file mode 100644 index 00000000..97c598c6 --- /dev/null +++ b/conf/reference.conf @@ -0,0 +1,142 @@ +mp { + log.level=warn + log.dir=${user.dir}/../logs + + core { + max-packet-size=10k + compress-threshold=10k + min-heartbeat=10s + max-heartbeat=3m + max-hb-timeout-times=2 + session-expired-time=1d + } + + security { + private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" + public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" + aes-key-length=16 + ras-key-length=1024 + } + + net { + connect-server-port=3000 + gateway-server-port=3001 + admin-server-port=3002 + public-host-mapping { + "127.0.0.1":"127.0.0.1" + } + } + + zk { + server-address="127.0.0.1:2181" + namespace=mpush + digest=mpush + local-cache-path=/ + retry { + #initial amount of time to wait between retries + baseSleepTimeMs=3s + #max number of times to retry + maxRetries=3 + #max time in ms to sleep on each retry + maxSleepMs=5s + } + connectionTimeoutMs=5s + sessionTimeoutMs=5s + } + + redis { + write-to-zk=true + cluster-group:[//redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + [ + { + host:"111.1.57.148" + port:6379 + password:ShineMoIpo + } + ] + ] + config { + maxTotal:8, + maxIdle:4, + minIdle:1, + lifo:true, + fairness:false, + maxWaitMillis:5000, + minEvictableIdleTimeMillis:300000, + softMinEvictableIdleTimeMillis:1800000, + numTestsPerEvictionRun:3, + testOnCreate:false, + testOnBorrow:false, + testOnReturn:false, + testWhileIdle:false, + timeBetweenEvictionRunsMillis:60000, + blockWhenExhausted:true, + jmxEnabled:true, + jmxNamePrefix:pool, + jmxNameBase:pool + } + } + + http { + proxy-enable=false + max-conn-per-host=5 + default-read-timeout=10s + dns-mapping { + "mpush.com":["127.0.0.1:8080","127.0.0.1:8081"] + } + } + + thread { + pool { + boss { + min:4 + max:16 + queue-size:1000 + } + + work { + min:8 + max:32 + queue-size:1000 + } + + event-bus { + min:4 + max:4 + queue-size:100 + } + + http-proxy { + min:8 + max:64 + queue-size:1000 + } + + biz { + min:4 + max:64 + queue-size:10 + } + + mq { + min:2 + max:4 + queue-size:10000 + } + + push-client { + min:2 + max:16 + queue-size:100000 + } + } + } + + monitor { + dump-stack=false + } + + spi { + + } +} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/BaseService.java b/mpush-api/src/main/java/com/mpush/api/BaseService.java new file mode 100644 index 00000000..4dc12e5e --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/BaseService.java @@ -0,0 +1,30 @@ +package com.mpush.api; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public abstract class BaseService implements Service { + + protected final AtomicBoolean started = new AtomicBoolean(); + + @Override + public void init() { + } + + @Override + public boolean isRunning() { + return started.get(); + } + + public final void start() { + start(null); + } + + public final void stop() { + start(null); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/Client.java b/mpush-api/src/main/java/com/mpush/api/Client.java index f63a0ca0..332db5cb 100644 --- a/mpush-api/src/main/java/com/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/mpush/api/Client.java @@ -1,39 +1,5 @@ package com.mpush.api; -import com.mpush.api.connection.Connection; -import io.netty.channel.Channel; - - -public interface Client { - - void init(Channel channel); - - boolean isConnected(); - - String getHost(); - - int getPort(); - - void close(String cause); - - boolean isEnabled(); - - void resetHbTimes(); - - int inceaseAndGetHbTimes(); - - String getUrl(); - - void startHeartBeat() throws Exception; - - void startHeartBeat(final int heartbeat) throws Exception; - - void stop(); - - Connection getConnection(); - - Channel getChannel(); - - void initConnection(Connection connection); +public interface Client extends Service { } diff --git a/mpush-api/src/main/java/com/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java index 11c15dbd..c861f4a3 100644 --- a/mpush-api/src/main/java/com/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -9,6 +9,7 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); + byte[] EMPTY_BYTES = new byte[0]; String HTTP_HEAD_READ_TIMEOUT = "readTimeout"; diff --git a/mpush-api/src/main/java/com/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/Server.java index f9785345..7d45390b 100644 --- a/mpush-api/src/main/java/com/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/mpush/api/Server.java @@ -5,19 +5,6 @@ * * @author ohun@live.cn */ -public interface Server { +public interface Server extends Service { - void start(Listener listener); - - void stop(Listener listener); - - void init(); - - boolean isRunning(); - - interface Listener { - void onSuccess(int port); - - void onFailure(Throwable cause); - } } diff --git a/mpush-api/src/main/java/com/mpush/api/Service.java b/mpush-api/src/main/java/com/mpush/api/Service.java new file mode 100644 index 00000000..25422e39 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/Service.java @@ -0,0 +1,23 @@ +package com.mpush.api; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public interface Service { + + void start(Listener listener); + + void stop(Listener listener); + + void init(); + + boolean isRunning(); + + interface Listener { + void onSuccess(Object... args); + + void onFailure(Throwable cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index f1ea56aa..39b15f94 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -2,7 +2,6 @@ import com.mpush.api.protocol.Packet; import io.netty.channel.Channel; - import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -35,12 +34,8 @@ public interface Connection { void updateLastReadTime(); - int inceaseAndGetHbTimes(); + long getLastReadTime(); - void resetHbTimes(); + Channel getChannel(); - long getLastReadTime(); - - Channel getChannel(); - } diff --git a/mpush-api/src/main/java/com/mpush/api/exception/BootException.java b/mpush-api/src/main/java/com/mpush/api/exception/BootException.java new file mode 100644 index 00000000..52c2621a --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/exception/BootException.java @@ -0,0 +1,12 @@ +package com.mpush.api.exception; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class BootException extends RuntimeException { + public BootException(String s) { + super(s); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/PushContent.java b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java similarity index 98% rename from mpush-api/src/main/java/com/mpush/api/PushContent.java rename to mpush-api/src/main/java/com/mpush/api/push/PushContent.java index 7abe5307..7815b473 100644 --- a/mpush-api/src/main/java/com/mpush/api/PushContent.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java @@ -1,4 +1,4 @@ -package com.mpush.api; +package com.mpush.api.push; import java.io.Serializable; diff --git a/mpush-api/src/main/java/com/mpush/api/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java similarity index 59% rename from mpush-api/src/main/java/com/mpush/api/PushSender.java rename to mpush-api/src/main/java/com/mpush/api/push/PushSender.java index 83b1d519..1d70dac3 100644 --- a/mpush-api/src/main/java/com/mpush/api/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -1,4 +1,8 @@ -package com.mpush.api; +package com.mpush.api.push; + +import com.mpush.api.Service; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.client.PusherFactory; import java.util.Collection; @@ -7,11 +11,17 @@ * * @author ohun@live.cn */ -public interface PushSender { +public interface PushSender extends Service { + PusherFactory factory = SpiLoader.load(PusherFactory.class); + void send(String content, Collection userIds, Callback callback); - + void send(String content, String userId, Callback callback); + void start(); + + void stop(); + interface Callback { void onSuccess(String userId); diff --git a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java new file mode 100644 index 00000000..cf5cd1e8 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java @@ -0,0 +1,10 @@ +package com.mpush.api.spi; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public interface Factory { + T get(); +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java similarity index 91% rename from mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java rename to mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index 3e8cc3d1..b9ff34ce 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -1,11 +1,11 @@ -package com.mpush.tools.spi; +package com.mpush.api.spi; import java.util.Iterator; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; -public class ServiceContainer { +public final class SpiLoader { private static final Map CACHE = new ConcurrentHashMap<>(); public static T load(Class clazz) { @@ -32,7 +32,7 @@ public static T load0(Class clazz, String name) { T t = filterByName(factories, name); if (t == null) { - factories = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()); + factories = ServiceLoader.load(clazz, SpiLoader.class.getClassLoader()); t = filterByName(factories, name); } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java new file mode 100644 index 00000000..b0898727 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java @@ -0,0 +1,13 @@ +package com.mpush.api.spi.client; + +import com.mpush.api.push.PushSender; +import com.mpush.api.spi.Factory; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public interface PusherFactory extends Factory { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java new file mode 100644 index 00000000..9e17a790 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java @@ -0,0 +1,20 @@ +package com.mpush.api.spi.common; + +import java.util.concurrent.Executor; + +/** + * Created by yxx on 2016/5/20. + * + * @author ohun@live.cn + */ +public interface ThreadPoolFactory { + String SERVER_BOSS = "sb"; + String SERVER_WORK = "sw"; + String HTTP_CLIENT_WORK = "hcw"; + String PUSH_CLIENT_WORK = "puw"; + String EVENT_BUS = "eb"; + String MQ = "r"; + String BIZ = "b"; + + Executor get(String name); +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java new file mode 100644 index 00000000..87ff8af2 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java @@ -0,0 +1,13 @@ +package com.mpush.api.spi.core; + +import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.Factory; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public interface CipherFactory extends Factory { + Cipher get(); +} diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index 8d71b814..4b65b52d 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -1,8 +1,5 @@ - + release mpush true @@ -11,10 +8,33 @@ - target/classes/ + ../ - config.properties + LICENSE + README.md + + + + ../bin/ + bin + + *.sh + *.cmd + + + + ../conf/ + conf + + reference.conf + + + + target/classes/ + conf + + mpush.conf diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index c5eb3d3f..90aeaa6b 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -25,10 +25,6 @@ ${mpush.groupId} mpush-zk - - org.codehaus.janino - janino -
@@ -59,7 +55,7 @@ lib/ - com.mpush.Main + com.mpush.boot.Main diff --git a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java deleted file mode 100644 index 850df861..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.manage.DnsMappingManage; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class HttpProxyBoot extends BootJob { - @Override - void run() { - if (ConfigCenter.I.httpProxyEnable()) { - DnsMappingManage.holder.init(); - } - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/Main.java b/mpush-boot/src/main/java/com/mpush/boot/Main.java similarity index 70% rename from mpush-boot/src/main/java/com/mpush/Main.java rename to mpush-boot/src/main/java/com/mpush/boot/Main.java index 295480e4..0f82640c 100644 --- a/mpush-boot/src/main/java/com/mpush/Main.java +++ b/mpush-boot/src/main/java/com/mpush/boot/Main.java @@ -1,11 +1,11 @@ -package com.mpush; +package com.mpush.boot; -import com.mpush.tools.ConsoleLog; +import com.mpush.tools.log.Logs; public class Main { public static void main(String[] args) { - ConsoleLog.i("launch app..."); + Logs.Console.info("launch app..."); ServerLauncher launcher = new ServerLauncher(); launcher.start(); addHook(launcher); @@ -15,7 +15,7 @@ private static void addHook(final ServerLauncher serverBoot) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { serverBoot.stop(); - ConsoleLog.i("jvm exit all server stopped..."); + Logs.Console.info("jvm exit all server stopped..."); } }); } diff --git a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java deleted file mode 100644 index be85f539..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mpush.boot; - -import com.mpush.monitor.service.MonitorDataCollector; -import com.mpush.tools.config.ConfigCenter; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class MonitorBoot extends BootJob { - @Override - void run() { - MonitorDataCollector.start(ConfigCenter.I.skipDump()); - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java deleted file mode 100644 index c13efa35..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.boot; - -import com.google.common.base.Strings; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.zk.ZKClient; - -import java.util.List; - -import static com.mpush.zk.ZKPath.REDIS_SERVER; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class RedisBoot extends BootJob { - - @Override - public void run() { - List groupList = ConfigCenter.I.redisGroups(); - if (groupList.size() > 0) { - if (ConfigCenter.I.forceWriteRedisGroupInfo()) { - register(groupList); - } else if (!ZKClient.I.isExisted(REDIS_SERVER.getPath())) { - register(groupList); - } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getPath()))) { - register(groupList); - } - } else { - throw new RuntimeException("init redis sever fail groupList is null"); - } - - RedisManage.test(groupList); - next(); - } - - private void register(List groupList) { - String data = Jsons.toJson(groupList); - ZKClient.I.registerPersist(REDIS_SERVER.getPath(), data); - ConsoleLog.i("register redis server group success, group=" + data); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/boot/ServerLauncher.java similarity index 80% rename from mpush-boot/src/main/java/com/mpush/ServerLauncher.java rename to mpush-boot/src/main/java/com/mpush/boot/ServerLauncher.java index 87315101..2bbc8434 100644 --- a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/boot/ServerLauncher.java @@ -1,13 +1,13 @@ -package com.mpush; +package com.mpush.boot; import com.mpush.api.Server; -import com.mpush.boot.*; +import com.mpush.boot.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.GatewayServer; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.zk.ZKServerNode; +import com.mpush.tools.config.CC; +import com.mpush.zk.node.ZKServerNode; /** * Created by yxx on 2016/5/14. @@ -23,14 +23,14 @@ public class ServerLauncher { private final Server gatewayServer = new GatewayServer(gsNode.getPort()); - private final Server adminServer = new AdminServer(ConfigCenter.I.adminPort()); + private final Server adminServer = new AdminServer(CC.mp.net.admin_server_port); public void start() { BootChain chain = BootChain.chain(); chain.boot() - .setNext(new RedisBoot())//1.注册redis sever 到ZK - .setNext(new ZKBoot())//2.启动ZK节点数据变化监听 + .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 + .setNext(new RedisBoot())//2.注册redis sever 到ZK .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 diff --git a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java deleted file mode 100644 index dbdb2360..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mpush.boot; - -import com.google.common.collect.Lists; -import com.mpush.zk.ZKClient; -import com.mpush.zk.listener.ZKDataChangeListener; -import com.mpush.zk.listener.ZKRedisNodeListener; - -import java.util.List; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ZKBoot extends BootJob { - private List dataChangeListeners = Lists.newArrayList(); - - public ZKBoot() { - registerListener(new ZKRedisNodeListener()); - } - - public void registerListener(ZKDataChangeListener listener) { - dataChangeListeners.add(listener); - } - - private void registerListeners() { - for (ZKDataChangeListener listener : dataChangeListeners) { - ZKClient.I.registerListener(listener); - } - } - - private void initListenerData() { - for (ZKDataChangeListener listener : dataChangeListeners) { - listener.initData(); - } - } - - - @Override - public void run() { - registerListeners(); - initListenerData(); - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java b/mpush-boot/src/main/java/com/mpush/boot/job/BootChain.java similarity index 79% rename from mpush-boot/src/main/java/com/mpush/boot/BootChain.java rename to mpush-boot/src/main/java/com/mpush/boot/job/BootChain.java index fb4183f4..752c1dba 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/boot/job/BootChain.java @@ -1,6 +1,6 @@ -package com.mpush.boot; +package com.mpush.boot.job; -import com.mpush.tools.ConsoleLog; +import com.mpush.tools.log.Logs; /** * Created by yxx on 2016/5/15. @@ -22,7 +22,7 @@ private BootJob first() { return new BootJob() { @Override public void run() { - ConsoleLog.i("begin run boot chain..."); + Logs.Console.info("begin run boot chain..."); next(); } }; diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java b/mpush-boot/src/main/java/com/mpush/boot/job/BootJob.java similarity index 69% rename from mpush-boot/src/main/java/com/mpush/boot/BootJob.java rename to mpush-boot/src/main/java/com/mpush/boot/job/BootJob.java index 4273651d..7903bc5c 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/boot/job/BootJob.java @@ -1,6 +1,6 @@ -package com.mpush.boot; +package com.mpush.boot.job; -import com.mpush.tools.ConsoleLog; +import com.mpush.tools.log.Logs; /** * Created by yxx on 2016/5/14. @@ -14,7 +14,7 @@ public abstract class BootJob { public void next() { if (next != null) { - ConsoleLog.i("run next boot [" + next.getClass().getSimpleName() + "]"); + Logs.Console.info("run next boot [" + next.getClass().getSimpleName() + "]"); next.run(); } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java new file mode 100644 index 00000000..823229dd --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java @@ -0,0 +1,20 @@ +package com.mpush.boot.job; + +import com.mpush.common.net.DnsMappingManager; +import com.mpush.tools.config.CC; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class HttpProxyBoot extends BootJob { + @Override + void run() { + if (CC.mp.http.proxy_enable) { + DnsMappingManager.I.init(); + DnsMappingManager.I.start(); + } + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java b/mpush-boot/src/main/java/com/mpush/boot/job/LastBoot.java similarity index 50% rename from mpush-boot/src/main/java/com/mpush/boot/LastBoot.java rename to mpush-boot/src/main/java/com/mpush/boot/job/LastBoot.java index 4be3db0c..c86230f1 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/boot/job/LastBoot.java @@ -1,7 +1,7 @@ -package com.mpush.boot; +package com.mpush.boot.job; -import com.mpush.common.manage.user.UserManager; -import com.mpush.tools.ConsoleLog; +import com.mpush.common.user.UserManager; +import com.mpush.tools.log.Logs; /** * Created by yxx on 2016/5/14. @@ -12,7 +12,7 @@ public class LastBoot extends BootJob { @Override public void run() { UserManager.INSTANCE.clearUserOnlineData(); - ConsoleLog.i("end run boot chain..."); - ConsoleLog.i("app start success..."); + Logs.Console.info("end run boot chain..."); + Logs.Console.info("app start success..."); } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java new file mode 100644 index 00000000..88656e8c --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java @@ -0,0 +1,20 @@ +package com.mpush.boot.job; + +import com.mpush.monitor.service.MonitorService; +import com.mpush.tools.config.CC; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class MonitorBoot extends BootJob { + @Override + void run() { + MonitorService.I + .setEnableDump(CC.mp.monitor.dump_stack) + .setDumpLogDir(CC.mp.log_dir) + .start(); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java new file mode 100644 index 00000000..9c3f676e --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java @@ -0,0 +1,17 @@ +package com.mpush.boot.job; + +import com.mpush.cache.redis.manager.RedisManager; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class RedisBoot extends BootJob { + + @Override + public void run() { + RedisManager.I.init(); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/boot/job/ServerBoot.java similarity index 58% rename from mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java rename to mpush-boot/src/main/java/com/mpush/boot/job/ServerBoot.java index ff80675d..0b50c370 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/boot/job/ServerBoot.java @@ -1,13 +1,11 @@ -package com.mpush.boot; +package com.mpush.boot.job; import com.mpush.api.Server; -import com.mpush.tools.ConsoleLog; +import com.mpush.tools.log.Logs; import com.mpush.tools.Jsons; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.pool.ThreadPoolManager; import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKServerNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.zk.node.ZKServerNode; /** * Created by yxx on 2016/5/14. @@ -15,8 +13,6 @@ * @author ohun@live.cn */ public class ServerBoot extends BootJob { - private final Logger logger = LoggerFactory.getLogger(ServerBoot.class); - private final Server server; private final ZKServerNode node; @@ -28,16 +24,14 @@ public ServerBoot(Server server, ZKServerNode node) { @Override public void run() { final String serverName = server.getClass().getSimpleName(); - ThreadPoolManager.newThread(serverName, new Runnable() { + ThreadPoolManager.I.newThread(serverName, new Runnable() { @Override public void run() { server.init(); server.start(new Server.Listener() { @Override - public void onSuccess(int port) { - String msg = "start " + serverName + " success listen:" + port; - logger.error(msg); - ConsoleLog.i(msg); + public void onSuccess(Object... args) { + Logs.Console.info("start " + serverName + " success listen:" + args[0]); if (node != null) { registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } @@ -46,9 +40,7 @@ public void onSuccess(int port) { @Override public void onFailure(Throwable cause) { - String msg = "start " + serverName + " failure, jvm exit with code -1"; - logger.error(msg); - ConsoleLog.e(cause, msg); + Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); System.exit(-1); } }); @@ -59,8 +51,6 @@ public void onFailure(Throwable cause) { //step7 注册应用到zk public void registerServerToZk(String path, String value) { ZKClient.I.registerEphemeralSequential(path, value); - String msg = "register server node=" + value + " to zk path=" + path; - logger.error(msg); - ConsoleLog.i(msg); + Logs.Console.info("register server node=" + value + " to zk name=" + path); } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java new file mode 100644 index 00000000..f8835cab --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java @@ -0,0 +1,21 @@ +package com.mpush.boot.job; + +import com.mpush.api.exception.BootException; +import com.mpush.zk.ZKClient; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKBoot extends BootJob { + + @Override + public void run() { + if (ZKClient.I.getZKConfig() != null) { + next(); + } else { + throw new BootException("init zk client failure"); + } + } +} diff --git a/mpush-boot/src/main/resources/config.properties b/mpush-boot/src/main/resources/config.properties deleted file mode 100644 index 27237da8..00000000 --- a/mpush-boot/src/main/resources/config.properties +++ /dev/null @@ -1,37 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 180000 -## -max_hb_timeout_times = 2 -## -private_key = ${private_key} -## -public_key = ${public_key} -## -gateway_server_port = ${gateway_server_port} -## -connection_server_port = ${connection_server_port} -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 - -## zk ip -zk_ip = ${zk_ip} -zk_namespace = ${zk_namespace} -zk_digest = ${zk_digest} - -redis_group = ${redis_group} -force_write_redis_group_info = ${force_write_redis_group_info} -jvm_log_path = /opt/shinemo/mpush/ -dns_mapping=${dns_mapping} -skip_dump=${skip_dump} -admin_port=${admin_port} -remote_ip_mapping=${remote_ip_mapping} diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml deleted file mode 100644 index 17dc0dee..00000000 --- a/mpush-boot/src/main/resources/logback.xml +++ /dev/null @@ -1,209 +0,0 @@ - - - - - - ${log.home}/mpush.error.log - - error - ACCEPT - DENY - - true - - ${log.home}/mpush.error.log.%d{yyyy-MM-dd} - - 3 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - ${log.home}/mpush.info.log - - info - ACCEPT - DENY - - true - - ${log.home}/mpush.info.log.%d{yyyy-MM-dd} - - 3 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - ${log.home}/mpush.log - true - - ${log.home}/mpush.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - ${log.home}/mpush-monitor.log - true - - ${log.home}/mpush-monitor.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-connection.log - true - - ${log.home}/mpush-connection.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-push.log - true - - ${log.home}/mpush-push.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - - - - - - ${log.home}/mpush-heartbeat.log - true - - ${log.home}/mpush-heartbeat.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-redis.log - true - - ${log.home}/mpush-redis.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-http.log - true - - ${log.home}/mpush-http.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-zk.log - true - - ${log.home}/mpush-zk.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - - - - diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf new file mode 100644 index 00000000..557e80b4 --- /dev/null +++ b/mpush-boot/src/main/resources/mpush.conf @@ -0,0 +1,2 @@ +mp.log.level=debug +mp.zk.namespace=mpush \ No newline at end of file diff --git a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java b/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java deleted file mode 100644 index 2a2d6d5e..00000000 --- a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mpush; - -import org.junit.Test; - -public class ConnectionServerApplicationTest { - - @Test - public void testJson() throws Exception{ - - - } - -} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java b/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java deleted file mode 100644 index d4c66e7b..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java +++ /dev/null @@ -1,80 +0,0 @@ -//package com.mpush.core.zk; -// -//import java.util.concurrent.CountDownLatch; -//import java.util.concurrent.Executor; -//import java.util.concurrent.Executors; -// -//import org.junit.Test; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import MPushUtil; -//import com.mpush.tools.zk.ServerApp; -//import com.mpush.tools.zk.ZKPath; -//import com.mpush.tools.zk.manage.ServerManage; -// -//public class ServerManageTest { -// -// private static Executor executor = Executors.newCachedThreadPool(); -// -// private ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// -// private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); -// -// @Test -// public void testMulThreadRegisterApp() throws InterruptedException { -// CountDownLatch latch = new CountDownLatch(1); -// for (int i = 1; i <= 2; i++) { -// executor.execute(new Worker("192.168.1." + i, latch)); -// } -// latch.countDown(); -// -// Thread.sleep(Integer.MAX_VALUE); -// } -// -// -// @Test -// public void testServerManageStart() throws InterruptedException { -// manage.start(); -// Thread.sleep(Integer.MAX_VALUE); -// } -// -// -// private static class Worker implements Runnable { -// -// private static final Logger log = LoggerFactory.getLogger(Worker.class); -// -// private final String ip; -// private final CountDownLatch latch; -// -// public Worker(String ip, CountDownLatch latch) { -// this.ip = ip; -// this.latch = latch; -// } -// -// @Override -// public void run() { -// try { -// latch.await(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// log.warn("start init " + ip); -// ServerApp app = new ServerApp(ip, 3000); -// ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); -// manage.start(); -// -// try { -// Thread.sleep(20000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// manage.close(); -// -// log.warn("end init " + ip); -// } -// -// } -// -//} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java deleted file mode 100644 index 52c93350..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.zk; - -import org.junit.Test; - - -public class ZkTest { - - @Test - public void remove() { - ZKClient zkRegister = ZKClient.I; - - zkRegister.remove("/"); - - } - -} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java deleted file mode 100644 index 8f49408a..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.mpush.zk; - -import com.google.common.collect.Lists; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -public class ZkUtilTest { - - private ZKClient zkUtil = ZKClient.I; - - @Before - public void setUp() throws Exception { - } - - @Test - public void test() { - - String dubbo = zkUtil.get("/dubbo"); - System.out.println(dubbo); - - List child = zkUtil.getChildrenKeys("/dubbo"); - System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); - - zkUtil.registerPersist("/hi", "hello world"); - - zkUtil.registerEphemeral("/huang", "hi"); - zkUtil.registerEphemeralSequential("/huang2"); - - String huang = zkUtil.get("/huang"); - System.out.println(huang); - - String huang2 = zkUtil.get("/huang2"); - System.out.println(huang2); - - } - - @Test - public void getTest() { - String value = zkUtil.get("/hi"); - System.out.println(value); - } - - /** - * 注册机器到/mpush/allhost 目录下边 - */ - @Test - public void testRegister() { - - String path = "/" + zkUtil.getZKConfig().getNamespace(); - - String prefix = "machine"; - - List hosts = zkUtil.getChildrenKeys(path); - - System.out.println("before register"); - - for (int i = 0; i < hosts.size(); i++) { - String value = zkUtil.get(hosts.get(i)); - System.out.println(hosts.get(i) + "," + value); - } - - System.out.println("start register"); - - zkUtil.registerEphemeralSequential(path + "/" + prefix); - - zkUtil.registerEphemeralSequential(path + "/" + prefix); - - hosts = zkUtil.getChildrenKeys(path); - - for (int i = 0; i < hosts.size(); i++) { - String value = zkUtil.get(path + "/" + hosts.get(i)); - System.out.println(hosts.get(i) + "," + value); - } - - System.out.println("end register"); - - } - - @Test - public void testLocalIp() { - System.out.println(MPushUtil.getLocalIp()); - - } - - @Test - public void testRegisterIp() throws Exception { - String localIp = MPushUtil.getInetAddress(); - ZKServerNode app = new ZKServerNode(); - zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); - String value = zkUtil.get("/" + localIp); - System.out.println(value); - } - - @Test - public void testRemove() { - zkUtil.remove("/"); - } - - @Test - public void testAddKickOff() { - String localIp = MPushUtil.getInetAddress(); - String kick = "kickoff"; - String ip = "10.1.10.65"; - zkUtil.registerEphemeral("/" + localIp + "/" + kick, ip); - - } - - @Test - public void testAddRedis() { - - RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); - //RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - RedisGroup group1 = new RedisGroup(); - group1.addRedisNode(node1); - - /*RedisGroup group2 = new RedisGroup(); - group2.addRedisNode(node2);*/ - - List groupList = Lists.newArrayList(group1); - - zkUtil.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - - - } - - @Test - public void getRedisTest() { - String value = zkUtil.get(ZKPath.REDIS_SERVER.getPath()); - List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); - for (RedisGroup group : newGroupList) { - for (RedisNode node : group.getRedisNodeList()) { - System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - - -} diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml new file mode 100644 index 00000000..0f1b4dae --- /dev/null +++ b/mpush-cache/pom.xml @@ -0,0 +1,27 @@ + + + + mpush + com.mpush + 1.0 + + 4.0.0 + + ${mpush.groupId} + mpush-cache + ${mpush.version} + + + + ${mpush.groupId} + mpush-zk + + + redis.clients + jedis + + + + \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java similarity index 71% rename from mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 6c160b30..71d02bd5 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -1,11 +1,9 @@ -package com.mpush.tools.redis; +package com.mpush.cache.redis; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.log.Logs; -import com.mpush.tools.Constants; +import com.mpush.tools.log.Logs; import com.mpush.tools.Jsons; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; import redis.clients.jedis.*; import java.util.Iterator; @@ -13,15 +11,54 @@ import java.util.Map; import java.util.Set; -public class RedisUtil { - - private static Map holder = Maps.newConcurrentMap(); - - public static Jedis getClient(RedisNode node) { - JedisPool pool = holder.get(node); +public class RedisClient { + private static final int REDIS_TIMEOUT = 2000; + private static final int REDIS_MAX_TOTAL = 8; + private static final int REDIS_MAX_IDLE = 4; + private static final int REDIS_MIN_IDLE = 1; + private static final int REDIS_MAX_WAIT_MILLIS = 5000; + private static final int REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 300000; + private static final int REDIS_NUM_TESTS_PER_EVICTION_RUN = 3; + private static final int REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; + private static final boolean REDIS_TEST_ON_BORROW = false; + private static final boolean REDIS_TEST_ON_RETURN = false; + private static final boolean REDIS_TEST_WHILE_IDLE = false; + + public static final JedisPoolConfig CONFIG = new JedisPoolConfig(); + private static final Map POOL_MAP = Maps.newConcurrentMap(); + + static { + //连接池中最大连接数。高版本:maxTotal,低版本:maxActive + CONFIG.setMaxTotal(REDIS_MAX_TOTAL); + //连接池中最大空闲的连接数 + CONFIG.setMaxIdle(REDIS_MAX_IDLE); + //连接池中最少空闲的连接数 + CONFIG.setMinIdle(REDIS_MIN_IDLE); + //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait + CONFIG.setMaxWaitMillis(REDIS_MAX_WAIT_MILLIS); + //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 + CONFIG.setMinEvictableIdleTimeMillis(REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS); + //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 + CONFIG.setNumTestsPerEvictionRun(REDIS_NUM_TESTS_PER_EVICTION_RUN); + //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 + CONFIG.setTimeBetweenEvictionRunsMillis(REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); + //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. + CONFIG.setTestOnBorrow(REDIS_TEST_ON_BORROW); + //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 + CONFIG.setTestOnReturn(REDIS_TEST_ON_RETURN); + //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. + CONFIG.setTestWhileIdle(REDIS_TEST_WHILE_IDLE); + } + + public static void main(String[] args) { + System.out.println(Jsons.toJson(CONFIG)); + } + + public static Jedis getClient(RedisServer node) { + JedisPool pool = POOL_MAP.get(node); if (pool == null) { - pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); - holder.put(node, pool); + pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), REDIS_TIMEOUT, node.getPassword()); + POOL_MAP.put(node, pool); } return pool.getResource(); } @@ -30,9 +67,9 @@ public static void close(Jedis jedis) { jedis.close(); } - public static long incr(List nodeList, String key, Integer time) { + public static long incr(List nodeList, String key, Integer time) { long incrRet = -1; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -52,9 +89,9 @@ public static long incr(List nodeList, String key, Integer time) { } - public static long incrBy(List nodeList, String key, long delt) { + public static long incrBy(List nodeList, String key, long delt) { long incrRet = -1; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -79,7 +116,7 @@ public static long incrBy(List nodeList, String key, long delt) { * @return */ @SuppressWarnings("unchecked") - public static T get(RedisNode node, String key, Class clazz) { + public static T get(RedisServer node, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -96,17 +133,17 @@ public static T get(RedisNode node, String key, Class clazz) { return Jsons.fromJson(value, clazz); } - public static void set(List nodeList, String key, String value) { + public static void set(List nodeList, String key, String value) { set(nodeList, key, value, null); } - public static void set(List nodeList, String key, T value) { + public static void set(List nodeList, String key, T value) { set(nodeList, key, value, null); } - public static void set(List nodeList, String key, T value, Integer time) { + public static void set(List nodeList, String key, T value, Integer time) { String jsonValue = Jsons.toJson(value); set(nodeList, key, jsonValue, time); } @@ -117,11 +154,11 @@ public static void set(List nodeList, String key, T value, Intege * @param value * @param time seconds */ - public static void set(List nodeList, String key, String value, Integer time) { + public static void set(List nodeList, String key, String value, Integer time) { if (time == null) { time = -1; } - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -138,9 +175,9 @@ public static void set(List nodeList, String key, String value, Integ } } - public static void del(List nodeList, String key) { + public static void del(List nodeList, String key) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -160,8 +197,8 @@ public static void del(List nodeList, String key) { /********************* * hash redis start ********************************/ - public static void hset(List nodeList, String namespace, String key, String value) { - for (RedisNode node : nodeList) { + public static void hset(List nodeList, String namespace, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -175,11 +212,11 @@ public static void hset(List nodeList, String namespace, String key, } } - public static void hset(List nodeList, String namespace, String key, T value) { + public static void hset(List nodeList, String namespace, String key, T value) { hset(nodeList, namespace, key, Jsons.toJson(value)); } - public static T hget(RedisNode node, String namespace, String key, Class clazz) { + public static T hget(RedisServer node, String namespace, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -196,9 +233,9 @@ public static T hget(RedisNode node, String namespace, String key, Class } - public static void hdel(List nodeList, String namespace, String key) { + public static void hdel(List nodeList, String namespace, String key) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -212,7 +249,7 @@ public static void hdel(List nodeList, String namespace, String key) } } - public static Map hgetAll(RedisNode node, String namespace) { + public static Map hgetAll(RedisServer node, String namespace) { Map result = null; Jedis jedis = null; try { @@ -227,7 +264,7 @@ public static Map hgetAll(RedisNode node, String namespace) { return result; } - public static Map hgetAll(RedisNode node, String namespace, Class clazz) { + public static Map hgetAll(RedisServer node, String namespace, Class clazz) { Map result = null; Jedis jedis = null; try { @@ -262,7 +299,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class * @param key * @return */ - public static Set hkeys(RedisNode node, String key) { + public static Set hkeys(RedisServer node, String key) { Set result = null; Jedis jedis = null; try { @@ -284,10 +321,9 @@ public static Set hkeys(RedisNode node, String key) { * @param node * @param key * @param clazz - * @param fields * @return */ - public static List hmget(RedisNode node, String namespace, Class clazz, String... key) { + public static List hmget(RedisServer node, String namespace, Class clazz, String... key) { List value = null; Jedis jedis = null; @@ -316,16 +352,15 @@ public static List hmget(RedisNode node, String namespace, Class clazz * 关联 * * @param nodeList - * @param key * @param hash * @param time */ - public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + public static void hmset(List nodeList, String namespace, Map hash, Integer time) { if (time == null) { time = -1; } - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -344,7 +379,7 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String namespace, Map hash) { + public static void hmset(List nodeList, String namespace, Map hash) { hmset(nodeList, namespace, hash, null); } @@ -354,9 +389,9 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String key, String value) { + public static void lpush(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -371,7 +406,7 @@ public static void lpush(List nodeList, String key, String value) { } - public static void lpush(List nodeList, String key, T value) { + public static void lpush(List nodeList, String key, T value) { lpush(nodeList, key, Jsons.toJson(value)); @@ -380,9 +415,9 @@ public static void lpush(List nodeList, String key, T value) { /** * 从队列的右边入队 */ - public static void rpush(List nodeList, String key, String value) { + public static void rpush(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -396,16 +431,16 @@ public static void rpush(List nodeList, String key, String value) { } } - public static void rpush(List nodeList, String key, T value) { + public static void rpush(List nodeList, String key, T value) { rpush(nodeList, key, Jsons.toJson(value)); } /** * 移除并且返回 key 对应的 list 的第一个元素 */ - public static T lpop(List nodeList, String key, Class clazz) { + public static T lpop(List nodeList, String key, Class clazz) { String retValue = null; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -426,9 +461,9 @@ public static T lpop(List nodeList, String key, Class clazz) { /** * 从队列的右边出队一个元素 */ - public static T rpop(List nodeList, String key, Class clazz) { + public static T rpop(List nodeList, String key, Class clazz) { String retValue = null; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -451,7 +486,7 @@ public static T rpop(List nodeList, String key, Class clazz) { * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List lrange(RedisNode node, String key, int start, int end, Class clazz) { + public static List lrange(RedisServer node, String key, int start, int end, Class clazz) { List value = null; Jedis jedis = null; try { @@ -477,7 +512,7 @@ public static List lrange(RedisNode node, String key, int start, int end, * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key * 里的值不是一个list的话,会返回error。 */ - public static long llen(RedisNode node, String key) { + public static long llen(RedisServer node, String key) { long len = 0; Jedis jedis = null; @@ -500,9 +535,9 @@ public static long llen(RedisNode node, String key) { * @param key * @param value */ - public static void lRem(List nodeList, String key, String value) { + public static void lRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -520,11 +555,11 @@ public static void lRem(List nodeList, String key, String value) { /********************* list redis end ********************************/ /********************* - * pubsub redis start + * mq redis start ********************************/ - public static void publish(RedisNode node, String channel, T message) { + public static void publish(RedisServer node, String channel, T message) { Jedis jedis = null; String value = null; if (message instanceof String) { @@ -543,20 +578,19 @@ public static void publish(RedisNode node, String channel, T message) { } } - public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { - for (final RedisNode node : nodeList) { - String name = node.getIp() + "_" + Jsons.toJson(channels); - ThreadPoolManager.newThread(name, new Runnable() { + public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { + for (final RedisServer node : nodeList) { + new Thread(new Runnable() { @Override public void run() { subscribe(node, pubsub, channels); } - }).start(); + }, node.getHost() + "_" + Jsons.toJson(channels) + ).start(); } } - public static void subscribe(RedisNode node, JedisPubSub pubsub, String... channel) { - + public static void subscribe(RedisServer node, JedisPubSub pubsub, String... channel) { Jedis jedis = null; try { jedis = getClient(node); @@ -577,10 +611,9 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann * @param nodeList * @param key * @param value - * @param time seconds */ - public static void sAdd(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + public static void sAdd(List nodeList, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -595,12 +628,11 @@ public static void sAdd(List nodeList, String key, String value) { } /** - * @param node 返回个数 + * @param node 返回个数 * @param key - * @param clazz * @return */ - public static Long sCard(RedisNode node, String key) { + public static Long sCard(RedisServer node, String key) { Long value = null; Jedis jedis = null; @@ -616,9 +648,9 @@ public static Long sCard(RedisNode node, String key) { return value; } - public static void sRem(List nodeList, String key, String value) { + public static void sRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -639,10 +671,9 @@ public static void sRem(List nodeList, String key, String value) { * @param node * @param key * @param clazz - * @param fields * @return */ - public static List sScan(RedisNode node, String key, Class clazz, int start) { + public static List sScan(RedisServer node, String key, Class clazz, int start) { List value = null; Jedis jedis = null; @@ -677,8 +708,8 @@ public static List sScan(RedisNode node, String key, Class clazz, int * @param key * @param value */ - public static void zAdd(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + public static void zAdd(List nodeList, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -693,12 +724,11 @@ public static void zAdd(List nodeList, String key, String value) { } /** - * @param node 返回个数 + * @param node 返回个数 * @param key - * @param clazz * @return */ - public static Long zCard(RedisNode node, String key) { + public static Long zCard(RedisServer node, String key) { Long value = null; Jedis jedis = null; @@ -714,9 +744,9 @@ public static Long zCard(RedisNode node, String key) { return value; } - public static void zRem(List nodeList, String key, String value) { + public static void zRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -736,7 +766,7 @@ public static void zRem(List nodeList, String key, String value) { * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List zrange(RedisNode node, String key, int start, int end, Class clazz) { + public static List zrange(RedisServer node, String key, int start, int end, Class clazz) { Set value = null; Jedis jedis = null; try { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java new file mode 100644 index 00000000..ac920952 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java @@ -0,0 +1,50 @@ +package com.mpush.cache.redis; + +import com.google.common.collect.Lists; + +import java.util.List; + + +/** + * redis 组 + */ +public class RedisGroup { + private List redisServerList = Lists.newArrayList(); + + public List getRedisServerList() { + return redisServerList; + } + + public void setRedisServerList(List redisServerList) { + this.redisServerList = redisServerList; + } + + public void addRedisNode(RedisServer node) { + redisServerList.add(node); + } + + public void remove(int i) { + if (redisServerList != null) { + redisServerList.remove(i); + } + } + + public void clear() { + if (redisServerList != null) { + redisServerList.clear(); + } + } + + public RedisServer get(String key) { + int i = key.hashCode() % redisServerList.size(); + return redisServerList.get(i); + } + + public static RedisGroup from(com.mpush.tools.config.data.RedisGroup node) { + RedisGroup group = new RedisGroup(); + for (com.mpush.tools.config.data.RedisServer rs : node.redisNodeList) { + group.addRedisNode(new RedisServer(rs.getHost(), rs.getPort(), rs.getPassword())); + } + return group; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java similarity index 96% rename from mpush-api/src/main/java/com/mpush/api/RedisKey.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 0077c4d1..1830e96c 100644 --- a/mpush-api/src/main/java/com/mpush/api/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -1,4 +1,4 @@ -package com.mpush.api; +package com.mpush.cache.redis; public final class RedisKey { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java new file mode 100644 index 00000000..d3d82380 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java @@ -0,0 +1,11 @@ +package com.mpush.cache.redis; + +/** + * redis 相关的配置信息 + */ +public class RedisServer extends com.mpush.tools.config.data.RedisServer { + + public RedisServer(String ip, int port, String password) { + super(ip, port, password); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java similarity index 96% rename from mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java index c50d3eac..dd648861 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java @@ -1,4 +1,4 @@ -package com.mpush.tools.redis.consistenthash; +package com.mpush.cache.redis.hash; import java.util.Collection; import java.util.SortedMap; diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java similarity index 88% rename from mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java index 59beaacb..8b9b55bf 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java @@ -1,4 +1,4 @@ -package com.mpush.tools.redis.consistenthash; +package com.mpush.cache.redis.hash; public class Node { diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java similarity index 68% rename from mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java index 428bf120..89a6c09a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -1,4 +1,4 @@ -package com.mpush.tools.redis.listener; +package com.mpush.cache.redis.listener; import java.util.List; import java.util.Map; @@ -6,21 +6,21 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.log.Logs; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.log.Logs; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.tools.redis.pubsub.Subscriber; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.cache.redis.mq.Subscriber; +import com.mpush.tools.thread.pool.ThreadPoolManager; public class ListenerDispatcher implements MessageListener { - public static final ListenerDispatcher INSTANCE = new ListenerDispatcher(); + public static final ListenerDispatcher I = new ListenerDispatcher(); - private Map> subscribes = Maps.newTreeMap(); - - private ListenerDispatcher(){} + private final Map> subscribes = Maps.newTreeMap(); + + private final Executor executor = ThreadPoolManager.I.getRedisExecutor(); - private Executor executor = ThreadPoolManager.redisExecutor; + private ListenerDispatcher(){} @Override public void onMessage(final String channel, final String message) { @@ -46,6 +46,6 @@ public void subscribe(String channel, MessageListener listener) { subscribes.put(channel, listeners); } listeners.add(listener); - RedisManage.subscribe(Subscriber.holder, channel); + RedisManager.I.subscribe(Subscriber.holder, channel); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java similarity index 69% rename from mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java index 627bfefc..fcaf3cc6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java @@ -1,4 +1,4 @@ -package com.mpush.tools.redis.listener; +package com.mpush.cache.redis.listener; public interface MessageListener { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java new file mode 100644 index 00000000..a57fa125 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java @@ -0,0 +1,17 @@ +package com.mpush.cache.redis.manager; + +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; + +import java.util.List; + +public interface RedisClusterManager { + + void init(); + + List getGroupList(); + + RedisServer randomGetRedisNode(String key); + + List hashSet(String key); +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java new file mode 100644 index 00000000..ff9c23ef --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -0,0 +1,299 @@ +package com.mpush.cache.redis.manager; + +import com.google.common.collect.Sets; +import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.tools.Jsons; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPubSub; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * redis 对外封装接口 + */ +public class RedisManager { + public static final RedisManager I = new RedisManager(); + + private final RedisClusterManager clusterManager = ZKRedisClusterManager.I; + + public void init() { + ZKRedisClusterManager.I.init(); + test(clusterManager.getGroupList()); + } + + public long incr(String key, Integer time) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.incr(nodeList, key, time); + } + + public long incrBy(String key, long delt) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.incrBy(nodeList, key, delt); + } + + /********************* + * k v redis start + ********************************/ + + public T get(String key, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.get(node, key, clazz); + } + + public void set(String key, T value) { + set(key, value, null); + } + + public void set(String key, T value, Integer time) { + String jsonValue = Jsons.toJson(value); + set(key, jsonValue, time); + } + + /** + * @param key + * @param value + * @param time seconds + */ + public void set(String key, String value, Integer time) { + List nodeList = clusterManager.hashSet(key); + RedisClient.set(nodeList, key, value, time); + } + + public void del(String key) { + + List nodeList = clusterManager.hashSet(key); + RedisClient.del(nodeList, key); + + } + + /*********************k v redis end********************************/ + + + /********************* + * hash redis start + ********************************/ + public void hset(String namespace, String key, String value) { + + List nodeList = clusterManager.hashSet(key); + RedisClient.hset(nodeList, namespace, key, value); + + } + + public void hset(String namespace, String key, T value) { + hset(namespace, key, Jsons.toJson(value)); + } + + public T hget(String namespace, String key, Class clazz) { + + RedisServer node = clusterManager.randomGetRedisNode(namespace); + return RedisClient.hget(node, namespace, key, clazz); + + } + + public void hdel(String namespace, String key) { + List nodeList = clusterManager.hashSet(namespace); + RedisClient.hdel(nodeList, namespace, key); + } + + public Map hgetAll(String namespace) { + + RedisServer node = clusterManager.randomGetRedisNode(namespace); + return RedisClient.hgetAll(node, namespace); + + } + + public Map hgetAll(String namespace, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(namespace); + return RedisClient.hgetAll(node, namespace, clazz); + } + + /** + * 返回 key 指定的哈希集中所有字段的名字。 + * + * @return + */ + public Set hkeys(String namespace) { + RedisServer node = clusterManager.randomGetRedisNode(namespace); + return RedisClient.hkeys(node, namespace); + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * + * @param key + * @param clazz + * @return + */ + public List hmget(String namespace, Class clazz, String... key) { + RedisServer node = clusterManager.randomGetRedisNode(namespace); + return RedisClient.hmget(node, namespace, clazz, key); + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * + * @param hash + * @param time + */ + public void hmset(String namespace, Map hash, Integer time) { + List nodeList = clusterManager.hashSet(namespace); + RedisClient.hmset(nodeList, namespace, hash, time); + } + + public void hmset(String namespace, Map hash) { + hmset(namespace, hash, null); + } + + + /*********************hash redis end********************************/ + + + /*********************list redis start********************************/ + /** + * 从队列的左边入队 + */ + public void lpush(String key, String value) { + List nodeList = clusterManager.hashSet(key); + RedisClient.lpush(nodeList, key, value); + } + + public void lpush(String key, T value) { + lpush(key, Jsons.toJson(value)); + } + + /** + * 从队列的右边入队 + */ + public void rpush(String key, String value) { + List nodeList = clusterManager.hashSet(key); + RedisClient.rpush(nodeList, key, value); + } + + public void rpush(String key, T value) { + rpush(key, Jsons.toJson(value)); + } + + /** + * 移除并且返回 key 对应的 list 的第一个元素 + */ + public T lpop(String key, Class clazz) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.lpop(nodeList, key, clazz); + } + + /** + * 从队列的右边出队一个元素 + */ + public T rpop(String key, Class clazz) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.rpop(nodeList, key, clazz); + } + + + /** + * 从列表中获取指定返回的元素 + * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public List lrange(String key, int start, int end, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.lrange(node, key, start, end, clazz); + } + + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + */ + public long llen(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.llen(node, key); + } + + public void lrem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.lRem(nodeList, key, jsonValue); + } + + public void publish(String channel, T message) { + + RedisServer node = clusterManager.randomGetRedisNode(channel); + RedisClient.publish(node, channel, message); + + } + + public void subscribe(JedisPubSub pubsub, String... channels) { + + Set set = Sets.newHashSet(); + for (String channel : channels) { + List nodeList = clusterManager.hashSet(channel); + set.addAll(nodeList); + } + + RedisClient.subscribe(set, pubsub, channels); + } + + public void sAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.sAdd(nodeList, key, jsonValue); + } + + public Long sCard(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.sCard(node, key); + } + + public void sRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.sRem(nodeList, key, jsonValue); + } + + public List sScan(String key, int start, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.sScan(node, key, clazz, start); + } + + public void zAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.zAdd(nodeList, key, jsonValue); + } + + public Long zCard(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.zCard(node, key); + } + + public void zRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.zRem(nodeList, key, jsonValue); + } + + public List zrange(String key, int start, int end, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.zrange(node, key, start, end, clazz); + } + + public void test(List groupList) { + if (groupList == null || groupList.isEmpty()) { + throw new RuntimeException("init redis sever error."); + } + for (RedisGroup group : groupList) { + List list = group.getRedisServerList(); + if (list == null || list.isEmpty()) { + throw new RuntimeException("init redis sever error."); + } + for (RedisServer node : list) { + Jedis jedis = RedisClient.getClient(node); + if (jedis == null) throw new RuntimeException("init redis sever error."); + jedis.close(); + } + } + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java new file mode 100644 index 00000000..aa7aa168 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -0,0 +1,109 @@ +package com.mpush.cache.redis.manager; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKRedisNodeWatcher; +import com.mpush.zk.node.ZKRedisNode; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static com.mpush.zk.ZKPath.REDIS_SERVER; + +public class ZKRedisClusterManager implements RedisClusterManager { + public static final ZKRedisClusterManager I = new ZKRedisClusterManager(); + + private ZKRedisClusterManager() { + } + + private final List groups = new ArrayList<>(); + + /** + * zk 启动的时候需要调用这个 + */ + @Override + public void init() { + Logs.Console.info("begin init redis cluster"); + List groupList = CC.mp.redis.cluster_group; + if (groupList.size() > 0) { + if (CC.mp.redis.write_to_zk) { + register(groupList); + } else if (!ZKClient.I.isExisted(REDIS_SERVER.getRootPath())) { + register(groupList); + } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getRootPath()))) { + register(groupList); + } + } + + ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); + watcher.beginWatch(); + Collection nodes = watcher.getCache().values(); + if (nodes == null || nodes.isEmpty()) { + Logs.REDIS.info("init redis client error, redis server is none."); + throw new RuntimeException("init redis client error, redis server is none."); + } + for (ZKRedisNode node : nodes) { + groups.add(RedisGroup.from(node)); + } + if (groups.isEmpty()) throw new RuntimeException("init redis sever fail groupList is null"); + Logs.Console.info("init redis cluster success..."); + } + + @Override + public List getGroupList() { + return Collections.unmodifiableList(groups); + } + + public int groupSize() { + return groups.size(); + } + + /** + * 随机获取一个redis 实例 + * + * @param key + * @return + */ + @Override + public RedisServer randomGetRedisNode(String key) { + int size = groupSize(); + if (size == 1) return groups.get(0).get(key); + int i = (int) ((Math.random() % size) * size); + RedisGroup group = groups.get(i); + return group.get(key); + } + + /** + * 写操作的时候,获取所有redis 实例 + * + * @param key + * @return + */ + @Override + public List hashSet(String key) { + List nodeList = Lists.newArrayList(); + for (RedisGroup group : groups) { + RedisServer node = group.get(key); + nodeList.add(node); + } + return nodeList; + } + + private void register(List groupList) { + String data = Jsons.toJson(groupList); + ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data); + Logs.Console.info("register redis server group success, group=" + data); + } + + public void addGroup(RedisGroup group) { + groups.add(group); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java similarity index 93% rename from mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java index 54534522..7a0a4ed1 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java @@ -1,15 +1,13 @@ -package com.mpush.tools.redis.pubsub; - -import com.mpush.log.Logs; +package com.mpush.cache.redis.mq; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.tools.log.Logs; import com.mpush.tools.Jsons; -import com.mpush.tools.redis.listener.ListenerDispatcher; - import redis.clients.jedis.JedisPubSub; public class Subscriber extends JedisPubSub { - private static ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; + private static ListenerDispatcher dispatcher = ListenerDispatcher.I; public static Subscriber holder = new Subscriber(); diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 54440364..c62a39b4 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -11,7 +11,7 @@ 4.0.0 ${mpush.groupId} mpush-client - ${mpush-client-version} + ${mpush.version} mpush-client jar @@ -25,39 +25,11 @@ ${mpush.groupId} - mpush-zk + mpush-common ${mpush.groupId} - mpush-common + mpush-cache - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-client - - - - - - - -
diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java new file mode 100644 index 00000000..1a27275e --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java @@ -0,0 +1,79 @@ +package com.mpush.client.connect; + + +public class ClientConfig { + private byte[] clientKey; + private byte[] iv; + private String clientVersion; + private String deviceId; + private String osName; + private String osVersion; + private String userId; + private String cipher; //快速重连的时候使用 + + + public byte[] getClientKey() { + return clientKey; + } + + public void setClientKey(byte[] clientKey) { + this.clientKey = clientKey; + } + + public byte[] getIv() { + return iv; + } + + public void setIv(byte[] iv) { + this.iv = iv; + } + + public String getClientVersion() { + return clientVersion; + } + + public void setClientVersion(String clientVersion) { + this.clientVersion = clientVersion; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getOsName() { + return osName; + } + + public void setOsName(String osName) { + this.osName = osName; + } + + public String getOsVersion() { + return osVersion; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public String getCipher() { + return cipher; + } + + public void setCipher(String cipher) { + this.cipher = cipher; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + +} diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java new file mode 100644 index 00000000..c60aedb4 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -0,0 +1,225 @@ +package com.mpush.client.connect; + + +import com.google.common.collect.Maps; +import com.mpush.cache.redis.RedisKey; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.message.*; +import com.mpush.common.security.AesCipher; +import com.mpush.common.security.CipherBox; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.ThreadNames; +import io.netty.channel.*; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timeout; +import io.netty.util.Timer; +import io.netty.util.TimerTask; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn + */ +@ChannelHandler.Sharable +public final class ConnClientChannelHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); + private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.NETTY_TIMER)); + + private Connection connection = new NettyConnection(); + private ClientConfig clientConfig; + + public ConnClientChannelHandler(ClientConfig clientConfig) { + this.clientConfig = clientConfig; + } + + public Connection getConnection() { + return connection; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + connection.updateLastReadTime(); + //加密 + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + Command command = Command.toCMD(packet.cmd); + if (command == Command.HANDSHAKE) { + connection.getSessionContext().changeCipher(new AesCipher(clientConfig.getClientKey(), clientConfig.getIv())); + HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); + byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); + connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); + startHeartBeat(message.heartbeat); + LOGGER.warn("会话密钥:{},message={}", sessionKey, message); + bindUser(clientConfig); + saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); + } else if (command == Command.FAST_CONNECT) { + String cipherStr = clientConfig.getCipher(); + String[] cs = cipherStr.split(","); + byte[] key = AesCipher.toArray(cs[0]); + byte[] iv = AesCipher.toArray(cs[1]); + connection.getSessionContext().changeCipher(new AesCipher(key, iv)); + + FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); + startHeartBeat(message.heartbeat); + bindUser(clientConfig); + LOGGER.warn("fast connect success, message=" + message); + } else if (command == Command.KICK) { + KickUserMessage message = new KickUserMessage(packet, connection); + LOGGER.error("receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); + ctx.close(); + } else if (command == Command.ERROR) { + ErrorMessage errorMessage = new ErrorMessage(packet, connection); + LOGGER.error("receive an error packet=" + errorMessage); + } else if (command == Command.BIND) { + OkMessage okMessage = new OkMessage(packet, connection); + LOGGER.warn("receive an success packet=" + okMessage); + HttpRequestMessage message = new HttpRequestMessage(connection); + message.uri = "http://baidu.com"; + message.send(); + } else if (command == Command.PUSH) { + PushMessage message = new PushMessage(packet, connection); + LOGGER.warn("receive an push message, content=" + message.content); + } else if (command == Command.HEARTBEAT) { + LOGGER.warn("receive a heartbeat pong..."); + } else { + LOGGER.warn("receive a message, type=" + command + "," + packet); + } + } + + + LOGGER.warn("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + connection.init(ctx.channel(), true); + tryFastConnect(); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + LOGGER.info("client disconnect channel={}", ctx.channel()); + } + + private void tryFastConnect() { + + Map sessionTickets = getFastConnectionInfo(clientConfig.getDeviceId()); + + if (sessionTickets == null) { + handshake(clientConfig); + return; + } + String sessionId = sessionTickets.get("sessionId"); + if (sessionId == null) { + handshake(clientConfig); + return; + } + String expireTime = sessionTickets.get("expireTime"); + if (expireTime != null) { + long exp = Long.parseLong(expireTime); + if (exp < System.currentTimeMillis()) { + handshake(clientConfig); + return; + } + } + + final String cipher = sessionTickets.get("cipherStr"); + + FastConnectMessage message = new FastConnectMessage(connection); + message.deviceId = clientConfig.getDeviceId(); + message.sessionId = sessionId; + + message.sendRaw(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + if (channelFuture.isSuccess()) { + clientConfig.setCipher(cipher); + } else { + handshake(clientConfig); + } + } + }); + } + + private void bindUser(ClientConfig client) { + BindUserMessage message = new BindUserMessage(connection); + message.userId = client.getUserId(); + message.send(); + } + + private void saveToRedisForFastConnection(ClientConfig client, String sessionId, Long expireTime, byte[] sessionKey) { + Map map = Maps.newHashMap(); + map.put("sessionId", sessionId); + map.put("expireTime", expireTime + ""); + map.put("cipherStr", connection.getSessionContext().cipher.toString()); + String key = RedisKey.getDeviceIdKey(client.getDeviceId()); + RedisManager.I.set(key, map, 60 * 5); //5分钟 + } + + private Map getFastConnectionInfo(String deviceId) { + String key = RedisKey.getDeviceIdKey(deviceId); + return RedisManager.I.get(key, Map.class); + } + + private void handshake(ClientConfig client) { + HandshakeMessage message = new HandshakeMessage(connection); + message.clientKey = client.getClientKey(); + message.iv = client.getIv(); + message.clientVersion = client.getClientVersion(); + message.deviceId = client.getDeviceId(); + message.osName = client.getOsName(); + message.osVersion = client.getOsVersion(); + message.timestamp = System.currentTimeMillis(); + message.send(); + } + + + public void startHeartBeat(final int heartbeat) throws Exception { + HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + final Channel channel = connection.getChannel(); + + try { + ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); + channelFuture.addListener(new ChannelFutureListener() { + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + if (!channel.isActive()) { + LOGGER.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); + } + LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString()); + } else { + LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + } + } + }); + } finally { + if (channel.isActive()) { + HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); + } + } + } + }, heartbeat, TimeUnit.MILLISECONDS); + } +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java new file mode 100644 index 00000000..949edc78 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java @@ -0,0 +1,20 @@ +package com.mpush.client.connect; + +import com.mpush.netty.client.NettyClient; +import io.netty.channel.ChannelHandler; + +public class ConnectClient extends NettyClient { + + private final ConnClientChannelHandler handler; + + public ConnectClient(String host, int port, ClientConfig config) { + super(host, port); + handler = new ConnClientChannelHandler(config); + } + + @Override + public ChannelHandler getChannelHandler() { + return handler; + } + +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java new file mode 100644 index 00000000..8616f73c --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -0,0 +1,27 @@ +package com.mpush.client.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.netty.client.NettyClient; +import io.netty.channel.ChannelHandler; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClient extends NettyClient { + private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); + + public GatewayClient(String host, int port) { + super(host, port); + } + + @Override + public ChannelHandler getChannelHandler() { + return handler; + } + + public Connection getConnection() { + return handler.getConnection(); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java new file mode 100644 index 00000000..b880c12b --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java @@ -0,0 +1,20 @@ +package com.mpush.client.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.zk.ZKPath; +import com.mpush.zk.listener.ZKServerNodeWatcher; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClientBoot { + + public void start() { + + } + + +} diff --git a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java similarity index 67% rename from mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java rename to mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java index c91d3486..f12eb428 100644 --- a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -1,28 +1,20 @@ -package com.mpush.push.client; - +package com.mpush.client.gateway; +import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.Client; import com.mpush.common.message.ErrorMessage; -import com.mpush.netty.client.ChannelClientHandler; import com.mpush.netty.connection.NettyConnection; -import com.mpush.push.PushRequest; -import com.mpush.push.PushRequestBus; - +import com.mpush.client.push.PushRequest; +import com.mpush.client.push.PushRequestBus; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static com.mpush.common.ErrorCode.OFFLINE; -import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.mpush.common.ErrorCode.ROUTER_CHANGE; +import static com.mpush.common.ErrorCode.*; /** * Created by ohun on 2015/12/19. @@ -30,24 +22,15 @@ * @author ohun@live.cn */ @ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { +public final class GatewayClientChannelHandler extends ChannelHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); + + private Connection connection = new NettyConnection(); - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); + connection.updateLastReadTime(); if (msg instanceof Packet) { Packet packet = ((Packet) msg); PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); @@ -59,7 +42,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception if (packet.cmd == Command.OK.cmd) { request.success(); } else { - ErrorMessage message = new ErrorMessage(packet, client.getConnection()); + ErrorMessage message = new ErrorMessage(packet, connection); if (message.code == OFFLINE.errorCode) { request.offline(); } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { @@ -70,28 +53,28 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.warn("receive an error gateway response, message={}", message); } } - LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); + LOGGER.info("receive msg:" + ctx.channel() + "," + msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - client.close("exception"); + connection.close(); LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); connection.init(ctx.channel(), false); - client.initConnection(connection); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - client.close("inactive"); + connection.close(); LOGGER.info("client disconnect channel={}", ctx.channel()); } - + public Connection getConnection() { + return connection; + } } \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java new file mode 100644 index 00000000..c66d4868 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java @@ -0,0 +1,86 @@ +package com.mpush.client.gateway; + +import com.google.common.collect.Maps; +import com.mpush.api.Client; +import com.mpush.api.Service; +import com.mpush.api.connection.Connection; +import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.zk.node.ZKServerNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClientFactory extends ZKServerNodeCache { + public static final GatewayClientFactory I = new GatewayClientFactory(); + + private final Logger logger = LoggerFactory.getLogger(GatewayClientFactory.class); + + private final Map ip_client = Maps.newConcurrentMap(); + + @Override + public void put(String fullPath, ZKServerNode node) { + super.put(fullPath, node); + addClient(node); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = super.remove(fullPath); + removeClient(node); + return node; + } + + @Override + public void clear() { + super.clear(); + for (GatewayClient client : ip_client.values()) { + client.stop(null); + } + } + + public GatewayClient getClient(String ip) { + GatewayClient client = ip_client.get(ip); + if (client == null) { + return null;//TODO create client + } + return client; + } + + public Connection getConnection(String ip) { + GatewayClient client = ip_client.get(ip); + if (client == null) { + return null;//TODO create client + } + return client.getConnection(); + } + + private void removeClient(ZKServerNode node) { + if (node != null) { + Client client = ip_client.remove(node.getIp()); + if (client != null) { + client.stop(null); + } + } + } + + private void addClient(final ZKServerNode node) { + final GatewayClient client = new GatewayClient(node.getIp(), node.getPort()); + client.start(new Service.Listener() { + @Override + public void onSuccess(Object... args) { + ip_client.put(node.getIp(), client); + } + + @Override + public void onFailure(Throwable cause) { + logger.error("create gateway client ex, node", node, cause); + } + }); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java new file mode 100644 index 00000000..10ed1bcb --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -0,0 +1,70 @@ +package com.mpush.client.push; + +import com.google.common.base.Strings; +import com.mpush.api.BaseService; +import com.mpush.api.connection.Connection; +import com.mpush.api.push.PushSender; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.client.gateway.GatewayClientFactory; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.listener.ZKServerNodeWatcher; + +import java.util.Collection; + +/*package*/ class PushClient extends BaseService implements PushSender { + private static final int DEFAULT_TIMEOUT = 3000; + private final GatewayClientFactory factory = GatewayClientFactory.I; + + public void send(String content, Collection userIds, Callback callback) { + if (Strings.isNullOrEmpty(content)) return; + for (String userId : userIds) { + PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(); + } + } + + @Override + public void send(String content, String userId, Callback callback) { + PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(); + } + + public Connection getGatewayConnection(String host) { + return factory.getConnection(host); + } + + @Override + public void start(Listener listener) { + if (started.compareAndSet(false, true)) { + ZKClient.I.init(); + RedisManager.I.init(); + ZKServerNodeWatcher.build(ZKPath.GATEWAY_SERVER, factory).beginWatch(); + if (listener != null) { + listener.onSuccess(0); + } + } + } + + @Override + public void stop(Listener listener) { + if (started.compareAndSet(true, false)) { + factory.clear(); + } + } + + @Override + public boolean isRunning() { + return started.get(); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java new file mode 100644 index 00000000..98f96f91 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java @@ -0,0 +1,23 @@ +package com.mpush.client.push; + +import com.mpush.api.push.PushSender; +import com.mpush.api.spi.client.PusherFactory; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class PushClientFactory implements PusherFactory { + private static PushClient CLIENT; + + @Override + public PushSender get() { + if (CLIENT == null) { + synchronized (PushClientFactory.class) { + CLIENT = new PushClient(); + } + } + return CLIENT; + } +} diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java similarity index 98% rename from mpush-client/src/main/java/com/mpush/push/PushRequest.java rename to mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index cda87e8b..2156bcee 100644 --- a/mpush-client/src/main/java/com/mpush/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -1,7 +1,7 @@ -package com.mpush.push; +package com.mpush.client.push; import com.google.common.collect.Maps; -import com.mpush.api.PushSender; +import com.mpush.api.push.PushSender; import com.mpush.api.connection.Connection; import com.mpush.api.router.ClientLocation; import com.mpush.common.message.gateway.GatewayPushMessage; diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java similarity index 97% rename from mpush-client/src/main/java/com/mpush/push/PushRequestBus.java rename to mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 3bfc3b8e..62f2e880 100644 --- a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -1,4 +1,4 @@ -package com.mpush.push; +package com.mpush.client.push; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java deleted file mode 100644 index 238d12ad..00000000 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ /dev/null @@ -1,223 +0,0 @@ -package com.mpush.conn.client; - - -import java.util.Map; - -import com.google.common.collect.Maps; -import com.mpush.api.protocol.Command; -import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.Client; -import com.mpush.api.RedisKey; -import com.mpush.common.message.*; -import com.mpush.common.security.AesCipher; -import com.mpush.common.security.CipherBox; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.netty.client.ChannelClientHandler; -import com.mpush.netty.client.NettyClient; -import com.mpush.netty.client.SecurityNettyClient; -import com.mpush.netty.connection.NettyConnection; -import com.mpush.tools.redis.manage.RedisManage; - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/19. - * - * @author ohun@live.cn - */ -@ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); - if (client instanceof SecurityNettyClient) { - SecurityNettyClient securityNettyClient = (SecurityNettyClient) client; - Connection connection = client.getConnection(); - //加密 - if (msg instanceof Packet) { - Packet packet = (Packet) msg; - Command command = Command.toCMD(packet.cmd); - if (command == Command.HANDSHAKE) { - connection.getSessionContext().changeCipher(new AesCipher(securityNettyClient.getClientKey(), securityNettyClient.getIv())); - HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); - connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); - client.startHeartBeat(message.heartbeat); - LOGGER.warn("会话密钥:{},message={}", sessionKey, message); - bindUser(securityNettyClient); - saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); - } else if (command == Command.FAST_CONNECT) { - String cipherStr = securityNettyClient.getCipher(); - String[] cs = cipherStr.split(","); - byte[] key = AesCipher.toArray(cs[0]); - byte[] iv = AesCipher.toArray(cs[1]); - connection.getSessionContext().changeCipher(new AesCipher(key, iv)); - - FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); - client.startHeartBeat(message.heartbeat); - bindUser(securityNettyClient); - LOGGER.warn("fast connect success, message=" + message); - } else if (command == Command.KICK) { - KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); - ctx.close(); - } else if (command == Command.ERROR) { - ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error packet=" + errorMessage); - } else if (command == Command.BIND) { - OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.warn("receive an success packet=" + okMessage); - HttpRequestMessage message = new HttpRequestMessage(connection); - message.uri = "http://baidu.com"; - message.send(); - } else if (command == Command.PUSH) { - PushMessage message = new PushMessage(packet, connection); - LOGGER.warn("receive an push message, content=" + message.content); - } else if (command == Command.HEARTBEAT) { - LOGGER.warn("receive a heartbeat pong..."); - } else { - LOGGER.warn("receive a message, type=" + command + "," + packet); - } - } - - } else if (client instanceof NettyClient) {//不加密 - - } - LOGGER.warn("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.remove(ctx.channel()); - } else { - client.close("exception"); - } - - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); - - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.put(ctx.channel(), client); - connection.init(ctx.channel(), true); - client.initConnection(connection); - client.init(ctx.channel()); - tryFastConnect((SecurityNettyClient) client); - } else { - LOGGER.error("connection is not support appear hear:" + client); - connection.init(ctx.channel(), false); - client.initConnection(connection); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.remove(ctx.channel()); - } else { - client.close("inactive"); - } - LOGGER.info("client disconnect channel={}", ctx.channel()); - } - - private void tryFastConnect(final SecurityNettyClient securityNettyClient) { - - Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); - - if (sessionTickets == null) { - handshake(securityNettyClient); - return; - } - String sessionId = (String) sessionTickets.get("sessionId"); - if (sessionId == null) { - handshake(securityNettyClient); - return; - } - String expireTime = (String) sessionTickets.get("expireTime"); - if (expireTime != null) { - long exp = Long.parseLong(expireTime); - if (exp < System.currentTimeMillis()) { - handshake(securityNettyClient); - return; - } - } - - final String cipher = sessionTickets.get("cipherStr"); - - FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); - message.deviceId = securityNettyClient.getDeviceId(); - message.sessionId = sessionId; - - message.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) throws Exception { - if (channelFuture.isSuccess()) { - securityNettyClient.setCipher(cipher); - } else { - handshake(securityNettyClient); - } - } - }); - } - - private void bindUser(SecurityNettyClient client) { - BindUserMessage message = new BindUserMessage(client.getConnection()); - message.userId = client.getUserId(); - message.send(); - } - - private void saveToRedisForFastConnection(SecurityNettyClient client, String sessionId, Long expireTime, byte[] sessionKey) { - Map map = Maps.newHashMap(); - map.put("sessionId", sessionId); - map.put("expireTime", expireTime + ""); - map.put("cipherStr", client.getConnection().getSessionContext().cipher.toString()); - String key = RedisKey.getDeviceIdKey(client.getDeviceId()); - RedisManage.set(key, map, 60 * 5); //5分钟 - } - - private Map getFastConnectionInfo(String deviceId) { - String key = RedisKey.getDeviceIdKey(deviceId); - return RedisManage.get(key, Map.class); - } - - private void handshake(SecurityNettyClient client) { - HandshakeMessage message = new HandshakeMessage(client.getConnection()); - message.clientKey = client.getClientKey(); - message.iv = client.getIv(); - message.clientVersion = client.getClientVersion(); - message.deviceId = client.getDeviceId(); - message.osName = client.getOsName(); - message.osVersion = client.getOsVersion(); - message.timestamp = System.currentTimeMillis(); - message.send(); - } - -} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java b/mpush-client/src/main/java/com/mpush/push/ConnectClient.java deleted file mode 100644 index 65ef68b9..00000000 --- a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.push; - -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; - -public class ConnectClient extends AbstractClient{ - - public ConnectClient() { - registerListener(new ConnectZKListener()); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushClient.java b/mpush-client/src/main/java/com/mpush/push/PushClient.java deleted file mode 100644 index 953b81c5..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushClient.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.mpush.push; - -import com.google.common.base.Strings; -import com.mpush.api.PushSender; -import com.mpush.api.connection.Connection; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.GatewayZKListener; - -import java.util.Collection; - -public class PushClient extends AbstractClient implements PushSender { - private static final int DEFAULT_TIMEOUT = 3000; - private final GatewayZKListener listener = new GatewayZKListener(); - - public PushClient() { - registerListener(listener); - } - - public void send(String content, Collection userIds, Callback callback) { - if (Strings.isNullOrEmpty(content)) return; - for (String userId : userIds) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); - } - } - - @Override - public void send(String content, String userId, Callback callback) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); - } - - public Connection getGatewayConnection(String ip) { - return listener.getManager().getConnection(ip); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java deleted file mode 100644 index 98091e43..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.push.zk.listener; - - -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeListener; -import com.mpush.push.zk.manager.ConnectZKNodeManager; - -/** - * connection server 应用 监控 - */ -public class ConnectZKListener extends ZKServerNodeListener { - - private final ConnectZKNodeManager manager = new ConnectZKNodeManager(); - - @Override - public String listenerPath() { - return ZKPath.CONNECTION_SERVER.getWatchPath(); - } - - @Override - public String getRegisterPath() { - return ZKPath.CONNECTION_SERVER.getPath(); - } - - @Override - public ConnectZKNodeManager getManager() { - return manager; - } - - @Override - public String getFullPath(String raw) { - return ZKPath.CONNECTION_SERVER.getFullPath(raw); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java deleted file mode 100644 index 17f90570..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.push.zk.listener; - -import com.mpush.push.zk.manager.GatewayZKNodeManager; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeListener; - -/** - * gateway server 应用 监控 - */ -public class GatewayZKListener extends ZKServerNodeListener { - - private final GatewayZKNodeManager manager = new GatewayZKNodeManager(); - - @Override - public String listenerPath() { - return ZKPath.GATEWAY_SERVER.getWatchPath(); - } - - @Override - public GatewayZKNodeManager getManager() { - return manager; - } - - @Override - public String getRegisterPath() { - return ZKPath.GATEWAY_SERVER.getPath(); - } - - @Override - public String getFullPath(String raw) { - return ZKPath.GATEWAY_SERVER.getFullPath(raw); - } - - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java deleted file mode 100644 index df9c939a..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.mpush.push.zk.manager; - -import com.google.common.collect.Maps; -import com.mpush.zk.ZKServerNode; -import com.mpush.zk.ZKNodeManager; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -public class ConnectZKNodeManager implements ZKNodeManager { - - private final Logger log = LoggerFactory.getLogger(ConnectZKNodeManager.class); - - private final Map cache = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, ZKServerNode node) { - if (StringUtils.isNotBlank(fullPath) && node != null) { - cache.put(fullPath, node); - } else { - log.error("fullPath is null or application is null"); - } - printList(); - } - - @Override - public void remove(String fullPath) { - cache.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(cache.values()); - } - - private void printList() { - for (ZKServerNode app : cache.values()) { - log.warn(app.toString()); - } - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java deleted file mode 100644 index 66648c52..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.mpush.push.zk.manager; - -import com.google.common.collect.Maps; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; -import com.mpush.api.Client; -import com.mpush.api.connection.Connection; -import com.mpush.netty.client.NettyClient; -import com.mpush.push.client.ClientChannelHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -public class GatewayZKNodeManager implements ZKNodeManager { - - private final Logger log = LoggerFactory.getLogger(GatewayZKNodeManager.class); - - private static Map holder = Maps.newConcurrentMap(); - - private final Map application2Client = Maps.newConcurrentMap(); - - private final Map ip2Client = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, ZKServerNode application) { - holder.put(fullPath, application); - try { - Client client = new NettyClient(application.getIp(), application.getPort()); - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - application2Client.put(application, client); - ip2Client.put(application.getIp(), client); - } catch (Exception e) { - log.error("addOrUpdate:{},{}", fullPath, application, e); - } - printList(); - } - - @Override - public void remove(String fullPath) { - ZKServerNode application = get(fullPath); - if (application != null) { - Client client = application2Client.get(application); - if (client != null) { - client.stop(); - } - } - ip2Client.remove(application.getIp() + ":" + application.getPort()); - holder.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(holder.values()); - } - - private void printList() { - for (ZKServerNode app : holder.values()) { - log.warn(app.toString()); - } - } - - public ZKServerNode get(String fullpath) { - return holder.get(fullpath); - } - - public Client getClient(ZKServerNode application) { - return application2Client.get(application); - } - - public Connection getConnection(String ipAndPort) { - Client client = ip2Client.get(ipAndPort); - if (client == null) return null; - return client.getConnection(); - } - -} - diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory b/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory new file mode 100644 index 00000000..93607f48 --- /dev/null +++ b/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory @@ -0,0 +1 @@ +com.mpush.client.push.PushClientFactory diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager deleted file mode 100644 index d9e4979b..00000000 --- a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager +++ /dev/null @@ -1,2 +0,0 @@ -com.mpush.push.zk.manager.ConnectZKNodeManager -com.mpush.push.zk.manager.GatewayZKNodeManager diff --git a/mpush-client/src/test/java/com/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/mpush/client/PushClientTest.java deleted file mode 100644 index cd9c4979..00000000 --- a/mpush-client/src/test/java/com/mpush/client/PushClientTest.java +++ /dev/null @@ -1,50 +0,0 @@ -//package com.mpush.client; -// -//import PushSender; -//import org.junit.Test; -// -//import java.util.Arrays; -//import java.util.concurrent.locks.LockSupport; -// -//import static org.junit.Assert.*; -// -///** -// * Created by ohun on 2016/1/7. -// */ -//public class PushClientTest { -// -// @Test -// public void testSend() throws Exception { -// -// } -// -// public static void main(String[] args) throws Exception { -// PushClient client = new PushClient(); -// client.init(); -// Thread.sleep(1000); -// client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), -// new PushSender.Callback() { -// @Override -// public void onSuccess(String userId) { -// System.err.println("push onSuccess userId=" + userId); -// } -// -// @Override -// public void onFailure(String userId) { -// System.err.println("push onFailure userId=" + userId); -// } -// -// @Override -// public void onOffline(String userId) { -// System.err.println("push onOffline userId=" + userId); -// } -// -// @Override -// public void onTimeout(String userId) { -// System.err.println("push onTimeout userId=" + userId); -// } -// } -// ); -// LockSupport.park(); -// } -//} \ No newline at end of file diff --git a/mpush-client/src/test/resources/logback.xml b/mpush-client/src/test/resources/logback.xml deleted file mode 100644 index 20979b40..00000000 --- a/mpush-client/src/test/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 867dbf8e..d97f639b 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -11,7 +11,7 @@ ${mpush.groupId} mpush-common - ${mpush-common-version} + ${mpush.version} mpush-common jar @@ -28,6 +28,10 @@ ${mpush.groupId} mpush-zk + + ${mpush.groupId} + mpush-cache + diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java deleted file mode 100644 index 1c5e2a5f..00000000 --- a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.mpush.common; - -import com.google.common.collect.Lists; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKDataChangeListener; -import com.mpush.zk.listener.ZKRedisNodeListener; - -import java.util.List; - -public abstract class AbstractClient { - - protected List dataChangeListeners = Lists.newArrayList(); - - protected ZKClient zkClient = ZKClient.I; - - public AbstractClient() { - registerListener(new ZKRedisNodeListener()); - } - - public void registerListener(ZKDataChangeListener listener) { - dataChangeListeners.add(listener); - } - - //step2 获取redis - private void initRedis() { - boolean exist = zkClient.isExisted(ZKPath.REDIS_SERVER.getPath()); - if (!exist) { - List groupList = ConfigCenter.I.redisGroups(); - zkClient.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - } - - //step3 注册listener - private void registerListeners() { - for (ZKDataChangeListener listener : dataChangeListeners) { - zkClient.registerListener(listener); - } - } - - //step4 初始化 listener data - private void initListenerData() { - for (ZKDataChangeListener listener : dataChangeListeners) { - listener.initData(); - } - } - - public void start() { - initRedis(); - registerListeners(); - initListenerData(); - } -} diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java b/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java deleted file mode 100644 index a9081df0..00000000 --- a/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.mpush.common; - -public abstract class AbstractEventContainer { - - public AbstractEventContainer() { - EventBus.INSTANCE.register(this); - } - -} diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 14b0eaf3..5c650300 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -5,8 +5,8 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.Profiler; import com.mpush.common.message.ErrorMessage; +import com.mpush.tools.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +37,7 @@ public void onReceive(Packet packet, Connection connection) { handler.handle(packet, connection); } } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={}, conn={}", packet, connection, throwable); + LOGGER.error("dispatch packet ex, packet={}, connect={}", packet, connection, throwable); ErrorMessage .from(packet, connection) .setErrorCode(ErrorCode.DISPATCH_ERROR) diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 7c056e6e..bc6c0c42 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -6,7 +6,7 @@ import com.mpush.api.protocol.Packet; import com.mpush.tools.IOUtils; import com.mpush.tools.Profiler; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; @@ -59,7 +59,7 @@ protected void encodeBody() { byte[] tmp = encode(); if (tmp != null && tmp.length > 0) { //1.压缩 - if (tmp.length > ConfigCenter.I.compressLimit()) { + if (tmp.length > CC.mp.core.compress_threshold) { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; diff --git a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java new file mode 100644 index 00000000..44cd3dea --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java @@ -0,0 +1,101 @@ +package com.mpush.common.net; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.api.BaseService; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.data.DnsMapping; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import static com.mpush.tools.MPushUtil.checkHealth; + +public class DnsMappingManager extends BaseService implements Runnable { + private final Logger logger = LoggerFactory.getLogger(DnsMappingManager.class); + + public static final DnsMappingManager I = new DnsMappingManager(); + + private DnsMappingManager() { + } + + private final Map> all = Maps.newConcurrentMap(); + private Map> available = Maps.newConcurrentMap(); + + private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + + @Override + public void start(Listener listener) { + if (started.compareAndSet(false, true)) { + scheduledExecutorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + } + } + + @Override + public void stop(Listener listener) { + if (started.compareAndSet(true, false)) { + scheduledExecutorService.shutdown(); + } + } + + @Override + public void init() { + logger.error("start init dnsMapping"); + all.putAll(CC.mp.http.dns_mapping); + available.putAll(CC.mp.http.dns_mapping); + logger.error("end init dnsMapping"); + } + + @Override + public boolean isRunning() { + return !scheduledExecutorService.isShutdown(); + } + + public void update(Map> nowAvailable) { + available = nowAvailable; + } + + public Map> getAll() { + return all; + } + + public DnsMapping translate(String origin) { + if (available.isEmpty()) + return null; + List list = available.get(origin); + if (list == null || list.isEmpty()) + return null; + int L = list.size(); + if (L == 1) + return list.get(0); + return list.get((int) (Math.random() * L % L)); + } + + @Override + public void run() { + logger.debug("start dns mapping checkHealth"); + Map> all = I.getAll(); + Map> available = Maps.newConcurrentMap(); + for (Map.Entry> entry : all.entrySet()) { + String key = entry.getKey(); + List value = entry.getValue(); + List nowValue = Lists.newArrayList(); + for (DnsMapping temp : value) { + boolean isOk = checkHealth(temp.getIp(), temp.getPort()); + if (isOk) { + nowValue.add(temp); + } else { + logger.error("dns can not reachable:" + Jsons.toJson(temp)); + } + } + available.put(key, nowValue); + } + I.update(available); + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 510372b5..a1b52e16 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -1,8 +1,8 @@ package com.mpush.common.router; -import com.mpush.api.RedisKey; +import com.mpush.cache.redis.RedisKey; import com.mpush.api.router.RouterManager; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.cache.redis.manager.RedisManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,18 +19,18 @@ public class RemoteRouterManager implements RouterManager { public RemoteRouter register(String userId, RemoteRouter router) { LOGGER.info("register remote router success userId={}, router={}", userId, router); String key = RedisKey.getUserKey(userId); - RemoteRouter old = RedisManage.get(key, RemoteRouter.class); + RemoteRouter old = RedisManager.I.get(key, RemoteRouter.class); if (old != null) { - RedisManage.del(key); + RedisManager.I.del(key); } - RedisManage.set(key, router); + RedisManager.I.set(key, router); return old; } @Override public boolean unRegister(String userId) { String key = RedisKey.getUserKey(userId); - RedisManage.del(key); + RedisManager.I.del(key); LOGGER.info("unRegister remote router success userId={}", userId); return true; } @@ -38,6 +38,6 @@ public boolean unRegister(String userId) { @Override public RemoteRouter lookup(String userId) { String key = RedisKey.getUserKey(userId); - return RedisManage.get(key, RemoteRouter.class); + return RedisManager.I.get(key, RemoteRouter.class); } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index dd1a16db..5a2a22dd 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -1,52 +1,50 @@ package com.mpush.common.router; -import com.mpush.tools.redis.listener.MessageListener; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.cache.redis.listener.MessageListener; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.MPushUtil; +import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.common.AbstractEventContainer; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.listener.ListenerDispatcher; -import com.mpush.tools.redis.manage.RedisManage; - /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ -public class UserChangeListener extends AbstractEventContainer implements MessageListener { - - private static final Logger log = LoggerFactory.getLogger(UserChangeListener.class); - +public class UserChangeListener extends EventConsumer implements MessageListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserChangeListener.class); + public static final String ONLINE_CHANNEL = "/mpush/online/"; - + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; //只需要一台机器注册online、offline 消息通道 public UserChangeListener() { - if(ConfigCenter.I.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ - ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); - ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this); - }else{ - log.error("UserChangeListener is not localhost,required:{},but:{}",ConfigCenter.I.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp()); - } + if ("127.0.0.1".equals(MPushUtil.getLocalIp())) { + ListenerDispatcher.I.subscribe(getOnlineChannel(), this); + ListenerDispatcher.I.subscribe(getOfflineChannel(), this); + } else { + LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", MPushUtil.getLocalIp()); + } } public String getOnlineChannel() { return ONLINE_CHANNEL; } - + public String getOfflineChannel() { return OFFLINE_CHANNEL; } - + public void userOnline(String userId) { - RedisManage.publish(getOnlineChannel(), userId); + RedisManager.I.publish(getOnlineChannel(), userId); } - - public void userOffline(String userId){ - RedisManage.publish(getOnlineChannel(), userId); + + public void userOffline(String userId) { + RedisManager.I.publish(getOnlineChannel(), userId); } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 2bf17464..44b2dc7b 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -55,7 +55,7 @@ public String toString(byte[] a) { public static byte[] toArray(String str) { String[] a = str.split("\\|"); - if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { + if (a.length != CipherBox.I.getAesKeyLength()) { return null; } byte[] bytes = new byte[a.length]; diff --git a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index d8585cdb..cd9dd03a 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -1,6 +1,6 @@ package com.mpush.common.security; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import com.mpush.tools.crypto.RSAUtils; import java.security.SecureRandom; @@ -13,15 +13,15 @@ * @author ohun@live.cn */ public final class CipherBox { - public int aesKeyLength = ConfigCenter.I.aesKeyLength(); - public static final CipherBox INSTANCE = new CipherBox(); + public int aesKeyLength = CC.mp.security.aes_key_length; + public static final CipherBox I = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; private RSAPublicKey publicKey; public RSAPrivateKey getPrivateKey() { if (privateKey == null) { - String key = ConfigCenter.I.privateKey(); + String key = CC.mp.security.private_key; try { privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(key); } catch (Exception e) { @@ -33,7 +33,7 @@ public RSAPrivateKey getPrivateKey() { public RSAPublicKey getPublicKey() { if (publicKey == null) { - String key = ConfigCenter.I.publicKey(); + String key = CC.mp.security.public_key; try { publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(key); } catch (Exception e) { @@ -70,8 +70,4 @@ public byte[] mixKey(byte[] clientKey, byte[] serverKey) { public int getAesKeyLength() { return aesKeyLength; } - - public RsaCipher getRsaCipher() { - return new RsaCipher(getPrivateKey(), getPublicKey()); - } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index 2afce590..f5552e6f 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -14,7 +14,6 @@ */ public final class RsaCipher implements Cipher { private final RSAPrivateKey privateKey; - private final RSAPublicKey publicKey; public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @@ -24,28 +23,31 @@ public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @Override public byte[] decrypt(byte[] data) { - try{ - Profiler.enter("start rsa decrypt"); - return RSAUtils.decryptByPrivateKey(data, privateKey); - }finally{ - Profiler.release(); - } - + try { + Profiler.enter("start rsa decrypt"); + return RSAUtils.decryptByPrivateKey(data, privateKey); + } finally { + Profiler.release(); + } + } @Override public byte[] encrypt(byte[] data) { - try{ - Profiler.enter("start rsa encrypt"); - return RSAUtils.encryptByPublicKey(data, publicKey); - }finally{ - Profiler.release(); - } + try { + Profiler.enter("start rsa encrypt"); + return RSAUtils.encryptByPublicKey(data, publicKey); + } finally { + Profiler.release(); + } + } + + @Override + public String toString() { + return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; } - @Override - public String toString() { - return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; - } - + public static RsaCipher create() { + return new RsaCipher(CipherBox.I.getPrivateKey(), CipherBox.I.getPublicKey()); + } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java new file mode 100644 index 00000000..d6767359 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java @@ -0,0 +1,18 @@ +package com.mpush.common.security; + +import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.core.CipherFactory; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class RsaCipherFactory implements CipherFactory { + private static final RsaCipher RSA_CIPHER = RsaCipher.create(); + + @Override + public Cipher get() { + return RSA_CIPHER; + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java similarity index 57% rename from mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java rename to mpush-common/src/main/java/com/mpush/common/user/UserManager.java index 1269273b..49a3b9c7 100644 --- a/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -1,17 +1,17 @@ -package com.mpush.common.manage.user; +package com.mpush.common.user; import java.util.List; import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.cache.redis.manager.RedisManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.api.RedisKey; +import com.mpush.cache.redis.RedisKey; //查询使用 public final class UserManager { - private static final Logger log = LoggerFactory.getLogger(UserManager.class); + private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); public static final UserManager INSTANCE = new UserManager(); private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); @@ -21,22 +21,22 @@ public UserManager() { } public void clearUserOnlineData() { - RedisManage.del(ONLINE_KEY); + RedisManager.I.del(ONLINE_KEY); } public void recordUserOnline(String userId) { - RedisManage.zAdd(ONLINE_KEY, userId); - log.info("user online {}", userId); + RedisManager.I.zAdd(ONLINE_KEY, userId); + LOGGER.info("user online {}", userId); } public void recordUserOffline(String userId) { - RedisManage.zRem(ONLINE_KEY, userId); - log.info("user offline {}", userId); + RedisManager.I.zRem(ONLINE_KEY, userId); + LOGGER.info("user offline {}", userId); } //在线用户 public long getOnlineUserNum() { - return RedisManage.zCard(ONLINE_KEY); + return RedisManager.I.zCard(ONLINE_KEY); } //在线用户列表 @@ -44,6 +44,6 @@ public List getOnlineUserList(int start, int size) { if (size < 10) { size = 10; } - return RedisManage.zrange(ONLINE_KEY, start, size - 1, String.class); + return RedisManager.I.zrange(ONLINE_KEY, start, size - 1, String.class); } } diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory new file mode 100644 index 00000000..02ce67e8 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory @@ -0,0 +1 @@ +com.mpush.common.security.RsaCipherFactory diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index de05f1c5..174efd1a 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -2,11 +2,11 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} mpush-core @@ -20,27 +20,7 @@ ${mpush.groupId} - mpush-client - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring + mpush-common diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 82df0f24..ff247235 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -1,12 +1,13 @@ package com.mpush.core.handler; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.zk.ZKServerNode; -import com.mpush.api.RedisKey; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.config.ConfigManager; import com.mpush.tools.Jsons; import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.node.ZKServerNode; import io.netty.channel.*; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -18,13 +19,11 @@ @ChannelHandler.Sharable public final class AdminHandler extends SimpleChannelInboundHandler { - private static final Logger log = LoggerFactory.getLogger(AdminHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AdminHandler.class); private static final String DOUBLE_END = "\r\n\r\n"; - private static final String ONE_END = "\r\n"; - - protected static final ZKClient zkClient = ZKClient.I; + private static final String EOL = "\r\n"; @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { @@ -38,7 +37,7 @@ protected void messageReceived(ChannelHandlerContext ctx, String request) throws @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + ONE_END); + ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + EOL); ctx.write("It is " + new Date() + " now." + DOUBLE_END); ctx.flush(); } @@ -53,11 +52,11 @@ public enum Command { @Override public String handler(String request) { StringBuilder buf = new StringBuilder(); - buf.append("Command:" + ONE_END); - buf.append("help:display all command." + ONE_END); - buf.append("quit:exit telnet." + ONE_END); - buf.append("scn:statistics conn num." + ONE_END); - buf.append("rcs:remove connection server zk info." + ONE_END); + buf.append("Command:" + EOL); + buf.append("help:display all command." + EOL); + buf.append("quit:exit checkHealth." + EOL); + buf.append("scn:statistics connect num." + EOL); + buf.append("rcs:remove current server zk info." + EOL); buf.append("scs:stop connection server."); return buf.toString(); } @@ -71,7 +70,7 @@ public String handler(String request) { SCN("scn") { @Override public String handler(String request) { - Long value = RedisManage.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); + Long value = RedisManager.I.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); if (value == null) { value = 0L; } @@ -82,17 +81,19 @@ public String handler(String request) { @Override public String handler(String request) { - List rawData = zkClient.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + List rawData = ZKClient.I.getChildrenKeys(ZKPath.CONNECT_SERVER.getRootPath()); boolean removeSuccess = false; + String localIp = ConfigManager.I.getLocalIp(); for (String raw : rawData) { - String data = zkClient.get(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + String dataPath = ZKPath.CONNECT_SERVER.getFullPath(raw); + String data = ZKClient.I.get(dataPath); ZKServerNode serverNode = Jsons.fromJson(data, ZKServerNode.class); - if (serverNode.getIp().equals(MPushUtil.getInetAddress())) { - zkClient.remove(ZKPath.CONNECTION_SERVER.getFullPath(raw)); - log.info("delete connection server success:{}", data); + if (serverNode.getIp().equals(localIp)) { + ZKClient.I.remove(dataPath); + LOGGER.info("delete connection server success:{}", data); removeSuccess = true; } else { - log.info("delete connection server failed: required ip:{}, but:{}", serverNode.getIp(), MPushUtil.getInetAddress()); + LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), MPushUtil.getInetAddress()); } } if (removeSuccess) { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 30c25115..1bf5de8c 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -5,13 +5,13 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOnlineEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.OkMessage; import com.mpush.core.router.RouterCenter; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; +import com.mpush.tools.event.EventBus; /** * Created by ohun on 2015/12/23. @@ -39,7 +39,7 @@ public void handle(BindUserMessage message) { boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); if (success) { - EventBus.INSTANCE.post(new UserOnlineEvent(message.getConnection(), message.userId)); + EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").send(); Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index de0c9e40..6d3be522 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -6,10 +6,10 @@ import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.FastConnectMessage; import com.mpush.common.message.FastConnectOkMessage; +import com.mpush.tools.config.ConfigManager; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.log.Logs; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,7 +41,7 @@ public void handle(FastConnectMessage message) { Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); } else { //3.校验成功,重新计算心跳,完成快速重连 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); session.context.setHeartbeat(heartbeat); message.getConnection().setSessionContext(session.context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 69acb209..35f072a7 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -10,7 +10,7 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.core.router.LocalRouter; import com.mpush.core.router.RouterCenter; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; import com.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 30f3400b..e30e4733 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -5,17 +5,17 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.HandshakeEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.HandshakeMessage; import com.mpush.common.message.HandshakeOkMessage; import com.mpush.common.security.AesCipher; import com.mpush.common.security.CipherBox; +import com.mpush.tools.config.ConfigManager; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.log.Logs; +import com.mpush.tools.event.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,21 +29,21 @@ public final class HandshakeHandler extends BaseMessageHandler @Override public HandshakeMessage decode(Packet packet, Connection connection) { - return new HandshakeMessage(packet, connection); + return new HandshakeMessage(packet, connection); } @Override public void handle(HandshakeMessage message) { - + byte[] iv = message.iv;//AES密钥向量16位 byte[] clientKey = message.clientKey;//客户端随机数16位 - byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数16位 - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥16位 + byte[] serverKey = CipherBox.I.randomAESKey();//服务端随机数16位 + byte[] sessionKey = CipherBox.I.mixKey(clientKey, serverKey);//会话密钥16位 //1.校验客户端消息字段 if (Strings.isNullOrEmpty(message.deviceId) - || iv.length != CipherBox.INSTANCE.getAesKeyLength() - || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { + || iv.length != CipherBox.I.getAesKeyLength() + || clientKey.length != CipherBox.I.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); Logs.Conn.info("client handshake false:{}", message.getConnection()); return; @@ -63,7 +63,7 @@ public void handle(HandshakeMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); //5.计算心跳时间 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); //6.响应握手成功消息 HandshakeOkMessage @@ -88,7 +88,7 @@ public void handle(HandshakeMessage message) { ReusableSessionManager.INSTANCE.cacheSession(session); //10.触发握手成功事件 - EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); + EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); Logs.Conn.info("client handshake success:{}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 5feea063..493e3d31 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -3,7 +3,7 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/22. diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 4cc42f1d..916c0ce0 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -6,13 +6,13 @@ import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.log.Logs; -import com.mpush.netty.client.HttpCallback; -import com.mpush.netty.client.HttpClient; -import com.mpush.netty.client.RequestInfo; +import com.mpush.tools.config.data.DnsMapping; +import com.mpush.common.net.DnsMappingManager; +import com.mpush.tools.log.Logs; +import com.mpush.netty.http.HttpCallback; +import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.RequestInfo; import com.mpush.tools.Profiler; -import com.mpush.tools.dns.DnsMapping; -import com.mpush.tools.dns.manage.DnsMappingManage; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; import org.slf4j.Logger; @@ -183,7 +183,7 @@ private String doDnsMapping(String url) { } if (uri == null) return url; String host = uri.getHost(); - DnsMapping mapping = DnsMappingManage.holder.translate(host); + DnsMapping mapping = DnsMappingManager.I.translate(host); if (mapping == null) return url; return url.replaceFirst(host, mapping.toString()); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index 8b339af8..b55a37e2 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -7,7 +7,6 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; @@ -15,7 +14,8 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.core.router.LocalRouter; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; +import com.mpush.tools.event.EventBus; /** @@ -70,7 +70,7 @@ public void handle(BindUserMessage message) { //4.路由删除成功,广播用户下线事件 if (unRegisterSuccess) { - EventBus.INSTANCE.post(new UserOfflineEvent(message.getConnection(), message.userId)); + EventBus.I.post(new UserOfflineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("unbind success").send(); Logs.Conn.info("unbind user success, userId={}, session={}", message.userId, context); } else { diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 391bb377..8b4c02f4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -5,10 +5,9 @@ import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.router.RouterManager; -import com.mpush.common.AbstractEventContainer; -import com.mpush.common.EventBus; -import com.mpush.common.router.RemoteRouter; +import com.mpush.tools.event.EventConsumer; +import com.mpush.tools.event.EventBus; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import org.slf4j.Logger; @@ -21,7 +20,7 @@ * * @author ohun@live.cn */ -public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { +public final class LocalRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); /** @@ -78,7 +77,7 @@ void onConnectionCloseEvent(ConnectionCloseEvent event) { //1.清除反向关系 String userId = connIdUserIds.remove(id); if (userId == null) return; - EventBus.INSTANCE.post(new UserOfflineEvent(event.connection, userId)); + EventBus.I.post(new UserOfflineEvent(event.connection, userId)); LocalRouter router = routers.get(userId); if (router == null) return; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index b072bb28..e5ac38d0 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -4,11 +4,11 @@ import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; -import com.mpush.common.EventBus; import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.tools.MPushUtil; +import com.mpush.tools.event.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,12 +53,12 @@ public boolean register(String userId, Connection connection) { } if (oldLocalRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); + EventBus.I.post(new RouterChangeEvent(userId, oldLocalRouter)); LOGGER.info("register router success, find old local router={}, userId={}", oldLocalRouter, userId); } if (oldRemoteRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldRemoteRouter)); + EventBus.I.post(new RouterChangeEvent(userId, oldRemoteRouter)); LOGGER.info("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); } return true; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index e218cbee..1512a24e 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -1,22 +1,22 @@ package com.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.mpush.api.RedisKey; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; import com.mpush.api.router.Router; -import com.mpush.common.AbstractEventContainer; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.cache.redis.listener.MessageListener; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.event.EventConsumer; import com.mpush.common.message.KickUserMessage; import com.mpush.common.router.RemoteRouter; -import com.mpush.log.Logs; - +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.log.Logs; import com.mpush.tools.Jsons; import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.listener.ListenerDispatcher; -import com.mpush.tools.redis.listener.MessageListener; -import com.mpush.tools.redis.manage.RedisManage; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -25,12 +25,12 @@ * * @author ohun@live.cn */ -public final class RouterChangeListener extends AbstractEventContainer implements MessageListener { +public final class RouterChangeListener extends EventConsumer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); public RouterChangeListener() { - ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); + ListenerDispatcher.I.subscribe(getKickChannel(), this); } public String getKickChannel() { @@ -99,7 +99,7 @@ public void kickRemote(String userId, RemoteRouter router) { msg.deviceId = location.getDeviceId(); msg.targetServer = location.getHost(); msg.userId = userId; - RedisManage.publish(getKickChannel(msg.targetServer), msg); + RedisManager.I.publish(getKickChannel(msg.targetServer), msg); } /** @@ -148,6 +148,6 @@ public void onMessage(String channel, String message) { } private void remStatUser(String userId) { - RedisManage.zRem(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp()), userId); + RedisManager.I.zRem(RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()), userId); } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 1b514a5d..10ba41f8 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -3,9 +3,9 @@ import com.google.common.eventbus.Subscribe; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.event.UserOnlineEvent; -import com.mpush.common.EventBus; -import com.mpush.common.manage.user.UserManager; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.common.user.UserManager; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.event.EventBus; /** * Created by ohun on 2015/12/23. @@ -19,18 +19,18 @@ public final class UserOnlineOfflineListener { public static final String OFFLINE_CHANNEL = "/mpush/offline/"; public UserOnlineOfflineListener() { - EventBus.INSTANCE.register(this); + EventBus.I.register(this); } @Subscribe void onUserOnline(UserOnlineEvent event) { UserManager.INSTANCE.recordUserOnline(event.getUserId()); - RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); + RedisManager.I.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe void onUserOffline(UserOfflineEvent event) { UserManager.INSTANCE.recordUserOffline(event.getUserId()); - RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); + RedisManager.I.publish(OFFLINE_CHANNEL, event.getUserId()); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index dd5a5398..5ac02596 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -1,15 +1,14 @@ package com.mpush.core.server; -import com.mpush.core.handler.*; -import com.mpush.netty.client.HttpClient; -import com.mpush.netty.client.NettyHttpClient; -import com.mpush.netty.connection.NettyConnectionManager; -import com.mpush.netty.server.NettyServer; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.protocol.Command; import com.mpush.common.MessageDispatcher; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.core.handler.*; +import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.NettyHttpClient; +import com.mpush.netty.server.NettyServer; +import com.mpush.tools.config.CC; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -20,7 +19,7 @@ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; - private ConnectionManager connectionManager = new NettyConnectionManager(); + private ConnectionManager connectionManager = new ServerConnectionManager(); private HttpClient httpClient; public ConnectionServer(int port) { @@ -38,7 +37,7 @@ public void init() { receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - if (ConfigCenter.I.httpProxyEnable()) { + if (CC.mp.http.proxy_enable) { httpClient = new NettyHttpClient(); receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 6ec0ce7f..4c1cbadb 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -3,7 +3,6 @@ import com.mpush.api.protocol.Command; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; -import com.mpush.netty.connection.NettyConnectionManager; import com.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; @@ -15,7 +14,7 @@ public final class GatewayServer extends NettyServer { private ServerChannelHandler channelHandler; - private NettyConnectionManager connectionManager; + private ServerConnectionManager connectionManager; public GatewayServer(int port) { super(port); @@ -26,7 +25,7 @@ public void init() { super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); - connectionManager = new NettyConnectionManager(); + connectionManager = new ServerConnectionManager(); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 73a8d6eb..eb481393 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -7,11 +7,11 @@ import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; import com.mpush.api.PacketReceiver; -import com.mpush.common.EventBus; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; import com.mpush.tools.Profiler; +import com.mpush.tools.event.EventBus; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -80,7 +80,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception { Logs.Conn.info("client disconnect channel={}", ctx.channel()); Connection connection = connectionManager.get(ctx.channel()); - EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); + EventBus.I.post(new ConnectionCloseEvent(connection)); connectionManager.remove(ctx.channel()); } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java similarity index 86% rename from mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java rename to mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 43d14a39..4cceff92 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -1,4 +1,4 @@ -package com.mpush.netty.connection; +package com.mpush.core.server; import com.google.common.collect.Lists; @@ -6,16 +6,16 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.HandshakeEvent; -import com.mpush.common.EventBus; -import com.mpush.log.Logs; - -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; @@ -26,7 +26,7 @@ * * @author ohun@live.cn */ -public final class NettyConnectionManager implements ConnectionManager { +public final class ServerConnectionManager implements ConnectionManager { //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); @@ -36,9 +36,9 @@ public final class NettyConnectionManager implements ConnectionManager { public void init() { //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s - int ticksPerWheel = (int) (ConfigCenter.I.maxHeartbeat() / tickDuration); + int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); - EventBus.INSTANCE.register(this); + EventBus.I.register(this); } @Override @@ -90,7 +90,7 @@ public HeartbeatCheckTask(Connection connection) { public void startTimeout() { int timeout = connection.getSessionContext().heartbeat; - timer.newTimeout(this, timeout > 0 ? timeout : ConfigCenter.I.minHeartbeat(), TimeUnit.MILLISECONDS); + timer.newTimeout(this, timeout > 0 ? timeout : CC.mp.core.min_heartbeat, TimeUnit.MILLISECONDS); } @Override @@ -100,7 +100,7 @@ public void run(Timeout timeout) throws Exception { return; } if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.I.maxHBTimeoutTimes()) { + if (++expiredTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); Logs.HB.info("connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); return; diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index 1fbf2c78..a665e316 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -1,11 +1,11 @@ package com.mpush.core.session; -import com.mpush.api.RedisKey; import com.mpush.api.connection.SessionContext; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; import com.mpush.tools.Strings; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import com.mpush.tools.crypto.MD5Utils; -import com.mpush.tools.redis.manage.RedisManage; /** * Created by ohun on 2015/12/25. @@ -14,17 +14,17 @@ */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private int expiredTime = ConfigCenter.I.sessionExpiredTime(); + private int expiredTime = CC.mp.core.session_expired_time; public boolean cacheSession(ReusableSession session) { - String key = RedisKey.getSessionKey(session.sessionId); - RedisManage.set(key, ReusableSession.encode(session.context), expiredTime); + String key = RedisKey.getSessionKey(session.sessionId); + RedisManager.I.set(key, ReusableSession.encode(session.context), expiredTime); return true; } public ReusableSession querySession(String sessionId) { - String key = RedisKey.getSessionKey(sessionId); - String value = RedisManage.get(key, String.class); + String key = RedisKey.getSessionKey(sessionId); + String value = RedisManager.I.get(key, String.class); if (Strings.isBlank(value)) return null; return ReusableSession.decode(value); } diff --git a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java index f51ae52f..27bd38f3 100644 --- a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java @@ -16,7 +16,7 @@ public void testStart() throws Exception { server.init(); server.start(new Server.Listener() { @Override - public void onSuccess(int port) { + public void onSuccess(Object... port) { } @Override diff --git a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java index 7810ae22..b48b60a7 100644 --- a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java @@ -17,9 +17,9 @@ public void testGetPrivateKey() throws Exception { @Test public void testGetPublicKey() throws Exception { for (int i = 0; i < 1000; i++) { - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] serverKey = CipherBox.INSTANCE.randomAESKey(); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey); + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] serverKey = CipherBox.I.randomAESKey(); + byte[] sessionKey = CipherBox.I.mixKey(clientKey, serverKey); //System.out.println("clientKey:" + Arrays.toString(clientKey)); //System.out.println("serverKey:" + Arrays.toString(serverKey)); System.out.println("sessionKey:" + Arrays.toString(sessionKey)); diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml deleted file mode 100644 index f8f5595e..00000000 --- a/mpush-log/pom.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - mpush - com.mpush - 1.0 - - 4.0.0 - ${mpush.groupId} - mpush-log - ${mpush-log-version} - jar - mpush-log - - - UTF-8 - - - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring - - - diff --git a/mpush-log/src/main/java/com/mpush/log/Logs.java b/mpush-log/src/main/java/com/mpush/log/Logs.java deleted file mode 100644 index d9504ab2..00000000 --- a/mpush-log/src/main/java/com/mpush/log/Logs.java +++ /dev/null @@ -1,331 +0,0 @@ -package com.mpush.log; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.Marker; - -/** - * Created by ohun on 2016/5/16. - * - * @author ohun@live.cn - */ -public enum Logs implements Logger { - Conn("connectionLog"), - PUSH("pushLog"), - HB("heartBeatLog"), - REDIS("redisLog"), - ZK("zkLog"), - HTTP("httpLog"); - - private final Logger logger; - - Logs(String loggerName) { - this.logger = LoggerFactory.getLogger(loggerName); - } - - @Override - public String getName() { - return logger.getName(); - } - - @Override - public boolean isTraceEnabled() { - return logger.isTraceEnabled(); - } - - @Override - public void trace(String s) { - logger.trace(s); - } - - @Override - public void trace(String s, Object o) { - logger.trace(s, o); - } - - @Override - public void trace(String s, Object o, Object o1) { - logger.trace(s, o, o1); - } - - @Override - public void trace(String s, Object... objects) { - logger.trace(s, objects); - } - - @Override - public void trace(String s, Throwable throwable) { - logger.trace(s, throwable); - } - - @Override - public boolean isTraceEnabled(Marker marker) { - return isTraceEnabled(marker); - } - - @Override - public void trace(Marker marker, String s) { - logger.trace(marker, s); - } - - @Override - public void trace(Marker marker, String s, Object o) { - logger.trace(marker, s, o); - } - - @Override - public void trace(Marker marker, String s, Object o, Object o1) { - logger.trace(marker, s, o, o1); - } - - @Override - public void trace(Marker marker, String s, Object... objects) { - logger.trace(marker, s, objects); - } - - @Override - public void trace(Marker marker, String s, Throwable throwable) { - logger.trace(marker, s, throwable); - } - - @Override - public boolean isDebugEnabled() { - return isErrorEnabled(); - } - - @Override - public void debug(String s) { - logger.debug(s); - } - - @Override - public void debug(String s, Object o) { - logger.debug(s, o); - } - - @Override - public void debug(String s, Object o, Object o1) { - logger.debug(s, o, o1); - } - - @Override - public void debug(String s, Object... objects) { - logger.debug(s, objects); - } - - @Override - public void debug(String s, Throwable throwable) { - logger.debug(s, throwable); - } - - @Override - public boolean isDebugEnabled(Marker marker) { - return logger.isDebugEnabled(marker); - } - - @Override - public void debug(Marker marker, String s) { - logger.debug(marker, s); - } - - @Override - public void debug(Marker marker, String s, Object o) { - logger.debug(marker, s, o); - } - - @Override - public void debug(Marker marker, String s, Object o, Object o1) { - logger.debug(marker, s, o, o1); - } - - @Override - public void debug(Marker marker, String s, Object... objects) { - logger.debug(marker, s, objects); - } - - @Override - public void debug(Marker marker, String s, Throwable throwable) { - logger.debug(marker, s, throwable); - } - - @Override - public boolean isInfoEnabled() { - return logger.isInfoEnabled(); - } - - @Override - public void info(String s) { - logger.info(s); - } - - @Override - public void info(String s, Object o) { - logger.info(s, o); - } - - @Override - public void info(String s, Object o, Object o1) { - logger.info(s, o, o1); - } - - @Override - public void info(String s, Object... objects) { - logger.info(s, objects); - } - - @Override - public void info(String s, Throwable throwable) { - logger.info(s, throwable); - } - - @Override - public boolean isInfoEnabled(Marker marker) { - return logger.isInfoEnabled(marker); - } - - @Override - public void info(Marker marker, String s) { - logger.info(marker, s); - } - - @Override - public void info(Marker marker, String s, Object o) { - logger.info(marker, s, o); - } - - @Override - public void info(Marker marker, String s, Object o, Object o1) { - logger.info(marker, s, o, o1); - } - - @Override - public void info(Marker marker, String s, Object... objects) { - logger.info(marker, s, objects); - } - - @Override - public void info(Marker marker, String s, Throwable throwable) { - logger.info(marker, s, throwable); - } - - @Override - public boolean isWarnEnabled() { - return logger.isWarnEnabled(); - } - - @Override - public void warn(String s) { - logger.warn(s); - } - - @Override - public void warn(String s, Object o) { - logger.warn(s, o); - } - - @Override - public void warn(String s, Object... objects) { - logger.warn(s, objects); - } - - @Override - public void warn(String s, Object o, Object o1) { - logger.warn(s, o, o1); - } - - @Override - public void warn(String s, Throwable throwable) { - logger.warn(s, throwable); - } - - @Override - public boolean isWarnEnabled(Marker marker) { - return isWarnEnabled(marker); - } - - @Override - public void warn(Marker marker, String s) { - logger.warn(marker, s); - } - - @Override - public void warn(Marker marker, String s, Object o) { - logger.warn(marker, s, o); - } - - @Override - public void warn(Marker marker, String s, Object o, Object o1) { - logger.warn(marker, s, o, o1); - } - - @Override - public void warn(Marker marker, String s, Object... objects) { - logger.warn(marker, s, objects); - } - - @Override - public void warn(Marker marker, String s, Throwable throwable) { - logger.warn(marker, s, throwable); - } - - @Override - public boolean isErrorEnabled() { - return isErrorEnabled(); - } - - @Override - public void error(String s) { - logger.error(s); - } - - @Override - public void error(String s, Object o) { - logger.error(s, o); - } - - @Override - public void error(String s, Object o, Object o1) { - logger.error(s, o, o1); - } - - @Override - public void error(String s, Object... objects) { - logger.error(s, objects); - } - - @Override - public void error(String s, Throwable throwable) { - logger.error(s, throwable); - } - - @Override - public boolean isErrorEnabled(Marker marker) { - return isErrorEnabled(marker); - } - - @Override - public void error(Marker marker, String s) { - logger.error(marker, s); - } - - @Override - public void error(Marker marker, String s, Object o) { - logger.error(marker, s, o); - } - - @Override - public void error(Marker marker, String s, Object o, Object o1) { - logger.error(marker, s, o, o1); - } - - @Override - public void error(Marker marker, String s, Object... objects) { - logger.error(marker, s, objects); - } - - @Override - public void error(Marker marker, String s, Throwable throwable) { - logger.error(marker, s, throwable); - } - -} diff --git a/mpush-log/src/test/java/com/mpush/AppTest.java b/mpush-log/src/test/java/com/mpush/AppTest.java deleted file mode 100644 index 796b8466..00000000 --- a/mpush-log/src/test/java/com/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 6da74f1c..886b22fc 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -1,10 +1,10 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} @@ -14,9 +14,13 @@ mpush-monitor - - ${mpush.groupId} - mpush-tools - + + ${mpush.groupId} + mpush-api + + + ${mpush.groupId} + mpush-tools + diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java similarity index 93% rename from mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java rename to mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java index 9ae26d87..5a9d193f 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java @@ -1,8 +1,8 @@ -package com.mpush.monitor.domain; +package com.mpush.monitor.data; import java.util.Map; -public class MonitorData { +public class MonitorResult { private Long timestamp; @@ -16,7 +16,7 @@ public class MonitorData { private Map infoMap; - public MonitorData() { + public MonitorResult() { this.timestamp = System.currentTimeMillis(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java new file mode 100644 index 00000000..8448d67f --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java @@ -0,0 +1,22 @@ +package com.mpush.monitor.data; + +import com.mpush.monitor.quota.impl.*; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class ResultCollector { + + public MonitorResult collect() { + MonitorResult data = new MonitorResult(); + data.setInfoMap(JVMInfo.I.toMap()); + data.setGcMap(JVMGC.I.toMap()); + data.setMemoryMap(JVMMemory.I.toMap()); + data.setThreadMap(JVMThread.I.toMap()); + data.setThreadPoolMap(JVMThreadPool.I.toMap()); + return data; + } + +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java index 2c7ad2e7..6555be2c 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java @@ -1,35 +1,8 @@ package com.mpush.monitor.quota; -import java.util.List; import java.util.Map; -import com.google.common.collect.Lists; - public abstract class BaseQuota { - protected final static List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", - "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", - "Garbage collection optimized for deterministic pausetimes Old Collector"); - - protected final static List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", - "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); - - protected final static List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); - - protected final static List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); - - protected final static List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); - - protected final static List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); - - public static boolean contain(String name, List list) { - for (String str : list) { - if (name.equals(str)) { - return true; - } - } - return false; - } - - public abstract Map toMap(); + public abstract Map toMap(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java index 05c1f177..1258b497 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java @@ -1,136 +1,143 @@ package com.mpush.monitor.quota.impl; -import java.lang.management.GarbageCollectorMXBean; -import java.lang.management.ManagementFactory; -import java.util.Map; - +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.GCMQuota; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.List; +import java.util.Map; + public class JVMGC extends BaseQuota implements GCMQuota { - public static final JVMGC instance = new JVMGC(); - - private GarbageCollectorMXBean fullGc; - private GarbageCollectorMXBean yongGc; - - private long lastYoungGcCollectionCount = -1; - private long lastYoungGcCollectionTime = -1; - private long lastFullGcCollectionCount = -1; - private long lastFullGcCollectionTime = -1; - - - private JVMGC() { - for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { - String name = item.getName(); - if (contain(name, youngGcName)) { - yongGc = item; - } else if (contain(name,fullGcName)) { - fullGc = item; - } - } - - } - - @Override - public long yongGcCollectionCount() { - if (yongGc == null) { - return 0; - } - return yongGc.getCollectionCount(); - } - - @Override - public long yongGcCollectionTime() { - if (yongGc == null) { - return 0; - } - return yongGc.getCollectionTime(); - } - - @Override - public long fullGcCollectionCount() { - if (fullGc == null) { - return 0; - } - return fullGc.getCollectionCount(); - } - - @Override - public long fullGcCollectionTime() { - if (fullGc == null) { - return 0; - } - return fullGc.getCollectionTime(); - } - - @Override - public long spanYongGcCollectionCount() { - - long current = yongGcCollectionCount(); - if (lastYoungGcCollectionCount == -1) { - lastYoungGcCollectionCount = current; - return 0; - } else { - long result = current - lastYoungGcCollectionCount; - lastYoungGcCollectionCount = current; - return result; - } - - } - - @Override - public long spanYongGcCollectionTime() { - long current = yongGcCollectionTime(); - if (lastYoungGcCollectionTime == -1) { - lastYoungGcCollectionTime = current; - return 0; - } else { - long result = current - lastYoungGcCollectionTime; - lastYoungGcCollectionTime = current; - return result; - } - } - - @Override - public long spanFullGcCollectionCount() { - long current = fullGcCollectionCount(); - if (lastFullGcCollectionCount == -1) { - lastFullGcCollectionCount = current; - return 0; - } else { - long result = current - lastFullGcCollectionCount; - lastFullGcCollectionCount = current; - return result; - } - } - - @Override - public long spanFullGcCollectionTime() { - long current = fullGcCollectionTime(); - if (lastFullGcCollectionTime == -1) { - lastFullGcCollectionTime = current; - return 0; - } else { - long result = current - lastFullGcCollectionTime; - lastFullGcCollectionTime = current; - return result; - } - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("yongGcCollectionCount", yongGcCollectionCount()); - map.put("yongGcCollectionTime", yongGcCollectionTime()); - map.put("fullGcCollectionCount", fullGcCollectionCount()); - map.put("fullGcCollectionTime", fullGcCollectionTime()); - map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); - map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); - map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); - map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); - return map; - } - + private final List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", + "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", + "Garbage collection optimized for deterministic pausetimes Old Collector"); + + private final List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", + "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); + + public static final JVMGC I = new JVMGC(); + + private GarbageCollectorMXBean fullGc; + private GarbageCollectorMXBean yongGc; + + private long lastYoungGcCollectionCount = -1; + private long lastYoungGcCollectionTime = -1; + private long lastFullGcCollectionCount = -1; + private long lastFullGcCollectionTime = -1; + + + private JVMGC() { + for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { + String name = item.getName(); + if (youngGcName.contains(name)) { + yongGc = item; + } else if (fullGcName.contains(name)) { + fullGc = item; + } + } + } + + @Override + public long yongGcCollectionCount() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionCount(); + } + + @Override + public long yongGcCollectionTime() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionTime(); + } + + @Override + public long fullGcCollectionCount() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionCount(); + } + + @Override + public long fullGcCollectionTime() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionTime(); + } + + @Override + public long spanYongGcCollectionCount() { + + long current = yongGcCollectionCount(); + if (lastYoungGcCollectionCount == -1) { + lastYoungGcCollectionCount = current; + return 0; + } else { + long result = current - lastYoungGcCollectionCount; + lastYoungGcCollectionCount = current; + return result; + } + } + + @Override + public long spanYongGcCollectionTime() { + long current = yongGcCollectionTime(); + if (lastYoungGcCollectionTime == -1) { + lastYoungGcCollectionTime = current; + return 0; + } else { + long result = current - lastYoungGcCollectionTime; + lastYoungGcCollectionTime = current; + return result; + } + } + + @Override + public long spanFullGcCollectionCount() { + long current = fullGcCollectionCount(); + if (lastFullGcCollectionCount == -1) { + lastFullGcCollectionCount = current; + return 0; + } else { + long result = current - lastFullGcCollectionCount; + lastFullGcCollectionCount = current; + return result; + } + } + + @Override + public long spanFullGcCollectionTime() { + long current = fullGcCollectionTime(); + if (lastFullGcCollectionTime == -1) { + lastFullGcCollectionTime = current; + return 0; + } else { + long result = current - lastFullGcCollectionTime; + lastFullGcCollectionTime = current; + return result; + } + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("yongGcCollectionCount", yongGcCollectionCount()); + map.put("yongGcCollectionTime", yongGcCollectionTime()); + map.put("fullGcCollectionCount", fullGcCollectionCount()); + map.put("fullGcCollectionTime", fullGcCollectionTime()); + map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); + map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); + map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); + map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); + return map; + } + } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index 42e2bd04..457e6f41 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -11,7 +11,7 @@ public class JVMInfo extends BaseQuota implements InfoQuota { - public static final JVMInfo instance = new JVMInfo(); + public static final JVMInfo I = new JVMInfo(); private RuntimeMXBean runtimeMXBean; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java index 2ccb5f19..3df43788 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java @@ -1,239 +1,248 @@ package com.mpush.monitor.quota.impl; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.MemoryQuota; + import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryPoolMXBean; import java.util.List; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.MemoryQuota; - public class JVMMemory extends BaseQuota implements MemoryQuota { - public static final JVMMemory instance = new JVMMemory(); - - private MemoryMXBean memoryMXBean; - - private MemoryPoolMXBean permGenMxBean; - private MemoryPoolMXBean oldGenMxBean; - private MemoryPoolMXBean edenSpaceMxBean; - private MemoryPoolMXBean pSSurvivorSpaceMxBean; - - private JVMMemory() { - memoryMXBean = ManagementFactory.getMemoryMXBean(); - List list = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean item : list) { - String name = item.getName(); - if (contain(name, permGenName)) { - permGenMxBean = item; - } else if (contain(name, oldGenName)) { - oldGenMxBean = item; - } else if (contain(name, edenSpaceName)) { - edenSpaceMxBean = item; - } else if (contain(name, psSurvivorName)) { - pSSurvivorSpaceMxBean = item; - } - } - } - - @Override - public long heapMemoryCommitted() { - return memoryMXBean.getHeapMemoryUsage().getCommitted(); - } - - @Override - public long heapMemoryInit() { - return memoryMXBean.getHeapMemoryUsage().getInit(); - } - - @Override - public long heapMemoryMax() { - return memoryMXBean.getHeapMemoryUsage().getMax(); - } - - @Override - public long heapMemoryUsed() { - return memoryMXBean.getHeapMemoryUsage().getUsed(); - } - - @Override - public long nonHeapMemoryCommitted() { - return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); - } - - @Override - public long nonHeapMemoryInit() { - return memoryMXBean.getNonHeapMemoryUsage().getInit(); - } - - @Override - public long nonHeapMemoryMax() { - return memoryMXBean.getNonHeapMemoryUsage().getMax(); - } - - @Override - public long nonHeapMemoryUsed() { - return memoryMXBean.getNonHeapMemoryUsage().getUsed(); - } - - @Override - public long permGenCommitted() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getCommitted(); - } - - @Override - public long permGenInit() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getInit(); - } - - @Override - public long permGenMax() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getMax(); - } - - @Override - public long permGenUsed() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getUsed(); - } - - @Override - public long oldGenCommitted() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getCommitted(); - } - - @Override - public long oldGenInit() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getInit(); - } - - @Override - public long oldGenMax() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getMax(); - } - - @Override - public long oldGenUsed() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getUsed(); - } - - @Override - public long edenSpaceCommitted() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getCommitted(); - } - - @Override - public long edenSpaceInit() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getInit(); - } - - @Override - public long edenSpaceMax() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getMax(); - } - - @Override - public long edenSpaceUsed() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getUsed(); - } - - @Override - public long survivorCommitted() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getCommitted(); - } - - @Override - public long survivorInit() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getInit(); - } - - @Override - public long survivorMax() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getMax(); - } - - @Override - public long survivorUsed() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getUsed(); - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("heapMemoryCommitted", heapMemoryCommitted()); - map.put("heapMemoryInit", heapMemoryInit()); - map.put("heapMemoryMax", heapMemoryMax()); - map.put("heapMemoryUsed", heapMemoryUsed()); - map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); - map.put("nonHeapMemoryInit", nonHeapMemoryInit()); - map.put("nonHeapMemoryMax", nonHeapMemoryMax()); - map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); - map.put("permGenCommitted", permGenCommitted()); - map.put("permGenInit", permGenInit()); - map.put("permGenMax", permGenMax()); - map.put("permGenUsed", permGenUsed()); - map.put("oldGenCommitted", oldGenCommitted()); - map.put("oldGenInit", oldGenInit()); - map.put("oldGenMax", oldGenMax()); - map.put("oldGenUsed", oldGenUsed()); - map.put("edenSpaceCommitted", edenSpaceCommitted()); - map.put("edenSpaceInit", edenSpaceInit()); - map.put("edenSpaceMax", edenSpaceMax()); - map.put("edenSpaceUsed", edenSpaceUsed()); - map.put("survivorCommitted", survivorCommitted()); - map.put("survivorInit", survivorInit()); - map.put("survivorMax", survivorMax()); - map.put("survivorUsed", survivorUsed()); - return map; - } + private final List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); + + private final List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); + + private final List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); + + private final List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); + + public static final JVMMemory I = new JVMMemory(); + + private MemoryMXBean memoryMXBean; + + private MemoryPoolMXBean permGenMxBean; + private MemoryPoolMXBean oldGenMxBean; + private MemoryPoolMXBean edenSpaceMxBean; + private MemoryPoolMXBean pSSurvivorSpaceMxBean; + + private JVMMemory() { + memoryMXBean = ManagementFactory.getMemoryMXBean(); + List list = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean item : list) { + String name = item.getName(); + if (permGenName.contains(name)) { + permGenMxBean = item; + } else if (oldGenName.contains(name)) { + oldGenMxBean = item; + } else if (edenSpaceName.contains(name)) { + edenSpaceMxBean = item; + } else if (psSurvivorName.contains(name)) { + pSSurvivorSpaceMxBean = item; + } + } + } + + @Override + public long heapMemoryCommitted() { + return memoryMXBean.getHeapMemoryUsage().getCommitted(); + } + + @Override + public long heapMemoryInit() { + return memoryMXBean.getHeapMemoryUsage().getInit(); + } + + @Override + public long heapMemoryMax() { + return memoryMXBean.getHeapMemoryUsage().getMax(); + } + + @Override + public long heapMemoryUsed() { + return memoryMXBean.getHeapMemoryUsage().getUsed(); + } + + @Override + public long nonHeapMemoryCommitted() { + return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); + } + + @Override + public long nonHeapMemoryInit() { + return memoryMXBean.getNonHeapMemoryUsage().getInit(); + } + + @Override + public long nonHeapMemoryMax() { + return memoryMXBean.getNonHeapMemoryUsage().getMax(); + } + + @Override + public long nonHeapMemoryUsed() { + return memoryMXBean.getNonHeapMemoryUsage().getUsed(); + } + + @Override + public long permGenCommitted() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getCommitted(); + } + + @Override + public long permGenInit() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getInit(); + } + + @Override + public long permGenMax() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getMax(); + } + + @Override + public long permGenUsed() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getUsed(); + } + + @Override + public long oldGenCommitted() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getCommitted(); + } + + @Override + public long oldGenInit() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getInit(); + } + + @Override + public long oldGenMax() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getMax(); + } + + @Override + public long oldGenUsed() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getUsed(); + } + + @Override + public long edenSpaceCommitted() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long edenSpaceInit() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getInit(); + } + + @Override + public long edenSpaceMax() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getMax(); + } + + @Override + public long edenSpaceUsed() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getUsed(); + } + + @Override + public long survivorCommitted() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long survivorInit() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getInit(); + } + + @Override + public long survivorMax() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getMax(); + } + + @Override + public long survivorUsed() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getUsed(); + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("heapMemoryCommitted", heapMemoryCommitted()); + map.put("heapMemoryInit", heapMemoryInit()); + map.put("heapMemoryMax", heapMemoryMax()); + map.put("heapMemoryUsed", heapMemoryUsed()); + map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); + map.put("nonHeapMemoryInit", nonHeapMemoryInit()); + map.put("nonHeapMemoryMax", nonHeapMemoryMax()); + map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); + map.put("permGenCommitted", permGenCommitted()); + map.put("permGenInit", permGenInit()); + map.put("permGenMax", permGenMax()); + map.put("permGenUsed", permGenUsed()); + map.put("oldGenCommitted", oldGenCommitted()); + map.put("oldGenInit", oldGenInit()); + map.put("oldGenMax", oldGenMax()); + map.put("oldGenUsed", oldGenUsed()); + map.put("edenSpaceCommitted", edenSpaceCommitted()); + map.put("edenSpaceInit", edenSpaceInit()); + map.put("edenSpaceMax", edenSpaceMax()); + map.put("edenSpaceUsed", edenSpaceUsed()); + map.put("survivorCommitted", survivorCommitted()); + map.put("survivorInit", survivorInit()); + map.put("survivorMax", survivorMax()); + map.put("survivorUsed", survivorUsed()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index a2c7ef03..0c2251c1 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -12,7 +12,7 @@ public class JVMThread extends BaseQuota implements ThreadQuota{ private ThreadMXBean threadMXBean; - public static final JVMThread instance = new JVMThread(); + public static final JVMThread I = new JVMThread(); private JVMThread() { threadMXBean = ManagementFactory.getThreadMXBean(); diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index c9c77e7f..f3464eee 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -1,44 +1,36 @@ package com.mpush.monitor.quota.impl; -import java.util.Iterator; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.ThreadPoolQuota; +import com.mpush.tools.thread.pool.ThreadPoolManager; + import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.ThreadPoolQuota; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota { + public static final JVMThreadPool I = new JVMThreadPool(); + + private JVMThreadPool() { + } -public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota{ + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + Map pool = ThreadPoolManager.I.getActivePools(); + for (Map.Entry entry : pool.entrySet()) { + String serviceName = entry.getKey(); + ThreadPoolExecutor executor = (ThreadPoolExecutor) entry.getValue(); + String info = "coreThreadNum:" + executor.getCorePoolSize() + + " maxThreadNum:" + executor.getMaximumPoolSize() + + " workingThreadNum:" + executor.getActiveCount() + + " workThreadNum:" + executor.getPoolSize() + + " blockTaskNum:" + executor.getQueue().size(); + map.put(serviceName, info); + } + return map; + } - public static final JVMThreadPool instance = new JVMThreadPool(); - - private JVMThreadPool() { - } - - private Map pool = null; - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - if(pool == null){ - pool = ThreadPoolManager.getPool(); - } - - Iterator> ite = pool.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - String serviceUniqName = entry.getKey(); - ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue(); - StringBuilder sb = new StringBuilder(); - sb.append("coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:" - + executor.getActiveCount()); - map.put(serviceUniqName, sb.toString()); - } - return map; - } - - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java deleted file mode 100644 index f299ed5c..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.mpush.monitor.service; - -import com.mpush.tools.JVMUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.monitor.domain.MonitorData; -import com.mpush.monitor.quota.impl.JVMGC; -import com.mpush.monitor.quota.impl.JVMInfo; -import com.mpush.monitor.quota.impl.JVMMemory; -import com.mpush.monitor.quota.impl.JVMThread; -import com.mpush.monitor.quota.impl.JVMThreadPool; -import com.mpush.tools.Jsons; - -public class MonitorDataCollector { - - private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); - - private static volatile boolean dumpFirstJstack = false; - - private static volatile boolean dumpSecondJstack = false; - - private static volatile boolean dumpThirdJstack = false; - - private static volatile boolean dumpJmap = false; - - private static String currentPath = "/tmp/logs/mpush/"; - - private static int firstJstack = 2; - - private static int secondJstack = 4; - - private static int thirdJstack = 6; - - private static int firstJmap = 4; - - public static MonitorData collect() { - MonitorData data = new MonitorData(); - data.setInfoMap(JVMInfo.instance.toMap()); - data.setGcMap(JVMGC.instance.toMap()); - data.setMemoryMap(JVMMemory.instance.toMap()); - data.setThreadMap(JVMThread.instance.toMap()); - data.setThreadPoolMap(JVMThreadPool.instance.toMap()); - return data; - } - - public void setPath(String path) { - currentPath = path; - } - - public static void start(final boolean skipdump) { - new Thread(new Runnable() { - - @Override - public void run() { - while (true) { - MonitorData monitorData = MonitorDataCollector.collect(); - log.error("monitor data:" + Jsons.toJson(monitorData)); - - if(!skipdump){ - double load = JVMInfo.instance.load(); - if (load > firstJstack) { - if (!dumpFirstJstack) { - dumpFirstJstack = true; - JVMUtil.dumpJstack(currentPath); - } - } - - if (load > secondJstack) { - if (!dumpSecondJstack) { - dumpSecondJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if (load > thirdJstack) { - if (!dumpThirdJstack) { - dumpThirdJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if (load > firstJmap) { - if (!dumpJmap) { - dumpJmap = true; - JVMUtil.dumpJmap(currentPath); - } - } - } - - - try {//30s - Thread.sleep(30000L); - } catch (InterruptedException e) { - log.warn("monitor data exception", e); - } - } - } - }).start(); - } - - public static void main(String[] args) { - MonitorDataCollector.start(true); - } - -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java new file mode 100644 index 00000000..dac32ea4 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -0,0 +1,105 @@ +package com.mpush.monitor.service; + +import com.mpush.api.BaseService; +import com.mpush.tools.log.Logs; +import com.mpush.monitor.data.MonitorResult; +import com.mpush.monitor.data.ResultCollector; +import com.mpush.monitor.quota.impl.JVMInfo; +import com.mpush.tools.JVMUtil; +import com.mpush.tools.Jsons; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MonitorService extends BaseService implements Runnable { + + private final Logger LOGGER = LoggerFactory.getLogger(MonitorService.class); + + public static final MonitorService I = new MonitorService(); + + private static final int firstJstack = 2, secondJstack = 4, thirdJstack = 6, firstJmap = 4; + + private String dumpLogDir = "/tmp/logs/mpush/"; + private boolean dumpFirstJstack = false; + private boolean dumpSecondJstack = false; + private boolean dumpThirdJstack = false; + private boolean dumpJmap = false; + private boolean enableDump = false; + + private final ResultCollector collector = new ResultCollector(); + + @Override + public void run() { + while (started.get()) { + MonitorResult result = collector.collect(); + Logs.Monitor.info(Jsons.toJson(result)); + if (enableDump) { + dump(); + } + try {//30s + Thread.sleep(30000L); + } catch (InterruptedException e) { + LOGGER.warn("monitor data exception", e); + } + } + } + + @Override + public void start(Listener listener) { + if (started.compareAndSet(false, true)) { + Thread thread = new Thread(this, "mp-t-monitor"); + thread.start(); + } + } + + @Override + public void stop(Listener listener) { + started.set(false); + } + + @Override + public boolean isRunning() { + return started.get(); + } + + private void dump() { + double load = JVMInfo.I.load(); + if (load > firstJstack) { + if (!dumpFirstJstack) { + dumpFirstJstack = true; + JVMUtil.dumpJstack(dumpLogDir); + } + } + + if (load > secondJstack) { + if (!dumpSecondJstack) { + dumpSecondJstack = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + + if (load > thirdJstack) { + if (!dumpThirdJstack) { + dumpThirdJstack = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + + if (load > firstJmap) { + if (!dumpJmap) { + dumpJmap = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + } + + + public MonitorService setDumpLogDir(String dumpLogDir) { + this.dumpLogDir = dumpLogDir; + return this; + } + + public MonitorService setEnableDump(boolean enableDump) { + this.enableDump = enableDump; + return this; + } +} diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index b0bcbe3f..c0c37221 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -18,7 +18,11 @@ ${mpush.groupId} - mpush-common + mpush-api + + + ${mpush.groupId} + mpush-tools io.netty diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java deleted file mode 100644 index 5aa506c2..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.netty.client; - -import com.mpush.api.Client; - -import io.netty.channel.ChannelHandler; - -public interface ChannelClientHandler extends ChannelHandler{ - - public Client getClient(); - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 7929ff42..3a827434 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -1,154 +1,81 @@ package com.mpush.netty.client; -import java.util.concurrent.TimeUnit; - -import com.mpush.netty.util.NettySharedHolder; +import com.mpush.api.BaseService; +import com.mpush.api.Client; +import com.mpush.netty.codec.PacketDecoder; +import com.mpush.netty.codec.PacketEncoder; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; - -import org.apache.commons.lang3.StringUtils; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.api.Client; -import com.mpush.api.connection.Connection; -import com.mpush.api.protocol.Packet; -import com.mpush.tools.config.ConfigCenter; +import java.net.InetSocketAddress; +import java.util.concurrent.atomic.AtomicBoolean; -public class NettyClient implements Client { - - private static final String format = "%s:%s"; - - private static final Logger log = LoggerFactory.getLogger(NettyClient.class); +public abstract class NettyClient extends BaseService implements Client { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); private final String host; private final int port; - private Channel channel; - private int hbTimes; - private Connection connection; - - public NettyClient(final String host, final int port) { + private EventLoopGroup workerGroup; + + public NettyClient(String host, int port) { this.host = host; this.port = port; } - - @Override - public Channel getChannel() { - return channel; - } - - public void setChannel(Channel channel) { - this.channel = channel; - } - @Override - public void initConnection(Connection connection){ - this.connection = connection; - } - @Override - public void close(String cause) { - if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { - log.error("close channel:"+cause); - } - if(this.channel!=null){ - this.channel.close(); - } - - } - - @Override - public boolean isEnabled() { - return channel.isWritable(); - } - - @Override - public boolean isConnected() { - return channel.isActive(); - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } - - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void startHeartBeat() throws Exception { - startHeartBeat((int)ConfigCenter.I.scanConnTaskCycle()/1000); - } - - - @Override - public String getUrl() { - return String.format(format, host, port); - } - - @Override - public String getHost() { - return host; - } - - @Override - public int getPort() { - return port; - } - - @Override - public void stop(){ - this.close("stop"); - } - - @Override - public Connection getConnection() { - return connection; - } - - @Override - public void init(Channel channel) { - this.channel = channel; - } - - @Override - public String toString() { - return "NettyClient [host=" + host + ", port=" + port + ", channel=" + channel + ", hbTimes=" + hbTimes + ", connection=" + connection + "]"; - } - - @Override - public void startHeartBeat(final int heartbeat) throws Exception { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { + public void start(final Listener listener) { + if (started.compareAndSet(false, true)) { + Bootstrap bootstrap = new Bootstrap(); + workerGroup = new NioEventLoopGroup(); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new PacketDecoder()); + ch.pipeline().addLast(PacketEncoder.INSTANCE); + ch.pipeline().addLast(getChannelHandler()); + } + }); + + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + if (listener != null) listener.onSuccess(port); + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + if (listener != null) listener.onFailure(future.cause()); + LOGGER.error("start netty client failure, host={}, port={}", host, port, future.cause()); + } + } + }); + } else { + listener.onFailure(new RuntimeException("client has started!")); + } + } - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); - } - log.warn("client send msg hb false:" + channel.remoteAddress().toString()); - } else { - log.warn("client send msg hb success:" + channel.remoteAddress().toString()); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); - } - } - } - }, heartbeat, TimeUnit.MILLISECONDS); - - } + @Override + public void stop(Listener listener) { + if (workerGroup != null) { + workerGroup.shutdownGracefully(); + } + started.set(false); + } - + public abstract ChannelHandler getChannelHandler(); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java deleted file mode 100644 index d3cce35e..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.mpush.netty.client; - -import java.net.InetSocketAddress; -import java.util.Map; - -import com.mpush.netty.codec.PacketDecoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; - -import com.google.common.collect.Maps; -import com.mpush.api.Client; -import com.mpush.netty.codec.PacketEncoder; -import com.mpush.netty.util.NettySharedHolder; - -public class NettyClientFactory { - - private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); - - public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - - private final Map channel2Client = Maps.newConcurrentMap(); - - public Client create(final ChannelClientHandler handler){ - final Bootstrap bootstrap = new Bootstrap(); - bootstrap.group(NettySharedHolder.workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(handler); - } - }); - - Client client = handler.getClient(); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - log.error("init channel:"+channel); - return client; - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + client); - return null; - } - } - - public Client getCientByChannel(final Channel channel) { - return channel2Client.get(channel); - } - - public void remove(final Channel channel) { - channel2Client.remove(channel); - } - - public void put(Channel channel,final Client client){ - channel2Client.put(channel, client); - } - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java deleted file mode 100644 index 6e55531b..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.mpush.netty.client; - - - -public class SecurityNettyClient extends NettyClient { - - private byte[] clientKey; - private byte[] iv; - private String clientVersion; - private String deviceId; - private String osName; - private String osVersion; - - private String userId; - - private String cipher; //快速重连的时候使用 - - public SecurityNettyClient(String host, int port) { - super(host, port); - } - - public byte[] getClientKey() { - return clientKey; - } - - public void setClientKey(byte[] clientKey) { - this.clientKey = clientKey; - } - - public byte[] getIv() { - return iv; - } - - public void setIv(byte[] iv) { - this.iv = iv; - } - - public String getClientVersion() { - return clientVersion; - } - - public void setClientVersion(String clientVersion) { - this.clientVersion = clientVersion; - } - - public String getDeviceId() { - return deviceId; - } - - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; - } - - public String getOsName() { - return osName; - } - - public void setOsName(String osName) { - this.osName = osName; - } - - public String getOsVersion() { - return osVersion; - } - - public void setOsVersion(String osVersion) { - this.osVersion = osVersion; - } - - public String getCipher() { - return cipher; - } - - public void setCipher(String cipher) { - this.cipher = cipher; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 764bf760..0a6fb191 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -3,7 +3,7 @@ import com.mpush.api.exception.DecodeException; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -17,6 +17,7 @@ * @author ohun@live.cn */ public final class PacketDecoder extends ByteToMessageDecoder { + private static final int maxPacketSize = CC.mp.core.max_packet_size; @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { @@ -65,7 +66,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { - if (bodyLength > ConfigCenter.I.maxPacketSize()) { + if (bodyLength > maxPacketSize) { throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); } body = new byte[bodyLength]; diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 0b1a12e2..5c67366f 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -3,7 +3,8 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.common.security.CipherBox; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.core.CipherFactory; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -17,23 +18,21 @@ */ public final class NettyConnection implements Connection, ChannelFutureListener { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); - + private static final CipherFactory factory = SpiLoader.load(CipherFactory.class); private SessionContext context; private Channel channel; private volatile int status = STATUS_NEW; private long lastReadTime; private long lastWriteTime; - private int hbTimes = 0; - @Override public void init(Channel channel, boolean security) { this.channel = channel; this.context = new SessionContext(); this.lastReadTime = System.currentTimeMillis(); this.status = STATUS_CONNECTED; - if (security) { - this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); + if (security && factory != null) { + this.context.changeCipher(factory.get()); } } @@ -112,19 +111,14 @@ public void updateLastWriteTime() { lastWriteTime = System.currentTimeMillis(); } - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } @Override public String toString() { - return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes + return "NettyConnection [context=" + context + + ", channel=" + channel + + ", status=" + status + + ", lastReadTime=" + lastReadTime + + ", lastWriteTime=" + lastWriteTime + "]"; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java similarity index 92% rename from mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java rename to mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java index 43b27000..74b77915 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java @@ -1,4 +1,4 @@ -package com.mpush.netty.client; +package com.mpush.netty.http; import io.netty.handler.codec.http.HttpResponse; diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java similarity index 86% rename from mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java rename to mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java index 014e0ba0..3e3e2cb5 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java @@ -1,4 +1,4 @@ -package com.mpush.netty.client; +package com.mpush.netty.http; /** * Created by ohun on 2016/2/15. diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java similarity index 96% rename from mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java rename to mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index d7677c43..e3cf38f7 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -1,10 +1,9 @@ -package com.mpush.netty.client; +package com.mpush.netty.http; import com.google.common.collect.ArrayListMultimap; -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; -import com.mpush.tools.config.ConfigCenter; - +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -13,7 +12,6 @@ import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.util.*; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,7 +30,7 @@ public class NettyHttpClient implements HttpClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); - private final int maxConnPerHost = ConfigCenter.I.maxHttpConnCountPerHost(); + private final int maxConnPerHost = CC.mp.http.max_conn_per_host; private final AttributeKey requestKey = AttributeKey.newInstance("request"); private final AttributeKey hostKey = AttributeKey.newInstance("host"); private final ArrayListMultimap channelPool = ArrayListMultimap.create(); @@ -43,7 +41,7 @@ public class NettyHttpClient implements HttpClient { @Override public void start() { - workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.httpExecutor); + workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); @@ -61,7 +59,7 @@ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("handler", new HttpClientHandler()); } }); - timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), + timer = new HashedWheelTimer(new PoolThreadFactory("http-client-timer-"), 1, TimeUnit.SECONDS, 64); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java similarity index 94% rename from mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java rename to mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java index 28c61ee4..70f1bf72 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java @@ -1,8 +1,8 @@ -package com.mpush.netty.client; +package com.mpush.netty.http; import com.google.common.primitives.Ints; import com.mpush.api.Constants; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Timeout; @@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class RequestInfo implements TimerTask, HttpCallback { - private static final int TIMEOUT = ConfigCenter.I.httpDefaultReadTimeout(); + private static final int TIMEOUT = CC.mp.http.default_read_timeout; final AtomicBoolean cancelled = new AtomicBoolean(false); final long startTime = System.currentTimeMillis(); long endTime = startTime; diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index db065085..8a395c3d 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -3,7 +3,7 @@ import com.mpush.api.Server; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -17,6 +17,8 @@ import java.util.concurrent.atomic.AtomicReference; +import static com.mpush.tools.thread.pool.ThreadPoolManager.I; + /** * Created by ohun on 2015/12/22. * @@ -159,16 +161,16 @@ public void operationComplete(ChannelFuture future) throws Exception { } private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolManager.bossExecutor); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, I.getBossExecutor()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, I.getWorkExecutor()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } @SuppressWarnings("unused") private void createEpollServer(final Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, I.getBossExecutor()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, I.getWorkExecutor()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java deleted file mode 100644 index 34874d67..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.mpush.netty.util; - - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.ThreadNameSpace; - -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; -import io.netty.util.Timer; - -public class NettySharedHolder { - - public static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); - - public static EventLoopGroup workerGroup = new NioEventLoopGroup(); - -} diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 2c830035..7a1312d7 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -34,20 +34,10 @@ ${mpush.groupId} mpush-boot - - ${mpush.groupId} - mpush-log - ${mpush.groupId} mpush-client - - - args4j - args4j - - diff --git a/mpush-test/src/test/java/com/mpush/AppTest.java b/mpush-test/src/test/java/com/mpush/AppTest.java deleted file mode 100644 index 796b8466..00000000 --- a/mpush-test/src/test/java/com/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java new file mode 100644 index 00000000..e9ba4e36 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -0,0 +1,53 @@ +package com.mpush.test.client; + +import com.mpush.api.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain { + + public static void main(String[] args) throws Exception { + Logs.init(); + ConnectClientBoot main = new ConnectClientBoot(); + main.start(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + + for (int i = 0; i < 1; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "user-" + i; + String deviceId = "test-device-id-" + i; + String cipher = ""; + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] iv = CipherBox.I.randomAESIV(); + + ClientConfig config = new ClientConfig(); + config.setClientKey(clientKey); + config.setIv(iv); + config.setClientVersion(clientVersion); + config.setDeviceId(deviceId); + config.setOsName(osName); + config.setOsVersion(osVersion); + config.setUserId(userId); + config.setCipher(cipher); + Client client = new ConnectClient(server.getIp(), server.getPort(), config); + client.start(null); + Thread.sleep(10); + } + + LockSupport.park(); + + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java new file mode 100644 index 00000000..74db2cb7 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java @@ -0,0 +1,24 @@ +package com.mpush.test.client; + +import com.google.common.collect.Lists; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public class ConnectClientBoot { + private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); + + public void start() { + ZKClient.I.init(); + RedisManager.I.init(); + listener.beginWatch(); + } + + public List getServers() { + return Lists.newArrayList(listener.getCache().values()); + } +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 61ae1c36..905b5bc7 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -1,29 +1,92 @@ package com.mpush.test.configcenter; -import java.util.List; -import java.util.Map; - +import com.mpush.tools.config.CC; +import com.typesafe.config.ConfigObject; +import com.typesafe.config.ConfigValue; +import org.junit.Before; import org.junit.Test; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.DnsMapping; +import java.util.*; public class ConfigCenterTest { - - @Test - public void test(){ - - System.out.println(ConfigCenter.I.forceWriteRedisGroupInfo()); - - } - - @Test - public void testDnsMapping(){ - Map> ret = ConfigCenter.I.dnsMapping(); - - System.out.println(Jsons.toJson(ret)); - - } + @Before + public void setUp() throws Exception { + // ConfigManager.I.getLocalIp(); + } + + @Test + public void testKey() { + //String t = ConfigKey.app_env.getString(); + System.out.println(CC.mp.security.aes_key_length); + System.out.println(CC.mp.redis.cluster_group); + } + + @Test + public void testLoad() { + Map map = new HashMap<>(); + CC.cfg.entrySet().forEach(e -> print(e.getKey(), e.getValue(), map)); + List list = new ArrayList<>(map.values()); + Collections.sort(list); + list.forEach(s -> System.out.println(s.substring(s.indexOf(".") + 1) + ",")); + } + + public static void print(String s, ConfigValue configValue, Map map) { + if (s.startsWith("mp") && !s.endsWith("\"")) { + String[] keys = s.split("\\."); + if (keys.length >= 4) return; + for (int i = keys.length - 1; i > 0; i--) { + String key = keys[i]; + String value = map.get(key); + if (value != null) continue; + String p = keys[i - 1]; + map.put(key, p + "." + key.replace('-', '_') + "(" + p + ")"); + } + } + } + + @Test + public void testLoad2() { + Map map = new HashMap<>(); + System.out.println("public interface mp {"); + System.out.printf(" Config cfg = ConfigManager.I.mp().toConfig();%n%n"); + CC.mp.cfg.root().forEach((s, configValue) -> print2(s, configValue, "mp", 1)); + System.out.println("}"); + } + + public static void print2(String s, ConfigValue configValue, String p, int level) { + int l = level + 1; + switch (configValue.valueType()) { + case OBJECT: + printTab(level); + System.out.printf("interface %s {%n", s.replace('-', '_')); + printTab(level); + System.out.printf(" Config cfg = %s.cfg.getObject(\"%s\").toConfig();%n%n", p, s); + ((ConfigObject) configValue).forEach((s1, configValue2) -> print2(s1, configValue2, s, l)); + printTab(level); + System.out.printf("}%n%n"); + break; + case STRING: + printTab(level); + System.out.printf(" String %s = cfg.getString(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case NUMBER: + printTab(level); + System.out.printf(" Number %s = cfg.getNumber(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case BOOLEAN: + printTab(level); + System.out.printf(" Boolean %s = cfg.getBoolean(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case LIST: + printTab(level); + System.out.printf(" List %s = cfg.getList(\"%s\").unwrapped();%n%n", s.replace('-', '_'), s); + break; + } + } + private static void printTab(int l) { + while (l-- > 0) { + System.out.print(" "); + } + } } diff --git a/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java deleted file mode 100644 index ca5b9c0d..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mpush.test.connection.client; - -import com.google.common.collect.Lists; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; -import com.mpush.zk.ZKServerNode; - -import java.util.List; - -public class ConnectTestClient extends AbstractClient { - private final ConnectZKListener listener = new ConnectZKListener(); - - public ConnectTestClient() { - registerListener(listener); - } - - public List getServers() { - return Lists.newArrayList(listener.getManager().getList()); - } -} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java deleted file mode 100644 index e0a0aea7..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/client/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.connection.client; - -import com.mpush.conn.client.ClientChannelHandler; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.zk.ZKServerNode; -import com.mpush.common.security.CipherBox; -import com.mpush.netty.client.SecurityNettyClient; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class Main { - - public static void main(String[] args) throws Exception { - ConnectTestClient main = new ConnectTestClient(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for(int i = 0;i<1;i++){ - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "user-"+i; - String deviceId = "test-device-id-"+i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(10); - } - - LockSupport.park(); - - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java new file mode 100644 index 00000000..4bfcd6ee --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java @@ -0,0 +1,50 @@ +package com.mpush.test.connection.mpns; + +import com.mpush.api.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain { + + public static void main(String[] args) throws InterruptedException { + + ConnectTestClientBoot main = new ConnectTestClientBoot(); + main.start(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + + for (int i = 0; i < 1000; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "uh-" + i; + String deviceId = "test-device-id-" + i; + String cipher = ""; + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] iv = CipherBox.I.randomAESIV(); + + ClientConfig config = new ClientConfig(); + config.setClientKey(clientKey); + config.setIv(iv); + config.setClientVersion(clientVersion); + config.setDeviceId(deviceId); + config.setOsName(osName); + config.setOsVersion(osVersion); + config.setUserId(userId); + config.setCipher(cipher); + Client client = new ConnectClient(server.getIp(), server.getPort(), config); + Thread.sleep(100); + } + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java deleted file mode 100644 index 1126476d..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mpush.test.connection.mpns; - -import com.google.common.collect.Lists; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; -import com.mpush.zk.ZKServerNode; - -import java.util.List; - -public class ConnectTestClient extends AbstractClient { - private final ConnectZKListener listener = new ConnectZKListener(); - - public ConnectTestClient() { - registerListener(listener); - } - - public List getServers() { - return Lists.newArrayList(listener.getManager().getList()); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java new file mode 100644 index 00000000..4c2adc12 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java @@ -0,0 +1,19 @@ +package com.mpush.test.connection.mpns; + +import com.google.common.collect.Lists; +import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public class ConnectTestClientBoot { + private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); + + public void start() { + listener.beginWatch(); + } + + public List getServers() { + return Lists.newArrayList(listener.getCache().values()); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java deleted file mode 100644 index 9de9a50e..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.mpush.test.connection.mpns; - -import com.mpush.common.security.CipherBox; -import com.mpush.conn.client.ClientChannelHandler; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.netty.client.SecurityNettyClient; -import com.mpush.zk.ZKServerNode; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class Main { - - public static void main(String[] args) throws InterruptedException { - - ConnectTestClient main = new ConnectTestClient(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for (int i = 0; i < 1000; i++) { - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "uh-" + i; - String deviceId = "test-device-id-" + i; - String cipher = ""; - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] iv = CipherBox.INSTANCE.randomAESIV(); - - SecurityNettyClient client = new SecurityNettyClient(server.getIp(), server.getPort()); - client.setClientKey(clientKey); - client.setIv(iv); - client.setClientVersion(clientVersion); - client.setDeviceId(deviceId); - client.setOsName(osName); - client.setOsVersion(osVersion); - client.setUserId(userId); - client.setCipher(cipher); - - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - Thread.sleep(100); - } - - LockSupport.park(); - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java index 5ff50659..aa7e36c5 100644 --- a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java +++ b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java @@ -33,8 +33,8 @@ public void init(){ public void test(){ HandshakeMessage message = new HandshakeMessage(null); - message.clientKey = CipherBox.INSTANCE.randomAESKey(); - message.iv = CipherBox.INSTANCE.randomAESIV(); + message.clientKey = CipherBox.I.randomAESKey(); + message.iv = CipherBox.I.randomAESIV(); message.clientVersion = "1.1.0"; message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; message.osName = "android"; @@ -115,8 +115,8 @@ public Worker(int i,CountDownLatch encodeLatch,CountDownLatch decodeLatch) { @Override public void run() { HandshakeMessage message = new HandshakeMessage(null); - message.clientKey = CipherBox.INSTANCE.randomAESKey(); - message.iv = CipherBox.INSTANCE.randomAESIV(); + message.clientKey = CipherBox.I.randomAESKey(); + message.iv = CipherBox.I.randomAESIV(); message.clientVersion = "1.1.0"+i; message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; message.osName = "android"; diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 7406913d..73388dd2 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -6,8 +6,8 @@ import org.junit.Test; import com.google.common.collect.Maps; -import com.mpush.api.PushContent; -import com.mpush.api.PushContent.PushType; +import com.mpush.api.push.PushContent; +import com.mpush.api.push.PushContent.PushType; public class GsonTest { diff --git a/mpush-test/src/test/java/com/mpush/test/push/Main.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java similarity index 78% rename from mpush-test/src/test/java/com/mpush/test/push/Main.java rename to mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 38b6b120..7a94d2ff 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -1,10 +1,10 @@ package com.mpush.test.push; -import com.mpush.api.PushContent; -import com.mpush.api.PushContent.PushType; -import com.mpush.api.PushSender; -import com.mpush.push.PushClient; +import com.mpush.api.push.PushContent; +import com.mpush.api.push.PushContent.PushType; +import com.mpush.api.push.PushSender; import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; import java.util.Arrays; import java.util.concurrent.locks.LockSupport; @@ -14,16 +14,17 @@ * * @author ohun@live.cn */ -public class Main { +public class PushClientTestMain { public static void main(String[] args) throws Exception { - PushClient client = new PushClient(); - client.start(); + Logs.init(); + PushSender sender = PushSender.factory.get(); + sender.start(); Thread.sleep(1000); for (int i = 0; i < 100; i++) { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); - client.send(Jsons.toJson(content), Arrays.asList("doctor43test", "user-0", "huang"), new PushSender.Callback() { + sender.send(Jsons.toJson(content), Arrays.asList("doctor43test", "user-0", "huang"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); diff --git a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java index fe122a9f..697bedea 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java @@ -7,8 +7,8 @@ import java.util.Map; import java.util.UUID; -import com.mpush.tools.redis.consistenthash.ConsistentHash; -import com.mpush.tools.redis.consistenthash.Node; +import com.mpush.cache.redis.hash.ConsistentHash; +import com.mpush.cache.redis.hash.Node; import org.junit.Test; import redis.clients.util.Hashing; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java index a2913fa5..5775a05f 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java @@ -1,59 +1,54 @@ package com.mpush.test.redis; -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.tools.redis.pubsub.Subscriber; -import com.mpush.tools.redis.RedisRegister; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.cache.redis.manager.ZKRedisClusterManager; +import com.mpush.cache.redis.mq.Subscriber; import org.junit.Before; import org.junit.Test; -import com.google.common.collect.Lists; -import com.mpush.tools.spi.ServiceContainer; +import java.util.concurrent.locks.LockSupport; public class PubSubTest { - - private RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - + + private ZKRedisClusterManager redisClusterManager = ZKRedisClusterManager.I; + @Before - public void init(){ - RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - RedisGroup group = new RedisGroup(); - group.addRedisNode(node); - List listGroup = Lists.newArrayList(group); - redisRegister.init(listGroup); + public void init() { + RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + RedisGroup group = new RedisGroup(); + group.addRedisNode(node); + redisClusterManager.addGroup(group); + } + + @Test + public void subpubTest() { + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); } - - @Test - public void subpubTest(){ - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - } - - @Test - public void pubsubTest(){ - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - } - - @Test - public void pubTest(){ - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - } - - @Test - public void subTest(){ - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - LockSupport.park(); - } - + + @Test + public void pubsubTest() { + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + } + + @Test + public void pubTest() { + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); + } + + @Test + public void subTest() { + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + LockSupport.park(); + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java index 13052f07..1fcf8d90 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java @@ -1,46 +1,44 @@ package com.mpush.test.redis; -import java.util.Date; -import java.util.HashSet; -import java.util.Set; - -import com.mpush.tools.redis.RedisPoolConfig; +import com.mpush.cache.redis.RedisClient; +import com.mpush.tools.Jsons; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; - -import com.mpush.tools.Jsons; - import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + public class RedisClusterTest { - Set jedisClusterNodes = new HashSet(); - - JedisCluster cluster = null; - - @Before - public void init() { - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); - cluster = new JedisCluster(jedisClusterNodes, RedisPoolConfig.config); - } - - @Test - public void test() { - - User user = new User("huang", 18, new Date()); - cluster.set("huang", Jsons.toJson(user)); - String ret = cluster.get("huang"); - User newUser = Jsons.fromJson(ret, User.class); - System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); - - } + Set jedisClusterNodes = new HashSet(); + + JedisCluster cluster = null; + + @Before + public void init() { + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); + cluster = new JedisCluster(jedisClusterNodes, RedisClient.CONFIG); + } + + @Test + public void test() { + + User user = new User("huang", 18, new Date()); + cluster.set("huang", Jsons.toJson(user)); + String ret = cluster.get("huang"); + User newUser = Jsons.fromJson(ret, User.class); + System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java deleted file mode 100644 index 2d376e59..00000000 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java +++ /dev/null @@ -1,109 +0,0 @@ -//package com.mpush.tools.redis; -// -//import java.util.Date; -//import java.util.List; -// -//import MessageListener; -// -//import org.apache.commons.lang3.builder.ToStringBuilder; -//import org.apache.commons.lang3.builder.ToStringStyle; -//import org.junit.Before; -//import org.junit.Test; -// -//import MPushUtil; -//import RedisManage; -//import Subscriber; -//import ServiceContainer; -//import com.mpush.tools.zk.ServerApp; -// -//public class RedisGroupManageTest { -// -// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// List groupList = null; -// -// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); -// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); -// -// RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); -// -// @Before -// public void init() { -// groupList = redisRegister.getGroupList(); -// } -// -// @Test -// public void testGetRedisGroup() { -// for (RedisGroup group : groupList) { -// for (RedisNode node : group.getRedisNodeList()) { -// System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); -// } -// -// } -// } -// -// @Test -// public void testAdd() { -// User user = RedisManage.get("huang2", User.class); -// if (user == null) { -// user = new User("hi", 10, new Date()); -// RedisManage.set("huang2", user); -// user = RedisManage.get("huang2", User.class); -// } -// System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); -// -// User nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// -// RedisManage.del("huang2"); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// if (nowUser == null) { -// System.out.println("node2 nowUser is null"); -// } else { -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); -// -// } -// -// @Test -// public void testPub() { -// for (int i = 0; i < 20; i++) { -// User user = new User("pub" + i, 10, new Date()); -// RedisManage.publish("channel1", user); -// RedisManage.publish("channel2", user); -// } -// } -// -// @Test -// public void testSub() { -// RedisManage.subscribe(new MessageListener() { -// @Override -// public void onMessage(String channel, String message) { -// System.out.printf("on message channel=%s, message=%s%n", channel, message); -// } -// }, "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// @Test -// public void testSub2() { -// RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// -//} diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index c8c4a45f..b1ae18c1 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -1,199 +1,196 @@ package com.mpush.test.redis; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.RedisServer; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; +import redis.clients.jedis.Jedis; + import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.junit.Test; +public class RedisUtilTest { -import com.google.common.collect.Lists; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.RedisUtil; -import redis.clients.jedis.Jedis; + RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + + List nodeList = Lists.newArrayList(node); + + @Test + public void testAddAndGetAndDelete() { + Jedis jedis = RedisClient.getClient(node); + jedis.set("hi", "huang"); + + String ret = jedis.get("hi"); + System.out.println(ret); + + jedis.del("hi"); + ret = jedis.get("hi"); + if (ret == null) { + System.out.println("ret is null"); + } else { + System.out.println("ret is not null:" + ret); + } + + } + + @Test + public void testJedisPool() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 0; i < 10; i++) { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + } + } + + @Test + public void testJedisPool2() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 1; i <= 8; i++) { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + } + + System.out.println(jedisList.size()); + + try { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + System.out.println("first get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + // 关闭一个链接 + RedisClient.close(jedisList.get(0)); + + try { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + System.out.println("second get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + System.out.println(jedisList.size()); + } + + @Test + public void testKV() { + User user = new User("huang", 18, new Date()); + RedisClient.set(nodeList, "test", user); + + User nowUser = RedisClient.get(node, "test", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisClient.get(node, "test", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisClient.del(nodeList, "test"); + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + RedisClient.set(nodeList, "test", user, 10); + + try { + Thread.sleep(12000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void hashTest() { + + User user = new User("huang", 18, new Date()); + + RedisClient.hset(nodeList, "hashhuang", "hi", user); + + User nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + Map ret = RedisClient.hgetAll(node, "hashhuang", User.class); + Iterator> iterator = ret.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + User val = entry.getValue(); + System.out.println("all:" + key + "," + ToStringBuilder.reflectionToString(val)); + } + + RedisClient.hdel(nodeList, "hashhuang", "hi"); + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void testSet() { +// System.out.println(RedisClient.sCard(node, RedisKey.getUserOnlineKey())); + +// List onlineUserIdList = RedisClient.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); +// System.out.println(onlineUserIdList.size()); -public class RedisUtilTest { + } - - - RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - - List nodeList = Lists.newArrayList(node); - - @Test - public void testAddAndGetAndDelete() { - Jedis jedis = RedisUtil.getClient(node); - jedis.set("hi", "huang"); - - String ret = jedis.get("hi"); - System.out.println(ret); - - jedis.del("hi"); - ret = jedis.get("hi"); - if (ret == null) { - System.out.println("ret is null"); - } else { - System.out.println("ret is not null:" + ret); - } - - } - - @Test - public void testJedisPool() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 0; i < 10; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - } - - @Test - public void testJedisPool2() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 1; i <= 8; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - - System.out.println(jedisList.size()); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("first get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - // 关闭一个链接 - RedisUtil.close(jedisList.get(0)); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("second get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - System.out.println(jedisList.size()); - } - - @Test - public void testKV() { - User user = new User("huang", 18, new Date()); - RedisUtil.set(nodeList, "test", user); - - User nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisUtil.del(nodeList, "test"); - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - RedisUtil.set(nodeList, "test", user, 10); - - try { - Thread.sleep(12000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void hashTest(){ - - User user = new User("huang", 18, new Date()); - - RedisUtil.hset(nodeList, "hashhuang", "hi", user); - - User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - - Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); - Iterator> iterator = ret.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String key = entry.getKey(); - User val = entry.getValue(); - System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); - } - - RedisUtil.hdel(nodeList, "hashhuang", "hi"); - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node2 nowUser is null"); - }else{ - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node nowUser is null"); - }else{ - System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void testSet(){ -// System.out.println(RedisUtil.sCard(node, RedisKey.getUserOnlineKey())); - -// List onlineUserIdList = RedisUtil.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); -// System.out.println(onlineUserIdList.size()); - - } - - @Test - public void testlist(){ -// RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); - } - - @Test - public void testsortedset(){ -// RedisUtil.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); - -// long len =RedisUtil.zCard(node, RedisKey.getUserOnlineKey()); + @Test + public void testlist() { +// RedisClient.del(nodeList, RedisKey.getUserOfflineKey()); + } + + @Test + public void testsortedset() { +// RedisClient.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); + +// long len =RedisClient.zCard(node, RedisKey.getUserOnlineKey()); // System.out.println(len); - } + } } diff --git a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java similarity index 51% rename from mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java rename to mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java index 778a4778..ac9eac45 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -1,12 +1,14 @@ -package com.mpush.test.connection.severs; +package com.mpush.test.sever; + +import com.mpush.boot.Main; /** * Created by yxx on 2016/5/16. * * @author ohun@live.cn */ -public class Main { +public class ServerTestMain { public static void main(String[] args) { - com.mpush.Main.main(args); + Main.main(args); } } diff --git a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java index 5965f67d..69d472a1 100644 --- a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java @@ -10,13 +10,13 @@ public class TelnetTest { @Test public void test(){ - boolean ret = MPushUtil.telnet("120.27.196.68", 82); + boolean ret = MPushUtil.checkHealth("120.27.196.68", 82); System.out.println(ret); } @Test public void test2(){ - boolean ret = MPushUtil.telnet("120.27.196.68", 80); + boolean ret = MPushUtil.checkHealth("120.27.196.68", 80); System.out.println(ret); } diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf new file mode 100644 index 00000000..b13d6c3d --- /dev/null +++ b/mpush-test/src/test/resources/application.conf @@ -0,0 +1,3 @@ +mp.log.dir=${user.dir}/mpush-test/target/logs +mp.log.level=debug +mp.zk.namespace=mpush \ No newline at end of file diff --git a/mpush-test/src/test/resources/config.properties b/mpush-test/src/test/resources/config.properties deleted file mode 100644 index 23e53825..00000000 --- a/mpush-test/src/test/resources/config.properties +++ /dev/null @@ -1,35 +0,0 @@ -## -max_packet_size=10240 -## -compress_limit=10240 -## -min_heartbeat=10000 -## -max_heartbeat=180000 -## -max_hb_timeout_times=2 -## -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -## -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -## -gateway_server_port=20882 -## -connection_server_port=3000 -## -aes_key_length=16 -## -ras_key_length=1024 -## -session_expired_time=86400 -#zk_ip = 115.29.169.109:5666 -zk_ip=127.0.0.1:2181 -zk_namespace=mpush-daily -zk_digest=shinemoIpo -##redis_group = 127.0.0.1:6379:shinemoIpo,127.0.0.1:6380:shinemoIpo;127.0.0.1:6381:shinemoIpo -#redis_group = 111.1.57.148:6379:ShineMoIpo -redis_group=111.1.57.148:6379:ShineMoIpo -force_write_redis_group_info=false -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 -#IpIpӳ -remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml deleted file mode 100644 index ea3f95d0..00000000 --- a/mpush-test/src/test/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - debug - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory b/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory new file mode 100644 index 00000000..93607f48 --- /dev/null +++ b/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory @@ -0,0 +1 @@ +com.mpush.client.push.PushClientFactory diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 218927c0..31ae2c40 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -14,7 +14,24 @@ ${mpush-tools-version} jar mpush-tools - + + + + src/main/resources + + **/* + + false + + + ../conf + + reference.conf + + false + + + com.google.code.gson @@ -28,54 +45,37 @@ org.apache.commons commons-lang3 - - redis.clients - jedis + org.aeonbits.owner + owner + + + com.typesafe + config + + + ${mpush.groupId} + mpush-api org.slf4j slf4j-api - ${mpush.groupId} - mpush-log + org.slf4j + jcl-over-slf4j - org.aeonbits.owner - owner + ch.qos.logback + logback-classic - commons-net - commons-net + commons-logging + commons-logging + + + log4j + log4j - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-tools - - - - - - - - diff --git a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java b/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java deleted file mode 100644 index 27497d15..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.mpush.tools; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public final class ConsoleLog { - private static final String TAG = "[mpush] "; - private static final DateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); - - public static void d(String s, Object... args) { - System.out.printf(format.format(new Date()) + " [D] " + TAG + s + '\n', args); - } - - public static void i(String s, Object... args) { - System.out.printf(format.format(new Date()) + " [I] " + TAG + s + '\n', args); - } - - public static void w(String s, Object... args) { - System.err.printf(format.format(new Date()) + " [W] " + TAG + s + '\n', args); - } - - public static void e(Throwable e, String s, Object... args) { - System.err.printf(format.format(new Date()) + " [E] " + TAG + s + '\n', args); - e.printStackTrace(); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/mpush/tools/Constants.java deleted file mode 100644 index 458c6068..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/Constants.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.mpush.tools; - -import java.nio.charset.Charset; - -/** - * Created by ohun on 2015/12/25. - * - * @author ohun@live.cn - */ -public interface Constants { - Charset UTF_8 = Charset.forName("UTF-8"); - byte[] EMPTY_BYTES = new byte[0]; - - int MIN_BOSS_POOL_SIZE = 50; - int MAX_BOSS_POOL_SIZE = 100; - int BOSS_THREAD_QUEUE_SIZE = 10000; - - int MIN_WORK_POOL_SIZE = 50; - int MAX_WORK_POOL_SIZE = 150; - int WORK_THREAD_QUEUE_SIZE = 10000; - - int BIZ_POOL_SIZE = 20; - - int EVENT_BUS_POOL_SIZE = 10; - - int REDIS_POOL_SIZE = 3; - int REDIS_THREAD_QUEUE_SIZE = 10000; - - //redis - int REDIS_TIMEOUT = 2000; - int REDIS_MAX_TOTAL = 8; - int REDIS_MAX_IDLE = 4; - int REDIS_MIN_IDLE = 1; - int REDIS_MAX_WAIT_MILLIS = 5000; - int REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 300000; - int REDIS_NUM_TESTS_PER_EVICTION_RUN = 3; - int REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; - boolean REDIS_TEST_ON_BORROW = false; - boolean REDIS_TEST_ON_RETURN = false; - boolean REDIS_TEST_WHILE_IDLE = false; -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java index f322e8f8..443fb17a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java @@ -1,5 +1,6 @@ package com.mpush.tools; +import com.mpush.api.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java index 1bffb0b0..f7ff57ff 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java @@ -22,10 +22,10 @@ public class JVMUtil { - private static final Logger log = LoggerFactory.getLogger(JVMUtil.class); + private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class); - private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; - private static volatile HotSpotDiagnosticMXBean hotspotMBean; + private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; + private static volatile HotSpotDiagnosticMXBean mxBean; private static Object lock = new Object(); @@ -58,10 +58,10 @@ public void run() { String logPath = jvmPath; FileOutputStream jstackStream = null; try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.log")); + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.LOGGER")); JVMUtil.jstack(jstackStream); } catch (Throwable t) { - log.error("Dump JVM cache Error!", t); + LOGGER.error("Dump JVM cache Error!", t); } finally { if (jstackStream != null) { try { @@ -74,12 +74,12 @@ public void run() { }); } - private static HotSpotDiagnosticMXBean getHotspotMBean() { + private static HotSpotDiagnosticMXBean getMxBean() { try { return AccessController.doPrivileged(new PrivilegedExceptionAction() { public HotSpotDiagnosticMXBean run() throws Exception { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - Set s = server.queryNames(new ObjectName(HOTSPOT_BEAN_NAME), null); + Set s = server.queryNames(new ObjectName(HOT_SPOT_BEAN_NAME), null); Iterator itr = s.iterator(); if (itr.hasNext()) { ObjectName name = itr.next(); @@ -92,45 +92,42 @@ public HotSpotDiagnosticMXBean run() throws Exception { } }); } catch (Exception e) { - log.error("getHotspotMBean Error!", e); + LOGGER.error("getMxBean Error!", e); return null; } } private static void initHotspotMBean() throws Exception { - if (hotspotMBean == null) { + if (mxBean == null) { synchronized (lock) { - if (hotspotMBean == null) { - hotspotMBean = getHotspotMBean(); + if (mxBean == null) { + mxBean = getMxBean(); } } } } public static void jMap(String fileName, boolean live) { - File f = new File(fileName, System.currentTimeMillis()+"-jmap.log"); + File f = new File(fileName, System.currentTimeMillis()+"-jmap.LOGGER"); String currentFileName = f.getPath(); try { initHotspotMBean(); if (f.exists()) { f.delete(); } - - hotspotMBean.dumpHeap(currentFileName, live); + mxBean.dumpHeap(currentFileName, live); } catch (Exception e) { - log.error("dumpHeap Error!"+currentFileName, e); + LOGGER.error("dumpHeap Error!"+currentFileName, e); } } public static void dumpJmap(final String jvmPath){ - Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { jMap(jvmPath, false); } }); - } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java index 5b7e726e..d863cdd7 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -2,7 +2,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; - +import com.mpush.api.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java index 5e846c06..ab3b4a9b 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java @@ -1,13 +1,13 @@ package com.mpush.tools; -import com.mpush.tools.config.ConfigCenter; -import org.apache.commons.net.telnet.TelnetClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.NetworkInterface; +import java.net.Socket; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -42,13 +42,6 @@ public static String getLocalIp() { return LOCAL_IP; } - public static int getHeartbeat(int min, int max) { - return Math.max( - ConfigCenter.I.minHeartbeat(), - Math.min(max, ConfigCenter.I.maxHeartbeat()) - ); - } - /** * 获取本机ip * 只获取第一块网卡绑定的ip地址 @@ -105,20 +98,7 @@ public static String getExtranetAddress() { } catch (Throwable e) { LOGGER.error("getExtranetAddress exception", e); } - - - String localIp = getInetAddress(); - String remoteIp = null; - Map mapping = ConfigCenter.I.remoteIpMapping(); - if (mapping != null) { - remoteIp = mapping.get(localIp); - } - - if (remoteIp == null) { - remoteIp = localIp; - } - - return remoteIp; + return null; } public static String headerToString(Map headers) { @@ -157,21 +137,14 @@ public static Map headerFromString(String headersString) { return headers; } - public static boolean telnet(String ip, int port) { - TelnetClient client = new TelnetClient(); + public static boolean checkHealth(String ip, int port) { try { - client.connect(ip, port); + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(ip, port), 1000); + socket.close(); return true; - } catch (Exception e) { + } catch (IOException e) { return false; - } finally { - try { - if (client.isConnected()) { - client.disconnect(); - } - } catch (IOException e) { - LOGGER.error("disconnect error", e); - } } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java new file mode 100644 index 00000000..0b11edab --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -0,0 +1,263 @@ +package com.mpush.tools.config; + +import com.mpush.tools.config.data.DnsMapping; +import com.mpush.tools.config.data.RedisGroup; +import com.mpush.tools.config.data.RedisServer; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigList; +import com.typesafe.config.ConfigObject; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static com.typesafe.config.ConfigBeanFactory.create; +import static java.util.stream.Collectors.toCollection; + +/** + * Created by yxx on 2016/5/20. + * + * @author ohun@live.cn + */ +public interface CC { + Config cfg = load(); + + static Config load() { + Config config = ConfigFactory.load(); + String custom_conf = "mp.conf"; + if (config.hasPath(custom_conf)) { + File file = new File(config.getString(custom_conf)); + if (file.exists()) { + Config custom = ConfigFactory.parseFile(file); + config = custom.withFallback(config); + } + } + return config; + } + + interface mp { + Config cfg = CC.cfg.getObject("mp").toConfig(); + + String log_dir = cfg.getString("log.dir"); + String log_level = cfg.getString("log.level"); + + interface security { + Config cfg = mp.cfg.getObject("security").toConfig(); + + int aes_key_length = cfg.getInt("aes-key-length"); + + String public_key = cfg.getString("public-key"); + + String private_key = cfg.getString("private-key"); + + int ras_key_length = cfg.getInt("ras-key-length"); + + } + + interface zk { + Config cfg = mp.cfg.getObject("zk").toConfig(); + + int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS); + + String local_cache_path = cfg.getString("local-cache-path"); + + int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS); + + String namespace = cfg.getString("namespace"); + + String digest = cfg.getString("digest"); + + String server_address = cfg.getString("server-address"); + + interface retry { + Config cfg = zk.cfg.getObject("retry").toConfig(); + + int maxRetries = cfg.getInt("maxRetries"); + + int baseSleepTimeMs = (int) cfg.getDuration("baseSleepTimeMs", TimeUnit.MILLISECONDS); + + int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS); + + } + } + + interface thread { + Config cfg = mp.cfg.getObject("thread").toConfig(); + + interface pool { + Config cfg = thread.cfg.getObject("pool").toConfig(); + + interface boss { + Config cfg = pool.cfg.getObject("boss").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + + interface work { + Config cfg = pool.cfg.getObject("work").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + + interface event_bus { + Config cfg = pool.cfg.getObject("event-bus").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + + interface http_proxy { + Config cfg = pool.cfg.getObject("http-proxy").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + + interface biz { + Config cfg = pool.cfg.getObject("biz").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + + interface mq { + Config cfg = pool.cfg.getObject("mq").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + } + } + + interface redis { + Config cfg = mp.cfg.getObject("redis").toConfig(); + boolean write_to_zk = cfg.getBoolean("write-to-zk"); + + List cluster_group = cfg.getList("cluster-group") + .stream() + .map(v -> new RedisGroup(ConfigList.class.cast(v) + .stream() + .map(cv -> create(ConfigObject.class.cast(cv).toConfig(), RedisServer.class)) + .collect(toCollection(ArrayList::new)) + ) + ) + .collect(toCollection(ArrayList::new)); + + interface config { + Config cfg = redis.cfg.getObject("config").toConfig(); + + boolean jmxEnabled = cfg.getBoolean("jmxEnabled"); + + int minIdle = cfg.getInt("minIdle"); + + boolean testOnReturn = cfg.getBoolean("testOnReturn"); + + long softMinEvictableIdleTimeMillis = cfg.getDuration("softMinEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); + + boolean testOnBorrow = cfg.getBoolean("testOnBorrow"); + + boolean testWhileIdle = cfg.getBoolean("testWhileIdle"); + + long maxWaitMillis = cfg.getDuration("maxWaitMillis", TimeUnit.MILLISECONDS); + + String jmxNameBase = cfg.getString("jmxNameBase"); + + long numTestsPerEvictionRun = cfg.getDuration("numTestsPerEvictionRun", TimeUnit.MILLISECONDS); + + String jmxNamePrefix = cfg.getString("jmxNamePrefix"); + + long minEvictableIdleTimeMillis = cfg.getDuration("minEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); + + boolean blockWhenExhausted = cfg.getBoolean("blockWhenExhausted"); + + boolean fairness = cfg.getBoolean("fairness"); + + long timeBetweenEvictionRunsMillis = cfg.getDuration("timeBetweenEvictionRunsMillis", TimeUnit.MILLISECONDS); + + boolean testOnCreate = cfg.getBoolean("testOnCreate"); + + int maxIdle = cfg.getInt("maxIdle"); + + boolean lifo = cfg.getBoolean("lifo"); + + int maxTotal = cfg.getInt("maxTotal"); + + } + + } + + interface monitor { + Config cfg = mp.cfg.getObject("monitor").toConfig(); + + boolean dump_stack = cfg.getBoolean("dump-stack"); + + } + + interface http { + Config cfg = mp.cfg.getObject("http").toConfig(); + + boolean proxy_enable = cfg.getBoolean("proxy-enable"); + + Map> dns_mapping = loadMapping(); + + static Map> loadMapping() { + Map> map = new HashMap<>(); + cfg.getObject("dns-mapping").forEach((s, v) -> + map.put(s, ConfigList.class.cast(v) + .stream() + .map(cv -> DnsMapping.parse((String) cv.unwrapped())) + .collect(toCollection(ArrayList::new)) + ) + ); + return map; + } + + int default_read_timeout = (int) cfg.getDuration("default-read-timeout", TimeUnit.MILLISECONDS); + + int max_conn_per_host = cfg.getInt("max-conn-per-host"); + + } + + interface net { + Config cfg = mp.cfg.getObject("net").toConfig(); + + int gateway_server_port = cfg.getInt("gateway-server-port"); + + int connect_server_port = cfg.getInt("connect-server-port"); + + interface public_ip_mapping { + Config cfg = net.cfg.getObject("public-host-mapping").toConfig(); + + static String getString(String localIp) { + return cfg.hasPath(localIp) ? cfg.getString(localIp) : localIp; + } + } + + int admin_server_port = cfg.getInt("admin-server-port"); + + } + + interface core { + Config cfg = mp.cfg.getObject("core").toConfig(); + + int session_expired_time = (int) cfg.getDuration("session-expired-time").getSeconds(); + + int max_heartbeat = (int) cfg.getDuration("max-heartbeat", TimeUnit.MILLISECONDS); + + int max_packet_size = (int) cfg.getMemorySize("max-packet-size").toBytes(); + + int min_heartbeat = (int) cfg.getDuration("min-heartbeat", TimeUnit.MILLISECONDS); + + long compress_threshold = cfg.getBytes("compress-threshold"); + + int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times"); + } + } + +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java index 29d7588a..1b970b31 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java @@ -1,8 +1,11 @@ package com.mpush.tools.config; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.dns.DnsMapping; +import com.mpush.tools.config.converter.DnsMappingConverter; +import com.mpush.tools.config.converter.MapConverter; +import com.mpush.tools.config.converter.RedisGroupConverter; +import com.mpush.tools.config.data.DnsMapping; +import com.mpush.tools.config.data.RedisGroup; import org.aeonbits.owner.Config; import org.aeonbits.owner.Config.Sources; import org.aeonbits.owner.ConfigFactory; @@ -19,7 +22,7 @@ }) public interface ConfigCenter extends Config { - ConfigCenter I = ConfigFactory.create(ConfigCenter.class); + //ConfigCenter I = ConfigFactory.create(ConfigCenter.class); /** * 最大包长度 @@ -140,7 +143,7 @@ public interface ConfigCenter extends Config { String publicKey(); /** - * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd + * redis集群机器列表格式ip:port:pwd,host:port:pwd,host:port:pwd * 多台机器用“,”分割 * * @return @@ -178,7 +181,7 @@ public interface ConfigCenter extends Config { String zkDigest(); /** - * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd + * redis集群机器列表格式ip:port:pwd,host:port:pwd,host:port:pwd * 多台机器用“;”分割 * * @return diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java new file mode 100644 index 00000000..7ffc6876 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -0,0 +1,36 @@ +package com.mpush.tools.config; + +import com.mpush.tools.MPushUtil; + +import static com.mpush.tools.MPushUtil.getInetAddress; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ConfigManager { + public static final ConfigManager I = new ConfigManager(); + + private ConfigManager() { + } + + public int getHeartbeat(int min, int max) { + return Math.max( + CC.mp.core.min_heartbeat, + Math.min(max, CC.mp.core.max_heartbeat) + ); + } + + public String getLocalIp() { + return MPushUtil.getLocalIp(); + } + + public String getPublicIp() { + String localIp = getInetAddress(); + + String remoteIp = CC.mp.net.public_ip_mapping.getString(localIp); + + return remoteIp; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java deleted file mode 100644 index 7696bbbb..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.mpush.tools.config; - -import com.google.common.base.Function; -import com.google.common.base.Splitter; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.primitives.Ints; -import com.mpush.tools.dns.DnsMapping; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public class DnsMappingConverter implements Converter>>{ - - private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); - - @Override - public Map> convert(Method method, String input) { - - log.warn("method:"+method.getName()+","+input); - Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); - Map> result = Maps.newConcurrentMap(); - for (Map.Entry entry : map.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - List dnsMappings = Lists.transform(Arrays.asList(value.split(",")), new Function() { - @Override - public DnsMapping apply(String ipAndPort) { - if(ipAndPort.contains(":")){ - String[] temp = ipAndPort.split(":"); - return new DnsMapping(temp[0], Ints.tryParse(temp[1])); - }else{ - return new DnsMapping(ipAndPort, 80); - } - } - }); - result.put(key, dnsMappings); - } - return result; - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java deleted file mode 100644 index 7ca15bc0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.mpush.tools.config; - -import java.lang.reflect.Method; - -import com.mpush.tools.redis.RedisGroup; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.redis.RedisNode; - -public class RedisGroupConverter implements Converter{ - - private static final Logger log = LoggerFactory.getLogger(RedisGroupConverter.class); - - @Override - public RedisGroup convert(Method method, String input) { - - log.warn("method:"+method.getName()+","+input); - - - RedisGroup group = new RedisGroup(); - - String[] chunks = input.split(","); - for (String chunk : chunks) { - String[] entry = chunk.split(":"); - String ip = entry[0].trim(); - String port = entry[1].trim(); - // 如果配置了redis密码(redis_group = 111.1.57.148:6379:ShineMoIpo)才设置密码 - // 否则密码为空,JedisPool可以兼容两种情况 - String password = null; - if(entry.length >=3){ - password = entry[2].trim(); - } - RedisNode node = new RedisNode(ip, Integer.parseInt(port), password); - group.addRedisNode(node); - } - return group; - } - - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java new file mode 100644 index 00000000..f41b64f6 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java @@ -0,0 +1,45 @@ +package com.mpush.tools.config.converter; + +import com.google.common.base.Function; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.primitives.Ints; +import com.mpush.tools.config.data.DnsMapping; +import com.mpush.tools.log.Logs; +import org.aeonbits.owner.Converter; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +public class DnsMappingConverter implements Converter>> { + + @Override + public Map> convert(Method method, String input) { + Logs.Console.info("method:" + method.getName() + ", input:" + input); + + Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); + Map> result = Maps.newConcurrentMap(); + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + List dnsMappings = Lists.transform(Arrays.asList(value.split(",")), new Function() { + @Override + public DnsMapping apply(String ipAndPort) { + if (ipAndPort.contains(":")) { + String[] temp = ipAndPort.split(":"); + return new DnsMapping(temp[0], Ints.tryParse(temp[1])); + } else { + return new DnsMapping(ipAndPort, 80); + } + } + }); + result.put(key, dnsMappings); + } + return result; + + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java similarity index 70% rename from mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java rename to mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java index f16e05a2..0644f7db 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java @@ -1,10 +1,9 @@ -package com.mpush.tools.config; +package com.mpush.tools.config.converter; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import com.mpush.tools.log.Logs; import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.lang.reflect.Method; import java.util.Collections; @@ -16,11 +15,10 @@ * @author ohun@live.cn */ public class MapConverter implements Converter> { - private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); @Override public Map convert(Method method, String input) { - log.warn("method:" + method.getName() + "," + input); + Logs.Console.info("method:" + method.getName() + ", input:" + input); if (Strings.isNullOrEmpty(input)) return Collections.emptyMap(); return Splitter.on(',').withKeyValueSeparator(':').split(input); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java new file mode 100644 index 00000000..33d81262 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java @@ -0,0 +1,35 @@ +package com.mpush.tools.config.converter; + +import com.mpush.tools.config.data.RedisGroup; +import com.mpush.tools.config.data.RedisServer; +import com.mpush.tools.log.Logs; +import org.aeonbits.owner.Converter; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +public class RedisGroupConverter implements Converter { + + @Override + public RedisGroup convert(Method method, String input) { + Logs.Console.info("method:" + method.getName() + ", input:" + input); + + List servers = new ArrayList<>(); + String[] chunks = input.split(","); + for (String chunk : chunks) { + String[] entry = chunk.split(":"); + String ip = entry[0].trim(); + String port = entry[1].trim(); + // 如果配置了redis密码(redis_group = 111.1.57.148:6379:ShineMoIpo)才设置密码 + // 否则密码为空,JedisPool可以兼容两种情况 + String password = null; + if (entry.length >= 3) { + password = entry[2].trim(); + } + servers.add(new RedisServer(ip, Integer.parseInt(port), password)); + } + + return new RedisGroup(servers); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java new file mode 100644 index 00000000..6970ad06 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java @@ -0,0 +1,37 @@ +package com.mpush.tools.config.data; + + +import java.util.Objects; + +public class DnsMapping { + private String ip; + private int port; + + public DnsMapping(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public String getIp() { + return ip; + } + + public int getPort() { + return port; + } + + public static DnsMapping parse(String addr) { + String[] host_port = Objects.requireNonNull(addr, "dns mapping can not be null") + .split(":"); + if (host_port.length == 1) { + return new DnsMapping(host_port[0], 80); + } else { + return new DnsMapping(host_port[0], Integer.valueOf(host_port[1])); + } + } + + @Override + public String toString() { + return ip + ":" + port; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java new file mode 100644 index 00000000..81064638 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java @@ -0,0 +1,26 @@ +package com.mpush.tools.config.data; + +import java.util.Collections; +import java.util.List; + + +/** + * redis 组 + */ +public class RedisGroup { + public List redisNodeList = Collections.emptyList(); + + public RedisGroup() { + } + + public RedisGroup(List redisNodeList) { + this.redisNodeList = redisNodeList; + } + + @Override + public String toString() { + return "RedisGroup{" + + "redisNodeList=" + redisNodeList + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java new file mode 100644 index 00000000..dc5bf913 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java @@ -0,0 +1,52 @@ +package com.mpush.tools.config.data; + +/** + * redis 相关的配置信息 + */ +public class RedisServer { + public String host; + public int port; + public String password; + + public RedisServer() { + } + + public RedisServer(String host, int port, String password) { + this.host = host; + this.port = port; + this.password = password; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "RedisServer{" + + "host='" + host + '\'' + + ", port=" + port + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index c7280a07..bf181b51 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -1,7 +1,8 @@ package com.mpush.tools.crypto; -import com.mpush.tools.Constants; + +import com.mpush.api.Constants; import java.io.*; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index 02b2ff17..2a1f4bce 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -1,6 +1,6 @@ package com.mpush.tools.crypto; -import com.mpush.tools.Constants; +import com.mpush.api.Constants; import com.mpush.tools.IOUtils; import com.mpush.tools.Strings; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index e3201180..646335bb 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -1,6 +1,6 @@ package com.mpush.tools.crypto; -import com.mpush.tools.Constants; +import com.mpush.api.Constants; import com.mpush.tools.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java deleted file mode 100644 index 6f541b3a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.mpush.tools.dns; - - -public class DnsMapping { - - private String ip; - private int port; - - public DnsMapping(String ip, int port) { - this.ip = ip; - this.port = port; - } - - public String getIp() { - return ip; - } - public int getPort() { - return port; - } - - @Override - public String toString() { - return ip+":"+port; - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java deleted file mode 100644 index 905f4e96..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.mpush.tools.dns.manage; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.DnsMapping; - -public class DnsMappingManage { - - private static final Logger LOG = LoggerFactory.getLogger(DnsMappingManage.class); - - private DnsMappingManage() { - } - - public static final DnsMappingManage holder = new DnsMappingManage(); - - private Map> all = Maps.newConcurrentMap(); - private Map> available = Maps.newConcurrentMap(); - - private Worker worker = new Worker(); - - private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); - - public void init() { - LOG.error("start init dnsMapping"); - all.putAll(ConfigCenter.I.dnsMapping()); - available.putAll(ConfigCenter.I.dnsMapping()); - pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns - LOG.error("end init dnsMapping"); - } - - public void update(Map> nowAvailable) { - available = nowAvailable; - } - - public Map> getAll() { - return all; - } - - public DnsMapping translate(String origin) { - if (available.isEmpty()) - return null; - List list = available.get(origin); - if (list == null || list.isEmpty()) - return null; - int L = list.size(); - if (L == 1) - return list.get(0); - return list.get((int) (Math.random() * L % L)); - } - - public void shutdown() { - pool.shutdown(); - } - - public static class Worker implements Runnable { - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - - log.debug("start dns mapping telnet"); - - Map> all = DnsMappingManage.holder.getAll(); - - Map> available = Maps.newConcurrentMap(); - - for (Map.Entry> entry : all.entrySet()) { - String key = entry.getKey(); - List value = entry.getValue(); - List nowValue = Lists.newArrayList(); - for (DnsMapping temp : value) { - boolean canTelnet = MPushUtil.telnet(temp.getIp(), temp.getPort()); - if (canTelnet) { - nowValue.add(temp); - } else { - log.error("dns can not reachable:" + Jsons.toJson(temp)); - } - } - available.put(key, nowValue); - } - - DnsMappingManage.holder.update(available); - - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/Event.java b/mpush-tools/src/main/java/com/mpush/tools/event/Event.java deleted file mode 100644 index afabfdee..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/Event.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.mpush.tools.event; - -public class Event { - - private final EventType eventType; - private final Object source; - - public Event(final EventType eventType, final Object source) { - this.eventType = eventType; - this.source = source; - } - - public EventType getEventType() { - return eventType; - } - - public Object getSource() { - return source; - } - -} diff --git a/mpush-common/src/main/java/com/mpush/common/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java similarity index 72% rename from mpush-common/src/main/java/com/mpush/common/EventBus.java rename to mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index e27184c4..a5aeb72a 100644 --- a/mpush-common/src/main/java/com/mpush/common/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -1,11 +1,10 @@ -package com.mpush.common; +package com.mpush.tools.event; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; -import com.mpush.api.event.Event; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,12 +16,12 @@ * @author ohun@live.cn */ public class EventBus { - private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); - public static final EventBus INSTANCE = new EventBus(); + private final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); + public static final EventBus I = new EventBus(); private final com.google.common.eventbus.EventBus eventBus; public EventBus() { - Executor executor = ThreadPoolManager.eventBusExecutor; + Executor executor = ThreadPoolManager.I.getEventBusExecutor(); eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { @@ -31,11 +30,10 @@ public void handleException(Throwable exception, SubscriberExceptionContext cont }); } - public void post(Event event) { + public void post(Object event) { eventBus.post(event); } - public void register(Object bean) { eventBus.register(bean); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java new file mode 100644 index 00000000..d83e5914 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java @@ -0,0 +1,9 @@ +package com.mpush.tools.event; + +public abstract class EventConsumer { + + public EventConsumer() { + EventBus.I.register(this); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java deleted file mode 100644 index fbcb19ee..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.tools.event; - -import java.util.ArrayList; -import java.util.List; - -public class EventDispatcher { - - private static final List listeners = new ArrayList(); - - public static void addEventListener(EventListener listener) { - listeners.add(listener); - } - - public static void fireEvent(Event event) { - for (EventListener listener : listeners) { - listener.onEvent(event); - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java deleted file mode 100644 index 5a9b6d82..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.mpush.tools.event; - -public interface EventListener { - - public void onEvent(Event event); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java deleted file mode 100644 index ccd77b3f..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.mpush.tools.event; - -public enum EventType { - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java new file mode 100644 index 00000000..0529e67c --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -0,0 +1,41 @@ +package com.mpush.tools.log; + +import com.mpush.tools.config.CC; +import com.typesafe.config.ConfigRenderOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2016/5/16. + * + * @author ohun@live.cn + */ +public interface Logs { + boolean logInited = init(); + + static boolean init() { + System.setProperty("log.home", CC.mp.log_dir); + System.setProperty("log.root.level", CC.mp.log_level); + LoggerFactory + .getLogger("console") + .info( + CC.mp.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true)) + ); + return true; + } + + Logger Console = LoggerFactory.getLogger("console"), + Conn = LoggerFactory.getLogger("connLog"), + + Monitor = LoggerFactory.getLogger("monitorLog"), + + PUSH = LoggerFactory.getLogger("pushLog"), + + HB = LoggerFactory.getLogger("heartbeatLog"), + + REDIS = LoggerFactory.getLogger("redisLog"), + + ZK = LoggerFactory.getLogger("zkLog"), + + HTTP = LoggerFactory.getLogger("httpLog"); +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java deleted file mode 100644 index 73b69ec3..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.tools.redis; - -import java.util.List; - -import com.google.common.collect.Lists; - - -/** - * redis 组 - * - */ -public class RedisGroup { - - private List redisNodeList; - - public List getRedisNodeList() { - return redisNodeList; - } - - public void setRedisNodeList(List redisNodeList) { - this.redisNodeList = redisNodeList; - } - - public void addRedisNode(RedisNode node){ - if(redisNodeList==null){ - redisNodeList = Lists.newArrayList(); - } - redisNodeList.add(node); - } - - public void remove(int i){ - if(redisNodeList!=null){ - redisNodeList.remove(i); - } - } - - public void clear(){ - if(redisNodeList!=null){ - redisNodeList.clear(); - } - } - - public RedisNode get(String key){ - int i = key.hashCode() %redisNodeList.size(); - return redisNodeList.get(i); - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java deleted file mode 100644 index 82f3de80..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.mpush.tools.redis; - -/** - * redis 相关的配置信息 - * - */ -public class RedisNode { - - private String ip; - private int port; - private String password; - public String getIp() { - return ip; - } - public void setIp(String ip) { - this.ip = ip; - } - public int getPort() { - return port; - } - public void setPort(int port) { - this.port = port; - } - public String getPassword() { - return password; - } - public void setPassword(String password) { - this.password = password; - } - - public RedisNode(String ip, int port, String password) { - this.ip = ip; - this.port = port; - this.password = password; - } - @Override - public String toString() { - return "RedisNode [ip=" + ip + ", port=" + port + ", password=" + password + "]"; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java deleted file mode 100644 index b1a1e383..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.tools.redis; - -import com.mpush.tools.Constants; - -import redis.clients.jedis.JedisPoolConfig; - -public class RedisPoolConfig { - - public static JedisPoolConfig config = new JedisPoolConfig(); - - static{ - //连接池中最大连接数。高版本:maxTotal,低版本:maxActive - config.setMaxTotal(Constants.REDIS_MAX_TOTAL); - //连接池中最大空闲的连接数 - config.setMaxIdle(Constants.REDIS_MAX_IDLE); - //连接池中最少空闲的连接数 - config.setMinIdle(Constants.REDIS_MIN_IDLE); - //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait - config.setMaxWaitMillis(Constants.REDIS_MAX_WAIT_MILLIS); - //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 - config.setMinEvictableIdleTimeMillis(Constants.REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS); - //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 - config.setNumTestsPerEvictionRun(Constants.REDIS_NUM_TESTS_PER_EVICTION_RUN); - //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 - config.setTimeBetweenEvictionRunsMillis(Constants.REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); - //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. - config.setTestOnBorrow(Constants.REDIS_TEST_ON_BORROW); - //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 - config.setTestOnReturn(Constants.REDIS_TEST_ON_RETURN); - //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. - config.setTestWhileIdle(Constants.REDIS_TEST_WHILE_IDLE); - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java deleted file mode 100644 index 834babfa..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mpush.tools.redis; - -import com.mpush.tools.spi.SPI; - -import java.util.List; - -@SPI("redisRegister") -public interface RedisRegister { - - void init(List group); - - List getGroupList(); - - RedisNode randomGetRedisNode(String key); - - List hashSet(String key); -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java deleted file mode 100644 index 71a5765d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.mpush.tools.redis.jedis.services; - -import com.google.common.collect.Lists; -import com.mpush.log.Logs; - -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisRegister; - -import java.util.Collections; -import java.util.List; - -public class JedisRegisterManager implements RedisRegister { - - private static List groups = Lists.newArrayList(); - - /** - * zk 启动的时候需要调用这个 - */ - @Override - public void init(List group) { - if (group == null || group.isEmpty()) { - Logs.REDIS.info("init redis client error, redis server is none."); - throw new RuntimeException("init redis client error, redis server is none."); - } - groups = group; - printGroupList(); - } - - - @Override - public List getGroupList() { - return Collections.unmodifiableList(groups); - } - - private void printGroupList() { - for (RedisGroup app : groups) { - Logs.REDIS.info(Jsons.toJson(app)); - } - } - - public int groupSize() { - return groups.size(); - } - - /** - * 随机获取一个redis 实例 - * - * @param key - * @return - */ - @Override - public RedisNode randomGetRedisNode(String key) { - int size = groupSize(); - if (size == 1) return groups.get(0).get(key); - int i = (int) ((Math.random() % size) * size); - RedisGroup group = groups.get(i); - return group.get(key); - } - - /** - * 写操作的时候,获取所有redis 实例 - * - * @param key - * @return - */ - @Override - public List hashSet(String key) { - List nodeList = Lists.newArrayList(); - for (RedisGroup group : groups) { - RedisNode node = group.get(key); - nodeList.add(node); - } - return nodeList; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java deleted file mode 100644 index cc0c9790..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java +++ /dev/null @@ -1,289 +0,0 @@ -package com.mpush.tools.redis.manage; - -import com.google.common.collect.Sets; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.RedisRegister; -import com.mpush.tools.redis.RedisUtil; -import com.mpush.tools.spi.ServiceContainer; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPubSub; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * redis 对外封装接口 - */ -public class RedisManage { - - private static final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - - public static long incr(String key, Integer time) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incr(nodeList, key, time); - } - - public static long incrBy(String key, long delt) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incrBy(nodeList, key, delt); - } - - /********************* - * k v redis start - ********************************/ - - public static T get(String key, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.get(node, key, clazz); - } - - public static void set(String key, T value) { - set(key, value, null); - } - - public static void set(String key, T value, Integer time) { - String jsonValue = Jsons.toJson(value); - set(key, jsonValue, time); - } - - /** - * @param key - * @param value - * @param time seconds - */ - public static void set(String key, String value, Integer time) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.set(nodeList, key, value, time); - } - - public static void del(String key) { - - List nodeList = redisRegister.hashSet(key); - RedisUtil.del(nodeList, key); - - } - - /*********************k v redis end********************************/ - - - /********************* - * hash redis start - ********************************/ - public static void hset(String namespace, String key, String value) { - - List nodeList = redisRegister.hashSet(key); - RedisUtil.hset(nodeList, namespace, key, value); - - } - - public static void hset(String namespace, String key, T value) { - hset(namespace, key, Jsons.toJson(value)); - } - - public static T hget(String namespace, String key, Class clazz) { - - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hget(node, namespace, key, clazz); - - } - - public static void hdel(String namespace, String key) { - List nodeList = redisRegister.hashSet(namespace); - RedisUtil.hdel(nodeList, namespace, key); - } - - public static Map hgetAll(String namespace) { - - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace); - - } - - public static Map hgetAll(String namespace, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace, clazz); - } - - /** - * 返回 key 指定的哈希集中所有字段的名字。 - * - * @return - */ - public static Set hkeys(String namespace) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hkeys(node, namespace); - } - - /** - * 返回 key 指定的哈希集中指定字段的值 - * - * @param key - * @param clazz - * @return - */ - public static List hmget(String namespace, Class clazz, String... key) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hmget(node, namespace, clazz, key); - } - - /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 - * - * @param hash - * @param time - */ - public static void hmset(String namespace, Map hash, Integer time) { - List nodeList = redisRegister.hashSet(namespace); - RedisUtil.hmset(nodeList, namespace, hash, time); - } - - public static void hmset(String namespace, Map hash) { - hmset(namespace, hash, null); - } - - - /*********************hash redis end********************************/ - - - /*********************list redis start********************************/ - /** - * 从队列的左边入队 - */ - public static void lpush(String key, String value) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.lpush(nodeList, key, value); - } - - public static void lpush(String key, T value) { - lpush(key, Jsons.toJson(value)); - } - - /** - * 从队列的右边入队 - */ - public static void rpush(String key, String value) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.rpush(nodeList, key, value); - } - - public static void rpush(String key, T value) { - rpush(key, Jsons.toJson(value)); - } - - /** - * 移除并且返回 key 对应的 list 的第一个元素 - */ - public static T lpop(String key, Class clazz) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.lpop(nodeList, key, clazz); - } - - /** - * 从队列的右边出队一个元素 - */ - public static T rpop(String key, Class clazz) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.rpop(nodeList, key, clazz); - } - - - /** - * 从列表中获取指定返回的元素 - * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List lrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.lrange(node, key, start, end, clazz); - } - - /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 - */ - public static long llen(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.llen(node, key); - } - - public static void lrem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.lRem(nodeList, key, jsonValue); - } - - public static void publish(String channel, T message) { - - RedisNode node = redisRegister.randomGetRedisNode(channel); - RedisUtil.publish(node, channel, message); - - } - - public static void subscribe(JedisPubSub pubsub, String... channels) { - - Set set = Sets.newHashSet(); - for (String channel : channels) { - List nodeList = redisRegister.hashSet(channel); - set.addAll(nodeList); - } - - RedisUtil.subscribe(set, pubsub, channels); - } - - public static void sAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sAdd(nodeList, key, jsonValue); - } - - public static Long sCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.sCard(node, key); - } - - public static void sRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sRem(nodeList, key, jsonValue); - } - - public static List sScan(String key, int start, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.sScan(node, key, clazz, start); - } - - public static void zAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.zAdd(nodeList, key, jsonValue); - } - - public static Long zCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zCard(node, key); - } - - public static void zRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.zRem(nodeList, key, jsonValue); - } - - public static List zrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zrange(node, key, start, end, clazz); - } - - public static void test(List groupList) { - for (RedisGroup group : groupList) { - for (RedisNode node : group.getRedisNodeList()) { - Jedis jedis = RedisUtil.getClient(node); - if (jedis == null) throw new RuntimeException("init redis sever error."); - jedis.close(); - } - } - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java b/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java deleted file mode 100644 index e212649e..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.mpush.tools.spi; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE }) -public @interface SPI { - - String value() default ""; - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 39fa5f51..04dc1a1a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -3,45 +3,35 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -public class NamedThreadFactory implements ThreadFactory{ - - private static final AtomicInteger poolNum = new AtomicInteger(1); - - private final AtomicInteger threadNum = new AtomicInteger(1); - - private final ThreadGroup group; - private final String namePre; - private final boolean isDaemon; - - public NamedThreadFactory(){ - this("pool"); - } - - public NamedThreadFactory(String prefix){ - this(prefix,true); - } - - public NamedThreadFactory(String prefix,boolean daemon) { - SecurityManager manager = System.getSecurityManager(); - if(manager!=null){ - group = manager.getThreadGroup(); - }else{ - group = Thread.currentThread().getThreadGroup(); - } - isDaemon = daemon; - namePre = prefix+"-"+poolNum.getAndIncrement()+"-thread-"; - } - - /** - * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 - */ - @Override - public Thread newThread(Runnable runnable) { - Thread t = new Thread(group, runnable,namePre+threadNum.getAndIncrement(),0); - t.setContextClassLoader(NamedThreadFactory.class.getClassLoader()); - t.setPriority(Thread.MAX_PRIORITY); - t.setDaemon(isDaemon); - return t; - } +import static com.mpush.tools.thread.ThreadNames.THREAD_NAME_PREFIX; +/** + * Created by xiaoxu.yxx on 2015/7/19. + */ +public final class NamedThreadFactory implements ThreadFactory { + protected final AtomicInteger threadNumber = new AtomicInteger(1); + protected final String namePrefix; + protected final ThreadGroup group; + + + public NamedThreadFactory() { + this(THREAD_NAME_PREFIX); + } + + public NamedThreadFactory(final String namePrefix) { + this.namePrefix = namePrefix; + this.group = Thread.currentThread().getThreadGroup(); + } + + public Thread newThread(String name, Runnable r) { + return new Thread(group, r, name); + } + + @Override + public Thread newThread(Runnable r) { + Thread t = newThread(namePrefix + threadNumber.getAndIncrement(), r); + if (t.isDaemon()) + t.setDaemon(false); + return t; + } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java new file mode 100644 index 00000000..9de80e94 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java @@ -0,0 +1,42 @@ +package com.mpush.tools.thread; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class PoolThreadFactory implements ThreadFactory { + private static final AtomicInteger poolNum = new AtomicInteger(1); + + private final AtomicInteger threadNum = new AtomicInteger(1); + + private final ThreadGroup group; + private final String namePre; + private final boolean isDaemon; + + public PoolThreadFactory(String prefix) { + this(prefix, true); + } + + public PoolThreadFactory(String prefix, boolean daemon) { + SecurityManager manager = System.getSecurityManager(); + if (manager != null) { + group = manager.getThreadGroup(); + } else { + group = Thread.currentThread().getThreadGroup(); + } + isDaemon = daemon; + namePre = prefix + "p-" + poolNum.getAndIncrement() + "-t-"; + } + + /** + * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 + */ + @Override + public Thread newThread(Runnable runnable) { + Thread t = new Thread(group, runnable, namePre + threadNum.getAndIncrement(), 0); + t.setContextClassLoader(PoolThreadFactory.class.getClassLoader()); + t.setPriority(Thread.MAX_PRIORITY); + t.setDaemon(isDaemon); + return t; + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java deleted file mode 100644 index 63fe7d72..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.tools.thread; - -public class ThreadNameSpace { - - /** - * netty boss 线程 - */ - public static final String NETTY_BOSS = "mp-boss"; - - /** - * netty worker 线程 - */ - public static final String NETTY_WORKER = "mp-worker"; - - public static final String NETTY_HTTP = "mp-http"; - - public static final String EVENT_BUS = "mp-event-bus"; - - public static final String REDIS = "mp-redis"; - - public static final String ZK = "mp-zk"; - - public static final String BIZ = "mp-biz"; - - /** - * connection 定期检测线程 - */ - public static final String NETTY_TIMER = "mp-timer"; - - public static final String getUniqueName(String serviceName) { - return "mp-sn-" + serviceName; - } - - public static final String THREAD_NAME_PREFIX = "mp-t-"; - - public static final String getServerName(String serverName){ - if(serverName.equals("ConnectionServer")){ - return "mp-start-cs"; - }else if(serverName.equals("GatewayServer")){ - return "mp-start-gs"; - }else{ - return "mp-start-unknow"; - } - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java new file mode 100644 index 00000000..57ad731f --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -0,0 +1,32 @@ +package com.mpush.tools.thread; + +public final class ThreadNames { + public static final String NS = "mp"; + public static final String THREAD_NAME_PREFIX = NS + "-t-"; + + /** + * netty boss 线程 + */ + public static final String NETTY_BOSS = NS + "-boss-"; + + /** + * netty worker 线程 + */ + public static final String NETTY_WORKER = NS + "-worker-"; + + public static final String NETTY_HTTP = NS + "-http-"; + + public static final String EVENT_BUS = NS + "-event-"; + + public static final String REDIS = NS + "-redis-"; + + public static final String ZK = NS + "-zk-"; + + public static final String BIZ = NS + "-biz-"; + + /** + * connection 定期检测线程 + */ + public static final String NETTY_TIMER = NS + "-timer-"; + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java new file mode 100644 index 00000000..43575e77 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java @@ -0,0 +1,88 @@ +package com.mpush.tools.thread.pool; + +import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.PoolThreadFactory; + +import java.util.concurrent.*; + +import static com.mpush.tools.thread.ThreadNames.*; + +/** + * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 + */ +public class DefaultThreadPoolFactory implements ThreadPoolFactory { + + private Executor get(ThreadPoolConfig config) { + String name = config.getName(); + int corePoolSize = config.getCorePoolSize(); + int maxPoolSize = config.getMaxPoolSize(); + int keepAliveSeconds = config.getKeepAliveSeconds(); + + BlockingQueue queue = config.getQueue(); + ThreadFactory threadFactory = new PoolThreadFactory(name); + + return new ThreadPoolExecutor(corePoolSize + , maxPoolSize + , keepAliveSeconds + , TimeUnit.SECONDS + , queue + , threadFactory + , new DumpThreadRejectedHandler(config)); + } + + @Override + public Executor get(String name) { + final ThreadPoolConfig config; + switch (name) { + case SERVER_BOSS: + config = ThreadPoolConfig + .build(NETTY_BOSS) + .setCorePoolSize(CC.mp.thread.pool.boss.min) + .setMaxPoolSize(CC.mp.thread.pool.boss.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.boss.queue_size); + break; + case SERVER_WORK: + config = ThreadPoolConfig + .build(NETTY_WORKER) + .setCorePoolSize(CC.mp.thread.pool.work.min) + .setMaxPoolSize(CC.mp.thread.pool.work.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.work.queue_size); + break; + case HTTP_CLIENT_WORK: + config = ThreadPoolConfig + .buildFixed(NETTY_HTTP, + CC.mp.thread.pool.http_proxy.min, + CC.mp.thread.pool.http_proxy.queue_size + ); + break; + case EVENT_BUS: + config = ThreadPoolConfig + .buildFixed(EVENT_BUS, + CC.mp.thread.pool.event_bus.min, + CC.mp.thread.pool.event_bus.queue_size + ); + break; + case MQ: + config = ThreadPoolConfig + .buildFixed(MQ, + CC.mp.thread.pool.mq.min, + CC.mp.thread.pool.mq.queue_size + ); + break; + default: + case BIZ: + config = ThreadPoolConfig + .build(BIZ) + .setCorePoolSize(CC.mp.thread.pool.biz.min) + .setMaxPoolSize(CC.mp.thread.pool.biz.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.biz.queue_size); + break; + } + + return get(config); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java new file mode 100644 index 00000000..c31c5b8c --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -0,0 +1,40 @@ +package com.mpush.tools.thread.pool; + +import com.mpush.tools.JVMUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; + +public class DumpThreadRejectedHandler implements RejectedExecutionHandler { + + private final static Logger LOGGER = LoggerFactory.getLogger(DumpThreadRejectedHandler.class); + + private volatile boolean dumping = false; + + private static final String preFixPath = "/tmp/mpush/logs/dump/"; + + private final ThreadPoolConfig context; + + public DumpThreadRejectedHandler(ThreadPoolConfig context) { + this.context = context; + } + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + if (!dumping) { + dumping = true; + dumpJVMInfo(); + } + throw new RejectedExecutionException(); + } + + private void dumpJVMInfo() { + LOGGER.error("start dump jvm info"); + JVMUtil.dumpJstack(preFixPath + "/" + context.getName()); + LOGGER.error("end dump jvm info"); + } +} + diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java new file mode 100644 index 00000000..8e0a9b43 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -0,0 +1,104 @@ +package com.mpush.tools.thread.pool; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; + +public class ThreadPoolConfig { + private String name;//名字 + private int corePoolSize; //最小线程大小 + private int maxPoolSize; //最大线程大小 + private int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) + private int keepAliveSeconds;// 存活时间 + + public ThreadPoolConfig(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public ThreadPoolConfig setName(String name) { + this.name = name; + return this; + } + + public int getCorePoolSize() { + return corePoolSize; + } + + public ThreadPoolConfig setCorePoolSize(int corePoolSize) { + this.corePoolSize = corePoolSize; + return this; + } + + public int getMaxPoolSize() { + return maxPoolSize; + } + + public ThreadPoolConfig setMaxPoolSize(int maxPoolSize) { + this.maxPoolSize = maxPoolSize; + return this; + } + + public int getQueueCapacity() { + return queueCapacity; + } + + public ThreadPoolConfig setQueueCapacity(int queueCapacity) { + this.queueCapacity = queueCapacity; + return this; + } + + public int getKeepAliveSeconds() { + return keepAliveSeconds; + } + + public ThreadPoolConfig setKeepAliveSeconds(long keepAliveSeconds) { + this.keepAliveSeconds = (int) keepAliveSeconds; + return this; + } + + + public static ThreadPoolConfig buildFixed(String name, int threads, int queueCapacity) { + return new ThreadPoolConfig(name) + .setCorePoolSize(threads) + .setMaxPoolSize(threads) + .setQueueCapacity(queueCapacity) + .setKeepAliveSeconds(0); + } + + public static ThreadPoolConfig buildCached(String name) { + return new ThreadPoolConfig(name) + .setKeepAliveSeconds(0); + } + + public static ThreadPoolConfig build(String name) { + return new ThreadPoolConfig(name); + } + + + public BlockingQueue getQueue() { + BlockingQueue blockingQueue; + if (queueCapacity == 0) { + blockingQueue = new SynchronousQueue<>(); + } else if (queueCapacity < 0) { + blockingQueue = new LinkedBlockingQueue<>(); + } else { + blockingQueue = new LinkedBlockingQueue<>(queueCapacity); + } + return blockingQueue; + } + + @Override + public String toString() { + return "ThreadPoolConfig{" + + "name='" + name + '\'' + + ", corePoolSize=" + corePoolSize + + ", maxPoolSize=" + maxPoolSize + + ", queueCapacity=" + queueCapacity + + ", keepAliveSeconds=" + keepAliveSeconds + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java new file mode 100644 index 00000000..64178405 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -0,0 +1,93 @@ +package com.mpush.tools.thread.pool; + +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.tools.thread.NamedThreadFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; + +public class ThreadPoolManager { + public static final ThreadPoolManager I = new ThreadPoolManager(); + + private final ThreadPoolFactory threadPoolFactory = SpiLoader.load(ThreadPoolFactory.class); + private final NamedThreadFactory threadFactory = new NamedThreadFactory(); + + private Executor bossExecutor; + private Executor workExecutor; + private Executor bizExecutor; + private Executor eventBusExecutor; + private Executor redisExecutor; + private Executor httpExecutor; + + public final Thread newThread(String name, Runnable target) { + return threadFactory.newThread(name, target); + } + + public Executor getHttpExecutor() { + if (httpExecutor == null) { + synchronized (this) { + httpExecutor = threadPoolFactory.get(ThreadPoolFactory.HTTP_CLIENT_WORK); + } + } + return httpExecutor; + } + + public Executor getRedisExecutor() { + if (redisExecutor == null) { + synchronized (this) { + redisExecutor = threadPoolFactory.get(ThreadPoolFactory.MQ); + } + } + return redisExecutor; + } + + public Executor getEventBusExecutor() { + if (eventBusExecutor == null) { + synchronized (this) { + eventBusExecutor = threadPoolFactory.get(ThreadPoolFactory.EVENT_BUS); + } + } + return eventBusExecutor; + } + + public Executor getBizExecutor() { + if (bizExecutor == null) { + synchronized (this) { + bizExecutor = threadPoolFactory.get(ThreadPoolFactory.BIZ); + } + } + return bizExecutor; + } + + public Executor getWorkExecutor() { + if (workExecutor == null) { + synchronized (this) { + workExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_WORK); + } + } + return workExecutor; + } + + public Executor getBossExecutor() { + if (bossExecutor == null) { + synchronized (this) { + bossExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_BOSS); + } + } + return bossExecutor; + } + + public Map getActivePools() { + Map map = new HashMap<>(); + if (bossExecutor != null) map.put("bossExecutor", bossExecutor); + if (workExecutor != null) map.put("workExecutor", workExecutor); + if (bizExecutor != null) map.put("bizExecutor", bizExecutor); + if (eventBusExecutor != null) map.put("eventBusExecutor", eventBusExecutor); + if (redisExecutor != null) map.put("redisExecutor", redisExecutor); + if (httpExecutor != null) map.put("httpExecutor", httpExecutor); + return map; + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java deleted file mode 100644 index 88255500..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadPoolExecutor; - -import com.mpush.tools.JVMUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.config.ConfigCenter; - -public class IgnoreRunsPolicy implements RejectedExecutionHandler{ - - private final static Logger log = LoggerFactory.getLogger(IgnoreRunsPolicy.class); - - private volatile boolean dump = false; - - private static final String preFixPath = ConfigCenter.I.logPath(); - - private final ThreadPoolContext context; - - public IgnoreRunsPolicy(ThreadPoolContext context) { - this.context = context; - } - - @Override - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - dumpJVMInfo(); - throw new RejectedExecutionException(); - } - - private void dumpJVMInfo(){ - if (!dump) { - dump = true; - log.error("start dump jvm info"); - JVMUtil.dumpJstack(preFixPath+"/"+context.getName()); - log.error("end dump jvm info"); - } - } -} - diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java deleted file mode 100644 index da079304..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import java.util.concurrent.Executor; - -import com.mpush.tools.spi.SPI; - -@SPI("cachedThreadPool") -public interface ThreadPool { - - public Executor getExecutor(ThreadPoolContext context); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java deleted file mode 100644 index 5823bad0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import com.mpush.tools.Constants; -import com.mpush.tools.thread.ThreadNameSpace; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; - - -public class ThreadPoolContext { - - private final String name;//名字 - private final int corePoolSize; //最小线程大小 - private final int maxPoolSize; //最大线程大小 - private final int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) - private final int keepAliveSeconds;// 存活时间 - - public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POOL_SIZE, 60*5,Constants.BOSS_THREAD_QUEUE_SIZE); - - public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5,Constants.WORK_THREAD_QUEUE_SIZE); - - public static ThreadPoolContext HTTP_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.NETTY_HTTP, Constants.BIZ_POOL_SIZE); - - public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); - - public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); - - public static ThreadPoolContext REDIS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.REDIS, Constants.REDIS_POOL_SIZE,Constants.REDIS_THREAD_QUEUE_SIZE); - - public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { - this.name = name; - this.corePoolSize = corePoolSize; - this.maxPoolSize = maxPoolSize; - this.queueCapacity = queueCapacity; - this.keepAliveSeconds = keepAliveSeconds; - } - - public String getName() { - return name; - } - - public int getCorePoolSize() { - return corePoolSize; - } - - public int getMaxPoolSize() { - return maxPoolSize; - } - - public int getQueueCapacity() { - return queueCapacity; - } - - public int getKeepAliveSeconds() { - return keepAliveSeconds; - } - - @Override - public String toString() { - return "ThreadPoolContext [name=" + name + ", corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity + ", keepAliveSeconds=" + keepAliveSeconds - + "]"; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java deleted file mode 100644 index db2b1d79..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Executor; - -public class ThreadPoolManager { - - private static final Map poolCache = new HashMap(); - - private static ThreadPool cachedThreadPool = new CachedThreadPool(); - - private static ThreadPool fixedThreadPool = new FixedThreadPool(); - - public static Executor bossExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.BOSS_THREAD_POOL); - public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); - public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); - public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); - public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); - - public static Executor httpExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); - - static { - poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); - poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); - poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); - poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); - poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); - poolCache.put(ThreadPoolContext.HTTP_THREAD_POOL.getName(), httpExecutor); - } - - public static final Map getPool() { - return poolCache; - } - - public static final Thread newThread(String name, Runnable target) { - Thread thread = new Thread(target, name); - return thread; - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java deleted file mode 100644 index e432d46d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.tools.thread.threadpool.cached; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.mpush.tools.thread.threadpool.ThreadPoolContext; -import com.mpush.tools.thread.threadpool.ThreadPool; - -/** - * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 - * - */ -public class CachedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - - String name = context.getName(); - int corePoolSize = context.getCorePoolSize(); - int maxPoolSize = context.getMaxPoolSize(); - int queueCapacity = context.getQueueCapacity(); - int keepAliveSeconds = context.getKeepAliveSeconds(); - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - BlockingQueue blockingQueue = null; - if(queueCapacity == 0){ - blockingQueue = new SynchronousQueue(); - }else if(queueCapacity<0){ - blockingQueue = new LinkedBlockingQueue(); - }else{ - blockingQueue = new LinkedBlockingQueue(queueCapacity); - } - - return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java deleted file mode 100644 index def69d45..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.tools.thread.threadpool.cached; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -public class CachedThreadPoolContext extends ThreadPoolContext { - - public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) { - this(name, corePoolSize, maxPoolSize, keepAliveSeconds, 0); - } - - public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity) { - super(name, corePoolSize, maxPoolSize, queueCapacity, keepAliveSeconds); - } - - public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds){ - return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds); - } - - public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity){ - return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds,queueCapacity); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java deleted file mode 100644 index 743ec71d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mpush.tools.thread.threadpool.fixed; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.mpush.tools.thread.threadpool.ThreadPool; -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -/** - *此线程池启动时即创建固定大小的线程数,不做任何伸缩 - * - */ -public class FixedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - String name = context.getName(); - int corePoolSize = context.getCorePoolSize(); - int queueCapacity = context.getQueueCapacity(); - - BlockingQueue blockingQueue = null; - if (queueCapacity == 0) { - blockingQueue = new SynchronousQueue(); - } else if (queueCapacity < 0) { - blockingQueue = new LinkedBlockingQueue(); - } else { - blockingQueue = new LinkedBlockingQueue(queueCapacity); - } - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java deleted file mode 100644 index 09cc9305..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.tools.thread.threadpool.fixed; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -public class FixedThreadPoolContext extends ThreadPoolContext { - - public FixedThreadPoolContext(String name, int threads,int queueCapacity) { - super(name, threads, 0, queueCapacity, 0); - } - - public FixedThreadPoolContext(String name, int threads) { - super(name, threads, 0, -1, 0); - } - - public static FixedThreadPoolContext create(String name,int threads){ - return new FixedThreadPoolContext(name, threads); - } - - public static FixedThreadPoolContext create(String name,int threads,int queueCapacity){ - return new FixedThreadPoolContext(name, threads,queueCapacity); - } - -} diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory new file mode 100644 index 00000000..be69a4a6 --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory @@ -0,0 +1 @@ +com.mpush.tools.thread.pool.DefaultThreadPoolFactory \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister b/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister deleted file mode 100644 index ff33af00..00000000 --- a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister +++ /dev/null @@ -1 +0,0 @@ -com.mpush.tools.redis.jedis.services.JedisRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/main/resources/logback.xml b/mpush-tools/src/main/resources/logback.xml new file mode 100644 index 00000000..283b7297 --- /dev/null +++ b/mpush-tools/src/main/resources/logback.xml @@ -0,0 +1,220 @@ + + + + + + + + + + + ${log.home}/error.log + + error + ACCEPT + DENY + + true + + ${log.home}/error.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + ${log.home}/info.log + + info + ACCEPT + DENY + + true + + ${log.home}/info.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/mpush.log + true + + ${log.home}/mpush.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + ${log.home}/monitor.log + true + + ${log.home}/monitor.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + + ${log.home}/conn.log + true + + ${log.home}/conn.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + + ${log.home}/push.log + true + + ${log.home}/push.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + + ${log.home}/heartbeat.log + true + + ${log.home}/heartbeat.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + + ${log.home}/redis.log + true + + ${log.home}/redis.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + + ${log.home}/http.log + true + + ${log.home}/http.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + + ${log.home}/zk.log + true + + ${log.home}/zk.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + System.out + UTF-8 + + DEBUG + + + %d{HH:mm:ss.SSS} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java index 36d4b0d6..026cc152 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java @@ -1,6 +1,6 @@ package com.mpush.tools.crypto; -import com.mpush.tools.Constants; +import com.mpush.api.Constants; import org.junit.Test; import java.util.Random; diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java deleted file mode 100644 index f9907f95..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.mpush.tools.owner; - -import com.mpush.tools.config.ConfigCenter; -import org.aeonbits.owner.ConfigFactory; -import org.junit.Test; - -public class OwnerTest { - - @Test - public void test1(){ - - ServerConfig cfg = ConfigFactory.create(ServerConfig.class); - - System.out.println(cfg.zkDigest()); - - System.out.println(cfg.zkIp()); - - System.out.println(cfg.hello()); - - System.out.println(cfg.maxHbTimeoutTimes()); - - System.out.println(cfg.test()); - - Integer tset = cfg.testnotexist(); - if(tset == null){ - System.out.println("not exist"); - }else{ - System.out.println(tset); - } - } - - @Test - public void test2(){ - - System.out.println(ConfigCenter.I.zkIp()); - - System.out.println("aesKeyLength:"+ConfigCenter.I.aesKeyLength()); - - System.out.println("compressLimit:"+ConfigCenter.I.compressLimit()); - - System.out.println("connectionServerPort:"+ConfigCenter.I.connectionServerPort()); - - System.out.println("gatewayServerPort:"+ConfigCenter.I.gatewayServerPort()); - - System.out.println("maxHBTimeoutTimes:"+ConfigCenter.I.maxHBTimeoutTimes()); - - System.out.println(ConfigCenter.I.maxHeartbeat()); - - System.out.println(ConfigCenter.I.maxPacketSize()); - - System.out.println(ConfigCenter.I.minHeartbeat()); - - System.out.println(ConfigCenter.I.privateKey()); - - System.out.println(ConfigCenter.I.publicKey()); - - System.out.println(ConfigCenter.I.rsaKeyLength()); - - System.out.println(ConfigCenter.I.redisIp()); - - System.out.println(ConfigCenter.I.sessionExpiredTime()); - - System.out.println(ConfigCenter.I.zkDigest()); - - - - System.out.println(ConfigCenter.I.zkNamespace()); - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java b/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java deleted file mode 100644 index e3060630..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.mpush.tools.owner; - -import org.aeonbits.owner.Config; -import org.aeonbits.owner.Config.Sources; - - -@Sources({"classpath:serverconfig.properties"}) -public interface ServerConfig extends Config{ - - @Key("zk_ip") - @DefaultValue("zkIp") - public String zkIp(); - - @Key("zk_digest") - public String zkDigest(); - - @Key("hello") - @DefaultValue("hello world") - public String hello(); - - @Key("max_hb_timeout_times") - public int maxHbTimeoutTimes(); - - @Key("test") - @DefaultValue("10") - public int test(); - - public Integer testnotexist(); - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java deleted file mode 100644 index 9cf9a807..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mpush.tools.spi; - - -import com.mpush.tools.spi.test.TestService; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - - -public class SpiTest { - - private static Executor pool = Executors.newCachedThreadPool(); - - @Test - public void baseTest() { - TestService testService = ServiceContainer.load(TestService.class); - System.out.println(testService.sayHi(" huang")); - - ServiceContainer.load(TestService.class); - } - - @Ignore - @Test - public void listTest() { - - - } - - @Ignore - @Test - public void mulThreadTest() throws InterruptedException { - pool.execute(new Worker()); - pool.execute(new Worker()); - pool.execute(new ListWorker()); - pool.execute(new ListWorker()); - Thread.sleep(Integer.MAX_VALUE); - } - - - private static final class Worker implements Runnable { - - @Override - public void run() { - } - - } - - private static final class ListWorker implements Runnable { - - @Override - public void run() { - - - } - - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java index 8460ca51..3e177fdd 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java @@ -1,11 +1,8 @@ package com.mpush.tools.spi.test; -import com.mpush.tools.spi.SPI; - -@SPI("test1") public interface TestService { - public String sayHi(String name); + String sayHi(String name); } diff --git a/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java deleted file mode 100644 index 1e69324d..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.mpush.tools.thread; - - -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.thread.threadpool.ThreadPool; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; - -public class SyncTest { - - //pri MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIvbj8CvXqWcXAeNw7yruM2TZPfIkp2JIbPwpptAMt6t6zoByRLDjPkbkFVbJrmmto5dGLFCQHz9NM24gb5R9U5dotApgCYFabbocIUx+mkUcd+ui+SZ5yTTyXtVhqKBFGqCTEB73S7b5y0xof/r781EZWYA3sh47pNXVYisRh7rAgMBAAECgYARri8NH95qN0sXFV/iUR8qtfB0tqF6UuS0175oMAR+TCRJkAI4YgpHT6m+cKiDncTEWJaPih2W73embiXQxpGpJt0HKegmKF5RiSU8iXjbFQvmlfTRrgo7qLIjgqUxaM0h+ef0p/T3EV+HZ8sk2bHZPd5OzTcAx1UOSgz88VEDEQJBAONTXI88w+cIkeS5uDDCDjV5S5ljQCBmBTlwIp0UCLDZ0KQDFCiOM54ltgcsMrKQFyj2EwTWsevbikTP3KRmXzMCQQCdf78HkzGnGAJUzPchIHvQBC1Q95X002rYPxNrlF86iU7n1fan++xuGDTYnz+eNRKJFEY85SQq0eld0KI57qFpAkAZ9Lu90ydfKthVsGr6jj3HF0ltgyqgSGXSUB5zpwTzBHvRLlTP6KS2KwIkwYQsZU1vrOExDT6VeqTIBJ/h2ZqHAkAW9dKRdiHc7CEa365/Q88I6jL5BL71rAR9deSM4FppnC7GmWiV4KH9AsZhdgW+OJp1JWF/6x+0pllQ9eNQcrtRAkBczJJ5l3BtCE+VToy16DPPQCfyDpb6dmyR4IkTwECWMomz9jnt1fK7nETfBeeP4ySea8jrUCgZdu06iPtoLCAK - //pub MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL24/Ar16lnFwHjcO8q7jNk2T3yJKdiSGz8KabQDLeres6AckSw4z5G5BVWya5praOXRixQkB8/TTNuIG+UfVOXaLQKYAmBWm26HCFMfppFHHfrovkmeck08l7VYaigRRqgkxAe90u2+ctMaH/6+/NRGVmAN7IeO6TV1WIrEYe6wIDAQAB - - final static ThreadPool cachedThreadPool = new CachedThreadPool(); - final static ThreadPool fixedThreadPool = new FixedThreadPool(); - - // 最小线程1,最大线程2,使用sync blockqueue,线程缓存5分钟 - final static ThreadPoolContext cachedThreadPoolContext = CachedThreadPoolContext.create("cached-test", 1, 2, 60*5); - - // 最小线程1,最大线程2,使用LinkedBlockingQueue,线程缓存5分钟 - final static ThreadPoolContext cachedThreadPoolContext2 = CachedThreadPoolContext.create("cached-test2", 1, 2, 60*5,1); - - //线程数1,blockqueue大小为2 - final static ThreadPoolContext fixedThreadPoolContext = FixedThreadPoolContext.create("fix-test",1,2); - - final static Executor cachedExecutor = cachedThreadPool.getExecutor(cachedThreadPoolContext); - - final static Executor fixedExecutor = fixedThreadPool.getExecutor(fixedThreadPoolContext); - - final static Executor cachedExecutor2 = cachedThreadPool.getExecutor(cachedThreadPoolContext2); - - Worker worker1 = new Worker(); - Worker worker2 = new Worker(); - Worker worker3 = new Worker(); - Worker worker4 = new Worker(); - Worker worker5 = new Worker(); - - Monitor monitor = new Monitor(fixedExecutor,cachedExecutor); - - - @Before - public void init(){ - monitor.start(); - } - - /** - * 容量为2,因此最多可以接收三个任务。第四个任务开始抛异常 - */ - @Test - public void testFixed(){ - fixedExecutor.execute(worker1); - fixedExecutor.execute(worker2); - fixedExecutor.execute(worker3); - fixedExecutor.execute(worker4); - fixedExecutor.execute(worker5); - } - - /** - * 最小线程1,最大线程2,没有缓冲队列。所以,第三个任务提交的时候,就已经抛错了。 - */ - @Test - public void testCached(){ - cachedExecutor.execute(worker1); - cachedExecutor.execute(worker2); - cachedExecutor.execute(worker3); - cachedExecutor.execute(worker4); - cachedExecutor.execute(worker5); - } - - /** - * 最小线程1,最大线程2,缓冲队列长度为1。所以,第四个任务提交的时候,就已经抛错了。 - */ - @Test - public void testCached2(){ - cachedExecutor2.execute(worker1); - cachedExecutor2.execute(worker2); - cachedExecutor2.execute(worker3); - cachedExecutor2.execute(worker4); - cachedExecutor2.execute(worker5); - } - -} - - -class Worker implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - log.warn("start run Worker:"+Thread.currentThread().getName()); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - log.warn("end run Worker:"+Thread.currentThread().getName()); - } -} - -class Monitor extends Thread{ - - private static final Logger log = LoggerFactory.getLogger(Monitor.class); - - private ThreadPoolExecutor fixedExecutor; - private ThreadPoolExecutor cachedExecutor; - - public Monitor(Executor fixedExecutor,Executor cachedExecutor) { - this.fixedExecutor = (ThreadPoolExecutor)fixedExecutor; - this.cachedExecutor = (ThreadPoolExecutor)cachedExecutor; - } - - @Override - public void run() { - - while(true){ - StringBuilder sb = new StringBuilder(); - sb.append("[fixedExecutor]"+"coreThreadNums:" + fixedExecutor.getCorePoolSize() + " maxThreadNums:" + fixedExecutor.getMaximumPoolSize() + " activityThreadNums:" - + fixedExecutor.getActiveCount()); - log.error(sb.toString()); - - StringBuilder sb2 = new StringBuilder(); - sb2.append("[cachedExecutor]"+"coreThreadNums:" + cachedExecutor.getCorePoolSize() + " maxThreadNums:" + cachedExecutor.getMaximumPoolSize() + " activityThreadNums:" - + cachedExecutor.getActiveCount()); - log.error(sb2.toString()); - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - - } - - } - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index d727e523..b074ae34 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -1,12 +1,10 @@ package com.mpush.zk; -import com.mpush.log.Logs; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Constants; +import com.mpush.api.Constants; import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; import com.mpush.tools.exception.ZKException; -import com.mpush.zk.listener.ZKDataChangeListener; +import com.mpush.tools.log.Logs; +import com.mpush.zk.listener.ZKNodeCacheWatcher; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; @@ -38,25 +36,20 @@ private synchronized static ZKClient I() { } private ZKClient() { - try { - init(); - } catch (Exception e) { - throw new ZKException("init zk error, config=" + zkConfig, e); - } + init(); } /** * 初始化 */ - private void init() throws Exception { - zkConfig = ZKConfig.build(ConfigCenter.I.zkIp()) - .setDigest(ConfigCenter.I.zkDigest()) - .setNamespace(ConfigCenter.I.zkNamespace()); - ConsoleLog.i("init zk client, config=" + zkConfig); + public void init() { + if (zkConfig != null) return; + zkConfig = ZKConfig.build(); + Logs.Console.info("init zk client, config=" + zkConfig); Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) - .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepMs())) .namespace(zkConfig.getNamespace()); if (zkConfig.getConnectionTimeout() > 0) { @@ -83,15 +76,25 @@ public List getAclForPath(final String path) { } client = builder.build(); client.start(); - ConsoleLog.i("init zk client waiting for connected..."); - if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { - throw new ZKException("init zk error, config=" + zkConfig); + Logs.Console.info("init zk client waiting for connected..."); + try { + if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { + throw new ZKException("init zk error, config=" + zkConfig); + } + initLocalCache(zkConfig.getLocalCachePath()); + } catch (Exception e) { + throw new ZKException("init zk error, config=" + zkConfig, e); } - initLocalCache(zkConfig.getLocalCachePath()); registerConnectionLostListener(); - Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); - ConsoleLog.i("init zk client success..."); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + close(); + } + }); + + Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); + Logs.Console.info("init zk client success..."); } // 注册连接状态监听器 @@ -298,7 +301,7 @@ public void remove(final String key) { } } - public void registerListener(ZKDataChangeListener listener) { + public void registerListener(ZKNodeCacheWatcher listener) { cache.getListenable().addListener(listener); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java index efa19e4d..1a8dc177 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -1,6 +1,6 @@ package com.mpush.zk; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC.mp.zk; public class ZKConfig { public static final int ZK_MAX_RETRY = 3; @@ -16,11 +16,11 @@ public class ZKConfig { private String namespace; - private int maxRetry = ZK_MAX_RETRY; + private int maxRetries = ZK_MAX_RETRY; - private int minTime = ZK_MIN_TIME; + private int baseSleepTimeMs = ZK_MIN_TIME; - private int maxTime = ZK_MAX_TIME; + private int maxSleepMs = ZK_MAX_TIME; private int sessionTimeout = ZK_SESSION_TIMEOUT; @@ -32,8 +32,17 @@ public ZKConfig(String hosts) { this.hosts = hosts; } - public static ZKConfig build(String hosts) { - return new ZKConfig(hosts); + public static ZKConfig build() { + return new ZKConfig(zk.server_address) + .setConnectionTimeout(zk.connectionTimeoutMs) + .setDigest(zk.digest) + .setLocalCachePath(zk.local_cache_path) + .setMaxRetries(zk.retry.maxRetries) + .setMaxSleepMs(zk.retry.maxSleepMs) + .setBaseSleepTimeMs(zk.retry.baseSleepTimeMs) + .setNamespace(zk.namespace) + .setSessionTimeout(zk.sessionTimeoutMs) + ; } public String getHosts() { @@ -54,30 +63,30 @@ public ZKConfig setNamespace(String namespace) { return this; } - public int getMaxRetry() { - return maxRetry; + public int getMaxRetries() { + return maxRetries; } - public ZKConfig setMaxRetry(int maxRetry) { - this.maxRetry = maxRetry; + public ZKConfig setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; return this; } - public int getMinTime() { - return minTime; + public int getBaseSleepTimeMs() { + return baseSleepTimeMs; } - public ZKConfig setMinTime(int minTime) { - this.minTime = minTime; + public ZKConfig setBaseSleepTimeMs(int baseSleepTimeMs) { + this.baseSleepTimeMs = baseSleepTimeMs; return this; } - public int getMaxTime() { - return maxTime; + public int getMaxSleepMs() { + return maxSleepMs; } - public ZKConfig setMaxTime(int maxTime) { - this.maxTime = maxTime; + public ZKConfig setMaxSleepMs(int maxSleepMs) { + this.maxSleepMs = maxSleepMs; return this; } @@ -123,9 +132,9 @@ public String toString() { "hosts='" + hosts + '\'' + ", digest='" + digest + '\'' + ", namespace='" + namespace + '\'' + - ", maxRetry=" + maxRetry + - ", minTime=" + minTime + - ", maxTime=" + maxTime + + ", maxRetries=" + maxRetries + + ", baseSleepTimeMs=" + baseSleepTimeMs + + ", maxSleepMs=" + maxSleepMs + ", sessionTimeout=" + sessionTimeout + ", connectionTimeout=" + connectionTimeout + ", localCachePath='" + localCachePath + '\'' + diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java b/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java deleted file mode 100644 index 1040b0cb..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.zk; - -import com.mpush.tools.spi.SPI; - -import java.util.Collection; - -@SPI("connectionServerManage") -public interface ZKNodeManager { - - void addOrUpdate(String fullPath, T application); - - void remove(String fullPath); - - Collection getList(); - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index c13f1a08..91c5d5ae 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -5,29 +5,29 @@ public enum ZKPath { REDIS_SERVER("/redis", "machine", "redis注册的地方"), - CONNECTION_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), + CONNECT_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), PUSH_SERVER("/ps/hosts", "machine", "push server服务器应用注册的路径"), GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"); - ZKPath(String path, String name, String desc) { - this.path = path; + ZKPath(String root, String name, String desc) { + this.root = root; this.name = name; } - private final String path; + private final String root; private final String name; - public String getPath() { - return path; + public String getRootPath() { + return root; } - public String getWatchPath() { - return path + ZKPaths.PATH_SEPARATOR + name; + public String getNodePath() { + return root + ZKPaths.PATH_SEPARATOR + name; } //根据从zk中获取的app的值,拼装全路径 - public String getFullPath(String nodeName) { - return path + ZKPaths.PATH_SEPARATOR + nodeName; + public String getFullPath(String tail) { + return root + ZKPaths.PATH_SEPARATOR + tail; } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java new file mode 100644 index 00000000..9444b461 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -0,0 +1,20 @@ +package com.mpush.zk.cache; + +import com.mpush.zk.node.ZKNode; + +import java.util.Collection; +import java.util.List; + +public interface ZKNodeCache { + + void addAll(List list); + + void put(String fullPath, T node); + + T remove(String fullPath); + + Collection values(); + + void clear(); + +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java new file mode 100644 index 00000000..8e5fcd41 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java @@ -0,0 +1,43 @@ +package com.mpush.zk.cache; + +import com.mpush.zk.node.ZKRedisNode; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ZKRedisNodeCache implements ZKNodeCache { + + private List nodes = Collections.emptyList(); + + @Override + public void addAll(List list) { + nodes = list; + } + + @Override + public void put(String fullPath, ZKRedisNode node) { + throw new UnsupportedOperationException("can not put one redis node, name=" + fullPath); + } + + @Override + public ZKRedisNode remove(String fullPath) { + nodes = Collections.emptyList(); + return null; + } + + @Override + public Collection values() { + return nodes; + } + + @Override + public void clear() { + nodes = Collections.emptyList(); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java new file mode 100644 index 00000000..911b9bb8 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java @@ -0,0 +1,62 @@ +package com.mpush.zk.cache; + +import com.google.common.collect.Maps; +import com.mpush.zk.node.ZKServerNode; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class ZKServerNodeCache implements ZKNodeCache { + + private final Logger logger = LoggerFactory.getLogger(ZKServerNodeCache.class); + + protected final Map cache = Maps.newConcurrentMap(); + + @Override + public void addAll(List list) { + + } + + @Override + public void put(String fullPath, ZKServerNode node) { + if (StringUtils.isNotBlank(fullPath) && node != null) { + cache.put(fullPath, node); + } else { + logger.error("fullPath is null or application is null"); + } + printList(); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = cache.remove(fullPath); + printList(); + return node; + } + + @Override + public Collection values() { + return Collections.unmodifiableCollection(cache.values()); + } + + @Override + public void clear() { + cache.clear(); + } + + private void printList() { + for (ZKServerNode app : cache.values()) { + logger.warn(app.toString()); + } + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java deleted file mode 100644 index 2945a837..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.mpush.zk.listener; - -import com.mpush.log.Logs; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; - -public abstract class ZKDataChangeListener implements TreeCacheListener { - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - String path = null == event.getData() ? "" : event.getData().getPath(); - if (path.isEmpty()) { - return; - } - Logs.ZK.info("DataChangeListener:{},{},namespace:{}", path, listenerPath(), client.getNamespace()); - if (path.startsWith(listenerPath())) { - dataChanged(client, event, path); - } - } - - public abstract void initData(); - - public abstract void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception; - - public abstract String listenerPath(); -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java new file mode 100644 index 00000000..8c4d4b89 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -0,0 +1,57 @@ +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; + +public abstract class ZKNodeCacheWatcher implements TreeCacheListener { + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + ChildData data = event.getData(); + if (data == null) return; + String path = data.getPath(); + if (Strings.isNullOrEmpty(path)) return; + if (path.startsWith(watchPath())) { + switch (event.getType()) { + case NODE_ADDED: + onNodeAdded(path, data.getData()); + break; + case NODE_REMOVED: + onNodeRemoved(path, data.getData()); + break; + case NODE_UPDATED: + onNodeUpdated(path, data.getData()); + break; + } + } + Logs.ZK.info("ZK node data change, name={}, listener={}, ns={}", path, watchPath(), client.getNamespace()); + } + + public final void beginWatch() { + beforeWatch(); + ZKClient.I.registerListener(this); + } + + public abstract String watchPath(); + + protected void beforeWatch() { + + } + + protected void onNodeAdded(String path, byte[] data) { + + } + + protected void onNodeRemoved(String path, byte[] data) { + + } + + protected void onNodeUpdated(String path, byte[] data) { + + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java deleted file mode 100644 index d7266ad4..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.mpush.zk.listener; - -import com.google.common.base.Strings; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisRegister; -import com.mpush.tools.spi.ServiceContainer; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collections; -import java.util.List; - -/** - * redis 监控 - */ -public class ZKRedisNodeListener extends ZKDataChangeListener { - private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeListener.class); - - private final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - - // 获取redis列表 - private void _initData() { - logger.warn("start init redis data"); - List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); - redisRegister.init(group); - logger.warn("end init redis data"); - } - - private void dataRemove(ChildData data) { - _initData(); - } - - private void dataAddOrUpdate(ChildData data) { - _initData(); - } - - @SuppressWarnings("unchecked") - private List getRedisGroup(String fullPath) { - String rawGroup = ZKClient.I.get(fullPath); - if (Strings.isNullOrEmpty(rawGroup)) - return Collections.EMPTY_LIST; - List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); - if (group == null) - return Collections.EMPTY_LIST; - return group; - } - - @Override - public void initData() { - _initData(); - } - - @Override - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - logger.warn("RedisPathListener other path:" + data + "," + event.getType().name() + "," + data); - } - - } - - @Override - public String listenerPath() { - return ZKPath.REDIS_SERVER.getPath(); - } -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java new file mode 100644 index 00000000..44022e87 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java @@ -0,0 +1,60 @@ +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.Jsons; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKRedisNodeCache; +import com.mpush.zk.node.ZKRedisNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * redis 监控 + */ +public class ZKRedisNodeWatcher extends ZKNodeCacheWatcher { + + private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeWatcher.class); + + private final ZKRedisNodeCache cache = new ZKRedisNodeCache(); + + private void refresh() { + String rawGroup = ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); + logger.warn("refresh zk redis node cache, data=" + rawGroup); + if (Strings.isNullOrEmpty(rawGroup)) return; + ZKRedisNode[] group = Jsons.fromJson(rawGroup, ZKRedisNode[].class); + if (group == null) return; + cache.addAll(Arrays.asList(group)); + } + + @Override + protected void beforeWatch() { + refresh(); + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + refresh(); + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + refresh(); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + refresh(); + } + + @Override + public String watchPath() { + return ZKPath.REDIS_SERVER.getRootPath(); + } + + public ZKRedisNodeCache getCache() { + return cache; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java deleted file mode 100644 index 8bc0a15b..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.mpush.zk.listener; - -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; -import com.mpush.tools.Jsons; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public abstract class ZKServerNodeListener> extends ZKDataChangeListener { - - private final Logger log = LoggerFactory.getLogger(ZKServerNodeListener.class); - - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn(this.getClass().getSimpleName() + "other path:" + path + "," + event.getType().name() + "," + data); - } - } - - public void initData() { - log.warn(" start init " + this.getClass().getSimpleName() + " server data"); - initData0(); - log.warn(" end init " + this.getClass().getSimpleName() + " server data"); - } - - public abstract String getRegisterPath(); - - public abstract String getFullPath(String raw); - - public abstract T getManager(); - - private void initData0() { - // 获取机器列表 - List rawData = ZKClient.I.getChildrenKeys(getRegisterPath()); - for (String raw : rawData) { - String fullPath = getFullPath(raw); - ZKServerNode app = getServerNode(fullPath); - getManager().addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data) { - String path = data.getPath(); - getManager().remove(path); - } - - private void dataAddOrUpdate(ChildData data) { - String path = data.getPath(); - byte[] rawData = data.getData(); - ZKServerNode serverApp = Jsons.fromJson(rawData, ZKServerNode.class); - getManager().addOrUpdate(path, serverApp); - } - - private ZKServerNode getServerNode(String fullPath) { - String rawApp = ZKClient.I.get(fullPath); - ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); - return app; - } - - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java new file mode 100644 index 00000000..f30b6e79 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java @@ -0,0 +1,90 @@ +package com.mpush.zk.listener; + +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public final class ZKServerNodeWatcher extends ZKNodeCacheWatcher { + private final ZKPath path; + private final ZKServerNodeCache cache; + + public static ZKServerNodeWatcher buildConnect() { + return new ZKServerNodeWatcher(ZKPath.CONNECT_SERVER); + } + + public static ZKServerNodeWatcher buildGateway() { + return new ZKServerNodeWatcher(ZKPath.GATEWAY_SERVER); + } + + public static ZKServerNodeWatcher build(ZKPath path, ZKServerNodeCache cache) { + return new ZKServerNodeWatcher(path, cache); + } + + public ZKServerNodeWatcher(ZKPath path) { + this.path = path; + this.cache = new ZKServerNodeCache(); + } + + public ZKServerNodeWatcher(ZKPath path, ZKServerNodeCache cache) { + this.path = path; + this.cache = cache; + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + ZKServerNode serverApp = Jsons.fromJson(data, ZKServerNode.class); + cache.put(path, serverApp); + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + cache.remove(path); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + ZKServerNode serverApp = Jsons.fromJson(data, ZKServerNode.class); + cache.put(path, serverApp); + } + + @Override + public String watchPath() { + return path.getNodePath(); + } + + @Override + protected void beforeWatch() { + Logs.Console.info("start init zk server data"); + List rawData = ZKClient.I.getChildrenKeys(getRegisterPath()); + for (String raw : rawData) { + String fullPath = getFullPath(raw); + ZKServerNode app = getServerNode(fullPath); + cache.put(fullPath, app); + } + Logs.Console.info("end init zk server data"); + } + + public String getRegisterPath() { + return path.getRootPath(); + } + + public String getFullPath(String raw) { + return path.getFullPath(raw); + } + + private ZKServerNode getServerNode(String fullPath) { + String rawApp = ZKClient.I.get(fullPath); + ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); + return app; + } + + public ZKServerNodeCache getCache() { + return cache; + } + +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java new file mode 100644 index 00000000..1d5fff0c --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -0,0 +1,10 @@ +package com.mpush.zk.node; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public interface ZKNode { + String encode(); +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java new file mode 100644 index 00000000..4a4676a0 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -0,0 +1,26 @@ +package com.mpush.zk.node; + +import com.mpush.tools.config.data.RedisGroup; +import com.mpush.tools.Jsons; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ZKRedisNode extends RedisGroup implements ZKNode { + private transient String zkPath; + + public String getZkPath() { + return zkPath; + } + + public void setZkPath(String zkPath) { + this.zkPath = zkPath; + } + + @Override + public String encode() { + return Jsons.toJson(this); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java similarity index 77% rename from mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java rename to mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index b56dba59..f833c37b 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -1,13 +1,16 @@ -package com.mpush.zk; +package com.mpush.zk.node; +import com.mpush.tools.Jsons; import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.ConfigManager; +import com.mpush.zk.ZKPath; /** * 系统配置 */ -public class ZKServerNode { +public class ZKServerNode implements ZKNode { private String ip; @@ -29,16 +32,16 @@ public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { public static ZKServerNode csNode() { return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.I.connectionServerPort(), - MPushUtil.getExtranetAddress(), - ZKPath.CONNECTION_SERVER.getWatchPath()); + CC.mp.net.connect_server_port, + ConfigManager.I.getPublicIp(), + ZKPath.CONNECT_SERVER.getNodePath()); } public static ZKServerNode gsNode() { return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.I.gatewayServerPort(), + CC.mp.net.gateway_server_port, null, - ZKPath.GATEWAY_SERVER.getWatchPath()); + ZKPath.GATEWAY_SERVER.getNodePath()); } public String getIp() { @@ -99,10 +102,15 @@ public int hashCode() { @Override public String toString() { return "ZKServerNode{" + - "ip='" + ip + '\'' + + "host='" + ip + '\'' + ", port=" + port + ", extranetIp='" + extranetIp + '\'' + ", zkPath='" + zkPath + '\'' + '}'; } + + @Override + public String encode() { + return Jsons.toJson(this); + } } diff --git a/pom.xml b/pom.xml index da4d2d61..64f9cf95 100644 --- a/pom.xml +++ b/pom.xml @@ -40,28 +40,28 @@ mpush-client mpush-test mpush-monitor - mpush-log mpush-zk + mpush-cache UTF-8 UTF-8 UTF-8 - 1.7 + 1.8 com.mpush - 0.0.1 - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} + 0.0.1 + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} 5.0.0.Alpha2 linux-x86_64 @@ -127,60 +127,60 @@ + + ${mpush.groupId} + mpush-test + ${mpush.version} + ${mpush.groupId} mpush-api - ${mpush-api-version} + ${mpush.version} ${mpush.groupId} mpush-tools - ${mpush-tools-version} + ${mpush.version} ${mpush.groupId} mpush-common - ${mpush-common-version} + ${mpush.version} ${mpush.groupId} mpush-netty - ${mpush-netty-version} + ${mpush.version} ${mpush.groupId} mpush-core - ${mpush-core-version} + ${mpush.version} ${mpush.groupId} mpush-client - ${mpush-client-version} + ${mpush.version} ${mpush.groupId} mpush-monitor - ${mpush-monitor-version} + ${mpush.version} ${mpush.groupId} - mpush-log - ${mpush-log-version} - - - ${mpush.groupId} - mpush-test - ${mpush-test-version} + mpush-boot + ${mpush.version} ${mpush.groupId} - mpush-boot - ${mpush-boot-version} + mpush-zk + ${mpush.version} ${mpush.groupId} - mpush-zk - ${mpush-zk-version} + mpush-cache + ${mpush.version} @@ -204,18 +204,14 @@ commons-logging commons-logging 1.1.3 + provided log4j log4j 1.2.17 + provided - - org.logback-extensions - logback-ext-spring - 0.1.2 - - @@ -234,31 +230,31 @@ gson 2.5 - org.aeonbits.owner owner 1.0.9 - + + com.typesafe + config + 1.3.0 + org.codehaus.janino janino 2.7.8 - args4j args4j 2.33 - - commons-net - commons-net - 3.4 + net.sf.jopt-simple + jopt-simple + 5.0.1 - @@ -303,12 +299,6 @@ - - daily - - daily - - dev @@ -319,15 +309,9 @@ - pre - - pre - - - - online + pub - online + pub From 5fd3168ce59d88dad708534778f3239482eeb446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 22 May 2016 16:42:12 +0200 Subject: [PATCH 546/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/conf-dev.properties | 26 ------ conf/conf-pub.properties | 15 ---- .../src/main/resources/logback.xml | 89 +++++++++---------- .../main/java/com/mpush/tools/log/Logs.java | 16 ++-- 4 files changed, 51 insertions(+), 95 deletions(-) rename {mpush-tools => mpush-boot}/src/main/resources/logback.xml (80%) diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties index 6d29e48a..e69de29b 100644 --- a/conf/conf-dev.properties +++ b/conf/conf-dev.properties @@ -1,26 +0,0 @@ -#日志根目录 -log.home=/tmp/logs/mpush -#日志级别 -loglevel=debug -#zk配置 -zk_ip=127.0.0.1:2181 -zk_digest=shinemoIpo -zk_namespace=mpush-daily -#redis配置多个用,隔开 -redis_group=111.1.57.148:6379:ShineMoIpo -#私钥 -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -#公钥 -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info=true -#长连服务端口 -connection_server_port=3000 -#网关服务端口 -gateway_server_port=4000 -#控制台服务端口 -admin_port=4001 -skip_dump=true -dns_mapping=111.1.57.148=127.0.0.1 -#本机Ip和外网Ip的映射 -remote_ip_mapping=127.0.0.1:111.1.57.148 - diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties index a5e35416..e69de29b 100644 --- a/conf/conf-pub.properties +++ b/conf/conf-pub.properties @@ -1,15 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=debug -zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -zk_digest = shinemoIpo -zk_namespace = mpush-online -redis_group = 10.161.223.238:6379:ShineMoIpo -private_key = MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB -force_write_redis_group_info = false -connection_server_port = 3000 -gateway_server_port = 4000 -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 -skip_dump=false -admin_port=4001 diff --git a/mpush-tools/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml similarity index 80% rename from mpush-tools/src/main/resources/logback.xml rename to mpush-boot/src/main/resources/logback.xml index 283b7297..7b8ebb77 100644 --- a/mpush-tools/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -1,5 +1,6 @@ + @@ -7,26 +8,23 @@ - - ${log.home}/error.log - - error - ACCEPT - DENY - + + + ${log.home}/mpush.log true - ${log.home}/error.log.%d{yyyy-MM-dd} + ${log.home}/mpush.log.%d{yyyy-MM-dd} - 3 + 10 %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + - ${log.home}/info.log + ${log.home}/info-mpush.log info ACCEPT @@ -34,7 +32,7 @@ true - ${log.home}/info.log.%d{yyyy-MM-dd} + ${log.home}/info-mpush.log.%d{yyyy-MM-dd} 3 @@ -43,14 +41,19 @@ - - - ${log.home}/mpush.log + + + ${log.home}/debug-mpush.log + + debug + ACCEPT + DENY + true - ${log.home}/mpush.log.%d{yyyy-MM-dd} + ${log.home}/debug-mpush.log.%d{yyyy-MM-dd} - 10 + 3 %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -59,11 +62,10 @@ - - ${log.home}/monitor.log + ${log.home}/monitor-mpush.log true - ${log.home}/monitor.log.%d{yyyy-MM-dd} + ${log.home}/monitor-mpush.log.%d{yyyy-MM-dd} 5 @@ -74,11 +76,10 @@ - - ${log.home}/conn.log + ${log.home}/conn-mpush.log true - ${log.home}/conn.log.%d{yyyy-MM-dd} + ${log.home}/conn-mpush.log.%d{yyyy-MM-dd} 30 @@ -89,11 +90,10 @@ - - ${log.home}/push.log + ${log.home}/push-mpush.log true - ${log.home}/push.log.%d{yyyy-MM-dd} + ${log.home}/push-mpush.log.%d{yyyy-MM-dd} 30 @@ -104,11 +104,10 @@ - - ${log.home}/heartbeat.log + ${log.home}/heartbeat-mpush.log true - ${log.home}/heartbeat.log.%d{yyyy-MM-dd} + ${log.home}/heartbeat-mpush.log.%d{yyyy-MM-dd} 30 @@ -119,11 +118,10 @@ - - ${log.home}/redis.log + ${log.home}/redis-mpush.log true - ${log.home}/redis.log.%d{yyyy-MM-dd} + ${log.home}/redis-mpush.log.%d{yyyy-MM-dd} 5 @@ -132,13 +130,12 @@ - + - - ${log.home}/http.log + ${log.home}/http-mpush.log true - ${log.home}/http.log.%d{yyyy-MM-dd} + ${log.home}/http-mpush.log.%d{yyyy-MM-dd} 5 @@ -149,11 +146,10 @@ - - ${log.home}/zk.log + ${log.home}/zk-mpush.log true - ${log.home}/zk.log.%d{yyyy-MM-dd} + ${log.home}/zk-mpush.log.%d{yyyy-MM-dd} 10 @@ -173,42 +169,41 @@ - - + - + - + - + - + - + - + - + diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index 0529e67c..70a6456f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -14,6 +14,7 @@ public interface Logs { boolean logInited = init(); static boolean init() { + if (logInited) return true; System.setProperty("log.home", CC.mp.log_dir); System.setProperty("log.root.level", CC.mp.log_level); LoggerFactory @@ -25,17 +26,18 @@ static boolean init() { } Logger Console = LoggerFactory.getLogger("console"), - Conn = LoggerFactory.getLogger("connLog"), - Monitor = LoggerFactory.getLogger("monitorLog"), + Conn = LoggerFactory.getLogger("mpush.conn.log"), - PUSH = LoggerFactory.getLogger("pushLog"), + Monitor = LoggerFactory.getLogger("mpush.monitor.log"), - HB = LoggerFactory.getLogger("heartbeatLog"), + PUSH = LoggerFactory.getLogger("mpush.push.log"), - REDIS = LoggerFactory.getLogger("redisLog"), + HB = LoggerFactory.getLogger("mpush.heartbeat.log"), - ZK = LoggerFactory.getLogger("zkLog"), + REDIS = LoggerFactory.getLogger("mpush.redis.log"), - HTTP = LoggerFactory.getLogger("httpLog"); + ZK = LoggerFactory.getLogger("mpush.zk.log"), + + HTTP = LoggerFactory.getLogger("mpush.http.log"); } From cfd1b18f2b2d742f7290db822ce1aa53d9fbb18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 23 May 2016 03:26:13 +0200 Subject: [PATCH 547/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=8A=A0=E5=85=A5=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.sh | 11 +++++++---- bin/set-env.sh | 2 ++ mpush-boot/assembly.xml | 2 +- mpush-boot/pom.xml | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 bin/set-env.sh diff --git a/bin/mp.sh b/bin/mp.sh index ca02e9fe..943428c3 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -76,7 +76,7 @@ else MPMAIN="" fi -MPMAIN="$MPMAIN -jar $MPUSH_PREFIX/boot.jar" +MPMAIN="$MPMAIN -jar $MPBINDIR/boot.jar" if [ "x$SERVER_JVMFLAGS" != "x" ] then @@ -174,8 +174,11 @@ start-foreground) -cp "$CLASSPATH" $JVMFLAGS $MPMAIN "-Dmp.conf=$MPCFG" ;; print-cmd) - echo "\"$JAVA\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" - echo -cp \"$CLASSPATH\" $JVMFLAGS $MPMAIN \"-Dmp.conf=$MPCFG\" > \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" + echo "\"$JAVA\" $MPMAIN " + echo "\"-Dmp.conf=$MPCFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " + echo "$JVMFLAGS " + echo "-cp \"$CLASSPATH\" " + echo "> \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" ;; stop) echo -n "Stopping mpush ... " @@ -199,7 +202,7 @@ upgrade) restart) shift "$0" stop ${@} - sleep 3 + sleep 5 "$0" start ${@} ;; status) diff --git a/bin/set-env.sh b/bin/set-env.sh new file mode 100644 index 00000000..5325b967 --- /dev/null +++ b/bin/set-env.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +#JVMFLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index 4b65b52d..ff218037 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -39,7 +39,7 @@ target/ - + bin boot.jar diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 90aeaa6b..7fe8c171 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -53,7 +53,7 @@ true - lib/ + ../lib/ com.mpush.boot.Main From 29d9e039e85e96797552a3cd10c7d3e1399b20c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 23 May 2016 03:44:15 +0200 Subject: [PATCH 548/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=8A=A0=E5=85=A5=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.sh | 2 +- mpush-boot/assembly.xml | 4 ++-- mpush-boot/pom.xml | 4 ++-- .../src/main/java/com/mpush/{boot => bootstrap}/Main.java | 3 ++- .../java/com/mpush/{boot => bootstrap}/ServerLauncher.java | 4 ++-- .../java/com/mpush/{boot => bootstrap}/job/BootChain.java | 4 ++-- .../main/java/com/mpush/{boot => bootstrap}/job/BootJob.java | 4 ++-- .../java/com/mpush/{boot => bootstrap}/job/HttpProxyBoot.java | 2 +- .../main/java/com/mpush/{boot => bootstrap}/job/LastBoot.java | 4 ++-- .../java/com/mpush/{boot => bootstrap}/job/MonitorBoot.java | 2 +- .../java/com/mpush/{boot => bootstrap}/job/RedisBoot.java | 2 +- .../java/com/mpush/{boot => bootstrap}/job/ServerBoot.java | 2 +- .../main/java/com/mpush/{boot => bootstrap}/job/ZKBoot.java | 2 +- .../src/test/java/com/mpush/test/sever/ServerTestMain.java | 2 +- 14 files changed, 21 insertions(+), 20 deletions(-) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/Main.java (91%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/ServerLauncher.java (96%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/BootChain.java (83%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/BootJob.java (74%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/HttpProxyBoot.java (92%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/LastBoot.java (78%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/MonitorBoot.java (92%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/RedisBoot.java (88%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/ServerBoot.java (98%) rename mpush-boot/src/main/java/com/mpush/{boot => bootstrap}/job/ZKBoot.java (92%) diff --git a/bin/mp.sh b/bin/mp.sh index 943428c3..c40e2d3d 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -76,7 +76,7 @@ else MPMAIN="" fi -MPMAIN="$MPMAIN -jar $MPBINDIR/boot.jar" +MPMAIN="$MPMAIN -jar $MPBINDIR/bootstrap.jar" if [ "x$SERVER_JVMFLAGS" != "x" ] then diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index ff218037..62afe58e 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -1,6 +1,6 @@ - release + release-${mpush.version} mpush true @@ -41,7 +41,7 @@ target/ bin - boot.jar + bootstrap.jar diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 7fe8c171..cb0b9a07 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -28,7 +28,7 @@ - boot + bootstrap ../conf/conf-${deploy.env}.properties @@ -55,7 +55,7 @@ ../lib/ - com.mpush.boot.Main + com.mpush.bootstrap.Main diff --git a/mpush-boot/src/main/java/com/mpush/boot/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java similarity index 91% rename from mpush-boot/src/main/java/com/mpush/boot/Main.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 0f82640c..97c459a0 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -1,10 +1,11 @@ -package com.mpush.boot; +package com.mpush.bootstrap; import com.mpush.tools.log.Logs; public class Main { public static void main(String[] args) { + Logs.init(); Logs.Console.info("launch app..."); ServerLauncher launcher = new ServerLauncher(); launcher.start(); diff --git a/mpush-boot/src/main/java/com/mpush/boot/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java similarity index 96% rename from mpush-boot/src/main/java/com/mpush/boot/ServerLauncher.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 2bbc8434..eac70bfc 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -1,8 +1,8 @@ -package com.mpush.boot; +package com.mpush.bootstrap; import com.mpush.api.Server; -import com.mpush.boot.job.*; +import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.GatewayServer; diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java similarity index 83% rename from mpush-boot/src/main/java/com/mpush/boot/job/BootChain.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index 752c1dba..d3c6186c 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.tools.log.Logs; @@ -22,7 +22,7 @@ private BootJob first() { return new BootJob() { @Override public void run() { - Logs.Console.info("begin run boot chain..."); + Logs.Console.info("begin run bootstrap chain..."); next(); } }; diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java similarity index 74% rename from mpush-boot/src/main/java/com/mpush/boot/job/BootJob.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java index 7903bc5c..070afca1 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.tools.log.Logs; @@ -14,7 +14,7 @@ public abstract class BootJob { public void next() { if (next != null) { - Logs.Console.info("run next boot [" + next.getClass().getSimpleName() + "]"); + Logs.Console.info("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); next.run(); } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java similarity index 92% rename from mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index 823229dd..3ba92423 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.common.net.DnsMappingManager; import com.mpush.tools.config.CC; diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java similarity index 78% rename from mpush-boot/src/main/java/com/mpush/boot/job/LastBoot.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index c86230f1..cd679053 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.common.user.UserManager; import com.mpush.tools.log.Logs; @@ -12,7 +12,7 @@ public class LastBoot extends BootJob { @Override public void run() { UserManager.INSTANCE.clearUserOnlineData(); - Logs.Console.info("end run boot chain..."); + Logs.Console.info("end run bootstrap chain..."); Logs.Console.info("app start success..."); } } diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java similarity index 92% rename from mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java index 88656e8c..f5880f90 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/MonitorBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.monitor.service.MonitorService; import com.mpush.tools.config.CC; diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java similarity index 88% rename from mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index 9c3f676e..53157075 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.cache.redis.manager.RedisManager; diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java similarity index 98% rename from mpush-boot/src/main/java/com/mpush/boot/job/ServerBoot.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 0b50c370..474d8dfa 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.api.Server; import com.mpush.tools.log.Logs; diff --git a/mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java similarity index 92% rename from mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java index f8835cab..86e2ae95 100644 --- a/mpush-boot/src/main/java/com/mpush/boot/job/ZKBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -1,4 +1,4 @@ -package com.mpush.boot.job; +package com.mpush.bootstrap.job; import com.mpush.api.exception.BootException; import com.mpush.zk.ZKClient; diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java index ac9eac45..09180ec3 100644 --- a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -1,6 +1,6 @@ package com.mpush.test.sever; -import com.mpush.boot.Main; +import com.mpush.bootstrap.Main; /** * Created by yxx on 2016/5/16. From fb4a28e6ff6494d1318d322f103437ba9028def1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 23 May 2016 11:19:15 +0200 Subject: [PATCH 549/890] =?UTF-8?q?=E6=89=93=E5=8C=85=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=8A=A0=E5=85=A5=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 64f9cf95..da369c61 100644 --- a/pom.xml +++ b/pom.xml @@ -255,6 +255,11 @@ jopt-simple 5.0.1 + + com.beust + jcommander + 1.48 + From 7642cc075cb6d53cbdf9f162a0b2d4b22f2a6047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 23 May 2016 11:36:44 +0200 Subject: [PATCH 550/890] Add Apache License Header --- .../main/java/com/mpush/api/BaseService.java | 19 ++++++++++++ .../src/main/java/com/mpush/api/Client.java | 19 ++++++++++++ .../main/java/com/mpush/api/Constants.java | 19 ++++++++++++ .../src/main/java/com/mpush/api/Message.java | 19 ++++++++++++ .../java/com/mpush/api/MessageHandler.java | 19 ++++++++++++ .../java/com/mpush/api/PacketReceiver.java | 19 ++++++++++++ .../src/main/java/com/mpush/api/Server.java | 19 ++++++++++++ .../src/main/java/com/mpush/api/Service.java | 19 ++++++++++++ .../java/com/mpush/api/connection/Cipher.java | 19 ++++++++++++ .../com/mpush/api/connection/Connection.java | 19 ++++++++++++ .../api/connection/ConnectionManager.java | 19 ++++++++++++ .../mpush/api/connection/SessionContext.java | 19 ++++++++++++ .../mpush/api/event/ConnectionCloseEvent.java | 19 ++++++++++++ .../main/java/com/mpush/api/event/Event.java | 19 ++++++++++++ .../com/mpush/api/event/HandshakeEvent.java | 19 ++++++++++++ .../com/mpush/api/event/KickUserEvent.java | 19 ++++++++++++ .../mpush/api/event/RouterChangeEvent.java | 19 ++++++++++++ .../com/mpush/api/event/UserOfflineEvent.java | 19 ++++++++++++ .../com/mpush/api/event/UserOnlineEvent.java | 19 ++++++++++++ .../mpush/api/exception/BootException.java | 19 ++++++++++++ .../mpush/api/exception/CryptoException.java | 19 ++++++++++++ .../mpush/api/exception/DecodeException.java | 19 ++++++++++++ .../mpush/api/exception/MessageException.java | 19 ++++++++++++ .../api/exception/SendMessageException.java | 19 ++++++++++++ .../java/com/mpush/api/protocol/Command.java | 19 ++++++++++++ .../java/com/mpush/api/protocol/Packet.java | 19 ++++++++++++ .../java/com/mpush/api/push/PushContent.java | 19 ++++++++++++ .../java/com/mpush/api/push/PushSender.java | 19 ++++++++++++ .../com/mpush/api/router/ClientLocation.java | 19 ++++++++++++ .../java/com/mpush/api/router/Router.java | 19 ++++++++++++ .../com/mpush/api/router/RouterManager.java | 19 ++++++++++++ .../main/java/com/mpush/api/spi/Factory.java | 19 ++++++++++++ .../java/com/mpush/api/spi/SpiLoader.java | 19 ++++++++++++ .../mpush/api/spi/client/PusherFactory.java | 19 ++++++++++++ .../api/spi/common/ThreadPoolFactory.java | 19 ++++++++++++ .../com/mpush/api/spi/core/CipherFactory.java | 19 ++++++++++++ .../mpush/api/spi/net/DnsMappingManager.java | 30 +++++++++++++++++++ .../main/java/com/mpush/bootstrap/Main.java | 19 ++++++++++++ .../com/mpush/bootstrap/ServerLauncher.java | 19 ++++++++++++ .../com/mpush/bootstrap/job/BootChain.java | 19 ++++++++++++ .../java/com/mpush/bootstrap/job/BootJob.java | 19 ++++++++++++ .../mpush/bootstrap/job/HttpProxyBoot.java | 19 ++++++++++++ .../com/mpush/bootstrap/job/LastBoot.java | 19 ++++++++++++ .../com/mpush/bootstrap/job/MonitorBoot.java | 19 ++++++++++++ .../com/mpush/bootstrap/job/RedisBoot.java | 19 ++++++++++++ .../com/mpush/bootstrap/job/ServerBoot.java | 19 ++++++++++++ .../java/com/mpush/bootstrap/job/ZKBoot.java | 19 ++++++++++++ .../com/mpush/cache/redis/RedisClient.java | 19 ++++++++++++ .../com/mpush/cache/redis/RedisGroup.java | 19 ++++++++++++ .../java/com/mpush/cache/redis/RedisKey.java | 19 ++++++++++++ .../com/mpush/cache/redis/RedisServer.java | 19 ++++++++++++ .../cache/redis/hash/ConsistentHash.java | 19 ++++++++++++ .../java/com/mpush/cache/redis/hash/Node.java | 19 ++++++++++++ .../redis/listener/ListenerDispatcher.java | 19 ++++++++++++ .../cache/redis/listener/MessageListener.java | 19 ++++++++++++ .../redis/manager/RedisClusterManager.java | 19 ++++++++++++ .../cache/redis/manager/RedisManager.java | 19 ++++++++++++ .../redis/manager/ZKRedisClusterManager.java | 19 ++++++++++++ .../com/mpush/cache/redis/mq/Subscriber.java | 19 ++++++++++++ .../mpush/client/connect/ClientConfig.java | 19 ++++++++++++ .../connect/ConnClientChannelHandler.java | 19 ++++++++++++ .../mpush/client/connect/ConnectClient.java | 19 ++++++++++++ .../mpush/client/gateway/GatewayClient.java | 19 ++++++++++++ .../client/gateway/GatewayClientBoot.java | 19 ++++++++++++ .../gateway/GatewayClientChannelHandler.java | 19 ++++++++++++ .../client/gateway/GatewayClientFactory.java | 19 ++++++++++++ .../com/mpush/client/push/PushClient.java | 19 ++++++++++++ .../mpush/client/push/PushClientFactory.java | 19 ++++++++++++ .../com/mpush/client/push/PushRequest.java | 19 ++++++++++++ .../com/mpush/client/push/PushRequestBus.java | 19 ++++++++++++ .../main/java/com/mpush/common/ErrorCode.java | 19 ++++++++++++ .../com/mpush/common/MessageDispatcher.java | 19 ++++++++++++ .../common/handler/BaseMessageHandler.java | 19 ++++++++++++ .../common/handler/ErrorMessageHandler.java | 19 ++++++++++++ .../common/handler/OkMessageHandler.java | 19 ++++++++++++ .../com/mpush/common/message/BaseMessage.java | 19 ++++++++++++ .../mpush/common/message/BindUserMessage.java | 19 ++++++++++++ .../mpush/common/message/ByteBufMessage.java | 19 ++++++++++++ .../mpush/common/message/ErrorMessage.java | 19 ++++++++++++ .../common/message/FastConnectMessage.java | 19 ++++++++++++ .../common/message/FastConnectOkMessage.java | 19 ++++++++++++ .../common/message/HandshakeMessage.java | 19 ++++++++++++ .../common/message/HandshakeOkMessage.java | 19 ++++++++++++ .../common/message/HttpRequestMessage.java | 19 ++++++++++++ .../common/message/HttpResponseMessage.java | 19 ++++++++++++ .../mpush/common/message/KickUserMessage.java | 19 ++++++++++++ .../com/mpush/common/message/OkMessage.java | 19 ++++++++++++ .../com/mpush/common/message/PushMessage.java | 19 ++++++++++++ .../message/gateway/GatewayPushMessage.java | 19 ++++++++++++ .../mpush/common/net/DnsMappingManager.java | 19 ++++++++++++ .../router/ConnectionRouterManager.java | 19 ++++++++++++ .../com/mpush/common/router/RemoteRouter.java | 19 ++++++++++++ .../common/router/RemoteRouterManager.java | 19 ++++++++++++ .../common/router/UserChangeListener.java | 19 ++++++++++++ .../com/mpush/common/security/AesCipher.java | 19 ++++++++++++ .../com/mpush/common/security/CipherBox.java | 19 ++++++++++++ .../com/mpush/common/security/RsaCipher.java | 19 ++++++++++++ .../common/security/RsaCipherFactory.java | 19 ++++++++++++ .../com/mpush/common/user/UserManager.java | 19 ++++++++++++ .../com/mpush/core/handler/AdminHandler.java | 19 ++++++++++++ .../mpush/core/handler/BindUserHandler.java | 19 ++++++++++++ .../core/handler/FastConnectHandler.java | 19 ++++++++++++ .../core/handler/GatewayPushHandler.java | 19 ++++++++++++ .../mpush/core/handler/HandshakeHandler.java | 19 ++++++++++++ .../mpush/core/handler/HeartBeatHandler.java | 19 ++++++++++++ .../mpush/core/handler/HttpProxyHandler.java | 19 ++++++++++++ .../mpush/core/handler/UnbindUserHandler.java | 19 ++++++++++++ .../com/mpush/core/router/KickRemoteMsg.java | 19 ++++++++++++ .../com/mpush/core/router/LocalRouter.java | 19 ++++++++++++ .../mpush/core/router/LocalRouterManager.java | 19 ++++++++++++ .../com/mpush/core/router/RouterCenter.java | 19 ++++++++++++ .../core/router/RouterChangeListener.java | 19 ++++++++++++ .../router/UserOnlineOfflineListener.java | 19 ++++++++++++ .../com/mpush/core/server/AdminServer.java | 19 ++++++++++++ .../mpush/core/server/ConnectionServer.java | 19 ++++++++++++ .../com/mpush/core/server/GatewayServer.java | 19 ++++++++++++ .../core/server/ServerChannelHandler.java | 19 ++++++++++++ .../core/server/ServerConnectionManager.java | 19 ++++++++++++ .../mpush/core/session/ReusableSession.java | 19 ++++++++++++ .../core/session/ReusableSessionManager.java | 19 ++++++++++++ .../com/mpush/core/netty/NettyServerTest.java | 19 ++++++++++++ .../mpush/core/security/CipherBoxTest.java | 19 ++++++++++++ .../com/mpush/monitor/data/MonitorResult.java | 19 ++++++++++++ .../mpush/monitor/data/ResultCollector.java | 19 ++++++++++++ .../com/mpush/monitor/quota/BaseQuota.java | 19 ++++++++++++ .../com/mpush/monitor/quota/GCMQuota.java | 19 ++++++++++++ .../com/mpush/monitor/quota/InfoQuota.java | 19 ++++++++++++ .../com/mpush/monitor/quota/MemoryQuota.java | 19 ++++++++++++ .../mpush/monitor/quota/ThreadPoolQuota.java | 19 ++++++++++++ .../com/mpush/monitor/quota/ThreadQuota.java | 19 ++++++++++++ .../com/mpush/monitor/quota/impl/JVMGC.java | 19 ++++++++++++ .../com/mpush/monitor/quota/impl/JVMInfo.java | 19 ++++++++++++ .../mpush/monitor/quota/impl/JVMMemory.java | 19 ++++++++++++ .../mpush/monitor/quota/impl/JVMThread.java | 19 ++++++++++++ .../monitor/quota/impl/JVMThreadPool.java | 19 ++++++++++++ .../mpush/monitor/service/MonitorService.java | 19 ++++++++++++ .../com/mpush/netty/client/NettyClient.java | 19 ++++++++++++ .../com/mpush/netty/codec/PacketDecoder.java | 19 ++++++++++++ .../com/mpush/netty/codec/PacketEncoder.java | 19 ++++++++++++ .../netty/connection/NettyConnection.java | 19 ++++++++++++ .../com/mpush/netty/http/HttpCallback.java | 19 ++++++++++++ .../java/com/mpush/netty/http/HttpClient.java | 19 ++++++++++++ .../com/mpush/netty/http/NettyHttpClient.java | 19 ++++++++++++ .../com/mpush/netty/http/RequestInfo.java | 19 ++++++++++++ .../com/mpush/netty/server/NettyServer.java | 19 ++++++++++++ .../mpush/test/client/ConnClientTestMain.java | 19 ++++++++++++ .../mpush/test/client/ConnectClientBoot.java | 19 ++++++++++++ .../test/configcenter/ConfigCenterTest.java | 19 ++++++++++++ .../mpush/test/connection/body/BodyTest.java | 19 ++++++++++++ .../connection/mpns/ConnClientTestMain.java | 19 ++++++++++++ .../mpns/ConnectTestClientBoot.java | 19 ++++++++++++ .../java/com/mpush/test/crypto/RsaTest.java | 19 ++++++++++++ .../com/mpush/test/gson/DnsMappingTest.java | 19 ++++++++++++ .../java/com/mpush/test/gson/GsonTest.java | 19 ++++++++++++ .../mpush/test/push/PushClientTestMain.java | 19 ++++++++++++ .../mpush/test/redis/ConsistentHashTest.java | 19 ++++++++++++ .../com/mpush/test/redis/MPushUtilTest.java | 19 ++++++++++++ .../java/com/mpush/test/redis/PubSubTest.java | 19 ++++++++++++ .../mpush/test/redis/RedisClusterTest.java | 19 ++++++++++++ .../com/mpush/test/redis/RedisUtilTest.java | 19 ++++++++++++ .../test/java/com/mpush/test/redis/User.java | 19 ++++++++++++ .../com/mpush/test/sever/ServerTestMain.java | 19 ++++++++++++ .../java/com/mpush/test/util/TelnetTest.java | 19 ++++++++++++ .../java/com/mpush/tools/GenericsUtil.java | 19 ++++++++++++ .../main/java/com/mpush/tools/IOUtils.java | 19 ++++++++++++ .../main/java/com/mpush/tools/JVMUtil.java | 19 ++++++++++++ .../src/main/java/com/mpush/tools/Jsons.java | 19 ++++++++++++ .../main/java/com/mpush/tools/MPushUtil.java | 19 ++++++++++++ .../src/main/java/com/mpush/tools/Pair.java | 19 ++++++++++++ .../main/java/com/mpush/tools/Profiler.java | 19 ++++++++++++ .../main/java/com/mpush/tools/Strings.java | 19 ++++++++++++ .../main/java/com/mpush/tools/config/CC.java | 19 ++++++++++++ .../com/mpush/tools/config/ConfigCenter.java | 19 ++++++++++++ .../com/mpush/tools/config/ConfigManager.java | 19 ++++++++++++ .../config/converter/DnsMappingConverter.java | 19 ++++++++++++ .../tools/config/converter/MapConverter.java | 19 ++++++++++++ .../config/converter/RedisGroupConverter.java | 19 ++++++++++++ .../mpush/tools/config/data/DnsMapping.java | 19 ++++++++++++ .../mpush/tools/config/data/RedisGroup.java | 19 ++++++++++++ .../mpush/tools/config/data/RedisServer.java | 19 ++++++++++++ .../java/com/mpush/tools/crypto/AESUtils.java | 19 ++++++++++++ .../java/com/mpush/tools/crypto/Base64.java | 19 ++++++++++++ .../com/mpush/tools/crypto/Base64Utils.java | 19 ++++++++++++ .../java/com/mpush/tools/crypto/MD5Utils.java | 19 ++++++++++++ .../java/com/mpush/tools/crypto/RSAUtils.java | 19 ++++++++++++ .../java/com/mpush/tools/event/EventBus.java | 19 ++++++++++++ .../com/mpush/tools/event/EventConsumer.java | 19 ++++++++++++ .../mpush/tools/exception/ZKException.java | 19 ++++++++++++ .../main/java/com/mpush/tools/log/Logs.java | 19 ++++++++++++ .../tools/thread/NamedThreadFactory.java | 19 ++++++++++++ .../mpush/tools/thread/PoolThreadFactory.java | 19 ++++++++++++ .../com/mpush/tools/thread/ThreadNames.java | 19 ++++++++++++ .../thread/pool/DefaultThreadPoolFactory.java | 19 ++++++++++++ .../pool/DumpThreadRejectedHandler.java | 19 ++++++++++++ .../tools/thread/pool/ThreadPoolConfig.java | 19 ++++++++++++ .../tools/thread/pool/ThreadPoolManager.java | 19 ++++++++++++ .../java/com/mpush/tools/IOUtilsTest.java | 19 ++++++++++++ .../com/mpush/tools/crypto/AESUtilsTest.java | 19 ++++++++++++ .../com/mpush/tools/crypto/RSAUtilsTest.java | 19 ++++++++++++ .../java/com/mpush/tools/delayqueue/Exam.java | 19 ++++++++++++ .../com/mpush/tools/spi/test/TestService.java | 19 ++++++++++++ .../mpush/tools/spi/test/TestServiceImpl.java | 19 ++++++++++++ .../tools/spi/test/TestServiceImpl2.java | 19 ++++++++++++ .../src/main/java/com/mpush/zk/ZKClient.java | 19 ++++++++++++ .../src/main/java/com/mpush/zk/ZKConfig.java | 19 ++++++++++++ .../src/main/java/com/mpush/zk/ZKPath.java | 19 ++++++++++++ .../java/com/mpush/zk/cache/ZKNodeCache.java | 19 ++++++++++++ .../com/mpush/zk/cache/ZKRedisNodeCache.java | 19 ++++++++++++ .../com/mpush/zk/cache/ZKServerNodeCache.java | 19 ++++++++++++ .../mpush/zk/listener/ZKNodeCacheWatcher.java | 19 ++++++++++++ .../mpush/zk/listener/ZKRedisNodeWatcher.java | 19 ++++++++++++ .../zk/listener/ZKServerNodeWatcher.java | 19 ++++++++++++ .../main/java/com/mpush/zk/node/ZKNode.java | 19 ++++++++++++ .../java/com/mpush/zk/node/ZKRedisNode.java | 19 ++++++++++++ .../java/com/mpush/zk/node/ZKServerNode.java | 19 ++++++++++++ 215 files changed, 4096 insertions(+) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java diff --git a/mpush-api/src/main/java/com/mpush/api/BaseService.java b/mpush-api/src/main/java/com/mpush/api/BaseService.java index 4dc12e5e..91f1602f 100644 --- a/mpush-api/src/main/java/com/mpush/api/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/BaseService.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/mpush-api/src/main/java/com/mpush/api/Client.java b/mpush-api/src/main/java/com/mpush/api/Client.java index 332db5cb..4b9ebbf6 100644 --- a/mpush-api/src/main/java/com/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/mpush/api/Client.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; public interface Client extends Service { diff --git a/mpush-api/src/main/java/com/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java index c861f4a3..27e5cece 100644 --- a/mpush-api/src/main/java/com/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import java.nio.charset.Charset; diff --git a/mpush-api/src/main/java/com/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java index cbf37d56..581c48fb 100644 --- a/mpush-api/src/main/java/com/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java index e444949a..ad44841f 100644 --- a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import com.mpush.api.protocol.Packet; diff --git a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java index da0bffe6..0184f285 100644 --- a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import com.mpush.api.protocol.Packet; diff --git a/mpush-api/src/main/java/com/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/Server.java index 7d45390b..608916b7 100644 --- a/mpush-api/src/main/java/com/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/mpush/api/Server.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; /** diff --git a/mpush-api/src/main/java/com/mpush/api/Service.java b/mpush-api/src/main/java/com/mpush/api/Service.java index 25422e39..cc56b819 100644 --- a/mpush-api/src/main/java/com/mpush/api/Service.java +++ b/mpush-api/src/main/java/com/mpush/api/Service.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; /** diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java index e365f656..3751b49b 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; /** diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index 39b15f94..7eee23f2 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; import com.mpush.api.protocol.Packet; diff --git a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index 554486e9..b1447ce5 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; import java.util.List; diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index aa94e9bd..ce6c6333 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java index 2eaf18e7..a31dea4b 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/Event.java b/mpush-api/src/main/java/com/mpush/api/event/Event.java index e1043d2d..8174f047 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/Event.java +++ b/mpush-api/src/main/java/com/mpush/api/event/Event.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java index d7cafa10..a3922ea6 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java index bcb2608b..fc3321cb 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java index 926bb71d..c4c5c024 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.router.Router; diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java index a5087299..68cac550 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java index 6ecb3596..e2682010 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/exception/BootException.java b/mpush-api/src/main/java/com/mpush/api/exception/BootException.java index 52c2621a..97df1842 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/BootException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/BootException.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.exception; /** diff --git a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java index add6460c..6f59c33d 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.exception; /** diff --git a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java index ce7f8435..8d464669 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.exception; /** diff --git a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java index d2dd1a3b..bd20afd7 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.exception; public class MessageException extends RuntimeException { diff --git a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java index 62080127..0f8e84ff 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java +++ b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.exception; /** diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java index 7e2dae4d..ad2e739a 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.protocol; /** diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index b800417c..11e99ca5 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.protocol; import io.netty.buffer.ByteBuf; diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java index 7815b473..8269daf3 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.push; import java.io.Serializable; diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java index 1d70dac3..e64217ea 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.push; import com.mpush.api.Service; diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index c7267088..b77f1529 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; import com.mpush.api.connection.SessionContext; diff --git a/mpush-api/src/main/java/com/mpush/api/router/Router.java b/mpush-api/src/main/java/com/mpush/api/router/Router.java index f5b1582c..acc39a90 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/Router.java +++ b/mpush-api/src/main/java/com/mpush/api/router/Router.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; /** diff --git a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index 70dd37ae..ae07aa15 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; /** diff --git a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java index cf5cd1e8..3e99ce53 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.spi; /** diff --git a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index b9ff34ce..beb2ad45 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.spi; import java.util.Iterator; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java index b0898727..6abcd77c 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.spi.client; import com.mpush.api.push.PushSender; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java index 9e17a790..84573a10 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.spi.common; import java.util.concurrent.Executor; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java index 87ff8af2..b3a486b9 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.spi.core; import com.mpush.api.connection.Cipher; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java new file mode 100644 index 00000000..9abd9264 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java @@ -0,0 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.net; + +import com.mpush.api.Service; + +/** + * Created by yxx on 2016/5/23. + * + * @author ohun@live.cn (夜色) + */ +public interface DnsMappingManager extends Service { +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 97c459a0..f15c560e 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap; import com.mpush.tools.log.Logs; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index eac70bfc..a5035d2e 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index d3c6186c..4e2d1f08 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.tools.log.Logs; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java index 070afca1..bd4fd16f 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.tools.log.Logs; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index 3ba92423..7c023ef5 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.common.net.DnsMappingManager; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index cd679053..cd8935c3 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.common.user.UserManager; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java index f5880f90..808d535f 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.monitor.service.MonitorService; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index 53157075..e98ca3ee 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.cache.redis.manager.RedisManager; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 474d8dfa..c7495fbb 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.api.Server; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java index 86e2ae95..ee51ca9b 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.bootstrap.job; import com.mpush.api.exception.BootException; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 71d02bd5..d1f7d0cc 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis; import com.google.common.collect.Lists; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java index ac920952..cd11ef06 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis; import com.google.common.collect.Lists; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 1830e96c..3ed187e4 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis; public final class RedisKey { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java index d3d82380..60c254a3 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis; /** diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java index dd648861..f1fb4d0b 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.hash; import java.util.Collection; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java index 8b9b55bf..67afb2fb 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.hash; public class Node { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java index 89a6c09a..9dfd2dd3 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.listener; import java.util.List; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java index fcaf3cc6..7ebb2bd7 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.listener; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java index a57fa125..9c90877b 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.manager; import com.mpush.cache.redis.RedisGroup; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index ff9c23ef..f0cc71de 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.manager; import com.google.common.collect.Sets; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index aa7aa168..d171a3a5 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.manager; import com.google.common.base.Strings; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java index 7a0a4ed1..6f8e9c61 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.cache.redis.mq; import com.mpush.cache.redis.listener.ListenerDispatcher; diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java index 1a27275e..ed5e5131 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.connect; diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index c60aedb4..20d01d45 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.connect; diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java index 949edc78..aab6382b 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.connect; import com.mpush.netty.client.NettyClient; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java index 8616f73c..ac56df15 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.gateway; import com.mpush.api.connection.Connection; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java index b880c12b..cd763bd2 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.gateway; import com.mpush.api.connection.Connection; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java index f12eb428..040a54da 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.gateway; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java index c66d4868..089e8999 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.gateway; import com.google.common.collect.Maps; diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 10ed1bcb..133eb404 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.push; import com.google.common.base.Strings; diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java index 98f96f91..5e91639d 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.push; import com.mpush.api.push.PushSender; diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 2156bcee..d12228fe 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.push; import com.google.common.collect.Maps; diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 62f2e880..688060b2 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.client.push; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; diff --git a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java index cd881c08..6aa428b3 100644 --- a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common; /** diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 5c650300..c2aeedb9 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common; import com.mpush.api.MessageHandler; diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index bbd346a0..af2dc2d9 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; diff --git a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java index b6c0c5e9..a451a1c0 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java index 6271a2cf..d537c452 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index bc6c0c42..4996084e 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.Message; diff --git a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index 4638e36a..31f1349f 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index 81e7674a..2682d8f3 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index 435bb0ad..dc8b50b1 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java index e60bd0f7..ed7899f9 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index 2e4982c6..bb027331 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 6b67aea7..1eb1f62e 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index 990cad06..39af35d1 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index bc1edcdd..d9d58ba6 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 5b53aa40..02a414bb 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java index 3c2293df..15571038 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index b48fbc9b..5edcb2d9 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index c8b3b608..3a61f39a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.Constants; diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 328eace3..4e586793 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message.gateway; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java index 44cd3dea..dc22161a 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.net; import com.google.common.collect.Lists; diff --git a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java index 4c548316..f203baf0 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; import com.google.common.cache.Cache; diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index 5f1f78f1..4df7437c 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; import com.mpush.api.router.Router; diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index a1b52e16..e67528f4 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; import com.mpush.cache.redis.RedisKey; diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index 5a2a22dd..e4212060 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; import com.mpush.cache.redis.listener.ListenerDispatcher; diff --git a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 44b2dc7b..70d42d27 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; diff --git a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index cd9dd03a..a8863739 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.tools.config.CC; diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index f5552e6f..82a30052 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java index d6767359..441f5006 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java index 49a3b9c7..28a0ab76 100644 --- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.user; import java.util.List; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index ff247235..95c97007 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.cache.redis.RedisKey; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 1bf5de8c..b6b4268a 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 6d3be522..adff3d7b 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.connection.Connection; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 35f072a7..36f3897c 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.connection.Connection; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index e30e4733..bba3cd4a 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 493e3d31..259996d1 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.MessageHandler; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 916c0ce0..f6a4b285 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index b55a37e2..a6f04b56 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index 71f635e9..3d519a16 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; /** diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index c078ec3f..c1695986 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.mpush.api.connection.Connection; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 8b4c02f4..2f588d25 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index e5ac38d0..f66e0c1b 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.mpush.api.connection.Connection; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index 1512a24e..b8e751e9 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 10ba41f8..b1b1f7ef 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index f4fbf926..ba295468 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; import com.mpush.core.handler.AdminHandler; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 5ac02596..d7a41c21 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 4c1cbadb..f6d1988f 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; import com.mpush.api.protocol.Command; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index eb481393..5e8196b8 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 4cceff92..1d81605b 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java index 7a8e2f72..4336b491 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.session; import com.mpush.api.connection.SessionContext; diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index a665e316..80182395 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.session; import com.mpush.api.connection.SessionContext; diff --git a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java index 27bd38f3..55b7e688 100644 --- a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java +++ b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.netty; import com.mpush.api.Server; diff --git a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java index b48b60a7..818c9719 100644 --- a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.security; import com.mpush.common.security.CipherBox; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java index 5a9d193f..e930038f 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.data; import java.util.Map; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java index 8448d67f..b366abba 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.data; import com.mpush.monitor.quota.impl.*; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java index 6555be2c..4db0aa6e 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; import java.util.Map; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java index 5dca627d..2cddc285 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; public interface GCMQuota { diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java index 23a4b810..89c8f2e2 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; public interface InfoQuota { diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java index 3a368af2..a9e48a20 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; public interface MemoryQuota { diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java index 2396a0e6..16b3e7cf 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; public interface ThreadPoolQuota { diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java index 8d000cdf..0691708b 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java index 1258b497..b84d94fe 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; import com.google.common.collect.Lists; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index 457e6f41..2bcb9dd1 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java index 3df43788..1c9ca2f8 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; import com.google.common.collect.Lists; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index 0c2251c1..51bca628 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; import java.lang.management.ManagementFactory; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index f3464eee..39a00fe1 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; import com.google.common.collect.Maps; diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java index dac32ea4..cea51274 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.service; import com.mpush.api.BaseService; diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 3a827434..6ae5365b 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.client; import com.mpush.api.BaseService; diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 0a6fb191..9a7e90e7 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.codec; import com.mpush.api.exception.DecodeException; diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index 57a887a7..ae6bfed3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.codec; import com.mpush.api.protocol.Command; diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 5c67366f..9573d703 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.connection; import com.mpush.api.connection.Connection; diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java index 74b77915..a628253a 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.http; diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java index 3e3e2cb5..cb94c6ad 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.http; /** diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index e3cf38f7..40bcf2c4 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.http; import com.google.common.collect.ArrayListMultimap; diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java index 70f1bf72..2a023062 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.http; import com.google.common.primitives.Ints; diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 8a395c3d..d948749d 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.server; import com.mpush.api.Server; diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index e9ba4e36..c70e10f3 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.client; import com.mpush.api.Client; diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java index 74db2cb7..20615b46 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.client; import com.google.common.collect.Lists; diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 905b5bc7..97bedcc4 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.configcenter; import com.mpush.tools.config.CC; diff --git a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java index 2a0ed803..2bad14c9 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.connection.body; import java.security.interfaces.RSAPrivateKey; diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java index 4bfcd6ee..45fd7a32 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.connection.mpns; import com.mpush.api.Client; diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java index 4c2adc12..d927e1c6 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.connection.mpns; import com.google.common.collect.Lists; diff --git a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java index aa7e36c5..39c1459d 100644 --- a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java +++ b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.crypto; import java.security.interfaces.RSAPrivateKey; diff --git a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java index 4e67ed76..2734a796 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.gson; import java.util.Map; diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 73388dd2..2f565f57 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.gson; import java.util.Map; diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 7a94d2ff..f15c1eb6 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.push; import com.mpush.api.push.PushContent; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java index 697bedea..17762180 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import java.util.ArrayList; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java index c52c091c..763f83eb 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import com.mpush.tools.MPushUtil; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java index 5775a05f..f3cf52e6 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import com.mpush.cache.redis.RedisGroup; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java index 1fcf8d90..7069153a 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import com.mpush.cache.redis.RedisClient; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index b1ae18c1..89ef5cb3 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import com.google.common.collect.Lists; diff --git a/mpush-test/src/test/java/com/mpush/test/redis/User.java b/mpush-test/src/test/java/com/mpush/test/redis/User.java index 180ebf1e..76aded74 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/User.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/User.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import java.io.Serializable; diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java index 09180ec3..199de67d 100644 --- a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.sever; import com.mpush.bootstrap.Main; diff --git a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java index 69d472a1..ea20f24b 100644 --- a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.util; import java.net.URI; diff --git a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java index 342c312a..36c16940 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import java.lang.reflect.Field; diff --git a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java index 443fb17a..c603c4c6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import com.mpush.api.Constants; diff --git a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java index f7ff57ff..7e991c9f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import java.io.File; diff --git a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java index d863cdd7..cd928e04 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import com.google.gson.Gson; diff --git a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java index ab3b4a9b..aaf8ee94 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import org.slf4j.Logger; diff --git a/mpush-tools/src/main/java/com/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/Pair.java index 23019945..25edf5e4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Pair.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Pair.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; /** diff --git a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/Profiler.java index d4a3d1fb..ff4def8b 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Profiler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; diff --git a/mpush-tools/src/main/java/com/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/mpush/tools/Strings.java index c81fc531..35c8a2fa 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Strings.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; /** diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 0b11edab..f8c8a166 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config; import com.mpush.tools.config.data.DnsMapping; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java index 1b970b31..f9b9eccc 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java index 7ffc6876..fe1b22ff 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config; import com.mpush.tools.MPushUtil; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java index f41b64f6..cb427e00 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config.converter; import com.google.common.base.Function; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java index 0644f7db..d3b2cfcf 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config.converter; import com.google.common.base.Splitter; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java index 33d81262..c2414224 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config.converter; import com.mpush.tools.config.data.RedisGroup; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java index 6970ad06..1460abe9 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config.data; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java index 81064638..30369c96 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config.data; import java.util.Collections; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java index dc5bf913..0e34dff7 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.config.data; /** diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index bc9eb644..6a3c4a39 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import org.slf4j.Logger; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java index 207fd55e..7bd6fbd6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java @@ -23,6 +23,25 @@ * */ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import java.io.FilterOutputStream; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index bf181b51..2f6ade10 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index 2a1f4bce..f483310d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import com.mpush.api.Constants; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index 646335bb..52558c86 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import com.mpush.api.Constants; diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index a5aeb72a..71e004bd 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -1,4 +1,23 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.event; import com.google.common.eventbus.AsyncEventBus; diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java index d83e5914..5f7d6c75 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.event; public abstract class EventConsumer { diff --git a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java b/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java index c5326f53..23adf5a5 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java +++ b/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.exception; /** diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index 70a6456f..5d06ce7a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.log; import com.mpush.tools.config.CC; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 04dc1a1a..4e4f407e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread; import java.util.concurrent.ThreadFactory; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java index 9de80e94..adb945ba 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread; import java.util.concurrent.ThreadFactory; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index 57ad731f..f898f8b8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread; public final class ThreadNames { diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java index 43575e77..bd7a5ebf 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread.pool; import com.mpush.api.spi.common.ThreadPoolFactory; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java index c31c5b8c..9a645d78 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread.pool; import com.mpush.tools.JVMUtil; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java index 8e0a9b43..9297510d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread.pool; import java.util.concurrent.BlockingQueue; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 64178405..12063cde 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread.pool; import com.mpush.api.spi.SpiLoader; diff --git a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java index cdab4c60..a74ea208 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import org.junit.Test; diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java index 026cc152..a57f89dd 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import com.mpush.api.Constants; diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java index 84c7eec6..a740cbe6 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import com.mpush.tools.Pair; diff --git a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java index 45d7a82f..e926d19c 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java +++ b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.delayqueue; import java.util.Iterator; diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java index 3e177fdd..8c5aa2ea 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.spi.test; diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java index a23cf509..27048a43 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.spi.test; import org.slf4j.Logger; diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java index 575e4316..2dcaefec 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java +++ b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.spi.test; import org.slf4j.Logger; diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index b074ae34..02ec691c 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; import com.mpush.api.Constants; diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java index 1a8dc177..8843d450 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; import com.mpush.tools.config.CC.mp.zk; diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index 91c5d5ae..94b545f7 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java index 9444b461..c4a3023c 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.cache; import com.mpush.zk.node.ZKNode; diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java index 8e5fcd41..338704ab 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.cache; import com.mpush.zk.node.ZKRedisNode; diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java index 911b9bb8..b41a271f 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.cache; import com.google.common.collect.Maps; diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java index 8c4d4b89..00efec67 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.listener; import com.google.common.base.Strings; diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java index 44022e87..ef805ff0 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.listener; import com.google.common.base.Strings; diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java index f30b6e79..9ba1741d 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.listener; import com.mpush.tools.Jsons; diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java index 1d5fff0c..7a279ead 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.node; /** diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java index 4a4676a0..f367eccc 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.node; import com.mpush.tools.config.data.RedisGroup; diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index f833c37b..12a804e9 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk.node; From d15fabd69ce692362397ea945cdfb55f72d46451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 24 May 2016 09:22:58 +0200 Subject: [PATCH 551/890] add Admin command --- conf/conf-dev.properties | 1 + conf/conf-pub.properties | 1 + mpush-boot/assembly.xml | 2 +- .../main/java/com/mpush/bootstrap/Main.java | 8 +- .../com/mpush/bootstrap/ServerLauncher.java | 8 +- .../com/mpush/bootstrap/job/BootChain.java | 2 +- .../java/com/mpush/bootstrap/job/BootJob.java | 2 +- .../com/mpush/bootstrap/job/LastBoot.java | 4 +- .../com/mpush/bootstrap/job/ServerBoot.java | 4 +- mpush-boot/src/main/resources/mpush.conf | 6 +- .../redis/manager/ZKRedisClusterManager.java | 6 +- .../com/mpush/core/handler/AdminHandler.java | 183 ++++++++---- .../com/mpush/core/server/AdminServer.java | 18 +- .../mpush/core/server/ConnectionServer.java | 8 + .../com/mpush/netty/server/NettyServer.java | 12 +- .../com/mpush/tools/config/ConfigCenter.java | 263 ------------------ .../config/converter/DnsMappingConverter.java | 2 +- .../tools/config/converter/MapConverter.java | 2 +- .../config/converter/RedisGroupConverter.java | 2 +- .../java/com/mpush/tools/crypto/RSAUtils.java | 6 +- .../src/main/java/com/mpush/zk/ZKClient.java | 6 +- .../src/main/java/com/mpush/zk/ZKPath.java | 1 - .../zk/listener/ZKServerNodeWatcher.java | 16 +- pom.xml | 20 -- 24 files changed, 201 insertions(+), 382 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties index e69de29b..4a36a6eb 100644 --- a/conf/conf-dev.properties +++ b/conf/conf-dev.properties @@ -0,0 +1 @@ +log.level=debug \ No newline at end of file diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties index e69de29b..a5051a64 100644 --- a/conf/conf-pub.properties +++ b/conf/conf-pub.properties @@ -0,0 +1 @@ +log.level=warn \ No newline at end of file diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index 62afe58e..24fc2cd4 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -1,7 +1,7 @@ release-${mpush.version} - mpush + mpush-${mpush.version} true tar.gz diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index f15c560e..8c287e4d 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -25,17 +25,17 @@ public class Main { public static void main(String[] args) { Logs.init(); - Logs.Console.info("launch app..."); + Logs.Console.error("launch app..."); ServerLauncher launcher = new ServerLauncher(); launcher.start(); addHook(launcher); } - private static void addHook(final ServerLauncher serverBoot) { + private static void addHook(final ServerLauncher launcher) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { - serverBoot.stop(); - Logs.Console.info("jvm exit all server stopped..."); + launcher.stop(); + Logs.Console.error("jvm exit, all server stopped..."); } }); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index a5035d2e..9527ffe6 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -38,11 +38,11 @@ public class ServerLauncher { private final ZKServerNode gsNode = ZKServerNode.gsNode(); - private final Server connectServer = new ConnectionServer(csNode.getPort()); + private final ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); - private final Server gatewayServer = new GatewayServer(gsNode.getPort()); + private final GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); - private final Server adminServer = new AdminServer(CC.mp.net.admin_server_port); + private final AdminServer adminServer = new AdminServer(CC.mp.net.admin_server_port, connectServer, gatewayServer); public void start() { @@ -60,7 +60,7 @@ public void start() { } public void stop() { - stopServer(gatewayServer); + stopServer(connectServer); stopServer(gatewayServer); stopServer(adminServer); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index 4e2d1f08..32544f85 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -41,7 +41,7 @@ private BootJob first() { return new BootJob() { @Override public void run() { - Logs.Console.info("begin run bootstrap chain..."); + Logs.Console.error("begin run bootstrap chain..."); next(); } }; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java index bd4fd16f..32bb5b36 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -33,7 +33,7 @@ public abstract class BootJob { public void next() { if (next != null) { - Logs.Console.info("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); + Logs.Console.error("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); next.run(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index cd8935c3..ba65d171 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -31,7 +31,7 @@ public class LastBoot extends BootJob { @Override public void run() { UserManager.INSTANCE.clearUserOnlineData(); - Logs.Console.info("end run bootstrap chain..."); - Logs.Console.info("app start success..."); + Logs.Console.error("end run bootstrap chain..."); + Logs.Console.error("app start success..."); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index c7495fbb..7fd5d62d 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -50,7 +50,7 @@ public void run() { server.start(new Server.Listener() { @Override public void onSuccess(Object... args) { - Logs.Console.info("start " + serverName + " success listen:" + args[0]); + Logs.Console.error("start " + serverName + " success listen:" + args[0]); if (node != null) { registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } @@ -70,6 +70,6 @@ public void onFailure(Throwable cause) { //step7 注册应用到zk public void registerServerToZk(String path, String value) { ZKClient.I.registerEphemeralSequential(path, value); - Logs.Console.info("register server node=" + value + " to zk name=" + path); + Logs.Console.error("register server node=" + value + " to zk name=" + path); } } diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 557e80b4..6ee565ff 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,2 +1,4 @@ -mp.log.level=debug -mp.zk.namespace=mpush \ No newline at end of file +mp.log.level=${log.level} +mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" +mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" +mp.zk.namespace=mpush diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index d171a3a5..91e775df 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -50,7 +50,7 @@ private ZKRedisClusterManager() { */ @Override public void init() { - Logs.Console.info("begin init redis cluster"); + Logs.Console.error("begin init redis cluster"); List groupList = CC.mp.redis.cluster_group; if (groupList.size() > 0) { if (CC.mp.redis.write_to_zk) { @@ -73,7 +73,7 @@ public void init() { groups.add(RedisGroup.from(node)); } if (groups.isEmpty()) throw new RuntimeException("init redis sever fail groupList is null"); - Logs.Console.info("init redis cluster success..."); + Logs.Console.error("init redis cluster success..."); } @Override @@ -119,7 +119,7 @@ public List hashSet(String key) { private void register(List groupList) { String data = Jsons.toJson(groupList); ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data); - Logs.Console.info("register redis server group success, group=" + data); + Logs.Console.error("register redis server group success, group=" + data); } public void addGroup(RedisGroup group) { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 95c97007..5f00cc15 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -19,19 +19,26 @@ package com.mpush.core.handler; +import com.google.common.base.Strings; +import com.mpush.api.Service; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.config.ConfigManager; +import com.mpush.common.router.RemoteRouter; +import com.mpush.core.router.RouterCenter; +import com.mpush.core.server.AdminServer; import com.mpush.tools.Jsons; import com.mpush.tools.MPushUtil; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKClient; import com.mpush.zk.ZKPath; import com.mpush.zk.node.ZKServerNode; +import com.typesafe.config.ConfigRenderOptions; import io.netty.channel.*; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.Serializable; import java.util.Date; import java.util.List; @@ -40,24 +47,37 @@ public final class AdminHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LoggerFactory.getLogger(AdminHandler.class); - private static final String DOUBLE_END = "\r\n\r\n"; private static final String EOL = "\r\n"; + private static AdminServer adminServer; + + public AdminHandler(AdminServer adminServer) { + this.adminServer = adminServer; + } + @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { - Command command = Command.getCommand(request); - ChannelFuture future = ctx.write(command.handler(request) + DOUBLE_END); - if (command.equals(Command.QUIT)) { + Command command = Command.help; + String args = null; + if (request != null) { + String[] cmd_args = request.split(" "); + command = Command.toCmd(cmd_args[0].trim()); + if (cmd_args.length == 2) { + args = cmd_args[1]; + } + } + Object result = command.handler(ctx, args); + ChannelFuture future = ctx.writeAndFlush(result + EOL + EOL); + if (command == Command.quit) { future.addListener(ChannelFutureListener.CLOSE); } - } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + EOL); - ctx.write("It is " + new Date() + " now." + DOUBLE_END); + ctx.write("It is " + new Date() + " now." + EOL + EOL); ctx.flush(); } @@ -67,38 +87,118 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { } public enum Command { - HELP("help") { + help { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { StringBuilder buf = new StringBuilder(); - buf.append("Command:" + EOL); - buf.append("help:display all command." + EOL); - buf.append("quit:exit checkHealth." + EOL); - buf.append("scn:statistics connect num." + EOL); - buf.append("rcs:remove current server zk info." + EOL); - buf.append("scs:stop connection server."); + buf.append("Option Description" + EOL); + buf.append("------ -----------" + EOL); + buf.append("help show help" + EOL); + buf.append("quit exit console mode" + EOL); + buf.append("shutdown stop mpush server" + EOL); + buf.append("restart restart mpush server" + EOL); + buf.append("zk: query zk node" + EOL); + buf.append("count: count conn num or online user count" + EOL); + buf.append("route: show user route info" + EOL); + buf.append("conf:[key] show config info" + EOL); return buf.toString(); } }, - QUIT("quit") { + quit { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { return "have a good day!"; } }, - SCN("scn") { + shutdown { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + ctx.writeAndFlush("try close connect server..."); + adminServer.getConnectionServer().stop(new Service.Listener() { + @Override + public void onSuccess(Object... args) { + ctx.writeAndFlush("connect server close success" + EOL); + adminServer.stop(null);//这个一定要在System.exit之前调用,不然jvm 会卡死 @see com.mpush.bootstrap.Main#addHook + System.exit(0); + } + + @Override + public void onFailure(Throwable cause) { + ctx.writeAndFlush("connect server close failure, msg=" + cause.getLocalizedMessage()); + } + }); + return null; + } + }, + restart { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + return "unsupported"; + } + }, + zk { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + switch (args) { + case "redis": + return ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); + case "cs": + return getNodeData(ZKPath.CONNECT_SERVER); + case "gs": + return getNodeData(ZKPath.GATEWAY_SERVER); + + } + return "[" + args + "] unsupported, try help."; + } + + private String getNodeData(ZKPath path) { + List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); + StringBuilder sb = new StringBuilder(); + for (String raw : rawData) { + sb.append(ZKClient.I.get(path.getFullPath(raw))).append('\n'); + } + return sb.toString(); + } + }, + count { @Override - public String handler(String request) { - Long value = RedisManager.I.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); - if (value == null) { - value = 0L; + public Serializable handler(ChannelHandlerContext ctx, String args) { + switch (args) { + case "conn": + return adminServer.getConnectionServer().getConnectionManager().getConnections().size(); + case "online": { + Long value = RedisManager.I.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); + return value == null ? 0 : value; + } + } - return value.toString() + "."; + return "[" + args + "] unsupported, try help."; + } + }, + route { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + if (Strings.isNullOrEmpty(args)) return "please input userId"; + RemoteRouter router = RouterCenter.INSTANCE.getRemoteRouterManager().lookup(args); + if (router == null) return "user [" + args + "] offline now."; + return router.getRouteValue().toString(); } }, - RCS("rcs") { + conf { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { + if (Strings.isNullOrEmpty(args)) { + return CC.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true)); + } + if (CC.cfg.hasPath(args)) { + return CC.cfg.getAnyRef(args).toString(); + } + return "key [" + args + "] not find in config"; + } + }, + rcs { + @Override + public String handler(ChannelHandlerContext ctx, String args) { List rawData = ZKClient.I.getChildrenKeys(ZKPath.CONNECT_SERVER.getRootPath()); boolean removeSuccess = false; @@ -121,35 +221,16 @@ public String handler(String request) { return "remove false."; } } - }, - SCS("scs") { - @Override - public String handler(String request) { - return "not support now."; - } }; - private final String cmd; - - public abstract String handler(String request); - private Command(String cmd) { - this.cmd = cmd; - } + public abstract Object handler(ChannelHandlerContext ctx, String args); - public String getCmd() { - return cmd; - } - - public static Command getCommand(String request) { - if (StringUtils.isNoneEmpty(request)) { - for (Command command : Command.values()) { - if (command.getCmd().equals(request)) { - return command; - } - } + public static Command toCmd(String cmd) { + try { + return Command.valueOf(cmd); + } catch (Exception e) { } - return HELP; + return help; } } - } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index ba295468..1f625c0a 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -23,18 +23,22 @@ import com.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; -import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; public final class AdminServer extends NettyServer { + private final ConnectionServer connectionServer; + private final GatewayServer gatewayServer; - private final AdminHandler adminHandler = new AdminHandler(); + private final AdminHandler adminHandler; - public AdminServer(int port) { + public AdminServer(int port, ConnectionServer connectionServer, GatewayServer gatewayServer) { super(port); + this.connectionServer = connectionServer; + this.gatewayServer = gatewayServer; + this.adminHandler = new AdminHandler(this); } @Override @@ -57,4 +61,12 @@ protected ChannelHandler getDecoder() { protected ChannelHandler getEncoder() { return new StringEncoder(); } + + public ConnectionServer getConnectionServer() { + return connectionServer; + } + + public GatewayServer getGatewayServer() { + return gatewayServer; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index d7a41c21..70592506 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -94,4 +94,12 @@ protected void initOptions(ServerBootstrap b) { public ChannelHandler getChannelHandler() { return channelHandler; } + + public ConnectionManager getConnectionManager() { + return connectionManager; + } + + public HttpClient getHttpClient() { + return httpClient; + } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index d948749d..5e5a5807 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -22,7 +22,7 @@ import com.mpush.api.Server; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; -import com.mpush.tools.thread.pool.ThreadPoolManager; +import com.mpush.tools.log.Logs; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -75,11 +75,13 @@ public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { IllegalStateException e = new IllegalStateException("server was already shutdown."); if (listener != null) listener.onFailure(e); - throw e; + Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName()); + return; } + Logs.Console.error("try shutdown {}...", this.getClass().getSimpleName()); if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); - logger.error("netty server stop now"); + Logs.Console.error("{} shutdown success.", this.getClass().getSimpleName()); if (listener != null) { listener.onSuccess(port); } @@ -151,10 +153,10 @@ public void initChannel(SocketChannel ch) throws Exception { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - logger.error("server start success on:" + port); + Logs.Console.error("server start success on:{}", port); if (listener != null) listener.onSuccess(port); } else { - logger.error("server start failure on:" + port, future.cause()); + Logs.Console.error("server start failure on:{}", port, future.cause()); if (listener != null) listener.onFailure(future.cause()); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java deleted file mode 100644 index f9b9eccc..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.config; - - -import com.mpush.tools.config.converter.DnsMappingConverter; -import com.mpush.tools.config.converter.MapConverter; -import com.mpush.tools.config.converter.RedisGroupConverter; -import com.mpush.tools.config.data.DnsMapping; -import com.mpush.tools.config.data.RedisGroup; -import org.aeonbits.owner.Config; -import org.aeonbits.owner.Config.Sources; -import org.aeonbits.owner.ConfigFactory; - -import java.util.List; -import java.util.Map; - -/** - * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 - */ -@Sources({ - "classpath:config.properties", - "file:/${user.dir}/config.properties" -}) -public interface ConfigCenter extends Config { - - //ConfigCenter I = ConfigFactory.create(ConfigCenter.class); - - /** - * 最大包长度 - * - * @return - */ - @Key("max_packet_size") - @DefaultValue("10240") - int maxPacketSize(); - - /** - * 包启用压缩特性阈值 - * - * @return - */ - @Key("compress_limit") - @DefaultValue("1024") - int compressLimit(); - - /** - * 最小心跳间隔 10s - * - * @return - */ - @Key("min_heartbeat") - @DefaultValue("10000") - int minHeartbeat(); - - /** - * 最大心跳间隔 10s - * - * @return - */ - @Key("max_heartbeat") - @DefaultValue("180000") - int maxHeartbeat(); - - /** - * 最大心跳超时次数 - * - * @return - */ - @Key("max_hb_timeout_times") - @DefaultValue("2") - int maxHBTimeoutTimes(); - - /** - * 快速重连session超时时间 - * - * @return - */ - @Key("session_expired_time") - @DefaultValue("86400") - int sessionExpiredTime(); - - /** - * RSA密钥长度 - * - * @return - */ - @Key("ras_key_length") - @DefaultValue("1024") - int rsaKeyLength(); - - /** - * AES密钥长度 - * - * @return - */ - @Key("aes_key_length") - @DefaultValue("16") - int aesKeyLength(); - - /** - * 长连接服务端口 - * - * @return - */ - @Key("connection_server_port") - @DefaultValue("3000") - int connectionServerPort(); - - /** - * 网关服务端口 - * - * @return - */ - @Key("gateway_server_port") - @DefaultValue("4000") - int gatewayServerPort(); - - - /** - * 控制台服务端口 - * - * @return - */ - @Key("admin_port") - @DefaultValue("4001") - int adminPort(); - - /** - * RSA私钥 - * - * @return - */ - @Key("private_key") - @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") - String privateKey(); - - /** - * RSA公钥 - * - * @return - */ - @Key("public_key") - @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") - String publicKey(); - - /** - * redis集群机器列表格式ip:port:pwd,host:port:pwd,host:port:pwd - * 多台机器用“,”分割 - * - * @return - */ - @Deprecated - @Key("redis_ip") - @DefaultValue("127.0.0.1:6379:ShineMoIpo") - String redisIp(); - - /** - * zookeeper机器,格式ip:port - * - * @return - */ - @Key("zk_ip") - @DefaultValue("127.0.0.1:2181") - String zkIp(); - - /** - * zookeeper 空间 - * - * @return - */ - @Key("zk_namespace") - @DefaultValue("mpush") - String zkNamespace(); - - /** - * zookeeper 权限密码 - * - * @return - */ - @Key("zk_digest") - @DefaultValue("shinemoIpo") - String zkDigest(); - - /** - * redis集群机器列表格式ip:port:pwd,host:port:pwd,host:port:pwd - * 多台机器用“;”分割 - * - * @return - */ - @Separator(";") - @Key("redis_group") - @ConverterClass(RedisGroupConverter.class) - List redisGroups(); - - /** - * 自动把配置的redis机器集群写入到zk - * - * @return - */ - @Key("force_write_redis_group_info") - boolean forceWriteRedisGroupInfo(); - - @Key("scan_conn_task_cycle") - @DefaultValue("59000") - long scanConnTaskCycle(); - - @Key("jvm_log_path") - @DefaultValue("/opt/shinemo/mpush/") - String logPath(); - - @Key("http_proxy_enable") - @DefaultValue("false") - boolean httpProxyEnable(); - - @Key("dns_mapping") - @ConverterClass(DnsMappingConverter.class) - Map> dnsMapping(); - - @Key("max_http_client_conn_count_per_host") - @DefaultValue("5") - int maxHttpConnCountPerHost(); - - //10s - @Key("http_default_read_timeout") - @DefaultValue("10000") - int httpDefaultReadTimeout(); - - @Key("online_and_offline_listener_ip") - @DefaultValue("127.0.0.1") - String onlineAndOfflineListenerIp(); - - @Key("skip_dump") - @DefaultValue("true") - boolean skipDump(); - - /** - * 本机IP到外网Ip的映射 格式localIp:remoteIp,localIp:remoteIp - * - * @return - */ - @Key("remote_ip_mapping") - @ConverterClass(MapConverter.class) - Map remoteIpMapping(); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java index cb427e00..2a6017f4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java @@ -37,7 +37,7 @@ public class DnsMappingConverter implements Converter> convert(Method method, String input) { - Logs.Console.info("method:" + method.getName() + ", input:" + input); + Logs.Console.error("method:" + method.getName() + ", input:" + input); Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); Map> result = Maps.newConcurrentMap(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java index d3b2cfcf..a2df46ed 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java @@ -37,7 +37,7 @@ public class MapConverter implements Converter> { @Override public Map convert(Method method, String input) { - Logs.Console.info("method:" + method.getName() + ", input:" + input); + Logs.Console.error("method:" + method.getName() + ", input:" + input); if (Strings.isNullOrEmpty(input)) return Collections.emptyMap(); return Splitter.on(',').withKeyValueSeparator(':').split(input); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java index c2414224..b480a322 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java @@ -32,7 +32,7 @@ public class RedisGroupConverter implements Converter { @Override public RedisGroup convert(Method method, String input) { - Logs.Console.info("method:" + method.getName() + ", input:" + input); + Logs.Console.error("method:" + method.getName() + ", input:" + input); List servers = new ArrayList<>(); String[] chunks = input.split(","); diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index 52558c86..99825f2a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -366,8 +366,12 @@ public static void main(String[] args) throws Exception { byte[] ming = "123456789".getBytes(Constants.UTF_8); System.out.println("明文:" + new String(ming, Constants.UTF_8)); //使用模和指数生成公钥和私钥 - RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); + RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); + System.out.println("privateKey=" + priKey); + System.out.println("publicKey=" + pubKey); + System.out.println("privateKey=" + priKey); + System.out.println("publicKey=" + pubKey); //加密后的密文 byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey); System.out.println("密文:" + new String(mi, Constants.UTF_8)); diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 02ec691c..c6331275 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -64,7 +64,7 @@ private ZKClient() { public void init() { if (zkConfig != null) return; zkConfig = ZKConfig.build(); - Logs.Console.info("init zk client, config=" + zkConfig); + Logs.Console.error("init zk client, config=" + zkConfig); Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) @@ -95,7 +95,7 @@ public List getAclForPath(final String path) { } client = builder.build(); client.start(); - Logs.Console.info("init zk client waiting for connected..."); + Logs.Console.error("init zk client waiting for connected..."); try { if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { throw new ZKException("init zk error, config=" + zkConfig); @@ -113,7 +113,7 @@ public void run() { }); Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); - Logs.Console.info("init zk client success..."); + Logs.Console.error("init zk client success..."); } // 注册连接状态监听器 diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index 94b545f7..ed1c7aa0 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -25,7 +25,6 @@ public enum ZKPath { REDIS_SERVER("/redis", "machine", "redis注册的地方"), CONNECT_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), - PUSH_SERVER("/ps/hosts", "machine", "push server服务器应用注册的路径"), GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"); ZKPath(String root, String name, String desc) { diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java index 9ba1741d..a88b6c14 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java @@ -78,22 +78,14 @@ public String watchPath() { @Override protected void beforeWatch() { - Logs.Console.info("start init zk server data"); - List rawData = ZKClient.I.getChildrenKeys(getRegisterPath()); + Logs.Console.error("start init zk server data"); + List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); for (String raw : rawData) { - String fullPath = getFullPath(raw); + String fullPath = path.getFullPath(raw); ZKServerNode app = getServerNode(fullPath); cache.put(fullPath, app); } - Logs.Console.info("end init zk server data"); - } - - public String getRegisterPath() { - return path.getRootPath(); - } - - public String getFullPath(String raw) { - return path.getFullPath(raw); + Logs.Console.error("end init zk server data"); } private ZKServerNode getServerNode(String fullPath) { diff --git a/pom.xml b/pom.xml index da369c61..57891f08 100644 --- a/pom.xml +++ b/pom.xml @@ -240,26 +240,6 @@ config 1.3.0 - - org.codehaus.janino - janino - 2.7.8 - - - args4j - args4j - 2.33 - - - net.sf.jopt-simple - jopt-simple - 5.0.1 - - - com.beust - jcommander - 1.48 - From 83c60f1ed9d1d3ac320440300c12f21ca262cc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 24 May 2016 12:47:57 +0200 Subject: [PATCH 552/890] add Admin command --- .../src/main/java/com/mpush/core/server/GatewayServer.java | 4 +++- mpush-tools/src/main/java/com/mpush/tools/config/CC.java | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index f6d1988f..ee00f839 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -51,7 +51,9 @@ public void init() { @Override public void stop(Listener listener) { super.stop(listener); - connectionManager.destroy(); + if (connectionManager != null) { + connectionManager.destroy(); + } } @Override diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index f8c8a166..9dec835e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -251,10 +251,10 @@ interface net { int connect_server_port = cfg.getInt("connect-server-port"); interface public_ip_mapping { - Config cfg = net.cfg.getObject("public-host-mapping").toConfig(); + Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); static String getString(String localIp) { - return cfg.hasPath(localIp) ? cfg.getString(localIp) : localIp; + return (String) mappings.getOrDefault(localIp, localIp); } } From 8d4e9be52aef8633ef788f081d2cf3572b75e201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 29 May 2016 11:54:26 +0200 Subject: [PATCH 553/890] =?UTF-8?q?Aes=20=E4=BC=98=E5=8C=96=EF=BC=8C=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=81=E9=87=8F=E6=95=B4=E5=BD=A2=20?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9C=8D=E5=8A=A1=E5=8C=96=20httpClient=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E6=97=A5=E5=BF=97=E5=AE=8C=E5=96=84=20?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E5=A2=9E=E5=8A=A0=E6=8B=92=E7=BB=9D?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 70 ++- .../main/java/com/mpush/api/BaseService.java | 49 --- .../java/com/mpush/api/MessageHandler.java | 2 +- .../java/com/mpush/api/PacketReceiver.java | 2 +- .../api/connection/ConnectionManager.java | 10 +- .../mpush/api/connection/SessionContext.java | 13 +- .../com/mpush/api/event/UserOnlineEvent.java | 31 +- .../mpush/api/exception/MessageException.java | 30 -- .../api/exception/SendMessageException.java | 28 -- .../java/com/mpush/api/push/PushContent.java | 109 +++-- .../com/mpush/api/push/PushException.java | 39 ++ .../java/com/mpush/api/push/PushSender.java | 11 +- .../com/mpush/api/service/BaseService.java | 134 ++++++ .../com/mpush/api/{ => service}/Client.java | 2 +- .../java/com/mpush/api/service/Listener.java | 7 + .../com/mpush/api/{ => service}/Server.java | 2 +- .../com/mpush/api/{ => service}/Service.java | 13 +- .../mpush/api/service/ServiceException.java | 39 ++ .../api/spi/common/ThreadPoolFactory.java | 2 +- .../mpush/api/spi/net/DnsMappingManager.java | 2 +- .../com/mpush/bootstrap}/BootException.java | 6 +- .../com/mpush/bootstrap/ServerLauncher.java | 6 +- .../mpush/bootstrap/job/HttpProxyBoot.java | 3 +- .../com/mpush/bootstrap/job/MonitorBoot.java | 6 +- .../com/mpush/bootstrap/job/ServerBoot.java | 7 +- .../java/com/mpush/bootstrap/job/ZKBoot.java | 19 +- .../com/mpush/cache/redis/RedisClient.java | 120 ++---- .../com/mpush/cache/redis/RedisException.java | 42 ++ .../com/mpush/cache/redis/RedisServer.java | 1 + .../cache/redis/hash/ConsistentHash.java | 104 ++--- .../java/com/mpush/cache/redis/hash/Node.java | 47 ++- .../redis/listener/ListenerDispatcher.java | 18 +- .../cache/redis/listener/MessageListener.java | 4 +- .../redis/manager/ZKRedisClusterManager.java | 6 +- .../com/mpush/cache/redis/mq/Subscriber.java | 27 +- .../connect/ConnClientChannelHandler.java | 51 +-- .../mpush/client/connect/ConnectClient.java | 10 +- .../mpush/client/gateway/GatewayClient.java | 32 ++ .../client/gateway/GatewayClientBoot.java | 39 -- .../gateway/GatewayClientChannelHandler.java | 63 ++- .../client/gateway/GatewayClientFactory.java | 39 +- .../com/mpush/client/push/PushClient.java | 26 +- .../com/mpush/client/push/PushRequest.java | 234 +++++------ .../com/mpush/client/push/PushRequestBus.java | 51 ++- .../main/java/com/mpush/common/ErrorCode.java | 1 + .../com/mpush/common/MessageDispatcher.java | 36 +- .../common/handler/BaseMessageHandler.java | 2 +- .../common/handler/OkMessageHandler.java | 2 +- .../com/mpush/common/message/BaseMessage.java | 19 +- .../mpush/common/message/BindUserMessage.java | 10 + .../mpush/common/message/ByteBufMessage.java | 2 +- .../common/message/FastConnectMessage.java | 11 + .../common/message/FastConnectOkMessage.java | 8 + .../common/message/HandshakeMessage.java | 18 + .../common/message/HandshakeOkMessage.java | 13 + .../common/message/HttpRequestMessage.java | 6 +- .../common/message/HttpResponseMessage.java | 6 +- .../com/mpush/common/message/PushMessage.java | 8 + .../mpush/common/net/DnsMappingManager.java | 20 +- .../com/mpush/common/router/RemoteRouter.java | 2 +- .../common/router/RemoteRouterManager.java | 11 +- .../common/router/UserChangeListener.java | 6 +- .../com/mpush/common/security/AesCipher.java | 29 +- .../com/mpush/common/security/CipherBox.java | 2 +- .../com/mpush/common/security/RsaCipher.java | 16 +- .../com/mpush/common/user/UserManager.java | 9 +- .../com/mpush/core/handler/AdminHandler.java | 17 +- .../mpush/core/handler/BindUserHandler.java | 2 +- .../core/handler/FastConnectHandler.java | 11 +- .../core/handler/GatewayPushHandler.java | 12 +- .../mpush/core/handler/HandshakeHandler.java | 13 +- .../mpush/core/handler/HeartBeatHandler.java | 3 +- .../mpush/core/handler/HttpProxyHandler.java | 51 +-- .../mpush/core/handler/UnbindUserHandler.java | 6 +- .../com/mpush/core/router/KickRemoteMsg.java | 14 +- .../mpush/core/router/LocalRouterManager.java | 10 +- .../com/mpush/core/router/RouterCenter.java | 7 +- .../core/router/RouterChangeListener.java | 16 +- .../router/UserOnlineOfflineListener.java | 6 +- .../com/mpush/core/server/AdminServer.java | 13 + .../mpush/core/server/ConnectionServer.java | 41 +- .../com/mpush/core/server/GatewayServer.java | 26 ++ .../core/server/ServerChannelHandler.java | 55 ++- .../core/server/ServerConnectionManager.java | 27 +- .../core/session/ReusableSessionManager.java | 2 +- .../com/mpush/core/netty/NettyServerTest.java | 46 -- mpush-core/src/test/resources/logback.xml | 48 +-- .../src/main/java/com/mpush/App.java | 9 +- .../com/mpush/monitor/data/MonitorResult.java | 92 ++-- .../com/mpush/monitor/quota/GCMQuota.java | 32 +- .../com/mpush/monitor/quota/InfoQuota.java | 6 +- .../com/mpush/monitor/quota/MemoryQuota.java | 60 +-- .../com/mpush/monitor/quota/ThreadQuota.java | 16 +- .../com/mpush/monitor/quota/impl/JVMInfo.java | 68 +-- .../mpush/monitor/quota/impl/JVMThread.java | 78 ++-- .../monitor/quota/impl/JVMThreadPool.java | 11 +- .../mpush/monitor/service/MonitorService.java | 54 +-- .../src/test/java/com/mpush/AppTest.java | 20 +- mpush-netty/pom.xml | 4 + .../com/mpush/netty/client/NettyClient.java | 58 ++- .../mpush/netty/codec}/DecodeException.java | 2 +- .../com/mpush/netty/codec/PacketDecoder.java | 1 - .../netty/connection/NettyConnection.java | 4 +- .../java/com/mpush/netty/http/HttpClient.java | 11 +- .../mpush/netty/http/HttpClientHandler.java | 126 ++++++ .../mpush/netty/http/HttpConnectionPool.java | 85 ++++ .../com/mpush/netty/http/NettyHttpClient.java | 278 ++++-------- .../{RequestInfo.java => RequestContext.java} | 25 +- .../com/mpush/netty/server/NettyServer.java | 58 ++- mpush-netty/src/test/resources/logback.xml | 50 +-- .../mpush/test/client/ConnClientTestMain.java | 14 +- .../mpush/test/client/ConnectClientBoot.java | 5 +- .../mpush/test/connection/body/BodyTest.java | 98 ++--- .../connection/mpns/ConnClientTestMain.java | 2 +- .../java/com/mpush/test/crypto/RsaTest.java | 237 ++++++----- .../com/mpush/test/gson/DnsMappingTest.java | 37 +- .../java/com/mpush/test/gson/GsonTest.java | 109 +++-- .../mpush/test/push/PushClientTestMain.java | 12 +- .../mpush/test/redis/ConsistentHashTest.java | 74 ++-- .../com/mpush/test/redis/MPushUtilTest.java | 12 +- .../test/java/com/mpush/test/redis/User.java | 71 ++-- .../java/com/mpush/test/util/TelnetTest.java | 46 +- mpush-test/src/test/resources/logback.xml | 30 ++ .../src/main/java/com/mpush/tools/Jsons.java | 7 +- .../tools/{MPushUtil.java => Utils.java} | 5 +- .../tools/{ => common}/GenericsUtil.java | 192 +++++---- .../com/mpush/tools/{ => common}/IOUtils.java | 14 +- .../com/mpush/tools/{ => common}/JVMUtil.java | 62 ++- .../com/mpush/tools/{ => common}/Pair.java | 2 +- .../mpush/tools/{ => common}/Profiler.java | 14 +- .../com/mpush/tools/{ => common}/Strings.java | 2 +- .../java/com/mpush/tools/common/TimeLine.java | 94 +++++ .../main/java/com/mpush/tools/config/CC.java | 190 ++++++--- .../com/mpush/tools/config/ConfigManager.java | 6 +- .../config/converter/DnsMappingConverter.java | 64 --- .../tools/config/converter/MapConverter.java | 44 -- .../config/converter/RedisGroupConverter.java | 54 --- .../mpush/tools/config/data/DnsMapping.java | 13 + .../mpush/tools/config/data/RedisServer.java | 19 + .../java/com/mpush/tools/crypto/AESUtils.java | 31 +- .../java/com/mpush/tools/crypto/Base64.java | 397 ++++++++---------- .../com/mpush/tools/crypto/Base64Utils.java | 104 +---- .../mpush/tools/crypto}/CryptoException.java | 5 +- .../java/com/mpush/tools/crypto/MD5Utils.java | 4 +- .../java/com/mpush/tools/crypto/RSAUtils.java | 10 +- .../java/com/mpush/tools/event/EventBus.java | 4 +- .../com/mpush/tools/event/EventConsumer.java | 8 +- .../main/java/com/mpush/tools/log/Logs.java | 8 +- .../com/mpush/tools/thread/ThreadNames.java | 18 +- .../tools/thread/pool/DefaultExecutor.java | 34 ++ .../thread/pool/DefaultThreadPoolFactory.java | 31 +- .../pool/DumpThreadRejectedHandler.java | 31 +- .../tools/thread/pool/ThreadPoolConfig.java | 12 + .../tools/thread/pool/ThreadPoolManager.java | 21 + .../java/com/mpush/tools/IOUtilsTest.java | 7 +- .../com/mpush/tools/crypto/RSAUtilsTest.java | 3 +- .../java/com/mpush/tools/delayqueue/Exam.java | 174 -------- .../com/mpush/tools/spi/test/TestService.java | 27 -- .../mpush/tools/spi/test/TestServiceImpl.java | 38 -- .../tools/spi/test/TestServiceImpl2.java | 39 -- mpush-tools/src/test/resources/logback.xml | 50 +-- .../src/main/java/com/mpush/zk/ZKClient.java | 78 ++-- .../main/java/com/mpush/zk}/ZKException.java | 2 +- .../mpush/zk/listener/ZKNodeCacheWatcher.java | 2 +- .../java/com/mpush/zk/node/ZKRedisNode.java | 2 +- .../java/com/mpush/zk/node/ZKServerNode.java | 6 +- pom.xml | 5 + 167 files changed, 3077 insertions(+), 2970 deletions(-) delete mode 100644 mpush-api/src/main/java/com/mpush/api/BaseService.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/MessageException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/BaseService.java rename mpush-api/src/main/java/com/mpush/api/{ => service}/Client.java (95%) create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Listener.java rename mpush-api/src/main/java/com/mpush/api/{ => service}/Server.java (96%) rename mpush-api/src/main/java/com/mpush/api/{ => service}/Service.java (86%) create mode 100644 mpush-api/src/main/java/com/mpush/api/service/ServiceException.java rename {mpush-api/src/main/java/com/mpush/api/exception => mpush-boot/src/main/java/com/mpush/bootstrap}/BootException.java (87%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java delete mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java delete mode 100644 mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java rename {mpush-api/src/main/java/com/mpush/api/exception => mpush-netty/src/main/java/com/mpush/netty/codec}/DecodeException.java (96%) create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java rename mpush-netty/src/main/java/com/mpush/netty/http/{RequestInfo.java => RequestContext.java} (87%) create mode 100644 mpush-test/src/test/resources/logback.xml rename mpush-tools/src/main/java/com/mpush/tools/{MPushUtil.java => Utils.java} (98%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/GenericsUtil.java (52%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/IOUtils.java (92%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/JVMUtil.java (83%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Pair.java (97%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Profiler.java (98%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Strings.java (97%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java rename {mpush-api/src/main/java/com/mpush/api/exception => mpush-tools/src/main/java/com/mpush/tools/crypto}/CryptoException.java (87%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java rename {mpush-tools/src/main/java/com/mpush/tools/exception => mpush-zk/src/main/java/com/mpush/zk}/ZKException.java (96%) diff --git a/conf/reference.conf b/conf/reference.conf index 97c598c6..6f75b89f 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -1,14 +1,27 @@ +################################################################################################################## +# +# NOTICE: +# +# 系统配置文件,所有列出的项是系统所支持全部配置项 +# 如果要覆盖某项的值可以添加到mpush.conf中。 +# +# 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 +# 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 +# +############################################################################################################## + mp { log.level=warn log.dir=${user.dir}/../logs core { - max-packet-size=10k - compress-threshold=10k + max-packet-size=10k//系统允许传输的最大包的大小 + compress-threshold=10k//数据包启用压缩的临界值,超过该值后对数据进行压缩 min-heartbeat=10s max-heartbeat=3m - max-hb-timeout-times=2 - session-expired-time=1d + max-hb-timeout-times=2//允许的心跳连续超时的最大次数 + session-expired-time=1d//用于快速重连的session 过期时间默认1天 + epoll-provider=netty//nio:jdk 自带,netty:有netty实现 } security { @@ -22,8 +35,36 @@ mp { connect-server-port=3000 gateway-server-port=3001 admin-server-port=3002 - public-host-mapping { - "127.0.0.1":"127.0.0.1" + public-host-mapping {//本机局域网IP和公网IP的映射关系 + "10.1.0.32":"111.1.57.148" + } + traffic-shaping { + gateway-client { + enabled:true + check-interval:100ms + write-global-limit:1k + read-global-limit:0 + write-channel-limit:256b + read-channel-limit:0 + } + + gateway-server { + enabled:true + check-interval:100ms + write-global-limit:0 + read-global-limit:10k + write-channel-limit:0 + read-channel-limit:0.5k + } + + connect-server { + enabled:false + check-interval:100ms + write-global-limit:0 + read-global-limit:100k + write-channel-limit:3k + read-channel-limit:3k + } } } @@ -46,7 +87,8 @@ mp { redis { write-to-zk=true - cluster-group:[//redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ [ { host:"111.1.57.148" @@ -78,9 +120,10 @@ mp { } http { - proxy-enable=false + proxy-enabled=false max-conn-per-host=5 default-read-timeout=10s + max-content-length=5m dns-mapping { "mpush.com":["127.0.0.1:8080","127.0.0.1:8081"] } @@ -103,7 +146,7 @@ mp { event-bus { min:4 max:4 - queue-size:100 + queue-size:10000 //大量的online,offline, } http-proxy { @@ -124,16 +167,19 @@ mp { queue-size:10000 } - push-client { + push-callback { min:2 - max:16 - queue-size:100000 + max:2 + queue-size:1 } } } monitor { + dump-dir=/tmp/logs/mpush/ dump-stack=false + dump-period=1m + print-log=true } spi { diff --git a/mpush-api/src/main/java/com/mpush/api/BaseService.java b/mpush-api/src/main/java/com/mpush/api/BaseService.java deleted file mode 100644 index 91f1602f..00000000 --- a/mpush-api/src/main/java/com/mpush/api/BaseService.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.api; - -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * Created by yxx on 2016/5/19. - * - * @author ohun@live.cn - */ -public abstract class BaseService implements Service { - - protected final AtomicBoolean started = new AtomicBoolean(); - - @Override - public void init() { - } - - @Override - public boolean isRunning() { - return started.get(); - } - - public final void start() { - start(null); - } - - public final void stop() { - start(null); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java index ad44841f..5d7ee6b0 100644 --- a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java @@ -19,8 +19,8 @@ package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java index 0184f285..d26e5cf7 100644 --- a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java @@ -19,8 +19,8 @@ package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index b1447ce5..58f12e76 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -19,10 +19,10 @@ package com.mpush.api.connection; -import java.util.List; - import io.netty.channel.Channel; +import java.util.List; + /** * Created by ohun on 2015/12/30. */ @@ -30,13 +30,13 @@ public interface ConnectionManager { Connection get(Channel channel); - void remove(Channel channel); + Connection removeAndClose(Channel channel); void add(Connection connection); - List getConnections(); + List getConnections(); - void init(); + void init(); void destroy(); } diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index ce6c6333..c10c4ec8 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -64,9 +64,14 @@ public boolean handshakeOk() { return deviceId != null && deviceId.length() > 0; } - @Override - public String toString() { - return "SessionContext [osName=" + osName + ", osVersion=" + osVersion + ", clientVersion=" + clientVersion + ", deviceId=" + deviceId + ", heartbeat=" + heartbeat + "]"; - } + @Override + public String toString() { + return "SessionContext [osName=" + osName + + ", osVersion=" + osVersion + + ", clientVersion=" + clientVersion + + ", deviceId=" + deviceId + + ", heartbeat=" + heartbeat + + "]"; + } } diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java index e2682010..d6053ee9 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java @@ -25,21 +25,22 @@ * 绑定用户的时候才会触发该事件 */ public final class UserOnlineEvent implements Event { - - private final Connection connection; + + private final Connection connection; private final String userId; - - public UserOnlineEvent(Connection connection, String userId) { - this.connection = connection; - this.userId = userId; - } - - public Connection getConnection() { - return connection; - } - public String getUserId() { - return userId; - } - + + public UserOnlineEvent(Connection connection, String userId) { + this.connection = connection; + this.userId = userId; + } + + public Connection getConnection() { + return connection; + } + + public String getUserId() { + return userId; + } + } diff --git a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java deleted file mode 100644 index bd20afd7..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.api.exception; - -public class MessageException extends RuntimeException { - - private static final long serialVersionUID = 8731698346169093329L; - - public MessageException(String message) { - super(message); - } - -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java deleted file mode 100644 index 0f8e84ff..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class SendMessageException extends RuntimeException { -} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java index 8269daf3..e4e741c3 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java @@ -23,73 +23,72 @@ /** - * msgId、msgType 必填 + * msgId、msgType 必填 * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 + * 必填:content。 * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 */ -public final class PushContent implements Serializable{ - private static final long serialVersionUID = -1805329333995385960L; - private String msgId; //返回使用 +public final class PushContent implements Serializable { + private static final long serialVersionUID = -1805329333995385960L; + private String msgId; //返回使用 private String content; //content private int msgType; //type - + public PushContent(int msgType) { - this.msgType = msgType; - } + this.msgType = msgType; + } + + public static PushContent build(PushType msgType, String content) { + PushContent pushContent = new PushContent(msgType.getValue()); + pushContent.setContent(content); + return pushContent; + } + + public String getMsgId() { + return msgId; + } + + public int getMsgType() { + return msgType; + } + + public void setMsgId(String msgId) { + this.msgId = msgId; + } + + public String getContent() { + return content; + } - public static PushContent build(PushType msgType,String content){ - PushContent pushContent = new PushContent(msgType.getValue()); - pushContent.setContent(content); - return pushContent; + public void setContent(String content) { + this.content = content; } - public String getMsgId() { - return msgId; - } - - public int getMsgType() { - return msgType; - } - - public void setMsgId(String msgId) { - this.msgId = msgId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public enum PushType{ - NOTIFICATION("提醒",1), - MESSAGE("消息",2), - NOTIFICATIONANDMESSAGE("提醒+消息",3); - - PushType(String desc, int value) { - this.desc = desc; - this.value = value; + public enum PushType { + NOTIFICATION("提醒", 1), + MESSAGE("消息", 2), + NOTIFICATIONANDMESSAGE("提醒+消息", 3); + + PushType(String desc, int value) { + this.desc = desc; + this.value = value; } private final String desc; private final int value; - - public String getDesc() { - return desc; - } - public int getValue() { - return value; - } - } - + + public String getDesc() { + return desc; + } + + public int getValue() { + return value; + } + } + } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushException.java b/mpush-api/src/main/java/com/mpush/api/push/PushException.java new file mode 100644 index 00000000..6c9f3d6a --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.api.push; + +/** + * Created by yxx on 2016/5/28. + * + * @author ohun@live.cn (夜色) + */ +public class PushException extends RuntimeException { + + public PushException(Throwable cause) { + super(cause); + } + + public PushException(String message) { + super(message); + } + + public PushException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java index e64217ea..e94a3c32 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -19,7 +19,7 @@ package com.mpush.api.push; -import com.mpush.api.Service; +import com.mpush.api.service.Service; import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.client.PusherFactory; @@ -31,16 +31,15 @@ * @author ohun@live.cn */ public interface PushSender extends Service { - PusherFactory factory = SpiLoader.load(PusherFactory.class); + + static PushSender create() { + return SpiLoader.load(PusherFactory.class).get(); + } void send(String content, Collection userIds, Callback callback); void send(String content, String userId, Callback callback); - void start(); - - void stop(); - interface Callback { void onSuccess(String userId); diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java new file mode 100644 index 00000000..9f2c58bc --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -0,0 +1,134 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +import com.sun.istack.internal.NotNull; + +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public abstract class BaseService implements Service { + + protected final AtomicBoolean started = new AtomicBoolean(); + + @Override + public void init() { + } + + @Override + public boolean isRunning() { + return started.get(); + } + + protected void tryStart(Listener listener, Function function) { + listener = new FutureListener(listener); + if (started.compareAndSet(false, true)) { + try { + init(); + function.apply(listener); + listener.onSuccess("service " + this.getClass().getSimpleName() + " start success"); + } catch (Throwable e) { + listener.onFailure(e); + } + } else { + listener.onFailure(new ServiceException("service already started.")); + } + } + + protected void tryStop(Listener listener, Function function) { + listener = new FutureListener(listener); + if (started.compareAndSet(true, false)) { + try { + function.apply(listener); + listener.onSuccess("service " + this.getClass().getSimpleName() + " stop success"); + } catch (Throwable e) { + listener.onFailure(e); + } + } else { + listener.onFailure(new ServiceException("service already stopped.")); + } + } + + public final Future start() { + FutureListener listener = new FutureListener(null); + start(listener); + return listener; + } + + public final Future stop() { + FutureListener listener = new FutureListener(null); + stop(listener); + return listener; + } + + @Override + public void start(Listener listener) { + tryStart(listener, this::doStart); + } + + @Override + public void stop(Listener listener) { + tryStop(listener, this::doStop); + } + + protected abstract void doStart(@NotNull Listener listener) throws Throwable; + + protected abstract void doStop(@NotNull Listener listener) throws Throwable; + + protected interface Function { + void apply(@NotNull Listener l) throws Throwable; + } + + protected class FutureListener extends FutureTask implements Listener { + private final Listener l; + + public FutureListener(Listener l) { + super(BaseService.this::isRunning); + this.l = l; + } + + @Override + public void onSuccess(Object... args) { + if (isDone()) return; + set(started.get()); + if (l != null) l.onSuccess(args); + } + + @Override + public void onFailure(Throwable cause) { + if (isDone()) return; + set(started.get()); + setException(cause); + if (l != null) l.onFailure(cause); + throw new ServiceException(cause); + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + throw new UnsupportedOperationException(); + } + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/Client.java b/mpush-api/src/main/java/com/mpush/api/service/Client.java similarity index 95% rename from mpush-api/src/main/java/com/mpush/api/Client.java rename to mpush-api/src/main/java/com/mpush/api/service/Client.java index 4b9ebbf6..4cb61fb0 100644 --- a/mpush-api/src/main/java/com/mpush/api/Client.java +++ b/mpush-api/src/main/java/com/mpush/api/service/Client.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.api; +package com.mpush.api.service; public interface Client extends Service { diff --git a/mpush-api/src/main/java/com/mpush/api/service/Listener.java b/mpush-api/src/main/java/com/mpush/api/service/Listener.java new file mode 100644 index 00000000..51d1c18b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Listener.java @@ -0,0 +1,7 @@ +package com.mpush.api.service; + +public interface Listener { + void onSuccess(Object... args); + + void onFailure(Throwable cause); +} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/service/Server.java similarity index 96% rename from mpush-api/src/main/java/com/mpush/api/Server.java rename to mpush-api/src/main/java/com/mpush/api/service/Server.java index 608916b7..5249daba 100644 --- a/mpush-api/src/main/java/com/mpush/api/Server.java +++ b/mpush-api/src/main/java/com/mpush/api/service/Server.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.api; +package com.mpush.api.service; /** * Created by ohun on 2015/12/24. diff --git a/mpush-api/src/main/java/com/mpush/api/Service.java b/mpush-api/src/main/java/com/mpush/api/service/Service.java similarity index 86% rename from mpush-api/src/main/java/com/mpush/api/Service.java rename to mpush-api/src/main/java/com/mpush/api/service/Service.java index cc56b819..c158751b 100644 --- a/mpush-api/src/main/java/com/mpush/api/Service.java +++ b/mpush-api/src/main/java/com/mpush/api/service/Service.java @@ -17,7 +17,9 @@ * ohun@live.cn (夜色) */ -package com.mpush.api; +package com.mpush.api.service; + +import java.util.concurrent.Future; /** * Created by yxx on 2016/5/17. @@ -30,13 +32,12 @@ public interface Service { void stop(Listener listener); + Future start(); + + Future stop(); + void init(); boolean isRunning(); - interface Listener { - void onSuccess(Object... args); - - void onFailure(Throwable cause); - } } diff --git a/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java b/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java new file mode 100644 index 00000000..63f05bf4 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.api.service; + +/** + * Created by yxx on 2016/5/27. + * + * @author ohun@live.cn (夜色) + */ +public class ServiceException extends RuntimeException { + + public ServiceException(String message) { + super(message); + } + + public ServiceException(Throwable cause) { + super(cause); + } + + public ServiceException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java index 84573a10..84d394cb 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java @@ -30,7 +30,7 @@ public interface ThreadPoolFactory { String SERVER_BOSS = "sb"; String SERVER_WORK = "sw"; String HTTP_CLIENT_WORK = "hcw"; - String PUSH_CLIENT_WORK = "puw"; + String PUSH_CALLBACK = "pc"; String EVENT_BUS = "eb"; String MQ = "r"; String BIZ = "b"; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java index 9abd9264..c23e7260 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java @@ -19,7 +19,7 @@ package com.mpush.api.spi.net; -import com.mpush.api.Service; +import com.mpush.api.service.Service; /** * Created by yxx on 2016/5/23. diff --git a/mpush-api/src/main/java/com/mpush/api/exception/BootException.java b/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java similarity index 87% rename from mpush-api/src/main/java/com/mpush/api/exception/BootException.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java index 97df1842..eb63eb23 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/BootException.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.api.exception; +package com.mpush.bootstrap; /** * Created by yxx on 2016/5/19. @@ -28,4 +28,8 @@ public class BootException extends RuntimeException { public BootException(String s) { super(s); } + + public BootException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 9527ffe6..18edd96b 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,12 +20,14 @@ package com.mpush.bootstrap; -import com.mpush.api.Server; +import com.mpush.api.service.Server; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.GatewayServer; +import com.mpush.monitor.service.MonitorService; import com.mpush.tools.config.CC; +import com.mpush.zk.ZKClient; import com.mpush.zk.node.ZKServerNode; /** @@ -63,6 +65,8 @@ public void stop() { stopServer(connectServer); stopServer(gatewayServer); stopServer(adminServer); + ZKClient.I.stop(); + MonitorService.I.stop(); } private void stopServer(Server server) { diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index 7c023ef5..22e9f210 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -30,8 +30,7 @@ public class HttpProxyBoot extends BootJob { @Override void run() { - if (CC.mp.http.proxy_enable) { - DnsMappingManager.I.init(); + if (CC.mp.http.proxy_enabled) { DnsMappingManager.I.start(); } next(); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java index 808d535f..d0b59ae7 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -20,7 +20,6 @@ package com.mpush.bootstrap.job; import com.mpush.monitor.service.MonitorService; -import com.mpush.tools.config.CC; /** * Created by yxx on 2016/5/15. @@ -30,10 +29,7 @@ public class MonitorBoot extends BootJob { @Override void run() { - MonitorService.I - .setEnableDump(CC.mp.monitor.dump_stack) - .setDumpLogDir(CC.mp.log_dir) - .start(); + MonitorService.I.start(); next(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 7fd5d62d..86bde932 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -19,9 +19,10 @@ package com.mpush.bootstrap.job; -import com.mpush.api.Server; -import com.mpush.tools.log.Logs; +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; import com.mpush.tools.thread.pool.ThreadPoolManager; import com.mpush.zk.ZKClient; import com.mpush.zk.node.ZKServerNode; @@ -47,7 +48,7 @@ public void run() { @Override public void run() { server.init(); - server.start(new Server.Listener() { + server.start(new Listener() { @Override public void onSuccess(Object... args) { Logs.Console.error("start " + serverName + " success listen:" + args[0]); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java index ee51ca9b..e8c33d1d 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -19,7 +19,8 @@ package com.mpush.bootstrap.job; -import com.mpush.api.exception.BootException; +import com.mpush.api.service.Listener; +import com.mpush.bootstrap.BootException; import com.mpush.zk.ZKClient; /** @@ -31,10 +32,16 @@ public class ZKBoot extends BootJob { @Override public void run() { - if (ZKClient.I.getZKConfig() != null) { - next(); - } else { - throw new BootException("init zk client failure"); - } + ZKClient.I.start(new Listener() { + @Override + public void onSuccess(Object... args) { + next(); + } + + @Override + public void onFailure(Throwable cause) { + throw new BootException("init zk client failure", cause); + } + }); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index d1f7d0cc..6a0abb71 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -21,69 +21,53 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.tools.log.Logs; import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import redis.clients.jedis.*; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public class RedisClient { - private static final int REDIS_TIMEOUT = 2000; - private static final int REDIS_MAX_TOTAL = 8; - private static final int REDIS_MAX_IDLE = 4; - private static final int REDIS_MIN_IDLE = 1; - private static final int REDIS_MAX_WAIT_MILLIS = 5000; - private static final int REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 300000; - private static final int REDIS_NUM_TESTS_PER_EVICTION_RUN = 3; - private static final int REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; - private static final boolean REDIS_TEST_ON_BORROW = false; - private static final boolean REDIS_TEST_ON_RETURN = false; - private static final boolean REDIS_TEST_WHILE_IDLE = false; - - public static final JedisPoolConfig CONFIG = new JedisPoolConfig(); + public static final JedisPoolConfig CONFIG = buildConfig(); private static final Map POOL_MAP = Maps.newConcurrentMap(); - static { + private static JedisPoolConfig buildConfig() { + JedisPoolConfig config = new JedisPoolConfig(); //连接池中最大连接数。高版本:maxTotal,低版本:maxActive - CONFIG.setMaxTotal(REDIS_MAX_TOTAL); + config.setMaxTotal(CC.mp.redis.config.maxTotal); //连接池中最大空闲的连接数 - CONFIG.setMaxIdle(REDIS_MAX_IDLE); + config.setMaxIdle(CC.mp.redis.config.maxIdle); //连接池中最少空闲的连接数 - CONFIG.setMinIdle(REDIS_MIN_IDLE); + config.setMinIdle(CC.mp.redis.config.minIdle); //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait - CONFIG.setMaxWaitMillis(REDIS_MAX_WAIT_MILLIS); + config.setMaxWaitMillis(CC.mp.redis.config.maxWaitMillis); //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 - CONFIG.setMinEvictableIdleTimeMillis(REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS); + config.setMinEvictableIdleTimeMillis(CC.mp.redis.config.minEvictableIdleTimeMillis); //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 - CONFIG.setNumTestsPerEvictionRun(REDIS_NUM_TESTS_PER_EVICTION_RUN); + config.setNumTestsPerEvictionRun(CC.mp.redis.config.numTestsPerEvictionRun); //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 - CONFIG.setTimeBetweenEvictionRunsMillis(REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); + config.setTimeBetweenEvictionRunsMillis(CC.mp.redis.config.timeBetweenEvictionRunsMillis); //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. - CONFIG.setTestOnBorrow(REDIS_TEST_ON_BORROW); + config.setTestOnBorrow(CC.mp.redis.config.testOnBorrow); //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 - CONFIG.setTestOnReturn(REDIS_TEST_ON_RETURN); + config.setTestOnReturn(CC.mp.redis.config.testOnReturn); //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. - CONFIG.setTestWhileIdle(REDIS_TEST_WHILE_IDLE); - } - - public static void main(String[] args) { - System.out.println(Jsons.toJson(CONFIG)); + config.setTestWhileIdle(CC.mp.redis.config.testWhileIdle); + return config; } public static Jedis getClient(RedisServer node) { JedisPool pool = POOL_MAP.get(node); if (pool == null) { - pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), REDIS_TIMEOUT, node.getPassword()); + pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), Protocol.DEFAULT_TIMEOUT, node.getPassword()); POOL_MAP.put(node, pool); } return pool.getResource(); } public static void close(Jedis jedis) { - jedis.close(); + if (jedis != null) jedis.close(); } public static long incr(List nodeList, String key, Integer time) { @@ -98,7 +82,7 @@ public static long incr(List nodeList, String key, Integer time) { } incrRet = ret; } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{},{},{},{}", key, time, node, e); + Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -117,7 +101,7 @@ public static long incrBy(List nodeList, String key, long delt) { long ret = jedis.incrBy(key, delt); incrRet = ret; } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{},{},{},{}", key, delt, node, e); + Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, delt, node, e); } finally { // 返还到连接池 close(jedis); @@ -143,7 +127,7 @@ public static T get(RedisServer node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - Logs.REDIS.error("redis get exception:{},{}", key, node, e); + Logs.REDIS.error("redis get exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -153,9 +137,7 @@ public static T get(RedisServer node, String key, Class clazz) { } public static void set(List nodeList, String key, String value) { - set(nodeList, key, value, null); - } public static void set(List nodeList, String key, T value) { @@ -186,7 +168,7 @@ public static void set(List nodeList, String key, String value, Int jedis.expire(key, time); } } catch (Exception e) { - Logs.REDIS.error("redis set exception:{},{},{},{}", key, value, time, node, e); + Logs.REDIS.error("redis set exception:{}, {}, {}, {}", key, value, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -202,7 +184,7 @@ public static void del(List nodeList, String key) { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - Logs.REDIS.error("redis del exception:{},{}", key, node, e); + Logs.REDIS.error("redis del exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -223,7 +205,7 @@ public static void hset(List nodeList, String namespace, String key jedis = getClient(node); jedis.hset(namespace, key, value); } catch (Exception e) { - Logs.REDIS.error("redis hset exception:{},{},{},{}", namespace, key, value, node, e); + Logs.REDIS.error("redis hset exception:{}, {}, {}, {}", namespace, key, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -243,7 +225,7 @@ public static T hget(RedisServer node, String namespace, String key, Class nodeList, String namespace, String key jedis = getClient(node); jedis.hdel(namespace, key); } catch (Exception e) { - Logs.REDIS.error("redis hdel exception:{},{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hdel exception:{}, {}, {}", namespace, key, node, e); } finally { // 返还到连接池 close(jedis); @@ -275,7 +257,7 @@ public static Map hgetAll(RedisServer node, String namespace) { jedis = getClient(node); result = jedis.hgetAll(namespace); } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); + Logs.REDIS.error("redis hgetAll exception:{}, {}", namespace, node, e); } finally { // 返还到连接池 close(jedis); @@ -284,17 +266,7 @@ public static Map hgetAll(RedisServer node, String namespace) { } public static Map hgetAll(RedisServer node, String namespace, Class clazz) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(namespace); - } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); - } finally { - // 返还到连接池 - close(jedis); - } + Map result = hgetAll(node, namespace); if (result != null) { Map newMap = Maps.newHashMap(); Iterator> iterator = result.entrySet().iterator(); @@ -308,7 +280,6 @@ public static Map hgetAll(RedisServer node, String namespace, Cla } else { return null; } - } /** @@ -355,14 +326,7 @@ public static List hmget(RedisServer node, String namespace, Class cla // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; + return toList(value, clazz); } @@ -517,14 +481,7 @@ public static List lrange(RedisServer node, String key, int start, int en // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; + return toList(value, clazz); } /** @@ -708,15 +665,7 @@ public static List sScan(RedisServer node, String key, Class clazz, in // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - + return toList(value, clazz); } /********************* @@ -797,6 +746,11 @@ public static List zrange(RedisServer node, String key, int start, int en // 返还到连接池 close(jedis); } + + return toList(value, clazz); + } + + private static List toList(Collection value, Class clazz) { if (value != null) { List newValue = Lists.newArrayList(); for (String temp : value) { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java new file mode 100644 index 00000000..d7237eef --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.cache.redis; + +/** + * Created by yxx on 2016/5/27. + * + * @author ohun@live.cn (夜色) + */ +public class RedisException extends RuntimeException { + + public RedisException() { + } + + public RedisException(Throwable cause) { + super(cause); + } + + public RedisException(String message) { + super(message); + } + + public RedisException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java index 60c254a3..88a4abdc 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java @@ -27,4 +27,5 @@ public class RedisServer extends com.mpush.tools.config.data.RedisServer { public RedisServer(String ip, int port, String password) { super(ip, port, password); } + } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java index f1fb4d0b..ee43587a 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java @@ -19,67 +19,67 @@ package com.mpush.cache.redis.hash; +import redis.clients.util.Hashing; + import java.util.Collection; import java.util.SortedMap; import java.util.TreeMap; -import redis.clients.util.Hashing; - public class ConsistentHash { - private final Hashing hash; - private final int numberOfReplicas; - private final SortedMap circle = new TreeMap(); + private final Hashing hash; + private final int numberOfReplicas; + private final SortedMap circle = new TreeMap(); + + public ConsistentHash(Hashing hash, int numberOfReplicas, + Collection nodes) { + super(); + this.hash = hash; + this.numberOfReplicas = numberOfReplicas; + for (Node node : nodes) { + add(node); + } + } - public ConsistentHash(Hashing hash, int numberOfReplicas, - Collection nodes) { - super(); - this.hash = hash; - this.numberOfReplicas = numberOfReplicas; - for (Node node : nodes) { - add(node); - } - } + /** + * 增加真实机器节点 + * + * @param node + */ + public void add(Node node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.put(this.hash.hash(node.toString() + i), node); + } + } - /** - * 增加真实机器节点 - * - * @param node - */ - public void add(Node node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.put(this.hash.hash(node.toString() + i), node); - } - } + /** + * 删除真实机器节点 + * + * @param node + */ + public void remove(String node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.remove(this.hash.hash(node.toString() + i)); + } + } - /** - * 删除真实机器节点 - * - * @param node - */ - public void remove(String node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.remove(this.hash.hash(node.toString() + i)); - } - } + /** + * 取得真实机器节点 + * + * @param key + * @return + */ + public Node get(String key) { + if (circle.isEmpty()) { + return null; + } + long hash = this.hash.hash(key); + if (!circle.containsKey(hash)) { + SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 + hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); + } + return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 + } - /** - * 取得真实机器节点 - * - * @param key - * @return - */ - public Node get(String key) { - if (circle.isEmpty()) { - return null; - } - long hash = this.hash.hash(key); - if (!circle.containsKey(hash)) { - SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 - hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); - } - return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 - } - } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java index 67afb2fb..295dfbf3 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java @@ -20,26 +20,29 @@ package com.mpush.cache.redis.hash; public class Node { - - private String ip; //机器ip - private String name;//名字 - - public Node(String ip, String name) { - this.ip = ip; - this.name = name; - } - - public String getIp() { - return ip; - } - public void setIp(String ip) { - this.ip = ip; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - + + private String ip; //机器ip + private String name;//名字 + + public Node(String ip, String name) { + this.ip = ip; + this.name = name; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java index 9dfd2dd3..014bfade 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -19,18 +19,17 @@ package com.mpush.cache.redis.listener; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; - import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.log.Logs; - import com.mpush.cache.redis.mq.Subscriber; +import com.mpush.tools.log.Logs; import com.mpush.tools.thread.pool.ThreadPoolManager; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; + public class ListenerDispatcher implements MessageListener { public static final ListenerDispatcher I = new ListenerDispatcher(); @@ -39,20 +38,21 @@ public class ListenerDispatcher implements MessageListener { private final Executor executor = ThreadPoolManager.I.getRedisExecutor(); - private ListenerDispatcher(){} + private ListenerDispatcher() { + } @Override public void onMessage(final String channel, final String message) { List listeners = subscribes.get(channel); if (listeners == null) { - Logs.REDIS.info("cannot find listener:%s,%s", channel,message); + Logs.REDIS.info("cannot find listener:%s,%s", channel, message); return; } for (final MessageListener listener : listeners) { executor.execute(new Runnable() { @Override public void run() { - listener.onMessage(channel, message); + listener.onMessage(channel, message); } }); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java index 7ebb2bd7..da6989d1 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java @@ -21,7 +21,7 @@ public interface MessageListener { - - void onMessage(String channel, String message); + + void onMessage(String channel, String message); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index 91e775df..54abe851 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -21,6 +21,7 @@ import com.google.common.base.Strings; import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisException; import com.mpush.cache.redis.RedisGroup; import com.mpush.cache.redis.RedisServer; import com.mpush.tools.Jsons; @@ -51,6 +52,7 @@ private ZKRedisClusterManager() { @Override public void init() { Logs.Console.error("begin init redis cluster"); + if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); List groupList = CC.mp.redis.cluster_group; if (groupList.size() > 0) { if (CC.mp.redis.write_to_zk) { @@ -67,12 +69,12 @@ public void init() { Collection nodes = watcher.getCache().values(); if (nodes == null || nodes.isEmpty()) { Logs.REDIS.info("init redis client error, redis server is none."); - throw new RuntimeException("init redis client error, redis server is none."); + throw new RedisException("init redis client error, redis server is none."); } for (ZKRedisNode node : nodes) { groups.add(RedisGroup.from(node)); } - if (groups.isEmpty()) throw new RuntimeException("init redis sever fail groupList is null"); + if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); Logs.Console.error("init redis cluster success..."); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java index 6f8e9c61..95afbcf6 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java @@ -20,65 +20,66 @@ package com.mpush.cache.redis.mq; import com.mpush.cache.redis.listener.ListenerDispatcher; -import com.mpush.tools.log.Logs; import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; import redis.clients.jedis.JedisPubSub; public class Subscriber extends JedisPubSub { private static ListenerDispatcher dispatcher = ListenerDispatcher.I; - + public static Subscriber holder = new Subscriber(); - - private Subscriber(){} - + + private Subscriber() { + } + @Override public void onMessage(String channel, String message) { - Logs.REDIS.info("onMessage:{},{}", channel,message); + Logs.REDIS.info("onMessage:{},{}", channel, message); dispatcher.onMessage(channel, message); super.onMessage(channel, message); } @Override public void onPMessage(String pattern, String channel, String message) { - Logs.REDIS.info("onPMessage:{},{},{}",pattern,channel,message); + Logs.REDIS.info("onPMessage:{},{},{}", pattern, channel, message); super.onPMessage(pattern, channel, message); } @Override public void onPSubscribe(String pattern, int subscribedChannels) { - Logs.REDIS.info("onPSubscribe:{},{}",pattern,subscribedChannels); + Logs.REDIS.info("onPSubscribe:{},{}", pattern, subscribedChannels); super.onPSubscribe(pattern, subscribedChannels); } @Override public void onPUnsubscribe(String pattern, int subscribedChannels) { - Logs.REDIS.info("onPUnsubscribe:{},{}",pattern,subscribedChannels); + Logs.REDIS.info("onPUnsubscribe:{},{}", pattern, subscribedChannels); super.onPUnsubscribe(pattern, subscribedChannels); } @Override public void onSubscribe(String channel, int subscribedChannels) { - Logs.REDIS.info("onSubscribe:{},{}",channel,subscribedChannels); + Logs.REDIS.info("onSubscribe:{},{}", channel, subscribedChannels); super.onSubscribe(channel, subscribedChannels); } @Override public void onUnsubscribe(String channel, int subscribedChannels) { - Logs.REDIS.info("onUnsubscribe:{},{}",channel,subscribedChannels); + Logs.REDIS.info("onUnsubscribe:{},{}", channel, subscribedChannels); super.onUnsubscribe(channel, subscribedChannels); } @Override public void unsubscribe() { - Logs.REDIS.info("unsubscribe"); + Logs.REDIS.info("unsubscribe"); super.unsubscribe(); } @Override public void unsubscribe(String... channels) { - Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); + Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); super.unsubscribe(channels); } diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 20d01d45..5f5c7cc7 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -21,15 +21,17 @@ import com.google.common.collect.Maps; -import com.mpush.cache.redis.RedisKey; import com.mpush.api.connection.Connection; +import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; +import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.message.*; import com.mpush.common.security.AesCipher; import com.mpush.common.security.CipherBox; import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.event.EventBus; import com.mpush.tools.thread.PoolThreadFactory; import com.mpush.tools.thread.ThreadNames; import io.netty.channel.*; @@ -37,7 +39,6 @@ import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; -import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,10 +53,10 @@ @ChannelHandler.Sharable public final class ConnClientChannelHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); - private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.NETTY_TIMER)); + private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.T_NETTY_TIMER)); - private Connection connection = new NettyConnection(); - private ClientConfig clientConfig; + private final Connection connection = new NettyConnection(); + private final ClientConfig clientConfig; public ConnClientChannelHandler(ClientConfig clientConfig) { this.clientConfig = clientConfig; @@ -116,7 +117,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } - LOGGER.warn("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); + LOGGER.warn("update currentTime:" + ctx.channel() + "," + msg); } @Override @@ -135,7 +136,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { connection.close(); - LOGGER.info("client disconnect channel={}", ctx.channel()); + EventBus.I.post(new ConnectionCloseEvent(connection)); + LOGGER.info("client disconnect connection={}", connection); } private void tryFastConnect() { @@ -166,14 +168,11 @@ private void tryFastConnect() { message.deviceId = clientConfig.getDeviceId(); message.sessionId = sessionId; - message.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) throws Exception { - if (channelFuture.isSuccess()) { - clientConfig.setCipher(cipher); - } else { - handshake(clientConfig); - } + message.sendRaw(channelFuture -> { + if (channelFuture.isSuccess()) { + clientConfig.setCipher(cipher); + } else { + handshake(clientConfig); } }); } @@ -210,33 +209,27 @@ private void handshake(ClientConfig client) { message.send(); } - public void startHeartBeat(final int heartbeat) throws Exception { HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { + final TimerTask self = this; final Channel channel = connection.getChannel(); - - try { + if (channel.isActive()) { ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); channelFuture.addListener(new ChannelFutureListener() { - @Override public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - LOGGER.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); - } - LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString()); - } else { + if (future.isSuccess()) { LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + } else { + LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); } + HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); } }); - } finally { - if (channel.isActive()) { - HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); - } + } else { + LOGGER.error("connection was closed, connection={}", connection); } } }, heartbeat, TimeUnit.MILLISECONDS); diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java index aab6382b..0a69d57d 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java @@ -19,16 +19,19 @@ package com.mpush.client.connect; +import com.google.common.eventbus.Subscribe; +import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.netty.client.NettyClient; +import com.mpush.tools.event.EventBus; import io.netty.channel.ChannelHandler; public class ConnectClient extends NettyClient { - private final ConnClientChannelHandler handler; public ConnectClient(String host, int port, ClientConfig config) { super(host, port); handler = new ConnClientChannelHandler(config); + EventBus.I.register(this); } @Override @@ -36,4 +39,9 @@ public ChannelHandler getChannelHandler() { return handler; } + @Subscribe + void on(ConnectionCloseEvent event) { + this.stop(); + } + } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java index ac56df15..fbe13473 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -20,8 +20,16 @@ package com.mpush.client.gateway; import com.mpush.api.connection.Connection; +import com.mpush.api.service.Listener; import com.mpush.netty.client.NettyClient; +import com.sun.istack.internal.NotNull; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_client.*; /** * Created by yxx on 2016/5/17. @@ -30,9 +38,17 @@ */ public class GatewayClient extends NettyClient { private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); + private GlobalChannelTrafficShapingHandler trafficShapingHandler; public GatewayClient(String host, int port) { super(host, port); + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override @@ -43,4 +59,20 @@ public ChannelHandler getChannelHandler() { public Connection getConnection() { return handler.getConnection(); } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + + @Override + protected void doStop(@NotNull Listener listener) throws Throwable { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } + super.doStop(listener); + } } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java deleted file mode 100644 index cd763bd2..00000000 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientBoot.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.client.gateway; - -import com.mpush.api.connection.Connection; -import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeWatcher; - -/** - * Created by yxx on 2016/5/17. - * - * @author ohun@live.cn - */ -public class GatewayClientBoot { - - public void start() { - - } - - -} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java index 040a54da..cfb9bcd9 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -23,10 +23,11 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.common.message.ErrorMessage; -import com.mpush.netty.connection.NettyConnection; import com.mpush.client.push.PushRequest; import com.mpush.client.push.PushRequestBus; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.netty.connection.NettyConnection; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; @@ -45,34 +46,53 @@ public final class GatewayClientChannelHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); - private Connection connection = new NettyConnection(); + private final Connection connection = new NettyConnection(); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + LOGGER.info("receive gateway packet={}, channel={}", msg, ctx.channel()); connection.updateLastReadTime(); if (msg instanceof Packet) { - Packet packet = ((Packet) msg); - PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); - return; + Packet packet = (Packet) msg; + if (packet.cmd == Command.OK.cmd) { + handleOK(new OkMessage(packet, connection)); + } else if (packet.cmd == Command.ERROR.cmd) { + handleError(new ErrorMessage(packet, connection)); } + } + } - if (packet.cmd == Command.OK.cmd) { - request.success(); - } else { - ErrorMessage message = new ErrorMessage(packet, connection); - if (message.code == OFFLINE.errorCode) { - request.offline(); - } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { - request.failure(); - } else if (message.code == ROUTER_CHANGE.errorCode) { - request.redirect(); - } - LOGGER.warn("receive an error gateway response, message={}", message); + private void handleOK(OkMessage message) { + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + handPush(message, null, message.getPacket()); + } + } + + private void handleError(ErrorMessage message) { + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + handPush(null, message, message.getPacket()); + } + } + + private void handPush(OkMessage ok, ErrorMessage error, Packet packet) { + PushRequest request = PushRequestBus.I.getAndRemove(packet.sessionId); + if (request == null) { + LOGGER.warn("receive a gateway response, but request has timeout. ok={}, error={}", ok, error); + return; + } + + if (ok != null) {//推送成功 + request.success(); + } else if (error != null) {//推送失败 + LOGGER.warn("receive an error gateway response, message={}", error); + if (error.code == OFFLINE.errorCode) {//用户离线 + request.offline(); + } else if (error.code == PUSH_CLIENT_FAILURE.errorCode) {//下发到客户端失败 + request.failure(); + } else if (error.code == ROUTER_CHANGE.errorCode) {//用户路由信息更改 + request.redirect(); } } - LOGGER.info("receive msg:" + ctx.channel() + "," + msg); } @Override @@ -91,6 +111,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception { connection.close(); LOGGER.info("client disconnect channel={}", ctx.channel()); + //TODO notify gateway-client-factory to removeAndClose this gateway-client } public Connection getConnection() { diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java index 089e8999..6714c4d9 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java @@ -20,9 +20,9 @@ package com.mpush.client.gateway; import com.google.common.collect.Maps; -import com.mpush.api.Client; -import com.mpush.api.Service; import com.mpush.api.connection.Connection; +import com.mpush.api.service.Client; +import com.mpush.api.service.Listener; import com.mpush.zk.cache.ZKServerNodeCache; import com.mpush.zk.node.ZKServerNode; import org.slf4j.Logger; @@ -45,13 +45,14 @@ public class GatewayClientFactory extends ZKServerNodeCache { @Override public void put(String fullPath, ZKServerNode node) { super.put(fullPath, node); - addClient(node); + addClient(node.getIp(), node.getPort()); } @Override public ZKServerNode remove(String fullPath) { ZKServerNode node = super.remove(fullPath); removeClient(node); + logger.warn("Gateway Server zkNode={} was removed.", node); return node; } @@ -76,7 +77,27 @@ public Connection getConnection(String ip) { if (client == null) { return null;//TODO create client } - return client.getConnection(); + Connection connection = client.getConnection(); + if (connection.isConnected()) { + return connection; + } + restartClient(client); + return null; + } + + private void restartClient(final GatewayClient client) { + ip_client.remove(client.getHost()); + client.stop(new Listener() { + @Override + public void onSuccess(Object... args) { + addClient(client.getHost(), client.getPort()); + } + + @Override + public void onFailure(Throwable cause) { + addClient(client.getHost(), client.getPort()); + } + }); } private void removeClient(ZKServerNode node) { @@ -88,17 +109,17 @@ private void removeClient(ZKServerNode node) { } } - private void addClient(final ZKServerNode node) { - final GatewayClient client = new GatewayClient(node.getIp(), node.getPort()); - client.start(new Service.Listener() { + private void addClient(final String host, final int port) { + final GatewayClient client = new GatewayClient(host, port); + client.start(new Listener() { @Override public void onSuccess(Object... args) { - ip_client.put(node.getIp(), client); + ip_client.put(host, client); } @Override public void onFailure(Throwable cause) { - logger.error("create gateway client ex, node", node, cause); + logger.error("create gateway client ex, client={}", client, cause); } }); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 133eb404..0fecb3cd 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -20,17 +20,21 @@ package com.mpush.client.push; import com.google.common.base.Strings; -import com.mpush.api.BaseService; import com.mpush.api.connection.Connection; import com.mpush.api.push.PushSender; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.gateway.GatewayClientFactory; import com.mpush.zk.ZKClient; import com.mpush.zk.ZKPath; import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.sun.istack.internal.NotNull; import java.util.Collection; +import static com.mpush.zk.ZKPath.GATEWAY_SERVER; + /*package*/ class PushClient extends BaseService implements PushSender { private static final int DEFAULT_TIMEOUT = 3000; private final GatewayClientFactory factory = GatewayClientFactory.I; @@ -64,22 +68,16 @@ public Connection getGatewayConnection(String host) { } @Override - public void start(Listener listener) { - if (started.compareAndSet(false, true)) { - ZKClient.I.init(); - RedisManager.I.init(); - ZKServerNodeWatcher.build(ZKPath.GATEWAY_SERVER, factory).beginWatch(); - if (listener != null) { - listener.onSuccess(0); - } - } + protected void doStart(@NotNull Listener listener) throws Throwable { + ZKClient.I.start(listener); + RedisManager.I.init(); + ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).beginWatch(); } @Override - public void stop(Listener listener) { - if (started.compareAndSet(true, false)) { - factory.clear(); - } + protected void doStop(@NotNull Listener listener) throws Throwable { + factory.clear(); + ZKClient.I.stop(listener); } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index d12228fe..ed379265 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -19,207 +19,199 @@ package com.mpush.client.push; -import com.google.common.collect.Maps; -import com.mpush.api.push.PushSender; import com.mpush.api.connection.Connection; +import com.mpush.api.push.PushSender; import com.mpush.api.router.ClientLocation; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.common.router.ConnectionRouterManager; import com.mpush.common.router.RemoteRouter; -import com.mpush.tools.Jsons; +import com.mpush.tools.common.TimeLine; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicReference; /** * Created by ohun on 2015/12/30. * * @author ohun@live.cn */ -public class PushRequest implements PushSender.Callback, Runnable { +public class PushRequest extends FutureTask implements Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); + private enum Status {init, success, failure, offline, timeout} + + private static final Callable NONE = () -> Boolean.FALSE; + + private final AtomicReference status = new AtomicReference<>(Status.init); + private final TimeLine timeLine = new TimeLine("Push-Time-Line"); + private final PushClient client; private PushSender.Callback callback; private String userId; private String content; private long timeout; - private long timeout_; - private int sessionId; - private long sendTime; - private AtomicInteger status = new AtomicInteger(0); - private Map times = Maps.newHashMap(); + private ClientLocation location; + private Future future; - public PushRequest(PushClient client) { - this.client = client; - } - - public static PushRequest build(PushClient client) { - return new PushRequest(client); - } - - public PushRequest setCallback(PushSender.Callback callback) { - this.callback = callback; - return this; - } + private void sendToConnServer() { + timeLine.addTimePoint("lookup-remote"); - public PushRequest setUserId(String userId) { - this.userId = userId; - return this; - } + //1.查询用户长连接所在的机器 + RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); + if (router == null) { + //1.1没有查到说明用户已经下线 + offline(); + return; + } - public PushRequest setContent(String content) { - this.content = content; - return this; - } + timeLine.addTimePoint("get-gateway-conn"); - public PushRequest setTimeout(long timeout) { - this.timeout = timeout; - return this; - } - @Override - public void onSuccess(String userId) { - putTime("success"); - LOGGER.info("success,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(1); - } + //2.通过网关连接,把消息发送到所在机器 + location = router.getRouteValue(); + Connection gatewayConn = client.getGatewayConnection(location.getHost()); + if (gatewayConn == null) { + LOGGER.error("get gateway connection failure, location={}", location); + failure(); + return; + } - @Override - public void onFailure(String userId) { - putTime("failure"); - LOGGER.info("failure,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(2); - } + timeLine.addTimePoint("send-to-gateway-begin"); - @Override - public void onOffline(String userId) { - putTime("offline"); - LOGGER.info("offline,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(3); - } + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); + timeLine.addTimePoint("put-request-bus"); + future = PushRequestBus.I.put(pushMessage.getSessionId(), this); - @Override - public void onTimeout(String userId) { - putTime("timeout"); - LOGGER.info("timeout,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(4); + pushMessage.sendRaw(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + timeLine.addTimePoint("send-to-gateway-end"); + if (future.isSuccess()) { + } else { + failure(); + } + } + }); } - private void submit(int status) { - if (this.status.compareAndSet(0, status)) {//防止重复调用 + private void submit(Status status) { + if (this.status.compareAndSet(Status.init, status)) {//防止重复调用 + if (future != null) future.cancel(true); if (callback != null) { - PushRequestBus.INSTANCE.getExecutor().execute(this); + PushRequestBus.I.asyncCall(this); } else { LOGGER.warn("callback is null"); } } + timeLine.end(); + LOGGER.info("push request {} end, userId={}, content={}, location={}, timeLine={}" + , status, userId, content, location, timeLine); } @Override public void run() { switch (status.get()) { - case 1: + case success: callback.onSuccess(userId); break; - case 2: + case failure: callback.onFailure(userId); break; - case 3: + case offline: callback.onOffline(userId); break; - case 4: + case timeout: callback.onTimeout(userId); break; + case init://从定时任务过来的,超时时间到了 + submit(Status.timeout); + break; + } + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + throw new UnsupportedOperationException(); + } + + public void send() { + timeLine.begin(); + sendToConnServer(); + } + + public void redirect() { + timeLine.addTimePoint("redirect"); + LOGGER.warn("user route has changed, userId={}, location={}", userId, location); + ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + if (status.get() == Status.init) {//表示任务还没有完成,还可以重新发送 + send(); } } - public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; + public long getTimeout() { + return timeout; } public void timeout() { - onTimeout(userId); + submit(Status.timeout); } public void success() { - onSuccess(userId); + submit(Status.success); } public void failure() { - onFailure(userId); + submit(Status.failure); } public void offline() { ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - onOffline(userId); + submit(Status.offline); } - public void send() { - this.timeout_ = timeout + System.currentTimeMillis(); - putTime("startsend"); - sendToConnServer(); + public PushRequest(PushClient client) { + super(NONE); + this.client = client; } - public void redirect() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - LOGGER.warn("user route has changed, userId={}, content={}", userId, content); - if (status.get() == 0) { - send(); - } + public static PushRequest build(PushClient client) { + return new PushRequest(client); } - private void sendToConnServer() { - //1.查询用户长连接所在的机器 - RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); - if (router == null) { - //1.1没有查到说明用户已经下线 - this.onOffline(userId); - return; - } - - //2.通过网关连接,把消息发送到所在机器 - ClientLocation location = router.getRouteValue(); - Connection gatewayConn = client.getGatewayConnection(location.getHost()); - if (gatewayConn == null || !gatewayConn.isConnected()) { - this.onFailure(userId); - return; - } - - putTime("sendtoconnserver"); - - final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); - pushMessage.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - putTime("sendsuccess"); - } else { - PushRequest.this.onFailure(userId); - } - } - }); - - sessionId = pushMessage.getSessionId(); - putTime("putrequestbus"); - PushRequestBus.INSTANCE.put(sessionId, this); + public PushRequest setCallback(PushSender.Callback callback) { + this.callback = callback; + return this; } - public long getSendTime() { - return sendTime; + public PushRequest setUserId(String userId) { + this.userId = userId; + return this; } - public Map getTimes() { - return times; + public PushRequest setContent(String content) { + this.content = content; + return this; } - public void putTime(String key) { - this.times.put(key, System.currentTimeMillis()); + public PushRequest setTimeout(long timeout) { + this.timeout = timeout; + return this; } + @Override + public String toString() { + return "PushRequest{" + + "content='" + content + '\'' + + ", userId='" + userId + '\'' + + ", timeout=" + timeout + + ", location=" + location + + '}'; + } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 688060b2..fbb846c0 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -19,50 +19,47 @@ package com.mpush.client.push; +import com.mpush.api.push.PushException; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.util.Iterator; import java.util.Map; import java.util.concurrent.*; +import static com.mpush.tools.thread.ThreadNames.T_PUSH_REQ_TIMER; + /** * Created by ohun on 2015/12/30. * * @author ohun@live.cn */ -public class PushRequestBus implements Runnable { - - public static final PushRequestBus INSTANCE = new PushRequestBus(); - private Map requests = new ConcurrentHashMapV8<>(1024); - private Executor executor = Executors.newFixedThreadPool(5);//test - private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test +public class PushRequestBus { + public static final PushRequestBus I = new PushRequestBus(); + private final Logger logger = LoggerFactory.getLogger(PushRequestBus.class); + private final Map reqQueue = new ConcurrentHashMapV8<>(1024); + private final Executor executor = ThreadPoolManager.I.getPushCallbackExecutor(); + private final ScheduledExecutorService scheduledExecutor; private PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); - } - - public void put(int sessionId, PushRequest request) { - requests.put(sessionId, request); + scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { + logger.error("one push request was rejected, request=" + r); + throw new PushException("one push request was rejected. request=" + r); + }); } - public PushRequest remove(int sessionId) { - return requests.remove(sessionId); + public Future put(int sessionId, PushRequest request) { + reqQueue.put(sessionId, request); + return scheduledExecutor.schedule(request, request.getTimeout(), TimeUnit.MILLISECONDS); } - public Executor getExecutor() { - return executor; + public PushRequest getAndRemove(int sessionId) { + return reqQueue.remove(sessionId); } - @Override - public void run() { - if (requests.isEmpty()) return; - Iterator it = requests.values().iterator(); - while (it.hasNext()) { - PushRequest request = it.next(); - if (request.isTimeout()) { - it.remove();//清除超时的请求 - request.timeout(); - } - } + public void asyncCall(Runnable runnable) { + executor.execute(runnable); } } diff --git a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java index 6aa428b3..6bcc470f 100644 --- a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java @@ -29,6 +29,7 @@ public enum ErrorCode { PUSH_CLIENT_FAILURE(2, "push to client failure"), ROUTER_CHANGE(3, "router change"), DISPATCH_ERROR(100, "handle message error"), + UNSUPPORTED_CMD(101, "unsupported command"), UNKNOWN(-1, "unknown"); ErrorCode(int code, String errorMsg) { diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index c2aeedb9..4cfda9b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -25,14 +25,17 @@ import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import com.mpush.common.message.ErrorMessage; -import com.mpush.tools.Profiler; - +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import static com.mpush.common.ErrorCode.DISPATCH_ERROR; +import static com.mpush.common.ErrorCode.UNSUPPORTED_CMD; + /** * Created by ohun on 2015/12/22. * @@ -46,25 +49,30 @@ public void register(Command command, MessageHandler handler) { handlers.put(command.cmd, handler); } - @Override public void onReceive(Packet packet, Connection connection) { - MessageHandler handler = handlers.get(packet.cmd); - try { - if (handler != null) { - Profiler.enter("start handle:"+handler.getClass().getSimpleName()); + MessageHandler handler = handlers.get(packet.cmd); + if (handler != null) { + try { + Profiler.enter("start handle:" + handler.getClass().getSimpleName()); handler.handle(packet, connection); + } catch (Throwable throwable) { + LOGGER.error("dispatch message ex, packet={}, connect={}, body={}" + , packet, connection, Arrays.toString(packet.body), throwable); + ErrorMessage + .from(packet, connection) + .setErrorCode(DISPATCH_ERROR) + .close(); + } finally { + Profiler.release(); } - } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={}, connect={}", packet, connection, throwable); + } else { + LOGGER.error("dispatch message failure unsupported cmd, packet={}, connect={}, body={}" + , packet, connection); ErrorMessage .from(packet, connection) - .setErrorCode(ErrorCode.DISPATCH_ERROR) + .setErrorCode(UNSUPPORTED_CMD) .close(); - }finally{ - if(handler!=null){ - Profiler.release(); - } } } } diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index af2dc2d9..181e3d5c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -24,7 +24,7 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.tools.Profiler; +import com.mpush.tools.common.Profiler; /** * Created by ohun on 2015/12/22. diff --git a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java index d537c452..7d77b11c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java @@ -20,8 +20,8 @@ package com.mpush.common.handler; import com.mpush.api.connection.Connection; -import com.mpush.common.message.OkMessage; import com.mpush.api.protocol.Packet; +import com.mpush.common.message.OkMessage; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 4996084e..59de5a69 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -23,8 +23,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.tools.IOUtils; -import com.mpush.tools.Profiler; +import com.mpush.tools.common.IOUtils; import com.mpush.tools.config.CC; import io.netty.channel.ChannelFutureListener; @@ -43,12 +42,7 @@ public abstract class BaseMessage implements Message { public BaseMessage(Packet packet, Connection connection) { this.packet = packet; this.connection = connection; - Profiler.enter("start decode message"); - try { - decodeBody(); - } finally { - Profiler.release(); - } + decodeBody(); } protected void decodeBody() { @@ -62,7 +56,7 @@ protected void decodeBody() { } //2.解压 if (packet.hasFlag(Packet.FLAG_COMPRESS)) { - tmp = IOUtils.uncompress(tmp); + tmp = IOUtils.decompress(tmp); } if (tmp.length == 0) { @@ -150,10 +144,5 @@ public int getSessionId() { } @Override - public String toString() { - return "BaseMessage{" + - "packet=" + packet + - ", connection=" + connection + - '}'; - } + public abstract String toString(); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index 31f1349f..18a10f6a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -55,4 +55,14 @@ public void encode(ByteBuf body) { encodeString(body, alias); encodeString(body, tags); } + + @Override + public String toString() { + return "BindUserMessage{" + + "alias='" + alias + '\'' + + ", userId='" + userId + '\'' + + ", tags='" + tags + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index 2682d8f3..bb7ecb83 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -19,8 +19,8 @@ package com.mpush.common.message; -import com.mpush.api.connection.Connection; import com.mpush.api.Constants; +import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java index ed7899f9..b1cbf8b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java @@ -59,4 +59,15 @@ public void encode(ByteBuf body) { encodeInt(body, minHeartbeat); encodeInt(body, maxHeartbeat); } + + @Override + public String toString() { + return "FastConnectMessage{" + + "deviceId='" + deviceId + '\'' + + ", sessionId='" + sessionId + '\'' + + ", minHeartbeat=" + minHeartbeat + + ", maxHeartbeat=" + maxHeartbeat + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index bb027331..917f206c 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -53,4 +53,12 @@ public FastConnectOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; } + + @Override + public String toString() { + return "FastConnectOkMessage{" + + "heartbeat=" + heartbeat + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 1eb1f62e..02d20c5b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -23,6 +23,8 @@ import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Arrays; + import static com.mpush.api.protocol.Command.HANDSHAKE; /** @@ -73,4 +75,20 @@ public void encode(ByteBuf body) { encodeInt(body, maxHeartbeat); encodeLong(body, timestamp); } + + @Override + public String toString() { + return "HandshakeMessage{" + + "clientKey=" + Arrays.toString(clientKey) + + ", deviceId='" + deviceId + '\'' + + ", osName='" + osName + '\'' + + ", osVersion='" + osVersion + '\'' + + ", clientVersion='" + clientVersion + '\'' + + ", iv=" + Arrays.toString(iv) + + ", minHeartbeat=" + minHeartbeat + + ", maxHeartbeat=" + maxHeartbeat + + ", timestamp=" + timestamp + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index 39af35d1..26fa9ca3 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -23,6 +23,8 @@ import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Arrays; + /** * Created by ohun on 2015/12/27. * @@ -77,4 +79,15 @@ public HandshakeOkMessage setExpireTime(long expireTime) { this.expireTime = expireTime; return this; } + + @Override + public String toString() { + return "HandshakeOkMessage{" + + "expireTime=" + expireTime + + ", serverKey=" + Arrays.toString(serverKey) + + ", heartbeat=" + heartbeat + + ", sessionId='" + sessionId + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index d9d58ba6..0102f5c4 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -22,7 +22,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; import java.util.Map; @@ -50,7 +50,7 @@ public HttpRequestMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { method = decodeByte(body); uri = decodeString(body); - headers = MPushUtil.headerFromString(decodeString(body)); + headers = Utils.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -58,7 +58,7 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeByte(body, method); encodeString(body, uri); - encodeString(body, MPushUtil.headerToString(headers)); + encodeString(body, Utils.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 02a414bb..4fde32d6 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -21,7 +21,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; import java.util.HashMap; @@ -46,7 +46,7 @@ public HttpResponseMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { statusCode = decodeInt(body); reasonPhrase = decodeString(body); - headers = MPushUtil.headerFromString(decodeString(body)); + headers = Utils.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -54,7 +54,7 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeInt(body, statusCode); encodeString(body, reasonPhrase); - encodeString(body, MPushUtil.headerToString(headers)); + encodeString(body, Utils.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index 3a61f39a..fed4a788 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -52,4 +52,12 @@ public void decode(byte[] body) { public byte[] encode() { return content == null ? null : content.getBytes(Constants.UTF_8); } + + @Override + public String toString() { + return "PushMessage{" + + "content='" + content + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java index dc22161a..3c55a211 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java @@ -21,7 +21,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.api.BaseService; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; import com.mpush.tools.config.data.DnsMapping; @@ -34,7 +35,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import static com.mpush.tools.MPushUtil.checkHealth; +import static com.mpush.tools.Utils.checkHealth; public class DnsMappingManager extends BaseService implements Runnable { private final Logger logger = LoggerFactory.getLogger(DnsMappingManager.class); @@ -50,19 +51,16 @@ private DnsMappingManager() { private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); @Override - public void start(Listener listener) { - if (started.compareAndSet(false, true)) { - scheduledExecutorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns - } + protected void doStart(Listener listener) throws Throwable { + scheduledExecutorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns } @Override - public void stop(Listener listener) { - if (started.compareAndSet(true, false)) { - scheduledExecutorService.shutdown(); - } + protected void doStop(Listener listener) throws Throwable { + scheduledExecutorService.shutdown(); } + @Override public void init() { logger.error("start init dnsMapping"); @@ -84,7 +82,7 @@ public Map> getAll() { return all; } - public DnsMapping translate(String origin) { + public DnsMapping lookup(String origin) { if (available.isEmpty()) return null; List list = available.get(origin); diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index 4df7437c..d7d9efed 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -19,8 +19,8 @@ package com.mpush.common.router; -import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.Router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index e67528f4..fa36618e 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -19,10 +19,9 @@ package com.mpush.common.router; -import com.mpush.cache.redis.RedisKey; import com.mpush.api.router.RouterManager; +import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,8 +35,8 @@ public class RemoteRouterManager implements RouterManager { @Override public RemoteRouter register(String userId, RemoteRouter router) { - LOGGER.info("register remote router success userId={}, router={}", userId, router); - String key = RedisKey.getUserKey(userId); + LOGGER.info("register remote router success userId={}, router={}", userId, router); + String key = RedisKey.getUserKey(userId); RemoteRouter old = RedisManager.I.get(key, RemoteRouter.class); if (old != null) { RedisManager.I.del(key); @@ -48,7 +47,7 @@ public RemoteRouter register(String userId, RemoteRouter router) { @Override public boolean unRegister(String userId) { - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserKey(userId); RedisManager.I.del(key); LOGGER.info("unRegister remote router success userId={}", userId); return true; @@ -56,7 +55,7 @@ public boolean unRegister(String userId) { @Override public RemoteRouter lookup(String userId) { - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserKey(userId); return RedisManager.I.get(key, RemoteRouter.class); } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index e4212060..e619ddf4 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -22,7 +22,7 @@ import com.mpush.cache.redis.listener.ListenerDispatcher; import com.mpush.cache.redis.listener.MessageListener; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,11 +42,11 @@ public class UserChangeListener extends EventConsumer implements MessageListener //只需要一台机器注册online、offline 消息通道 public UserChangeListener() { - if ("127.0.0.1".equals(MPushUtil.getLocalIp())) { + if ("127.0.0.1".equals(Utils.getLocalIp())) { ListenerDispatcher.I.subscribe(getOnlineChannel(), this); ListenerDispatcher.I.subscribe(getOfflineChannel(), this); } else { - LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", MPushUtil.getLocalIp()); + LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", Utils.getLocalIp()); } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 70d42d27..52c6e4b6 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -20,9 +20,13 @@ package com.mpush.common.security; import com.mpush.api.connection.Cipher; -import com.mpush.tools.Profiler; import com.mpush.tools.crypto.AESUtils; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import static com.mpush.tools.crypto.AESUtils.KEY_ALGORITHM; + /** * Created by ohun on 2015/12/28. @@ -32,30 +36,25 @@ public final class AesCipher implements Cipher { public final byte[] key; public final byte[] iv; + private final IvParameterSpec zeroIv; + private final SecretKeySpec keySpec; public AesCipher(byte[] key, byte[] iv) { this.key = key; this.iv = iv; + this.zeroIv = new IvParameterSpec(iv); + this.keySpec = new SecretKeySpec(key, KEY_ALGORITHM); } + @Override - public byte[] decrypt(byte[] data) { - try { - Profiler.enter("start aes decrypt"); - return AESUtils.decrypt(data, key, iv); - } finally { - Profiler.release(); - } + public byte[] encrypt(byte[] data) { + return AESUtils.encrypt(data, zeroIv, keySpec); } @Override - public byte[] encrypt(byte[] data) { - try { - Profiler.enter("start encrypt"); - return AESUtils.encrypt(data, key, iv); - } finally { - Profiler.release(); - } + public byte[] decrypt(byte[] data) { + return AESUtils.decrypt(data, zeroIv, keySpec); } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index a8863739..c471a5d5 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -32,7 +32,7 @@ * @author ohun@live.cn */ public final class CipherBox { - public int aesKeyLength = CC.mp.security.aes_key_length; + public final int aesKeyLength = CC.mp.security.aes_key_length; public static final CipherBox I = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index 82a30052..4acfa4b9 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -20,7 +20,6 @@ package com.mpush.common.security; import com.mpush.api.connection.Cipher; -import com.mpush.tools.Profiler; import com.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; @@ -42,23 +41,12 @@ public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @Override public byte[] decrypt(byte[] data) { - try { - Profiler.enter("start rsa decrypt"); - return RSAUtils.decryptByPrivateKey(data, privateKey); - } finally { - Profiler.release(); - } - + return RSAUtils.decryptByPrivateKey(data, privateKey); } @Override public byte[] encrypt(byte[] data) { - try { - Profiler.enter("start rsa encrypt"); - return RSAUtils.encryptByPublicKey(data, publicKey); - } finally { - Profiler.release(); - } + return RSAUtils.encryptByPublicKey(data, publicKey); } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java index 28a0ab76..204751f9 100644 --- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -19,21 +19,20 @@ package com.mpush.common.user; -import java.util.List; - -import com.mpush.tools.MPushUtil; +import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.cache.redis.RedisKey; +import java.util.List; //查询使用 public final class UserManager { private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); public static final UserManager INSTANCE = new UserManager(); - private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); + private final String ONLINE_KEY = RedisKey.getUserOnlineKey(Utils.getExtranetAddress()); public UserManager() { clearUserOnlineData(); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 5f00cc15..f47d66c3 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -20,14 +20,14 @@ package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.api.Service; +import com.mpush.api.service.Listener; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.router.RemoteRouter; import com.mpush.core.router.RouterCenter; import com.mpush.core.server.AdminServer; import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import com.mpush.tools.config.CC; import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKClient; @@ -76,7 +76,7 @@ protected void messageReceived(ChannelHandlerContext ctx, String request) throws @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + EOL); + ctx.write("welcome to " + Utils.getInetAddress() + "!" + EOL); ctx.write("It is " + new Date() + " now." + EOL + EOL); ctx.flush(); } @@ -101,6 +101,7 @@ public String handler(ChannelHandlerContext ctx, String args) { buf.append("count: count conn num or online user count" + EOL); buf.append("route: show user route info" + EOL); buf.append("conf:[key] show config info" + EOL); + buf.append("monitor:[mxBean] show system monitor" + EOL); return buf.toString(); } }, @@ -114,7 +115,7 @@ public String handler(ChannelHandlerContext ctx, String args) { @Override public String handler(ChannelHandlerContext ctx, String args) { ctx.writeAndFlush("try close connect server..."); - adminServer.getConnectionServer().stop(new Service.Listener() { + adminServer.getConnectionServer().stop(new Listener() { @Override public void onSuccess(Object... args) { ctx.writeAndFlush("connect server close success" + EOL); @@ -167,7 +168,7 @@ public Serializable handler(ChannelHandlerContext ctx, String args) { case "conn": return adminServer.getConnectionServer().getConnectionManager().getConnections().size(); case "online": { - Long value = RedisManager.I.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); + Long value = RedisManager.I.zCard(RedisKey.getUserOnlineKey(Utils.getExtranetAddress())); return value == null ? 0 : value; } @@ -212,13 +213,13 @@ public String handler(ChannelHandlerContext ctx, String args) { LOGGER.info("delete connection server success:{}", data); removeSuccess = true; } else { - LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), MPushUtil.getInetAddress()); + LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), Utils.getInetAddress()); } } if (removeSuccess) { - return "remove success."; + return "removeAndClose success."; } else { - return "remove false."; + return "removeAndClose false."; } } }; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index b6b4268a..b4df42d5 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -29,8 +29,8 @@ import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.OkMessage; import com.mpush.core.router.RouterCenter; -import com.mpush.tools.log.Logs; import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/23. diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index adff3d7b..bdf0abba 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -25,12 +25,10 @@ import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.FastConnectMessage; import com.mpush.common.message.FastConnectOkMessage; -import com.mpush.tools.config.ConfigManager; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; +import com.mpush.tools.config.ConfigManager; import com.mpush.tools.log.Logs; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Created by ohun on 2015/12/25. @@ -38,7 +36,6 @@ * @author ohun@live.cn */ public final class FastConnectHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(FastConnectHandler.class); @Override public FastConnectMessage decode(Packet packet, Connection connection) { @@ -53,11 +50,13 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); + Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}" + , message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); + Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}" + , message.deviceId, session.context); } else { //3.校验成功,重新计算心跳,完成快速重连 int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 36f3897c..d5c3171f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -29,8 +29,8 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.core.router.LocalRouter; import com.mpush.core.router.RouterCenter; +import com.mpush.tools.Utils; import com.mpush.tools.log.Logs; -import com.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -51,7 +51,7 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { /** * 处理PushClient发送过来的Push推送请求 *

- * 查推送策略,先查本地路由,本地不存在,查远程,(注意:有可能远程也是本机) + * 查寻路由策略,先查本地路由,本地不存在,查远程,(注意:有可能远程查到也是本机IP) *

* 正常情况本地路由应该存在,如果不存在或链接失效,有以下几种情况: *

@@ -59,9 +59,9 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { * 2.客户端下线,本地路由失效,远程路由还未清除 * 3.PushClient使用了本地缓存,但缓存数据已经和实际情况不一致了 *

- * 对于三种情况的处理方式是, 再检查下远程路由: - * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 - * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 + * 对于三种情况的处理方式是, 再重新查寻下远程路由: + * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 (解决场景2) + * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 (解决场景1,3) *

* * @param message @@ -144,7 +144,7 @@ private void checkRemote(GatewayPushMessage message) { } //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 - if (MPushUtil.getLocalIp().equals(router.getRouteValue().getHost())) { + if (Utils.getLocalIp().equals(router.getRouteValue().getHost())) { ErrorMessage.from(message).setErrorCode(OFFLINE).send(); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index bba3cd4a..17879376 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -30,13 +30,11 @@ import com.mpush.common.message.HandshakeOkMessage; import com.mpush.common.security.AesCipher; import com.mpush.common.security.CipherBox; -import com.mpush.tools.config.ConfigManager; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.tools.log.Logs; +import com.mpush.tools.config.ConfigManager; import com.mpush.tools.event.EventBus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/24. @@ -44,7 +42,6 @@ * @author ohun@live.cn */ public final class HandshakeHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); @Override public HandshakeMessage decode(Packet packet, Connection connection) { @@ -64,14 +61,14 @@ public void handle(HandshakeMessage message) { || iv.length != CipherBox.I.getAesKeyLength() || clientKey.length != CipherBox.I.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - Logs.Conn.info("client handshake false:{}", message.getConnection()); + Logs.Conn.info("handshake failure, message={}", message.toString()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - Logs.Conn.info("client handshake false for repeat handshake:{}", message.getConnection().getSessionContext()); + Logs.Conn.info("handshake failure, repeat handshake, session={}", message.getConnection().getSessionContext()); return; } @@ -108,6 +105,6 @@ public void handle(HandshakeMessage message) { //10.触发握手成功事件 EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); - Logs.Conn.info("client handshake success:{}", context); + Logs.Conn.info("handshake success, session={}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 259996d1..88ffaf18 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -34,7 +34,6 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong - Logs.HB.info("response client heartbeat:{}, {}", - connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("ping -> pong, {}", connection); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index f6a4b285..cbb9ce9d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -25,20 +25,20 @@ import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.tools.config.data.DnsMapping; import com.mpush.common.net.DnsMappingManager; -import com.mpush.tools.log.Logs; import com.mpush.netty.http.HttpCallback; import com.mpush.netty.http.HttpClient; -import com.mpush.netty.http.RequestInfo; -import com.mpush.tools.Profiler; +import com.mpush.netty.http.RequestContext; +import com.mpush.tools.common.Profiler; +import com.mpush.tools.config.data.DnsMapping; +import com.mpush.tools.log.Logs; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; import org.slf4j.Logger; import java.net.InetSocketAddress; -import java.net.URI; -import java.net.URISyntaxException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Map; import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; @@ -67,6 +67,7 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { public void handle(HttpRequestMessage message) { try { Profiler.enter("start http proxy handler"); + //1.参数校验 String method = message.getMethod(); String uri = message.uri; if (Strings.isNullOrEmpty(uri)) { @@ -78,18 +79,16 @@ public void handle(HttpRequestMessage message) { LOGGER.warn("request url is empty!"); } + //2.url转换 uri = doDnsMapping(uri); + + //3.包装成HTTP request FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); - Profiler.enter("start set full http headers"); - setHeaders(request, message); - Profiler.release(); - Profiler.enter("start set full http body"); - setBody(request, message); - Profiler.release(); + setHeaders(request, message);//处理header + setBody(request, message);//处理body - Profiler.enter("start http proxy request"); - httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); - Profiler.release(); + //4.发送请求 + httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage .from(message) @@ -179,10 +178,8 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { } } InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); - Profiler.enter("start set x-forwarded-for"); - String remoteIp = remoteAddress.getAddress().getHostAddress(); + String remoteIp = remoteAddress.getAddress().getHostAddress();//这个要小心,不要使用getHostName,不然会耗时比较大 request.headers().add("x-forwarded-for", remoteIp); - Profiler.release(); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } @@ -195,15 +192,19 @@ private void setBody(FullHttpRequest request, HttpRequestMessage message) { } private String doDnsMapping(String url) { - URI uri = null; + URL uri = null; try { - uri = new URI(url); - } catch (URISyntaxException e) { + uri = new URL(url); + } catch (MalformedURLException e) { + } + if (uri == null) { + return url; } - if (uri == null) return url; String host = uri.getHost(); - DnsMapping mapping = DnsMappingManager.I.translate(host); - if (mapping == null) return url; - return url.replaceFirst(host, mapping.toString()); + DnsMapping mapping = DnsMappingManager.I.lookup(host); + if (mapping == null) { + return url; + } + return mapping.translate(uri); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index a6f04b56..a655051c 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -20,8 +20,6 @@ package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.core.router.LocalRouterManager; -import com.mpush.core.router.RouterCenter; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOfflineEvent; @@ -33,8 +31,10 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.core.router.LocalRouter; -import com.mpush.tools.log.Logs; +import com.mpush.core.router.LocalRouterManager; +import com.mpush.core.router.RouterCenter; import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index 3d519a16..9db92bcb 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -25,12 +25,12 @@ * @author ohun@live.cn */ public final class KickRemoteMsg { - public String userId; - public String deviceId; - public String targetServer; + public String userId; + public String deviceId; + public String targetServer; - @Override - public String toString() { - return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + ", targetServer='" + targetServer + '\'' + '}'; - } + @Override + public String toString() { + return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + ", targetServer='" + targetServer + '\'' + '}'; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 2f588d25..2b14f9c7 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -24,11 +24,9 @@ import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.router.RouterManager; -import com.mpush.tools.event.EventConsumer; - import com.mpush.tools.event.EventBus; +import com.mpush.tools.event.EventConsumer; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -88,9 +86,9 @@ public String getUserIdByConnId(String connId) { * @param event */ @Subscribe - void onConnectionCloseEvent(ConnectionCloseEvent event) { - Connection connection = event.connection; - if(connection == null) return; + void on(ConnectionCloseEvent event) { + Connection connection = event.connection; + if (connection == null) return; String id = event.connection.getId(); //1.清除反向关系 diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index f66e0c1b..31c18ad4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -21,12 +21,11 @@ import com.mpush.api.connection.Connection; import com.mpush.api.event.RouterChangeEvent; -import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.Router; import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; -import com.mpush.tools.MPushUtil; - +import com.mpush.tools.Utils; import com.mpush.tools.event.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +55,7 @@ public final class RouterCenter { public boolean register(String userId, Connection connection) { ClientLocation location = ClientLocation .from(connection.getSessionContext()) - .setHost(MPushUtil.getLocalIp()); + .setHost(Utils.getLocalIp()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(location); diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index b8e751e9..899bf871 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -29,13 +29,13 @@ import com.mpush.cache.redis.listener.ListenerDispatcher; import com.mpush.cache.redis.listener.MessageListener; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.event.EventConsumer; import com.mpush.common.message.KickUserMessage; import com.mpush.common.router.RemoteRouter; +import com.mpush.tools.Jsons; +import com.mpush.tools.Utils; import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.event.EventConsumer; import com.mpush.tools.log.Logs; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -46,7 +46,7 @@ */ public final class RouterChangeListener extends EventConsumer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; - private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); + private final String kick_channel = KICK_CHANNEL_ + Utils.getLocalIp(); public RouterChangeListener() { ListenerDispatcher.I.subscribe(getKickChannel(), this); @@ -61,7 +61,7 @@ public String getKickChannel(String remoteIp) { } @Subscribe - void onRouteChange(RouterChangeEvent event) { + void on(RouterChangeEvent event) { String userId = event.userId; Router r = event.router; if (r.getRouteType().equals(Router.RouterType.LOCAL)) { @@ -107,7 +107,7 @@ public void operationComplete(ChannelFuture future) throws Exception { public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 - if (location.getHost().equals(MPushUtil.getLocalIp())) { + if (location.getHost().equals(Utils.getLocalIp())) { Logs.Conn.info("kick remote user but router in local, userId={}", userId); return; } @@ -131,8 +131,8 @@ public void kickRemote(String userId, RemoteRouter router) { */ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 - if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + if (!msg.targetServer.equals(Utils.getLocalIp())) { + Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); return; } diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index b1b1f7ef..20de3b91 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -22,8 +22,8 @@ import com.google.common.eventbus.Subscribe; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.event.UserOnlineEvent; -import com.mpush.common.user.UserManager; import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.user.UserManager; import com.mpush.tools.event.EventBus; /** @@ -42,13 +42,13 @@ public UserOnlineOfflineListener() { } @Subscribe - void onUserOnline(UserOnlineEvent event) { + void on(UserOnlineEvent event) { UserManager.INSTANCE.recordUserOnline(event.getUserId()); RedisManager.I.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe - void onUserOffline(UserOfflineEvent event) { + void on(UserOfflineEvent event) { UserManager.INSTANCE.recordUserOffline(event.getUserId()); RedisManager.I.publish(OFFLINE_CHANNEL, event.getUserId()); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index 1f625c0a..589d7474 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -21,6 +21,7 @@ import com.mpush.core.handler.AdminHandler; import com.mpush.netty.server.NettyServer; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.DelimiterBasedFrameDecoder; @@ -28,6 +29,8 @@ import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; +import java.util.concurrent.Executor; + public final class AdminServer extends NettyServer { private final ConnectionServer connectionServer; private final GatewayServer gatewayServer; @@ -47,6 +50,16 @@ protected void initPipeline(ChannelPipeline pipeline) { super.initPipeline(pipeline); } + @Override + protected Executor getBossExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + @Override + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + @Override public ChannelHandler getChannelHandler() { return adminHandler; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 70592506..b691e027 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -22,21 +22,31 @@ import com.mpush.api.connection.ConnectionManager; import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.*; import com.mpush.netty.http.HttpClient; import com.mpush.netty.http.NettyHttpClient; import com.mpush.netty.server.NettyServer; import com.mpush.tools.config.CC; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.connect_server.*; /** * Created by ohun on 2015/12/30. */ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; + private GlobalChannelTrafficShapingHandler trafficShapingHandler; private ConnectionManager connectionManager = new ServerConnectionManager(); private HttpClient httpClient; @@ -56,20 +66,49 @@ public void init() { receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - if (CC.mp.http.proxy_enable) { + if (CC.mp.http.proxy_enabled) { httpClient = new NettyHttpClient(); receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); } channelHandler = new ServerChannelHandler(true, connectionManager, receiver); + + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override public void stop(Listener listener) { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } super.stop(listener); if (httpClient != null) httpClient.stop(); connectionManager.destroy(); } + @Override + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + @Override + protected Executor getBossExecutor() { + return ThreadPoolManager.I.getBossExecutor(); + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + @Override protected void initOptions(ServerBootstrap b) { super.initOptions(b); diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index ee00f839..fb7976ee 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -20,10 +20,17 @@ package com.mpush.core.server; import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; import com.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_server.*; /** * Created by ohun on 2015/12/30. @@ -34,6 +41,7 @@ public final class GatewayServer extends NettyServer { private ServerChannelHandler channelHandler; private ServerConnectionManager connectionManager; + private GlobalChannelTrafficShapingHandler trafficShapingHandler; public GatewayServer(int port) { super(port); @@ -46,16 +54,34 @@ public void init() { receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); connectionManager = new ServerConnectionManager(); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override public void stop(Listener listener) { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } super.stop(listener); if (connectionManager != null) { connectionManager.destroy(); } } + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + @Override public ChannelHandler getChannelHandler() { return channelHandler; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 5e8196b8..9aa7c078 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -20,21 +20,18 @@ package com.mpush.core.server; -import com.mpush.netty.connection.NettyConnection; +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.PacketReceiver; -import com.mpush.tools.log.Logs; - -import com.mpush.tools.Profiler; - +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.common.Profiler; import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,33 +60,32 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - try{ - Profiler.start("end channel read:"); - Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("channelRead channel={}, packet={}", ctx.channel(), msg); - connection.updateLastReadTime(); - receiver.onReceive((Packet) msg, connection); - }finally{ - Profiler.release(); - long duration = Profiler.getDuration(); - if(duration>80){ - LOGGER.error("end channel read:"+duration+","+Profiler.dump()); - } - Profiler.reset(); - } - + try { + Profiler.start("channel read:"); + Connection connection = connectionManager.get(ctx.channel()); + LOGGER.debug("channelRead channel={}, connection={}, packet={}", ctx.channel(), connection, msg); + connection.updateLastReadTime(); + receiver.onReceive((Packet) msg, connection); + } finally { + Profiler.release(); + long duration = Profiler.getDuration(); + if (duration > 80) { + LOGGER.error("channel read busy:" + duration + "," + Profiler.dump()); + } + Profiler.reset(); + } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - connectionManager.remove(ctx.channel()); - Logs.Conn.info("client exceptionCaught channel={}", ctx.channel()); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + Connection connection = connectionManager.removeAndClose(ctx.channel()); + Logs.Conn.error("client exceptionCaught channel={}, connection={}", ctx.channel(), connection); + LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client connect channel={}", ctx.channel()); + Logs.Conn.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -97,9 +93,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client disconnect channel={}", ctx.channel()); - Connection connection = connectionManager.get(ctx.channel()); + Connection connection = connectionManager.removeAndClose(ctx.channel()); EventBus.I.post(new ConnectionCloseEvent(connection)); - connectionManager.remove(ctx.channel()); + Logs.Conn.info("client disconnect channel={}, connection={}", ctx.channel(), connection); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 1d81605b..f18d9f91 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -46,7 +46,6 @@ * @author ohun@live.cn */ public final class ServerConnectionManager implements ConnectionManager { - //可能会有20w的链接数 private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); private Timer timer; @@ -71,20 +70,21 @@ public void destroy() { @Override public Connection get(final Channel channel) { - return connections.get(channel.id().asLongText()); + return connections.get(channel.id().asShortText()); } @Override public void add(Connection connection) { - connections.putIfAbsent(connection.getId(), connection); + connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); } @Override - public void remove(Channel channel) { - Connection connection = connections.remove(channel.id().asLongText()); + public Connection removeAndClose(Channel channel) { + Connection connection = connections.remove(channel.id().asShortText()); if (connection != null) { connection.close(); } + return connection; } @Override @@ -93,14 +93,14 @@ public List getConnections() { } @Subscribe - void onHandshakeOk(HandshakeEvent event) { + void on(HandshakeEvent event) { HeartbeatCheckTask task = new HeartbeatCheckTask(event.connection); task.startTimeout(); } private class HeartbeatCheckTask implements TimerTask { - private int expiredTimes = 0; + private int timeoutTimes = 0; private final Connection connection; public HeartbeatCheckTask(Connection connection) { @@ -115,22 +115,21 @@ public void startTimeout() { @Override public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) { - Logs.HB.info("connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection was disconnected, heartbeat timeout times={}, connection={}", timeoutTimes, connection); return; } if (connection.heartbeatTimeout()) { - if (++expiredTimes > CC.mp.core.max_hb_timeout_times) { + if (++timeoutTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); - Logs.HB.info("connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("client heartbeat timeout times={}, do close connection={}", timeoutTimes, connection); return; } else { - Logs.HB.info("connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("client heartbeat timeout times={}, connection={}", timeoutTimes, connection); } } else { - expiredTimes = 0; - Logs.HB.info("connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + timeoutTimes = 0; + //Logs.HB.info("client heartbeat timeout times reset 0, connection={}", connection); } - startTimeout(); } } diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index 80182395..5b9394bc 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -22,7 +22,7 @@ import com.mpush.api.connection.SessionContext; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.Strings; +import com.mpush.tools.common.Strings; import com.mpush.tools.config.CC; import com.mpush.tools.crypto.MD5Utils; diff --git a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java deleted file mode 100644 index 55b7e688..00000000 --- a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.core.netty; - -import com.mpush.api.Server; -import com.mpush.core.server.ConnectionServer; -import org.junit.Test; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public class NettyServerTest { - @Test - public void testStart() throws Exception { - ConnectionServer server = new ConnectionServer(3000); - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess(Object... port) { - } - - @Override - public void onFailure(Throwable cause) { - } - }); - } -} \ No newline at end of file diff --git a/mpush-core/src/test/resources/logback.xml b/mpush-core/src/test/resources/logback.xml index 20979b40..1ff25c6d 100644 --- a/mpush-core/src/test/resources/logback.xml +++ b/mpush-core/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - + + System.out + UTF-8 + DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-monitor/src/main/java/com/mpush/App.java b/mpush-monitor/src/main/java/com/mpush/App.java index 0a7102ea..80d28238 100644 --- a/mpush-monitor/src/main/java/com/mpush/App.java +++ b/mpush-monitor/src/main/java/com/mpush/App.java @@ -2,12 +2,9 @@ /** * Hello world! - * */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); +public class App { + public static void main(String[] args) { + System.out.println("Hello World!"); } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java index e930038f..865345b0 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java @@ -22,65 +22,65 @@ import java.util.Map; public class MonitorResult { - - private Long timestamp; - private Map memoryMap; + private Long timestamp; - private Map gcMap; + private Map memoryMap; - private Map threadMap; - - private Map threadPoolMap; - - private Map infoMap; + private Map gcMap; - public MonitorResult() { - this.timestamp = System.currentTimeMillis(); - } + private Map threadMap; - public Map getMemoryMap() { - return memoryMap; - } + private Map threadPoolMap; - public void setMemoryMap(Map memoryMap) { - this.memoryMap = memoryMap; - } + private Map infoMap; - public Map getGcMap() { - return gcMap; - } + public MonitorResult() { + this.timestamp = System.currentTimeMillis(); + } - public void setGcMap(Map gcMap) { - this.gcMap = gcMap; - } + public Map getMemoryMap() { + return memoryMap; + } - public Map getThreadMap() { - return threadMap; - } + public void setMemoryMap(Map memoryMap) { + this.memoryMap = memoryMap; + } - public void setThreadMap(Map threadMap) { - this.threadMap = threadMap; - } + public Map getGcMap() { + return gcMap; + } - public Long getTimestamp() { - return timestamp; - } + public void setGcMap(Map gcMap) { + this.gcMap = gcMap; + } - public Map getInfoMap() { - return infoMap; - } + public Map getThreadMap() { + return threadMap; + } - public void setInfoMap(Map infoMap) { - this.infoMap = infoMap; - } + public void setThreadMap(Map threadMap) { + this.threadMap = threadMap; + } - public Map getThreadPoolMap() { - return threadPoolMap; - } + public Long getTimestamp() { + return timestamp; + } + + public Map getInfoMap() { + return infoMap; + } + + public void setInfoMap(Map infoMap) { + this.infoMap = infoMap; + } + + public Map getThreadPoolMap() { + return threadPoolMap; + } + + public void setThreadPoolMap(Map threadPoolMap) { + this.threadPoolMap = threadPoolMap; + } - public void setThreadPoolMap(Map threadPoolMap) { - this.threadPoolMap = threadPoolMap; - } - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java index 2cddc285..6bcf3b3f 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java @@ -21,20 +21,20 @@ public interface GCMQuota { - public long yongGcCollectionCount(); - - public long yongGcCollectionTime(); - - public long fullGcCollectionCount(); - - public long fullGcCollectionTime(); - - public long spanYongGcCollectionCount(); - - public long spanYongGcCollectionTime(); - - public long spanFullGcCollectionCount(); - - public long spanFullGcCollectionTime(); - + public long yongGcCollectionCount(); + + public long yongGcCollectionTime(); + + public long fullGcCollectionCount(); + + public long fullGcCollectionTime(); + + public long spanYongGcCollectionCount(); + + public long spanYongGcCollectionTime(); + + public long spanFullGcCollectionCount(); + + public long spanFullGcCollectionTime(); + } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java index 89c8f2e2..a2d7eef6 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java @@ -21,7 +21,7 @@ public interface InfoQuota { - public String pid(); - - public double load(); + public String pid(); + + public double load(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java index a9e48a20..53c66c1d 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java @@ -21,58 +21,58 @@ public interface MemoryQuota { - // Heap - long heapMemoryCommitted(); + // Heap + long heapMemoryCommitted(); - long heapMemoryInit(); + long heapMemoryInit(); - long heapMemoryMax(); + long heapMemoryMax(); - long heapMemoryUsed(); + long heapMemoryUsed(); - // NonHeap - long nonHeapMemoryCommitted(); + // NonHeap + long nonHeapMemoryCommitted(); - long nonHeapMemoryInit(); + long nonHeapMemoryInit(); - long nonHeapMemoryMax(); + long nonHeapMemoryMax(); - long nonHeapMemoryUsed(); + long nonHeapMemoryUsed(); - // PermGen - long permGenCommitted(); + // PermGen + long permGenCommitted(); - long permGenInit(); + long permGenInit(); - long permGenMax(); + long permGenMax(); - long permGenUsed(); + long permGenUsed(); - // OldGen - long oldGenCommitted(); + // OldGen + long oldGenCommitted(); - long oldGenInit(); + long oldGenInit(); - long oldGenMax(); + long oldGenMax(); - long oldGenUsed(); + long oldGenUsed(); - // EdenSpace - long edenSpaceCommitted(); + // EdenSpace + long edenSpaceCommitted(); - long edenSpaceInit(); + long edenSpaceInit(); - long edenSpaceMax(); + long edenSpaceMax(); - long edenSpaceUsed(); + long edenSpaceUsed(); - // Survivor - long survivorCommitted(); + // Survivor + long survivorCommitted(); - long survivorInit(); + long survivorInit(); - long survivorMax(); + long survivorMax(); - long survivorUsed(); + long survivorUsed(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java index 0691708b..1c94762a 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java @@ -22,12 +22,12 @@ public interface ThreadQuota { - public int daemonThreadCount(); - - public int threadCount(); - - public long totalStartedThreadCount(); - - public int deadLockedThreadCount(); - + public int daemonThreadCount(); + + public int threadCount(); + + public long totalStartedThreadCount(); + + public int deadLockedThreadCount(); + } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index 2bcb9dd1..baff0a65 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -19,50 +19,50 @@ package com.mpush.monitor.quota.impl; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.InfoQuota; + import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.InfoQuota; -import com.mpush.monitor.quota.BaseQuota; - public class JVMInfo extends BaseQuota implements InfoQuota { - - public static final JVMInfo I = new JVMInfo(); - - private RuntimeMXBean runtimeMXBean; - - private OperatingSystemMXBean systemMXBean; - - private String currentPid; - - private JVMInfo() { - runtimeMXBean = ManagementFactory.getRuntimeMXBean(); - systemMXBean = ManagementFactory.getOperatingSystemMXBean(); - } - @Override - public String pid() { - if (null == currentPid) { - currentPid = runtimeMXBean.getName().split("@")[0]; + public static final JVMInfo I = new JVMInfo(); + + private RuntimeMXBean runtimeMXBean; + + private OperatingSystemMXBean systemMXBean; + + private String currentPid; + + private JVMInfo() { + runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + systemMXBean = ManagementFactory.getOperatingSystemMXBean(); + } + + @Override + public String pid() { + if (null == currentPid) { + currentPid = runtimeMXBean.getName().split("@")[0]; } return currentPid; - } + } - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("pid", pid()); - map.put("load", load()); - return map; - } + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("pid", pid()); + map.put("load", load()); + return map; + } - @Override - public double load() { - double averageLoad = systemMXBean.getSystemLoadAverage(); - return averageLoad; - } + @Override + public double load() { + double averageLoad = systemMXBean.getSystemLoadAverage(); + return averageLoad; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index 51bca628..cde994ed 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -19,61 +19,61 @@ package com.mpush.monitor.quota.impl; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.BaseQuota; +import com.mpush.monitor.quota.ThreadQuota; + import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.ThreadQuota; +public class JVMThread extends BaseQuota implements ThreadQuota { + + private ThreadMXBean threadMXBean; -public class JVMThread extends BaseQuota implements ThreadQuota{ + public static final JVMThread I = new JVMThread(); - private ThreadMXBean threadMXBean; - - public static final JVMThread I = new JVMThread(); - - private JVMThread() { - threadMXBean = ManagementFactory.getThreadMXBean(); - } - - @Override - public int daemonThreadCount() { - return threadMXBean.getDaemonThreadCount(); - } + private JVMThread() { + threadMXBean = ManagementFactory.getThreadMXBean(); + } - @Override - public int threadCount() { - return threadMXBean.getThreadCount(); - } + @Override + public int daemonThreadCount() { + return threadMXBean.getDaemonThreadCount(); + } - @Override - public long totalStartedThreadCount() { - return threadMXBean.getTotalStartedThreadCount(); - } + @Override + public int threadCount() { + return threadMXBean.getThreadCount(); + } - @Override - public int deadLockedThreadCount() { - try { + @Override + public long totalStartedThreadCount() { + return threadMXBean.getTotalStartedThreadCount(); + } + + @Override + public int deadLockedThreadCount() { + try { long[] deadLockedThreadIds = threadMXBean.findDeadlockedThreads(); if (deadLockedThreadIds == null) { return 0; } return deadLockedThreadIds.length; } catch (Exception e) { - return 0; + return 0; } - } + } + + @Override + public Map toMap() { + Map map = Maps.newHashMap(); + map.put("daemonThreadCount", daemonThreadCount()); + map.put("threadCount", threadCount()); + map.put("totalStartedThreadCount", totalStartedThreadCount()); + map.put("deadLockedThreadCount", deadLockedThreadCount()); + return map; + } - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("daemonThreadCount", daemonThreadCount()); - map.put("threadCount", threadCount()); - map.put("totalStartedThreadCount", totalStartedThreadCount()); - map.put("deadLockedThreadCount", deadLockedThreadCount()); - return map; - } - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index 39a00fe1..2d231742 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -19,11 +19,11 @@ package com.mpush.monitor.quota.impl; -import com.google.common.collect.Maps; import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.ThreadPoolQuota; import com.mpush.tools.thread.pool.ThreadPoolManager; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; @@ -36,17 +36,12 @@ private JVMThreadPool() { @Override public Map toMap() { - Map map = Maps.newHashMap(); + Map map = new HashMap<>(); Map pool = ThreadPoolManager.I.getActivePools(); for (Map.Entry entry : pool.entrySet()) { String serviceName = entry.getKey(); ThreadPoolExecutor executor = (ThreadPoolExecutor) entry.getValue(); - String info = "coreThreadNum:" + executor.getCorePoolSize() - + " maxThreadNum:" + executor.getMaximumPoolSize() - + " workingThreadNum:" + executor.getActiveCount() - + " workThreadNum:" + executor.getPoolSize() - + " blockTaskNum:" + executor.getQueue().size(); - map.put(serviceName, info); + map.put(serviceName, ThreadPoolManager.getPoolInfo(executor)); } return map; } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java index cea51274..3f53ffcd 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -19,30 +19,34 @@ package com.mpush.monitor.service; -import com.mpush.api.BaseService; -import com.mpush.tools.log.Logs; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.monitor.data.MonitorResult; import com.mpush.monitor.data.ResultCollector; import com.mpush.monitor.quota.impl.JVMInfo; -import com.mpush.tools.JVMUtil; import com.mpush.tools.Jsons; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.tools.common.JVMUtil; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import com.sun.istack.internal.NotNull; -public class MonitorService extends BaseService implements Runnable { +import java.util.concurrent.TimeUnit; - private final Logger LOGGER = LoggerFactory.getLogger(MonitorService.class); +public class MonitorService extends BaseService implements Runnable { public static final MonitorService I = new MonitorService(); private static final int firstJstack = 2, secondJstack = 4, thirdJstack = 6, firstJmap = 4; - private String dumpLogDir = "/tmp/logs/mpush/"; + private static final String dumpLogDir = CC.mp.monitor.dump_dir; + private static final boolean enableDump = CC.mp.monitor.dump_stack; + private static final boolean printLog = CC.mp.monitor.print_log; + private static final long dumpPeriod = CC.mp.monitor.dump_period.getSeconds(); + private boolean dumpFirstJstack = false; private boolean dumpSecondJstack = false; private boolean dumpThirdJstack = false; private boolean dumpJmap = false; - private boolean enableDump = false; private final ResultCollector collector = new ResultCollector(); @@ -50,34 +54,31 @@ public class MonitorService extends BaseService implements Runnable { public void run() { while (started.get()) { MonitorResult result = collector.collect(); - Logs.Monitor.info(Jsons.toJson(result)); + if (printLog) { + Logs.Monitor.info(Jsons.toJson(result)); + } if (enableDump) { dump(); } - try {//30s - Thread.sleep(30000L); + try { + TimeUnit.SECONDS.sleep(dumpPeriod); } catch (InterruptedException e) { - LOGGER.warn("monitor data exception", e); + stop(); } } } @Override - public void start(Listener listener) { - if (started.compareAndSet(false, true)) { + protected void doStart(@NotNull Listener listener) throws Throwable { + if (CC.mp.monitor.print_log || enableDump) { Thread thread = new Thread(this, "mp-t-monitor"); thread.start(); } } @Override - public void stop(Listener listener) { - started.set(false); - } + protected void doStop(@NotNull Listener listener) throws Throwable { - @Override - public boolean isRunning() { - return started.get(); } private void dump() { @@ -110,15 +111,4 @@ private void dump() { } } } - - - public MonitorService setDumpLogDir(String dumpLogDir) { - this.dumpLogDir = dumpLogDir; - return this; - } - - public MonitorService setEnableDump(boolean enableDump) { - this.enableDump = enableDump; - return this; - } } diff --git a/mpush-monitor/src/test/java/com/mpush/AppTest.java b/mpush-monitor/src/test/java/com/mpush/AppTest.java index 796b8466..6c96a1d2 100644 --- a/mpush-monitor/src/test/java/com/mpush/AppTest.java +++ b/mpush-monitor/src/test/java/com/mpush/AppTest.java @@ -7,32 +7,28 @@ /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ +public class AppTest + extends TestCase { /** * Create the test case * * @param testName name of the test case */ - public AppTest( String testName ) - { - super( testName ); + public AppTest(String testName) { + super(testName); } /** * @return the suite of tests being tested */ - public static Test suite() - { - return new TestSuite( AppTest.class ); + public static Test suite() { + return new TestSuite(AppTest.class); } /** * Rigourous Test :-) */ - public void testApp() - { - assertTrue( true ); + public void testApp() { + assertTrue(true); } } diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index c0c37221..4fb8bc19 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -41,5 +41,9 @@ io.netty netty-codec-http + + io.netty + netty-handler + diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 6ae5365b..54af8dc8 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -19,10 +19,13 @@ package com.mpush.netty.client; -import com.mpush.api.BaseService; -import com.mpush.api.Client; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Client; +import com.mpush.api.service.Listener; +import com.mpush.api.service.ServiceException; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; +import com.sun.istack.internal.NotNull; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -33,7 +36,6 @@ import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; -import java.util.concurrent.atomic.AtomicBoolean; public abstract class NettyClient extends BaseService implements Client { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); @@ -63,13 +65,10 @@ public void start(final Listener listener) { bootstrap.handler(new ChannelInitializer() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(getChannelHandler()); + initPipeline(ch.pipeline()); } }); - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener(new ChannelFutureListener() { @Override @@ -84,17 +83,54 @@ public void operationComplete(ChannelFuture future) throws Exception { } }); } else { - listener.onFailure(new RuntimeException("client has started!")); + listener.onFailure(new ServiceException("client has started!")); } } + protected void initPipeline(ChannelPipeline pipeline) { + pipeline.addLast("decoder", getDecoder()); + pipeline.addLast("encoder", getEncoder()); + pipeline.addLast("handler", getChannelHandler()); + } + + protected ChannelHandler getDecoder() { + return new PacketDecoder(); + } + + protected ChannelHandler getEncoder() { + return PacketEncoder.INSTANCE; + } + + + public abstract ChannelHandler getChannelHandler(); + @Override - public void stop(Listener listener) { + protected void doStart(@NotNull Listener listener) throws Throwable { + + } + + @Override + protected void doStop(@NotNull Listener listener) throws Throwable { if (workerGroup != null) { workerGroup.shutdownGracefully(); } - started.set(false); + LOGGER.error("netty client [{}] stopped.", this.getClass().getSimpleName()); } - public abstract ChannelHandler getChannelHandler(); + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + @Override + public String toString() { + return "NettyClient{" + + "host='" + host + '\'' + + ", port=" + port + + ", name=" + this.getClass().getSimpleName() + + '}'; + } } diff --git a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java b/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java similarity index 96% rename from mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java rename to mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java index 8d464669..ec646692 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.api.exception; +package com.mpush.netty.codec; /** * Created by ohun on 2015/12/23. diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 9a7e90e7..27267d38 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -19,7 +19,6 @@ package com.mpush.netty.codec; -import com.mpush.api.exception.DecodeException; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import com.mpush.tools.config.CC; diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 9573d703..1f084740 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -67,7 +67,7 @@ public SessionContext getSessionContext() { @Override public String getId() { - return channel.id().asLongText(); + return channel.id().asShortText(); } @Override @@ -122,7 +122,7 @@ public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { lastWriteTime = System.currentTimeMillis(); } else { - LOGGER.error("send msg error"); + LOGGER.error("connection send msg error", future.cause()); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java index cb94c6ad..06305e55 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java @@ -19,16 +19,13 @@ package com.mpush.netty.http; +import com.mpush.api.service.Service; + /** * Created by ohun on 2016/2/15. * * @author ohun@live.cn */ -public interface HttpClient { - - void start(); - - void stop(); - - void request(RequestInfo requestInfo) throws Exception; +public interface HttpClient extends Service { + void request(RequestContext context) throws Exception; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java new file mode 100644 index 00000000..26f6a31e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java @@ -0,0 +1,126 @@ +package com.mpush.netty.http; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; +import io.netty.util.IllegalReferenceCountException; +import io.netty.util.ReferenceCountUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URL; +import java.net.URLDecoder; + +@ChannelHandler.Sharable +public class HttpClientHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private final NettyHttpClient client; + + public HttpClientHandler(NettyHttpClient client) { + this.client = client; + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); + try { + if (context != null && context.tryDone()) { + context.onException(cause); + } + } finally { + client.pool.tryRelease(ctx.channel()); + } + LOGGER.error("http client caught an error, info={}", context, cause); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); + try { + if (context != null && context.tryDone()) { + HttpResponse response = (HttpResponse) msg; + if (isRedirect(response)) { + if (context.onRedirect(response)) { + String location = getRedirectLocation(context.request, response); + if (location != null && location.length() > 0) { + context.cancelled.set(false); + context.request = context.request.copy().setUri(location); + client.request(context); + return; + } + } + } + context.onResponse(response); + LOGGER.debug("request done request={}", context); + } + } finally { + client.pool.tryRelease(ctx.channel()); + ReferenceCountUtil.release(msg); + } + } + + private boolean isRedirect(HttpResponse response) { + HttpResponseStatus status = response.status(); + switch (status.code()) { + case 300: + case 301: + case 302: + case 303: + case 305: + case 307: + return true; + default: + return false; + } + } + + private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { + String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); + if (hdr != null) { + if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { + return hdr; + } else { + URL orig = new URL(request.uri()); + String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); + if (hdr.startsWith("/")) { + pth = hdr; + } else if (pth.endsWith("/")) { + pth += hdr; + } else { + pth += "/" + hdr; + } + StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); + sb.append("://").append(orig.getHost()); + if (orig.getPort() > 0) { + sb.append(":").append(orig.getPort()); + } + if (pth.charAt(0) != '/') { + sb.append('/'); + } + sb.append(pth); + return sb.toString(); + } + } + return null; + } + + private HttpRequest copy(String uri, HttpRequest request) { + HttpRequest nue = request; + if (request instanceof DefaultFullHttpRequest) { + DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; + FullHttpRequest rq; + try { + rq = dfrq.copy(); + } catch (IllegalReferenceCountException e) { // Empty bytebuf + rq = dfrq; + } + rq.setUri(uri); + } else { + DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); + dfr.headers().set(request.headers()); + nue = dfr; + } + return nue; + } +} \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java new file mode 100644 index 00000000..ce55224e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java @@ -0,0 +1,85 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.netty.http; + +import com.google.common.collect.ArrayListMultimap; +import com.mpush.tools.config.CC; +import io.netty.channel.Channel; +import io.netty.util.AttributeKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by yxx on 2016/5/28. + * + * @author ohun@live.cn (夜色) + */ +public class HttpConnectionPool { + private static final int maxConnPerHost = CC.mp.http.max_conn_per_host; + + private final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPool.class); + + private final AttributeKey hostKey = AttributeKey.newInstance("host"); + + private final ArrayListMultimap channelPool = ArrayListMultimap.create(); + + public synchronized Channel tryAcquire(String host) { + List channels = channelPool.get(host); + if (channels == null || channels.isEmpty()) return null; + Iterator it = channels.iterator(); + while (it.hasNext()) { + Channel channel = it.next(); + it.remove(); + if (channel.isActive()) { + LOGGER.debug("tryAcquire channel success, host={}", host); + channel.attr(hostKey).set(host); + return channel; + } else {//链接由于意外情况不可用了,keepAlive_timeout + LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); + } + } + return null; + } + + public synchronized void tryRelease(Channel channel) { + String host = channel.attr(hostKey).getAndRemove(); + List channels = channelPool.get(host); + if (channels == null || channels.size() < maxConnPerHost) { + LOGGER.debug("tryRelease channel success, host={}", host); + channelPool.put(host, channel); + } else { + LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); + channel.close(); + } + } + + public void attachHost(String host, Channel channel) { + channel.attr(hostKey).set(host); + } + + public void close() { + for (Channel channel : channelPool.values()) { + channel.close(); + } + channelPool.clear(); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index 40bcf2c4..5e554589 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -19,261 +19,133 @@ package com.mpush.netty.http; -import com.google.common.collect.ArrayListMultimap; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.tools.config.CC; -import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.NamedThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; +import com.sun.istack.internal.NotNull; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.http.*; -import io.netty.util.*; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestEncoder; +import io.netty.handler.codec.http.HttpResponseDecoder; +import io.netty.util.AttributeKey; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; -import java.net.URL; -import java.net.URLDecoder; -import java.util.Iterator; -import java.util.List; import java.util.concurrent.TimeUnit; +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; +import static io.netty.handler.codec.http.HttpHeaderNames.HOST; +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; + /** + * Netty的一个Bootstrap是可以关联多个channel的, + * 本Client采用的就是这种模式,在种模式下如果Handler添加了@ChannelHandler.Sharable + * 注解的话,要特殊处理,因为这时的client和handler是被所有请求共享的。 + *

* Created by ohun on 2016/2/15. * * @author ohun@live.cn */ -public class NettyHttpClient implements HttpClient { +public class NettyHttpClient extends BaseService implements HttpClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); - - private final int maxConnPerHost = CC.mp.http.max_conn_per_host; - private final AttributeKey requestKey = AttributeKey.newInstance("request"); - private final AttributeKey hostKey = AttributeKey.newInstance("host"); - private final ArrayListMultimap channelPool = ArrayListMultimap.create(); - + private static final int maxContentLength = (int) CC.mp.http.max_content_length; + /*package*/ final AttributeKey requestKey = AttributeKey.newInstance("request"); + /*package*/ final HttpConnectionPool pool = new HttpConnectionPool(); private Bootstrap b; private EventLoopGroup workerGroup; private Timer timer; @Override - public void start() { - workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); - b = new Bootstrap(); - b.group(workerGroup); - b.channel(NioSocketChannel.class); - b.option(ChannelOption.SO_KEEPALIVE, true); - b.option(ChannelOption.TCP_NODELAY, true); - b.option(ChannelOption.SO_REUSEADDR, true); - b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast("decoder", new HttpResponseDecoder()); - ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 5));//5M - ch.pipeline().addLast("encoder", new HttpRequestEncoder()); - ch.pipeline().addLast("handler", new HttpClientHandler()); - } - }); - timer = new HashedWheelTimer(new PoolThreadFactory("http-client-timer-"), - 1, TimeUnit.SECONDS, 64); - } + public void request(RequestContext context) throws Exception { + URI uri = new URI(context.request.uri()); + String host = context.host = uri.getHost(); + int port = uri.getPort() == -1 ? 80 : uri.getPort(); + //1.设置请求头 + context.request.headers().set(HOST, host);//映射后的host + context.request.headers().set(CONNECTION, KEEP_ALIVE);//保存长链接 - @Override - public void stop() { - for (Channel channel : channelPool.values()) { - channel.close(); - } - channelPool.clear(); - workerGroup.shutdownGracefully(); - timer.stop(); - } + //2.添加请求超时检测队列 + timer.newTimeout(context, context.readTimeout, TimeUnit.MILLISECONDS); - @Override - public void request(final RequestInfo info) throws Exception { - URI uri = new URI(info.request.uri()); - String host = info.host = uri.getHost(); - int port = uri.getPort() == -1 ? 80 : uri.getPort(); - info.request.headers().set(HttpHeaderNames.HOST, host); - info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); - timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); - Channel channel = tryAcquire(host); + //3.先尝试从连接池里取可用链接,去取不到就创建新链接。 + Channel channel = pool.tryAcquire(host); if (channel == null) { - final long startConnectChannel = System.currentTimeMillis(); + final long startCreate = System.currentTimeMillis(); LOGGER.debug("create new channel, host={}", host); ChannelFuture f = b.connect(host, port); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - LOGGER.debug("create new channel cost:" + (System.currentTimeMillis() - startConnectChannel)); - if (future.isSuccess()) { - writeRequest(future.channel(), info); - } else { - info.tryDone(); - info.onFailure(504, "Gateway Timeout"); - LOGGER.debug("request failure request={}", info); + LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); + if (future.isSuccess()) {//3.1.把请求写到http server + writeRequest(future.channel(), context); + } else {//3.2如果链接创建失败,直接返回客户端网关超时 + context.tryDone(); + context.onFailure(504, "Gateway Timeout"); + LOGGER.warn("create new channel failure, request={}", context); } } }); } else { - writeRequest(channel, info); + //3.1.把请求写到http server + writeRequest(channel, context); } } - private synchronized Channel tryAcquire(String host) { - List channels = channelPool.get(host); - if (channels == null || channels.isEmpty()) return null; - Iterator it = channels.iterator(); - while (it.hasNext()) { - Channel channel = it.next(); - it.remove(); - if (channel.isActive()) { - LOGGER.debug("tryAcquire channel success, host={}", host); - channel.attr(hostKey).set(host); - return channel; - } else {//链接由于意外情况不可用了,keepAlive_timeout - LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); - } - } - return null; - } - - private synchronized void tryRelease(Channel channel) { - String host = channel.attr(hostKey).getAndRemove(); - List channels = channelPool.get(host); - if (channels == null || channels.size() < maxConnPerHost) { - LOGGER.debug("tryRelease channel success, host={}", host); - channelPool.put(host, channel); - } else { - LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); - channel.close(); - } - } - - private void writeRequest(Channel channel, RequestInfo info) { - channel.attr(requestKey).set(info); - channel.attr(hostKey).set(info.host); - channel.writeAndFlush(info.request).addListener(new ChannelFutureListener() { + private void writeRequest(Channel channel, RequestContext context) { + channel.attr(requestKey).set(context); + pool.attachHost(context.host, channel); + channel.writeAndFlush(context.request).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { - RequestInfo info = future.channel().attr(requestKey).getAndRemove(); + RequestContext info = future.channel().attr(requestKey).getAndRemove(); info.tryDone(); info.onFailure(503, "Service Unavailable"); LOGGER.debug("request failure request={}", info); - tryRelease(future.channel()); + pool.tryRelease(future.channel()); } } }); } - @ChannelHandler.Sharable - private class HttpClientHandler extends ChannelHandlerAdapter { - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - try { - if (info != null && info.tryDone()) { - info.onException(cause); - } - } finally { - tryRelease(ctx.channel()); - } - LOGGER.error("http client caught an error, info={}", info, cause); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - try { - if (info != null && info.tryDone()) { - HttpResponse response = (HttpResponse) msg; - if (isRedirect(response)) { - if (info.onRedirect(response)) { - String location = getRedirectLocation(info.request, response); - if (location != null && location.length() > 0) { - info.cancelled.set(false); - info.request = info.request.copy().setUri(location); - request(info); - return; - } - } - } - info.onResponse(response); - LOGGER.debug("request done request={}", info); - } - } finally { - tryRelease(ctx.channel()); - ReferenceCountUtil.release(msg); - } - } - - private boolean isRedirect(HttpResponse response) { - HttpResponseStatus status = response.status(); - switch (status.code()) { - case 300: - case 301: - case 302: - case 303: - case 305: - case 307: - return true; - default: - return false; - } - } - - private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { - String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); - if (hdr != null) { - if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { - return hdr; - } else { - URL orig = new URL(request.uri()); - String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); - if (hdr.startsWith("/")) { - pth = hdr; - } else if (pth.endsWith("/")) { - pth += hdr; - } else { - pth += "/" + hdr; - } - StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); - sb.append("://").append(orig.getHost()); - if (orig.getPort() > 0) { - sb.append(":").append(orig.getPort()); - } - if (pth.charAt(0) != '/') { - sb.append('/'); - } - sb.append(pth); - return sb.toString(); - } + @Override + protected void doStart(@NotNull Listener listener) throws Throwable { + workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); + b = new Bootstrap(); + b.group(workerGroup); + b.channel(NioSocketChannel.class); + b.option(ChannelOption.SO_KEEPALIVE, true); + b.option(ChannelOption.TCP_NODELAY, true); + b.option(ChannelOption.SO_REUSEADDR, true); + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast("decoder", new HttpResponseDecoder()); + ch.pipeline().addLast("aggregator", new HttpObjectAggregator(maxContentLength)); + ch.pipeline().addLast("encoder", new HttpRequestEncoder()); + ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this)); } - return null; - } + }); + timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), 1, TimeUnit.SECONDS, 64); + } - private HttpRequest copy(String uri, HttpRequest request) { - HttpRequest nue = request; - if (request instanceof DefaultFullHttpRequest) { - DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; - FullHttpRequest rq; - try { - rq = dfrq.copy(); - } catch (IllegalReferenceCountException e) { // Empty bytebuf - rq = dfrq; - } - rq.setUri(uri); - } else { - DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); - dfr.headers().set(request.headers()); - nue = dfr; - } - return nue; - } + @Override + protected void doStop(@NotNull Listener listener) throws Throwable { + pool.close(); + workerGroup.shutdownGracefully(); + timer.stop(); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java similarity index 87% rename from mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java rename to mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java index 2a023062..915a09b3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java @@ -29,37 +29,39 @@ import java.util.concurrent.atomic.AtomicBoolean; -public class RequestInfo implements TimerTask, HttpCallback { +public class RequestContext implements TimerTask, HttpCallback { private static final int TIMEOUT = CC.mp.http.default_read_timeout; final AtomicBoolean cancelled = new AtomicBoolean(false); final long startTime = System.currentTimeMillis(); + final int readTimeout; long endTime = startTime; - int readTimeout = TIMEOUT; private String uri; private HttpCallback callback; FullHttpRequest request; String host; - public RequestInfo(FullHttpRequest request, HttpCallback callback) { + public RequestContext(FullHttpRequest request, HttpCallback callback) { this.callback = callback; this.request = request; this.uri = request.uri(); + this.readTimeout = parseTimeout(); + } + + private int parseTimeout() { String timeout = request.headers().getAndRemoveAndConvert(Constants.HTTP_HEAD_READ_TIMEOUT); if (timeout != null) { Integer integer = Ints.tryParse(timeout); - if (integer != null && integer > 0) readTimeout = integer; + if (integer != null && integer > 0) { + return integer; + } } + return TIMEOUT; } public int getReadTimeout() { return readTimeout; } - public RequestInfo setReadTimeout(int timeout) { - this.readTimeout = timeout; - return this; - } - @Override public void run(Timeout timeout) throws Exception { if (tryDone()) { @@ -69,6 +71,11 @@ public void run(Timeout timeout) throws Exception { } } + /** + * 由于检测请求超时的任务存在,为了防止多线程下重复处理 + * + * @return + */ public boolean tryDone() { return cancelled.compareAndSet(false, true); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 5e5a5807..be8495aa 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -19,10 +19,15 @@ package com.mpush.netty.server; -import com.mpush.api.Server; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; +import com.mpush.api.service.ServiceException; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; +import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; +import com.sun.istack.internal.NotNull; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -34,16 +39,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Locale; +import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; -import static com.mpush.tools.thread.pool.ThreadPoolManager.I; - /** * Created by ohun on 2015/12/22. * * @author ohun@live.cn */ -public abstract class NettyServer implements Server { +public abstract class NettyServer extends BaseService implements Server { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -92,7 +97,11 @@ public void start(final Listener listener) { if (!serverState.compareAndSet(State.Initialized, State.Starting)) { throw new IllegalStateException("Server already started or have not init"); } - createNioServer(listener); + if (useNettyEpoll()) { + createEpollServer(listener); + } else { + createNioServer(listener); + } } private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { @@ -172,7 +181,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } catch (Exception e) { logger.error("server start exception", e); if (listener != null) listener.onFailure(e); - throw new RuntimeException("server start exception, port=" + port, e); + throw new ServiceException("server start exception, port=" + port, e); } finally { /*** * 优雅关闭 @@ -181,17 +190,16 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, I.getBossExecutor()); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, I.getWorkExecutor()); + private void createNioServer(Listener listener) { + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, getBossExecutor()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, getWorkExecutor()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } - @SuppressWarnings("unused") - private void createEpollServer(final Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, I.getBossExecutor()); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, I.getWorkExecutor()); + private void createEpollServer(Listener listener) { + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, getBossExecutor()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, getWorkExecutor()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -231,4 +239,28 @@ protected void initPipeline(ChannelPipeline pipeline) { pipeline.addLast("encoder", getEncoder()); pipeline.addLast("handler", getChannelHandler()); } + + protected Executor getBossExecutor() { + return null; + } + + protected Executor getWorkExecutor() { + return null; + } + + private boolean useNettyEpoll() { + if (!"netty".equals(CC.mp.core.epoll_provider)) return false; + String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); + return name.startsWith("linux"); + } + + @Override + protected void doStart(@NotNull Listener listener) throws Throwable { + + } + + @Override + protected void doStop(@NotNull Listener listener) throws Throwable { + + } } diff --git a/mpush-netty/src/test/resources/logback.xml b/mpush-netty/src/test/resources/logback.xml index e638aca3..8b23ef25 100644 --- a/mpush-netty/src/test/resources/logback.xml +++ b/mpush-netty/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN + + System.out + UTF-8 + + INFO - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index c70e10f3..38cb099d 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -19,7 +19,7 @@ package com.mpush.test.client; -import com.mpush.api.Client; +import com.mpush.api.service.Client; import com.mpush.client.connect.ClientConfig; import com.mpush.client.connect.ConnectClient; import com.mpush.common.security.CipherBox; @@ -27,6 +27,7 @@ import com.mpush.zk.node.ZKServerNode; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; public class ConnClientTestMain { @@ -34,20 +35,19 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { Logs.init(); ConnectClientBoot main = new ConnectClientBoot(); - main.start(); + main.run(); List serverList = main.getServers(); int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ZKServerNode server = serverList.get(index); - + server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); for (int i = 0; i < 1; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; String userId = "user-" + i; String deviceId = "test-device-id-" + i; - String cipher = ""; byte[] clientKey = CipherBox.I.randomAESKey(); byte[] iv = CipherBox.I.randomAESIV(); @@ -59,10 +59,8 @@ public static void main(String[] args) throws Exception { config.setOsName(osName); config.setOsVersion(osVersion); config.setUserId(userId); - config.setCipher(cipher); - Client client = new ConnectClient(server.getIp(), server.getPort(), config); - client.start(null); - Thread.sleep(10); + Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); } LockSupport.park(); diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java index 20615b46..940dd8c3 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java @@ -21,7 +21,6 @@ import com.google.common.collect.Lists; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.log.Logs; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; import com.mpush.zk.node.ZKServerNode; @@ -31,8 +30,8 @@ public class ConnectClientBoot { private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); - public void start() { - ZKClient.I.init(); + public void run() { + ZKClient.I.start(); RedisManager.I.init(); listener.beginWatch(); } diff --git a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java index 2bad14c9..b85ca668 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java @@ -19,8 +19,6 @@ package com.mpush.test.connection.body; -import java.security.interfaces.RSAPrivateKey; - import com.mpush.common.message.HandshakeMessage; import com.mpush.tools.crypto.RSAUtils; import org.apache.commons.lang3.builder.ToStringBuilder; @@ -28,66 +26,68 @@ import org.junit.Before; import org.junit.Test; +import java.security.interfaces.RSAPrivateKey; + public class BodyTest { - + private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; private String daily_allocServer = "http://111.1.57.148/mpns/push/server/"; private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; private RSAPrivateKey dailyPrivateKey = null; - + private String pre_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB"; private String pre_allocServer = "http://115.29.230.2/mpns/push/server/"; private String pre_privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA=="; private RSAPrivateKey prePrivateKey = null; - + //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB private String online_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB"; private String online_allocServer = "http://allot.mangguoyisheng.com/"; private String online_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g="; - private RSAPrivateKey onlinePrivateKey = null; + private RSAPrivateKey onlinePrivateKey = null; + + String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; + + byte[] data = null; + + @Before + public void init() { + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); + onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); + String[] temp = rawData.split(","); + data = new byte[temp.length]; + for (int i = 0; i < temp.length; i++) { + data[i] = Byte.parseByte(temp[i].trim()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void dailyDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, dailyPrivateKey); + decode(message); + } + + @Test + public void preDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, prePrivateKey); + decode(message); + } + + @Test + public void onlineDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, onlinePrivateKey); + decode(message); + } - String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; - - byte[]data = null; - - @Before - public void init(){ - try { - dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); - prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); - onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); - String[] temp = rawData.split(","); - data = new byte[temp.length]; - for(int i = 0;i mappings = ArrayListMultimap.create(); - - String dnsString = "111.1.57.148=127.0.0.1,127.0.0.2;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1"; - if (Strings.isNullOrEmpty(dnsString)) return; - Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); - Splitter vsp = Splitter.on(','); - for (Map.Entry entry : map.entrySet()) { - String value = entry.getValue(); - if (Strings.isNullOrEmpty(value)) continue; - Iterable it = vsp.split(entry.getValue()); - mappings.putAll(entry.getKey(), it); - } - - System.out.println(mappings); - - } + @Test + public void test() throws MalformedURLException { + String url = "http://baidu.com:9001/xxx/xxx?s=nc=1"; + DnsMapping mapping = new DnsMapping("127.0.0.1", 8080); + String s = mapping.translate(new URL(url)); + System.out.println(url); + System.out.println(s); + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 2f565f57..0a89949e 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -19,65 +19,64 @@ package com.mpush.test.gson; -import java.util.Map; - -import com.mpush.tools.Jsons; -import org.junit.Test; - import com.google.common.collect.Maps; import com.mpush.api.push.PushContent; import com.mpush.api.push.PushContent.PushType; +import com.mpush.tools.Jsons; +import org.junit.Test; + +import java.util.Map; public class GsonTest { - - @Test - public void test(){ - Map map = Maps.newHashMap(); - map.put("key1", 1121+""); - map.put("key2", "value2"); - - PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); - - - System.out.println(Jsons.toJson(content)); - - } - - @Test - public void test2(){ - ValueMap map = new ValueMap("1122", "value2"); - - PushContent content = PushContent.build(PushType.MESSAGE,Jsons.toJson(map)); - - - System.out.println(Jsons.toJson(content)); - - PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); - - System.out.println(newContetn.getContent()); - - - } - - private static class ValueMap{ - - private String key1; - private String key2; - - public ValueMap(String key1, String key2) { - this.key1 = key1; - this.key2 = key2; - } - - public String getKey1() { - return key1; - } - - public String getKey2() { - return key2; - } - - - } + + @Test + public void test() { + Map map = Maps.newHashMap(); + map.put("key1", 1121 + ""); + map.put("key2", "value2"); + + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + } + + @Test + public void test2() { + ValueMap map = new ValueMap("1122", "value2"); + + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); + + System.out.println(newContetn.getContent()); + + + } + + private static class ValueMap { + + private String key1; + private String key2; + + public ValueMap(String key1, String key2) { + this.key1 = key1; + this.key2 = key2; + } + + public String getKey1() { + return key1; + } + + public String getKey2() { + return key2; + } + + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index f15c1eb6..ac13d0bd 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -36,14 +36,13 @@ public class PushClientTestMain { public static void main(String[] args) throws Exception { Logs.init(); - PushSender sender = PushSender.factory.get(); - sender.start(); - Thread.sleep(1000); - for (int i = 0; i < 100; i++) { + PushSender sender = PushSender.create(); + sender.start().get(); + for (int i = 0; i < 10000; i++) { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); - - sender.send(Jsons.toJson(content), Arrays.asList("doctor43test", "user-0", "huang"), new PushSender.Callback() { + //Thread.sleep(1000); + sender.send(Jsons.toJson(content), Arrays.asList("user-0"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); @@ -64,7 +63,6 @@ public void onTimeout(String userId) { System.err.println("push onTimeout userId=" + userId); } }); - Thread.sleep(10000); } LockSupport.park(); } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java index 17762180..69be00b2 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java @@ -19,52 +19,46 @@ package com.mpush.test.redis; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - import com.mpush.cache.redis.hash.ConsistentHash; import com.mpush.cache.redis.hash.Node; import org.junit.Test; - import redis.clients.util.Hashing; import redis.clients.util.MurmurHash; +import java.util.*; + public class ConsistentHashTest { - - private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 - - @Test - public void test(){ - Map map = new HashMap();// 每台真实机器节点上保存的记录条数 - List nodes = new ArrayList();// 真实机器节点 - // 10台真实机器节点集群 - for (int i = 1; i <= 10; i++) { - map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 - Node node = new Node(IP_PREFIX + i, "node" + i); - nodes.add(node); - } - Hashing hashFunction = new MurmurHash(); // hash函数实例 - ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 - // 将5000条记录尽可能均匀的存储到10台机器节点 - for (int i = 0; i < 5000; i++) { - // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 - String data = UUID.randomUUID().toString() + i; - // 通过记录找到真实机器节点 - Node node = consistentHash.get(data); - // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 - // 每台真实机器节点上保存的记录条数加1 - map.put(node.getIp(), map.get(node.getIp()) + 1); - } - // 打印每台真实机器节点保存的记录条数 - for (int i = 1; i <= 10; i++) { - System.out.println(IP_PREFIX + i + "节点记录条数:" - + map.get("192.168.1." + i)); - } - - } + + private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 + + @Test + public void test() { + Map map = new HashMap();// 每台真实机器节点上保存的记录条数 + List nodes = new ArrayList();// 真实机器节点 + // 10台真实机器节点集群 + for (int i = 1; i <= 10; i++) { + map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 + Node node = new Node(IP_PREFIX + i, "node" + i); + nodes.add(node); + } + Hashing hashFunction = new MurmurHash(); // hash函数实例 + ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 + // 将5000条记录尽可能均匀的存储到10台机器节点 + for (int i = 0; i < 5000; i++) { + // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 + String data = UUID.randomUUID().toString() + i; + // 通过记录找到真实机器节点 + Node node = consistentHash.get(data); + // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 + // 每台真实机器节点上保存的记录条数加1 + map.put(node.getIp(), map.get(node.getIp()) + 1); + } + // 打印每台真实机器节点保存的记录条数 + for (int i = 1; i <= 10; i++) { + System.out.println(IP_PREFIX + i + "节点记录条数:" + + map.get("192.168.1." + i)); + } + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java index 763f83eb..43a30342 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java @@ -19,14 +19,14 @@ package com.mpush.test.redis; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import org.junit.Test; public class MPushUtilTest { - - @Test - public void getIp() throws Exception{ - System.out.println(MPushUtil.getExtranetAddress()); - } + + @Test + public void getIp() throws Exception { + System.out.println(Utils.getExtranetAddress()); + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/User.java b/mpush-test/src/test/java/com/mpush/test/redis/User.java index 76aded74..f9279eee 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/User.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/User.java @@ -22,38 +22,43 @@ import java.io.Serializable; import java.util.Date; -public class User implements Serializable{ - - private static final long serialVersionUID = -6269129553435313118L; - - private String name; - private int age; - private Date date; - - public User(String name, int age, Date date) { - this.name = name; - this.age = age; - this.date = date; - } - - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public int getAge() { - return age; - } - public void setAge(int age) { - this.age = age; - } - public Date getDate() { - return date; - } - public void setDate(Date date) { - this.date = date; - } - +public class User implements Serializable { + + private static final long serialVersionUID = -6269129553435313118L; + + private String name; + private int age; + private Date date; + + public User(String name, int age, Date date) { + this.name = name; + this.age = age; + this.date = date; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java index ea20f24b..5e3433de 100644 --- a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java @@ -19,32 +19,32 @@ package com.mpush.test.util; +import com.mpush.tools.Utils; +import org.junit.Test; + import java.net.URI; import java.net.URISyntaxException; -import com.mpush.tools.MPushUtil; -import org.junit.Test; - public class TelnetTest { - @Test - public void test(){ - boolean ret = MPushUtil.checkHealth("120.27.196.68", 82); - System.out.println(ret); - } - - @Test - public void test2(){ - boolean ret = MPushUtil.checkHealth("120.27.196.68", 80); - System.out.println(ret); - } - - @Test - public void uriTest() throws URISyntaxException{ - String url = "http://127.0.0.1"; - URI uri = new URI(url); - System.out.println(uri.getPort()); - } - - + @Test + public void test() { + boolean ret = Utils.checkHealth("120.27.196.68", 82); + System.out.println(ret); + } + + @Test + public void test2() { + boolean ret = Utils.checkHealth("120.27.196.68", 80); + System.out.println(ret); + } + + @Test + public void uriTest() throws URISyntaxException { + String url = "http://127.0.0.1"; + URI uri = new URI(url); + System.out.println(uri.getPort()); + } + + } diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml new file mode 100644 index 00000000..0d67de0e --- /dev/null +++ b/mpush-test/src/test/resources/logback.xml @@ -0,0 +1,30 @@ + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.err + UTF-8 + + debug + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + + diff --git a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java index cd928e04..b5963e34 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -22,6 +22,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.mpush.api.Constants; +import com.mpush.tools.common.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -117,10 +118,4 @@ private static void append(Map.Entry entry, StringBuilder sb) { sb.append(':'); sb.append('"').append(value).append('"'); } - - public static void main(String[] args) { - String tet = "7"; - Long l = Jsons.fromJson(tet, Long.class); - System.out.println(l); - } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java similarity index 98% rename from mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/Utils.java index aaf8ee94..6a041dfd 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -19,6 +19,7 @@ package com.mpush.tools; +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,8 +38,8 @@ * * @author ohun@live.cn */ -public final class MPushUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); +public final class Utils { + private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); private static String LOCAL_IP; diff --git a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java similarity index 52% rename from mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java index 36c16940..f8db214d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools; +package com.mpush.tools.common; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -29,130 +29,136 @@ public class GenericsUtil { - /** - * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * + /** + * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * * @param clazz clazz 需要反射的类,该类必须继承范型父类 - * @param index 泛型参数所在索引,从0开始. + * @param index 泛型参数所在索引,从0开始. * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz, int index) { + */ + public static Class getSuperClassGenericType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass();//得到泛型父类 //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class if (!(genType instanceof ParameterizedType)) { - return Object.class; - } + return Object.class; + } //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 - Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); - if (index >= params.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } + Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); + if (index >= params.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } if (!(params[index] instanceof Class)) { - return Object.class; - } + return Object.class; + } return (Class) params[index]; } - /** - * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * + + /** + * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * * @param clazz clazz 需要反射的类,该类必须继承泛型父类 * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz) { - return getSuperClassGenericType(clazz, 0); + */ + public static Class getSuperClassGenericType(Class clazz) { + return getSuperClassGenericType(clazz, 0); } - /** + + /** * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} - * + * * @param method 方法 - * @param index 泛型参数所在索引,从0开始. + * @param index 泛型参数所在索引,从0开始. * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method, int index) { - Type returnType = method.getGenericReturnType(); - if(returnType instanceof ParameterizedType){ - ParameterizedType type = (ParameterizedType) returnType; - Type[] typeArguments = type.getActualTypeArguments(); - if (index >= typeArguments.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class)typeArguments[index]; - } - return Object.class; + */ + public static Class getMethodGenericReturnType(Method method, int index) { + Type returnType = method.getGenericReturnType(); + if (returnType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) returnType; + Type[] typeArguments = type.getActualTypeArguments(); + if (index >= typeArguments.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class) typeArguments[index]; + } + return Object.class; } - /** + + /** * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} - * + * * @param method 方法 * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method) { - return getMethodGenericReturnType(method, 0); + */ + public static Class getMethodGenericReturnType(Method method) { + return getMethodGenericReturnType(method, 0); } - - /** + + /** * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * + * * @param method 方法 - * @param index 第几个输入参数 + * @param index 第几个输入参数 * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method, int index) { - List> results = new ArrayList>(); - Type[] genericParameterTypes = method.getGenericParameterTypes(); - if (index >= genericParameterTypes.length ||index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - Type genericParameterType = genericParameterTypes[index]; - if(genericParameterType instanceof ParameterizedType){ - ParameterizedType aType = (ParameterizedType) genericParameterType; - Type[] parameterArgTypes = aType.getActualTypeArguments(); - for(Type parameterArgType : parameterArgTypes){ - Class parameterArgClass = (Class) parameterArgType; - results.add(parameterArgClass); - } - return results; - } - return results; + */ + public static List> getMethodGenericParameterTypes(Method method, int index) { + List> results = new ArrayList>(); + Type[] genericParameterTypes = method.getGenericParameterTypes(); + if (index >= genericParameterTypes.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + Type genericParameterType = genericParameterTypes[index]; + if (genericParameterType instanceof ParameterizedType) { + ParameterizedType aType = (ParameterizedType) genericParameterType; + Type[] parameterArgTypes = aType.getActualTypeArguments(); + for (Type parameterArgType : parameterArgTypes) { + Class parameterArgClass = (Class) parameterArgType; + results.add(parameterArgClass); + } + return results; + } + return results; } - /** + + /** * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 + * + * @param method 方法 * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method) { - return getMethodGenericParameterTypes(method, 0); + */ + public static List> getMethodGenericParameterTypes(Method method) { + return getMethodGenericParameterTypes(method, 0); } - /** + + /** * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @param index 泛型参数所在索引,从0开始. + * + * @param field 字段 + * @param index 泛型参数所在索引,从0开始. * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field, int index) { - Type genericFieldType = field.getGenericType(); - - if(genericFieldType instanceof ParameterizedType){ - ParameterizedType aType = (ParameterizedType) genericFieldType; - Type[] fieldArgTypes = aType.getActualTypeArguments(); - if (index >= fieldArgTypes.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class)fieldArgTypes[index]; - } - return Object.class; + */ + public static Class getFieldGenericType(Field field, int index) { + Type genericFieldType = field.getGenericType(); + + if (genericFieldType instanceof ParameterizedType) { + ParameterizedType aType = (ParameterizedType) genericFieldType; + Type[] fieldArgTypes = aType.getActualTypeArguments(); + if (index >= fieldArgTypes.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class) fieldArgTypes[index]; + } + return Object.class; } - /** + + /** * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 + * + * @param field 字段 * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field) { - return getFieldGenericType(field, 0); + */ + public static Class getFieldGenericType(Field field) { + return getFieldGenericType(field, 0); } - + } diff --git a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java similarity index 92% rename from mpush-tools/src/main/java/com/mpush/tools/IOUtils.java rename to mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java index c603c4c6..da840eec 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools; +package com.mpush.tools.common; import com.mpush.api.Constants; import org.slf4j.Logger; @@ -49,9 +49,9 @@ public static void close(Closeable closeable) { } public static byte[] compress(byte[] data) { - - Profiler.enter("start compress"); - + + Profiler.enter("start compress"); + ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); DeflaterOutputStream zipOut = new DeflaterOutputStream(out); try { @@ -68,8 +68,8 @@ public static byte[] compress(byte[] data) { return out.toByteArray(); } - public static byte[] uncompress(byte[] data) { - Profiler.enter("start uncompress"); + public static byte[] decompress(byte[] data) { + Profiler.enter("start decompress"); InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4); byte[] buffer = new byte[1024]; @@ -79,7 +79,7 @@ public static byte[] uncompress(byte[] data) { out.write(buffer, 0, length); } } catch (IOException e) { - LOGGER.error("uncompress ex", e); + LOGGER.error("decompress ex", e); return Constants.EMPTY_BYTES; } finally { close(zipIn); diff --git a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java similarity index 83% rename from mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java index 7e991c9f..e0ee5771 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -17,8 +17,14 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools; +package com.mpush.tools.common; +import com.sun.management.HotSpotDiagnosticMXBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.management.MBeanServer; +import javax.management.ObjectName; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -31,24 +37,16 @@ import java.util.Set; import java.util.concurrent.Executors; -import javax.management.MBeanServer; -import javax.management.ObjectName; +public class JVMUtil { -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class); -import com.sun.management.HotSpotDiagnosticMXBean; - -public class JVMUtil { - - private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class); - - private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; + private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; private static volatile HotSpotDiagnosticMXBean mxBean; - + private static Object lock = new Object(); - - public static void jstack(OutputStream stream) throws Exception { + + public static void jstack(OutputStream stream) throws Exception { try { Map map = Thread.getAllStackTraces(); Iterator> ite = map.entrySet().iterator(); @@ -69,18 +67,18 @@ public static void jstack(OutputStream stream) throws Exception { throw e; } } - - public static void dumpJstack(final String jvmPath){ - Executors.newSingleThreadExecutor().execute(new Runnable() { + + public static void dumpJstack(final String jvmPath) { + Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { String logPath = jvmPath; FileOutputStream jstackStream = null; try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.LOGGER")); + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); JVMUtil.jstack(jstackStream); } catch (Throwable t) { - LOGGER.error("Dump JVM cache Error!", t); + LOGGER.error("Dump JVM cache Error!", t); } finally { if (jstackStream != null) { try { @@ -91,8 +89,8 @@ public void run() { } } }); - } - + } + private static HotSpotDiagnosticMXBean getMxBean() { try { return AccessController.doPrivileged(new PrivilegedExceptionAction() { @@ -115,7 +113,7 @@ public HotSpotDiagnosticMXBean run() throws Exception { return null; } } - + private static void initHotspotMBean() throws Exception { if (mxBean == null) { synchronized (lock) { @@ -125,10 +123,10 @@ private static void initHotspotMBean() throws Exception { } } } - + public static void jMap(String fileName, boolean live) { - File f = new File(fileName, System.currentTimeMillis()+"-jmap.LOGGER"); - String currentFileName = f.getPath(); + File f = new File(fileName, System.currentTimeMillis() + "-jmap.LOGGER"); + String currentFileName = f.getPath(); try { initHotspotMBean(); if (f.exists()) { @@ -136,17 +134,17 @@ public static void jMap(String fileName, boolean live) { } mxBean.dumpHeap(currentFileName, live); } catch (Exception e) { - LOGGER.error("dumpHeap Error!"+currentFileName, e); + LOGGER.error("dumpHeap Error!" + currentFileName, e); } } - - public static void dumpJmap(final String jvmPath){ - Executors.newSingleThreadExecutor().execute(new Runnable() { + + public static void dumpJmap(final String jvmPath) { + Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { - jMap(jvmPath, false); + jMap(jvmPath, false); } }); - } + } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java similarity index 97% rename from mpush-tools/src/main/java/com/mpush/tools/Pair.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Pair.java index 25edf5e4..f32cdb9f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Pair.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools; +package com.mpush.tools.common; /** * Created by ohun on 2015/12/24. diff --git a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java similarity index 98% rename from mpush-tools/src/main/java/com/mpush/tools/Profiler.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java index ff4def8b..0d8f29c2 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java @@ -17,23 +17,23 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools; +package com.mpush.tools.common; +import org.apache.commons.lang3.StringUtils; + import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.apache.commons.lang3.StringUtils; - /** * 用来测试并统计线程执行时间的工具。 */ -@SuppressWarnings(value={"rawtypes","unchecked"}) +@SuppressWarnings(value = {"rawtypes", "unchecked"}) public class Profiler { - private static final ThreadLocal entryStack = new ThreadLocal(); + private static final ThreadLocal entryStack = new ThreadLocal(); public static final String EMPTY_STRING = ""; /** @@ -49,7 +49,7 @@ public static void start() { * @param message 第一个entry的信息 */ - public static void start(String message) { + public static void start(String message) { entryStack.set(new Entry(message, null, null)); } @@ -212,7 +212,7 @@ private Entry(Object message, Entry parentEntry, Entry firstEntry) { this.message = message; this.startTime = System.currentTimeMillis(); this.parentEntry = parentEntry; - this.firstEntry = (Entry) firstEntry == null ? this : firstEntry; + this.firstEntry = firstEntry == null ? this : firstEntry; this.baseTime = (firstEntry == null) ? 0 : firstEntry.startTime; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/mpush/tools/common/Strings.java similarity index 97% rename from mpush-tools/src/main/java/com/mpush/tools/Strings.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Strings.java index 35c8a2fa..c7e0dac4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Strings.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools; +package com.mpush.tools.common; /** * Created by ohun on 2015/12/23. diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java new file mode 100644 index 00000000..112cf893 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java @@ -0,0 +1,94 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.common; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by yxx on 2016/5/26. + * + * @author ohun@live.cn (夜色) + */ +public final class TimeLine { + private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + private final TimePoint root = new TimePoint("root"); + private final String name; + private TimePoint current = root; + + public TimeLine() { + name = "TimeLine"; + } + + public TimeLine(String name) { + this.name = name; + } + + public void begin() { + addTimePoint("begin"); + } + + public void addTimePoint(String name) { + current = current.next = new TimePoint(name); + } + + public void end() { + addTimePoint("end"); + } + + public void clean() { + root.next = null; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(name); + if (root.next != null) { + sb.append('[').append(current.point - root.next.point).append(']'); + } + sb.append('{'); + TimePoint next = root; + while ((next = next.next) != null) { + sb.append(next.toString()); + } + sb.append('}'); + return sb.toString(); + } + + private static class TimePoint { + private final String name; + private final long point = System.currentTimeMillis(); + private TimePoint next; + + public TimePoint(String name) { + this.name = name; + } + + public void setNext(TimePoint next) { + this.next = next; + } + + @Override + public String toString() { + String header = name + "[" + formatter.format(new Date(point)) + "]"; + if (next == null) return header; + return header + " --" + (next.point - point) + "(ms)--> "; + } + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 9dec835e..482ab48f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -28,6 +28,7 @@ import com.typesafe.config.ConfigObject; import java.io.File; +import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -60,54 +61,99 @@ static Config load() { interface mp { Config cfg = CC.cfg.getObject("mp").toConfig(); - String log_dir = cfg.getString("log.dir"); String log_level = cfg.getString("log.level"); - interface security { - Config cfg = mp.cfg.getObject("security").toConfig(); + interface core { + Config cfg = mp.cfg.getObject("core").toConfig(); - int aes_key_length = cfg.getInt("aes-key-length"); + int session_expired_time = (int) cfg.getDuration("session-expired-time").getSeconds(); - String public_key = cfg.getString("public-key"); + int max_heartbeat = (int) cfg.getDuration("max-heartbeat", TimeUnit.MILLISECONDS); - String private_key = cfg.getString("private-key"); + int max_packet_size = (int) cfg.getMemorySize("max-packet-size").toBytes(); - int ras_key_length = cfg.getInt("ras-key-length"); + int min_heartbeat = (int) cfg.getDuration("min-heartbeat", TimeUnit.MILLISECONDS); + + long compress_threshold = cfg.getBytes("compress-threshold"); + int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times"); + + String epoll_provider = cfg.getString("epoll-provider"); } - interface zk { - Config cfg = mp.cfg.getObject("zk").toConfig(); + interface net { + Config cfg = mp.cfg.getObject("net").toConfig(); - int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS); + int connect_server_port = cfg.getInt("connect-server-port"); + int gateway_server_port = cfg.getInt("gateway-server-port"); + int admin_server_port = cfg.getInt("admin-server-port"); - String local_cache_path = cfg.getString("local-cache-path"); + interface public_ip_mapping { - int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS); + Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); - String namespace = cfg.getString("namespace"); + static String getString(String localIp) { + return (String) mappings.getOrDefault(localIp, localIp); + } - String digest = cfg.getString("digest"); + } - String server_address = cfg.getString("server-address"); + interface traffic_shaping { + Config cfg = net.cfg.getObject("traffic-shaping").toConfig(); + + interface gateway_client { + Config cfg = traffic_shaping.cfg.getObject("gateway-client").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } - interface retry { - Config cfg = zk.cfg.getObject("retry").toConfig(); + interface gateway_server { + Config cfg = traffic_shaping.cfg.getObject("gateway-server").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } - int maxRetries = cfg.getInt("maxRetries"); + interface connect_server { + Config cfg = traffic_shaping.cfg.getObject("connect-server").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + } + } - int baseSleepTimeMs = (int) cfg.getDuration("baseSleepTimeMs", TimeUnit.MILLISECONDS); + interface security { - int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS); + Config cfg = mp.cfg.getObject("security").toConfig(); + + int aes_key_length = cfg.getInt("aes-key-length"); + + String public_key = cfg.getString("public-key"); + + String private_key = cfg.getString("private-key"); + + int ras_key_length = cfg.getInt("ras-key-length"); - } } interface thread { + Config cfg = mp.cfg.getObject("thread").toConfig(); interface pool { + Config cfg = thread.cfg.getObject("pool").toConfig(); interface boss { @@ -115,6 +161,7 @@ interface boss { int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); + } interface work { @@ -122,6 +169,7 @@ interface work { int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); + } interface event_bus { @@ -129,6 +177,7 @@ interface event_bus { int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); + } interface http_proxy { @@ -136,6 +185,7 @@ interface http_proxy { int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); + } interface biz { @@ -143,6 +193,7 @@ interface biz { int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); + } interface mq { @@ -150,12 +201,51 @@ interface mq { int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); + + } + + interface push_callback { + Config cfg = pool.cfg.getObject("push-callback").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); } } + + } + + interface zk { + + Config cfg = mp.cfg.getObject("zk").toConfig(); + + int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS); + + String local_cache_path = cfg.getString("local-cache-path"); + + int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS); + + String namespace = cfg.getString("namespace"); + + String digest = cfg.getString("digest"); + + String server_address = cfg.getString("server-address"); + + interface retry { + + Config cfg = zk.cfg.getObject("retry").toConfig(); + + int maxRetries = cfg.getInt("maxRetries"); + + int baseSleepTimeMs = (int) cfg.getDuration("baseSleepTimeMs", TimeUnit.MILLISECONDS); + + int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS); + } + } interface redis { Config cfg = mp.cfg.getObject("redis").toConfig(); + boolean write_to_zk = cfg.getBoolean("write-to-zk"); List cluster_group = cfg.getList("cluster-group") @@ -169,6 +259,7 @@ interface redis { .collect(toCollection(ArrayList::new)); interface config { + Config cfg = redis.cfg.getObject("config").toConfig(); boolean jmxEnabled = cfg.getBoolean("jmxEnabled"); @@ -187,7 +278,7 @@ interface config { String jmxNameBase = cfg.getString("jmxNameBase"); - long numTestsPerEvictionRun = cfg.getDuration("numTestsPerEvictionRun", TimeUnit.MILLISECONDS); + int numTestsPerEvictionRun = (int) cfg.getDuration("numTestsPerEvictionRun", TimeUnit.MILLISECONDS); String jmxNamePrefix = cfg.getString("jmxNamePrefix"); @@ -211,17 +302,15 @@ interface config { } - interface monitor { - Config cfg = mp.cfg.getObject("monitor").toConfig(); - - boolean dump_stack = cfg.getBoolean("dump-stack"); - - } - interface http { + Config cfg = mp.cfg.getObject("http").toConfig(); + boolean proxy_enabled = cfg.getBoolean("proxy-enabled"); + int default_read_timeout = (int) cfg.getDuration("default-read-timeout", TimeUnit.MILLISECONDS); + int max_conn_per_host = cfg.getInt("max-conn-per-host"); - boolean proxy_enable = cfg.getBoolean("proxy-enable"); + + long max_content_length = cfg.getBytes("max-content-length"); Map> dns_mapping = loadMapping(); @@ -237,46 +326,17 @@ static Map> loadMapping() { return map; } - int default_read_timeout = (int) cfg.getDuration("default-read-timeout", TimeUnit.MILLISECONDS); - - int max_conn_per_host = cfg.getInt("max-conn-per-host"); - } - interface net { - Config cfg = mp.cfg.getObject("net").toConfig(); - - int gateway_server_port = cfg.getInt("gateway-server-port"); - - int connect_server_port = cfg.getInt("connect-server-port"); - - interface public_ip_mapping { - Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); - - static String getString(String localIp) { - return (String) mappings.getOrDefault(localIp, localIp); - } - } - - int admin_server_port = cfg.getInt("admin-server-port"); - - } - - interface core { - Config cfg = mp.cfg.getObject("core").toConfig(); - - int session_expired_time = (int) cfg.getDuration("session-expired-time").getSeconds(); - - int max_heartbeat = (int) cfg.getDuration("max-heartbeat", TimeUnit.MILLISECONDS); - - int max_packet_size = (int) cfg.getMemorySize("max-packet-size").toBytes(); + interface monitor { - int min_heartbeat = (int) cfg.getDuration("min-heartbeat", TimeUnit.MILLISECONDS); + Config cfg = mp.cfg.getObject("monitor").toConfig(); + String dump_dir = cfg.getString("dump-dir"); + boolean dump_stack = cfg.getBoolean("dump-stack"); + boolean print_log = cfg.getBoolean("print-log"); - long compress_threshold = cfg.getBytes("compress-threshold"); + Duration dump_period = cfg.getDuration("dump-period"); - int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times"); } } - } \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java index fe1b22ff..7f305bf3 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -19,9 +19,9 @@ package com.mpush.tools.config; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; -import static com.mpush.tools.MPushUtil.getInetAddress; +import static com.mpush.tools.Utils.getInetAddress; /** * Created by yxx on 2016/5/18. @@ -42,7 +42,7 @@ public int getHeartbeat(int min, int max) { } public String getLocalIp() { - return MPushUtil.getLocalIp(); + return Utils.getLocalIp(); } public String getPublicIp() { diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java deleted file mode 100644 index 2a6017f4..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/DnsMappingConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.config.converter; - -import com.google.common.base.Function; -import com.google.common.base.Splitter; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.primitives.Ints; -import com.mpush.tools.config.data.DnsMapping; -import com.mpush.tools.log.Logs; -import org.aeonbits.owner.Converter; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public class DnsMappingConverter implements Converter>> { - - @Override - public Map> convert(Method method, String input) { - Logs.Console.error("method:" + method.getName() + ", input:" + input); - - Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); - Map> result = Maps.newConcurrentMap(); - for (Map.Entry entry : map.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - List dnsMappings = Lists.transform(Arrays.asList(value.split(",")), new Function() { - @Override - public DnsMapping apply(String ipAndPort) { - if (ipAndPort.contains(":")) { - String[] temp = ipAndPort.split(":"); - return new DnsMapping(temp[0], Ints.tryParse(temp[1])); - } else { - return new DnsMapping(ipAndPort, 80); - } - } - }); - result.put(key, dnsMappings); - } - return result; - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java deleted file mode 100644 index a2df46ed..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/MapConverter.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.config.converter; - -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import com.mpush.tools.log.Logs; -import org.aeonbits.owner.Converter; - -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Map; - -/** - * Created by yxx on 2016/5/12. - * - * @author ohun@live.cn - */ -public class MapConverter implements Converter> { - - @Override - public Map convert(Method method, String input) { - Logs.Console.error("method:" + method.getName() + ", input:" + input); - if (Strings.isNullOrEmpty(input)) return Collections.emptyMap(); - return Splitter.on(',').withKeyValueSeparator(':').split(input); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java deleted file mode 100644 index b480a322..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/converter/RedisGroupConverter.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.config.converter; - -import com.mpush.tools.config.data.RedisGroup; -import com.mpush.tools.config.data.RedisServer; -import com.mpush.tools.log.Logs; -import org.aeonbits.owner.Converter; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -public class RedisGroupConverter implements Converter { - - @Override - public RedisGroup convert(Method method, String input) { - Logs.Console.error("method:" + method.getName() + ", input:" + input); - - List servers = new ArrayList<>(); - String[] chunks = input.split(","); - for (String chunk : chunks) { - String[] entry = chunk.split(":"); - String ip = entry[0].trim(); - String port = entry[1].trim(); - // 如果配置了redis密码(redis_group = 111.1.57.148:6379:ShineMoIpo)才设置密码 - // 否则密码为空,JedisPool可以兼容两种情况 - String password = null; - if (entry.length >= 3) { - password = entry[2].trim(); - } - servers.add(new RedisServer(ip, Integer.parseInt(port), password)); - } - - return new RedisGroup(servers); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java index 1460abe9..c9ad8e9a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java @@ -20,6 +20,7 @@ package com.mpush.tools.config.data; +import java.net.URL; import java.util.Objects; public class DnsMapping { @@ -49,6 +50,18 @@ public static DnsMapping parse(String addr) { } } + public String translate(URL uri) { + StringBuilder sb = new StringBuilder(128); + sb.append(uri.getProtocol()).append("://") + .append(ip) + .append(':') + .append(port) + .append(uri.getPath()); + String query = uri.getQuery(); + if (query != null) sb.append('?').append(query); + return sb.toString(); + } + @Override public String toString() { return ip + ":" + port; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java index 0e34dff7..451efdf6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java @@ -60,6 +60,25 @@ public void setPassword(String password) { this.password = password; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + RedisServer server = (RedisServer) o; + + if (port != server.port) return false; + return host.equals(server.host); + + } + + @Override + public int hashCode() { + int result = host.hashCode(); + result = 31 * result + port; + return result; + } + @Override public String toString() { return "RedisServer{" + diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index 6a3c4a39..80050e8f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -28,6 +28,7 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; +import java.util.Arrays; /** * Created by ohun on 2015/12/25. @@ -58,26 +59,38 @@ public static SecretKey getSecretKey(byte[] seed) throws Exception { public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); + return encrypt(data, zeroIv, key); + } + + public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + return decrypt(data, zeroIv, key); + } + + public static byte[] encrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); - cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); + cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { - LOGGER.error("encrypt ex, decryptKey=" + encryptKey, e); - throw new RuntimeException("AES encrypt ex", e); + LOGGER.error("AES encrypt ex, iv={}, key={}", + Arrays.toString(zeroIv.getIV()), + Arrays.toString(keySpec.getEncoded()), e); + throw new CryptoException("AES encrypt ex", e); } } - public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { - IvParameterSpec zeroIv = new IvParameterSpec(iv); - SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + public static byte[] decrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); - cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); + cipher.init(Cipher.DECRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { - LOGGER.error("decrypt ex, decryptKey=" + decryptKey, e); - throw new RuntimeException("AES decrypt ex", e); + LOGGER.error("AES decrypt ex, iv={}, key={}", + Arrays.toString(zeroIv.getIV()), + Arrays.toString(keySpec.getEncoded()), e); + throw new CryptoException("AES decrypt ex", e); } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java index 7bd6fbd6..370c72f4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java @@ -22,31 +22,11 @@ * * */ - -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - package com.mpush.tools.crypto; import java.io.FilterOutputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -60,53 +40,54 @@ * as specified in * RFC 4648 and * RFC 2045. - * + *

*

    *
  • Basic *

    Uses "The Base64 Alphabet" as specified in Table 1 of - * RFC 4648 and RFC 2045 for encoding and decoding operation. - * The encoder does not add any line feed (line separator) - * character. The decoder rejects data that contains characters - * outside the base64 alphabet.

  • - * + * RFC 4648 and RFC 2045 for encoding and decoding operation. + * The encoder does not add any line feed (line separator) + * character. The decoder rejects data that contains characters + * outside the base64 alphabet.

    + *

    *

  • URL and Filename safe *

    Uses the "URL and Filename safe Base64 Alphabet" as specified - * in Table 2 of RFC 4648 for encoding and decoding. The - * encoder does not add any line feed (line separator) character. - * The decoder rejects data that contains characters outside the - * base64 alphabet.

  • - * + * in Table 2 of RFC 4648 for encoding and decoding. The + * encoder does not add any line feed (line separator) character. + * The decoder rejects data that contains characters outside the + * base64 alphabet.

    + *

    *

  • MIME *

    Uses the "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045 for encoding and decoding operation. The encoded output - * must be represented in lines of no more than 76 characters each - * and uses a carriage return {@code '\r'} followed immediately by - * a linefeed {@code '\n'} as the line separator. No line separator - * is added to the end of the encoded output. All line separators - * or other characters not found in the base64 alphabet table are - * ignored in decoding operation.

  • + * RFC 2045 for encoding and decoding operation. The encoded output + * must be represented in lines of no more than 76 characters each + * and uses a carriage return {@code '\r'} followed immediately by + * a linefeed {@code '\n'} as the line separator. No line separator + * is added to the end of the encoded output. All line separators + * or other characters not found in the base64 alphabet table are + * ignored in decoding operation.

    *
- * + *

*

Unless otherwise noted, passing a {@code null} argument to a * method of this class will cause a {@link java.lang.NullPointerException * NullPointerException} to be thrown. * - * @author Xueming Shen - * @since 1.8 + * @author Xueming Shen + * @since 1.8 */ public class Base64 { - private Base64() {} + private Base64() { + } /** * Returns a {@link Encoder} that encodes using the * Basic type base64 encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getEncoder() { - return Encoder.RFC4648; + return Encoder.RFC4648; } /** @@ -114,17 +95,17 @@ public static Encoder getEncoder() { * URL and Filename safe type base64 * encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getUrlEncoder() { - return Encoder.RFC4648_URLSAFE; + return Encoder.RFC4648_URLSAFE; } /** * Returns a {@link Encoder} that encodes using the * MIME type base64 encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getMimeEncoder() { return Encoder.RFC2045; @@ -135,41 +116,37 @@ public static Encoder getMimeEncoder() { * MIME type base64 encoding scheme * with specified line length and line separators. * - * @param lineLength - * the length of each output line (rounded down to nearest multiple - * of 4). If {@code lineLength <= 0} the output will not be separated - * in lines - * @param lineSeparator - * the line separator for each output line - * - * @return A Base64 encoder. - * - * @throws IllegalArgumentException if {@code lineSeparator} includes any - * character of "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045. + * @param lineLength the length of each output line (rounded down to nearest multiple + * of 4). If {@code lineLength <= 0} the output will not be separated + * in lines + * @param lineSeparator the line separator for each output line + * @return A Base64 encoder. + * @throws IllegalArgumentException if {@code lineSeparator} includes any + * character of "The Base64 Alphabet" as specified in Table 1 of + * RFC 2045. */ public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) { - Objects.requireNonNull(lineSeparator); - int[] base64 = Decoder.fromBase64; - for (byte b : lineSeparator) { - if (base64[b & 0xff] != -1) - throw new IllegalArgumentException( - "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); - } - if (lineLength <= 0) { - return Encoder.RFC4648; - } - return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); + Objects.requireNonNull(lineSeparator); + int[] base64 = Decoder.fromBase64; + for (byte b : lineSeparator) { + if (base64[b & 0xff] != -1) + throw new IllegalArgumentException( + "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); + } + if (lineLength <= 0) { + return Encoder.RFC4648; + } + return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); } /** * Returns a {@link Decoder} that decodes using the * Basic type base64 encoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getDecoder() { - return Decoder.RFC4648; + return Decoder.RFC4648; } /** @@ -177,36 +154,36 @@ public static Decoder getDecoder() { * URL and Filename safe type base64 * encoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getUrlDecoder() { - return Decoder.RFC4648_URLSAFE; + return Decoder.RFC4648_URLSAFE; } /** * Returns a {@link Decoder} that decodes using the * MIME type base64 decoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getMimeDecoder() { - return Decoder.RFC2045; + return Decoder.RFC2045; } /** * This class implements an encoder for encoding byte data using * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - * + *

*

Instances of {@link Encoder} class are safe for use by * multiple concurrent threads. - * + *

*

Unless otherwise noted, passing a {@code null} argument to * a method of this class will cause a * {@link java.lang.NullPointerException NullPointerException} to * be thrown. * - * @see Decoder - * @since 1.8 + * @see Decoder + * @since 1.8 */ public static class Encoder { @@ -228,11 +205,11 @@ private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648). */ private static final char[] toBase64 = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; /** @@ -241,15 +218,15 @@ private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { * '_'. This table is used when BASE64_URL is specified. */ private static final char[] toBase64URL = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' }; private static final int MIMELINEMAX = 76; - private static final byte[] CRLF = new byte[] {'\r', '\n'}; + private static final byte[] CRLF = new byte[]{'\r', '\n'}; static final Encoder RFC4648 = new Encoder(false, null, -1, true); static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true); @@ -273,17 +250,16 @@ private final int outLength(int srclen) { * byte array using the {@link Base64} encoding scheme. The returned byte * array is of the length of the resulting bytes. * - * @param src - * the byte array to encode - * @return A newly-allocated byte array containing the resulting - * encoded bytes. + * @param src the byte array to encode + * @return A newly-allocated byte array containing the resulting + * encoded bytes. */ public byte[] encode(byte[] src) { int len = outLength(src.length); // dst array size byte[] dst = new byte[len]; int ret = encode0(src, 0, src.length, dst); if (ret != dst.length) - return Arrays.copyOf(dst, ret); + return Arrays.copyOf(dst, ret); return dst; } @@ -291,45 +267,41 @@ public byte[] encode(byte[] src) { * Encodes all bytes from the specified byte array using the * {@link Base64} encoding scheme, writing the resulting bytes to the * given output byte array, starting at offset 0. - * + *

*

It is the responsibility of the invoker of this method to make * sure the output byte array {@code dst} has enough space for encoding * all bytes from the input byte array. No bytes will be written to the * output byte array if the output byte array is not big enough. * - * @param src - * the byte array to encode - * @param dst - * the output byte array - * @return The number of bytes written to the output byte array - * - * @throws IllegalArgumentException if {@code dst} does not have enough - * space for encoding all input bytes. + * @param src the byte array to encode + * @param dst the output byte array + * @return The number of bytes written to the output byte array + * @throws IllegalArgumentException if {@code dst} does not have enough + * space for encoding all input bytes. */ public int encode(byte[] src, byte[] dst) { int len = outLength(src.length); // dst array size if (dst.length < len) throw new IllegalArgumentException( - "Output byte array is too small for encoding all input bytes"); + "Output byte array is too small for encoding all input bytes"); return encode0(src, 0, src.length, dst); } /** * Encodes the specified byte array into a String using the {@link Base64} * encoding scheme. - * + *

*

This method first encodes all input bytes into a base64 encoded * byte array and then constructs a new String by using the encoded byte * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1 * ISO-8859-1} charset. - * + *

*

In other words, an invocation of this method has exactly the same * effect as invoking * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}. * - * @param src - * the byte array to encode - * @return A String containing the resulting Base64 encoded characters + * @param src the byte array to encode + * @return A String containing the resulting Base64 encoded characters */ @SuppressWarnings("deprecation") public String encodeToString(byte[] src) { @@ -341,15 +313,14 @@ public String encodeToString(byte[] src) { * Encodes all remaining bytes from the specified byte buffer into * a newly-allocated ByteBuffer using the {@link Base64} encoding * scheme. - * + *

* Upon return, the source buffer's position will be updated to * its limit; its limit will not have been changed. The returned * output buffer's position will be zero and its limit will be the * number of resulting encoded bytes. * - * @param buffer - * the source ByteBuffer to encode - * @return A newly-allocated byte buffer containing the encoded bytes. + * @param buffer the source ByteBuffer to encode + * @return A newly-allocated byte buffer containing the encoded bytes. */ public ByteBuffer encode(ByteBuffer buffer) { int len = outLength(buffer.remaining()); @@ -357,9 +328,9 @@ public ByteBuffer encode(ByteBuffer buffer) { int ret = 0; if (buffer.hasArray()) { ret = encode0(buffer.array(), - buffer.arrayOffset() + buffer.position(), - buffer.arrayOffset() + buffer.limit(), - dst); + buffer.arrayOffset() + buffer.position(), + buffer.arrayOffset() + buffer.limit(), + dst); buffer.position(buffer.limit()); } else { byte[] src = new byte[buffer.remaining()]; @@ -367,41 +338,40 @@ public ByteBuffer encode(ByteBuffer buffer) { ret = encode0(src, 0, src.length, dst); } if (ret != dst.length) - dst = Arrays.copyOf(dst, ret); + dst = Arrays.copyOf(dst, ret); return ByteBuffer.wrap(dst); } /** * Wraps an output stream for encoding byte data using the {@link Base64} * encoding scheme. - * + *

*

It is recommended to promptly close the returned output stream after * use, during which it will flush all possible leftover bytes to the underlying * output stream. Closing the returned output stream will close the underlying * output stream. * - * @param os - * the output stream. - * @return the output stream for encoding the byte data into the - * specified Base64 encoded format + * @param os the output stream. + * @return the output stream for encoding the byte data into the + * specified Base64 encoded format */ public OutputStream wrap(OutputStream os) { Objects.requireNonNull(os); return new EncOutputStream(os, isURL ? toBase64URL : toBase64, - newline, linemax, doPadding); + newline, linemax, doPadding); } /** * Returns an encoder instance that encodes equivalently to this one, * but without adding any padding character at the end of the encoded * byte data. - * + *

*

The encoding scheme of this encoder instance is unaffected by * this invocation. The returned encoder instance should be used for * non-padding encoding operation. * * @return an equivalent encoder that encodes without adding any - * padding character at the end + * padding character at the end */ public Encoder withoutPadding() { if (!doPadding) @@ -414,42 +384,42 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { int sp = off; int slen = (end - off) / 3 * 3; int sl = off + slen; - if (linemax > 0 && slen > linemax / 4 * 3) + if (linemax > 0 && slen > linemax / 4 * 3) slen = linemax / 4 * 3; int dp = 0; while (sp < sl) { int sl0 = Math.min(sp + slen, sl); - for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) { + for (int sp0 = sp, dp0 = dp; sp0 < sl0; ) { int bits = (src[sp0++] & 0xff) << 16 | - (src[sp0++] & 0xff) << 8 | - (src[sp0++] & 0xff); - dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f]; - dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f]; - dst[dp0++] = (byte)base64[(bits >>> 6) & 0x3f]; - dst[dp0++] = (byte)base64[bits & 0x3f]; + (src[sp0++] & 0xff) << 8 | + (src[sp0++] & 0xff); + dst[dp0++] = (byte) base64[(bits >>> 18) & 0x3f]; + dst[dp0++] = (byte) base64[(bits >>> 12) & 0x3f]; + dst[dp0++] = (byte) base64[(bits >>> 6) & 0x3f]; + dst[dp0++] = (byte) base64[bits & 0x3f]; } int dlen = (sl0 - sp) / 3 * 4; dp += dlen; sp = sl0; if (dlen == linemax && sp < end) { - for (byte b : newline){ + for (byte b : newline) { dst[dp++] = b; } } } if (sp < end) { // 1 or 2 leftover bytes int b0 = src[sp++] & 0xff; - dst[dp++] = (byte)base64[b0 >> 2]; + dst[dp++] = (byte) base64[b0 >> 2]; if (sp == end) { - dst[dp++] = (byte)base64[(b0 << 4) & 0x3f]; + dst[dp++] = (byte) base64[(b0 << 4) & 0x3f]; if (doPadding) { dst[dp++] = '='; dst[dp++] = '='; } } else { int b1 = src[sp++] & 0xff; - dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; - dst[dp++] = (byte)base64[(b1 << 2) & 0x3f]; + dst[dp++] = (byte) base64[(b0 << 4) & 0x3f | (b1 >> 4)]; + dst[dp++] = (byte) base64[(b1 << 2) & 0x3f]; if (doPadding) { dst[dp++] = '='; } @@ -462,7 +432,7 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { /** * This class implements a decoder for decoding byte data using the * Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - * + *

*

The Base64 padding character {@code '='} is accepted and * interpreted as the end of the encoded byte data, but is not * required. So if the final unit of the encoded byte data only has @@ -473,17 +443,17 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { * present, otherwise {@code IllegalArgumentException} ( * {@code IOException} when reading from a Base64 stream) is thrown * during decoding. - * + *

*

Instances of {@link Decoder} class are safe for use by * multiple concurrent threads. - * + *

*

Unless otherwise noted, passing a {@code null} argument to * a method of this class will cause a * {@link java.lang.NullPointerException NullPointerException} to * be thrown. * - * @see Encoder - * @since 1.8 + * @see Encoder + * @since 1.8 */ public static class Decoder { @@ -501,9 +471,9 @@ private Decoder(boolean isURL, boolean isMIME) { * their 6-bit positive integer equivalents. Characters that * are not in the Base64 alphabet but fall within the bounds of * the array are encoded to -1. - * */ private static final int[] fromBase64 = new int[256]; + static { Arrays.fill(fromBase64, -1); for (int i = 0; i < Encoder.toBase64.length; i++) @@ -524,9 +494,9 @@ private Decoder(boolean isURL, boolean isMIME) { fromBase64URL['='] = -2; } - static final Decoder RFC4648 = new Decoder(false, false); + static final Decoder RFC4648 = new Decoder(false, false); static final Decoder RFC4648_URLSAFE = new Decoder(true, false); - static final Decoder RFC2045 = new Decoder(false, true); + static final Decoder RFC2045 = new Decoder(false, true); /** * Decodes all bytes from the input byte array using the {@link Base64} @@ -534,13 +504,9 @@ private Decoder(boolean isURL, boolean isMIME) { * byte array. The returned byte array is of the length of the resulting * bytes. * - * @param src - * the byte array to decode - * - * @return A newly-allocated byte array containing the decoded bytes. - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme + * @param src the byte array to decode + * @return A newly-allocated byte array containing the decoded bytes. + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme */ public byte[] decode(byte[] src) { byte[] dst = new byte[outLength(src, 0, src.length)]; @@ -554,17 +520,13 @@ public byte[] decode(byte[] src) { /** * Decodes a Base64 encoded String into a newly-allocated byte array * using the {@link Base64} encoding scheme. - * + *

*

An invocation of this method has exactly the same effect as invoking * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))} * - * @param src - * the string to decode - * - * @return A newly-allocated byte array containing the decoded bytes. - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme + * @param src the string to decode + * @return A newly-allocated byte array containing the decoded bytes. + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme */ public byte[] decode(String src) { return decode(src.getBytes(StandardCharsets.ISO_8859_1)); @@ -574,55 +536,46 @@ public byte[] decode(String src) { * Decodes all bytes from the input byte array using the {@link Base64} * encoding scheme, writing the results into the given output byte array, * starting at offset 0. - * + *

*

It is the responsibility of the invoker of this method to make * sure the output byte array {@code dst} has enough space for decoding * all bytes from the input byte array. No bytes will be be written to * the output byte array if the output byte array is not big enough. - * + *

*

If the input byte array is not in valid Base64 encoding scheme * then some bytes may have been written to the output byte array before * IllegalargumentException is thrown. * - * @param src - * the byte array to decode - * @param dst - * the output byte array - * - * @return The number of bytes written to the output byte array - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme, or {@code dst} - * does not have enough space for decoding all input bytes. + * @param src the byte array to decode + * @param dst the output byte array + * @return The number of bytes written to the output byte array + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme, or {@code dst} + * does not have enough space for decoding all input bytes. */ public int decode(byte[] src, byte[] dst) { int len = outLength(src, 0, src.length); if (dst.length < len) throw new IllegalArgumentException( - "Output byte array is too small for decoding all input bytes"); + "Output byte array is too small for decoding all input bytes"); return decode0(src, 0, src.length, dst); } /** * Decodes all bytes from the input byte buffer using the {@link Base64} * encoding scheme, writing the results into a newly-allocated ByteBuffer. - * + *

*

Upon return, the source buffer's position will be updated to * its limit; its limit will not have been changed. The returned * output buffer's position will be zero and its limit will be the * number of resulting decoded bytes - * + *

*

{@code IllegalArgumentException} is thrown if the input buffer * is not in valid Base64 encoding scheme. The position of the input * buffer will not be advanced in this case. * - * @param buffer - * the ByteBuffer to decode - * - * @return A newly-allocated byte buffer containing the decoded bytes - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme. + * @param buffer the ByteBuffer to decode + * @return A newly-allocated byte buffer containing the decoded bytes + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme. */ public ByteBuffer decode(ByteBuffer buffer) { int pos0 = buffer.position(); @@ -650,18 +603,16 @@ public ByteBuffer decode(ByteBuffer buffer) { /** * Returns an input stream for decoding {@link Base64} encoded byte stream. - * + *

*

The {@code read} methods of the returned {@code InputStream} will * throw {@code IOException} when reading bytes that cannot be decoded. - * + *

*

Closing the returned input stream will close the underlying * input stream. * - * @param is - * the input stream - * - * @return the input stream for decoding the specified Base64 encoded - * byte stream + * @param is the input stream + * @return the input stream for decoding the specified Base64 encoded + * byte stream */ public InputStream wrap(InputStream is) { Objects.requireNonNull(is); @@ -678,7 +629,7 @@ private int outLength(byte[] src, int sp, int sl) { if (isMIME && base64[0] == -1) return 0; throw new IllegalArgumentException( - "Input byte[] should at least have 2 bytes for base64 bytes"); + "Input byte[] should at least have 2 bytes for base64 bytes"); } if (isMIME) { // scan all bytes to fill out all non-alphabet. a performance @@ -701,7 +652,7 @@ private int outLength(byte[] src, int sp, int sl) { paddings++; } } - if (paddings == 0 && (len & 0x3) != 0) + if (paddings == 0 && (len & 0x3) != 0) paddings = 4 - (len & 0x3); return 3 * ((len + 3) / 4) - paddings; } @@ -721,9 +672,9 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { // xx= shiftto==6&&sp==sl missing last = // xx=y shiftto==6 last is not = if (shiftto == 6 && (sp == sl || src[sp++] != '=') || - shiftto == 18) { + shiftto == 18) { throw new IllegalArgumentException( - "Input byte array has wrong 4-byte ending unit"); + "Input byte array has wrong 4-byte ending unit"); } break; } @@ -731,29 +682,29 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { continue; else throw new IllegalArgumentException( - "Illegal base64 character " + - Integer.toString(src[sp - 1], 16)); + "Illegal base64 character " + + Integer.toString(src[sp - 1], 16)); } bits |= (b << shiftto); shiftto -= 6; if (shiftto < 0) { - dst[dp++] = (byte)(bits >> 16); - dst[dp++] = (byte)(bits >> 8); - dst[dp++] = (byte)(bits); + dst[dp++] = (byte) (bits >> 16); + dst[dp++] = (byte) (bits >> 8); + dst[dp++] = (byte) (bits); shiftto = 18; bits = 0; } } // reached end of byte array or hit padding '=' characters. if (shiftto == 6) { - dst[dp++] = (byte)(bits >> 16); + dst[dp++] = (byte) (bits >> 16); } else if (shiftto == 0) { - dst[dp++] = (byte)(bits >> 16); - dst[dp++] = (byte)(bits >> 8); + dst[dp++] = (byte) (bits >> 16); + dst[dp++] = (byte) (bits >> 8); } else if (shiftto == 12) { // dangling single "x", incorrectly encoded. throw new IllegalArgumentException( - "Last unit does not have enough valid bits"); + "Last unit does not have enough valid bits"); } // anything left is invalid, if is not MIME. // if MIME, ignore all non-base64 character @@ -761,7 +712,7 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { if (isMIME && base64[src[sp++]] < 0) continue; throw new IllegalArgumentException( - "Input byte array has incorrect ending byte at " + sp); + "Input byte array has incorrect ending byte at " + sp); } return dp; } @@ -794,7 +745,7 @@ private static class EncOutputStream extends FilterOutputStream { @Override public void write(int b) throws IOException { byte[] buf = new byte[1]; - buf[0] = (byte)(b & 0xff); + buf[0] = (byte) (b & 0xff); write(buf, 0, 1); } @@ -836,14 +787,14 @@ public void write(byte[] b, int off, int len) throws IOException { while (nBits24-- > 0) { checkNewline(); int bits = (b[off++] & 0xff) << 16 | - (b[off++] & 0xff) << 8 | - (b[off++] & 0xff); + (b[off++] & 0xff) << 8 | + (b[off++] & 0xff); out.write(base64[(bits >>> 18) & 0x3f]); out.write(base64[(bits >>> 12) & 0x3f]); - out.write(base64[(bits >>> 6) & 0x3f]); + out.write(base64[(bits >>> 6) & 0x3f]); out.write(base64[bits & 0x3f]); linepos += 4; - } + } if (leftover == 1) { b0 = b[off++] & 0xff; } else if (leftover == 2) { @@ -870,7 +821,7 @@ public void close() throws IOException { out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); out.write(base64[(b1 << 2) & 0x3f]); if (doPadding) { - out.write('='); + out.write('='); } } leftover = 0; @@ -889,9 +840,9 @@ private static class DecInputStream extends InputStream { private final int[] base64; // base64 -> byte mapping private int bits = 0; // 24-bit buffer for decoding private int nextin = 18; // next available "off" in "bits" for input; - // -> 18, 12, 6, 0 + // -> 18, 12, 6, 0 private int nextout = -8; // next available "off" in "bits" for output; - // -> 8, 0, -8 (no byte for output) + // -> 8, 0, -8 (no byte for output) private boolean eof = false; private boolean closed = false; @@ -921,7 +872,7 @@ public int read(byte[] b, int off, int len) throws IOException { do { if (len == 0) return off - oldOff; - b[off++] = (byte)(bits >> nextout); + b[off++] = (byte) (bits >> nextout); len--; nextout -= 8; } while (nextout >= 0); @@ -936,14 +887,14 @@ public int read(byte[] b, int off, int len) throws IOException { throw new IOException("Base64 stream has one un-decoded dangling byte."); // treat ending xx/xxx without padding character legal. // same logic as v == '=' below - b[off++] = (byte)(bits >> (16)); + b[off++] = (byte) (bits >> (16)); len--; if (nextin == 0) { // only one padding byte if (len == 0) { // no enough output space bits >>= 8; // shift to lowest byte nextout = 0; } else { - b[off++] = (byte) (bits >> 8); + b[off++] = (byte) (bits >> 8); } } } @@ -958,17 +909,17 @@ public int read(byte[] b, int off, int len) throws IOException { // xx= shiftto==6 && missing last '=' // xx=y or last is not '=' if (nextin == 18 || nextin == 12 || - nextin == 6 && is.read() != '=') { + nextin == 6 && is.read() != '=') { throw new IOException("Illegal base64 ending sequence:" + nextin); } - b[off++] = (byte)(bits >> (16)); + b[off++] = (byte) (bits >> (16)); len--; if (nextin == 0) { // only one padding byte if (len == 0) { // no enough output space bits >>= 8; // shift to lowest byte nextout = 0; } else { - b[off++] = (byte) (bits >> 8); + b[off++] = (byte) (bits >> 8); } } eof = true; @@ -979,14 +930,14 @@ public int read(byte[] b, int off, int len) throws IOException { continue; else throw new IOException("Illegal base64 character " + - Integer.toString(v, 16)); + Integer.toString(v, 16)); } bits |= (v << nextin); if (nextin == 0) { nextin = 18; // clear for next nextout = 16; while (nextout >= 0) { - b[off++] = (byte)(bits >> nextout); + b[off++] = (byte) (bits >> nextout); len--; nextout -= 8; if (len == 0 && nextout >= 0) { // don't clean "bits" diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index 2f6ade10..3c2f1393 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -20,26 +20,10 @@ package com.mpush.tools.crypto; - import com.mpush.api.Constants; -import java.io.*; - -/** - *

- * BASE64编码解码工具包 - *

- *

- * 依赖javabase64-1.3.1.jar - *

- */ public class Base64Utils { - /** - * 文件读取缓冲区大小 - */ - private static final int CACHE_SIZE = 1024; - /** *

* BASE64字符串解码为二进制数据 @@ -50,7 +34,7 @@ public class Base64Utils { * @throws Exception */ public static byte[] decode(String base64) throws Exception { - return Base64.getDecoder().decode(base64); + return Base64.getDecoder().decode(base64.getBytes(Constants.UTF_8)); } /** @@ -66,90 +50,4 @@ public static String encode(byte[] bytes) throws Exception { return new String(Base64.getEncoder().encode(bytes), Constants.UTF_8); } - /** - *

- * 将文件编码为BASE64字符串 - *

- *

- * 大文件慎用,可能会导致内存溢出 - *

- * - * @param filePath 文件绝对路径 - * @return - * @throws Exception - */ - public static String encodeFile(String filePath) throws Exception { - byte[] bytes = fileToByte(filePath); - return encode(bytes); - } - - /** - *

- * BASE64字符串转回文件 - *

- * - * @param filePath 文件绝对路径 - * @param base64 编码字符串 - * @throws Exception - */ - public static void decodeToFile(String filePath, String base64) throws Exception { - byte[] bytes = decode(base64); - byteArrayToFile(bytes, filePath); - } - - /** - *

- * 文件转换为二进制数组 - *

- * - * @param filePath 文件路径 - * @return - * @throws Exception - */ - public static byte[] fileToByte(String filePath) throws Exception { - byte[] data = new byte[0]; - File file = new File(filePath); - if (file.exists()) { - FileInputStream in = new FileInputStream(file); - ByteArrayOutputStream out = new ByteArrayOutputStream(2048); - byte[] cache = new byte[CACHE_SIZE]; - int nRead = 0; - while ((nRead = in.read(cache)) != -1) { - out.write(cache, 0, nRead); - out.flush(); - } - out.close(); - in.close(); - data = out.toByteArray(); - } - return data; - } - - /** - *

- * 二进制数据写文件 - *

- * - * @param bytes 二进制数据 - * @param filePath 文件生成目录 - */ - public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { - InputStream in = new ByteArrayInputStream(bytes); - File destFile = new File(filePath); - if (!destFile.getParentFile().exists()) { - destFile.getParentFile().mkdirs(); - } - destFile.createNewFile(); - OutputStream out = new FileOutputStream(destFile); - byte[] cache = new byte[CACHE_SIZE]; - int nRead = 0; - while ((nRead = in.read(cache)) != -1) { - out.write(cache, 0, nRead); - out.flush(); - } - out.close(); - in.close(); - } - - } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java similarity index 87% rename from mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java rename to mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java index 6f59c33d..b7a39da8 100644 --- a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.api.exception; +package com.mpush.tools.crypto; /** * Created by ohun on 2015/12/23. @@ -32,4 +32,7 @@ public CryptoException(String message) { super(message); } + public CryptoException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index f483310d..536f9ba8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -20,8 +20,8 @@ package com.mpush.tools.crypto; import com.mpush.api.Constants; -import com.mpush.tools.IOUtils; -import com.mpush.tools.Strings; +import com.mpush.tools.common.IOUtils; +import com.mpush.tools.common.Strings; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index 99825f2a..b1edd6db 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -20,7 +20,7 @@ package com.mpush.tools.crypto; import com.mpush.api.Constants; -import com.mpush.tools.Pair; +import com.mpush.tools.common.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -188,7 +188,7 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { LOGGER.error("getPublicKey ex modulus={}, exponent={}", modulus, exponent, e); - throw new RuntimeException("Get PublicKey ex", e); + throw new CryptoException("Get PublicKey ex", e); } } @@ -211,7 +211,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { LOGGER.error("getPrivateKey ex modulus={}, exponent={}", modulus, exponent, e); - throw new RuntimeException("Get PrivateKey ex", e); + throw new CryptoException("Get PrivateKey ex", e); } } @@ -234,7 +234,7 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { return doFinal(cipher, data, key_len - 11); } catch (Exception e) { LOGGER.error("encryptByPublicKey ex", e); - throw new RuntimeException("RSA encrypt ex", e); + throw new CryptoException("RSA encrypt ex", e); } } @@ -256,7 +256,7 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) return doFinal(cipher, data, key_len); } catch (Exception e) { LOGGER.error("decryptByPrivateKey ex", e); - throw new RuntimeException("RSA decrypt ex", e); + throw new CryptoException("RSA decrypt ex", e); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index 71e004bd..f547073d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -1,4 +1,3 @@ - /* * (C) Copyright 2015-2016 the original author or authors. * @@ -23,6 +22,7 @@ import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; +import com.mpush.api.event.Event; import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,7 +49,7 @@ public void handleException(Throwable exception, SubscriberExceptionContext cont }); } - public void post(Object event) { + public void post(Event event) { eventBus.post(event); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java index 5f7d6c75..c7e7ccab 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java @@ -21,8 +21,8 @@ public abstract class EventConsumer { - public EventConsumer() { - EventBus.I.register(this); - } - + public EventConsumer() { + EventBus.I.register(this); + } + } diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index 5d06ce7a..44fb5e5e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -30,17 +30,15 @@ * @author ohun@live.cn */ public interface Logs { - boolean logInited = init(); + boolean logInit = init(); static boolean init() { - if (logInited) return true; + if (logInit) return true; System.setProperty("log.home", CC.mp.log_dir); System.setProperty("log.root.level", CC.mp.log_level); LoggerFactory .getLogger("console") - .info( - CC.mp.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true)) - ); + .info(CC.mp.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true))); return true; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index f898f8b8..a1d49ad6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -26,26 +26,28 @@ public final class ThreadNames { /** * netty boss 线程 */ - public static final String NETTY_BOSS = NS + "-boss-"; + public static final String T_SERVER_BOSS = NS + "-boss-"; /** * netty worker 线程 */ - public static final String NETTY_WORKER = NS + "-worker-"; + public static final String T_SERVER_WORKER = NS + "-worker-"; - public static final String NETTY_HTTP = NS + "-http-"; + public static final String T_HTTP_CLIENT = NS + "-http-"; - public static final String EVENT_BUS = NS + "-event-"; + public static final String T_EVENT_BUS = NS + "-event-"; - public static final String REDIS = NS + "-redis-"; + public static final String T_MQ = NS + "-mq-"; - public static final String ZK = NS + "-zk-"; + public static final String T_ZK = NS + "-zk-"; - public static final String BIZ = NS + "-biz-"; + public static final String T_BIZ = NS + "-biz-"; + public static final String T_PUSH_CALLBACK = NS + "-push-cb-"; + public static final String T_PUSH_REQ_TIMER = NS + "-push-timer-"; /** * connection 定期检测线程 */ - public static final String NETTY_TIMER = NS + "-timer-"; + public static final String T_NETTY_TIMER = NS + "-timer-"; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java new file mode 100644 index 00000000..aad039ea --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java @@ -0,0 +1,34 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.thread.pool; + +import java.util.concurrent.*; + +/** + * Created by yxx on 2016/5/29. + * + * @author ohun@live.cn (夜色) + */ +public class DefaultExecutor extends ThreadPoolExecutor { + + public DefaultExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java index bd7a5ebf..ef83ac45 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java @@ -23,7 +23,10 @@ import com.mpush.tools.config.CC; import com.mpush.tools.thread.PoolThreadFactory; -import java.util.concurrent.*; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; import static com.mpush.tools.thread.ThreadNames.*; @@ -41,7 +44,7 @@ private Executor get(ThreadPoolConfig config) { BlockingQueue queue = config.getQueue(); ThreadFactory threadFactory = new PoolThreadFactory(name); - return new ThreadPoolExecutor(corePoolSize + return new DefaultExecutor(corePoolSize , maxPoolSize , keepAliveSeconds , TimeUnit.SECONDS @@ -56,7 +59,7 @@ public Executor get(String name) { switch (name) { case SERVER_BOSS: config = ThreadPoolConfig - .build(NETTY_BOSS) + .build(T_SERVER_BOSS) .setCorePoolSize(CC.mp.thread.pool.boss.min) .setMaxPoolSize(CC.mp.thread.pool.boss.max) .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) @@ -64,7 +67,7 @@ public Executor get(String name) { break; case SERVER_WORK: config = ThreadPoolConfig - .build(NETTY_WORKER) + .build(T_SERVER_WORKER) .setCorePoolSize(CC.mp.thread.pool.work.min) .setMaxPoolSize(CC.mp.thread.pool.work.max) .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) @@ -72,29 +75,39 @@ public Executor get(String name) { break; case HTTP_CLIENT_WORK: config = ThreadPoolConfig - .buildFixed(NETTY_HTTP, + .buildFixed(T_HTTP_CLIENT, CC.mp.thread.pool.http_proxy.min, CC.mp.thread.pool.http_proxy.queue_size - ); + ) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_DISCARD); break; case EVENT_BUS: config = ThreadPoolConfig - .buildFixed(EVENT_BUS, + .buildFixed(T_EVENT_BUS, CC.mp.thread.pool.event_bus.min, CC.mp.thread.pool.event_bus.queue_size ); break; case MQ: config = ThreadPoolConfig - .buildFixed(MQ, + .buildFixed(T_MQ, CC.mp.thread.pool.mq.min, CC.mp.thread.pool.mq.queue_size ); break; + case PUSH_CALLBACK: + config = ThreadPoolConfig + .build(T_PUSH_CALLBACK) + .setCorePoolSize(CC.mp.thread.pool.push_callback.min) + .setMaxPoolSize(CC.mp.thread.pool.push_callback.max) + .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) + .setQueueCapacity(CC.mp.thread.pool.push_callback.queue_size) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); + break; default: case BIZ: config = ThreadPoolConfig - .build(BIZ) + .build(T_BIZ) .setCorePoolSize(CC.mp.thread.pool.biz.min) .setMaxPoolSize(CC.mp.thread.pool.biz.max) .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java index 9a645d78..f5d5165d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -19,7 +19,8 @@ package com.mpush.tools.thread.pool; -import com.mpush.tools.JVMUtil; +import com.mpush.tools.common.JVMUtil; +import com.mpush.tools.config.CC; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,32 +28,46 @@ import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; +import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_ABORT; +import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS; + public class DumpThreadRejectedHandler implements RejectedExecutionHandler { private final static Logger LOGGER = LoggerFactory.getLogger(DumpThreadRejectedHandler.class); private volatile boolean dumping = false; - private static final String preFixPath = "/tmp/mpush/logs/dump/"; + private static final String DUMP_DIR = CC.mp.monitor.dump_dir; + + private final ThreadPoolConfig poolConfig; - private final ThreadPoolConfig context; + private final int rejectedPolicy; - public DumpThreadRejectedHandler(ThreadPoolConfig context) { - this.context = context; + public DumpThreadRejectedHandler(ThreadPoolConfig poolConfig) { + this.poolConfig = poolConfig; + this.rejectedPolicy = poolConfig.getRejectedPolicy(); } @Override - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { + LOGGER.warn("one task rejected, poolConfig={}, poolInfo={}", poolConfig, ThreadPoolManager.getPoolInfo(e)); if (!dumping) { dumping = true; dumpJVMInfo(); } - throw new RejectedExecutionException(); + + if (rejectedPolicy == REJECTED_POLICY_ABORT) { + throw new RejectedExecutionException("one task rejected, pool=" + poolConfig.getName()); + } else if (rejectedPolicy == REJECTED_POLICY_CALLER_RUNS) { + if (!e.isShutdown()) { + r.run(); + } + } } private void dumpJVMInfo() { LOGGER.error("start dump jvm info"); - JVMUtil.dumpJstack(preFixPath + "/" + context.getName()); + JVMUtil.dumpJstack(DUMP_DIR + "/" + poolConfig.getName()); LOGGER.error("end dump jvm info"); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java index 9297510d..d82cf1a0 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -24,11 +24,15 @@ import java.util.concurrent.SynchronousQueue; public class ThreadPoolConfig { + public static final int REJECTED_POLICY_ABORT = 0; + public static final int REJECTED_POLICY_DISCARD = 1; + public static final int REJECTED_POLICY_CALLER_RUNS = 2; private String name;//名字 private int corePoolSize; //最小线程大小 private int maxPoolSize; //最大线程大小 private int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) private int keepAliveSeconds;// 存活时间 + private int rejectedPolicy = REJECTED_POLICY_ABORT; public ThreadPoolConfig(String name) { this.name = name; @@ -79,6 +83,14 @@ public ThreadPoolConfig setKeepAliveSeconds(long keepAliveSeconds) { return this; } + public int getRejectedPolicy() { + return rejectedPolicy; + } + + public ThreadPoolConfig setRejectedPolicy(int rejectedPolicy) { + this.rejectedPolicy = rejectedPolicy; + return this; + } public static ThreadPoolConfig buildFixed(String name, int threads, int queueCapacity) { return new ThreadPoolConfig(name) diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 12063cde..900237c4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; public class ThreadPoolManager { public static final ThreadPoolManager I = new ThreadPoolManager(); @@ -39,6 +40,7 @@ public class ThreadPoolManager { private Executor eventBusExecutor; private Executor redisExecutor; private Executor httpExecutor; + private Executor pushCallbackExecutor; public final Thread newThread(String name, Runnable target) { return threadFactory.newThread(name, target); @@ -98,6 +100,15 @@ public Executor getBossExecutor() { return bossExecutor; } + public Executor getPushCallbackExecutor() { + if (pushCallbackExecutor == null) { + synchronized (this) { + pushCallbackExecutor = threadPoolFactory.get(ThreadPoolFactory.PUSH_CALLBACK); + } + } + return pushCallbackExecutor; + } + public Map getActivePools() { Map map = new HashMap<>(); if (bossExecutor != null) map.put("bossExecutor", bossExecutor); @@ -106,7 +117,17 @@ public Map getActivePools() { if (eventBusExecutor != null) map.put("eventBusExecutor", eventBusExecutor); if (redisExecutor != null) map.put("redisExecutor", redisExecutor); if (httpExecutor != null) map.put("httpExecutor", httpExecutor); + if (pushCallbackExecutor != null) map.put("pushCallbackExecutor", pushCallbackExecutor); return map; } + public static Map getPoolInfo(ThreadPoolExecutor executor) { + Map info = new HashMap<>(); + info.put("corePoolSize", executor.getCorePoolSize()); + info.put("maximumPoolSize", executor.getMaximumPoolSize()); + info.put("activeCount[workingThread]", executor.getActiveCount()); + info.put("poolSize[workThread]", executor.getPoolSize()); + info.put("queueSize[blockTask]", executor.getQueue().size()); + return info; + } } diff --git a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java index a74ea208..7e63de28 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java @@ -19,6 +19,7 @@ package com.mpush.tools; +import com.mpush.tools.common.IOUtils; import org.junit.Test; /** @@ -65,12 +66,12 @@ public void testCompress() throws Exception { for (int i = 0; i < 100; i++) { IOUtils.compress(s); } - System.out.println((System.currentTimeMillis()-t1)/100); + System.out.println((System.currentTimeMillis() - t1) / 100); System.out.println("src:" + s.length); byte[] out = IOUtils.compress(s); System.out.println("compress:" + out.length); - byte[] ss = IOUtils.uncompress(out); - System.out.println("uncompress:" + ss.length); + byte[] ss = IOUtils.decompress(out); + System.out.println("decompress:" + ss.length); } @Test diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java index a740cbe6..d723f7c6 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java @@ -19,8 +19,7 @@ package com.mpush.tools.crypto; -import com.mpush.tools.Pair; - +import com.mpush.tools.common.Pair; import org.junit.Before; import org.junit.Test; diff --git a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java deleted file mode 100644 index e926d19c..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.delayqueue; - -import java.util.Iterator; -import java.util.Random; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.DelayQueue; -import java.util.concurrent.Delayed; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * 考试时间为120分钟,30分钟后才可交卷,初始化考生完成试卷时间最小应为30分钟 - * 对于能够在120分钟内交卷的考生,如何实现这些考生交卷 - * 对于120分钟内没有完成考试的考生,在120分钟考试时间到后需要让他们强制交卷 - * 在所有的考生都交完卷后,需要将控制线程关闭 - * - * 实现思想:用DelayQueue存储考生(Student类),每一个考生都有自己的名字和完成试卷的时间, - * Teacher线程对DelayQueue进行监控,收取完成试卷小于120分钟的学生的试卷。 - * 当考试时间120分钟到时,先关闭Teacher线程,然后强制DelayQueue中还存在的考生交卷。 - * 每一个考生交卷都会进行一次countDownLatch.countDown(),当countDownLatch.await()不再阻塞说明所有考生都交完卷了,而后结束考试。 - * - */ - -public class Exam { - - public static void main(String[] args) throws InterruptedException { - int studentNumber = 20; - CountDownLatch countDownLatch = new CountDownLatch(studentNumber+1); - DelayQueue students = new DelayQueue(); - Random random = new Random(); - for (int i = 0; i < studentNumber; i++) { - students.put(new Student("student"+(i+1), 30+random.nextInt(120),countDownLatch)); - } - Thread teacherThread =new Thread(new Teacher(students)); - students.put(new EndExam(students, 120,countDownLatch,teacherThread)); - teacherThread.start(); - countDownLatch.await(); - System.out.println(" 考试时间到,全部交卷!"); - } - -} - -class Student implements Runnable,Delayed{ - - private String name; - private long workTime; //考试时间 - private long submitTime; //交卷时间 - private boolean isForce = false; - private CountDownLatch countDownLatch; - - public Student() { - } - - public Student(String name,long workTime,CountDownLatch countDownLatch){ - this.name = name; - this.workTime = workTime; - this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS)+System.nanoTime(); - this.countDownLatch = countDownLatch; - } - - @Override - public int compareTo(Delayed o) { - if(o == null || ! (o instanceof Student)) return 1; - if(o == this) return 0; - Student s = (Student)o; - if (this.workTime > s.workTime) { - return 1; - }else if (this.workTime == s.workTime) { - return 0; - }else { - return -1; - } - } - - @Override - public long getDelay(TimeUnit unit) { - return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS); - } - - @Override - public void run() { - if (isForce) { - System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 120分钟" ); - }else { - System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 "+workTime +" 分钟"); - } - countDownLatch.countDown(); - } - - public boolean isForce() { - return isForce; - } - - public void setForce(boolean isForce) { - this.isForce = isForce; - } - -} - -class EndExam extends Student{ - - private DelayQueue students; - private CountDownLatch countDownLatch; - private Thread teacherThread; - - public EndExam(DelayQueue students, long workTime, CountDownLatch countDownLatch,Thread teacherThread) { - super("强制收卷", workTime,countDownLatch); - this.students = students; - this.countDownLatch = countDownLatch; - this.teacherThread = teacherThread; - } - - - - @Override - public void run() { - // TODO Auto-generated method stub - - teacherThread.interrupt(); - Student tmpStudent; - for (Iterator iterator2 = students.iterator(); iterator2.hasNext();) { - tmpStudent = iterator2.next(); - tmpStudent.setForce(true); - tmpStudent.run(); - } - countDownLatch.countDown(); - } - -} - - -class Teacher implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Teacher.class); - - private DelayQueue students; - - public Teacher(DelayQueue students) { - this.students = students; - } - - @Override - public void run() { - try{ - log.warn(" test start "); - while(!Thread.interrupted()){ - students.take().run(); - } - }catch(Exception e){ - log.warn("exception :",e); - } - } -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java deleted file mode 100644 index 8c5aa2ea..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.spi.test; - - -public interface TestService { - - String sayHi(String name); - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java deleted file mode 100644 index 27048a43..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.spi.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TestServiceImpl implements TestService { - - private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); - - public TestServiceImpl() { - log.warn("init"); - } - - @Override - public String sayHi(String name) { - return "TestServiceImpl1 hi,"+name; - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java deleted file mode 100644 index 2dcaefec..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.spi.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class TestServiceImpl2 implements TestService { - - private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); - - public TestServiceImpl2() { - log.warn("init"); - } - - @Override - public String sayHi(String name) { - return "TestServiceImpl2 hi,"+name; - } - -} diff --git a/mpush-tools/src/test/resources/logback.xml b/mpush-tools/src/test/resources/logback.xml index 3e167126..c1d001d8 100644 --- a/mpush-tools/src/test/resources/logback.xml +++ b/mpush-tools/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN + + System.out + UTF-8 + + INFO - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index c6331275..9d7677fa 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -20,8 +20,9 @@ package com.mpush.zk; import com.mpush.api.Constants; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.exception.ZKException; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.Utils; import com.mpush.tools.log.Logs; import com.mpush.zk.listener.ZKNodeCacheWatcher; import org.apache.curator.framework.CuratorFramework; @@ -33,7 +34,6 @@ import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.CloseableUtils; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; @@ -43,7 +43,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; -public class ZKClient { +public class ZKClient extends BaseService { public static final ZKClient I = I(); private ZKConfig zkConfig; private CuratorFramework client; @@ -55,16 +55,36 @@ private synchronized static ZKClient I() { } private ZKClient() { - init(); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + client.start(); + Logs.Console.error("init zk client waiting for connected..."); + if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { + throw new ZKException("init zk error, config=" + zkConfig); + } + initLocalCache(zkConfig.getLocalCachePath()); + registerConnectionLostListener(); + listener.onSuccess(zkConfig.getHosts()); + Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); + Logs.Console.error("init zk client success..."); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (cache != null) cache.close(); + TimeUnit.MILLISECONDS.sleep(600); + client.close(); } /** * 初始化 */ + @Override public void init() { if (zkConfig != null) return; zkConfig = ZKConfig.build(); - Logs.Console.error("init zk client, config=" + zkConfig); Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) @@ -94,26 +114,7 @@ public List getAclForPath(final String path) { }); } client = builder.build(); - client.start(); - Logs.Console.error("init zk client waiting for connected..."); - try { - if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { - throw new ZKException("init zk error, config=" + zkConfig); - } - initLocalCache(zkConfig.getLocalCachePath()); - } catch (Exception e) { - throw new ZKException("init zk error, config=" + zkConfig, e); - } - registerConnectionLostListener(); - - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - close(); - } - }); - - Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); - Logs.Console.error("init zk client success..."); + Logs.Console.error("init zk client, config=" + zkConfig); } // 注册连接状态监听器 @@ -123,9 +124,9 @@ private void registerConnectionLostListener() { @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { if (ConnectionState.LOST == newState) { - Logs.ZK.info("{} lost connection", MPushUtil.getInetAddress()); + Logs.ZK.info("{} lost connection", Utils.getInetAddress()); } else if (ConnectionState.RECONNECTED == newState) { - Logs.ZK.info("{} reconnected", MPushUtil.getInetAddress()); + Logs.ZK.info("{} reconnected", Utils.getInetAddress()); } } }); @@ -137,25 +138,6 @@ private void initLocalCache(String cachePath) throws Exception { cache.start(); } - private void waitClose() { - try { - Thread.sleep(600); - } catch (final InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - - /** - * 关闭 - */ - public void close() { - if (null != cache) { - cache.close(); - } - waitClose(); - CloseableUtils.closeQuietly(client); - } - /** * 获取数据,先从本地获取,本地找不到,从远程获取 * @@ -315,7 +297,7 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - Logs.ZK.error("remove:{}", key, ex); + Logs.ZK.error("removeAndClose:{}", key, ex); throw new ZKException(ex); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java b/mpush-zk/src/main/java/com/mpush/zk/ZKException.java similarity index 96% rename from mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java rename to mpush-zk/src/main/java/com/mpush/zk/ZKException.java index 23adf5a5..92f3bbff 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKException.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools.exception; +package com.mpush.zk; /** * Created by yxx on 2016/5/14. diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java index 00efec67..4cd661cd 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -48,7 +48,7 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc break; } } - Logs.ZK.info("ZK node data change, name={}, listener={}, ns={}", path, watchPath(), client.getNamespace()); + Logs.ZK.info("ZK node data change={}, name={}, listener={}, ns={}", event.getType(), path, watchPath(), client.getNamespace()); } public final void beginWatch() { diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java index f367eccc..0d30efec 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -19,8 +19,8 @@ package com.mpush.zk.node; -import com.mpush.tools.config.data.RedisGroup; import com.mpush.tools.Jsons; +import com.mpush.tools.config.data.RedisGroup; /** * Created by yxx on 2016/5/18. diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index 12a804e9..73d24e84 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -21,7 +21,7 @@ import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import com.mpush.tools.config.CC; import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKPath; @@ -50,14 +50,14 @@ public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { } public static ZKServerNode csNode() { - return new ZKServerNode(MPushUtil.getLocalIp(), + return new ZKServerNode(Utils.getLocalIp(), CC.mp.net.connect_server_port, ConfigManager.I.getPublicIp(), ZKPath.CONNECT_SERVER.getNodePath()); } public static ZKServerNode gsNode() { - return new ZKServerNode(MPushUtil.getLocalIp(), + return new ZKServerNode(Utils.getLocalIp(), CC.mp.net.gateway_server_port, null, ZKPath.GATEWAY_SERVER.getNodePath()); diff --git a/pom.xml b/pom.xml index 57891f08..5c4d8401 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,11 @@ netty-codec-http ${netty.version} + + io.netty + netty-handler + 5.0.0.Alpha2 + From a45eabd55ec01b0b12ce72dfa8099e1eb9c349b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 29 May 2016 17:25:07 +0200 Subject: [PATCH 554/890] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- .../com/mpush/api/service/BaseService.java | 41 +++++-- .../java/com/mpush/api/spi/SpiLoader.java | 3 +- .../com/mpush/client/push/PushClient.java | 6 +- .../com/mpush/monitor/data/MonitorResult.java | 69 ++++------- .../mpush/monitor/data/ResultCollector.java | 14 +-- .../com/mpush/monitor/quota/GCMQuota.java | 18 +-- .../com/mpush/monitor/quota/InfoQuota.java | 6 +- .../com/mpush/monitor/quota/MemoryQuota.java | 2 +- .../{BaseQuota.java => MonitorQuota.java} | 6 +- .../mpush/monitor/quota/ThreadPoolQuota.java | 2 +- .../com/mpush/monitor/quota/ThreadQuota.java | 10 +- .../com/mpush/monitor/quota/impl/JVMGC.java | 6 +- .../com/mpush/monitor/quota/impl/JVMInfo.java | 22 ++-- .../mpush/monitor/quota/impl/JVMMemory.java | 6 +- .../mpush/monitor/quota/impl/JVMThread.java | 7 +- .../monitor/quota/impl/JVMThreadPool.java | 8 +- .../mpush/monitor/service/MonitorService.java | 25 ++-- .../com/mpush/netty/client/NettyClient.java | 5 +- .../com/mpush/netty/http/NettyHttpClient.java | 5 +- .../com/mpush/netty/server/NettyServer.java | 5 +- .../java/com/mpush/tools/common/JVMUtil.java | 115 ++++++++++++------ .../main/java/com/mpush/tools/config/CC.java | 6 +- .../tools/thread/pool/ThreadPoolManager.java | 10 +- 24 files changed, 213 insertions(+), 186 deletions(-) rename mpush-monitor/src/main/java/com/mpush/monitor/quota/{BaseQuota.java => MonitorQuota.java} (86%) diff --git a/conf/reference.conf b/conf/reference.conf index 6f75b89f..a6fff22d 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -183,6 +183,6 @@ mp { } spi { - + thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" } } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java index 9f2c58bc..8d63c14b 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -19,8 +19,6 @@ package com.mpush.api.service; -import com.sun.istack.internal.NotNull; - import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.atomic.AtomicBoolean; @@ -44,7 +42,7 @@ public boolean isRunning() { } protected void tryStart(Listener listener, Function function) { - listener = new FutureListener(listener); + listener = wrap(listener); if (started.compareAndSet(false, true)) { try { init(); @@ -52,6 +50,7 @@ protected void tryStart(Listener listener, Function function) { listener.onSuccess("service " + this.getClass().getSimpleName() + " start success"); } catch (Throwable e) { listener.onFailure(e); + throw new ServiceException(e); } } else { listener.onFailure(new ServiceException("service already started.")); @@ -59,13 +58,14 @@ protected void tryStart(Listener listener, Function function) { } protected void tryStop(Listener listener, Function function) { - listener = new FutureListener(listener); + listener = wrap(listener); if (started.compareAndSet(true, false)) { try { function.apply(listener); listener.onSuccess("service " + this.getClass().getSimpleName() + " stop success"); } catch (Throwable e) { listener.onFailure(e); + throw new ServiceException(e); } } else { listener.onFailure(new ServiceException("service already stopped.")); @@ -73,13 +73,13 @@ protected void tryStop(Listener listener, Function function) { } public final Future start() { - FutureListener listener = new FutureListener(null); + FutureListener listener = new FutureListener(); start(listener); return listener; } public final Future stop() { - FutureListener listener = new FutureListener(null); + FutureListener listener = new FutureListener(); stop(listener); return listener; } @@ -94,16 +94,33 @@ public void stop(Listener listener) { tryStop(listener, this::doStop); } - protected abstract void doStart(@NotNull Listener listener) throws Throwable; + protected abstract void doStart(Listener listener) throws Throwable; - protected abstract void doStop(@NotNull Listener listener) throws Throwable; + protected abstract void doStop(Listener listener) throws Throwable; protected interface Function { - void apply(@NotNull Listener l) throws Throwable; + void apply(Listener l) throws Throwable; + } + + /** + * 防止Listener被重复执行 + * + * @param l + * @return + */ + public FutureListener wrap(Listener l) { + if (l == null) return new FutureListener(); + if (l instanceof FutureListener) return (FutureListener) l; + return new FutureListener(l); } protected class FutureListener extends FutureTask implements Listener { - private final Listener l; + private final Listener l;// 防止Listener被重复执行 + + public FutureListener() { + super(BaseService.this::isRunning); + this.l = null; + } public FutureListener(Listener l) { super(BaseService.this::isRunning); @@ -112,14 +129,14 @@ public FutureListener(Listener l) { @Override public void onSuccess(Object... args) { - if (isDone()) return; + if (isDone()) return;// 防止Listener被重复执行 set(started.get()); if (l != null) l.onSuccess(args); } @Override public void onFailure(Throwable cause) { - if (isDone()) return; + if (isDone()) return;// 防止Listener被重复执行 set(started.get()); setException(cause); if (l != null) l.onFailure(cause); diff --git a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index beb2ad45..59df059f 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -71,7 +71,8 @@ private static T filterByName(ServiceLoader factories, String name) { } else { while (it.hasNext()) { T t = it.next(); - if (name.equals(t.getClass().getSimpleName())) { + if (name.equals(t.getClass().getName()) || + name.equals(t.getClass().getSimpleName())) { return t; } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 0fecb3cd..d88f105c 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -27,9 +27,7 @@ import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.gateway.GatewayClientFactory; import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; import com.mpush.zk.listener.ZKServerNodeWatcher; -import com.sun.istack.internal.NotNull; import java.util.Collection; @@ -68,14 +66,14 @@ public Connection getGatewayConnection(String host) { } @Override - protected void doStart(@NotNull Listener listener) throws Throwable { + protected void doStart(Listener listener) throws Throwable { ZKClient.I.start(listener); RedisManager.I.init(); ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).beginWatch(); } @Override - protected void doStop(@NotNull Listener listener) throws Throwable { + protected void doStop(Listener listener) throws Throwable { factory.clear(); ZKClient.I.stop(listener); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java index 865345b0..e064273a 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java @@ -19,68 +19,47 @@ package com.mpush.monitor.data; +import com.mpush.tools.Jsons; + +import java.util.HashMap; import java.util.Map; public class MonitorResult { + private Long timestamp = System.currentTimeMillis(); + private Map results = new HashMap<>(8); - private Long timestamp; - - private Map memoryMap; - - private Map gcMap; - - private Map threadMap; - - private Map threadPoolMap; - - private Map infoMap; - - public MonitorResult() { - this.timestamp = System.currentTimeMillis(); - } - - public Map getMemoryMap() { - return memoryMap; + public MonitorResult addResult(String name, Object result) { + results.put(name, result); + return this; } - public void setMemoryMap(Map memoryMap) { - this.memoryMap = memoryMap; + public Map getResults() { + return results; } - public Map getGcMap() { - return gcMap; - } - - public void setGcMap(Map gcMap) { - this.gcMap = gcMap; - } - - public Map getThreadMap() { - return threadMap; - } - - public void setThreadMap(Map threadMap) { - this.threadMap = threadMap; + public MonitorResult setResults(Map results) { + this.results = results; + return this; } public Long getTimestamp() { return timestamp; } - public Map getInfoMap() { - return infoMap; - } - - public void setInfoMap(Map infoMap) { - this.infoMap = infoMap; + public MonitorResult setTimestamp(Long timestamp) { + this.timestamp = timestamp; + return this; } - public Map getThreadPoolMap() { - return threadPoolMap; + @Override + public String toString() { + return "MonitorResult{" + + "results=" + results + + ", timestamp=" + timestamp + + '}'; } - public void setThreadPoolMap(Map threadPoolMap) { - this.threadPoolMap = threadPoolMap; + public String toJson() { + return Jsons.toJson(this); } - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java index b366abba..f6d5f244 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java @@ -29,13 +29,13 @@ public class ResultCollector { public MonitorResult collect() { - MonitorResult data = new MonitorResult(); - data.setInfoMap(JVMInfo.I.toMap()); - data.setGcMap(JVMGC.I.toMap()); - data.setMemoryMap(JVMMemory.I.toMap()); - data.setThreadMap(JVMThread.I.toMap()); - data.setThreadPoolMap(JVMThreadPool.I.toMap()); - return data; + MonitorResult result = new MonitorResult(); + result.addResult("jvm-info", JVMInfo.I.monitor()); + result.addResult("jvm-gc", JVMGC.I.monitor()); + result.addResult("jvm-memory", JVMMemory.I.monitor()); + result.addResult("jvm-thread", JVMThread.I.monitor()); + result.addResult("jvm-thread-pool", JVMThreadPool.I.monitor()); + return result; } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java index 6bcf3b3f..91d4cb24 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java @@ -19,22 +19,22 @@ package com.mpush.monitor.quota; -public interface GCMQuota { +public interface GCMQuota extends MonitorQuota { - public long yongGcCollectionCount(); + long yongGcCollectionCount(); - public long yongGcCollectionTime(); + long yongGcCollectionTime(); - public long fullGcCollectionCount(); + long fullGcCollectionCount(); - public long fullGcCollectionTime(); + long fullGcCollectionTime(); - public long spanYongGcCollectionCount(); + long spanYongGcCollectionCount(); - public long spanYongGcCollectionTime(); + long spanYongGcCollectionTime(); - public long spanFullGcCollectionCount(); + long spanFullGcCollectionCount(); - public long spanFullGcCollectionTime(); + long spanFullGcCollectionTime(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java index a2d7eef6..cec75350 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java @@ -19,9 +19,9 @@ package com.mpush.monitor.quota; -public interface InfoQuota { +public interface InfoQuota extends MonitorQuota { - public String pid(); + String pid(); - public double load(); + double load(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java index 53c66c1d..83a01a82 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java @@ -19,7 +19,7 @@ package com.mpush.monitor.quota; -public interface MemoryQuota { +public interface MemoryQuota extends MonitorQuota { // Heap long heapMemoryCommitted(); diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java similarity index 86% rename from mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java rename to mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java index 4db0aa6e..dc3c61ab 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java @@ -19,9 +19,7 @@ package com.mpush.monitor.quota; -import java.util.Map; +public interface MonitorQuota { -public abstract class BaseQuota { - - public abstract Map toMap(); + Object monitor(Object... args); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java index 16b3e7cf..1cba7c14 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java @@ -19,6 +19,6 @@ package com.mpush.monitor.quota; -public interface ThreadPoolQuota { +public interface ThreadPoolQuota extends MonitorQuota { } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java index 1c94762a..623572ec 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java @@ -20,14 +20,14 @@ package com.mpush.monitor.quota; -public interface ThreadQuota { +public interface ThreadQuota extends MonitorQuota { - public int daemonThreadCount(); + int daemonThreadCount(); - public int threadCount(); + int threadCount(); - public long totalStartedThreadCount(); + long totalStartedThreadCount(); - public int deadLockedThreadCount(); + int deadLockedThreadCount(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java index b84d94fe..2b9d0903 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java @@ -21,7 +21,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.GCMQuota; import java.lang.management.GarbageCollectorMXBean; @@ -29,7 +28,7 @@ import java.util.List; import java.util.Map; -public class JVMGC extends BaseQuota implements GCMQuota { +public class JVMGC implements GCMQuota { private final List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", @@ -146,7 +145,7 @@ public long spanFullGcCollectionTime() { } @Override - public Map toMap() { + public Object monitor(Object... args) { Map map = Maps.newHashMap(); map.put("yongGcCollectionCount", yongGcCollectionCount()); map.put("yongGcCollectionTime", yongGcCollectionTime()); @@ -158,5 +157,4 @@ public Map toMap() { map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); return map; } - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index baff0a65..eb0276a4 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -20,15 +20,15 @@ package com.mpush.monitor.quota.impl; import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.InfoQuota; +import com.mpush.monitor.quota.MonitorQuota; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.util.Map; -public class JVMInfo extends BaseQuota implements InfoQuota { +public class JVMInfo implements InfoQuota { public static final JVMInfo I = new JVMInfo(); @@ -51,18 +51,20 @@ public String pid() { return currentPid; } - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("pid", pid()); - map.put("load", load()); - return map; - } - @Override public double load() { double averageLoad = systemMXBean.getSystemLoadAverage(); return averageLoad; } + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("pid", pid()); + map.put("load", load()); + map.put("totalMemory", Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m"); + map.put("freeMemory", Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m"); + map.put("maxMemory", Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m"); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java index 1c9ca2f8..441822a4 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java @@ -21,7 +21,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.MemoryQuota; import java.lang.management.ManagementFactory; @@ -30,7 +29,7 @@ import java.util.List; import java.util.Map; -public class JVMMemory extends BaseQuota implements MemoryQuota { +public class JVMMemory implements MemoryQuota { private final List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); @@ -235,7 +234,7 @@ public long survivorUsed() { } @Override - public Map toMap() { + public Object monitor(Object... args) { Map map = Maps.newHashMap(); map.put("heapMemoryCommitted", heapMemoryCommitted()); map.put("heapMemoryInit", heapMemoryInit()); @@ -263,5 +262,4 @@ public Map toMap() { map.put("survivorUsed", survivorUsed()); return map; } - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index cde994ed..abae771a 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -20,14 +20,13 @@ package com.mpush.monitor.quota.impl; import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.ThreadQuota; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Map; -public class JVMThread extends BaseQuota implements ThreadQuota { +public class JVMThread implements ThreadQuota { private ThreadMXBean threadMXBean; @@ -66,7 +65,7 @@ public int deadLockedThreadCount() { } @Override - public Map toMap() { + public Object monitor(Object... args) { Map map = Maps.newHashMap(); map.put("daemonThreadCount", daemonThreadCount()); map.put("threadCount", threadCount()); @@ -74,6 +73,4 @@ public Map toMap() { map.put("deadLockedThreadCount", deadLockedThreadCount()); return map; } - - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index 2d231742..52bcd554 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -19,7 +19,6 @@ package com.mpush.monitor.quota.impl; -import com.mpush.monitor.quota.BaseQuota; import com.mpush.monitor.quota.ThreadPoolQuota; import com.mpush.tools.thread.pool.ThreadPoolManager; @@ -28,14 +27,15 @@ import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota { +public class JVMThreadPool implements ThreadPoolQuota { public static final JVMThreadPool I = new JVMThreadPool(); private JVMThreadPool() { } + @Override - public Map toMap() { + public Object monitor(Object... args) { Map map = new HashMap<>(); Map pool = ThreadPoolManager.I.getActivePools(); for (Map.Entry entry : pool.entrySet()) { @@ -45,6 +45,4 @@ public Map toMap() { } return map; } - - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java index 3f53ffcd..3ce12611 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -24,22 +24,22 @@ import com.mpush.monitor.data.MonitorResult; import com.mpush.monitor.data.ResultCollector; import com.mpush.monitor.quota.impl.JVMInfo; -import com.mpush.tools.Jsons; import com.mpush.tools.common.JVMUtil; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; -import com.sun.istack.internal.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.TimeUnit; public class MonitorService extends BaseService implements Runnable { - + private final Logger logger = LoggerFactory.getLogger(this.getClass()); public static final MonitorService I = new MonitorService(); private static final int firstJstack = 2, secondJstack = 4, thirdJstack = 6, firstJmap = 4; private static final String dumpLogDir = CC.mp.monitor.dump_dir; - private static final boolean enableDump = CC.mp.monitor.dump_stack; + private static final boolean dumpEnabled = CC.mp.monitor.dump_stack; private static final boolean printLog = CC.mp.monitor.print_log; private static final long dumpPeriod = CC.mp.monitor.dump_period.getSeconds(); @@ -52,14 +52,17 @@ public class MonitorService extends BaseService implements Runnable { @Override public void run() { - while (started.get()) { + while (isRunning()) { MonitorResult result = collector.collect(); + if (printLog) { - Logs.Monitor.info(Jsons.toJson(result)); + Logs.Monitor.info(result.toJson()); } - if (enableDump) { + + if (dumpEnabled) { dump(); } + try { TimeUnit.SECONDS.sleep(dumpPeriod); } catch (InterruptedException e) { @@ -69,16 +72,16 @@ public void run() { } @Override - protected void doStart(@NotNull Listener listener) throws Throwable { - if (CC.mp.monitor.print_log || enableDump) { + protected void doStart(Listener listener) throws Throwable { + if (printLog || dumpEnabled) { Thread thread = new Thread(this, "mp-t-monitor"); thread.start(); } } @Override - protected void doStop(@NotNull Listener listener) throws Throwable { - + protected void doStop(Listener listener) throws Throwable { + logger.error("monitor service stopped!"); } private void dump() { diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 54af8dc8..b3efb889 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -25,7 +25,6 @@ import com.mpush.api.service.ServiceException; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; -import com.sun.istack.internal.NotNull; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -105,12 +104,12 @@ protected ChannelHandler getEncoder() { public abstract ChannelHandler getChannelHandler(); @Override - protected void doStart(@NotNull Listener listener) throws Throwable { + protected void doStart(Listener listener) throws Throwable { } @Override - protected void doStop(@NotNull Listener listener) throws Throwable { + protected void doStop(Listener listener) throws Throwable { if (workerGroup != null) { workerGroup.shutdownGracefully(); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index 5e554589..9af0db7e 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -24,7 +24,6 @@ import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; -import com.sun.istack.internal.NotNull; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -120,7 +119,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } @Override - protected void doStart(@NotNull Listener listener) throws Throwable { + protected void doStart(Listener listener) throws Throwable { workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); b = new Bootstrap(); b.group(workerGroup); @@ -143,7 +142,7 @@ public void initChannel(SocketChannel ch) throws Exception { } @Override - protected void doStop(@NotNull Listener listener) throws Throwable { + protected void doStop(Listener listener) throws Throwable { pool.close(); workerGroup.shutdownGracefully(); timer.stop(); diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index be8495aa..0b242517 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -27,7 +27,6 @@ import com.mpush.netty.codec.PacketEncoder; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; -import com.sun.istack.internal.NotNull; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -255,12 +254,12 @@ private boolean useNettyEpoll() { } @Override - protected void doStart(@NotNull Listener listener) throws Throwable { + protected void doStart(Listener listener) throws Throwable { } @Override - protected void doStop(@NotNull Listener listener) throws Throwable { + protected void doStop(Listener listener) throws Throwable { } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java index e0ee5771..34159ba4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -25,51 +25,89 @@ import javax.management.MBeanServer; import javax.management.ObjectName; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.lang.management.ManagementFactory; +import java.io.*; +import java.lang.management.*; import java.security.AccessController; import java.security.PrivilegedExceptionAction; import java.util.Iterator; import java.util.Map; import java.util.Set; -import java.util.concurrent.Executors; public class JVMUtil { + private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class); - private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; - private static volatile HotSpotDiagnosticMXBean mxBean; - - private static Object lock = new Object(); + private static HotSpotDiagnosticMXBean hotSpotMXBean; + private static ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); public static void jstack(OutputStream stream) throws Exception { - try { - Map map = Thread.getAllStackTraces(); - Iterator> ite = map.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - StackTraceElement[] elements = entry.getValue(); - if (elements != null && elements.length > 0) { - String threadName = entry.getKey().getName(); - stream.write(("Thread Name :[" + threadName + "]\n").getBytes()); - for (StackTraceElement el : elements) { - String stack = el.toString() + "\n"; - stream.write(stack.getBytes()); + PrintStream out = new PrintStream(stream); + boolean cpuTimeEnabled = threadMXBean.isThreadCpuTimeSupported() && threadMXBean.isThreadCpuTimeEnabled(); + Map map = Thread.getAllStackTraces(); + + for (Map.Entry entry : map.entrySet()) { + Thread t = entry.getKey(); + StackTraceElement[] elements = entry.getValue(); + + ThreadInfo tt = threadMXBean.getThreadInfo(t.getId()); + long tid = t.getId(); + Thread.State state = t.getState(); + long cpuTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadCpuTime(tid) / 1000000 : -1; + long userTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadUserTime(tid) / 1000000 : -1; + + out.printf("%s id=%d state=%s deamon=%s priority=%s cpu[total=%sms,user=%sms]", t.getName(), + tid, t.getState(), t.isDaemon(), t.getPriority(), cpuTimeMillis, userTimeMillis); + final LockInfo lock = tt.getLockInfo(); + if (lock != null && state != Thread.State.BLOCKED) { + out.printf("%n - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()); + out.printf("%n - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()); + } else if (lock != null && state == Thread.State.BLOCKED) { + out.printf("%n - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), + lock.getClassName()); + } + + if (tt.isSuspended()) { + out.print(" (suspended)"); + } + + if (tt.isInNative()) { + out.print(" (running in native)"); + } + + out.println(); + if (tt.getLockOwnerName() != null) { + out.printf(" owned by %s id=%d%n", tt.getLockOwnerName(), tt.getLockOwnerId()); + } + + final MonitorInfo[] monitors = tt.getLockedMonitors(); + + for (int i = 0; i < elements.length; i++) { + final StackTraceElement element = elements[i]; + out.printf(" at %s%n", element); + for (int j = 1; j < monitors.length; j++) { + final MonitorInfo monitor = monitors[j]; + if (monitor.getLockedStackDepth() == i) { + out.printf(" - locked %s%n", monitor); } - stream.write("\n".getBytes()); } } - } catch (Exception e) { - throw e; + + out.println(); + + final LockInfo[] locks = tt.getLockedSynchronizers(); + if (locks.length > 0) { + out.printf(" Locked synchronizers: count = %d%n", locks.length); + for (LockInfo l : locks) { + out.printf(" - %s%n", l); + } + out.println(); + } } } public static void dumpJstack(final String jvmPath) { - Executors.newSingleThreadExecutor().execute(new Runnable() { + new Thread((new Runnable() { @Override public void run() { String logPath = jvmPath; @@ -88,10 +126,10 @@ public void run() { } } } - }); + })).start(); } - private static HotSpotDiagnosticMXBean getMxBean() { + private static HotSpotDiagnosticMXBean getHotSpotMXBean() { try { return AccessController.doPrivileged(new PrivilegedExceptionAction() { public HotSpotDiagnosticMXBean run() throws Exception { @@ -109,16 +147,16 @@ public HotSpotDiagnosticMXBean run() throws Exception { } }); } catch (Exception e) { - LOGGER.error("getMxBean Error!", e); + LOGGER.error("getHotSpotMXBean Error!", e); return null; } } - private static void initHotspotMBean() throws Exception { - if (mxBean == null) { - synchronized (lock) { - if (mxBean == null) { - mxBean = getMxBean(); + private static void initHotSpotMBean() throws Exception { + if (hotSpotMXBean == null) { + synchronized (JVMUtil.class) { + if (hotSpotMXBean == null) { + hotSpotMXBean = getHotSpotMXBean(); } } } @@ -128,23 +166,22 @@ public static void jMap(String fileName, boolean live) { File f = new File(fileName, System.currentTimeMillis() + "-jmap.LOGGER"); String currentFileName = f.getPath(); try { - initHotspotMBean(); + initHotSpotMBean(); if (f.exists()) { f.delete(); } - mxBean.dumpHeap(currentFileName, live); + hotSpotMXBean.dumpHeap(currentFileName, live); } catch (Exception e) { LOGGER.error("dumpHeap Error!" + currentFileName, e); } } public static void dumpJmap(final String jvmPath) { - Executors.newSingleThreadExecutor().execute(new Runnable() { + new Thread(new Runnable() { @Override public void run() { jMap(jvmPath, false); } - }); + }).start(); } - } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 482ab48f..bf20ea92 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -329,14 +329,16 @@ static Map> loadMapping() { } interface monitor { - Config cfg = mp.cfg.getObject("monitor").toConfig(); String dump_dir = cfg.getString("dump-dir"); boolean dump_stack = cfg.getBoolean("dump-stack"); boolean print_log = cfg.getBoolean("print-log"); - Duration dump_period = cfg.getDuration("dump-period"); + } + interface spi { + Config cfg = mp.cfg.getObject("spi").toConfig(); + String thread_pool_factory = cfg.getString("thread-pool-factory"); } } } \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 900237c4..e0c5886e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -28,10 +28,12 @@ import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; +import static com.mpush.tools.config.CC.mp.spi.thread_pool_factory; + public class ThreadPoolManager { public static final ThreadPoolManager I = new ThreadPoolManager(); - private final ThreadPoolFactory threadPoolFactory = SpiLoader.load(ThreadPoolFactory.class); + private final ThreadPoolFactory threadPoolFactory = SpiLoader.load(ThreadPoolFactory.class, thread_pool_factory); private final NamedThreadFactory threadFactory = new NamedThreadFactory(); private Executor bossExecutor; @@ -125,9 +127,9 @@ public static Map getPoolInfo(ThreadPoolExecutor executor) { Map info = new HashMap<>(); info.put("corePoolSize", executor.getCorePoolSize()); info.put("maximumPoolSize", executor.getMaximumPoolSize()); - info.put("activeCount[workingThread]", executor.getActiveCount()); - info.put("poolSize[workThread]", executor.getPoolSize()); - info.put("queueSize[blockTask]", executor.getQueue().size()); + info.put("activeCount(workingThread)", executor.getActiveCount()); + info.put("poolSize(workThread)", executor.getPoolSize()); + info.put("queueSize(blockedTask)", executor.getQueue().size()); return info; } } From 844b0e62a1d9d44205abaf9a1e56ea4881abd2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 29 May 2016 18:02:47 +0200 Subject: [PATCH 555/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/java/com/mpush/bootstrap/Main.java | 3 +-- .../src/main/java/com/mpush/bootstrap/job/LastBoot.java | 4 ++-- .../src/main/java/com/mpush/cache/redis/RedisClient.java | 4 +++- .../src/main/java/com/mpush/common/user/UserManager.java | 9 +++++---- .../main/java/com/mpush/core/handler/AdminHandler.java | 6 ++---- .../com/mpush/core/router/UserOnlineOfflineListener.java | 4 ++-- .../java/com/mpush/test/push/PushClientTestMain.java | 2 +- .../com/mpush/tools/thread/pool/ThreadPoolManager.java | 2 +- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 7 +------ 9 files changed, 18 insertions(+), 23 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 8c287e4d..2a0550ff 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -25,7 +25,7 @@ public class Main { public static void main(String[] args) { Logs.init(); - Logs.Console.error("launch app..."); + Logs.Console.error("launch mpush server..."); ServerLauncher launcher = new ServerLauncher(); launcher.start(); addHook(launcher); @@ -39,5 +39,4 @@ public void run() { } }); } - } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index ba65d171..5a1f5ea2 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -30,8 +30,8 @@ public class LastBoot extends BootJob { @Override public void run() { - UserManager.INSTANCE.clearUserOnlineData(); + UserManager.I.clearUserOnlineData(); Logs.Console.error("end run bootstrap chain..."); - Logs.Console.error("app start success..."); + Logs.Console.error("mpush server start success..."); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 6a0abb71..f2b86646 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -28,6 +28,8 @@ import java.util.*; +import static redis.clients.jedis.Protocol.DEFAULT_TIMEOUT; + public class RedisClient { public static final JedisPoolConfig CONFIG = buildConfig(); private static final Map POOL_MAP = Maps.newConcurrentMap(); @@ -60,7 +62,7 @@ private static JedisPoolConfig buildConfig() { public static Jedis getClient(RedisServer node) { JedisPool pool = POOL_MAP.get(node); if (pool == null) { - pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), Protocol.DEFAULT_TIMEOUT, node.getPassword()); + pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), DEFAULT_TIMEOUT, node.getPassword()); POOL_MAP.put(node, pool); } return pool.getResource(); diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java index 204751f9..7a9d8372 100644 --- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -21,7 +21,7 @@ import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.tools.Utils; +import com.mpush.tools.config.ConfigManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,9 +30,9 @@ //查询使用 public final class UserManager { private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); - public static final UserManager INSTANCE = new UserManager(); + public static final UserManager I = new UserManager(); - private final String ONLINE_KEY = RedisKey.getUserOnlineKey(Utils.getExtranetAddress()); + private final String ONLINE_KEY = RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()); public UserManager() { clearUserOnlineData(); @@ -54,7 +54,8 @@ public void recordUserOffline(String userId) { //在线用户 public long getOnlineUserNum() { - return RedisManager.I.zCard(ONLINE_KEY); + Long value = RedisManager.I.zCard(ONLINE_KEY); + return value == null ? 0 : value; } //在线用户列表 diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index f47d66c3..82a4981a 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -21,9 +21,8 @@ import com.google.common.base.Strings; import com.mpush.api.service.Listener; -import com.mpush.cache.redis.RedisKey; -import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.router.RemoteRouter; +import com.mpush.common.user.UserManager; import com.mpush.core.router.RouterCenter; import com.mpush.core.server.AdminServer; import com.mpush.tools.Jsons; @@ -168,8 +167,7 @@ public Serializable handler(ChannelHandlerContext ctx, String args) { case "conn": return adminServer.getConnectionServer().getConnectionManager().getConnections().size(); case "online": { - Long value = RedisManager.I.zCard(RedisKey.getUserOnlineKey(Utils.getExtranetAddress())); - return value == null ? 0 : value; + return UserManager.I.getOnlineUserNum(); } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 20de3b91..d0987f04 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -43,13 +43,13 @@ public UserOnlineOfflineListener() { @Subscribe void on(UserOnlineEvent event) { - UserManager.INSTANCE.recordUserOnline(event.getUserId()); + UserManager.I.recordUserOnline(event.getUserId()); RedisManager.I.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe void on(UserOfflineEvent event) { - UserManager.INSTANCE.recordUserOffline(event.getUserId()); + UserManager.I.recordUserOffline(event.getUserId()); RedisManager.I.publish(OFFLINE_CHANNEL, event.getUserId()); } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index ac13d0bd..9c18129e 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -38,7 +38,7 @@ public static void main(String[] args) throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().get(); - for (int i = 0; i < 10000; i++) { + for (int i = 0; i < 1000; i++) { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); //Thread.sleep(1000); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index e0c5886e..45b28813 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -126,7 +126,7 @@ public Map getActivePools() { public static Map getPoolInfo(ThreadPoolExecutor executor) { Map info = new HashMap<>(); info.put("corePoolSize", executor.getCorePoolSize()); - info.put("maximumPoolSize", executor.getMaximumPoolSize()); + info.put("maxPoolSize", executor.getMaximumPoolSize()); info.put("activeCount(workingThread)", executor.getActiveCount()); info.put("poolSize(workThread)", executor.getPoolSize()); info.put("queueSize(blockedTask)", executor.getQueue().size()); diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 9d7677fa..c74d2a95 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -22,7 +22,6 @@ import com.mpush.api.Constants; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; -import com.mpush.tools.Utils; import com.mpush.tools.log.Logs; import com.mpush.zk.listener.ZKNodeCacheWatcher; import org.apache.curator.framework.CuratorFramework; @@ -123,11 +122,7 @@ private void registerConnectionLostListener() { //TODO need close jvm? @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - if (ConnectionState.LOST == newState) { - Logs.ZK.info("{} lost connection", Utils.getInetAddress()); - } else if (ConnectionState.RECONNECTED == newState) { - Logs.ZK.info("{} reconnected", Utils.getInetAddress()); - } + Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); } }); } From 53ae8c399ef161c05fc78021a54b59ebb16a50f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 31 May 2016 04:52:15 +0200 Subject: [PATCH 556/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- .../test/java/com/mpush/test/client/ConnClientTestMain.java | 2 +- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index a6fff22d..07526222 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -170,7 +170,7 @@ mp { push-callback { min:2 max:2 - queue-size:1 + queue-size:0 } } } diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index 38cb099d..c8913e04 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -41,7 +41,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ZKServerNode server = serverList.get(index); - server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); + //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); for (int i = 0; i < 1; i++) { String clientVersion = "1.0." + i; String osName = "android"; diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index c74d2a95..81b578f6 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -64,7 +64,7 @@ protected void doStart(Listener listener) throws Throwable { throw new ZKException("init zk error, config=" + zkConfig); } initLocalCache(zkConfig.getLocalCachePath()); - registerConnectionLostListener(); + addConnectionStateListener(); listener.onSuccess(zkConfig.getHosts()); Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); Logs.Console.error("init zk client success..."); @@ -117,7 +117,7 @@ public List getAclForPath(final String path) { } // 注册连接状态监听器 - private void registerConnectionLostListener() { + private void addConnectionStateListener() { client.getConnectionStateListenable().addListener(new ConnectionStateListener() { //TODO need close jvm? @Override From 82953903d29ff7a61a94276ed9e7db185dfb4678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 31 May 2016 04:57:21 +0200 Subject: [PATCH 557/890] New changelist --- mpush-test/src/test/resources/application.conf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index b13d6c3d..b286ed87 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -1,3 +1,5 @@ mp.log.dir=${user.dir}/mpush-test/target/logs mp.log.level=debug -mp.zk.namespace=mpush \ No newline at end of file +mp.zk.namespace=mpush +mp.zk.server-address="111.1.57.148:5666" +mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} \ No newline at end of file From 58d78674be93299f96e1f0ce31511347455c0e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 31 May 2016 05:09:19 +0200 Subject: [PATCH 558/890] New changelist --- .../src/main/java/com/mpush/client/gateway/GatewayClient.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java index fbe13473..4b423d04 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -22,7 +22,6 @@ import com.mpush.api.connection.Connection; import com.mpush.api.service.Listener; import com.mpush.netty.client.NettyClient; -import com.sun.istack.internal.NotNull; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; @@ -69,7 +68,7 @@ protected void initPipeline(ChannelPipeline pipeline) { } @Override - protected void doStop(@NotNull Listener listener) throws Throwable { + protected void doStop(Listener listener) throws Throwable { if (trafficShapingHandler != null) { trafficShapingHandler.release(); } From 7b720921f2050e561972bb54e174c6012ba27a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 16 Aug 2016 16:57:15 +0800 Subject: [PATCH 559/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9Push=20content=20?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=20string->byte[]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/protocol/Packet.java | 3 ++- .../java/com/mpush/api/push/PushSender.java | 4 ++++ .../com/mpush/client/push/PushClient.java | 23 +++++++++++-------- .../com/mpush/client/push/PushRequest.java | 4 ++-- .../com/mpush/common/message/BaseMessage.java | 4 ++-- .../com/mpush/common/message/PushMessage.java | 12 ++++++---- .../message/gateway/GatewayPushMessage.java | 10 ++++---- .../com/mpush/netty/codec/PacketDecoder.java | 2 +- .../mpush/test/push/PushClientTestMain.java | 4 ++-- .../src/test/resources/application.conf | 3 ++- 10 files changed, 41 insertions(+), 28 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 11e99ca5..d9921802 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -35,6 +35,7 @@ public final class Packet { public static final byte HB_PACKET_BYTE = -33; public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; + public static final Packet HB_PACKE = new Packet(Command.HEARTBEAT); public byte cmd; //命令 public short cc; //校验码 暂时没有用到 @@ -65,7 +66,7 @@ public int getBodyLength() { return body == null ? 0 : body.length; } - public void setFlag(byte flag) { + public void addFlag(byte flag) { this.flags |= flag; } diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java index e94a3c32..648787c6 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -40,6 +40,10 @@ static PushSender create() { void send(String content, String userId, Callback callback); + void send(byte[] content, Collection userIds, Callback callback); + + void send(byte[] content, String userId, Callback callback); + interface Callback { void onSuccess(String userId); diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index d88f105c..ce3502d1 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -20,6 +20,7 @@ package com.mpush.client.push; import com.google.common.base.Strings; +import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.push.PushSender; import com.mpush.api.service.BaseService; @@ -38,20 +39,24 @@ private final GatewayClientFactory factory = GatewayClientFactory.I; public void send(String content, Collection userIds, Callback callback) { - if (Strings.isNullOrEmpty(content)) return; + send(content.getBytes(Constants.UTF_8), userIds, callback); + } + + @Override + public void send(String content, String userId, Callback callback) { + send(content.getBytes(Constants.UTF_8), userId, callback); + } + + @Override + public void send(byte[] content, Collection userIds, Callback callback) { + if (content == null || content.length == 0) return; for (String userId : userIds) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); + send(content, userId, callback); } } @Override - public void send(String content, String userId, Callback callback) { + public void send(byte[] content, String userId, Callback callback) { PushRequest .build(this) .setCallback(callback) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index ed379265..37fffdcf 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -55,7 +55,7 @@ private enum Status {init, success, failure, offline, timeout} private PushSender.Callback callback; private String userId; - private String content; + private byte[] content; private long timeout; private ClientLocation location; private Future future; @@ -195,7 +195,7 @@ public PushRequest setUserId(String userId) { return this; } - public PushRequest setContent(String content) { + public PushRequest setContent(byte[] content) { this.content = content; return this; } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 59de5a69..8c3bd221 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -76,7 +76,7 @@ protected void encodeBody() { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Packet.FLAG_COMPRESS); + packet.addFlag(Packet.FLAG_COMPRESS); } } @@ -86,7 +86,7 @@ protected void encodeBody() { byte[] result = context.cipher.encrypt(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Packet.FLAG_CRYPTO); + packet.addFlag(Packet.FLAG_CRYPTO); } } packet.body = tmp; diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index fed4a788..b93a252b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -23,6 +23,8 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import java.util.Arrays; + import static com.mpush.api.protocol.Command.PUSH; /** @@ -32,31 +34,31 @@ */ public final class PushMessage extends BaseMessage { - public String content; + public byte[] content; public PushMessage(Packet packet, Connection connection) { super(packet, connection); } - public PushMessage(String content, Connection connection) { + public PushMessage(byte[] content, Connection connection) { super(new Packet(PUSH, genSessionId()), connection); this.content = content; } @Override public void decode(byte[] body) { - content = new String(body, Constants.UTF_8); + content = body; } @Override public byte[] encode() { - return content == null ? null : content.getBytes(Constants.UTF_8); + return content; } @Override public String toString() { return "PushMessage{" + - "content='" + content + '\'' + + "content='" + content.length + '\'' + ", packet=" + packet + '}'; } diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 4e586793..61320593 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -34,9 +34,9 @@ */ public class GatewayPushMessage extends ByteBufMessage { public String userId; - public String content; + public byte[] content; - public GatewayPushMessage(String userId, String content, Connection connection) { + public GatewayPushMessage(String userId, byte[] content, Connection connection) { super(new Packet(GATEWAY_PUSH, genSessionId()), connection); this.userId = userId; this.content = content; @@ -49,13 +49,13 @@ public GatewayPushMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { userId = decodeString(body); - content = decodeString(body); + content = decodeBytes(body); } @Override public void encode(ByteBuf body) { encodeString(body, userId); - encodeString(body, content); + encodeBytes(body, content); } @Override @@ -72,7 +72,7 @@ public void send(ChannelFutureListener listener) { public String toString() { return "GatewayPushMessage{" + "userId='" + userId + '\'' + - ", content='" + content + '\'' + + ", content='" + content.length + '\'' + '}'; } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 27267d38..158678e9 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -46,7 +46,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET_BYTE) { - out.add(new Packet(Command.HEARTBEAT)); + out.add(Packet.HB_PACKE); } else { in.readerIndex(in.readerIndex() - 1); break; diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 9c18129e..cd29314e 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -41,8 +41,8 @@ public static void main(String[] args) throws Exception { for (int i = 0; i < 1000; i++) { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); - //Thread.sleep(1000); - sender.send(Jsons.toJson(content), Arrays.asList("user-0"), new PushSender.Callback() { + Thread.sleep(1000); + sender.send(Jsons.toJson(content), Arrays.asList("user_1"), new PushSender.Callback() { @Override public void onSuccess(String userId) { System.err.println("push onSuccess userId=" + userId); diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index b286ed87..4c50b7c1 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -2,4 +2,5 @@ mp.log.dir=${user.dir}/mpush-test/target/logs mp.log.level=debug mp.zk.namespace=mpush mp.zk.server-address="111.1.57.148:5666" -mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} \ No newline at end of file +mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} +mp.core.compress-threshold=10k \ No newline at end of file From afde33cf238169db7b3dff91c4a933391a1242a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 19 Aug 2016 11:43:52 +0800 Subject: [PATCH 560/890] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=AB=AF=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 1 + .../mpush/api/connection/SessionContext.java | 16 ++++ .../java/com/mpush/api/push/PushSender.java | 14 ++-- .../com/mpush/api/router/ClientLocation.java | 77 ++++++++++++++----- .../java/com/mpush/api/router/ClientType.java | 58 ++++++++++++++ .../com/mpush/api/router/RouterManager.java | 16 +++- .../com/mpush/api/spi/net}/DnsMapping.java | 2 +- .../mpush/api/spi/net/DnsMappingManager.java | 7 ++ .../mpush/bootstrap/job/HttpProxyBoot.java | 6 +- .../com/mpush/cache/redis/RedisClient.java | 58 +++++++------- .../java/com/mpush/cache/redis/RedisKey.java | 2 +- .../redis/listener/ListenerDispatcher.java | 7 +- .../cache/redis/manager/RedisManager.java | 58 +++++++------- .../connect/ConnClientChannelHandler.java | 2 +- .../com/mpush/client/push/PushClient.java | 40 +++++++--- .../com/mpush/client/push/PushRequest.java | 58 +++++++------- .../mpush/common/message/ErrorMessage.java | 8 ++ .../message/gateway/GatewayPushMessage.java | 7 +- ...r.java => HttpProxyDnsMappingManager.java} | 38 ++++----- .../router/ConnectionRouterManager.java | 25 ++++-- .../common/router/RemoteRouterManager.java | 72 ++++++++++++++--- .../com.mpush.api.spi.net.DnsMappingManager | 1 + .../com/mpush/core/handler/AdminHandler.java | 37 +++++++-- .../mpush/core/handler/BindUserHandler.java | 7 +- .../core/handler/GatewayPushHandler.java | 49 ++++++------ .../mpush/core/handler/HttpProxyHandler.java | 10 ++- .../mpush/core/handler/UnbindUserHandler.java | 21 ++--- .../com/mpush/core/router/KickRemoteMsg.java | 10 ++- .../com/mpush/core/router/LocalRouter.java | 23 ++++++ .../mpush/core/router/LocalRouterManager.java | 51 ++++++------ .../com/mpush/core/router/RouterCenter.java | 26 ++++--- .../core/router/RouterChangeListener.java | 24 +++--- .../src/main/java/com/mpush/App.java | 10 --- .../netty/connection/NettyConnection.java | 1 - .../test/client/ConnClientTestMain2.java | 71 +++++++++++++++++ .../com/mpush/test/gson/DnsMappingTest.java | 2 +- .../mpush/test/push/PushClientTestMain.java | 13 ++-- .../main/java/com/mpush/tools/config/CC.java | 3 +- 38 files changed, 639 insertions(+), 292 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/router/ClientType.java rename {mpush-tools/src/main/java/com/mpush/tools/config/data => mpush-api/src/main/java/com/mpush/api/spi/net}/DnsMapping.java (98%) rename mpush-common/src/main/java/com/mpush/common/net/{DnsMappingManager.java => HttpProxyDnsMappingManager.java} (75%) create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager delete mode 100644 mpush-monitor/src/main/java/com/mpush/App.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java diff --git a/conf/reference.conf b/conf/reference.conf index 07526222..263e7849 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -184,5 +184,6 @@ mp { spi { thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" + dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" } } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index c10c4ec8..46fb0647 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -19,6 +19,8 @@ package com.mpush.api.connection; +import com.mpush.api.router.ClientType; + /** * Created by ohun on 2015/12/22. * @@ -29,8 +31,10 @@ public final class SessionContext { public String osVersion; public String clientVersion; public String deviceId; + public String userId; public int heartbeat; public Cipher cipher; + private int clientType; public void changeCipher(Cipher cipher) { this.cipher = cipher; @@ -56,6 +60,11 @@ public SessionContext setDeviceId(String deviceId) { return this; } + public SessionContext setUserId(String userId) { + this.userId = userId; + return this; + } + public void setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; } @@ -64,6 +73,13 @@ public boolean handshakeOk() { return deviceId != null && deviceId.length() > 0; } + public int getClientType() { + if (clientType == 0) { + clientType = ClientType.find(osName).type; + } + return clientType; + } + @Override public String toString() { return "SessionContext [osName=" + osName diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java index 648787c6..3c20996e 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -19,11 +19,13 @@ package com.mpush.api.push; +import com.mpush.api.router.ClientLocation; import com.mpush.api.service.Service; import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.client.PusherFactory; import java.util.Collection; +import java.util.concurrent.FutureTask; /** * Created by ohun on 2015/12/30. @@ -38,19 +40,19 @@ static PushSender create() { void send(String content, Collection userIds, Callback callback); - void send(String content, String userId, Callback callback); + FutureTask send(String content, String userId, Callback callback); void send(byte[] content, Collection userIds, Callback callback); - void send(byte[] content, String userId, Callback callback); + FutureTask send(byte[] content, String userId, Callback callback); interface Callback { - void onSuccess(String userId); + void onSuccess(String userId, ClientLocation location); - void onFailure(String userId); + void onFailure(String userId, ClientLocation location); - void onOffline(String userId); + void onOffline(String userId, ClientLocation location); - void onTimeout(String userId); + void onTimeout(String userId, ClientLocation location); } } diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index b77f1529..ca1f4102 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -19,6 +19,7 @@ package com.mpush.api.router; +import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; /** @@ -48,16 +49,15 @@ public final class ClientLocation { */ private String deviceId; + /** + * 链接ID + */ + private String connId; - public String getDeviceId() { - return deviceId; - } - - public ClientLocation setDeviceId(String deviceId) { - this.deviceId = deviceId; - return this; - } - + /** + * 客户端类型 + */ + private transient int clientType; public String getHost() { return host; @@ -72,26 +72,64 @@ public String getOsName() { return osName; } - public ClientLocation setOsName(String osName) { + public void setOsName(String osName) { this.osName = osName; - return this; } public String getClientVersion() { return clientVersion; } - public ClientLocation setClientVersion(String clientVersion) { + public void setClientVersion(String clientVersion) { this.clientVersion = clientVersion; - return this; } - public static ClientLocation from(SessionContext context) { - ClientLocation config = new ClientLocation(); - config.osName = context.osName; - config.clientVersion = context.clientVersion; - config.deviceId = context.deviceId; - return config; + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getConnId() { + return connId; + } + + public void setConnId(String connId) { + this.connId = connId; + } + + public int getClientType() { + if (clientType == 0) { + clientType = ClientType.find(osName).type; + } + return clientType; + } + + public static ClientLocation from(Connection connection) { + SessionContext context = connection.getSessionContext(); + ClientLocation location = new ClientLocation(); + location.osName = context.osName; + location.clientVersion = context.clientVersion; + location.deviceId = context.deviceId; + location.connId = connection.getId(); + return location; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ClientLocation location = (ClientLocation) o; + + return clientType == location.clientType; + } + + @Override + public int hashCode() { + return Integer.hashCode(clientType); } @Override @@ -101,6 +139,7 @@ public String toString() { ", osName='" + osName + '\'' + ", clientVersion='" + clientVersion + '\'' + ", deviceId='" + deviceId + '\'' + + ", connId='" + connId + '\'' + '}'; } } diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientType.java b/mpush-api/src/main/java/com/mpush/api/router/ClientType.java new file mode 100644 index 00000000..14216be5 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientType.java @@ -0,0 +1,58 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.router; + +import java.util.Arrays; + +/** + * Created by ohun on 16/8/18. + * + * @author ohun@live.cn (夜色) + */ +public enum ClientType { + MOBILE(1, "android", "ios"), + PC(2, "windows", "mac", "linux"), + WEB(3, "web", "h5"), + UNKNOWN(-1); + + public final int type; + public final String[] os; + + ClientType(int type, String... os) { + this.type = type; + this.os = os; + } + + public boolean contains(String osName) { + return Arrays.stream(os).anyMatch(s -> s.equalsIgnoreCase(osName)); + } + + public static boolean isSameClient(String osName1, String osName2) { + if (osName1.equals(osName2)) return true; + return find(osName1).contains(osName2); + } + + public static ClientType find(String osName) { + for (ClientType type : values()) { + if (type.contains(osName)) return type; + } + return UNKNOWN; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index ae07aa15..cd1bc7e3 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -19,6 +19,8 @@ package com.mpush.api.router; +import java.util.Set; + /** * Created by ohun on 2015/12/23. * @@ -39,15 +41,25 @@ public interface RouterManager { * 删除路由 * * @param userId + * @param clientType + * @return + */ + boolean unRegister(String userId, int clientType); + + /** + * 查询路由 + * + * @param userId * @return */ - boolean unRegister(String userId); + Set lookupAll(String userId); /** * 查询路由 * * @param userId + * @param clientType * @return */ - R lookup(String userId); + R lookup(String userId, int clientType); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java similarity index 98% rename from mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java rename to mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java index c9ad8e9a..0270d4d3 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/DnsMapping.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.tools.config.data; +package com.mpush.api.spi.net; import java.net.URL; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java index c23e7260..17bfc637 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java @@ -20,6 +20,7 @@ package com.mpush.api.spi.net; import com.mpush.api.service.Service; +import com.mpush.api.spi.SpiLoader; /** * Created by yxx on 2016/5/23. @@ -27,4 +28,10 @@ * @author ohun@live.cn (夜色) */ public interface DnsMappingManager extends Service { + + static DnsMappingManager create() { + return SpiLoader.load(DnsMappingManager.class); + } + + DnsMapping lookup(String origin); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index 22e9f210..c1a01431 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -19,7 +19,9 @@ package com.mpush.bootstrap.job; -import com.mpush.common.net.DnsMappingManager; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMappingManager; +import com.mpush.common.net.HttpProxyDnsMappingManager; import com.mpush.tools.config.CC; /** @@ -31,7 +33,7 @@ public class HttpProxyBoot extends BootJob { @Override void run() { if (CC.mp.http.proxy_enabled) { - DnsMappingManager.I.start(); + SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager).start(); } next(); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index f2b86646..2ff4c2cf 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -200,14 +200,14 @@ public static void del(List nodeList, String key) { /********************* * hash redis start ********************************/ - public static void hset(List nodeList, String namespace, String key, String value) { + public static void hset(List nodeList, String key, String field, String value) { for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hset(namespace, key, value); + jedis.hset(key, field, value); } catch (Exception e) { - Logs.REDIS.error("redis hset exception:{}, {}, {}, {}", namespace, key, value, node, e); + Logs.REDIS.error("redis hset exception:{}, {}, {}, {}", key, field, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -215,19 +215,19 @@ public static void hset(List nodeList, String namespace, String key } } - public static void hset(List nodeList, String namespace, String key, T value) { - hset(nodeList, namespace, key, Jsons.toJson(value)); + public static void hset(List nodeList, String key, String field, T value) { + hset(nodeList, key, field, Jsons.toJson(value)); } - public static T hget(RedisServer node, String namespace, String key, Class clazz) { + public static T hget(RedisServer node, String key, String field, Class clazz) { String value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hget(namespace, key); + value = jedis.hget(key, field); } catch (Exception e) { - Logs.REDIS.error("redis hget exception:{}, {}", namespace, key, node, e); + Logs.REDIS.error("redis hget exception:{}, {}", key, field, node, e); } finally { // 返还到连接池 close(jedis); @@ -236,15 +236,15 @@ public static T hget(RedisServer node, String namespace, String key, Class nodeList, String namespace, String key) { + public static void hdel(List nodeList, String key, String field) { for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hdel(namespace, key); + jedis.hdel(key, field); } catch (Exception e) { - Logs.REDIS.error("redis hdel exception:{}, {}, {}", namespace, key, node, e); + Logs.REDIS.error("redis hdel exception:{}, {}, {}", key, field, node, e); } finally { // 返还到连接池 close(jedis); @@ -252,14 +252,14 @@ public static void hdel(List nodeList, String namespace, String key } } - public static Map hgetAll(RedisServer node, String namespace) { + public static Map hgetAll(RedisServer node, String key) { Map result = null; Jedis jedis = null; try { jedis = getClient(node); - result = jedis.hgetAll(namespace); + result = jedis.hgetAll(key); } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{}, {}", namespace, node, e); + Logs.REDIS.error("redis hgetAll exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -267,16 +267,16 @@ public static Map hgetAll(RedisServer node, String namespace) { return result; } - public static Map hgetAll(RedisServer node, String namespace, Class clazz) { - Map result = hgetAll(node, namespace); + public static Map hgetAll(RedisServer node, String key, Class clazz) { + Map result = hgetAll(node, key); if (result != null) { Map newMap = Maps.newHashMap(); Iterator> iterator = result.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - String key = entry.getKey(); - String val = entry.getValue(); - newMap.put(key, Jsons.fromJson(val, clazz)); + String k = entry.getKey(); + String v = entry.getValue(); + newMap.put(k, Jsons.fromJson(v, clazz)); } return newMap; } else { @@ -311,19 +311,19 @@ public static Set hkeys(RedisServer node, String key) { * 返回 key 指定的哈希集中指定字段的值 * * @param node - * @param key + * @param fields * @param clazz * @return */ - public static List hmget(RedisServer node, String namespace, Class clazz, String... key) { + public static List hmget(RedisServer node, String key, Class clazz, String... fields) { List value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hmget(namespace, key); + value = jedis.hmget(key, fields); } catch (Exception e) { - Logs.REDIS.error("redis hmget exception:{},{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hmget exception:{},{},{}", key, fields, node, e); } finally { // 返还到连接池 close(jedis); @@ -340,7 +340,7 @@ public static List hmget(RedisServer node, String namespace, Class cla * @param hash * @param time */ - public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + public static void hmset(List nodeList, String key, Map hash, Integer time) { if (time == null) { time = -1; @@ -349,13 +349,13 @@ public static void hmset(List nodeList, String namespace, Map 0) { - jedis.expire(namespace, time); + jedis.expire(key, time); } } catch (Exception e) { - Logs.REDIS.error("redis hmset exception:{},{},{}", namespace, time, node, e); + Logs.REDIS.error("redis hmset exception:{},{},{}", key, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -364,8 +364,8 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String namespace, Map hash) { - hmset(nodeList, namespace, hash, null); + public static void hmset(List nodeList, String key, Map hash) { + hmset(nodeList, key, hash, null); } /********************* hash redis end ********************************/ diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 3ed187e4..b4b82e8a 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -21,7 +21,7 @@ public final class RedisKey { - private static final String USER_PREFIX = "mp_u_"; + private static final String USER_PREFIX = "mp_uc_"; private static final String SESSION_PREFIX = "mp_s_"; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java index 014bfade..f74f1c27 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -49,12 +49,7 @@ public void onMessage(final String channel, final String message) { return; } for (final MessageListener listener : listeners) { - executor.execute(new Runnable() { - @Override - public void run() { - listener.onMessage(channel, message); - } - }); + executor.execute(() -> listener.onMessage(channel, message)); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index f0cc71de..50c10e21 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -95,39 +95,39 @@ public void del(String key) { /********************* * hash redis start ********************************/ - public void hset(String namespace, String key, String value) { + public void hset(String key, String field, String value) { - List nodeList = clusterManager.hashSet(key); - RedisClient.hset(nodeList, namespace, key, value); + List nodeList = clusterManager.hashSet(field); + RedisClient.hset(nodeList, key, field, value); } - public void hset(String namespace, String key, T value) { - hset(namespace, key, Jsons.toJson(value)); + public void hset(String key, String field, T value) { + hset(key, field, Jsons.toJson(value)); } - public T hget(String namespace, String key, Class clazz) { + public T hget(String key, String field, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(namespace); - return RedisClient.hget(node, namespace, key, clazz); + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hget(node, key, field, clazz); } - public void hdel(String namespace, String key) { - List nodeList = clusterManager.hashSet(namespace); - RedisClient.hdel(nodeList, namespace, key); + public void hdel(String key, String field) { + List nodeList = clusterManager.hashSet(key); + RedisClient.hdel(nodeList, key, field); } - public Map hgetAll(String namespace) { + public Map hgetAll(String key) { - RedisServer node = clusterManager.randomGetRedisNode(namespace); - return RedisClient.hgetAll(node, namespace); + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hgetAll(node, key); } - public Map hgetAll(String namespace, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(namespace); - return RedisClient.hgetAll(node, namespace, clazz); + public Map hgetAll(String key, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hgetAll(node, key, clazz); } /** @@ -135,21 +135,21 @@ public Map hgetAll(String namespace, Class clazz) { * * @return */ - public Set hkeys(String namespace) { - RedisServer node = clusterManager.randomGetRedisNode(namespace); - return RedisClient.hkeys(node, namespace); + public Set hkeys(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hkeys(node, key); } /** * 返回 key 指定的哈希集中指定字段的值 * - * @param key + * @param fields * @param clazz * @return */ - public List hmget(String namespace, Class clazz, String... key) { - RedisServer node = clusterManager.randomGetRedisNode(namespace); - return RedisClient.hmget(node, namespace, clazz, key); + public List hmget(String key, Class clazz, String... fields) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hmget(node, key, clazz, fields); } /** @@ -158,13 +158,13 @@ public List hmget(String namespace, Class clazz, String... key) { * @param hash * @param time */ - public void hmset(String namespace, Map hash, Integer time) { - List nodeList = clusterManager.hashSet(namespace); - RedisClient.hmset(nodeList, namespace, hash, time); + public void hmset(String key, Map hash, Integer time) { + List nodeList = clusterManager.hashSet(key); + RedisClient.hmset(nodeList, key, hash, time); } - public void hmset(String namespace, Map hash) { - hmset(namespace, hash, null); + public void hmset(String key, Map hash) { + hmset(key, hash, null); } diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 5f5c7cc7..7daaf8ba 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -130,7 +130,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); connection.init(ctx.channel(), true); - tryFastConnect(); + handshake(clientConfig); } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index ce3502d1..e053fbd0 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -19,7 +19,6 @@ package com.mpush.client.push; -import com.google.common.base.Strings; import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.push.PushSender; @@ -27,43 +26,60 @@ import com.mpush.api.service.Listener; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.gateway.GatewayClientFactory; +import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.RemoteRouter; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; import java.util.Collection; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.FutureTask; import static com.mpush.zk.ZKPath.GATEWAY_SERVER; /*package*/ class PushClient extends BaseService implements PushSender { private static final int DEFAULT_TIMEOUT = 3000; private final GatewayClientFactory factory = GatewayClientFactory.I; + private final ConnectionRouterManager routerManager = ConnectionRouterManager.I; public void send(String content, Collection userIds, Callback callback) { send(content.getBytes(Constants.UTF_8), userIds, callback); } @Override - public void send(String content, String userId, Callback callback) { - send(content.getBytes(Constants.UTF_8), userId, callback); + public FutureTask send(String content, String userId, Callback callback) { + return send(content.getBytes(Constants.UTF_8), userId, callback); } @Override public void send(byte[] content, Collection userIds, Callback callback) { - if (content == null || content.length == 0) return; for (String userId : userIds) { send(content, userId, callback); } } @Override - public void send(byte[] content, String userId, Callback callback) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); + public FutureTask send(byte[] content, String userId, Callback callback) { + Set routers = routerManager.lookupAll(userId); + if (routers == null || routers.isEmpty()) { + return PushRequest + .build(this) + .setCallback(callback) + .offline(); + + } + FutureTask task = null; + for (RemoteRouter router : routers) { + task = PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(router); + } + return task; } public Connection getGatewayConnection(String host) { diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 37fffdcf..3df50970 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -26,11 +26,10 @@ import com.mpush.common.router.ConnectionRouterManager; import com.mpush.common.router.RemoteRouter; import com.mpush.tools.common.TimeLine; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -41,13 +40,13 @@ * * @author ohun@live.cn */ -public class PushRequest extends FutureTask implements Runnable { +public class PushRequest extends FutureTask { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - private enum Status {init, success, failure, offline, timeout} - private static final Callable NONE = () -> Boolean.FALSE; + private enum Status {init, success, failure, offline, timeout} + private final AtomicReference status = new AtomicReference<>(Status.init); private final TimeLine timeLine = new TimeLine("Push-Time-Line"); @@ -60,11 +59,9 @@ private enum Status {init, success, failure, offline, timeout} private ClientLocation location; private Future future; - private void sendToConnServer() { + private void sendToConnServer(RemoteRouter router) { timeLine.addTimePoint("lookup-remote"); - //1.查询用户长连接所在的机器 - RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); if (router == null) { //1.1没有查到说明用户已经下线 offline(); @@ -85,18 +82,15 @@ private void sendToConnServer() { timeLine.addTimePoint("send-to-gateway-begin"); - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, location.getClientType(), content, gatewayConn); timeLine.addTimePoint("put-request-bus"); future = PushRequestBus.I.put(pushMessage.getSessionId(), this); - pushMessage.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - timeLine.addTimePoint("send-to-gateway-end"); - if (future.isSuccess()) { - } else { - failure(); - } + pushMessage.sendRaw(f -> { + timeLine.addTimePoint("send-to-gateway-end"); + if (f.isSuccess()) { + } else { + failure(); } }); } @@ -109,6 +103,7 @@ private void submit(Status status) { } else { LOGGER.warn("callback is null"); } + super.set(this.status.get() == Status.success); } timeLine.end(); LOGGER.info("push request {} end, userId={}, content={}, location={}, timeLine={}" @@ -119,16 +114,16 @@ private void submit(Status status) { public void run() { switch (status.get()) { case success: - callback.onSuccess(userId); + callback.onSuccess(userId, location); break; case failure: - callback.onFailure(userId); + callback.onFailure(userId, location); break; case offline: - callback.onOffline(userId); + callback.onOffline(userId, location); break; case timeout: - callback.onTimeout(userId); + callback.onTimeout(userId, location); break; case init://从定时任务过来的,超时时间到了 submit(Status.timeout); @@ -141,22 +136,26 @@ public boolean cancel(boolean mayInterruptIfRunning) { throw new UnsupportedOperationException(); } - public void send() { + public FutureTask send(RemoteRouter router) { timeLine.begin(); - sendToConnServer(); + sendToConnServer(router); + return this; } public void redirect() { timeLine.addTimePoint("redirect"); LOGGER.warn("user route has changed, userId={}, location={}", userId, location); - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); + ConnectionRouterManager.I.invalidateLocalCache(userId); if (status.get() == Status.init) {//表示任务还没有完成,还可以重新发送 - send(); + RemoteRouter route = ConnectionRouterManager.I.lookup(userId, location.getClientType()); + send(route); } } - public long getTimeout() { - return timeout; + public FutureTask offline() { + ConnectionRouterManager.I.invalidateLocalCache(userId); + submit(Status.offline); + return this; } public void timeout() { @@ -171,9 +170,8 @@ public void failure() { submit(Status.failure); } - public void offline() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - submit(Status.offline); + public long getTimeout() { + return timeout; } public PushRequest(PushClient client) { diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index dc8b50b1..9126e3aa 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -36,6 +36,7 @@ public final class ErrorMessage extends ByteBufMessage { public byte cmd; public byte code; public String reason; + public String data; public ErrorMessage(byte cmd, Packet message, Connection connection) { super(message, connection); @@ -51,6 +52,7 @@ public void decode(ByteBuf body) { cmd = decodeByte(body); code = decodeByte(body); reason = decodeString(body); + data = decodeString(body); } @Override @@ -58,6 +60,7 @@ public void encode(ByteBuf body) { encodeByte(body, cmd); encodeByte(body, code); encodeString(body, reason); + encodeString(body, data); } public static ErrorMessage from(BaseMessage src) { @@ -75,6 +78,11 @@ public ErrorMessage setReason(String reason) { return this; } + public ErrorMessage setData(String data) { + this.data = data; + return this; + } + public ErrorMessage setErrorCode(ErrorCode code) { this.code = code.errorCode; this.reason = code.errorMsg; diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 61320593..ead4f160 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -34,11 +34,13 @@ */ public class GatewayPushMessage extends ByteBufMessage { public String userId; + public int clientType; public byte[] content; - public GatewayPushMessage(String userId, byte[] content, Connection connection) { + public GatewayPushMessage(String userId, int clientType, byte[] content, Connection connection) { super(new Packet(GATEWAY_PUSH, genSessionId()), connection); this.userId = userId; + this.clientType = clientType; this.content = content; } @@ -49,12 +51,14 @@ public GatewayPushMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { userId = decodeString(body); + clientType = decodeInt(body); content = decodeBytes(body); } @Override public void encode(ByteBuf body) { encodeString(body, userId); + encodeInt(body, clientType); encodeBytes(body, content); } @@ -72,6 +76,7 @@ public void send(ChannelFutureListener listener) { public String toString() { return "GatewayPushMessage{" + "userId='" + userId + '\'' + + "clientType='" + clientType + '\'' + ", content='" + content.length + '\'' + '}'; } diff --git a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java similarity index 75% rename from mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java rename to mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index 3c55a211..f1ad04ea 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/DnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -23,9 +23,10 @@ import com.google.common.collect.Maps; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; -import com.mpush.tools.config.data.DnsMapping; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,30 +38,32 @@ import static com.mpush.tools.Utils.checkHealth; -public class DnsMappingManager extends BaseService implements Runnable { - private final Logger logger = LoggerFactory.getLogger(DnsMappingManager.class); +public class HttpProxyDnsMappingManager extends BaseService implements DnsMappingManager, Runnable { + private final Logger logger = LoggerFactory.getLogger(HttpProxyDnsMappingManager.class); - public static final DnsMappingManager I = new DnsMappingManager(); - - private DnsMappingManager() { + public HttpProxyDnsMappingManager() { } private final Map> all = Maps.newConcurrentMap(); private Map> available = Maps.newConcurrentMap(); - private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + private ScheduledExecutorService executorService; @Override protected void doStart(Listener listener) throws Throwable { - scheduledExecutorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + if (all.size() > 0) { + executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + } } @Override protected void doStop(Listener listener) throws Throwable { - scheduledExecutorService.shutdown(); + if (executorService != null) { + executorService.shutdown(); + } } - @Override public void init() { logger.error("start init dnsMapping"); @@ -71,7 +74,7 @@ public void init() { @Override public boolean isRunning() { - return !scheduledExecutorService.isShutdown(); + return executorService != null && !executorService.isShutdown(); } public void update(Map> nowAvailable) { @@ -83,21 +86,18 @@ public Map> getAll() { } public DnsMapping lookup(String origin) { - if (available.isEmpty()) - return null; + if (available.isEmpty()) return null; List list = available.get(origin); - if (list == null || list.isEmpty()) - return null; + if (list == null || list.isEmpty()) return null; int L = list.size(); - if (L == 1) - return list.get(0); + if (L == 1) return list.get(0); return list.get((int) (Math.random() * L % L)); } @Override public void run() { logger.debug("start dns mapping checkHealth"); - Map> all = I.getAll(); + Map> all = this.getAll(); Map> available = Maps.newConcurrentMap(); for (Map.Entry> entry : all.entrySet()) { String key = entry.getKey(); @@ -113,6 +113,6 @@ public void run() { } available.put(key, nowValue); } - I.update(available); + this.update(available); } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java index f203baf0..fec83fde 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java @@ -21,32 +21,45 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.mpush.api.router.ClientType; +import java.util.Set; import java.util.concurrent.TimeUnit; /** * Created by ohun on 2016/1/4. */ public final class ConnectionRouterManager extends RemoteRouterManager { - public static final ConnectionRouterManager INSTANCE = new ConnectionRouterManager(); + public static final ConnectionRouterManager I = new ConnectionRouterManager(); // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 - private final Cache cache = CacheBuilder + private final Cache> cache = CacheBuilder .newBuilder() .expireAfterWrite(5, TimeUnit.MINUTES) .expireAfterAccess(5, TimeUnit.MINUTES) .build(); @Override - public RemoteRouter lookup(String userId) { - RemoteRouter cached = cache.getIfPresent(userId); + public Set lookupAll(String userId) { + Set cached = cache.getIfPresent(userId); if (cached != null) return cached; - RemoteRouter router = super.lookup(userId); + Set router = super.lookupAll(userId); if (router != null) { cache.put(userId, router); } return router; } + @Override + public RemoteRouter lookup(String userId, int clientType) { + Set cached = this.lookupAll(userId); + for (RemoteRouter router : cached) { + if (router.getRouteValue().getClientType() == clientType) { + return router; + } + } + return null; + } + /** * 如果推送失败,可能是缓存不一致了,可以让本地缓存失效 *

@@ -55,6 +68,6 @@ public RemoteRouter lookup(String userId) { * @param userId */ public void invalidateLocalCache(String userId) { - cache.invalidate(userId); + if (userId != null) cache.invalidate(userId); } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index fa36618e..ef71818d 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -19,43 +19,95 @@ package com.mpush.common.router; +import com.google.common.eventbus.Subscribe; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + /** * Created by ohun on 2015/12/23. * * @author ohun@live.cn */ -public class RemoteRouterManager implements RouterManager { +public class RemoteRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(RemoteRouterManager.class); @Override public RemoteRouter register(String userId, RemoteRouter router) { LOGGER.info("register remote router success userId={}, router={}", userId, router); String key = RedisKey.getUserKey(userId); - RemoteRouter old = RedisManager.I.get(key, RemoteRouter.class); + String field = Integer.toString(router.getRouteValue().getClientType()); + ClientLocation old = RedisManager.I.hget(key, field, ClientLocation.class); if (old != null) { - RedisManager.I.del(key); + RedisManager.I.hdel(key, field); } - RedisManager.I.set(key, router); - return old; + RedisManager.I.hset(key, field, router.getRouteValue()); + return old == null ? null : new RemoteRouter(old); } @Override - public boolean unRegister(String userId) { + public boolean unRegister(String userId, int clientType) { String key = RedisKey.getUserKey(userId); - RedisManager.I.del(key); - LOGGER.info("unRegister remote router success userId={}", userId); + String field = Integer.toString(clientType); + RedisManager.I.hdel(key, field); + LOGGER.info("unRegister remote router success userId={}, clientType={}", userId, clientType); return true; } @Override - public RemoteRouter lookup(String userId) { + public Set lookupAll(String userId) { + String key = RedisKey.getUserKey(userId); + Map values = RedisManager.I.hgetAll(key, ClientLocation.class); + if (values == null || values.isEmpty()) return Collections.emptySet(); + return values.values().stream().map(RemoteRouter::new).collect(Collectors.toSet()); + } + + @Override + public RemoteRouter lookup(String userId, int clientType) { String key = RedisKey.getUserKey(userId); - return RedisManager.I.get(key, RemoteRouter.class); + String field = Integer.toString(clientType); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + LOGGER.info("lookup remote router userId={}, router={}", userId, location); + return location == null ? null : new RemoteRouter(location); + } + + /** + * 监听链接关闭事件,清理失效的路由 + * + * @param event + */ + @Subscribe + void on(ConnectionCloseEvent event) { + Connection connection = event.connection; + if (connection == null) return; + SessionContext context = connection.getSessionContext(); + String userId = context.userId; + if (userId == null) return; + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(context.getClientType()); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + if (location == null) return; + + String connId = connection.getId(); + //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 + if (connId.equals(location.getConnId())) { + RedisManager.I.hdel(key, field); + LOGGER.info("clean disconnected remote route, userId={}, route={}", userId, location); + } else { + LOGGER.info("clean disconnected remote route, not clean:userId={}, route={}", userId, location); + } } } diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager new file mode 100644 index 00000000..d0dd9b36 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager @@ -0,0 +1 @@ +com.mpush.common.net.HttpProxyDnsMappingManager \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 82a4981a..b8858281 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -20,6 +20,7 @@ package com.mpush.core.handler; import com.google.common.base.Strings; +import com.mpush.api.push.PushSender; import com.mpush.api.service.Listener; import com.mpush.common.router.RemoteRouter; import com.mpush.common.user.UserManager; @@ -38,8 +39,11 @@ import org.slf4j.LoggerFactory; import java.io.Serializable; +import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; @ChannelHandler.Sharable public final class AdminHandler extends SimpleChannelInboundHandler { @@ -58,15 +62,19 @@ public AdminHandler(AdminServer adminServer) { @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { Command command = Command.help; - String args = null; + String arg = null; + String[] args = null; if (request != null) { String[] cmd_args = request.split(" "); command = Command.toCmd(cmd_args[0].trim()); if (cmd_args.length == 2) { - args = cmd_args[1]; + arg = cmd_args[1]; + } else if (cmd_args.length > 2) { + args = Arrays.copyOfRange(cmd_args, 1, cmd_args.length - 1); } } - Object result = command.handler(ctx, args); + + Object result = args != null ? command.handler(ctx, args) : command.handler(ctx, arg); ChannelFuture future = ctx.writeAndFlush(result + EOL + EOL); if (command == Command.quit) { future.addListener(ChannelFutureListener.CLOSE); @@ -99,6 +107,7 @@ public String handler(ChannelHandlerContext ctx, String args) { buf.append("zk: query zk node" + EOL); buf.append("count: count conn num or online user count" + EOL); buf.append("route: show user route info" + EOL); + buf.append("push:, push test msg to client" + EOL); buf.append("conf:[key] show config info" + EOL); buf.append("monitor:[mxBean] show system monitor" + EOL); return buf.toString(); @@ -178,9 +187,17 @@ public Serializable handler(ChannelHandlerContext ctx, String args) { @Override public String handler(ChannelHandlerContext ctx, String args) { if (Strings.isNullOrEmpty(args)) return "please input userId"; - RemoteRouter router = RouterCenter.INSTANCE.getRemoteRouterManager().lookup(args); - if (router == null) return "user [" + args + "] offline now."; - return router.getRouteValue().toString(); + Set routers = RouterCenter.I.getRemoteRouterManager().lookupAll(args); + if (routers.isEmpty()) return "user [" + args + "] offline now."; + return Jsons.toJson(routers); + } + }, + push { + @Override + public String handler(ChannelHandlerContext ctx, String... args) throws Exception { + Boolean success = PushSender.create().send(args[1], args[0], null).get(5, TimeUnit.SECONDS); + + return success.toString(); } }, conf { @@ -222,7 +239,13 @@ public String handler(ChannelHandlerContext ctx, String args) { } }; - public abstract Object handler(ChannelHandlerContext ctx, String args); + public Object handler(ChannelHandlerContext ctx, String... args) throws Exception { + return "unsupported"; + } + + public Object handler(ChannelHandlerContext ctx, String args) throws Exception { + return "unsupported"; + } public static Command toCmd(String cmd) { try { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index b4df42d5..b02c3adf 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -55,16 +55,15 @@ public void handle(BindUserMessage message) { SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 - boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); + boolean success = RouterCenter.I.register(message.userId, message.getConnection()); if (success) { - + context.userId = message.userId; EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); - OkMessage.from(message).setData("bind success").send(); Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 - RouterCenter.INSTANCE.unRegister(message.userId); + RouterCenter.I.unRegister(message.userId, context.getClientType()); ErrorMessage.from(message).setReason("bind failed").close(); Logs.Conn.info("bind user failure, userId={}, session={}", message.userId, context); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index d5c3171f..dc7cfe78 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -31,8 +31,6 @@ import com.mpush.core.router.RouterCenter; import com.mpush.tools.Utils; import com.mpush.tools.log.Logs; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import static com.mpush.common.ErrorCode.*; @@ -81,7 +79,9 @@ public void handle(GatewayPushMessage message) { * @return */ private boolean checkLocal(final GatewayPushMessage message) { - LocalRouter router = RouterCenter.INSTANCE.getLocalRouterManager().lookup(message.userId); + String userId = message.userId; + int deviceId = message.clientType; + LocalRouter router = RouterCenter.I.getLocalRouterManager().lookup(userId, deviceId); //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 if (router == null) return false; @@ -91,10 +91,10 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - Logs.PUSH.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection); //删除已经失效的本地路由 - RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); + RouterCenter.I.getLocalRouterManager().unRegister(userId, deviceId); return false; } @@ -102,21 +102,18 @@ private boolean checkLocal(final GatewayPushMessage message) { //3.链接可用,直接下发消息到手机客户端 PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - //推送成功 - OkMessage.from(message).setData(message.userId).send(); + pushMessage.send(future -> { + if (future.isSuccess()) { + //推送成功 + OkMessage.from(message).setData(userId + ',' + deviceId).send(); - Logs.PUSH.info("gateway push message to client success userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push message to client success, message={}", message); - } else { - //推送失败 - ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); + } else { + //推送失败 + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + deviceId).send(); - Logs.PUSH.info("gateway push message to client failure userId={}, content={}", message.userId, message.content); - } + Logs.PUSH.info("gateway push message to client failure, message={}", message); } }); return true; @@ -131,14 +128,16 @@ public void operationComplete(ChannelFuture future) throws Exception { * @param message */ private void checkRemote(GatewayPushMessage message) { - RemoteRouter router = RouterCenter.INSTANCE.getRemoteRouterManager().lookup(message.userId); + String userId = message.userId; + int clientType = message.clientType; + RemoteRouter router = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); // 1.如果远程路由信息也不存在, 说明用户此时不在线, if (router == null) { - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send(); - Logs.PUSH.info("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push, router not exists user offline, message={}", message); return; } @@ -146,20 +145,20 @@ private void checkRemote(GatewayPushMessage message) { //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 if (Utils.getLocalIp().equals(router.getRouteValue().getHost())) { - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send(); //删除失效的远程缓存 - RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); + RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); - Logs.PUSH.info("gateway push error remote is local, userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, router); return; } //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 - ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).send(); - Logs.PUSH.info("gateway push, router in remote userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, router); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index cbb9ce9d..efc014e2 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -22,15 +22,18 @@ import com.google.common.base.Strings; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.common.net.DnsMappingManager; +import com.mpush.common.net.HttpProxyDnsMappingManager; import com.mpush.netty.http.HttpCallback; import com.mpush.netty.http.HttpClient; import com.mpush.netty.http.RequestContext; import com.mpush.tools.common.Profiler; -import com.mpush.tools.config.data.DnsMapping; +import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; @@ -52,6 +55,7 @@ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = Logs.HTTP; private final HttpClient httpClient; + private final DnsMappingManager dnsMappingManager = SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager); public HttpProxyHandler(HttpClient httpClient) { this.httpClient = httpClient; @@ -201,7 +205,7 @@ private String doDnsMapping(String url) { return url; } String host = uri.getHost(); - DnsMapping mapping = DnsMappingManager.I.lookup(host); + DnsMapping mapping = dnsMappingManager.lookup(host); if (mapping == null) { return url; } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index a655051c..ad8739e8 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -68,33 +68,36 @@ public void handle(BindUserMessage message) { if (context.handshakeOk()) { //2.先删除远程路由, 必须是同一个设备才允许解绑 boolean unRegisterSuccess = true; - RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); - RemoteRouter remoteRouter = remoteRouterManager.lookup(message.userId); + int clientType = context.getClientType(); + String userId = message.userId; + RemoteRouterManager remoteRouterManager = RouterCenter.I.getRemoteRouterManager(); + RemoteRouter remoteRouter = remoteRouterManager.lookup(userId, clientType); if (remoteRouter != null) { String deviceId = remoteRouter.getRouteValue().getDeviceId(); if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = remoteRouterManager.unRegister(message.userId); + unRegisterSuccess = remoteRouterManager.unRegister(userId, clientType); } } //3.删除本地路由信息 - LocalRouterManager localRouterManager = RouterCenter.INSTANCE.getLocalRouterManager(); - LocalRouter localRouter = localRouterManager.lookup(message.userId); + LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter localRouter = localRouterManager.lookup(userId, clientType); if (localRouter != null) { String deviceId = localRouter.getRouteValue().getSessionContext().deviceId; if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = localRouterManager.unRegister(message.userId) && unRegisterSuccess; + unRegisterSuccess = localRouterManager.unRegister(userId, clientType) && unRegisterSuccess; } } //4.路由删除成功,广播用户下线事件 if (unRegisterSuccess) { - EventBus.I.post(new UserOfflineEvent(message.getConnection(), message.userId)); + context.userId = null; + EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").send(); - Logs.Conn.info("unbind user success, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").send(); - Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index 9db92bcb..f670f428 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -19,6 +19,8 @@ package com.mpush.core.router; +import com.mpush.api.router.ClientType; + /** * Created by ohun on 2016/1/4. * @@ -27,10 +29,16 @@ public final class KickRemoteMsg { public String userId; public String deviceId; + public int clientType; public String targetServer; @Override public String toString() { - return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + ", targetServer='" + targetServer + '\'' + '}'; + return "KickRemoteMsg{" + + "userId='" + userId + '\'' + + ", deviceId='" + deviceId + '\'' + + ", clientType='" + clientType + '\'' + + ", targetServer='" + targetServer + '\'' + + '}'; } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index c1695986..43130d9b 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -20,6 +20,7 @@ package com.mpush.core.router; import com.mpush.api.connection.Connection; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; /** @@ -29,9 +30,15 @@ */ public final class LocalRouter implements Router { private final Connection connection; + private final int clientType; public LocalRouter(Connection connection) { this.connection = connection; + this.clientType = connection.getSessionContext().getClientType(); + } + + public int getClientType() { + return clientType; } @Override @@ -44,6 +51,22 @@ public RouterType getRouteType() { return RouterType.LOCAL; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + LocalRouter that = (LocalRouter) o; + + return clientType == that.clientType; + + } + + @Override + public int hashCode() { + return Integer.hashCode(clientType); + } + @Override public String toString() { return "LocalRouter{" + connection + '}'; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 2b14f9c7..57de36a4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -21,8 +21,10 @@ import com.google.common.eventbus.Subscribe; import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.event.UserOfflineEvent; +import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; import com.mpush.tools.event.EventBus; import com.mpush.tools.event.EventConsumer; @@ -30,7 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; +import java.util.*; /** * Created by ohun on 2015/12/23. @@ -39,45 +41,37 @@ */ public final class LocalRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); + private static final Map EMPTY = Collections.unmodifiableMap(new HashMap<>(0)); /** * 本地路由表 */ - private final Map routers = new ConcurrentHashMapV8<>(); - - /** - * 反向关系表 - */ - private final Map connIdUserIds = new ConcurrentHashMapV8<>(); + private final Map> routers = new ConcurrentHashMapV8<>(); @Override public LocalRouter register(String userId, LocalRouter router) { LOGGER.info("register local router success userId={}, router={}", userId, router); - connIdUserIds.put(router.getRouteValue().getId(), userId); - //add online userId - return routers.put(userId, router); + return routers.computeIfAbsent(userId, s -> new HashMap<>()).put(router.getClientType(), router); } @Override - public boolean unRegister(String userId) { - LocalRouter router = routers.remove(userId); - if (router != null) { - connIdUserIds.remove(router.getRouteValue().getId()); - } + public boolean unRegister(String userId, int clientType) { + LocalRouter router = routers.getOrDefault(userId, EMPTY).remove(clientType); LOGGER.info("unRegister local router success userId={}, router={}", userId, router); return true; } @Override - public LocalRouter lookup(String userId) { - LocalRouter router = routers.get(userId); - LOGGER.info("lookup local router userId={}, router={}", userId, router); - return router; + public Set lookupAll(String userId) { + return new HashSet<>(routers.getOrDefault(userId, EMPTY).values()); } - public String getUserIdByConnId(String connId) { - return connIdUserIds.get(connId); + @Override + public LocalRouter lookup(String userId, int clientType) { + LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); + LOGGER.info("lookup local router userId={}, router={}", userId, router); + return router; } /** @@ -89,21 +83,22 @@ public String getUserIdByConnId(String connId) { void on(ConnectionCloseEvent event) { Connection connection = event.connection; if (connection == null) return; - String id = event.connection.getId(); + SessionContext context = connection.getSessionContext(); - //1.清除反向关系 - String userId = connIdUserIds.remove(id); + String userId = context.userId; if (userId == null) return; + EventBus.I.post(new UserOfflineEvent(event.connection, userId)); - LocalRouter router = routers.get(userId); + int clientType = context.getClientType(); + LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); if (router == null) return; + String connId = connection.getId(); //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 - if (id.equals(router.getRouteValue().getId())) { + if (connId.equals(router.getRouteValue().getId())) { //3.删除路由 - routers.remove(userId); + routers.getOrDefault(userId, EMPTY).remove(clientType); LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); - } else { //如果不相等,则log一下 LOGGER.info("clean disconnected local route, not clean:userId={}, route={}", userId, router); } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index 31c18ad4..0140d2a4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -22,6 +22,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; @@ -30,6 +31,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Set; + /** * Created by ohun on 2015/12/23. * @@ -37,7 +40,7 @@ */ public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); - public static final RouterCenter INSTANCE = new RouterCenter(); + public static final RouterCenter I = new RouterCenter(); private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); @@ -54,7 +57,7 @@ public final class RouterCenter { */ public boolean register(String userId, Connection connection) { ClientLocation location = ClientLocation - .from(connection.getSessionContext()) + .from(connection) .setHost(Utils.getLocalIp()); LocalRouter localRouter = new LocalRouter(connection); @@ -65,7 +68,6 @@ public boolean register(String userId, Connection connection) { try { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); - } catch (Exception e) { LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); } @@ -82,19 +84,25 @@ public boolean register(String userId, Connection connection) { return true; } - public boolean unRegister(String userId) { - localRouterManager.unRegister(userId); - remoteRouterManager.unRegister(userId); + public boolean unRegister(String userId, int clientType) { + localRouterManager.unRegister(userId, clientType); + remoteRouterManager.unRegister(userId, clientType); return true; } - public Router lookup(String userId) { - LocalRouter local = localRouterManager.lookup(userId); + public Router lookup(String userId, int clientType) { + LocalRouter local = localRouterManager.lookup(userId, clientType); if (local != null) return local; - RemoteRouter remote = remoteRouterManager.lookup(userId); + RemoteRouter remote = remoteRouterManager.lookup(userId, clientType); return remote; } + public Set> lookupAll(String userId) { + Set locals = localRouterManager.lookupAll(userId); + if (locals != null) return locals; + Set remotes = remoteRouterManager.lookupAll(userId); + return remotes; + } public LocalRouterManager getLocalRouterManager() { return localRouterManager; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index 899bf871..e0f5b61f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -24,6 +24,7 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.listener.ListenerDispatcher; @@ -36,8 +37,6 @@ import com.mpush.tools.config.ConfigManager; import com.mpush.tools.event.EventConsumer; import com.mpush.tools.log.Logs; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; /** * Created by ohun on 2016/1/4. @@ -83,14 +82,11 @@ public void kickLocal(final String userId, final LocalRouter router) { KickUserMessage message = new KickUserMessage(connection); message.deviceId = context.deviceId; message.userId = userId; - message.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); - } else { - Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); - } + message.send(future -> { + if (future.isSuccess()) { + Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); + } else { + Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); } }); } @@ -116,6 +112,7 @@ public void kickRemote(String userId, RemoteRouter router) { //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); + msg.clientType = location.getClientType(); msg.targetServer = location.getHost(); msg.userId = userId; RedisManager.I.publish(getKickChannel(msg.targetServer), msg); @@ -138,12 +135,13 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //2.查询本地路由,找到要被踢下线的链接,并删除该本地路由 String userId = msg.userId; - LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); - LocalRouter router = routerManager.lookup(userId); + int clientType = msg.clientType; + LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter router = routerManager.lookup(userId, clientType); if (router != null) { Logs.Conn.info("receive kick remote msg, msg={}", msg); //2.1删除本地路由信息 - routerManager.unRegister(userId); + routerManager.unRegister(userId, clientType); //2.2发送踢人消息到客户端 kickLocal(userId, router); remStatUser(userId); diff --git a/mpush-monitor/src/main/java/com/mpush/App.java b/mpush-monitor/src/main/java/com/mpush/App.java deleted file mode 100644 index 80d28238..00000000 --- a/mpush-monitor/src/main/java/com/mpush/App.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.mpush; - -/** - * Hello world! - */ -public class App { - public static void main(String[] args) { - System.out.println("Hello World!"); - } -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 1f084740..a963c00c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -85,7 +85,6 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { } } else { return this.close(); - } } diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java new file mode 100644 index 00000000..5abd1cc5 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java @@ -0,0 +1,71 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain2 { + + public static void main(String[] args) throws Exception { + Logs.init(); + ConnectClientBoot main = new ConnectClientBoot(); + main.run(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); + + ClientConfig config = new ClientConfig(); + config.setClientKey(CipherBox.I.randomAESKey()); + config.setIv(CipherBox.I.randomAESIV()); + config.setClientVersion("1.0.0"); + config.setDeviceId("android-device-id-1"); + config.setOsName("android"); + config.setOsVersion("1.0.1"); + config.setUserId("user-0"); + Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + + config = new ClientConfig(); + config.setClientKey(CipherBox.I.randomAESKey()); + config.setIv(CipherBox.I.randomAESIV()); + config.setClientVersion("1.0.0"); + config.setDeviceId("pc-device-id-2"); + config.setOsName("pc"); + config.setOsVersion("1.0.1"); + config.setUserId("user-0"); + client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java index e8b5f3d1..806ac033 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java @@ -19,7 +19,7 @@ package com.mpush.test.gson; -import com.mpush.tools.config.data.DnsMapping; +import com.mpush.api.spi.net.DnsMapping; import org.junit.Test; import java.net.MalformedURLException; diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index cd29314e..29b4b7bd 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -22,6 +22,7 @@ import com.mpush.api.push.PushContent; import com.mpush.api.push.PushContent.PushType; import com.mpush.api.push.PushSender; +import com.mpush.api.router.ClientLocation; import com.mpush.tools.Jsons; import com.mpush.tools.log.Logs; @@ -38,28 +39,28 @@ public static void main(String[] args) throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().get(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 1; i++) { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); Thread.sleep(1000); - sender.send(Jsons.toJson(content), Arrays.asList("user_1"), new PushSender.Callback() { + sender.send(Jsons.toJson(content), Arrays.asList("user-0"), new PushSender.Callback() { @Override - public void onSuccess(String userId) { + public void onSuccess(String userId, ClientLocation location) { System.err.println("push onSuccess userId=" + userId); } @Override - public void onFailure(String userId) { + public void onFailure(String userId, ClientLocation location) { System.err.println("push onFailure userId=" + userId); } @Override - public void onOffline(String userId) { + public void onOffline(String userId, ClientLocation location) { System.err.println("push onOffline userId=" + userId); } @Override - public void onTimeout(String userId) { + public void onTimeout(String userId, ClientLocation location) { System.err.println("push onTimeout userId=" + userId); } }); diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index bf20ea92..2f597c11 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -19,7 +19,7 @@ package com.mpush.tools.config; -import com.mpush.tools.config.data.DnsMapping; +import com.mpush.api.spi.net.DnsMapping; import com.mpush.tools.config.data.RedisGroup; import com.mpush.tools.config.data.RedisServer; import com.typesafe.config.Config; @@ -339,6 +339,7 @@ interface monitor { interface spi { Config cfg = mp.cfg.getObject("spi").toConfig(); String thread_pool_factory = cfg.getString("thread-pool-factory"); + String dns_mapping_manager = cfg.getString("dns-mapping-manager"); } } } \ No newline at end of file From 5d04dc4d7a31d5cc73283bbfc9f6f3736b0456a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 20 Aug 2016 09:19:16 +0800 Subject: [PATCH 561/890] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=AB=AF=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/bootstrap/job/LastBoot.java | 4 +++- .../src/main/java/com/mpush/core/handler/AdminHandler.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index 5a1f5ea2..bc194877 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -32,6 +32,8 @@ public class LastBoot extends BootJob { public void run() { UserManager.I.clearUserOnlineData(); Logs.Console.error("end run bootstrap chain..."); - Logs.Console.error("mpush server start success..."); + Logs.Console.error("==================================================================="); + Logs.Console.error("====================MPUSH SERVER START SUCCESS====================="); + Logs.Console.error("==================================================================="); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index b8858281..039e4d4f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -70,7 +70,7 @@ protected void messageReceived(ChannelHandlerContext ctx, String request) throws if (cmd_args.length == 2) { arg = cmd_args[1]; } else if (cmd_args.length > 2) { - args = Arrays.copyOfRange(cmd_args, 1, cmd_args.length - 1); + args = Arrays.copyOfRange(cmd_args, 1, cmd_args.length); } } From 7a8d15964ec1c6c0d7f4c6e6514237a2adb752b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sat, 20 Aug 2016 13:03:46 +0800 Subject: [PATCH 562/890] init v2 --- .gitignore | 20 - bin/daily-pub.py | 214 -------- bin/debug.sh | 19 - bin/mp-env.cmd | 49 ++ bin/mp-env.sh | 122 +++++ bin/mp.cmd | 24 + bin/mp.sh | 232 +++++++++ bin/pub-daily.py | 239 --------- bin/pub-online.py | 239 --------- bin/pub-pre.py | 101 ---- bin/run.sh | 20 - bin/set-env.sh | 2 + bin/start.sh | 23 - bin/test.py | 69 --- bin/test.sh | 11 - conf/conf-daily.properties | 16 - conf/conf-dev.properties | 1 + conf/conf-online.properties | 15 - conf/conf-pre.properties | 15 - conf/conf-pub.properties | 1 + conf/reference.conf | 189 +++++++ mpush-api/.gitignore | 1 - .../src/main/java/com/mpush/api/Client.java | 39 -- .../main/java/com/mpush/api/Constants.java | 20 + .../src/main/java/com/mpush/api/Message.java | 19 + .../java/com/mpush/api/MessageHandler.java | 21 +- .../java/com/mpush/api/PacketReceiver.java | 21 +- .../main/java/com/mpush/api/PushContent.java | 76 --- .../main/java/com/mpush/api/PushSender.java | 24 - .../src/main/java/com/mpush/api/Server.java | 23 - .../java/com/mpush/api/connection/Cipher.java | 19 + .../com/mpush/api/connection/Connection.java | 28 +- .../api/connection/ConnectionManager.java | 29 +- .../mpush/api/connection/SessionContext.java | 48 +- .../mpush/api/event/ConnectionCloseEvent.java | 19 + .../main/java/com/mpush/api/event/Event.java | 19 + .../com/mpush/api/event/HandshakeEvent.java | 19 + .../com/mpush/api/event/KickUserEvent.java | 19 + .../mpush/api/event/RouterChangeEvent.java | 19 + .../com/mpush/api/event/UserOfflineEvent.java | 19 + .../com/mpush/api/event/UserOnlineEvent.java | 50 +- .../mpush/api/exception/CryptoException.java | 16 - .../mpush/api/exception/DecodeException.java | 12 - .../mpush/api/exception/MessageException.java | 11 - .../api/exception/SendMessageException.java | 9 - .../java/com/mpush/api/protocol/Command.java | 19 + .../java/com/mpush/api/protocol/Packet.java | 22 +- .../java/com/mpush/api/push/PushContent.java | 94 ++++ .../com/mpush/api/push/PushException.java | 39 ++ .../java/com/mpush/api/push/PushSender.java | 58 +++ .../com/mpush/api/router/ClientLocation.java | 96 +++- .../java/com/mpush/api/router/ClientType.java | 58 +++ .../java/com/mpush/api/router/Router.java | 19 + .../com/mpush/api/router/RouterManager.java | 35 +- .../com/mpush/api/service/BaseService.java | 151 ++++++ .../java/com/mpush/api/service/Client.java | 24 + .../java/com/mpush/api/service/Listener.java | 7 + .../java/com/mpush/api/service/Server.java | 29 ++ .../java/com/mpush/api/service/Service.java | 43 ++ .../mpush/api/service/ServiceException.java | 39 ++ .../main/java/com/mpush/api/spi/Factory.java | 29 ++ .../java/com/mpush/api/spi/SpiLoader.java | 28 +- .../mpush/api/spi/client/PusherFactory.java | 32 ++ .../api/spi/common/ThreadPoolFactory.java | 39 ++ .../com/mpush/api/spi/core/CipherFactory.java | 32 ++ .../com/mpush/api/spi/net/DnsMapping.java | 69 +++ .../mpush/api/spi/net/DnsMappingManager.java | 37 ++ mpush-boot/.gitignore | 1 - mpush-boot/assembly.xml | 40 +- mpush-boot/pom.xml | 10 +- mpush-boot/src/main/java/com/mpush/Main.java | 23 - .../main/java/com/mpush/ServerLauncher.java | 54 -- .../main/java/com/mpush/boot/BootChain.java | 34 -- .../src/main/java/com/mpush/boot/BootJob.java | 26 - .../java/com/mpush/boot/HttpProxyBoot.java | 19 - .../main/java/com/mpush/boot/LastBoot.java | 18 - .../main/java/com/mpush/boot/MonitorBoot.java | 17 - .../main/java/com/mpush/boot/RedisBoot.java | 46 -- .../main/java/com/mpush/boot/ServerBoot.java | 66 --- .../src/main/java/com/mpush/boot/ZKBoot.java | 45 -- .../com/mpush/bootstrap/BootException.java | 35 ++ .../main/java/com/mpush/bootstrap/Main.java | 42 ++ .../com/mpush/bootstrap/ServerLauncher.java | 77 +++ .../com/mpush/bootstrap/job/BootChain.java | 53 ++ .../java/com/mpush/bootstrap/job/BootJob.java | 45 ++ .../mpush/bootstrap/job/HttpProxyBoot.java | 40 ++ .../com/mpush/bootstrap/job/LastBoot.java | 39 ++ .../com/mpush/bootstrap/job/MonitorBoot.java | 35 ++ .../com/mpush/bootstrap/job/RedisBoot.java | 36 ++ .../com/mpush/bootstrap/job/ServerBoot.java | 76 +++ .../java/com/mpush/bootstrap/job/ZKBoot.java | 47 ++ .../src/main/resources/config.properties | 37 -- mpush-boot/src/main/resources/logback.xml | 374 +++++++------- mpush-boot/src/main/resources/mpush.conf | 4 + .../ConnectionServerApplicationTest.java | 13 - .../java/com/mpush/zk/ServerManageTest.java | 80 --- .../src/test/java/com/mpush/zk/ZkTest.java | 16 - .../test/java/com/mpush/zk/ZkUtilTest.java | 148 ------ mpush-cache/pom.xml | 27 + .../com/mpush/cache/redis/RedisClient.java | 303 +++++------ .../com/mpush/cache/redis/RedisException.java | 42 ++ .../com/mpush/cache/redis/RedisGroup.java | 69 +++ .../java/com/mpush/cache/redis}/RedisKey.java | 23 +- .../com/mpush/cache/redis/RedisServer.java | 31 ++ .../cache/redis/hash/ConsistentHash.java | 85 +++ .../java/com/mpush/cache/redis/hash/Node.java | 48 ++ .../redis/listener/ListenerDispatcher.java | 65 +++ .../cache/redis/listener/MessageListener.java | 27 + .../redis/manager/RedisClusterManager.java | 36 ++ .../cache/redis/manager/RedisManager.java | 318 ++++++++++++ .../redis/manager/ZKRedisClusterManager.java | 130 +++++ .../com/mpush/cache/redis/mq/Subscriber.java | 86 ++++ mpush-client/.gitignore | 1 - mpush-client/pom.xml | 34 +- .../mpush/client/connect/ClientConfig.java | 98 ++++ .../connect/ConnClientChannelHandler.java | 237 +++++++++ .../mpush/client/connect/ConnectClient.java | 47 ++ .../mpush/client/gateway/GatewayClient.java | 77 +++ .../gateway/GatewayClientChannelHandler.java | 120 +++++ .../client/gateway/GatewayClientFactory.java | 126 +++++ .../com/mpush/client/push/PushClient.java | 109 ++++ .../mpush/client/push/PushClientFactory.java | 42 ++ .../com/mpush/client/push/PushRequest.java | 215 ++++++++ .../com/mpush/client/push/PushRequestBus.java | 65 +++ .../conn/client/ClientChannelHandler.java | 223 -------- .../java/com/mpush/push/ConnectClient.java | 12 - .../main/java/com/mpush/push/PushClient.java | 47 -- .../main/java/com/mpush/push/PushRequest.java | 206 -------- .../java/com/mpush/push/PushRequestBus.java | 49 -- .../push/client/ClientChannelHandler.java | 97 ---- .../push/zk/listener/ConnectZKListener.java | 35 -- .../push/zk/listener/GatewayZKListener.java | 35 -- .../push/zk/manager/ConnectZKNodeManager.java | 47 -- .../push/zk/manager/GatewayZKNodeManager.java | 83 --- .../com.mpush.api.spi.client.PusherFactory | 1 + .../services/com.mpush.zk.ZKNodeManager | 2 - .../java/com/mpush/client/PushClientTest.java | 50 -- mpush-client/src/test/resources/logback.xml | 30 -- mpush-common/.gitignore | 1 - mpush-common/pom.xml | 6 +- .../java/com/mpush/common/AbstractClient.java | 56 -- .../mpush/common/AbstractEventContainer.java | 9 - .../main/java/com/mpush/common/ErrorCode.java | 20 + .../com/mpush/common/MessageDispatcher.java | 55 +- .../common/handler/BaseMessageHandler.java | 21 +- .../common/handler/ErrorMessageHandler.java | 19 + .../common/handler/OkMessageHandler.java | 21 +- .../mpush/common/manage/user/UserManager.java | 49 -- .../com/mpush/common/message/BaseMessage.java | 46 +- .../mpush/common/message/BindUserMessage.java | 29 ++ .../mpush/common/message/ByteBufMessage.java | 21 +- .../mpush/common/message/ErrorMessage.java | 27 + .../common/message/FastConnectMessage.java | 30 ++ .../common/message/FastConnectOkMessage.java | 27 + .../common/message/HandshakeMessage.java | 37 ++ .../common/message/HandshakeOkMessage.java | 32 ++ .../common/message/HttpRequestMessage.java | 25 +- .../common/message/HttpResponseMessage.java | 25 +- .../mpush/common/message/KickUserMessage.java | 19 + .../com/mpush/common/message/OkMessage.java | 19 + .../com/mpush/common/message/PushMessage.java | 37 +- .../message/gateway/GatewayPushMessage.java | 34 +- .../net/HttpProxyDnsMappingManager.java | 118 +++++ .../router/ConnectionRouterManager.java | 44 +- .../com/mpush/common/router/RemoteRouter.java | 21 +- .../common/router/RemoteRouterManager.java | 104 +++- .../common/router/UserChangeListener.java | 65 ++- .../com/mpush/common/security/AesCipher.java | 50 +- .../com/mpush/common/security/CipherBox.java | 33 +- .../com/mpush/common/security/RsaCipher.java | 49 +- .../common/security/RsaCipherFactory.java | 37 ++ .../com/mpush/common/user/UserManager.java | 68 +++ .../com.mpush.api.spi.core.CipherFactory | 1 + .../com.mpush.api.spi.net.DnsMappingManager | 1 + mpush-core/.gitignore | 1 - mpush-core/pom.xml | 32 +- .../com/mpush/core/handler/AdminHandler.java | 261 +++++++--- .../mpush/core/handler/BindUserHandler.java | 32 +- .../core/handler/FastConnectHandler.java | 34 +- .../core/handler/GatewayPushHandler.java | 82 +-- .../mpush/core/handler/HandshakeHandler.java | 50 +- .../mpush/core/handler/HeartBeatHandler.java | 24 +- .../mpush/core/handler/HttpProxyHandler.java | 80 ++- .../mpush/core/handler/UnbindUserHandler.java | 48 +- .../com/mpush/core/router/KickRemoteMsg.java | 41 +- .../com/mpush/core/router/LocalRouter.java | 42 ++ .../mpush/core/router/LocalRouterManager.java | 87 ++-- .../com/mpush/core/router/RouterCenter.java | 58 ++- .../core/router/RouterChangeListener.java | 77 +-- .../router/UserOnlineOfflineListener.java | 39 +- .../com/mpush/core/server/AdminServer.java | 50 +- .../mpush/core/server/ConnectionServer.java | 81 ++- .../com/mpush/core/server/GatewayServer.java | 54 +- .../core/server/ServerChannelHandler.java | 78 +-- .../core/server/ServerConnectionManager.java | 64 ++- .../mpush/core/session/ReusableSession.java | 19 + .../core/session/ReusableSessionManager.java | 37 +- .../com/mpush/core/netty/NettyServerTest.java | 27 - .../mpush/core/security/CipherBoxTest.java | 25 +- mpush-core/src/test/resources/logback.xml | 48 +- mpush-log/.gitignore | 1 - mpush-log/pom.xml | 41 -- .../src/main/java/com/mpush/log/Logs.java | 331 ------------ .../src/test/java/com/mpush/AppTest.java | 38 -- mpush-monitor/.gitignore | 1 - mpush-monitor/pom.xml | 22 +- .../src/main/java/com/mpush/App.java | 13 - .../com/mpush/monitor/data/MonitorResult.java | 65 +++ .../mpush/monitor/data/ResultCollector.java | 41 ++ .../com/mpush/monitor/domain/MonitorData.java | 67 --- .../com/mpush/monitor/quota/BaseQuota.java | 35 -- .../com/mpush/monitor/quota/GCMQuota.java | 55 +- .../com/mpush/monitor/quota/InfoQuota.java | 27 +- .../com/mpush/monitor/quota/MemoryQuota.java | 81 +-- .../com/mpush/monitor/quota/MonitorQuota.java | 25 + .../mpush/monitor/quota/ThreadPoolQuota.java | 21 +- .../com/mpush/monitor/quota/ThreadQuota.java | 37 +- .../com/mpush/monitor/quota/impl/JVMGC.java | 282 +++++----- .../com/mpush/monitor/quota/impl/JVMInfo.java | 97 ++-- .../mpush/monitor/quota/impl/JVMMemory.java | 486 +++++++++--------- .../mpush/monitor/quota/impl/JVMThread.java | 106 ++-- .../monitor/quota/impl/JVMThreadPool.java | 72 +-- .../monitor/service/MonitorDataCollector.java | 106 ---- .../mpush/monitor/service/MonitorService.java | 117 +++++ .../src/test/java/com/mpush/AppTest.java | 20 +- mpush-netty/.gitignore | 1 - mpush-netty/pom.xml | 10 +- .../netty/client/ChannelClientHandler.java | 11 - .../com/mpush/netty/client/HttpCallback.java | 22 - .../com/mpush/netty/client/HttpClient.java | 15 - .../com/mpush/netty/client/NettyClient.java | 251 +++++---- .../netty/client/NettyClientFactory.java | 75 --- .../mpush/netty/client/NettyHttpClient.java | 262 ---------- .../netty/client/SecurityNettyClient.java | 86 ---- .../mpush/netty/codec/DecodeException.java | 31 ++ .../com/mpush/netty/codec/PacketDecoder.java | 27 +- .../com/mpush/netty/codec/PacketEncoder.java | 19 + .../netty/connection/NettyConnection.java | 50 +- .../com/mpush/netty/http/HttpCallback.java | 41 ++ .../java/com/mpush/netty/http/HttpClient.java | 31 ++ .../mpush/netty/http/HttpClientHandler.java | 126 +++++ .../mpush/netty/http/HttpConnectionPool.java | 85 +++ .../com/mpush/netty/http/NettyHttpClient.java | 150 ++++++ .../RequestContext.java} | 50 +- .../com/mpush/netty/server/NettyServer.java | 86 +++- .../mpush/netty/util/NettySharedHolder.java | 18 - mpush-netty/src/test/resources/logback.xml | 50 +- mpush-test/.gitignore | 1 - mpush-test/pom.xml | 10 - .../src/test/java/com/mpush/AppTest.java | 38 -- .../mpush/test/client/ConnClientTestMain.java | 70 +++ .../test/client/ConnClientTestMain2.java | 71 +++ .../mpush/test/client/ConnectClientBoot.java | 42 ++ .../test/configcenter/ConfigCenterTest.java | 124 ++++- .../mpush/test/connection/body/BodyTest.java | 117 +++-- .../connection/client/ConnectTestClient.java | 20 - .../connection/mpns/ConnClientTestMain.java | 69 +++ .../connection/mpns/ConnectTestClient.java | 20 - .../mpns/ConnectTestClientBoot.java | 38 ++ .../mpush/test/connection/severs/Main.java | 12 - .../java/com/mpush/test/crypto/RsaTest.java | 260 +++++----- .../com/mpush/test/gson/DnsMappingTest.java | 56 +- .../java/com/mpush/test/gson/GsonTest.java | 128 +++-- .../mpush/test/push/PushClientTestMain.java | 71 +++ .../mpush/test/redis/ConsistentHashTest.java | 97 ++-- .../com/mpush/test/redis/MPushUtilTest.java | 31 +- .../java/com/mpush/test/redis/PubSubTest.java | 112 ++-- .../mpush/test/redis/RedisClusterTest.java | 83 +-- .../test/redis/RedisGroupManageTest.java | 109 ---- .../com/mpush/test/redis/RedisUtilTest.java | 386 +++++++------- .../test/java/com/mpush/test/redis/User.java | 90 ++-- .../com/mpush/test/sever/ServerTestMain.java | 33 ++ .../java/com/mpush/test/util/TelnetTest.java | 65 ++- .../src/test/resources/application.conf | 6 + mpush-test/src/test/resources/logback.xml | 48 +- .../services/com.mpush.api.spi.PusherFactory | 1 + mpush-tools/.gitignore | 1 - mpush-tools/pom.xml | 76 +-- .../main/java/com/mpush/tools/ConsoleLog.java | 32 -- .../main/java/com/mpush/tools/Constants.java | 41 -- .../java/com/mpush/tools/GenericsUtil.java | 139 ----- .../main/java/com/mpush/tools/JVMUtil.java | 136 ----- .../src/main/java/com/mpush/tools/Jsons.java | 28 +- .../src/main/java/com/mpush/tools/Pair.java | 28 - .../tools/{MPushUtil.java => Utils.java} | 67 ++- .../com/mpush/tools/common/GenericsUtil.java | 164 ++++++ .../com/mpush/tools/{ => common}/IOUtils.java | 34 +- .../java/com/mpush/tools/common/JVMUtil.java | 187 +++++++ .../java/com/mpush/tools/common/Pair.java | 47 ++ .../mpush/tools/{ => common}/Profiler.java | 33 +- .../com/mpush/tools/{ => common}/Strings.java | 21 +- .../java/com/mpush/tools/common/TimeLine.java | 94 ++++ .../main/java/com/mpush/tools/config/CC.java | 345 +++++++++++++ .../com/mpush/tools/config/ConfigCenter.java | 241 --------- .../com/mpush/tools/config/ConfigManager.java | 55 ++ .../tools/config/DnsMappingConverter.java | 48 -- .../com/mpush/tools/config/MapConverter.java | 27 - .../tools/config/RedisGroupConverter.java | 43 -- .../mpush/tools/config/data/RedisGroup.java | 45 ++ .../mpush/tools/config/data/RedisServer.java | 90 ++++ .../java/com/mpush/tools/crypto/AESUtils.java | 50 +- .../java/com/mpush/tools/crypto/Base64.java | 378 +++++++------- .../com/mpush/tools/crypto/Base64Utils.java | 124 +---- .../mpush/tools/crypto/CryptoException.java | 38 ++ .../java/com/mpush/tools/crypto/MD5Utils.java | 25 +- .../java/com/mpush/tools/crypto/RSAUtils.java | 37 +- .../java/com/mpush/tools/dns/DnsMapping.java | 25 - .../tools/dns/manage/DnsMappingManage.java | 99 ---- .../java/com/mpush/tools/event/Event.java | 21 - .../java/com/mpush/tools/event}/EventBus.java | 29 +- .../com/mpush/tools/event/EventConsumer.java | 28 + .../mpush/tools/event/EventDispatcher.java | 19 - .../com/mpush/tools/event/EventListener.java | 7 - .../java/com/mpush/tools/event/EventType.java | 5 - .../mpush/tools/exception/ZKException.java | 24 - .../main/java/com/mpush/tools/log/Logs.java | 60 +++ .../com/mpush/tools/redis/RedisGroup.java | 49 -- .../java/com/mpush/tools/redis/RedisNode.java | 41 -- .../mpush/tools/redis/RedisPoolConfig.java | 35 -- .../com/mpush/tools/redis/RedisRegister.java | 17 - .../redis/consistenthash/ConsistentHash.java | 66 --- .../tools/redis/consistenthash/Node.java | 26 - .../jedis/services/JedisRegisterManager.java | 78 --- .../redis/listener/ListenerDispatcher.java | 51 -- .../tools/redis/listener/MessageListener.java | 8 - .../mpush/tools/redis/manage/RedisManage.java | 289 ----------- .../mpush/tools/redis/pubsub/Subscriber.java | 68 --- .../main/java/com/mpush/tools/spi/SPI.java | 14 - .../tools/thread/NamedThreadFactory.java | 89 ++-- .../mpush/tools/thread/PoolThreadFactory.java | 61 +++ .../mpush/tools/thread/ThreadNameSpace.java | 46 -- .../com/mpush/tools/thread/ThreadNames.java | 53 ++ .../tools/thread/pool/DefaultExecutor.java | 34 ++ .../thread/pool/DefaultThreadPoolFactory.java | 120 +++++ .../pool/DumpThreadRejectedHandler.java | 74 +++ .../tools/thread/pool/ThreadPoolConfig.java | 135 +++++ .../tools/thread/pool/ThreadPoolManager.java | 135 +++++ .../thread/threadpool/IgnoreRunsPolicy.java | 42 -- .../tools/thread/threadpool/ThreadPool.java | 12 - .../thread/threadpool/ThreadPoolContext.java | 63 --- .../thread/threadpool/ThreadPoolManager.java | 45 -- .../threadpool/cached/CachedThreadPool.java | 46 -- .../cached/CachedThreadPoolContext.java | 23 - .../threadpool/fixed/FixedThreadPool.java | 42 -- .../fixed/FixedThreadPoolContext.java | 23 - ...com.mpush.api.spi.common.ThreadPoolFactory | 1 + .../com.mpush.tools.redis.RedisRegister | 1 - .../java/com/mpush/tools/IOUtilsTest.java | 26 +- .../com/mpush/tools/crypto/AESUtilsTest.java | 21 +- .../com/mpush/tools/crypto/RSAUtilsTest.java | 22 +- .../java/com/mpush/tools/delayqueue/Exam.java | 155 ------ .../java/com/mpush/tools/owner/OwnerTest.java | 70 --- .../com/mpush/tools/owner/ServerConfig.java | 30 -- .../java/com/mpush/tools/spi/SpiTest.java | 60 --- .../com/mpush/tools/spi/test/TestService.java | 11 - .../mpush/tools/spi/test/TestServiceImpl.java | 19 - .../tools/spi/test/TestServiceImpl2.java | 20 - .../java/com/mpush/tools/thread/SyncTest.java | 145 ------ mpush-tools/src/test/resources/logback.xml | 50 +- mpush-zk/.gitignore | 1 - .../src/main/java/com/mpush/zk/ZKClient.java | 111 ++-- .../src/main/java/com/mpush/zk/ZKConfig.java | 70 ++- .../main/java/com/mpush/zk/ZKException.java | 43 ++ .../main/java/com/mpush/zk/ZKNodeManager.java | 16 - .../src/main/java/com/mpush/zk/ZKPath.java | 40 +- .../java/com/mpush/zk/cache/ZKNodeCache.java | 39 ++ .../com/mpush/zk/cache/ZKRedisNodeCache.java | 62 +++ .../com/mpush/zk/cache/ZKServerNodeCache.java | 81 +++ .../zk/listener/ZKDataChangeListener.java | 28 - .../mpush/zk/listener/ZKNodeCacheWatcher.java | 76 +++ .../zk/listener/ZKRedisNodeListener.java | 85 --- .../mpush/zk/listener/ZKRedisNodeWatcher.java | 79 +++ .../zk/listener/ZKServerNodeListener.java | 79 --- .../zk/listener/ZKServerNodeWatcher.java | 101 ++++ .../main/java/com/mpush/zk/node/ZKNode.java | 29 ++ .../java/com/mpush/zk/node/ZKRedisNode.java | 45 ++ .../com/mpush/zk/{ => node}/ZKServerNode.java | 51 +- pom.xml | 114 ++-- 378 files changed, 13261 insertions(+), 10089 deletions(-) delete mode 100644 bin/daily-pub.py delete mode 100644 bin/debug.sh create mode 100644 bin/mp-env.cmd create mode 100644 bin/mp-env.sh create mode 100644 bin/mp.cmd create mode 100644 bin/mp.sh delete mode 100644 bin/pub-daily.py delete mode 100644 bin/pub-online.py delete mode 100644 bin/pub-pre.py delete mode 100644 bin/run.sh create mode 100644 bin/set-env.sh delete mode 100644 bin/start.sh delete mode 100644 bin/test.py delete mode 100644 bin/test.sh delete mode 100644 conf/conf-daily.properties create mode 100644 conf/conf-dev.properties delete mode 100644 conf/conf-online.properties delete mode 100644 conf/conf-pre.properties create mode 100644 conf/conf-pub.properties create mode 100644 conf/reference.conf delete mode 100644 mpush-api/.gitignore delete mode 100644 mpush-api/src/main/java/com/mpush/api/Client.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/PushContent.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/PushSender.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/Server.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/MessageException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushContent.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushSender.java create mode 100644 mpush-api/src/main/java/com/mpush/api/router/ClientType.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/BaseService.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Client.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Listener.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Server.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Service.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/ServiceException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/Factory.java rename mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java => mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java (62%) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java delete mode 100644 mpush-boot/.gitignore delete mode 100644 mpush-boot/src/main/java/com/mpush/Main.java delete mode 100644 mpush-boot/src/main/java/com/mpush/ServerLauncher.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/BootChain.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/BootJob.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/LastBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/Main.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java delete mode 100644 mpush-boot/src/main/resources/config.properties create mode 100644 mpush-boot/src/main/resources/mpush.conf delete mode 100644 mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java create mode 100644 mpush-cache/pom.xml rename mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java => mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java (62%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java rename {mpush-api/src/main/java/com/mpush/api => mpush-cache/src/main/java/com/mpush/cache/redis}/RedisKey.java (55%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushRequest.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java delete mode 100644 mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/ConnectClient.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushClient.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushRequest.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushRequestBus.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java create mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory delete mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager delete mode 100644 mpush-client/src/test/java/com/mpush/client/PushClientTest.java delete mode 100644 mpush-client/src/test/resources/logback.xml delete mode 100644 mpush-common/.gitignore delete mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractClient.java delete mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java delete mode 100644 mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java create mode 100644 mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java create mode 100644 mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java create mode 100644 mpush-common/src/main/java/com/mpush/common/user/UserManager.java create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager delete mode 100644 mpush-core/.gitignore rename mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java => mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java (55%) delete mode 100644 mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java delete mode 100644 mpush-log/.gitignore delete mode 100644 mpush-log/pom.xml delete mode 100644 mpush-log/src/main/java/com/mpush/log/Logs.java delete mode 100644 mpush-log/src/test/java/com/mpush/AppTest.java delete mode 100644 mpush-monitor/.gitignore delete mode 100644 mpush-monitor/src/main/java/com/mpush/App.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java delete mode 100644 mpush-netty/.gitignore delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java rename mpush-netty/src/main/java/com/mpush/netty/{client/RequestInfo.java => http/RequestContext.java} (66%) delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java delete mode 100644 mpush-test/.gitignore delete mode 100644 mpush-test/src/test/java/com/mpush/AppTest.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java create mode 100644 mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java create mode 100644 mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java create mode 100644 mpush-test/src/test/resources/application.conf create mode 100644 mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory delete mode 100644 mpush-tools/.gitignore delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/Constants.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/Pair.java rename mpush-tools/src/main/java/com/mpush/tools/{MPushUtil.java => Utils.java} (80%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/IOUtils.java (66%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/Pair.java rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Profiler.java (93%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Strings.java (52%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/CC.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/Event.java rename {mpush-common/src/main/java/com/mpush/common => mpush-tools/src/main/java/com/mpush/tools/event}/EventBus.java (52%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventType.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/log/Logs.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java create mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory delete mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java delete mode 100644 mpush-zk/.gitignore create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKException.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java rename mpush-zk/src/main/java/com/mpush/zk/{ => node}/ZKServerNode.java (57%) diff --git a/.gitignore b/.gitignore index 3c74f152..a0043cc4 100644 --- a/.gitignore +++ b/.gitignore @@ -23,23 +23,3 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* - -mpush-test/src/test/resources/config.properties - -build.sh - -*.prefs - -conf/conf-dev.properties - -*.properties - -mpush-test/src/test/java/com/mpush/test/connection/client/Main.java - -mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java - -mpush-test/src/test/java/com/mpush/test/push/Main.java - -*.properties - -conf/conf-dev.properties diff --git a/bin/daily-pub.py b/bin/daily-pub.py deleted file mode 100644 index a5810bd0..00000000 --- a/bin/daily-pub.py +++ /dev/null @@ -1,214 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'daily' - - -class SSH(object): - def __init__(self): - self.client = None - self.chan = None - self.shell = None - - def __enter__(self): - return self - - def __exit__(self, typ, value, trace): - self.close() - - def connect(self, host, port=22, username='root', password=None): - self.client = paramiko.SSHClient() - ##self.client.load_system_host_keys() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=10) - return self - - def close(self): - if self.client: - self.client.close() - - def exec_command(self, cmd, isprint=True): - """执行命令,每次执行都是新的session""" - if not cmd: - return - print_cmd(cmd) - - stdin, stdout, stderr = self.client.exec_command(cmd) - out = stdout.read() - if isprint: - print_out(out) - - err = stderr.read() - if err: - print_out(err) - return out, err - - def _invoke_shell(self): - """创建一个shell""" - self.shell = self.client.invoke_shell(width=200) - is_recv = False - while True: - if self.shell.recv_ready(): - print_out_stream(self.shell.recv(1024)) - is_recv = True - else: - if is_recv: - return - else: - time.sleep(0.1) - - def shell_exec(self, cmd): - """在shell中执行命令,使用的是同一个session""" - if not cmd: - return - - if not self.shell: - self._invoke_shell() - - self.shell.send(cmd + "\n") - - out = '' - is_recv = False - while True: - if self.shell.recv_ready(): - tmp = self.shell.recv(1024) - out += tmp - print_out_stream(tmp) - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -def getPid(ssh): - stdout = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) - return stdout -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean) - runShell('mvn package -P %s'%ENV) - print showText('package success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.shell_exec('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - - - ##4 kill process - pid = getPid(ssh) - if pid : - ssh.shell_exec('kill -9 %s'%pid) - else: - print showText('there is no process to kill','YELLOW') - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.shell_exec('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME)) - print showText('tar success','greenText') - - ##7 start process - ssh.shell_exec('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/debug.sh b/bin/debug.sh deleted file mode 100644 index 82a5fc7d..00000000 --- a/bin/debug.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -ENV=daily -cd `dirname $0` -cd .. - -echo "start package project..." -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd ./mpush-boot/target -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." -cd mpush -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -Dio.netty.leakDetectionLevel=advanced -jar boot.jar & - -echo "end start mpush..." diff --git a/bin/mp-env.cmd b/bin/mp-env.cmd new file mode 100644 index 00000000..fa39e14c --- /dev/null +++ b/bin/mp-env.cmd @@ -0,0 +1,49 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +set MPCFGDIR=%~dp0%..\conf +set MP_LOG_DIR=%~dp0%.. +set MP_LOG4J_PROP=INFO,CONSOLE + +REM for sanity sake assume Java 1.6 +REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html + +REM add the mpcfg dir to classpath +set CLASSPATH=%MPCFGDIR% + +REM make it work in the release +SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% + +REM make it work for developers +SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% + +set MPCFG=%MPCFGDIR%\zoo.cfg + +@REM setup java environment variables + +if not defined JAVA_HOME ( + echo Error: JAVA_HOME is not set. + goto :eof +) + +set JAVA_HOME=%JAVA_HOME:"=% + +if not exist "%JAVA_HOME%"\bin\java.exe ( + echo Error: JAVA_HOME is incorrectly set. + goto :eof +) + +set JAVA="%JAVA_HOME%"\bin\java diff --git a/bin/mp-env.sh b/bin/mp-env.sh new file mode 100644 index 00000000..618dee65 --- /dev/null +++ b/bin/mp-env.sh @@ -0,0 +1,122 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script should be sourced into other mpush +# scripts to setup the env variables + +# We use MPCFGDIR if defined, +# otherwise we use /etc/mp +# or the conf directory that is +# a sibling of this script's directory + +MPBINDIR="${MPBINDIR:-/usr/bin}" +MPUSH_PREFIX="${MPBINDIR}/.." + +if [ "x$MPCFGDIR" = "x" ] +then + if [ -e "${MPUSH_PREFIX}/conf" ]; then + MPCFGDIR="$MPBINDIR/../conf" + else + MPCFGDIR="$MPBINDIR/../etc/mpush" + fi +fi + +if [ -f "${MPBINDIR}/set-env.sh" ]; then + . "${MPBINDIR}/set-env.sh" +fi + +if [ "x$MPCFG" = "x" ] +then + MPCFG="mpush.conf" +fi + +MPCFG="$MPCFGDIR/$MPCFG" + +if [ -f "$MPBINDIR/java.env" ] +then + . "$MPBINDIR/java.env" +fi + +if [ "x${MP_DATADIR}" = "x" ] +then + MP_DATADIR="${MPUSH_PREFIX}/tmp" +fi + +if [ "x${MP_LOG_DIR}" = "x" ] +then + MP_LOG_DIR="${MPUSH_PREFIX}/logs" +fi + +if [ "x${MP_LOG4J_PROP}" = "x" ] +then + MP_LOG4J_PROP="INFO,CONSOLE" +fi + +if [ "$JAVA_HOME" != "" ]; then + JAVA="$JAVA_HOME/bin/java" +else + JAVA=java +fi + + +#add the conf dir to classpath +CLASSPATH="$MPCFGDIR:$CLASSPATH" + +for i in "$MPBINDIR"/../src/java/lib/*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work in the binary package +#(use array for LIBPATH to account for spaces within wildcard expansion) +if [ -e "${MPUSH_PREFIX}"/share/mpush/mpush-*.jar ]; then + LIBPATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) +else + #release tarball format + for i in "$MPBINDIR"/../mpush-*.jar + do + CLASSPATH="$i:$CLASSPATH" + done + LIBPATH=("${MPBINDIR}"/../lib/*.jar) +fi + +for i in "${LIBPATH[@]}" +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work for developers +for d in "$MPBINDIR"/../build/lib/*.jar +do + CLASSPATH="$d:$CLASSPATH" +done + +#make it work for developers +CLASSPATH="$MPBINDIR/../build/classes:$CLASSPATH" + + +case "`uname`" in + CYGWIN*) cygwin=true ;; + *) cygwin=false ;; +esac + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + +#echo "CLASSPATH=$CLASSPATH" \ No newline at end of file diff --git a/bin/mp.cmd b/bin/mp.cmd new file mode 100644 index 00000000..f578f4e0 --- /dev/null +++ b/bin/mp.cmd @@ -0,0 +1,24 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0mpEnv.cmd" + +set MPMAIN="-jar ../boot.jar" +echo on +call %JAVA% "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% "%MPCFG%" %* + +endlocal diff --git a/bin/mp.sh b/bin/mp.sh new file mode 100644 index 00000000..c40e2d3d --- /dev/null +++ b/bin/mp.sh @@ -0,0 +1,232 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + + + +# use POSTIX interface, symlink is followed automatically +MPBIN="${BASH_SOURCE-$0}" +MPBIN="$(dirname "${MPBIN}")" +MPBINDIR="$(cd "${MPBIN}"; pwd)" + +if [ -e "$MPBIN/../libexec/mp-env.sh" ]; then + . "$MPBINDIR/../libexec/mp-env.sh" +else + . "$MPBINDIR/mp-env.sh" +fi + +# See the following page for extensive details on setting +# up the JVM to accept JMX remote management: +# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html +# by default we allow local JMX connections +if [ "x$JMXLOCALONLY" = "x" ] +then + JMXLOCALONLY=false +fi + +if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ] +then + echo "MPush JMX enabled by default" >&2 + if [ "x$JMXPORT" = "x" ] + then + # for some reason these two options are necessary on jdk6 on Ubuntu + # accord to the docs they are not necessary, but otw jconsole cannot + # do a local attach + MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" + else + if [ "x$JMXAUTH" = "x" ] + then + JMXAUTH=false + fi + if [ "x$JMXSSL" = "x" ] + then + JMXSSL=false + fi + if [ "x$JMXLOG4J" = "x" ] + then + JMXLOG4J=true + fi + echo "MPush remote JMX Port set to $JMXPORT" >&2 + echo "MPush remote JMX authenticate set to $JMXAUTH" >&2 + echo "MPush remote JMX ssl set to $JMXSSL" >&2 + echo "MPush remote JMX log4j set to $JMXLOG4J" >&2 + MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" + fi +else + echo "JMX disabled by user request" >&2 + MPMAIN="" +fi + +MPMAIN="$MPMAIN -jar $MPBINDIR/bootstrap.jar" + +if [ "x$SERVER_JVMFLAGS" != "x" ] +then + JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" +fi + +if [ "x$2" != "x" ] +then + MPCFG="$MPCFGDIR/$2" +fi + +# if we give a more complicated path to the config, don't screw around in $MPCFGDIR +if [ "x$(dirname "$MPCFG")" != "x$MPCFGDIR" ] +then + MPCFG="$2" +fi + +if $cygwin +then + MPCFG=`cygpath -wp "$MPCFG"` + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +echo "Using config: $MPCFG" >&2 + +case "$OSTYPE" in +*solaris*) + GREP=/usr/xpg4/bin/grep + ;; +*) + GREP=grep + ;; +esac +if [ -z "$MPPIDFILE" ]; then +# MP_DATADIR="$($GREP "^[[:space:]]*dataDir" "$MPCFG" | sed -e 's/.*=//')" + if [ ! -d "$MP_DATADIR" ]; then + mkdir -p "$MP_DATADIR" + fi + MPPIDFILE="$MP_DATADIR/mpush_server.pid" +else + # ensure it exists, otw stop will fail + mkdir -p "$(dirname "$MPPIDFILE")" +fi + +if [ ! -w "$MP_LOG_DIR" ] ; then +echo $MP_LOG_DIR +mkdir -p "$MP_LOG_DIR" +fi + +_MP_DAEMON_OUT="$MP_LOG_DIR/mpush.out" + +case $1 in +start) + echo -n "Starting mpush ... " + if [ -f "$MPPIDFILE" ]; then + if kill -0 `cat "$MPPIDFILE"` > /dev/null 2>&1; then + echo $command already running as process `cat "$MPPIDFILE"`. + exit 0 + fi + fi + nohup "$JAVA" "-Dmp.conf=$MPCFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $MPMAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & + if [ $? -eq 0 ] + then + case "$OSTYPE" in + *solaris*) + /bin/echo "${!}\\c" > "$MPPIDFILE" + ;; + *) + /bin/echo -n $! > "$MPPIDFILE" + ;; + esac + if [ $? -eq 0 ]; + then + sleep 1 + echo STARTED + else + echo FAILED TO WRITE PID + exit 1 + fi + else + echo SERVER DID NOT START + exit 1 + fi + ;; +start-foreground) + MP_CMD=(exec "$JAVA") + if [ "${MP_NOEXEC}" != "" ]; then + MP_CMD=("$JAVA") + fi + "${MP_CMD[@]}" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $MPMAIN "-Dmp.conf=$MPCFG" + ;; +print-cmd) + echo "\"$JAVA\" $MPMAIN " + echo "\"-Dmp.conf=$MPCFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " + echo "$JVMFLAGS " + echo "-cp \"$CLASSPATH\" " + echo "> \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" + ;; +stop) + echo -n "Stopping mpush ... " + if [ ! -f "$MPPIDFILE" ] + then + echo "no mpush to stop (could not find file $MPPIDFILE)" + else + $KILL -9 $(cat "$MPPIDFILE") + rm "$MPPIDFILE" + echo STOPPED + fi + exit 0 + ;; +upgrade) + shift + echo "upgrading the servers to 3.*" + "$JAVA" "-Dmpush.log.dir=${MP_LOG_DIR}" "-Dmpush.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS com.mpush.tools.upgrade.UpgradeMain ${@} + echo "Upgrading ... " + ;; +restart) + shift + "$0" stop ${@} + sleep 5 + "$0" start ${@} + ;; +status) + # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + if ! [ $clientPortAddress ] + then + clientPortAddress="localhost" + fi + clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + STAT=`"$JAVA" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS org.apache.mpush.client.FourLetterWordMain \ + $clientPortAddress $clientPort srvr 2> /dev/null \ + | $GREP Mode` + if [ "x$STAT" = "x" ] + then + echo "Error contacting service. It is probably not running." + exit 1 + else + echo $STAT + exit 0 + fi + ;; +*) + echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2 + +esac diff --git a/bin/pub-daily.py b/bin/pub-daily.py deleted file mode 100644 index 9344ed27..00000000 --- a/bin/pub-daily.py +++ /dev/null @@ -1,239 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'daily' - -class Telnet(object): - def __init__(self, chan): - self.chan = chan - - def send(self, cmd,isprint=True): - self.chan.send(cmd+'\n') - print_cmd(cmd) - out = '' - is_recv = False - is_recv_err = False - while True: - # 结束 - if self.chan.recv_stderr_ready(): - tmp = self.chan.recv_stderr(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv_err = True - else: - if is_recv_err: - return out - else: - time.sleep(0.1) - - if self.chan.recv_ready(): - tmp = self.chan.recv(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - def telnet(self, cmd,isprint=True): - chan = self.client.get_transport().open_session(timeout=10) - chan.exec_command(cmd) - - t = Telnet(chan) - - is_recv = False - - while True: - if chan.recv_ready(): - if isprint: - print_out_stream(chan.recv(1024)) - is_recv = True - else: - if is_recv: - return t - else: - time.sleep(0.1) - - - def close(self): - if self.client: - self.client.close() - -def getPid(keyword,ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - except: - print showText('telnet exception','redText') - - - print showText('start kill process','greenText') - - ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid : - ssh.exe('kill %s'%pid) - sleep(15) - else: - print showText('there is no process to kill','YELLOW') - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid: - ssh.exe('kill -9 %s'%pid) - - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) - print showText('tar success','greenText') - - ##7 start process - ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/pub-online.py b/bin/pub-online.py deleted file mode 100644 index 1d6739ec..00000000 --- a/bin/pub-online.py +++ /dev/null @@ -1,239 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'online' - -class Telnet(object): - def __init__(self, chan): - self.chan = chan - - def send(self, cmd,isprint=True): - self.chan.send(cmd+'\n') - print_cmd(cmd) - out = '' - is_recv = False - is_recv_err = False - while True: - # 结束 - if self.chan.recv_stderr_ready(): - tmp = self.chan.recv_stderr(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv_err = True - else: - if is_recv_err: - return out - else: - time.sleep(0.1) - - if self.chan.recv_ready(): - tmp = self.chan.recv(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - def telnet(self, cmd,isprint=True): - chan = self.client.get_transport().open_session(timeout=10) - chan.exec_command(cmd) - - t = Telnet(chan) - - is_recv = False - - while True: - if chan.recv_ready(): - if isprint: - print_out_stream(chan.recv(1024)) - is_recv = True - else: - if is_recv: - return t - else: - time.sleep(0.1) - - - def close(self): - if self.client: - self.client.close() - -def getPid(keyword,ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - except: - print showText('telnet exception','redText') - - - print showText('start kill process','greenText') - - ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid : - ssh.exe('kill %s'%pid) - sleep(15) - else: - print showText('there is no process to kill','YELLOW') - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid: - ssh.exe('kill -9 %s'%pid) - - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) - print showText('tar success','greenText') - - ##7 start process - ssh.exe('nohup %s -Dio.netty.leakDetectionLevel=advanced -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/pub-pre.py b/bin/pub-pre.py deleted file mode 100644 index e3bc8ca8..00000000 --- a/bin/pub-pre.py +++ /dev/null @@ -1,101 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - - -BASEPATH = '/root/mpush' - -STARTPROCESS = 'java -jar boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/mpush-release.tar.gz' - -ENV= 'pre' - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##1 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(Y/N):") - - if confirmPub != 'Y': - return - - ##4 cp - runShell('cp %s %s'%(GITLABPATH,BASEPATH)) - print showText('cp success','greenText') - - ##5 tar package - runShell('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz') - print showText('tar success','greenText') - - ##6 start process - runShell('nohup /opt/shinemo/jdk1.7.0_40/bin/java -Dio.netty.leakDetectionLevel=advanced -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') - print showText('start process success','greenText') - - -if __name__ == "__main__": - main() diff --git a/bin/run.sh b/bin/run.sh deleted file mode 100644 index d5f5ab08..00000000 --- a/bin/run.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -ENV=dev - -cd `dirname $0` -cd .. - -echo "start tar mpush..." -cd ./mpush-boot/target - -rm -rf mpush -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." - -cd mpush -java \ --Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ --Dio.netty.leakDetectionLevel=advanced \ --jar boot.jar diff --git a/bin/set-env.sh b/bin/set-env.sh new file mode 100644 index 00000000..5325b967 --- /dev/null +++ b/bin/set-env.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +#JVMFLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file diff --git a/bin/start.sh b/bin/start.sh deleted file mode 100644 index 0f807d5a..00000000 --- a/bin/start.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -ENV=dev - -cd `dirname $0` -cd .. - -echo "start package project..." -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd ./mpush-boot/target -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." -cd mpush -nohup java \ --Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ --Dio.netty.leakDetectionLevel=advanced \ --jar boot.jar >/dev/null 2>&1 & - -echo "end start mpush..." \ No newline at end of file diff --git a/bin/test.py b/bin/test.py deleted file mode 100644 index 8a624cd8..00000000 --- a/bin/test.py +++ /dev/null @@ -1,69 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=9092,username='shinemo',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - - confirmPub = raw_input("") - - print(confirmPub) - - -if __name__ == "__main__": - main() diff --git a/bin/test.sh b/bin/test.sh deleted file mode 100644 index 6d2aa540..00000000 --- a/bin/test.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -ENV=dev - -base_dir=`pwd` - -DIR=`dirname $0` - -PROG=`basename $0` - -echo $DIR diff --git a/conf/conf-daily.properties b/conf/conf-daily.properties deleted file mode 100644 index 92ca7c8f..00000000 --- a/conf/conf-daily.properties +++ /dev/null @@ -1,16 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=debug -zk_ip = 115.29.169.109:5666 -zk_digest = shinemoIpo -zk_namespace = mpush-daily -redis_group = 111.1.57.148:6379:ShineMoIpo -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info = true -connection_server_port = 20882 -gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 -skip_dump=true -admin_port=4001 -remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties new file mode 100644 index 00000000..4a36a6eb --- /dev/null +++ b/conf/conf-dev.properties @@ -0,0 +1 @@ +log.level=debug \ No newline at end of file diff --git a/conf/conf-online.properties b/conf/conf-online.properties deleted file mode 100644 index a5e35416..00000000 --- a/conf/conf-online.properties +++ /dev/null @@ -1,15 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=debug -zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -zk_digest = shinemoIpo -zk_namespace = mpush-online -redis_group = 10.161.223.238:6379:ShineMoIpo -private_key = MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB -force_write_redis_group_info = false -connection_server_port = 3000 -gateway_server_port = 4000 -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 -skip_dump=false -admin_port=4001 diff --git a/conf/conf-pre.properties b/conf/conf-pre.properties deleted file mode 100644 index c022613c..00000000 --- a/conf/conf-pre.properties +++ /dev/null @@ -1,15 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=DEBUG -zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -zk_digest = shinemoIpo -zk_namespace = mpush-pre -redis_group = 10.161.223.238:6379:ShineMoIpo -private_key = MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA== -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB -force_write_redis_group_info = true -connection_server_port = 3000 -gateway_server_port = 4000 -dns_mapping=115.29.230.2=127.0.0.1 -skip_dump=false -admin_port=4001 diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties new file mode 100644 index 00000000..a5051a64 --- /dev/null +++ b/conf/conf-pub.properties @@ -0,0 +1 @@ +log.level=warn \ No newline at end of file diff --git a/conf/reference.conf b/conf/reference.conf new file mode 100644 index 00000000..263e7849 --- /dev/null +++ b/conf/reference.conf @@ -0,0 +1,189 @@ +################################################################################################################## +# +# NOTICE: +# +# 系统配置文件,所有列出的项是系统所支持全部配置项 +# 如果要覆盖某项的值可以添加到mpush.conf中。 +# +# 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 +# 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 +# +############################################################################################################## + +mp { + log.level=warn + log.dir=${user.dir}/../logs + + core { + max-packet-size=10k//系统允许传输的最大包的大小 + compress-threshold=10k//数据包启用压缩的临界值,超过该值后对数据进行压缩 + min-heartbeat=10s + max-heartbeat=3m + max-hb-timeout-times=2//允许的心跳连续超时的最大次数 + session-expired-time=1d//用于快速重连的session 过期时间默认1天 + epoll-provider=netty//nio:jdk 自带,netty:有netty实现 + } + + security { + private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" + public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" + aes-key-length=16 + ras-key-length=1024 + } + + net { + connect-server-port=3000 + gateway-server-port=3001 + admin-server-port=3002 + public-host-mapping {//本机局域网IP和公网IP的映射关系 + "10.1.0.32":"111.1.57.148" + } + traffic-shaping { + gateway-client { + enabled:true + check-interval:100ms + write-global-limit:1k + read-global-limit:0 + write-channel-limit:256b + read-channel-limit:0 + } + + gateway-server { + enabled:true + check-interval:100ms + write-global-limit:0 + read-global-limit:10k + write-channel-limit:0 + read-channel-limit:0.5k + } + + connect-server { + enabled:false + check-interval:100ms + write-global-limit:0 + read-global-limit:100k + write-channel-limit:3k + read-channel-limit:3k + } + } + } + + zk { + server-address="127.0.0.1:2181" + namespace=mpush + digest=mpush + local-cache-path=/ + retry { + #initial amount of time to wait between retries + baseSleepTimeMs=3s + #max number of times to retry + maxRetries=3 + #max time in ms to sleep on each retry + maxSleepMs=5s + } + connectionTimeoutMs=5s + sessionTimeoutMs=5s + } + + redis { + write-to-zk=true + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"111.1.57.148" + port:6379 + password:ShineMoIpo + } + ] + ] + config { + maxTotal:8, + maxIdle:4, + minIdle:1, + lifo:true, + fairness:false, + maxWaitMillis:5000, + minEvictableIdleTimeMillis:300000, + softMinEvictableIdleTimeMillis:1800000, + numTestsPerEvictionRun:3, + testOnCreate:false, + testOnBorrow:false, + testOnReturn:false, + testWhileIdle:false, + timeBetweenEvictionRunsMillis:60000, + blockWhenExhausted:true, + jmxEnabled:true, + jmxNamePrefix:pool, + jmxNameBase:pool + } + } + + http { + proxy-enabled=false + max-conn-per-host=5 + default-read-timeout=10s + max-content-length=5m + dns-mapping { + "mpush.com":["127.0.0.1:8080","127.0.0.1:8081"] + } + } + + thread { + pool { + boss { + min:4 + max:16 + queue-size:1000 + } + + work { + min:8 + max:32 + queue-size:1000 + } + + event-bus { + min:4 + max:4 + queue-size:10000 //大量的online,offline, + } + + http-proxy { + min:8 + max:64 + queue-size:1000 + } + + biz { + min:4 + max:64 + queue-size:10 + } + + mq { + min:2 + max:4 + queue-size:10000 + } + + push-callback { + min:2 + max:2 + queue-size:0 + } + } + } + + monitor { + dump-dir=/tmp/logs/mpush/ + dump-stack=false + dump-period=1m + print-log=true + } + + spi { + thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" + dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" + } +} \ No newline at end of file diff --git a/mpush-api/.gitignore b/mpush-api/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-api/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-api/src/main/java/com/mpush/api/Client.java b/mpush-api/src/main/java/com/mpush/api/Client.java deleted file mode 100644 index f63a0ca0..00000000 --- a/mpush-api/src/main/java/com/mpush/api/Client.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.mpush.api; - -import com.mpush.api.connection.Connection; -import io.netty.channel.Channel; - - -public interface Client { - - void init(Channel channel); - - boolean isConnected(); - - String getHost(); - - int getPort(); - - void close(String cause); - - boolean isEnabled(); - - void resetHbTimes(); - - int inceaseAndGetHbTimes(); - - String getUrl(); - - void startHeartBeat() throws Exception; - - void startHeartBeat(final int heartbeat) throws Exception; - - void stop(); - - Connection getConnection(); - - Channel getChannel(); - - void initConnection(Connection connection); - -} diff --git a/mpush-api/src/main/java/com/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java index 11c15dbd..27e5cece 100644 --- a/mpush-api/src/main/java/com/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import java.nio.charset.Charset; @@ -9,6 +28,7 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); + byte[] EMPTY_BYTES = new byte[0]; String HTTP_HEAD_READ_TIMEOUT = "readTimeout"; diff --git a/mpush-api/src/main/java/com/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java index cbf37d56..581c48fb 100644 --- a/mpush-api/src/main/java/com/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java index e444949a..5d7ee6b0 100644 --- a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java index da0bffe6..d26e5cf7 100644 --- a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/mpush/api/PushContent.java b/mpush-api/src/main/java/com/mpush/api/PushContent.java deleted file mode 100644 index 7abe5307..00000000 --- a/mpush-api/src/main/java/com/mpush/api/PushContent.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.mpush.api; - -import java.io.Serializable; - - -/** - * msgId、msgType 必填 - * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 - * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 - * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * - */ -public final class PushContent implements Serializable{ - private static final long serialVersionUID = -1805329333995385960L; - private String msgId; //返回使用 - private String content; //content - private int msgType; //type - - public PushContent(int msgType) { - this.msgType = msgType; - } - - public static PushContent build(PushType msgType,String content){ - PushContent pushContent = new PushContent(msgType.getValue()); - pushContent.setContent(content); - return pushContent; - } - - public String getMsgId() { - return msgId; - } - - public int getMsgType() { - return msgType; - } - - public void setMsgId(String msgId) { - this.msgId = msgId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public enum PushType{ - NOTIFICATION("提醒",1), - MESSAGE("消息",2), - NOTIFICATIONANDMESSAGE("提醒+消息",3); - - PushType(String desc, int value) { - this.desc = desc; - this.value = value; - } - - private final String desc; - private final int value; - - public String getDesc() { - return desc; - } - public int getValue() { - return value; - } - } - -} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/PushSender.java b/mpush-api/src/main/java/com/mpush/api/PushSender.java deleted file mode 100644 index 83b1d519..00000000 --- a/mpush-api/src/main/java/com/mpush/api/PushSender.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mpush.api; - -import java.util.Collection; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public interface PushSender { - void send(String content, Collection userIds, Callback callback); - - void send(String content, String userId, Callback callback); - - interface Callback { - void onSuccess(String userId); - - void onFailure(String userId); - - void onOffline(String userId); - - void onTimeout(String userId); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/Server.java deleted file mode 100644 index f9785345..00000000 --- a/mpush-api/src/main/java/com/mpush/api/Server.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.api; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public interface Server { - - void start(Listener listener); - - void stop(Listener listener); - - void init(); - - boolean isRunning(); - - interface Listener { - void onSuccess(int port); - - void onFailure(Throwable cause); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java index e365f656..3751b49b 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; /** diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index f1ea56aa..7eee23f2 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -1,8 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; import com.mpush.api.protocol.Packet; import io.netty.channel.Channel; - import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -35,12 +53,8 @@ public interface Connection { void updateLastReadTime(); - int inceaseAndGetHbTimes(); + long getLastReadTime(); - void resetHbTimes(); + Channel getChannel(); - long getLastReadTime(); - - Channel getChannel(); - } diff --git a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index 554486e9..58f12e76 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -1,9 +1,28 @@ -package com.mpush.api.connection; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.List; +package com.mpush.api.connection; import io.netty.channel.Channel; +import java.util.List; + /** * Created by ohun on 2015/12/30. */ @@ -11,13 +30,13 @@ public interface ConnectionManager { Connection get(Channel channel); - void remove(Channel channel); + Connection removeAndClose(Channel channel); void add(Connection connection); - List getConnections(); + List getConnections(); - void init(); + void init(); void destroy(); } diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index aa94e9bd..46fb0647 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -1,5 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; +import com.mpush.api.router.ClientType; + /** * Created by ohun on 2015/12/22. * @@ -10,8 +31,10 @@ public final class SessionContext { public String osVersion; public String clientVersion; public String deviceId; + public String userId; public int heartbeat; public Cipher cipher; + private int clientType; public void changeCipher(Cipher cipher) { this.cipher = cipher; @@ -37,6 +60,11 @@ public SessionContext setDeviceId(String deviceId) { return this; } + public SessionContext setUserId(String userId) { + this.userId = userId; + return this; + } + public void setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; } @@ -45,9 +73,21 @@ public boolean handshakeOk() { return deviceId != null && deviceId.length() > 0; } - @Override - public String toString() { - return "SessionContext [osName=" + osName + ", osVersion=" + osVersion + ", clientVersion=" + clientVersion + ", deviceId=" + deviceId + ", heartbeat=" + heartbeat + "]"; - } + public int getClientType() { + if (clientType == 0) { + clientType = ClientType.find(osName).type; + } + return clientType; + } + + @Override + public String toString() { + return "SessionContext [osName=" + osName + + ", osVersion=" + osVersion + + ", clientVersion=" + clientVersion + + ", deviceId=" + deviceId + + ", heartbeat=" + heartbeat + + "]"; + } } diff --git a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java index 2eaf18e7..a31dea4b 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/Event.java b/mpush-api/src/main/java/com/mpush/api/event/Event.java index e1043d2d..8174f047 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/Event.java +++ b/mpush-api/src/main/java/com/mpush/api/event/Event.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java index d7cafa10..a3922ea6 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java index bcb2608b..fc3321cb 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java index 926bb71d..c4c5c024 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.router.Router; diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java index a5087299..68cac550 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java index 6ecb3596..d6053ee9 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; @@ -6,21 +25,22 @@ * 绑定用户的时候才会触发该事件 */ public final class UserOnlineEvent implements Event { - - private final Connection connection; + + private final Connection connection; private final String userId; - - public UserOnlineEvent(Connection connection, String userId) { - this.connection = connection; - this.userId = userId; - } - - public Connection getConnection() { - return connection; - } - public String getUserId() { - return userId; - } - + + public UserOnlineEvent(Connection connection, String userId) { + this.connection = connection; + this.userId = userId; + } + + public Connection getConnection() { + return connection; + } + + public String getUserId() { + return userId; + } + } diff --git a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java deleted file mode 100644 index add6460c..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/23. - * - * @author ohun@live.cn - */ -public class CryptoException extends RuntimeException { - - private static final long serialVersionUID = 368277451733324220L; - - public CryptoException(String message) { - super(message); - } - -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java deleted file mode 100644 index ce7f8435..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/23. - * - * @author ohun@live.cn - */ -public class DecodeException extends RuntimeException { - public DecodeException(String message) { - super(message); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java deleted file mode 100644 index d2dd1a3b..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.api.exception; - -public class MessageException extends RuntimeException { - - private static final long serialVersionUID = 8731698346169093329L; - - public MessageException(String message) { - super(message); - } - -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java deleted file mode 100644 index 62080127..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class SendMessageException extends RuntimeException { -} diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java index 7e2dae4d..ad2e739a 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.protocol; /** diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index b800417c..d9921802 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.protocol; import io.netty.buffer.ByteBuf; @@ -16,6 +35,7 @@ public final class Packet { public static final byte HB_PACKET_BYTE = -33; public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; + public static final Packet HB_PACKE = new Packet(Command.HEARTBEAT); public byte cmd; //命令 public short cc; //校验码 暂时没有用到 @@ -46,7 +66,7 @@ public int getBodyLength() { return body == null ? 0 : body.length; } - public void setFlag(byte flag) { + public void addFlag(byte flag) { this.flags |= flag; } diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java new file mode 100644 index 00000000..e4e741c3 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java @@ -0,0 +1,94 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.push; + +import java.io.Serializable; + + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + */ +public final class PushContent implements Serializable { + private static final long serialVersionUID = -1805329333995385960L; + private String msgId; //返回使用 + private String content; //content + private int msgType; //type + + public PushContent(int msgType) { + this.msgType = msgType; + } + + public static PushContent build(PushType msgType, String content) { + PushContent pushContent = new PushContent(msgType.getValue()); + pushContent.setContent(content); + return pushContent; + } + + public String getMsgId() { + return msgId; + } + + public int getMsgType() { + return msgType; + } + + public void setMsgId(String msgId) { + this.msgId = msgId; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public enum PushType { + NOTIFICATION("提醒", 1), + MESSAGE("消息", 2), + NOTIFICATIONANDMESSAGE("提醒+消息", 3); + + PushType(String desc, int value) { + this.desc = desc; + this.value = value; + } + + private final String desc; + private final int value; + + public String getDesc() { + return desc; + } + + public int getValue() { + return value; + } + } + +} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushException.java b/mpush-api/src/main/java/com/mpush/api/push/PushException.java new file mode 100644 index 00000000..6c9f3d6a --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.api.push; + +/** + * Created by yxx on 2016/5/28. + * + * @author ohun@live.cn (夜色) + */ +public class PushException extends RuntimeException { + + public PushException(Throwable cause) { + super(cause); + } + + public PushException(String message) { + super(message); + } + + public PushException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java new file mode 100644 index 00000000..3c20996e --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -0,0 +1,58 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.push; + +import com.mpush.api.router.ClientLocation; +import com.mpush.api.service.Service; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.client.PusherFactory; + +import java.util.Collection; +import java.util.concurrent.FutureTask; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public interface PushSender extends Service { + + static PushSender create() { + return SpiLoader.load(PusherFactory.class).get(); + } + + void send(String content, Collection userIds, Callback callback); + + FutureTask send(String content, String userId, Callback callback); + + void send(byte[] content, Collection userIds, Callback callback); + + FutureTask send(byte[] content, String userId, Callback callback); + + interface Callback { + void onSuccess(String userId, ClientLocation location); + + void onFailure(String userId, ClientLocation location); + + void onOffline(String userId, ClientLocation location); + + void onTimeout(String userId, ClientLocation location); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index c7267088..ca1f4102 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -1,5 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; +import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; /** @@ -29,16 +49,15 @@ public final class ClientLocation { */ private String deviceId; + /** + * 链接ID + */ + private String connId; - public String getDeviceId() { - return deviceId; - } - - public ClientLocation setDeviceId(String deviceId) { - this.deviceId = deviceId; - return this; - } - + /** + * 客户端类型 + */ + private transient int clientType; public String getHost() { return host; @@ -53,26 +72,64 @@ public String getOsName() { return osName; } - public ClientLocation setOsName(String osName) { + public void setOsName(String osName) { this.osName = osName; - return this; } public String getClientVersion() { return clientVersion; } - public ClientLocation setClientVersion(String clientVersion) { + public void setClientVersion(String clientVersion) { this.clientVersion = clientVersion; - return this; } - public static ClientLocation from(SessionContext context) { - ClientLocation config = new ClientLocation(); - config.osName = context.osName; - config.clientVersion = context.clientVersion; - config.deviceId = context.deviceId; - return config; + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getConnId() { + return connId; + } + + public void setConnId(String connId) { + this.connId = connId; + } + + public int getClientType() { + if (clientType == 0) { + clientType = ClientType.find(osName).type; + } + return clientType; + } + + public static ClientLocation from(Connection connection) { + SessionContext context = connection.getSessionContext(); + ClientLocation location = new ClientLocation(); + location.osName = context.osName; + location.clientVersion = context.clientVersion; + location.deviceId = context.deviceId; + location.connId = connection.getId(); + return location; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ClientLocation location = (ClientLocation) o; + + return clientType == location.clientType; + } + + @Override + public int hashCode() { + return Integer.hashCode(clientType); } @Override @@ -82,6 +139,7 @@ public String toString() { ", osName='" + osName + '\'' + ", clientVersion='" + clientVersion + '\'' + ", deviceId='" + deviceId + '\'' + + ", connId='" + connId + '\'' + '}'; } } diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientType.java b/mpush-api/src/main/java/com/mpush/api/router/ClientType.java new file mode 100644 index 00000000..14216be5 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientType.java @@ -0,0 +1,58 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.router; + +import java.util.Arrays; + +/** + * Created by ohun on 16/8/18. + * + * @author ohun@live.cn (夜色) + */ +public enum ClientType { + MOBILE(1, "android", "ios"), + PC(2, "windows", "mac", "linux"), + WEB(3, "web", "h5"), + UNKNOWN(-1); + + public final int type; + public final String[] os; + + ClientType(int type, String... os) { + this.type = type; + this.os = os; + } + + public boolean contains(String osName) { + return Arrays.stream(os).anyMatch(s -> s.equalsIgnoreCase(osName)); + } + + public static boolean isSameClient(String osName1, String osName2) { + if (osName1.equals(osName2)) return true; + return find(osName1).contains(osName2); + } + + public static ClientType find(String osName) { + for (ClientType type : values()) { + if (type.contains(osName)) return type; + } + return UNKNOWN; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/Router.java b/mpush-api/src/main/java/com/mpush/api/router/Router.java index f5b1582c..acc39a90 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/Router.java +++ b/mpush-api/src/main/java/com/mpush/api/router/Router.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; /** diff --git a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index 70dd37ae..cd1bc7e3 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -1,5 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; +import java.util.Set; + /** * Created by ohun on 2015/12/23. * @@ -20,15 +41,25 @@ public interface RouterManager { * 删除路由 * * @param userId + * @param clientType + * @return + */ + boolean unRegister(String userId, int clientType); + + /** + * 查询路由 + * + * @param userId * @return */ - boolean unRegister(String userId); + Set lookupAll(String userId); /** * 查询路由 * * @param userId + * @param clientType * @return */ - R lookup(String userId); + R lookup(String userId, int clientType); } diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java new file mode 100644 index 00000000..8d63c14b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -0,0 +1,151 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public abstract class BaseService implements Service { + + protected final AtomicBoolean started = new AtomicBoolean(); + + @Override + public void init() { + } + + @Override + public boolean isRunning() { + return started.get(); + } + + protected void tryStart(Listener listener, Function function) { + listener = wrap(listener); + if (started.compareAndSet(false, true)) { + try { + init(); + function.apply(listener); + listener.onSuccess("service " + this.getClass().getSimpleName() + " start success"); + } catch (Throwable e) { + listener.onFailure(e); + throw new ServiceException(e); + } + } else { + listener.onFailure(new ServiceException("service already started.")); + } + } + + protected void tryStop(Listener listener, Function function) { + listener = wrap(listener); + if (started.compareAndSet(true, false)) { + try { + function.apply(listener); + listener.onSuccess("service " + this.getClass().getSimpleName() + " stop success"); + } catch (Throwable e) { + listener.onFailure(e); + throw new ServiceException(e); + } + } else { + listener.onFailure(new ServiceException("service already stopped.")); + } + } + + public final Future start() { + FutureListener listener = new FutureListener(); + start(listener); + return listener; + } + + public final Future stop() { + FutureListener listener = new FutureListener(); + stop(listener); + return listener; + } + + @Override + public void start(Listener listener) { + tryStart(listener, this::doStart); + } + + @Override + public void stop(Listener listener) { + tryStop(listener, this::doStop); + } + + protected abstract void doStart(Listener listener) throws Throwable; + + protected abstract void doStop(Listener listener) throws Throwable; + + protected interface Function { + void apply(Listener l) throws Throwable; + } + + /** + * 防止Listener被重复执行 + * + * @param l + * @return + */ + public FutureListener wrap(Listener l) { + if (l == null) return new FutureListener(); + if (l instanceof FutureListener) return (FutureListener) l; + return new FutureListener(l); + } + + protected class FutureListener extends FutureTask implements Listener { + private final Listener l;// 防止Listener被重复执行 + + public FutureListener() { + super(BaseService.this::isRunning); + this.l = null; + } + + public FutureListener(Listener l) { + super(BaseService.this::isRunning); + this.l = l; + } + + @Override + public void onSuccess(Object... args) { + if (isDone()) return;// 防止Listener被重复执行 + set(started.get()); + if (l != null) l.onSuccess(args); + } + + @Override + public void onFailure(Throwable cause) { + if (isDone()) return;// 防止Listener被重复执行 + set(started.get()); + setException(cause); + if (l != null) l.onFailure(cause); + throw new ServiceException(cause); + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + throw new UnsupportedOperationException(); + } + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/Client.java b/mpush-api/src/main/java/com/mpush/api/service/Client.java new file mode 100644 index 00000000..4cb61fb0 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Client.java @@ -0,0 +1,24 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +public interface Client extends Service { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/Listener.java b/mpush-api/src/main/java/com/mpush/api/service/Listener.java new file mode 100644 index 00000000..51d1c18b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Listener.java @@ -0,0 +1,7 @@ +package com.mpush.api.service; + +public interface Listener { + void onSuccess(Object... args); + + void onFailure(Throwable cause); +} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/service/Server.java b/mpush-api/src/main/java/com/mpush/api/service/Server.java new file mode 100644 index 00000000..5249daba --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Server.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +/** + * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn + */ +public interface Server extends Service { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/Service.java b/mpush-api/src/main/java/com/mpush/api/service/Service.java new file mode 100644 index 00000000..c158751b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Service.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +import java.util.concurrent.Future; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public interface Service { + + void start(Listener listener); + + void stop(Listener listener); + + Future start(); + + Future stop(); + + void init(); + + boolean isRunning(); + +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java b/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java new file mode 100644 index 00000000..63f05bf4 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.api.service; + +/** + * Created by yxx on 2016/5/27. + * + * @author ohun@live.cn (夜色) + */ +public class ServiceException extends RuntimeException { + + public ServiceException(String message) { + super(message); + } + + public ServiceException(Throwable cause) { + super(cause); + } + + public ServiceException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java new file mode 100644 index 00000000..3e99ce53 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public interface Factory { + T get(); +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java similarity index 62% rename from mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java rename to mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index 3e8cc3d1..59df059f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -1,11 +1,30 @@ -package com.mpush.tools.spi; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi; import java.util.Iterator; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; -public class ServiceContainer { +public final class SpiLoader { private static final Map CACHE = new ConcurrentHashMap<>(); public static T load(Class clazz) { @@ -32,7 +51,7 @@ public static T load0(Class clazz, String name) { T t = filterByName(factories, name); if (t == null) { - factories = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()); + factories = ServiceLoader.load(clazz, SpiLoader.class.getClassLoader()); t = filterByName(factories, name); } @@ -52,7 +71,8 @@ private static T filterByName(ServiceLoader factories, String name) { } else { while (it.hasNext()) { T t = it.next(); - if (name.equals(t.getClass().getSimpleName())) { + if (name.equals(t.getClass().getName()) || + name.equals(t.getClass().getSimpleName())) { return t; } } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java new file mode 100644 index 00000000..6abcd77c --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java @@ -0,0 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.client; + +import com.mpush.api.push.PushSender; +import com.mpush.api.spi.Factory; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public interface PusherFactory extends Factory { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java new file mode 100644 index 00000000..84d394cb --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.common; + +import java.util.concurrent.Executor; + +/** + * Created by yxx on 2016/5/20. + * + * @author ohun@live.cn + */ +public interface ThreadPoolFactory { + String SERVER_BOSS = "sb"; + String SERVER_WORK = "sw"; + String HTTP_CLIENT_WORK = "hcw"; + String PUSH_CALLBACK = "pc"; + String EVENT_BUS = "eb"; + String MQ = "r"; + String BIZ = "b"; + + Executor get(String name); +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java new file mode 100644 index 00000000..b3a486b9 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java @@ -0,0 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.core; + +import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.Factory; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public interface CipherFactory extends Factory { + Cipher get(); +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java new file mode 100644 index 00000000..0270d4d3 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.net; + + +import java.net.URL; +import java.util.Objects; + +public class DnsMapping { + private String ip; + private int port; + + public DnsMapping(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public String getIp() { + return ip; + } + + public int getPort() { + return port; + } + + public static DnsMapping parse(String addr) { + String[] host_port = Objects.requireNonNull(addr, "dns mapping can not be null") + .split(":"); + if (host_port.length == 1) { + return new DnsMapping(host_port[0], 80); + } else { + return new DnsMapping(host_port[0], Integer.valueOf(host_port[1])); + } + } + + public String translate(URL uri) { + StringBuilder sb = new StringBuilder(128); + sb.append(uri.getProtocol()).append("://") + .append(ip) + .append(':') + .append(port) + .append(uri.getPath()); + String query = uri.getQuery(); + if (query != null) sb.append('?').append(query); + return sb.toString(); + } + + @Override + public String toString() { + return ip + ":" + port; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java new file mode 100644 index 00000000..17bfc637 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.net; + +import com.mpush.api.service.Service; +import com.mpush.api.spi.SpiLoader; + +/** + * Created by yxx on 2016/5/23. + * + * @author ohun@live.cn (夜色) + */ +public interface DnsMappingManager extends Service { + + static DnsMappingManager create() { + return SpiLoader.load(DnsMappingManager.class); + } + + DnsMapping lookup(String origin); +} diff --git a/mpush-boot/.gitignore b/mpush-boot/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-boot/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index 8d71b814..24fc2cd4 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -1,27 +1,47 @@ - - release - mpush + + release-${mpush.version} + mpush-${mpush.version} true tar.gz - target/classes/ + ../ - config.properties + LICENSE + README.md + + + + ../bin/ + bin + + *.sh + *.cmd + + + + ../conf/ + conf + + reference.conf + + + + target/classes/ + conf + + mpush.conf target/ - + bin - boot.jar + bootstrap.jar diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index c5eb3d3f..cb0b9a07 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -25,14 +25,10 @@ ${mpush.groupId} mpush-zk - - org.codehaus.janino - janino - - boot + bootstrap ../conf/conf-${deploy.env}.properties @@ -57,9 +53,9 @@ true - lib/ + ../lib/ - com.mpush.Main + com.mpush.bootstrap.Main diff --git a/mpush-boot/src/main/java/com/mpush/Main.java b/mpush-boot/src/main/java/com/mpush/Main.java deleted file mode 100644 index 295480e4..00000000 --- a/mpush-boot/src/main/java/com/mpush/Main.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush; - -import com.mpush.tools.ConsoleLog; - -public class Main { - - public static void main(String[] args) { - ConsoleLog.i("launch app..."); - ServerLauncher launcher = new ServerLauncher(); - launcher.start(); - addHook(launcher); - } - - private static void addHook(final ServerLauncher serverBoot) { - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - serverBoot.stop(); - ConsoleLog.i("jvm exit all server stopped..."); - } - }); - } - -} diff --git a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java deleted file mode 100644 index 87315101..00000000 --- a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.mpush; - - -import com.mpush.api.Server; -import com.mpush.boot.*; -import com.mpush.core.server.AdminServer; -import com.mpush.core.server.ConnectionServer; -import com.mpush.core.server.GatewayServer; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.zk.ZKServerNode; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ServerLauncher { - private final ZKServerNode csNode = ZKServerNode.csNode(); - - private final ZKServerNode gsNode = ZKServerNode.gsNode(); - - private final Server connectServer = new ConnectionServer(csNode.getPort()); - - private final Server gatewayServer = new GatewayServer(gsNode.getPort()); - - private final Server adminServer = new AdminServer(ConfigCenter.I.adminPort()); - - - public void start() { - BootChain chain = BootChain.chain(); - chain.boot() - .setNext(new RedisBoot())//1.注册redis sever 到ZK - .setNext(new ZKBoot())//2.启动ZK节点数据变化监听 - .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 - .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 - .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 - .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns - .setNext(new MonitorBoot())//7.启动监控 - .setNext(new LastBoot());//8.启动结束 - chain.run(); - } - - public void stop() { - stopServer(gatewayServer); - stopServer(gatewayServer); - stopServer(adminServer); - } - - private void stopServer(Server server) { - if (server != null) { - server.stop(null); - } - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java b/mpush-boot/src/main/java/com/mpush/boot/BootChain.java deleted file mode 100644 index fb4183f4..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.ConsoleLog; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class BootChain { - private BootJob first = first(); - - public void run() { - first.run(); - } - - public static BootChain chain() { - return new BootChain(); - } - - private BootJob first() { - return new BootJob() { - @Override - public void run() { - ConsoleLog.i("begin run boot chain..."); - next(); - } - }; - } - - public BootJob boot() { - return first; - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java b/mpush-boot/src/main/java/com/mpush/boot/BootJob.java deleted file mode 100644 index 4273651d..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.ConsoleLog; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public abstract class BootJob { - private BootJob next; - - abstract void run(); - - public void next() { - if (next != null) { - ConsoleLog.i("run next boot [" + next.getClass().getSimpleName() + "]"); - next.run(); - } - } - - public BootJob setNext(BootJob next) { - this.next = next; - return next; - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java deleted file mode 100644 index 850df861..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.manage.DnsMappingManage; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class HttpProxyBoot extends BootJob { - @Override - void run() { - if (ConfigCenter.I.httpProxyEnable()) { - DnsMappingManage.holder.init(); - } - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java b/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java deleted file mode 100644 index 4be3db0c..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.mpush.boot; - -import com.mpush.common.manage.user.UserManager; -import com.mpush.tools.ConsoleLog; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class LastBoot extends BootJob { - @Override - public void run() { - UserManager.INSTANCE.clearUserOnlineData(); - ConsoleLog.i("end run boot chain..."); - ConsoleLog.i("app start success..."); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java deleted file mode 100644 index be85f539..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mpush.boot; - -import com.mpush.monitor.service.MonitorDataCollector; -import com.mpush.tools.config.ConfigCenter; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class MonitorBoot extends BootJob { - @Override - void run() { - MonitorDataCollector.start(ConfigCenter.I.skipDump()); - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java deleted file mode 100644 index c13efa35..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.boot; - -import com.google.common.base.Strings; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.zk.ZKClient; - -import java.util.List; - -import static com.mpush.zk.ZKPath.REDIS_SERVER; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class RedisBoot extends BootJob { - - @Override - public void run() { - List groupList = ConfigCenter.I.redisGroups(); - if (groupList.size() > 0) { - if (ConfigCenter.I.forceWriteRedisGroupInfo()) { - register(groupList); - } else if (!ZKClient.I.isExisted(REDIS_SERVER.getPath())) { - register(groupList); - } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getPath()))) { - register(groupList); - } - } else { - throw new RuntimeException("init redis sever fail groupList is null"); - } - - RedisManage.test(groupList); - next(); - } - - private void register(List groupList) { - String data = Jsons.toJson(groupList); - ZKClient.I.registerPersist(REDIS_SERVER.getPath(), data); - ConsoleLog.i("register redis server group success, group=" + data); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java deleted file mode 100644 index ff80675d..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.mpush.boot; - -import com.mpush.api.Server; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Jsons; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKServerNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ServerBoot extends BootJob { - private final Logger logger = LoggerFactory.getLogger(ServerBoot.class); - - private final Server server; - private final ZKServerNode node; - - public ServerBoot(Server server, ZKServerNode node) { - this.server = server; - this.node = node; - } - - @Override - public void run() { - final String serverName = server.getClass().getSimpleName(); - ThreadPoolManager.newThread(serverName, new Runnable() { - @Override - public void run() { - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess(int port) { - String msg = "start " + serverName + " success listen:" + port; - logger.error(msg); - ConsoleLog.i(msg); - if (node != null) { - registerServerToZk(node.getZkPath(), Jsons.toJson(node)); - } - next(); - } - - @Override - public void onFailure(Throwable cause) { - String msg = "start " + serverName + " failure, jvm exit with code -1"; - logger.error(msg); - ConsoleLog.e(cause, msg); - System.exit(-1); - } - }); - } - }).start(); - } - - //step7 注册应用到zk - public void registerServerToZk(String path, String value) { - ZKClient.I.registerEphemeralSequential(path, value); - String msg = "register server node=" + value + " to zk path=" + path; - logger.error(msg); - ConsoleLog.i(msg); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java deleted file mode 100644 index dbdb2360..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mpush.boot; - -import com.google.common.collect.Lists; -import com.mpush.zk.ZKClient; -import com.mpush.zk.listener.ZKDataChangeListener; -import com.mpush.zk.listener.ZKRedisNodeListener; - -import java.util.List; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ZKBoot extends BootJob { - private List dataChangeListeners = Lists.newArrayList(); - - public ZKBoot() { - registerListener(new ZKRedisNodeListener()); - } - - public void registerListener(ZKDataChangeListener listener) { - dataChangeListeners.add(listener); - } - - private void registerListeners() { - for (ZKDataChangeListener listener : dataChangeListeners) { - ZKClient.I.registerListener(listener); - } - } - - private void initListenerData() { - for (ZKDataChangeListener listener : dataChangeListeners) { - listener.initData(); - } - } - - - @Override - public void run() { - registerListeners(); - initListenerData(); - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java b/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java new file mode 100644 index 00000000..eb63eb23 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java @@ -0,0 +1,35 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class BootException extends RuntimeException { + public BootException(String s) { + super(s); + } + + public BootException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java new file mode 100644 index 00000000..2a0550ff --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap; + +import com.mpush.tools.log.Logs; + +public class Main { + + public static void main(String[] args) { + Logs.init(); + Logs.Console.error("launch mpush server..."); + ServerLauncher launcher = new ServerLauncher(); + launcher.start(); + addHook(launcher); + } + + private static void addHook(final ServerLauncher launcher) { + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + launcher.stop(); + Logs.Console.error("jvm exit, all server stopped..."); + } + }); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java new file mode 100644 index 00000000..18edd96b --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -0,0 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap; + + +import com.mpush.api.service.Server; +import com.mpush.bootstrap.job.*; +import com.mpush.core.server.AdminServer; +import com.mpush.core.server.ConnectionServer; +import com.mpush.core.server.GatewayServer; +import com.mpush.monitor.service.MonitorService; +import com.mpush.tools.config.CC; +import com.mpush.zk.ZKClient; +import com.mpush.zk.node.ZKServerNode; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ServerLauncher { + private final ZKServerNode csNode = ZKServerNode.csNode(); + + private final ZKServerNode gsNode = ZKServerNode.gsNode(); + + private final ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); + + private final GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); + + private final AdminServer adminServer = new AdminServer(CC.mp.net.admin_server_port, connectServer, gatewayServer); + + + public void start() { + BootChain chain = BootChain.chain(); + chain.boot() + .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 + .setNext(new RedisBoot())//2.注册redis sever 到ZK + .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 + .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 + .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 + .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns + .setNext(new MonitorBoot())//7.启动监控 + .setNext(new LastBoot());//8.启动结束 + chain.run(); + } + + public void stop() { + stopServer(connectServer); + stopServer(gatewayServer); + stopServer(adminServer); + ZKClient.I.stop(); + MonitorService.I.stop(); + } + + private void stopServer(Server server) { + if (server != null) { + server.stop(null); + } + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java new file mode 100644 index 00000000..32544f85 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -0,0 +1,53 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class BootChain { + private BootJob first = first(); + + public void run() { + first.run(); + } + + public static BootChain chain() { + return new BootChain(); + } + + private BootJob first() { + return new BootJob() { + @Override + public void run() { + Logs.Console.error("begin run bootstrap chain..."); + next(); + } + }; + } + + public BootJob boot() { + return first; + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java new file mode 100644 index 00000000..32bb5b36 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public abstract class BootJob { + private BootJob next; + + abstract void run(); + + public void next() { + if (next != null) { + Logs.Console.error("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); + next.run(); + } + } + + public BootJob setNext(BootJob next) { + this.next = next; + return next; + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java new file mode 100644 index 00000000..c1a01431 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -0,0 +1,40 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMappingManager; +import com.mpush.common.net.HttpProxyDnsMappingManager; +import com.mpush.tools.config.CC; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class HttpProxyBoot extends BootJob { + @Override + void run() { + if (CC.mp.http.proxy_enabled) { + SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager).start(); + } + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java new file mode 100644 index 00000000..bc194877 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.common.user.UserManager; +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class LastBoot extends BootJob { + @Override + public void run() { + UserManager.I.clearUserOnlineData(); + Logs.Console.error("end run bootstrap chain..."); + Logs.Console.error("==================================================================="); + Logs.Console.error("====================MPUSH SERVER START SUCCESS====================="); + Logs.Console.error("==================================================================="); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java new file mode 100644 index 00000000..d0b59ae7 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -0,0 +1,35 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.monitor.service.MonitorService; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class MonitorBoot extends BootJob { + @Override + void run() { + MonitorService.I.start(); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java new file mode 100644 index 00000000..e98ca3ee --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -0,0 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.cache.redis.manager.RedisManager; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class RedisBoot extends BootJob { + + @Override + public void run() { + RedisManager.I.init(); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java new file mode 100644 index 00000000..86bde932 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -0,0 +1,76 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import com.mpush.zk.ZKClient; +import com.mpush.zk.node.ZKServerNode; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ServerBoot extends BootJob { + private final Server server; + private final ZKServerNode node; + + public ServerBoot(Server server, ZKServerNode node) { + this.server = server; + this.node = node; + } + + @Override + public void run() { + final String serverName = server.getClass().getSimpleName(); + ThreadPoolManager.I.newThread(serverName, new Runnable() { + @Override + public void run() { + server.init(); + server.start(new Listener() { + @Override + public void onSuccess(Object... args) { + Logs.Console.error("start " + serverName + " success listen:" + args[0]); + if (node != null) { + registerServerToZk(node.getZkPath(), Jsons.toJson(node)); + } + next(); + } + + @Override + public void onFailure(Throwable cause) { + Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); + System.exit(-1); + } + }); + } + }).start(); + } + + //step7 注册应用到zk + public void registerServerToZk(String path, String value) { + ZKClient.I.registerEphemeralSequential(path, value); + Logs.Console.error("register server node=" + value + " to zk name=" + path); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java new file mode 100644 index 00000000..e8c33d1d --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.service.Listener; +import com.mpush.bootstrap.BootException; +import com.mpush.zk.ZKClient; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKBoot extends BootJob { + + @Override + public void run() { + ZKClient.I.start(new Listener() { + @Override + public void onSuccess(Object... args) { + next(); + } + + @Override + public void onFailure(Throwable cause) { + throw new BootException("init zk client failure", cause); + } + }); + } +} diff --git a/mpush-boot/src/main/resources/config.properties b/mpush-boot/src/main/resources/config.properties deleted file mode 100644 index 27237da8..00000000 --- a/mpush-boot/src/main/resources/config.properties +++ /dev/null @@ -1,37 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 180000 -## -max_hb_timeout_times = 2 -## -private_key = ${private_key} -## -public_key = ${public_key} -## -gateway_server_port = ${gateway_server_port} -## -connection_server_port = ${connection_server_port} -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 - -## zk ip -zk_ip = ${zk_ip} -zk_namespace = ${zk_namespace} -zk_digest = ${zk_digest} - -redis_group = ${redis_group} -force_write_redis_group_info = ${force_write_redis_group_info} -jvm_log_path = /opt/shinemo/mpush/ -dns_mapping=${dns_mapping} -skip_dump=${skip_dump} -admin_port=${admin_port} -remote_ip_mapping=${remote_ip_mapping} diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml index 17dc0dee..7b8ebb77 100644 --- a/mpush-boot/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -1,209 +1,215 @@ + + + + + + + + + + ${log.home}/mpush.log + true + + ${log.home}/mpush.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/info-mpush.log + + info + ACCEPT + DENY + + true + + ${log.home}/info-mpush.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/debug-mpush.log + + debug + ACCEPT + DENY + + true + + ${log.home}/debug-mpush.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/monitor-mpush.log + true + + ${log.home}/monitor-mpush.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - ${log.home}/mpush.error.log - - error - ACCEPT - DENY - - true - - ${log.home}/mpush.error.log.%d{yyyy-MM-dd} - - 3 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/conn-mpush.log + true + + ${log.home}/conn-mpush.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - ${log.home}/mpush.info.log - - info - ACCEPT - DENY - - true - - ${log.home}/mpush.info.log.%d{yyyy-MM-dd} - - 3 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/push-mpush.log + true + + ${log.home}/push-mpush.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - - ${log.home}/mpush.log - true - - ${log.home}/mpush.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/heartbeat-mpush.log + true + + ${log.home}/heartbeat-mpush.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - - - ${log.home}/mpush-monitor.log - true - - ${log.home}/mpush-monitor.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/redis-mpush.log + true + + ${log.home}/redis-mpush.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + ${log.home}/http-mpush.log + true + + ${log.home}/http-mpush.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - + + + ${log.home}/zk-mpush.log + true + + ${log.home}/zk-mpush.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + System.out + UTF-8 + + DEBUG + + + %d{HH:mm:ss.SSS} - %msg%n + + + + + + + + + + - + - - - - - ${log.home}/mpush-connection.log - true - - ${log.home}/mpush-connection.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - - - - - ${log.home}/mpush-push.log - true - - ${log.home}/mpush-push.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - + - - - - - - - - - - ${log.home}/mpush-heartbeat.log - true - - ${log.home}/mpush-heartbeat.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - + - - - - - ${log.home}/mpush-redis.log - true - - ${log.home}/mpush-redis.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + + + + + + + + + - - - - - ${log.home}/mpush-http.log - true - - ${log.home}/mpush-http.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-zk.log - true - - ${log.home}/mpush-zk.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - - - - - - + + + + diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf new file mode 100644 index 00000000..6ee565ff --- /dev/null +++ b/mpush-boot/src/main/resources/mpush.conf @@ -0,0 +1,4 @@ +mp.log.level=${log.level} +mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" +mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" +mp.zk.namespace=mpush diff --git a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java b/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java deleted file mode 100644 index 2a2d6d5e..00000000 --- a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mpush; - -import org.junit.Test; - -public class ConnectionServerApplicationTest { - - @Test - public void testJson() throws Exception{ - - - } - -} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java b/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java deleted file mode 100644 index d4c66e7b..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java +++ /dev/null @@ -1,80 +0,0 @@ -//package com.mpush.core.zk; -// -//import java.util.concurrent.CountDownLatch; -//import java.util.concurrent.Executor; -//import java.util.concurrent.Executors; -// -//import org.junit.Test; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import MPushUtil; -//import com.mpush.tools.zk.ServerApp; -//import com.mpush.tools.zk.ZKPath; -//import com.mpush.tools.zk.manage.ServerManage; -// -//public class ServerManageTest { -// -// private static Executor executor = Executors.newCachedThreadPool(); -// -// private ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// -// private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); -// -// @Test -// public void testMulThreadRegisterApp() throws InterruptedException { -// CountDownLatch latch = new CountDownLatch(1); -// for (int i = 1; i <= 2; i++) { -// executor.execute(new Worker("192.168.1." + i, latch)); -// } -// latch.countDown(); -// -// Thread.sleep(Integer.MAX_VALUE); -// } -// -// -// @Test -// public void testServerManageStart() throws InterruptedException { -// manage.start(); -// Thread.sleep(Integer.MAX_VALUE); -// } -// -// -// private static class Worker implements Runnable { -// -// private static final Logger log = LoggerFactory.getLogger(Worker.class); -// -// private final String ip; -// private final CountDownLatch latch; -// -// public Worker(String ip, CountDownLatch latch) { -// this.ip = ip; -// this.latch = latch; -// } -// -// @Override -// public void run() { -// try { -// latch.await(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// log.warn("start init " + ip); -// ServerApp app = new ServerApp(ip, 3000); -// ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); -// manage.start(); -// -// try { -// Thread.sleep(20000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// manage.close(); -// -// log.warn("end init " + ip); -// } -// -// } -// -//} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java deleted file mode 100644 index 52c93350..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.zk; - -import org.junit.Test; - - -public class ZkTest { - - @Test - public void remove() { - ZKClient zkRegister = ZKClient.I; - - zkRegister.remove("/"); - - } - -} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java deleted file mode 100644 index 8f49408a..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.mpush.zk; - -import com.google.common.collect.Lists; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -public class ZkUtilTest { - - private ZKClient zkUtil = ZKClient.I; - - @Before - public void setUp() throws Exception { - } - - @Test - public void test() { - - String dubbo = zkUtil.get("/dubbo"); - System.out.println(dubbo); - - List child = zkUtil.getChildrenKeys("/dubbo"); - System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); - - zkUtil.registerPersist("/hi", "hello world"); - - zkUtil.registerEphemeral("/huang", "hi"); - zkUtil.registerEphemeralSequential("/huang2"); - - String huang = zkUtil.get("/huang"); - System.out.println(huang); - - String huang2 = zkUtil.get("/huang2"); - System.out.println(huang2); - - } - - @Test - public void getTest() { - String value = zkUtil.get("/hi"); - System.out.println(value); - } - - /** - * 注册机器到/mpush/allhost 目录下边 - */ - @Test - public void testRegister() { - - String path = "/" + zkUtil.getZKConfig().getNamespace(); - - String prefix = "machine"; - - List hosts = zkUtil.getChildrenKeys(path); - - System.out.println("before register"); - - for (int i = 0; i < hosts.size(); i++) { - String value = zkUtil.get(hosts.get(i)); - System.out.println(hosts.get(i) + "," + value); - } - - System.out.println("start register"); - - zkUtil.registerEphemeralSequential(path + "/" + prefix); - - zkUtil.registerEphemeralSequential(path + "/" + prefix); - - hosts = zkUtil.getChildrenKeys(path); - - for (int i = 0; i < hosts.size(); i++) { - String value = zkUtil.get(path + "/" + hosts.get(i)); - System.out.println(hosts.get(i) + "," + value); - } - - System.out.println("end register"); - - } - - @Test - public void testLocalIp() { - System.out.println(MPushUtil.getLocalIp()); - - } - - @Test - public void testRegisterIp() throws Exception { - String localIp = MPushUtil.getInetAddress(); - ZKServerNode app = new ZKServerNode(); - zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); - String value = zkUtil.get("/" + localIp); - System.out.println(value); - } - - @Test - public void testRemove() { - zkUtil.remove("/"); - } - - @Test - public void testAddKickOff() { - String localIp = MPushUtil.getInetAddress(); - String kick = "kickoff"; - String ip = "10.1.10.65"; - zkUtil.registerEphemeral("/" + localIp + "/" + kick, ip); - - } - - @Test - public void testAddRedis() { - - RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); - //RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - RedisGroup group1 = new RedisGroup(); - group1.addRedisNode(node1); - - /*RedisGroup group2 = new RedisGroup(); - group2.addRedisNode(node2);*/ - - List groupList = Lists.newArrayList(group1); - - zkUtil.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - - - } - - @Test - public void getRedisTest() { - String value = zkUtil.get(ZKPath.REDIS_SERVER.getPath()); - List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); - for (RedisGroup group : newGroupList) { - for (RedisNode node : group.getRedisNodeList()) { - System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - - -} diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml new file mode 100644 index 00000000..0f1b4dae --- /dev/null +++ b/mpush-cache/pom.xml @@ -0,0 +1,27 @@ + + + + mpush + com.mpush + 1.0 + + 4.0.0 + + ${mpush.groupId} + mpush-cache + ${mpush.version} + + + + ${mpush.groupId} + mpush-zk + + + redis.clients + jedis + + + + \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java similarity index 62% rename from mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 6c160b30..2ff4c2cf 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -1,38 +1,80 @@ -package com.mpush.tools.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.log.Logs; -import com.mpush.tools.Constants; import com.mpush.tools.Jsons; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import redis.clients.jedis.*; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class RedisUtil { - - private static Map holder = Maps.newConcurrentMap(); - - public static Jedis getClient(RedisNode node) { - JedisPool pool = holder.get(node); +import java.util.*; + +import static redis.clients.jedis.Protocol.DEFAULT_TIMEOUT; + +public class RedisClient { + public static final JedisPoolConfig CONFIG = buildConfig(); + private static final Map POOL_MAP = Maps.newConcurrentMap(); + + private static JedisPoolConfig buildConfig() { + JedisPoolConfig config = new JedisPoolConfig(); + //连接池中最大连接数。高版本:maxTotal,低版本:maxActive + config.setMaxTotal(CC.mp.redis.config.maxTotal); + //连接池中最大空闲的连接数 + config.setMaxIdle(CC.mp.redis.config.maxIdle); + //连接池中最少空闲的连接数 + config.setMinIdle(CC.mp.redis.config.minIdle); + //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait + config.setMaxWaitMillis(CC.mp.redis.config.maxWaitMillis); + //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 + config.setMinEvictableIdleTimeMillis(CC.mp.redis.config.minEvictableIdleTimeMillis); + //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 + config.setNumTestsPerEvictionRun(CC.mp.redis.config.numTestsPerEvictionRun); + //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 + config.setTimeBetweenEvictionRunsMillis(CC.mp.redis.config.timeBetweenEvictionRunsMillis); + //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. + config.setTestOnBorrow(CC.mp.redis.config.testOnBorrow); + //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 + config.setTestOnReturn(CC.mp.redis.config.testOnReturn); + //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. + config.setTestWhileIdle(CC.mp.redis.config.testWhileIdle); + return config; + } + + public static Jedis getClient(RedisServer node) { + JedisPool pool = POOL_MAP.get(node); if (pool == null) { - pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); - holder.put(node, pool); + pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), DEFAULT_TIMEOUT, node.getPassword()); + POOL_MAP.put(node, pool); } return pool.getResource(); } public static void close(Jedis jedis) { - jedis.close(); + if (jedis != null) jedis.close(); } - public static long incr(List nodeList, String key, Integer time) { + public static long incr(List nodeList, String key, Integer time) { long incrRet = -1; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -42,7 +84,7 @@ public static long incr(List nodeList, String key, Integer time) { } incrRet = ret; } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{},{},{},{}", key, time, node, e); + Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -52,16 +94,16 @@ public static long incr(List nodeList, String key, Integer time) { } - public static long incrBy(List nodeList, String key, long delt) { + public static long incrBy(List nodeList, String key, long delt) { long incrRet = -1; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); long ret = jedis.incrBy(key, delt); incrRet = ret; } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{},{},{},{}", key, delt, node, e); + Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, delt, node, e); } finally { // 返还到连接池 close(jedis); @@ -79,7 +121,7 @@ public static long incrBy(List nodeList, String key, long delt) { * @return */ @SuppressWarnings("unchecked") - public static T get(RedisNode node, String key, Class clazz) { + public static T get(RedisServer node, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -87,7 +129,7 @@ public static T get(RedisNode node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - Logs.REDIS.error("redis get exception:{},{}", key, node, e); + Logs.REDIS.error("redis get exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -96,17 +138,15 @@ public static T get(RedisNode node, String key, Class clazz) { return Jsons.fromJson(value, clazz); } - public static void set(List nodeList, String key, String value) { - + public static void set(List nodeList, String key, String value) { set(nodeList, key, value, null); - } - public static void set(List nodeList, String key, T value) { + public static void set(List nodeList, String key, T value) { set(nodeList, key, value, null); } - public static void set(List nodeList, String key, T value, Integer time) { + public static void set(List nodeList, String key, T value, Integer time) { String jsonValue = Jsons.toJson(value); set(nodeList, key, jsonValue, time); } @@ -117,11 +157,11 @@ public static void set(List nodeList, String key, T value, Intege * @param value * @param time seconds */ - public static void set(List nodeList, String key, String value, Integer time) { + public static void set(List nodeList, String key, String value, Integer time) { if (time == null) { time = -1; } - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -130,7 +170,7 @@ public static void set(List nodeList, String key, String value, Integ jedis.expire(key, time); } } catch (Exception e) { - Logs.REDIS.error("redis set exception:{},{},{},{}", key, value, time, node, e); + Logs.REDIS.error("redis set exception:{}, {}, {}, {}", key, value, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -138,15 +178,15 @@ public static void set(List nodeList, String key, String value, Integ } } - public static void del(List nodeList, String key) { + public static void del(List nodeList, String key) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - Logs.REDIS.error("redis del exception:{},{}", key, node, e); + Logs.REDIS.error("redis del exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -160,14 +200,14 @@ public static void del(List nodeList, String key) { /********************* * hash redis start ********************************/ - public static void hset(List nodeList, String namespace, String key, String value) { - for (RedisNode node : nodeList) { + public static void hset(List nodeList, String key, String field, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hset(namespace, key, value); + jedis.hset(key, field, value); } catch (Exception e) { - Logs.REDIS.error("redis hset exception:{},{},{},{}", namespace, key, value, node, e); + Logs.REDIS.error("redis hset exception:{}, {}, {}, {}", key, field, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -175,19 +215,19 @@ public static void hset(List nodeList, String namespace, String key, } } - public static void hset(List nodeList, String namespace, String key, T value) { - hset(nodeList, namespace, key, Jsons.toJson(value)); + public static void hset(List nodeList, String key, String field, T value) { + hset(nodeList, key, field, Jsons.toJson(value)); } - public static T hget(RedisNode node, String namespace, String key, Class clazz) { + public static T hget(RedisServer node, String key, String field, Class clazz) { String value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hget(namespace, key); + value = jedis.hget(key, field); } catch (Exception e) { - Logs.REDIS.error("redis hget exception:{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hget exception:{}, {}", key, field, node, e); } finally { // 返还到连接池 close(jedis); @@ -196,15 +236,15 @@ public static T hget(RedisNode node, String namespace, String key, Class } - public static void hdel(List nodeList, String namespace, String key) { + public static void hdel(List nodeList, String key, String field) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hdel(namespace, key); + jedis.hdel(key, field); } catch (Exception e) { - Logs.REDIS.error("redis hdel exception:{},{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hdel exception:{}, {}, {}", key, field, node, e); } finally { // 返还到连接池 close(jedis); @@ -212,14 +252,14 @@ public static void hdel(List nodeList, String namespace, String key) } } - public static Map hgetAll(RedisNode node, String namespace) { + public static Map hgetAll(RedisServer node, String key) { Map result = null; Jedis jedis = null; try { jedis = getClient(node); - result = jedis.hgetAll(namespace); + result = jedis.hgetAll(key); } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); + Logs.REDIS.error("redis hgetAll exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -227,32 +267,21 @@ public static Map hgetAll(RedisNode node, String namespace) { return result; } - public static Map hgetAll(RedisNode node, String namespace, Class clazz) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(namespace); - } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); - } finally { - // 返还到连接池 - close(jedis); - } + public static Map hgetAll(RedisServer node, String key, Class clazz) { + Map result = hgetAll(node, key); if (result != null) { Map newMap = Maps.newHashMap(); Iterator> iterator = result.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - String key = entry.getKey(); - String val = entry.getValue(); - newMap.put(key, Jsons.fromJson(val, clazz)); + String k = entry.getKey(); + String v = entry.getValue(); + newMap.put(k, Jsons.fromJson(v, clazz)); } return newMap; } else { return null; } - } /** @@ -262,7 +291,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class * @param key * @return */ - public static Set hkeys(RedisNode node, String key) { + public static Set hkeys(RedisServer node, String key) { Set result = null; Jedis jedis = null; try { @@ -282,32 +311,24 @@ public static Set hkeys(RedisNode node, String key) { * 返回 key 指定的哈希集中指定字段的值 * * @param node - * @param key - * @param clazz * @param fields + * @param clazz * @return */ - public static List hmget(RedisNode node, String namespace, Class clazz, String... key) { + public static List hmget(RedisServer node, String key, Class clazz, String... fields) { List value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hmget(namespace, key); + value = jedis.hmget(key, fields); } catch (Exception e) { - Logs.REDIS.error("redis hmget exception:{},{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hmget exception:{},{},{}", key, fields, node, e); } finally { // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; + return toList(value, clazz); } @@ -316,26 +337,25 @@ public static List hmget(RedisNode node, String namespace, Class clazz * 关联 * * @param nodeList - * @param key * @param hash * @param time */ - public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + public static void hmset(List nodeList, String key, Map hash, Integer time) { if (time == null) { time = -1; } - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hmset(namespace, hash); + jedis.hmset(key, hash); if (time > 0) { - jedis.expire(namespace, time); + jedis.expire(key, time); } } catch (Exception e) { - Logs.REDIS.error("redis hmset exception:{},{},{}", namespace, time, node, e); + Logs.REDIS.error("redis hmset exception:{},{},{}", key, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -344,8 +364,8 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String namespace, Map hash) { - hmset(nodeList, namespace, hash, null); + public static void hmset(List nodeList, String key, Map hash) { + hmset(nodeList, key, hash, null); } /********************* hash redis end ********************************/ @@ -354,9 +374,9 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String key, String value) { + public static void lpush(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -371,7 +391,7 @@ public static void lpush(List nodeList, String key, String value) { } - public static void lpush(List nodeList, String key, T value) { + public static void lpush(List nodeList, String key, T value) { lpush(nodeList, key, Jsons.toJson(value)); @@ -380,9 +400,9 @@ public static void lpush(List nodeList, String key, T value) { /** * 从队列的右边入队 */ - public static void rpush(List nodeList, String key, String value) { + public static void rpush(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -396,16 +416,16 @@ public static void rpush(List nodeList, String key, String value) { } } - public static void rpush(List nodeList, String key, T value) { + public static void rpush(List nodeList, String key, T value) { rpush(nodeList, key, Jsons.toJson(value)); } /** * 移除并且返回 key 对应的 list 的第一个元素 */ - public static T lpop(List nodeList, String key, Class clazz) { + public static T lpop(List nodeList, String key, Class clazz) { String retValue = null; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -426,9 +446,9 @@ public static T lpop(List nodeList, String key, Class clazz) { /** * 从队列的右边出队一个元素 */ - public static T rpop(List nodeList, String key, Class clazz) { + public static T rpop(List nodeList, String key, Class clazz) { String retValue = null; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -451,7 +471,7 @@ public static T rpop(List nodeList, String key, Class clazz) { * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List lrange(RedisNode node, String key, int start, int end, Class clazz) { + public static List lrange(RedisServer node, String key, int start, int end, Class clazz) { List value = null; Jedis jedis = null; try { @@ -463,21 +483,14 @@ public static List lrange(RedisNode node, String key, int start, int end, // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; + return toList(value, clazz); } /** * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key * 里的值不是一个list的话,会返回error。 */ - public static long llen(RedisNode node, String key) { + public static long llen(RedisServer node, String key) { long len = 0; Jedis jedis = null; @@ -500,9 +513,9 @@ public static long llen(RedisNode node, String key) { * @param key * @param value */ - public static void lRem(List nodeList, String key, String value) { + public static void lRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -520,11 +533,11 @@ public static void lRem(List nodeList, String key, String value) { /********************* list redis end ********************************/ /********************* - * pubsub redis start + * mq redis start ********************************/ - public static void publish(RedisNode node, String channel, T message) { + public static void publish(RedisServer node, String channel, T message) { Jedis jedis = null; String value = null; if (message instanceof String) { @@ -543,20 +556,19 @@ public static void publish(RedisNode node, String channel, T message) { } } - public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { - for (final RedisNode node : nodeList) { - String name = node.getIp() + "_" + Jsons.toJson(channels); - ThreadPoolManager.newThread(name, new Runnable() { + public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { + for (final RedisServer node : nodeList) { + new Thread(new Runnable() { @Override public void run() { subscribe(node, pubsub, channels); } - }).start(); + }, node.getHost() + "_" + Jsons.toJson(channels) + ).start(); } } - public static void subscribe(RedisNode node, JedisPubSub pubsub, String... channel) { - + public static void subscribe(RedisServer node, JedisPubSub pubsub, String... channel) { Jedis jedis = null; try { jedis = getClient(node); @@ -577,10 +589,9 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann * @param nodeList * @param key * @param value - * @param time seconds */ - public static void sAdd(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + public static void sAdd(List nodeList, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -595,12 +606,11 @@ public static void sAdd(List nodeList, String key, String value) { } /** - * @param node 返回个数 + * @param node 返回个数 * @param key - * @param clazz * @return */ - public static Long sCard(RedisNode node, String key) { + public static Long sCard(RedisServer node, String key) { Long value = null; Jedis jedis = null; @@ -616,9 +626,9 @@ public static Long sCard(RedisNode node, String key) { return value; } - public static void sRem(List nodeList, String key, String value) { + public static void sRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -639,10 +649,9 @@ public static void sRem(List nodeList, String key, String value) { * @param node * @param key * @param clazz - * @param fields * @return */ - public static List sScan(RedisNode node, String key, Class clazz, int start) { + public static List sScan(RedisServer node, String key, Class clazz, int start) { List value = null; Jedis jedis = null; @@ -658,15 +667,7 @@ public static List sScan(RedisNode node, String key, Class clazz, int // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - + return toList(value, clazz); } /********************* @@ -677,8 +678,8 @@ public static List sScan(RedisNode node, String key, Class clazz, int * @param key * @param value */ - public static void zAdd(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + public static void zAdd(List nodeList, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -693,12 +694,11 @@ public static void zAdd(List nodeList, String key, String value) { } /** - * @param node 返回个数 + * @param node 返回个数 * @param key - * @param clazz * @return */ - public static Long zCard(RedisNode node, String key) { + public static Long zCard(RedisServer node, String key) { Long value = null; Jedis jedis = null; @@ -714,9 +714,9 @@ public static Long zCard(RedisNode node, String key) { return value; } - public static void zRem(List nodeList, String key, String value) { + public static void zRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -736,7 +736,7 @@ public static void zRem(List nodeList, String key, String value) { * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List zrange(RedisNode node, String key, int start, int end, Class clazz) { + public static List zrange(RedisServer node, String key, int start, int end, Class clazz) { Set value = null; Jedis jedis = null; try { @@ -748,6 +748,11 @@ public static List zrange(RedisNode node, String key, int start, int end, // 返还到连接池 close(jedis); } + + return toList(value, clazz); + } + + private static List toList(Collection value, Class clazz) { if (value != null) { List newValue = Lists.newArrayList(); for (String temp : value) { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java new file mode 100644 index 00000000..d7237eef --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.cache.redis; + +/** + * Created by yxx on 2016/5/27. + * + * @author ohun@live.cn (夜色) + */ +public class RedisException extends RuntimeException { + + public RedisException() { + } + + public RedisException(Throwable cause) { + super(cause); + } + + public RedisException(String message) { + super(message); + } + + public RedisException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java new file mode 100644 index 00000000..cd11ef06 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; + +import com.google.common.collect.Lists; + +import java.util.List; + + +/** + * redis 组 + */ +public class RedisGroup { + private List redisServerList = Lists.newArrayList(); + + public List getRedisServerList() { + return redisServerList; + } + + public void setRedisServerList(List redisServerList) { + this.redisServerList = redisServerList; + } + + public void addRedisNode(RedisServer node) { + redisServerList.add(node); + } + + public void remove(int i) { + if (redisServerList != null) { + redisServerList.remove(i); + } + } + + public void clear() { + if (redisServerList != null) { + redisServerList.clear(); + } + } + + public RedisServer get(String key) { + int i = key.hashCode() % redisServerList.size(); + return redisServerList.get(i); + } + + public static RedisGroup from(com.mpush.tools.config.data.RedisGroup node) { + RedisGroup group = new RedisGroup(); + for (com.mpush.tools.config.data.RedisServer rs : node.redisNodeList) { + group.addRedisNode(new RedisServer(rs.getHost(), rs.getPort(), rs.getPassword())); + } + return group; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java similarity index 55% rename from mpush-api/src/main/java/com/mpush/api/RedisKey.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 0077c4d1..b4b82e8a 100644 --- a/mpush-api/src/main/java/com/mpush/api/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -1,8 +1,27 @@ -package com.mpush.api; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; public final class RedisKey { - private static final String USER_PREFIX = "mp_u_"; + private static final String USER_PREFIX = "mp_uc_"; private static final String SESSION_PREFIX = "mp_s_"; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java new file mode 100644 index 00000000..88a4abdc --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; + +/** + * redis 相关的配置信息 + */ +public class RedisServer extends com.mpush.tools.config.data.RedisServer { + + public RedisServer(String ip, int port, String password) { + super(ip, port, password); + } + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java new file mode 100644 index 00000000..ee43587a --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java @@ -0,0 +1,85 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.hash; + +import redis.clients.util.Hashing; + +import java.util.Collection; +import java.util.SortedMap; +import java.util.TreeMap; + +public class ConsistentHash { + + private final Hashing hash; + private final int numberOfReplicas; + private final SortedMap circle = new TreeMap(); + + public ConsistentHash(Hashing hash, int numberOfReplicas, + Collection nodes) { + super(); + this.hash = hash; + this.numberOfReplicas = numberOfReplicas; + for (Node node : nodes) { + add(node); + } + } + + /** + * 增加真实机器节点 + * + * @param node + */ + public void add(Node node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.put(this.hash.hash(node.toString() + i), node); + } + } + + /** + * 删除真实机器节点 + * + * @param node + */ + public void remove(String node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.remove(this.hash.hash(node.toString() + i)); + } + } + + /** + * 取得真实机器节点 + * + * @param key + * @return + */ + public Node get(String key) { + if (circle.isEmpty()) { + return null; + } + long hash = this.hash.hash(key); + if (!circle.containsKey(hash)) { + SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 + hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); + } + return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 + } + + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java new file mode 100644 index 00000000..295dfbf3 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.hash; + +public class Node { + + private String ip; //机器ip + private String name;//名字 + + public Node(String ip, String name) { + this.ip = ip; + this.name = name; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java new file mode 100644 index 00000000..f74f1c27 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.listener; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.cache.redis.mq.Subscriber; +import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; + +public class ListenerDispatcher implements MessageListener { + + public static final ListenerDispatcher I = new ListenerDispatcher(); + + private final Map> subscribes = Maps.newTreeMap(); + + private final Executor executor = ThreadPoolManager.I.getRedisExecutor(); + + private ListenerDispatcher() { + } + + @Override + public void onMessage(final String channel, final String message) { + List listeners = subscribes.get(channel); + if (listeners == null) { + Logs.REDIS.info("cannot find listener:%s,%s", channel, message); + return; + } + for (final MessageListener listener : listeners) { + executor.execute(() -> listener.onMessage(channel, message)); + } + } + + public void subscribe(String channel, MessageListener listener) { + List listeners = subscribes.get(channel); + if (listeners == null) { + listeners = Lists.newArrayList(); + subscribes.put(channel, listeners); + } + listeners.add(listener); + RedisManager.I.subscribe(Subscriber.holder, channel); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java new file mode 100644 index 00000000..da6989d1 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java @@ -0,0 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.listener; + + +public interface MessageListener { + + void onMessage(String channel, String message); + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java new file mode 100644 index 00000000..9c90877b --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java @@ -0,0 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.manager; + +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; + +import java.util.List; + +public interface RedisClusterManager { + + void init(); + + List getGroupList(); + + RedisServer randomGetRedisNode(String key); + + List hashSet(String key); +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java new file mode 100644 index 00000000..50c10e21 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -0,0 +1,318 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.manager; + +import com.google.common.collect.Sets; +import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.tools.Jsons; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPubSub; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * redis 对外封装接口 + */ +public class RedisManager { + public static final RedisManager I = new RedisManager(); + + private final RedisClusterManager clusterManager = ZKRedisClusterManager.I; + + public void init() { + ZKRedisClusterManager.I.init(); + test(clusterManager.getGroupList()); + } + + public long incr(String key, Integer time) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.incr(nodeList, key, time); + } + + public long incrBy(String key, long delt) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.incrBy(nodeList, key, delt); + } + + /********************* + * k v redis start + ********************************/ + + public T get(String key, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.get(node, key, clazz); + } + + public void set(String key, T value) { + set(key, value, null); + } + + public void set(String key, T value, Integer time) { + String jsonValue = Jsons.toJson(value); + set(key, jsonValue, time); + } + + /** + * @param key + * @param value + * @param time seconds + */ + public void set(String key, String value, Integer time) { + List nodeList = clusterManager.hashSet(key); + RedisClient.set(nodeList, key, value, time); + } + + public void del(String key) { + + List nodeList = clusterManager.hashSet(key); + RedisClient.del(nodeList, key); + + } + + /*********************k v redis end********************************/ + + + /********************* + * hash redis start + ********************************/ + public void hset(String key, String field, String value) { + + List nodeList = clusterManager.hashSet(field); + RedisClient.hset(nodeList, key, field, value); + + } + + public void hset(String key, String field, T value) { + hset(key, field, Jsons.toJson(value)); + } + + public T hget(String key, String field, Class clazz) { + + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hget(node, key, field, clazz); + + } + + public void hdel(String key, String field) { + List nodeList = clusterManager.hashSet(key); + RedisClient.hdel(nodeList, key, field); + } + + public Map hgetAll(String key) { + + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hgetAll(node, key); + + } + + public Map hgetAll(String key, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hgetAll(node, key, clazz); + } + + /** + * 返回 key 指定的哈希集中所有字段的名字。 + * + * @return + */ + public Set hkeys(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hkeys(node, key); + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * + * @param fields + * @param clazz + * @return + */ + public List hmget(String key, Class clazz, String... fields) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hmget(node, key, clazz, fields); + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * + * @param hash + * @param time + */ + public void hmset(String key, Map hash, Integer time) { + List nodeList = clusterManager.hashSet(key); + RedisClient.hmset(nodeList, key, hash, time); + } + + public void hmset(String key, Map hash) { + hmset(key, hash, null); + } + + + /*********************hash redis end********************************/ + + + /*********************list redis start********************************/ + /** + * 从队列的左边入队 + */ + public void lpush(String key, String value) { + List nodeList = clusterManager.hashSet(key); + RedisClient.lpush(nodeList, key, value); + } + + public void lpush(String key, T value) { + lpush(key, Jsons.toJson(value)); + } + + /** + * 从队列的右边入队 + */ + public void rpush(String key, String value) { + List nodeList = clusterManager.hashSet(key); + RedisClient.rpush(nodeList, key, value); + } + + public void rpush(String key, T value) { + rpush(key, Jsons.toJson(value)); + } + + /** + * 移除并且返回 key 对应的 list 的第一个元素 + */ + public T lpop(String key, Class clazz) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.lpop(nodeList, key, clazz); + } + + /** + * 从队列的右边出队一个元素 + */ + public T rpop(String key, Class clazz) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.rpop(nodeList, key, clazz); + } + + + /** + * 从列表中获取指定返回的元素 + * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public List lrange(String key, int start, int end, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.lrange(node, key, start, end, clazz); + } + + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + */ + public long llen(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.llen(node, key); + } + + public void lrem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.lRem(nodeList, key, jsonValue); + } + + public void publish(String channel, T message) { + + RedisServer node = clusterManager.randomGetRedisNode(channel); + RedisClient.publish(node, channel, message); + + } + + public void subscribe(JedisPubSub pubsub, String... channels) { + + Set set = Sets.newHashSet(); + for (String channel : channels) { + List nodeList = clusterManager.hashSet(channel); + set.addAll(nodeList); + } + + RedisClient.subscribe(set, pubsub, channels); + } + + public void sAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.sAdd(nodeList, key, jsonValue); + } + + public Long sCard(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.sCard(node, key); + } + + public void sRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.sRem(nodeList, key, jsonValue); + } + + public List sScan(String key, int start, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.sScan(node, key, clazz, start); + } + + public void zAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.zAdd(nodeList, key, jsonValue); + } + + public Long zCard(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.zCard(node, key); + } + + public void zRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.zRem(nodeList, key, jsonValue); + } + + public List zrange(String key, int start, int end, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.zrange(node, key, start, end, clazz); + } + + public void test(List groupList) { + if (groupList == null || groupList.isEmpty()) { + throw new RuntimeException("init redis sever error."); + } + for (RedisGroup group : groupList) { + List list = group.getRedisServerList(); + if (list == null || list.isEmpty()) { + throw new RuntimeException("init redis sever error."); + } + for (RedisServer node : list) { + Jedis jedis = RedisClient.getClient(node); + if (jedis == null) throw new RuntimeException("init redis sever error."); + jedis.close(); + } + } + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java new file mode 100644 index 00000000..54abe851 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -0,0 +1,130 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.manager; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisException; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKRedisNodeWatcher; +import com.mpush.zk.node.ZKRedisNode; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static com.mpush.zk.ZKPath.REDIS_SERVER; + +public class ZKRedisClusterManager implements RedisClusterManager { + public static final ZKRedisClusterManager I = new ZKRedisClusterManager(); + + private ZKRedisClusterManager() { + } + + private final List groups = new ArrayList<>(); + + /** + * zk 启动的时候需要调用这个 + */ + @Override + public void init() { + Logs.Console.error("begin init redis cluster"); + if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); + List groupList = CC.mp.redis.cluster_group; + if (groupList.size() > 0) { + if (CC.mp.redis.write_to_zk) { + register(groupList); + } else if (!ZKClient.I.isExisted(REDIS_SERVER.getRootPath())) { + register(groupList); + } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getRootPath()))) { + register(groupList); + } + } + + ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); + watcher.beginWatch(); + Collection nodes = watcher.getCache().values(); + if (nodes == null || nodes.isEmpty()) { + Logs.REDIS.info("init redis client error, redis server is none."); + throw new RedisException("init redis client error, redis server is none."); + } + for (ZKRedisNode node : nodes) { + groups.add(RedisGroup.from(node)); + } + if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); + Logs.Console.error("init redis cluster success..."); + } + + @Override + public List getGroupList() { + return Collections.unmodifiableList(groups); + } + + public int groupSize() { + return groups.size(); + } + + /** + * 随机获取一个redis 实例 + * + * @param key + * @return + */ + @Override + public RedisServer randomGetRedisNode(String key) { + int size = groupSize(); + if (size == 1) return groups.get(0).get(key); + int i = (int) ((Math.random() % size) * size); + RedisGroup group = groups.get(i); + return group.get(key); + } + + /** + * 写操作的时候,获取所有redis 实例 + * + * @param key + * @return + */ + @Override + public List hashSet(String key) { + List nodeList = Lists.newArrayList(); + for (RedisGroup group : groups) { + RedisServer node = group.get(key); + nodeList.add(node); + } + return nodeList; + } + + private void register(List groupList) { + String data = Jsons.toJson(groupList); + ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data); + Logs.Console.error("register redis server group success, group=" + data); + } + + public void addGroup(RedisGroup group) { + groups.add(group); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java new file mode 100644 index 00000000..95afbcf6 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java @@ -0,0 +1,86 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.mq; + +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import redis.clients.jedis.JedisPubSub; + +public class Subscriber extends JedisPubSub { + + private static ListenerDispatcher dispatcher = ListenerDispatcher.I; + + public static Subscriber holder = new Subscriber(); + + private Subscriber() { + } + + @Override + public void onMessage(String channel, String message) { + Logs.REDIS.info("onMessage:{},{}", channel, message); + dispatcher.onMessage(channel, message); + super.onMessage(channel, message); + } + + @Override + public void onPMessage(String pattern, String channel, String message) { + Logs.REDIS.info("onPMessage:{},{},{}", pattern, channel, message); + super.onPMessage(pattern, channel, message); + } + + @Override + public void onPSubscribe(String pattern, int subscribedChannels) { + Logs.REDIS.info("onPSubscribe:{},{}", pattern, subscribedChannels); + super.onPSubscribe(pattern, subscribedChannels); + } + + @Override + public void onPUnsubscribe(String pattern, int subscribedChannels) { + Logs.REDIS.info("onPUnsubscribe:{},{}", pattern, subscribedChannels); + super.onPUnsubscribe(pattern, subscribedChannels); + } + + @Override + public void onSubscribe(String channel, int subscribedChannels) { + Logs.REDIS.info("onSubscribe:{},{}", channel, subscribedChannels); + super.onSubscribe(channel, subscribedChannels); + } + + @Override + public void onUnsubscribe(String channel, int subscribedChannels) { + Logs.REDIS.info("onUnsubscribe:{},{}", channel, subscribedChannels); + super.onUnsubscribe(channel, subscribedChannels); + } + + + @Override + public void unsubscribe() { + Logs.REDIS.info("unsubscribe"); + super.unsubscribe(); + } + + @Override + public void unsubscribe(String... channels) { + Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); + super.unsubscribe(channels); + } + +} diff --git a/mpush-client/.gitignore b/mpush-client/.gitignore index 09e3bc9b..ae3c1726 100644 --- a/mpush-client/.gitignore +++ b/mpush-client/.gitignore @@ -1,2 +1 @@ /bin/ -/target/ diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 54440364..c62a39b4 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -11,7 +11,7 @@ 4.0.0 ${mpush.groupId} mpush-client - ${mpush-client-version} + ${mpush.version} mpush-client jar @@ -25,39 +25,11 @@ ${mpush.groupId} - mpush-zk + mpush-common ${mpush.groupId} - mpush-common + mpush-cache - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-client - - - - - - - - diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java new file mode 100644 index 00000000..ed5e5131 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java @@ -0,0 +1,98 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + + +public class ClientConfig { + private byte[] clientKey; + private byte[] iv; + private String clientVersion; + private String deviceId; + private String osName; + private String osVersion; + private String userId; + private String cipher; //快速重连的时候使用 + + + public byte[] getClientKey() { + return clientKey; + } + + public void setClientKey(byte[] clientKey) { + this.clientKey = clientKey; + } + + public byte[] getIv() { + return iv; + } + + public void setIv(byte[] iv) { + this.iv = iv; + } + + public String getClientVersion() { + return clientVersion; + } + + public void setClientVersion(String clientVersion) { + this.clientVersion = clientVersion; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getOsName() { + return osName; + } + + public void setOsName(String osName) { + this.osName = osName; + } + + public String getOsVersion() { + return osVersion; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public String getCipher() { + return cipher; + } + + public void setCipher(String cipher) { + this.cipher = cipher; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + +} diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java new file mode 100644 index 00000000..7daaf8ba --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -0,0 +1,237 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + + +import com.google.common.collect.Maps; +import com.mpush.api.connection.Connection; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.message.*; +import com.mpush.common.security.AesCipher; +import com.mpush.common.security.CipherBox; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.ThreadNames; +import io.netty.channel.*; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timeout; +import io.netty.util.Timer; +import io.netty.util.TimerTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn + */ +@ChannelHandler.Sharable +public final class ConnClientChannelHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); + private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.T_NETTY_TIMER)); + + private final Connection connection = new NettyConnection(); + private final ClientConfig clientConfig; + + public ConnClientChannelHandler(ClientConfig clientConfig) { + this.clientConfig = clientConfig; + } + + public Connection getConnection() { + return connection; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + connection.updateLastReadTime(); + //加密 + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + Command command = Command.toCMD(packet.cmd); + if (command == Command.HANDSHAKE) { + connection.getSessionContext().changeCipher(new AesCipher(clientConfig.getClientKey(), clientConfig.getIv())); + HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); + byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); + connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); + startHeartBeat(message.heartbeat); + LOGGER.warn("会话密钥:{},message={}", sessionKey, message); + bindUser(clientConfig); + saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); + } else if (command == Command.FAST_CONNECT) { + String cipherStr = clientConfig.getCipher(); + String[] cs = cipherStr.split(","); + byte[] key = AesCipher.toArray(cs[0]); + byte[] iv = AesCipher.toArray(cs[1]); + connection.getSessionContext().changeCipher(new AesCipher(key, iv)); + + FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); + startHeartBeat(message.heartbeat); + bindUser(clientConfig); + LOGGER.warn("fast connect success, message=" + message); + } else if (command == Command.KICK) { + KickUserMessage message = new KickUserMessage(packet, connection); + LOGGER.error("receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); + ctx.close(); + } else if (command == Command.ERROR) { + ErrorMessage errorMessage = new ErrorMessage(packet, connection); + LOGGER.error("receive an error packet=" + errorMessage); + } else if (command == Command.BIND) { + OkMessage okMessage = new OkMessage(packet, connection); + LOGGER.warn("receive an success packet=" + okMessage); + HttpRequestMessage message = new HttpRequestMessage(connection); + message.uri = "http://baidu.com"; + message.send(); + } else if (command == Command.PUSH) { + PushMessage message = new PushMessage(packet, connection); + LOGGER.warn("receive an push message, content=" + message.content); + } else if (command == Command.HEARTBEAT) { + LOGGER.warn("receive a heartbeat pong..."); + } else { + LOGGER.warn("receive a message, type=" + command + "," + packet); + } + } + + + LOGGER.warn("update currentTime:" + ctx.channel() + "," + msg); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + connection.init(ctx.channel(), true); + handshake(clientConfig); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + EventBus.I.post(new ConnectionCloseEvent(connection)); + LOGGER.info("client disconnect connection={}", connection); + } + + private void tryFastConnect() { + + Map sessionTickets = getFastConnectionInfo(clientConfig.getDeviceId()); + + if (sessionTickets == null) { + handshake(clientConfig); + return; + } + String sessionId = sessionTickets.get("sessionId"); + if (sessionId == null) { + handshake(clientConfig); + return; + } + String expireTime = sessionTickets.get("expireTime"); + if (expireTime != null) { + long exp = Long.parseLong(expireTime); + if (exp < System.currentTimeMillis()) { + handshake(clientConfig); + return; + } + } + + final String cipher = sessionTickets.get("cipherStr"); + + FastConnectMessage message = new FastConnectMessage(connection); + message.deviceId = clientConfig.getDeviceId(); + message.sessionId = sessionId; + + message.sendRaw(channelFuture -> { + if (channelFuture.isSuccess()) { + clientConfig.setCipher(cipher); + } else { + handshake(clientConfig); + } + }); + } + + private void bindUser(ClientConfig client) { + BindUserMessage message = new BindUserMessage(connection); + message.userId = client.getUserId(); + message.send(); + } + + private void saveToRedisForFastConnection(ClientConfig client, String sessionId, Long expireTime, byte[] sessionKey) { + Map map = Maps.newHashMap(); + map.put("sessionId", sessionId); + map.put("expireTime", expireTime + ""); + map.put("cipherStr", connection.getSessionContext().cipher.toString()); + String key = RedisKey.getDeviceIdKey(client.getDeviceId()); + RedisManager.I.set(key, map, 60 * 5); //5分钟 + } + + private Map getFastConnectionInfo(String deviceId) { + String key = RedisKey.getDeviceIdKey(deviceId); + return RedisManager.I.get(key, Map.class); + } + + private void handshake(ClientConfig client) { + HandshakeMessage message = new HandshakeMessage(connection); + message.clientKey = client.getClientKey(); + message.iv = client.getIv(); + message.clientVersion = client.getClientVersion(); + message.deviceId = client.getDeviceId(); + message.osName = client.getOsName(); + message.osVersion = client.getOsVersion(); + message.timestamp = System.currentTimeMillis(); + message.send(); + } + + public void startHeartBeat(final int heartbeat) throws Exception { + HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + final TimerTask self = this; + final Channel channel = connection.getChannel(); + if (channel.isActive()) { + ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); + channelFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + } else { + LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); + } + HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); + } + }); + } else { + LOGGER.error("connection was closed, connection={}", connection); + } + } + }, heartbeat, TimeUnit.MILLISECONDS); + } +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java new file mode 100644 index 00000000..0a69d57d --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + +import com.google.common.eventbus.Subscribe; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.netty.client.NettyClient; +import com.mpush.tools.event.EventBus; +import io.netty.channel.ChannelHandler; + +public class ConnectClient extends NettyClient { + private final ConnClientChannelHandler handler; + + public ConnectClient(String host, int port, ClientConfig config) { + super(host, port); + handler = new ConnClientChannelHandler(config); + EventBus.I.register(this); + } + + @Override + public ChannelHandler getChannelHandler() { + return handler; + } + + @Subscribe + void on(ConnectionCloseEvent event) { + this.stop(); + } + +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java new file mode 100644 index 00000000..4b423d04 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -0,0 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.api.service.Listener; +import com.mpush.netty.client.NettyClient; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_client.*; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClient extends NettyClient { + private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); + private GlobalChannelTrafficShapingHandler trafficShapingHandler; + + public GatewayClient(String host, int port) { + super(host, port); + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } + } + + @Override + public ChannelHandler getChannelHandler() { + return handler; + } + + public Connection getConnection() { + return handler.getConnection(); + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } + super.doStop(listener); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java new file mode 100644 index 00000000..cfb9bcd9 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -0,0 +1,120 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.client.push.PushRequest; +import com.mpush.client.push.PushRequestBus; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.netty.connection.NettyConnection; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.mpush.common.ErrorCode.*; + +/** + * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn + */ +@ChannelHandler.Sharable +public final class GatewayClientChannelHandler extends ChannelHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); + + private final Connection connection = new NettyConnection(); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + LOGGER.info("receive gateway packet={}, channel={}", msg, ctx.channel()); + connection.updateLastReadTime(); + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + if (packet.cmd == Command.OK.cmd) { + handleOK(new OkMessage(packet, connection)); + } else if (packet.cmd == Command.ERROR.cmd) { + handleError(new ErrorMessage(packet, connection)); + } + } + } + + private void handleOK(OkMessage message) { + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + handPush(message, null, message.getPacket()); + } + } + + private void handleError(ErrorMessage message) { + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + handPush(null, message, message.getPacket()); + } + } + + private void handPush(OkMessage ok, ErrorMessage error, Packet packet) { + PushRequest request = PushRequestBus.I.getAndRemove(packet.sessionId); + if (request == null) { + LOGGER.warn("receive a gateway response, but request has timeout. ok={}, error={}", ok, error); + return; + } + + if (ok != null) {//推送成功 + request.success(); + } else if (error != null) {//推送失败 + LOGGER.warn("receive an error gateway response, message={}", error); + if (error.code == OFFLINE.errorCode) {//用户离线 + request.offline(); + } else if (error.code == PUSH_CLIENT_FAILURE.errorCode) {//下发到客户端失败 + request.failure(); + } else if (error.code == ROUTER_CHANGE.errorCode) {//用户路由信息更改 + request.redirect(); + } + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + connection.init(ctx.channel(), false); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + LOGGER.info("client disconnect channel={}", ctx.channel()); + //TODO notify gateway-client-factory to removeAndClose this gateway-client + } + + public Connection getConnection() { + return connection; + } +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java new file mode 100644 index 00000000..6714c4d9 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java @@ -0,0 +1,126 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.google.common.collect.Maps; +import com.mpush.api.connection.Connection; +import com.mpush.api.service.Client; +import com.mpush.api.service.Listener; +import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.zk.node.ZKServerNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClientFactory extends ZKServerNodeCache { + public static final GatewayClientFactory I = new GatewayClientFactory(); + + private final Logger logger = LoggerFactory.getLogger(GatewayClientFactory.class); + + private final Map ip_client = Maps.newConcurrentMap(); + + @Override + public void put(String fullPath, ZKServerNode node) { + super.put(fullPath, node); + addClient(node.getIp(), node.getPort()); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = super.remove(fullPath); + removeClient(node); + logger.warn("Gateway Server zkNode={} was removed.", node); + return node; + } + + @Override + public void clear() { + super.clear(); + for (GatewayClient client : ip_client.values()) { + client.stop(null); + } + } + + public GatewayClient getClient(String ip) { + GatewayClient client = ip_client.get(ip); + if (client == null) { + return null;//TODO create client + } + return client; + } + + public Connection getConnection(String ip) { + GatewayClient client = ip_client.get(ip); + if (client == null) { + return null;//TODO create client + } + Connection connection = client.getConnection(); + if (connection.isConnected()) { + return connection; + } + restartClient(client); + return null; + } + + private void restartClient(final GatewayClient client) { + ip_client.remove(client.getHost()); + client.stop(new Listener() { + @Override + public void onSuccess(Object... args) { + addClient(client.getHost(), client.getPort()); + } + + @Override + public void onFailure(Throwable cause) { + addClient(client.getHost(), client.getPort()); + } + }); + } + + private void removeClient(ZKServerNode node) { + if (node != null) { + Client client = ip_client.remove(node.getIp()); + if (client != null) { + client.stop(null); + } + } + } + + private void addClient(final String host, final int port) { + final GatewayClient client = new GatewayClient(host, port); + client.start(new Listener() { + @Override + public void onSuccess(Object... args) { + ip_client.put(host, client); + } + + @Override + public void onFailure(Throwable cause) { + logger.error("create gateway client ex, client={}", client, cause); + } + }); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java new file mode 100644 index 00000000..9b69df55 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -0,0 +1,109 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.Constants; +import com.mpush.api.connection.Connection; +import com.mpush.api.push.PushSender; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.client.gateway.GatewayClientFactory; +import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.RemoteRouter; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKServerNodeWatcher; + +import java.util.Collection; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.FutureTask; + +import static com.mpush.zk.ZKPath.GATEWAY_SERVER; + +/*package*/ class PushClient extends BaseService implements PushSender { + private static final int DEFAULT_TIMEOUT = 3000; + private final GatewayClientFactory factory = GatewayClientFactory.I; + private final ConnectionRouterManager routerManager = ConnectionRouterManager.I; + + public void send(String content, Collection userIds, Callback callback) { + send(content.getBytes(Constants.UTF_8), userIds, callback); + } + + @Override + public FutureTask send(String content, String userId, Callback callback) { + return send(content.getBytes(Constants.UTF_8), userId, callback); + } + + @Override + public void send(byte[] content, Collection userIds, Callback callback) { + for (String userId : userIds) { + send(content, userId, callback); + } + } + + @Override + public FutureTask send(byte[] content, String userId, Callback callback) { + Set routers = routerManager.lookupAll(userId); + if (routers == null || routers.isEmpty()) { + return PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .offline(); + + } + FutureTask task = null; + for (RemoteRouter router : routers) { + task = PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(router); + } + return task; + } + + public Connection getGatewayConnection(String host) { + return factory.getConnection(host); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + ZKClient.I.start(listener); + RedisManager.I.init(); + ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).beginWatch(); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + factory.clear(); + ZKClient.I.stop(listener); + } + + @Override + public boolean isRunning() { + return started.get(); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java new file mode 100644 index 00000000..5e91639d --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.push.PushSender; +import com.mpush.api.spi.client.PusherFactory; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class PushClientFactory implements PusherFactory { + private static PushClient CLIENT; + + @Override + public PushSender get() { + if (CLIENT == null) { + synchronized (PushClientFactory.class) { + CLIENT = new PushClient(); + } + } + return CLIENT; + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java new file mode 100644 index 00000000..3df50970 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -0,0 +1,215 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.connection.Connection; +import com.mpush.api.push.PushSender; +import com.mpush.api.router.ClientLocation; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.RemoteRouter; +import com.mpush.tools.common.TimeLine; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public class PushRequest extends FutureTask { + private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); + + private static final Callable NONE = () -> Boolean.FALSE; + + private enum Status {init, success, failure, offline, timeout} + + private final AtomicReference status = new AtomicReference<>(Status.init); + private final TimeLine timeLine = new TimeLine("Push-Time-Line"); + + private final PushClient client; + + private PushSender.Callback callback; + private String userId; + private byte[] content; + private long timeout; + private ClientLocation location; + private Future future; + + private void sendToConnServer(RemoteRouter router) { + timeLine.addTimePoint("lookup-remote"); + + if (router == null) { + //1.1没有查到说明用户已经下线 + offline(); + return; + } + + timeLine.addTimePoint("get-gateway-conn"); + + + //2.通过网关连接,把消息发送到所在机器 + location = router.getRouteValue(); + Connection gatewayConn = client.getGatewayConnection(location.getHost()); + if (gatewayConn == null) { + LOGGER.error("get gateway connection failure, location={}", location); + failure(); + return; + } + + timeLine.addTimePoint("send-to-gateway-begin"); + + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, location.getClientType(), content, gatewayConn); + timeLine.addTimePoint("put-request-bus"); + future = PushRequestBus.I.put(pushMessage.getSessionId(), this); + + pushMessage.sendRaw(f -> { + timeLine.addTimePoint("send-to-gateway-end"); + if (f.isSuccess()) { + } else { + failure(); + } + }); + } + + private void submit(Status status) { + if (this.status.compareAndSet(Status.init, status)) {//防止重复调用 + if (future != null) future.cancel(true); + if (callback != null) { + PushRequestBus.I.asyncCall(this); + } else { + LOGGER.warn("callback is null"); + } + super.set(this.status.get() == Status.success); + } + timeLine.end(); + LOGGER.info("push request {} end, userId={}, content={}, location={}, timeLine={}" + , status, userId, content, location, timeLine); + } + + @Override + public void run() { + switch (status.get()) { + case success: + callback.onSuccess(userId, location); + break; + case failure: + callback.onFailure(userId, location); + break; + case offline: + callback.onOffline(userId, location); + break; + case timeout: + callback.onTimeout(userId, location); + break; + case init://从定时任务过来的,超时时间到了 + submit(Status.timeout); + break; + } + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + throw new UnsupportedOperationException(); + } + + public FutureTask send(RemoteRouter router) { + timeLine.begin(); + sendToConnServer(router); + return this; + } + + public void redirect() { + timeLine.addTimePoint("redirect"); + LOGGER.warn("user route has changed, userId={}, location={}", userId, location); + ConnectionRouterManager.I.invalidateLocalCache(userId); + if (status.get() == Status.init) {//表示任务还没有完成,还可以重新发送 + RemoteRouter route = ConnectionRouterManager.I.lookup(userId, location.getClientType()); + send(route); + } + } + + public FutureTask offline() { + ConnectionRouterManager.I.invalidateLocalCache(userId); + submit(Status.offline); + return this; + } + + public void timeout() { + submit(Status.timeout); + } + + public void success() { + submit(Status.success); + } + + public void failure() { + submit(Status.failure); + } + + public long getTimeout() { + return timeout; + } + + public PushRequest(PushClient client) { + super(NONE); + this.client = client; + } + + public static PushRequest build(PushClient client) { + return new PushRequest(client); + } + + public PushRequest setCallback(PushSender.Callback callback) { + this.callback = callback; + return this; + } + + public PushRequest setUserId(String userId) { + this.userId = userId; + return this; + } + + public PushRequest setContent(byte[] content) { + this.content = content; + return this; + } + + public PushRequest setTimeout(long timeout) { + this.timeout = timeout; + return this; + } + + @Override + public String toString() { + return "PushRequest{" + + "content='" + content + '\'' + + ", userId='" + userId + '\'' + + ", timeout=" + timeout + + ", location=" + location + + '}'; + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java new file mode 100644 index 00000000..fbb846c0 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.push.PushException; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.util.internal.chmv8.ConcurrentHashMapV8; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.concurrent.*; + +import static com.mpush.tools.thread.ThreadNames.T_PUSH_REQ_TIMER; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public class PushRequestBus { + public static final PushRequestBus I = new PushRequestBus(); + private final Logger logger = LoggerFactory.getLogger(PushRequestBus.class); + private final Map reqQueue = new ConcurrentHashMapV8<>(1024); + private final Executor executor = ThreadPoolManager.I.getPushCallbackExecutor(); + private final ScheduledExecutorService scheduledExecutor; + + private PushRequestBus() { + scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { + logger.error("one push request was rejected, request=" + r); + throw new PushException("one push request was rejected. request=" + r); + }); + } + + public Future put(int sessionId, PushRequest request) { + reqQueue.put(sessionId, request); + return scheduledExecutor.schedule(request, request.getTimeout(), TimeUnit.MILLISECONDS); + } + + public PushRequest getAndRemove(int sessionId) { + return reqQueue.remove(sessionId); + } + + public void asyncCall(Runnable runnable) { + executor.execute(runnable); + } +} diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java deleted file mode 100644 index 4200b5ad..00000000 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ /dev/null @@ -1,223 +0,0 @@ -package com.mpush.conn.client; - - -import java.util.Map; - -import com.google.common.collect.Maps; -import com.mpush.api.protocol.Command; -import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.Client; -import com.mpush.api.RedisKey; -import com.mpush.common.message.*; -import com.mpush.common.security.AesCipher; -import com.mpush.common.security.CipherBox; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.netty.client.ChannelClientHandler; -import com.mpush.netty.client.NettyClient; -import com.mpush.netty.client.SecurityNettyClient; -import com.mpush.netty.connection.NettyConnection; -import com.mpush.tools.redis.manage.RedisManage; - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/19. - * - * @author ohun@live.cn - */ -@ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); - if (client instanceof SecurityNettyClient) { - SecurityNettyClient securityNettyClient = (SecurityNettyClient) client; - Connection connection = client.getConnection(); - //加密 - if (msg instanceof Packet) { - Packet packet = (Packet) msg; - Command command = Command.toCMD(packet.cmd); - if (command == Command.HANDSHAKE) { - connection.getSessionContext().changeCipher(new AesCipher(securityNettyClient.getClientKey(), securityNettyClient.getIv())); - HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); - connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); - client.startHeartBeat(message.heartbeat); - LOGGER.debug("会话密钥:{},message={}", sessionKey, message); - bindUser(securityNettyClient); - saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); - } else if (command == Command.FAST_CONNECT) { - String cipherStr = securityNettyClient.getCipher(); - String[] cs = cipherStr.split(","); - byte[] key = AesCipher.toArray(cs[0]); - byte[] iv = AesCipher.toArray(cs[1]); - connection.getSessionContext().changeCipher(new AesCipher(key, iv)); - - FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); - client.startHeartBeat(message.heartbeat); - bindUser(securityNettyClient); - LOGGER.debug("fast connect success, message=" + message); - } else if (command == Command.KICK) { - KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); - ctx.close(); - } else if (command == Command.ERROR) { - ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error packet=" + errorMessage); - } else if (command == Command.BIND) { - OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.debug("receive an success packet=" + okMessage); - HttpRequestMessage message = new HttpRequestMessage(connection); - message.uri = "http://baidu.com"; - message.send(); - } else if (command == Command.PUSH) { - PushMessage message = new PushMessage(packet, connection); - LOGGER.warn("receive an push message, content=" + message.content); - } else if (command == Command.HEARTBEAT) { - LOGGER.debug("receive a heartbeat pong..."); - } else { - LOGGER.warn("receive a message, type=" + command + "," + packet); - } - } - - } else if (client instanceof NettyClient) {//不加密 - - } - LOGGER.debug("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.remove(ctx.channel()); - } else { - client.close("exception"); - } - - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); - - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.put(ctx.channel(), client); - connection.init(ctx.channel(), true); - client.initConnection(connection); - client.init(ctx.channel()); - tryFastConnect((SecurityNettyClient) client); - } else { - LOGGER.error("connection is not support appear hear:" + client); - connection.init(ctx.channel(), false); - client.initConnection(connection); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.remove(ctx.channel()); - } else { - client.close("inactive"); - } - LOGGER.info("client disconnect channel={}", ctx.channel()); - } - - private void tryFastConnect(final SecurityNettyClient securityNettyClient) { - - Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); - - if (sessionTickets == null) { - handshake(securityNettyClient); - return; - } - String sessionId = (String) sessionTickets.get("sessionId"); - if (sessionId == null) { - handshake(securityNettyClient); - return; - } - String expireTime = (String) sessionTickets.get("expireTime"); - if (expireTime != null) { - long exp = Long.parseLong(expireTime); - if (exp < System.currentTimeMillis()) { - handshake(securityNettyClient); - return; - } - } - - final String cipher = sessionTickets.get("cipherStr"); - - FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); - message.deviceId = securityNettyClient.getDeviceId(); - message.sessionId = sessionId; - - message.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) throws Exception { - if (channelFuture.isSuccess()) { - securityNettyClient.setCipher(cipher); - } else { - handshake(securityNettyClient); - } - } - }); - } - - private void bindUser(SecurityNettyClient client) { - BindUserMessage message = new BindUserMessage(client.getConnection()); - message.userId = client.getUserId(); - message.send(); - } - - private void saveToRedisForFastConnection(SecurityNettyClient client, String sessionId, Long expireTime, byte[] sessionKey) { - Map map = Maps.newHashMap(); - map.put("sessionId", sessionId); - map.put("expireTime", expireTime + ""); - map.put("cipherStr", client.getConnection().getSessionContext().cipher.toString()); - String key = RedisKey.getDeviceIdKey(client.getDeviceId()); - RedisManage.set(key, map, 60 * 5); //5分钟 - } - - private Map getFastConnectionInfo(String deviceId) { - String key = RedisKey.getDeviceIdKey(deviceId); - return RedisManage.get(key, Map.class); - } - - private void handshake(SecurityNettyClient client) { - HandshakeMessage message = new HandshakeMessage(client.getConnection()); - message.clientKey = client.getClientKey(); - message.iv = client.getIv(); - message.clientVersion = client.getClientVersion(); - message.deviceId = client.getDeviceId(); - message.osName = client.getOsName(); - message.osVersion = client.getOsVersion(); - message.timestamp = System.currentTimeMillis(); - message.send(); - } - -} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java b/mpush-client/src/main/java/com/mpush/push/ConnectClient.java deleted file mode 100644 index 65ef68b9..00000000 --- a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.push; - -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; - -public class ConnectClient extends AbstractClient{ - - public ConnectClient() { - registerListener(new ConnectZKListener()); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushClient.java b/mpush-client/src/main/java/com/mpush/push/PushClient.java deleted file mode 100644 index 953b81c5..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushClient.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.mpush.push; - -import com.google.common.base.Strings; -import com.mpush.api.PushSender; -import com.mpush.api.connection.Connection; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.GatewayZKListener; - -import java.util.Collection; - -public class PushClient extends AbstractClient implements PushSender { - private static final int DEFAULT_TIMEOUT = 3000; - private final GatewayZKListener listener = new GatewayZKListener(); - - public PushClient() { - registerListener(listener); - } - - public void send(String content, Collection userIds, Callback callback) { - if (Strings.isNullOrEmpty(content)) return; - for (String userId : userIds) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); - } - } - - @Override - public void send(String content, String userId, Callback callback) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); - } - - public Connection getGatewayConnection(String ip) { - return listener.getManager().getConnection(ip); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/push/PushRequest.java deleted file mode 100644 index cda87e8b..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushRequest.java +++ /dev/null @@ -1,206 +0,0 @@ -package com.mpush.push; - -import com.google.common.collect.Maps; -import com.mpush.api.PushSender; -import com.mpush.api.connection.Connection; -import com.mpush.api.router.ClientLocation; -import com.mpush.common.message.gateway.GatewayPushMessage; -import com.mpush.common.router.ConnectionRouterManager; -import com.mpush.common.router.RemoteRouter; -import com.mpush.tools.Jsons; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class PushRequest implements PushSender.Callback, Runnable { - private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - - private final PushClient client; - - private PushSender.Callback callback; - private String userId; - private String content; - private long timeout; - private long timeout_; - private int sessionId; - private long sendTime; - private AtomicInteger status = new AtomicInteger(0); - private Map times = Maps.newHashMap(); - - public PushRequest(PushClient client) { - this.client = client; - } - - public static PushRequest build(PushClient client) { - return new PushRequest(client); - } - - public PushRequest setCallback(PushSender.Callback callback) { - this.callback = callback; - return this; - } - - public PushRequest setUserId(String userId) { - this.userId = userId; - return this; - } - - public PushRequest setContent(String content) { - this.content = content; - return this; - } - - public PushRequest setTimeout(long timeout) { - this.timeout = timeout; - return this; - } - - @Override - public void onSuccess(String userId) { - putTime("success"); - LOGGER.info("success,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(1); - } - - @Override - public void onFailure(String userId) { - putTime("failure"); - LOGGER.info("failure,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(2); - } - - @Override - public void onOffline(String userId) { - putTime("offline"); - LOGGER.info("offline,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(3); - } - - @Override - public void onTimeout(String userId) { - putTime("timeout"); - LOGGER.info("timeout,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(4); - } - - private void submit(int status) { - if (this.status.compareAndSet(0, status)) {//防止重复调用 - if (callback != null) { - PushRequestBus.INSTANCE.getExecutor().execute(this); - } else { - LOGGER.warn("callback is null"); - } - } - } - - @Override - public void run() { - switch (status.get()) { - case 1: - callback.onSuccess(userId); - break; - case 2: - callback.onFailure(userId); - break; - case 3: - callback.onOffline(userId); - break; - case 4: - callback.onTimeout(userId); - break; - } - } - - public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; - } - - public void timeout() { - onTimeout(userId); - } - - public void success() { - onSuccess(userId); - } - - public void failure() { - onFailure(userId); - } - - public void offline() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - onOffline(userId); - } - - public void send() { - this.timeout_ = timeout + System.currentTimeMillis(); - putTime("startsend"); - sendToConnServer(); - } - - public void redirect() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - LOGGER.warn("user route has changed, userId={}, content={}", userId, content); - if (status.get() == 0) { - send(); - } - } - - private void sendToConnServer() { - //1.查询用户长连接所在的机器 - RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); - if (router == null) { - //1.1没有查到说明用户已经下线 - this.onOffline(userId); - return; - } - - //2.通过网关连接,把消息发送到所在机器 - ClientLocation location = router.getRouteValue(); - Connection gatewayConn = client.getGatewayConnection(location.getHost()); - if (gatewayConn == null || !gatewayConn.isConnected()) { - this.onFailure(userId); - return; - } - - putTime("sendtoconnserver"); - - final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); - pushMessage.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - putTime("sendsuccess"); - } else { - PushRequest.this.onFailure(userId); - } - } - }); - - sessionId = pushMessage.getSessionId(); - putTime("putrequestbus"); - PushRequestBus.INSTANCE.put(sessionId, this); - } - - public long getSendTime() { - return sendTime; - } - - public Map getTimes() { - return times; - } - - public void putTime(String key) { - this.times.put(key, System.currentTimeMillis()); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java deleted file mode 100644 index 3bfc3b8e..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.push; - -import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.*; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class PushRequestBus implements Runnable { - - public static final PushRequestBus INSTANCE = new PushRequestBus(); - private Map requests = new ConcurrentHashMapV8<>(1024); - private Executor executor = Executors.newFixedThreadPool(5);//test - private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test - - private PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); - } - - public void put(int sessionId, PushRequest request) { - requests.put(sessionId, request); - } - - public PushRequest remove(int sessionId) { - return requests.remove(sessionId); - } - - public Executor getExecutor() { - return executor; - } - - @Override - public void run() { - if (requests.isEmpty()) return; - Iterator it = requests.values().iterator(); - while (it.hasNext()) { - PushRequest request = it.next(); - if (request.isTimeout()) { - it.remove();//清除超时的请求 - request.timeout(); - } - } - } -} diff --git a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java deleted file mode 100644 index c91d3486..00000000 --- a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.mpush.push.client; - - - -import com.mpush.api.protocol.Command; -import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.Client; -import com.mpush.common.message.ErrorMessage; -import com.mpush.netty.client.ChannelClientHandler; -import com.mpush.netty.connection.NettyConnection; -import com.mpush.push.PushRequest; -import com.mpush.push.PushRequestBus; - -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.mpush.common.ErrorCode.OFFLINE; -import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.mpush.common.ErrorCode.ROUTER_CHANGE; - -/** - * Created by ohun on 2015/12/19. - * - * @author ohun@live.cn - */ -@ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); - if (msg instanceof Packet) { - Packet packet = ((Packet) msg); - PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); - return; - } - - if (packet.cmd == Command.OK.cmd) { - request.success(); - } else { - ErrorMessage message = new ErrorMessage(packet, client.getConnection()); - if (message.code == OFFLINE.errorCode) { - request.offline(); - } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { - request.failure(); - } else if (message.code == ROUTER_CHANGE.errorCode) { - request.redirect(); - } - LOGGER.warn("receive an error gateway response, message={}", message); - } - } - LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - client.close("exception"); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); - connection.init(ctx.channel(), false); - client.initConnection(connection); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - client.close("inactive"); - LOGGER.info("client disconnect channel={}", ctx.channel()); - } - - -} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java deleted file mode 100644 index 98091e43..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.push.zk.listener; - - -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeListener; -import com.mpush.push.zk.manager.ConnectZKNodeManager; - -/** - * connection server 应用 监控 - */ -public class ConnectZKListener extends ZKServerNodeListener { - - private final ConnectZKNodeManager manager = new ConnectZKNodeManager(); - - @Override - public String listenerPath() { - return ZKPath.CONNECTION_SERVER.getWatchPath(); - } - - @Override - public String getRegisterPath() { - return ZKPath.CONNECTION_SERVER.getPath(); - } - - @Override - public ConnectZKNodeManager getManager() { - return manager; - } - - @Override - public String getFullPath(String raw) { - return ZKPath.CONNECTION_SERVER.getFullPath(raw); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java deleted file mode 100644 index 17f90570..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.push.zk.listener; - -import com.mpush.push.zk.manager.GatewayZKNodeManager; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeListener; - -/** - * gateway server 应用 监控 - */ -public class GatewayZKListener extends ZKServerNodeListener { - - private final GatewayZKNodeManager manager = new GatewayZKNodeManager(); - - @Override - public String listenerPath() { - return ZKPath.GATEWAY_SERVER.getWatchPath(); - } - - @Override - public GatewayZKNodeManager getManager() { - return manager; - } - - @Override - public String getRegisterPath() { - return ZKPath.GATEWAY_SERVER.getPath(); - } - - @Override - public String getFullPath(String raw) { - return ZKPath.GATEWAY_SERVER.getFullPath(raw); - } - - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java deleted file mode 100644 index df9c939a..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.mpush.push.zk.manager; - -import com.google.common.collect.Maps; -import com.mpush.zk.ZKServerNode; -import com.mpush.zk.ZKNodeManager; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -public class ConnectZKNodeManager implements ZKNodeManager { - - private final Logger log = LoggerFactory.getLogger(ConnectZKNodeManager.class); - - private final Map cache = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, ZKServerNode node) { - if (StringUtils.isNotBlank(fullPath) && node != null) { - cache.put(fullPath, node); - } else { - log.error("fullPath is null or application is null"); - } - printList(); - } - - @Override - public void remove(String fullPath) { - cache.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(cache.values()); - } - - private void printList() { - for (ZKServerNode app : cache.values()) { - log.warn(app.toString()); - } - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java deleted file mode 100644 index 66648c52..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.mpush.push.zk.manager; - -import com.google.common.collect.Maps; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; -import com.mpush.api.Client; -import com.mpush.api.connection.Connection; -import com.mpush.netty.client.NettyClient; -import com.mpush.push.client.ClientChannelHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -public class GatewayZKNodeManager implements ZKNodeManager { - - private final Logger log = LoggerFactory.getLogger(GatewayZKNodeManager.class); - - private static Map holder = Maps.newConcurrentMap(); - - private final Map application2Client = Maps.newConcurrentMap(); - - private final Map ip2Client = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, ZKServerNode application) { - holder.put(fullPath, application); - try { - Client client = new NettyClient(application.getIp(), application.getPort()); - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - application2Client.put(application, client); - ip2Client.put(application.getIp(), client); - } catch (Exception e) { - log.error("addOrUpdate:{},{}", fullPath, application, e); - } - printList(); - } - - @Override - public void remove(String fullPath) { - ZKServerNode application = get(fullPath); - if (application != null) { - Client client = application2Client.get(application); - if (client != null) { - client.stop(); - } - } - ip2Client.remove(application.getIp() + ":" + application.getPort()); - holder.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(holder.values()); - } - - private void printList() { - for (ZKServerNode app : holder.values()) { - log.warn(app.toString()); - } - } - - public ZKServerNode get(String fullpath) { - return holder.get(fullpath); - } - - public Client getClient(ZKServerNode application) { - return application2Client.get(application); - } - - public Connection getConnection(String ipAndPort) { - Client client = ip2Client.get(ipAndPort); - if (client == null) return null; - return client.getConnection(); - } - -} - diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory b/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory new file mode 100644 index 00000000..93607f48 --- /dev/null +++ b/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory @@ -0,0 +1 @@ +com.mpush.client.push.PushClientFactory diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager deleted file mode 100644 index d9e4979b..00000000 --- a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager +++ /dev/null @@ -1,2 +0,0 @@ -com.mpush.push.zk.manager.ConnectZKNodeManager -com.mpush.push.zk.manager.GatewayZKNodeManager diff --git a/mpush-client/src/test/java/com/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/mpush/client/PushClientTest.java deleted file mode 100644 index cd9c4979..00000000 --- a/mpush-client/src/test/java/com/mpush/client/PushClientTest.java +++ /dev/null @@ -1,50 +0,0 @@ -//package com.mpush.client; -// -//import PushSender; -//import org.junit.Test; -// -//import java.util.Arrays; -//import java.util.concurrent.locks.LockSupport; -// -//import static org.junit.Assert.*; -// -///** -// * Created by ohun on 2016/1/7. -// */ -//public class PushClientTest { -// -// @Test -// public void testSend() throws Exception { -// -// } -// -// public static void main(String[] args) throws Exception { -// PushClient client = new PushClient(); -// client.init(); -// Thread.sleep(1000); -// client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), -// new PushSender.Callback() { -// @Override -// public void onSuccess(String userId) { -// System.err.println("push onSuccess userId=" + userId); -// } -// -// @Override -// public void onFailure(String userId) { -// System.err.println("push onFailure userId=" + userId); -// } -// -// @Override -// public void onOffline(String userId) { -// System.err.println("push onOffline userId=" + userId); -// } -// -// @Override -// public void onTimeout(String userId) { -// System.err.println("push onTimeout userId=" + userId); -// } -// } -// ); -// LockSupport.park(); -// } -//} \ No newline at end of file diff --git a/mpush-client/src/test/resources/logback.xml b/mpush-client/src/test/resources/logback.xml deleted file mode 100644 index 20979b40..00000000 --- a/mpush-client/src/test/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-common/.gitignore b/mpush-common/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-common/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 867dbf8e..d97f639b 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -11,7 +11,7 @@ ${mpush.groupId} mpush-common - ${mpush-common-version} + ${mpush.version} mpush-common jar @@ -28,6 +28,10 @@ ${mpush.groupId} mpush-zk + + ${mpush.groupId} + mpush-cache + diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java deleted file mode 100644 index 1c5e2a5f..00000000 --- a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.mpush.common; - -import com.google.common.collect.Lists; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKDataChangeListener; -import com.mpush.zk.listener.ZKRedisNodeListener; - -import java.util.List; - -public abstract class AbstractClient { - - protected List dataChangeListeners = Lists.newArrayList(); - - protected ZKClient zkClient = ZKClient.I; - - public AbstractClient() { - registerListener(new ZKRedisNodeListener()); - } - - public void registerListener(ZKDataChangeListener listener) { - dataChangeListeners.add(listener); - } - - //step2 获取redis - private void initRedis() { - boolean exist = zkClient.isExisted(ZKPath.REDIS_SERVER.getPath()); - if (!exist) { - List groupList = ConfigCenter.I.redisGroups(); - zkClient.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - } - - //step3 注册listener - private void registerListeners() { - for (ZKDataChangeListener listener : dataChangeListeners) { - zkClient.registerListener(listener); - } - } - - //step4 初始化 listener data - private void initListenerData() { - for (ZKDataChangeListener listener : dataChangeListeners) { - listener.initData(); - } - } - - public void start() { - initRedis(); - registerListeners(); - initListenerData(); - } -} diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java b/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java deleted file mode 100644 index a9081df0..00000000 --- a/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.mpush.common; - -public abstract class AbstractEventContainer { - - public AbstractEventContainer() { - EventBus.INSTANCE.register(this); - } - -} diff --git a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java index cd881c08..6bcc470f 100644 --- a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common; /** @@ -10,6 +29,7 @@ public enum ErrorCode { PUSH_CLIENT_FAILURE(2, "push to client failure"), ROUTER_CHANGE(3, "router change"), DISPATCH_ERROR(100, "handle message error"), + UNSUPPORTED_CMD(101, "unsupported command"), UNKNOWN(-1, "unknown"); ErrorCode(int code, String errorMsg) { diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 14b0eaf3..4cfda9b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common; import com.mpush.api.MessageHandler; @@ -5,15 +24,18 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.Profiler; import com.mpush.common.message.ErrorMessage; - +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import static com.mpush.common.ErrorCode.DISPATCH_ERROR; +import static com.mpush.common.ErrorCode.UNSUPPORTED_CMD; + /** * Created by ohun on 2015/12/22. * @@ -27,25 +49,30 @@ public void register(Command command, MessageHandler handler) { handlers.put(command.cmd, handler); } - @Override public void onReceive(Packet packet, Connection connection) { - MessageHandler handler = handlers.get(packet.cmd); - try { - if (handler != null) { - Profiler.enter("start handle:"+handler.getClass().getSimpleName()); + MessageHandler handler = handlers.get(packet.cmd); + if (handler != null) { + try { + Profiler.enter("start handle:" + handler.getClass().getSimpleName()); handler.handle(packet, connection); + } catch (Throwable throwable) { + LOGGER.error("dispatch message ex, packet={}, connect={}, body={}" + , packet, connection, Arrays.toString(packet.body), throwable); + ErrorMessage + .from(packet, connection) + .setErrorCode(DISPATCH_ERROR) + .close(); + } finally { + Profiler.release(); } - } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={}, conn={}", packet, connection, throwable); + } else { + LOGGER.error("dispatch message failure unsupported cmd, packet={}, connect={}, body={}" + , packet, connection); ErrorMessage .from(packet, connection) - .setErrorCode(ErrorCode.DISPATCH_ERROR) + .setErrorCode(UNSUPPORTED_CMD) .close(); - }finally{ - if(handler!=null){ - Profiler.release(); - } } } } diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index bbd346a0..181e3d5c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; @@ -5,7 +24,7 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.tools.Profiler; +import com.mpush.tools.common.Profiler; /** * Created by ohun on 2015/12/22. diff --git a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java index b6c0c5e9..a451a1c0 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java index 6271a2cf..7d77b11c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; import com.mpush.api.connection.Connection; -import com.mpush.common.message.OkMessage; import com.mpush.api.protocol.Packet; +import com.mpush.common.message.OkMessage; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java deleted file mode 100644 index 1269273b..00000000 --- a/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.common.manage.user; - -import java.util.List; - -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.manage.RedisManage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.api.RedisKey; - -//查询使用 -public final class UserManager { - private static final Logger log = LoggerFactory.getLogger(UserManager.class); - public static final UserManager INSTANCE = new UserManager(); - - private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); - - public UserManager() { - clearUserOnlineData(); - } - - public void clearUserOnlineData() { - RedisManage.del(ONLINE_KEY); - } - - public void recordUserOnline(String userId) { - RedisManage.zAdd(ONLINE_KEY, userId); - log.info("user online {}", userId); - } - - public void recordUserOffline(String userId) { - RedisManage.zRem(ONLINE_KEY, userId); - log.info("user offline {}", userId); - } - - //在线用户 - public long getOnlineUserNum() { - return RedisManage.zCard(ONLINE_KEY); - } - - //在线用户列表 - public List getOnlineUserList(int start, int size) { - if (size < 10) { - size = 10; - } - return RedisManage.zrange(ONLINE_KEY, start, size - 1, String.class); - } -} diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 7c056e6e..8c3bd221 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -1,12 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.Message; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.tools.IOUtils; -import com.mpush.tools.Profiler; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.common.IOUtils; +import com.mpush.tools.config.CC; import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; @@ -24,12 +42,7 @@ public abstract class BaseMessage implements Message { public BaseMessage(Packet packet, Connection connection) { this.packet = packet; this.connection = connection; - Profiler.enter("start decode message"); - try { - decodeBody(); - } finally { - Profiler.release(); - } + decodeBody(); } protected void decodeBody() { @@ -43,7 +56,7 @@ protected void decodeBody() { } //2.解压 if (packet.hasFlag(Packet.FLAG_COMPRESS)) { - tmp = IOUtils.uncompress(tmp); + tmp = IOUtils.decompress(tmp); } if (tmp.length == 0) { @@ -59,11 +72,11 @@ protected void encodeBody() { byte[] tmp = encode(); if (tmp != null && tmp.length > 0) { //1.压缩 - if (tmp.length > ConfigCenter.I.compressLimit()) { + if (tmp.length > CC.mp.core.compress_threshold) { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Packet.FLAG_COMPRESS); + packet.addFlag(Packet.FLAG_COMPRESS); } } @@ -73,7 +86,7 @@ protected void encodeBody() { byte[] result = context.cipher.encrypt(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Packet.FLAG_CRYPTO); + packet.addFlag(Packet.FLAG_CRYPTO); } } packet.body = tmp; @@ -131,10 +144,5 @@ public int getSessionId() { } @Override - public String toString() { - return "BaseMessage{" + - "packet=" + packet + - ", connection=" + connection + - '}'; - } + public abstract String toString(); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index 4638e36a..18a10f6a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -36,4 +55,14 @@ public void encode(ByteBuf body) { encodeString(body, alias); encodeString(body, tags); } + + @Override + public String toString() { + return "BindUserMessage{" + + "alias='" + alias + '\'' + + ", userId='" + userId + '\'' + + ", tags='" + tags + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index 81e7674a..bb7ecb83 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; -import com.mpush.api.connection.Connection; import com.mpush.api.Constants; +import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index 435bb0ad..9126e3aa 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -17,6 +36,7 @@ public final class ErrorMessage extends ByteBufMessage { public byte cmd; public byte code; public String reason; + public String data; public ErrorMessage(byte cmd, Packet message, Connection connection) { super(message, connection); @@ -32,6 +52,7 @@ public void decode(ByteBuf body) { cmd = decodeByte(body); code = decodeByte(body); reason = decodeString(body); + data = decodeString(body); } @Override @@ -39,6 +60,7 @@ public void encode(ByteBuf body) { encodeByte(body, cmd); encodeByte(body, code); encodeString(body, reason); + encodeString(body, data); } public static ErrorMessage from(BaseMessage src) { @@ -56,6 +78,11 @@ public ErrorMessage setReason(String reason) { return this; } + public ErrorMessage setData(String data) { + this.data = data; + return this; + } + public ErrorMessage setErrorCode(ErrorCode code) { this.code = code.errorCode; this.reason = code.errorMsg; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java index e60bd0f7..b1cbf8b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -40,4 +59,15 @@ public void encode(ByteBuf body) { encodeInt(body, minHeartbeat); encodeInt(body, maxHeartbeat); } + + @Override + public String toString() { + return "FastConnectMessage{" + + "deviceId='" + deviceId + '\'' + + ", sessionId='" + sessionId + '\'' + + ", minHeartbeat=" + minHeartbeat + + ", maxHeartbeat=" + maxHeartbeat + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index 2e4982c6..917f206c 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -34,4 +53,12 @@ public FastConnectOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; } + + @Override + public String toString() { + return "FastConnectOkMessage{" + + "heartbeat=" + heartbeat + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 6b67aea7..02d20c5b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -1,9 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Arrays; + import static com.mpush.api.protocol.Command.HANDSHAKE; /** @@ -54,4 +75,20 @@ public void encode(ByteBuf body) { encodeInt(body, maxHeartbeat); encodeLong(body, timestamp); } + + @Override + public String toString() { + return "HandshakeMessage{" + + "clientKey=" + Arrays.toString(clientKey) + + ", deviceId='" + deviceId + '\'' + + ", osName='" + osName + '\'' + + ", osVersion='" + osVersion + '\'' + + ", clientVersion='" + clientVersion + '\'' + + ", iv=" + Arrays.toString(iv) + + ", minHeartbeat=" + minHeartbeat + + ", maxHeartbeat=" + maxHeartbeat + + ", timestamp=" + timestamp + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index 990cad06..26fa9ca3 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -1,9 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Arrays; + /** * Created by ohun on 2015/12/27. * @@ -58,4 +79,15 @@ public HandshakeOkMessage setExpireTime(long expireTime) { this.expireTime = expireTime; return this; } + + @Override + public String toString() { + return "HandshakeOkMessage{" + + "expireTime=" + expireTime + + ", serverKey=" + Arrays.toString(serverKey) + + ", heartbeat=" + heartbeat + + ", sessionId='" + sessionId + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index bc1edcdd..0102f5c4 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -1,9 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; import java.util.Map; @@ -31,7 +50,7 @@ public HttpRequestMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { method = decodeByte(body); uri = decodeString(body); - headers = MPushUtil.headerFromString(decodeString(body)); + headers = Utils.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -39,7 +58,7 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeByte(body, method); encodeString(body, uri); - encodeString(body, MPushUtil.headerToString(headers)); + encodeString(body, Utils.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 5b53aa40..4fde32d6 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; import java.util.HashMap; @@ -27,7 +46,7 @@ public HttpResponseMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { statusCode = decodeInt(body); reasonPhrase = decodeString(body); - headers = MPushUtil.headerFromString(decodeString(body)); + headers = Utils.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -35,7 +54,7 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeInt(body, statusCode); encodeString(body, reasonPhrase); - encodeString(body, MPushUtil.headerToString(headers)); + encodeString(body, Utils.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java index 3c2293df..15571038 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index b48fbc9b..5edcb2d9 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index c8b3b608..b93a252b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -1,9 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import java.util.Arrays; + import static com.mpush.api.protocol.Command.PUSH; /** @@ -13,24 +34,32 @@ */ public final class PushMessage extends BaseMessage { - public String content; + public byte[] content; public PushMessage(Packet packet, Connection connection) { super(packet, connection); } - public PushMessage(String content, Connection connection) { + public PushMessage(byte[] content, Connection connection) { super(new Packet(PUSH, genSessionId()), connection); this.content = content; } @Override public void decode(byte[] body) { - content = new String(body, Constants.UTF_8); + content = body; } @Override public byte[] encode() { - return content == null ? null : content.getBytes(Constants.UTF_8); + return content; + } + + @Override + public String toString() { + return "PushMessage{" + + "content='" + content.length + '\'' + + ", packet=" + packet + + '}'; } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 328eace3..ead4f160 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message.gateway; import com.mpush.api.connection.Connection; @@ -15,11 +34,13 @@ */ public class GatewayPushMessage extends ByteBufMessage { public String userId; - public String content; + public int clientType; + public byte[] content; - public GatewayPushMessage(String userId, String content, Connection connection) { + public GatewayPushMessage(String userId, int clientType, byte[] content, Connection connection) { super(new Packet(GATEWAY_PUSH, genSessionId()), connection); this.userId = userId; + this.clientType = clientType; this.content = content; } @@ -30,13 +51,15 @@ public GatewayPushMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { userId = decodeString(body); - content = decodeString(body); + clientType = decodeInt(body); + content = decodeBytes(body); } @Override public void encode(ByteBuf body) { encodeString(body, userId); - encodeString(body, content); + encodeInt(body, clientType); + encodeBytes(body, content); } @Override @@ -53,7 +76,8 @@ public void send(ChannelFutureListener listener) { public String toString() { return "GatewayPushMessage{" + "userId='" + userId + '\'' + - ", content='" + content + '\'' + + "clientType='" + clientType + '\'' + + ", content='" + content.length + '\'' + '}'; } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java new file mode 100644 index 00000000..f1ad04ea --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -0,0 +1,118 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.net; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.api.spi.net.DnsMappingManager; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import static com.mpush.tools.Utils.checkHealth; + +public class HttpProxyDnsMappingManager extends BaseService implements DnsMappingManager, Runnable { + private final Logger logger = LoggerFactory.getLogger(HttpProxyDnsMappingManager.class); + + public HttpProxyDnsMappingManager() { + } + + private final Map> all = Maps.newConcurrentMap(); + private Map> available = Maps.newConcurrentMap(); + + private ScheduledExecutorService executorService; + + @Override + protected void doStart(Listener listener) throws Throwable { + if (all.size() > 0) { + executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + } + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (executorService != null) { + executorService.shutdown(); + } + } + + @Override + public void init() { + logger.error("start init dnsMapping"); + all.putAll(CC.mp.http.dns_mapping); + available.putAll(CC.mp.http.dns_mapping); + logger.error("end init dnsMapping"); + } + + @Override + public boolean isRunning() { + return executorService != null && !executorService.isShutdown(); + } + + public void update(Map> nowAvailable) { + available = nowAvailable; + } + + public Map> getAll() { + return all; + } + + public DnsMapping lookup(String origin) { + if (available.isEmpty()) return null; + List list = available.get(origin); + if (list == null || list.isEmpty()) return null; + int L = list.size(); + if (L == 1) return list.get(0); + return list.get((int) (Math.random() * L % L)); + } + + @Override + public void run() { + logger.debug("start dns mapping checkHealth"); + Map> all = this.getAll(); + Map> available = Maps.newConcurrentMap(); + for (Map.Entry> entry : all.entrySet()) { + String key = entry.getKey(); + List value = entry.getValue(); + List nowValue = Lists.newArrayList(); + for (DnsMapping temp : value) { + boolean isOk = checkHealth(temp.getIp(), temp.getPort()); + if (isOk) { + nowValue.add(temp); + } else { + logger.error("dns can not reachable:" + Jsons.toJson(temp)); + } + } + available.put(key, nowValue); + } + this.update(available); + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java index 4c548316..fec83fde 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java @@ -1,33 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.mpush.api.router.ClientType; +import java.util.Set; import java.util.concurrent.TimeUnit; /** * Created by ohun on 2016/1/4. */ public final class ConnectionRouterManager extends RemoteRouterManager { - public static final ConnectionRouterManager INSTANCE = new ConnectionRouterManager(); + public static final ConnectionRouterManager I = new ConnectionRouterManager(); // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 - private final Cache cache = CacheBuilder + private final Cache> cache = CacheBuilder .newBuilder() .expireAfterWrite(5, TimeUnit.MINUTES) .expireAfterAccess(5, TimeUnit.MINUTES) .build(); @Override - public RemoteRouter lookup(String userId) { - RemoteRouter cached = cache.getIfPresent(userId); + public Set lookupAll(String userId) { + Set cached = cache.getIfPresent(userId); if (cached != null) return cached; - RemoteRouter router = super.lookup(userId); + Set router = super.lookupAll(userId); if (router != null) { cache.put(userId, router); } return router; } + @Override + public RemoteRouter lookup(String userId, int clientType) { + Set cached = this.lookupAll(userId); + for (RemoteRouter router : cached) { + if (router.getRouteValue().getClientType() == clientType) { + return router; + } + } + return null; + } + /** * 如果推送失败,可能是缓存不一致了,可以让本地缓存失效 *

@@ -36,6 +68,6 @@ public RemoteRouter lookup(String userId) { * @param userId */ public void invalidateLocalCache(String userId) { - cache.invalidate(userId); + if (userId != null) cache.invalidate(userId); } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index 5f1f78f1..d7d9efed 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; -import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.Router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 510372b5..ef71818d 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -1,43 +1,113 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; -import com.mpush.api.RedisKey; +import com.google.common.eventbus.Subscribe; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; -import com.mpush.tools.redis.manage.RedisManage; - +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + /** * Created by ohun on 2015/12/23. * * @author ohun@live.cn */ -public class RemoteRouterManager implements RouterManager { +public class RemoteRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(RemoteRouterManager.class); @Override public RemoteRouter register(String userId, RemoteRouter router) { - LOGGER.info("register remote router success userId={}, router={}", userId, router); - String key = RedisKey.getUserKey(userId); - RemoteRouter old = RedisManage.get(key, RemoteRouter.class); + LOGGER.info("register remote router success userId={}, router={}", userId, router); + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(router.getRouteValue().getClientType()); + ClientLocation old = RedisManager.I.hget(key, field, ClientLocation.class); if (old != null) { - RedisManage.del(key); + RedisManager.I.hdel(key, field); } - RedisManage.set(key, router); - return old; + RedisManager.I.hset(key, field, router.getRouteValue()); + return old == null ? null : new RemoteRouter(old); } @Override - public boolean unRegister(String userId) { - String key = RedisKey.getUserKey(userId); - RedisManage.del(key); - LOGGER.info("unRegister remote router success userId={}", userId); + public boolean unRegister(String userId, int clientType) { + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(clientType); + RedisManager.I.hdel(key, field); + LOGGER.info("unRegister remote router success userId={}, clientType={}", userId, clientType); return true; } @Override - public RemoteRouter lookup(String userId) { - String key = RedisKey.getUserKey(userId); - return RedisManage.get(key, RemoteRouter.class); + public Set lookupAll(String userId) { + String key = RedisKey.getUserKey(userId); + Map values = RedisManager.I.hgetAll(key, ClientLocation.class); + if (values == null || values.isEmpty()) return Collections.emptySet(); + return values.values().stream().map(RemoteRouter::new).collect(Collectors.toSet()); + } + + @Override + public RemoteRouter lookup(String userId, int clientType) { + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(clientType); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + LOGGER.info("lookup remote router userId={}, router={}", userId, location); + return location == null ? null : new RemoteRouter(location); + } + + /** + * 监听链接关闭事件,清理失效的路由 + * + * @param event + */ + @Subscribe + void on(ConnectionCloseEvent event) { + Connection connection = event.connection; + if (connection == null) return; + SessionContext context = connection.getSessionContext(); + String userId = context.userId; + if (userId == null) return; + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(context.getClientType()); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + if (location == null) return; + + String connId = connection.getId(); + //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 + if (connId.equals(location.getConnId())) { + RedisManager.I.hdel(key, field); + LOGGER.info("clean disconnected remote route, userId={}, route={}", userId, location); + } else { + LOGGER.info("clean disconnected remote route, not clean:userId={}, route={}", userId, location); + } } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index dd1a16db..e619ddf4 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -1,52 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; -import com.mpush.tools.redis.listener.MessageListener; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.cache.redis.listener.MessageListener; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.Utils; +import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.common.AbstractEventContainer; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.listener.ListenerDispatcher; -import com.mpush.tools.redis.manage.RedisManage; - /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ -public class UserChangeListener extends AbstractEventContainer implements MessageListener { - - private static final Logger log = LoggerFactory.getLogger(UserChangeListener.class); - +public class UserChangeListener extends EventConsumer implements MessageListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserChangeListener.class); + public static final String ONLINE_CHANNEL = "/mpush/online/"; - + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; //只需要一台机器注册online、offline 消息通道 public UserChangeListener() { - if(ConfigCenter.I.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ - ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); - ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this); - }else{ - log.error("UserChangeListener is not localhost,required:{},but:{}",ConfigCenter.I.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp()); - } + if ("127.0.0.1".equals(Utils.getLocalIp())) { + ListenerDispatcher.I.subscribe(getOnlineChannel(), this); + ListenerDispatcher.I.subscribe(getOfflineChannel(), this); + } else { + LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", Utils.getLocalIp()); + } } public String getOnlineChannel() { return ONLINE_CHANNEL; } - + public String getOfflineChannel() { return OFFLINE_CHANNEL; } - + public void userOnline(String userId) { - RedisManage.publish(getOnlineChannel(), userId); + RedisManager.I.publish(getOnlineChannel(), userId); } - - public void userOffline(String userId){ - RedisManage.publish(getOnlineChannel(), userId); + + public void userOffline(String userId) { + RedisManager.I.publish(getOnlineChannel(), userId); } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 2bf17464..52c6e4b6 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -1,9 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; -import com.mpush.tools.Profiler; import com.mpush.tools.crypto.AESUtils; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import static com.mpush.tools.crypto.AESUtils.KEY_ALGORITHM; + /** * Created by ohun on 2015/12/28. @@ -13,30 +36,25 @@ public final class AesCipher implements Cipher { public final byte[] key; public final byte[] iv; + private final IvParameterSpec zeroIv; + private final SecretKeySpec keySpec; public AesCipher(byte[] key, byte[] iv) { this.key = key; this.iv = iv; + this.zeroIv = new IvParameterSpec(iv); + this.keySpec = new SecretKeySpec(key, KEY_ALGORITHM); } + @Override - public byte[] decrypt(byte[] data) { - try { - Profiler.enter("start aes decrypt"); - return AESUtils.decrypt(data, key, iv); - } finally { - Profiler.release(); - } + public byte[] encrypt(byte[] data) { + return AESUtils.encrypt(data, zeroIv, keySpec); } @Override - public byte[] encrypt(byte[] data) { - try { - Profiler.enter("start encrypt"); - return AESUtils.encrypt(data, key, iv); - } finally { - Profiler.release(); - } + public byte[] decrypt(byte[] data) { + return AESUtils.decrypt(data, zeroIv, keySpec); } @Override @@ -55,7 +73,7 @@ public String toString(byte[] a) { public static byte[] toArray(String str) { String[] a = str.split("\\|"); - if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { + if (a.length != CipherBox.I.getAesKeyLength()) { return null; } byte[] bytes = new byte[a.length]; diff --git a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index d8585cdb..c471a5d5 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -1,6 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import com.mpush.tools.crypto.RSAUtils; import java.security.SecureRandom; @@ -13,15 +32,15 @@ * @author ohun@live.cn */ public final class CipherBox { - public int aesKeyLength = ConfigCenter.I.aesKeyLength(); - public static final CipherBox INSTANCE = new CipherBox(); + public final int aesKeyLength = CC.mp.security.aes_key_length; + public static final CipherBox I = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; private RSAPublicKey publicKey; public RSAPrivateKey getPrivateKey() { if (privateKey == null) { - String key = ConfigCenter.I.privateKey(); + String key = CC.mp.security.private_key; try { privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(key); } catch (Exception e) { @@ -33,7 +52,7 @@ public RSAPrivateKey getPrivateKey() { public RSAPublicKey getPublicKey() { if (publicKey == null) { - String key = ConfigCenter.I.publicKey(); + String key = CC.mp.security.public_key; try { publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(key); } catch (Exception e) { @@ -70,8 +89,4 @@ public byte[] mixKey(byte[] clientKey, byte[] serverKey) { public int getAesKeyLength() { return aesKeyLength; } - - public RsaCipher getRsaCipher() { - return new RsaCipher(getPrivateKey(), getPublicKey()); - } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index 2afce590..4acfa4b9 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -1,7 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; -import com.mpush.tools.Profiler; import com.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; @@ -14,7 +32,6 @@ */ public final class RsaCipher implements Cipher { private final RSAPrivateKey privateKey; - private final RSAPublicKey publicKey; public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @@ -24,28 +41,20 @@ public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @Override public byte[] decrypt(byte[] data) { - try{ - Profiler.enter("start rsa decrypt"); - return RSAUtils.decryptByPrivateKey(data, privateKey); - }finally{ - Profiler.release(); - } - + return RSAUtils.decryptByPrivateKey(data, privateKey); } @Override public byte[] encrypt(byte[] data) { - try{ - Profiler.enter("start rsa encrypt"); - return RSAUtils.encryptByPublicKey(data, publicKey); - }finally{ - Profiler.release(); - } + return RSAUtils.encryptByPublicKey(data, publicKey); } - @Override - public String toString() { - return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; - } - + @Override + public String toString() { + return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; + } + + public static RsaCipher create() { + return new RsaCipher(CipherBox.I.getPrivateKey(), CipherBox.I.getPublicKey()); + } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java new file mode 100644 index 00000000..441f5006 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.security; + +import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.core.CipherFactory; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class RsaCipherFactory implements CipherFactory { + private static final RsaCipher RSA_CIPHER = RsaCipher.create(); + + @Override + public Cipher get() { + return RSA_CIPHER; + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java new file mode 100644 index 00000000..7a9d8372 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -0,0 +1,68 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.user; + +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.config.ConfigManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +//查询使用 +public final class UserManager { + private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); + public static final UserManager I = new UserManager(); + + private final String ONLINE_KEY = RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()); + + public UserManager() { + clearUserOnlineData(); + } + + public void clearUserOnlineData() { + RedisManager.I.del(ONLINE_KEY); + } + + public void recordUserOnline(String userId) { + RedisManager.I.zAdd(ONLINE_KEY, userId); + LOGGER.info("user online {}", userId); + } + + public void recordUserOffline(String userId) { + RedisManager.I.zRem(ONLINE_KEY, userId); + LOGGER.info("user offline {}", userId); + } + + //在线用户 + public long getOnlineUserNum() { + Long value = RedisManager.I.zCard(ONLINE_KEY); + return value == null ? 0 : value; + } + + //在线用户列表 + public List getOnlineUserList(int start, int size) { + if (size < 10) { + size = 10; + } + return RedisManager.I.zrange(ONLINE_KEY, start, size - 1, String.class); + } +} diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory new file mode 100644 index 00000000..02ce67e8 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory @@ -0,0 +1 @@ +com.mpush.common.security.RsaCipherFactory diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager new file mode 100644 index 00000000..d0dd9b36 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager @@ -0,0 +1 @@ +com.mpush.common.net.HttpProxyDnsMappingManager \ No newline at end of file diff --git a/mpush-core/.gitignore b/mpush-core/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-core/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index de05f1c5..174efd1a 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -2,11 +2,11 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} mpush-core @@ -20,27 +20,7 @@ ${mpush.groupId} - mpush-client - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring + mpush-common diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 82df0f24..bdc19126 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -1,45 +1,98 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; +import com.google.common.base.Strings; +import com.mpush.api.push.PushSender; +import com.mpush.api.service.Listener; +import com.mpush.common.router.RemoteRouter; +import com.mpush.common.user.UserManager; +import com.mpush.core.router.RouterCenter; +import com.mpush.core.server.AdminServer; +import com.mpush.tools.Jsons; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKClient; import com.mpush.zk.ZKPath; -import com.mpush.zk.ZKServerNode; -import com.mpush.api.RedisKey; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.zk.node.ZKServerNode; +import com.typesafe.config.ConfigRenderOptions; import io.netty.channel.*; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.PrintWriter; +import java.io.Serializable; +import java.io.StringWriter; +import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; @ChannelHandler.Sharable public final class AdminHandler extends SimpleChannelInboundHandler { - private static final Logger log = LoggerFactory.getLogger(AdminHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AdminHandler.class); - private static final String DOUBLE_END = "\r\n\r\n"; - private static final String ONE_END = "\r\n"; + private static final String EOL = "\r\n"; - protected static final ZKClient zkClient = ZKClient.I; + private static AdminServer adminServer; + + public AdminHandler(AdminServer adminServer) { + this.adminServer = adminServer; + } @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { - Command command = Command.getCommand(request); - ChannelFuture future = ctx.write(command.handler(request) + DOUBLE_END); - if (command.equals(Command.QUIT)) { - future.addListener(ChannelFutureListener.CLOSE); + Command command = Command.help; + String arg = null; + String[] args = null; + if (request != null) { + String[] cmd_args = request.split(" "); + command = Command.toCmd(cmd_args[0].trim()); + if (cmd_args.length == 2) { + arg = cmd_args[1]; + } else if (cmd_args.length > 2) { + args = Arrays.copyOfRange(cmd_args, 1, cmd_args.length); + } + } + try { + Object result = args != null ? command.handler(ctx, args) : command.handler(ctx, arg); + ChannelFuture future = ctx.writeAndFlush(result + EOL + EOL); + if (command == Command.quit) { + future.addListener(ChannelFutureListener.CLOSE); + } + } catch (Throwable throwable) { + ctx.writeAndFlush(throwable.getLocalizedMessage() + EOL + EOL); + StringWriter writer = new StringWriter(1024); + throwable.printStackTrace(new PrintWriter(writer)); + ctx.writeAndFlush(writer.toString()); } - } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + ONE_END); - ctx.write("It is " + new Date() + " now." + DOUBLE_END); + ctx.write("welcome to " + Utils.getInetAddress() + "!" + EOL); + ctx.write("It is " + new Date() + " now." + EOL + EOL); ctx.flush(); } @@ -49,87 +102,165 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { } public enum Command { - HELP("help") { + help { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { StringBuilder buf = new StringBuilder(); - buf.append("Command:" + ONE_END); - buf.append("help:display all command." + ONE_END); - buf.append("quit:exit telnet." + ONE_END); - buf.append("scn:statistics conn num." + ONE_END); - buf.append("rcs:remove connection server zk info." + ONE_END); - buf.append("scs:stop connection server."); + buf.append("Option Description" + EOL); + buf.append("------ -----------" + EOL); + buf.append("help show help" + EOL); + buf.append("quit exit console mode" + EOL); + buf.append("shutdown stop mpush server" + EOL); + buf.append("restart restart mpush server" + EOL); + buf.append("zk: query zk node" + EOL); + buf.append("count: count conn num or online user count" + EOL); + buf.append("route: show user route info" + EOL); + buf.append("push:, push test msg to client" + EOL); + buf.append("conf:[key] show config info" + EOL); + buf.append("monitor:[mxBean] show system monitor" + EOL); return buf.toString(); } }, - QUIT("quit") { + quit { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { return "have a good day!"; } }, - SCN("scn") { + shutdown { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + ctx.writeAndFlush("try close connect server..."); + adminServer.getConnectionServer().stop(new Listener() { + @Override + public void onSuccess(Object... args) { + ctx.writeAndFlush("connect server close success" + EOL); + adminServer.stop(null);//这个一定要在System.exit之前调用,不然jvm 会卡死 @see com.mpush.bootstrap.Main#addHook + System.exit(0); + } + + @Override + public void onFailure(Throwable cause) { + ctx.writeAndFlush("connect server close failure, msg=" + cause.getLocalizedMessage()); + } + }); + return null; + } + }, + restart { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + return "unsupported"; + } + }, + zk { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + switch (args) { + case "redis": + return ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); + case "cs": + return getNodeData(ZKPath.CONNECT_SERVER); + case "gs": + return getNodeData(ZKPath.GATEWAY_SERVER); + + } + return "[" + args + "] unsupported, try help."; + } + + private String getNodeData(ZKPath path) { + List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); + StringBuilder sb = new StringBuilder(); + for (String raw : rawData) { + sb.append(ZKClient.I.get(path.getFullPath(raw))).append('\n'); + } + return sb.toString(); + } + }, + count { + @Override + public Serializable handler(ChannelHandlerContext ctx, String args) { + switch (args) { + case "conn": + return adminServer.getConnectionServer().getConnectionManager().getConnections().size(); + case "online": { + return UserManager.I.getOnlineUserNum(); + } + + } + return "[" + args + "] unsupported, try help."; + } + }, + route { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + if (Strings.isNullOrEmpty(args)) return "please input userId"; + Set routers = RouterCenter.I.getRemoteRouterManager().lookupAll(args); + if (routers.isEmpty()) return "user [" + args + "] offline now."; + return Jsons.toJson(routers); + } + }, + push { @Override - public String handler(String request) { - Long value = RedisManage.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); - if (value == null) { - value = 0L; + public String handler(ChannelHandlerContext ctx, String... args) throws Exception { + Boolean success = PushSender.create().send(args[1], args[0], null).get(5, TimeUnit.SECONDS); + + return success.toString(); + } + }, + conf { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + if (Strings.isNullOrEmpty(args)) { + return CC.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true)); + } + if (CC.cfg.hasPath(args)) { + return CC.cfg.getAnyRef(args).toString(); } - return value.toString() + "."; + return "key [" + args + "] not find in config"; } }, - RCS("rcs") { + rcs { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { - List rawData = zkClient.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + List rawData = ZKClient.I.getChildrenKeys(ZKPath.CONNECT_SERVER.getRootPath()); boolean removeSuccess = false; + String localIp = ConfigManager.I.getLocalIp(); for (String raw : rawData) { - String data = zkClient.get(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + String dataPath = ZKPath.CONNECT_SERVER.getFullPath(raw); + String data = ZKClient.I.get(dataPath); ZKServerNode serverNode = Jsons.fromJson(data, ZKServerNode.class); - if (serverNode.getIp().equals(MPushUtil.getInetAddress())) { - zkClient.remove(ZKPath.CONNECTION_SERVER.getFullPath(raw)); - log.info("delete connection server success:{}", data); + if (serverNode.getIp().equals(localIp)) { + ZKClient.I.remove(dataPath); + LOGGER.info("delete connection server success:{}", data); removeSuccess = true; } else { - log.info("delete connection server failed: required ip:{}, but:{}", serverNode.getIp(), MPushUtil.getInetAddress()); + LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), Utils.getInetAddress()); } } if (removeSuccess) { - return "remove success."; + return "removeAndClose success."; } else { - return "remove false."; + return "removeAndClose false."; } } - }, - SCS("scs") { - @Override - public String handler(String request) { - return "not support now."; - } }; - private final String cmd; - - public abstract String handler(String request); - private Command(String cmd) { - this.cmd = cmd; + public Object handler(ChannelHandlerContext ctx, String... args) throws Exception { + return "unsupported"; } - public String getCmd() { - return cmd; + public Object handler(ChannelHandlerContext ctx, String args) throws Exception { + return "unsupported"; } - public static Command getCommand(String request) { - if (StringUtils.isNoneEmpty(request)) { - for (Command command : Command.values()) { - if (command.getCmd().equals(request)) { - return command; - } - } + public static Command toCmd(String cmd) { + try { + return Command.valueOf(cmd); + } catch (Exception e) { } - return HELP; + return help; } } - } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 30c25115..b02c3adf 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; @@ -5,13 +24,13 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOnlineEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.OkMessage; import com.mpush.core.router.RouterCenter; -import com.mpush.log.Logs; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/23. @@ -36,16 +55,15 @@ public void handle(BindUserMessage message) { SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 - boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); + boolean success = RouterCenter.I.register(message.userId, message.getConnection()); if (success) { - - EventBus.INSTANCE.post(new UserOnlineEvent(message.getConnection(), message.userId)); - + context.userId = message.userId; + EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").send(); Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 - RouterCenter.INSTANCE.unRegister(message.userId); + RouterCenter.I.unRegister(message.userId, context.getClientType()); ErrorMessage.from(message).setReason("bind failed").close(); Logs.Conn.info("bind user failure, userId={}, session={}", message.userId, context); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index de0c9e40..bdf0abba 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.connection.Connection; @@ -8,10 +27,8 @@ import com.mpush.common.message.FastConnectOkMessage; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/25. @@ -19,7 +36,6 @@ * @author ohun@live.cn */ public final class FastConnectHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(FastConnectHandler.class); @Override public FastConnectMessage decode(Packet packet, Connection connection) { @@ -34,14 +50,16 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); + Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}" + , message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); + Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}" + , message.deviceId, session.context); } else { //3.校验成功,重新计算心跳,完成快速重连 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); session.context.setHeartbeat(heartbeat); message.getConnection().setSessionContext(session.context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 69acb209..dc7cfe78 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.connection.Connection; @@ -10,10 +29,8 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.core.router.LocalRouter; import com.mpush.core.router.RouterCenter; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; +import com.mpush.tools.Utils; +import com.mpush.tools.log.Logs; import static com.mpush.common.ErrorCode.*; @@ -32,7 +49,7 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { /** * 处理PushClient发送过来的Push推送请求 *

- * 查推送策略,先查本地路由,本地不存在,查远程,(注意:有可能远程也是本机) + * 查寻路由策略,先查本地路由,本地不存在,查远程,(注意:有可能远程查到也是本机IP) *

* 正常情况本地路由应该存在,如果不存在或链接失效,有以下几种情况: *

@@ -40,9 +57,9 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { * 2.客户端下线,本地路由失效,远程路由还未清除 * 3.PushClient使用了本地缓存,但缓存数据已经和实际情况不一致了 *

- * 对于三种情况的处理方式是, 再检查下远程路由: - * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 - * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 + * 对于三种情况的处理方式是, 再重新查寻下远程路由: + * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 (解决场景2) + * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 (解决场景1,3) *

* * @param message @@ -62,7 +79,9 @@ public void handle(GatewayPushMessage message) { * @return */ private boolean checkLocal(final GatewayPushMessage message) { - LocalRouter router = RouterCenter.INSTANCE.getLocalRouterManager().lookup(message.userId); + String userId = message.userId; + int deviceId = message.clientType; + LocalRouter router = RouterCenter.I.getLocalRouterManager().lookup(userId, deviceId); //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 if (router == null) return false; @@ -72,10 +91,10 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - Logs.PUSH.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection); //删除已经失效的本地路由 - RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); + RouterCenter.I.getLocalRouterManager().unRegister(userId, deviceId); return false; } @@ -83,21 +102,18 @@ private boolean checkLocal(final GatewayPushMessage message) { //3.链接可用,直接下发消息到手机客户端 PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - //推送成功 - OkMessage.from(message).setData(message.userId).send(); + pushMessage.send(future -> { + if (future.isSuccess()) { + //推送成功 + OkMessage.from(message).setData(userId + ',' + deviceId).send(); - Logs.PUSH.info("gateway push message to client success userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push message to client success, message={}", message); - } else { - //推送失败 - ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); + } else { + //推送失败 + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + deviceId).send(); - Logs.PUSH.info("gateway push message to client failure userId={}, content={}", message.userId, message.content); - } + Logs.PUSH.info("gateway push message to client failure, message={}", message); } }); return true; @@ -112,35 +128,37 @@ public void operationComplete(ChannelFuture future) throws Exception { * @param message */ private void checkRemote(GatewayPushMessage message) { - RemoteRouter router = RouterCenter.INSTANCE.getRemoteRouterManager().lookup(message.userId); + String userId = message.userId; + int clientType = message.clientType; + RemoteRouter router = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); // 1.如果远程路由信息也不存在, 说明用户此时不在线, if (router == null) { - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send(); - Logs.PUSH.info("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push, router not exists user offline, message={}", message); return; } //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 - if (MPushUtil.getLocalIp().equals(router.getRouteValue().getHost())) { + if (Utils.getLocalIp().equals(router.getRouteValue().getHost())) { - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send(); //删除失效的远程缓存 - RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); + RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); - Logs.PUSH.info("gateway push error remote is local, userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, router); return; } //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 - ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).send(); - Logs.PUSH.info("gateway push, router in remote userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, router); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 30f3400b..17879376 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; @@ -5,7 +24,6 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.HandshakeEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.HandshakeMessage; @@ -14,10 +32,9 @@ import com.mpush.common.security.CipherBox; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/24. @@ -25,34 +42,33 @@ * @author ohun@live.cn */ public final class HandshakeHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); @Override public HandshakeMessage decode(Packet packet, Connection connection) { - return new HandshakeMessage(packet, connection); + return new HandshakeMessage(packet, connection); } @Override public void handle(HandshakeMessage message) { - + byte[] iv = message.iv;//AES密钥向量16位 byte[] clientKey = message.clientKey;//客户端随机数16位 - byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数16位 - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥16位 + byte[] serverKey = CipherBox.I.randomAESKey();//服务端随机数16位 + byte[] sessionKey = CipherBox.I.mixKey(clientKey, serverKey);//会话密钥16位 //1.校验客户端消息字段 if (Strings.isNullOrEmpty(message.deviceId) - || iv.length != CipherBox.INSTANCE.getAesKeyLength() - || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { + || iv.length != CipherBox.I.getAesKeyLength() + || clientKey.length != CipherBox.I.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - Logs.Conn.info("client handshake false:{}", message.getConnection()); + Logs.Conn.info("handshake failure, message={}", message.toString()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - Logs.Conn.info("client handshake false for repeat handshake:{}", message.getConnection().getSessionContext()); + Logs.Conn.info("handshake failure, repeat handshake, session={}", message.getConnection().getSessionContext()); return; } @@ -63,7 +79,7 @@ public void handle(HandshakeMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); //5.计算心跳时间 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); //6.响应握手成功消息 HandshakeOkMessage @@ -88,7 +104,7 @@ public void handle(HandshakeMessage message) { ReusableSessionManager.INSTANCE.cacheSession(session); //10.触发握手成功事件 - EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); - Logs.Conn.info("client handshake success:{}", context); + EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); + Logs.Conn.info("handshake success, session={}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 5feea063..88ffaf18 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -1,9 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/22. @@ -15,7 +34,6 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong - Logs.HB.info("response client heartbeat:{}, {}", - connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("ping -> pong, {}", connection); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 4cc42f1d..efc014e2 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -1,25 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.log.Logs; -import com.mpush.netty.client.HttpCallback; -import com.mpush.netty.client.HttpClient; -import com.mpush.netty.client.RequestInfo; -import com.mpush.tools.Profiler; -import com.mpush.tools.dns.DnsMapping; -import com.mpush.tools.dns.manage.DnsMappingManage; +import com.mpush.common.net.HttpProxyDnsMappingManager; +import com.mpush.netty.http.HttpCallback; +import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.RequestContext; +import com.mpush.tools.common.Profiler; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; import org.slf4j.Logger; import java.net.InetSocketAddress; -import java.net.URI; -import java.net.URISyntaxException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Map; import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; @@ -33,6 +55,7 @@ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = Logs.HTTP; private final HttpClient httpClient; + private final DnsMappingManager dnsMappingManager = SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager); public HttpProxyHandler(HttpClient httpClient) { this.httpClient = httpClient; @@ -48,6 +71,7 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { public void handle(HttpRequestMessage message) { try { Profiler.enter("start http proxy handler"); + //1.参数校验 String method = message.getMethod(); String uri = message.uri; if (Strings.isNullOrEmpty(uri)) { @@ -59,18 +83,16 @@ public void handle(HttpRequestMessage message) { LOGGER.warn("request url is empty!"); } + //2.url转换 uri = doDnsMapping(uri); + + //3.包装成HTTP request FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); - Profiler.enter("start set full http headers"); - setHeaders(request, message); - Profiler.release(); - Profiler.enter("start set full http body"); - setBody(request, message); - Profiler.release(); + setHeaders(request, message);//处理header + setBody(request, message);//处理body - Profiler.enter("start http proxy request"); - httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); - Profiler.release(); + //4.发送请求 + httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage .from(message) @@ -160,10 +182,8 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { } } InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); - Profiler.enter("start set x-forwarded-for"); - String remoteIp = remoteAddress.getAddress().getHostAddress(); + String remoteIp = remoteAddress.getAddress().getHostAddress();//这个要小心,不要使用getHostName,不然会耗时比较大 request.headers().add("x-forwarded-for", remoteIp); - Profiler.release(); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } @@ -176,15 +196,19 @@ private void setBody(FullHttpRequest request, HttpRequestMessage message) { } private String doDnsMapping(String url) { - URI uri = null; + URL uri = null; try { - uri = new URI(url); - } catch (URISyntaxException e) { + uri = new URL(url); + } catch (MalformedURLException e) { + } + if (uri == null) { + return url; } - if (uri == null) return url; String host = uri.getHost(); - DnsMapping mapping = DnsMappingManage.holder.translate(host); - if (mapping == null) return url; - return url.replaceFirst(host, mapping.toString()); + DnsMapping mapping = dnsMappingManager.lookup(host); + if (mapping == null) { + return url; + } + return mapping.translate(uri); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index 8b339af8..ad8739e8 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -1,13 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.core.router.LocalRouterManager; -import com.mpush.core.router.RouterCenter; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; @@ -15,7 +31,10 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.core.router.LocalRouter; -import com.mpush.log.Logs; +import com.mpush.core.router.LocalRouterManager; +import com.mpush.core.router.RouterCenter; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** @@ -49,33 +68,36 @@ public void handle(BindUserMessage message) { if (context.handshakeOk()) { //2.先删除远程路由, 必须是同一个设备才允许解绑 boolean unRegisterSuccess = true; - RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); - RemoteRouter remoteRouter = remoteRouterManager.lookup(message.userId); + int clientType = context.getClientType(); + String userId = message.userId; + RemoteRouterManager remoteRouterManager = RouterCenter.I.getRemoteRouterManager(); + RemoteRouter remoteRouter = remoteRouterManager.lookup(userId, clientType); if (remoteRouter != null) { String deviceId = remoteRouter.getRouteValue().getDeviceId(); if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = remoteRouterManager.unRegister(message.userId); + unRegisterSuccess = remoteRouterManager.unRegister(userId, clientType); } } //3.删除本地路由信息 - LocalRouterManager localRouterManager = RouterCenter.INSTANCE.getLocalRouterManager(); - LocalRouter localRouter = localRouterManager.lookup(message.userId); + LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter localRouter = localRouterManager.lookup(userId, clientType); if (localRouter != null) { String deviceId = localRouter.getRouteValue().getSessionContext().deviceId; if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = localRouterManager.unRegister(message.userId) && unRegisterSuccess; + unRegisterSuccess = localRouterManager.unRegister(userId, clientType) && unRegisterSuccess; } } //4.路由删除成功,广播用户下线事件 if (unRegisterSuccess) { - EventBus.INSTANCE.post(new UserOfflineEvent(message.getConnection(), message.userId)); + context.userId = null; + EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").send(); - Logs.Conn.info("unbind user success, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").send(); - Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index 71f635e9..f670f428 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -1,17 +1,44 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; +import com.mpush.api.router.ClientType; + /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ public final class KickRemoteMsg { - public String userId; - public String deviceId; - public String targetServer; + public String userId; + public String deviceId; + public int clientType; + public String targetServer; - @Override - public String toString() { - return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + ", targetServer='" + targetServer + '\'' + '}'; - } + @Override + public String toString() { + return "KickRemoteMsg{" + + "userId='" + userId + '\'' + + ", deviceId='" + deviceId + '\'' + + ", clientType='" + clientType + '\'' + + ", targetServer='" + targetServer + '\'' + + '}'; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index c078ec3f..43130d9b 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -1,6 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.mpush.api.connection.Connection; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; /** @@ -10,9 +30,15 @@ */ public final class LocalRouter implements Router { private final Connection connection; + private final int clientType; public LocalRouter(Connection connection) { this.connection = connection; + this.clientType = connection.getSessionContext().getClientType(); + } + + public int getClientType() { + return clientType; } @Override @@ -25,6 +51,22 @@ public RouterType getRouteType() { return RouterType.LOCAL; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + LocalRouter that = (LocalRouter) o; + + return clientType == that.clientType; + + } + + @Override + public int hashCode() { + return Integer.hashCode(clientType); + } + @Override public String toString() { return "LocalRouter{" + connection + '}'; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 391bb377..57de36a4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -1,67 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.event.UserOfflineEvent; +import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; -import com.mpush.common.AbstractEventContainer; -import com.mpush.common.EventBus; -import com.mpush.common.router.RemoteRouter; - +import com.mpush.tools.event.EventBus; +import com.mpush.tools.event.EventConsumer; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; +import java.util.*; /** * Created by ohun on 2015/12/23. * * @author ohun@live.cn */ -public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { +public final class LocalRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); + private static final Map EMPTY = Collections.unmodifiableMap(new HashMap<>(0)); /** * 本地路由表 */ - private final Map routers = new ConcurrentHashMapV8<>(); - - /** - * 反向关系表 - */ - private final Map connIdUserIds = new ConcurrentHashMapV8<>(); + private final Map> routers = new ConcurrentHashMapV8<>(); @Override public LocalRouter register(String userId, LocalRouter router) { LOGGER.info("register local router success userId={}, router={}", userId, router); - connIdUserIds.put(router.getRouteValue().getId(), userId); - //add online userId - return routers.put(userId, router); + return routers.computeIfAbsent(userId, s -> new HashMap<>()).put(router.getClientType(), router); } @Override - public boolean unRegister(String userId) { - LocalRouter router = routers.remove(userId); - if (router != null) { - connIdUserIds.remove(router.getRouteValue().getId()); - } + public boolean unRegister(String userId, int clientType) { + LocalRouter router = routers.getOrDefault(userId, EMPTY).remove(clientType); LOGGER.info("unRegister local router success userId={}, router={}", userId, router); return true; } @Override - public LocalRouter lookup(String userId) { - LocalRouter router = routers.get(userId); - LOGGER.info("lookup local router userId={}, router={}", userId, router); - return router; + public Set lookupAll(String userId) { + return new HashSet<>(routers.getOrDefault(userId, EMPTY).values()); } - public String getUserIdByConnId(String connId) { - return connIdUserIds.get(connId); + @Override + public LocalRouter lookup(String userId, int clientType) { + LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); + LOGGER.info("lookup local router userId={}, router={}", userId, router); + return router; } /** @@ -70,24 +80,25 @@ public String getUserIdByConnId(String connId) { * @param event */ @Subscribe - void onConnectionCloseEvent(ConnectionCloseEvent event) { - Connection connection = event.connection; - if(connection == null) return; - String id = event.connection.getId(); + void on(ConnectionCloseEvent event) { + Connection connection = event.connection; + if (connection == null) return; + SessionContext context = connection.getSessionContext(); - //1.清除反向关系 - String userId = connIdUserIds.remove(id); + String userId = context.userId; if (userId == null) return; - EventBus.INSTANCE.post(new UserOfflineEvent(event.connection, userId)); - LocalRouter router = routers.get(userId); + + EventBus.I.post(new UserOfflineEvent(event.connection, userId)); + int clientType = context.getClientType(); + LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); if (router == null) return; + String connId = connection.getId(); //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 - if (id.equals(router.getRouteValue().getId())) { + if (connId.equals(router.getRouteValue().getId())) { //3.删除路由 - routers.remove(userId); + routers.getOrDefault(userId, EMPTY).remove(clientType); LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); - } else { //如果不相等,则log一下 LOGGER.info("clean disconnected local route, not clean:userId={}, route={}", userId, router); } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index b072bb28..0140d2a4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -1,17 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.mpush.api.connection.Connection; import com.mpush.api.event.RouterChangeEvent; -import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; -import com.mpush.common.EventBus; +import com.mpush.api.router.ClientType; +import com.mpush.api.router.Router; import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; -import com.mpush.tools.MPushUtil; - +import com.mpush.tools.Utils; +import com.mpush.tools.event.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Set; + /** * Created by ohun on 2015/12/23. * @@ -19,7 +40,7 @@ */ public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); - public static final RouterCenter INSTANCE = new RouterCenter(); + public static final RouterCenter I = new RouterCenter(); private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); @@ -36,8 +57,8 @@ public final class RouterCenter { */ public boolean register(String userId, Connection connection) { ClientLocation location = ClientLocation - .from(connection.getSessionContext()) - .setHost(MPushUtil.getLocalIp()); + .from(connection) + .setHost(Utils.getLocalIp()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(location); @@ -47,36 +68,41 @@ public boolean register(String userId, Connection connection) { try { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); - } catch (Exception e) { LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); } if (oldLocalRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); + EventBus.I.post(new RouterChangeEvent(userId, oldLocalRouter)); LOGGER.info("register router success, find old local router={}, userId={}", oldLocalRouter, userId); } if (oldRemoteRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldRemoteRouter)); + EventBus.I.post(new RouterChangeEvent(userId, oldRemoteRouter)); LOGGER.info("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); } return true; } - public boolean unRegister(String userId) { - localRouterManager.unRegister(userId); - remoteRouterManager.unRegister(userId); + public boolean unRegister(String userId, int clientType) { + localRouterManager.unRegister(userId, clientType); + remoteRouterManager.unRegister(userId, clientType); return true; } - public Router lookup(String userId) { - LocalRouter local = localRouterManager.lookup(userId); + public Router lookup(String userId, int clientType) { + LocalRouter local = localRouterManager.lookup(userId, clientType); if (local != null) return local; - RemoteRouter remote = remoteRouterManager.lookup(userId); + RemoteRouter remote = remoteRouterManager.lookup(userId, clientType); return remote; } + public Set> lookupAll(String userId) { + Set locals = localRouterManager.lookupAll(userId); + if (locals != null) return locals; + Set remotes = remoteRouterManager.lookupAll(userId); + return remotes; + } public LocalRouterManager getLocalRouterManager() { return localRouterManager; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index e218cbee..e0f5b61f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -1,36 +1,54 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.mpush.api.RedisKey; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; -import com.mpush.common.AbstractEventContainer; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.cache.redis.listener.MessageListener; +import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.message.KickUserMessage; import com.mpush.common.router.RemoteRouter; -import com.mpush.log.Logs; - import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.listener.ListenerDispatcher; -import com.mpush.tools.redis.listener.MessageListener; -import com.mpush.tools.redis.manage.RedisManage; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; +import com.mpush.tools.Utils; +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.event.EventConsumer; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ -public final class RouterChangeListener extends AbstractEventContainer implements MessageListener { +public final class RouterChangeListener extends EventConsumer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; - private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); + private final String kick_channel = KICK_CHANNEL_ + Utils.getLocalIp(); public RouterChangeListener() { - ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); + ListenerDispatcher.I.subscribe(getKickChannel(), this); } public String getKickChannel() { @@ -42,7 +60,7 @@ public String getKickChannel(String remoteIp) { } @Subscribe - void onRouteChange(RouterChangeEvent event) { + void on(RouterChangeEvent event) { String userId = event.userId; Router r = event.router; if (r.getRouteType().equals(Router.RouterType.LOCAL)) { @@ -64,14 +82,11 @@ public void kickLocal(final String userId, final LocalRouter router) { KickUserMessage message = new KickUserMessage(connection); message.deviceId = context.deviceId; message.userId = userId; - message.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); - } else { - Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); - } + message.send(future -> { + if (future.isSuccess()) { + Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); + } else { + Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); } }); } @@ -88,7 +103,7 @@ public void operationComplete(ChannelFuture future) throws Exception { public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 - if (location.getHost().equals(MPushUtil.getLocalIp())) { + if (location.getHost().equals(Utils.getLocalIp())) { Logs.Conn.info("kick remote user but router in local, userId={}", userId); return; } @@ -97,9 +112,10 @@ public void kickRemote(String userId, RemoteRouter router) { //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); + msg.clientType = location.getClientType(); msg.targetServer = location.getHost(); msg.userId = userId; - RedisManage.publish(getKickChannel(msg.targetServer), msg); + RedisManager.I.publish(getKickChannel(msg.targetServer), msg); } /** @@ -112,19 +128,20 @@ public void kickRemote(String userId, RemoteRouter router) { */ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 - if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + if (!msg.targetServer.equals(Utils.getLocalIp())) { + Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); return; } //2.查询本地路由,找到要被踢下线的链接,并删除该本地路由 String userId = msg.userId; - LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); - LocalRouter router = routerManager.lookup(userId); + int clientType = msg.clientType; + LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter router = routerManager.lookup(userId, clientType); if (router != null) { Logs.Conn.info("receive kick remote msg, msg={}", msg); //2.1删除本地路由信息 - routerManager.unRegister(userId); + routerManager.unRegister(userId, clientType); //2.2发送踢人消息到客户端 kickLocal(userId, router); remStatUser(userId); @@ -148,6 +165,6 @@ public void onMessage(String channel, String message) { } private void remStatUser(String userId) { - RedisManage.zRem(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp()), userId); + RedisManager.I.zRem(RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()), userId); } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 1b514a5d..d0987f04 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -1,11 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.event.UserOnlineEvent; -import com.mpush.common.EventBus; -import com.mpush.common.manage.user.UserManager; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.user.UserManager; +import com.mpush.tools.event.EventBus; /** * Created by ohun on 2015/12/23. @@ -19,18 +38,18 @@ public final class UserOnlineOfflineListener { public static final String OFFLINE_CHANNEL = "/mpush/offline/"; public UserOnlineOfflineListener() { - EventBus.INSTANCE.register(this); + EventBus.I.register(this); } @Subscribe - void onUserOnline(UserOnlineEvent event) { - UserManager.INSTANCE.recordUserOnline(event.getUserId()); - RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); + void on(UserOnlineEvent event) { + UserManager.I.recordUserOnline(event.getUserId()); + RedisManager.I.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe - void onUserOffline(UserOfflineEvent event) { - UserManager.INSTANCE.recordUserOffline(event.getUserId()); - RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); + void on(UserOfflineEvent event) { + UserManager.I.recordUserOffline(event.getUserId()); + RedisManager.I.publish(OFFLINE_CHANNEL, event.getUserId()); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index f4fbf926..589d7474 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -1,21 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; import com.mpush.core.handler.AdminHandler; import com.mpush.netty.server.NettyServer; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; -import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; +import java.util.concurrent.Executor; + public final class AdminServer extends NettyServer { + private final ConnectionServer connectionServer; + private final GatewayServer gatewayServer; - private final AdminHandler adminHandler = new AdminHandler(); + private final AdminHandler adminHandler; - public AdminServer(int port) { + public AdminServer(int port, ConnectionServer connectionServer, GatewayServer gatewayServer) { super(port); + this.connectionServer = connectionServer; + this.gatewayServer = gatewayServer; + this.adminHandler = new AdminHandler(this); } @Override @@ -24,6 +50,16 @@ protected void initPipeline(ChannelPipeline pipeline) { super.initPipeline(pipeline); } + @Override + protected Executor getBossExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + @Override + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + @Override public ChannelHandler getChannelHandler() { return adminHandler; @@ -38,4 +74,12 @@ protected ChannelHandler getDecoder() { protected ChannelHandler getEncoder() { return new StringEncoder(); } + + public ConnectionServer getConnectionServer() { + return connectionServer; + } + + public GatewayServer getGatewayServer() { + return gatewayServer; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index dd5a5398..b691e027 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -1,26 +1,54 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; -import com.mpush.core.handler.*; -import com.mpush.netty.client.HttpClient; -import com.mpush.netty.client.NettyHttpClient; -import com.mpush.netty.connection.NettyConnectionManager; -import com.mpush.netty.server.NettyServer; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.core.handler.*; +import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.NettyHttpClient; +import com.mpush.netty.server.NettyServer; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.connect_server.*; /** * Created by ohun on 2015/12/30. */ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; + private GlobalChannelTrafficShapingHandler trafficShapingHandler; - private ConnectionManager connectionManager = new NettyConnectionManager(); + private ConnectionManager connectionManager = new ServerConnectionManager(); private HttpClient httpClient; public ConnectionServer(int port) { @@ -38,20 +66,49 @@ public void init() { receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - if (ConfigCenter.I.httpProxyEnable()) { + if (CC.mp.http.proxy_enabled) { httpClient = new NettyHttpClient(); receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); } channelHandler = new ServerChannelHandler(true, connectionManager, receiver); + + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override public void stop(Listener listener) { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } super.stop(listener); if (httpClient != null) httpClient.stop(); connectionManager.destroy(); } + @Override + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + @Override + protected Executor getBossExecutor() { + return ThreadPoolManager.I.getBossExecutor(); + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + @Override protected void initOptions(ServerBootstrap b) { super.initOptions(b); @@ -76,4 +133,12 @@ protected void initOptions(ServerBootstrap b) { public ChannelHandler getChannelHandler() { return channelHandler; } + + public ConnectionManager getConnectionManager() { + return connectionManager; + } + + public HttpClient getHttpClient() { + return httpClient; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 6ec0ce7f..fb7976ee 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -1,11 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; -import com.mpush.netty.connection.NettyConnectionManager; import com.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_server.*; /** * Created by ohun on 2015/12/30. @@ -15,7 +40,8 @@ public final class GatewayServer extends NettyServer { private ServerChannelHandler channelHandler; - private NettyConnectionManager connectionManager; + private ServerConnectionManager connectionManager; + private GlobalChannelTrafficShapingHandler trafficShapingHandler; public GatewayServer(int port) { super(port); @@ -26,14 +52,34 @@ public void init() { super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); - connectionManager = new NettyConnectionManager(); + connectionManager = new ServerConnectionManager(); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override public void stop(Listener listener) { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } super.stop(listener); - connectionManager.destroy(); + if (connectionManager != null) { + connectionManager.destroy(); + } + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 73a8d6eb..9aa7c078 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -1,21 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; -import com.mpush.netty.connection.NettyConnection; +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.PacketReceiver; -import com.mpush.common.EventBus; -import com.mpush.log.Logs; - -import com.mpush.tools.Profiler; - +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.common.Profiler; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,33 +60,32 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - try{ - Profiler.start("end channel read:"); - Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("channelRead channel={}, packet={}", ctx.channel(), msg); - connection.updateLastReadTime(); - receiver.onReceive((Packet) msg, connection); - }finally{ - Profiler.release(); - long duration = Profiler.getDuration(); - if(duration>80){ - LOGGER.error("end channel read:"+duration+","+Profiler.dump()); - } - Profiler.reset(); - } - + try { + Profiler.start("channel read:"); + Connection connection = connectionManager.get(ctx.channel()); + LOGGER.debug("channelRead channel={}, connection={}, packet={}", ctx.channel(), connection, msg); + connection.updateLastReadTime(); + receiver.onReceive((Packet) msg, connection); + } finally { + Profiler.release(); + long duration = Profiler.getDuration(); + if (duration > 80) { + LOGGER.error("channel read busy:" + duration + "," + Profiler.dump()); + } + Profiler.reset(); + } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - connectionManager.remove(ctx.channel()); - Logs.Conn.info("client exceptionCaught channel={}", ctx.channel()); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + Connection connection = connectionManager.removeAndClose(ctx.channel()); + Logs.Conn.error("client exceptionCaught channel={}, connection={}", ctx.channel(), connection); + LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client connect channel={}", ctx.channel()); + Logs.Conn.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -78,9 +93,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client disconnect channel={}", ctx.channel()); - Connection connection = connectionManager.get(ctx.channel()); - EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); - connectionManager.remove(ctx.channel()); + Connection connection = connectionManager.removeAndClose(ctx.channel()); + EventBus.I.post(new ConnectionCloseEvent(connection)); + Logs.Conn.info("client disconnect channel={}, connection={}", ctx.channel(), connection); } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java similarity index 55% rename from mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java rename to mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 43d14a39..f18d9f91 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -1,4 +1,23 @@ -package com.mpush.netty.connection; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.server; import com.google.common.collect.Lists; @@ -6,16 +25,16 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.HandshakeEvent; -import com.mpush.common.EventBus; -import com.mpush.log.Logs; - -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; @@ -26,8 +45,7 @@ * * @author ohun@live.cn */ -public final class NettyConnectionManager implements ConnectionManager { - //可能会有20w的链接数 +public final class ServerConnectionManager implements ConnectionManager { private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); private Timer timer; @@ -36,9 +54,9 @@ public final class NettyConnectionManager implements ConnectionManager { public void init() { //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s - int ticksPerWheel = (int) (ConfigCenter.I.maxHeartbeat() / tickDuration); + int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); - EventBus.INSTANCE.register(this); + EventBus.I.register(this); } @Override @@ -52,20 +70,21 @@ public void destroy() { @Override public Connection get(final Channel channel) { - return connections.get(channel.id().asLongText()); + return connections.get(channel.id().asShortText()); } @Override public void add(Connection connection) { - connections.putIfAbsent(connection.getId(), connection); + connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); } @Override - public void remove(Channel channel) { - Connection connection = connections.remove(channel.id().asLongText()); + public Connection removeAndClose(Channel channel) { + Connection connection = connections.remove(channel.id().asShortText()); if (connection != null) { connection.close(); } + return connection; } @Override @@ -74,14 +93,14 @@ public List getConnections() { } @Subscribe - void onHandshakeOk(HandshakeEvent event) { + void on(HandshakeEvent event) { HeartbeatCheckTask task = new HeartbeatCheckTask(event.connection); task.startTimeout(); } private class HeartbeatCheckTask implements TimerTask { - private int expiredTimes = 0; + private int timeoutTimes = 0; private final Connection connection; public HeartbeatCheckTask(Connection connection) { @@ -90,28 +109,27 @@ public HeartbeatCheckTask(Connection connection) { public void startTimeout() { int timeout = connection.getSessionContext().heartbeat; - timer.newTimeout(this, timeout > 0 ? timeout : ConfigCenter.I.minHeartbeat(), TimeUnit.MILLISECONDS); + timer.newTimeout(this, timeout > 0 ? timeout : CC.mp.core.min_heartbeat, TimeUnit.MILLISECONDS); } @Override public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) { - Logs.HB.info("connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection was disconnected, heartbeat timeout times={}, connection={}", timeoutTimes, connection); return; } if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.I.maxHBTimeoutTimes()) { + if (++timeoutTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); - Logs.HB.info("connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("client heartbeat timeout times={}, do close connection={}", timeoutTimes, connection); return; } else { - Logs.HB.info("connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("client heartbeat timeout times={}, connection={}", timeoutTimes, connection); } } else { - expiredTimes = 0; - Logs.HB.info("connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + timeoutTimes = 0; + //Logs.HB.info("client heartbeat timeout times reset 0, connection={}", connection); } - startTimeout(); } } diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java index 7a8e2f72..4336b491 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.session; import com.mpush.api.connection.SessionContext; diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index 1fbf2c78..5b9394bc 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -1,11 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.session; -import com.mpush.api.RedisKey; import com.mpush.api.connection.SessionContext; -import com.mpush.tools.Strings; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.common.Strings; +import com.mpush.tools.config.CC; import com.mpush.tools.crypto.MD5Utils; -import com.mpush.tools.redis.manage.RedisManage; /** * Created by ohun on 2015/12/25. @@ -14,17 +33,17 @@ */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private int expiredTime = ConfigCenter.I.sessionExpiredTime(); + private int expiredTime = CC.mp.core.session_expired_time; public boolean cacheSession(ReusableSession session) { - String key = RedisKey.getSessionKey(session.sessionId); - RedisManage.set(key, ReusableSession.encode(session.context), expiredTime); + String key = RedisKey.getSessionKey(session.sessionId); + RedisManager.I.set(key, ReusableSession.encode(session.context), expiredTime); return true; } public ReusableSession querySession(String sessionId) { - String key = RedisKey.getSessionKey(sessionId); - String value = RedisManage.get(key, String.class); + String key = RedisKey.getSessionKey(sessionId); + String value = RedisManager.I.get(key, String.class); if (Strings.isBlank(value)) return null; return ReusableSession.decode(value); } diff --git a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java deleted file mode 100644 index f51ae52f..00000000 --- a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.mpush.core.netty; - -import com.mpush.api.Server; -import com.mpush.core.server.ConnectionServer; -import org.junit.Test; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public class NettyServerTest { - @Test - public void testStart() throws Exception { - ConnectionServer server = new ConnectionServer(3000); - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess(int port) { - } - - @Override - public void onFailure(Throwable cause) { - } - }); - } -} \ No newline at end of file diff --git a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java index 7810ae22..818c9719 100644 --- a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.security; import com.mpush.common.security.CipherBox; @@ -17,9 +36,9 @@ public void testGetPrivateKey() throws Exception { @Test public void testGetPublicKey() throws Exception { for (int i = 0; i < 1000; i++) { - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] serverKey = CipherBox.INSTANCE.randomAESKey(); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey); + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] serverKey = CipherBox.I.randomAESKey(); + byte[] sessionKey = CipherBox.I.mixKey(clientKey, serverKey); //System.out.println("clientKey:" + Arrays.toString(clientKey)); //System.out.println("serverKey:" + Arrays.toString(serverKey)); System.out.println("sessionKey:" + Arrays.toString(sessionKey)); diff --git a/mpush-core/src/test/resources/logback.xml b/mpush-core/src/test/resources/logback.xml index 20979b40..1ff25c6d 100644 --- a/mpush-core/src/test/resources/logback.xml +++ b/mpush-core/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - + + System.out + UTF-8 + DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-log/.gitignore b/mpush-log/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-log/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml deleted file mode 100644 index f8f5595e..00000000 --- a/mpush-log/pom.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - mpush - com.mpush - 1.0 - - 4.0.0 - ${mpush.groupId} - mpush-log - ${mpush-log-version} - jar - mpush-log - - - UTF-8 - - - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring - - - diff --git a/mpush-log/src/main/java/com/mpush/log/Logs.java b/mpush-log/src/main/java/com/mpush/log/Logs.java deleted file mode 100644 index d9504ab2..00000000 --- a/mpush-log/src/main/java/com/mpush/log/Logs.java +++ /dev/null @@ -1,331 +0,0 @@ -package com.mpush.log; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.Marker; - -/** - * Created by ohun on 2016/5/16. - * - * @author ohun@live.cn - */ -public enum Logs implements Logger { - Conn("connectionLog"), - PUSH("pushLog"), - HB("heartBeatLog"), - REDIS("redisLog"), - ZK("zkLog"), - HTTP("httpLog"); - - private final Logger logger; - - Logs(String loggerName) { - this.logger = LoggerFactory.getLogger(loggerName); - } - - @Override - public String getName() { - return logger.getName(); - } - - @Override - public boolean isTraceEnabled() { - return logger.isTraceEnabled(); - } - - @Override - public void trace(String s) { - logger.trace(s); - } - - @Override - public void trace(String s, Object o) { - logger.trace(s, o); - } - - @Override - public void trace(String s, Object o, Object o1) { - logger.trace(s, o, o1); - } - - @Override - public void trace(String s, Object... objects) { - logger.trace(s, objects); - } - - @Override - public void trace(String s, Throwable throwable) { - logger.trace(s, throwable); - } - - @Override - public boolean isTraceEnabled(Marker marker) { - return isTraceEnabled(marker); - } - - @Override - public void trace(Marker marker, String s) { - logger.trace(marker, s); - } - - @Override - public void trace(Marker marker, String s, Object o) { - logger.trace(marker, s, o); - } - - @Override - public void trace(Marker marker, String s, Object o, Object o1) { - logger.trace(marker, s, o, o1); - } - - @Override - public void trace(Marker marker, String s, Object... objects) { - logger.trace(marker, s, objects); - } - - @Override - public void trace(Marker marker, String s, Throwable throwable) { - logger.trace(marker, s, throwable); - } - - @Override - public boolean isDebugEnabled() { - return isErrorEnabled(); - } - - @Override - public void debug(String s) { - logger.debug(s); - } - - @Override - public void debug(String s, Object o) { - logger.debug(s, o); - } - - @Override - public void debug(String s, Object o, Object o1) { - logger.debug(s, o, o1); - } - - @Override - public void debug(String s, Object... objects) { - logger.debug(s, objects); - } - - @Override - public void debug(String s, Throwable throwable) { - logger.debug(s, throwable); - } - - @Override - public boolean isDebugEnabled(Marker marker) { - return logger.isDebugEnabled(marker); - } - - @Override - public void debug(Marker marker, String s) { - logger.debug(marker, s); - } - - @Override - public void debug(Marker marker, String s, Object o) { - logger.debug(marker, s, o); - } - - @Override - public void debug(Marker marker, String s, Object o, Object o1) { - logger.debug(marker, s, o, o1); - } - - @Override - public void debug(Marker marker, String s, Object... objects) { - logger.debug(marker, s, objects); - } - - @Override - public void debug(Marker marker, String s, Throwable throwable) { - logger.debug(marker, s, throwable); - } - - @Override - public boolean isInfoEnabled() { - return logger.isInfoEnabled(); - } - - @Override - public void info(String s) { - logger.info(s); - } - - @Override - public void info(String s, Object o) { - logger.info(s, o); - } - - @Override - public void info(String s, Object o, Object o1) { - logger.info(s, o, o1); - } - - @Override - public void info(String s, Object... objects) { - logger.info(s, objects); - } - - @Override - public void info(String s, Throwable throwable) { - logger.info(s, throwable); - } - - @Override - public boolean isInfoEnabled(Marker marker) { - return logger.isInfoEnabled(marker); - } - - @Override - public void info(Marker marker, String s) { - logger.info(marker, s); - } - - @Override - public void info(Marker marker, String s, Object o) { - logger.info(marker, s, o); - } - - @Override - public void info(Marker marker, String s, Object o, Object o1) { - logger.info(marker, s, o, o1); - } - - @Override - public void info(Marker marker, String s, Object... objects) { - logger.info(marker, s, objects); - } - - @Override - public void info(Marker marker, String s, Throwable throwable) { - logger.info(marker, s, throwable); - } - - @Override - public boolean isWarnEnabled() { - return logger.isWarnEnabled(); - } - - @Override - public void warn(String s) { - logger.warn(s); - } - - @Override - public void warn(String s, Object o) { - logger.warn(s, o); - } - - @Override - public void warn(String s, Object... objects) { - logger.warn(s, objects); - } - - @Override - public void warn(String s, Object o, Object o1) { - logger.warn(s, o, o1); - } - - @Override - public void warn(String s, Throwable throwable) { - logger.warn(s, throwable); - } - - @Override - public boolean isWarnEnabled(Marker marker) { - return isWarnEnabled(marker); - } - - @Override - public void warn(Marker marker, String s) { - logger.warn(marker, s); - } - - @Override - public void warn(Marker marker, String s, Object o) { - logger.warn(marker, s, o); - } - - @Override - public void warn(Marker marker, String s, Object o, Object o1) { - logger.warn(marker, s, o, o1); - } - - @Override - public void warn(Marker marker, String s, Object... objects) { - logger.warn(marker, s, objects); - } - - @Override - public void warn(Marker marker, String s, Throwable throwable) { - logger.warn(marker, s, throwable); - } - - @Override - public boolean isErrorEnabled() { - return isErrorEnabled(); - } - - @Override - public void error(String s) { - logger.error(s); - } - - @Override - public void error(String s, Object o) { - logger.error(s, o); - } - - @Override - public void error(String s, Object o, Object o1) { - logger.error(s, o, o1); - } - - @Override - public void error(String s, Object... objects) { - logger.error(s, objects); - } - - @Override - public void error(String s, Throwable throwable) { - logger.error(s, throwable); - } - - @Override - public boolean isErrorEnabled(Marker marker) { - return isErrorEnabled(marker); - } - - @Override - public void error(Marker marker, String s) { - logger.error(marker, s); - } - - @Override - public void error(Marker marker, String s, Object o) { - logger.error(marker, s, o); - } - - @Override - public void error(Marker marker, String s, Object o, Object o1) { - logger.error(marker, s, o, o1); - } - - @Override - public void error(Marker marker, String s, Object... objects) { - logger.error(marker, s, objects); - } - - @Override - public void error(Marker marker, String s, Throwable throwable) { - logger.error(marker, s, throwable); - } - -} diff --git a/mpush-log/src/test/java/com/mpush/AppTest.java b/mpush-log/src/test/java/com/mpush/AppTest.java deleted file mode 100644 index 796b8466..00000000 --- a/mpush-log/src/test/java/com/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-monitor/.gitignore b/mpush-monitor/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-monitor/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 6da74f1c..886b22fc 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -1,10 +1,10 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} @@ -14,9 +14,13 @@ mpush-monitor - - ${mpush.groupId} - mpush-tools - + + ${mpush.groupId} + mpush-api + + + ${mpush.groupId} + mpush-tools + diff --git a/mpush-monitor/src/main/java/com/mpush/App.java b/mpush-monitor/src/main/java/com/mpush/App.java deleted file mode 100644 index 0a7102ea..00000000 --- a/mpush-monitor/src/main/java/com/mpush/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mpush; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java new file mode 100644 index 00000000..e064273a --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.data; + +import com.mpush.tools.Jsons; + +import java.util.HashMap; +import java.util.Map; + +public class MonitorResult { + private Long timestamp = System.currentTimeMillis(); + private Map results = new HashMap<>(8); + + public MonitorResult addResult(String name, Object result) { + results.put(name, result); + return this; + } + + public Map getResults() { + return results; + } + + public MonitorResult setResults(Map results) { + this.results = results; + return this; + } + + public Long getTimestamp() { + return timestamp; + } + + public MonitorResult setTimestamp(Long timestamp) { + this.timestamp = timestamp; + return this; + } + + @Override + public String toString() { + return "MonitorResult{" + + "results=" + results + + ", timestamp=" + timestamp + + '}'; + } + + public String toJson() { + return Jsons.toJson(this); + } +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java new file mode 100644 index 00000000..f6d5f244 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java @@ -0,0 +1,41 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.data; + +import com.mpush.monitor.quota.impl.*; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class ResultCollector { + + public MonitorResult collect() { + MonitorResult result = new MonitorResult(); + result.addResult("jvm-info", JVMInfo.I.monitor()); + result.addResult("jvm-gc", JVMGC.I.monitor()); + result.addResult("jvm-memory", JVMMemory.I.monitor()); + result.addResult("jvm-thread", JVMThread.I.monitor()); + result.addResult("jvm-thread-pool", JVMThreadPool.I.monitor()); + return result; + } + +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java deleted file mode 100644 index 9ae26d87..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.mpush.monitor.domain; - -import java.util.Map; - -public class MonitorData { - - private Long timestamp; - - private Map memoryMap; - - private Map gcMap; - - private Map threadMap; - - private Map threadPoolMap; - - private Map infoMap; - - public MonitorData() { - this.timestamp = System.currentTimeMillis(); - } - - public Map getMemoryMap() { - return memoryMap; - } - - public void setMemoryMap(Map memoryMap) { - this.memoryMap = memoryMap; - } - - public Map getGcMap() { - return gcMap; - } - - public void setGcMap(Map gcMap) { - this.gcMap = gcMap; - } - - public Map getThreadMap() { - return threadMap; - } - - public void setThreadMap(Map threadMap) { - this.threadMap = threadMap; - } - - public Long getTimestamp() { - return timestamp; - } - - public Map getInfoMap() { - return infoMap; - } - - public void setInfoMap(Map infoMap) { - this.infoMap = infoMap; - } - - public Map getThreadPoolMap() { - return threadPoolMap; - } - - public void setThreadPoolMap(Map threadPoolMap) { - this.threadPoolMap = threadPoolMap; - } - -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java deleted file mode 100644 index 2c7ad2e7..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.monitor.quota; - -import java.util.List; -import java.util.Map; - -import com.google.common.collect.Lists; - -public abstract class BaseQuota { - - protected final static List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", - "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", - "Garbage collection optimized for deterministic pausetimes Old Collector"); - - protected final static List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", - "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); - - protected final static List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); - - protected final static List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); - - protected final static List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); - - protected final static List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); - - public static boolean contain(String name, List list) { - for (String str : list) { - if (name.equals(str)) { - return true; - } - } - return false; - } - - public abstract Map toMap(); -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java index 5dca627d..91d4cb24 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java @@ -1,21 +1,40 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface GCMQuota { - - public long yongGcCollectionCount(); - - public long yongGcCollectionTime(); - - public long fullGcCollectionCount(); - - public long fullGcCollectionTime(); - - public long spanYongGcCollectionCount(); - - public long spanYongGcCollectionTime(); - - public long spanFullGcCollectionCount(); - - public long spanFullGcCollectionTime(); - +public interface GCMQuota extends MonitorQuota { + + long yongGcCollectionCount(); + + long yongGcCollectionTime(); + + long fullGcCollectionCount(); + + long fullGcCollectionTime(); + + long spanYongGcCollectionCount(); + + long spanYongGcCollectionTime(); + + long spanFullGcCollectionCount(); + + long spanFullGcCollectionTime(); + } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java index 23a4b810..cec75350 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface InfoQuota { +public interface InfoQuota extends MonitorQuota { + + String pid(); - public String pid(); - - public double load(); + double load(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java index 3a368af2..83a01a82 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java @@ -1,59 +1,78 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface MemoryQuota { +public interface MemoryQuota extends MonitorQuota { - // Heap - long heapMemoryCommitted(); + // Heap + long heapMemoryCommitted(); - long heapMemoryInit(); + long heapMemoryInit(); - long heapMemoryMax(); + long heapMemoryMax(); - long heapMemoryUsed(); + long heapMemoryUsed(); - // NonHeap - long nonHeapMemoryCommitted(); + // NonHeap + long nonHeapMemoryCommitted(); - long nonHeapMemoryInit(); + long nonHeapMemoryInit(); - long nonHeapMemoryMax(); + long nonHeapMemoryMax(); - long nonHeapMemoryUsed(); + long nonHeapMemoryUsed(); - // PermGen - long permGenCommitted(); + // PermGen + long permGenCommitted(); - long permGenInit(); + long permGenInit(); - long permGenMax(); + long permGenMax(); - long permGenUsed(); + long permGenUsed(); - // OldGen - long oldGenCommitted(); + // OldGen + long oldGenCommitted(); - long oldGenInit(); + long oldGenInit(); - long oldGenMax(); + long oldGenMax(); - long oldGenUsed(); + long oldGenUsed(); - // EdenSpace - long edenSpaceCommitted(); + // EdenSpace + long edenSpaceCommitted(); - long edenSpaceInit(); + long edenSpaceInit(); - long edenSpaceMax(); + long edenSpaceMax(); - long edenSpaceUsed(); + long edenSpaceUsed(); - // Survivor - long survivorCommitted(); + // Survivor + long survivorCommitted(); - long survivorInit(); + long survivorInit(); - long survivorMax(); + long survivorMax(); - long survivorUsed(); + long survivorUsed(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java new file mode 100644 index 00000000..dc3c61ab --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java @@ -0,0 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.quota; + +public interface MonitorQuota { + + Object monitor(Object... args); +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java index 2396a0e6..1cba7c14 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java @@ -1,5 +1,24 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface ThreadPoolQuota { +public interface ThreadPoolQuota extends MonitorQuota { } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java index 8d000cdf..623572ec 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java @@ -1,14 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface ThreadQuota { +public interface ThreadQuota extends MonitorQuota { + + int daemonThreadCount(); + + int threadCount(); + + long totalStartedThreadCount(); + + int deadLockedThreadCount(); - public int daemonThreadCount(); - - public int threadCount(); - - public long totalStartedThreadCount(); - - public int deadLockedThreadCount(); - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java index 05c1f177..2b9d0903 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java @@ -1,136 +1,160 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.GCMQuota; + import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; +import java.util.List; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.GCMQuota; - -public class JVMGC extends BaseQuota implements GCMQuota { - - public static final JVMGC instance = new JVMGC(); - - private GarbageCollectorMXBean fullGc; - private GarbageCollectorMXBean yongGc; - - private long lastYoungGcCollectionCount = -1; - private long lastYoungGcCollectionTime = -1; - private long lastFullGcCollectionCount = -1; - private long lastFullGcCollectionTime = -1; - - - private JVMGC() { - for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { - String name = item.getName(); - if (contain(name, youngGcName)) { - yongGc = item; - } else if (contain(name,fullGcName)) { - fullGc = item; - } - } - - } - - @Override - public long yongGcCollectionCount() { - if (yongGc == null) { - return 0; - } - return yongGc.getCollectionCount(); - } - - @Override - public long yongGcCollectionTime() { - if (yongGc == null) { - return 0; - } - return yongGc.getCollectionTime(); - } - - @Override - public long fullGcCollectionCount() { - if (fullGc == null) { - return 0; - } - return fullGc.getCollectionCount(); - } - - @Override - public long fullGcCollectionTime() { - if (fullGc == null) { - return 0; - } - return fullGc.getCollectionTime(); - } - - @Override - public long spanYongGcCollectionCount() { - - long current = yongGcCollectionCount(); - if (lastYoungGcCollectionCount == -1) { - lastYoungGcCollectionCount = current; - return 0; - } else { - long result = current - lastYoungGcCollectionCount; - lastYoungGcCollectionCount = current; - return result; - } - - } - - @Override - public long spanYongGcCollectionTime() { - long current = yongGcCollectionTime(); - if (lastYoungGcCollectionTime == -1) { - lastYoungGcCollectionTime = current; - return 0; - } else { - long result = current - lastYoungGcCollectionTime; - lastYoungGcCollectionTime = current; - return result; - } - } - - @Override - public long spanFullGcCollectionCount() { - long current = fullGcCollectionCount(); - if (lastFullGcCollectionCount == -1) { - lastFullGcCollectionCount = current; - return 0; - } else { - long result = current - lastFullGcCollectionCount; - lastFullGcCollectionCount = current; - return result; - } - } - - @Override - public long spanFullGcCollectionTime() { - long current = fullGcCollectionTime(); - if (lastFullGcCollectionTime == -1) { - lastFullGcCollectionTime = current; - return 0; - } else { - long result = current - lastFullGcCollectionTime; - lastFullGcCollectionTime = current; - return result; - } - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("yongGcCollectionCount", yongGcCollectionCount()); - map.put("yongGcCollectionTime", yongGcCollectionTime()); - map.put("fullGcCollectionCount", fullGcCollectionCount()); - map.put("fullGcCollectionTime", fullGcCollectionTime()); - map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); - map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); - map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); - map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); - return map; - } - +public class JVMGC implements GCMQuota { + + private final List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", + "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", + "Garbage collection optimized for deterministic pausetimes Old Collector"); + + private final List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", + "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); + + public static final JVMGC I = new JVMGC(); + + private GarbageCollectorMXBean fullGc; + private GarbageCollectorMXBean yongGc; + + private long lastYoungGcCollectionCount = -1; + private long lastYoungGcCollectionTime = -1; + private long lastFullGcCollectionCount = -1; + private long lastFullGcCollectionTime = -1; + + + private JVMGC() { + for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { + String name = item.getName(); + if (youngGcName.contains(name)) { + yongGc = item; + } else if (fullGcName.contains(name)) { + fullGc = item; + } + } + } + + @Override + public long yongGcCollectionCount() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionCount(); + } + + @Override + public long yongGcCollectionTime() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionTime(); + } + + @Override + public long fullGcCollectionCount() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionCount(); + } + + @Override + public long fullGcCollectionTime() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionTime(); + } + + @Override + public long spanYongGcCollectionCount() { + + long current = yongGcCollectionCount(); + if (lastYoungGcCollectionCount == -1) { + lastYoungGcCollectionCount = current; + return 0; + } else { + long result = current - lastYoungGcCollectionCount; + lastYoungGcCollectionCount = current; + return result; + } + } + + @Override + public long spanYongGcCollectionTime() { + long current = yongGcCollectionTime(); + if (lastYoungGcCollectionTime == -1) { + lastYoungGcCollectionTime = current; + return 0; + } else { + long result = current - lastYoungGcCollectionTime; + lastYoungGcCollectionTime = current; + return result; + } + } + + @Override + public long spanFullGcCollectionCount() { + long current = fullGcCollectionCount(); + if (lastFullGcCollectionCount == -1) { + lastFullGcCollectionCount = current; + return 0; + } else { + long result = current - lastFullGcCollectionCount; + lastFullGcCollectionCount = current; + return result; + } + } + + @Override + public long spanFullGcCollectionTime() { + long current = fullGcCollectionTime(); + if (lastFullGcCollectionTime == -1) { + lastFullGcCollectionTime = current; + return 0; + } else { + long result = current - lastFullGcCollectionTime; + lastFullGcCollectionTime = current; + return result; + } + } + + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("yongGcCollectionCount", yongGcCollectionCount()); + map.put("yongGcCollectionTime", yongGcCollectionTime()); + map.put("fullGcCollectionCount", fullGcCollectionCount()); + map.put("fullGcCollectionTime", fullGcCollectionTime()); + map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); + map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); + map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); + map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index 42e2bd04..eb0276a4 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -1,49 +1,70 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.InfoQuota; +import com.mpush.monitor.quota.MonitorQuota; + import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.InfoQuota; -import com.mpush.monitor.quota.BaseQuota; - -public class JVMInfo extends BaseQuota implements InfoQuota { - - public static final JVMInfo instance = new JVMInfo(); - - private RuntimeMXBean runtimeMXBean; - - private OperatingSystemMXBean systemMXBean; - - private String currentPid; - - private JVMInfo() { - runtimeMXBean = ManagementFactory.getRuntimeMXBean(); - systemMXBean = ManagementFactory.getOperatingSystemMXBean(); - } - - @Override - public String pid() { - if (null == currentPid) { - currentPid = runtimeMXBean.getName().split("@")[0]; +public class JVMInfo implements InfoQuota { + + public static final JVMInfo I = new JVMInfo(); + + private RuntimeMXBean runtimeMXBean; + + private OperatingSystemMXBean systemMXBean; + + private String currentPid; + + private JVMInfo() { + runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + systemMXBean = ManagementFactory.getOperatingSystemMXBean(); + } + + @Override + public String pid() { + if (null == currentPid) { + currentPid = runtimeMXBean.getName().split("@")[0]; } return currentPid; - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("pid", pid()); - map.put("load", load()); - return map; - } - - @Override - public double load() { - double averageLoad = systemMXBean.getSystemLoadAverage(); - return averageLoad; - } + } + + @Override + public double load() { + double averageLoad = systemMXBean.getSystemLoadAverage(); + return averageLoad; + } + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("pid", pid()); + map.put("load", load()); + map.put("totalMemory", Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m"); + map.put("freeMemory", Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m"); + map.put("maxMemory", Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m"); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java index 2ccb5f19..441822a4 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java @@ -1,239 +1,265 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.MemoryQuota; + import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryPoolMXBean; import java.util.List; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.MemoryQuota; - -public class JVMMemory extends BaseQuota implements MemoryQuota { - - public static final JVMMemory instance = new JVMMemory(); - - private MemoryMXBean memoryMXBean; - - private MemoryPoolMXBean permGenMxBean; - private MemoryPoolMXBean oldGenMxBean; - private MemoryPoolMXBean edenSpaceMxBean; - private MemoryPoolMXBean pSSurvivorSpaceMxBean; - - private JVMMemory() { - memoryMXBean = ManagementFactory.getMemoryMXBean(); - List list = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean item : list) { - String name = item.getName(); - if (contain(name, permGenName)) { - permGenMxBean = item; - } else if (contain(name, oldGenName)) { - oldGenMxBean = item; - } else if (contain(name, edenSpaceName)) { - edenSpaceMxBean = item; - } else if (contain(name, psSurvivorName)) { - pSSurvivorSpaceMxBean = item; - } - } - } - - @Override - public long heapMemoryCommitted() { - return memoryMXBean.getHeapMemoryUsage().getCommitted(); - } - - @Override - public long heapMemoryInit() { - return memoryMXBean.getHeapMemoryUsage().getInit(); - } - - @Override - public long heapMemoryMax() { - return memoryMXBean.getHeapMemoryUsage().getMax(); - } - - @Override - public long heapMemoryUsed() { - return memoryMXBean.getHeapMemoryUsage().getUsed(); - } - - @Override - public long nonHeapMemoryCommitted() { - return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); - } - - @Override - public long nonHeapMemoryInit() { - return memoryMXBean.getNonHeapMemoryUsage().getInit(); - } - - @Override - public long nonHeapMemoryMax() { - return memoryMXBean.getNonHeapMemoryUsage().getMax(); - } - - @Override - public long nonHeapMemoryUsed() { - return memoryMXBean.getNonHeapMemoryUsage().getUsed(); - } - - @Override - public long permGenCommitted() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getCommitted(); - } - - @Override - public long permGenInit() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getInit(); - } - - @Override - public long permGenMax() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getMax(); - } - - @Override - public long permGenUsed() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getUsed(); - } - - @Override - public long oldGenCommitted() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getCommitted(); - } - - @Override - public long oldGenInit() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getInit(); - } - - @Override - public long oldGenMax() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getMax(); - } - - @Override - public long oldGenUsed() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getUsed(); - } - - @Override - public long edenSpaceCommitted() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getCommitted(); - } - - @Override - public long edenSpaceInit() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getInit(); - } - - @Override - public long edenSpaceMax() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getMax(); - } - - @Override - public long edenSpaceUsed() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getUsed(); - } - - @Override - public long survivorCommitted() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getCommitted(); - } - - @Override - public long survivorInit() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getInit(); - } - - @Override - public long survivorMax() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getMax(); - } - - @Override - public long survivorUsed() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getUsed(); - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("heapMemoryCommitted", heapMemoryCommitted()); - map.put("heapMemoryInit", heapMemoryInit()); - map.put("heapMemoryMax", heapMemoryMax()); - map.put("heapMemoryUsed", heapMemoryUsed()); - map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); - map.put("nonHeapMemoryInit", nonHeapMemoryInit()); - map.put("nonHeapMemoryMax", nonHeapMemoryMax()); - map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); - map.put("permGenCommitted", permGenCommitted()); - map.put("permGenInit", permGenInit()); - map.put("permGenMax", permGenMax()); - map.put("permGenUsed", permGenUsed()); - map.put("oldGenCommitted", oldGenCommitted()); - map.put("oldGenInit", oldGenInit()); - map.put("oldGenMax", oldGenMax()); - map.put("oldGenUsed", oldGenUsed()); - map.put("edenSpaceCommitted", edenSpaceCommitted()); - map.put("edenSpaceInit", edenSpaceInit()); - map.put("edenSpaceMax", edenSpaceMax()); - map.put("edenSpaceUsed", edenSpaceUsed()); - map.put("survivorCommitted", survivorCommitted()); - map.put("survivorInit", survivorInit()); - map.put("survivorMax", survivorMax()); - map.put("survivorUsed", survivorUsed()); - return map; - } - +public class JVMMemory implements MemoryQuota { + + private final List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); + + private final List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); + + private final List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); + + private final List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); + + public static final JVMMemory I = new JVMMemory(); + + private MemoryMXBean memoryMXBean; + + private MemoryPoolMXBean permGenMxBean; + private MemoryPoolMXBean oldGenMxBean; + private MemoryPoolMXBean edenSpaceMxBean; + private MemoryPoolMXBean pSSurvivorSpaceMxBean; + + private JVMMemory() { + memoryMXBean = ManagementFactory.getMemoryMXBean(); + List list = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean item : list) { + String name = item.getName(); + if (permGenName.contains(name)) { + permGenMxBean = item; + } else if (oldGenName.contains(name)) { + oldGenMxBean = item; + } else if (edenSpaceName.contains(name)) { + edenSpaceMxBean = item; + } else if (psSurvivorName.contains(name)) { + pSSurvivorSpaceMxBean = item; + } + } + } + + @Override + public long heapMemoryCommitted() { + return memoryMXBean.getHeapMemoryUsage().getCommitted(); + } + + @Override + public long heapMemoryInit() { + return memoryMXBean.getHeapMemoryUsage().getInit(); + } + + @Override + public long heapMemoryMax() { + return memoryMXBean.getHeapMemoryUsage().getMax(); + } + + @Override + public long heapMemoryUsed() { + return memoryMXBean.getHeapMemoryUsage().getUsed(); + } + + @Override + public long nonHeapMemoryCommitted() { + return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); + } + + @Override + public long nonHeapMemoryInit() { + return memoryMXBean.getNonHeapMemoryUsage().getInit(); + } + + @Override + public long nonHeapMemoryMax() { + return memoryMXBean.getNonHeapMemoryUsage().getMax(); + } + + @Override + public long nonHeapMemoryUsed() { + return memoryMXBean.getNonHeapMemoryUsage().getUsed(); + } + + @Override + public long permGenCommitted() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getCommitted(); + } + + @Override + public long permGenInit() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getInit(); + } + + @Override + public long permGenMax() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getMax(); + } + + @Override + public long permGenUsed() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getUsed(); + } + + @Override + public long oldGenCommitted() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getCommitted(); + } + + @Override + public long oldGenInit() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getInit(); + } + + @Override + public long oldGenMax() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getMax(); + } + + @Override + public long oldGenUsed() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getUsed(); + } + + @Override + public long edenSpaceCommitted() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long edenSpaceInit() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getInit(); + } + + @Override + public long edenSpaceMax() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getMax(); + } + + @Override + public long edenSpaceUsed() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getUsed(); + } + + @Override + public long survivorCommitted() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long survivorInit() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getInit(); + } + + @Override + public long survivorMax() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getMax(); + } + + @Override + public long survivorUsed() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getUsed(); + } + + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("heapMemoryCommitted", heapMemoryCommitted()); + map.put("heapMemoryInit", heapMemoryInit()); + map.put("heapMemoryMax", heapMemoryMax()); + map.put("heapMemoryUsed", heapMemoryUsed()); + map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); + map.put("nonHeapMemoryInit", nonHeapMemoryInit()); + map.put("nonHeapMemoryMax", nonHeapMemoryMax()); + map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); + map.put("permGenCommitted", permGenCommitted()); + map.put("permGenInit", permGenInit()); + map.put("permGenMax", permGenMax()); + map.put("permGenUsed", permGenUsed()); + map.put("oldGenCommitted", oldGenCommitted()); + map.put("oldGenInit", oldGenInit()); + map.put("oldGenMax", oldGenMax()); + map.put("oldGenUsed", oldGenUsed()); + map.put("edenSpaceCommitted", edenSpaceCommitted()); + map.put("edenSpaceInit", edenSpaceInit()); + map.put("edenSpaceMax", edenSpaceMax()); + map.put("edenSpaceUsed", edenSpaceUsed()); + map.put("survivorCommitted", survivorCommitted()); + map.put("survivorInit", survivorInit()); + map.put("survivorMax", survivorMax()); + map.put("survivorUsed", survivorUsed()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index a2c7ef03..abae771a 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -1,60 +1,76 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.ThreadQuota; + import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.ThreadQuota; +public class JVMThread implements ThreadQuota { + + private ThreadMXBean threadMXBean; -public class JVMThread extends BaseQuota implements ThreadQuota{ - - private ThreadMXBean threadMXBean; - - public static final JVMThread instance = new JVMThread(); - - private JVMThread() { - threadMXBean = ManagementFactory.getThreadMXBean(); - } - - @Override - public int daemonThreadCount() { - return threadMXBean.getDaemonThreadCount(); - } - - @Override - public int threadCount() { - return threadMXBean.getThreadCount(); - } - - @Override - public long totalStartedThreadCount() { - return threadMXBean.getTotalStartedThreadCount(); - } - - @Override - public int deadLockedThreadCount() { - try { + public static final JVMThread I = new JVMThread(); + + private JVMThread() { + threadMXBean = ManagementFactory.getThreadMXBean(); + } + + @Override + public int daemonThreadCount() { + return threadMXBean.getDaemonThreadCount(); + } + + @Override + public int threadCount() { + return threadMXBean.getThreadCount(); + } + + @Override + public long totalStartedThreadCount() { + return threadMXBean.getTotalStartedThreadCount(); + } + + @Override + public int deadLockedThreadCount() { + try { long[] deadLockedThreadIds = threadMXBean.findDeadlockedThreads(); if (deadLockedThreadIds == null) { return 0; } return deadLockedThreadIds.length; } catch (Exception e) { - return 0; + return 0; } - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("daemonThreadCount", daemonThreadCount()); - map.put("threadCount", threadCount()); - map.put("totalStartedThreadCount", totalStartedThreadCount()); - map.put("deadLockedThreadCount", deadLockedThreadCount()); - return map; - } - - + } + + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("daemonThreadCount", daemonThreadCount()); + map.put("threadCount", threadCount()); + map.put("totalStartedThreadCount", totalStartedThreadCount()); + map.put("deadLockedThreadCount", deadLockedThreadCount()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index c9c77e7f..52bcd554 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -1,44 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; -import java.util.Iterator; +import com.mpush.monitor.quota.ThreadPoolQuota; +import com.mpush.tools.thread.pool.ThreadPoolManager; + +import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.ThreadPoolQuota; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +public class JVMThreadPool implements ThreadPoolQuota { + public static final JVMThreadPool I = new JVMThreadPool(); -public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota{ + private JVMThreadPool() { + } - public static final JVMThreadPool instance = new JVMThreadPool(); - - private JVMThreadPool() { - } - - private Map pool = null; - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - if(pool == null){ - pool = ThreadPoolManager.getPool(); - } - - Iterator> ite = pool.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - String serviceUniqName = entry.getKey(); - ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue(); - StringBuilder sb = new StringBuilder(); - sb.append("coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:" - + executor.getActiveCount()); - map.put(serviceUniqName, sb.toString()); - } - return map; - } - - + @Override + public Object monitor(Object... args) { + Map map = new HashMap<>(); + Map pool = ThreadPoolManager.I.getActivePools(); + for (Map.Entry entry : pool.entrySet()) { + String serviceName = entry.getKey(); + ThreadPoolExecutor executor = (ThreadPoolExecutor) entry.getValue(); + map.put(serviceName, ThreadPoolManager.getPoolInfo(executor)); + } + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java deleted file mode 100644 index f299ed5c..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.mpush.monitor.service; - -import com.mpush.tools.JVMUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.monitor.domain.MonitorData; -import com.mpush.monitor.quota.impl.JVMGC; -import com.mpush.monitor.quota.impl.JVMInfo; -import com.mpush.monitor.quota.impl.JVMMemory; -import com.mpush.monitor.quota.impl.JVMThread; -import com.mpush.monitor.quota.impl.JVMThreadPool; -import com.mpush.tools.Jsons; - -public class MonitorDataCollector { - - private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); - - private static volatile boolean dumpFirstJstack = false; - - private static volatile boolean dumpSecondJstack = false; - - private static volatile boolean dumpThirdJstack = false; - - private static volatile boolean dumpJmap = false; - - private static String currentPath = "/tmp/logs/mpush/"; - - private static int firstJstack = 2; - - private static int secondJstack = 4; - - private static int thirdJstack = 6; - - private static int firstJmap = 4; - - public static MonitorData collect() { - MonitorData data = new MonitorData(); - data.setInfoMap(JVMInfo.instance.toMap()); - data.setGcMap(JVMGC.instance.toMap()); - data.setMemoryMap(JVMMemory.instance.toMap()); - data.setThreadMap(JVMThread.instance.toMap()); - data.setThreadPoolMap(JVMThreadPool.instance.toMap()); - return data; - } - - public void setPath(String path) { - currentPath = path; - } - - public static void start(final boolean skipdump) { - new Thread(new Runnable() { - - @Override - public void run() { - while (true) { - MonitorData monitorData = MonitorDataCollector.collect(); - log.error("monitor data:" + Jsons.toJson(monitorData)); - - if(!skipdump){ - double load = JVMInfo.instance.load(); - if (load > firstJstack) { - if (!dumpFirstJstack) { - dumpFirstJstack = true; - JVMUtil.dumpJstack(currentPath); - } - } - - if (load > secondJstack) { - if (!dumpSecondJstack) { - dumpSecondJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if (load > thirdJstack) { - if (!dumpThirdJstack) { - dumpThirdJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if (load > firstJmap) { - if (!dumpJmap) { - dumpJmap = true; - JVMUtil.dumpJmap(currentPath); - } - } - } - - - try {//30s - Thread.sleep(30000L); - } catch (InterruptedException e) { - log.warn("monitor data exception", e); - } - } - } - }).start(); - } - - public static void main(String[] args) { - MonitorDataCollector.start(true); - } - -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java new file mode 100644 index 00000000..3ce12611 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -0,0 +1,117 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.service; + +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.monitor.data.MonitorResult; +import com.mpush.monitor.data.ResultCollector; +import com.mpush.monitor.quota.impl.JVMInfo; +import com.mpush.tools.common.JVMUtil; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.TimeUnit; + +public class MonitorService extends BaseService implements Runnable { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + public static final MonitorService I = new MonitorService(); + + private static final int firstJstack = 2, secondJstack = 4, thirdJstack = 6, firstJmap = 4; + + private static final String dumpLogDir = CC.mp.monitor.dump_dir; + private static final boolean dumpEnabled = CC.mp.monitor.dump_stack; + private static final boolean printLog = CC.mp.monitor.print_log; + private static final long dumpPeriod = CC.mp.monitor.dump_period.getSeconds(); + + private boolean dumpFirstJstack = false; + private boolean dumpSecondJstack = false; + private boolean dumpThirdJstack = false; + private boolean dumpJmap = false; + + private final ResultCollector collector = new ResultCollector(); + + @Override + public void run() { + while (isRunning()) { + MonitorResult result = collector.collect(); + + if (printLog) { + Logs.Monitor.info(result.toJson()); + } + + if (dumpEnabled) { + dump(); + } + + try { + TimeUnit.SECONDS.sleep(dumpPeriod); + } catch (InterruptedException e) { + stop(); + } + } + } + + @Override + protected void doStart(Listener listener) throws Throwable { + if (printLog || dumpEnabled) { + Thread thread = new Thread(this, "mp-t-monitor"); + thread.start(); + } + } + + @Override + protected void doStop(Listener listener) throws Throwable { + logger.error("monitor service stopped!"); + } + + private void dump() { + double load = JVMInfo.I.load(); + if (load > firstJstack) { + if (!dumpFirstJstack) { + dumpFirstJstack = true; + JVMUtil.dumpJstack(dumpLogDir); + } + } + + if (load > secondJstack) { + if (!dumpSecondJstack) { + dumpSecondJstack = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + + if (load > thirdJstack) { + if (!dumpThirdJstack) { + dumpThirdJstack = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + + if (load > firstJmap) { + if (!dumpJmap) { + dumpJmap = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + } +} diff --git a/mpush-monitor/src/test/java/com/mpush/AppTest.java b/mpush-monitor/src/test/java/com/mpush/AppTest.java index 796b8466..6c96a1d2 100644 --- a/mpush-monitor/src/test/java/com/mpush/AppTest.java +++ b/mpush-monitor/src/test/java/com/mpush/AppTest.java @@ -7,32 +7,28 @@ /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ +public class AppTest + extends TestCase { /** * Create the test case * * @param testName name of the test case */ - public AppTest( String testName ) - { - super( testName ); + public AppTest(String testName) { + super(testName); } /** * @return the suite of tests being tested */ - public static Test suite() - { - return new TestSuite( AppTest.class ); + public static Test suite() { + return new TestSuite(AppTest.class); } /** * Rigourous Test :-) */ - public void testApp() - { - assertTrue( true ); + public void testApp() { + assertTrue(true); } } diff --git a/mpush-netty/.gitignore b/mpush-netty/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-netty/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index b0bcbe3f..4fb8bc19 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -18,7 +18,11 @@ ${mpush.groupId} - mpush-common + mpush-api + + + ${mpush.groupId} + mpush-tools io.netty @@ -37,5 +41,9 @@ io.netty netty-codec-http + + io.netty + netty-handler + diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java deleted file mode 100644 index 5aa506c2..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.netty.client; - -import com.mpush.api.Client; - -import io.netty.channel.ChannelHandler; - -public interface ChannelClientHandler extends ChannelHandler{ - - public Client getClient(); - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java deleted file mode 100644 index 43b27000..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.mpush.netty.client; - - -import io.netty.handler.codec.http.HttpResponse; - -/** - * Created by ohun on 2016/2/15. - * - * @author ohun@live.cn - */ -public interface HttpCallback { - - void onResponse(HttpResponse response); - - void onFailure(int statusCode, String reasonPhrase); - - void onException(Throwable throwable); - - void onTimeout(); - - boolean onRedirect(HttpResponse response); -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java deleted file mode 100644 index 014e0ba0..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.mpush.netty.client; - -/** - * Created by ohun on 2016/2/15. - * - * @author ohun@live.cn - */ -public interface HttpClient { - - void start(); - - void stop(); - - void request(RequestInfo requestInfo) throws Exception; -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 0a214a21..b3efb889 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -1,154 +1,135 @@ -package com.mpush.netty.client; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.concurrent.TimeUnit; +package com.mpush.netty.client; -import com.mpush.netty.util.NettySharedHolder; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Client; +import com.mpush.api.service.Listener; +import com.mpush.api.service.ServiceException; +import com.mpush.netty.codec.PacketDecoder; +import com.mpush.netty.codec.PacketEncoder; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; - -import org.apache.commons.lang3.StringUtils; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.api.Client; -import com.mpush.api.connection.Connection; -import com.mpush.api.protocol.Packet; -import com.mpush.tools.config.ConfigCenter; +import java.net.InetSocketAddress; -public class NettyClient implements Client { - - private static final String format = "%s:%s"; - - private static final Logger log = LoggerFactory.getLogger(NettyClient.class); +public abstract class NettyClient extends BaseService implements Client { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); private final String host; private final int port; - private Channel channel; - private int hbTimes; - private Connection connection; - - public NettyClient(final String host, final int port) { + private EventLoopGroup workerGroup; + + public NettyClient(String host, int port) { this.host = host; this.port = port; } - + + @Override + public void start(final Listener listener) { + if (started.compareAndSet(false, true)) { + Bootstrap bootstrap = new Bootstrap(); + workerGroup = new NioEventLoopGroup(); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + initPipeline(ch.pipeline()); + } + }); + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + if (listener != null) listener.onSuccess(port); + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + if (listener != null) listener.onFailure(future.cause()); + LOGGER.error("start netty client failure, host={}, port={}", host, port, future.cause()); + } + } + }); + } else { + listener.onFailure(new ServiceException("client has started!")); + } + } + + protected void initPipeline(ChannelPipeline pipeline) { + pipeline.addLast("decoder", getDecoder()); + pipeline.addLast("encoder", getEncoder()); + pipeline.addLast("handler", getChannelHandler()); + } + + protected ChannelHandler getDecoder() { + return new PacketDecoder(); + } + + protected ChannelHandler getEncoder() { + return PacketEncoder.INSTANCE; + } + + + public abstract ChannelHandler getChannelHandler(); + + @Override + protected void doStart(Listener listener) throws Throwable { + + } + @Override - public Channel getChannel() { - return channel; - } + protected void doStop(Listener listener) throws Throwable { + if (workerGroup != null) { + workerGroup.shutdownGracefully(); + } + LOGGER.error("netty client [{}] stopped.", this.getClass().getSimpleName()); + } - public void setChannel(Channel channel) { - this.channel = channel; - } + public String getHost() { + return host; + } - @Override - public void initConnection(Connection connection){ - this.connection = connection; + public int getPort() { + return port; } - + @Override - public void close(String cause) { - if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { - log.error("close channel:"+cause); - } - if(this.channel!=null){ - this.channel.close(); - } - - } - - @Override - public boolean isEnabled() { - return channel.isWritable(); - } - - @Override - public boolean isConnected() { - return channel.isActive(); - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } - - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void startHeartBeat() throws Exception { - startHeartBeat((int)ConfigCenter.I.scanConnTaskCycle()/1000); - } - - - @Override - public String getUrl() { - return String.format(format, host, port); - } - - @Override - public String getHost() { - return host; - } - - @Override - public int getPort() { - return port; - } - - @Override - public void stop(){ - this.close("stop"); - } - - @Override - public Connection getConnection() { - return connection; - } - - @Override - public void init(Channel channel) { - this.channel = channel; - } - - @Override - public String toString() { - return "NettyClient [host=" + host + ", port=" + port + ", channel=" + channel + ", hbTimes=" + hbTimes + ", connection=" + connection + "]"; - } - - @Override - public void startHeartBeat(final int heartbeat) throws Exception { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); - } - log.warn("client send msg hb false:" + channel.remoteAddress().toString()); - } else { - log.debug("client send msg hb success:" + channel.remoteAddress().toString()); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); - } - } - } - }, heartbeat, TimeUnit.MILLISECONDS); - - } - - + public String toString() { + return "NettyClient{" + + "host='" + host + '\'' + + ", port=" + port + + ", name=" + this.getClass().getSimpleName() + + '}'; + } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java deleted file mode 100644 index d3cce35e..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.mpush.netty.client; - -import java.net.InetSocketAddress; -import java.util.Map; - -import com.mpush.netty.codec.PacketDecoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; - -import com.google.common.collect.Maps; -import com.mpush.api.Client; -import com.mpush.netty.codec.PacketEncoder; -import com.mpush.netty.util.NettySharedHolder; - -public class NettyClientFactory { - - private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); - - public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - - private final Map channel2Client = Maps.newConcurrentMap(); - - public Client create(final ChannelClientHandler handler){ - final Bootstrap bootstrap = new Bootstrap(); - bootstrap.group(NettySharedHolder.workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(handler); - } - }); - - Client client = handler.getClient(); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - log.error("init channel:"+channel); - return client; - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + client); - return null; - } - } - - public Client getCientByChannel(final Channel channel) { - return channel2Client.get(channel); - } - - public void remove(final Channel channel) { - channel2Client.remove(channel); - } - - public void put(Channel channel,final Client client){ - channel2Client.put(channel, client); - } - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java deleted file mode 100644 index d7677c43..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java +++ /dev/null @@ -1,262 +0,0 @@ -package com.mpush.netty.client; - -import com.google.common.collect.ArrayListMultimap; -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; -import com.mpush.tools.config.ConfigCenter; - -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.http.*; -import io.netty.util.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.URI; -import java.net.URL; -import java.net.URLDecoder; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * Created by ohun on 2016/2/15. - * - * @author ohun@live.cn - */ -public class NettyHttpClient implements HttpClient { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); - - private final int maxConnPerHost = ConfigCenter.I.maxHttpConnCountPerHost(); - private final AttributeKey requestKey = AttributeKey.newInstance("request"); - private final AttributeKey hostKey = AttributeKey.newInstance("host"); - private final ArrayListMultimap channelPool = ArrayListMultimap.create(); - - private Bootstrap b; - private EventLoopGroup workerGroup; - private Timer timer; - - @Override - public void start() { - workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.httpExecutor); - b = new Bootstrap(); - b.group(workerGroup); - b.channel(NioSocketChannel.class); - b.option(ChannelOption.SO_KEEPALIVE, true); - b.option(ChannelOption.TCP_NODELAY, true); - b.option(ChannelOption.SO_REUSEADDR, true); - b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast("decoder", new HttpResponseDecoder()); - ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 5));//5M - ch.pipeline().addLast("encoder", new HttpRequestEncoder()); - ch.pipeline().addLast("handler", new HttpClientHandler()); - } - }); - timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), - 1, TimeUnit.SECONDS, 64); - } - - @Override - public void stop() { - for (Channel channel : channelPool.values()) { - channel.close(); - } - channelPool.clear(); - workerGroup.shutdownGracefully(); - timer.stop(); - } - - @Override - public void request(final RequestInfo info) throws Exception { - URI uri = new URI(info.request.uri()); - String host = info.host = uri.getHost(); - int port = uri.getPort() == -1 ? 80 : uri.getPort(); - info.request.headers().set(HttpHeaderNames.HOST, host); - info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); - timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); - Channel channel = tryAcquire(host); - if (channel == null) { - final long startConnectChannel = System.currentTimeMillis(); - LOGGER.debug("create new channel, host={}", host); - ChannelFuture f = b.connect(host, port); - f.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - LOGGER.debug("create new channel cost:" + (System.currentTimeMillis() - startConnectChannel)); - if (future.isSuccess()) { - writeRequest(future.channel(), info); - } else { - info.tryDone(); - info.onFailure(504, "Gateway Timeout"); - LOGGER.debug("request failure request={}", info); - } - } - }); - } else { - writeRequest(channel, info); - } - } - - private synchronized Channel tryAcquire(String host) { - List channels = channelPool.get(host); - if (channels == null || channels.isEmpty()) return null; - Iterator it = channels.iterator(); - while (it.hasNext()) { - Channel channel = it.next(); - it.remove(); - if (channel.isActive()) { - LOGGER.debug("tryAcquire channel success, host={}", host); - channel.attr(hostKey).set(host); - return channel; - } else {//链接由于意外情况不可用了,keepAlive_timeout - LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); - } - } - return null; - } - - private synchronized void tryRelease(Channel channel) { - String host = channel.attr(hostKey).getAndRemove(); - List channels = channelPool.get(host); - if (channels == null || channels.size() < maxConnPerHost) { - LOGGER.debug("tryRelease channel success, host={}", host); - channelPool.put(host, channel); - } else { - LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); - channel.close(); - } - } - - private void writeRequest(Channel channel, RequestInfo info) { - channel.attr(requestKey).set(info); - channel.attr(hostKey).set(info.host); - channel.writeAndFlush(info.request).addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - RequestInfo info = future.channel().attr(requestKey).getAndRemove(); - info.tryDone(); - info.onFailure(503, "Service Unavailable"); - LOGGER.debug("request failure request={}", info); - tryRelease(future.channel()); - } - } - }); - } - - @ChannelHandler.Sharable - private class HttpClientHandler extends ChannelHandlerAdapter { - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - try { - if (info != null && info.tryDone()) { - info.onException(cause); - } - } finally { - tryRelease(ctx.channel()); - } - LOGGER.error("http client caught an error, info={}", info, cause); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - try { - if (info != null && info.tryDone()) { - HttpResponse response = (HttpResponse) msg; - if (isRedirect(response)) { - if (info.onRedirect(response)) { - String location = getRedirectLocation(info.request, response); - if (location != null && location.length() > 0) { - info.cancelled.set(false); - info.request = info.request.copy().setUri(location); - request(info); - return; - } - } - } - info.onResponse(response); - LOGGER.debug("request done request={}", info); - } - } finally { - tryRelease(ctx.channel()); - ReferenceCountUtil.release(msg); - } - } - - private boolean isRedirect(HttpResponse response) { - HttpResponseStatus status = response.status(); - switch (status.code()) { - case 300: - case 301: - case 302: - case 303: - case 305: - case 307: - return true; - default: - return false; - } - } - - private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { - String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); - if (hdr != null) { - if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { - return hdr; - } else { - URL orig = new URL(request.uri()); - String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); - if (hdr.startsWith("/")) { - pth = hdr; - } else if (pth.endsWith("/")) { - pth += hdr; - } else { - pth += "/" + hdr; - } - StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); - sb.append("://").append(orig.getHost()); - if (orig.getPort() > 0) { - sb.append(":").append(orig.getPort()); - } - if (pth.charAt(0) != '/') { - sb.append('/'); - } - sb.append(pth); - return sb.toString(); - } - } - return null; - } - - private HttpRequest copy(String uri, HttpRequest request) { - HttpRequest nue = request; - if (request instanceof DefaultFullHttpRequest) { - DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; - FullHttpRequest rq; - try { - rq = dfrq.copy(); - } catch (IllegalReferenceCountException e) { // Empty bytebuf - rq = dfrq; - } - rq.setUri(uri); - } else { - DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); - dfr.headers().set(request.headers()); - nue = dfr; - } - return nue; - } - } -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java deleted file mode 100644 index 6e55531b..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.mpush.netty.client; - - - -public class SecurityNettyClient extends NettyClient { - - private byte[] clientKey; - private byte[] iv; - private String clientVersion; - private String deviceId; - private String osName; - private String osVersion; - - private String userId; - - private String cipher; //快速重连的时候使用 - - public SecurityNettyClient(String host, int port) { - super(host, port); - } - - public byte[] getClientKey() { - return clientKey; - } - - public void setClientKey(byte[] clientKey) { - this.clientKey = clientKey; - } - - public byte[] getIv() { - return iv; - } - - public void setIv(byte[] iv) { - this.iv = iv; - } - - public String getClientVersion() { - return clientVersion; - } - - public void setClientVersion(String clientVersion) { - this.clientVersion = clientVersion; - } - - public String getDeviceId() { - return deviceId; - } - - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; - } - - public String getOsName() { - return osName; - } - - public void setOsName(String osName) { - this.osName = osName; - } - - public String getOsVersion() { - return osVersion; - } - - public void setOsVersion(String osVersion) { - this.osVersion = osVersion; - } - - public String getCipher() { - return cipher; - } - - public void setCipher(String cipher) { - this.cipher = cipher; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java b/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java new file mode 100644 index 00000000..ec646692 --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.codec; + +/** + * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn + */ +public class DecodeException extends RuntimeException { + public DecodeException(String message) { + super(message); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 764bf760..158678e9 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -1,9 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.codec; -import com.mpush.api.exception.DecodeException; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -17,6 +35,7 @@ * @author ohun@live.cn */ public final class PacketDecoder extends ByteToMessageDecoder { + private static final int maxPacketSize = CC.mp.core.max_packet_size; @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { @@ -27,7 +46,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET_BYTE) { - out.add(new Packet(Command.HEARTBEAT)); + out.add(Packet.HB_PACKE); } else { in.readerIndex(in.readerIndex() - 1); break; @@ -65,7 +84,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { - if (bodyLength > ConfigCenter.I.maxPacketSize()) { + if (bodyLength > maxPacketSize) { throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); } body = new byte[bodyLength]; diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index 57a887a7..ae6bfed3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.codec; import com.mpush.api.protocol.Command; diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 0b1a12e2..a963c00c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -1,9 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.connection; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.common.security.CipherBox; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.core.CipherFactory; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -17,23 +37,21 @@ */ public final class NettyConnection implements Connection, ChannelFutureListener { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); - + private static final CipherFactory factory = SpiLoader.load(CipherFactory.class); private SessionContext context; private Channel channel; private volatile int status = STATUS_NEW; private long lastReadTime; private long lastWriteTime; - private int hbTimes = 0; - @Override public void init(Channel channel, boolean security) { this.channel = channel; this.context = new SessionContext(); this.lastReadTime = System.currentTimeMillis(); this.status = STATUS_CONNECTED; - if (security) { - this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); + if (security && factory != null) { + this.context.changeCipher(factory.get()); } } @@ -49,7 +67,7 @@ public SessionContext getSessionContext() { @Override public String getId() { - return channel.id().asLongText(); + return channel.id().asShortText(); } @Override @@ -67,7 +85,6 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { } } else { return this.close(); - } } @@ -104,7 +121,7 @@ public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { lastWriteTime = System.currentTimeMillis(); } else { - LOGGER.error("send msg error"); + LOGGER.error("connection send msg error", future.cause()); } } @@ -112,19 +129,14 @@ public void updateLastWriteTime() { lastWriteTime = System.currentTimeMillis(); } - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } @Override public String toString() { - return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes + return "NettyConnection [context=" + context + + ", channel=" + channel + + ", status=" + status + + ", lastReadTime=" + lastReadTime + + ", lastWriteTime=" + lastWriteTime + "]"; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java new file mode 100644 index 00000000..a628253a --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java @@ -0,0 +1,41 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; + + +import io.netty.handler.codec.http.HttpResponse; + +/** + * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn + */ +public interface HttpCallback { + + void onResponse(HttpResponse response); + + void onFailure(int statusCode, String reasonPhrase); + + void onException(Throwable throwable); + + void onTimeout(); + + boolean onRedirect(HttpResponse response); +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java new file mode 100644 index 00000000..06305e55 --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; + +import com.mpush.api.service.Service; + +/** + * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn + */ +public interface HttpClient extends Service { + void request(RequestContext context) throws Exception; +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java new file mode 100644 index 00000000..26f6a31e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java @@ -0,0 +1,126 @@ +package com.mpush.netty.http; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; +import io.netty.util.IllegalReferenceCountException; +import io.netty.util.ReferenceCountUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URL; +import java.net.URLDecoder; + +@ChannelHandler.Sharable +public class HttpClientHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private final NettyHttpClient client; + + public HttpClientHandler(NettyHttpClient client) { + this.client = client; + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); + try { + if (context != null && context.tryDone()) { + context.onException(cause); + } + } finally { + client.pool.tryRelease(ctx.channel()); + } + LOGGER.error("http client caught an error, info={}", context, cause); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); + try { + if (context != null && context.tryDone()) { + HttpResponse response = (HttpResponse) msg; + if (isRedirect(response)) { + if (context.onRedirect(response)) { + String location = getRedirectLocation(context.request, response); + if (location != null && location.length() > 0) { + context.cancelled.set(false); + context.request = context.request.copy().setUri(location); + client.request(context); + return; + } + } + } + context.onResponse(response); + LOGGER.debug("request done request={}", context); + } + } finally { + client.pool.tryRelease(ctx.channel()); + ReferenceCountUtil.release(msg); + } + } + + private boolean isRedirect(HttpResponse response) { + HttpResponseStatus status = response.status(); + switch (status.code()) { + case 300: + case 301: + case 302: + case 303: + case 305: + case 307: + return true; + default: + return false; + } + } + + private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { + String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); + if (hdr != null) { + if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { + return hdr; + } else { + URL orig = new URL(request.uri()); + String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); + if (hdr.startsWith("/")) { + pth = hdr; + } else if (pth.endsWith("/")) { + pth += hdr; + } else { + pth += "/" + hdr; + } + StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); + sb.append("://").append(orig.getHost()); + if (orig.getPort() > 0) { + sb.append(":").append(orig.getPort()); + } + if (pth.charAt(0) != '/') { + sb.append('/'); + } + sb.append(pth); + return sb.toString(); + } + } + return null; + } + + private HttpRequest copy(String uri, HttpRequest request) { + HttpRequest nue = request; + if (request instanceof DefaultFullHttpRequest) { + DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; + FullHttpRequest rq; + try { + rq = dfrq.copy(); + } catch (IllegalReferenceCountException e) { // Empty bytebuf + rq = dfrq; + } + rq.setUri(uri); + } else { + DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); + dfr.headers().set(request.headers()); + nue = dfr; + } + return nue; + } +} \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java new file mode 100644 index 00000000..ce55224e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java @@ -0,0 +1,85 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.netty.http; + +import com.google.common.collect.ArrayListMultimap; +import com.mpush.tools.config.CC; +import io.netty.channel.Channel; +import io.netty.util.AttributeKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by yxx on 2016/5/28. + * + * @author ohun@live.cn (夜色) + */ +public class HttpConnectionPool { + private static final int maxConnPerHost = CC.mp.http.max_conn_per_host; + + private final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPool.class); + + private final AttributeKey hostKey = AttributeKey.newInstance("host"); + + private final ArrayListMultimap channelPool = ArrayListMultimap.create(); + + public synchronized Channel tryAcquire(String host) { + List channels = channelPool.get(host); + if (channels == null || channels.isEmpty()) return null; + Iterator it = channels.iterator(); + while (it.hasNext()) { + Channel channel = it.next(); + it.remove(); + if (channel.isActive()) { + LOGGER.debug("tryAcquire channel success, host={}", host); + channel.attr(hostKey).set(host); + return channel; + } else {//链接由于意外情况不可用了,keepAlive_timeout + LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); + } + } + return null; + } + + public synchronized void tryRelease(Channel channel) { + String host = channel.attr(hostKey).getAndRemove(); + List channels = channelPool.get(host); + if (channels == null || channels.size() < maxConnPerHost) { + LOGGER.debug("tryRelease channel success, host={}", host); + channelPool.put(host, channel); + } else { + LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); + channel.close(); + } + } + + public void attachHost(String host, Channel channel) { + channel.attr(hostKey).set(host); + } + + public void close() { + for (Channel channel : channelPool.values()) { + channel.close(); + } + channelPool.clear(); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java new file mode 100644 index 00000000..9af0db7e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -0,0 +1,150 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; + +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestEncoder; +import io.netty.handler.codec.http.HttpResponseDecoder; +import io.netty.util.AttributeKey; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.util.concurrent.TimeUnit; + +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; +import static io.netty.handler.codec.http.HttpHeaderNames.HOST; +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; + +/** + * Netty的一个Bootstrap是可以关联多个channel的, + * 本Client采用的就是这种模式,在种模式下如果Handler添加了@ChannelHandler.Sharable + * 注解的话,要特殊处理,因为这时的client和handler是被所有请求共享的。 + *

+ * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn + */ +public class NettyHttpClient extends BaseService implements HttpClient { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private static final int maxContentLength = (int) CC.mp.http.max_content_length; + /*package*/ final AttributeKey requestKey = AttributeKey.newInstance("request"); + /*package*/ final HttpConnectionPool pool = new HttpConnectionPool(); + private Bootstrap b; + private EventLoopGroup workerGroup; + private Timer timer; + + @Override + public void request(RequestContext context) throws Exception { + URI uri = new URI(context.request.uri()); + String host = context.host = uri.getHost(); + int port = uri.getPort() == -1 ? 80 : uri.getPort(); + //1.设置请求头 + context.request.headers().set(HOST, host);//映射后的host + context.request.headers().set(CONNECTION, KEEP_ALIVE);//保存长链接 + + //2.添加请求超时检测队列 + timer.newTimeout(context, context.readTimeout, TimeUnit.MILLISECONDS); + + //3.先尝试从连接池里取可用链接,去取不到就创建新链接。 + Channel channel = pool.tryAcquire(host); + if (channel == null) { + final long startCreate = System.currentTimeMillis(); + LOGGER.debug("create new channel, host={}", host); + ChannelFuture f = b.connect(host, port); + f.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); + if (future.isSuccess()) {//3.1.把请求写到http server + writeRequest(future.channel(), context); + } else {//3.2如果链接创建失败,直接返回客户端网关超时 + context.tryDone(); + context.onFailure(504, "Gateway Timeout"); + LOGGER.warn("create new channel failure, request={}", context); + } + } + }); + } else { + //3.1.把请求写到http server + writeRequest(channel, context); + } + } + + private void writeRequest(Channel channel, RequestContext context) { + channel.attr(requestKey).set(context); + pool.attachHost(context.host, channel); + channel.writeAndFlush(context.request).addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + RequestContext info = future.channel().attr(requestKey).getAndRemove(); + info.tryDone(); + info.onFailure(503, "Service Unavailable"); + LOGGER.debug("request failure request={}", info); + pool.tryRelease(future.channel()); + } + } + }); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); + b = new Bootstrap(); + b.group(workerGroup); + b.channel(NioSocketChannel.class); + b.option(ChannelOption.SO_KEEPALIVE, true); + b.option(ChannelOption.TCP_NODELAY, true); + b.option(ChannelOption.SO_REUSEADDR, true); + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast("decoder", new HttpResponseDecoder()); + ch.pipeline().addLast("aggregator", new HttpObjectAggregator(maxContentLength)); + ch.pipeline().addLast("encoder", new HttpRequestEncoder()); + ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this)); + } + }); + timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), 1, TimeUnit.SECONDS, 64); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + pool.close(); + workerGroup.shutdownGracefully(); + timer.stop(); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java similarity index 66% rename from mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java rename to mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java index 28c61ee4..915a09b3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java @@ -1,8 +1,27 @@ -package com.mpush.netty.client; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; import com.google.common.primitives.Ints; import com.mpush.api.Constants; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Timeout; @@ -10,37 +29,39 @@ import java.util.concurrent.atomic.AtomicBoolean; -public class RequestInfo implements TimerTask, HttpCallback { - private static final int TIMEOUT = ConfigCenter.I.httpDefaultReadTimeout(); +public class RequestContext implements TimerTask, HttpCallback { + private static final int TIMEOUT = CC.mp.http.default_read_timeout; final AtomicBoolean cancelled = new AtomicBoolean(false); final long startTime = System.currentTimeMillis(); + final int readTimeout; long endTime = startTime; - int readTimeout = TIMEOUT; private String uri; private HttpCallback callback; FullHttpRequest request; String host; - public RequestInfo(FullHttpRequest request, HttpCallback callback) { + public RequestContext(FullHttpRequest request, HttpCallback callback) { this.callback = callback; this.request = request; this.uri = request.uri(); + this.readTimeout = parseTimeout(); + } + + private int parseTimeout() { String timeout = request.headers().getAndRemoveAndConvert(Constants.HTTP_HEAD_READ_TIMEOUT); if (timeout != null) { Integer integer = Ints.tryParse(timeout); - if (integer != null && integer > 0) readTimeout = integer; + if (integer != null && integer > 0) { + return integer; + } } + return TIMEOUT; } public int getReadTimeout() { return readTimeout; } - public RequestInfo setReadTimeout(int timeout) { - this.readTimeout = timeout; - return this; - } - @Override public void run(Timeout timeout) throws Exception { if (tryDone()) { @@ -50,6 +71,11 @@ public void run(Timeout timeout) throws Exception { } } + /** + * 由于检测请求超时的任务存在,为了防止多线程下重复处理 + * + * @return + */ public boolean tryDone() { return cancelled.compareAndSet(false, true); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index db065085..0b242517 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -1,9 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.server; -import com.mpush.api.Server; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; +import com.mpush.api.service.ServiceException; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -15,6 +38,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Locale; +import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; /** @@ -22,7 +47,7 @@ * * @author ohun@live.cn */ -public abstract class NettyServer implements Server { +public abstract class NettyServer extends BaseService implements Server { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -54,11 +79,13 @@ public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { IllegalStateException e = new IllegalStateException("server was already shutdown."); if (listener != null) listener.onFailure(e); - throw e; + Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName()); + return; } + Logs.Console.error("try shutdown {}...", this.getClass().getSimpleName()); if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); - logger.error("netty server stop now"); + Logs.Console.error("{} shutdown success.", this.getClass().getSimpleName()); if (listener != null) { listener.onSuccess(port); } @@ -69,7 +96,11 @@ public void start(final Listener listener) { if (!serverState.compareAndSet(State.Initialized, State.Starting)) { throw new IllegalStateException("Server already started or have not init"); } - createNioServer(listener); + if (useNettyEpoll()) { + createEpollServer(listener); + } else { + createNioServer(listener); + } } private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { @@ -130,10 +161,10 @@ public void initChannel(SocketChannel ch) throws Exception { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - logger.error("server start success on:" + port); + Logs.Console.error("server start success on:{}", port); if (listener != null) listener.onSuccess(port); } else { - logger.error("server start failure on:" + port, future.cause()); + Logs.Console.error("server start failure on:{}", port, future.cause()); if (listener != null) listener.onFailure(future.cause()); } } @@ -149,7 +180,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } catch (Exception e) { logger.error("server start exception", e); if (listener != null) listener.onFailure(e); - throw new RuntimeException("server start exception, port=" + port, e); + throw new ServiceException("server start exception, port=" + port, e); } finally { /*** * 优雅关闭 @@ -158,17 +189,16 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolManager.bossExecutor); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); + private void createNioServer(Listener listener) { + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, getBossExecutor()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, getWorkExecutor()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } - @SuppressWarnings("unused") - private void createEpollServer(final Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); + private void createEpollServer(Listener listener) { + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, getBossExecutor()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, getWorkExecutor()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -208,4 +238,28 @@ protected void initPipeline(ChannelPipeline pipeline) { pipeline.addLast("encoder", getEncoder()); pipeline.addLast("handler", getChannelHandler()); } + + protected Executor getBossExecutor() { + return null; + } + + protected Executor getWorkExecutor() { + return null; + } + + private boolean useNettyEpoll() { + if (!"netty".equals(CC.mp.core.epoll_provider)) return false; + String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); + return name.startsWith("linux"); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + + } + + @Override + protected void doStop(Listener listener) throws Throwable { + + } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java deleted file mode 100644 index 34874d67..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.mpush.netty.util; - - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.ThreadNameSpace; - -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; -import io.netty.util.Timer; - -public class NettySharedHolder { - - public static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); - - public static EventLoopGroup workerGroup = new NioEventLoopGroup(); - -} diff --git a/mpush-netty/src/test/resources/logback.xml b/mpush-netty/src/test/resources/logback.xml index e638aca3..8b23ef25 100644 --- a/mpush-netty/src/test/resources/logback.xml +++ b/mpush-netty/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN + + System.out + UTF-8 + + INFO - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-test/.gitignore b/mpush-test/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 2c830035..7a1312d7 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -34,20 +34,10 @@ ${mpush.groupId} mpush-boot - - ${mpush.groupId} - mpush-log - ${mpush.groupId} mpush-client - - - args4j - args4j - - diff --git a/mpush-test/src/test/java/com/mpush/AppTest.java b/mpush-test/src/test/java/com/mpush/AppTest.java deleted file mode 100644 index 796b8466..00000000 --- a/mpush-test/src/test/java/com/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java new file mode 100644 index 00000000..c8913e04 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -0,0 +1,70 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain { + + public static void main(String[] args) throws Exception { + Logs.init(); + ConnectClientBoot main = new ConnectClientBoot(); + main.run(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); + for (int i = 0; i < 1; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "user-" + i; + String deviceId = "test-device-id-" + i; + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] iv = CipherBox.I.randomAESIV(); + + ClientConfig config = new ClientConfig(); + config.setClientKey(clientKey); + config.setIv(iv); + config.setClientVersion(clientVersion); + config.setDeviceId(deviceId); + config.setOsName(osName); + config.setOsVersion(osVersion); + config.setUserId(userId); + Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + } + + LockSupport.park(); + + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java new file mode 100644 index 00000000..5abd1cc5 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java @@ -0,0 +1,71 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain2 { + + public static void main(String[] args) throws Exception { + Logs.init(); + ConnectClientBoot main = new ConnectClientBoot(); + main.run(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); + + ClientConfig config = new ClientConfig(); + config.setClientKey(CipherBox.I.randomAESKey()); + config.setIv(CipherBox.I.randomAESIV()); + config.setClientVersion("1.0.0"); + config.setDeviceId("android-device-id-1"); + config.setOsName("android"); + config.setOsVersion("1.0.1"); + config.setUserId("user-0"); + Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + + config = new ClientConfig(); + config.setClientKey(CipherBox.I.randomAESKey()); + config.setIv(CipherBox.I.randomAESIV()); + config.setClientVersion("1.0.0"); + config.setDeviceId("pc-device-id-2"); + config.setOsName("pc"); + config.setOsVersion("1.0.1"); + config.setUserId("user-0"); + client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java new file mode 100644 index 00000000..940dd8c3 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.google.common.collect.Lists; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public class ConnectClientBoot { + private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); + + public void run() { + ZKClient.I.start(); + RedisManager.I.init(); + listener.beginWatch(); + } + + public List getServers() { + return Lists.newArrayList(listener.getCache().values()); + } +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 61ae1c36..97bedcc4 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -1,29 +1,111 @@ -package com.mpush.test.configcenter; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.List; -import java.util.Map; +package com.mpush.test.configcenter; +import com.mpush.tools.config.CC; +import com.typesafe.config.ConfigObject; +import com.typesafe.config.ConfigValue; +import org.junit.Before; import org.junit.Test; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.DnsMapping; +import java.util.*; public class ConfigCenterTest { - - @Test - public void test(){ - - System.out.println(ConfigCenter.I.forceWriteRedisGroupInfo()); - - } - - @Test - public void testDnsMapping(){ - Map> ret = ConfigCenter.I.dnsMapping(); - - System.out.println(Jsons.toJson(ret)); - - } + @Before + public void setUp() throws Exception { + // ConfigManager.I.getLocalIp(); + } + + @Test + public void testKey() { + //String t = ConfigKey.app_env.getString(); + System.out.println(CC.mp.security.aes_key_length); + System.out.println(CC.mp.redis.cluster_group); + } + + @Test + public void testLoad() { + Map map = new HashMap<>(); + CC.cfg.entrySet().forEach(e -> print(e.getKey(), e.getValue(), map)); + List list = new ArrayList<>(map.values()); + Collections.sort(list); + list.forEach(s -> System.out.println(s.substring(s.indexOf(".") + 1) + ",")); + } + + public static void print(String s, ConfigValue configValue, Map map) { + if (s.startsWith("mp") && !s.endsWith("\"")) { + String[] keys = s.split("\\."); + if (keys.length >= 4) return; + for (int i = keys.length - 1; i > 0; i--) { + String key = keys[i]; + String value = map.get(key); + if (value != null) continue; + String p = keys[i - 1]; + map.put(key, p + "." + key.replace('-', '_') + "(" + p + ")"); + } + } + } + + @Test + public void testLoad2() { + Map map = new HashMap<>(); + System.out.println("public interface mp {"); + System.out.printf(" Config cfg = ConfigManager.I.mp().toConfig();%n%n"); + CC.mp.cfg.root().forEach((s, configValue) -> print2(s, configValue, "mp", 1)); + System.out.println("}"); + } + + public static void print2(String s, ConfigValue configValue, String p, int level) { + int l = level + 1; + switch (configValue.valueType()) { + case OBJECT: + printTab(level); + System.out.printf("interface %s {%n", s.replace('-', '_')); + printTab(level); + System.out.printf(" Config cfg = %s.cfg.getObject(\"%s\").toConfig();%n%n", p, s); + ((ConfigObject) configValue).forEach((s1, configValue2) -> print2(s1, configValue2, s, l)); + printTab(level); + System.out.printf("}%n%n"); + break; + case STRING: + printTab(level); + System.out.printf(" String %s = cfg.getString(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case NUMBER: + printTab(level); + System.out.printf(" Number %s = cfg.getNumber(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case BOOLEAN: + printTab(level); + System.out.printf(" Boolean %s = cfg.getBoolean(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case LIST: + printTab(level); + System.out.printf(" List %s = cfg.getList(\"%s\").unwrapped();%n%n", s.replace('-', '_'), s); + break; + } + } + private static void printTab(int l) { + while (l-- > 0) { + System.out.print(" "); + } + } } diff --git a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java index 2a0ed803..b85ca668 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java @@ -1,6 +1,23 @@ -package com.mpush.test.connection.body; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.security.interfaces.RSAPrivateKey; +package com.mpush.test.connection.body; import com.mpush.common.message.HandshakeMessage; import com.mpush.tools.crypto.RSAUtils; @@ -9,66 +26,68 @@ import org.junit.Before; import org.junit.Test; +import java.security.interfaces.RSAPrivateKey; + public class BodyTest { - + private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; private String daily_allocServer = "http://111.1.57.148/mpns/push/server/"; private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; private RSAPrivateKey dailyPrivateKey = null; - + private String pre_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB"; private String pre_allocServer = "http://115.29.230.2/mpns/push/server/"; private String pre_privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA=="; private RSAPrivateKey prePrivateKey = null; - + //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB private String online_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB"; private String online_allocServer = "http://allot.mangguoyisheng.com/"; private String online_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g="; - private RSAPrivateKey onlinePrivateKey = null; + private RSAPrivateKey onlinePrivateKey = null; + + String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; + + byte[] data = null; + + @Before + public void init() { + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); + onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); + String[] temp = rawData.split(","); + data = new byte[temp.length]; + for (int i = 0; i < temp.length; i++) { + data[i] = Byte.parseByte(temp[i].trim()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void dailyDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, dailyPrivateKey); + decode(message); + } + + @Test + public void preDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, prePrivateKey); + decode(message); + } + + @Test + public void onlineDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, onlinePrivateKey); + decode(message); + } - String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; - - byte[]data = null; - - @Before - public void init(){ - try { - dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); - prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); - onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); - String[] temp = rawData.split(","); - data = new byte[temp.length]; - for(int i = 0;i getServers() { - return Lists.newArrayList(listener.getManager().getList()); - } -} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java new file mode 100644 index 00000000..9452bccc --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.connection.mpns; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain { + + public static void main(String[] args) throws InterruptedException { + + ConnectTestClientBoot main = new ConnectTestClientBoot(); + main.start(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + + for (int i = 0; i < 1000; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "uh-" + i; + String deviceId = "test-device-id-" + i; + String cipher = ""; + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] iv = CipherBox.I.randomAESIV(); + + ClientConfig config = new ClientConfig(); + config.setClientKey(clientKey); + config.setIv(iv); + config.setClientVersion(clientVersion); + config.setDeviceId(deviceId); + config.setOsName(osName); + config.setOsVersion(osVersion); + config.setUserId(userId); + config.setCipher(cipher); + Client client = new ConnectClient(server.getIp(), server.getPort(), config); + Thread.sleep(100); + } + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java deleted file mode 100644 index 1126476d..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mpush.test.connection.mpns; - -import com.google.common.collect.Lists; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; -import com.mpush.zk.ZKServerNode; - -import java.util.List; - -public class ConnectTestClient extends AbstractClient { - private final ConnectZKListener listener = new ConnectZKListener(); - - public ConnectTestClient() { - registerListener(listener); - } - - public List getServers() { - return Lists.newArrayList(listener.getManager().getList()); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java new file mode 100644 index 00000000..d927e1c6 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java @@ -0,0 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.connection.mpns; + +import com.google.common.collect.Lists; +import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public class ConnectTestClientBoot { + private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); + + public void start() { + listener.beginWatch(); + } + + public List getServers() { + return Lists.newArrayList(listener.getCache().values()); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java deleted file mode 100644 index 778a4778..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.test.connection.severs; - -/** - * Created by yxx on 2016/5/16. - * - * @author ohun@live.cn - */ -public class Main { - public static void main(String[] args) { - com.mpush.Main.main(args); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java index 5ff50659..4b7823f7 100644 --- a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java +++ b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java @@ -1,10 +1,23 @@ -package com.mpush.test.crypto; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; +package com.mpush.test.crypto; import com.mpush.common.message.HandshakeMessage; import com.mpush.common.security.CipherBox; @@ -12,145 +25,150 @@ import org.junit.Before; import org.junit.Test; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + public class RsaTest { private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; private static RSAPrivateKey dailyPrivateKey = null; private static RSAPublicKey dailyPublicKey = null; - + @Before - public void init(){ - try { - dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); - dailyPublicKey = (RSAPublicKey)RSAUtils.decodePublicKey(daily_publicKey); - } catch (Exception e) { - e.printStackTrace(); - } - } - + public void init() { + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + dailyPublicKey = (RSAPublicKey) RSAUtils.decodePublicKey(daily_publicKey); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test - public void test(){ - + public void test() { + HandshakeMessage message = new HandshakeMessage(null); - message.clientKey = CipherBox.INSTANCE.randomAESKey(); - message.iv = CipherBox.INSTANCE.randomAESIV(); + message.clientKey = CipherBox.I.randomAESKey(); + message.iv = CipherBox.I.randomAESIV(); message.clientVersion = "1.1.0"; message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; message.osName = "android"; message.osVersion = "1.2.1"; message.timestamp = System.currentTimeMillis(); - + byte[] temp = message.encode(); - + long startencode = System.currentTimeMillis(); byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); long encodeTime = System.currentTimeMillis() - startencode; - + long startdecode = System.currentTimeMillis(); byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); long decodeTime = System.currentTimeMillis() - startdecode; decode(temp2); - System.out.println(encodeTime+","+decodeTime); - + System.out.println(encodeTime + "," + decodeTime); + } - + @Test - public void mulTest(){ - - Executor pool = Executors.newFixedThreadPool(20); - - CountDownLatch encodeLatch = new CountDownLatch(1); - CountDownLatch decodeLatch = new CountDownLatch(1); - - for(int i = 0;i<18;i++){ - pool.execute(new Worker(i, encodeLatch, decodeLatch)); - } - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - encodeLatch.countDown(); - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - decodeLatch.countDown(); - - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println("end"); - + public void mulTest() { + + Executor pool = Executors.newFixedThreadPool(20); + + CountDownLatch encodeLatch = new CountDownLatch(1); + CountDownLatch decodeLatch = new CountDownLatch(1); + + for (int i = 0; i < 18; i++) { + pool.execute(new Worker(i, encodeLatch, decodeLatch)); + } + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + encodeLatch.countDown(); + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + decodeLatch.countDown(); + + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println("end"); + } - - private static void decode(byte[] message){ - HandshakeMessage handshakeMessage = new HandshakeMessage(null); - handshakeMessage.decode(message); + + private static void decode(byte[] message) { + HandshakeMessage handshakeMessage = new HandshakeMessage(null); + handshakeMessage.decode(message); // System.out.println(ToStringBuilder.reflectionToString(handshakeMessage, ToStringStyle.MULTI_LINE_STYLE)); - } - - public static class Worker implements Runnable{ - - private int i; - private CountDownLatch encodeLatch; - private CountDownLatch decodeLatch; - - public Worker(int i,CountDownLatch encodeLatch,CountDownLatch decodeLatch) { - this.i = i; - this.encodeLatch = encodeLatch; - this.decodeLatch = decodeLatch; - } - - @Override - public void run() { - HandshakeMessage message = new HandshakeMessage(null); - message.clientKey = CipherBox.INSTANCE.randomAESKey(); - message.iv = CipherBox.INSTANCE.randomAESIV(); - message.clientVersion = "1.1.0"+i; - message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; - message.osName = "android"; - message.osVersion = "1.2.1"; - message.timestamp = System.currentTimeMillis(); - - byte[] temp = message.encode(); - System.out.println(i+":wait encode"); - try { - encodeLatch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - long startencode = System.currentTimeMillis(); - byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); - long encodeTime = System.currentTimeMillis() - startencode; - - - - System.out.println(i+":wait decode"); - - try { - decodeLatch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - long startdecode = System.currentTimeMillis(); - byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); - long decodeTime = System.currentTimeMillis() - startdecode; - decode(temp2); - System.out.println(i+":"+encodeTime+","+decodeTime); - - } - - } - + } + + public static class Worker implements Runnable { + + private int i; + private CountDownLatch encodeLatch; + private CountDownLatch decodeLatch; + + public Worker(int i, CountDownLatch encodeLatch, CountDownLatch decodeLatch) { + this.i = i; + this.encodeLatch = encodeLatch; + this.decodeLatch = decodeLatch; + } + + @Override + public void run() { + HandshakeMessage message = new HandshakeMessage(null); + message.clientKey = CipherBox.I.randomAESKey(); + message.iv = CipherBox.I.randomAESIV(); + message.clientVersion = "1.1.0" + i; + message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; + message.osName = "android"; + message.osVersion = "1.2.1"; + message.timestamp = System.currentTimeMillis(); + + byte[] temp = message.encode(); + System.out.println(i + ":wait encode"); + try { + encodeLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long startencode = System.currentTimeMillis(); + byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); + long encodeTime = System.currentTimeMillis() - startencode; + + + System.out.println(i + ":wait decode"); + + try { + decodeLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long startdecode = System.currentTimeMillis(); + byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); + long decodeTime = System.currentTimeMillis() - startdecode; + decode(temp2); + System.out.println(i + ":" + encodeTime + "," + decodeTime); + + } + + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java index 4e67ed76..806ac033 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java @@ -1,34 +1,40 @@ -package com.mpush.test.gson; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.Map; +package com.mpush.test.gson; +import com.mpush.api.spi.net.DnsMapping; import org.junit.Test; -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import com.google.common.collect.ArrayListMultimap; +import java.net.MalformedURLException; +import java.net.URL; public class DnsMappingTest { - - @Test - public void test(){ - - ArrayListMultimap mappings = ArrayListMultimap.create(); - - String dnsString = "111.1.57.148=127.0.0.1,127.0.0.2;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1"; - if (Strings.isNullOrEmpty(dnsString)) return; - Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); - Splitter vsp = Splitter.on(','); - for (Map.Entry entry : map.entrySet()) { - String value = entry.getValue(); - if (Strings.isNullOrEmpty(value)) continue; - Iterable it = vsp.split(entry.getValue()); - mappings.putAll(entry.getKey(), it); - } - - System.out.println(mappings); - - } + @Test + public void test() throws MalformedURLException { + String url = "http://baidu.com:9001/xxx/xxx?s=nc=1"; + DnsMapping mapping = new DnsMapping("127.0.0.1", 8080); + String s = mapping.translate(new URL(url)); + System.out.println(url); + System.out.println(s); + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 7406913d..0a89949e 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -1,64 +1,82 @@ -package com.mpush.test.gson; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.Map; +package com.mpush.test.gson; +import com.google.common.collect.Maps; +import com.mpush.api.push.PushContent; +import com.mpush.api.push.PushContent.PushType; import com.mpush.tools.Jsons; import org.junit.Test; -import com.google.common.collect.Maps; -import com.mpush.api.PushContent; -import com.mpush.api.PushContent.PushType; +import java.util.Map; public class GsonTest { - - @Test - public void test(){ - Map map = Maps.newHashMap(); - map.put("key1", 1121+""); - map.put("key2", "value2"); - - PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); - - - System.out.println(Jsons.toJson(content)); - - } - - @Test - public void test2(){ - ValueMap map = new ValueMap("1122", "value2"); - - PushContent content = PushContent.build(PushType.MESSAGE,Jsons.toJson(map)); - - - System.out.println(Jsons.toJson(content)); - - PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); - - System.out.println(newContetn.getContent()); - - - } - - private static class ValueMap{ - - private String key1; - private String key2; - - public ValueMap(String key1, String key2) { - this.key1 = key1; - this.key2 = key2; - } - - public String getKey1() { - return key1; - } - - public String getKey2() { - return key2; - } - - - } + + @Test + public void test() { + Map map = Maps.newHashMap(); + map.put("key1", 1121 + ""); + map.put("key2", "value2"); + + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + } + + @Test + public void test2() { + ValueMap map = new ValueMap("1122", "value2"); + + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); + + System.out.println(newContetn.getContent()); + + + } + + private static class ValueMap { + + private String key1; + private String key2; + + public ValueMap(String key1, String key2) { + this.key1 = key1; + this.key2 = key2; + } + + public String getKey1() { + return key1; + } + + public String getKey2() { + return key2; + } + + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java new file mode 100644 index 00000000..fbbe8199 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -0,0 +1,71 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.push; + +import com.mpush.api.push.PushContent; +import com.mpush.api.push.PushContent.PushType; +import com.mpush.api.push.PushSender; +import com.mpush.api.router.ClientLocation; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + +/** + * Created by ohun on 2016/1/7. + * + * @author ohun@live.cn + */ +public class PushClientTestMain { + public static void main(String[] args) throws Exception { + Logs.init(); + PushSender sender = PushSender.create(); + sender.start().get(); + for (int i = 0; i < 1; i++) { + PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); + content.setMsgId("msgId_" + (i % 2)); + Thread.sleep(1000); + sender.send(Jsons.toJson(content), Arrays.asList("user-0","doctor43test"), new PushSender.Callback() { + @Override + public void onSuccess(String userId, ClientLocation location) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId, ClientLocation location) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId, ClientLocation location) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId, ClientLocation location) { + System.err.println("push onTimeout userId=" + userId); + } + }); + } + LockSupport.park(); + } + +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java index fe122a9f..69be00b2 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java @@ -1,51 +1,64 @@ -package com.mpush.test.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +package com.mpush.test.redis; -import com.mpush.tools.redis.consistenthash.ConsistentHash; -import com.mpush.tools.redis.consistenthash.Node; +import com.mpush.cache.redis.hash.ConsistentHash; +import com.mpush.cache.redis.hash.Node; import org.junit.Test; - import redis.clients.util.Hashing; import redis.clients.util.MurmurHash; +import java.util.*; + public class ConsistentHashTest { - - private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 - - @Test - public void test(){ - Map map = new HashMap();// 每台真实机器节点上保存的记录条数 - List nodes = new ArrayList();// 真实机器节点 - // 10台真实机器节点集群 - for (int i = 1; i <= 10; i++) { - map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 - Node node = new Node(IP_PREFIX + i, "node" + i); - nodes.add(node); - } - Hashing hashFunction = new MurmurHash(); // hash函数实例 - ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 - // 将5000条记录尽可能均匀的存储到10台机器节点 - for (int i = 0; i < 5000; i++) { - // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 - String data = UUID.randomUUID().toString() + i; - // 通过记录找到真实机器节点 - Node node = consistentHash.get(data); - // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 - // 每台真实机器节点上保存的记录条数加1 - map.put(node.getIp(), map.get(node.getIp()) + 1); - } - // 打印每台真实机器节点保存的记录条数 - for (int i = 1; i <= 10; i++) { - System.out.println(IP_PREFIX + i + "节点记录条数:" - + map.get("192.168.1." + i)); - } - - } + + private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 + + @Test + public void test() { + Map map = new HashMap();// 每台真实机器节点上保存的记录条数 + List nodes = new ArrayList();// 真实机器节点 + // 10台真实机器节点集群 + for (int i = 1; i <= 10; i++) { + map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 + Node node = new Node(IP_PREFIX + i, "node" + i); + nodes.add(node); + } + Hashing hashFunction = new MurmurHash(); // hash函数实例 + ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 + // 将5000条记录尽可能均匀的存储到10台机器节点 + for (int i = 0; i < 5000; i++) { + // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 + String data = UUID.randomUUID().toString() + i; + // 通过记录找到真实机器节点 + Node node = consistentHash.get(data); + // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 + // 每台真实机器节点上保存的记录条数加1 + map.put(node.getIp(), map.get(node.getIp()) + 1); + } + // 打印每台真实机器节点保存的记录条数 + for (int i = 1; i <= 10; i++) { + System.out.println(IP_PREFIX + i + "节点记录条数:" + + map.get("192.168.1." + i)); + } + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java index c52c091c..43a30342 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java @@ -1,13 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import org.junit.Test; public class MPushUtilTest { - - @Test - public void getIp() throws Exception{ - System.out.println(MPushUtil.getExtranetAddress()); - } + + @Test + public void getIp() throws Exception { + System.out.println(Utils.getExtranetAddress()); + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java index a2913fa5..f3cf52e6 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java @@ -1,59 +1,73 @@ -package com.mpush.test.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.List; -import java.util.concurrent.locks.LockSupport; +package com.mpush.test.redis; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.tools.redis.pubsub.Subscriber; -import com.mpush.tools.redis.RedisRegister; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.cache.redis.manager.ZKRedisClusterManager; +import com.mpush.cache.redis.mq.Subscriber; import org.junit.Before; import org.junit.Test; -import com.google.common.collect.Lists; -import com.mpush.tools.spi.ServiceContainer; +import java.util.concurrent.locks.LockSupport; public class PubSubTest { - - private RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - + + private ZKRedisClusterManager redisClusterManager = ZKRedisClusterManager.I; + @Before - public void init(){ - RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - RedisGroup group = new RedisGroup(); - group.addRedisNode(node); - List listGroup = Lists.newArrayList(group); - redisRegister.init(listGroup); + public void init() { + RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + RedisGroup group = new RedisGroup(); + group.addRedisNode(node); + redisClusterManager.addGroup(group); + } + + @Test + public void subpubTest() { + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); } - - @Test - public void subpubTest(){ - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - } - - @Test - public void pubsubTest(){ - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - } - - @Test - public void pubTest(){ - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - } - - @Test - public void subTest(){ - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - LockSupport.park(); - } - + + @Test + public void pubsubTest() { + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + } + + @Test + public void pubTest() { + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); + } + + @Test + public void subTest() { + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + LockSupport.park(); + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java index 13052f07..7069153a 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java @@ -1,46 +1,63 @@ -package com.mpush.test.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.Date; -import java.util.HashSet; -import java.util.Set; +package com.mpush.test.redis; -import com.mpush.tools.redis.RedisPoolConfig; +import com.mpush.cache.redis.RedisClient; +import com.mpush.tools.Jsons; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; - -import com.mpush.tools.Jsons; - import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + public class RedisClusterTest { - Set jedisClusterNodes = new HashSet(); - - JedisCluster cluster = null; - - @Before - public void init() { - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); - cluster = new JedisCluster(jedisClusterNodes, RedisPoolConfig.config); - } - - @Test - public void test() { - - User user = new User("huang", 18, new Date()); - cluster.set("huang", Jsons.toJson(user)); - String ret = cluster.get("huang"); - User newUser = Jsons.fromJson(ret, User.class); - System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); - - } + Set jedisClusterNodes = new HashSet(); + + JedisCluster cluster = null; + + @Before + public void init() { + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); + cluster = new JedisCluster(jedisClusterNodes, RedisClient.CONFIG); + } + + @Test + public void test() { + + User user = new User("huang", 18, new Date()); + cluster.set("huang", Jsons.toJson(user)); + String ret = cluster.get("huang"); + User newUser = Jsons.fromJson(ret, User.class); + System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java deleted file mode 100644 index 2d376e59..00000000 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java +++ /dev/null @@ -1,109 +0,0 @@ -//package com.mpush.tools.redis; -// -//import java.util.Date; -//import java.util.List; -// -//import MessageListener; -// -//import org.apache.commons.lang3.builder.ToStringBuilder; -//import org.apache.commons.lang3.builder.ToStringStyle; -//import org.junit.Before; -//import org.junit.Test; -// -//import MPushUtil; -//import RedisManage; -//import Subscriber; -//import ServiceContainer; -//import com.mpush.tools.zk.ServerApp; -// -//public class RedisGroupManageTest { -// -// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// List groupList = null; -// -// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); -// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); -// -// RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); -// -// @Before -// public void init() { -// groupList = redisRegister.getGroupList(); -// } -// -// @Test -// public void testGetRedisGroup() { -// for (RedisGroup group : groupList) { -// for (RedisNode node : group.getRedisNodeList()) { -// System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); -// } -// -// } -// } -// -// @Test -// public void testAdd() { -// User user = RedisManage.get("huang2", User.class); -// if (user == null) { -// user = new User("hi", 10, new Date()); -// RedisManage.set("huang2", user); -// user = RedisManage.get("huang2", User.class); -// } -// System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); -// -// User nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// -// RedisManage.del("huang2"); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// if (nowUser == null) { -// System.out.println("node2 nowUser is null"); -// } else { -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); -// -// } -// -// @Test -// public void testPub() { -// for (int i = 0; i < 20; i++) { -// User user = new User("pub" + i, 10, new Date()); -// RedisManage.publish("channel1", user); -// RedisManage.publish("channel2", user); -// } -// } -// -// @Test -// public void testSub() { -// RedisManage.subscribe(new MessageListener() { -// @Override -// public void onMessage(String channel, String message) { -// System.out.printf("on message channel=%s, message=%s%n", channel, message); -// } -// }, "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// @Test -// public void testSub2() { -// RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// -//} diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index c8c4a45f..89ef5cb3 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -1,199 +1,215 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.RedisServer; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; +import redis.clients.jedis.Jedis; + import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.junit.Test; +public class RedisUtilTest { -import com.google.common.collect.Lists; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.RedisUtil; -import redis.clients.jedis.Jedis; + RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + + List nodeList = Lists.newArrayList(node); + + @Test + public void testAddAndGetAndDelete() { + Jedis jedis = RedisClient.getClient(node); + jedis.set("hi", "huang"); + + String ret = jedis.get("hi"); + System.out.println(ret); + + jedis.del("hi"); + ret = jedis.get("hi"); + if (ret == null) { + System.out.println("ret is null"); + } else { + System.out.println("ret is not null:" + ret); + } + + } + + @Test + public void testJedisPool() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 0; i < 10; i++) { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + } + } + + @Test + public void testJedisPool2() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 1; i <= 8; i++) { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + } + + System.out.println(jedisList.size()); + + try { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + System.out.println("first get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + // 关闭一个链接 + RedisClient.close(jedisList.get(0)); + + try { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + System.out.println("second get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + System.out.println(jedisList.size()); + } + + @Test + public void testKV() { + User user = new User("huang", 18, new Date()); + RedisClient.set(nodeList, "test", user); + + User nowUser = RedisClient.get(node, "test", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisClient.get(node, "test", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisClient.del(nodeList, "test"); + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + RedisClient.set(nodeList, "test", user, 10); + + try { + Thread.sleep(12000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void hashTest() { + + User user = new User("huang", 18, new Date()); + + RedisClient.hset(nodeList, "hashhuang", "hi", user); + + User nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + Map ret = RedisClient.hgetAll(node, "hashhuang", User.class); + Iterator> iterator = ret.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + User val = entry.getValue(); + System.out.println("all:" + key + "," + ToStringBuilder.reflectionToString(val)); + } + + RedisClient.hdel(nodeList, "hashhuang", "hi"); + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void testSet() { +// System.out.println(RedisClient.sCard(node, RedisKey.getUserOnlineKey())); + +// List onlineUserIdList = RedisClient.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); +// System.out.println(onlineUserIdList.size()); -public class RedisUtilTest { + } - - - RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - - List nodeList = Lists.newArrayList(node); - - @Test - public void testAddAndGetAndDelete() { - Jedis jedis = RedisUtil.getClient(node); - jedis.set("hi", "huang"); - - String ret = jedis.get("hi"); - System.out.println(ret); - - jedis.del("hi"); - ret = jedis.get("hi"); - if (ret == null) { - System.out.println("ret is null"); - } else { - System.out.println("ret is not null:" + ret); - } - - } - - @Test - public void testJedisPool() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 0; i < 10; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - } - - @Test - public void testJedisPool2() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 1; i <= 8; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - - System.out.println(jedisList.size()); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("first get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - // 关闭一个链接 - RedisUtil.close(jedisList.get(0)); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("second get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - System.out.println(jedisList.size()); - } - - @Test - public void testKV() { - User user = new User("huang", 18, new Date()); - RedisUtil.set(nodeList, "test", user); - - User nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisUtil.del(nodeList, "test"); - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - RedisUtil.set(nodeList, "test", user, 10); - - try { - Thread.sleep(12000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void hashTest(){ - - User user = new User("huang", 18, new Date()); - - RedisUtil.hset(nodeList, "hashhuang", "hi", user); - - User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - - Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); - Iterator> iterator = ret.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String key = entry.getKey(); - User val = entry.getValue(); - System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); - } - - RedisUtil.hdel(nodeList, "hashhuang", "hi"); - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node2 nowUser is null"); - }else{ - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node nowUser is null"); - }else{ - System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void testSet(){ -// System.out.println(RedisUtil.sCard(node, RedisKey.getUserOnlineKey())); - -// List onlineUserIdList = RedisUtil.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); -// System.out.println(onlineUserIdList.size()); - - } - - @Test - public void testlist(){ -// RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); - } - - @Test - public void testsortedset(){ -// RedisUtil.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); - -// long len =RedisUtil.zCard(node, RedisKey.getUserOnlineKey()); + @Test + public void testlist() { +// RedisClient.del(nodeList, RedisKey.getUserOfflineKey()); + } + + @Test + public void testsortedset() { +// RedisClient.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); + +// long len =RedisClient.zCard(node, RedisKey.getUserOnlineKey()); // System.out.println(len); - } + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/User.java b/mpush-test/src/test/java/com/mpush/test/redis/User.java index 180ebf1e..f9279eee 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/User.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/User.java @@ -1,40 +1,64 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import java.io.Serializable; import java.util.Date; -public class User implements Serializable{ - - private static final long serialVersionUID = -6269129553435313118L; - - private String name; - private int age; - private Date date; - - public User(String name, int age, Date date) { - this.name = name; - this.age = age; - this.date = date; - } - - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public int getAge() { - return age; - } - public void setAge(int age) { - this.age = age; - } - public Date getDate() { - return date; - } - public void setDate(Date date) { - this.date = date; - } - +public class User implements Serializable { + + private static final long serialVersionUID = -6269129553435313118L; + + private String name; + private int age; + private Date date; + + public User(String name, int age, Date date) { + this.name = name; + this.age = age; + this.date = date; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java new file mode 100644 index 00000000..199de67d --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -0,0 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.sever; + +import com.mpush.bootstrap.Main; + +/** + * Created by yxx on 2016/5/16. + * + * @author ohun@live.cn + */ +public class ServerTestMain { + public static void main(String[] args) { + Main.main(args); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java index 5965f67d..5e3433de 100644 --- a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java @@ -1,31 +1,50 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.util; +import com.mpush.tools.Utils; +import org.junit.Test; + import java.net.URI; import java.net.URISyntaxException; -import com.mpush.tools.MPushUtil; -import org.junit.Test; - public class TelnetTest { - @Test - public void test(){ - boolean ret = MPushUtil.telnet("120.27.196.68", 82); - System.out.println(ret); - } - - @Test - public void test2(){ - boolean ret = MPushUtil.telnet("120.27.196.68", 80); - System.out.println(ret); - } - - @Test - public void uriTest() throws URISyntaxException{ - String url = "http://127.0.0.1"; - URI uri = new URI(url); - System.out.println(uri.getPort()); - } - - + @Test + public void test() { + boolean ret = Utils.checkHealth("120.27.196.68", 82); + System.out.println(ret); + } + + @Test + public void test2() { + boolean ret = Utils.checkHealth("120.27.196.68", 80); + System.out.println(ret); + } + + @Test + public void uriTest() throws URISyntaxException { + String url = "http://127.0.0.1"; + URI uri = new URI(url); + System.out.println(uri.getPort()); + } + + } diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf new file mode 100644 index 00000000..4c50b7c1 --- /dev/null +++ b/mpush-test/src/test/resources/application.conf @@ -0,0 +1,6 @@ +mp.log.dir=${user.dir}/mpush-test/target/logs +mp.log.level=debug +mp.zk.namespace=mpush +mp.zk.server-address="111.1.57.148:5666" +mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} +mp.core.compress-threshold=10k \ No newline at end of file diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index ea3f95d0..0d67de0e 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - + + System.out + UTF-8 + DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - debug - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + debug + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory b/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory new file mode 100644 index 00000000..93607f48 --- /dev/null +++ b/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory @@ -0,0 +1 @@ +com.mpush.client.push.PushClientFactory diff --git a/mpush-tools/.gitignore b/mpush-tools/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-tools/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 218927c0..31ae2c40 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -14,7 +14,24 @@ ${mpush-tools-version} jar mpush-tools - + + + + src/main/resources + + **/* + + false + + + ../conf + + reference.conf + + false + + + com.google.code.gson @@ -28,54 +45,37 @@ org.apache.commons commons-lang3 - - redis.clients - jedis + org.aeonbits.owner + owner + + + com.typesafe + config + + + ${mpush.groupId} + mpush-api org.slf4j slf4j-api - ${mpush.groupId} - mpush-log + org.slf4j + jcl-over-slf4j - org.aeonbits.owner - owner + ch.qos.logback + logback-classic - commons-net - commons-net + commons-logging + commons-logging + + + log4j + log4j - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-tools - - - - - - - - diff --git a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java b/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java deleted file mode 100644 index 27497d15..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.mpush.tools; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public final class ConsoleLog { - private static final String TAG = "[mpush] "; - private static final DateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); - - public static void d(String s, Object... args) { - System.out.printf(format.format(new Date()) + " [D] " + TAG + s + '\n', args); - } - - public static void i(String s, Object... args) { - System.out.printf(format.format(new Date()) + " [I] " + TAG + s + '\n', args); - } - - public static void w(String s, Object... args) { - System.err.printf(format.format(new Date()) + " [W] " + TAG + s + '\n', args); - } - - public static void e(Throwable e, String s, Object... args) { - System.err.printf(format.format(new Date()) + " [E] " + TAG + s + '\n', args); - e.printStackTrace(); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/mpush/tools/Constants.java deleted file mode 100644 index 458c6068..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/Constants.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.mpush.tools; - -import java.nio.charset.Charset; - -/** - * Created by ohun on 2015/12/25. - * - * @author ohun@live.cn - */ -public interface Constants { - Charset UTF_8 = Charset.forName("UTF-8"); - byte[] EMPTY_BYTES = new byte[0]; - - int MIN_BOSS_POOL_SIZE = 50; - int MAX_BOSS_POOL_SIZE = 100; - int BOSS_THREAD_QUEUE_SIZE = 10000; - - int MIN_WORK_POOL_SIZE = 50; - int MAX_WORK_POOL_SIZE = 150; - int WORK_THREAD_QUEUE_SIZE = 10000; - - int BIZ_POOL_SIZE = 20; - - int EVENT_BUS_POOL_SIZE = 10; - - int REDIS_POOL_SIZE = 3; - int REDIS_THREAD_QUEUE_SIZE = 10000; - - //redis - int REDIS_TIMEOUT = 2000; - int REDIS_MAX_TOTAL = 8; - int REDIS_MAX_IDLE = 4; - int REDIS_MIN_IDLE = 1; - int REDIS_MAX_WAIT_MILLIS = 5000; - int REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 300000; - int REDIS_NUM_TESTS_PER_EVICTION_RUN = 3; - int REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; - boolean REDIS_TEST_ON_BORROW = false; - boolean REDIS_TEST_ON_RETURN = false; - boolean REDIS_TEST_WHILE_IDLE = false; -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java deleted file mode 100644 index 342c312a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java +++ /dev/null @@ -1,139 +0,0 @@ -package com.mpush.tools; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - - -public class GenericsUtil { - - /** - * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * - * @param clazz clazz 需要反射的类,该类必须继承范型父类 - * @param index 泛型参数所在索引,从0开始. - * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz, int index) { - Type genType = clazz.getGenericSuperclass();//得到泛型父类 - //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class - - if (!(genType instanceof ParameterizedType)) { - return Object.class; - } - //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 - Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); - if (index >= params.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - if (!(params[index] instanceof Class)) { - return Object.class; - } - return (Class) params[index]; - } - /** - * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * - * @param clazz clazz 需要反射的类,该类必须继承泛型父类 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz) { - return getSuperClassGenericType(clazz, 0); - } - /** - * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} - * - * @param method 方法 - * @param index 泛型参数所在索引,从0开始. - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method, int index) { - Type returnType = method.getGenericReturnType(); - if(returnType instanceof ParameterizedType){ - ParameterizedType type = (ParameterizedType) returnType; - Type[] typeArguments = type.getActualTypeArguments(); - if (index >= typeArguments.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class)typeArguments[index]; - } - return Object.class; - } - /** - * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} - * - * @param method 方法 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method) { - return getMethodGenericReturnType(method, 0); - } - - /** - * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 - * @param index 第几个输入参数 - * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method, int index) { - List> results = new ArrayList>(); - Type[] genericParameterTypes = method.getGenericParameterTypes(); - if (index >= genericParameterTypes.length ||index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - Type genericParameterType = genericParameterTypes[index]; - if(genericParameterType instanceof ParameterizedType){ - ParameterizedType aType = (ParameterizedType) genericParameterType; - Type[] parameterArgTypes = aType.getActualTypeArguments(); - for(Type parameterArgType : parameterArgTypes){ - Class parameterArgClass = (Class) parameterArgType; - results.add(parameterArgClass); - } - return results; - } - return results; - } - /** - * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 - * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method) { - return getMethodGenericParameterTypes(method, 0); - } - /** - * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @param index 泛型参数所在索引,从0开始. - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field, int index) { - Type genericFieldType = field.getGenericType(); - - if(genericFieldType instanceof ParameterizedType){ - ParameterizedType aType = (ParameterizedType) genericFieldType; - Type[] fieldArgTypes = aType.getActualTypeArguments(); - if (index >= fieldArgTypes.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class)fieldArgTypes[index]; - } - return Object.class; - } - /** - * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field) { - return getFieldGenericType(field, 0); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java deleted file mode 100644 index 1bffb0b0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java +++ /dev/null @@ -1,136 +0,0 @@ -package com.mpush.tools; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.lang.management.ManagementFactory; -import java.security.AccessController; -import java.security.PrivilegedExceptionAction; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Executors; - -import javax.management.MBeanServer; -import javax.management.ObjectName; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.sun.management.HotSpotDiagnosticMXBean; - -public class JVMUtil { - - private static final Logger log = LoggerFactory.getLogger(JVMUtil.class); - - private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; - private static volatile HotSpotDiagnosticMXBean hotspotMBean; - - private static Object lock = new Object(); - - public static void jstack(OutputStream stream) throws Exception { - try { - Map map = Thread.getAllStackTraces(); - Iterator> ite = map.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - StackTraceElement[] elements = entry.getValue(); - if (elements != null && elements.length > 0) { - String threadName = entry.getKey().getName(); - stream.write(("Thread Name :[" + threadName + "]\n").getBytes()); - for (StackTraceElement el : elements) { - String stack = el.toString() + "\n"; - stream.write(stack.getBytes()); - } - stream.write("\n".getBytes()); - } - } - } catch (Exception e) { - throw e; - } - } - - public static void dumpJstack(final String jvmPath){ - Executors.newSingleThreadExecutor().execute(new Runnable() { - @Override - public void run() { - String logPath = jvmPath; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.log")); - JVMUtil.jstack(jstackStream); - } catch (Throwable t) { - log.error("Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } - } - } - } - }); - } - - private static HotSpotDiagnosticMXBean getHotspotMBean() { - try { - return AccessController.doPrivileged(new PrivilegedExceptionAction() { - public HotSpotDiagnosticMXBean run() throws Exception { - MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - Set s = server.queryNames(new ObjectName(HOTSPOT_BEAN_NAME), null); - Iterator itr = s.iterator(); - if (itr.hasNext()) { - ObjectName name = itr.next(); - HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, - name.toString(), HotSpotDiagnosticMXBean.class); - return bean; - } else { - return null; - } - } - }); - } catch (Exception e) { - log.error("getHotspotMBean Error!", e); - return null; - } - } - - private static void initHotspotMBean() throws Exception { - if (hotspotMBean == null) { - synchronized (lock) { - if (hotspotMBean == null) { - hotspotMBean = getHotspotMBean(); - } - } - } - } - - public static void jMap(String fileName, boolean live) { - File f = new File(fileName, System.currentTimeMillis()+"-jmap.log"); - String currentFileName = f.getPath(); - try { - initHotspotMBean(); - if (f.exists()) { - f.delete(); - } - - hotspotMBean.dumpHeap(currentFileName, live); - } catch (Exception e) { - log.error("dumpHeap Error!"+currentFileName, e); - } - } - - public static void dumpJmap(final String jvmPath){ - - Executors.newSingleThreadExecutor().execute(new Runnable() { - @Override - public void run() { - jMap(jvmPath, false); - } - }); - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java index 5b7e726e..b5963e34 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -1,8 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import com.google.gson.Gson; import com.google.gson.GsonBuilder; - +import com.mpush.api.Constants; +import com.mpush.tools.common.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,10 +118,4 @@ private static void append(Map.Entry entry, StringBuilder sb) { sb.append(':'); sb.append('"').append(value).append('"'); } - - public static void main(String[] args) { - String tet = "7"; - Long l = Jsons.fromJson(tet, Long.class); - System.out.println(l); - } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/Pair.java deleted file mode 100644 index 23019945..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/Pair.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.mpush.tools; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public final class Pair { - public final K key; - public final V value; - - public Pair(K key, V value) { - this.key = key; - this.value = value; - } - - public K first() { - return key; - } - - public V second() { - return value; - } - - public static Pair of(K k, V v) { - return new Pair<>(k, v); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java similarity index 80% rename from mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/Utils.java index 5e846c06..6a041dfd 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -1,13 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; -import com.mpush.tools.config.ConfigCenter; -import org.apache.commons.net.telnet.TelnetClient; +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.NetworkInterface; +import java.net.Socket; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -18,8 +38,8 @@ * * @author ohun@live.cn */ -public final class MPushUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); +public final class Utils { + private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); private static String LOCAL_IP; @@ -42,13 +62,6 @@ public static String getLocalIp() { return LOCAL_IP; } - public static int getHeartbeat(int min, int max) { - return Math.max( - ConfigCenter.I.minHeartbeat(), - Math.min(max, ConfigCenter.I.maxHeartbeat()) - ); - } - /** * 获取本机ip * 只获取第一块网卡绑定的ip地址 @@ -105,20 +118,7 @@ public static String getExtranetAddress() { } catch (Throwable e) { LOGGER.error("getExtranetAddress exception", e); } - - - String localIp = getInetAddress(); - String remoteIp = null; - Map mapping = ConfigCenter.I.remoteIpMapping(); - if (mapping != null) { - remoteIp = mapping.get(localIp); - } - - if (remoteIp == null) { - remoteIp = localIp; - } - - return remoteIp; + return null; } public static String headerToString(Map headers) { @@ -157,21 +157,14 @@ public static Map headerFromString(String headersString) { return headers; } - public static boolean telnet(String ip, int port) { - TelnetClient client = new TelnetClient(); + public static boolean checkHealth(String ip, int port) { try { - client.connect(ip, port); + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(ip, port), 1000); + socket.close(); return true; - } catch (Exception e) { + } catch (IOException e) { return false; - } finally { - try { - if (client.isConnected()) { - client.disconnect(); - } - } catch (IOException e) { - LOGGER.error("disconnect error", e); - } } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java new file mode 100644 index 00000000..f8db214d --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java @@ -0,0 +1,164 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + + +public class GenericsUtil { + + /** + * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * + * @param clazz clazz 需要反射的类,该类必须继承范型父类 + * @param index 泛型参数所在索引,从0开始. + * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getSuperClassGenericType(Class clazz, int index) { + Type genType = clazz.getGenericSuperclass();//得到泛型父类 + //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class + + if (!(genType instanceof ParameterizedType)) { + return Object.class; + } + //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 + Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); + if (index >= params.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + if (!(params[index] instanceof Class)) { + return Object.class; + } + return (Class) params[index]; + } + + /** + * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * + * @param clazz clazz 需要反射的类,该类必须继承泛型父类 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getSuperClassGenericType(Class clazz) { + return getSuperClassGenericType(clazz, 0); + } + + /** + * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} + * + * @param method 方法 + * @param index 泛型参数所在索引,从0开始. + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getMethodGenericReturnType(Method method, int index) { + Type returnType = method.getGenericReturnType(); + if (returnType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) returnType; + Type[] typeArguments = type.getActualTypeArguments(); + if (index >= typeArguments.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class) typeArguments[index]; + } + return Object.class; + } + + /** + * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} + * + * @param method 方法 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getMethodGenericReturnType(Method method) { + return getMethodGenericReturnType(method, 0); + } + + /** + * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} + * + * @param method 方法 + * @param index 第几个输入参数 + * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 + */ + public static List> getMethodGenericParameterTypes(Method method, int index) { + List> results = new ArrayList>(); + Type[] genericParameterTypes = method.getGenericParameterTypes(); + if (index >= genericParameterTypes.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + Type genericParameterType = genericParameterTypes[index]; + if (genericParameterType instanceof ParameterizedType) { + ParameterizedType aType = (ParameterizedType) genericParameterType; + Type[] parameterArgTypes = aType.getActualTypeArguments(); + for (Type parameterArgType : parameterArgTypes) { + Class parameterArgClass = (Class) parameterArgType; + results.add(parameterArgClass); + } + return results; + } + return results; + } + + /** + * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} + * + * @param method 方法 + * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 + */ + public static List> getMethodGenericParameterTypes(Method method) { + return getMethodGenericParameterTypes(method, 0); + } + + /** + * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; + * + * @param field 字段 + * @param index 泛型参数所在索引,从0开始. + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getFieldGenericType(Field field, int index) { + Type genericFieldType = field.getGenericType(); + + if (genericFieldType instanceof ParameterizedType) { + ParameterizedType aType = (ParameterizedType) genericFieldType; + Type[] fieldArgTypes = aType.getActualTypeArguments(); + if (index >= fieldArgTypes.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class) fieldArgTypes[index]; + } + return Object.class; + } + + /** + * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; + * + * @param field 字段 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getFieldGenericType(Field field) { + return getFieldGenericType(field, 0); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java similarity index 66% rename from mpush-tools/src/main/java/com/mpush/tools/IOUtils.java rename to mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java index f322e8f8..da840eec 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java @@ -1,5 +1,25 @@ -package com.mpush.tools; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; +import com.mpush.api.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,9 +49,9 @@ public static void close(Closeable closeable) { } public static byte[] compress(byte[] data) { - - Profiler.enter("start compress"); - + + Profiler.enter("start compress"); + ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); DeflaterOutputStream zipOut = new DeflaterOutputStream(out); try { @@ -48,8 +68,8 @@ public static byte[] compress(byte[] data) { return out.toByteArray(); } - public static byte[] uncompress(byte[] data) { - Profiler.enter("start uncompress"); + public static byte[] decompress(byte[] data) { + Profiler.enter("start decompress"); InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4); byte[] buffer = new byte[1024]; @@ -59,7 +79,7 @@ public static byte[] uncompress(byte[] data) { out.write(buffer, 0, length); } } catch (IOException e) { - LOGGER.error("uncompress ex", e); + LOGGER.error("decompress ex", e); return Constants.EMPTY_BYTES; } finally { close(zipIn); diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java new file mode 100644 index 00000000..34159ba4 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -0,0 +1,187 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +import com.sun.management.HotSpotDiagnosticMXBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.management.MBeanServer; +import javax.management.ObjectName; +import java.io.*; +import java.lang.management.*; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class JVMUtil { + private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; + + private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class); + + private static HotSpotDiagnosticMXBean hotSpotMXBean; + private static ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + + public static void jstack(OutputStream stream) throws Exception { + PrintStream out = new PrintStream(stream); + boolean cpuTimeEnabled = threadMXBean.isThreadCpuTimeSupported() && threadMXBean.isThreadCpuTimeEnabled(); + Map map = Thread.getAllStackTraces(); + + for (Map.Entry entry : map.entrySet()) { + Thread t = entry.getKey(); + StackTraceElement[] elements = entry.getValue(); + + ThreadInfo tt = threadMXBean.getThreadInfo(t.getId()); + long tid = t.getId(); + Thread.State state = t.getState(); + long cpuTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadCpuTime(tid) / 1000000 : -1; + long userTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadUserTime(tid) / 1000000 : -1; + + out.printf("%s id=%d state=%s deamon=%s priority=%s cpu[total=%sms,user=%sms]", t.getName(), + tid, t.getState(), t.isDaemon(), t.getPriority(), cpuTimeMillis, userTimeMillis); + final LockInfo lock = tt.getLockInfo(); + if (lock != null && state != Thread.State.BLOCKED) { + out.printf("%n - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()); + out.printf("%n - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()); + } else if (lock != null && state == Thread.State.BLOCKED) { + out.printf("%n - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), + lock.getClassName()); + } + + if (tt.isSuspended()) { + out.print(" (suspended)"); + } + + if (tt.isInNative()) { + out.print(" (running in native)"); + } + + out.println(); + if (tt.getLockOwnerName() != null) { + out.printf(" owned by %s id=%d%n", tt.getLockOwnerName(), tt.getLockOwnerId()); + } + + final MonitorInfo[] monitors = tt.getLockedMonitors(); + + for (int i = 0; i < elements.length; i++) { + final StackTraceElement element = elements[i]; + out.printf(" at %s%n", element); + for (int j = 1; j < monitors.length; j++) { + final MonitorInfo monitor = monitors[j]; + if (monitor.getLockedStackDepth() == i) { + out.printf(" - locked %s%n", monitor); + } + } + } + + out.println(); + + final LockInfo[] locks = tt.getLockedSynchronizers(); + if (locks.length > 0) { + out.printf(" Locked synchronizers: count = %d%n", locks.length); + for (LockInfo l : locks) { + out.printf(" - %s%n", l); + } + out.println(); + } + } + } + + public static void dumpJstack(final String jvmPath) { + new Thread((new Runnable() { + @Override + public void run() { + String logPath = jvmPath; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); + JVMUtil.jstack(jstackStream); + } catch (Throwable t) { + LOGGER.error("Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { + } + } + } + } + })).start(); + } + + private static HotSpotDiagnosticMXBean getHotSpotMXBean() { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + public HotSpotDiagnosticMXBean run() throws Exception { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + Set s = server.queryNames(new ObjectName(HOT_SPOT_BEAN_NAME), null); + Iterator itr = s.iterator(); + if (itr.hasNext()) { + ObjectName name = itr.next(); + HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, + name.toString(), HotSpotDiagnosticMXBean.class); + return bean; + } else { + return null; + } + } + }); + } catch (Exception e) { + LOGGER.error("getHotSpotMXBean Error!", e); + return null; + } + } + + private static void initHotSpotMBean() throws Exception { + if (hotSpotMXBean == null) { + synchronized (JVMUtil.class) { + if (hotSpotMXBean == null) { + hotSpotMXBean = getHotSpotMXBean(); + } + } + } + } + + public static void jMap(String fileName, boolean live) { + File f = new File(fileName, System.currentTimeMillis() + "-jmap.LOGGER"); + String currentFileName = f.getPath(); + try { + initHotSpotMBean(); + if (f.exists()) { + f.delete(); + } + hotSpotMXBean.dumpHeap(currentFileName, live); + } catch (Exception e) { + LOGGER.error("dumpHeap Error!" + currentFileName, e); + } + } + + public static void dumpJmap(final String jvmPath) { + new Thread(new Runnable() { + @Override + public void run() { + jMap(jvmPath, false); + } + }).start(); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java new file mode 100644 index 00000000..f32cdb9f --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +/** + * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn + */ +public final class Pair { + public final K key; + public final V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K first() { + return key; + } + + public V second() { + return value; + } + + public static Pair of(K k, V v) { + return new Pair<>(k, v); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java similarity index 93% rename from mpush-tools/src/main/java/com/mpush/tools/Profiler.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java index d4a3d1fb..0d8f29c2 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java @@ -1,20 +1,39 @@ -package com.mpush.tools; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +import org.apache.commons.lang3.StringUtils; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.apache.commons.lang3.StringUtils; - /** * 用来测试并统计线程执行时间的工具。 */ -@SuppressWarnings(value={"rawtypes","unchecked"}) +@SuppressWarnings(value = {"rawtypes", "unchecked"}) public class Profiler { - private static final ThreadLocal entryStack = new ThreadLocal(); + private static final ThreadLocal entryStack = new ThreadLocal(); public static final String EMPTY_STRING = ""; /** @@ -30,7 +49,7 @@ public static void start() { * @param message 第一个entry的信息 */ - public static void start(String message) { + public static void start(String message) { entryStack.set(new Entry(message, null, null)); } @@ -193,7 +212,7 @@ private Entry(Object message, Entry parentEntry, Entry firstEntry) { this.message = message; this.startTime = System.currentTimeMillis(); this.parentEntry = parentEntry; - this.firstEntry = (Entry) firstEntry == null ? this : firstEntry; + this.firstEntry = firstEntry == null ? this : firstEntry; this.baseTime = (firstEntry == null) ? 0 : firstEntry.startTime; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/mpush/tools/common/Strings.java similarity index 52% rename from mpush-tools/src/main/java/com/mpush/tools/Strings.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Strings.java index c81fc531..c7e0dac4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Strings.java @@ -1,4 +1,23 @@ -package com.mpush.tools; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; /** * Created by ohun on 2015/12/23. diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java new file mode 100644 index 00000000..112cf893 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java @@ -0,0 +1,94 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.common; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by yxx on 2016/5/26. + * + * @author ohun@live.cn (夜色) + */ +public final class TimeLine { + private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + private final TimePoint root = new TimePoint("root"); + private final String name; + private TimePoint current = root; + + public TimeLine() { + name = "TimeLine"; + } + + public TimeLine(String name) { + this.name = name; + } + + public void begin() { + addTimePoint("begin"); + } + + public void addTimePoint(String name) { + current = current.next = new TimePoint(name); + } + + public void end() { + addTimePoint("end"); + } + + public void clean() { + root.next = null; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(name); + if (root.next != null) { + sb.append('[').append(current.point - root.next.point).append(']'); + } + sb.append('{'); + TimePoint next = root; + while ((next = next.next) != null) { + sb.append(next.toString()); + } + sb.append('}'); + return sb.toString(); + } + + private static class TimePoint { + private final String name; + private final long point = System.currentTimeMillis(); + private TimePoint next; + + public TimePoint(String name) { + this.name = name; + } + + public void setNext(TimePoint next) { + this.next = next; + } + + @Override + public String toString() { + String header = name + "[" + formatter.format(new Date(point)) + "]"; + if (next == null) return header; + return header + " --" + (next.point - point) + "(ms)--> "; + } + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java new file mode 100644 index 00000000..2f597c11 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -0,0 +1,345 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config; + +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.tools.config.data.RedisGroup; +import com.mpush.tools.config.data.RedisServer; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigList; +import com.typesafe.config.ConfigObject; + +import java.io.File; +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static com.typesafe.config.ConfigBeanFactory.create; +import static java.util.stream.Collectors.toCollection; + +/** + * Created by yxx on 2016/5/20. + * + * @author ohun@live.cn + */ +public interface CC { + Config cfg = load(); + + static Config load() { + Config config = ConfigFactory.load(); + String custom_conf = "mp.conf"; + if (config.hasPath(custom_conf)) { + File file = new File(config.getString(custom_conf)); + if (file.exists()) { + Config custom = ConfigFactory.parseFile(file); + config = custom.withFallback(config); + } + } + return config; + } + + interface mp { + Config cfg = CC.cfg.getObject("mp").toConfig(); + String log_dir = cfg.getString("log.dir"); + String log_level = cfg.getString("log.level"); + + interface core { + Config cfg = mp.cfg.getObject("core").toConfig(); + + int session_expired_time = (int) cfg.getDuration("session-expired-time").getSeconds(); + + int max_heartbeat = (int) cfg.getDuration("max-heartbeat", TimeUnit.MILLISECONDS); + + int max_packet_size = (int) cfg.getMemorySize("max-packet-size").toBytes(); + + int min_heartbeat = (int) cfg.getDuration("min-heartbeat", TimeUnit.MILLISECONDS); + + long compress_threshold = cfg.getBytes("compress-threshold"); + + int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times"); + + String epoll_provider = cfg.getString("epoll-provider"); + } + + interface net { + Config cfg = mp.cfg.getObject("net").toConfig(); + + int connect_server_port = cfg.getInt("connect-server-port"); + int gateway_server_port = cfg.getInt("gateway-server-port"); + int admin_server_port = cfg.getInt("admin-server-port"); + + interface public_ip_mapping { + + Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); + + static String getString(String localIp) { + return (String) mappings.getOrDefault(localIp, localIp); + } + + } + + interface traffic_shaping { + Config cfg = net.cfg.getObject("traffic-shaping").toConfig(); + + interface gateway_client { + Config cfg = traffic_shaping.cfg.getObject("gateway-client").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + + interface gateway_server { + Config cfg = traffic_shaping.cfg.getObject("gateway-server").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + + interface connect_server { + Config cfg = traffic_shaping.cfg.getObject("connect-server").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + } + } + + interface security { + + Config cfg = mp.cfg.getObject("security").toConfig(); + + int aes_key_length = cfg.getInt("aes-key-length"); + + String public_key = cfg.getString("public-key"); + + String private_key = cfg.getString("private-key"); + + int ras_key_length = cfg.getInt("ras-key-length"); + + } + + interface thread { + + Config cfg = mp.cfg.getObject("thread").toConfig(); + + interface pool { + + Config cfg = thread.cfg.getObject("pool").toConfig(); + + interface boss { + Config cfg = pool.cfg.getObject("boss").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface work { + Config cfg = pool.cfg.getObject("work").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface event_bus { + Config cfg = pool.cfg.getObject("event-bus").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface http_proxy { + Config cfg = pool.cfg.getObject("http-proxy").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface biz { + Config cfg = pool.cfg.getObject("biz").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface mq { + Config cfg = pool.cfg.getObject("mq").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface push_callback { + Config cfg = pool.cfg.getObject("push-callback").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + } + + } + + interface zk { + + Config cfg = mp.cfg.getObject("zk").toConfig(); + + int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS); + + String local_cache_path = cfg.getString("local-cache-path"); + + int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS); + + String namespace = cfg.getString("namespace"); + + String digest = cfg.getString("digest"); + + String server_address = cfg.getString("server-address"); + + interface retry { + + Config cfg = zk.cfg.getObject("retry").toConfig(); + + int maxRetries = cfg.getInt("maxRetries"); + + int baseSleepTimeMs = (int) cfg.getDuration("baseSleepTimeMs", TimeUnit.MILLISECONDS); + + int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS); + } + + } + + interface redis { + Config cfg = mp.cfg.getObject("redis").toConfig(); + + boolean write_to_zk = cfg.getBoolean("write-to-zk"); + + List cluster_group = cfg.getList("cluster-group") + .stream() + .map(v -> new RedisGroup(ConfigList.class.cast(v) + .stream() + .map(cv -> create(ConfigObject.class.cast(cv).toConfig(), RedisServer.class)) + .collect(toCollection(ArrayList::new)) + ) + ) + .collect(toCollection(ArrayList::new)); + + interface config { + + Config cfg = redis.cfg.getObject("config").toConfig(); + + boolean jmxEnabled = cfg.getBoolean("jmxEnabled"); + + int minIdle = cfg.getInt("minIdle"); + + boolean testOnReturn = cfg.getBoolean("testOnReturn"); + + long softMinEvictableIdleTimeMillis = cfg.getDuration("softMinEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); + + boolean testOnBorrow = cfg.getBoolean("testOnBorrow"); + + boolean testWhileIdle = cfg.getBoolean("testWhileIdle"); + + long maxWaitMillis = cfg.getDuration("maxWaitMillis", TimeUnit.MILLISECONDS); + + String jmxNameBase = cfg.getString("jmxNameBase"); + + int numTestsPerEvictionRun = (int) cfg.getDuration("numTestsPerEvictionRun", TimeUnit.MILLISECONDS); + + String jmxNamePrefix = cfg.getString("jmxNamePrefix"); + + long minEvictableIdleTimeMillis = cfg.getDuration("minEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); + + boolean blockWhenExhausted = cfg.getBoolean("blockWhenExhausted"); + + boolean fairness = cfg.getBoolean("fairness"); + + long timeBetweenEvictionRunsMillis = cfg.getDuration("timeBetweenEvictionRunsMillis", TimeUnit.MILLISECONDS); + + boolean testOnCreate = cfg.getBoolean("testOnCreate"); + + int maxIdle = cfg.getInt("maxIdle"); + + boolean lifo = cfg.getBoolean("lifo"); + + int maxTotal = cfg.getInt("maxTotal"); + + } + + } + + interface http { + + Config cfg = mp.cfg.getObject("http").toConfig(); + boolean proxy_enabled = cfg.getBoolean("proxy-enabled"); + int default_read_timeout = (int) cfg.getDuration("default-read-timeout", TimeUnit.MILLISECONDS); + int max_conn_per_host = cfg.getInt("max-conn-per-host"); + + + long max_content_length = cfg.getBytes("max-content-length"); + + Map> dns_mapping = loadMapping(); + + static Map> loadMapping() { + Map> map = new HashMap<>(); + cfg.getObject("dns-mapping").forEach((s, v) -> + map.put(s, ConfigList.class.cast(v) + .stream() + .map(cv -> DnsMapping.parse((String) cv.unwrapped())) + .collect(toCollection(ArrayList::new)) + ) + ); + return map; + } + + } + + interface monitor { + Config cfg = mp.cfg.getObject("monitor").toConfig(); + String dump_dir = cfg.getString("dump-dir"); + boolean dump_stack = cfg.getBoolean("dump-stack"); + boolean print_log = cfg.getBoolean("print-log"); + Duration dump_period = cfg.getDuration("dump-period"); + } + + interface spi { + Config cfg = mp.cfg.getObject("spi").toConfig(); + String thread_pool_factory = cfg.getString("thread-pool-factory"); + String dns_mapping_manager = cfg.getString("dns-mapping-manager"); + } + } +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java deleted file mode 100644 index 29d7588a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.mpush.tools.config; - - -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.dns.DnsMapping; -import org.aeonbits.owner.Config; -import org.aeonbits.owner.Config.Sources; -import org.aeonbits.owner.ConfigFactory; - -import java.util.List; -import java.util.Map; - -/** - * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 - */ -@Sources({ - "classpath:config.properties", - "file:/${user.dir}/config.properties" -}) -public interface ConfigCenter extends Config { - - ConfigCenter I = ConfigFactory.create(ConfigCenter.class); - - /** - * 最大包长度 - * - * @return - */ - @Key("max_packet_size") - @DefaultValue("10240") - int maxPacketSize(); - - /** - * 包启用压缩特性阈值 - * - * @return - */ - @Key("compress_limit") - @DefaultValue("1024") - int compressLimit(); - - /** - * 最小心跳间隔 10s - * - * @return - */ - @Key("min_heartbeat") - @DefaultValue("10000") - int minHeartbeat(); - - /** - * 最大心跳间隔 10s - * - * @return - */ - @Key("max_heartbeat") - @DefaultValue("180000") - int maxHeartbeat(); - - /** - * 最大心跳超时次数 - * - * @return - */ - @Key("max_hb_timeout_times") - @DefaultValue("2") - int maxHBTimeoutTimes(); - - /** - * 快速重连session超时时间 - * - * @return - */ - @Key("session_expired_time") - @DefaultValue("86400") - int sessionExpiredTime(); - - /** - * RSA密钥长度 - * - * @return - */ - @Key("ras_key_length") - @DefaultValue("1024") - int rsaKeyLength(); - - /** - * AES密钥长度 - * - * @return - */ - @Key("aes_key_length") - @DefaultValue("16") - int aesKeyLength(); - - /** - * 长连接服务端口 - * - * @return - */ - @Key("connection_server_port") - @DefaultValue("3000") - int connectionServerPort(); - - /** - * 网关服务端口 - * - * @return - */ - @Key("gateway_server_port") - @DefaultValue("4000") - int gatewayServerPort(); - - - /** - * 控制台服务端口 - * - * @return - */ - @Key("admin_port") - @DefaultValue("4001") - int adminPort(); - - /** - * RSA私钥 - * - * @return - */ - @Key("private_key") - @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") - String privateKey(); - - /** - * RSA公钥 - * - * @return - */ - @Key("public_key") - @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") - String publicKey(); - - /** - * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd - * 多台机器用“,”分割 - * - * @return - */ - @Deprecated - @Key("redis_ip") - @DefaultValue("127.0.0.1:6379:ShineMoIpo") - String redisIp(); - - /** - * zookeeper机器,格式ip:port - * - * @return - */ - @Key("zk_ip") - @DefaultValue("127.0.0.1:2181") - String zkIp(); - - /** - * zookeeper 空间 - * - * @return - */ - @Key("zk_namespace") - @DefaultValue("mpush") - String zkNamespace(); - - /** - * zookeeper 权限密码 - * - * @return - */ - @Key("zk_digest") - @DefaultValue("shinemoIpo") - String zkDigest(); - - /** - * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd - * 多台机器用“;”分割 - * - * @return - */ - @Separator(";") - @Key("redis_group") - @ConverterClass(RedisGroupConverter.class) - List redisGroups(); - - /** - * 自动把配置的redis机器集群写入到zk - * - * @return - */ - @Key("force_write_redis_group_info") - boolean forceWriteRedisGroupInfo(); - - @Key("scan_conn_task_cycle") - @DefaultValue("59000") - long scanConnTaskCycle(); - - @Key("jvm_log_path") - @DefaultValue("/opt/shinemo/mpush/") - String logPath(); - - @Key("http_proxy_enable") - @DefaultValue("false") - boolean httpProxyEnable(); - - @Key("dns_mapping") - @ConverterClass(DnsMappingConverter.class) - Map> dnsMapping(); - - @Key("max_http_client_conn_count_per_host") - @DefaultValue("5") - int maxHttpConnCountPerHost(); - - //10s - @Key("http_default_read_timeout") - @DefaultValue("10000") - int httpDefaultReadTimeout(); - - @Key("online_and_offline_listener_ip") - @DefaultValue("127.0.0.1") - String onlineAndOfflineListenerIp(); - - @Key("skip_dump") - @DefaultValue("true") - boolean skipDump(); - - /** - * 本机IP到外网Ip的映射 格式localIp:remoteIp,localIp:remoteIp - * - * @return - */ - @Key("remote_ip_mapping") - @ConverterClass(MapConverter.class) - Map remoteIpMapping(); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java new file mode 100644 index 00000000..7f305bf3 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -0,0 +1,55 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config; + +import com.mpush.tools.Utils; + +import static com.mpush.tools.Utils.getInetAddress; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ConfigManager { + public static final ConfigManager I = new ConfigManager(); + + private ConfigManager() { + } + + public int getHeartbeat(int min, int max) { + return Math.max( + CC.mp.core.min_heartbeat, + Math.min(max, CC.mp.core.max_heartbeat) + ); + } + + public String getLocalIp() { + return Utils.getLocalIp(); + } + + public String getPublicIp() { + String localIp = getInetAddress(); + + String remoteIp = CC.mp.net.public_ip_mapping.getString(localIp); + + return remoteIp; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java deleted file mode 100644 index 7696bbbb..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.mpush.tools.config; - -import com.google.common.base.Function; -import com.google.common.base.Splitter; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.primitives.Ints; -import com.mpush.tools.dns.DnsMapping; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public class DnsMappingConverter implements Converter>>{ - - private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); - - @Override - public Map> convert(Method method, String input) { - - log.warn("method:"+method.getName()+","+input); - Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); - Map> result = Maps.newConcurrentMap(); - for (Map.Entry entry : map.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - List dnsMappings = Lists.transform(Arrays.asList(value.split(",")), new Function() { - @Override - public DnsMapping apply(String ipAndPort) { - if(ipAndPort.contains(":")){ - String[] temp = ipAndPort.split(":"); - return new DnsMapping(temp[0], Ints.tryParse(temp[1])); - }else{ - return new DnsMapping(ipAndPort, 80); - } - } - }); - result.put(key, dnsMappings); - } - return result; - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java deleted file mode 100644 index f16e05a2..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.mpush.tools.config; - -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Map; - -/** - * Created by yxx on 2016/5/12. - * - * @author ohun@live.cn - */ -public class MapConverter implements Converter> { - private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); - - @Override - public Map convert(Method method, String input) { - log.warn("method:" + method.getName() + "," + input); - if (Strings.isNullOrEmpty(input)) return Collections.emptyMap(); - return Splitter.on(',').withKeyValueSeparator(':').split(input); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java deleted file mode 100644 index 7ca15bc0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.mpush.tools.config; - -import java.lang.reflect.Method; - -import com.mpush.tools.redis.RedisGroup; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.redis.RedisNode; - -public class RedisGroupConverter implements Converter{ - - private static final Logger log = LoggerFactory.getLogger(RedisGroupConverter.class); - - @Override - public RedisGroup convert(Method method, String input) { - - log.warn("method:"+method.getName()+","+input); - - - RedisGroup group = new RedisGroup(); - - String[] chunks = input.split(","); - for (String chunk : chunks) { - String[] entry = chunk.split(":"); - String ip = entry[0].trim(); - String port = entry[1].trim(); - // 如果配置了redis密码(redis_group = 111.1.57.148:6379:ShineMoIpo)才设置密码 - // 否则密码为空,JedisPool可以兼容两种情况 - String password = null; - if(entry.length >=3){ - password = entry[2].trim(); - } - RedisNode node = new RedisNode(ip, Integer.parseInt(port), password); - group.addRedisNode(node); - } - return group; - } - - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java new file mode 100644 index 00000000..30369c96 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config.data; + +import java.util.Collections; +import java.util.List; + + +/** + * redis 组 + */ +public class RedisGroup { + public List redisNodeList = Collections.emptyList(); + + public RedisGroup() { + } + + public RedisGroup(List redisNodeList) { + this.redisNodeList = redisNodeList; + } + + @Override + public String toString() { + return "RedisGroup{" + + "redisNodeList=" + redisNodeList + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java new file mode 100644 index 00000000..451efdf6 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java @@ -0,0 +1,90 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config.data; + +/** + * redis 相关的配置信息 + */ +public class RedisServer { + public String host; + public int port; + public String password; + + public RedisServer() { + } + + public RedisServer(String host, int port, String password) { + this.host = host; + this.port = port; + this.password = password; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + RedisServer server = (RedisServer) o; + + if (port != server.port) return false; + return host.equals(server.host); + + } + + @Override + public int hashCode() { + int result = host.hashCode(); + result = 31 * result + port; + return result; + } + + @Override + public String toString() { + return "RedisServer{" + + "host='" + host + '\'' + + ", port=" + port + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index bc9eb644..80050e8f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import org.slf4j.Logger; @@ -9,6 +28,7 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; +import java.util.Arrays; /** * Created by ohun on 2015/12/25. @@ -39,26 +59,38 @@ public static SecretKey getSecretKey(byte[] seed) throws Exception { public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); + return encrypt(data, zeroIv, key); + } + + public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + return decrypt(data, zeroIv, key); + } + + public static byte[] encrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); - cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); + cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { - LOGGER.error("encrypt ex, decryptKey=" + encryptKey, e); - throw new RuntimeException("AES encrypt ex", e); + LOGGER.error("AES encrypt ex, iv={}, key={}", + Arrays.toString(zeroIv.getIV()), + Arrays.toString(keySpec.getEncoded()), e); + throw new CryptoException("AES encrypt ex", e); } } - public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { - IvParameterSpec zeroIv = new IvParameterSpec(iv); - SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + public static byte[] decrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); - cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); + cipher.init(Cipher.DECRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { - LOGGER.error("decrypt ex, decryptKey=" + decryptKey, e); - throw new RuntimeException("AES decrypt ex", e); + LOGGER.error("AES decrypt ex, iv={}, key={}", + Arrays.toString(zeroIv.getIV()), + Arrays.toString(keySpec.getEncoded()), e); + throw new CryptoException("AES decrypt ex", e); } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java index 207fd55e..370c72f4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java @@ -22,12 +22,11 @@ * * */ - package com.mpush.tools.crypto; import java.io.FilterOutputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -41,53 +40,54 @@ * as specified in * RFC 4648 and * RFC 2045. - * + *

*

    *
  • Basic *

    Uses "The Base64 Alphabet" as specified in Table 1 of - * RFC 4648 and RFC 2045 for encoding and decoding operation. - * The encoder does not add any line feed (line separator) - * character. The decoder rejects data that contains characters - * outside the base64 alphabet.

  • - * + * RFC 4648 and RFC 2045 for encoding and decoding operation. + * The encoder does not add any line feed (line separator) + * character. The decoder rejects data that contains characters + * outside the base64 alphabet.

    + *

    *

  • URL and Filename safe *

    Uses the "URL and Filename safe Base64 Alphabet" as specified - * in Table 2 of RFC 4648 for encoding and decoding. The - * encoder does not add any line feed (line separator) character. - * The decoder rejects data that contains characters outside the - * base64 alphabet.

  • - * + * in Table 2 of RFC 4648 for encoding and decoding. The + * encoder does not add any line feed (line separator) character. + * The decoder rejects data that contains characters outside the + * base64 alphabet.

    + *

    *

  • MIME *

    Uses the "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045 for encoding and decoding operation. The encoded output - * must be represented in lines of no more than 76 characters each - * and uses a carriage return {@code '\r'} followed immediately by - * a linefeed {@code '\n'} as the line separator. No line separator - * is added to the end of the encoded output. All line separators - * or other characters not found in the base64 alphabet table are - * ignored in decoding operation.

  • + * RFC 2045 for encoding and decoding operation. The encoded output + * must be represented in lines of no more than 76 characters each + * and uses a carriage return {@code '\r'} followed immediately by + * a linefeed {@code '\n'} as the line separator. No line separator + * is added to the end of the encoded output. All line separators + * or other characters not found in the base64 alphabet table are + * ignored in decoding operation.

    *
- * + *

*

Unless otherwise noted, passing a {@code null} argument to a * method of this class will cause a {@link java.lang.NullPointerException * NullPointerException} to be thrown. * - * @author Xueming Shen - * @since 1.8 + * @author Xueming Shen + * @since 1.8 */ public class Base64 { - private Base64() {} + private Base64() { + } /** * Returns a {@link Encoder} that encodes using the * Basic type base64 encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getEncoder() { - return Encoder.RFC4648; + return Encoder.RFC4648; } /** @@ -95,17 +95,17 @@ public static Encoder getEncoder() { * URL and Filename safe type base64 * encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getUrlEncoder() { - return Encoder.RFC4648_URLSAFE; + return Encoder.RFC4648_URLSAFE; } /** * Returns a {@link Encoder} that encodes using the * MIME type base64 encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getMimeEncoder() { return Encoder.RFC2045; @@ -116,41 +116,37 @@ public static Encoder getMimeEncoder() { * MIME type base64 encoding scheme * with specified line length and line separators. * - * @param lineLength - * the length of each output line (rounded down to nearest multiple - * of 4). If {@code lineLength <= 0} the output will not be separated - * in lines - * @param lineSeparator - * the line separator for each output line - * - * @return A Base64 encoder. - * - * @throws IllegalArgumentException if {@code lineSeparator} includes any - * character of "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045. + * @param lineLength the length of each output line (rounded down to nearest multiple + * of 4). If {@code lineLength <= 0} the output will not be separated + * in lines + * @param lineSeparator the line separator for each output line + * @return A Base64 encoder. + * @throws IllegalArgumentException if {@code lineSeparator} includes any + * character of "The Base64 Alphabet" as specified in Table 1 of + * RFC 2045. */ public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) { - Objects.requireNonNull(lineSeparator); - int[] base64 = Decoder.fromBase64; - for (byte b : lineSeparator) { - if (base64[b & 0xff] != -1) - throw new IllegalArgumentException( - "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); - } - if (lineLength <= 0) { - return Encoder.RFC4648; - } - return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); + Objects.requireNonNull(lineSeparator); + int[] base64 = Decoder.fromBase64; + for (byte b : lineSeparator) { + if (base64[b & 0xff] != -1) + throw new IllegalArgumentException( + "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); + } + if (lineLength <= 0) { + return Encoder.RFC4648; + } + return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); } /** * Returns a {@link Decoder} that decodes using the * Basic type base64 encoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getDecoder() { - return Decoder.RFC4648; + return Decoder.RFC4648; } /** @@ -158,36 +154,36 @@ public static Decoder getDecoder() { * URL and Filename safe type base64 * encoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getUrlDecoder() { - return Decoder.RFC4648_URLSAFE; + return Decoder.RFC4648_URLSAFE; } /** * Returns a {@link Decoder} that decodes using the * MIME type base64 decoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getMimeDecoder() { - return Decoder.RFC2045; + return Decoder.RFC2045; } /** * This class implements an encoder for encoding byte data using * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - * + *

*

Instances of {@link Encoder} class are safe for use by * multiple concurrent threads. - * + *

*

Unless otherwise noted, passing a {@code null} argument to * a method of this class will cause a * {@link java.lang.NullPointerException NullPointerException} to * be thrown. * - * @see Decoder - * @since 1.8 + * @see Decoder + * @since 1.8 */ public static class Encoder { @@ -209,11 +205,11 @@ private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648). */ private static final char[] toBase64 = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; /** @@ -222,15 +218,15 @@ private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { * '_'. This table is used when BASE64_URL is specified. */ private static final char[] toBase64URL = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' }; private static final int MIMELINEMAX = 76; - private static final byte[] CRLF = new byte[] {'\r', '\n'}; + private static final byte[] CRLF = new byte[]{'\r', '\n'}; static final Encoder RFC4648 = new Encoder(false, null, -1, true); static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true); @@ -254,17 +250,16 @@ private final int outLength(int srclen) { * byte array using the {@link Base64} encoding scheme. The returned byte * array is of the length of the resulting bytes. * - * @param src - * the byte array to encode - * @return A newly-allocated byte array containing the resulting - * encoded bytes. + * @param src the byte array to encode + * @return A newly-allocated byte array containing the resulting + * encoded bytes. */ public byte[] encode(byte[] src) { int len = outLength(src.length); // dst array size byte[] dst = new byte[len]; int ret = encode0(src, 0, src.length, dst); if (ret != dst.length) - return Arrays.copyOf(dst, ret); + return Arrays.copyOf(dst, ret); return dst; } @@ -272,45 +267,41 @@ public byte[] encode(byte[] src) { * Encodes all bytes from the specified byte array using the * {@link Base64} encoding scheme, writing the resulting bytes to the * given output byte array, starting at offset 0. - * + *

*

It is the responsibility of the invoker of this method to make * sure the output byte array {@code dst} has enough space for encoding * all bytes from the input byte array. No bytes will be written to the * output byte array if the output byte array is not big enough. * - * @param src - * the byte array to encode - * @param dst - * the output byte array - * @return The number of bytes written to the output byte array - * - * @throws IllegalArgumentException if {@code dst} does not have enough - * space for encoding all input bytes. + * @param src the byte array to encode + * @param dst the output byte array + * @return The number of bytes written to the output byte array + * @throws IllegalArgumentException if {@code dst} does not have enough + * space for encoding all input bytes. */ public int encode(byte[] src, byte[] dst) { int len = outLength(src.length); // dst array size if (dst.length < len) throw new IllegalArgumentException( - "Output byte array is too small for encoding all input bytes"); + "Output byte array is too small for encoding all input bytes"); return encode0(src, 0, src.length, dst); } /** * Encodes the specified byte array into a String using the {@link Base64} * encoding scheme. - * + *

*

This method first encodes all input bytes into a base64 encoded * byte array and then constructs a new String by using the encoded byte * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1 * ISO-8859-1} charset. - * + *

*

In other words, an invocation of this method has exactly the same * effect as invoking * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}. * - * @param src - * the byte array to encode - * @return A String containing the resulting Base64 encoded characters + * @param src the byte array to encode + * @return A String containing the resulting Base64 encoded characters */ @SuppressWarnings("deprecation") public String encodeToString(byte[] src) { @@ -322,15 +313,14 @@ public String encodeToString(byte[] src) { * Encodes all remaining bytes from the specified byte buffer into * a newly-allocated ByteBuffer using the {@link Base64} encoding * scheme. - * + *

* Upon return, the source buffer's position will be updated to * its limit; its limit will not have been changed. The returned * output buffer's position will be zero and its limit will be the * number of resulting encoded bytes. * - * @param buffer - * the source ByteBuffer to encode - * @return A newly-allocated byte buffer containing the encoded bytes. + * @param buffer the source ByteBuffer to encode + * @return A newly-allocated byte buffer containing the encoded bytes. */ public ByteBuffer encode(ByteBuffer buffer) { int len = outLength(buffer.remaining()); @@ -338,9 +328,9 @@ public ByteBuffer encode(ByteBuffer buffer) { int ret = 0; if (buffer.hasArray()) { ret = encode0(buffer.array(), - buffer.arrayOffset() + buffer.position(), - buffer.arrayOffset() + buffer.limit(), - dst); + buffer.arrayOffset() + buffer.position(), + buffer.arrayOffset() + buffer.limit(), + dst); buffer.position(buffer.limit()); } else { byte[] src = new byte[buffer.remaining()]; @@ -348,41 +338,40 @@ public ByteBuffer encode(ByteBuffer buffer) { ret = encode0(src, 0, src.length, dst); } if (ret != dst.length) - dst = Arrays.copyOf(dst, ret); + dst = Arrays.copyOf(dst, ret); return ByteBuffer.wrap(dst); } /** * Wraps an output stream for encoding byte data using the {@link Base64} * encoding scheme. - * + *

*

It is recommended to promptly close the returned output stream after * use, during which it will flush all possible leftover bytes to the underlying * output stream. Closing the returned output stream will close the underlying * output stream. * - * @param os - * the output stream. - * @return the output stream for encoding the byte data into the - * specified Base64 encoded format + * @param os the output stream. + * @return the output stream for encoding the byte data into the + * specified Base64 encoded format */ public OutputStream wrap(OutputStream os) { Objects.requireNonNull(os); return new EncOutputStream(os, isURL ? toBase64URL : toBase64, - newline, linemax, doPadding); + newline, linemax, doPadding); } /** * Returns an encoder instance that encodes equivalently to this one, * but without adding any padding character at the end of the encoded * byte data. - * + *

*

The encoding scheme of this encoder instance is unaffected by * this invocation. The returned encoder instance should be used for * non-padding encoding operation. * * @return an equivalent encoder that encodes without adding any - * padding character at the end + * padding character at the end */ public Encoder withoutPadding() { if (!doPadding) @@ -395,42 +384,42 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { int sp = off; int slen = (end - off) / 3 * 3; int sl = off + slen; - if (linemax > 0 && slen > linemax / 4 * 3) + if (linemax > 0 && slen > linemax / 4 * 3) slen = linemax / 4 * 3; int dp = 0; while (sp < sl) { int sl0 = Math.min(sp + slen, sl); - for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) { + for (int sp0 = sp, dp0 = dp; sp0 < sl0; ) { int bits = (src[sp0++] & 0xff) << 16 | - (src[sp0++] & 0xff) << 8 | - (src[sp0++] & 0xff); - dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f]; - dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f]; - dst[dp0++] = (byte)base64[(bits >>> 6) & 0x3f]; - dst[dp0++] = (byte)base64[bits & 0x3f]; + (src[sp0++] & 0xff) << 8 | + (src[sp0++] & 0xff); + dst[dp0++] = (byte) base64[(bits >>> 18) & 0x3f]; + dst[dp0++] = (byte) base64[(bits >>> 12) & 0x3f]; + dst[dp0++] = (byte) base64[(bits >>> 6) & 0x3f]; + dst[dp0++] = (byte) base64[bits & 0x3f]; } int dlen = (sl0 - sp) / 3 * 4; dp += dlen; sp = sl0; if (dlen == linemax && sp < end) { - for (byte b : newline){ + for (byte b : newline) { dst[dp++] = b; } } } if (sp < end) { // 1 or 2 leftover bytes int b0 = src[sp++] & 0xff; - dst[dp++] = (byte)base64[b0 >> 2]; + dst[dp++] = (byte) base64[b0 >> 2]; if (sp == end) { - dst[dp++] = (byte)base64[(b0 << 4) & 0x3f]; + dst[dp++] = (byte) base64[(b0 << 4) & 0x3f]; if (doPadding) { dst[dp++] = '='; dst[dp++] = '='; } } else { int b1 = src[sp++] & 0xff; - dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; - dst[dp++] = (byte)base64[(b1 << 2) & 0x3f]; + dst[dp++] = (byte) base64[(b0 << 4) & 0x3f | (b1 >> 4)]; + dst[dp++] = (byte) base64[(b1 << 2) & 0x3f]; if (doPadding) { dst[dp++] = '='; } @@ -443,7 +432,7 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { /** * This class implements a decoder for decoding byte data using the * Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - * + *

*

The Base64 padding character {@code '='} is accepted and * interpreted as the end of the encoded byte data, but is not * required. So if the final unit of the encoded byte data only has @@ -454,17 +443,17 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { * present, otherwise {@code IllegalArgumentException} ( * {@code IOException} when reading from a Base64 stream) is thrown * during decoding. - * + *

*

Instances of {@link Decoder} class are safe for use by * multiple concurrent threads. - * + *

*

Unless otherwise noted, passing a {@code null} argument to * a method of this class will cause a * {@link java.lang.NullPointerException NullPointerException} to * be thrown. * - * @see Encoder - * @since 1.8 + * @see Encoder + * @since 1.8 */ public static class Decoder { @@ -482,9 +471,9 @@ private Decoder(boolean isURL, boolean isMIME) { * their 6-bit positive integer equivalents. Characters that * are not in the Base64 alphabet but fall within the bounds of * the array are encoded to -1. - * */ private static final int[] fromBase64 = new int[256]; + static { Arrays.fill(fromBase64, -1); for (int i = 0; i < Encoder.toBase64.length; i++) @@ -505,9 +494,9 @@ private Decoder(boolean isURL, boolean isMIME) { fromBase64URL['='] = -2; } - static final Decoder RFC4648 = new Decoder(false, false); + static final Decoder RFC4648 = new Decoder(false, false); static final Decoder RFC4648_URLSAFE = new Decoder(true, false); - static final Decoder RFC2045 = new Decoder(false, true); + static final Decoder RFC2045 = new Decoder(false, true); /** * Decodes all bytes from the input byte array using the {@link Base64} @@ -515,13 +504,9 @@ private Decoder(boolean isURL, boolean isMIME) { * byte array. The returned byte array is of the length of the resulting * bytes. * - * @param src - * the byte array to decode - * - * @return A newly-allocated byte array containing the decoded bytes. - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme + * @param src the byte array to decode + * @return A newly-allocated byte array containing the decoded bytes. + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme */ public byte[] decode(byte[] src) { byte[] dst = new byte[outLength(src, 0, src.length)]; @@ -535,17 +520,13 @@ public byte[] decode(byte[] src) { /** * Decodes a Base64 encoded String into a newly-allocated byte array * using the {@link Base64} encoding scheme. - * + *

*

An invocation of this method has exactly the same effect as invoking * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))} * - * @param src - * the string to decode - * - * @return A newly-allocated byte array containing the decoded bytes. - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme + * @param src the string to decode + * @return A newly-allocated byte array containing the decoded bytes. + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme */ public byte[] decode(String src) { return decode(src.getBytes(StandardCharsets.ISO_8859_1)); @@ -555,55 +536,46 @@ public byte[] decode(String src) { * Decodes all bytes from the input byte array using the {@link Base64} * encoding scheme, writing the results into the given output byte array, * starting at offset 0. - * + *

*

It is the responsibility of the invoker of this method to make * sure the output byte array {@code dst} has enough space for decoding * all bytes from the input byte array. No bytes will be be written to * the output byte array if the output byte array is not big enough. - * + *

*

If the input byte array is not in valid Base64 encoding scheme * then some bytes may have been written to the output byte array before * IllegalargumentException is thrown. * - * @param src - * the byte array to decode - * @param dst - * the output byte array - * - * @return The number of bytes written to the output byte array - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme, or {@code dst} - * does not have enough space for decoding all input bytes. + * @param src the byte array to decode + * @param dst the output byte array + * @return The number of bytes written to the output byte array + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme, or {@code dst} + * does not have enough space for decoding all input bytes. */ public int decode(byte[] src, byte[] dst) { int len = outLength(src, 0, src.length); if (dst.length < len) throw new IllegalArgumentException( - "Output byte array is too small for decoding all input bytes"); + "Output byte array is too small for decoding all input bytes"); return decode0(src, 0, src.length, dst); } /** * Decodes all bytes from the input byte buffer using the {@link Base64} * encoding scheme, writing the results into a newly-allocated ByteBuffer. - * + *

*

Upon return, the source buffer's position will be updated to * its limit; its limit will not have been changed. The returned * output buffer's position will be zero and its limit will be the * number of resulting decoded bytes - * + *

*

{@code IllegalArgumentException} is thrown if the input buffer * is not in valid Base64 encoding scheme. The position of the input * buffer will not be advanced in this case. * - * @param buffer - * the ByteBuffer to decode - * - * @return A newly-allocated byte buffer containing the decoded bytes - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme. + * @param buffer the ByteBuffer to decode + * @return A newly-allocated byte buffer containing the decoded bytes + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme. */ public ByteBuffer decode(ByteBuffer buffer) { int pos0 = buffer.position(); @@ -631,18 +603,16 @@ public ByteBuffer decode(ByteBuffer buffer) { /** * Returns an input stream for decoding {@link Base64} encoded byte stream. - * + *

*

The {@code read} methods of the returned {@code InputStream} will * throw {@code IOException} when reading bytes that cannot be decoded. - * + *

*

Closing the returned input stream will close the underlying * input stream. * - * @param is - * the input stream - * - * @return the input stream for decoding the specified Base64 encoded - * byte stream + * @param is the input stream + * @return the input stream for decoding the specified Base64 encoded + * byte stream */ public InputStream wrap(InputStream is) { Objects.requireNonNull(is); @@ -659,7 +629,7 @@ private int outLength(byte[] src, int sp, int sl) { if (isMIME && base64[0] == -1) return 0; throw new IllegalArgumentException( - "Input byte[] should at least have 2 bytes for base64 bytes"); + "Input byte[] should at least have 2 bytes for base64 bytes"); } if (isMIME) { // scan all bytes to fill out all non-alphabet. a performance @@ -682,7 +652,7 @@ private int outLength(byte[] src, int sp, int sl) { paddings++; } } - if (paddings == 0 && (len & 0x3) != 0) + if (paddings == 0 && (len & 0x3) != 0) paddings = 4 - (len & 0x3); return 3 * ((len + 3) / 4) - paddings; } @@ -702,9 +672,9 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { // xx= shiftto==6&&sp==sl missing last = // xx=y shiftto==6 last is not = if (shiftto == 6 && (sp == sl || src[sp++] != '=') || - shiftto == 18) { + shiftto == 18) { throw new IllegalArgumentException( - "Input byte array has wrong 4-byte ending unit"); + "Input byte array has wrong 4-byte ending unit"); } break; } @@ -712,29 +682,29 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { continue; else throw new IllegalArgumentException( - "Illegal base64 character " + - Integer.toString(src[sp - 1], 16)); + "Illegal base64 character " + + Integer.toString(src[sp - 1], 16)); } bits |= (b << shiftto); shiftto -= 6; if (shiftto < 0) { - dst[dp++] = (byte)(bits >> 16); - dst[dp++] = (byte)(bits >> 8); - dst[dp++] = (byte)(bits); + dst[dp++] = (byte) (bits >> 16); + dst[dp++] = (byte) (bits >> 8); + dst[dp++] = (byte) (bits); shiftto = 18; bits = 0; } } // reached end of byte array or hit padding '=' characters. if (shiftto == 6) { - dst[dp++] = (byte)(bits >> 16); + dst[dp++] = (byte) (bits >> 16); } else if (shiftto == 0) { - dst[dp++] = (byte)(bits >> 16); - dst[dp++] = (byte)(bits >> 8); + dst[dp++] = (byte) (bits >> 16); + dst[dp++] = (byte) (bits >> 8); } else if (shiftto == 12) { // dangling single "x", incorrectly encoded. throw new IllegalArgumentException( - "Last unit does not have enough valid bits"); + "Last unit does not have enough valid bits"); } // anything left is invalid, if is not MIME. // if MIME, ignore all non-base64 character @@ -742,7 +712,7 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { if (isMIME && base64[src[sp++]] < 0) continue; throw new IllegalArgumentException( - "Input byte array has incorrect ending byte at " + sp); + "Input byte array has incorrect ending byte at " + sp); } return dp; } @@ -775,7 +745,7 @@ private static class EncOutputStream extends FilterOutputStream { @Override public void write(int b) throws IOException { byte[] buf = new byte[1]; - buf[0] = (byte)(b & 0xff); + buf[0] = (byte) (b & 0xff); write(buf, 0, 1); } @@ -817,14 +787,14 @@ public void write(byte[] b, int off, int len) throws IOException { while (nBits24-- > 0) { checkNewline(); int bits = (b[off++] & 0xff) << 16 | - (b[off++] & 0xff) << 8 | - (b[off++] & 0xff); + (b[off++] & 0xff) << 8 | + (b[off++] & 0xff); out.write(base64[(bits >>> 18) & 0x3f]); out.write(base64[(bits >>> 12) & 0x3f]); - out.write(base64[(bits >>> 6) & 0x3f]); + out.write(base64[(bits >>> 6) & 0x3f]); out.write(base64[bits & 0x3f]); linepos += 4; - } + } if (leftover == 1) { b0 = b[off++] & 0xff; } else if (leftover == 2) { @@ -851,7 +821,7 @@ public void close() throws IOException { out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); out.write(base64[(b1 << 2) & 0x3f]); if (doPadding) { - out.write('='); + out.write('='); } } leftover = 0; @@ -870,9 +840,9 @@ private static class DecInputStream extends InputStream { private final int[] base64; // base64 -> byte mapping private int bits = 0; // 24-bit buffer for decoding private int nextin = 18; // next available "off" in "bits" for input; - // -> 18, 12, 6, 0 + // -> 18, 12, 6, 0 private int nextout = -8; // next available "off" in "bits" for output; - // -> 8, 0, -8 (no byte for output) + // -> 8, 0, -8 (no byte for output) private boolean eof = false; private boolean closed = false; @@ -902,7 +872,7 @@ public int read(byte[] b, int off, int len) throws IOException { do { if (len == 0) return off - oldOff; - b[off++] = (byte)(bits >> nextout); + b[off++] = (byte) (bits >> nextout); len--; nextout -= 8; } while (nextout >= 0); @@ -917,14 +887,14 @@ public int read(byte[] b, int off, int len) throws IOException { throw new IOException("Base64 stream has one un-decoded dangling byte."); // treat ending xx/xxx without padding character legal. // same logic as v == '=' below - b[off++] = (byte)(bits >> (16)); + b[off++] = (byte) (bits >> (16)); len--; if (nextin == 0) { // only one padding byte if (len == 0) { // no enough output space bits >>= 8; // shift to lowest byte nextout = 0; } else { - b[off++] = (byte) (bits >> 8); + b[off++] = (byte) (bits >> 8); } } } @@ -939,17 +909,17 @@ public int read(byte[] b, int off, int len) throws IOException { // xx= shiftto==6 && missing last '=' // xx=y or last is not '=' if (nextin == 18 || nextin == 12 || - nextin == 6 && is.read() != '=') { + nextin == 6 && is.read() != '=') { throw new IOException("Illegal base64 ending sequence:" + nextin); } - b[off++] = (byte)(bits >> (16)); + b[off++] = (byte) (bits >> (16)); len--; if (nextin == 0) { // only one padding byte if (len == 0) { // no enough output space bits >>= 8; // shift to lowest byte nextout = 0; } else { - b[off++] = (byte) (bits >> 8); + b[off++] = (byte) (bits >> 8); } } eof = true; @@ -960,14 +930,14 @@ public int read(byte[] b, int off, int len) throws IOException { continue; else throw new IOException("Illegal base64 character " + - Integer.toString(v, 16)); + Integer.toString(v, 16)); } bits |= (v << nextin); if (nextin == 0) { nextin = 18; // clear for next nextout = 16; while (nextout >= 0) { - b[off++] = (byte)(bits >> nextout); + b[off++] = (byte) (bits >> nextout); len--; nextout -= 8; if (len == 0 && nextout >= 0) { // don't clean "bits" diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index c7280a07..3c2f1393 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -1,25 +1,29 @@ -package com.mpush.tools.crypto; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.crypto; -import com.mpush.tools.Constants; -import java.io.*; +import com.mpush.api.Constants; -/** - *

- * BASE64编码解码工具包 - *

- *

- * 依赖javabase64-1.3.1.jar - *

- */ public class Base64Utils { - /** - * 文件读取缓冲区大小 - */ - private static final int CACHE_SIZE = 1024; - /** *

* BASE64字符串解码为二进制数据 @@ -30,7 +34,7 @@ public class Base64Utils { * @throws Exception */ public static byte[] decode(String base64) throws Exception { - return Base64.getDecoder().decode(base64); + return Base64.getDecoder().decode(base64.getBytes(Constants.UTF_8)); } /** @@ -46,90 +50,4 @@ public static String encode(byte[] bytes) throws Exception { return new String(Base64.getEncoder().encode(bytes), Constants.UTF_8); } - /** - *

- * 将文件编码为BASE64字符串 - *

- *

- * 大文件慎用,可能会导致内存溢出 - *

- * - * @param filePath 文件绝对路径 - * @return - * @throws Exception - */ - public static String encodeFile(String filePath) throws Exception { - byte[] bytes = fileToByte(filePath); - return encode(bytes); - } - - /** - *

- * BASE64字符串转回文件 - *

- * - * @param filePath 文件绝对路径 - * @param base64 编码字符串 - * @throws Exception - */ - public static void decodeToFile(String filePath, String base64) throws Exception { - byte[] bytes = decode(base64); - byteArrayToFile(bytes, filePath); - } - - /** - *

- * 文件转换为二进制数组 - *

- * - * @param filePath 文件路径 - * @return - * @throws Exception - */ - public static byte[] fileToByte(String filePath) throws Exception { - byte[] data = new byte[0]; - File file = new File(filePath); - if (file.exists()) { - FileInputStream in = new FileInputStream(file); - ByteArrayOutputStream out = new ByteArrayOutputStream(2048); - byte[] cache = new byte[CACHE_SIZE]; - int nRead = 0; - while ((nRead = in.read(cache)) != -1) { - out.write(cache, 0, nRead); - out.flush(); - } - out.close(); - in.close(); - data = out.toByteArray(); - } - return data; - } - - /** - *

- * 二进制数据写文件 - *

- * - * @param bytes 二进制数据 - * @param filePath 文件生成目录 - */ - public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { - InputStream in = new ByteArrayInputStream(bytes); - File destFile = new File(filePath); - if (!destFile.getParentFile().exists()) { - destFile.getParentFile().mkdirs(); - } - destFile.createNewFile(); - OutputStream out = new FileOutputStream(destFile); - byte[] cache = new byte[CACHE_SIZE]; - int nRead = 0; - while ((nRead = in.read(cache)) != -1) { - out.write(cache, 0, nRead); - out.flush(); - } - out.close(); - in.close(); - } - - } \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java new file mode 100644 index 00000000..b7a39da8 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java @@ -0,0 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.crypto; + +/** + * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn + */ +public class CryptoException extends RuntimeException { + + private static final long serialVersionUID = 368277451733324220L; + + public CryptoException(String message) { + super(message); + } + + public CryptoException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index 02b2ff17..536f9ba8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; -import com.mpush.tools.Constants; -import com.mpush.tools.IOUtils; -import com.mpush.tools.Strings; +import com.mpush.api.Constants; +import com.mpush.tools.common.IOUtils; +import com.mpush.tools.common.Strings; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index e3201180..b1edd6db 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; -import com.mpush.tools.Constants; -import com.mpush.tools.Pair; +import com.mpush.api.Constants; +import com.mpush.tools.common.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -169,7 +188,7 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { LOGGER.error("getPublicKey ex modulus={}, exponent={}", modulus, exponent, e); - throw new RuntimeException("Get PublicKey ex", e); + throw new CryptoException("Get PublicKey ex", e); } } @@ -192,7 +211,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { LOGGER.error("getPrivateKey ex modulus={}, exponent={}", modulus, exponent, e); - throw new RuntimeException("Get PrivateKey ex", e); + throw new CryptoException("Get PrivateKey ex", e); } } @@ -215,7 +234,7 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { return doFinal(cipher, data, key_len - 11); } catch (Exception e) { LOGGER.error("encryptByPublicKey ex", e); - throw new RuntimeException("RSA encrypt ex", e); + throw new CryptoException("RSA encrypt ex", e); } } @@ -237,7 +256,7 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) return doFinal(cipher, data, key_len); } catch (Exception e) { LOGGER.error("decryptByPrivateKey ex", e); - throw new RuntimeException("RSA decrypt ex", e); + throw new CryptoException("RSA decrypt ex", e); } } @@ -347,8 +366,12 @@ public static void main(String[] args) throws Exception { byte[] ming = "123456789".getBytes(Constants.UTF_8); System.out.println("明文:" + new String(ming, Constants.UTF_8)); //使用模和指数生成公钥和私钥 - RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); + RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); + System.out.println("privateKey=" + priKey); + System.out.println("publicKey=" + pubKey); + System.out.println("privateKey=" + priKey); + System.out.println("publicKey=" + pubKey); //加密后的密文 byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey); System.out.println("密文:" + new String(mi, Constants.UTF_8)); diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java deleted file mode 100644 index 6f541b3a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.mpush.tools.dns; - - -public class DnsMapping { - - private String ip; - private int port; - - public DnsMapping(String ip, int port) { - this.ip = ip; - this.port = port; - } - - public String getIp() { - return ip; - } - public int getPort() { - return port; - } - - @Override - public String toString() { - return ip+":"+port; - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java deleted file mode 100644 index 905f4e96..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.mpush.tools.dns.manage; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.DnsMapping; - -public class DnsMappingManage { - - private static final Logger LOG = LoggerFactory.getLogger(DnsMappingManage.class); - - private DnsMappingManage() { - } - - public static final DnsMappingManage holder = new DnsMappingManage(); - - private Map> all = Maps.newConcurrentMap(); - private Map> available = Maps.newConcurrentMap(); - - private Worker worker = new Worker(); - - private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); - - public void init() { - LOG.error("start init dnsMapping"); - all.putAll(ConfigCenter.I.dnsMapping()); - available.putAll(ConfigCenter.I.dnsMapping()); - pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns - LOG.error("end init dnsMapping"); - } - - public void update(Map> nowAvailable) { - available = nowAvailable; - } - - public Map> getAll() { - return all; - } - - public DnsMapping translate(String origin) { - if (available.isEmpty()) - return null; - List list = available.get(origin); - if (list == null || list.isEmpty()) - return null; - int L = list.size(); - if (L == 1) - return list.get(0); - return list.get((int) (Math.random() * L % L)); - } - - public void shutdown() { - pool.shutdown(); - } - - public static class Worker implements Runnable { - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - - log.debug("start dns mapping telnet"); - - Map> all = DnsMappingManage.holder.getAll(); - - Map> available = Maps.newConcurrentMap(); - - for (Map.Entry> entry : all.entrySet()) { - String key = entry.getKey(); - List value = entry.getValue(); - List nowValue = Lists.newArrayList(); - for (DnsMapping temp : value) { - boolean canTelnet = MPushUtil.telnet(temp.getIp(), temp.getPort()); - if (canTelnet) { - nowValue.add(temp); - } else { - log.error("dns can not reachable:" + Jsons.toJson(temp)); - } - } - available.put(key, nowValue); - } - - DnsMappingManage.holder.update(available); - - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/Event.java b/mpush-tools/src/main/java/com/mpush/tools/event/Event.java deleted file mode 100644 index afabfdee..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/Event.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.mpush.tools.event; - -public class Event { - - private final EventType eventType; - private final Object source; - - public Event(final EventType eventType, final Object source) { - this.eventType = eventType; - this.source = source; - } - - public EventType getEventType() { - return eventType; - } - - public Object getSource() { - return source; - } - -} diff --git a/mpush-common/src/main/java/com/mpush/common/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java similarity index 52% rename from mpush-common/src/main/java/com/mpush/common/EventBus.java rename to mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index e27184c4..f547073d 100644 --- a/mpush-common/src/main/java/com/mpush/common/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -1,11 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -package com.mpush.common; +package com.mpush.tools.event; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; import com.mpush.api.event.Event; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,12 +35,12 @@ * @author ohun@live.cn */ public class EventBus { - private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); - public static final EventBus INSTANCE = new EventBus(); + private final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); + public static final EventBus I = new EventBus(); private final com.google.common.eventbus.EventBus eventBus; public EventBus() { - Executor executor = ThreadPoolManager.eventBusExecutor; + Executor executor = ThreadPoolManager.I.getEventBusExecutor(); eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { @@ -35,7 +53,6 @@ public void post(Event event) { eventBus.post(event); } - public void register(Object bean) { eventBus.register(bean); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java new file mode 100644 index 00000000..c7e7ccab --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java @@ -0,0 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.event; + +public abstract class EventConsumer { + + public EventConsumer() { + EventBus.I.register(this); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java deleted file mode 100644 index fbcb19ee..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.tools.event; - -import java.util.ArrayList; -import java.util.List; - -public class EventDispatcher { - - private static final List listeners = new ArrayList(); - - public static void addEventListener(EventListener listener) { - listeners.add(listener); - } - - public static void fireEvent(Event event) { - for (EventListener listener : listeners) { - listener.onEvent(event); - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java deleted file mode 100644 index 5a9b6d82..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.mpush.tools.event; - -public interface EventListener { - - public void onEvent(Event event); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java deleted file mode 100644 index ccd77b3f..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.mpush.tools.event; - -public enum EventType { - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java b/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java deleted file mode 100644 index c5326f53..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mpush.tools.exception; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ZKException extends RuntimeException { - - public ZKException() { - } - - public ZKException(String message) { - super(message); - } - - public ZKException(String message, Throwable cause) { - super(message, cause); - } - - public ZKException(Throwable cause) { - super(cause); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java new file mode 100644 index 00000000..44fb5e5e --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -0,0 +1,60 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.log; + +import com.mpush.tools.config.CC; +import com.typesafe.config.ConfigRenderOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2016/5/16. + * + * @author ohun@live.cn + */ +public interface Logs { + boolean logInit = init(); + + static boolean init() { + if (logInit) return true; + System.setProperty("log.home", CC.mp.log_dir); + System.setProperty("log.root.level", CC.mp.log_level); + LoggerFactory + .getLogger("console") + .info(CC.mp.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true))); + return true; + } + + Logger Console = LoggerFactory.getLogger("console"), + + Conn = LoggerFactory.getLogger("mpush.conn.log"), + + Monitor = LoggerFactory.getLogger("mpush.monitor.log"), + + PUSH = LoggerFactory.getLogger("mpush.push.log"), + + HB = LoggerFactory.getLogger("mpush.heartbeat.log"), + + REDIS = LoggerFactory.getLogger("mpush.redis.log"), + + ZK = LoggerFactory.getLogger("mpush.zk.log"), + + HTTP = LoggerFactory.getLogger("mpush.http.log"); +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java deleted file mode 100644 index 73b69ec3..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.tools.redis; - -import java.util.List; - -import com.google.common.collect.Lists; - - -/** - * redis 组 - * - */ -public class RedisGroup { - - private List redisNodeList; - - public List getRedisNodeList() { - return redisNodeList; - } - - public void setRedisNodeList(List redisNodeList) { - this.redisNodeList = redisNodeList; - } - - public void addRedisNode(RedisNode node){ - if(redisNodeList==null){ - redisNodeList = Lists.newArrayList(); - } - redisNodeList.add(node); - } - - public void remove(int i){ - if(redisNodeList!=null){ - redisNodeList.remove(i); - } - } - - public void clear(){ - if(redisNodeList!=null){ - redisNodeList.clear(); - } - } - - public RedisNode get(String key){ - int i = key.hashCode() %redisNodeList.size(); - return redisNodeList.get(i); - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java deleted file mode 100644 index 82f3de80..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.mpush.tools.redis; - -/** - * redis 相关的配置信息 - * - */ -public class RedisNode { - - private String ip; - private int port; - private String password; - public String getIp() { - return ip; - } - public void setIp(String ip) { - this.ip = ip; - } - public int getPort() { - return port; - } - public void setPort(int port) { - this.port = port; - } - public String getPassword() { - return password; - } - public void setPassword(String password) { - this.password = password; - } - - public RedisNode(String ip, int port, String password) { - this.ip = ip; - this.port = port; - this.password = password; - } - @Override - public String toString() { - return "RedisNode [ip=" + ip + ", port=" + port + ", password=" + password + "]"; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java deleted file mode 100644 index b1a1e383..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.tools.redis; - -import com.mpush.tools.Constants; - -import redis.clients.jedis.JedisPoolConfig; - -public class RedisPoolConfig { - - public static JedisPoolConfig config = new JedisPoolConfig(); - - static{ - //连接池中最大连接数。高版本:maxTotal,低版本:maxActive - config.setMaxTotal(Constants.REDIS_MAX_TOTAL); - //连接池中最大空闲的连接数 - config.setMaxIdle(Constants.REDIS_MAX_IDLE); - //连接池中最少空闲的连接数 - config.setMinIdle(Constants.REDIS_MIN_IDLE); - //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait - config.setMaxWaitMillis(Constants.REDIS_MAX_WAIT_MILLIS); - //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 - config.setMinEvictableIdleTimeMillis(Constants.REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS); - //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 - config.setNumTestsPerEvictionRun(Constants.REDIS_NUM_TESTS_PER_EVICTION_RUN); - //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 - config.setTimeBetweenEvictionRunsMillis(Constants.REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); - //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. - config.setTestOnBorrow(Constants.REDIS_TEST_ON_BORROW); - //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 - config.setTestOnReturn(Constants.REDIS_TEST_ON_RETURN); - //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. - config.setTestWhileIdle(Constants.REDIS_TEST_WHILE_IDLE); - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java deleted file mode 100644 index 834babfa..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mpush.tools.redis; - -import com.mpush.tools.spi.SPI; - -import java.util.List; - -@SPI("redisRegister") -public interface RedisRegister { - - void init(List group); - - List getGroupList(); - - RedisNode randomGetRedisNode(String key); - - List hashSet(String key); -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java deleted file mode 100644 index c50d3eac..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.mpush.tools.redis.consistenthash; - -import java.util.Collection; -import java.util.SortedMap; -import java.util.TreeMap; - -import redis.clients.util.Hashing; - -public class ConsistentHash { - - private final Hashing hash; - private final int numberOfReplicas; - private final SortedMap circle = new TreeMap(); - - public ConsistentHash(Hashing hash, int numberOfReplicas, - Collection nodes) { - super(); - this.hash = hash; - this.numberOfReplicas = numberOfReplicas; - for (Node node : nodes) { - add(node); - } - } - - /** - * 增加真实机器节点 - * - * @param node - */ - public void add(Node node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.put(this.hash.hash(node.toString() + i), node); - } - } - - /** - * 删除真实机器节点 - * - * @param node - */ - public void remove(String node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.remove(this.hash.hash(node.toString() + i)); - } - } - - /** - * 取得真实机器节点 - * - * @param key - * @return - */ - public Node get(String key) { - if (circle.isEmpty()) { - return null; - } - long hash = this.hash.hash(key); - if (!circle.containsKey(hash)) { - SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 - hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); - } - return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java deleted file mode 100644 index 59beaacb..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mpush.tools.redis.consistenthash; - -public class Node { - - private String ip; //机器ip - private String name;//名字 - - public Node(String ip, String name) { - this.ip = ip; - this.name = name; - } - - public String getIp() { - return ip; - } - public void setIp(String ip) { - this.ip = ip; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java deleted file mode 100644 index 71a5765d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.mpush.tools.redis.jedis.services; - -import com.google.common.collect.Lists; -import com.mpush.log.Logs; - -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisRegister; - -import java.util.Collections; -import java.util.List; - -public class JedisRegisterManager implements RedisRegister { - - private static List groups = Lists.newArrayList(); - - /** - * zk 启动的时候需要调用这个 - */ - @Override - public void init(List group) { - if (group == null || group.isEmpty()) { - Logs.REDIS.info("init redis client error, redis server is none."); - throw new RuntimeException("init redis client error, redis server is none."); - } - groups = group; - printGroupList(); - } - - - @Override - public List getGroupList() { - return Collections.unmodifiableList(groups); - } - - private void printGroupList() { - for (RedisGroup app : groups) { - Logs.REDIS.info(Jsons.toJson(app)); - } - } - - public int groupSize() { - return groups.size(); - } - - /** - * 随机获取一个redis 实例 - * - * @param key - * @return - */ - @Override - public RedisNode randomGetRedisNode(String key) { - int size = groupSize(); - if (size == 1) return groups.get(0).get(key); - int i = (int) ((Math.random() % size) * size); - RedisGroup group = groups.get(i); - return group.get(key); - } - - /** - * 写操作的时候,获取所有redis 实例 - * - * @param key - * @return - */ - @Override - public List hashSet(String key) { - List nodeList = Lists.newArrayList(); - for (RedisGroup group : groups) { - RedisNode node = group.get(key); - nodeList.add(node); - } - return nodeList; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java deleted file mode 100644 index 428bf120..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.mpush.tools.redis.listener; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.mpush.log.Logs; - -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.tools.redis.pubsub.Subscriber; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; - -public class ListenerDispatcher implements MessageListener { - - public static final ListenerDispatcher INSTANCE = new ListenerDispatcher(); - - private Map> subscribes = Maps.newTreeMap(); - - private ListenerDispatcher(){} - - private Executor executor = ThreadPoolManager.redisExecutor; - - @Override - public void onMessage(final String channel, final String message) { - List listeners = subscribes.get(channel); - if (listeners == null) { - Logs.REDIS.info("cannot find listener:%s,%s", channel,message); - return; - } - for (final MessageListener listener : listeners) { - executor.execute(new Runnable() { - @Override - public void run() { - listener.onMessage(channel, message); - } - }); - } - } - - public void subscribe(String channel, MessageListener listener) { - List listeners = subscribes.get(channel); - if (listeners == null) { - listeners = Lists.newArrayList(); - subscribes.put(channel, listeners); - } - listeners.add(listener); - RedisManage.subscribe(Subscriber.holder, channel); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java deleted file mode 100644 index 627bfefc..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mpush.tools.redis.listener; - - -public interface MessageListener { - - void onMessage(String channel, String message); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java deleted file mode 100644 index cc0c9790..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java +++ /dev/null @@ -1,289 +0,0 @@ -package com.mpush.tools.redis.manage; - -import com.google.common.collect.Sets; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.RedisRegister; -import com.mpush.tools.redis.RedisUtil; -import com.mpush.tools.spi.ServiceContainer; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPubSub; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * redis 对外封装接口 - */ -public class RedisManage { - - private static final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - - public static long incr(String key, Integer time) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incr(nodeList, key, time); - } - - public static long incrBy(String key, long delt) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incrBy(nodeList, key, delt); - } - - /********************* - * k v redis start - ********************************/ - - public static T get(String key, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.get(node, key, clazz); - } - - public static void set(String key, T value) { - set(key, value, null); - } - - public static void set(String key, T value, Integer time) { - String jsonValue = Jsons.toJson(value); - set(key, jsonValue, time); - } - - /** - * @param key - * @param value - * @param time seconds - */ - public static void set(String key, String value, Integer time) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.set(nodeList, key, value, time); - } - - public static void del(String key) { - - List nodeList = redisRegister.hashSet(key); - RedisUtil.del(nodeList, key); - - } - - /*********************k v redis end********************************/ - - - /********************* - * hash redis start - ********************************/ - public static void hset(String namespace, String key, String value) { - - List nodeList = redisRegister.hashSet(key); - RedisUtil.hset(nodeList, namespace, key, value); - - } - - public static void hset(String namespace, String key, T value) { - hset(namespace, key, Jsons.toJson(value)); - } - - public static T hget(String namespace, String key, Class clazz) { - - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hget(node, namespace, key, clazz); - - } - - public static void hdel(String namespace, String key) { - List nodeList = redisRegister.hashSet(namespace); - RedisUtil.hdel(nodeList, namespace, key); - } - - public static Map hgetAll(String namespace) { - - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace); - - } - - public static Map hgetAll(String namespace, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace, clazz); - } - - /** - * 返回 key 指定的哈希集中所有字段的名字。 - * - * @return - */ - public static Set hkeys(String namespace) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hkeys(node, namespace); - } - - /** - * 返回 key 指定的哈希集中指定字段的值 - * - * @param key - * @param clazz - * @return - */ - public static List hmget(String namespace, Class clazz, String... key) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hmget(node, namespace, clazz, key); - } - - /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 - * - * @param hash - * @param time - */ - public static void hmset(String namespace, Map hash, Integer time) { - List nodeList = redisRegister.hashSet(namespace); - RedisUtil.hmset(nodeList, namespace, hash, time); - } - - public static void hmset(String namespace, Map hash) { - hmset(namespace, hash, null); - } - - - /*********************hash redis end********************************/ - - - /*********************list redis start********************************/ - /** - * 从队列的左边入队 - */ - public static void lpush(String key, String value) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.lpush(nodeList, key, value); - } - - public static void lpush(String key, T value) { - lpush(key, Jsons.toJson(value)); - } - - /** - * 从队列的右边入队 - */ - public static void rpush(String key, String value) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.rpush(nodeList, key, value); - } - - public static void rpush(String key, T value) { - rpush(key, Jsons.toJson(value)); - } - - /** - * 移除并且返回 key 对应的 list 的第一个元素 - */ - public static T lpop(String key, Class clazz) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.lpop(nodeList, key, clazz); - } - - /** - * 从队列的右边出队一个元素 - */ - public static T rpop(String key, Class clazz) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.rpop(nodeList, key, clazz); - } - - - /** - * 从列表中获取指定返回的元素 - * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List lrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.lrange(node, key, start, end, clazz); - } - - /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 - */ - public static long llen(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.llen(node, key); - } - - public static void lrem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.lRem(nodeList, key, jsonValue); - } - - public static void publish(String channel, T message) { - - RedisNode node = redisRegister.randomGetRedisNode(channel); - RedisUtil.publish(node, channel, message); - - } - - public static void subscribe(JedisPubSub pubsub, String... channels) { - - Set set = Sets.newHashSet(); - for (String channel : channels) { - List nodeList = redisRegister.hashSet(channel); - set.addAll(nodeList); - } - - RedisUtil.subscribe(set, pubsub, channels); - } - - public static void sAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sAdd(nodeList, key, jsonValue); - } - - public static Long sCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.sCard(node, key); - } - - public static void sRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sRem(nodeList, key, jsonValue); - } - - public static List sScan(String key, int start, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.sScan(node, key, clazz, start); - } - - public static void zAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.zAdd(nodeList, key, jsonValue); - } - - public static Long zCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zCard(node, key); - } - - public static void zRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.zRem(nodeList, key, jsonValue); - } - - public static List zrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zrange(node, key, start, end, clazz); - } - - public static void test(List groupList) { - for (RedisGroup group : groupList) { - for (RedisNode node : group.getRedisNodeList()) { - Jedis jedis = RedisUtil.getClient(node); - if (jedis == null) throw new RuntimeException("init redis sever error."); - jedis.close(); - } - } - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java deleted file mode 100644 index 54534522..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.mpush.tools.redis.pubsub; - -import com.mpush.log.Logs; - -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.listener.ListenerDispatcher; - -import redis.clients.jedis.JedisPubSub; - -public class Subscriber extends JedisPubSub { - - private static ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; - - public static Subscriber holder = new Subscriber(); - - private Subscriber(){} - - @Override - public void onMessage(String channel, String message) { - Logs.REDIS.info("onMessage:{},{}", channel,message); - dispatcher.onMessage(channel, message); - super.onMessage(channel, message); - } - - @Override - public void onPMessage(String pattern, String channel, String message) { - Logs.REDIS.info("onPMessage:{},{},{}",pattern,channel,message); - super.onPMessage(pattern, channel, message); - } - - @Override - public void onPSubscribe(String pattern, int subscribedChannels) { - Logs.REDIS.info("onPSubscribe:{},{}",pattern,subscribedChannels); - super.onPSubscribe(pattern, subscribedChannels); - } - - @Override - public void onPUnsubscribe(String pattern, int subscribedChannels) { - Logs.REDIS.info("onPUnsubscribe:{},{}",pattern,subscribedChannels); - super.onPUnsubscribe(pattern, subscribedChannels); - } - - @Override - public void onSubscribe(String channel, int subscribedChannels) { - Logs.REDIS.info("onSubscribe:{},{}",channel,subscribedChannels); - super.onSubscribe(channel, subscribedChannels); - } - - @Override - public void onUnsubscribe(String channel, int subscribedChannels) { - Logs.REDIS.info("onUnsubscribe:{},{}",channel,subscribedChannels); - super.onUnsubscribe(channel, subscribedChannels); - } - - - @Override - public void unsubscribe() { - Logs.REDIS.info("unsubscribe"); - super.unsubscribe(); - } - - @Override - public void unsubscribe(String... channels) { - Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); - super.unsubscribe(channels); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java b/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java deleted file mode 100644 index e212649e..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.mpush.tools.spi; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE }) -public @interface SPI { - - String value() default ""; - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 39fa5f51..4e4f407e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -1,47 +1,56 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -public class NamedThreadFactory implements ThreadFactory{ - - private static final AtomicInteger poolNum = new AtomicInteger(1); - - private final AtomicInteger threadNum = new AtomicInteger(1); - - private final ThreadGroup group; - private final String namePre; - private final boolean isDaemon; - - public NamedThreadFactory(){ - this("pool"); - } - - public NamedThreadFactory(String prefix){ - this(prefix,true); - } - - public NamedThreadFactory(String prefix,boolean daemon) { - SecurityManager manager = System.getSecurityManager(); - if(manager!=null){ - group = manager.getThreadGroup(); - }else{ - group = Thread.currentThread().getThreadGroup(); - } - isDaemon = daemon; - namePre = prefix+"-"+poolNum.getAndIncrement()+"-thread-"; - } - - /** - * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 - */ - @Override - public Thread newThread(Runnable runnable) { - Thread t = new Thread(group, runnable,namePre+threadNum.getAndIncrement(),0); - t.setContextClassLoader(NamedThreadFactory.class.getClassLoader()); - t.setPriority(Thread.MAX_PRIORITY); - t.setDaemon(isDaemon); - return t; - } +import static com.mpush.tools.thread.ThreadNames.THREAD_NAME_PREFIX; + +/** + * Created by xiaoxu.yxx on 2015/7/19. + */ +public final class NamedThreadFactory implements ThreadFactory { + protected final AtomicInteger threadNumber = new AtomicInteger(1); + protected final String namePrefix; + protected final ThreadGroup group; + + + public NamedThreadFactory() { + this(THREAD_NAME_PREFIX); + } + + public NamedThreadFactory(final String namePrefix) { + this.namePrefix = namePrefix; + this.group = Thread.currentThread().getThreadGroup(); + } + + public Thread newThread(String name, Runnable r) { + return new Thread(group, r, name); + } + @Override + public Thread newThread(Runnable r) { + Thread t = newThread(namePrefix + threadNumber.getAndIncrement(), r); + if (t.isDaemon()) + t.setDaemon(false); + return t; + } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java new file mode 100644 index 00000000..adb945ba --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java @@ -0,0 +1,61 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class PoolThreadFactory implements ThreadFactory { + private static final AtomicInteger poolNum = new AtomicInteger(1); + + private final AtomicInteger threadNum = new AtomicInteger(1); + + private final ThreadGroup group; + private final String namePre; + private final boolean isDaemon; + + public PoolThreadFactory(String prefix) { + this(prefix, true); + } + + public PoolThreadFactory(String prefix, boolean daemon) { + SecurityManager manager = System.getSecurityManager(); + if (manager != null) { + group = manager.getThreadGroup(); + } else { + group = Thread.currentThread().getThreadGroup(); + } + isDaemon = daemon; + namePre = prefix + "p-" + poolNum.getAndIncrement() + "-t-"; + } + + /** + * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 + */ + @Override + public Thread newThread(Runnable runnable) { + Thread t = new Thread(group, runnable, namePre + threadNum.getAndIncrement(), 0); + t.setContextClassLoader(PoolThreadFactory.class.getClassLoader()); + t.setPriority(Thread.MAX_PRIORITY); + t.setDaemon(isDaemon); + return t; + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java deleted file mode 100644 index 63fe7d72..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.tools.thread; - -public class ThreadNameSpace { - - /** - * netty boss 线程 - */ - public static final String NETTY_BOSS = "mp-boss"; - - /** - * netty worker 线程 - */ - public static final String NETTY_WORKER = "mp-worker"; - - public static final String NETTY_HTTP = "mp-http"; - - public static final String EVENT_BUS = "mp-event-bus"; - - public static final String REDIS = "mp-redis"; - - public static final String ZK = "mp-zk"; - - public static final String BIZ = "mp-biz"; - - /** - * connection 定期检测线程 - */ - public static final String NETTY_TIMER = "mp-timer"; - - public static final String getUniqueName(String serviceName) { - return "mp-sn-" + serviceName; - } - - public static final String THREAD_NAME_PREFIX = "mp-t-"; - - public static final String getServerName(String serverName){ - if(serverName.equals("ConnectionServer")){ - return "mp-start-cs"; - }else if(serverName.equals("GatewayServer")){ - return "mp-start-gs"; - }else{ - return "mp-start-unknow"; - } - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java new file mode 100644 index 00000000..a1d49ad6 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -0,0 +1,53 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread; + +public final class ThreadNames { + public static final String NS = "mp"; + public static final String THREAD_NAME_PREFIX = NS + "-t-"; + + /** + * netty boss 线程 + */ + public static final String T_SERVER_BOSS = NS + "-boss-"; + + /** + * netty worker 线程 + */ + public static final String T_SERVER_WORKER = NS + "-worker-"; + + public static final String T_HTTP_CLIENT = NS + "-http-"; + + public static final String T_EVENT_BUS = NS + "-event-"; + + public static final String T_MQ = NS + "-mq-"; + + public static final String T_ZK = NS + "-zk-"; + + public static final String T_BIZ = NS + "-biz-"; + public static final String T_PUSH_CALLBACK = NS + "-push-cb-"; + public static final String T_PUSH_REQ_TIMER = NS + "-push-timer-"; + + /** + * connection 定期检测线程 + */ + public static final String T_NETTY_TIMER = NS + "-timer-"; + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java new file mode 100644 index 00000000..aad039ea --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java @@ -0,0 +1,34 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.thread.pool; + +import java.util.concurrent.*; + +/** + * Created by yxx on 2016/5/29. + * + * @author ohun@live.cn (夜色) + */ +public class DefaultExecutor extends ThreadPoolExecutor { + + public DefaultExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java new file mode 100644 index 00000000..ef83ac45 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java @@ -0,0 +1,120 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.PoolThreadFactory; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import static com.mpush.tools.thread.ThreadNames.*; + +/** + * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 + */ +public class DefaultThreadPoolFactory implements ThreadPoolFactory { + + private Executor get(ThreadPoolConfig config) { + String name = config.getName(); + int corePoolSize = config.getCorePoolSize(); + int maxPoolSize = config.getMaxPoolSize(); + int keepAliveSeconds = config.getKeepAliveSeconds(); + + BlockingQueue queue = config.getQueue(); + ThreadFactory threadFactory = new PoolThreadFactory(name); + + return new DefaultExecutor(corePoolSize + , maxPoolSize + , keepAliveSeconds + , TimeUnit.SECONDS + , queue + , threadFactory + , new DumpThreadRejectedHandler(config)); + } + + @Override + public Executor get(String name) { + final ThreadPoolConfig config; + switch (name) { + case SERVER_BOSS: + config = ThreadPoolConfig + .build(T_SERVER_BOSS) + .setCorePoolSize(CC.mp.thread.pool.boss.min) + .setMaxPoolSize(CC.mp.thread.pool.boss.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.boss.queue_size); + break; + case SERVER_WORK: + config = ThreadPoolConfig + .build(T_SERVER_WORKER) + .setCorePoolSize(CC.mp.thread.pool.work.min) + .setMaxPoolSize(CC.mp.thread.pool.work.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.work.queue_size); + break; + case HTTP_CLIENT_WORK: + config = ThreadPoolConfig + .buildFixed(T_HTTP_CLIENT, + CC.mp.thread.pool.http_proxy.min, + CC.mp.thread.pool.http_proxy.queue_size + ) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_DISCARD); + break; + case EVENT_BUS: + config = ThreadPoolConfig + .buildFixed(T_EVENT_BUS, + CC.mp.thread.pool.event_bus.min, + CC.mp.thread.pool.event_bus.queue_size + ); + break; + case MQ: + config = ThreadPoolConfig + .buildFixed(T_MQ, + CC.mp.thread.pool.mq.min, + CC.mp.thread.pool.mq.queue_size + ); + break; + case PUSH_CALLBACK: + config = ThreadPoolConfig + .build(T_PUSH_CALLBACK) + .setCorePoolSize(CC.mp.thread.pool.push_callback.min) + .setMaxPoolSize(CC.mp.thread.pool.push_callback.max) + .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) + .setQueueCapacity(CC.mp.thread.pool.push_callback.queue_size) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); + break; + default: + case BIZ: + config = ThreadPoolConfig + .build(T_BIZ) + .setCorePoolSize(CC.mp.thread.pool.biz.min) + .setMaxPoolSize(CC.mp.thread.pool.biz.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.biz.queue_size); + break; + } + + return get(config); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java new file mode 100644 index 00000000..f5d5165d --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -0,0 +1,74 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import com.mpush.tools.common.JVMUtil; +import com.mpush.tools.config.CC; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; + +import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_ABORT; +import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS; + +public class DumpThreadRejectedHandler implements RejectedExecutionHandler { + + private final static Logger LOGGER = LoggerFactory.getLogger(DumpThreadRejectedHandler.class); + + private volatile boolean dumping = false; + + private static final String DUMP_DIR = CC.mp.monitor.dump_dir; + + private final ThreadPoolConfig poolConfig; + + private final int rejectedPolicy; + + public DumpThreadRejectedHandler(ThreadPoolConfig poolConfig) { + this.poolConfig = poolConfig; + this.rejectedPolicy = poolConfig.getRejectedPolicy(); + } + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { + LOGGER.warn("one task rejected, poolConfig={}, poolInfo={}", poolConfig, ThreadPoolManager.getPoolInfo(e)); + if (!dumping) { + dumping = true; + dumpJVMInfo(); + } + + if (rejectedPolicy == REJECTED_POLICY_ABORT) { + throw new RejectedExecutionException("one task rejected, pool=" + poolConfig.getName()); + } else if (rejectedPolicy == REJECTED_POLICY_CALLER_RUNS) { + if (!e.isShutdown()) { + r.run(); + } + } + } + + private void dumpJVMInfo() { + LOGGER.error("start dump jvm info"); + JVMUtil.dumpJstack(DUMP_DIR + "/" + poolConfig.getName()); + LOGGER.error("end dump jvm info"); + } +} + diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java new file mode 100644 index 00000000..d82cf1a0 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -0,0 +1,135 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; + +public class ThreadPoolConfig { + public static final int REJECTED_POLICY_ABORT = 0; + public static final int REJECTED_POLICY_DISCARD = 1; + public static final int REJECTED_POLICY_CALLER_RUNS = 2; + private String name;//名字 + private int corePoolSize; //最小线程大小 + private int maxPoolSize; //最大线程大小 + private int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) + private int keepAliveSeconds;// 存活时间 + private int rejectedPolicy = REJECTED_POLICY_ABORT; + + public ThreadPoolConfig(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public ThreadPoolConfig setName(String name) { + this.name = name; + return this; + } + + public int getCorePoolSize() { + return corePoolSize; + } + + public ThreadPoolConfig setCorePoolSize(int corePoolSize) { + this.corePoolSize = corePoolSize; + return this; + } + + public int getMaxPoolSize() { + return maxPoolSize; + } + + public ThreadPoolConfig setMaxPoolSize(int maxPoolSize) { + this.maxPoolSize = maxPoolSize; + return this; + } + + public int getQueueCapacity() { + return queueCapacity; + } + + public ThreadPoolConfig setQueueCapacity(int queueCapacity) { + this.queueCapacity = queueCapacity; + return this; + } + + public int getKeepAliveSeconds() { + return keepAliveSeconds; + } + + public ThreadPoolConfig setKeepAliveSeconds(long keepAliveSeconds) { + this.keepAliveSeconds = (int) keepAliveSeconds; + return this; + } + + public int getRejectedPolicy() { + return rejectedPolicy; + } + + public ThreadPoolConfig setRejectedPolicy(int rejectedPolicy) { + this.rejectedPolicy = rejectedPolicy; + return this; + } + + public static ThreadPoolConfig buildFixed(String name, int threads, int queueCapacity) { + return new ThreadPoolConfig(name) + .setCorePoolSize(threads) + .setMaxPoolSize(threads) + .setQueueCapacity(queueCapacity) + .setKeepAliveSeconds(0); + } + + public static ThreadPoolConfig buildCached(String name) { + return new ThreadPoolConfig(name) + .setKeepAliveSeconds(0); + } + + public static ThreadPoolConfig build(String name) { + return new ThreadPoolConfig(name); + } + + + public BlockingQueue getQueue() { + BlockingQueue blockingQueue; + if (queueCapacity == 0) { + blockingQueue = new SynchronousQueue<>(); + } else if (queueCapacity < 0) { + blockingQueue = new LinkedBlockingQueue<>(); + } else { + blockingQueue = new LinkedBlockingQueue<>(queueCapacity); + } + return blockingQueue; + } + + @Override + public String toString() { + return "ThreadPoolConfig{" + + "name='" + name + '\'' + + ", corePoolSize=" + corePoolSize + + ", maxPoolSize=" + maxPoolSize + + ", queueCapacity=" + queueCapacity + + ", keepAliveSeconds=" + keepAliveSeconds + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java new file mode 100644 index 00000000..45b28813 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -0,0 +1,135 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.tools.thread.NamedThreadFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import static com.mpush.tools.config.CC.mp.spi.thread_pool_factory; + +public class ThreadPoolManager { + public static final ThreadPoolManager I = new ThreadPoolManager(); + + private final ThreadPoolFactory threadPoolFactory = SpiLoader.load(ThreadPoolFactory.class, thread_pool_factory); + private final NamedThreadFactory threadFactory = new NamedThreadFactory(); + + private Executor bossExecutor; + private Executor workExecutor; + private Executor bizExecutor; + private Executor eventBusExecutor; + private Executor redisExecutor; + private Executor httpExecutor; + private Executor pushCallbackExecutor; + + public final Thread newThread(String name, Runnable target) { + return threadFactory.newThread(name, target); + } + + public Executor getHttpExecutor() { + if (httpExecutor == null) { + synchronized (this) { + httpExecutor = threadPoolFactory.get(ThreadPoolFactory.HTTP_CLIENT_WORK); + } + } + return httpExecutor; + } + + public Executor getRedisExecutor() { + if (redisExecutor == null) { + synchronized (this) { + redisExecutor = threadPoolFactory.get(ThreadPoolFactory.MQ); + } + } + return redisExecutor; + } + + public Executor getEventBusExecutor() { + if (eventBusExecutor == null) { + synchronized (this) { + eventBusExecutor = threadPoolFactory.get(ThreadPoolFactory.EVENT_BUS); + } + } + return eventBusExecutor; + } + + public Executor getBizExecutor() { + if (bizExecutor == null) { + synchronized (this) { + bizExecutor = threadPoolFactory.get(ThreadPoolFactory.BIZ); + } + } + return bizExecutor; + } + + public Executor getWorkExecutor() { + if (workExecutor == null) { + synchronized (this) { + workExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_WORK); + } + } + return workExecutor; + } + + public Executor getBossExecutor() { + if (bossExecutor == null) { + synchronized (this) { + bossExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_BOSS); + } + } + return bossExecutor; + } + + public Executor getPushCallbackExecutor() { + if (pushCallbackExecutor == null) { + synchronized (this) { + pushCallbackExecutor = threadPoolFactory.get(ThreadPoolFactory.PUSH_CALLBACK); + } + } + return pushCallbackExecutor; + } + + public Map getActivePools() { + Map map = new HashMap<>(); + if (bossExecutor != null) map.put("bossExecutor", bossExecutor); + if (workExecutor != null) map.put("workExecutor", workExecutor); + if (bizExecutor != null) map.put("bizExecutor", bizExecutor); + if (eventBusExecutor != null) map.put("eventBusExecutor", eventBusExecutor); + if (redisExecutor != null) map.put("redisExecutor", redisExecutor); + if (httpExecutor != null) map.put("httpExecutor", httpExecutor); + if (pushCallbackExecutor != null) map.put("pushCallbackExecutor", pushCallbackExecutor); + return map; + } + + public static Map getPoolInfo(ThreadPoolExecutor executor) { + Map info = new HashMap<>(); + info.put("corePoolSize", executor.getCorePoolSize()); + info.put("maxPoolSize", executor.getMaximumPoolSize()); + info.put("activeCount(workingThread)", executor.getActiveCount()); + info.put("poolSize(workThread)", executor.getPoolSize()); + info.put("queueSize(blockedTask)", executor.getQueue().size()); + return info; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java deleted file mode 100644 index 88255500..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadPoolExecutor; - -import com.mpush.tools.JVMUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.config.ConfigCenter; - -public class IgnoreRunsPolicy implements RejectedExecutionHandler{ - - private final static Logger log = LoggerFactory.getLogger(IgnoreRunsPolicy.class); - - private volatile boolean dump = false; - - private static final String preFixPath = ConfigCenter.I.logPath(); - - private final ThreadPoolContext context; - - public IgnoreRunsPolicy(ThreadPoolContext context) { - this.context = context; - } - - @Override - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - dumpJVMInfo(); - throw new RejectedExecutionException(); - } - - private void dumpJVMInfo(){ - if (!dump) { - dump = true; - log.error("start dump jvm info"); - JVMUtil.dumpJstack(preFixPath+"/"+context.getName()); - log.error("end dump jvm info"); - } - } -} - diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java deleted file mode 100644 index da079304..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import java.util.concurrent.Executor; - -import com.mpush.tools.spi.SPI; - -@SPI("cachedThreadPool") -public interface ThreadPool { - - public Executor getExecutor(ThreadPoolContext context); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java deleted file mode 100644 index 5823bad0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import com.mpush.tools.Constants; -import com.mpush.tools.thread.ThreadNameSpace; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; - - -public class ThreadPoolContext { - - private final String name;//名字 - private final int corePoolSize; //最小线程大小 - private final int maxPoolSize; //最大线程大小 - private final int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) - private final int keepAliveSeconds;// 存活时间 - - public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POOL_SIZE, 60*5,Constants.BOSS_THREAD_QUEUE_SIZE); - - public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5,Constants.WORK_THREAD_QUEUE_SIZE); - - public static ThreadPoolContext HTTP_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.NETTY_HTTP, Constants.BIZ_POOL_SIZE); - - public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); - - public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); - - public static ThreadPoolContext REDIS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.REDIS, Constants.REDIS_POOL_SIZE,Constants.REDIS_THREAD_QUEUE_SIZE); - - public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { - this.name = name; - this.corePoolSize = corePoolSize; - this.maxPoolSize = maxPoolSize; - this.queueCapacity = queueCapacity; - this.keepAliveSeconds = keepAliveSeconds; - } - - public String getName() { - return name; - } - - public int getCorePoolSize() { - return corePoolSize; - } - - public int getMaxPoolSize() { - return maxPoolSize; - } - - public int getQueueCapacity() { - return queueCapacity; - } - - public int getKeepAliveSeconds() { - return keepAliveSeconds; - } - - @Override - public String toString() { - return "ThreadPoolContext [name=" + name + ", corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity + ", keepAliveSeconds=" + keepAliveSeconds - + "]"; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java deleted file mode 100644 index db2b1d79..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Executor; - -public class ThreadPoolManager { - - private static final Map poolCache = new HashMap(); - - private static ThreadPool cachedThreadPool = new CachedThreadPool(); - - private static ThreadPool fixedThreadPool = new FixedThreadPool(); - - public static Executor bossExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.BOSS_THREAD_POOL); - public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); - public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); - public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); - public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); - - public static Executor httpExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); - - static { - poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); - poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); - poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); - poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); - poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); - poolCache.put(ThreadPoolContext.HTTP_THREAD_POOL.getName(), httpExecutor); - } - - public static final Map getPool() { - return poolCache; - } - - public static final Thread newThread(String name, Runnable target) { - Thread thread = new Thread(target, name); - return thread; - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java deleted file mode 100644 index e432d46d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.tools.thread.threadpool.cached; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.mpush.tools.thread.threadpool.ThreadPoolContext; -import com.mpush.tools.thread.threadpool.ThreadPool; - -/** - * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 - * - */ -public class CachedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - - String name = context.getName(); - int corePoolSize = context.getCorePoolSize(); - int maxPoolSize = context.getMaxPoolSize(); - int queueCapacity = context.getQueueCapacity(); - int keepAliveSeconds = context.getKeepAliveSeconds(); - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - BlockingQueue blockingQueue = null; - if(queueCapacity == 0){ - blockingQueue = new SynchronousQueue(); - }else if(queueCapacity<0){ - blockingQueue = new LinkedBlockingQueue(); - }else{ - blockingQueue = new LinkedBlockingQueue(queueCapacity); - } - - return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java deleted file mode 100644 index def69d45..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.tools.thread.threadpool.cached; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -public class CachedThreadPoolContext extends ThreadPoolContext { - - public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) { - this(name, corePoolSize, maxPoolSize, keepAliveSeconds, 0); - } - - public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity) { - super(name, corePoolSize, maxPoolSize, queueCapacity, keepAliveSeconds); - } - - public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds){ - return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds); - } - - public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity){ - return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds,queueCapacity); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java deleted file mode 100644 index 743ec71d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mpush.tools.thread.threadpool.fixed; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.mpush.tools.thread.threadpool.ThreadPool; -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -/** - *此线程池启动时即创建固定大小的线程数,不做任何伸缩 - * - */ -public class FixedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - String name = context.getName(); - int corePoolSize = context.getCorePoolSize(); - int queueCapacity = context.getQueueCapacity(); - - BlockingQueue blockingQueue = null; - if (queueCapacity == 0) { - blockingQueue = new SynchronousQueue(); - } else if (queueCapacity < 0) { - blockingQueue = new LinkedBlockingQueue(); - } else { - blockingQueue = new LinkedBlockingQueue(queueCapacity); - } - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java deleted file mode 100644 index 09cc9305..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.tools.thread.threadpool.fixed; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -public class FixedThreadPoolContext extends ThreadPoolContext { - - public FixedThreadPoolContext(String name, int threads,int queueCapacity) { - super(name, threads, 0, queueCapacity, 0); - } - - public FixedThreadPoolContext(String name, int threads) { - super(name, threads, 0, -1, 0); - } - - public static FixedThreadPoolContext create(String name,int threads){ - return new FixedThreadPoolContext(name, threads); - } - - public static FixedThreadPoolContext create(String name,int threads,int queueCapacity){ - return new FixedThreadPoolContext(name, threads,queueCapacity); - } - -} diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory new file mode 100644 index 00000000..be69a4a6 --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory @@ -0,0 +1 @@ +com.mpush.tools.thread.pool.DefaultThreadPoolFactory \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister b/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister deleted file mode 100644 index ff33af00..00000000 --- a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister +++ /dev/null @@ -1 +0,0 @@ -com.mpush.tools.redis.jedis.services.JedisRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java index cdab4c60..7e63de28 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java @@ -1,5 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; +import com.mpush.tools.common.IOUtils; import org.junit.Test; /** @@ -46,12 +66,12 @@ public void testCompress() throws Exception { for (int i = 0; i < 100; i++) { IOUtils.compress(s); } - System.out.println((System.currentTimeMillis()-t1)/100); + System.out.println((System.currentTimeMillis() - t1) / 100); System.out.println("src:" + s.length); byte[] out = IOUtils.compress(s); System.out.println("compress:" + out.length); - byte[] ss = IOUtils.uncompress(out); - System.out.println("uncompress:" + ss.length); + byte[] ss = IOUtils.decompress(out); + System.out.println("decompress:" + ss.length); } @Test diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java index 36d4b0d6..a57f89dd 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java @@ -1,6 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; -import com.mpush.tools.Constants; +import com.mpush.api.Constants; import org.junit.Test; import java.util.Random; diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java index 84c7eec6..d723f7c6 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java @@ -1,7 +1,25 @@ -package com.mpush.tools.crypto; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import com.mpush.tools.Pair; +package com.mpush.tools.crypto; +import com.mpush.tools.common.Pair; import org.junit.Before; import org.junit.Test; diff --git a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java deleted file mode 100644 index 45d7a82f..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.mpush.tools.delayqueue; - -import java.util.Iterator; -import java.util.Random; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.DelayQueue; -import java.util.concurrent.Delayed; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * 考试时间为120分钟,30分钟后才可交卷,初始化考生完成试卷时间最小应为30分钟 - * 对于能够在120分钟内交卷的考生,如何实现这些考生交卷 - * 对于120分钟内没有完成考试的考生,在120分钟考试时间到后需要让他们强制交卷 - * 在所有的考生都交完卷后,需要将控制线程关闭 - * - * 实现思想:用DelayQueue存储考生(Student类),每一个考生都有自己的名字和完成试卷的时间, - * Teacher线程对DelayQueue进行监控,收取完成试卷小于120分钟的学生的试卷。 - * 当考试时间120分钟到时,先关闭Teacher线程,然后强制DelayQueue中还存在的考生交卷。 - * 每一个考生交卷都会进行一次countDownLatch.countDown(),当countDownLatch.await()不再阻塞说明所有考生都交完卷了,而后结束考试。 - * - */ - -public class Exam { - - public static void main(String[] args) throws InterruptedException { - int studentNumber = 20; - CountDownLatch countDownLatch = new CountDownLatch(studentNumber+1); - DelayQueue students = new DelayQueue(); - Random random = new Random(); - for (int i = 0; i < studentNumber; i++) { - students.put(new Student("student"+(i+1), 30+random.nextInt(120),countDownLatch)); - } - Thread teacherThread =new Thread(new Teacher(students)); - students.put(new EndExam(students, 120,countDownLatch,teacherThread)); - teacherThread.start(); - countDownLatch.await(); - System.out.println(" 考试时间到,全部交卷!"); - } - -} - -class Student implements Runnable,Delayed{ - - private String name; - private long workTime; //考试时间 - private long submitTime; //交卷时间 - private boolean isForce = false; - private CountDownLatch countDownLatch; - - public Student() { - } - - public Student(String name,long workTime,CountDownLatch countDownLatch){ - this.name = name; - this.workTime = workTime; - this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS)+System.nanoTime(); - this.countDownLatch = countDownLatch; - } - - @Override - public int compareTo(Delayed o) { - if(o == null || ! (o instanceof Student)) return 1; - if(o == this) return 0; - Student s = (Student)o; - if (this.workTime > s.workTime) { - return 1; - }else if (this.workTime == s.workTime) { - return 0; - }else { - return -1; - } - } - - @Override - public long getDelay(TimeUnit unit) { - return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS); - } - - @Override - public void run() { - if (isForce) { - System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 120分钟" ); - }else { - System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 "+workTime +" 分钟"); - } - countDownLatch.countDown(); - } - - public boolean isForce() { - return isForce; - } - - public void setForce(boolean isForce) { - this.isForce = isForce; - } - -} - -class EndExam extends Student{ - - private DelayQueue students; - private CountDownLatch countDownLatch; - private Thread teacherThread; - - public EndExam(DelayQueue students, long workTime, CountDownLatch countDownLatch,Thread teacherThread) { - super("强制收卷", workTime,countDownLatch); - this.students = students; - this.countDownLatch = countDownLatch; - this.teacherThread = teacherThread; - } - - - - @Override - public void run() { - // TODO Auto-generated method stub - - teacherThread.interrupt(); - Student tmpStudent; - for (Iterator iterator2 = students.iterator(); iterator2.hasNext();) { - tmpStudent = iterator2.next(); - tmpStudent.setForce(true); - tmpStudent.run(); - } - countDownLatch.countDown(); - } - -} - - -class Teacher implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Teacher.class); - - private DelayQueue students; - - public Teacher(DelayQueue students) { - this.students = students; - } - - @Override - public void run() { - try{ - log.warn(" test start "); - while(!Thread.interrupted()){ - students.take().run(); - } - }catch(Exception e){ - log.warn("exception :",e); - } - } -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java deleted file mode 100644 index f9907f95..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.mpush.tools.owner; - -import com.mpush.tools.config.ConfigCenter; -import org.aeonbits.owner.ConfigFactory; -import org.junit.Test; - -public class OwnerTest { - - @Test - public void test1(){ - - ServerConfig cfg = ConfigFactory.create(ServerConfig.class); - - System.out.println(cfg.zkDigest()); - - System.out.println(cfg.zkIp()); - - System.out.println(cfg.hello()); - - System.out.println(cfg.maxHbTimeoutTimes()); - - System.out.println(cfg.test()); - - Integer tset = cfg.testnotexist(); - if(tset == null){ - System.out.println("not exist"); - }else{ - System.out.println(tset); - } - } - - @Test - public void test2(){ - - System.out.println(ConfigCenter.I.zkIp()); - - System.out.println("aesKeyLength:"+ConfigCenter.I.aesKeyLength()); - - System.out.println("compressLimit:"+ConfigCenter.I.compressLimit()); - - System.out.println("connectionServerPort:"+ConfigCenter.I.connectionServerPort()); - - System.out.println("gatewayServerPort:"+ConfigCenter.I.gatewayServerPort()); - - System.out.println("maxHBTimeoutTimes:"+ConfigCenter.I.maxHBTimeoutTimes()); - - System.out.println(ConfigCenter.I.maxHeartbeat()); - - System.out.println(ConfigCenter.I.maxPacketSize()); - - System.out.println(ConfigCenter.I.minHeartbeat()); - - System.out.println(ConfigCenter.I.privateKey()); - - System.out.println(ConfigCenter.I.publicKey()); - - System.out.println(ConfigCenter.I.rsaKeyLength()); - - System.out.println(ConfigCenter.I.redisIp()); - - System.out.println(ConfigCenter.I.sessionExpiredTime()); - - System.out.println(ConfigCenter.I.zkDigest()); - - - - System.out.println(ConfigCenter.I.zkNamespace()); - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java b/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java deleted file mode 100644 index e3060630..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.mpush.tools.owner; - -import org.aeonbits.owner.Config; -import org.aeonbits.owner.Config.Sources; - - -@Sources({"classpath:serverconfig.properties"}) -public interface ServerConfig extends Config{ - - @Key("zk_ip") - @DefaultValue("zkIp") - public String zkIp(); - - @Key("zk_digest") - public String zkDigest(); - - @Key("hello") - @DefaultValue("hello world") - public String hello(); - - @Key("max_hb_timeout_times") - public int maxHbTimeoutTimes(); - - @Key("test") - @DefaultValue("10") - public int test(); - - public Integer testnotexist(); - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java deleted file mode 100644 index 9cf9a807..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mpush.tools.spi; - - -import com.mpush.tools.spi.test.TestService; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - - -public class SpiTest { - - private static Executor pool = Executors.newCachedThreadPool(); - - @Test - public void baseTest() { - TestService testService = ServiceContainer.load(TestService.class); - System.out.println(testService.sayHi(" huang")); - - ServiceContainer.load(TestService.class); - } - - @Ignore - @Test - public void listTest() { - - - } - - @Ignore - @Test - public void mulThreadTest() throws InterruptedException { - pool.execute(new Worker()); - pool.execute(new Worker()); - pool.execute(new ListWorker()); - pool.execute(new ListWorker()); - Thread.sleep(Integer.MAX_VALUE); - } - - - private static final class Worker implements Runnable { - - @Override - public void run() { - } - - } - - private static final class ListWorker implements Runnable { - - @Override - public void run() { - - - } - - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java deleted file mode 100644 index 8460ca51..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.tools.spi.test; - -import com.mpush.tools.spi.SPI; - - -@SPI("test1") -public interface TestService { - - public String sayHi(String name); - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java deleted file mode 100644 index a23cf509..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.tools.spi.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TestServiceImpl implements TestService { - - private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); - - public TestServiceImpl() { - log.warn("init"); - } - - @Override - public String sayHi(String name) { - return "TestServiceImpl1 hi,"+name; - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java deleted file mode 100644 index 575e4316..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mpush.tools.spi.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class TestServiceImpl2 implements TestService { - - private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); - - public TestServiceImpl2() { - log.warn("init"); - } - - @Override - public String sayHi(String name) { - return "TestServiceImpl2 hi,"+name; - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java deleted file mode 100644 index 1e69324d..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.mpush.tools.thread; - - -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.thread.threadpool.ThreadPool; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; - -public class SyncTest { - - //pri MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIvbj8CvXqWcXAeNw7yruM2TZPfIkp2JIbPwpptAMt6t6zoByRLDjPkbkFVbJrmmto5dGLFCQHz9NM24gb5R9U5dotApgCYFabbocIUx+mkUcd+ui+SZ5yTTyXtVhqKBFGqCTEB73S7b5y0xof/r781EZWYA3sh47pNXVYisRh7rAgMBAAECgYARri8NH95qN0sXFV/iUR8qtfB0tqF6UuS0175oMAR+TCRJkAI4YgpHT6m+cKiDncTEWJaPih2W73embiXQxpGpJt0HKegmKF5RiSU8iXjbFQvmlfTRrgo7qLIjgqUxaM0h+ef0p/T3EV+HZ8sk2bHZPd5OzTcAx1UOSgz88VEDEQJBAONTXI88w+cIkeS5uDDCDjV5S5ljQCBmBTlwIp0UCLDZ0KQDFCiOM54ltgcsMrKQFyj2EwTWsevbikTP3KRmXzMCQQCdf78HkzGnGAJUzPchIHvQBC1Q95X002rYPxNrlF86iU7n1fan++xuGDTYnz+eNRKJFEY85SQq0eld0KI57qFpAkAZ9Lu90ydfKthVsGr6jj3HF0ltgyqgSGXSUB5zpwTzBHvRLlTP6KS2KwIkwYQsZU1vrOExDT6VeqTIBJ/h2ZqHAkAW9dKRdiHc7CEa365/Q88I6jL5BL71rAR9deSM4FppnC7GmWiV4KH9AsZhdgW+OJp1JWF/6x+0pllQ9eNQcrtRAkBczJJ5l3BtCE+VToy16DPPQCfyDpb6dmyR4IkTwECWMomz9jnt1fK7nETfBeeP4ySea8jrUCgZdu06iPtoLCAK - //pub MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL24/Ar16lnFwHjcO8q7jNk2T3yJKdiSGz8KabQDLeres6AckSw4z5G5BVWya5praOXRixQkB8/TTNuIG+UfVOXaLQKYAmBWm26HCFMfppFHHfrovkmeck08l7VYaigRRqgkxAe90u2+ctMaH/6+/NRGVmAN7IeO6TV1WIrEYe6wIDAQAB - - final static ThreadPool cachedThreadPool = new CachedThreadPool(); - final static ThreadPool fixedThreadPool = new FixedThreadPool(); - - // 最小线程1,最大线程2,使用sync blockqueue,线程缓存5分钟 - final static ThreadPoolContext cachedThreadPoolContext = CachedThreadPoolContext.create("cached-test", 1, 2, 60*5); - - // 最小线程1,最大线程2,使用LinkedBlockingQueue,线程缓存5分钟 - final static ThreadPoolContext cachedThreadPoolContext2 = CachedThreadPoolContext.create("cached-test2", 1, 2, 60*5,1); - - //线程数1,blockqueue大小为2 - final static ThreadPoolContext fixedThreadPoolContext = FixedThreadPoolContext.create("fix-test",1,2); - - final static Executor cachedExecutor = cachedThreadPool.getExecutor(cachedThreadPoolContext); - - final static Executor fixedExecutor = fixedThreadPool.getExecutor(fixedThreadPoolContext); - - final static Executor cachedExecutor2 = cachedThreadPool.getExecutor(cachedThreadPoolContext2); - - Worker worker1 = new Worker(); - Worker worker2 = new Worker(); - Worker worker3 = new Worker(); - Worker worker4 = new Worker(); - Worker worker5 = new Worker(); - - Monitor monitor = new Monitor(fixedExecutor,cachedExecutor); - - - @Before - public void init(){ - monitor.start(); - } - - /** - * 容量为2,因此最多可以接收三个任务。第四个任务开始抛异常 - */ - @Test - public void testFixed(){ - fixedExecutor.execute(worker1); - fixedExecutor.execute(worker2); - fixedExecutor.execute(worker3); - fixedExecutor.execute(worker4); - fixedExecutor.execute(worker5); - } - - /** - * 最小线程1,最大线程2,没有缓冲队列。所以,第三个任务提交的时候,就已经抛错了。 - */ - @Test - public void testCached(){ - cachedExecutor.execute(worker1); - cachedExecutor.execute(worker2); - cachedExecutor.execute(worker3); - cachedExecutor.execute(worker4); - cachedExecutor.execute(worker5); - } - - /** - * 最小线程1,最大线程2,缓冲队列长度为1。所以,第四个任务提交的时候,就已经抛错了。 - */ - @Test - public void testCached2(){ - cachedExecutor2.execute(worker1); - cachedExecutor2.execute(worker2); - cachedExecutor2.execute(worker3); - cachedExecutor2.execute(worker4); - cachedExecutor2.execute(worker5); - } - -} - - -class Worker implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - log.warn("start run Worker:"+Thread.currentThread().getName()); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - log.warn("end run Worker:"+Thread.currentThread().getName()); - } -} - -class Monitor extends Thread{ - - private static final Logger log = LoggerFactory.getLogger(Monitor.class); - - private ThreadPoolExecutor fixedExecutor; - private ThreadPoolExecutor cachedExecutor; - - public Monitor(Executor fixedExecutor,Executor cachedExecutor) { - this.fixedExecutor = (ThreadPoolExecutor)fixedExecutor; - this.cachedExecutor = (ThreadPoolExecutor)cachedExecutor; - } - - @Override - public void run() { - - while(true){ - StringBuilder sb = new StringBuilder(); - sb.append("[fixedExecutor]"+"coreThreadNums:" + fixedExecutor.getCorePoolSize() + " maxThreadNums:" + fixedExecutor.getMaximumPoolSize() + " activityThreadNums:" - + fixedExecutor.getActiveCount()); - log.error(sb.toString()); - - StringBuilder sb2 = new StringBuilder(); - sb2.append("[cachedExecutor]"+"coreThreadNums:" + cachedExecutor.getCorePoolSize() + " maxThreadNums:" + cachedExecutor.getMaximumPoolSize() + " activityThreadNums:" - + cachedExecutor.getActiveCount()); - log.error(sb2.toString()); - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - - } - - } - -} diff --git a/mpush-tools/src/test/resources/logback.xml b/mpush-tools/src/test/resources/logback.xml index 3e167126..c1d001d8 100644 --- a/mpush-tools/src/test/resources/logback.xml +++ b/mpush-tools/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN + + System.out + UTF-8 + + INFO - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-zk/.gitignore b/mpush-zk/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-zk/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index d727e523..81b578f6 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -1,12 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; -import com.mpush.log.Logs; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Constants; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.exception.ZKException; -import com.mpush.zk.listener.ZKDataChangeListener; +import com.mpush.api.Constants; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.log.Logs; +import com.mpush.zk.listener.ZKNodeCacheWatcher; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; @@ -16,7 +33,6 @@ import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.CloseableUtils; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; @@ -26,7 +42,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; -public class ZKClient { +public class ZKClient extends BaseService { public static final ZKClient I = I(); private ZKConfig zkConfig; private CuratorFramework client; @@ -38,25 +54,40 @@ private synchronized static ZKClient I() { } private ZKClient() { - try { - init(); - } catch (Exception e) { - throw new ZKException("init zk error, config=" + zkConfig, e); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + client.start(); + Logs.Console.error("init zk client waiting for connected..."); + if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { + throw new ZKException("init zk error, config=" + zkConfig); } + initLocalCache(zkConfig.getLocalCachePath()); + addConnectionStateListener(); + listener.onSuccess(zkConfig.getHosts()); + Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); + Logs.Console.error("init zk client success..."); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (cache != null) cache.close(); + TimeUnit.MILLISECONDS.sleep(600); + client.close(); } /** * 初始化 */ - private void init() throws Exception { - zkConfig = ZKConfig.build(ConfigCenter.I.zkIp()) - .setDigest(ConfigCenter.I.zkDigest()) - .setNamespace(ConfigCenter.I.zkNamespace()); - ConsoleLog.i("init zk client, config=" + zkConfig); + @Override + public void init() { + if (zkConfig != null) return; + zkConfig = ZKConfig.build(); Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) - .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepMs())) .namespace(zkConfig.getNamespace()); if (zkConfig.getConnectionTimeout() > 0) { @@ -82,29 +113,16 @@ public List getAclForPath(final String path) { }); } client = builder.build(); - client.start(); - ConsoleLog.i("init zk client waiting for connected..."); - if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { - throw new ZKException("init zk error, config=" + zkConfig); - } - initLocalCache(zkConfig.getLocalCachePath()); - registerConnectionLostListener(); - Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); - - ConsoleLog.i("init zk client success..."); + Logs.Console.error("init zk client, config=" + zkConfig); } // 注册连接状态监听器 - private void registerConnectionLostListener() { + private void addConnectionStateListener() { client.getConnectionStateListenable().addListener(new ConnectionStateListener() { //TODO need close jvm? @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - if (ConnectionState.LOST == newState) { - Logs.ZK.info("{} lost connection", MPushUtil.getInetAddress()); - } else if (ConnectionState.RECONNECTED == newState) { - Logs.ZK.info("{} reconnected", MPushUtil.getInetAddress()); - } + Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); } }); } @@ -115,25 +133,6 @@ private void initLocalCache(String cachePath) throws Exception { cache.start(); } - private void waitClose() { - try { - Thread.sleep(600); - } catch (final InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - - /** - * 关闭 - */ - public void close() { - if (null != cache) { - cache.close(); - } - waitClose(); - CloseableUtils.closeQuietly(client); - } - /** * 获取数据,先从本地获取,本地找不到,从远程获取 * @@ -293,12 +292,12 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - Logs.ZK.error("remove:{}", key, ex); + Logs.ZK.error("removeAndClose:{}", key, ex); throw new ZKException(ex); } } - public void registerListener(ZKDataChangeListener listener) { + public void registerListener(ZKNodeCacheWatcher listener) { cache.getListenable().addListener(listener); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java index efa19e4d..8843d450 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -1,6 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC.mp.zk; public class ZKConfig { public static final int ZK_MAX_RETRY = 3; @@ -16,11 +35,11 @@ public class ZKConfig { private String namespace; - private int maxRetry = ZK_MAX_RETRY; + private int maxRetries = ZK_MAX_RETRY; - private int minTime = ZK_MIN_TIME; + private int baseSleepTimeMs = ZK_MIN_TIME; - private int maxTime = ZK_MAX_TIME; + private int maxSleepMs = ZK_MAX_TIME; private int sessionTimeout = ZK_SESSION_TIMEOUT; @@ -32,8 +51,17 @@ public ZKConfig(String hosts) { this.hosts = hosts; } - public static ZKConfig build(String hosts) { - return new ZKConfig(hosts); + public static ZKConfig build() { + return new ZKConfig(zk.server_address) + .setConnectionTimeout(zk.connectionTimeoutMs) + .setDigest(zk.digest) + .setLocalCachePath(zk.local_cache_path) + .setMaxRetries(zk.retry.maxRetries) + .setMaxSleepMs(zk.retry.maxSleepMs) + .setBaseSleepTimeMs(zk.retry.baseSleepTimeMs) + .setNamespace(zk.namespace) + .setSessionTimeout(zk.sessionTimeoutMs) + ; } public String getHosts() { @@ -54,30 +82,30 @@ public ZKConfig setNamespace(String namespace) { return this; } - public int getMaxRetry() { - return maxRetry; + public int getMaxRetries() { + return maxRetries; } - public ZKConfig setMaxRetry(int maxRetry) { - this.maxRetry = maxRetry; + public ZKConfig setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; return this; } - public int getMinTime() { - return minTime; + public int getBaseSleepTimeMs() { + return baseSleepTimeMs; } - public ZKConfig setMinTime(int minTime) { - this.minTime = minTime; + public ZKConfig setBaseSleepTimeMs(int baseSleepTimeMs) { + this.baseSleepTimeMs = baseSleepTimeMs; return this; } - public int getMaxTime() { - return maxTime; + public int getMaxSleepMs() { + return maxSleepMs; } - public ZKConfig setMaxTime(int maxTime) { - this.maxTime = maxTime; + public ZKConfig setMaxSleepMs(int maxSleepMs) { + this.maxSleepMs = maxSleepMs; return this; } @@ -123,9 +151,9 @@ public String toString() { "hosts='" + hosts + '\'' + ", digest='" + digest + '\'' + ", namespace='" + namespace + '\'' + - ", maxRetry=" + maxRetry + - ", minTime=" + minTime + - ", maxTime=" + maxTime + + ", maxRetries=" + maxRetries + + ", baseSleepTimeMs=" + baseSleepTimeMs + + ", maxSleepMs=" + maxSleepMs + ", sessionTimeout=" + sessionTimeout + ", connectionTimeout=" + connectionTimeout + ", localCachePath='" + localCachePath + '\'' + diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKException.java b/mpush-zk/src/main/java/com/mpush/zk/ZKException.java new file mode 100644 index 00000000..92f3bbff --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKException.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKException extends RuntimeException { + + public ZKException() { + } + + public ZKException(String message) { + super(message); + } + + public ZKException(String message, Throwable cause) { + super(message, cause); + } + + public ZKException(Throwable cause) { + super(cause); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java b/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java deleted file mode 100644 index 1040b0cb..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.zk; - -import com.mpush.tools.spi.SPI; - -import java.util.Collection; - -@SPI("connectionServerManage") -public interface ZKNodeManager { - - void addOrUpdate(String fullPath, T application); - - void remove(String fullPath); - - Collection getList(); - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index c13f1a08..ed1c7aa0 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; @@ -5,29 +24,28 @@ public enum ZKPath { REDIS_SERVER("/redis", "machine", "redis注册的地方"), - CONNECTION_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), - PUSH_SERVER("/ps/hosts", "machine", "push server服务器应用注册的路径"), + CONNECT_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"); - ZKPath(String path, String name, String desc) { - this.path = path; + ZKPath(String root, String name, String desc) { + this.root = root; this.name = name; } - private final String path; + private final String root; private final String name; - public String getPath() { - return path; + public String getRootPath() { + return root; } - public String getWatchPath() { - return path + ZKPaths.PATH_SEPARATOR + name; + public String getNodePath() { + return root + ZKPaths.PATH_SEPARATOR + name; } //根据从zk中获取的app的值,拼装全路径 - public String getFullPath(String nodeName) { - return path + ZKPaths.PATH_SEPARATOR + nodeName; + public String getFullPath(String tail) { + return root + ZKPaths.PATH_SEPARATOR + tail; } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java new file mode 100644 index 00000000..c4a3023c --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.mpush.zk.node.ZKNode; + +import java.util.Collection; +import java.util.List; + +public interface ZKNodeCache { + + void addAll(List list); + + void put(String fullPath, T node); + + T remove(String fullPath); + + Collection values(); + + void clear(); + +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java new file mode 100644 index 00000000..338704ab --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java @@ -0,0 +1,62 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.mpush.zk.node.ZKRedisNode; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ZKRedisNodeCache implements ZKNodeCache { + + private List nodes = Collections.emptyList(); + + @Override + public void addAll(List list) { + nodes = list; + } + + @Override + public void put(String fullPath, ZKRedisNode node) { + throw new UnsupportedOperationException("can not put one redis node, name=" + fullPath); + } + + @Override + public ZKRedisNode remove(String fullPath) { + nodes = Collections.emptyList(); + return null; + } + + @Override + public Collection values() { + return nodes; + } + + @Override + public void clear() { + nodes = Collections.emptyList(); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java new file mode 100644 index 00000000..b41a271f --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java @@ -0,0 +1,81 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.google.common.collect.Maps; +import com.mpush.zk.node.ZKServerNode; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class ZKServerNodeCache implements ZKNodeCache { + + private final Logger logger = LoggerFactory.getLogger(ZKServerNodeCache.class); + + protected final Map cache = Maps.newConcurrentMap(); + + @Override + public void addAll(List list) { + + } + + @Override + public void put(String fullPath, ZKServerNode node) { + if (StringUtils.isNotBlank(fullPath) && node != null) { + cache.put(fullPath, node); + } else { + logger.error("fullPath is null or application is null"); + } + printList(); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = cache.remove(fullPath); + printList(); + return node; + } + + @Override + public Collection values() { + return Collections.unmodifiableCollection(cache.values()); + } + + @Override + public void clear() { + cache.clear(); + } + + private void printList() { + for (ZKServerNode app : cache.values()) { + logger.warn(app.toString()); + } + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java deleted file mode 100644 index 2945a837..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.mpush.zk.listener; - -import com.mpush.log.Logs; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; - -public abstract class ZKDataChangeListener implements TreeCacheListener { - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - String path = null == event.getData() ? "" : event.getData().getPath(); - if (path.isEmpty()) { - return; - } - Logs.ZK.info("DataChangeListener:{},{},namespace:{}", path, listenerPath(), client.getNamespace()); - if (path.startsWith(listenerPath())) { - dataChanged(client, event, path); - } - } - - public abstract void initData(); - - public abstract void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception; - - public abstract String listenerPath(); -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java new file mode 100644 index 00000000..4cd661cd --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -0,0 +1,76 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; + +public abstract class ZKNodeCacheWatcher implements TreeCacheListener { + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + ChildData data = event.getData(); + if (data == null) return; + String path = data.getPath(); + if (Strings.isNullOrEmpty(path)) return; + if (path.startsWith(watchPath())) { + switch (event.getType()) { + case NODE_ADDED: + onNodeAdded(path, data.getData()); + break; + case NODE_REMOVED: + onNodeRemoved(path, data.getData()); + break; + case NODE_UPDATED: + onNodeUpdated(path, data.getData()); + break; + } + } + Logs.ZK.info("ZK node data change={}, name={}, listener={}, ns={}", event.getType(), path, watchPath(), client.getNamespace()); + } + + public final void beginWatch() { + beforeWatch(); + ZKClient.I.registerListener(this); + } + + public abstract String watchPath(); + + protected void beforeWatch() { + + } + + protected void onNodeAdded(String path, byte[] data) { + + } + + protected void onNodeRemoved(String path, byte[] data) { + + } + + protected void onNodeUpdated(String path, byte[] data) { + + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java deleted file mode 100644 index d7266ad4..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.mpush.zk.listener; - -import com.google.common.base.Strings; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisRegister; -import com.mpush.tools.spi.ServiceContainer; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collections; -import java.util.List; - -/** - * redis 监控 - */ -public class ZKRedisNodeListener extends ZKDataChangeListener { - private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeListener.class); - - private final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - - // 获取redis列表 - private void _initData() { - logger.warn("start init redis data"); - List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); - redisRegister.init(group); - logger.warn("end init redis data"); - } - - private void dataRemove(ChildData data) { - _initData(); - } - - private void dataAddOrUpdate(ChildData data) { - _initData(); - } - - @SuppressWarnings("unchecked") - private List getRedisGroup(String fullPath) { - String rawGroup = ZKClient.I.get(fullPath); - if (Strings.isNullOrEmpty(rawGroup)) - return Collections.EMPTY_LIST; - List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); - if (group == null) - return Collections.EMPTY_LIST; - return group; - } - - @Override - public void initData() { - _initData(); - } - - @Override - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - logger.warn("RedisPathListener other path:" + data + "," + event.getType().name() + "," + data); - } - - } - - @Override - public String listenerPath() { - return ZKPath.REDIS_SERVER.getPath(); - } -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java new file mode 100644 index 00000000..ef805ff0 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java @@ -0,0 +1,79 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.Jsons; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKRedisNodeCache; +import com.mpush.zk.node.ZKRedisNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * redis 监控 + */ +public class ZKRedisNodeWatcher extends ZKNodeCacheWatcher { + + private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeWatcher.class); + + private final ZKRedisNodeCache cache = new ZKRedisNodeCache(); + + private void refresh() { + String rawGroup = ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); + logger.warn("refresh zk redis node cache, data=" + rawGroup); + if (Strings.isNullOrEmpty(rawGroup)) return; + ZKRedisNode[] group = Jsons.fromJson(rawGroup, ZKRedisNode[].class); + if (group == null) return; + cache.addAll(Arrays.asList(group)); + } + + @Override + protected void beforeWatch() { + refresh(); + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + refresh(); + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + refresh(); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + refresh(); + } + + @Override + public String watchPath() { + return ZKPath.REDIS_SERVER.getRootPath(); + } + + public ZKRedisNodeCache getCache() { + return cache; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java deleted file mode 100644 index 8bc0a15b..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.mpush.zk.listener; - -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; -import com.mpush.tools.Jsons; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public abstract class ZKServerNodeListener> extends ZKDataChangeListener { - - private final Logger log = LoggerFactory.getLogger(ZKServerNodeListener.class); - - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn(this.getClass().getSimpleName() + "other path:" + path + "," + event.getType().name() + "," + data); - } - } - - public void initData() { - log.warn(" start init " + this.getClass().getSimpleName() + " server data"); - initData0(); - log.warn(" end init " + this.getClass().getSimpleName() + " server data"); - } - - public abstract String getRegisterPath(); - - public abstract String getFullPath(String raw); - - public abstract T getManager(); - - private void initData0() { - // 获取机器列表 - List rawData = ZKClient.I.getChildrenKeys(getRegisterPath()); - for (String raw : rawData) { - String fullPath = getFullPath(raw); - ZKServerNode app = getServerNode(fullPath); - getManager().addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data) { - String path = data.getPath(); - getManager().remove(path); - } - - private void dataAddOrUpdate(ChildData data) { - String path = data.getPath(); - byte[] rawData = data.getData(); - ZKServerNode serverApp = Jsons.fromJson(rawData, ZKServerNode.class); - getManager().addOrUpdate(path, serverApp); - } - - private ZKServerNode getServerNode(String fullPath) { - String rawApp = ZKClient.I.get(fullPath); - ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); - return app; - } - - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java new file mode 100644 index 00000000..a88b6c14 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java @@ -0,0 +1,101 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public final class ZKServerNodeWatcher extends ZKNodeCacheWatcher { + private final ZKPath path; + private final ZKServerNodeCache cache; + + public static ZKServerNodeWatcher buildConnect() { + return new ZKServerNodeWatcher(ZKPath.CONNECT_SERVER); + } + + public static ZKServerNodeWatcher buildGateway() { + return new ZKServerNodeWatcher(ZKPath.GATEWAY_SERVER); + } + + public static ZKServerNodeWatcher build(ZKPath path, ZKServerNodeCache cache) { + return new ZKServerNodeWatcher(path, cache); + } + + public ZKServerNodeWatcher(ZKPath path) { + this.path = path; + this.cache = new ZKServerNodeCache(); + } + + public ZKServerNodeWatcher(ZKPath path, ZKServerNodeCache cache) { + this.path = path; + this.cache = cache; + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + ZKServerNode serverApp = Jsons.fromJson(data, ZKServerNode.class); + cache.put(path, serverApp); + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + cache.remove(path); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + ZKServerNode serverApp = Jsons.fromJson(data, ZKServerNode.class); + cache.put(path, serverApp); + } + + @Override + public String watchPath() { + return path.getNodePath(); + } + + @Override + protected void beforeWatch() { + Logs.Console.error("start init zk server data"); + List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); + for (String raw : rawData) { + String fullPath = path.getFullPath(raw); + ZKServerNode app = getServerNode(fullPath); + cache.put(fullPath, app); + } + Logs.Console.error("end init zk server data"); + } + + private ZKServerNode getServerNode(String fullPath) { + String rawApp = ZKClient.I.get(fullPath); + ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); + return app; + } + + public ZKServerNodeCache getCache() { + return cache; + } + +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java new file mode 100644 index 00000000..7a279ead --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public interface ZKNode { + String encode(); +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java new file mode 100644 index 00000000..0d30efec --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; + +import com.mpush.tools.Jsons; +import com.mpush.tools.config.data.RedisGroup; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ZKRedisNode extends RedisGroup implements ZKNode { + private transient String zkPath; + + public String getZkPath() { + return zkPath; + } + + public void setZkPath(String zkPath) { + this.zkPath = zkPath; + } + + @Override + public String encode() { + return Jsons.toJson(this); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java similarity index 57% rename from mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java rename to mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index b56dba59..73d24e84 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -1,13 +1,35 @@ -package com.mpush.zk; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.Jsons; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.ConfigManager; +import com.mpush.zk.ZKPath; /** * 系统配置 */ -public class ZKServerNode { +public class ZKServerNode implements ZKNode { private String ip; @@ -28,17 +50,17 @@ public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { } public static ZKServerNode csNode() { - return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.I.connectionServerPort(), - MPushUtil.getExtranetAddress(), - ZKPath.CONNECTION_SERVER.getWatchPath()); + return new ZKServerNode(Utils.getLocalIp(), + CC.mp.net.connect_server_port, + ConfigManager.I.getPublicIp(), + ZKPath.CONNECT_SERVER.getNodePath()); } public static ZKServerNode gsNode() { - return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.I.gatewayServerPort(), + return new ZKServerNode(Utils.getLocalIp(), + CC.mp.net.gateway_server_port, null, - ZKPath.GATEWAY_SERVER.getWatchPath()); + ZKPath.GATEWAY_SERVER.getNodePath()); } public String getIp() { @@ -99,10 +121,15 @@ public int hashCode() { @Override public String toString() { return "ZKServerNode{" + - "ip='" + ip + '\'' + + "host='" + ip + '\'' + ", port=" + port + ", extranetIp='" + extranetIp + '\'' + ", zkPath='" + zkPath + '\'' + '}'; } + + @Override + public String encode() { + return Jsons.toJson(this); + } } diff --git a/pom.xml b/pom.xml index da4d2d61..5c4d8401 100644 --- a/pom.xml +++ b/pom.xml @@ -40,28 +40,28 @@ mpush-client mpush-test mpush-monitor - mpush-log mpush-zk + mpush-cache UTF-8 UTF-8 UTF-8 - 1.7 + 1.8 com.mpush - 0.0.1 - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} + 0.0.1 + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} 5.0.0.Alpha2 linux-x86_64 @@ -93,6 +93,11 @@ netty-codec-http ${netty.version} + + io.netty + netty-handler + 5.0.0.Alpha2 + @@ -127,60 +132,60 @@ + + ${mpush.groupId} + mpush-test + ${mpush.version} + ${mpush.groupId} mpush-api - ${mpush-api-version} + ${mpush.version} ${mpush.groupId} mpush-tools - ${mpush-tools-version} + ${mpush.version} ${mpush.groupId} mpush-common - ${mpush-common-version} + ${mpush.version} ${mpush.groupId} mpush-netty - ${mpush-netty-version} + ${mpush.version} ${mpush.groupId} mpush-core - ${mpush-core-version} + ${mpush.version} ${mpush.groupId} mpush-client - ${mpush-client-version} + ${mpush.version} ${mpush.groupId} mpush-monitor - ${mpush-monitor-version} - - - ${mpush.groupId} - mpush-log - ${mpush-log-version} + ${mpush.version} ${mpush.groupId} - mpush-test - ${mpush-test-version} + mpush-boot + ${mpush.version} ${mpush.groupId} - mpush-boot - ${mpush-boot-version} + mpush-zk + ${mpush.version} ${mpush.groupId} - mpush-zk - ${mpush-zk-version} + mpush-cache + ${mpush.version} @@ -204,18 +209,14 @@ commons-logging commons-logging 1.1.3 + provided log4j log4j 1.2.17 + provided - - org.logback-extensions - logback-ext-spring - 0.1.2 - - @@ -234,31 +235,16 @@ gson 2.5 - org.aeonbits.owner owner 1.0.9 - - - org.codehaus.janino - janino - 2.7.8 - - - args4j - args4j - 2.33 - - - - commons-net - commons-net - 3.4 + com.typesafe + config + 1.3.0 - @@ -303,12 +289,6 @@ - - daily - - daily - - dev @@ -319,15 +299,9 @@ - pre - - pre - - - - online + pub - online + pub From dbc0e49d1a684e9bacb98dc7d37d09132050eaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 20 Aug 2016 13:03:46 +0800 Subject: [PATCH 563/890] init v2 --- .gitignore | 20 - bin/daily-pub.py | 214 -------- bin/debug.sh | 19 - bin/mp-env.cmd | 49 ++ bin/mp-env.sh | 122 +++++ bin/mp.cmd | 24 + bin/mp.sh | 232 +++++++++ bin/pub-daily.py | 239 --------- bin/pub-online.py | 239 --------- bin/pub-pre.py | 101 ---- bin/run.sh | 20 - bin/set-env.sh | 2 + bin/start.sh | 23 - bin/test.py | 69 --- bin/test.sh | 11 - conf/conf-daily.properties | 16 - conf/conf-dev.properties | 1 + conf/conf-online.properties | 15 - conf/conf-pre.properties | 15 - conf/conf-pub.properties | 1 + conf/reference.conf | 189 +++++++ mpush-api/.gitignore | 1 - .../src/main/java/com/mpush/api/Client.java | 39 -- .../main/java/com/mpush/api/Constants.java | 20 + .../src/main/java/com/mpush/api/Message.java | 19 + .../java/com/mpush/api/MessageHandler.java | 21 +- .../java/com/mpush/api/PacketReceiver.java | 21 +- .../main/java/com/mpush/api/PushContent.java | 76 --- .../main/java/com/mpush/api/PushSender.java | 24 - .../src/main/java/com/mpush/api/Server.java | 23 - .../java/com/mpush/api/connection/Cipher.java | 19 + .../com/mpush/api/connection/Connection.java | 28 +- .../api/connection/ConnectionManager.java | 29 +- .../mpush/api/connection/SessionContext.java | 48 +- .../mpush/api/event/ConnectionCloseEvent.java | 19 + .../main/java/com/mpush/api/event/Event.java | 19 + .../com/mpush/api/event/HandshakeEvent.java | 19 + .../com/mpush/api/event/KickUserEvent.java | 19 + .../mpush/api/event/RouterChangeEvent.java | 19 + .../com/mpush/api/event/UserOfflineEvent.java | 19 + .../com/mpush/api/event/UserOnlineEvent.java | 50 +- .../mpush/api/exception/CryptoException.java | 16 - .../mpush/api/exception/DecodeException.java | 12 - .../mpush/api/exception/MessageException.java | 11 - .../api/exception/SendMessageException.java | 9 - .../java/com/mpush/api/protocol/Command.java | 19 + .../java/com/mpush/api/protocol/Packet.java | 22 +- .../java/com/mpush/api/push/PushContent.java | 94 ++++ .../com/mpush/api/push/PushException.java | 39 ++ .../java/com/mpush/api/push/PushSender.java | 58 +++ .../com/mpush/api/router/ClientLocation.java | 96 +++- .../java/com/mpush/api/router/ClientType.java | 58 +++ .../java/com/mpush/api/router/Router.java | 19 + .../com/mpush/api/router/RouterManager.java | 35 +- .../com/mpush/api/service/BaseService.java | 151 ++++++ .../java/com/mpush/api/service/Client.java | 24 + .../java/com/mpush/api/service/Listener.java | 7 + .../java/com/mpush/api/service/Server.java | 29 ++ .../java/com/mpush/api/service/Service.java | 43 ++ .../mpush/api/service/ServiceException.java | 39 ++ .../main/java/com/mpush/api/spi/Factory.java | 29 ++ .../java/com/mpush/api/spi/SpiLoader.java | 28 +- .../mpush/api/spi/client/PusherFactory.java | 32 ++ .../api/spi/common/ThreadPoolFactory.java | 39 ++ .../com/mpush/api/spi/core/CipherFactory.java | 32 ++ .../com/mpush/api/spi/net/DnsMapping.java | 69 +++ .../mpush/api/spi/net/DnsMappingManager.java | 37 ++ mpush-boot/.gitignore | 1 - mpush-boot/assembly.xml | 40 +- mpush-boot/pom.xml | 10 +- mpush-boot/src/main/java/com/mpush/Main.java | 23 - .../main/java/com/mpush/ServerLauncher.java | 54 -- .../main/java/com/mpush/boot/BootChain.java | 34 -- .../src/main/java/com/mpush/boot/BootJob.java | 26 - .../java/com/mpush/boot/HttpProxyBoot.java | 19 - .../main/java/com/mpush/boot/LastBoot.java | 18 - .../main/java/com/mpush/boot/MonitorBoot.java | 17 - .../main/java/com/mpush/boot/RedisBoot.java | 46 -- .../main/java/com/mpush/boot/ServerBoot.java | 66 --- .../src/main/java/com/mpush/boot/ZKBoot.java | 45 -- .../com/mpush/bootstrap/BootException.java | 35 ++ .../main/java/com/mpush/bootstrap/Main.java | 42 ++ .../com/mpush/bootstrap/ServerLauncher.java | 77 +++ .../com/mpush/bootstrap/job/BootChain.java | 53 ++ .../java/com/mpush/bootstrap/job/BootJob.java | 45 ++ .../mpush/bootstrap/job/HttpProxyBoot.java | 40 ++ .../com/mpush/bootstrap/job/LastBoot.java | 39 ++ .../com/mpush/bootstrap/job/MonitorBoot.java | 35 ++ .../com/mpush/bootstrap/job/RedisBoot.java | 36 ++ .../com/mpush/bootstrap/job/ServerBoot.java | 76 +++ .../java/com/mpush/bootstrap/job/ZKBoot.java | 47 ++ .../src/main/resources/config.properties | 37 -- mpush-boot/src/main/resources/logback.xml | 374 +++++++------- mpush-boot/src/main/resources/mpush.conf | 4 + .../ConnectionServerApplicationTest.java | 13 - .../java/com/mpush/zk/ServerManageTest.java | 80 --- .../src/test/java/com/mpush/zk/ZkTest.java | 16 - .../test/java/com/mpush/zk/ZkUtilTest.java | 148 ------ mpush-cache/pom.xml | 27 + .../com/mpush/cache/redis/RedisClient.java | 303 +++++------ .../com/mpush/cache/redis/RedisException.java | 42 ++ .../com/mpush/cache/redis/RedisGroup.java | 69 +++ .../java/com/mpush/cache/redis}/RedisKey.java | 23 +- .../com/mpush/cache/redis/RedisServer.java | 31 ++ .../cache/redis/hash/ConsistentHash.java | 85 +++ .../java/com/mpush/cache/redis/hash/Node.java | 48 ++ .../redis/listener/ListenerDispatcher.java | 65 +++ .../cache/redis/listener/MessageListener.java | 27 + .../redis/manager/RedisClusterManager.java | 36 ++ .../cache/redis/manager/RedisManager.java | 318 ++++++++++++ .../redis/manager/ZKRedisClusterManager.java | 130 +++++ .../com/mpush/cache/redis/mq/Subscriber.java | 86 ++++ mpush-client/.gitignore | 1 - mpush-client/pom.xml | 34 +- .../mpush/client/connect/ClientConfig.java | 98 ++++ .../connect/ConnClientChannelHandler.java | 237 +++++++++ .../mpush/client/connect/ConnectClient.java | 47 ++ .../mpush/client/gateway/GatewayClient.java | 77 +++ .../gateway/GatewayClientChannelHandler.java | 120 +++++ .../client/gateway/GatewayClientFactory.java | 126 +++++ .../com/mpush/client/push/PushClient.java | 109 ++++ .../mpush/client/push/PushClientFactory.java | 42 ++ .../com/mpush/client/push/PushRequest.java | 215 ++++++++ .../com/mpush/client/push/PushRequestBus.java | 65 +++ .../conn/client/ClientChannelHandler.java | 223 -------- .../java/com/mpush/push/ConnectClient.java | 12 - .../main/java/com/mpush/push/PushClient.java | 47 -- .../main/java/com/mpush/push/PushRequest.java | 206 -------- .../java/com/mpush/push/PushRequestBus.java | 49 -- .../push/client/ClientChannelHandler.java | 97 ---- .../push/zk/listener/ConnectZKListener.java | 35 -- .../push/zk/listener/GatewayZKListener.java | 35 -- .../push/zk/manager/ConnectZKNodeManager.java | 47 -- .../push/zk/manager/GatewayZKNodeManager.java | 83 --- .../com.mpush.api.spi.client.PusherFactory | 1 + .../services/com.mpush.zk.ZKNodeManager | 2 - .../java/com/mpush/client/PushClientTest.java | 50 -- mpush-client/src/test/resources/logback.xml | 30 -- mpush-common/.gitignore | 1 - mpush-common/pom.xml | 6 +- .../java/com/mpush/common/AbstractClient.java | 56 -- .../mpush/common/AbstractEventContainer.java | 9 - .../main/java/com/mpush/common/ErrorCode.java | 20 + .../com/mpush/common/MessageDispatcher.java | 55 +- .../common/handler/BaseMessageHandler.java | 21 +- .../common/handler/ErrorMessageHandler.java | 19 + .../common/handler/OkMessageHandler.java | 21 +- .../mpush/common/manage/user/UserManager.java | 49 -- .../com/mpush/common/message/BaseMessage.java | 46 +- .../mpush/common/message/BindUserMessage.java | 29 ++ .../mpush/common/message/ByteBufMessage.java | 21 +- .../mpush/common/message/ErrorMessage.java | 27 + .../common/message/FastConnectMessage.java | 30 ++ .../common/message/FastConnectOkMessage.java | 27 + .../common/message/HandshakeMessage.java | 37 ++ .../common/message/HandshakeOkMessage.java | 32 ++ .../common/message/HttpRequestMessage.java | 25 +- .../common/message/HttpResponseMessage.java | 25 +- .../mpush/common/message/KickUserMessage.java | 19 + .../com/mpush/common/message/OkMessage.java | 19 + .../com/mpush/common/message/PushMessage.java | 37 +- .../message/gateway/GatewayPushMessage.java | 34 +- .../net/HttpProxyDnsMappingManager.java | 118 +++++ .../router/ConnectionRouterManager.java | 44 +- .../com/mpush/common/router/RemoteRouter.java | 21 +- .../common/router/RemoteRouterManager.java | 104 +++- .../common/router/UserChangeListener.java | 65 ++- .../com/mpush/common/security/AesCipher.java | 50 +- .../com/mpush/common/security/CipherBox.java | 33 +- .../com/mpush/common/security/RsaCipher.java | 49 +- .../common/security/RsaCipherFactory.java | 37 ++ .../com/mpush/common/user/UserManager.java | 68 +++ .../com.mpush.api.spi.core.CipherFactory | 1 + .../com.mpush.api.spi.net.DnsMappingManager | 1 + mpush-core/.gitignore | 1 - mpush-core/pom.xml | 32 +- .../com/mpush/core/handler/AdminHandler.java | 261 +++++++--- .../mpush/core/handler/BindUserHandler.java | 32 +- .../core/handler/FastConnectHandler.java | 34 +- .../core/handler/GatewayPushHandler.java | 82 +-- .../mpush/core/handler/HandshakeHandler.java | 50 +- .../mpush/core/handler/HeartBeatHandler.java | 24 +- .../mpush/core/handler/HttpProxyHandler.java | 80 ++- .../mpush/core/handler/UnbindUserHandler.java | 48 +- .../com/mpush/core/router/KickRemoteMsg.java | 41 +- .../com/mpush/core/router/LocalRouter.java | 42 ++ .../mpush/core/router/LocalRouterManager.java | 87 ++-- .../com/mpush/core/router/RouterCenter.java | 58 ++- .../core/router/RouterChangeListener.java | 77 +-- .../router/UserOnlineOfflineListener.java | 39 +- .../com/mpush/core/server/AdminServer.java | 50 +- .../mpush/core/server/ConnectionServer.java | 81 ++- .../com/mpush/core/server/GatewayServer.java | 54 +- .../core/server/ServerChannelHandler.java | 78 +-- .../core/server/ServerConnectionManager.java | 64 ++- .../mpush/core/session/ReusableSession.java | 19 + .../core/session/ReusableSessionManager.java | 37 +- .../com/mpush/core/netty/NettyServerTest.java | 27 - .../mpush/core/security/CipherBoxTest.java | 25 +- mpush-core/src/test/resources/logback.xml | 48 +- mpush-log/.gitignore | 1 - mpush-log/pom.xml | 41 -- .../src/main/java/com/mpush/log/Logs.java | 331 ------------ .../src/test/java/com/mpush/AppTest.java | 38 -- mpush-monitor/.gitignore | 1 - mpush-monitor/pom.xml | 22 +- .../src/main/java/com/mpush/App.java | 13 - .../com/mpush/monitor/data/MonitorResult.java | 65 +++ .../mpush/monitor/data/ResultCollector.java | 41 ++ .../com/mpush/monitor/domain/MonitorData.java | 67 --- .../com/mpush/monitor/quota/BaseQuota.java | 35 -- .../com/mpush/monitor/quota/GCMQuota.java | 55 +- .../com/mpush/monitor/quota/InfoQuota.java | 27 +- .../com/mpush/monitor/quota/MemoryQuota.java | 81 +-- .../com/mpush/monitor/quota/MonitorQuota.java | 25 + .../mpush/monitor/quota/ThreadPoolQuota.java | 21 +- .../com/mpush/monitor/quota/ThreadQuota.java | 37 +- .../com/mpush/monitor/quota/impl/JVMGC.java | 282 +++++----- .../com/mpush/monitor/quota/impl/JVMInfo.java | 97 ++-- .../mpush/monitor/quota/impl/JVMMemory.java | 486 +++++++++--------- .../mpush/monitor/quota/impl/JVMThread.java | 106 ++-- .../monitor/quota/impl/JVMThreadPool.java | 72 +-- .../monitor/service/MonitorDataCollector.java | 106 ---- .../mpush/monitor/service/MonitorService.java | 117 +++++ .../src/test/java/com/mpush/AppTest.java | 20 +- mpush-netty/.gitignore | 1 - mpush-netty/pom.xml | 10 +- .../netty/client/ChannelClientHandler.java | 11 - .../com/mpush/netty/client/HttpCallback.java | 22 - .../com/mpush/netty/client/HttpClient.java | 15 - .../com/mpush/netty/client/NettyClient.java | 251 +++++---- .../netty/client/NettyClientFactory.java | 75 --- .../mpush/netty/client/NettyHttpClient.java | 262 ---------- .../netty/client/SecurityNettyClient.java | 86 ---- .../mpush/netty/codec/DecodeException.java | 31 ++ .../com/mpush/netty/codec/PacketDecoder.java | 27 +- .../com/mpush/netty/codec/PacketEncoder.java | 19 + .../netty/connection/NettyConnection.java | 50 +- .../com/mpush/netty/http/HttpCallback.java | 41 ++ .../java/com/mpush/netty/http/HttpClient.java | 31 ++ .../mpush/netty/http/HttpClientHandler.java | 126 +++++ .../mpush/netty/http/HttpConnectionPool.java | 85 +++ .../com/mpush/netty/http/NettyHttpClient.java | 150 ++++++ .../RequestContext.java} | 50 +- .../com/mpush/netty/server/NettyServer.java | 86 +++- .../mpush/netty/util/NettySharedHolder.java | 18 - mpush-netty/src/test/resources/logback.xml | 50 +- mpush-test/.gitignore | 1 - mpush-test/pom.xml | 10 - .../src/test/java/com/mpush/AppTest.java | 38 -- .../mpush/test/client/ConnClientTestMain.java | 70 +++ .../test/client/ConnClientTestMain2.java | 71 +++ .../mpush/test/client/ConnectClientBoot.java | 42 ++ .../test/configcenter/ConfigCenterTest.java | 124 ++++- .../mpush/test/connection/body/BodyTest.java | 117 +++-- .../connection/client/ConnectTestClient.java | 20 - .../connection/mpns/ConnClientTestMain.java | 69 +++ .../connection/mpns/ConnectTestClient.java | 20 - .../mpns/ConnectTestClientBoot.java | 38 ++ .../mpush/test/connection/severs/Main.java | 12 - .../java/com/mpush/test/crypto/RsaTest.java | 260 +++++----- .../com/mpush/test/gson/DnsMappingTest.java | 56 +- .../java/com/mpush/test/gson/GsonTest.java | 128 +++-- .../mpush/test/push/PushClientTestMain.java | 71 +++ .../mpush/test/redis/ConsistentHashTest.java | 97 ++-- .../com/mpush/test/redis/MPushUtilTest.java | 31 +- .../java/com/mpush/test/redis/PubSubTest.java | 112 ++-- .../mpush/test/redis/RedisClusterTest.java | 83 +-- .../test/redis/RedisGroupManageTest.java | 109 ---- .../com/mpush/test/redis/RedisUtilTest.java | 386 +++++++------- .../test/java/com/mpush/test/redis/User.java | 90 ++-- .../com/mpush/test/sever/ServerTestMain.java | 33 ++ .../java/com/mpush/test/util/TelnetTest.java | 65 ++- .../src/test/resources/application.conf | 6 + mpush-test/src/test/resources/logback.xml | 48 +- .../services/com.mpush.api.spi.PusherFactory | 1 + mpush-tools/.gitignore | 1 - mpush-tools/pom.xml | 76 +-- .../main/java/com/mpush/tools/ConsoleLog.java | 32 -- .../main/java/com/mpush/tools/Constants.java | 41 -- .../java/com/mpush/tools/GenericsUtil.java | 139 ----- .../main/java/com/mpush/tools/JVMUtil.java | 136 ----- .../src/main/java/com/mpush/tools/Jsons.java | 28 +- .../src/main/java/com/mpush/tools/Pair.java | 28 - .../tools/{MPushUtil.java => Utils.java} | 67 ++- .../com/mpush/tools/common/GenericsUtil.java | 164 ++++++ .../com/mpush/tools/{ => common}/IOUtils.java | 34 +- .../java/com/mpush/tools/common/JVMUtil.java | 187 +++++++ .../java/com/mpush/tools/common/Pair.java | 47 ++ .../mpush/tools/{ => common}/Profiler.java | 33 +- .../com/mpush/tools/{ => common}/Strings.java | 21 +- .../java/com/mpush/tools/common/TimeLine.java | 94 ++++ .../main/java/com/mpush/tools/config/CC.java | 345 +++++++++++++ .../com/mpush/tools/config/ConfigCenter.java | 241 --------- .../com/mpush/tools/config/ConfigManager.java | 55 ++ .../tools/config/DnsMappingConverter.java | 48 -- .../com/mpush/tools/config/MapConverter.java | 27 - .../tools/config/RedisGroupConverter.java | 43 -- .../mpush/tools/config/data/RedisGroup.java | 45 ++ .../mpush/tools/config/data/RedisServer.java | 90 ++++ .../java/com/mpush/tools/crypto/AESUtils.java | 50 +- .../java/com/mpush/tools/crypto/Base64.java | 378 +++++++------- .../com/mpush/tools/crypto/Base64Utils.java | 124 +---- .../mpush/tools/crypto/CryptoException.java | 38 ++ .../java/com/mpush/tools/crypto/MD5Utils.java | 25 +- .../java/com/mpush/tools/crypto/RSAUtils.java | 37 +- .../java/com/mpush/tools/dns/DnsMapping.java | 25 - .../tools/dns/manage/DnsMappingManage.java | 99 ---- .../java/com/mpush/tools/event/Event.java | 21 - .../java/com/mpush/tools/event}/EventBus.java | 29 +- .../com/mpush/tools/event/EventConsumer.java | 28 + .../mpush/tools/event/EventDispatcher.java | 19 - .../com/mpush/tools/event/EventListener.java | 7 - .../java/com/mpush/tools/event/EventType.java | 5 - .../mpush/tools/exception/ZKException.java | 24 - .../main/java/com/mpush/tools/log/Logs.java | 60 +++ .../com/mpush/tools/redis/RedisGroup.java | 49 -- .../java/com/mpush/tools/redis/RedisNode.java | 41 -- .../mpush/tools/redis/RedisPoolConfig.java | 35 -- .../com/mpush/tools/redis/RedisRegister.java | 17 - .../redis/consistenthash/ConsistentHash.java | 66 --- .../tools/redis/consistenthash/Node.java | 26 - .../jedis/services/JedisRegisterManager.java | 78 --- .../redis/listener/ListenerDispatcher.java | 51 -- .../tools/redis/listener/MessageListener.java | 8 - .../mpush/tools/redis/manage/RedisManage.java | 289 ----------- .../mpush/tools/redis/pubsub/Subscriber.java | 68 --- .../main/java/com/mpush/tools/spi/SPI.java | 14 - .../tools/thread/NamedThreadFactory.java | 89 ++-- .../mpush/tools/thread/PoolThreadFactory.java | 61 +++ .../mpush/tools/thread/ThreadNameSpace.java | 46 -- .../com/mpush/tools/thread/ThreadNames.java | 53 ++ .../tools/thread/pool/DefaultExecutor.java | 34 ++ .../thread/pool/DefaultThreadPoolFactory.java | 120 +++++ .../pool/DumpThreadRejectedHandler.java | 74 +++ .../tools/thread/pool/ThreadPoolConfig.java | 135 +++++ .../tools/thread/pool/ThreadPoolManager.java | 135 +++++ .../thread/threadpool/IgnoreRunsPolicy.java | 42 -- .../tools/thread/threadpool/ThreadPool.java | 12 - .../thread/threadpool/ThreadPoolContext.java | 63 --- .../thread/threadpool/ThreadPoolManager.java | 45 -- .../threadpool/cached/CachedThreadPool.java | 46 -- .../cached/CachedThreadPoolContext.java | 23 - .../threadpool/fixed/FixedThreadPool.java | 42 -- .../fixed/FixedThreadPoolContext.java | 23 - ...com.mpush.api.spi.common.ThreadPoolFactory | 1 + .../com.mpush.tools.redis.RedisRegister | 1 - .../java/com/mpush/tools/IOUtilsTest.java | 26 +- .../com/mpush/tools/crypto/AESUtilsTest.java | 21 +- .../com/mpush/tools/crypto/RSAUtilsTest.java | 22 +- .../java/com/mpush/tools/delayqueue/Exam.java | 155 ------ .../java/com/mpush/tools/owner/OwnerTest.java | 70 --- .../com/mpush/tools/owner/ServerConfig.java | 30 -- .../java/com/mpush/tools/spi/SpiTest.java | 60 --- .../com/mpush/tools/spi/test/TestService.java | 11 - .../mpush/tools/spi/test/TestServiceImpl.java | 19 - .../tools/spi/test/TestServiceImpl2.java | 20 - .../java/com/mpush/tools/thread/SyncTest.java | 145 ------ mpush-tools/src/test/resources/logback.xml | 50 +- mpush-zk/.gitignore | 1 - .../src/main/java/com/mpush/zk/ZKClient.java | 111 ++-- .../src/main/java/com/mpush/zk/ZKConfig.java | 70 ++- .../main/java/com/mpush/zk/ZKException.java | 43 ++ .../main/java/com/mpush/zk/ZKNodeManager.java | 16 - .../src/main/java/com/mpush/zk/ZKPath.java | 40 +- .../java/com/mpush/zk/cache/ZKNodeCache.java | 39 ++ .../com/mpush/zk/cache/ZKRedisNodeCache.java | 62 +++ .../com/mpush/zk/cache/ZKServerNodeCache.java | 81 +++ .../zk/listener/ZKDataChangeListener.java | 28 - .../mpush/zk/listener/ZKNodeCacheWatcher.java | 76 +++ .../zk/listener/ZKRedisNodeListener.java | 85 --- .../mpush/zk/listener/ZKRedisNodeWatcher.java | 79 +++ .../zk/listener/ZKServerNodeListener.java | 79 --- .../zk/listener/ZKServerNodeWatcher.java | 101 ++++ .../main/java/com/mpush/zk/node/ZKNode.java | 29 ++ .../java/com/mpush/zk/node/ZKRedisNode.java | 45 ++ .../com/mpush/zk/{ => node}/ZKServerNode.java | 51 +- pom.xml | 114 ++-- 378 files changed, 13261 insertions(+), 10089 deletions(-) delete mode 100644 bin/daily-pub.py delete mode 100644 bin/debug.sh create mode 100644 bin/mp-env.cmd create mode 100644 bin/mp-env.sh create mode 100644 bin/mp.cmd create mode 100644 bin/mp.sh delete mode 100644 bin/pub-daily.py delete mode 100644 bin/pub-online.py delete mode 100644 bin/pub-pre.py delete mode 100644 bin/run.sh create mode 100644 bin/set-env.sh delete mode 100644 bin/start.sh delete mode 100644 bin/test.py delete mode 100644 bin/test.sh delete mode 100644 conf/conf-daily.properties create mode 100644 conf/conf-dev.properties delete mode 100644 conf/conf-online.properties delete mode 100644 conf/conf-pre.properties create mode 100644 conf/conf-pub.properties create mode 100644 conf/reference.conf delete mode 100644 mpush-api/.gitignore delete mode 100644 mpush-api/src/main/java/com/mpush/api/Client.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/PushContent.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/PushSender.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/Server.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/MessageException.java delete mode 100644 mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushContent.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushSender.java create mode 100644 mpush-api/src/main/java/com/mpush/api/router/ClientType.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/BaseService.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Client.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Listener.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Server.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/Service.java create mode 100644 mpush-api/src/main/java/com/mpush/api/service/ServiceException.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/Factory.java rename mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java => mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java (62%) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java delete mode 100644 mpush-boot/.gitignore delete mode 100644 mpush-boot/src/main/java/com/mpush/Main.java delete mode 100644 mpush-boot/src/main/java/com/mpush/ServerLauncher.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/BootChain.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/BootJob.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/LastBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java delete mode 100644 mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/Main.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java delete mode 100644 mpush-boot/src/main/resources/config.properties create mode 100644 mpush-boot/src/main/resources/mpush.conf delete mode 100644 mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkTest.java delete mode 100644 mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java create mode 100644 mpush-cache/pom.xml rename mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java => mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java (62%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java rename {mpush-api/src/main/java/com/mpush/api => mpush-cache/src/main/java/com/mpush/cache/redis}/RedisKey.java (55%) create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushClient.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushRequest.java create mode 100644 mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java delete mode 100644 mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/ConnectClient.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushClient.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushRequest.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/PushRequestBus.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java delete mode 100644 mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java create mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory delete mode 100644 mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager delete mode 100644 mpush-client/src/test/java/com/mpush/client/PushClientTest.java delete mode 100644 mpush-client/src/test/resources/logback.xml delete mode 100644 mpush-common/.gitignore delete mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractClient.java delete mode 100644 mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java delete mode 100644 mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java create mode 100644 mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java create mode 100644 mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java create mode 100644 mpush-common/src/main/java/com/mpush/common/user/UserManager.java create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager delete mode 100644 mpush-core/.gitignore rename mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java => mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java (55%) delete mode 100644 mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java delete mode 100644 mpush-log/.gitignore delete mode 100644 mpush-log/pom.xml delete mode 100644 mpush-log/src/main/java/com/mpush/log/Logs.java delete mode 100644 mpush-log/src/test/java/com/mpush/AppTest.java delete mode 100644 mpush-monitor/.gitignore delete mode 100644 mpush-monitor/src/main/java/com/mpush/App.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java delete mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java delete mode 100644 mpush-netty/.gitignore delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java rename mpush-netty/src/main/java/com/mpush/netty/{client/RequestInfo.java => http/RequestContext.java} (66%) delete mode 100644 mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java delete mode 100644 mpush-test/.gitignore delete mode 100644 mpush-test/src/test/java/com/mpush/AppTest.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java create mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/client/ConnectTestClient.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java create mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java create mode 100644 mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java create mode 100644 mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java create mode 100644 mpush-test/src/test/resources/application.conf create mode 100644 mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory delete mode 100644 mpush-tools/.gitignore delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/Constants.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/Pair.java rename mpush-tools/src/main/java/com/mpush/tools/{MPushUtil.java => Utils.java} (80%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/IOUtils.java (66%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/Pair.java rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Profiler.java (93%) rename mpush-tools/src/main/java/com/mpush/tools/{ => common}/Strings.java (52%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/CC.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/Event.java rename {mpush-common/src/main/java/com/mpush/common => mpush-tools/src/main/java/com/mpush/tools/event}/EventBus.java (52%) create mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/event/EventType.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/log/Logs.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java create mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory delete mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java delete mode 100644 mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java delete mode 100644 mpush-zk/.gitignore create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKException.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java delete mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java rename mpush-zk/src/main/java/com/mpush/zk/{ => node}/ZKServerNode.java (57%) diff --git a/.gitignore b/.gitignore index 3c74f152..a0043cc4 100644 --- a/.gitignore +++ b/.gitignore @@ -23,23 +23,3 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* - -mpush-test/src/test/resources/config.properties - -build.sh - -*.prefs - -conf/conf-dev.properties - -*.properties - -mpush-test/src/test/java/com/mpush/test/connection/client/Main.java - -mpush-test/src/test/java/com/mpush/test/connection/mpns/Main.java - -mpush-test/src/test/java/com/mpush/test/push/Main.java - -*.properties - -conf/conf-dev.properties diff --git a/bin/daily-pub.py b/bin/daily-pub.py deleted file mode 100644 index a5810bd0..00000000 --- a/bin/daily-pub.py +++ /dev/null @@ -1,214 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'daily' - - -class SSH(object): - def __init__(self): - self.client = None - self.chan = None - self.shell = None - - def __enter__(self): - return self - - def __exit__(self, typ, value, trace): - self.close() - - def connect(self, host, port=22, username='root', password=None): - self.client = paramiko.SSHClient() - ##self.client.load_system_host_keys() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=10) - return self - - def close(self): - if self.client: - self.client.close() - - def exec_command(self, cmd, isprint=True): - """执行命令,每次执行都是新的session""" - if not cmd: - return - print_cmd(cmd) - - stdin, stdout, stderr = self.client.exec_command(cmd) - out = stdout.read() - if isprint: - print_out(out) - - err = stderr.read() - if err: - print_out(err) - return out, err - - def _invoke_shell(self): - """创建一个shell""" - self.shell = self.client.invoke_shell(width=200) - is_recv = False - while True: - if self.shell.recv_ready(): - print_out_stream(self.shell.recv(1024)) - is_recv = True - else: - if is_recv: - return - else: - time.sleep(0.1) - - def shell_exec(self, cmd): - """在shell中执行命令,使用的是同一个session""" - if not cmd: - return - - if not self.shell: - self._invoke_shell() - - self.shell.send(cmd + "\n") - - out = '' - is_recv = False - while True: - if self.shell.recv_ready(): - tmp = self.shell.recv(1024) - out += tmp - print_out_stream(tmp) - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -def getPid(ssh): - stdout = ssh.shell_exec(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%PROCESS_KEY_WORD) - return stdout -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean) - runShell('mvn package -P %s'%ENV) - print showText('package success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.shell_exec('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - - - ##4 kill process - pid = getPid(ssh) - if pid : - ssh.shell_exec('kill -9 %s'%pid) - else: - print showText('there is no process to kill','YELLOW') - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.shell_exec('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME)) - print showText('tar success','greenText') - - ##7 start process - ssh.shell_exec('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/debug.sh b/bin/debug.sh deleted file mode 100644 index 82a5fc7d..00000000 --- a/bin/debug.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -ENV=daily -cd `dirname $0` -cd .. - -echo "start package project..." -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd ./mpush-boot/target -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." -cd mpush -java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 -Dio.netty.leakDetectionLevel=advanced -jar boot.jar & - -echo "end start mpush..." diff --git a/bin/mp-env.cmd b/bin/mp-env.cmd new file mode 100644 index 00000000..fa39e14c --- /dev/null +++ b/bin/mp-env.cmd @@ -0,0 +1,49 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +set MPCFGDIR=%~dp0%..\conf +set MP_LOG_DIR=%~dp0%.. +set MP_LOG4J_PROP=INFO,CONSOLE + +REM for sanity sake assume Java 1.6 +REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html + +REM add the mpcfg dir to classpath +set CLASSPATH=%MPCFGDIR% + +REM make it work in the release +SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% + +REM make it work for developers +SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% + +set MPCFG=%MPCFGDIR%\zoo.cfg + +@REM setup java environment variables + +if not defined JAVA_HOME ( + echo Error: JAVA_HOME is not set. + goto :eof +) + +set JAVA_HOME=%JAVA_HOME:"=% + +if not exist "%JAVA_HOME%"\bin\java.exe ( + echo Error: JAVA_HOME is incorrectly set. + goto :eof +) + +set JAVA="%JAVA_HOME%"\bin\java diff --git a/bin/mp-env.sh b/bin/mp-env.sh new file mode 100644 index 00000000..618dee65 --- /dev/null +++ b/bin/mp-env.sh @@ -0,0 +1,122 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script should be sourced into other mpush +# scripts to setup the env variables + +# We use MPCFGDIR if defined, +# otherwise we use /etc/mp +# or the conf directory that is +# a sibling of this script's directory + +MPBINDIR="${MPBINDIR:-/usr/bin}" +MPUSH_PREFIX="${MPBINDIR}/.." + +if [ "x$MPCFGDIR" = "x" ] +then + if [ -e "${MPUSH_PREFIX}/conf" ]; then + MPCFGDIR="$MPBINDIR/../conf" + else + MPCFGDIR="$MPBINDIR/../etc/mpush" + fi +fi + +if [ -f "${MPBINDIR}/set-env.sh" ]; then + . "${MPBINDIR}/set-env.sh" +fi + +if [ "x$MPCFG" = "x" ] +then + MPCFG="mpush.conf" +fi + +MPCFG="$MPCFGDIR/$MPCFG" + +if [ -f "$MPBINDIR/java.env" ] +then + . "$MPBINDIR/java.env" +fi + +if [ "x${MP_DATADIR}" = "x" ] +then + MP_DATADIR="${MPUSH_PREFIX}/tmp" +fi + +if [ "x${MP_LOG_DIR}" = "x" ] +then + MP_LOG_DIR="${MPUSH_PREFIX}/logs" +fi + +if [ "x${MP_LOG4J_PROP}" = "x" ] +then + MP_LOG4J_PROP="INFO,CONSOLE" +fi + +if [ "$JAVA_HOME" != "" ]; then + JAVA="$JAVA_HOME/bin/java" +else + JAVA=java +fi + + +#add the conf dir to classpath +CLASSPATH="$MPCFGDIR:$CLASSPATH" + +for i in "$MPBINDIR"/../src/java/lib/*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work in the binary package +#(use array for LIBPATH to account for spaces within wildcard expansion) +if [ -e "${MPUSH_PREFIX}"/share/mpush/mpush-*.jar ]; then + LIBPATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) +else + #release tarball format + for i in "$MPBINDIR"/../mpush-*.jar + do + CLASSPATH="$i:$CLASSPATH" + done + LIBPATH=("${MPBINDIR}"/../lib/*.jar) +fi + +for i in "${LIBPATH[@]}" +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work for developers +for d in "$MPBINDIR"/../build/lib/*.jar +do + CLASSPATH="$d:$CLASSPATH" +done + +#make it work for developers +CLASSPATH="$MPBINDIR/../build/classes:$CLASSPATH" + + +case "`uname`" in + CYGWIN*) cygwin=true ;; + *) cygwin=false ;; +esac + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + +#echo "CLASSPATH=$CLASSPATH" \ No newline at end of file diff --git a/bin/mp.cmd b/bin/mp.cmd new file mode 100644 index 00000000..f578f4e0 --- /dev/null +++ b/bin/mp.cmd @@ -0,0 +1,24 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0mpEnv.cmd" + +set MPMAIN="-jar ../boot.jar" +echo on +call %JAVA% "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% "%MPCFG%" %* + +endlocal diff --git a/bin/mp.sh b/bin/mp.sh new file mode 100644 index 00000000..c40e2d3d --- /dev/null +++ b/bin/mp.sh @@ -0,0 +1,232 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + + + +# use POSTIX interface, symlink is followed automatically +MPBIN="${BASH_SOURCE-$0}" +MPBIN="$(dirname "${MPBIN}")" +MPBINDIR="$(cd "${MPBIN}"; pwd)" + +if [ -e "$MPBIN/../libexec/mp-env.sh" ]; then + . "$MPBINDIR/../libexec/mp-env.sh" +else + . "$MPBINDIR/mp-env.sh" +fi + +# See the following page for extensive details on setting +# up the JVM to accept JMX remote management: +# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html +# by default we allow local JMX connections +if [ "x$JMXLOCALONLY" = "x" ] +then + JMXLOCALONLY=false +fi + +if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ] +then + echo "MPush JMX enabled by default" >&2 + if [ "x$JMXPORT" = "x" ] + then + # for some reason these two options are necessary on jdk6 on Ubuntu + # accord to the docs they are not necessary, but otw jconsole cannot + # do a local attach + MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" + else + if [ "x$JMXAUTH" = "x" ] + then + JMXAUTH=false + fi + if [ "x$JMXSSL" = "x" ] + then + JMXSSL=false + fi + if [ "x$JMXLOG4J" = "x" ] + then + JMXLOG4J=true + fi + echo "MPush remote JMX Port set to $JMXPORT" >&2 + echo "MPush remote JMX authenticate set to $JMXAUTH" >&2 + echo "MPush remote JMX ssl set to $JMXSSL" >&2 + echo "MPush remote JMX log4j set to $JMXLOG4J" >&2 + MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" + fi +else + echo "JMX disabled by user request" >&2 + MPMAIN="" +fi + +MPMAIN="$MPMAIN -jar $MPBINDIR/bootstrap.jar" + +if [ "x$SERVER_JVMFLAGS" != "x" ] +then + JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" +fi + +if [ "x$2" != "x" ] +then + MPCFG="$MPCFGDIR/$2" +fi + +# if we give a more complicated path to the config, don't screw around in $MPCFGDIR +if [ "x$(dirname "$MPCFG")" != "x$MPCFGDIR" ] +then + MPCFG="$2" +fi + +if $cygwin +then + MPCFG=`cygpath -wp "$MPCFG"` + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +echo "Using config: $MPCFG" >&2 + +case "$OSTYPE" in +*solaris*) + GREP=/usr/xpg4/bin/grep + ;; +*) + GREP=grep + ;; +esac +if [ -z "$MPPIDFILE" ]; then +# MP_DATADIR="$($GREP "^[[:space:]]*dataDir" "$MPCFG" | sed -e 's/.*=//')" + if [ ! -d "$MP_DATADIR" ]; then + mkdir -p "$MP_DATADIR" + fi + MPPIDFILE="$MP_DATADIR/mpush_server.pid" +else + # ensure it exists, otw stop will fail + mkdir -p "$(dirname "$MPPIDFILE")" +fi + +if [ ! -w "$MP_LOG_DIR" ] ; then +echo $MP_LOG_DIR +mkdir -p "$MP_LOG_DIR" +fi + +_MP_DAEMON_OUT="$MP_LOG_DIR/mpush.out" + +case $1 in +start) + echo -n "Starting mpush ... " + if [ -f "$MPPIDFILE" ]; then + if kill -0 `cat "$MPPIDFILE"` > /dev/null 2>&1; then + echo $command already running as process `cat "$MPPIDFILE"`. + exit 0 + fi + fi + nohup "$JAVA" "-Dmp.conf=$MPCFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $MPMAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & + if [ $? -eq 0 ] + then + case "$OSTYPE" in + *solaris*) + /bin/echo "${!}\\c" > "$MPPIDFILE" + ;; + *) + /bin/echo -n $! > "$MPPIDFILE" + ;; + esac + if [ $? -eq 0 ]; + then + sleep 1 + echo STARTED + else + echo FAILED TO WRITE PID + exit 1 + fi + else + echo SERVER DID NOT START + exit 1 + fi + ;; +start-foreground) + MP_CMD=(exec "$JAVA") + if [ "${MP_NOEXEC}" != "" ]; then + MP_CMD=("$JAVA") + fi + "${MP_CMD[@]}" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $MPMAIN "-Dmp.conf=$MPCFG" + ;; +print-cmd) + echo "\"$JAVA\" $MPMAIN " + echo "\"-Dmp.conf=$MPCFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " + echo "$JVMFLAGS " + echo "-cp \"$CLASSPATH\" " + echo "> \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" + ;; +stop) + echo -n "Stopping mpush ... " + if [ ! -f "$MPPIDFILE" ] + then + echo "no mpush to stop (could not find file $MPPIDFILE)" + else + $KILL -9 $(cat "$MPPIDFILE") + rm "$MPPIDFILE" + echo STOPPED + fi + exit 0 + ;; +upgrade) + shift + echo "upgrading the servers to 3.*" + "$JAVA" "-Dmpush.log.dir=${MP_LOG_DIR}" "-Dmpush.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS com.mpush.tools.upgrade.UpgradeMain ${@} + echo "Upgrading ... " + ;; +restart) + shift + "$0" stop ${@} + sleep 5 + "$0" start ${@} + ;; +status) + # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + if ! [ $clientPortAddress ] + then + clientPortAddress="localhost" + fi + clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + STAT=`"$JAVA" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS org.apache.mpush.client.FourLetterWordMain \ + $clientPortAddress $clientPort srvr 2> /dev/null \ + | $GREP Mode` + if [ "x$STAT" = "x" ] + then + echo "Error contacting service. It is probably not running." + exit 1 + else + echo $STAT + exit 0 + fi + ;; +*) + echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2 + +esac diff --git a/bin/pub-daily.py b/bin/pub-daily.py deleted file mode 100644 index 9344ed27..00000000 --- a/bin/pub-daily.py +++ /dev/null @@ -1,239 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'daily' - -class Telnet(object): - def __init__(self, chan): - self.chan = chan - - def send(self, cmd,isprint=True): - self.chan.send(cmd+'\n') - print_cmd(cmd) - out = '' - is_recv = False - is_recv_err = False - while True: - # 结束 - if self.chan.recv_stderr_ready(): - tmp = self.chan.recv_stderr(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv_err = True - else: - if is_recv_err: - return out - else: - time.sleep(0.1) - - if self.chan.recv_ready(): - tmp = self.chan.recv(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - def telnet(self, cmd,isprint=True): - chan = self.client.get_transport().open_session(timeout=10) - chan.exec_command(cmd) - - t = Telnet(chan) - - is_recv = False - - while True: - if chan.recv_ready(): - if isprint: - print_out_stream(chan.recv(1024)) - is_recv = True - else: - if is_recv: - return t - else: - time.sleep(0.1) - - - def close(self): - if self.client: - self.client.close() - -def getPid(keyword,ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - except: - print showText('telnet exception','redText') - - - print showText('start kill process','greenText') - - ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid : - ssh.exe('kill %s'%pid) - sleep(15) - else: - print showText('there is no process to kill','YELLOW') - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid: - ssh.exe('kill -9 %s'%pid) - - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) - print showText('tar success','greenText') - - ##7 start process - ssh.exe('nohup %s -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/pub-online.py b/bin/pub-online.py deleted file mode 100644 index 1d6739ec..00000000 --- a/bin/pub-online.py +++ /dev/null @@ -1,239 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys -import time - -HOSTS = [ - { - 'HOST':'hive1_host', - 'PORT':9092, - 'USER':'root' - }, - { - 'HOST':'hive2_host', - 'PORT':9092, - 'USER':'root' - } -] - -BASEPATH = '/root/mpush' - -MPUSH_TAR_NAME = 'mpush-release.tar.gz' - -PROCESS_KEY_WORD = 'boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/'+MPUSH_TAR_NAME - -JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java' - -ENV= 'online' - -class Telnet(object): - def __init__(self, chan): - self.chan = chan - - def send(self, cmd,isprint=True): - self.chan.send(cmd+'\n') - print_cmd(cmd) - out = '' - is_recv = False - is_recv_err = False - while True: - # 结束 - if self.chan.recv_stderr_ready(): - tmp = self.chan.recv_stderr(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv_err = True - else: - if is_recv_err: - return out - else: - time.sleep(0.1) - - if self.chan.recv_ready(): - tmp = self.chan.recv(1024) - if isprint: - print_out_stream(tmp) - out += tmp - is_recv = True - else: - if is_recv: - return out - else: - time.sleep(0.1) - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - def telnet(self, cmd,isprint=True): - chan = self.client.get_transport().open_session(timeout=10) - chan.exec_command(cmd) - - t = Telnet(chan) - - is_recv = False - - while True: - if chan.recv_ready(): - if isprint: - print_out_stream(chan.recv(1024)) - is_recv = True - else: - if is_recv: - return t - else: - time.sleep(0.1) - - - def close(self): - if self.client: - self.client.close() - -def getPid(keyword,ssh): - stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def print_cmd(s): - """打印执行的命令""" - print yellowText(s) - - -def print_out(s): - """打印执行命令的结果""" - print greenText(s) - - -def print_out_stream(s): - """打印执行命令的结果""" - sys.stdout.write(greenText(s)) - -def sleep(checkCount): - while(checkCount>1): - checkCount = checkCount - 1 - sys.stdout.write(greenText(' . ')) - sys.stdout.flush() - time.sleep(1) - print greenText(' . ') - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 git pull - runShell('git pull origin master') - print showText('git pull master success','greenText') - - ##1 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##2 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(y/n):") - - if confirmPub != 'y': - return - - for item in HOSTS: - - pubHost = raw_input("发布 %s (y/n):"%item['HOST']) - if pubHost != 'y': - return - - ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER']) - - ##3 backup - base = BASEPATH+'/'+MPUSH_TAR_NAME - to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S') - ssh.exe('mv %s %s '%(base,to)) - print showText('backup mpush ok','greenText') - - ## remove zk info - try: - telnet = ssh.telnet('telnet 127.0.0.1 4001') - telnet.send(' ',False) - telnet.send('rcs') ## 删除zk - telnet.send('quit') ## 关闭连接 - except: - print showText('telnet exception','redText') - - - print showText('start kill process','greenText') - - ##4 kill process 先kill执行。等待15秒后,如果进程还是没有杀掉,则执行kill -9 - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid : - ssh.exe('kill %s'%pid) - sleep(15) - else: - print showText('there is no process to kill','YELLOW') - pid = getPid(PROCESS_KEY_WORD,ssh) - if pid: - ssh.exe('kill -9 %s'%pid) - - - ##5 scp - runShell('scp -P %s %s %s:%s'%(item['PORT'],GITLABPATH,item['HOST'],BASEPATH)) - print showText('scp success','greenText') - - ##6 tar package - ssh.exe('cd %s && rm -rf mpush/ && tar -xzvf ./%s'%(BASEPATH,MPUSH_TAR_NAME),False) - print showText('tar success','greenText') - - ##7 start process - ssh.exe('nohup %s -Dio.netty.leakDetectionLevel=advanced -jar %s/mpush/%s >> %s/mpush/nohup.out 2>&1 &'%(JAVA_PATH,BASEPATH,PROCESS_KEY_WORD,BASEPATH)) - print showText('start process success','greenText') - - - ssh.close() - - -if __name__ == "__main__": - main() diff --git a/bin/pub-pre.py b/bin/pub-pre.py deleted file mode 100644 index e3bc8ca8..00000000 --- a/bin/pub-pre.py +++ /dev/null @@ -1,101 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - - -BASEPATH = '/root/mpush' - -STARTPROCESS = 'java -jar boot.jar' - -GITLABPATH = '/data/localgit/mpush/mpush-boot/target/mpush-release.tar.gz' - -ENV= 'pre' - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=22,username='root',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def getPid(ssh): - stdin, stdout, stderr = ssh.exe(''' ps aux|grep "mpush-cs.jar" |grep -v "grep"|awk '{print $2}' ''',False) - return stdout.read().strip() -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - ##0 assembly - runShell('mvn clean') - runShell('mvn package -P %s'%ENV) - print showText('assembly success','greenText') - - ##1 包创建时间 - runShell('stat -c "%%y" %s'%GITLABPATH) - - confirmPub = raw_input("确认发布(Y/N):") - - if confirmPub != 'Y': - return - - ##4 cp - runShell('cp %s %s'%(GITLABPATH,BASEPATH)) - print showText('cp success','greenText') - - ##5 tar package - runShell('cd /root/mpush/ && tar -xzvf ./mpush-jar-with-dependency.tar.gz') - print showText('tar success','greenText') - - ##6 start process - runShell('nohup /opt/shinemo/jdk1.7.0_40/bin/java -Dio.netty.leakDetectionLevel=advanced -jar /root/mpush/mpush/mpush-cs.jar >> /root/mpush/mpush/nohup.out 2>&1 &') - print showText('start process success','greenText') - - -if __name__ == "__main__": - main() diff --git a/bin/run.sh b/bin/run.sh deleted file mode 100644 index d5f5ab08..00000000 --- a/bin/run.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -ENV=dev - -cd `dirname $0` -cd .. - -echo "start tar mpush..." -cd ./mpush-boot/target - -rm -rf mpush -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." - -cd mpush -java \ --Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ --Dio.netty.leakDetectionLevel=advanced \ --jar boot.jar diff --git a/bin/set-env.sh b/bin/set-env.sh new file mode 100644 index 00000000..5325b967 --- /dev/null +++ b/bin/set-env.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +#JVMFLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file diff --git a/bin/start.sh b/bin/start.sh deleted file mode 100644 index 0f807d5a..00000000 --- a/bin/start.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -ENV=dev - -cd `dirname $0` -cd .. - -echo "start package project..." -mvn clean -mvn package -P $ENV - -echo "start tar mpush..." -cd ./mpush-boot/target -tar -xzvf ./mpush-release.tar.gz - -echo "start start mpush..." -cd mpush -nohup java \ --Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7998 \ --Dio.netty.leakDetectionLevel=advanced \ --jar boot.jar >/dev/null 2>&1 & - -echo "end start mpush..." \ No newline at end of file diff --git a/bin/test.py b/bin/test.py deleted file mode 100644 index 8a624cd8..00000000 --- a/bin/test.py +++ /dev/null @@ -1,69 +0,0 @@ -# coding=utf8 - -import paramiko -import datetime -import telnetlib -import os -import sys - - -class SSH(): - def __init__(self): - self.client = None - - def connect(self,host,port=9092,username='shinemo',password=None): - self.client = paramiko.SSHClient() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.client.connect(host, port, username=username, password=password, timeout=120) - return self - - def exe(self,cmd,isprint=True): - if not cmd: - return - print greenText(cmd) - stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True) - if isprint: - for std in stdout.readlines(): - print std, - print stderr.read() - return stdin, stdout, stderr - - - def close(self): - if self.client: - self.client.close() - -def showText(s, typ): - if typ == 'RED': - return redText(s) - elif typ == 'GREEN': - return greenText(s) - elif typ == 'YELLOW': - return yellowText(s) - else: - return s - -def redText(s): - return "\033[1;31m%s\033[0m" % s - -def greenText(s): - return "\033[1;32m%s\033[0m" % s - - -def yellowText(s): - return "\033[1;33m%s\033[0m" % s - -def runShell(c): - print c - os.system(c) - -def main(): - - - confirmPub = raw_input("") - - print(confirmPub) - - -if __name__ == "__main__": - main() diff --git a/bin/test.sh b/bin/test.sh deleted file mode 100644 index 6d2aa540..00000000 --- a/bin/test.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -ENV=dev - -base_dir=`pwd` - -DIR=`dirname $0` - -PROG=`basename $0` - -echo $DIR diff --git a/conf/conf-daily.properties b/conf/conf-daily.properties deleted file mode 100644 index 92ca7c8f..00000000 --- a/conf/conf-daily.properties +++ /dev/null @@ -1,16 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=debug -zk_ip = 115.29.169.109:5666 -zk_digest = shinemoIpo -zk_namespace = mpush-daily -redis_group = 111.1.57.148:6379:ShineMoIpo -private_key = MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB -force_write_redis_group_info = true -connection_server_port = 20882 -gateway_server_port = 4000 -dns_mapping=111.1.57.148=127.0.0.1 -skip_dump=true -admin_port=4001 -remote_ip_mapping=127.0.0.1:111.1.57.148 diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties new file mode 100644 index 00000000..4a36a6eb --- /dev/null +++ b/conf/conf-dev.properties @@ -0,0 +1 @@ +log.level=debug \ No newline at end of file diff --git a/conf/conf-online.properties b/conf/conf-online.properties deleted file mode 100644 index a5e35416..00000000 --- a/conf/conf-online.properties +++ /dev/null @@ -1,15 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=debug -zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -zk_digest = shinemoIpo -zk_namespace = mpush-online -redis_group = 10.161.223.238:6379:ShineMoIpo -private_key = MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g= -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB -force_write_redis_group_info = false -connection_server_port = 3000 -gateway_server_port = 4000 -dns_mapping=api.server.mangguoyisheng.com=10.161.215.146:81,10.161.158.135:81;h5.server.mangguoyisheng.com=10.162.19.217,10.162.93.125 -skip_dump=false -admin_port=4001 diff --git a/conf/conf-pre.properties b/conf/conf-pre.properties deleted file mode 100644 index c022613c..00000000 --- a/conf/conf-pre.properties +++ /dev/null @@ -1,15 +0,0 @@ -#日志根目录 -log.home=/opt/logs/mpush -loglevel=DEBUG -zk_ip = 10.161.155.50:3356,10.162.19.217:3356,10.162.93.125:3356 -zk_digest = shinemoIpo -zk_namespace = mpush-pre -redis_group = 10.161.223.238:6379:ShineMoIpo -private_key = MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA== -public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB -force_write_redis_group_info = true -connection_server_port = 3000 -gateway_server_port = 4000 -dns_mapping=115.29.230.2=127.0.0.1 -skip_dump=false -admin_port=4001 diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties new file mode 100644 index 00000000..a5051a64 --- /dev/null +++ b/conf/conf-pub.properties @@ -0,0 +1 @@ +log.level=warn \ No newline at end of file diff --git a/conf/reference.conf b/conf/reference.conf new file mode 100644 index 00000000..263e7849 --- /dev/null +++ b/conf/reference.conf @@ -0,0 +1,189 @@ +################################################################################################################## +# +# NOTICE: +# +# 系统配置文件,所有列出的项是系统所支持全部配置项 +# 如果要覆盖某项的值可以添加到mpush.conf中。 +# +# 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 +# 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 +# +############################################################################################################## + +mp { + log.level=warn + log.dir=${user.dir}/../logs + + core { + max-packet-size=10k//系统允许传输的最大包的大小 + compress-threshold=10k//数据包启用压缩的临界值,超过该值后对数据进行压缩 + min-heartbeat=10s + max-heartbeat=3m + max-hb-timeout-times=2//允许的心跳连续超时的最大次数 + session-expired-time=1d//用于快速重连的session 过期时间默认1天 + epoll-provider=netty//nio:jdk 自带,netty:有netty实现 + } + + security { + private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" + public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" + aes-key-length=16 + ras-key-length=1024 + } + + net { + connect-server-port=3000 + gateway-server-port=3001 + admin-server-port=3002 + public-host-mapping {//本机局域网IP和公网IP的映射关系 + "10.1.0.32":"111.1.57.148" + } + traffic-shaping { + gateway-client { + enabled:true + check-interval:100ms + write-global-limit:1k + read-global-limit:0 + write-channel-limit:256b + read-channel-limit:0 + } + + gateway-server { + enabled:true + check-interval:100ms + write-global-limit:0 + read-global-limit:10k + write-channel-limit:0 + read-channel-limit:0.5k + } + + connect-server { + enabled:false + check-interval:100ms + write-global-limit:0 + read-global-limit:100k + write-channel-limit:3k + read-channel-limit:3k + } + } + } + + zk { + server-address="127.0.0.1:2181" + namespace=mpush + digest=mpush + local-cache-path=/ + retry { + #initial amount of time to wait between retries + baseSleepTimeMs=3s + #max number of times to retry + maxRetries=3 + #max time in ms to sleep on each retry + maxSleepMs=5s + } + connectionTimeoutMs=5s + sessionTimeoutMs=5s + } + + redis { + write-to-zk=true + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"111.1.57.148" + port:6379 + password:ShineMoIpo + } + ] + ] + config { + maxTotal:8, + maxIdle:4, + minIdle:1, + lifo:true, + fairness:false, + maxWaitMillis:5000, + minEvictableIdleTimeMillis:300000, + softMinEvictableIdleTimeMillis:1800000, + numTestsPerEvictionRun:3, + testOnCreate:false, + testOnBorrow:false, + testOnReturn:false, + testWhileIdle:false, + timeBetweenEvictionRunsMillis:60000, + blockWhenExhausted:true, + jmxEnabled:true, + jmxNamePrefix:pool, + jmxNameBase:pool + } + } + + http { + proxy-enabled=false + max-conn-per-host=5 + default-read-timeout=10s + max-content-length=5m + dns-mapping { + "mpush.com":["127.0.0.1:8080","127.0.0.1:8081"] + } + } + + thread { + pool { + boss { + min:4 + max:16 + queue-size:1000 + } + + work { + min:8 + max:32 + queue-size:1000 + } + + event-bus { + min:4 + max:4 + queue-size:10000 //大量的online,offline, + } + + http-proxy { + min:8 + max:64 + queue-size:1000 + } + + biz { + min:4 + max:64 + queue-size:10 + } + + mq { + min:2 + max:4 + queue-size:10000 + } + + push-callback { + min:2 + max:2 + queue-size:0 + } + } + } + + monitor { + dump-dir=/tmp/logs/mpush/ + dump-stack=false + dump-period=1m + print-log=true + } + + spi { + thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" + dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" + } +} \ No newline at end of file diff --git a/mpush-api/.gitignore b/mpush-api/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-api/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-api/src/main/java/com/mpush/api/Client.java b/mpush-api/src/main/java/com/mpush/api/Client.java deleted file mode 100644 index f63a0ca0..00000000 --- a/mpush-api/src/main/java/com/mpush/api/Client.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.mpush.api; - -import com.mpush.api.connection.Connection; -import io.netty.channel.Channel; - - -public interface Client { - - void init(Channel channel); - - boolean isConnected(); - - String getHost(); - - int getPort(); - - void close(String cause); - - boolean isEnabled(); - - void resetHbTimes(); - - int inceaseAndGetHbTimes(); - - String getUrl(); - - void startHeartBeat() throws Exception; - - void startHeartBeat(final int heartbeat) throws Exception; - - void stop(); - - Connection getConnection(); - - Channel getChannel(); - - void initConnection(Connection connection); - -} diff --git a/mpush-api/src/main/java/com/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java index 11c15dbd..27e5cece 100644 --- a/mpush-api/src/main/java/com/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import java.nio.charset.Charset; @@ -9,6 +28,7 @@ */ public interface Constants { Charset UTF_8 = Charset.forName("UTF-8"); + byte[] EMPTY_BYTES = new byte[0]; String HTTP_HEAD_READ_TIMEOUT = "readTimeout"; diff --git a/mpush-api/src/main/java/com/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java index cbf37d56..581c48fb 100644 --- a/mpush-api/src/main/java/com/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java index e444949a..5d7ee6b0 100644 --- a/mpush-api/src/main/java/com/mpush/api/MessageHandler.java +++ b/mpush-api/src/main/java/com/mpush/api/MessageHandler.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java index da0bffe6..d26e5cf7 100644 --- a/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java +++ b/mpush-api/src/main/java/com/mpush/api/PacketReceiver.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api; -import com.mpush.api.protocol.Packet; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; /** * Created by ohun on 2015/12/22. diff --git a/mpush-api/src/main/java/com/mpush/api/PushContent.java b/mpush-api/src/main/java/com/mpush/api/PushContent.java deleted file mode 100644 index 7abe5307..00000000 --- a/mpush-api/src/main/java/com/mpush/api/PushContent.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.mpush.api; - -import java.io.Serializable; - - -/** - * msgId、msgType 必填 - * msgType=1 :nofication,提醒。 - * 必填:title,content。没有title,则为应用名称。 - * 非必填。nid 通知id,主要用于聚合通知。 - * content 为push message。附加的一些业务属性,都在里边。json格式 - * msgType=2 :非通知消息。不在通知栏展示。 - * 必填:content。 - * msgType=3 :消息+提醒 - * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 - * - * - */ -public final class PushContent implements Serializable{ - private static final long serialVersionUID = -1805329333995385960L; - private String msgId; //返回使用 - private String content; //content - private int msgType; //type - - public PushContent(int msgType) { - this.msgType = msgType; - } - - public static PushContent build(PushType msgType,String content){ - PushContent pushContent = new PushContent(msgType.getValue()); - pushContent.setContent(content); - return pushContent; - } - - public String getMsgId() { - return msgId; - } - - public int getMsgType() { - return msgType; - } - - public void setMsgId(String msgId) { - this.msgId = msgId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public enum PushType{ - NOTIFICATION("提醒",1), - MESSAGE("消息",2), - NOTIFICATIONANDMESSAGE("提醒+消息",3); - - PushType(String desc, int value) { - this.desc = desc; - this.value = value; - } - - private final String desc; - private final int value; - - public String getDesc() { - return desc; - } - public int getValue() { - return value; - } - } - -} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/PushSender.java b/mpush-api/src/main/java/com/mpush/api/PushSender.java deleted file mode 100644 index 83b1d519..00000000 --- a/mpush-api/src/main/java/com/mpush/api/PushSender.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mpush.api; - -import java.util.Collection; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public interface PushSender { - void send(String content, Collection userIds, Callback callback); - - void send(String content, String userId, Callback callback); - - interface Callback { - void onSuccess(String userId); - - void onFailure(String userId); - - void onOffline(String userId); - - void onTimeout(String userId); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/Server.java b/mpush-api/src/main/java/com/mpush/api/Server.java deleted file mode 100644 index f9785345..00000000 --- a/mpush-api/src/main/java/com/mpush/api/Server.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.api; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public interface Server { - - void start(Listener listener); - - void stop(Listener listener); - - void init(); - - boolean isRunning(); - - interface Listener { - void onSuccess(int port); - - void onFailure(Throwable cause); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java index e365f656..3751b49b 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Cipher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; /** diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index f1ea56aa..7eee23f2 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -1,8 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; import com.mpush.api.protocol.Packet; import io.netty.channel.Channel; - import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -35,12 +53,8 @@ public interface Connection { void updateLastReadTime(); - int inceaseAndGetHbTimes(); + long getLastReadTime(); - void resetHbTimes(); + Channel getChannel(); - long getLastReadTime(); - - Channel getChannel(); - } diff --git a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index 554486e9..58f12e76 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -1,9 +1,28 @@ -package com.mpush.api.connection; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.List; +package com.mpush.api.connection; import io.netty.channel.Channel; +import java.util.List; + /** * Created by ohun on 2015/12/30. */ @@ -11,13 +30,13 @@ public interface ConnectionManager { Connection get(Channel channel); - void remove(Channel channel); + Connection removeAndClose(Channel channel); void add(Connection connection); - List getConnections(); + List getConnections(); - void init(); + void init(); void destroy(); } diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index aa94e9bd..46fb0647 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -1,5 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.connection; +import com.mpush.api.router.ClientType; + /** * Created by ohun on 2015/12/22. * @@ -10,8 +31,10 @@ public final class SessionContext { public String osVersion; public String clientVersion; public String deviceId; + public String userId; public int heartbeat; public Cipher cipher; + private int clientType; public void changeCipher(Cipher cipher) { this.cipher = cipher; @@ -37,6 +60,11 @@ public SessionContext setDeviceId(String deviceId) { return this; } + public SessionContext setUserId(String userId) { + this.userId = userId; + return this; + } + public void setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; } @@ -45,9 +73,21 @@ public boolean handshakeOk() { return deviceId != null && deviceId.length() > 0; } - @Override - public String toString() { - return "SessionContext [osName=" + osName + ", osVersion=" + osVersion + ", clientVersion=" + clientVersion + ", deviceId=" + deviceId + ", heartbeat=" + heartbeat + "]"; - } + public int getClientType() { + if (clientType == 0) { + clientType = ClientType.find(osName).type; + } + return clientType; + } + + @Override + public String toString() { + return "SessionContext [osName=" + osName + + ", osVersion=" + osVersion + + ", clientVersion=" + clientVersion + + ", deviceId=" + deviceId + + ", heartbeat=" + heartbeat + + "]"; + } } diff --git a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java index 2eaf18e7..a31dea4b 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/Event.java b/mpush-api/src/main/java/com/mpush/api/event/Event.java index e1043d2d..8174f047 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/Event.java +++ b/mpush-api/src/main/java/com/mpush/api/event/Event.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java index d7cafa10..a3922ea6 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/HandshakeEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java index bcb2608b..fc3321cb 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/KickUserEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; /** diff --git a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java index 926bb71d..c4c5c024 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/RouterChangeEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.router.Router; diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java index a5087299..68cac550 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOfflineEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; diff --git a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java index 6ecb3596..d6053ee9 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/UserOnlineEvent.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.event; import com.mpush.api.connection.Connection; @@ -6,21 +25,22 @@ * 绑定用户的时候才会触发该事件 */ public final class UserOnlineEvent implements Event { - - private final Connection connection; + + private final Connection connection; private final String userId; - - public UserOnlineEvent(Connection connection, String userId) { - this.connection = connection; - this.userId = userId; - } - - public Connection getConnection() { - return connection; - } - public String getUserId() { - return userId; - } - + + public UserOnlineEvent(Connection connection, String userId) { + this.connection = connection; + this.userId = userId; + } + + public Connection getConnection() { + return connection; + } + + public String getUserId() { + return userId; + } + } diff --git a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java b/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java deleted file mode 100644 index add6460c..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/CryptoException.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/23. - * - * @author ohun@live.cn - */ -public class CryptoException extends RuntimeException { - - private static final long serialVersionUID = 368277451733324220L; - - public CryptoException(String message) { - super(message); - } - -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java b/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java deleted file mode 100644 index ce7f8435..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/DecodeException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/23. - * - * @author ohun@live.cn - */ -public class DecodeException extends RuntimeException { - public DecodeException(String message) { - super(message); - } -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java deleted file mode 100644 index d2dd1a3b..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/MessageException.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.api.exception; - -public class MessageException extends RuntimeException { - - private static final long serialVersionUID = 8731698346169093329L; - - public MessageException(String message) { - super(message); - } - -} diff --git a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java b/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java deleted file mode 100644 index 62080127..00000000 --- a/mpush-api/src/main/java/com/mpush/api/exception/SendMessageException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.mpush.api.exception; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class SendMessageException extends RuntimeException { -} diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java index 7e2dae4d..ad2e739a 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.protocol; /** diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index b800417c..d9921802 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.protocol; import io.netty.buffer.ByteBuf; @@ -16,6 +35,7 @@ public final class Packet { public static final byte HB_PACKET_BYTE = -33; public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; + public static final Packet HB_PACKE = new Packet(Command.HEARTBEAT); public byte cmd; //命令 public short cc; //校验码 暂时没有用到 @@ -46,7 +66,7 @@ public int getBodyLength() { return body == null ? 0 : body.length; } - public void setFlag(byte flag) { + public void addFlag(byte flag) { this.flags |= flag; } diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java new file mode 100644 index 00000000..e4e741c3 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContent.java @@ -0,0 +1,94 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.push; + +import java.io.Serializable; + + +/** + * msgId、msgType 必填 + * msgType=1 :nofication,提醒。 + * 必填:title,content。没有title,则为应用名称。 + * 非必填。nid 通知id,主要用于聚合通知。 + * content 为push message。附加的一些业务属性,都在里边。json格式 + * msgType=2 :非通知消息。不在通知栏展示。 + * 必填:content。 + * msgType=3 :消息+提醒 + * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。 + */ +public final class PushContent implements Serializable { + private static final long serialVersionUID = -1805329333995385960L; + private String msgId; //返回使用 + private String content; //content + private int msgType; //type + + public PushContent(int msgType) { + this.msgType = msgType; + } + + public static PushContent build(PushType msgType, String content) { + PushContent pushContent = new PushContent(msgType.getValue()); + pushContent.setContent(content); + return pushContent; + } + + public String getMsgId() { + return msgId; + } + + public int getMsgType() { + return msgType; + } + + public void setMsgId(String msgId) { + this.msgId = msgId; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public enum PushType { + NOTIFICATION("提醒", 1), + MESSAGE("消息", 2), + NOTIFICATIONANDMESSAGE("提醒+消息", 3); + + PushType(String desc, int value) { + this.desc = desc; + this.value = value; + } + + private final String desc; + private final int value; + + public String getDesc() { + return desc; + } + + public int getValue() { + return value; + } + } + +} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushException.java b/mpush-api/src/main/java/com/mpush/api/push/PushException.java new file mode 100644 index 00000000..6c9f3d6a --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.api.push; + +/** + * Created by yxx on 2016/5/28. + * + * @author ohun@live.cn (夜色) + */ +public class PushException extends RuntimeException { + + public PushException(Throwable cause) { + super(cause); + } + + public PushException(String message) { + super(message); + } + + public PushException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java new file mode 100644 index 00000000..3c20996e --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -0,0 +1,58 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.push; + +import com.mpush.api.router.ClientLocation; +import com.mpush.api.service.Service; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.client.PusherFactory; + +import java.util.Collection; +import java.util.concurrent.FutureTask; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public interface PushSender extends Service { + + static PushSender create() { + return SpiLoader.load(PusherFactory.class).get(); + } + + void send(String content, Collection userIds, Callback callback); + + FutureTask send(String content, String userId, Callback callback); + + void send(byte[] content, Collection userIds, Callback callback); + + FutureTask send(byte[] content, String userId, Callback callback); + + interface Callback { + void onSuccess(String userId, ClientLocation location); + + void onFailure(String userId, ClientLocation location); + + void onOffline(String userId, ClientLocation location); + + void onTimeout(String userId, ClientLocation location); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index c7267088..ca1f4102 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -1,5 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; +import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; /** @@ -29,16 +49,15 @@ public final class ClientLocation { */ private String deviceId; + /** + * 链接ID + */ + private String connId; - public String getDeviceId() { - return deviceId; - } - - public ClientLocation setDeviceId(String deviceId) { - this.deviceId = deviceId; - return this; - } - + /** + * 客户端类型 + */ + private transient int clientType; public String getHost() { return host; @@ -53,26 +72,64 @@ public String getOsName() { return osName; } - public ClientLocation setOsName(String osName) { + public void setOsName(String osName) { this.osName = osName; - return this; } public String getClientVersion() { return clientVersion; } - public ClientLocation setClientVersion(String clientVersion) { + public void setClientVersion(String clientVersion) { this.clientVersion = clientVersion; - return this; } - public static ClientLocation from(SessionContext context) { - ClientLocation config = new ClientLocation(); - config.osName = context.osName; - config.clientVersion = context.clientVersion; - config.deviceId = context.deviceId; - return config; + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getConnId() { + return connId; + } + + public void setConnId(String connId) { + this.connId = connId; + } + + public int getClientType() { + if (clientType == 0) { + clientType = ClientType.find(osName).type; + } + return clientType; + } + + public static ClientLocation from(Connection connection) { + SessionContext context = connection.getSessionContext(); + ClientLocation location = new ClientLocation(); + location.osName = context.osName; + location.clientVersion = context.clientVersion; + location.deviceId = context.deviceId; + location.connId = connection.getId(); + return location; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ClientLocation location = (ClientLocation) o; + + return clientType == location.clientType; + } + + @Override + public int hashCode() { + return Integer.hashCode(clientType); } @Override @@ -82,6 +139,7 @@ public String toString() { ", osName='" + osName + '\'' + ", clientVersion='" + clientVersion + '\'' + ", deviceId='" + deviceId + '\'' + + ", connId='" + connId + '\'' + '}'; } } diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientType.java b/mpush-api/src/main/java/com/mpush/api/router/ClientType.java new file mode 100644 index 00000000..14216be5 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientType.java @@ -0,0 +1,58 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.router; + +import java.util.Arrays; + +/** + * Created by ohun on 16/8/18. + * + * @author ohun@live.cn (夜色) + */ +public enum ClientType { + MOBILE(1, "android", "ios"), + PC(2, "windows", "mac", "linux"), + WEB(3, "web", "h5"), + UNKNOWN(-1); + + public final int type; + public final String[] os; + + ClientType(int type, String... os) { + this.type = type; + this.os = os; + } + + public boolean contains(String osName) { + return Arrays.stream(os).anyMatch(s -> s.equalsIgnoreCase(osName)); + } + + public static boolean isSameClient(String osName1, String osName2) { + if (osName1.equals(osName2)) return true; + return find(osName1).contains(osName2); + } + + public static ClientType find(String osName) { + for (ClientType type : values()) { + if (type.contains(osName)) return type; + } + return UNKNOWN; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/Router.java b/mpush-api/src/main/java/com/mpush/api/router/Router.java index f5b1582c..acc39a90 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/Router.java +++ b/mpush-api/src/main/java/com/mpush/api/router/Router.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; /** diff --git a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index 70dd37ae..cd1bc7e3 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -1,5 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.api.router; +import java.util.Set; + /** * Created by ohun on 2015/12/23. * @@ -20,15 +41,25 @@ public interface RouterManager { * 删除路由 * * @param userId + * @param clientType + * @return + */ + boolean unRegister(String userId, int clientType); + + /** + * 查询路由 + * + * @param userId * @return */ - boolean unRegister(String userId); + Set lookupAll(String userId); /** * 查询路由 * * @param userId + * @param clientType * @return */ - R lookup(String userId); + R lookup(String userId, int clientType); } diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java new file mode 100644 index 00000000..8d63c14b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -0,0 +1,151 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public abstract class BaseService implements Service { + + protected final AtomicBoolean started = new AtomicBoolean(); + + @Override + public void init() { + } + + @Override + public boolean isRunning() { + return started.get(); + } + + protected void tryStart(Listener listener, Function function) { + listener = wrap(listener); + if (started.compareAndSet(false, true)) { + try { + init(); + function.apply(listener); + listener.onSuccess("service " + this.getClass().getSimpleName() + " start success"); + } catch (Throwable e) { + listener.onFailure(e); + throw new ServiceException(e); + } + } else { + listener.onFailure(new ServiceException("service already started.")); + } + } + + protected void tryStop(Listener listener, Function function) { + listener = wrap(listener); + if (started.compareAndSet(true, false)) { + try { + function.apply(listener); + listener.onSuccess("service " + this.getClass().getSimpleName() + " stop success"); + } catch (Throwable e) { + listener.onFailure(e); + throw new ServiceException(e); + } + } else { + listener.onFailure(new ServiceException("service already stopped.")); + } + } + + public final Future start() { + FutureListener listener = new FutureListener(); + start(listener); + return listener; + } + + public final Future stop() { + FutureListener listener = new FutureListener(); + stop(listener); + return listener; + } + + @Override + public void start(Listener listener) { + tryStart(listener, this::doStart); + } + + @Override + public void stop(Listener listener) { + tryStop(listener, this::doStop); + } + + protected abstract void doStart(Listener listener) throws Throwable; + + protected abstract void doStop(Listener listener) throws Throwable; + + protected interface Function { + void apply(Listener l) throws Throwable; + } + + /** + * 防止Listener被重复执行 + * + * @param l + * @return + */ + public FutureListener wrap(Listener l) { + if (l == null) return new FutureListener(); + if (l instanceof FutureListener) return (FutureListener) l; + return new FutureListener(l); + } + + protected class FutureListener extends FutureTask implements Listener { + private final Listener l;// 防止Listener被重复执行 + + public FutureListener() { + super(BaseService.this::isRunning); + this.l = null; + } + + public FutureListener(Listener l) { + super(BaseService.this::isRunning); + this.l = l; + } + + @Override + public void onSuccess(Object... args) { + if (isDone()) return;// 防止Listener被重复执行 + set(started.get()); + if (l != null) l.onSuccess(args); + } + + @Override + public void onFailure(Throwable cause) { + if (isDone()) return;// 防止Listener被重复执行 + set(started.get()); + setException(cause); + if (l != null) l.onFailure(cause); + throw new ServiceException(cause); + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + throw new UnsupportedOperationException(); + } + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/Client.java b/mpush-api/src/main/java/com/mpush/api/service/Client.java new file mode 100644 index 00000000..4cb61fb0 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Client.java @@ -0,0 +1,24 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +public interface Client extends Service { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/Listener.java b/mpush-api/src/main/java/com/mpush/api/service/Listener.java new file mode 100644 index 00000000..51d1c18b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Listener.java @@ -0,0 +1,7 @@ +package com.mpush.api.service; + +public interface Listener { + void onSuccess(Object... args); + + void onFailure(Throwable cause); +} \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/service/Server.java b/mpush-api/src/main/java/com/mpush/api/service/Server.java new file mode 100644 index 00000000..5249daba --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Server.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +/** + * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn + */ +public interface Server extends Service { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/Service.java b/mpush-api/src/main/java/com/mpush/api/service/Service.java new file mode 100644 index 00000000..c158751b --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/Service.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.service; + +import java.util.concurrent.Future; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public interface Service { + + void start(Listener listener); + + void stop(Listener listener); + + Future start(); + + Future stop(); + + void init(); + + boolean isRunning(); + +} diff --git a/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java b/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java new file mode 100644 index 00000000..63f05bf4 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/service/ServiceException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.api.service; + +/** + * Created by yxx on 2016/5/27. + * + * @author ohun@live.cn (夜色) + */ +public class ServiceException extends RuntimeException { + + public ServiceException(String message) { + super(message); + } + + public ServiceException(Throwable cause) { + super(cause); + } + + public ServiceException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java new file mode 100644 index 00000000..3e99ce53 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public interface Factory { + T get(); +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java similarity index 62% rename from mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java rename to mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index 3e8cc3d1..59df059f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/ServiceContainer.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -1,11 +1,30 @@ -package com.mpush.tools.spi; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi; import java.util.Iterator; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; -public class ServiceContainer { +public final class SpiLoader { private static final Map CACHE = new ConcurrentHashMap<>(); public static T load(Class clazz) { @@ -32,7 +51,7 @@ public static T load0(Class clazz, String name) { T t = filterByName(factories, name); if (t == null) { - factories = ServiceLoader.load(clazz, ServiceContainer.class.getClassLoader()); + factories = ServiceLoader.load(clazz, SpiLoader.class.getClassLoader()); t = filterByName(factories, name); } @@ -52,7 +71,8 @@ private static T filterByName(ServiceLoader factories, String name) { } else { while (it.hasNext()) { T t = it.next(); - if (name.equals(t.getClass().getSimpleName())) { + if (name.equals(t.getClass().getName()) || + name.equals(t.getClass().getSimpleName())) { return t; } } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java new file mode 100644 index 00000000..6abcd77c --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java @@ -0,0 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.client; + +import com.mpush.api.push.PushSender; +import com.mpush.api.spi.Factory; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public interface PusherFactory extends Factory { + +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java new file mode 100644 index 00000000..84d394cb --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.common; + +import java.util.concurrent.Executor; + +/** + * Created by yxx on 2016/5/20. + * + * @author ohun@live.cn + */ +public interface ThreadPoolFactory { + String SERVER_BOSS = "sb"; + String SERVER_WORK = "sw"; + String HTTP_CLIENT_WORK = "hcw"; + String PUSH_CALLBACK = "pc"; + String EVENT_BUS = "eb"; + String MQ = "r"; + String BIZ = "b"; + + Executor get(String name); +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java new file mode 100644 index 00000000..b3a486b9 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java @@ -0,0 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.core; + +import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.Factory; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public interface CipherFactory extends Factory { + Cipher get(); +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java new file mode 100644 index 00000000..0270d4d3 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.net; + + +import java.net.URL; +import java.util.Objects; + +public class DnsMapping { + private String ip; + private int port; + + public DnsMapping(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public String getIp() { + return ip; + } + + public int getPort() { + return port; + } + + public static DnsMapping parse(String addr) { + String[] host_port = Objects.requireNonNull(addr, "dns mapping can not be null") + .split(":"); + if (host_port.length == 1) { + return new DnsMapping(host_port[0], 80); + } else { + return new DnsMapping(host_port[0], Integer.valueOf(host_port[1])); + } + } + + public String translate(URL uri) { + StringBuilder sb = new StringBuilder(128); + sb.append(uri.getProtocol()).append("://") + .append(ip) + .append(':') + .append(port) + .append(uri.getPath()); + String query = uri.getQuery(); + if (query != null) sb.append('?').append(query); + return sb.toString(); + } + + @Override + public String toString() { + return ip + ":" + port; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java new file mode 100644 index 00000000..17bfc637 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMappingManager.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.net; + +import com.mpush.api.service.Service; +import com.mpush.api.spi.SpiLoader; + +/** + * Created by yxx on 2016/5/23. + * + * @author ohun@live.cn (夜色) + */ +public interface DnsMappingManager extends Service { + + static DnsMappingManager create() { + return SpiLoader.load(DnsMappingManager.class); + } + + DnsMapping lookup(String origin); +} diff --git a/mpush-boot/.gitignore b/mpush-boot/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-boot/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index 8d71b814..24fc2cd4 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -1,27 +1,47 @@ - - release - mpush + + release-${mpush.version} + mpush-${mpush.version} true tar.gz - target/classes/ + ../ - config.properties + LICENSE + README.md + + + + ../bin/ + bin + + *.sh + *.cmd + + + + ../conf/ + conf + + reference.conf + + + + target/classes/ + conf + + mpush.conf target/ - + bin - boot.jar + bootstrap.jar diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index c5eb3d3f..cb0b9a07 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -25,14 +25,10 @@ ${mpush.groupId} mpush-zk - - org.codehaus.janino - janino - - boot + bootstrap ../conf/conf-${deploy.env}.properties @@ -57,9 +53,9 @@ true - lib/ + ../lib/ - com.mpush.Main + com.mpush.bootstrap.Main diff --git a/mpush-boot/src/main/java/com/mpush/Main.java b/mpush-boot/src/main/java/com/mpush/Main.java deleted file mode 100644 index 295480e4..00000000 --- a/mpush-boot/src/main/java/com/mpush/Main.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush; - -import com.mpush.tools.ConsoleLog; - -public class Main { - - public static void main(String[] args) { - ConsoleLog.i("launch app..."); - ServerLauncher launcher = new ServerLauncher(); - launcher.start(); - addHook(launcher); - } - - private static void addHook(final ServerLauncher serverBoot) { - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - serverBoot.stop(); - ConsoleLog.i("jvm exit all server stopped..."); - } - }); - } - -} diff --git a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/ServerLauncher.java deleted file mode 100644 index 87315101..00000000 --- a/mpush-boot/src/main/java/com/mpush/ServerLauncher.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.mpush; - - -import com.mpush.api.Server; -import com.mpush.boot.*; -import com.mpush.core.server.AdminServer; -import com.mpush.core.server.ConnectionServer; -import com.mpush.core.server.GatewayServer; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.zk.ZKServerNode; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ServerLauncher { - private final ZKServerNode csNode = ZKServerNode.csNode(); - - private final ZKServerNode gsNode = ZKServerNode.gsNode(); - - private final Server connectServer = new ConnectionServer(csNode.getPort()); - - private final Server gatewayServer = new GatewayServer(gsNode.getPort()); - - private final Server adminServer = new AdminServer(ConfigCenter.I.adminPort()); - - - public void start() { - BootChain chain = BootChain.chain(); - chain.boot() - .setNext(new RedisBoot())//1.注册redis sever 到ZK - .setNext(new ZKBoot())//2.启动ZK节点数据变化监听 - .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 - .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 - .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 - .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns - .setNext(new MonitorBoot())//7.启动监控 - .setNext(new LastBoot());//8.启动结束 - chain.run(); - } - - public void stop() { - stopServer(gatewayServer); - stopServer(gatewayServer); - stopServer(adminServer); - } - - private void stopServer(Server server) { - if (server != null) { - server.stop(null); - } - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java b/mpush-boot/src/main/java/com/mpush/boot/BootChain.java deleted file mode 100644 index fb4183f4..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/BootChain.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.ConsoleLog; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class BootChain { - private BootJob first = first(); - - public void run() { - first.run(); - } - - public static BootChain chain() { - return new BootChain(); - } - - private BootJob first() { - return new BootJob() { - @Override - public void run() { - ConsoleLog.i("begin run boot chain..."); - next(); - } - }; - } - - public BootJob boot() { - return first; - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java b/mpush-boot/src/main/java/com/mpush/boot/BootJob.java deleted file mode 100644 index 4273651d..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/BootJob.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.ConsoleLog; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public abstract class BootJob { - private BootJob next; - - abstract void run(); - - public void next() { - if (next != null) { - ConsoleLog.i("run next boot [" + next.getClass().getSimpleName() + "]"); - next.run(); - } - } - - public BootJob setNext(BootJob next) { - this.next = next; - return next; - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java deleted file mode 100644 index 850df861..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/HttpProxyBoot.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.boot; - -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.manage.DnsMappingManage; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class HttpProxyBoot extends BootJob { - @Override - void run() { - if (ConfigCenter.I.httpProxyEnable()) { - DnsMappingManage.holder.init(); - } - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java b/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java deleted file mode 100644 index 4be3db0c..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/LastBoot.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.mpush.boot; - -import com.mpush.common.manage.user.UserManager; -import com.mpush.tools.ConsoleLog; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class LastBoot extends BootJob { - @Override - public void run() { - UserManager.INSTANCE.clearUserOnlineData(); - ConsoleLog.i("end run boot chain..."); - ConsoleLog.i("app start success..."); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java deleted file mode 100644 index be85f539..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/MonitorBoot.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mpush.boot; - -import com.mpush.monitor.service.MonitorDataCollector; -import com.mpush.tools.config.ConfigCenter; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public class MonitorBoot extends BootJob { - @Override - void run() { - MonitorDataCollector.start(ConfigCenter.I.skipDump()); - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java deleted file mode 100644 index c13efa35..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/RedisBoot.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.boot; - -import com.google.common.base.Strings; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.zk.ZKClient; - -import java.util.List; - -import static com.mpush.zk.ZKPath.REDIS_SERVER; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class RedisBoot extends BootJob { - - @Override - public void run() { - List groupList = ConfigCenter.I.redisGroups(); - if (groupList.size() > 0) { - if (ConfigCenter.I.forceWriteRedisGroupInfo()) { - register(groupList); - } else if (!ZKClient.I.isExisted(REDIS_SERVER.getPath())) { - register(groupList); - } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getPath()))) { - register(groupList); - } - } else { - throw new RuntimeException("init redis sever fail groupList is null"); - } - - RedisManage.test(groupList); - next(); - } - - private void register(List groupList) { - String data = Jsons.toJson(groupList); - ZKClient.I.registerPersist(REDIS_SERVER.getPath(), data); - ConsoleLog.i("register redis server group success, group=" + data); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java deleted file mode 100644 index ff80675d..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/ServerBoot.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.mpush.boot; - -import com.mpush.api.Server; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Jsons; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKServerNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ServerBoot extends BootJob { - private final Logger logger = LoggerFactory.getLogger(ServerBoot.class); - - private final Server server; - private final ZKServerNode node; - - public ServerBoot(Server server, ZKServerNode node) { - this.server = server; - this.node = node; - } - - @Override - public void run() { - final String serverName = server.getClass().getSimpleName(); - ThreadPoolManager.newThread(serverName, new Runnable() { - @Override - public void run() { - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess(int port) { - String msg = "start " + serverName + " success listen:" + port; - logger.error(msg); - ConsoleLog.i(msg); - if (node != null) { - registerServerToZk(node.getZkPath(), Jsons.toJson(node)); - } - next(); - } - - @Override - public void onFailure(Throwable cause) { - String msg = "start " + serverName + " failure, jvm exit with code -1"; - logger.error(msg); - ConsoleLog.e(cause, msg); - System.exit(-1); - } - }); - } - }).start(); - } - - //step7 注册应用到zk - public void registerServerToZk(String path, String value) { - ZKClient.I.registerEphemeralSequential(path, value); - String msg = "register server node=" + value + " to zk path=" + path; - logger.error(msg); - ConsoleLog.i(msg); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java deleted file mode 100644 index dbdb2360..00000000 --- a/mpush-boot/src/main/java/com/mpush/boot/ZKBoot.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mpush.boot; - -import com.google.common.collect.Lists; -import com.mpush.zk.ZKClient; -import com.mpush.zk.listener.ZKDataChangeListener; -import com.mpush.zk.listener.ZKRedisNodeListener; - -import java.util.List; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ZKBoot extends BootJob { - private List dataChangeListeners = Lists.newArrayList(); - - public ZKBoot() { - registerListener(new ZKRedisNodeListener()); - } - - public void registerListener(ZKDataChangeListener listener) { - dataChangeListeners.add(listener); - } - - private void registerListeners() { - for (ZKDataChangeListener listener : dataChangeListeners) { - ZKClient.I.registerListener(listener); - } - } - - private void initListenerData() { - for (ZKDataChangeListener listener : dataChangeListeners) { - listener.initData(); - } - } - - - @Override - public void run() { - registerListeners(); - initListenerData(); - next(); - } -} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java b/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java new file mode 100644 index 00000000..eb63eb23 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/BootException.java @@ -0,0 +1,35 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class BootException extends RuntimeException { + public BootException(String s) { + super(s); + } + + public BootException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java new file mode 100644 index 00000000..2a0550ff --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap; + +import com.mpush.tools.log.Logs; + +public class Main { + + public static void main(String[] args) { + Logs.init(); + Logs.Console.error("launch mpush server..."); + ServerLauncher launcher = new ServerLauncher(); + launcher.start(); + addHook(launcher); + } + + private static void addHook(final ServerLauncher launcher) { + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + launcher.stop(); + Logs.Console.error("jvm exit, all server stopped..."); + } + }); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java new file mode 100644 index 00000000..18edd96b --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -0,0 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap; + + +import com.mpush.api.service.Server; +import com.mpush.bootstrap.job.*; +import com.mpush.core.server.AdminServer; +import com.mpush.core.server.ConnectionServer; +import com.mpush.core.server.GatewayServer; +import com.mpush.monitor.service.MonitorService; +import com.mpush.tools.config.CC; +import com.mpush.zk.ZKClient; +import com.mpush.zk.node.ZKServerNode; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ServerLauncher { + private final ZKServerNode csNode = ZKServerNode.csNode(); + + private final ZKServerNode gsNode = ZKServerNode.gsNode(); + + private final ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); + + private final GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); + + private final AdminServer adminServer = new AdminServer(CC.mp.net.admin_server_port, connectServer, gatewayServer); + + + public void start() { + BootChain chain = BootChain.chain(); + chain.boot() + .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 + .setNext(new RedisBoot())//2.注册redis sever 到ZK + .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 + .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 + .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 + .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns + .setNext(new MonitorBoot())//7.启动监控 + .setNext(new LastBoot());//8.启动结束 + chain.run(); + } + + public void stop() { + stopServer(connectServer); + stopServer(gatewayServer); + stopServer(adminServer); + ZKClient.I.stop(); + MonitorService.I.stop(); + } + + private void stopServer(Server server) { + if (server != null) { + server.stop(null); + } + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java new file mode 100644 index 00000000..32544f85 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -0,0 +1,53 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class BootChain { + private BootJob first = first(); + + public void run() { + first.run(); + } + + public static BootChain chain() { + return new BootChain(); + } + + private BootJob first() { + return new BootJob() { + @Override + public void run() { + Logs.Console.error("begin run bootstrap chain..."); + next(); + } + }; + } + + public BootJob boot() { + return first; + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java new file mode 100644 index 00000000..32bb5b36 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public abstract class BootJob { + private BootJob next; + + abstract void run(); + + public void next() { + if (next != null) { + Logs.Console.error("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); + next.run(); + } + } + + public BootJob setNext(BootJob next) { + this.next = next; + return next; + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java new file mode 100644 index 00000000..c1a01431 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -0,0 +1,40 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMappingManager; +import com.mpush.common.net.HttpProxyDnsMappingManager; +import com.mpush.tools.config.CC; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class HttpProxyBoot extends BootJob { + @Override + void run() { + if (CC.mp.http.proxy_enabled) { + SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager).start(); + } + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java new file mode 100644 index 00000000..bc194877 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.common.user.UserManager; +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class LastBoot extends BootJob { + @Override + public void run() { + UserManager.I.clearUserOnlineData(); + Logs.Console.error("end run bootstrap chain..."); + Logs.Console.error("==================================================================="); + Logs.Console.error("====================MPUSH SERVER START SUCCESS====================="); + Logs.Console.error("==================================================================="); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java new file mode 100644 index 00000000..d0b59ae7 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -0,0 +1,35 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.monitor.service.MonitorService; + +/** + * Created by yxx on 2016/5/15. + * + * @author ohun@live.cn + */ +public class MonitorBoot extends BootJob { + @Override + void run() { + MonitorService.I.start(); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java new file mode 100644 index 00000000..e98ca3ee --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -0,0 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.cache.redis.manager.RedisManager; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class RedisBoot extends BootJob { + + @Override + public void run() { + RedisManager.I.init(); + next(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java new file mode 100644 index 00000000..86bde932 --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -0,0 +1,76 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import com.mpush.zk.ZKClient; +import com.mpush.zk.node.ZKServerNode; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ServerBoot extends BootJob { + private final Server server; + private final ZKServerNode node; + + public ServerBoot(Server server, ZKServerNode node) { + this.server = server; + this.node = node; + } + + @Override + public void run() { + final String serverName = server.getClass().getSimpleName(); + ThreadPoolManager.I.newThread(serverName, new Runnable() { + @Override + public void run() { + server.init(); + server.start(new Listener() { + @Override + public void onSuccess(Object... args) { + Logs.Console.error("start " + serverName + " success listen:" + args[0]); + if (node != null) { + registerServerToZk(node.getZkPath(), Jsons.toJson(node)); + } + next(); + } + + @Override + public void onFailure(Throwable cause) { + Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); + System.exit(-1); + } + }); + } + }).start(); + } + + //step7 注册应用到zk + public void registerServerToZk(String path, String value) { + ZKClient.I.registerEphemeralSequential(path, value); + Logs.Console.error("register server node=" + value + " to zk name=" + path); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java new file mode 100644 index 00000000..e8c33d1d --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.service.Listener; +import com.mpush.bootstrap.BootException; +import com.mpush.zk.ZKClient; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKBoot extends BootJob { + + @Override + public void run() { + ZKClient.I.start(new Listener() { + @Override + public void onSuccess(Object... args) { + next(); + } + + @Override + public void onFailure(Throwable cause) { + throw new BootException("init zk client failure", cause); + } + }); + } +} diff --git a/mpush-boot/src/main/resources/config.properties b/mpush-boot/src/main/resources/config.properties deleted file mode 100644 index 27237da8..00000000 --- a/mpush-boot/src/main/resources/config.properties +++ /dev/null @@ -1,37 +0,0 @@ -## -max_packet_size = 10240 -## -compress_limit = 10240 -## -min_heartbeat = 10000 -## -max_heartbeat = 180000 -## -max_hb_timeout_times = 2 -## -private_key = ${private_key} -## -public_key = ${public_key} -## -gateway_server_port = ${gateway_server_port} -## -connection_server_port = ${connection_server_port} -## -aes_key_length = 16 -## -ras_key_length = 1024 -## -session_expired_time = 86400 - -## zk ip -zk_ip = ${zk_ip} -zk_namespace = ${zk_namespace} -zk_digest = ${zk_digest} - -redis_group = ${redis_group} -force_write_redis_group_info = ${force_write_redis_group_info} -jvm_log_path = /opt/shinemo/mpush/ -dns_mapping=${dns_mapping} -skip_dump=${skip_dump} -admin_port=${admin_port} -remote_ip_mapping=${remote_ip_mapping} diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml index 17dc0dee..7b8ebb77 100644 --- a/mpush-boot/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -1,209 +1,215 @@ + + + + + + + + + + ${log.home}/mpush.log + true + + ${log.home}/mpush.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/info-mpush.log + + info + ACCEPT + DENY + + true + + ${log.home}/info-mpush.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/debug-mpush.log + + debug + ACCEPT + DENY + + true + + ${log.home}/debug-mpush.log.%d{yyyy-MM-dd} + + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + ${log.home}/monitor-mpush.log + true + + ${log.home}/monitor-mpush.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - ${log.home}/mpush.error.log - - error - ACCEPT - DENY - - true - - ${log.home}/mpush.error.log.%d{yyyy-MM-dd} - - 3 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/conn-mpush.log + true + + ${log.home}/conn-mpush.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - ${log.home}/mpush.info.log - - info - ACCEPT - DENY - - true - - ${log.home}/mpush.info.log.%d{yyyy-MM-dd} - - 3 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/push-mpush.log + true + + ${log.home}/push-mpush.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - - ${log.home}/mpush.log - true - - ${log.home}/mpush.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/heartbeat-mpush.log + true + + ${log.home}/heartbeat-mpush.log.%d{yyyy-MM-dd} + + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - - - - ${log.home}/mpush-monitor.log - true - - ${log.home}/mpush-monitor.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - + + + ${log.home}/redis-mpush.log + true + + ${log.home}/redis-mpush.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + ${log.home}/http-mpush.log + true + + ${log.home}/http-mpush.log.%d{yyyy-MM-dd} + + 5 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + - + + + ${log.home}/zk-mpush.log + true + + ${log.home}/zk-mpush.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + System.out + UTF-8 + + DEBUG + + + %d{HH:mm:ss.SSS} - %msg%n + + + + + + + + + + - + - - - - - ${log.home}/mpush-connection.log - true - - ${log.home}/mpush-connection.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - - - - - ${log.home}/mpush-push.log - true - - ${log.home}/mpush-push.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - + - - - - - - - - - - ${log.home}/mpush-heartbeat.log - true - - ${log.home}/mpush-heartbeat.log.%d{yyyy-MM-dd} - - 30 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - + - - - - - ${log.home}/mpush-redis.log - true - - ${log.home}/mpush-redis.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + + + + + + + + + - - - - - ${log.home}/mpush-http.log - true - - ${log.home}/mpush-http.log.%d{yyyy-MM-dd} - - 5 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - - - - - - - - - ${log.home}/mpush-zk.log - true - - ${log.home}/mpush-zk.log.%d{yyyy-MM-dd} - - 10 - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n - - - - + - - - - - - + + + + diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf new file mode 100644 index 00000000..6ee565ff --- /dev/null +++ b/mpush-boot/src/main/resources/mpush.conf @@ -0,0 +1,4 @@ +mp.log.level=${log.level} +mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" +mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" +mp.zk.namespace=mpush diff --git a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java b/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java deleted file mode 100644 index 2a2d6d5e..00000000 --- a/mpush-boot/src/test/java/com/mpush/ConnectionServerApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mpush; - -import org.junit.Test; - -public class ConnectionServerApplicationTest { - - @Test - public void testJson() throws Exception{ - - - } - -} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java b/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java deleted file mode 100644 index d4c66e7b..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ServerManageTest.java +++ /dev/null @@ -1,80 +0,0 @@ -//package com.mpush.core.zk; -// -//import java.util.concurrent.CountDownLatch; -//import java.util.concurrent.Executor; -//import java.util.concurrent.Executors; -// -//import org.junit.Test; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import MPushUtil; -//import com.mpush.tools.zk.ServerApp; -//import com.mpush.tools.zk.ZKPath; -//import com.mpush.tools.zk.manage.ServerManage; -// -//public class ServerManageTest { -// -// private static Executor executor = Executors.newCachedThreadPool(); -// -// private ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// -// private ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); -// -// @Test -// public void testMulThreadRegisterApp() throws InterruptedException { -// CountDownLatch latch = new CountDownLatch(1); -// for (int i = 1; i <= 2; i++) { -// executor.execute(new Worker("192.168.1." + i, latch)); -// } -// latch.countDown(); -// -// Thread.sleep(Integer.MAX_VALUE); -// } -// -// -// @Test -// public void testServerManageStart() throws InterruptedException { -// manage.start(); -// Thread.sleep(Integer.MAX_VALUE); -// } -// -// -// private static class Worker implements Runnable { -// -// private static final Logger log = LoggerFactory.getLogger(Worker.class); -// -// private final String ip; -// private final CountDownLatch latch; -// -// public Worker(String ip, CountDownLatch latch) { -// this.ip = ip; -// this.latch = latch; -// } -// -// @Override -// public void run() { -// try { -// latch.await(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// log.warn("start init " + ip); -// ServerApp app = new ServerApp(ip, 3000); -// ServerManage manage = new ServerManage(app, ZKPath.CONNECTION_SERVER); -// manage.start(); -// -// try { -// Thread.sleep(20000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// manage.close(); -// -// log.warn("end init " + ip); -// } -// -// } -// -//} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java deleted file mode 100644 index 52c93350..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.zk; - -import org.junit.Test; - - -public class ZkTest { - - @Test - public void remove() { - ZKClient zkRegister = ZKClient.I; - - zkRegister.remove("/"); - - } - -} diff --git a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java b/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java deleted file mode 100644 index 8f49408a..00000000 --- a/mpush-boot/src/test/java/com/mpush/zk/ZkUtilTest.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.mpush.zk; - -import com.google.common.collect.Lists; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -public class ZkUtilTest { - - private ZKClient zkUtil = ZKClient.I; - - @Before - public void setUp() throws Exception { - } - - @Test - public void test() { - - String dubbo = zkUtil.get("/dubbo"); - System.out.println(dubbo); - - List child = zkUtil.getChildrenKeys("/dubbo"); - System.out.println(ToStringBuilder.reflectionToString(child, ToStringStyle.JSON_STYLE)); - - zkUtil.registerPersist("/hi", "hello world"); - - zkUtil.registerEphemeral("/huang", "hi"); - zkUtil.registerEphemeralSequential("/huang2"); - - String huang = zkUtil.get("/huang"); - System.out.println(huang); - - String huang2 = zkUtil.get("/huang2"); - System.out.println(huang2); - - } - - @Test - public void getTest() { - String value = zkUtil.get("/hi"); - System.out.println(value); - } - - /** - * 注册机器到/mpush/allhost 目录下边 - */ - @Test - public void testRegister() { - - String path = "/" + zkUtil.getZKConfig().getNamespace(); - - String prefix = "machine"; - - List hosts = zkUtil.getChildrenKeys(path); - - System.out.println("before register"); - - for (int i = 0; i < hosts.size(); i++) { - String value = zkUtil.get(hosts.get(i)); - System.out.println(hosts.get(i) + "," + value); - } - - System.out.println("start register"); - - zkUtil.registerEphemeralSequential(path + "/" + prefix); - - zkUtil.registerEphemeralSequential(path + "/" + prefix); - - hosts = zkUtil.getChildrenKeys(path); - - for (int i = 0; i < hosts.size(); i++) { - String value = zkUtil.get(path + "/" + hosts.get(i)); - System.out.println(hosts.get(i) + "," + value); - } - - System.out.println("end register"); - - } - - @Test - public void testLocalIp() { - System.out.println(MPushUtil.getLocalIp()); - - } - - @Test - public void testRegisterIp() throws Exception { - String localIp = MPushUtil.getInetAddress(); - ZKServerNode app = new ZKServerNode(); - zkUtil.registerPersist("/" + localIp, Jsons.toJson(app)); - String value = zkUtil.get("/" + localIp); - System.out.println(value); - } - - @Test - public void testRemove() { - zkUtil.remove("/"); - } - - @Test - public void testAddKickOff() { - String localIp = MPushUtil.getInetAddress(); - String kick = "kickoff"; - String ip = "10.1.10.65"; - zkUtil.registerEphemeral("/" + localIp + "/" + kick, ip); - - } - - @Test - public void testAddRedis() { - - RedisNode node1 = new RedisNode("10.1.20.74", 6379, "ShineMoIpo"); - //RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); - - RedisGroup group1 = new RedisGroup(); - group1.addRedisNode(node1); - - /*RedisGroup group2 = new RedisGroup(); - group2.addRedisNode(node2);*/ - - List groupList = Lists.newArrayList(group1); - - zkUtil.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - - - } - - @Test - public void getRedisTest() { - String value = zkUtil.get(ZKPath.REDIS_SERVER.getPath()); - List newGroupList = Jsons.fromJsonToList(value, RedisGroup[].class); - for (RedisGroup group : newGroupList) { - for (RedisNode node : group.getRedisNodeList()) { - System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); - } - - } - } - - -} diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml new file mode 100644 index 00000000..0f1b4dae --- /dev/null +++ b/mpush-cache/pom.xml @@ -0,0 +1,27 @@ + + + + mpush + com.mpush + 1.0 + + 4.0.0 + + ${mpush.groupId} + mpush-cache + ${mpush.version} + + + + ${mpush.groupId} + mpush-zk + + + redis.clients + jedis + + + + \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java similarity index 62% rename from mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 6c160b30..2ff4c2cf 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisUtil.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -1,38 +1,80 @@ -package com.mpush.tools.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mpush.log.Logs; -import com.mpush.tools.Constants; import com.mpush.tools.Jsons; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import redis.clients.jedis.*; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class RedisUtil { - - private static Map holder = Maps.newConcurrentMap(); - - public static Jedis getClient(RedisNode node) { - JedisPool pool = holder.get(node); +import java.util.*; + +import static redis.clients.jedis.Protocol.DEFAULT_TIMEOUT; + +public class RedisClient { + public static final JedisPoolConfig CONFIG = buildConfig(); + private static final Map POOL_MAP = Maps.newConcurrentMap(); + + private static JedisPoolConfig buildConfig() { + JedisPoolConfig config = new JedisPoolConfig(); + //连接池中最大连接数。高版本:maxTotal,低版本:maxActive + config.setMaxTotal(CC.mp.redis.config.maxTotal); + //连接池中最大空闲的连接数 + config.setMaxIdle(CC.mp.redis.config.maxIdle); + //连接池中最少空闲的连接数 + config.setMinIdle(CC.mp.redis.config.minIdle); + //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait + config.setMaxWaitMillis(CC.mp.redis.config.maxWaitMillis); + //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 + config.setMinEvictableIdleTimeMillis(CC.mp.redis.config.minEvictableIdleTimeMillis); + //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 + config.setNumTestsPerEvictionRun(CC.mp.redis.config.numTestsPerEvictionRun); + //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 + config.setTimeBetweenEvictionRunsMillis(CC.mp.redis.config.timeBetweenEvictionRunsMillis); + //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. + config.setTestOnBorrow(CC.mp.redis.config.testOnBorrow); + //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 + config.setTestOnReturn(CC.mp.redis.config.testOnReturn); + //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. + config.setTestWhileIdle(CC.mp.redis.config.testWhileIdle); + return config; + } + + public static Jedis getClient(RedisServer node) { + JedisPool pool = POOL_MAP.get(node); if (pool == null) { - pool = new JedisPool(RedisPoolConfig.config, node.getIp(), node.getPort(), Constants.REDIS_TIMEOUT, node.getPassword()); - holder.put(node, pool); + pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), DEFAULT_TIMEOUT, node.getPassword()); + POOL_MAP.put(node, pool); } return pool.getResource(); } public static void close(Jedis jedis) { - jedis.close(); + if (jedis != null) jedis.close(); } - public static long incr(List nodeList, String key, Integer time) { + public static long incr(List nodeList, String key, Integer time) { long incrRet = -1; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -42,7 +84,7 @@ public static long incr(List nodeList, String key, Integer time) { } incrRet = ret; } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{},{},{},{}", key, time, node, e); + Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -52,16 +94,16 @@ public static long incr(List nodeList, String key, Integer time) { } - public static long incrBy(List nodeList, String key, long delt) { + public static long incrBy(List nodeList, String key, long delt) { long incrRet = -1; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); long ret = jedis.incrBy(key, delt); incrRet = ret; } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{},{},{},{}", key, delt, node, e); + Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, delt, node, e); } finally { // 返还到连接池 close(jedis); @@ -79,7 +121,7 @@ public static long incrBy(List nodeList, String key, long delt) { * @return */ @SuppressWarnings("unchecked") - public static T get(RedisNode node, String key, Class clazz) { + public static T get(RedisServer node, String key, Class clazz) { String value = null; Jedis jedis = null; @@ -87,7 +129,7 @@ public static T get(RedisNode node, String key, Class clazz) { jedis = getClient(node); value = jedis.get(key); } catch (Exception e) { - Logs.REDIS.error("redis get exception:{},{}", key, node, e); + Logs.REDIS.error("redis get exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -96,17 +138,15 @@ public static T get(RedisNode node, String key, Class clazz) { return Jsons.fromJson(value, clazz); } - public static void set(List nodeList, String key, String value) { - + public static void set(List nodeList, String key, String value) { set(nodeList, key, value, null); - } - public static void set(List nodeList, String key, T value) { + public static void set(List nodeList, String key, T value) { set(nodeList, key, value, null); } - public static void set(List nodeList, String key, T value, Integer time) { + public static void set(List nodeList, String key, T value, Integer time) { String jsonValue = Jsons.toJson(value); set(nodeList, key, jsonValue, time); } @@ -117,11 +157,11 @@ public static void set(List nodeList, String key, T value, Intege * @param value * @param time seconds */ - public static void set(List nodeList, String key, String value, Integer time) { + public static void set(List nodeList, String key, String value, Integer time) { if (time == null) { time = -1; } - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -130,7 +170,7 @@ public static void set(List nodeList, String key, String value, Integ jedis.expire(key, time); } } catch (Exception e) { - Logs.REDIS.error("redis set exception:{},{},{},{}", key, value, time, node, e); + Logs.REDIS.error("redis set exception:{}, {}, {}, {}", key, value, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -138,15 +178,15 @@ public static void set(List nodeList, String key, String value, Integ } } - public static void del(List nodeList, String key) { + public static void del(List nodeList, String key) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); jedis.del(key); } catch (Exception e) { - Logs.REDIS.error("redis del exception:{},{}", key, node, e); + Logs.REDIS.error("redis del exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -160,14 +200,14 @@ public static void del(List nodeList, String key) { /********************* * hash redis start ********************************/ - public static void hset(List nodeList, String namespace, String key, String value) { - for (RedisNode node : nodeList) { + public static void hset(List nodeList, String key, String field, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hset(namespace, key, value); + jedis.hset(key, field, value); } catch (Exception e) { - Logs.REDIS.error("redis hset exception:{},{},{},{}", namespace, key, value, node, e); + Logs.REDIS.error("redis hset exception:{}, {}, {}, {}", key, field, value, node, e); } finally { // 返还到连接池 close(jedis); @@ -175,19 +215,19 @@ public static void hset(List nodeList, String namespace, String key, } } - public static void hset(List nodeList, String namespace, String key, T value) { - hset(nodeList, namespace, key, Jsons.toJson(value)); + public static void hset(List nodeList, String key, String field, T value) { + hset(nodeList, key, field, Jsons.toJson(value)); } - public static T hget(RedisNode node, String namespace, String key, Class clazz) { + public static T hget(RedisServer node, String key, String field, Class clazz) { String value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hget(namespace, key); + value = jedis.hget(key, field); } catch (Exception e) { - Logs.REDIS.error("redis hget exception:{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hget exception:{}, {}", key, field, node, e); } finally { // 返还到连接池 close(jedis); @@ -196,15 +236,15 @@ public static T hget(RedisNode node, String namespace, String key, Class } - public static void hdel(List nodeList, String namespace, String key) { + public static void hdel(List nodeList, String key, String field) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hdel(namespace, key); + jedis.hdel(key, field); } catch (Exception e) { - Logs.REDIS.error("redis hdel exception:{},{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hdel exception:{}, {}, {}", key, field, node, e); } finally { // 返还到连接池 close(jedis); @@ -212,14 +252,14 @@ public static void hdel(List nodeList, String namespace, String key) } } - public static Map hgetAll(RedisNode node, String namespace) { + public static Map hgetAll(RedisServer node, String key) { Map result = null; Jedis jedis = null; try { jedis = getClient(node); - result = jedis.hgetAll(namespace); + result = jedis.hgetAll(key); } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); + Logs.REDIS.error("redis hgetAll exception:{}, {}", key, node, e); } finally { // 返还到连接池 close(jedis); @@ -227,32 +267,21 @@ public static Map hgetAll(RedisNode node, String namespace) { return result; } - public static Map hgetAll(RedisNode node, String namespace, Class clazz) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(namespace); - } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{},{}", namespace, node, e); - } finally { - // 返还到连接池 - close(jedis); - } + public static Map hgetAll(RedisServer node, String key, Class clazz) { + Map result = hgetAll(node, key); if (result != null) { Map newMap = Maps.newHashMap(); Iterator> iterator = result.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - String key = entry.getKey(); - String val = entry.getValue(); - newMap.put(key, Jsons.fromJson(val, clazz)); + String k = entry.getKey(); + String v = entry.getValue(); + newMap.put(k, Jsons.fromJson(v, clazz)); } return newMap; } else { return null; } - } /** @@ -262,7 +291,7 @@ public static Map hgetAll(RedisNode node, String namespace, Class * @param key * @return */ - public static Set hkeys(RedisNode node, String key) { + public static Set hkeys(RedisServer node, String key) { Set result = null; Jedis jedis = null; try { @@ -282,32 +311,24 @@ public static Set hkeys(RedisNode node, String key) { * 返回 key 指定的哈希集中指定字段的值 * * @param node - * @param key - * @param clazz * @param fields + * @param clazz * @return */ - public static List hmget(RedisNode node, String namespace, Class clazz, String... key) { + public static List hmget(RedisServer node, String key, Class clazz, String... fields) { List value = null; Jedis jedis = null; try { jedis = getClient(node); - value = jedis.hmget(namespace, key); + value = jedis.hmget(key, fields); } catch (Exception e) { - Logs.REDIS.error("redis hmget exception:{},{},{}", namespace, key, node, e); + Logs.REDIS.error("redis hmget exception:{},{},{}", key, fields, node, e); } finally { // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; + return toList(value, clazz); } @@ -316,26 +337,25 @@ public static List hmget(RedisNode node, String namespace, Class clazz * 关联 * * @param nodeList - * @param key * @param hash * @param time */ - public static void hmset(List nodeList, String namespace, Map hash, Integer time) { + public static void hmset(List nodeList, String key, Map hash, Integer time) { if (time == null) { time = -1; } - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); - jedis.hmset(namespace, hash); + jedis.hmset(key, hash); if (time > 0) { - jedis.expire(namespace, time); + jedis.expire(key, time); } } catch (Exception e) { - Logs.REDIS.error("redis hmset exception:{},{},{}", namespace, time, node, e); + Logs.REDIS.error("redis hmset exception:{},{},{}", key, time, node, e); } finally { // 返还到连接池 close(jedis); @@ -344,8 +364,8 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String namespace, Map hash) { - hmset(nodeList, namespace, hash, null); + public static void hmset(List nodeList, String key, Map hash) { + hmset(nodeList, key, hash, null); } /********************* hash redis end ********************************/ @@ -354,9 +374,9 @@ public static void hmset(List nodeList, String namespace, Map nodeList, String key, String value) { + public static void lpush(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -371,7 +391,7 @@ public static void lpush(List nodeList, String key, String value) { } - public static void lpush(List nodeList, String key, T value) { + public static void lpush(List nodeList, String key, T value) { lpush(nodeList, key, Jsons.toJson(value)); @@ -380,9 +400,9 @@ public static void lpush(List nodeList, String key, T value) { /** * 从队列的右边入队 */ - public static void rpush(List nodeList, String key, String value) { + public static void rpush(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -396,16 +416,16 @@ public static void rpush(List nodeList, String key, String value) { } } - public static void rpush(List nodeList, String key, T value) { + public static void rpush(List nodeList, String key, T value) { rpush(nodeList, key, Jsons.toJson(value)); } /** * 移除并且返回 key 对应的 list 的第一个元素 */ - public static T lpop(List nodeList, String key, Class clazz) { + public static T lpop(List nodeList, String key, Class clazz) { String retValue = null; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -426,9 +446,9 @@ public static T lpop(List nodeList, String key, Class clazz) { /** * 从队列的右边出队一个元素 */ - public static T rpop(List nodeList, String key, Class clazz) { + public static T rpop(List nodeList, String key, Class clazz) { String retValue = null; - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; String vaule = null; try { @@ -451,7 +471,7 @@ public static T rpop(List nodeList, String key, Class clazz) { * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List lrange(RedisNode node, String key, int start, int end, Class clazz) { + public static List lrange(RedisServer node, String key, int start, int end, Class clazz) { List value = null; Jedis jedis = null; try { @@ -463,21 +483,14 @@ public static List lrange(RedisNode node, String key, int start, int end, // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; + return toList(value, clazz); } /** * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key * 里的值不是一个list的话,会返回error。 */ - public static long llen(RedisNode node, String key) { + public static long llen(RedisServer node, String key) { long len = 0; Jedis jedis = null; @@ -500,9 +513,9 @@ public static long llen(RedisNode node, String key) { * @param key * @param value */ - public static void lRem(List nodeList, String key, String value) { + public static void lRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -520,11 +533,11 @@ public static void lRem(List nodeList, String key, String value) { /********************* list redis end ********************************/ /********************* - * pubsub redis start + * mq redis start ********************************/ - public static void publish(RedisNode node, String channel, T message) { + public static void publish(RedisServer node, String channel, T message) { Jedis jedis = null; String value = null; if (message instanceof String) { @@ -543,20 +556,19 @@ public static void publish(RedisNode node, String channel, T message) { } } - public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { - for (final RedisNode node : nodeList) { - String name = node.getIp() + "_" + Jsons.toJson(channels); - ThreadPoolManager.newThread(name, new Runnable() { + public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { + for (final RedisServer node : nodeList) { + new Thread(new Runnable() { @Override public void run() { subscribe(node, pubsub, channels); } - }).start(); + }, node.getHost() + "_" + Jsons.toJson(channels) + ).start(); } } - public static void subscribe(RedisNode node, JedisPubSub pubsub, String... channel) { - + public static void subscribe(RedisServer node, JedisPubSub pubsub, String... channel) { Jedis jedis = null; try { jedis = getClient(node); @@ -577,10 +589,9 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann * @param nodeList * @param key * @param value - * @param time seconds */ - public static void sAdd(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + public static void sAdd(List nodeList, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -595,12 +606,11 @@ public static void sAdd(List nodeList, String key, String value) { } /** - * @param node 返回个数 + * @param node 返回个数 * @param key - * @param clazz * @return */ - public static Long sCard(RedisNode node, String key) { + public static Long sCard(RedisServer node, String key) { Long value = null; Jedis jedis = null; @@ -616,9 +626,9 @@ public static Long sCard(RedisNode node, String key) { return value; } - public static void sRem(List nodeList, String key, String value) { + public static void sRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -639,10 +649,9 @@ public static void sRem(List nodeList, String key, String value) { * @param node * @param key * @param clazz - * @param fields * @return */ - public static List sScan(RedisNode node, String key, Class clazz, int start) { + public static List sScan(RedisServer node, String key, Class clazz, int start) { List value = null; Jedis jedis = null; @@ -658,15 +667,7 @@ public static List sScan(RedisNode node, String key, Class clazz, int // 返还到连接池 close(jedis); } - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - + return toList(value, clazz); } /********************* @@ -677,8 +678,8 @@ public static List sScan(RedisNode node, String key, Class clazz, int * @param key * @param value */ - public static void zAdd(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + public static void zAdd(List nodeList, String key, String value) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -693,12 +694,11 @@ public static void zAdd(List nodeList, String key, String value) { } /** - * @param node 返回个数 + * @param node 返回个数 * @param key - * @param clazz * @return */ - public static Long zCard(RedisNode node, String key) { + public static Long zCard(RedisServer node, String key) { Long value = null; Jedis jedis = null; @@ -714,9 +714,9 @@ public static Long zCard(RedisNode node, String key) { return value; } - public static void zRem(List nodeList, String key, String value) { + public static void zRem(List nodeList, String key, String value) { - for (RedisNode node : nodeList) { + for (RedisServer node : nodeList) { Jedis jedis = null; try { jedis = getClient(node); @@ -736,7 +736,7 @@ public static void zRem(List nodeList, String key, String value) { * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ - public static List zrange(RedisNode node, String key, int start, int end, Class clazz) { + public static List zrange(RedisServer node, String key, int start, int end, Class clazz) { Set value = null; Jedis jedis = null; try { @@ -748,6 +748,11 @@ public static List zrange(RedisNode node, String key, int start, int end, // 返还到连接池 close(jedis); } + + return toList(value, clazz); + } + + private static List toList(Collection value, Class clazz) { if (value != null) { List newValue = Lists.newArrayList(); for (String temp : value) { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java new file mode 100644 index 00000000..d7237eef --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisException.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.cache.redis; + +/** + * Created by yxx on 2016/5/27. + * + * @author ohun@live.cn (夜色) + */ +public class RedisException extends RuntimeException { + + public RedisException() { + } + + public RedisException(Throwable cause) { + super(cause); + } + + public RedisException(String message) { + super(message); + } + + public RedisException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java new file mode 100644 index 00000000..cd11ef06 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; + +import com.google.common.collect.Lists; + +import java.util.List; + + +/** + * redis 组 + */ +public class RedisGroup { + private List redisServerList = Lists.newArrayList(); + + public List getRedisServerList() { + return redisServerList; + } + + public void setRedisServerList(List redisServerList) { + this.redisServerList = redisServerList; + } + + public void addRedisNode(RedisServer node) { + redisServerList.add(node); + } + + public void remove(int i) { + if (redisServerList != null) { + redisServerList.remove(i); + } + } + + public void clear() { + if (redisServerList != null) { + redisServerList.clear(); + } + } + + public RedisServer get(String key) { + int i = key.hashCode() % redisServerList.size(); + return redisServerList.get(i); + } + + public static RedisGroup from(com.mpush.tools.config.data.RedisGroup node) { + RedisGroup group = new RedisGroup(); + for (com.mpush.tools.config.data.RedisServer rs : node.redisNodeList) { + group.addRedisNode(new RedisServer(rs.getHost(), rs.getPort(), rs.getPassword())); + } + return group; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java similarity index 55% rename from mpush-api/src/main/java/com/mpush/api/RedisKey.java rename to mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 0077c4d1..b4b82e8a 100644 --- a/mpush-api/src/main/java/com/mpush/api/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -1,8 +1,27 @@ -package com.mpush.api; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; public final class RedisKey { - private static final String USER_PREFIX = "mp_u_"; + private static final String USER_PREFIX = "mp_uc_"; private static final String SESSION_PREFIX = "mp_s_"; diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java new file mode 100644 index 00000000..88a4abdc --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis; + +/** + * redis 相关的配置信息 + */ +public class RedisServer extends com.mpush.tools.config.data.RedisServer { + + public RedisServer(String ip, int port, String password) { + super(ip, port, password); + } + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java new file mode 100644 index 00000000..ee43587a --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java @@ -0,0 +1,85 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.hash; + +import redis.clients.util.Hashing; + +import java.util.Collection; +import java.util.SortedMap; +import java.util.TreeMap; + +public class ConsistentHash { + + private final Hashing hash; + private final int numberOfReplicas; + private final SortedMap circle = new TreeMap(); + + public ConsistentHash(Hashing hash, int numberOfReplicas, + Collection nodes) { + super(); + this.hash = hash; + this.numberOfReplicas = numberOfReplicas; + for (Node node : nodes) { + add(node); + } + } + + /** + * 增加真实机器节点 + * + * @param node + */ + public void add(Node node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.put(this.hash.hash(node.toString() + i), node); + } + } + + /** + * 删除真实机器节点 + * + * @param node + */ + public void remove(String node) { + for (int i = 0; i < this.numberOfReplicas; i++) { + circle.remove(this.hash.hash(node.toString() + i)); + } + } + + /** + * 取得真实机器节点 + * + * @param key + * @return + */ + public Node get(String key) { + if (circle.isEmpty()) { + return null; + } + long hash = this.hash.hash(key); + if (!circle.containsKey(hash)) { + SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 + hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); + } + return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 + } + + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java new file mode 100644 index 00000000..295dfbf3 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.hash; + +public class Node { + + private String ip; //机器ip + private String name;//名字 + + public Node(String ip, String name) { + this.ip = ip; + this.name = name; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java new file mode 100644 index 00000000..f74f1c27 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.listener; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.cache.redis.mq.Subscriber; +import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; + +public class ListenerDispatcher implements MessageListener { + + public static final ListenerDispatcher I = new ListenerDispatcher(); + + private final Map> subscribes = Maps.newTreeMap(); + + private final Executor executor = ThreadPoolManager.I.getRedisExecutor(); + + private ListenerDispatcher() { + } + + @Override + public void onMessage(final String channel, final String message) { + List listeners = subscribes.get(channel); + if (listeners == null) { + Logs.REDIS.info("cannot find listener:%s,%s", channel, message); + return; + } + for (final MessageListener listener : listeners) { + executor.execute(() -> listener.onMessage(channel, message)); + } + } + + public void subscribe(String channel, MessageListener listener) { + List listeners = subscribes.get(channel); + if (listeners == null) { + listeners = Lists.newArrayList(); + subscribes.put(channel, listeners); + } + listeners.add(listener); + RedisManager.I.subscribe(Subscriber.holder, channel); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java new file mode 100644 index 00000000..da6989d1 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/MessageListener.java @@ -0,0 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.listener; + + +public interface MessageListener { + + void onMessage(String channel, String message); + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java new file mode 100644 index 00000000..9c90877b --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java @@ -0,0 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.manager; + +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; + +import java.util.List; + +public interface RedisClusterManager { + + void init(); + + List getGroupList(); + + RedisServer randomGetRedisNode(String key); + + List hashSet(String key); +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java new file mode 100644 index 00000000..50c10e21 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -0,0 +1,318 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.manager; + +import com.google.common.collect.Sets; +import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.tools.Jsons; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPubSub; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * redis 对外封装接口 + */ +public class RedisManager { + public static final RedisManager I = new RedisManager(); + + private final RedisClusterManager clusterManager = ZKRedisClusterManager.I; + + public void init() { + ZKRedisClusterManager.I.init(); + test(clusterManager.getGroupList()); + } + + public long incr(String key, Integer time) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.incr(nodeList, key, time); + } + + public long incrBy(String key, long delt) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.incrBy(nodeList, key, delt); + } + + /********************* + * k v redis start + ********************************/ + + public T get(String key, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.get(node, key, clazz); + } + + public void set(String key, T value) { + set(key, value, null); + } + + public void set(String key, T value, Integer time) { + String jsonValue = Jsons.toJson(value); + set(key, jsonValue, time); + } + + /** + * @param key + * @param value + * @param time seconds + */ + public void set(String key, String value, Integer time) { + List nodeList = clusterManager.hashSet(key); + RedisClient.set(nodeList, key, value, time); + } + + public void del(String key) { + + List nodeList = clusterManager.hashSet(key); + RedisClient.del(nodeList, key); + + } + + /*********************k v redis end********************************/ + + + /********************* + * hash redis start + ********************************/ + public void hset(String key, String field, String value) { + + List nodeList = clusterManager.hashSet(field); + RedisClient.hset(nodeList, key, field, value); + + } + + public void hset(String key, String field, T value) { + hset(key, field, Jsons.toJson(value)); + } + + public T hget(String key, String field, Class clazz) { + + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hget(node, key, field, clazz); + + } + + public void hdel(String key, String field) { + List nodeList = clusterManager.hashSet(key); + RedisClient.hdel(nodeList, key, field); + } + + public Map hgetAll(String key) { + + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hgetAll(node, key); + + } + + public Map hgetAll(String key, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hgetAll(node, key, clazz); + } + + /** + * 返回 key 指定的哈希集中所有字段的名字。 + * + * @return + */ + public Set hkeys(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hkeys(node, key); + } + + /** + * 返回 key 指定的哈希集中指定字段的值 + * + * @param fields + * @param clazz + * @return + */ + public List hmget(String key, Class clazz, String... fields) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.hmget(node, key, clazz, fields); + } + + /** + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * + * @param hash + * @param time + */ + public void hmset(String key, Map hash, Integer time) { + List nodeList = clusterManager.hashSet(key); + RedisClient.hmset(nodeList, key, hash, time); + } + + public void hmset(String key, Map hash) { + hmset(key, hash, null); + } + + + /*********************hash redis end********************************/ + + + /*********************list redis start********************************/ + /** + * 从队列的左边入队 + */ + public void lpush(String key, String value) { + List nodeList = clusterManager.hashSet(key); + RedisClient.lpush(nodeList, key, value); + } + + public void lpush(String key, T value) { + lpush(key, Jsons.toJson(value)); + } + + /** + * 从队列的右边入队 + */ + public void rpush(String key, String value) { + List nodeList = clusterManager.hashSet(key); + RedisClient.rpush(nodeList, key, value); + } + + public void rpush(String key, T value) { + rpush(key, Jsons.toJson(value)); + } + + /** + * 移除并且返回 key 对应的 list 的第一个元素 + */ + public T lpop(String key, Class clazz) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.lpop(nodeList, key, clazz); + } + + /** + * 从队列的右边出队一个元素 + */ + public T rpop(String key, Class clazz) { + List nodeList = clusterManager.hashSet(key); + return RedisClient.rpop(nodeList, key, clazz); + } + + + /** + * 从列表中获取指定返回的元素 + * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ + public List lrange(String key, int start, int end, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.lrange(node, key, start, end, clazz); + } + + /** + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + */ + public long llen(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.llen(node, key); + } + + public void lrem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.lRem(nodeList, key, jsonValue); + } + + public void publish(String channel, T message) { + + RedisServer node = clusterManager.randomGetRedisNode(channel); + RedisClient.publish(node, channel, message); + + } + + public void subscribe(JedisPubSub pubsub, String... channels) { + + Set set = Sets.newHashSet(); + for (String channel : channels) { + List nodeList = clusterManager.hashSet(channel); + set.addAll(nodeList); + } + + RedisClient.subscribe(set, pubsub, channels); + } + + public void sAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.sAdd(nodeList, key, jsonValue); + } + + public Long sCard(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.sCard(node, key); + } + + public void sRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.sRem(nodeList, key, jsonValue); + } + + public List sScan(String key, int start, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.sScan(node, key, clazz, start); + } + + public void zAdd(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.zAdd(nodeList, key, jsonValue); + } + + public Long zCard(String key) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.zCard(node, key); + } + + public void zRem(String key, T value) { + String jsonValue = Jsons.toJson(value); + List nodeList = clusterManager.hashSet(key); + RedisClient.zRem(nodeList, key, jsonValue); + } + + public List zrange(String key, int start, int end, Class clazz) { + RedisServer node = clusterManager.randomGetRedisNode(key); + return RedisClient.zrange(node, key, start, end, clazz); + } + + public void test(List groupList) { + if (groupList == null || groupList.isEmpty()) { + throw new RuntimeException("init redis sever error."); + } + for (RedisGroup group : groupList) { + List list = group.getRedisServerList(); + if (list == null || list.isEmpty()) { + throw new RuntimeException("init redis sever error."); + } + for (RedisServer node : list) { + Jedis jedis = RedisClient.getClient(node); + if (jedis == null) throw new RuntimeException("init redis sever error."); + jedis.close(); + } + } + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java new file mode 100644 index 00000000..54abe851 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -0,0 +1,130 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.manager; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisException; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKRedisNodeWatcher; +import com.mpush.zk.node.ZKRedisNode; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static com.mpush.zk.ZKPath.REDIS_SERVER; + +public class ZKRedisClusterManager implements RedisClusterManager { + public static final ZKRedisClusterManager I = new ZKRedisClusterManager(); + + private ZKRedisClusterManager() { + } + + private final List groups = new ArrayList<>(); + + /** + * zk 启动的时候需要调用这个 + */ + @Override + public void init() { + Logs.Console.error("begin init redis cluster"); + if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); + List groupList = CC.mp.redis.cluster_group; + if (groupList.size() > 0) { + if (CC.mp.redis.write_to_zk) { + register(groupList); + } else if (!ZKClient.I.isExisted(REDIS_SERVER.getRootPath())) { + register(groupList); + } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getRootPath()))) { + register(groupList); + } + } + + ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); + watcher.beginWatch(); + Collection nodes = watcher.getCache().values(); + if (nodes == null || nodes.isEmpty()) { + Logs.REDIS.info("init redis client error, redis server is none."); + throw new RedisException("init redis client error, redis server is none."); + } + for (ZKRedisNode node : nodes) { + groups.add(RedisGroup.from(node)); + } + if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); + Logs.Console.error("init redis cluster success..."); + } + + @Override + public List getGroupList() { + return Collections.unmodifiableList(groups); + } + + public int groupSize() { + return groups.size(); + } + + /** + * 随机获取一个redis 实例 + * + * @param key + * @return + */ + @Override + public RedisServer randomGetRedisNode(String key) { + int size = groupSize(); + if (size == 1) return groups.get(0).get(key); + int i = (int) ((Math.random() % size) * size); + RedisGroup group = groups.get(i); + return group.get(key); + } + + /** + * 写操作的时候,获取所有redis 实例 + * + * @param key + * @return + */ + @Override + public List hashSet(String key) { + List nodeList = Lists.newArrayList(); + for (RedisGroup group : groups) { + RedisServer node = group.get(key); + nodeList.add(node); + } + return nodeList; + } + + private void register(List groupList) { + String data = Jsons.toJson(groupList); + ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data); + Logs.Console.error("register redis server group success, group=" + data); + } + + public void addGroup(RedisGroup group) { + groups.add(group); + } +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java new file mode 100644 index 00000000..95afbcf6 --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java @@ -0,0 +1,86 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.cache.redis.mq; + +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import redis.clients.jedis.JedisPubSub; + +public class Subscriber extends JedisPubSub { + + private static ListenerDispatcher dispatcher = ListenerDispatcher.I; + + public static Subscriber holder = new Subscriber(); + + private Subscriber() { + } + + @Override + public void onMessage(String channel, String message) { + Logs.REDIS.info("onMessage:{},{}", channel, message); + dispatcher.onMessage(channel, message); + super.onMessage(channel, message); + } + + @Override + public void onPMessage(String pattern, String channel, String message) { + Logs.REDIS.info("onPMessage:{},{},{}", pattern, channel, message); + super.onPMessage(pattern, channel, message); + } + + @Override + public void onPSubscribe(String pattern, int subscribedChannels) { + Logs.REDIS.info("onPSubscribe:{},{}", pattern, subscribedChannels); + super.onPSubscribe(pattern, subscribedChannels); + } + + @Override + public void onPUnsubscribe(String pattern, int subscribedChannels) { + Logs.REDIS.info("onPUnsubscribe:{},{}", pattern, subscribedChannels); + super.onPUnsubscribe(pattern, subscribedChannels); + } + + @Override + public void onSubscribe(String channel, int subscribedChannels) { + Logs.REDIS.info("onSubscribe:{},{}", channel, subscribedChannels); + super.onSubscribe(channel, subscribedChannels); + } + + @Override + public void onUnsubscribe(String channel, int subscribedChannels) { + Logs.REDIS.info("onUnsubscribe:{},{}", channel, subscribedChannels); + super.onUnsubscribe(channel, subscribedChannels); + } + + + @Override + public void unsubscribe() { + Logs.REDIS.info("unsubscribe"); + super.unsubscribe(); + } + + @Override + public void unsubscribe(String... channels) { + Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); + super.unsubscribe(channels); + } + +} diff --git a/mpush-client/.gitignore b/mpush-client/.gitignore index 09e3bc9b..ae3c1726 100644 --- a/mpush-client/.gitignore +++ b/mpush-client/.gitignore @@ -1,2 +1 @@ /bin/ -/target/ diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 54440364..c62a39b4 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -11,7 +11,7 @@ 4.0.0 ${mpush.groupId} mpush-client - ${mpush-client-version} + ${mpush.version} mpush-client jar @@ -25,39 +25,11 @@ ${mpush.groupId} - mpush-zk + mpush-common ${mpush.groupId} - mpush-common + mpush-cache - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-client - - - - - - - - diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java new file mode 100644 index 00000000..ed5e5131 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java @@ -0,0 +1,98 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + + +public class ClientConfig { + private byte[] clientKey; + private byte[] iv; + private String clientVersion; + private String deviceId; + private String osName; + private String osVersion; + private String userId; + private String cipher; //快速重连的时候使用 + + + public byte[] getClientKey() { + return clientKey; + } + + public void setClientKey(byte[] clientKey) { + this.clientKey = clientKey; + } + + public byte[] getIv() { + return iv; + } + + public void setIv(byte[] iv) { + this.iv = iv; + } + + public String getClientVersion() { + return clientVersion; + } + + public void setClientVersion(String clientVersion) { + this.clientVersion = clientVersion; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getOsName() { + return osName; + } + + public void setOsName(String osName) { + this.osName = osName; + } + + public String getOsVersion() { + return osVersion; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public String getCipher() { + return cipher; + } + + public void setCipher(String cipher) { + this.cipher = cipher; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + +} diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java new file mode 100644 index 00000000..7daaf8ba --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -0,0 +1,237 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + + +import com.google.common.collect.Maps; +import com.mpush.api.connection.Connection; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.message.*; +import com.mpush.common.security.AesCipher; +import com.mpush.common.security.CipherBox; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.ThreadNames; +import io.netty.channel.*; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timeout; +import io.netty.util.Timer; +import io.netty.util.TimerTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn + */ +@ChannelHandler.Sharable +public final class ConnClientChannelHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); + private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.T_NETTY_TIMER)); + + private final Connection connection = new NettyConnection(); + private final ClientConfig clientConfig; + + public ConnClientChannelHandler(ClientConfig clientConfig) { + this.clientConfig = clientConfig; + } + + public Connection getConnection() { + return connection; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + connection.updateLastReadTime(); + //加密 + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + Command command = Command.toCMD(packet.cmd); + if (command == Command.HANDSHAKE) { + connection.getSessionContext().changeCipher(new AesCipher(clientConfig.getClientKey(), clientConfig.getIv())); + HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); + byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); + connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); + startHeartBeat(message.heartbeat); + LOGGER.warn("会话密钥:{},message={}", sessionKey, message); + bindUser(clientConfig); + saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); + } else if (command == Command.FAST_CONNECT) { + String cipherStr = clientConfig.getCipher(); + String[] cs = cipherStr.split(","); + byte[] key = AesCipher.toArray(cs[0]); + byte[] iv = AesCipher.toArray(cs[1]); + connection.getSessionContext().changeCipher(new AesCipher(key, iv)); + + FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); + startHeartBeat(message.heartbeat); + bindUser(clientConfig); + LOGGER.warn("fast connect success, message=" + message); + } else if (command == Command.KICK) { + KickUserMessage message = new KickUserMessage(packet, connection); + LOGGER.error("receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); + ctx.close(); + } else if (command == Command.ERROR) { + ErrorMessage errorMessage = new ErrorMessage(packet, connection); + LOGGER.error("receive an error packet=" + errorMessage); + } else if (command == Command.BIND) { + OkMessage okMessage = new OkMessage(packet, connection); + LOGGER.warn("receive an success packet=" + okMessage); + HttpRequestMessage message = new HttpRequestMessage(connection); + message.uri = "http://baidu.com"; + message.send(); + } else if (command == Command.PUSH) { + PushMessage message = new PushMessage(packet, connection); + LOGGER.warn("receive an push message, content=" + message.content); + } else if (command == Command.HEARTBEAT) { + LOGGER.warn("receive a heartbeat pong..."); + } else { + LOGGER.warn("receive a message, type=" + command + "," + packet); + } + } + + + LOGGER.warn("update currentTime:" + ctx.channel() + "," + msg); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + connection.init(ctx.channel(), true); + handshake(clientConfig); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + EventBus.I.post(new ConnectionCloseEvent(connection)); + LOGGER.info("client disconnect connection={}", connection); + } + + private void tryFastConnect() { + + Map sessionTickets = getFastConnectionInfo(clientConfig.getDeviceId()); + + if (sessionTickets == null) { + handshake(clientConfig); + return; + } + String sessionId = sessionTickets.get("sessionId"); + if (sessionId == null) { + handshake(clientConfig); + return; + } + String expireTime = sessionTickets.get("expireTime"); + if (expireTime != null) { + long exp = Long.parseLong(expireTime); + if (exp < System.currentTimeMillis()) { + handshake(clientConfig); + return; + } + } + + final String cipher = sessionTickets.get("cipherStr"); + + FastConnectMessage message = new FastConnectMessage(connection); + message.deviceId = clientConfig.getDeviceId(); + message.sessionId = sessionId; + + message.sendRaw(channelFuture -> { + if (channelFuture.isSuccess()) { + clientConfig.setCipher(cipher); + } else { + handshake(clientConfig); + } + }); + } + + private void bindUser(ClientConfig client) { + BindUserMessage message = new BindUserMessage(connection); + message.userId = client.getUserId(); + message.send(); + } + + private void saveToRedisForFastConnection(ClientConfig client, String sessionId, Long expireTime, byte[] sessionKey) { + Map map = Maps.newHashMap(); + map.put("sessionId", sessionId); + map.put("expireTime", expireTime + ""); + map.put("cipherStr", connection.getSessionContext().cipher.toString()); + String key = RedisKey.getDeviceIdKey(client.getDeviceId()); + RedisManager.I.set(key, map, 60 * 5); //5分钟 + } + + private Map getFastConnectionInfo(String deviceId) { + String key = RedisKey.getDeviceIdKey(deviceId); + return RedisManager.I.get(key, Map.class); + } + + private void handshake(ClientConfig client) { + HandshakeMessage message = new HandshakeMessage(connection); + message.clientKey = client.getClientKey(); + message.iv = client.getIv(); + message.clientVersion = client.getClientVersion(); + message.deviceId = client.getDeviceId(); + message.osName = client.getOsName(); + message.osVersion = client.getOsVersion(); + message.timestamp = System.currentTimeMillis(); + message.send(); + } + + public void startHeartBeat(final int heartbeat) throws Exception { + HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + final TimerTask self = this; + final Channel channel = connection.getChannel(); + if (channel.isActive()) { + ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); + channelFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + } else { + LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); + } + HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); + } + }); + } else { + LOGGER.error("connection was closed, connection={}", connection); + } + } + }, heartbeat, TimeUnit.MILLISECONDS); + } +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java new file mode 100644 index 00000000..0a69d57d --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + +import com.google.common.eventbus.Subscribe; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.netty.client.NettyClient; +import com.mpush.tools.event.EventBus; +import io.netty.channel.ChannelHandler; + +public class ConnectClient extends NettyClient { + private final ConnClientChannelHandler handler; + + public ConnectClient(String host, int port, ClientConfig config) { + super(host, port); + handler = new ConnClientChannelHandler(config); + EventBus.I.register(this); + } + + @Override + public ChannelHandler getChannelHandler() { + return handler; + } + + @Subscribe + void on(ConnectionCloseEvent event) { + this.stop(); + } + +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java new file mode 100644 index 00000000..4b423d04 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -0,0 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.api.service.Listener; +import com.mpush.netty.client.NettyClient; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_client.*; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClient extends NettyClient { + private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); + private GlobalChannelTrafficShapingHandler trafficShapingHandler; + + public GatewayClient(String host, int port) { + super(host, port); + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } + } + + @Override + public ChannelHandler getChannelHandler() { + return handler; + } + + public Connection getConnection() { + return handler.getConnection(); + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } + super.doStop(listener); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java new file mode 100644 index 00000000..cfb9bcd9 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -0,0 +1,120 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.client.push.PushRequest; +import com.mpush.client.push.PushRequestBus; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.netty.connection.NettyConnection; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.mpush.common.ErrorCode.*; + +/** + * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn + */ +@ChannelHandler.Sharable +public final class GatewayClientChannelHandler extends ChannelHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); + + private final Connection connection = new NettyConnection(); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + LOGGER.info("receive gateway packet={}, channel={}", msg, ctx.channel()); + connection.updateLastReadTime(); + if (msg instanceof Packet) { + Packet packet = (Packet) msg; + if (packet.cmd == Command.OK.cmd) { + handleOK(new OkMessage(packet, connection)); + } else if (packet.cmd == Command.ERROR.cmd) { + handleError(new ErrorMessage(packet, connection)); + } + } + } + + private void handleOK(OkMessage message) { + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + handPush(message, null, message.getPacket()); + } + } + + private void handleError(ErrorMessage message) { + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + handPush(null, message, message.getPacket()); + } + } + + private void handPush(OkMessage ok, ErrorMessage error, Packet packet) { + PushRequest request = PushRequestBus.I.getAndRemove(packet.sessionId); + if (request == null) { + LOGGER.warn("receive a gateway response, but request has timeout. ok={}, error={}", ok, error); + return; + } + + if (ok != null) {//推送成功 + request.success(); + } else if (error != null) {//推送失败 + LOGGER.warn("receive an error gateway response, message={}", error); + if (error.code == OFFLINE.errorCode) {//用户离线 + request.offline(); + } else if (error.code == PUSH_CLIENT_FAILURE.errorCode) {//下发到客户端失败 + request.failure(); + } else if (error.code == ROUTER_CHANGE.errorCode) {//用户路由信息更改 + request.redirect(); + } + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("client connect channel={}", ctx.channel()); + connection.init(ctx.channel(), false); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + LOGGER.info("client disconnect channel={}", ctx.channel()); + //TODO notify gateway-client-factory to removeAndClose this gateway-client + } + + public Connection getConnection() { + return connection; + } +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java new file mode 100644 index 00000000..6714c4d9 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java @@ -0,0 +1,126 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.google.common.collect.Maps; +import com.mpush.api.connection.Connection; +import com.mpush.api.service.Client; +import com.mpush.api.service.Listener; +import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.zk.node.ZKServerNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayClientFactory extends ZKServerNodeCache { + public static final GatewayClientFactory I = new GatewayClientFactory(); + + private final Logger logger = LoggerFactory.getLogger(GatewayClientFactory.class); + + private final Map ip_client = Maps.newConcurrentMap(); + + @Override + public void put(String fullPath, ZKServerNode node) { + super.put(fullPath, node); + addClient(node.getIp(), node.getPort()); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = super.remove(fullPath); + removeClient(node); + logger.warn("Gateway Server zkNode={} was removed.", node); + return node; + } + + @Override + public void clear() { + super.clear(); + for (GatewayClient client : ip_client.values()) { + client.stop(null); + } + } + + public GatewayClient getClient(String ip) { + GatewayClient client = ip_client.get(ip); + if (client == null) { + return null;//TODO create client + } + return client; + } + + public Connection getConnection(String ip) { + GatewayClient client = ip_client.get(ip); + if (client == null) { + return null;//TODO create client + } + Connection connection = client.getConnection(); + if (connection.isConnected()) { + return connection; + } + restartClient(client); + return null; + } + + private void restartClient(final GatewayClient client) { + ip_client.remove(client.getHost()); + client.stop(new Listener() { + @Override + public void onSuccess(Object... args) { + addClient(client.getHost(), client.getPort()); + } + + @Override + public void onFailure(Throwable cause) { + addClient(client.getHost(), client.getPort()); + } + }); + } + + private void removeClient(ZKServerNode node) { + if (node != null) { + Client client = ip_client.remove(node.getIp()); + if (client != null) { + client.stop(null); + } + } + } + + private void addClient(final String host, final int port) { + final GatewayClient client = new GatewayClient(host, port); + client.start(new Listener() { + @Override + public void onSuccess(Object... args) { + ip_client.put(host, client); + } + + @Override + public void onFailure(Throwable cause) { + logger.error("create gateway client ex, client={}", client, cause); + } + }); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java new file mode 100644 index 00000000..9b69df55 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -0,0 +1,109 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.Constants; +import com.mpush.api.connection.Connection; +import com.mpush.api.push.PushSender; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.client.gateway.GatewayClientFactory; +import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.RemoteRouter; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKServerNodeWatcher; + +import java.util.Collection; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.FutureTask; + +import static com.mpush.zk.ZKPath.GATEWAY_SERVER; + +/*package*/ class PushClient extends BaseService implements PushSender { + private static final int DEFAULT_TIMEOUT = 3000; + private final GatewayClientFactory factory = GatewayClientFactory.I; + private final ConnectionRouterManager routerManager = ConnectionRouterManager.I; + + public void send(String content, Collection userIds, Callback callback) { + send(content.getBytes(Constants.UTF_8), userIds, callback); + } + + @Override + public FutureTask send(String content, String userId, Callback callback) { + return send(content.getBytes(Constants.UTF_8), userId, callback); + } + + @Override + public void send(byte[] content, Collection userIds, Callback callback) { + for (String userId : userIds) { + send(content, userId, callback); + } + } + + @Override + public FutureTask send(byte[] content, String userId, Callback callback) { + Set routers = routerManager.lookupAll(userId); + if (routers == null || routers.isEmpty()) { + return PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .offline(); + + } + FutureTask task = null; + for (RemoteRouter router : routers) { + task = PushRequest + .build(this) + .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) + .send(router); + } + return task; + } + + public Connection getGatewayConnection(String host) { + return factory.getConnection(host); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + ZKClient.I.start(listener); + RedisManager.I.init(); + ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).beginWatch(); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + factory.clear(); + ZKClient.I.stop(listener); + } + + @Override + public boolean isRunning() { + return started.get(); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java new file mode 100644 index 00000000..5e91639d --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.push.PushSender; +import com.mpush.api.spi.client.PusherFactory; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class PushClientFactory implements PusherFactory { + private static PushClient CLIENT; + + @Override + public PushSender get() { + if (CLIENT == null) { + synchronized (PushClientFactory.class) { + CLIENT = new PushClient(); + } + } + return CLIENT; + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java new file mode 100644 index 00000000..3df50970 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -0,0 +1,215 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.connection.Connection; +import com.mpush.api.push.PushSender; +import com.mpush.api.router.ClientLocation; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.RemoteRouter; +import com.mpush.tools.common.TimeLine; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public class PushRequest extends FutureTask { + private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); + + private static final Callable NONE = () -> Boolean.FALSE; + + private enum Status {init, success, failure, offline, timeout} + + private final AtomicReference status = new AtomicReference<>(Status.init); + private final TimeLine timeLine = new TimeLine("Push-Time-Line"); + + private final PushClient client; + + private PushSender.Callback callback; + private String userId; + private byte[] content; + private long timeout; + private ClientLocation location; + private Future future; + + private void sendToConnServer(RemoteRouter router) { + timeLine.addTimePoint("lookup-remote"); + + if (router == null) { + //1.1没有查到说明用户已经下线 + offline(); + return; + } + + timeLine.addTimePoint("get-gateway-conn"); + + + //2.通过网关连接,把消息发送到所在机器 + location = router.getRouteValue(); + Connection gatewayConn = client.getGatewayConnection(location.getHost()); + if (gatewayConn == null) { + LOGGER.error("get gateway connection failure, location={}", location); + failure(); + return; + } + + timeLine.addTimePoint("send-to-gateway-begin"); + + GatewayPushMessage pushMessage = new GatewayPushMessage(userId, location.getClientType(), content, gatewayConn); + timeLine.addTimePoint("put-request-bus"); + future = PushRequestBus.I.put(pushMessage.getSessionId(), this); + + pushMessage.sendRaw(f -> { + timeLine.addTimePoint("send-to-gateway-end"); + if (f.isSuccess()) { + } else { + failure(); + } + }); + } + + private void submit(Status status) { + if (this.status.compareAndSet(Status.init, status)) {//防止重复调用 + if (future != null) future.cancel(true); + if (callback != null) { + PushRequestBus.I.asyncCall(this); + } else { + LOGGER.warn("callback is null"); + } + super.set(this.status.get() == Status.success); + } + timeLine.end(); + LOGGER.info("push request {} end, userId={}, content={}, location={}, timeLine={}" + , status, userId, content, location, timeLine); + } + + @Override + public void run() { + switch (status.get()) { + case success: + callback.onSuccess(userId, location); + break; + case failure: + callback.onFailure(userId, location); + break; + case offline: + callback.onOffline(userId, location); + break; + case timeout: + callback.onTimeout(userId, location); + break; + case init://从定时任务过来的,超时时间到了 + submit(Status.timeout); + break; + } + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + throw new UnsupportedOperationException(); + } + + public FutureTask send(RemoteRouter router) { + timeLine.begin(); + sendToConnServer(router); + return this; + } + + public void redirect() { + timeLine.addTimePoint("redirect"); + LOGGER.warn("user route has changed, userId={}, location={}", userId, location); + ConnectionRouterManager.I.invalidateLocalCache(userId); + if (status.get() == Status.init) {//表示任务还没有完成,还可以重新发送 + RemoteRouter route = ConnectionRouterManager.I.lookup(userId, location.getClientType()); + send(route); + } + } + + public FutureTask offline() { + ConnectionRouterManager.I.invalidateLocalCache(userId); + submit(Status.offline); + return this; + } + + public void timeout() { + submit(Status.timeout); + } + + public void success() { + submit(Status.success); + } + + public void failure() { + submit(Status.failure); + } + + public long getTimeout() { + return timeout; + } + + public PushRequest(PushClient client) { + super(NONE); + this.client = client; + } + + public static PushRequest build(PushClient client) { + return new PushRequest(client); + } + + public PushRequest setCallback(PushSender.Callback callback) { + this.callback = callback; + return this; + } + + public PushRequest setUserId(String userId) { + this.userId = userId; + return this; + } + + public PushRequest setContent(byte[] content) { + this.content = content; + return this; + } + + public PushRequest setTimeout(long timeout) { + this.timeout = timeout; + return this; + } + + @Override + public String toString() { + return "PushRequest{" + + "content='" + content + '\'' + + ", userId='" + userId + '\'' + + ", timeout=" + timeout + + ", location=" + location + + '}'; + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java new file mode 100644 index 00000000..fbb846c0 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.push; + +import com.mpush.api.push.PushException; +import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.util.internal.chmv8.ConcurrentHashMapV8; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.concurrent.*; + +import static com.mpush.tools.thread.ThreadNames.T_PUSH_REQ_TIMER; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public class PushRequestBus { + public static final PushRequestBus I = new PushRequestBus(); + private final Logger logger = LoggerFactory.getLogger(PushRequestBus.class); + private final Map reqQueue = new ConcurrentHashMapV8<>(1024); + private final Executor executor = ThreadPoolManager.I.getPushCallbackExecutor(); + private final ScheduledExecutorService scheduledExecutor; + + private PushRequestBus() { + scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { + logger.error("one push request was rejected, request=" + r); + throw new PushException("one push request was rejected. request=" + r); + }); + } + + public Future put(int sessionId, PushRequest request) { + reqQueue.put(sessionId, request); + return scheduledExecutor.schedule(request, request.getTimeout(), TimeUnit.MILLISECONDS); + } + + public PushRequest getAndRemove(int sessionId) { + return reqQueue.remove(sessionId); + } + + public void asyncCall(Runnable runnable) { + executor.execute(runnable); + } +} diff --git a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java deleted file mode 100644 index 4200b5ad..00000000 --- a/mpush-client/src/main/java/com/mpush/conn/client/ClientChannelHandler.java +++ /dev/null @@ -1,223 +0,0 @@ -package com.mpush.conn.client; - - -import java.util.Map; - -import com.google.common.collect.Maps; -import com.mpush.api.protocol.Command; -import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.Client; -import com.mpush.api.RedisKey; -import com.mpush.common.message.*; -import com.mpush.common.security.AesCipher; -import com.mpush.common.security.CipherBox; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.netty.client.ChannelClientHandler; -import com.mpush.netty.client.NettyClient; -import com.mpush.netty.client.SecurityNettyClient; -import com.mpush.netty.connection.NettyConnection; -import com.mpush.tools.redis.manage.RedisManage; - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by ohun on 2015/12/19. - * - * @author ohun@live.cn - */ -@ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); - if (client instanceof SecurityNettyClient) { - SecurityNettyClient securityNettyClient = (SecurityNettyClient) client; - Connection connection = client.getConnection(); - //加密 - if (msg instanceof Packet) { - Packet packet = (Packet) msg; - Command command = Command.toCMD(packet.cmd); - if (command == Command.HANDSHAKE) { - connection.getSessionContext().changeCipher(new AesCipher(securityNettyClient.getClientKey(), securityNettyClient.getIv())); - HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(securityNettyClient.getClientKey(), message.serverKey); - connection.getSessionContext().changeCipher(new AesCipher(sessionKey, securityNettyClient.getIv())); - client.startHeartBeat(message.heartbeat); - LOGGER.debug("会话密钥:{},message={}", sessionKey, message); - bindUser(securityNettyClient); - saveToRedisForFastConnection(securityNettyClient, message.sessionId, message.expireTime, sessionKey); - } else if (command == Command.FAST_CONNECT) { - String cipherStr = securityNettyClient.getCipher(); - String[] cs = cipherStr.split(","); - byte[] key = AesCipher.toArray(cs[0]); - byte[] iv = AesCipher.toArray(cs[1]); - connection.getSessionContext().changeCipher(new AesCipher(key, iv)); - - FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); - client.startHeartBeat(message.heartbeat); - bindUser(securityNettyClient); - LOGGER.debug("fast connect success, message=" + message); - } else if (command == Command.KICK) { - KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},", securityNettyClient.getUserId(), securityNettyClient.getDeviceId(), message); - ctx.close(); - } else if (command == Command.ERROR) { - ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error packet=" + errorMessage); - } else if (command == Command.BIND) { - OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.debug("receive an success packet=" + okMessage); - HttpRequestMessage message = new HttpRequestMessage(connection); - message.uri = "http://baidu.com"; - message.send(); - } else if (command == Command.PUSH) { - PushMessage message = new PushMessage(packet, connection); - LOGGER.warn("receive an push message, content=" + message.content); - } else if (command == Command.HEARTBEAT) { - LOGGER.debug("receive a heartbeat pong..."); - } else { - LOGGER.warn("receive a message, type=" + command + "," + packet); - } - } - - } else if (client instanceof NettyClient) {//不加密 - - } - LOGGER.debug("update currentTime:" + ctx.channel() + "," + ToStringBuilder.reflectionToString(msg)); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.remove(ctx.channel()); - } else { - client.close("exception"); - } - - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); - - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.put(ctx.channel(), client); - connection.init(ctx.channel(), true); - client.initConnection(connection); - client.init(ctx.channel()); - tryFastConnect((SecurityNettyClient) client); - } else { - LOGGER.error("connection is not support appear hear:" + client); - connection.init(ctx.channel(), false); - client.initConnection(connection); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - if (client instanceof SecurityNettyClient) { - NettyClientFactory.INSTANCE.remove(ctx.channel()); - } else { - client.close("inactive"); - } - LOGGER.info("client disconnect channel={}", ctx.channel()); - } - - private void tryFastConnect(final SecurityNettyClient securityNettyClient) { - - Map sessionTickets = getFastConnectionInfo(securityNettyClient.getDeviceId()); - - if (sessionTickets == null) { - handshake(securityNettyClient); - return; - } - String sessionId = (String) sessionTickets.get("sessionId"); - if (sessionId == null) { - handshake(securityNettyClient); - return; - } - String expireTime = (String) sessionTickets.get("expireTime"); - if (expireTime != null) { - long exp = Long.parseLong(expireTime); - if (exp < System.currentTimeMillis()) { - handshake(securityNettyClient); - return; - } - } - - final String cipher = sessionTickets.get("cipherStr"); - - FastConnectMessage message = new FastConnectMessage(securityNettyClient.getConnection()); - message.deviceId = securityNettyClient.getDeviceId(); - message.sessionId = sessionId; - - message.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) throws Exception { - if (channelFuture.isSuccess()) { - securityNettyClient.setCipher(cipher); - } else { - handshake(securityNettyClient); - } - } - }); - } - - private void bindUser(SecurityNettyClient client) { - BindUserMessage message = new BindUserMessage(client.getConnection()); - message.userId = client.getUserId(); - message.send(); - } - - private void saveToRedisForFastConnection(SecurityNettyClient client, String sessionId, Long expireTime, byte[] sessionKey) { - Map map = Maps.newHashMap(); - map.put("sessionId", sessionId); - map.put("expireTime", expireTime + ""); - map.put("cipherStr", client.getConnection().getSessionContext().cipher.toString()); - String key = RedisKey.getDeviceIdKey(client.getDeviceId()); - RedisManage.set(key, map, 60 * 5); //5分钟 - } - - private Map getFastConnectionInfo(String deviceId) { - String key = RedisKey.getDeviceIdKey(deviceId); - return RedisManage.get(key, Map.class); - } - - private void handshake(SecurityNettyClient client) { - HandshakeMessage message = new HandshakeMessage(client.getConnection()); - message.clientKey = client.getClientKey(); - message.iv = client.getIv(); - message.clientVersion = client.getClientVersion(); - message.deviceId = client.getDeviceId(); - message.osName = client.getOsName(); - message.osVersion = client.getOsVersion(); - message.timestamp = System.currentTimeMillis(); - message.send(); - } - -} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java b/mpush-client/src/main/java/com/mpush/push/ConnectClient.java deleted file mode 100644 index 65ef68b9..00000000 --- a/mpush-client/src/main/java/com/mpush/push/ConnectClient.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.push; - -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; - -public class ConnectClient extends AbstractClient{ - - public ConnectClient() { - registerListener(new ConnectZKListener()); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushClient.java b/mpush-client/src/main/java/com/mpush/push/PushClient.java deleted file mode 100644 index 953b81c5..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushClient.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.mpush.push; - -import com.google.common.base.Strings; -import com.mpush.api.PushSender; -import com.mpush.api.connection.Connection; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.GatewayZKListener; - -import java.util.Collection; - -public class PushClient extends AbstractClient implements PushSender { - private static final int DEFAULT_TIMEOUT = 3000; - private final GatewayZKListener listener = new GatewayZKListener(); - - public PushClient() { - registerListener(listener); - } - - public void send(String content, Collection userIds, Callback callback) { - if (Strings.isNullOrEmpty(content)) return; - for (String userId : userIds) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); - } - } - - @Override - public void send(String content, String userId, Callback callback) { - PushRequest - .build(this) - .setCallback(callback) - .setUserId(userId) - .setContent(content) - .setTimeout(DEFAULT_TIMEOUT) - .send(); - } - - public Connection getGatewayConnection(String ip) { - return listener.getManager().getConnection(ip); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/push/PushRequest.java deleted file mode 100644 index cda87e8b..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushRequest.java +++ /dev/null @@ -1,206 +0,0 @@ -package com.mpush.push; - -import com.google.common.collect.Maps; -import com.mpush.api.PushSender; -import com.mpush.api.connection.Connection; -import com.mpush.api.router.ClientLocation; -import com.mpush.common.message.gateway.GatewayPushMessage; -import com.mpush.common.router.ConnectionRouterManager; -import com.mpush.common.router.RemoteRouter; -import com.mpush.tools.Jsons; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class PushRequest implements PushSender.Callback, Runnable { - private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - - private final PushClient client; - - private PushSender.Callback callback; - private String userId; - private String content; - private long timeout; - private long timeout_; - private int sessionId; - private long sendTime; - private AtomicInteger status = new AtomicInteger(0); - private Map times = Maps.newHashMap(); - - public PushRequest(PushClient client) { - this.client = client; - } - - public static PushRequest build(PushClient client) { - return new PushRequest(client); - } - - public PushRequest setCallback(PushSender.Callback callback) { - this.callback = callback; - return this; - } - - public PushRequest setUserId(String userId) { - this.userId = userId; - return this; - } - - public PushRequest setContent(String content) { - this.content = content; - return this; - } - - public PushRequest setTimeout(long timeout) { - this.timeout = timeout; - return this; - } - - @Override - public void onSuccess(String userId) { - putTime("success"); - LOGGER.info("success,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(1); - } - - @Override - public void onFailure(String userId) { - putTime("failure"); - LOGGER.info("failure,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(2); - } - - @Override - public void onOffline(String userId) { - putTime("offline"); - LOGGER.info("offline,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(3); - } - - @Override - public void onTimeout(String userId) { - putTime("timeout"); - LOGGER.info("timeout,sessionId:{},times:{},content:{}", sessionId, Jsons.toJson(times), content); - submit(4); - } - - private void submit(int status) { - if (this.status.compareAndSet(0, status)) {//防止重复调用 - if (callback != null) { - PushRequestBus.INSTANCE.getExecutor().execute(this); - } else { - LOGGER.warn("callback is null"); - } - } - } - - @Override - public void run() { - switch (status.get()) { - case 1: - callback.onSuccess(userId); - break; - case 2: - callback.onFailure(userId); - break; - case 3: - callback.onOffline(userId); - break; - case 4: - callback.onTimeout(userId); - break; - } - } - - public boolean isTimeout() { - return System.currentTimeMillis() > timeout_; - } - - public void timeout() { - onTimeout(userId); - } - - public void success() { - onSuccess(userId); - } - - public void failure() { - onFailure(userId); - } - - public void offline() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - onOffline(userId); - } - - public void send() { - this.timeout_ = timeout + System.currentTimeMillis(); - putTime("startsend"); - sendToConnServer(); - } - - public void redirect() { - ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId); - LOGGER.warn("user route has changed, userId={}, content={}", userId, content); - if (status.get() == 0) { - send(); - } - } - - private void sendToConnServer() { - //1.查询用户长连接所在的机器 - RemoteRouter router = ConnectionRouterManager.INSTANCE.lookup(userId); - if (router == null) { - //1.1没有查到说明用户已经下线 - this.onOffline(userId); - return; - } - - //2.通过网关连接,把消息发送到所在机器 - ClientLocation location = router.getRouteValue(); - Connection gatewayConn = client.getGatewayConnection(location.getHost()); - if (gatewayConn == null || !gatewayConn.isConnected()) { - this.onFailure(userId); - return; - } - - putTime("sendtoconnserver"); - - final GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn); - pushMessage.sendRaw(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - putTime("sendsuccess"); - } else { - PushRequest.this.onFailure(userId); - } - } - }); - - sessionId = pushMessage.getSessionId(); - putTime("putrequestbus"); - PushRequestBus.INSTANCE.put(sessionId, this); - } - - public long getSendTime() { - return sendTime; - } - - public Map getTimes() { - return times; - } - - public void putTime(String key) { - this.times.put(key, System.currentTimeMillis()); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java deleted file mode 100644 index 3bfc3b8e..00000000 --- a/mpush-client/src/main/java/com/mpush/push/PushRequestBus.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.push; - -import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.*; - -/** - * Created by ohun on 2015/12/30. - * - * @author ohun@live.cn - */ -public class PushRequestBus implements Runnable { - - public static final PushRequestBus INSTANCE = new PushRequestBus(); - private Map requests = new ConcurrentHashMapV8<>(1024); - private Executor executor = Executors.newFixedThreadPool(5);//test - private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test - - private PushRequestBus() { - scheduledExecutor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS); - } - - public void put(int sessionId, PushRequest request) { - requests.put(sessionId, request); - } - - public PushRequest remove(int sessionId) { - return requests.remove(sessionId); - } - - public Executor getExecutor() { - return executor; - } - - @Override - public void run() { - if (requests.isEmpty()) return; - Iterator it = requests.values().iterator(); - while (it.hasNext()) { - PushRequest request = it.next(); - if (request.isTimeout()) { - it.remove();//清除超时的请求 - request.timeout(); - } - } - } -} diff --git a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java deleted file mode 100644 index c91d3486..00000000 --- a/mpush-client/src/main/java/com/mpush/push/client/ClientChannelHandler.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.mpush.push.client; - - - -import com.mpush.api.protocol.Command; -import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.Client; -import com.mpush.common.message.ErrorMessage; -import com.mpush.netty.client.ChannelClientHandler; -import com.mpush.netty.connection.NettyConnection; -import com.mpush.push.PushRequest; -import com.mpush.push.PushRequestBus; - -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.mpush.common.ErrorCode.OFFLINE; -import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; -import static com.mpush.common.ErrorCode.ROUTER_CHANGE; - -/** - * Created by ohun on 2015/12/19. - * - * @author ohun@live.cn - */ -@ChannelHandler.Sharable -public final class ClientChannelHandler extends ChannelHandlerAdapter implements ChannelClientHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); - - private Client client; - - public ClientChannelHandler(Client client) { - this.client = client; - } - - @Override - public Client getClient() { - return client; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - client.getConnection().updateLastReadTime(); - if (msg instanceof Packet) { - Packet packet = ((Packet) msg); - PushRequest request = PushRequestBus.INSTANCE.remove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request timeout. packet={}", packet); - return; - } - - if (packet.cmd == Command.OK.cmd) { - request.success(); - } else { - ErrorMessage message = new ErrorMessage(packet, client.getConnection()); - if (message.code == OFFLINE.errorCode) { - request.offline(); - } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) { - request.failure(); - } else if (message.code == ROUTER_CHANGE.errorCode) { - request.redirect(); - } - LOGGER.warn("receive an error gateway response, message={}", message); - } - } - LOGGER.warn("update currentTime:"+ctx.channel()+","+ToStringBuilder.reflectionToString(msg)); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - client.close("exception"); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - Connection connection = new NettyConnection(); - connection.init(ctx.channel(), false); - client.initConnection(connection); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - client.close("inactive"); - LOGGER.info("client disconnect channel={}", ctx.channel()); - } - - -} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java deleted file mode 100644 index 98091e43..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/listener/ConnectZKListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.push.zk.listener; - - -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeListener; -import com.mpush.push.zk.manager.ConnectZKNodeManager; - -/** - * connection server 应用 监控 - */ -public class ConnectZKListener extends ZKServerNodeListener { - - private final ConnectZKNodeManager manager = new ConnectZKNodeManager(); - - @Override - public String listenerPath() { - return ZKPath.CONNECTION_SERVER.getWatchPath(); - } - - @Override - public String getRegisterPath() { - return ZKPath.CONNECTION_SERVER.getPath(); - } - - @Override - public ConnectZKNodeManager getManager() { - return manager; - } - - @Override - public String getFullPath(String raw) { - return ZKPath.CONNECTION_SERVER.getFullPath(raw); - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java b/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java deleted file mode 100644 index 17f90570..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/listener/GatewayZKListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.push.zk.listener; - -import com.mpush.push.zk.manager.GatewayZKNodeManager; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKServerNodeListener; - -/** - * gateway server 应用 监控 - */ -public class GatewayZKListener extends ZKServerNodeListener { - - private final GatewayZKNodeManager manager = new GatewayZKNodeManager(); - - @Override - public String listenerPath() { - return ZKPath.GATEWAY_SERVER.getWatchPath(); - } - - @Override - public GatewayZKNodeManager getManager() { - return manager; - } - - @Override - public String getRegisterPath() { - return ZKPath.GATEWAY_SERVER.getPath(); - } - - @Override - public String getFullPath(String raw) { - return ZKPath.GATEWAY_SERVER.getFullPath(raw); - } - - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java deleted file mode 100644 index df9c939a..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/ConnectZKNodeManager.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.mpush.push.zk.manager; - -import com.google.common.collect.Maps; -import com.mpush.zk.ZKServerNode; -import com.mpush.zk.ZKNodeManager; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -public class ConnectZKNodeManager implements ZKNodeManager { - - private final Logger log = LoggerFactory.getLogger(ConnectZKNodeManager.class); - - private final Map cache = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, ZKServerNode node) { - if (StringUtils.isNotBlank(fullPath) && node != null) { - cache.put(fullPath, node); - } else { - log.error("fullPath is null or application is null"); - } - printList(); - } - - @Override - public void remove(String fullPath) { - cache.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(cache.values()); - } - - private void printList() { - for (ZKServerNode app : cache.values()) { - log.warn(app.toString()); - } - } - -} diff --git a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java b/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java deleted file mode 100644 index 66648c52..00000000 --- a/mpush-client/src/main/java/com/mpush/push/zk/manager/GatewayZKNodeManager.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.mpush.push.zk.manager; - -import com.google.common.collect.Maps; -import com.mpush.netty.client.NettyClientFactory; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; -import com.mpush.api.Client; -import com.mpush.api.connection.Connection; -import com.mpush.netty.client.NettyClient; -import com.mpush.push.client.ClientChannelHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -public class GatewayZKNodeManager implements ZKNodeManager { - - private final Logger log = LoggerFactory.getLogger(GatewayZKNodeManager.class); - - private static Map holder = Maps.newConcurrentMap(); - - private final Map application2Client = Maps.newConcurrentMap(); - - private final Map ip2Client = Maps.newConcurrentMap(); - - @Override - public void addOrUpdate(String fullPath, ZKServerNode application) { - holder.put(fullPath, application); - try { - Client client = new NettyClient(application.getIp(), application.getPort()); - ClientChannelHandler handler = new ClientChannelHandler(client); - NettyClientFactory.INSTANCE.create(handler); - application2Client.put(application, client); - ip2Client.put(application.getIp(), client); - } catch (Exception e) { - log.error("addOrUpdate:{},{}", fullPath, application, e); - } - printList(); - } - - @Override - public void remove(String fullPath) { - ZKServerNode application = get(fullPath); - if (application != null) { - Client client = application2Client.get(application); - if (client != null) { - client.stop(); - } - } - ip2Client.remove(application.getIp() + ":" + application.getPort()); - holder.remove(fullPath); - printList(); - } - - @Override - public Collection getList() { - return Collections.unmodifiableCollection(holder.values()); - } - - private void printList() { - for (ZKServerNode app : holder.values()) { - log.warn(app.toString()); - } - } - - public ZKServerNode get(String fullpath) { - return holder.get(fullpath); - } - - public Client getClient(ZKServerNode application) { - return application2Client.get(application); - } - - public Connection getConnection(String ipAndPort) { - Client client = ip2Client.get(ipAndPort); - if (client == null) return null; - return client.getConnection(); - } - -} - diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory b/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory new file mode 100644 index 00000000..93607f48 --- /dev/null +++ b/mpush-client/src/main/resources/META-INF/services/com.mpush.api.spi.client.PusherFactory @@ -0,0 +1 @@ +com.mpush.client.push.PushClientFactory diff --git a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager b/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager deleted file mode 100644 index d9e4979b..00000000 --- a/mpush-client/src/main/resources/META-INF/services/com.mpush.zk.ZKNodeManager +++ /dev/null @@ -1,2 +0,0 @@ -com.mpush.push.zk.manager.ConnectZKNodeManager -com.mpush.push.zk.manager.GatewayZKNodeManager diff --git a/mpush-client/src/test/java/com/mpush/client/PushClientTest.java b/mpush-client/src/test/java/com/mpush/client/PushClientTest.java deleted file mode 100644 index cd9c4979..00000000 --- a/mpush-client/src/test/java/com/mpush/client/PushClientTest.java +++ /dev/null @@ -1,50 +0,0 @@ -//package com.mpush.client; -// -//import PushSender; -//import org.junit.Test; -// -//import java.util.Arrays; -//import java.util.concurrent.locks.LockSupport; -// -//import static org.junit.Assert.*; -// -///** -// * Created by ohun on 2016/1/7. -// */ -//public class PushClientTest { -// -// @Test -// public void testSend() throws Exception { -// -// } -// -// public static void main(String[] args) throws Exception { -// PushClient client = new PushClient(); -// client.init(); -// Thread.sleep(1000); -// client.send("this a first push", Arrays.asList("user-0", "user-1", "user-2", "user-3", "user-4"), -// new PushSender.Callback() { -// @Override -// public void onSuccess(String userId) { -// System.err.println("push onSuccess userId=" + userId); -// } -// -// @Override -// public void onFailure(String userId) { -// System.err.println("push onFailure userId=" + userId); -// } -// -// @Override -// public void onOffline(String userId) { -// System.err.println("push onOffline userId=" + userId); -// } -// -// @Override -// public void onTimeout(String userId) { -// System.err.println("push onTimeout userId=" + userId); -// } -// } -// ); -// LockSupport.park(); -// } -//} \ No newline at end of file diff --git a/mpush-client/src/test/resources/logback.xml b/mpush-client/src/test/resources/logback.xml deleted file mode 100644 index 20979b40..00000000 --- a/mpush-client/src/test/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-common/.gitignore b/mpush-common/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-common/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 867dbf8e..d97f639b 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -11,7 +11,7 @@ ${mpush.groupId} mpush-common - ${mpush-common-version} + ${mpush.version} mpush-common jar @@ -28,6 +28,10 @@ ${mpush.groupId} mpush-zk + + ${mpush.groupId} + mpush-cache + diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java b/mpush-common/src/main/java/com/mpush/common/AbstractClient.java deleted file mode 100644 index 1c5e2a5f..00000000 --- a/mpush-common/src/main/java/com/mpush/common/AbstractClient.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.mpush.common; - -import com.google.common.collect.Lists; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.zk.listener.ZKDataChangeListener; -import com.mpush.zk.listener.ZKRedisNodeListener; - -import java.util.List; - -public abstract class AbstractClient { - - protected List dataChangeListeners = Lists.newArrayList(); - - protected ZKClient zkClient = ZKClient.I; - - public AbstractClient() { - registerListener(new ZKRedisNodeListener()); - } - - public void registerListener(ZKDataChangeListener listener) { - dataChangeListeners.add(listener); - } - - //step2 获取redis - private void initRedis() { - boolean exist = zkClient.isExisted(ZKPath.REDIS_SERVER.getPath()); - if (!exist) { - List groupList = ConfigCenter.I.redisGroups(); - zkClient.registerPersist(ZKPath.REDIS_SERVER.getPath(), Jsons.toJson(groupList)); - } - } - - //step3 注册listener - private void registerListeners() { - for (ZKDataChangeListener listener : dataChangeListeners) { - zkClient.registerListener(listener); - } - } - - //step4 初始化 listener data - private void initListenerData() { - for (ZKDataChangeListener listener : dataChangeListeners) { - listener.initData(); - } - } - - public void start() { - initRedis(); - registerListeners(); - initListenerData(); - } -} diff --git a/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java b/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java deleted file mode 100644 index a9081df0..00000000 --- a/mpush-common/src/main/java/com/mpush/common/AbstractEventContainer.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.mpush.common; - -public abstract class AbstractEventContainer { - - public AbstractEventContainer() { - EventBus.INSTANCE.register(this); - } - -} diff --git a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java index cd881c08..6bcc470f 100644 --- a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java +++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common; /** @@ -10,6 +29,7 @@ public enum ErrorCode { PUSH_CLIENT_FAILURE(2, "push to client failure"), ROUTER_CHANGE(3, "router change"), DISPATCH_ERROR(100, "handle message error"), + UNSUPPORTED_CMD(101, "unsupported command"), UNKNOWN(-1, "unknown"); ErrorCode(int code, String errorMsg) { diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 14b0eaf3..4cfda9b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common; import com.mpush.api.MessageHandler; @@ -5,15 +24,18 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.Profiler; import com.mpush.common.message.ErrorMessage; - +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import static com.mpush.common.ErrorCode.DISPATCH_ERROR; +import static com.mpush.common.ErrorCode.UNSUPPORTED_CMD; + /** * Created by ohun on 2015/12/22. * @@ -27,25 +49,30 @@ public void register(Command command, MessageHandler handler) { handlers.put(command.cmd, handler); } - @Override public void onReceive(Packet packet, Connection connection) { - MessageHandler handler = handlers.get(packet.cmd); - try { - if (handler != null) { - Profiler.enter("start handle:"+handler.getClass().getSimpleName()); + MessageHandler handler = handlers.get(packet.cmd); + if (handler != null) { + try { + Profiler.enter("start handle:" + handler.getClass().getSimpleName()); handler.handle(packet, connection); + } catch (Throwable throwable) { + LOGGER.error("dispatch message ex, packet={}, connect={}, body={}" + , packet, connection, Arrays.toString(packet.body), throwable); + ErrorMessage + .from(packet, connection) + .setErrorCode(DISPATCH_ERROR) + .close(); + } finally { + Profiler.release(); } - } catch (Throwable throwable) { - LOGGER.error("dispatch packet ex, packet={}, conn={}", packet, connection, throwable); + } else { + LOGGER.error("dispatch message failure unsupported cmd, packet={}, connect={}, body={}" + , packet, connection); ErrorMessage .from(packet, connection) - .setErrorCode(ErrorCode.DISPATCH_ERROR) + .setErrorCode(UNSUPPORTED_CMD) .close(); - }finally{ - if(handler!=null){ - Profiler.release(); - } } } } diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index bbd346a0..181e3d5c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; @@ -5,7 +24,7 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.tools.Profiler; +import com.mpush.tools.common.Profiler; /** * Created by ohun on 2015/12/22. diff --git a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java index b6c0c5e9..a451a1c0 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/ErrorMessageHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java index 6271a2cf..7d77b11c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/OkMessageHandler.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.handler; import com.mpush.api.connection.Connection; -import com.mpush.common.message.OkMessage; import com.mpush.api.protocol.Packet; +import com.mpush.common.message.OkMessage; /** * Created by ohun on 2015/12/30. diff --git a/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java deleted file mode 100644 index 1269273b..00000000 --- a/mpush-common/src/main/java/com/mpush/common/manage/user/UserManager.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.common.manage.user; - -import java.util.List; - -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.manage.RedisManage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.api.RedisKey; - -//查询使用 -public final class UserManager { - private static final Logger log = LoggerFactory.getLogger(UserManager.class); - public static final UserManager INSTANCE = new UserManager(); - - private final String ONLINE_KEY = RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress()); - - public UserManager() { - clearUserOnlineData(); - } - - public void clearUserOnlineData() { - RedisManage.del(ONLINE_KEY); - } - - public void recordUserOnline(String userId) { - RedisManage.zAdd(ONLINE_KEY, userId); - log.info("user online {}", userId); - } - - public void recordUserOffline(String userId) { - RedisManage.zRem(ONLINE_KEY, userId); - log.info("user offline {}", userId); - } - - //在线用户 - public long getOnlineUserNum() { - return RedisManage.zCard(ONLINE_KEY); - } - - //在线用户列表 - public List getOnlineUserList(int start, int size) { - if (size < 10) { - size = 10; - } - return RedisManage.zrange(ONLINE_KEY, start, size - 1, String.class); - } -} diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 7c056e6e..8c3bd221 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -1,12 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.Message; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.tools.IOUtils; -import com.mpush.tools.Profiler; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.common.IOUtils; +import com.mpush.tools.config.CC; import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; @@ -24,12 +42,7 @@ public abstract class BaseMessage implements Message { public BaseMessage(Packet packet, Connection connection) { this.packet = packet; this.connection = connection; - Profiler.enter("start decode message"); - try { - decodeBody(); - } finally { - Profiler.release(); - } + decodeBody(); } protected void decodeBody() { @@ -43,7 +56,7 @@ protected void decodeBody() { } //2.解压 if (packet.hasFlag(Packet.FLAG_COMPRESS)) { - tmp = IOUtils.uncompress(tmp); + tmp = IOUtils.decompress(tmp); } if (tmp.length == 0) { @@ -59,11 +72,11 @@ protected void encodeBody() { byte[] tmp = encode(); if (tmp != null && tmp.length > 0) { //1.压缩 - if (tmp.length > ConfigCenter.I.compressLimit()) { + if (tmp.length > CC.mp.core.compress_threshold) { byte[] result = IOUtils.compress(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Packet.FLAG_COMPRESS); + packet.addFlag(Packet.FLAG_COMPRESS); } } @@ -73,7 +86,7 @@ protected void encodeBody() { byte[] result = context.cipher.encrypt(tmp); if (result.length > 0) { tmp = result; - packet.setFlag(Packet.FLAG_CRYPTO); + packet.addFlag(Packet.FLAG_CRYPTO); } } packet.body = tmp; @@ -131,10 +144,5 @@ public int getSessionId() { } @Override - public String toString() { - return "BaseMessage{" + - "packet=" + packet + - ", connection=" + connection + - '}'; - } + public abstract String toString(); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index 4638e36a..18a10f6a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -36,4 +55,14 @@ public void encode(ByteBuf body) { encodeString(body, alias); encodeString(body, tags); } + + @Override + public String toString() { + return "BindUserMessage{" + + "alias='" + alias + '\'' + + ", userId='" + userId + '\'' + + ", tags='" + tags + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index 81e7674a..bb7ecb83 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; -import com.mpush.api.connection.Connection; import com.mpush.api.Constants; +import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index 435bb0ad..9126e3aa 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -17,6 +36,7 @@ public final class ErrorMessage extends ByteBufMessage { public byte cmd; public byte code; public String reason; + public String data; public ErrorMessage(byte cmd, Packet message, Connection connection) { super(message, connection); @@ -32,6 +52,7 @@ public void decode(ByteBuf body) { cmd = decodeByte(body); code = decodeByte(body); reason = decodeString(body); + data = decodeString(body); } @Override @@ -39,6 +60,7 @@ public void encode(ByteBuf body) { encodeByte(body, cmd); encodeByte(body, code); encodeString(body, reason); + encodeString(body, data); } public static ErrorMessage from(BaseMessage src) { @@ -56,6 +78,11 @@ public ErrorMessage setReason(String reason) { return this; } + public ErrorMessage setData(String data) { + this.data = data; + return this; + } + public ErrorMessage setErrorCode(ErrorCode code) { this.code = code.errorCode; this.reason = code.errorMsg; diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java index e60bd0f7..b1cbf8b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -40,4 +59,15 @@ public void encode(ByteBuf body) { encodeInt(body, minHeartbeat); encodeInt(body, maxHeartbeat); } + + @Override + public String toString() { + return "FastConnectMessage{" + + "deviceId='" + deviceId + '\'' + + ", sessionId='" + sessionId + '\'' + + ", minHeartbeat=" + minHeartbeat + + ", maxHeartbeat=" + maxHeartbeat + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index 2e4982c6..917f206c 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; @@ -34,4 +53,12 @@ public FastConnectOkMessage setHeartbeat(int heartbeat) { this.heartbeat = heartbeat; return this; } + + @Override + public String toString() { + return "FastConnectOkMessage{" + + "heartbeat=" + heartbeat + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 6b67aea7..02d20c5b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -1,9 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Arrays; + import static com.mpush.api.protocol.Command.HANDSHAKE; /** @@ -54,4 +75,20 @@ public void encode(ByteBuf body) { encodeInt(body, maxHeartbeat); encodeLong(body, timestamp); } + + @Override + public String toString() { + return "HandshakeMessage{" + + "clientKey=" + Arrays.toString(clientKey) + + ", deviceId='" + deviceId + '\'' + + ", osName='" + osName + '\'' + + ", osVersion='" + osVersion + '\'' + + ", clientVersion='" + clientVersion + '\'' + + ", iv=" + Arrays.toString(iv) + + ", minHeartbeat=" + minHeartbeat + + ", maxHeartbeat=" + maxHeartbeat + + ", timestamp=" + timestamp + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index 990cad06..26fa9ca3 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -1,9 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Arrays; + /** * Created by ohun on 2015/12/27. * @@ -58,4 +79,15 @@ public HandshakeOkMessage setExpireTime(long expireTime) { this.expireTime = expireTime; return this; } + + @Override + public String toString() { + return "HandshakeOkMessage{" + + "expireTime=" + expireTime + + ", serverKey=" + Arrays.toString(serverKey) + + ", heartbeat=" + heartbeat + + ", sessionId='" + sessionId + '\'' + + ", packet=" + packet + + '}'; + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index bc1edcdd..0102f5c4 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -1,9 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; import java.util.Map; @@ -31,7 +50,7 @@ public HttpRequestMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { method = decodeByte(body); uri = decodeString(body); - headers = MPushUtil.headerFromString(decodeString(body)); + headers = Utils.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -39,7 +58,7 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeByte(body, method); encodeString(body, uri); - encodeString(body, MPushUtil.headerToString(headers)); + encodeString(body, Utils.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 5b53aa40..4fde32d6 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; import java.util.HashMap; @@ -27,7 +46,7 @@ public HttpResponseMessage(Packet message, Connection connection) { public void decode(ByteBuf body) { statusCode = decodeInt(body); reasonPhrase = decodeString(body); - headers = MPushUtil.headerFromString(decodeString(body)); + headers = Utils.headerFromString(decodeString(body)); this.body = decodeBytes(body); } @@ -35,7 +54,7 @@ public void decode(ByteBuf body) { public void encode(ByteBuf body) { encodeInt(body, statusCode); encodeString(body, reasonPhrase); - encodeString(body, MPushUtil.headerToString(headers)); + encodeString(body, Utils.headerToString(headers)); encodeBytes(body, this.body); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java index 3c2293df..15571038 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index b48fbc9b..5edcb2d9 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.connection.Connection; diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index c8b3b608..b93a252b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -1,9 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message; import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import java.util.Arrays; + import static com.mpush.api.protocol.Command.PUSH; /** @@ -13,24 +34,32 @@ */ public final class PushMessage extends BaseMessage { - public String content; + public byte[] content; public PushMessage(Packet packet, Connection connection) { super(packet, connection); } - public PushMessage(String content, Connection connection) { + public PushMessage(byte[] content, Connection connection) { super(new Packet(PUSH, genSessionId()), connection); this.content = content; } @Override public void decode(byte[] body) { - content = new String(body, Constants.UTF_8); + content = body; } @Override public byte[] encode() { - return content == null ? null : content.getBytes(Constants.UTF_8); + return content; + } + + @Override + public String toString() { + return "PushMessage{" + + "content='" + content.length + '\'' + + ", packet=" + packet + + '}'; } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 328eace3..ead4f160 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.message.gateway; import com.mpush.api.connection.Connection; @@ -15,11 +34,13 @@ */ public class GatewayPushMessage extends ByteBufMessage { public String userId; - public String content; + public int clientType; + public byte[] content; - public GatewayPushMessage(String userId, String content, Connection connection) { + public GatewayPushMessage(String userId, int clientType, byte[] content, Connection connection) { super(new Packet(GATEWAY_PUSH, genSessionId()), connection); this.userId = userId; + this.clientType = clientType; this.content = content; } @@ -30,13 +51,15 @@ public GatewayPushMessage(Packet message, Connection connection) { @Override public void decode(ByteBuf body) { userId = decodeString(body); - content = decodeString(body); + clientType = decodeInt(body); + content = decodeBytes(body); } @Override public void encode(ByteBuf body) { encodeString(body, userId); - encodeString(body, content); + encodeInt(body, clientType); + encodeBytes(body, content); } @Override @@ -53,7 +76,8 @@ public void send(ChannelFutureListener listener) { public String toString() { return "GatewayPushMessage{" + "userId='" + userId + '\'' + - ", content='" + content + '\'' + + "clientType='" + clientType + '\'' + + ", content='" + content.length + '\'' + '}'; } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java new file mode 100644 index 00000000..f1ad04ea --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -0,0 +1,118 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.net; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.api.spi.net.DnsMappingManager; +import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import static com.mpush.tools.Utils.checkHealth; + +public class HttpProxyDnsMappingManager extends BaseService implements DnsMappingManager, Runnable { + private final Logger logger = LoggerFactory.getLogger(HttpProxyDnsMappingManager.class); + + public HttpProxyDnsMappingManager() { + } + + private final Map> all = Maps.newConcurrentMap(); + private Map> available = Maps.newConcurrentMap(); + + private ScheduledExecutorService executorService; + + @Override + protected void doStart(Listener listener) throws Throwable { + if (all.size() > 0) { + executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns + } + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (executorService != null) { + executorService.shutdown(); + } + } + + @Override + public void init() { + logger.error("start init dnsMapping"); + all.putAll(CC.mp.http.dns_mapping); + available.putAll(CC.mp.http.dns_mapping); + logger.error("end init dnsMapping"); + } + + @Override + public boolean isRunning() { + return executorService != null && !executorService.isShutdown(); + } + + public void update(Map> nowAvailable) { + available = nowAvailable; + } + + public Map> getAll() { + return all; + } + + public DnsMapping lookup(String origin) { + if (available.isEmpty()) return null; + List list = available.get(origin); + if (list == null || list.isEmpty()) return null; + int L = list.size(); + if (L == 1) return list.get(0); + return list.get((int) (Math.random() * L % L)); + } + + @Override + public void run() { + logger.debug("start dns mapping checkHealth"); + Map> all = this.getAll(); + Map> available = Maps.newConcurrentMap(); + for (Map.Entry> entry : all.entrySet()) { + String key = entry.getKey(); + List value = entry.getValue(); + List nowValue = Lists.newArrayList(); + for (DnsMapping temp : value) { + boolean isOk = checkHealth(temp.getIp(), temp.getPort()); + if (isOk) { + nowValue.add(temp); + } else { + logger.error("dns can not reachable:" + Jsons.toJson(temp)); + } + } + available.put(key, nowValue); + } + this.update(available); + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java index 4c548316..fec83fde 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java @@ -1,33 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.mpush.api.router.ClientType; +import java.util.Set; import java.util.concurrent.TimeUnit; /** * Created by ohun on 2016/1/4. */ public final class ConnectionRouterManager extends RemoteRouterManager { - public static final ConnectionRouterManager INSTANCE = new ConnectionRouterManager(); + public static final ConnectionRouterManager I = new ConnectionRouterManager(); // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 - private final Cache cache = CacheBuilder + private final Cache> cache = CacheBuilder .newBuilder() .expireAfterWrite(5, TimeUnit.MINUTES) .expireAfterAccess(5, TimeUnit.MINUTES) .build(); @Override - public RemoteRouter lookup(String userId) { - RemoteRouter cached = cache.getIfPresent(userId); + public Set lookupAll(String userId) { + Set cached = cache.getIfPresent(userId); if (cached != null) return cached; - RemoteRouter router = super.lookup(userId); + Set router = super.lookupAll(userId); if (router != null) { cache.put(userId, router); } return router; } + @Override + public RemoteRouter lookup(String userId, int clientType) { + Set cached = this.lookupAll(userId); + for (RemoteRouter router : cached) { + if (router.getRouteValue().getClientType() == clientType) { + return router; + } + } + return null; + } + /** * 如果推送失败,可能是缓存不一致了,可以让本地缓存失效 *

@@ -36,6 +68,6 @@ public RemoteRouter lookup(String userId) { * @param userId */ public void invalidateLocalCache(String userId) { - cache.invalidate(userId); + if (userId != null) cache.invalidate(userId); } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index 5f1f78f1..d7d9efed 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; -import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.Router; /** * Created by ohun on 2015/12/23. diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 510372b5..ef71818d 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -1,43 +1,113 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; -import com.mpush.api.RedisKey; +import com.google.common.eventbus.Subscribe; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; -import com.mpush.tools.redis.manage.RedisManage; - +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + /** * Created by ohun on 2015/12/23. * * @author ohun@live.cn */ -public class RemoteRouterManager implements RouterManager { +public class RemoteRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(RemoteRouterManager.class); @Override public RemoteRouter register(String userId, RemoteRouter router) { - LOGGER.info("register remote router success userId={}, router={}", userId, router); - String key = RedisKey.getUserKey(userId); - RemoteRouter old = RedisManage.get(key, RemoteRouter.class); + LOGGER.info("register remote router success userId={}, router={}", userId, router); + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(router.getRouteValue().getClientType()); + ClientLocation old = RedisManager.I.hget(key, field, ClientLocation.class); if (old != null) { - RedisManage.del(key); + RedisManager.I.hdel(key, field); } - RedisManage.set(key, router); - return old; + RedisManager.I.hset(key, field, router.getRouteValue()); + return old == null ? null : new RemoteRouter(old); } @Override - public boolean unRegister(String userId) { - String key = RedisKey.getUserKey(userId); - RedisManage.del(key); - LOGGER.info("unRegister remote router success userId={}", userId); + public boolean unRegister(String userId, int clientType) { + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(clientType); + RedisManager.I.hdel(key, field); + LOGGER.info("unRegister remote router success userId={}, clientType={}", userId, clientType); return true; } @Override - public RemoteRouter lookup(String userId) { - String key = RedisKey.getUserKey(userId); - return RedisManage.get(key, RemoteRouter.class); + public Set lookupAll(String userId) { + String key = RedisKey.getUserKey(userId); + Map values = RedisManager.I.hgetAll(key, ClientLocation.class); + if (values == null || values.isEmpty()) return Collections.emptySet(); + return values.values().stream().map(RemoteRouter::new).collect(Collectors.toSet()); + } + + @Override + public RemoteRouter lookup(String userId, int clientType) { + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(clientType); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + LOGGER.info("lookup remote router userId={}, router={}", userId, location); + return location == null ? null : new RemoteRouter(location); + } + + /** + * 监听链接关闭事件,清理失效的路由 + * + * @param event + */ + @Subscribe + void on(ConnectionCloseEvent event) { + Connection connection = event.connection; + if (connection == null) return; + SessionContext context = connection.getSessionContext(); + String userId = context.userId; + if (userId == null) return; + String key = RedisKey.getUserKey(userId); + String field = Integer.toString(context.getClientType()); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + if (location == null) return; + + String connId = connection.getId(); + //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 + if (connId.equals(location.getConnId())) { + RedisManager.I.hdel(key, field); + LOGGER.info("clean disconnected remote route, userId={}, route={}", userId, location); + } else { + LOGGER.info("clean disconnected remote route, not clean:userId={}, route={}", userId, location); + } } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java index dd1a16db..e619ddf4 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java @@ -1,52 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.router; -import com.mpush.tools.redis.listener.MessageListener; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.cache.redis.listener.MessageListener; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.Utils; +import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.common.AbstractEventContainer; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.redis.listener.ListenerDispatcher; -import com.mpush.tools.redis.manage.RedisManage; - /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ -public class UserChangeListener extends AbstractEventContainer implements MessageListener { - - private static final Logger log = LoggerFactory.getLogger(UserChangeListener.class); - +public class UserChangeListener extends EventConsumer implements MessageListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserChangeListener.class); + public static final String ONLINE_CHANNEL = "/mpush/online/"; - + public static final String OFFLINE_CHANNEL = "/mpush/offline/"; //只需要一台机器注册online、offline 消息通道 public UserChangeListener() { - if(ConfigCenter.I.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){ - ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this); - ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this); - }else{ - log.error("UserChangeListener is not localhost,required:{},but:{}",ConfigCenter.I.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp()); - } + if ("127.0.0.1".equals(Utils.getLocalIp())) { + ListenerDispatcher.I.subscribe(getOnlineChannel(), this); + ListenerDispatcher.I.subscribe(getOfflineChannel(), this); + } else { + LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", Utils.getLocalIp()); + } } public String getOnlineChannel() { return ONLINE_CHANNEL; } - + public String getOfflineChannel() { return OFFLINE_CHANNEL; } - + public void userOnline(String userId) { - RedisManage.publish(getOnlineChannel(), userId); + RedisManager.I.publish(getOnlineChannel(), userId); } - - public void userOffline(String userId){ - RedisManage.publish(getOnlineChannel(), userId); + + public void userOffline(String userId) { + RedisManager.I.publish(getOnlineChannel(), userId); } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java index 2bf17464..52c6e4b6 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/AesCipher.java @@ -1,9 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; -import com.mpush.tools.Profiler; import com.mpush.tools.crypto.AESUtils; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import static com.mpush.tools.crypto.AESUtils.KEY_ALGORITHM; + /** * Created by ohun on 2015/12/28. @@ -13,30 +36,25 @@ public final class AesCipher implements Cipher { public final byte[] key; public final byte[] iv; + private final IvParameterSpec zeroIv; + private final SecretKeySpec keySpec; public AesCipher(byte[] key, byte[] iv) { this.key = key; this.iv = iv; + this.zeroIv = new IvParameterSpec(iv); + this.keySpec = new SecretKeySpec(key, KEY_ALGORITHM); } + @Override - public byte[] decrypt(byte[] data) { - try { - Profiler.enter("start aes decrypt"); - return AESUtils.decrypt(data, key, iv); - } finally { - Profiler.release(); - } + public byte[] encrypt(byte[] data) { + return AESUtils.encrypt(data, zeroIv, keySpec); } @Override - public byte[] encrypt(byte[] data) { - try { - Profiler.enter("start encrypt"); - return AESUtils.encrypt(data, key, iv); - } finally { - Profiler.release(); - } + public byte[] decrypt(byte[] data) { + return AESUtils.decrypt(data, zeroIv, keySpec); } @Override @@ -55,7 +73,7 @@ public String toString(byte[] a) { public static byte[] toArray(String str) { String[] a = str.split("\\|"); - if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { + if (a.length != CipherBox.I.getAesKeyLength()) { return null; } byte[] bytes = new byte[a.length]; diff --git a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java index d8585cdb..c471a5d5 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java +++ b/mpush-common/src/main/java/com/mpush/common/security/CipherBox.java @@ -1,6 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import com.mpush.tools.crypto.RSAUtils; import java.security.SecureRandom; @@ -13,15 +32,15 @@ * @author ohun@live.cn */ public final class CipherBox { - public int aesKeyLength = ConfigCenter.I.aesKeyLength(); - public static final CipherBox INSTANCE = new CipherBox(); + public final int aesKeyLength = CC.mp.security.aes_key_length; + public static final CipherBox I = new CipherBox(); private SecureRandom random = new SecureRandom(); private RSAPrivateKey privateKey; private RSAPublicKey publicKey; public RSAPrivateKey getPrivateKey() { if (privateKey == null) { - String key = ConfigCenter.I.privateKey(); + String key = CC.mp.security.private_key; try { privateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(key); } catch (Exception e) { @@ -33,7 +52,7 @@ public RSAPrivateKey getPrivateKey() { public RSAPublicKey getPublicKey() { if (publicKey == null) { - String key = ConfigCenter.I.publicKey(); + String key = CC.mp.security.public_key; try { publicKey = (RSAPublicKey) RSAUtils.decodePublicKey(key); } catch (Exception e) { @@ -70,8 +89,4 @@ public byte[] mixKey(byte[] clientKey, byte[] serverKey) { public int getAesKeyLength() { return aesKeyLength; } - - public RsaCipher getRsaCipher() { - return new RsaCipher(getPrivateKey(), getPublicKey()); - } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java index 2afce590..4acfa4b9 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipher.java @@ -1,7 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.common.security; import com.mpush.api.connection.Cipher; -import com.mpush.tools.Profiler; import com.mpush.tools.crypto.RSAUtils; import java.security.interfaces.RSAPrivateKey; @@ -14,7 +32,6 @@ */ public final class RsaCipher implements Cipher { private final RSAPrivateKey privateKey; - private final RSAPublicKey publicKey; public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @@ -24,28 +41,20 @@ public RsaCipher(RSAPrivateKey privateKey, RSAPublicKey publicKey) { @Override public byte[] decrypt(byte[] data) { - try{ - Profiler.enter("start rsa decrypt"); - return RSAUtils.decryptByPrivateKey(data, privateKey); - }finally{ - Profiler.release(); - } - + return RSAUtils.decryptByPrivateKey(data, privateKey); } @Override public byte[] encrypt(byte[] data) { - try{ - Profiler.enter("start rsa encrypt"); - return RSAUtils.encryptByPublicKey(data, publicKey); - }finally{ - Profiler.release(); - } + return RSAUtils.encryptByPublicKey(data, publicKey); } - @Override - public String toString() { - return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; - } - + @Override + public String toString() { + return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; + } + + public static RsaCipher create() { + return new RsaCipher(CipherBox.I.getPrivateKey(), CipherBox.I.getPublicKey()); + } } diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java new file mode 100644 index 00000000..441f5006 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.security; + +import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.core.CipherFactory; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class RsaCipherFactory implements CipherFactory { + private static final RsaCipher RSA_CIPHER = RsaCipher.create(); + + @Override + public Cipher get() { + return RSA_CIPHER; + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java new file mode 100644 index 00000000..7a9d8372 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -0,0 +1,68 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.user; + +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.config.ConfigManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +//查询使用 +public final class UserManager { + private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); + public static final UserManager I = new UserManager(); + + private final String ONLINE_KEY = RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()); + + public UserManager() { + clearUserOnlineData(); + } + + public void clearUserOnlineData() { + RedisManager.I.del(ONLINE_KEY); + } + + public void recordUserOnline(String userId) { + RedisManager.I.zAdd(ONLINE_KEY, userId); + LOGGER.info("user online {}", userId); + } + + public void recordUserOffline(String userId) { + RedisManager.I.zRem(ONLINE_KEY, userId); + LOGGER.info("user offline {}", userId); + } + + //在线用户 + public long getOnlineUserNum() { + Long value = RedisManager.I.zCard(ONLINE_KEY); + return value == null ? 0 : value; + } + + //在线用户列表 + public List getOnlineUserList(int start, int size) { + if (size < 10) { + size = 10; + } + return RedisManager.I.zrange(ONLINE_KEY, start, size - 1, String.class); + } +} diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory new file mode 100644 index 00000000..02ce67e8 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.core.CipherFactory @@ -0,0 +1 @@ +com.mpush.common.security.RsaCipherFactory diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager new file mode 100644 index 00000000..d0dd9b36 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.net.DnsMappingManager @@ -0,0 +1 @@ +com.mpush.common.net.HttpProxyDnsMappingManager \ No newline at end of file diff --git a/mpush-core/.gitignore b/mpush-core/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-core/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index de05f1c5..174efd1a 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -2,11 +2,11 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} mpush-core @@ -20,27 +20,7 @@ ${mpush.groupId} - mpush-client - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring + mpush-common diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 82df0f24..bdc19126 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -1,45 +1,98 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; +import com.google.common.base.Strings; +import com.mpush.api.push.PushSender; +import com.mpush.api.service.Listener; +import com.mpush.common.router.RemoteRouter; +import com.mpush.common.user.UserManager; +import com.mpush.core.router.RouterCenter; +import com.mpush.core.server.AdminServer; +import com.mpush.tools.Jsons; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKClient; import com.mpush.zk.ZKPath; -import com.mpush.zk.ZKServerNode; -import com.mpush.api.RedisKey; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.zk.node.ZKServerNode; +import com.typesafe.config.ConfigRenderOptions; import io.netty.channel.*; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.PrintWriter; +import java.io.Serializable; +import java.io.StringWriter; +import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; @ChannelHandler.Sharable public final class AdminHandler extends SimpleChannelInboundHandler { - private static final Logger log = LoggerFactory.getLogger(AdminHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AdminHandler.class); - private static final String DOUBLE_END = "\r\n\r\n"; - private static final String ONE_END = "\r\n"; + private static final String EOL = "\r\n"; - protected static final ZKClient zkClient = ZKClient.I; + private static AdminServer adminServer; + + public AdminHandler(AdminServer adminServer) { + this.adminServer = adminServer; + } @Override protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { - Command command = Command.getCommand(request); - ChannelFuture future = ctx.write(command.handler(request) + DOUBLE_END); - if (command.equals(Command.QUIT)) { - future.addListener(ChannelFutureListener.CLOSE); + Command command = Command.help; + String arg = null; + String[] args = null; + if (request != null) { + String[] cmd_args = request.split(" "); + command = Command.toCmd(cmd_args[0].trim()); + if (cmd_args.length == 2) { + arg = cmd_args[1]; + } else if (cmd_args.length > 2) { + args = Arrays.copyOfRange(cmd_args, 1, cmd_args.length); + } + } + try { + Object result = args != null ? command.handler(ctx, args) : command.handler(ctx, arg); + ChannelFuture future = ctx.writeAndFlush(result + EOL + EOL); + if (command == Command.quit) { + future.addListener(ChannelFutureListener.CLOSE); + } + } catch (Throwable throwable) { + ctx.writeAndFlush(throwable.getLocalizedMessage() + EOL + EOL); + StringWriter writer = new StringWriter(1024); + throwable.printStackTrace(new PrintWriter(writer)); + ctx.writeAndFlush(writer.toString()); } - } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - ctx.write("welcome to " + MPushUtil.getInetAddress() + "!" + ONE_END); - ctx.write("It is " + new Date() + " now." + DOUBLE_END); + ctx.write("welcome to " + Utils.getInetAddress() + "!" + EOL); + ctx.write("It is " + new Date() + " now." + EOL + EOL); ctx.flush(); } @@ -49,87 +102,165 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { } public enum Command { - HELP("help") { + help { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { StringBuilder buf = new StringBuilder(); - buf.append("Command:" + ONE_END); - buf.append("help:display all command." + ONE_END); - buf.append("quit:exit telnet." + ONE_END); - buf.append("scn:statistics conn num." + ONE_END); - buf.append("rcs:remove connection server zk info." + ONE_END); - buf.append("scs:stop connection server."); + buf.append("Option Description" + EOL); + buf.append("------ -----------" + EOL); + buf.append("help show help" + EOL); + buf.append("quit exit console mode" + EOL); + buf.append("shutdown stop mpush server" + EOL); + buf.append("restart restart mpush server" + EOL); + buf.append("zk: query zk node" + EOL); + buf.append("count: count conn num or online user count" + EOL); + buf.append("route: show user route info" + EOL); + buf.append("push:, push test msg to client" + EOL); + buf.append("conf:[key] show config info" + EOL); + buf.append("monitor:[mxBean] show system monitor" + EOL); return buf.toString(); } }, - QUIT("quit") { + quit { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { return "have a good day!"; } }, - SCN("scn") { + shutdown { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + ctx.writeAndFlush("try close connect server..."); + adminServer.getConnectionServer().stop(new Listener() { + @Override + public void onSuccess(Object... args) { + ctx.writeAndFlush("connect server close success" + EOL); + adminServer.stop(null);//这个一定要在System.exit之前调用,不然jvm 会卡死 @see com.mpush.bootstrap.Main#addHook + System.exit(0); + } + + @Override + public void onFailure(Throwable cause) { + ctx.writeAndFlush("connect server close failure, msg=" + cause.getLocalizedMessage()); + } + }); + return null; + } + }, + restart { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + return "unsupported"; + } + }, + zk { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + switch (args) { + case "redis": + return ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); + case "cs": + return getNodeData(ZKPath.CONNECT_SERVER); + case "gs": + return getNodeData(ZKPath.GATEWAY_SERVER); + + } + return "[" + args + "] unsupported, try help."; + } + + private String getNodeData(ZKPath path) { + List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); + StringBuilder sb = new StringBuilder(); + for (String raw : rawData) { + sb.append(ZKClient.I.get(path.getFullPath(raw))).append('\n'); + } + return sb.toString(); + } + }, + count { + @Override + public Serializable handler(ChannelHandlerContext ctx, String args) { + switch (args) { + case "conn": + return adminServer.getConnectionServer().getConnectionManager().getConnections().size(); + case "online": { + return UserManager.I.getOnlineUserNum(); + } + + } + return "[" + args + "] unsupported, try help."; + } + }, + route { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + if (Strings.isNullOrEmpty(args)) return "please input userId"; + Set routers = RouterCenter.I.getRemoteRouterManager().lookupAll(args); + if (routers.isEmpty()) return "user [" + args + "] offline now."; + return Jsons.toJson(routers); + } + }, + push { @Override - public String handler(String request) { - Long value = RedisManage.zCard(RedisKey.getUserOnlineKey(MPushUtil.getExtranetAddress())); - if (value == null) { - value = 0L; + public String handler(ChannelHandlerContext ctx, String... args) throws Exception { + Boolean success = PushSender.create().send(args[1], args[0], null).get(5, TimeUnit.SECONDS); + + return success.toString(); + } + }, + conf { + @Override + public String handler(ChannelHandlerContext ctx, String args) { + if (Strings.isNullOrEmpty(args)) { + return CC.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true)); + } + if (CC.cfg.hasPath(args)) { + return CC.cfg.getAnyRef(args).toString(); } - return value.toString() + "."; + return "key [" + args + "] not find in config"; } }, - RCS("rcs") { + rcs { @Override - public String handler(String request) { + public String handler(ChannelHandlerContext ctx, String args) { - List rawData = zkClient.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath()); + List rawData = ZKClient.I.getChildrenKeys(ZKPath.CONNECT_SERVER.getRootPath()); boolean removeSuccess = false; + String localIp = ConfigManager.I.getLocalIp(); for (String raw : rawData) { - String data = zkClient.get(ZKPath.CONNECTION_SERVER.getFullPath(raw)); + String dataPath = ZKPath.CONNECT_SERVER.getFullPath(raw); + String data = ZKClient.I.get(dataPath); ZKServerNode serverNode = Jsons.fromJson(data, ZKServerNode.class); - if (serverNode.getIp().equals(MPushUtil.getInetAddress())) { - zkClient.remove(ZKPath.CONNECTION_SERVER.getFullPath(raw)); - log.info("delete connection server success:{}", data); + if (serverNode.getIp().equals(localIp)) { + ZKClient.I.remove(dataPath); + LOGGER.info("delete connection server success:{}", data); removeSuccess = true; } else { - log.info("delete connection server failed: required ip:{}, but:{}", serverNode.getIp(), MPushUtil.getInetAddress()); + LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), Utils.getInetAddress()); } } if (removeSuccess) { - return "remove success."; + return "removeAndClose success."; } else { - return "remove false."; + return "removeAndClose false."; } } - }, - SCS("scs") { - @Override - public String handler(String request) { - return "not support now."; - } }; - private final String cmd; - - public abstract String handler(String request); - private Command(String cmd) { - this.cmd = cmd; + public Object handler(ChannelHandlerContext ctx, String... args) throws Exception { + return "unsupported"; } - public String getCmd() { - return cmd; + public Object handler(ChannelHandlerContext ctx, String args) throws Exception { + return "unsupported"; } - public static Command getCommand(String request) { - if (StringUtils.isNoneEmpty(request)) { - for (Command command : Command.values()) { - if (command.getCmd().equals(request)) { - return command; - } - } + public static Command toCmd(String cmd) { + try { + return Command.valueOf(cmd); + } catch (Exception e) { } - return HELP; + return help; } } - } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 30c25115..b02c3adf 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; @@ -5,13 +24,13 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOnlineEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.OkMessage; import com.mpush.core.router.RouterCenter; -import com.mpush.log.Logs; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/23. @@ -36,16 +55,15 @@ public void handle(BindUserMessage message) { SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 - boolean success = RouterCenter.INSTANCE.register(message.userId, message.getConnection()); + boolean success = RouterCenter.I.register(message.userId, message.getConnection()); if (success) { - - EventBus.INSTANCE.post(new UserOnlineEvent(message.getConnection(), message.userId)); - + context.userId = message.userId; + EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").send(); Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 - RouterCenter.INSTANCE.unRegister(message.userId); + RouterCenter.I.unRegister(message.userId, context.getClientType()); ErrorMessage.from(message).setReason("bind failed").close(); Logs.Conn.info("bind user failure, userId={}, session={}", message.userId, context); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index de0c9e40..bdf0abba 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.connection.Connection; @@ -8,10 +27,8 @@ import com.mpush.common.message.FastConnectOkMessage; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/25. @@ -19,7 +36,6 @@ * @author ohun@live.cn */ public final class FastConnectHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(FastConnectHandler.class); @Override public FastConnectMessage decode(Packet packet, Connection connection) { @@ -34,14 +50,16 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); + Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}" + , message.sessionId, message.deviceId); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context); + Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}" + , message.deviceId, session.context); } else { //3.校验成功,重新计算心跳,完成快速重连 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); session.context.setHeartbeat(heartbeat); message.getConnection().setSessionContext(session.context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 69acb209..dc7cfe78 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.connection.Connection; @@ -10,10 +29,8 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.core.router.LocalRouter; import com.mpush.core.router.RouterCenter; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; +import com.mpush.tools.Utils; +import com.mpush.tools.log.Logs; import static com.mpush.common.ErrorCode.*; @@ -32,7 +49,7 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { /** * 处理PushClient发送过来的Push推送请求 *

- * 查推送策略,先查本地路由,本地不存在,查远程,(注意:有可能远程也是本机) + * 查寻路由策略,先查本地路由,本地不存在,查远程,(注意:有可能远程查到也是本机IP) *

* 正常情况本地路由应该存在,如果不存在或链接失效,有以下几种情况: *

@@ -40,9 +57,9 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { * 2.客户端下线,本地路由失效,远程路由还未清除 * 3.PushClient使用了本地缓存,但缓存数据已经和实际情况不一致了 *

- * 对于三种情况的处理方式是, 再检查下远程路由: - * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 - * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 + * 对于三种情况的处理方式是, 再重新查寻下远程路由: + * 1.如果发现远程路由是本机,直接删除,因为此时的路由已失效 (解决场景2) + * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 (解决场景1,3) *

* * @param message @@ -62,7 +79,9 @@ public void handle(GatewayPushMessage message) { * @return */ private boolean checkLocal(final GatewayPushMessage message) { - LocalRouter router = RouterCenter.INSTANCE.getLocalRouterManager().lookup(message.userId); + String userId = message.userId; + int deviceId = message.clientType; + LocalRouter router = RouterCenter.I.getLocalRouterManager().lookup(userId, deviceId); //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 if (router == null) return false; @@ -72,10 +91,10 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - Logs.PUSH.info("gateway push, router in local but disconnect, userId={}, connection={}", message.userId, connection); + Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection); //删除已经失效的本地路由 - RouterCenter.INSTANCE.getLocalRouterManager().unRegister(message.userId); + RouterCenter.I.getLocalRouterManager().unRegister(userId, deviceId); return false; } @@ -83,21 +102,18 @@ private boolean checkLocal(final GatewayPushMessage message) { //3.链接可用,直接下发消息到手机客户端 PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - //推送成功 - OkMessage.from(message).setData(message.userId).send(); + pushMessage.send(future -> { + if (future.isSuccess()) { + //推送成功 + OkMessage.from(message).setData(userId + ',' + deviceId).send(); - Logs.PUSH.info("gateway push message to client success userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push message to client success, message={}", message); - } else { - //推送失败 - ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).send(); + } else { + //推送失败 + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + deviceId).send(); - Logs.PUSH.info("gateway push message to client failure userId={}, content={}", message.userId, message.content); - } + Logs.PUSH.info("gateway push message to client failure, message={}", message); } }); return true; @@ -112,35 +128,37 @@ public void operationComplete(ChannelFuture future) throws Exception { * @param message */ private void checkRemote(GatewayPushMessage message) { - RemoteRouter router = RouterCenter.INSTANCE.getRemoteRouterManager().lookup(message.userId); + String userId = message.userId; + int clientType = message.clientType; + RemoteRouter router = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); // 1.如果远程路由信息也不存在, 说明用户此时不在线, if (router == null) { - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send(); - Logs.PUSH.info("gateway push, router not exists user offline userId={}, content={}", message.userId, message.content); + Logs.PUSH.info("gateway push, router not exists user offline, message={}", message); return; } //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 - if (MPushUtil.getLocalIp().equals(router.getRouteValue().getHost())) { + if (Utils.getLocalIp().equals(router.getRouteValue().getHost())) { - ErrorMessage.from(message).setErrorCode(OFFLINE).send(); + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send(); //删除失效的远程缓存 - RouterCenter.INSTANCE.getRemoteRouterManager().unRegister(message.userId); + RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); - Logs.PUSH.info("gateway push error remote is local, userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, router); return; } //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 - ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).send(); + ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).send(); - Logs.PUSH.info("gateway push, router in remote userId={}, router={}", message.userId, router); + Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, router); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 30f3400b..17879376 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; @@ -5,7 +24,6 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.HandshakeEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.HandshakeMessage; @@ -14,10 +32,9 @@ import com.mpush.common.security.CipherBox; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; -import com.mpush.log.Logs; -import com.mpush.tools.MPushUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/24. @@ -25,34 +42,33 @@ * @author ohun@live.cn */ public final class HandshakeHandler extends BaseMessageHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); @Override public HandshakeMessage decode(Packet packet, Connection connection) { - return new HandshakeMessage(packet, connection); + return new HandshakeMessage(packet, connection); } @Override public void handle(HandshakeMessage message) { - + byte[] iv = message.iv;//AES密钥向量16位 byte[] clientKey = message.clientKey;//客户端随机数16位 - byte[] serverKey = CipherBox.INSTANCE.randomAESKey();//服务端随机数16位 - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey);//会话密钥16位 + byte[] serverKey = CipherBox.I.randomAESKey();//服务端随机数16位 + byte[] sessionKey = CipherBox.I.mixKey(clientKey, serverKey);//会话密钥16位 //1.校验客户端消息字段 if (Strings.isNullOrEmpty(message.deviceId) - || iv.length != CipherBox.INSTANCE.getAesKeyLength() - || clientKey.length != CipherBox.INSTANCE.getAesKeyLength()) { + || iv.length != CipherBox.I.getAesKeyLength() + || clientKey.length != CipherBox.I.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - Logs.Conn.info("client handshake false:{}", message.getConnection()); + Logs.Conn.info("handshake failure, message={}", message.toString()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - Logs.Conn.info("client handshake false for repeat handshake:{}", message.getConnection().getSessionContext()); + Logs.Conn.info("handshake failure, repeat handshake, session={}", message.getConnection().getSessionContext()); return; } @@ -63,7 +79,7 @@ public void handle(HandshakeMessage message) { ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); //5.计算心跳时间 - int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); + int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); //6.响应握手成功消息 HandshakeOkMessage @@ -88,7 +104,7 @@ public void handle(HandshakeMessage message) { ReusableSessionManager.INSTANCE.cacheSession(session); //10.触发握手成功事件 - EventBus.INSTANCE.post(new HandshakeEvent(message.getConnection(), heartbeat)); - Logs.Conn.info("client handshake success:{}", context); + EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); + Logs.Conn.info("handshake success, session={}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java index 5feea063..88ffaf18 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HeartBeatHandler.java @@ -1,9 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.log.Logs; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2015/12/22. @@ -15,7 +34,6 @@ public final class HeartBeatHandler implements MessageHandler { @Override public void handle(Packet packet, Connection connection) { connection.send(packet);//ping -> pong - Logs.HB.info("response client heartbeat:{}, {}", - connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("ping -> pong, {}", connection); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 4cc42f1d..efc014e2 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -1,25 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.log.Logs; -import com.mpush.netty.client.HttpCallback; -import com.mpush.netty.client.HttpClient; -import com.mpush.netty.client.RequestInfo; -import com.mpush.tools.Profiler; -import com.mpush.tools.dns.DnsMapping; -import com.mpush.tools.dns.manage.DnsMappingManage; +import com.mpush.common.net.HttpProxyDnsMappingManager; +import com.mpush.netty.http.HttpCallback; +import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.RequestContext; +import com.mpush.tools.common.Profiler; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; import org.slf4j.Logger; import java.net.InetSocketAddress; -import java.net.URI; -import java.net.URISyntaxException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Map; import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; @@ -33,6 +55,7 @@ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = Logs.HTTP; private final HttpClient httpClient; + private final DnsMappingManager dnsMappingManager = SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager); public HttpProxyHandler(HttpClient httpClient) { this.httpClient = httpClient; @@ -48,6 +71,7 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { public void handle(HttpRequestMessage message) { try { Profiler.enter("start http proxy handler"); + //1.参数校验 String method = message.getMethod(); String uri = message.uri; if (Strings.isNullOrEmpty(uri)) { @@ -59,18 +83,16 @@ public void handle(HttpRequestMessage message) { LOGGER.warn("request url is empty!"); } + //2.url转换 uri = doDnsMapping(uri); + + //3.包装成HTTP request FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); - Profiler.enter("start set full http headers"); - setHeaders(request, message); - Profiler.release(); - Profiler.enter("start set full http body"); - setBody(request, message); - Profiler.release(); + setHeaders(request, message);//处理header + setBody(request, message);//处理body - Profiler.enter("start http proxy request"); - httpClient.request(new RequestInfo(request, new DefaultHttpCallback(message))); - Profiler.release(); + //4.发送请求 + httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage .from(message) @@ -160,10 +182,8 @@ private void setHeaders(FullHttpRequest request, HttpRequestMessage message) { } } InetSocketAddress remoteAddress = (InetSocketAddress) message.getConnection().getChannel().remoteAddress(); - Profiler.enter("start set x-forwarded-for"); - String remoteIp = remoteAddress.getAddress().getHostAddress(); + String remoteIp = remoteAddress.getAddress().getHostAddress();//这个要小心,不要使用getHostName,不然会耗时比较大 request.headers().add("x-forwarded-for", remoteIp); - Profiler.release(); request.headers().add("x-forwarded-port", Integer.toString(remoteAddress.getPort())); } @@ -176,15 +196,19 @@ private void setBody(FullHttpRequest request, HttpRequestMessage message) { } private String doDnsMapping(String url) { - URI uri = null; + URL uri = null; try { - uri = new URI(url); - } catch (URISyntaxException e) { + uri = new URL(url); + } catch (MalformedURLException e) { + } + if (uri == null) { + return url; } - if (uri == null) return url; String host = uri.getHost(); - DnsMapping mapping = DnsMappingManage.holder.translate(host); - if (mapping == null) return url; - return url.replaceFirst(host, mapping.toString()); + DnsMapping mapping = dnsMappingManager.lookup(host); + if (mapping == null) { + return url; + } + return mapping.translate(uri); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java index 8b339af8..ad8739e8 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java @@ -1,13 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.handler; import com.google.common.base.Strings; -import com.mpush.core.router.LocalRouterManager; -import com.mpush.core.router.RouterCenter; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.protocol.Packet; -import com.mpush.common.EventBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; @@ -15,7 +31,10 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.core.router.LocalRouter; -import com.mpush.log.Logs; +import com.mpush.core.router.LocalRouterManager; +import com.mpush.core.router.RouterCenter; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; /** @@ -49,33 +68,36 @@ public void handle(BindUserMessage message) { if (context.handshakeOk()) { //2.先删除远程路由, 必须是同一个设备才允许解绑 boolean unRegisterSuccess = true; - RemoteRouterManager remoteRouterManager = RouterCenter.INSTANCE.getRemoteRouterManager(); - RemoteRouter remoteRouter = remoteRouterManager.lookup(message.userId); + int clientType = context.getClientType(); + String userId = message.userId; + RemoteRouterManager remoteRouterManager = RouterCenter.I.getRemoteRouterManager(); + RemoteRouter remoteRouter = remoteRouterManager.lookup(userId, clientType); if (remoteRouter != null) { String deviceId = remoteRouter.getRouteValue().getDeviceId(); if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = remoteRouterManager.unRegister(message.userId); + unRegisterSuccess = remoteRouterManager.unRegister(userId, clientType); } } //3.删除本地路由信息 - LocalRouterManager localRouterManager = RouterCenter.INSTANCE.getLocalRouterManager(); - LocalRouter localRouter = localRouterManager.lookup(message.userId); + LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter localRouter = localRouterManager.lookup(userId, clientType); if (localRouter != null) { String deviceId = localRouter.getRouteValue().getSessionContext().deviceId; if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = localRouterManager.unRegister(message.userId) && unRegisterSuccess; + unRegisterSuccess = localRouterManager.unRegister(userId, clientType) && unRegisterSuccess; } } //4.路由删除成功,广播用户下线事件 if (unRegisterSuccess) { - EventBus.INSTANCE.post(new UserOfflineEvent(message.getConnection(), message.userId)); + context.userId = null; + EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").send(); - Logs.Conn.info("unbind user success, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").send(); - Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", message.userId, context); + Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index 71f635e9..f670f428 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -1,17 +1,44 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; +import com.mpush.api.router.ClientType; + /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ public final class KickRemoteMsg { - public String userId; - public String deviceId; - public String targetServer; + public String userId; + public String deviceId; + public int clientType; + public String targetServer; - @Override - public String toString() { - return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + ", targetServer='" + targetServer + '\'' + '}'; - } + @Override + public String toString() { + return "KickRemoteMsg{" + + "userId='" + userId + '\'' + + ", deviceId='" + deviceId + '\'' + + ", clientType='" + clientType + '\'' + + ", targetServer='" + targetServer + '\'' + + '}'; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index c078ec3f..43130d9b 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -1,6 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.mpush.api.connection.Connection; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; /** @@ -10,9 +30,15 @@ */ public final class LocalRouter implements Router { private final Connection connection; + private final int clientType; public LocalRouter(Connection connection) { this.connection = connection; + this.clientType = connection.getSessionContext().getClientType(); + } + + public int getClientType() { + return clientType; } @Override @@ -25,6 +51,22 @@ public RouterType getRouteType() { return RouterType.LOCAL; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + LocalRouter that = (LocalRouter) o; + + return clientType == that.clientType; + + } + + @Override + public int hashCode() { + return Integer.hashCode(clientType); + } + @Override public String toString() { return "LocalRouter{" + connection + '}'; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 391bb377..57de36a4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -1,67 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.event.UserOfflineEvent; +import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; -import com.mpush.common.AbstractEventContainer; -import com.mpush.common.EventBus; -import com.mpush.common.router.RemoteRouter; - +import com.mpush.tools.event.EventBus; +import com.mpush.tools.event.EventConsumer; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; +import java.util.*; /** * Created by ohun on 2015/12/23. * * @author ohun@live.cn */ -public final class LocalRouterManager extends AbstractEventContainer implements RouterManager { +public final class LocalRouterManager extends EventConsumer implements RouterManager { public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); + private static final Map EMPTY = Collections.unmodifiableMap(new HashMap<>(0)); /** * 本地路由表 */ - private final Map routers = new ConcurrentHashMapV8<>(); - - /** - * 反向关系表 - */ - private final Map connIdUserIds = new ConcurrentHashMapV8<>(); + private final Map> routers = new ConcurrentHashMapV8<>(); @Override public LocalRouter register(String userId, LocalRouter router) { LOGGER.info("register local router success userId={}, router={}", userId, router); - connIdUserIds.put(router.getRouteValue().getId(), userId); - //add online userId - return routers.put(userId, router); + return routers.computeIfAbsent(userId, s -> new HashMap<>()).put(router.getClientType(), router); } @Override - public boolean unRegister(String userId) { - LocalRouter router = routers.remove(userId); - if (router != null) { - connIdUserIds.remove(router.getRouteValue().getId()); - } + public boolean unRegister(String userId, int clientType) { + LocalRouter router = routers.getOrDefault(userId, EMPTY).remove(clientType); LOGGER.info("unRegister local router success userId={}, router={}", userId, router); return true; } @Override - public LocalRouter lookup(String userId) { - LocalRouter router = routers.get(userId); - LOGGER.info("lookup local router userId={}, router={}", userId, router); - return router; + public Set lookupAll(String userId) { + return new HashSet<>(routers.getOrDefault(userId, EMPTY).values()); } - public String getUserIdByConnId(String connId) { - return connIdUserIds.get(connId); + @Override + public LocalRouter lookup(String userId, int clientType) { + LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); + LOGGER.info("lookup local router userId={}, router={}", userId, router); + return router; } /** @@ -70,24 +80,25 @@ public String getUserIdByConnId(String connId) { * @param event */ @Subscribe - void onConnectionCloseEvent(ConnectionCloseEvent event) { - Connection connection = event.connection; - if(connection == null) return; - String id = event.connection.getId(); + void on(ConnectionCloseEvent event) { + Connection connection = event.connection; + if (connection == null) return; + SessionContext context = connection.getSessionContext(); - //1.清除反向关系 - String userId = connIdUserIds.remove(id); + String userId = context.userId; if (userId == null) return; - EventBus.INSTANCE.post(new UserOfflineEvent(event.connection, userId)); - LocalRouter router = routers.get(userId); + + EventBus.I.post(new UserOfflineEvent(event.connection, userId)); + int clientType = context.getClientType(); + LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); if (router == null) return; + String connId = connection.getId(); //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 - if (id.equals(router.getRouteValue().getId())) { + if (connId.equals(router.getRouteValue().getId())) { //3.删除路由 - routers.remove(userId); + routers.getOrDefault(userId, EMPTY).remove(clientType); LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); - } else { //如果不相等,则log一下 LOGGER.info("clean disconnected local route, not clean:userId={}, route={}", userId, router); } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index b072bb28..0140d2a4 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -1,17 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.mpush.api.connection.Connection; import com.mpush.api.event.RouterChangeEvent; -import com.mpush.api.router.Router; import com.mpush.api.router.ClientLocation; -import com.mpush.common.EventBus; +import com.mpush.api.router.ClientType; +import com.mpush.api.router.Router; import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; -import com.mpush.tools.MPushUtil; - +import com.mpush.tools.Utils; +import com.mpush.tools.event.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Set; + /** * Created by ohun on 2015/12/23. * @@ -19,7 +40,7 @@ */ public final class RouterCenter { public static final Logger LOGGER = LoggerFactory.getLogger(RouterCenter.class); - public static final RouterCenter INSTANCE = new RouterCenter(); + public static final RouterCenter I = new RouterCenter(); private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); @@ -36,8 +57,8 @@ public final class RouterCenter { */ public boolean register(String userId, Connection connection) { ClientLocation location = ClientLocation - .from(connection.getSessionContext()) - .setHost(MPushUtil.getLocalIp()); + .from(connection) + .setHost(Utils.getLocalIp()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(location); @@ -47,36 +68,41 @@ public boolean register(String userId, Connection connection) { try { oldLocalRouter = localRouterManager.register(userId, localRouter); oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter); - } catch (Exception e) { LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e); } if (oldLocalRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldLocalRouter)); + EventBus.I.post(new RouterChangeEvent(userId, oldLocalRouter)); LOGGER.info("register router success, find old local router={}, userId={}", oldLocalRouter, userId); } if (oldRemoteRouter != null) { - EventBus.INSTANCE.post(new RouterChangeEvent(userId, oldRemoteRouter)); + EventBus.I.post(new RouterChangeEvent(userId, oldRemoteRouter)); LOGGER.info("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); } return true; } - public boolean unRegister(String userId) { - localRouterManager.unRegister(userId); - remoteRouterManager.unRegister(userId); + public boolean unRegister(String userId, int clientType) { + localRouterManager.unRegister(userId, clientType); + remoteRouterManager.unRegister(userId, clientType); return true; } - public Router lookup(String userId) { - LocalRouter local = localRouterManager.lookup(userId); + public Router lookup(String userId, int clientType) { + LocalRouter local = localRouterManager.lookup(userId, clientType); if (local != null) return local; - RemoteRouter remote = remoteRouterManager.lookup(userId); + RemoteRouter remote = remoteRouterManager.lookup(userId, clientType); return remote; } + public Set> lookupAll(String userId) { + Set locals = localRouterManager.lookupAll(userId); + if (locals != null) return locals; + Set remotes = remoteRouterManager.lookupAll(userId); + return remotes; + } public LocalRouterManager getLocalRouterManager() { return localRouterManager; diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index e218cbee..e0f5b61f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -1,36 +1,54 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; -import com.mpush.api.RedisKey; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; +import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; -import com.mpush.common.AbstractEventContainer; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.listener.ListenerDispatcher; +import com.mpush.cache.redis.listener.MessageListener; +import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.message.KickUserMessage; import com.mpush.common.router.RemoteRouter; -import com.mpush.log.Logs; - import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.redis.listener.ListenerDispatcher; -import com.mpush.tools.redis.listener.MessageListener; -import com.mpush.tools.redis.manage.RedisManage; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; +import com.mpush.tools.Utils; +import com.mpush.tools.config.ConfigManager; +import com.mpush.tools.event.EventConsumer; +import com.mpush.tools.log.Logs; /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ -public final class RouterChangeListener extends AbstractEventContainer implements MessageListener { +public final class RouterChangeListener extends EventConsumer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; - private final String kick_channel = KICK_CHANNEL_ + MPushUtil.getLocalIp(); + private final String kick_channel = KICK_CHANNEL_ + Utils.getLocalIp(); public RouterChangeListener() { - ListenerDispatcher.INSTANCE.subscribe(getKickChannel(), this); + ListenerDispatcher.I.subscribe(getKickChannel(), this); } public String getKickChannel() { @@ -42,7 +60,7 @@ public String getKickChannel(String remoteIp) { } @Subscribe - void onRouteChange(RouterChangeEvent event) { + void on(RouterChangeEvent event) { String userId = event.userId; Router r = event.router; if (r.getRouteType().equals(Router.RouterType.LOCAL)) { @@ -64,14 +82,11 @@ public void kickLocal(final String userId, final LocalRouter router) { KickUserMessage message = new KickUserMessage(connection); message.deviceId = context.deviceId; message.userId = userId; - message.send(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); - } else { - Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); - } + message.send(future -> { + if (future.isSuccess()) { + Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); + } else { + Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); } }); } @@ -88,7 +103,7 @@ public void operationComplete(ChannelFuture future) throws Exception { public void kickRemote(String userId, RemoteRouter router) { ClientLocation location = router.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 - if (location.getHost().equals(MPushUtil.getLocalIp())) { + if (location.getHost().equals(Utils.getLocalIp())) { Logs.Conn.info("kick remote user but router in local, userId={}", userId); return; } @@ -97,9 +112,10 @@ public void kickRemote(String userId, RemoteRouter router) { //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); + msg.clientType = location.getClientType(); msg.targetServer = location.getHost(); msg.userId = userId; - RedisManage.publish(getKickChannel(msg.targetServer), msg); + RedisManager.I.publish(getKickChannel(msg.targetServer), msg); } /** @@ -112,19 +128,20 @@ public void kickRemote(String userId, RemoteRouter router) { */ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 - if (!msg.targetServer.equals(MPushUtil.getLocalIp())) { - Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", MPushUtil.getLocalIp(), msg); + if (!msg.targetServer.equals(Utils.getLocalIp())) { + Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); return; } //2.查询本地路由,找到要被踢下线的链接,并删除该本地路由 String userId = msg.userId; - LocalRouterManager routerManager = RouterCenter.INSTANCE.getLocalRouterManager(); - LocalRouter router = routerManager.lookup(userId); + int clientType = msg.clientType; + LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter router = routerManager.lookup(userId, clientType); if (router != null) { Logs.Conn.info("receive kick remote msg, msg={}", msg); //2.1删除本地路由信息 - routerManager.unRegister(userId); + routerManager.unRegister(userId, clientType); //2.2发送踢人消息到客户端 kickLocal(userId, router); remStatUser(userId); @@ -148,6 +165,6 @@ public void onMessage(String channel, String message) { } private void remStatUser(String userId) { - RedisManage.zRem(RedisKey.getUserOnlineKey(MPushUtil.getExtranetIp()), userId); + RedisManager.I.zRem(RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()), userId); } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java index 1b514a5d..d0987f04 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java @@ -1,11 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.router; import com.google.common.eventbus.Subscribe; import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.event.UserOnlineEvent; -import com.mpush.common.EventBus; -import com.mpush.common.manage.user.UserManager; -import com.mpush.tools.redis.manage.RedisManage; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.user.UserManager; +import com.mpush.tools.event.EventBus; /** * Created by ohun on 2015/12/23. @@ -19,18 +38,18 @@ public final class UserOnlineOfflineListener { public static final String OFFLINE_CHANNEL = "/mpush/offline/"; public UserOnlineOfflineListener() { - EventBus.INSTANCE.register(this); + EventBus.I.register(this); } @Subscribe - void onUserOnline(UserOnlineEvent event) { - UserManager.INSTANCE.recordUserOnline(event.getUserId()); - RedisManage.publish(ONLINE_CHANNEL, event.getUserId()); + void on(UserOnlineEvent event) { + UserManager.I.recordUserOnline(event.getUserId()); + RedisManager.I.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe - void onUserOffline(UserOfflineEvent event) { - UserManager.INSTANCE.recordUserOffline(event.getUserId()); - RedisManage.publish(OFFLINE_CHANNEL, event.getUserId()); + void on(UserOfflineEvent event) { + UserManager.I.recordUserOffline(event.getUserId()); + RedisManager.I.publish(OFFLINE_CHANNEL, event.getUserId()); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index f4fbf926..589d7474 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -1,21 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; import com.mpush.core.handler.AdminHandler; import com.mpush.netty.server.NettyServer; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; -import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; +import java.util.concurrent.Executor; + public final class AdminServer extends NettyServer { + private final ConnectionServer connectionServer; + private final GatewayServer gatewayServer; - private final AdminHandler adminHandler = new AdminHandler(); + private final AdminHandler adminHandler; - public AdminServer(int port) { + public AdminServer(int port, ConnectionServer connectionServer, GatewayServer gatewayServer) { super(port); + this.connectionServer = connectionServer; + this.gatewayServer = gatewayServer; + this.adminHandler = new AdminHandler(this); } @Override @@ -24,6 +50,16 @@ protected void initPipeline(ChannelPipeline pipeline) { super.initPipeline(pipeline); } + @Override + protected Executor getBossExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + @Override + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + @Override public ChannelHandler getChannelHandler() { return adminHandler; @@ -38,4 +74,12 @@ protected ChannelHandler getDecoder() { protected ChannelHandler getEncoder() { return new StringEncoder(); } + + public ConnectionServer getConnectionServer() { + return connectionServer; + } + + public GatewayServer getGatewayServer() { + return gatewayServer; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index dd5a5398..b691e027 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -1,26 +1,54 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; -import com.mpush.core.handler.*; -import com.mpush.netty.client.HttpClient; -import com.mpush.netty.client.NettyHttpClient; -import com.mpush.netty.connection.NettyConnectionManager; -import com.mpush.netty.server.NettyServer; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.core.handler.*; +import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.NettyHttpClient; +import com.mpush.netty.server.NettyServer; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.connect_server.*; /** * Created by ohun on 2015/12/30. */ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; + private GlobalChannelTrafficShapingHandler trafficShapingHandler; - private ConnectionManager connectionManager = new NettyConnectionManager(); + private ConnectionManager connectionManager = new ServerConnectionManager(); private HttpClient httpClient; public ConnectionServer(int port) { @@ -38,20 +66,49 @@ public void init() { receiver.register(Command.UNBIND, new UnbindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - if (ConfigCenter.I.httpProxyEnable()) { + if (CC.mp.http.proxy_enabled) { httpClient = new NettyHttpClient(); receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); } channelHandler = new ServerChannelHandler(true, connectionManager, receiver); + + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override public void stop(Listener listener) { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } super.stop(listener); if (httpClient != null) httpClient.stop(); connectionManager.destroy(); } + @Override + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + @Override + protected Executor getBossExecutor() { + return ThreadPoolManager.I.getBossExecutor(); + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } + } + @Override protected void initOptions(ServerBootstrap b) { super.initOptions(b); @@ -76,4 +133,12 @@ protected void initOptions(ServerBootstrap b) { public ChannelHandler getChannelHandler() { return channelHandler; } + + public ConnectionManager getConnectionManager() { + return connectionManager; + } + + public HttpClient getHttpClient() { + return httpClient; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 6ec0ce7f..fb7976ee 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -1,11 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; -import com.mpush.netty.connection.NettyConnectionManager; import com.mpush.netty.server.NettyServer; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; + +import java.util.concurrent.Executors; + +import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_server.*; /** * Created by ohun on 2015/12/30. @@ -15,7 +40,8 @@ public final class GatewayServer extends NettyServer { private ServerChannelHandler channelHandler; - private NettyConnectionManager connectionManager; + private ServerConnectionManager connectionManager; + private GlobalChannelTrafficShapingHandler trafficShapingHandler; public GatewayServer(int port) { super(port); @@ -26,14 +52,34 @@ public void init() { super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); - connectionManager = new NettyConnectionManager(); + connectionManager = new ServerConnectionManager(); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); + if (enabled) { + trafficShapingHandler = new GlobalChannelTrafficShapingHandler( + Executors.newSingleThreadScheduledExecutor() + , write_global_limit, read_global_limit, + write_channel_limit, read_channel_limit, + check_interval); + } } @Override public void stop(Listener listener) { + if (trafficShapingHandler != null) { + trafficShapingHandler.release(); + } super.stop(listener); - connectionManager.destroy(); + if (connectionManager != null) { + connectionManager.destroy(); + } + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + super.initPipeline(pipeline); + if (trafficShapingHandler != null) { + pipeline.addLast(trafficShapingHandler); + } } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 73a8d6eb..9aa7c078 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -1,21 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.server; -import com.mpush.netty.connection.NettyConnection; +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.protocol.Packet; -import com.mpush.api.connection.Connection; -import com.mpush.api.PacketReceiver; -import com.mpush.common.EventBus; -import com.mpush.log.Logs; - -import com.mpush.tools.Profiler; - +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.common.Profiler; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,33 +60,32 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - try{ - Profiler.start("end channel read:"); - Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("channelRead channel={}, packet={}", ctx.channel(), msg); - connection.updateLastReadTime(); - receiver.onReceive((Packet) msg, connection); - }finally{ - Profiler.release(); - long duration = Profiler.getDuration(); - if(duration>80){ - LOGGER.error("end channel read:"+duration+","+Profiler.dump()); - } - Profiler.reset(); - } - + try { + Profiler.start("channel read:"); + Connection connection = connectionManager.get(ctx.channel()); + LOGGER.debug("channelRead channel={}, connection={}, packet={}", ctx.channel(), connection, msg); + connection.updateLastReadTime(); + receiver.onReceive((Packet) msg, connection); + } finally { + Profiler.release(); + long duration = Profiler.getDuration(); + if (duration > 80) { + LOGGER.error("channel read busy:" + duration + "," + Profiler.dump()); + } + Profiler.reset(); + } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - connectionManager.remove(ctx.channel()); - Logs.Conn.info("client exceptionCaught channel={}", ctx.channel()); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + Connection connection = connectionManager.removeAndClose(ctx.channel()); + Logs.Conn.error("client exceptionCaught channel={}, connection={}", ctx.channel(), connection); + LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client connect channel={}", ctx.channel()); + Logs.Conn.info("client connect channel={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -78,9 +93,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client disconnect channel={}", ctx.channel()); - Connection connection = connectionManager.get(ctx.channel()); - EventBus.INSTANCE.post(new ConnectionCloseEvent(connection)); - connectionManager.remove(ctx.channel()); + Connection connection = connectionManager.removeAndClose(ctx.channel()); + EventBus.I.post(new ConnectionCloseEvent(connection)); + Logs.Conn.info("client disconnect channel={}, connection={}", ctx.channel(), connection); } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java similarity index 55% rename from mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java rename to mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 43d14a39..f18d9f91 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -1,4 +1,23 @@ -package com.mpush.netty.connection; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.server; import com.google.common.collect.Lists; @@ -6,16 +25,16 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.HandshakeEvent; -import com.mpush.common.EventBus; -import com.mpush.log.Logs; - -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; +import com.mpush.tools.event.EventBus; +import com.mpush.tools.log.Logs; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; import io.netty.util.internal.chmv8.ConcurrentHashMapV8; + import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; @@ -26,8 +45,7 @@ * * @author ohun@live.cn */ -public final class NettyConnectionManager implements ConnectionManager { - //可能会有20w的链接数 +public final class ServerConnectionManager implements ConnectionManager { private final ConcurrentMap connections = new ConcurrentHashMapV8<>(); private Timer timer; @@ -36,9 +54,9 @@ public final class NettyConnectionManager implements ConnectionManager { public void init() { //每秒钟走一步,一个心跳周期内走一圈 long tickDuration = 1000;//1s - int ticksPerWheel = (int) (ConfigCenter.I.maxHeartbeat() / tickDuration); + int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); - EventBus.INSTANCE.register(this); + EventBus.I.register(this); } @Override @@ -52,20 +70,21 @@ public void destroy() { @Override public Connection get(final Channel channel) { - return connections.get(channel.id().asLongText()); + return connections.get(channel.id().asShortText()); } @Override public void add(Connection connection) { - connections.putIfAbsent(connection.getId(), connection); + connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); } @Override - public void remove(Channel channel) { - Connection connection = connections.remove(channel.id().asLongText()); + public Connection removeAndClose(Channel channel) { + Connection connection = connections.remove(channel.id().asShortText()); if (connection != null) { connection.close(); } + return connection; } @Override @@ -74,14 +93,14 @@ public List getConnections() { } @Subscribe - void onHandshakeOk(HandshakeEvent event) { + void on(HandshakeEvent event) { HeartbeatCheckTask task = new HeartbeatCheckTask(event.connection); task.startTimeout(); } private class HeartbeatCheckTask implements TimerTask { - private int expiredTimes = 0; + private int timeoutTimes = 0; private final Connection connection; public HeartbeatCheckTask(Connection connection) { @@ -90,28 +109,27 @@ public HeartbeatCheckTask(Connection connection) { public void startTimeout() { int timeout = connection.getSessionContext().heartbeat; - timer.newTimeout(this, timeout > 0 ? timeout : ConfigCenter.I.minHeartbeat(), TimeUnit.MILLISECONDS); + timer.newTimeout(this, timeout > 0 ? timeout : CC.mp.core.min_heartbeat, TimeUnit.MILLISECONDS); } @Override public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) { - Logs.HB.info("connection is not connected:{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("connection was disconnected, heartbeat timeout times={}, connection={}", timeoutTimes, connection); return; } if (connection.heartbeatTimeout()) { - if (++expiredTimes > ConfigCenter.I.maxHBTimeoutTimes()) { + if (++timeoutTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); - Logs.HB.info("connection heartbeat timeout, connection has bean closed:{},{}", connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("client heartbeat timeout times={}, do close connection={}", timeoutTimes, connection); return; } else { - Logs.HB.info("connection heartbeat timeout, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + Logs.HB.info("client heartbeat timeout times={}, connection={}", timeoutTimes, connection); } } else { - expiredTimes = 0; - Logs.HB.info("connection heartbeat reset, expiredTimes:{},{},{}", expiredTimes, connection.getChannel(), connection.getSessionContext().deviceId); + timeoutTimes = 0; + //Logs.HB.info("client heartbeat timeout times reset 0, connection={}", connection); } - startTimeout(); } } diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java index 7a8e2f72..4336b491 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSession.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.session; import com.mpush.api.connection.SessionContext; diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index 1fbf2c78..5b9394bc 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -1,11 +1,30 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.session; -import com.mpush.api.RedisKey; import com.mpush.api.connection.SessionContext; -import com.mpush.tools.Strings; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.tools.common.Strings; +import com.mpush.tools.config.CC; import com.mpush.tools.crypto.MD5Utils; -import com.mpush.tools.redis.manage.RedisManage; /** * Created by ohun on 2015/12/25. @@ -14,17 +33,17 @@ */ public final class ReusableSessionManager { public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); - private int expiredTime = ConfigCenter.I.sessionExpiredTime(); + private int expiredTime = CC.mp.core.session_expired_time; public boolean cacheSession(ReusableSession session) { - String key = RedisKey.getSessionKey(session.sessionId); - RedisManage.set(key, ReusableSession.encode(session.context), expiredTime); + String key = RedisKey.getSessionKey(session.sessionId); + RedisManager.I.set(key, ReusableSession.encode(session.context), expiredTime); return true; } public ReusableSession querySession(String sessionId) { - String key = RedisKey.getSessionKey(sessionId); - String value = RedisManage.get(key, String.class); + String key = RedisKey.getSessionKey(sessionId); + String value = RedisManager.I.get(key, String.class); if (Strings.isBlank(value)) return null; return ReusableSession.decode(value); } diff --git a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java b/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java deleted file mode 100644 index f51ae52f..00000000 --- a/mpush-core/src/test/java/com/mpush/core/netty/NettyServerTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.mpush.core.netty; - -import com.mpush.api.Server; -import com.mpush.core.server.ConnectionServer; -import org.junit.Test; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public class NettyServerTest { - @Test - public void testStart() throws Exception { - ConnectionServer server = new ConnectionServer(3000); - server.init(); - server.start(new Server.Listener() { - @Override - public void onSuccess(int port) { - } - - @Override - public void onFailure(Throwable cause) { - } - }); - } -} \ No newline at end of file diff --git a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java index 7810ae22..818c9719 100644 --- a/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java +++ b/mpush-core/src/test/java/com/mpush/core/security/CipherBoxTest.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.core.security; import com.mpush.common.security.CipherBox; @@ -17,9 +36,9 @@ public void testGetPrivateKey() throws Exception { @Test public void testGetPublicKey() throws Exception { for (int i = 0; i < 1000; i++) { - byte[] clientKey = CipherBox.INSTANCE.randomAESKey(); - byte[] serverKey = CipherBox.INSTANCE.randomAESKey(); - byte[] sessionKey = CipherBox.INSTANCE.mixKey(clientKey, serverKey); + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] serverKey = CipherBox.I.randomAESKey(); + byte[] sessionKey = CipherBox.I.mixKey(clientKey, serverKey); //System.out.println("clientKey:" + Arrays.toString(clientKey)); //System.out.println("serverKey:" + Arrays.toString(serverKey)); System.out.println("sessionKey:" + Arrays.toString(sessionKey)); diff --git a/mpush-core/src/test/resources/logback.xml b/mpush-core/src/test/resources/logback.xml index 20979b40..1ff25c6d 100644 --- a/mpush-core/src/test/resources/logback.xml +++ b/mpush-core/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - + + System.out + UTF-8 + DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-log/.gitignore b/mpush-log/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-log/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-log/pom.xml b/mpush-log/pom.xml deleted file mode 100644 index f8f5595e..00000000 --- a/mpush-log/pom.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - mpush - com.mpush - 1.0 - - 4.0.0 - ${mpush.groupId} - mpush-log - ${mpush-log-version} - jar - mpush-log - - - UTF-8 - - - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - commons-logging - commons-logging - - - log4j - log4j - - - org.logback-extensions - logback-ext-spring - - - diff --git a/mpush-log/src/main/java/com/mpush/log/Logs.java b/mpush-log/src/main/java/com/mpush/log/Logs.java deleted file mode 100644 index d9504ab2..00000000 --- a/mpush-log/src/main/java/com/mpush/log/Logs.java +++ /dev/null @@ -1,331 +0,0 @@ -package com.mpush.log; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.Marker; - -/** - * Created by ohun on 2016/5/16. - * - * @author ohun@live.cn - */ -public enum Logs implements Logger { - Conn("connectionLog"), - PUSH("pushLog"), - HB("heartBeatLog"), - REDIS("redisLog"), - ZK("zkLog"), - HTTP("httpLog"); - - private final Logger logger; - - Logs(String loggerName) { - this.logger = LoggerFactory.getLogger(loggerName); - } - - @Override - public String getName() { - return logger.getName(); - } - - @Override - public boolean isTraceEnabled() { - return logger.isTraceEnabled(); - } - - @Override - public void trace(String s) { - logger.trace(s); - } - - @Override - public void trace(String s, Object o) { - logger.trace(s, o); - } - - @Override - public void trace(String s, Object o, Object o1) { - logger.trace(s, o, o1); - } - - @Override - public void trace(String s, Object... objects) { - logger.trace(s, objects); - } - - @Override - public void trace(String s, Throwable throwable) { - logger.trace(s, throwable); - } - - @Override - public boolean isTraceEnabled(Marker marker) { - return isTraceEnabled(marker); - } - - @Override - public void trace(Marker marker, String s) { - logger.trace(marker, s); - } - - @Override - public void trace(Marker marker, String s, Object o) { - logger.trace(marker, s, o); - } - - @Override - public void trace(Marker marker, String s, Object o, Object o1) { - logger.trace(marker, s, o, o1); - } - - @Override - public void trace(Marker marker, String s, Object... objects) { - logger.trace(marker, s, objects); - } - - @Override - public void trace(Marker marker, String s, Throwable throwable) { - logger.trace(marker, s, throwable); - } - - @Override - public boolean isDebugEnabled() { - return isErrorEnabled(); - } - - @Override - public void debug(String s) { - logger.debug(s); - } - - @Override - public void debug(String s, Object o) { - logger.debug(s, o); - } - - @Override - public void debug(String s, Object o, Object o1) { - logger.debug(s, o, o1); - } - - @Override - public void debug(String s, Object... objects) { - logger.debug(s, objects); - } - - @Override - public void debug(String s, Throwable throwable) { - logger.debug(s, throwable); - } - - @Override - public boolean isDebugEnabled(Marker marker) { - return logger.isDebugEnabled(marker); - } - - @Override - public void debug(Marker marker, String s) { - logger.debug(marker, s); - } - - @Override - public void debug(Marker marker, String s, Object o) { - logger.debug(marker, s, o); - } - - @Override - public void debug(Marker marker, String s, Object o, Object o1) { - logger.debug(marker, s, o, o1); - } - - @Override - public void debug(Marker marker, String s, Object... objects) { - logger.debug(marker, s, objects); - } - - @Override - public void debug(Marker marker, String s, Throwable throwable) { - logger.debug(marker, s, throwable); - } - - @Override - public boolean isInfoEnabled() { - return logger.isInfoEnabled(); - } - - @Override - public void info(String s) { - logger.info(s); - } - - @Override - public void info(String s, Object o) { - logger.info(s, o); - } - - @Override - public void info(String s, Object o, Object o1) { - logger.info(s, o, o1); - } - - @Override - public void info(String s, Object... objects) { - logger.info(s, objects); - } - - @Override - public void info(String s, Throwable throwable) { - logger.info(s, throwable); - } - - @Override - public boolean isInfoEnabled(Marker marker) { - return logger.isInfoEnabled(marker); - } - - @Override - public void info(Marker marker, String s) { - logger.info(marker, s); - } - - @Override - public void info(Marker marker, String s, Object o) { - logger.info(marker, s, o); - } - - @Override - public void info(Marker marker, String s, Object o, Object o1) { - logger.info(marker, s, o, o1); - } - - @Override - public void info(Marker marker, String s, Object... objects) { - logger.info(marker, s, objects); - } - - @Override - public void info(Marker marker, String s, Throwable throwable) { - logger.info(marker, s, throwable); - } - - @Override - public boolean isWarnEnabled() { - return logger.isWarnEnabled(); - } - - @Override - public void warn(String s) { - logger.warn(s); - } - - @Override - public void warn(String s, Object o) { - logger.warn(s, o); - } - - @Override - public void warn(String s, Object... objects) { - logger.warn(s, objects); - } - - @Override - public void warn(String s, Object o, Object o1) { - logger.warn(s, o, o1); - } - - @Override - public void warn(String s, Throwable throwable) { - logger.warn(s, throwable); - } - - @Override - public boolean isWarnEnabled(Marker marker) { - return isWarnEnabled(marker); - } - - @Override - public void warn(Marker marker, String s) { - logger.warn(marker, s); - } - - @Override - public void warn(Marker marker, String s, Object o) { - logger.warn(marker, s, o); - } - - @Override - public void warn(Marker marker, String s, Object o, Object o1) { - logger.warn(marker, s, o, o1); - } - - @Override - public void warn(Marker marker, String s, Object... objects) { - logger.warn(marker, s, objects); - } - - @Override - public void warn(Marker marker, String s, Throwable throwable) { - logger.warn(marker, s, throwable); - } - - @Override - public boolean isErrorEnabled() { - return isErrorEnabled(); - } - - @Override - public void error(String s) { - logger.error(s); - } - - @Override - public void error(String s, Object o) { - logger.error(s, o); - } - - @Override - public void error(String s, Object o, Object o1) { - logger.error(s, o, o1); - } - - @Override - public void error(String s, Object... objects) { - logger.error(s, objects); - } - - @Override - public void error(String s, Throwable throwable) { - logger.error(s, throwable); - } - - @Override - public boolean isErrorEnabled(Marker marker) { - return isErrorEnabled(marker); - } - - @Override - public void error(Marker marker, String s) { - logger.error(marker, s); - } - - @Override - public void error(Marker marker, String s, Object o) { - logger.error(marker, s, o); - } - - @Override - public void error(Marker marker, String s, Object o, Object o1) { - logger.error(marker, s, o, o1); - } - - @Override - public void error(Marker marker, String s, Object... objects) { - logger.error(marker, s, objects); - } - - @Override - public void error(Marker marker, String s, Throwable throwable) { - logger.error(marker, s, throwable); - } - -} diff --git a/mpush-log/src/test/java/com/mpush/AppTest.java b/mpush-log/src/test/java/com/mpush/AppTest.java deleted file mode 100644 index 796b8466..00000000 --- a/mpush-log/src/test/java/com/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-monitor/.gitignore b/mpush-monitor/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-monitor/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 6da74f1c..886b22fc 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -1,10 +1,10 @@ - - mpush - com.mpush - 1.0 - + + mpush + com.mpush + 1.0 + 4.0.0 ${mpush.groupId} @@ -14,9 +14,13 @@ mpush-monitor - - ${mpush.groupId} - mpush-tools - + + ${mpush.groupId} + mpush-api + + + ${mpush.groupId} + mpush-tools + diff --git a/mpush-monitor/src/main/java/com/mpush/App.java b/mpush-monitor/src/main/java/com/mpush/App.java deleted file mode 100644 index 0a7102ea..00000000 --- a/mpush-monitor/src/main/java/com/mpush/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mpush; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java new file mode 100644 index 00000000..e064273a --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/MonitorResult.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.data; + +import com.mpush.tools.Jsons; + +import java.util.HashMap; +import java.util.Map; + +public class MonitorResult { + private Long timestamp = System.currentTimeMillis(); + private Map results = new HashMap<>(8); + + public MonitorResult addResult(String name, Object result) { + results.put(name, result); + return this; + } + + public Map getResults() { + return results; + } + + public MonitorResult setResults(Map results) { + this.results = results; + return this; + } + + public Long getTimestamp() { + return timestamp; + } + + public MonitorResult setTimestamp(Long timestamp) { + this.timestamp = timestamp; + return this; + } + + @Override + public String toString() { + return "MonitorResult{" + + "results=" + results + + ", timestamp=" + timestamp + + '}'; + } + + public String toJson() { + return Jsons.toJson(this); + } +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java new file mode 100644 index 00000000..f6d5f244 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/data/ResultCollector.java @@ -0,0 +1,41 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.data; + +import com.mpush.monitor.quota.impl.*; + +/** + * Created by yxx on 2016/5/19. + * + * @author ohun@live.cn + */ +public class ResultCollector { + + public MonitorResult collect() { + MonitorResult result = new MonitorResult(); + result.addResult("jvm-info", JVMInfo.I.monitor()); + result.addResult("jvm-gc", JVMGC.I.monitor()); + result.addResult("jvm-memory", JVMMemory.I.monitor()); + result.addResult("jvm-thread", JVMThread.I.monitor()); + result.addResult("jvm-thread-pool", JVMThreadPool.I.monitor()); + return result; + } + +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java b/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java deleted file mode 100644 index 9ae26d87..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/domain/MonitorData.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.mpush.monitor.domain; - -import java.util.Map; - -public class MonitorData { - - private Long timestamp; - - private Map memoryMap; - - private Map gcMap; - - private Map threadMap; - - private Map threadPoolMap; - - private Map infoMap; - - public MonitorData() { - this.timestamp = System.currentTimeMillis(); - } - - public Map getMemoryMap() { - return memoryMap; - } - - public void setMemoryMap(Map memoryMap) { - this.memoryMap = memoryMap; - } - - public Map getGcMap() { - return gcMap; - } - - public void setGcMap(Map gcMap) { - this.gcMap = gcMap; - } - - public Map getThreadMap() { - return threadMap; - } - - public void setThreadMap(Map threadMap) { - this.threadMap = threadMap; - } - - public Long getTimestamp() { - return timestamp; - } - - public Map getInfoMap() { - return infoMap; - } - - public void setInfoMap(Map infoMap) { - this.infoMap = infoMap; - } - - public Map getThreadPoolMap() { - return threadPoolMap; - } - - public void setThreadPoolMap(Map threadPoolMap) { - this.threadPoolMap = threadPoolMap; - } - -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java deleted file mode 100644 index 2c7ad2e7..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/BaseQuota.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.monitor.quota; - -import java.util.List; -import java.util.Map; - -import com.google.common.collect.Lists; - -public abstract class BaseQuota { - - protected final static List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", - "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", - "Garbage collection optimized for deterministic pausetimes Old Collector"); - - protected final static List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", - "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); - - protected final static List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); - - protected final static List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); - - protected final static List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); - - protected final static List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); - - public static boolean contain(String name, List list) { - for (String str : list) { - if (name.equals(str)) { - return true; - } - } - return false; - } - - public abstract Map toMap(); -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java index 5dca627d..91d4cb24 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/GCMQuota.java @@ -1,21 +1,40 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface GCMQuota { - - public long yongGcCollectionCount(); - - public long yongGcCollectionTime(); - - public long fullGcCollectionCount(); - - public long fullGcCollectionTime(); - - public long spanYongGcCollectionCount(); - - public long spanYongGcCollectionTime(); - - public long spanFullGcCollectionCount(); - - public long spanFullGcCollectionTime(); - +public interface GCMQuota extends MonitorQuota { + + long yongGcCollectionCount(); + + long yongGcCollectionTime(); + + long fullGcCollectionCount(); + + long fullGcCollectionTime(); + + long spanYongGcCollectionCount(); + + long spanYongGcCollectionTime(); + + long spanFullGcCollectionCount(); + + long spanFullGcCollectionTime(); + } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java index 23a4b810..cec75350 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/InfoQuota.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface InfoQuota { +public interface InfoQuota extends MonitorQuota { + + String pid(); - public String pid(); - - public double load(); + double load(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java index 3a368af2..83a01a82 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MemoryQuota.java @@ -1,59 +1,78 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface MemoryQuota { +public interface MemoryQuota extends MonitorQuota { - // Heap - long heapMemoryCommitted(); + // Heap + long heapMemoryCommitted(); - long heapMemoryInit(); + long heapMemoryInit(); - long heapMemoryMax(); + long heapMemoryMax(); - long heapMemoryUsed(); + long heapMemoryUsed(); - // NonHeap - long nonHeapMemoryCommitted(); + // NonHeap + long nonHeapMemoryCommitted(); - long nonHeapMemoryInit(); + long nonHeapMemoryInit(); - long nonHeapMemoryMax(); + long nonHeapMemoryMax(); - long nonHeapMemoryUsed(); + long nonHeapMemoryUsed(); - // PermGen - long permGenCommitted(); + // PermGen + long permGenCommitted(); - long permGenInit(); + long permGenInit(); - long permGenMax(); + long permGenMax(); - long permGenUsed(); + long permGenUsed(); - // OldGen - long oldGenCommitted(); + // OldGen + long oldGenCommitted(); - long oldGenInit(); + long oldGenInit(); - long oldGenMax(); + long oldGenMax(); - long oldGenUsed(); + long oldGenUsed(); - // EdenSpace - long edenSpaceCommitted(); + // EdenSpace + long edenSpaceCommitted(); - long edenSpaceInit(); + long edenSpaceInit(); - long edenSpaceMax(); + long edenSpaceMax(); - long edenSpaceUsed(); + long edenSpaceUsed(); - // Survivor - long survivorCommitted(); + // Survivor + long survivorCommitted(); - long survivorInit(); + long survivorInit(); - long survivorMax(); + long survivorMax(); - long survivorUsed(); + long survivorUsed(); } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java new file mode 100644 index 00000000..dc3c61ab --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/MonitorQuota.java @@ -0,0 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.quota; + +public interface MonitorQuota { + + Object monitor(Object... args); +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java index 2396a0e6..1cba7c14 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadPoolQuota.java @@ -1,5 +1,24 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface ThreadPoolQuota { +public interface ThreadPoolQuota extends MonitorQuota { } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java index 8d000cdf..623572ec 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/ThreadQuota.java @@ -1,14 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota; -public interface ThreadQuota { +public interface ThreadQuota extends MonitorQuota { + + int daemonThreadCount(); + + int threadCount(); + + long totalStartedThreadCount(); + + int deadLockedThreadCount(); - public int daemonThreadCount(); - - public int threadCount(); - - public long totalStartedThreadCount(); - - public int deadLockedThreadCount(); - } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java index 05c1f177..2b9d0903 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMGC.java @@ -1,136 +1,160 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.GCMQuota; + import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; +import java.util.List; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.GCMQuota; - -public class JVMGC extends BaseQuota implements GCMQuota { - - public static final JVMGC instance = new JVMGC(); - - private GarbageCollectorMXBean fullGc; - private GarbageCollectorMXBean yongGc; - - private long lastYoungGcCollectionCount = -1; - private long lastYoungGcCollectionTime = -1; - private long lastFullGcCollectionCount = -1; - private long lastFullGcCollectionTime = -1; - - - private JVMGC() { - for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { - String name = item.getName(); - if (contain(name, youngGcName)) { - yongGc = item; - } else if (contain(name,fullGcName)) { - fullGc = item; - } - } - - } - - @Override - public long yongGcCollectionCount() { - if (yongGc == null) { - return 0; - } - return yongGc.getCollectionCount(); - } - - @Override - public long yongGcCollectionTime() { - if (yongGc == null) { - return 0; - } - return yongGc.getCollectionTime(); - } - - @Override - public long fullGcCollectionCount() { - if (fullGc == null) { - return 0; - } - return fullGc.getCollectionCount(); - } - - @Override - public long fullGcCollectionTime() { - if (fullGc == null) { - return 0; - } - return fullGc.getCollectionTime(); - } - - @Override - public long spanYongGcCollectionCount() { - - long current = yongGcCollectionCount(); - if (lastYoungGcCollectionCount == -1) { - lastYoungGcCollectionCount = current; - return 0; - } else { - long result = current - lastYoungGcCollectionCount; - lastYoungGcCollectionCount = current; - return result; - } - - } - - @Override - public long spanYongGcCollectionTime() { - long current = yongGcCollectionTime(); - if (lastYoungGcCollectionTime == -1) { - lastYoungGcCollectionTime = current; - return 0; - } else { - long result = current - lastYoungGcCollectionTime; - lastYoungGcCollectionTime = current; - return result; - } - } - - @Override - public long spanFullGcCollectionCount() { - long current = fullGcCollectionCount(); - if (lastFullGcCollectionCount == -1) { - lastFullGcCollectionCount = current; - return 0; - } else { - long result = current - lastFullGcCollectionCount; - lastFullGcCollectionCount = current; - return result; - } - } - - @Override - public long spanFullGcCollectionTime() { - long current = fullGcCollectionTime(); - if (lastFullGcCollectionTime == -1) { - lastFullGcCollectionTime = current; - return 0; - } else { - long result = current - lastFullGcCollectionTime; - lastFullGcCollectionTime = current; - return result; - } - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("yongGcCollectionCount", yongGcCollectionCount()); - map.put("yongGcCollectionTime", yongGcCollectionTime()); - map.put("fullGcCollectionCount", fullGcCollectionCount()); - map.put("fullGcCollectionTime", fullGcCollectionTime()); - map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); - map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); - map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); - map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); - return map; - } - +public class JVMGC implements GCMQuota { + + private final List fullGcName = Lists.newArrayList("ConcurrentMarkSweep", "MarkSweepCompact", "PS MarkSweep", "G1 Old Generation", + "Garbage collection optimized for short pausetimes Old Collector", "Garbage collection optimized for throughput Old Collector", + "Garbage collection optimized for deterministic pausetimes Old Collector"); + + private final List youngGcName = Lists.newArrayList("ParNew", "Copy", "PS Scavenge", "G1 Young Generation", "Garbage collection optimized for short pausetimes Young Collector", + "Garbage collection optimized for throughput Young Collector", "Garbage collection optimized for deterministic pausetimes Young Collector"); + + public static final JVMGC I = new JVMGC(); + + private GarbageCollectorMXBean fullGc; + private GarbageCollectorMXBean yongGc; + + private long lastYoungGcCollectionCount = -1; + private long lastYoungGcCollectionTime = -1; + private long lastFullGcCollectionCount = -1; + private long lastFullGcCollectionTime = -1; + + + private JVMGC() { + for (GarbageCollectorMXBean item : ManagementFactory.getGarbageCollectorMXBeans()) { + String name = item.getName(); + if (youngGcName.contains(name)) { + yongGc = item; + } else if (fullGcName.contains(name)) { + fullGc = item; + } + } + } + + @Override + public long yongGcCollectionCount() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionCount(); + } + + @Override + public long yongGcCollectionTime() { + if (yongGc == null) { + return 0; + } + return yongGc.getCollectionTime(); + } + + @Override + public long fullGcCollectionCount() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionCount(); + } + + @Override + public long fullGcCollectionTime() { + if (fullGc == null) { + return 0; + } + return fullGc.getCollectionTime(); + } + + @Override + public long spanYongGcCollectionCount() { + + long current = yongGcCollectionCount(); + if (lastYoungGcCollectionCount == -1) { + lastYoungGcCollectionCount = current; + return 0; + } else { + long result = current - lastYoungGcCollectionCount; + lastYoungGcCollectionCount = current; + return result; + } + } + + @Override + public long spanYongGcCollectionTime() { + long current = yongGcCollectionTime(); + if (lastYoungGcCollectionTime == -1) { + lastYoungGcCollectionTime = current; + return 0; + } else { + long result = current - lastYoungGcCollectionTime; + lastYoungGcCollectionTime = current; + return result; + } + } + + @Override + public long spanFullGcCollectionCount() { + long current = fullGcCollectionCount(); + if (lastFullGcCollectionCount == -1) { + lastFullGcCollectionCount = current; + return 0; + } else { + long result = current - lastFullGcCollectionCount; + lastFullGcCollectionCount = current; + return result; + } + } + + @Override + public long spanFullGcCollectionTime() { + long current = fullGcCollectionTime(); + if (lastFullGcCollectionTime == -1) { + lastFullGcCollectionTime = current; + return 0; + } else { + long result = current - lastFullGcCollectionTime; + lastFullGcCollectionTime = current; + return result; + } + } + + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("yongGcCollectionCount", yongGcCollectionCount()); + map.put("yongGcCollectionTime", yongGcCollectionTime()); + map.put("fullGcCollectionCount", fullGcCollectionCount()); + map.put("fullGcCollectionTime", fullGcCollectionTime()); + map.put("spanYongGcCollectionCount", spanYongGcCollectionCount()); + map.put("spanYongGcCollectionTime", spanYongGcCollectionTime()); + map.put("spanFullGcCollectionCount", spanFullGcCollectionCount()); + map.put("spanFullGcCollectionTime", spanFullGcCollectionTime()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java index 42e2bd04..eb0276a4 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMInfo.java @@ -1,49 +1,70 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.InfoQuota; +import com.mpush.monitor.quota.MonitorQuota; + import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.InfoQuota; -import com.mpush.monitor.quota.BaseQuota; - -public class JVMInfo extends BaseQuota implements InfoQuota { - - public static final JVMInfo instance = new JVMInfo(); - - private RuntimeMXBean runtimeMXBean; - - private OperatingSystemMXBean systemMXBean; - - private String currentPid; - - private JVMInfo() { - runtimeMXBean = ManagementFactory.getRuntimeMXBean(); - systemMXBean = ManagementFactory.getOperatingSystemMXBean(); - } - - @Override - public String pid() { - if (null == currentPid) { - currentPid = runtimeMXBean.getName().split("@")[0]; +public class JVMInfo implements InfoQuota { + + public static final JVMInfo I = new JVMInfo(); + + private RuntimeMXBean runtimeMXBean; + + private OperatingSystemMXBean systemMXBean; + + private String currentPid; + + private JVMInfo() { + runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + systemMXBean = ManagementFactory.getOperatingSystemMXBean(); + } + + @Override + public String pid() { + if (null == currentPid) { + currentPid = runtimeMXBean.getName().split("@")[0]; } return currentPid; - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("pid", pid()); - map.put("load", load()); - return map; - } - - @Override - public double load() { - double averageLoad = systemMXBean.getSystemLoadAverage(); - return averageLoad; - } + } + + @Override + public double load() { + double averageLoad = systemMXBean.getSystemLoadAverage(); + return averageLoad; + } + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("pid", pid()); + map.put("load", load()); + map.put("totalMemory", Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m"); + map.put("freeMemory", Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m"); + map.put("maxMemory", Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m"); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java index 2ccb5f19..441822a4 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMMemory.java @@ -1,239 +1,265 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.MemoryQuota; + import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryPoolMXBean; import java.util.List; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.MemoryQuota; - -public class JVMMemory extends BaseQuota implements MemoryQuota { - - public static final JVMMemory instance = new JVMMemory(); - - private MemoryMXBean memoryMXBean; - - private MemoryPoolMXBean permGenMxBean; - private MemoryPoolMXBean oldGenMxBean; - private MemoryPoolMXBean edenSpaceMxBean; - private MemoryPoolMXBean pSSurvivorSpaceMxBean; - - private JVMMemory() { - memoryMXBean = ManagementFactory.getMemoryMXBean(); - List list = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean item : list) { - String name = item.getName(); - if (contain(name, permGenName)) { - permGenMxBean = item; - } else if (contain(name, oldGenName)) { - oldGenMxBean = item; - } else if (contain(name, edenSpaceName)) { - edenSpaceMxBean = item; - } else if (contain(name, psSurvivorName)) { - pSSurvivorSpaceMxBean = item; - } - } - } - - @Override - public long heapMemoryCommitted() { - return memoryMXBean.getHeapMemoryUsage().getCommitted(); - } - - @Override - public long heapMemoryInit() { - return memoryMXBean.getHeapMemoryUsage().getInit(); - } - - @Override - public long heapMemoryMax() { - return memoryMXBean.getHeapMemoryUsage().getMax(); - } - - @Override - public long heapMemoryUsed() { - return memoryMXBean.getHeapMemoryUsage().getUsed(); - } - - @Override - public long nonHeapMemoryCommitted() { - return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); - } - - @Override - public long nonHeapMemoryInit() { - return memoryMXBean.getNonHeapMemoryUsage().getInit(); - } - - @Override - public long nonHeapMemoryMax() { - return memoryMXBean.getNonHeapMemoryUsage().getMax(); - } - - @Override - public long nonHeapMemoryUsed() { - return memoryMXBean.getNonHeapMemoryUsage().getUsed(); - } - - @Override - public long permGenCommitted() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getCommitted(); - } - - @Override - public long permGenInit() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getInit(); - } - - @Override - public long permGenMax() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getMax(); - } - - @Override - public long permGenUsed() { - if (permGenMxBean == null) { - return 0; - } - return permGenMxBean.getUsage().getUsed(); - } - - @Override - public long oldGenCommitted() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getCommitted(); - } - - @Override - public long oldGenInit() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getInit(); - } - - @Override - public long oldGenMax() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getMax(); - } - - @Override - public long oldGenUsed() { - if (oldGenMxBean == null) { - return 0; - } - return oldGenMxBean.getUsage().getUsed(); - } - - @Override - public long edenSpaceCommitted() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getCommitted(); - } - - @Override - public long edenSpaceInit() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getInit(); - } - - @Override - public long edenSpaceMax() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getMax(); - } - - @Override - public long edenSpaceUsed() { - if (null == edenSpaceMxBean) { - return 0; - } - return edenSpaceMxBean.getUsage().getUsed(); - } - - @Override - public long survivorCommitted() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getCommitted(); - } - - @Override - public long survivorInit() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getInit(); - } - - @Override - public long survivorMax() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getMax(); - } - - @Override - public long survivorUsed() { - if (null == pSSurvivorSpaceMxBean) { - return 0; - } - return pSSurvivorSpaceMxBean.getUsage().getUsed(); - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("heapMemoryCommitted", heapMemoryCommitted()); - map.put("heapMemoryInit", heapMemoryInit()); - map.put("heapMemoryMax", heapMemoryMax()); - map.put("heapMemoryUsed", heapMemoryUsed()); - map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); - map.put("nonHeapMemoryInit", nonHeapMemoryInit()); - map.put("nonHeapMemoryMax", nonHeapMemoryMax()); - map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); - map.put("permGenCommitted", permGenCommitted()); - map.put("permGenInit", permGenInit()); - map.put("permGenMax", permGenMax()); - map.put("permGenUsed", permGenUsed()); - map.put("oldGenCommitted", oldGenCommitted()); - map.put("oldGenInit", oldGenInit()); - map.put("oldGenMax", oldGenMax()); - map.put("oldGenUsed", oldGenUsed()); - map.put("edenSpaceCommitted", edenSpaceCommitted()); - map.put("edenSpaceInit", edenSpaceInit()); - map.put("edenSpaceMax", edenSpaceMax()); - map.put("edenSpaceUsed", edenSpaceUsed()); - map.put("survivorCommitted", survivorCommitted()); - map.put("survivorInit", survivorInit()); - map.put("survivorMax", survivorMax()); - map.put("survivorUsed", survivorUsed()); - return map; - } - +public class JVMMemory implements MemoryQuota { + + private final List permGenName = Lists.newArrayList("CMS Perm Gen", "Perm Gen", "PS Perm Gen", "G1 Perm Gen"); + + private final List oldGenName = Lists.newArrayList("CMS Old Gen", "Tenured Gen", "PS Old Gen", "G1 Old Gen"); + + private final List edenSpaceName = Lists.newArrayList("Par Eden Space", "Eden Space", "PS Eden Space", "G1 Eden"); + + private final List psSurvivorName = Lists.newArrayList("Par Survivor Space", "Survivor Space", "PS Survivor Space", "G1 Survivor"); + + public static final JVMMemory I = new JVMMemory(); + + private MemoryMXBean memoryMXBean; + + private MemoryPoolMXBean permGenMxBean; + private MemoryPoolMXBean oldGenMxBean; + private MemoryPoolMXBean edenSpaceMxBean; + private MemoryPoolMXBean pSSurvivorSpaceMxBean; + + private JVMMemory() { + memoryMXBean = ManagementFactory.getMemoryMXBean(); + List list = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean item : list) { + String name = item.getName(); + if (permGenName.contains(name)) { + permGenMxBean = item; + } else if (oldGenName.contains(name)) { + oldGenMxBean = item; + } else if (edenSpaceName.contains(name)) { + edenSpaceMxBean = item; + } else if (psSurvivorName.contains(name)) { + pSSurvivorSpaceMxBean = item; + } + } + } + + @Override + public long heapMemoryCommitted() { + return memoryMXBean.getHeapMemoryUsage().getCommitted(); + } + + @Override + public long heapMemoryInit() { + return memoryMXBean.getHeapMemoryUsage().getInit(); + } + + @Override + public long heapMemoryMax() { + return memoryMXBean.getHeapMemoryUsage().getMax(); + } + + @Override + public long heapMemoryUsed() { + return memoryMXBean.getHeapMemoryUsage().getUsed(); + } + + @Override + public long nonHeapMemoryCommitted() { + return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); + } + + @Override + public long nonHeapMemoryInit() { + return memoryMXBean.getNonHeapMemoryUsage().getInit(); + } + + @Override + public long nonHeapMemoryMax() { + return memoryMXBean.getNonHeapMemoryUsage().getMax(); + } + + @Override + public long nonHeapMemoryUsed() { + return memoryMXBean.getNonHeapMemoryUsage().getUsed(); + } + + @Override + public long permGenCommitted() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getCommitted(); + } + + @Override + public long permGenInit() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getInit(); + } + + @Override + public long permGenMax() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getMax(); + } + + @Override + public long permGenUsed() { + if (permGenMxBean == null) { + return 0; + } + return permGenMxBean.getUsage().getUsed(); + } + + @Override + public long oldGenCommitted() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getCommitted(); + } + + @Override + public long oldGenInit() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getInit(); + } + + @Override + public long oldGenMax() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getMax(); + } + + @Override + public long oldGenUsed() { + if (oldGenMxBean == null) { + return 0; + } + return oldGenMxBean.getUsage().getUsed(); + } + + @Override + public long edenSpaceCommitted() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long edenSpaceInit() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getInit(); + } + + @Override + public long edenSpaceMax() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getMax(); + } + + @Override + public long edenSpaceUsed() { + if (null == edenSpaceMxBean) { + return 0; + } + return edenSpaceMxBean.getUsage().getUsed(); + } + + @Override + public long survivorCommitted() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getCommitted(); + } + + @Override + public long survivorInit() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getInit(); + } + + @Override + public long survivorMax() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getMax(); + } + + @Override + public long survivorUsed() { + if (null == pSSurvivorSpaceMxBean) { + return 0; + } + return pSSurvivorSpaceMxBean.getUsage().getUsed(); + } + + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("heapMemoryCommitted", heapMemoryCommitted()); + map.put("heapMemoryInit", heapMemoryInit()); + map.put("heapMemoryMax", heapMemoryMax()); + map.put("heapMemoryUsed", heapMemoryUsed()); + map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted()); + map.put("nonHeapMemoryInit", nonHeapMemoryInit()); + map.put("nonHeapMemoryMax", nonHeapMemoryMax()); + map.put("nonHeapMemoryUsed", nonHeapMemoryUsed()); + map.put("permGenCommitted", permGenCommitted()); + map.put("permGenInit", permGenInit()); + map.put("permGenMax", permGenMax()); + map.put("permGenUsed", permGenUsed()); + map.put("oldGenCommitted", oldGenCommitted()); + map.put("oldGenInit", oldGenInit()); + map.put("oldGenMax", oldGenMax()); + map.put("oldGenUsed", oldGenUsed()); + map.put("edenSpaceCommitted", edenSpaceCommitted()); + map.put("edenSpaceInit", edenSpaceInit()); + map.put("edenSpaceMax", edenSpaceMax()); + map.put("edenSpaceUsed", edenSpaceUsed()); + map.put("survivorCommitted", survivorCommitted()); + map.put("survivorInit", survivorInit()); + map.put("survivorMax", survivorMax()); + map.put("survivorUsed", survivorUsed()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java index a2c7ef03..abae771a 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThread.java @@ -1,60 +1,76 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; +import com.google.common.collect.Maps; +import com.mpush.monitor.quota.ThreadQuota; + import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Map; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.ThreadQuota; +public class JVMThread implements ThreadQuota { + + private ThreadMXBean threadMXBean; -public class JVMThread extends BaseQuota implements ThreadQuota{ - - private ThreadMXBean threadMXBean; - - public static final JVMThread instance = new JVMThread(); - - private JVMThread() { - threadMXBean = ManagementFactory.getThreadMXBean(); - } - - @Override - public int daemonThreadCount() { - return threadMXBean.getDaemonThreadCount(); - } - - @Override - public int threadCount() { - return threadMXBean.getThreadCount(); - } - - @Override - public long totalStartedThreadCount() { - return threadMXBean.getTotalStartedThreadCount(); - } - - @Override - public int deadLockedThreadCount() { - try { + public static final JVMThread I = new JVMThread(); + + private JVMThread() { + threadMXBean = ManagementFactory.getThreadMXBean(); + } + + @Override + public int daemonThreadCount() { + return threadMXBean.getDaemonThreadCount(); + } + + @Override + public int threadCount() { + return threadMXBean.getThreadCount(); + } + + @Override + public long totalStartedThreadCount() { + return threadMXBean.getTotalStartedThreadCount(); + } + + @Override + public int deadLockedThreadCount() { + try { long[] deadLockedThreadIds = threadMXBean.findDeadlockedThreads(); if (deadLockedThreadIds == null) { return 0; } return deadLockedThreadIds.length; } catch (Exception e) { - return 0; + return 0; } - } - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - map.put("daemonThreadCount", daemonThreadCount()); - map.put("threadCount", threadCount()); - map.put("totalStartedThreadCount", totalStartedThreadCount()); - map.put("deadLockedThreadCount", deadLockedThreadCount()); - return map; - } - - + } + + @Override + public Object monitor(Object... args) { + Map map = Maps.newHashMap(); + map.put("daemonThreadCount", daemonThreadCount()); + map.put("threadCount", threadCount()); + map.put("totalStartedThreadCount", totalStartedThreadCount()); + map.put("deadLockedThreadCount", deadLockedThreadCount()); + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index c9c77e7f..52bcd554 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -1,44 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.monitor.quota.impl; -import java.util.Iterator; +import com.mpush.monitor.quota.ThreadPoolQuota; +import com.mpush.tools.thread.pool.ThreadPoolManager; + +import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -import com.google.common.collect.Maps; -import com.mpush.monitor.quota.BaseQuota; -import com.mpush.monitor.quota.ThreadPoolQuota; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +public class JVMThreadPool implements ThreadPoolQuota { + public static final JVMThreadPool I = new JVMThreadPool(); -public class JVMThreadPool extends BaseQuota implements ThreadPoolQuota{ + private JVMThreadPool() { + } - public static final JVMThreadPool instance = new JVMThreadPool(); - - private JVMThreadPool() { - } - - private Map pool = null; - - @Override - public Map toMap() { - Map map = Maps.newHashMap(); - if(pool == null){ - pool = ThreadPoolManager.getPool(); - } - - Iterator> ite = pool.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - String serviceUniqName = entry.getKey(); - ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue(); - StringBuilder sb = new StringBuilder(); - sb.append("coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:" - + executor.getActiveCount()); - map.put(serviceUniqName, sb.toString()); - } - return map; - } - - + @Override + public Object monitor(Object... args) { + Map map = new HashMap<>(); + Map pool = ThreadPoolManager.I.getActivePools(); + for (Map.Entry entry : pool.entrySet()) { + String serviceName = entry.getKey(); + ThreadPoolExecutor executor = (ThreadPoolExecutor) entry.getValue(); + map.put(serviceName, ThreadPoolManager.getPoolInfo(executor)); + } + return map; + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java deleted file mode 100644 index f299ed5c..00000000 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorDataCollector.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.mpush.monitor.service; - -import com.mpush.tools.JVMUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.monitor.domain.MonitorData; -import com.mpush.monitor.quota.impl.JVMGC; -import com.mpush.monitor.quota.impl.JVMInfo; -import com.mpush.monitor.quota.impl.JVMMemory; -import com.mpush.monitor.quota.impl.JVMThread; -import com.mpush.monitor.quota.impl.JVMThreadPool; -import com.mpush.tools.Jsons; - -public class MonitorDataCollector { - - private static final Logger log = LoggerFactory.getLogger(MonitorDataCollector.class); - - private static volatile boolean dumpFirstJstack = false; - - private static volatile boolean dumpSecondJstack = false; - - private static volatile boolean dumpThirdJstack = false; - - private static volatile boolean dumpJmap = false; - - private static String currentPath = "/tmp/logs/mpush/"; - - private static int firstJstack = 2; - - private static int secondJstack = 4; - - private static int thirdJstack = 6; - - private static int firstJmap = 4; - - public static MonitorData collect() { - MonitorData data = new MonitorData(); - data.setInfoMap(JVMInfo.instance.toMap()); - data.setGcMap(JVMGC.instance.toMap()); - data.setMemoryMap(JVMMemory.instance.toMap()); - data.setThreadMap(JVMThread.instance.toMap()); - data.setThreadPoolMap(JVMThreadPool.instance.toMap()); - return data; - } - - public void setPath(String path) { - currentPath = path; - } - - public static void start(final boolean skipdump) { - new Thread(new Runnable() { - - @Override - public void run() { - while (true) { - MonitorData monitorData = MonitorDataCollector.collect(); - log.error("monitor data:" + Jsons.toJson(monitorData)); - - if(!skipdump){ - double load = JVMInfo.instance.load(); - if (load > firstJstack) { - if (!dumpFirstJstack) { - dumpFirstJstack = true; - JVMUtil.dumpJstack(currentPath); - } - } - - if (load > secondJstack) { - if (!dumpSecondJstack) { - dumpSecondJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if (load > thirdJstack) { - if (!dumpThirdJstack) { - dumpThirdJstack = true; - JVMUtil.dumpJmap(currentPath); - } - } - - if (load > firstJmap) { - if (!dumpJmap) { - dumpJmap = true; - JVMUtil.dumpJmap(currentPath); - } - } - } - - - try {//30s - Thread.sleep(30000L); - } catch (InterruptedException e) { - log.warn("monitor data exception", e); - } - } - } - }).start(); - } - - public static void main(String[] args) { - MonitorDataCollector.start(true); - } - -} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java new file mode 100644 index 00000000..3ce12611 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -0,0 +1,117 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.service; + +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.monitor.data.MonitorResult; +import com.mpush.monitor.data.ResultCollector; +import com.mpush.monitor.quota.impl.JVMInfo; +import com.mpush.tools.common.JVMUtil; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.TimeUnit; + +public class MonitorService extends BaseService implements Runnable { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + public static final MonitorService I = new MonitorService(); + + private static final int firstJstack = 2, secondJstack = 4, thirdJstack = 6, firstJmap = 4; + + private static final String dumpLogDir = CC.mp.monitor.dump_dir; + private static final boolean dumpEnabled = CC.mp.monitor.dump_stack; + private static final boolean printLog = CC.mp.monitor.print_log; + private static final long dumpPeriod = CC.mp.monitor.dump_period.getSeconds(); + + private boolean dumpFirstJstack = false; + private boolean dumpSecondJstack = false; + private boolean dumpThirdJstack = false; + private boolean dumpJmap = false; + + private final ResultCollector collector = new ResultCollector(); + + @Override + public void run() { + while (isRunning()) { + MonitorResult result = collector.collect(); + + if (printLog) { + Logs.Monitor.info(result.toJson()); + } + + if (dumpEnabled) { + dump(); + } + + try { + TimeUnit.SECONDS.sleep(dumpPeriod); + } catch (InterruptedException e) { + stop(); + } + } + } + + @Override + protected void doStart(Listener listener) throws Throwable { + if (printLog || dumpEnabled) { + Thread thread = new Thread(this, "mp-t-monitor"); + thread.start(); + } + } + + @Override + protected void doStop(Listener listener) throws Throwable { + logger.error("monitor service stopped!"); + } + + private void dump() { + double load = JVMInfo.I.load(); + if (load > firstJstack) { + if (!dumpFirstJstack) { + dumpFirstJstack = true; + JVMUtil.dumpJstack(dumpLogDir); + } + } + + if (load > secondJstack) { + if (!dumpSecondJstack) { + dumpSecondJstack = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + + if (load > thirdJstack) { + if (!dumpThirdJstack) { + dumpThirdJstack = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + + if (load > firstJmap) { + if (!dumpJmap) { + dumpJmap = true; + JVMUtil.dumpJmap(dumpLogDir); + } + } + } +} diff --git a/mpush-monitor/src/test/java/com/mpush/AppTest.java b/mpush-monitor/src/test/java/com/mpush/AppTest.java index 796b8466..6c96a1d2 100644 --- a/mpush-monitor/src/test/java/com/mpush/AppTest.java +++ b/mpush-monitor/src/test/java/com/mpush/AppTest.java @@ -7,32 +7,28 @@ /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ +public class AppTest + extends TestCase { /** * Create the test case * * @param testName name of the test case */ - public AppTest( String testName ) - { - super( testName ); + public AppTest(String testName) { + super(testName); } /** * @return the suite of tests being tested */ - public static Test suite() - { - return new TestSuite( AppTest.class ); + public static Test suite() { + return new TestSuite(AppTest.class); } /** * Rigourous Test :-) */ - public void testApp() - { - assertTrue( true ); + public void testApp() { + assertTrue(true); } } diff --git a/mpush-netty/.gitignore b/mpush-netty/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-netty/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index b0bcbe3f..4fb8bc19 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -18,7 +18,11 @@ ${mpush.groupId} - mpush-common + mpush-api + + + ${mpush.groupId} + mpush-tools io.netty @@ -37,5 +41,9 @@ io.netty netty-codec-http + + io.netty + netty-handler + diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java deleted file mode 100644 index 5aa506c2..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/ChannelClientHandler.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.netty.client; - -import com.mpush.api.Client; - -import io.netty.channel.ChannelHandler; - -public interface ChannelClientHandler extends ChannelHandler{ - - public Client getClient(); - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java deleted file mode 100644 index 43b27000..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpCallback.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.mpush.netty.client; - - -import io.netty.handler.codec.http.HttpResponse; - -/** - * Created by ohun on 2016/2/15. - * - * @author ohun@live.cn - */ -public interface HttpCallback { - - void onResponse(HttpResponse response); - - void onFailure(int statusCode, String reasonPhrase); - - void onException(Throwable throwable); - - void onTimeout(); - - boolean onRedirect(HttpResponse response); -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java deleted file mode 100644 index 014e0ba0..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/HttpClient.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.mpush.netty.client; - -/** - * Created by ohun on 2016/2/15. - * - * @author ohun@live.cn - */ -public interface HttpClient { - - void start(); - - void stop(); - - void request(RequestInfo requestInfo) throws Exception; -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index 0a214a21..b3efb889 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -1,154 +1,135 @@ -package com.mpush.netty.client; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.concurrent.TimeUnit; +package com.mpush.netty.client; -import com.mpush.netty.util.NettySharedHolder; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Client; +import com.mpush.api.service.Listener; +import com.mpush.api.service.ServiceException; +import com.mpush.netty.codec.PacketDecoder; +import com.mpush.netty.codec.PacketEncoder; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; - -import org.apache.commons.lang3.StringUtils; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mpush.api.Client; -import com.mpush.api.connection.Connection; -import com.mpush.api.protocol.Packet; -import com.mpush.tools.config.ConfigCenter; +import java.net.InetSocketAddress; -public class NettyClient implements Client { - - private static final String format = "%s:%s"; - - private static final Logger log = LoggerFactory.getLogger(NettyClient.class); +public abstract class NettyClient extends BaseService implements Client { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); private final String host; private final int port; - private Channel channel; - private int hbTimes; - private Connection connection; - - public NettyClient(final String host, final int port) { + private EventLoopGroup workerGroup; + + public NettyClient(String host, int port) { this.host = host; this.port = port; } - + + @Override + public void start(final Listener listener) { + if (started.compareAndSet(false, true)) { + Bootstrap bootstrap = new Bootstrap(); + workerGroup = new NioEventLoopGroup(); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .channel(NioSocketChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + initPipeline(ch.pipeline()); + } + }); + + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + if (listener != null) listener.onSuccess(port); + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + if (listener != null) listener.onFailure(future.cause()); + LOGGER.error("start netty client failure, host={}, port={}", host, port, future.cause()); + } + } + }); + } else { + listener.onFailure(new ServiceException("client has started!")); + } + } + + protected void initPipeline(ChannelPipeline pipeline) { + pipeline.addLast("decoder", getDecoder()); + pipeline.addLast("encoder", getEncoder()); + pipeline.addLast("handler", getChannelHandler()); + } + + protected ChannelHandler getDecoder() { + return new PacketDecoder(); + } + + protected ChannelHandler getEncoder() { + return PacketEncoder.INSTANCE; + } + + + public abstract ChannelHandler getChannelHandler(); + + @Override + protected void doStart(Listener listener) throws Throwable { + + } + @Override - public Channel getChannel() { - return channel; - } + protected void doStop(Listener listener) throws Throwable { + if (workerGroup != null) { + workerGroup.shutdownGracefully(); + } + LOGGER.error("netty client [{}] stopped.", this.getClass().getSimpleName()); + } - public void setChannel(Channel channel) { - this.channel = channel; - } + public String getHost() { + return host; + } - @Override - public void initConnection(Connection connection){ - this.connection = connection; + public int getPort() { + return port; } - + @Override - public void close(String cause) { - if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { - log.error("close channel:"+cause); - } - if(this.channel!=null){ - this.channel.close(); - } - - } - - @Override - public boolean isEnabled() { - return channel.isWritable(); - } - - @Override - public boolean isConnected() { - return channel.isActive(); - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } - - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void startHeartBeat() throws Exception { - startHeartBeat((int)ConfigCenter.I.scanConnTaskCycle()/1000); - } - - - @Override - public String getUrl() { - return String.format(format, host, port); - } - - @Override - public String getHost() { - return host; - } - - @Override - public int getPort() { - return port; - } - - @Override - public void stop(){ - this.close("stop"); - } - - @Override - public Connection getConnection() { - return connection; - } - - @Override - public void init(Channel channel) { - this.channel = channel; - } - - @Override - public String toString() { - return "NettyClient [host=" + host + ", port=" + port + ", channel=" + channel + ", hbTimes=" + hbTimes + ", connection=" + connection + "]"; - } - - @Override - public void startHeartBeat(final int heartbeat) throws Exception { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { - @Override - public void run(Timeout timeout) throws Exception { - try { - ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - if (!channel.isActive()) { - log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); - } - log.warn("client send msg hb false:" + channel.remoteAddress().toString()); - } else { - log.debug("client send msg hb success:" + channel.remoteAddress().toString()); - } - } - }); - } finally { - if (channel.isActive()) { - NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); - } - } - } - }, heartbeat, TimeUnit.MILLISECONDS); - - } - - + public String toString() { + return "NettyClient{" + + "host='" + host + '\'' + + ", port=" + port + + ", name=" + this.getClass().getSimpleName() + + '}'; + } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java deleted file mode 100644 index d3cce35e..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClientFactory.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.mpush.netty.client; - -import java.net.InetSocketAddress; -import java.util.Map; - -import com.mpush.netty.codec.PacketDecoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; - -import com.google.common.collect.Maps; -import com.mpush.api.Client; -import com.mpush.netty.codec.PacketEncoder; -import com.mpush.netty.util.NettySharedHolder; - -public class NettyClientFactory { - - private static final Logger log = LoggerFactory.getLogger(NettyClientFactory.class); - - public static final NettyClientFactory INSTANCE = new NettyClientFactory(); - - private final Map channel2Client = Maps.newConcurrentMap(); - - public Client create(final ChannelClientHandler handler){ - final Bootstrap bootstrap = new Bootstrap(); - bootstrap.group(NettySharedHolder.workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new PacketDecoder()); - ch.pipeline().addLast(PacketEncoder.INSTANCE); - ch.pipeline().addLast(handler); - } - }); - - Client client = handler.getClient(); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(client.getHost(), client.getPort())); - if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { - Channel channel = future.channel(); - log.error("init channel:"+channel); - return client; - } else { - future.cancel(true); - future.channel().close(); - log.warn("[remoting] failure to connect:" + client); - return null; - } - } - - public Client getCientByChannel(final Channel channel) { - return channel2Client.get(channel); - } - - public void remove(final Channel channel) { - channel2Client.remove(channel); - } - - public void put(Channel channel,final Client client){ - channel2Client.put(channel, client); - } - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java deleted file mode 100644 index d7677c43..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyHttpClient.java +++ /dev/null @@ -1,262 +0,0 @@ -package com.mpush.netty.client; - -import com.google.common.collect.ArrayListMultimap; -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; -import com.mpush.tools.config.ConfigCenter; - -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.http.*; -import io.netty.util.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.URI; -import java.net.URL; -import java.net.URLDecoder; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * Created by ohun on 2016/2/15. - * - * @author ohun@live.cn - */ -public class NettyHttpClient implements HttpClient { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); - - private final int maxConnPerHost = ConfigCenter.I.maxHttpConnCountPerHost(); - private final AttributeKey requestKey = AttributeKey.newInstance("request"); - private final AttributeKey hostKey = AttributeKey.newInstance("host"); - private final ArrayListMultimap channelPool = ArrayListMultimap.create(); - - private Bootstrap b; - private EventLoopGroup workerGroup; - private Timer timer; - - @Override - public void start() { - workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.httpExecutor); - b = new Bootstrap(); - b.group(workerGroup); - b.channel(NioSocketChannel.class); - b.option(ChannelOption.SO_KEEPALIVE, true); - b.option(ChannelOption.TCP_NODELAY, true); - b.option(ChannelOption.SO_REUSEADDR, true); - b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast("decoder", new HttpResponseDecoder()); - ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 5));//5M - ch.pipeline().addLast("encoder", new HttpRequestEncoder()); - ch.pipeline().addLast("handler", new HttpClientHandler()); - } - }); - timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), - 1, TimeUnit.SECONDS, 64); - } - - @Override - public void stop() { - for (Channel channel : channelPool.values()) { - channel.close(); - } - channelPool.clear(); - workerGroup.shutdownGracefully(); - timer.stop(); - } - - @Override - public void request(final RequestInfo info) throws Exception { - URI uri = new URI(info.request.uri()); - String host = info.host = uri.getHost(); - int port = uri.getPort() == -1 ? 80 : uri.getPort(); - info.request.headers().set(HttpHeaderNames.HOST, host); - info.request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); - timer.newTimeout(info, info.readTimeout, TimeUnit.MILLISECONDS); - Channel channel = tryAcquire(host); - if (channel == null) { - final long startConnectChannel = System.currentTimeMillis(); - LOGGER.debug("create new channel, host={}", host); - ChannelFuture f = b.connect(host, port); - f.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - LOGGER.debug("create new channel cost:" + (System.currentTimeMillis() - startConnectChannel)); - if (future.isSuccess()) { - writeRequest(future.channel(), info); - } else { - info.tryDone(); - info.onFailure(504, "Gateway Timeout"); - LOGGER.debug("request failure request={}", info); - } - } - }); - } else { - writeRequest(channel, info); - } - } - - private synchronized Channel tryAcquire(String host) { - List channels = channelPool.get(host); - if (channels == null || channels.isEmpty()) return null; - Iterator it = channels.iterator(); - while (it.hasNext()) { - Channel channel = it.next(); - it.remove(); - if (channel.isActive()) { - LOGGER.debug("tryAcquire channel success, host={}", host); - channel.attr(hostKey).set(host); - return channel; - } else {//链接由于意外情况不可用了,keepAlive_timeout - LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); - } - } - return null; - } - - private synchronized void tryRelease(Channel channel) { - String host = channel.attr(hostKey).getAndRemove(); - List channels = channelPool.get(host); - if (channels == null || channels.size() < maxConnPerHost) { - LOGGER.debug("tryRelease channel success, host={}", host); - channelPool.put(host, channel); - } else { - LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); - channel.close(); - } - } - - private void writeRequest(Channel channel, RequestInfo info) { - channel.attr(requestKey).set(info); - channel.attr(hostKey).set(info.host); - channel.writeAndFlush(info.request).addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - RequestInfo info = future.channel().attr(requestKey).getAndRemove(); - info.tryDone(); - info.onFailure(503, "Service Unavailable"); - LOGGER.debug("request failure request={}", info); - tryRelease(future.channel()); - } - } - }); - } - - @ChannelHandler.Sharable - private class HttpClientHandler extends ChannelHandlerAdapter { - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - try { - if (info != null && info.tryDone()) { - info.onException(cause); - } - } finally { - tryRelease(ctx.channel()); - } - LOGGER.error("http client caught an error, info={}", info, cause); - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - RequestInfo info = ctx.channel().attr(requestKey).getAndRemove(); - try { - if (info != null && info.tryDone()) { - HttpResponse response = (HttpResponse) msg; - if (isRedirect(response)) { - if (info.onRedirect(response)) { - String location = getRedirectLocation(info.request, response); - if (location != null && location.length() > 0) { - info.cancelled.set(false); - info.request = info.request.copy().setUri(location); - request(info); - return; - } - } - } - info.onResponse(response); - LOGGER.debug("request done request={}", info); - } - } finally { - tryRelease(ctx.channel()); - ReferenceCountUtil.release(msg); - } - } - - private boolean isRedirect(HttpResponse response) { - HttpResponseStatus status = response.status(); - switch (status.code()) { - case 300: - case 301: - case 302: - case 303: - case 305: - case 307: - return true; - default: - return false; - } - } - - private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { - String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); - if (hdr != null) { - if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { - return hdr; - } else { - URL orig = new URL(request.uri()); - String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); - if (hdr.startsWith("/")) { - pth = hdr; - } else if (pth.endsWith("/")) { - pth += hdr; - } else { - pth += "/" + hdr; - } - StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); - sb.append("://").append(orig.getHost()); - if (orig.getPort() > 0) { - sb.append(":").append(orig.getPort()); - } - if (pth.charAt(0) != '/') { - sb.append('/'); - } - sb.append(pth); - return sb.toString(); - } - } - return null; - } - - private HttpRequest copy(String uri, HttpRequest request) { - HttpRequest nue = request; - if (request instanceof DefaultFullHttpRequest) { - DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; - FullHttpRequest rq; - try { - rq = dfrq.copy(); - } catch (IllegalReferenceCountException e) { // Empty bytebuf - rq = dfrq; - } - rq.setUri(uri); - } else { - DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); - dfr.headers().set(request.headers()); - nue = dfr; - } - return nue; - } - } -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java deleted file mode 100644 index 6e55531b..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/client/SecurityNettyClient.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.mpush.netty.client; - - - -public class SecurityNettyClient extends NettyClient { - - private byte[] clientKey; - private byte[] iv; - private String clientVersion; - private String deviceId; - private String osName; - private String osVersion; - - private String userId; - - private String cipher; //快速重连的时候使用 - - public SecurityNettyClient(String host, int port) { - super(host, port); - } - - public byte[] getClientKey() { - return clientKey; - } - - public void setClientKey(byte[] clientKey) { - this.clientKey = clientKey; - } - - public byte[] getIv() { - return iv; - } - - public void setIv(byte[] iv) { - this.iv = iv; - } - - public String getClientVersion() { - return clientVersion; - } - - public void setClientVersion(String clientVersion) { - this.clientVersion = clientVersion; - } - - public String getDeviceId() { - return deviceId; - } - - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; - } - - public String getOsName() { - return osName; - } - - public void setOsName(String osName) { - this.osName = osName; - } - - public String getOsVersion() { - return osVersion; - } - - public void setOsVersion(String osVersion) { - this.osVersion = osVersion; - } - - public String getCipher() { - return cipher; - } - - public void setCipher(String cipher) { - this.cipher = cipher; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - -} diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java b/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java new file mode 100644 index 00000000..ec646692 --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/DecodeException.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.codec; + +/** + * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn + */ +public class DecodeException extends RuntimeException { + public DecodeException(String message) { + super(message); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 764bf760..158678e9 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -1,9 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.codec; -import com.mpush.api.exception.DecodeException; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -17,6 +35,7 @@ * @author ohun@live.cn */ public final class PacketDecoder extends ByteToMessageDecoder { + private static final int maxPacketSize = CC.mp.core.max_packet_size; @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { @@ -27,7 +46,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t private void decodeHeartbeat(ByteBuf in, List out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET_BYTE) { - out.add(new Packet(Command.HEARTBEAT)); + out.add(Packet.HB_PACKE); } else { in.readerIndex(in.readerIndex() - 1); break; @@ -65,7 +84,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { byte lrc = in.readByte(); byte[] body = null; if (bodyLength > 0) { - if (bodyLength > ConfigCenter.I.maxPacketSize()) { + if (bodyLength > maxPacketSize) { throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); } body = new byte[bodyLength]; diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index 57a887a7..ae6bfed3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.codec; import com.mpush.api.protocol.Command; diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 0b1a12e2..a963c00c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -1,9 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.connection; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.common.security.CipherBox; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.core.CipherFactory; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -17,23 +37,21 @@ */ public final class NettyConnection implements Connection, ChannelFutureListener { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); - + private static final CipherFactory factory = SpiLoader.load(CipherFactory.class); private SessionContext context; private Channel channel; private volatile int status = STATUS_NEW; private long lastReadTime; private long lastWriteTime; - private int hbTimes = 0; - @Override public void init(Channel channel, boolean security) { this.channel = channel; this.context = new SessionContext(); this.lastReadTime = System.currentTimeMillis(); this.status = STATUS_CONNECTED; - if (security) { - this.context.changeCipher(CipherBox.INSTANCE.getRsaCipher()); + if (security && factory != null) { + this.context.changeCipher(factory.get()); } } @@ -49,7 +67,7 @@ public SessionContext getSessionContext() { @Override public String getId() { - return channel.id().asLongText(); + return channel.id().asShortText(); } @Override @@ -67,7 +85,6 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { } } else { return this.close(); - } } @@ -104,7 +121,7 @@ public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { lastWriteTime = System.currentTimeMillis(); } else { - LOGGER.error("send msg error"); + LOGGER.error("connection send msg error", future.cause()); } } @@ -112,19 +129,14 @@ public void updateLastWriteTime() { lastWriteTime = System.currentTimeMillis(); } - @Override - public int inceaseAndGetHbTimes() { - return ++hbTimes; - } - - @Override - public void resetHbTimes() { - hbTimes = 0; - } @Override public String toString() { - return "NettyConnection [context=" + context + ", channel=" + channel + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime + ", hbTimes=" + hbTimes + return "NettyConnection [context=" + context + + ", channel=" + channel + + ", status=" + status + + ", lastReadTime=" + lastReadTime + + ", lastWriteTime=" + lastWriteTime + "]"; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java new file mode 100644 index 00000000..a628253a --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpCallback.java @@ -0,0 +1,41 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; + + +import io.netty.handler.codec.http.HttpResponse; + +/** + * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn + */ +public interface HttpCallback { + + void onResponse(HttpResponse response); + + void onFailure(int statusCode, String reasonPhrase); + + void onException(Throwable throwable); + + void onTimeout(); + + boolean onRedirect(HttpResponse response); +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java new file mode 100644 index 00000000..06305e55 --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClient.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; + +import com.mpush.api.service.Service; + +/** + * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn + */ +public interface HttpClient extends Service { + void request(RequestContext context) throws Exception; +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java new file mode 100644 index 00000000..26f6a31e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java @@ -0,0 +1,126 @@ +package com.mpush.netty.http; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; +import io.netty.util.IllegalReferenceCountException; +import io.netty.util.ReferenceCountUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URL; +import java.net.URLDecoder; + +@ChannelHandler.Sharable +public class HttpClientHandler extends ChannelHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private final NettyHttpClient client; + + public HttpClientHandler(NettyHttpClient client) { + this.client = client; + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); + try { + if (context != null && context.tryDone()) { + context.onException(cause); + } + } finally { + client.pool.tryRelease(ctx.channel()); + } + LOGGER.error("http client caught an error, info={}", context, cause); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); + try { + if (context != null && context.tryDone()) { + HttpResponse response = (HttpResponse) msg; + if (isRedirect(response)) { + if (context.onRedirect(response)) { + String location = getRedirectLocation(context.request, response); + if (location != null && location.length() > 0) { + context.cancelled.set(false); + context.request = context.request.copy().setUri(location); + client.request(context); + return; + } + } + } + context.onResponse(response); + LOGGER.debug("request done request={}", context); + } + } finally { + client.pool.tryRelease(ctx.channel()); + ReferenceCountUtil.release(msg); + } + } + + private boolean isRedirect(HttpResponse response) { + HttpResponseStatus status = response.status(); + switch (status.code()) { + case 300: + case 301: + case 302: + case 303: + case 305: + case 307: + return true; + default: + return false; + } + } + + private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { + String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); + if (hdr != null) { + if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { + return hdr; + } else { + URL orig = new URL(request.uri()); + String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); + if (hdr.startsWith("/")) { + pth = hdr; + } else if (pth.endsWith("/")) { + pth += hdr; + } else { + pth += "/" + hdr; + } + StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); + sb.append("://").append(orig.getHost()); + if (orig.getPort() > 0) { + sb.append(":").append(orig.getPort()); + } + if (pth.charAt(0) != '/') { + sb.append('/'); + } + sb.append(pth); + return sb.toString(); + } + } + return null; + } + + private HttpRequest copy(String uri, HttpRequest request) { + HttpRequest nue = request; + if (request instanceof DefaultFullHttpRequest) { + DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; + FullHttpRequest rq; + try { + rq = dfrq.copy(); + } catch (IllegalReferenceCountException e) { // Empty bytebuf + rq = dfrq; + } + rq.setUri(uri); + } else { + DefaultHttpRequest dfr = new DefaultHttpRequest(request.protocolVersion(), request.method(), uri); + dfr.headers().set(request.headers()); + nue = dfr; + } + return nue; + } +} \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java new file mode 100644 index 00000000..ce55224e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java @@ -0,0 +1,85 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.netty.http; + +import com.google.common.collect.ArrayListMultimap; +import com.mpush.tools.config.CC; +import io.netty.channel.Channel; +import io.netty.util.AttributeKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by yxx on 2016/5/28. + * + * @author ohun@live.cn (夜色) + */ +public class HttpConnectionPool { + private static final int maxConnPerHost = CC.mp.http.max_conn_per_host; + + private final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPool.class); + + private final AttributeKey hostKey = AttributeKey.newInstance("host"); + + private final ArrayListMultimap channelPool = ArrayListMultimap.create(); + + public synchronized Channel tryAcquire(String host) { + List channels = channelPool.get(host); + if (channels == null || channels.isEmpty()) return null; + Iterator it = channels.iterator(); + while (it.hasNext()) { + Channel channel = it.next(); + it.remove(); + if (channel.isActive()) { + LOGGER.debug("tryAcquire channel success, host={}", host); + channel.attr(hostKey).set(host); + return channel; + } else {//链接由于意外情况不可用了,keepAlive_timeout + LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); + } + } + return null; + } + + public synchronized void tryRelease(Channel channel) { + String host = channel.attr(hostKey).getAndRemove(); + List channels = channelPool.get(host); + if (channels == null || channels.size() < maxConnPerHost) { + LOGGER.debug("tryRelease channel success, host={}", host); + channelPool.put(host, channel); + } else { + LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); + channel.close(); + } + } + + public void attachHost(String host, Channel channel) { + channel.attr(hostKey).set(host); + } + + public void close() { + for (Channel channel : channelPool.values()) { + channel.close(); + } + channelPool.clear(); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java new file mode 100644 index 00000000..9af0db7e --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -0,0 +1,150 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; + +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestEncoder; +import io.netty.handler.codec.http.HttpResponseDecoder; +import io.netty.util.AttributeKey; +import io.netty.util.HashedWheelTimer; +import io.netty.util.Timer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.util.concurrent.TimeUnit; + +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; +import static io.netty.handler.codec.http.HttpHeaderNames.HOST; +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; + +/** + * Netty的一个Bootstrap是可以关联多个channel的, + * 本Client采用的就是这种模式,在种模式下如果Handler添加了@ChannelHandler.Sharable + * 注解的话,要特殊处理,因为这时的client和handler是被所有请求共享的。 + *

+ * Created by ohun on 2016/2/15. + * + * @author ohun@live.cn + */ +public class NettyHttpClient extends BaseService implements HttpClient { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private static final int maxContentLength = (int) CC.mp.http.max_content_length; + /*package*/ final AttributeKey requestKey = AttributeKey.newInstance("request"); + /*package*/ final HttpConnectionPool pool = new HttpConnectionPool(); + private Bootstrap b; + private EventLoopGroup workerGroup; + private Timer timer; + + @Override + public void request(RequestContext context) throws Exception { + URI uri = new URI(context.request.uri()); + String host = context.host = uri.getHost(); + int port = uri.getPort() == -1 ? 80 : uri.getPort(); + //1.设置请求头 + context.request.headers().set(HOST, host);//映射后的host + context.request.headers().set(CONNECTION, KEEP_ALIVE);//保存长链接 + + //2.添加请求超时检测队列 + timer.newTimeout(context, context.readTimeout, TimeUnit.MILLISECONDS); + + //3.先尝试从连接池里取可用链接,去取不到就创建新链接。 + Channel channel = pool.tryAcquire(host); + if (channel == null) { + final long startCreate = System.currentTimeMillis(); + LOGGER.debug("create new channel, host={}", host); + ChannelFuture f = b.connect(host, port); + f.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); + if (future.isSuccess()) {//3.1.把请求写到http server + writeRequest(future.channel(), context); + } else {//3.2如果链接创建失败,直接返回客户端网关超时 + context.tryDone(); + context.onFailure(504, "Gateway Timeout"); + LOGGER.warn("create new channel failure, request={}", context); + } + } + }); + } else { + //3.1.把请求写到http server + writeRequest(channel, context); + } + } + + private void writeRequest(Channel channel, RequestContext context) { + channel.attr(requestKey).set(context); + pool.attachHost(context.host, channel); + channel.writeAndFlush(context.request).addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (!future.isSuccess()) { + RequestContext info = future.channel().attr(requestKey).getAndRemove(); + info.tryDone(); + info.onFailure(503, "Service Unavailable"); + LOGGER.debug("request failure request={}", info); + pool.tryRelease(future.channel()); + } + } + }); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); + b = new Bootstrap(); + b.group(workerGroup); + b.channel(NioSocketChannel.class); + b.option(ChannelOption.SO_KEEPALIVE, true); + b.option(ChannelOption.TCP_NODELAY, true); + b.option(ChannelOption.SO_REUSEADDR, true); + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + b.handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast("decoder", new HttpResponseDecoder()); + ch.pipeline().addLast("aggregator", new HttpObjectAggregator(maxContentLength)); + ch.pipeline().addLast("encoder", new HttpRequestEncoder()); + ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this)); + } + }); + timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), 1, TimeUnit.SECONDS, 64); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + pool.close(); + workerGroup.shutdownGracefully(); + timer.stop(); + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java similarity index 66% rename from mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java rename to mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java index 28c61ee4..915a09b3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/RequestInfo.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java @@ -1,8 +1,27 @@ -package com.mpush.netty.client; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.http; import com.google.common.primitives.Ints; import com.mpush.api.Constants; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Timeout; @@ -10,37 +29,39 @@ import java.util.concurrent.atomic.AtomicBoolean; -public class RequestInfo implements TimerTask, HttpCallback { - private static final int TIMEOUT = ConfigCenter.I.httpDefaultReadTimeout(); +public class RequestContext implements TimerTask, HttpCallback { + private static final int TIMEOUT = CC.mp.http.default_read_timeout; final AtomicBoolean cancelled = new AtomicBoolean(false); final long startTime = System.currentTimeMillis(); + final int readTimeout; long endTime = startTime; - int readTimeout = TIMEOUT; private String uri; private HttpCallback callback; FullHttpRequest request; String host; - public RequestInfo(FullHttpRequest request, HttpCallback callback) { + public RequestContext(FullHttpRequest request, HttpCallback callback) { this.callback = callback; this.request = request; this.uri = request.uri(); + this.readTimeout = parseTimeout(); + } + + private int parseTimeout() { String timeout = request.headers().getAndRemoveAndConvert(Constants.HTTP_HEAD_READ_TIMEOUT); if (timeout != null) { Integer integer = Ints.tryParse(timeout); - if (integer != null && integer > 0) readTimeout = integer; + if (integer != null && integer > 0) { + return integer; + } } + return TIMEOUT; } public int getReadTimeout() { return readTimeout; } - public RequestInfo setReadTimeout(int timeout) { - this.readTimeout = timeout; - return this; - } - @Override public void run(Timeout timeout) throws Exception { if (tryDone()) { @@ -50,6 +71,11 @@ public void run(Timeout timeout) throws Exception { } } + /** + * 由于检测请求超时的任务存在,为了防止多线程下重复处理 + * + * @return + */ public boolean tryDone() { return cancelled.compareAndSet(false, true); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index db065085..0b242517 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -1,9 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.netty.server; -import com.mpush.api.Server; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; +import com.mpush.api.service.ServiceException; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -15,6 +38,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Locale; +import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; /** @@ -22,7 +47,7 @@ * * @author ohun@live.cn */ -public abstract class NettyServer implements Server { +public abstract class NettyServer extends BaseService implements Server { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -54,11 +79,13 @@ public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { IllegalStateException e = new IllegalStateException("server was already shutdown."); if (listener != null) listener.onFailure(e); - throw e; + Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName()); + return; } + Logs.Console.error("try shutdown {}...", this.getClass().getSimpleName()); if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); - logger.error("netty server stop now"); + Logs.Console.error("{} shutdown success.", this.getClass().getSimpleName()); if (listener != null) { listener.onSuccess(port); } @@ -69,7 +96,11 @@ public void start(final Listener listener) { if (!serverState.compareAndSet(State.Initialized, State.Starting)) { throw new IllegalStateException("Server already started or have not init"); } - createNioServer(listener); + if (useNettyEpoll()) { + createEpollServer(listener); + } else { + createNioServer(listener); + } } private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { @@ -130,10 +161,10 @@ public void initChannel(SocketChannel ch) throws Exception { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - logger.error("server start success on:" + port); + Logs.Console.error("server start success on:{}", port); if (listener != null) listener.onSuccess(port); } else { - logger.error("server start failure on:" + port, future.cause()); + Logs.Console.error("server start failure on:{}", port, future.cause()); if (listener != null) listener.onFailure(future.cause()); } } @@ -149,7 +180,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } catch (Exception e) { logger.error("server start exception", e); if (listener != null) listener.onFailure(e); - throw new RuntimeException("server start exception, port=" + port, e); + throw new ServiceException("server start exception, port=" + port, e); } finally { /*** * 优雅关闭 @@ -158,17 +189,16 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - private void createNioServer(final Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadPoolManager.bossExecutor); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.workExecutor); + private void createNioServer(Listener listener) { + NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, getBossExecutor()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, getWorkExecutor()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } - @SuppressWarnings("unused") - private void createEpollServer(final Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); + private void createEpollServer(Listener listener) { + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, getBossExecutor()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, getWorkExecutor()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -208,4 +238,28 @@ protected void initPipeline(ChannelPipeline pipeline) { pipeline.addLast("encoder", getEncoder()); pipeline.addLast("handler", getChannelHandler()); } + + protected Executor getBossExecutor() { + return null; + } + + protected Executor getWorkExecutor() { + return null; + } + + private boolean useNettyEpoll() { + if (!"netty".equals(CC.mp.core.epoll_provider)) return false; + String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); + return name.startsWith("linux"); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + + } + + @Override + protected void doStop(Listener listener) throws Throwable { + + } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java b/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java deleted file mode 100644 index 34874d67..00000000 --- a/mpush-netty/src/main/java/com/mpush/netty/util/NettySharedHolder.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.mpush.netty.util; - - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.ThreadNameSpace; - -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; -import io.netty.util.Timer; - -public class NettySharedHolder { - - public static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedThreadFactory(ThreadNameSpace.NETTY_TIMER)); - - public static EventLoopGroup workerGroup = new NioEventLoopGroup(); - -} diff --git a/mpush-netty/src/test/resources/logback.xml b/mpush-netty/src/test/resources/logback.xml index e638aca3..8b23ef25 100644 --- a/mpush-netty/src/test/resources/logback.xml +++ b/mpush-netty/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN + + System.out + UTF-8 + + INFO - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-test/.gitignore b/mpush-test/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 2c830035..7a1312d7 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -34,20 +34,10 @@ ${mpush.groupId} mpush-boot - - ${mpush.groupId} - mpush-log - ${mpush.groupId} mpush-client - - - args4j - args4j - - diff --git a/mpush-test/src/test/java/com/mpush/AppTest.java b/mpush-test/src/test/java/com/mpush/AppTest.java deleted file mode 100644 index 796b8466..00000000 --- a/mpush-test/src/test/java/com/mpush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.mpush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java new file mode 100644 index 00000000..c8913e04 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -0,0 +1,70 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain { + + public static void main(String[] args) throws Exception { + Logs.init(); + ConnectClientBoot main = new ConnectClientBoot(); + main.run(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); + for (int i = 0; i < 1; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "user-" + i; + String deviceId = "test-device-id-" + i; + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] iv = CipherBox.I.randomAESIV(); + + ClientConfig config = new ClientConfig(); + config.setClientKey(clientKey); + config.setIv(iv); + config.setClientVersion(clientVersion); + config.setDeviceId(deviceId); + config.setOsName(osName); + config.setOsVersion(osVersion); + config.setUserId(userId); + Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + } + + LockSupport.park(); + + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java new file mode 100644 index 00000000..5abd1cc5 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java @@ -0,0 +1,71 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain2 { + + public static void main(String[] args) throws Exception { + Logs.init(); + ConnectClientBoot main = new ConnectClientBoot(); + main.run(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); + + ClientConfig config = new ClientConfig(); + config.setClientKey(CipherBox.I.randomAESKey()); + config.setIv(CipherBox.I.randomAESIV()); + config.setClientVersion("1.0.0"); + config.setDeviceId("android-device-id-1"); + config.setOsName("android"); + config.setOsVersion("1.0.1"); + config.setUserId("user-0"); + Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + + config = new ClientConfig(); + config.setClientKey(CipherBox.I.randomAESKey()); + config.setIv(CipherBox.I.randomAESIV()); + config.setClientVersion("1.0.0"); + config.setDeviceId("pc-device-id-2"); + config.setOsName("pc"); + config.setOsVersion("1.0.1"); + config.setUserId("user-0"); + client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); + client.start().get(10, TimeUnit.SECONDS); + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java new file mode 100644 index 00000000..940dd8c3 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java @@ -0,0 +1,42 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.client; + +import com.google.common.collect.Lists; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public class ConnectClientBoot { + private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); + + public void run() { + ZKClient.I.start(); + RedisManager.I.init(); + listener.beginWatch(); + } + + public List getServers() { + return Lists.newArrayList(listener.getCache().values()); + } +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 61ae1c36..97bedcc4 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -1,29 +1,111 @@ -package com.mpush.test.configcenter; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.List; -import java.util.Map; +package com.mpush.test.configcenter; +import com.mpush.tools.config.CC; +import com.typesafe.config.ConfigObject; +import com.typesafe.config.ConfigValue; +import org.junit.Before; import org.junit.Test; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.DnsMapping; +import java.util.*; public class ConfigCenterTest { - - @Test - public void test(){ - - System.out.println(ConfigCenter.I.forceWriteRedisGroupInfo()); - - } - - @Test - public void testDnsMapping(){ - Map> ret = ConfigCenter.I.dnsMapping(); - - System.out.println(Jsons.toJson(ret)); - - } + @Before + public void setUp() throws Exception { + // ConfigManager.I.getLocalIp(); + } + + @Test + public void testKey() { + //String t = ConfigKey.app_env.getString(); + System.out.println(CC.mp.security.aes_key_length); + System.out.println(CC.mp.redis.cluster_group); + } + + @Test + public void testLoad() { + Map map = new HashMap<>(); + CC.cfg.entrySet().forEach(e -> print(e.getKey(), e.getValue(), map)); + List list = new ArrayList<>(map.values()); + Collections.sort(list); + list.forEach(s -> System.out.println(s.substring(s.indexOf(".") + 1) + ",")); + } + + public static void print(String s, ConfigValue configValue, Map map) { + if (s.startsWith("mp") && !s.endsWith("\"")) { + String[] keys = s.split("\\."); + if (keys.length >= 4) return; + for (int i = keys.length - 1; i > 0; i--) { + String key = keys[i]; + String value = map.get(key); + if (value != null) continue; + String p = keys[i - 1]; + map.put(key, p + "." + key.replace('-', '_') + "(" + p + ")"); + } + } + } + + @Test + public void testLoad2() { + Map map = new HashMap<>(); + System.out.println("public interface mp {"); + System.out.printf(" Config cfg = ConfigManager.I.mp().toConfig();%n%n"); + CC.mp.cfg.root().forEach((s, configValue) -> print2(s, configValue, "mp", 1)); + System.out.println("}"); + } + + public static void print2(String s, ConfigValue configValue, String p, int level) { + int l = level + 1; + switch (configValue.valueType()) { + case OBJECT: + printTab(level); + System.out.printf("interface %s {%n", s.replace('-', '_')); + printTab(level); + System.out.printf(" Config cfg = %s.cfg.getObject(\"%s\").toConfig();%n%n", p, s); + ((ConfigObject) configValue).forEach((s1, configValue2) -> print2(s1, configValue2, s, l)); + printTab(level); + System.out.printf("}%n%n"); + break; + case STRING: + printTab(level); + System.out.printf(" String %s = cfg.getString(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case NUMBER: + printTab(level); + System.out.printf(" Number %s = cfg.getNumber(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case BOOLEAN: + printTab(level); + System.out.printf(" Boolean %s = cfg.getBoolean(\"%s\");%n%n", s.replace('-', '_'), s); + break; + case LIST: + printTab(level); + System.out.printf(" List %s = cfg.getList(\"%s\").unwrapped();%n%n", s.replace('-', '_'), s); + break; + } + } + private static void printTab(int l) { + while (l-- > 0) { + System.out.print(" "); + } + } } diff --git a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java index 2a0ed803..b85ca668 100644 --- a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java +++ b/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java @@ -1,6 +1,23 @@ -package com.mpush.test.connection.body; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.security.interfaces.RSAPrivateKey; +package com.mpush.test.connection.body; import com.mpush.common.message.HandshakeMessage; import com.mpush.tools.crypto.RSAUtils; @@ -9,66 +26,68 @@ import org.junit.Before; import org.junit.Test; +import java.security.interfaces.RSAPrivateKey; + public class BodyTest { - + private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; private String daily_allocServer = "http://111.1.57.148/mpns/push/server/"; private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; private RSAPrivateKey dailyPrivateKey = null; - + private String pre_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUzaTfOZcywslS08R1w0pLpSqM30lsM/+XiS9tI7Es7c5wOliVjeaXLdIU48bZxFRz+FyTmYCblekZA/LGlLgedtQf/kA1vHGBnrO/YHd0Re4JqHwmhdKjF/pCSVGqFRTKytDZ9/87tVqtRiPjE95r1Qdt44JzvNLcwVwCEFXMQQIDAQAB"; private String pre_allocServer = "http://115.29.230.2/mpns/push/server/"; private String pre_privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJTNpN85lzLCyVLTxHXDSkulKozfSWwz/5eJL20jsSztznA6WJWN5pct0hTjxtnEVHP4XJOZgJuV6RkD8saUuB521B/+QDW8cYGes79gd3RF7gmofCaF0qMX+kJJUaoVFMrK0Nn3/zu1Wq1GI+MT3mvVB23jgnO80tzBXAIQVcxBAgMBAAECgYAuChZp7pKmZPgPRXAyk3LIRtkMbwVzkf8RrqNa3EE0Snul45eG5jmjKs0mI3dh50nN+9kA3eyZtt9BGyShZmA0q6v5s24+6BMIz1Hmkkpjyq5bwwmAHu6DjB2lphYhn9OiFuimXkVKRA8KbEo3SijLmSLY/7kHNxp5F49b9KynAQJBANH+DUPJeBnyUIECcc0+je0tsH7jm6U7sj6x/BIhcvAe9RoqMe23TEysdwqys800VYuvzXoeQYnZZ1WyQA/WOVECQQC1Z6YiaEShoF9IGqYJe4JN3dj/6r4nuWn93hZRtDPJX8+sczsPmboJvE7cE4yfILDRsC16UkTobUq4z0XyZqfxAkA+M+gP/VzTKsEIBgZZyr7V0+PlIlzXjCBXi/dkE35tfG4UKw2RIeu7BpdYlujFz0vLze6qzs2RHNIMQ3nQdx+RAkBySi0nfF3RHsMpIHD/hGsiN+VhxzmquWyH34ZcT5ZZBx5GXgsV1xqYy6U7jq2IDAaa9c6RRIfZkBIFwcEl6XthAkEArpGszIis1caRAd47YXzfg1aBGG8eQ1wY2EEX1q1iJuz/KMLccCYmFl+4R5Vfsmzy65YqO+EOKw4jBhLBvtkCmA=="; private RSAPrivateKey prePrivateKey = null; - + //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB private String online_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcNLVG4noMfOIvKfV0eJAcADO4nr0hqoj42swL8DWY8CujpUGutw7Qk5LEn6i037wlF5CwIzJ7ix2xK+IcxEonOANtlS1NKbUXOCgUtA5mdZTnvAUByN0tzGp4BGywYNiXFQmLMXG5uxN0ZfcaoRKVqLzbcMnLB7VzS4L3OxzxqwIDAQAB"; private String online_allocServer = "http://allot.mangguoyisheng.com/"; private String online_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJw0tUbiegx84i8p9XR4kBwAM7ievSGqiPjazAvwNZjwK6OlQa63DtCTksSfqLTfvCUXkLAjMnuLHbEr4hzESic4A22VLU0ptRc4KBS0DmZ1lOe8BQHI3S3MangEbLBg2JcVCYsxcbm7E3Rl9xqhEpWovNtwycsHtXNLgvc7HPGrAgMBAAECgYBLDaRAH9dmoqaG5NE0Gi2b1pkDTvou7+KKN46s+ci26Eb/hQqvKPOsUgvF/7Su24VqCQ2TJAZAiBJHK3+kNMgvmbZt3BA0jn2F13djixHip7gHSMUMD4a+jQ5MDtiE7TFVDNrkYvfbhgmT4g5wdWI1xoaHftDQAPA01B4nUIe04QJBAMiRwmnZsxNWKGSriMCbEI2j2t4T25SLcCpVoC5MZ+E+1P997qGo//6iDL65yvxN7PNI+5qFZ8poT5wrsS2j8JsCQQDHYD3kbQTRdCQTmK8Vl6EcL7kZUcxGlq5XsdyRG3r2bIvVE4pNnyEdpG+6qsqPUw00JTOvZ3HmST9CNuWtF+wxAkB+9rYI54RSg0HCqEtDEWXjI4xS9GMVn0b7pYRmintfvLR8ny1GLIMQn2hN+7KhEHskbljHMhfHq0xp4cagy5xtAkEAp4GHXmPtmWAfc0tjRhvXowvBUrFzk5bDMTBgpJVW8LRvovxAxUg9lV7y8/zMJDBdtoLO8r5RZm4BtMrUmmGv8QJBAIbo67g2H84eZSSFTwQ1YnjdhLBBv4TBPuhtSHH00mUWOKR9qQSp+MixRUTE1HP1htn+DQ0KI5zbY4FLAT3Nb0g="; - private RSAPrivateKey onlinePrivateKey = null; + private RSAPrivateKey onlinePrivateKey = null; + + String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; + + byte[] data = null; + + @Before + public void init() { + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); + onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); + String[] temp = rawData.split(","); + data = new byte[temp.length]; + for (int i = 0; i < temp.length; i++) { + data[i] = Byte.parseByte(temp[i].trim()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void dailyDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, dailyPrivateKey); + decode(message); + } + + @Test + public void preDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, prePrivateKey); + decode(message); + } + + @Test + public void onlineDecode() { + byte[] message = RSAUtils.decryptByPrivateKey(data, onlinePrivateKey); + decode(message); + } - String rawData = "29, -114, -24, 82, -96, -116, -19, 64, -91, 88, -119, -88, 29, 33, 9, 9, 84, 120, -62, 54, -72, 47, -77, -126, -73, 2, -38, -62, -75, 39, -114, 92, -115, -9, -81, 43, -82, 73, 2, -101, 32, 67, 112, 46, 119, -16, -6, -85, -121, -44, 25, 28, -116, 6, 56, -2, -6, -109, -75, 91, -104, 9, -84, -28, 72, 24, 94, -54, 127, -124, -125, -93, 102, 38, 11, -55, 70, -86, 101, -76, -54, -11, 80, 104, -80, 44, 53, -117, 117, -88, -96, -19, 116, 0, -62, 103, -15, -106, -5, -55, -103, -86, 72, -18, -83, -117, 39, 80, -121, -31, -105, -28, 9, 23, -24, 106, -116, 127, -77, -122, 71, -112, -79, -106, 80, 9, -86, -22"; - - byte[]data = null; - - @Before - public void init(){ - try { - dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); - prePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(pre_privateKey); - onlinePrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(online_privateKey); - String[] temp = rawData.split(","); - data = new byte[temp.length]; - for(int i = 0;i getServers() { - return Lists.newArrayList(listener.getManager().getList()); - } -} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java new file mode 100644 index 00000000..9452bccc --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.connection.mpns; + +import com.mpush.api.service.Client; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnectClient; +import com.mpush.common.security.CipherBox; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; +import java.util.concurrent.locks.LockSupport; + +public class ConnClientTestMain { + + public static void main(String[] args) throws InterruptedException { + + ConnectTestClientBoot main = new ConnectTestClientBoot(); + main.start(); + + List serverList = main.getServers(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + + for (int i = 0; i < 1000; i++) { + String clientVersion = "1.0." + i; + String osName = "android"; + String osVersion = "1.0.1"; + String userId = "uh-" + i; + String deviceId = "test-device-id-" + i; + String cipher = ""; + byte[] clientKey = CipherBox.I.randomAESKey(); + byte[] iv = CipherBox.I.randomAESIV(); + + ClientConfig config = new ClientConfig(); + config.setClientKey(clientKey); + config.setIv(iv); + config.setClientVersion(clientVersion); + config.setDeviceId(deviceId); + config.setOsName(osName); + config.setOsVersion(osVersion); + config.setUserId(userId); + config.setCipher(cipher); + Client client = new ConnectClient(server.getIp(), server.getPort(), config); + Thread.sleep(100); + } + + LockSupport.park(); + } + +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java deleted file mode 100644 index 1126476d..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClient.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mpush.test.connection.mpns; - -import com.google.common.collect.Lists; -import com.mpush.common.AbstractClient; -import com.mpush.push.zk.listener.ConnectZKListener; -import com.mpush.zk.ZKServerNode; - -import java.util.List; - -public class ConnectTestClient extends AbstractClient { - private final ConnectZKListener listener = new ConnectZKListener(); - - public ConnectTestClient() { - registerListener(listener); - } - - public List getServers() { - return Lists.newArrayList(listener.getManager().getList()); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java new file mode 100644 index 00000000..d927e1c6 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java @@ -0,0 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.connection.mpns; + +import com.google.common.collect.Lists; +import com.mpush.zk.listener.ZKServerNodeWatcher; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public class ConnectTestClientBoot { + private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); + + public void start() { + listener.beginWatch(); + } + + public List getServers() { + return Lists.newArrayList(listener.getCache().values()); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java b/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java deleted file mode 100644 index 778a4778..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/severs/Main.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.test.connection.severs; - -/** - * Created by yxx on 2016/5/16. - * - * @author ohun@live.cn - */ -public class Main { - public static void main(String[] args) { - com.mpush.Main.main(args); - } -} diff --git a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java index 5ff50659..4b7823f7 100644 --- a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java +++ b/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java @@ -1,10 +1,23 @@ -package com.mpush.test.crypto; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; +package com.mpush.test.crypto; import com.mpush.common.message.HandshakeMessage; import com.mpush.common.security.CipherBox; @@ -12,145 +25,150 @@ import org.junit.Before; import org.junit.Test; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + public class RsaTest { private String daily_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"; private String daily_privateKey = "MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="; private static RSAPrivateKey dailyPrivateKey = null; private static RSAPublicKey dailyPublicKey = null; - + @Before - public void init(){ - try { - dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); - dailyPublicKey = (RSAPublicKey)RSAUtils.decodePublicKey(daily_publicKey); - } catch (Exception e) { - e.printStackTrace(); - } - } - + public void init() { + try { + dailyPrivateKey = (RSAPrivateKey) RSAUtils.decodePrivateKey(daily_privateKey); + dailyPublicKey = (RSAPublicKey) RSAUtils.decodePublicKey(daily_publicKey); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test - public void test(){ - + public void test() { + HandshakeMessage message = new HandshakeMessage(null); - message.clientKey = CipherBox.INSTANCE.randomAESKey(); - message.iv = CipherBox.INSTANCE.randomAESIV(); + message.clientKey = CipherBox.I.randomAESKey(); + message.iv = CipherBox.I.randomAESIV(); message.clientVersion = "1.1.0"; message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; message.osName = "android"; message.osVersion = "1.2.1"; message.timestamp = System.currentTimeMillis(); - + byte[] temp = message.encode(); - + long startencode = System.currentTimeMillis(); byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); long encodeTime = System.currentTimeMillis() - startencode; - + long startdecode = System.currentTimeMillis(); byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); long decodeTime = System.currentTimeMillis() - startdecode; decode(temp2); - System.out.println(encodeTime+","+decodeTime); - + System.out.println(encodeTime + "," + decodeTime); + } - + @Test - public void mulTest(){ - - Executor pool = Executors.newFixedThreadPool(20); - - CountDownLatch encodeLatch = new CountDownLatch(1); - CountDownLatch decodeLatch = new CountDownLatch(1); - - for(int i = 0;i<18;i++){ - pool.execute(new Worker(i, encodeLatch, decodeLatch)); - } - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - encodeLatch.countDown(); - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - decodeLatch.countDown(); - - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println("end"); - + public void mulTest() { + + Executor pool = Executors.newFixedThreadPool(20); + + CountDownLatch encodeLatch = new CountDownLatch(1); + CountDownLatch decodeLatch = new CountDownLatch(1); + + for (int i = 0; i < 18; i++) { + pool.execute(new Worker(i, encodeLatch, decodeLatch)); + } + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + encodeLatch.countDown(); + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + decodeLatch.countDown(); + + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println("end"); + } - - private static void decode(byte[] message){ - HandshakeMessage handshakeMessage = new HandshakeMessage(null); - handshakeMessage.decode(message); + + private static void decode(byte[] message) { + HandshakeMessage handshakeMessage = new HandshakeMessage(null); + handshakeMessage.decode(message); // System.out.println(ToStringBuilder.reflectionToString(handshakeMessage, ToStringStyle.MULTI_LINE_STYLE)); - } - - public static class Worker implements Runnable{ - - private int i; - private CountDownLatch encodeLatch; - private CountDownLatch decodeLatch; - - public Worker(int i,CountDownLatch encodeLatch,CountDownLatch decodeLatch) { - this.i = i; - this.encodeLatch = encodeLatch; - this.decodeLatch = decodeLatch; - } - - @Override - public void run() { - HandshakeMessage message = new HandshakeMessage(null); - message.clientKey = CipherBox.INSTANCE.randomAESKey(); - message.iv = CipherBox.INSTANCE.randomAESIV(); - message.clientVersion = "1.1.0"+i; - message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; - message.osName = "android"; - message.osVersion = "1.2.1"; - message.timestamp = System.currentTimeMillis(); - - byte[] temp = message.encode(); - System.out.println(i+":wait encode"); - try { - encodeLatch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - long startencode = System.currentTimeMillis(); - byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); - long encodeTime = System.currentTimeMillis() - startencode; - - - - System.out.println(i+":wait decode"); - - try { - decodeLatch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - long startdecode = System.currentTimeMillis(); - byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); - long decodeTime = System.currentTimeMillis() - startdecode; - decode(temp2); - System.out.println(i+":"+encodeTime+","+decodeTime); - - } - - } - + } + + public static class Worker implements Runnable { + + private int i; + private CountDownLatch encodeLatch; + private CountDownLatch decodeLatch; + + public Worker(int i, CountDownLatch encodeLatch, CountDownLatch decodeLatch) { + this.i = i; + this.encodeLatch = encodeLatch; + this.decodeLatch = decodeLatch; + } + + @Override + public void run() { + HandshakeMessage message = new HandshakeMessage(null); + message.clientKey = CipherBox.I.randomAESKey(); + message.iv = CipherBox.I.randomAESIV(); + message.clientVersion = "1.1.0" + i; + message.deviceId = "dscsdcsdcsdcdscsdcdscsdcdscdscds"; + message.osName = "android"; + message.osVersion = "1.2.1"; + message.timestamp = System.currentTimeMillis(); + + byte[] temp = message.encode(); + System.out.println(i + ":wait encode"); + try { + encodeLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long startencode = System.currentTimeMillis(); + byte[] encode = RSAUtils.encryptByPublicKey(temp, dailyPublicKey); + long encodeTime = System.currentTimeMillis() - startencode; + + + System.out.println(i + ":wait decode"); + + try { + decodeLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long startdecode = System.currentTimeMillis(); + byte[] temp2 = RSAUtils.decryptByPrivateKey(encode, dailyPrivateKey); + long decodeTime = System.currentTimeMillis() - startdecode; + decode(temp2); + System.out.println(i + ":" + encodeTime + "," + decodeTime); + + } + + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java index 4e67ed76..806ac033 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java @@ -1,34 +1,40 @@ -package com.mpush.test.gson; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.Map; +package com.mpush.test.gson; +import com.mpush.api.spi.net.DnsMapping; import org.junit.Test; -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import com.google.common.collect.ArrayListMultimap; +import java.net.MalformedURLException; +import java.net.URL; public class DnsMappingTest { - - @Test - public void test(){ - - ArrayListMultimap mappings = ArrayListMultimap.create(); - - String dnsString = "111.1.57.148=127.0.0.1,127.0.0.2;120.27.196.68=127.0.0.1;120.27.198.172=127.0.0.1"; - if (Strings.isNullOrEmpty(dnsString)) return; - Map map = Splitter.on(';').withKeyValueSeparator('=').split(dnsString); - Splitter vsp = Splitter.on(','); - for (Map.Entry entry : map.entrySet()) { - String value = entry.getValue(); - if (Strings.isNullOrEmpty(value)) continue; - Iterable it = vsp.split(entry.getValue()); - mappings.putAll(entry.getKey(), it); - } - - System.out.println(mappings); - - } + @Test + public void test() throws MalformedURLException { + String url = "http://baidu.com:9001/xxx/xxx?s=nc=1"; + DnsMapping mapping = new DnsMapping("127.0.0.1", 8080); + String s = mapping.translate(new URL(url)); + System.out.println(url); + System.out.println(s); + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 7406913d..0a89949e 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -1,64 +1,82 @@ -package com.mpush.test.gson; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.Map; +package com.mpush.test.gson; +import com.google.common.collect.Maps; +import com.mpush.api.push.PushContent; +import com.mpush.api.push.PushContent.PushType; import com.mpush.tools.Jsons; import org.junit.Test; -import com.google.common.collect.Maps; -import com.mpush.api.PushContent; -import com.mpush.api.PushContent.PushType; +import java.util.Map; public class GsonTest { - - @Test - public void test(){ - Map map = Maps.newHashMap(); - map.put("key1", 1121+""); - map.put("key2", "value2"); - - PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); - - - System.out.println(Jsons.toJson(content)); - - } - - @Test - public void test2(){ - ValueMap map = new ValueMap("1122", "value2"); - - PushContent content = PushContent.build(PushType.MESSAGE,Jsons.toJson(map)); - - - System.out.println(Jsons.toJson(content)); - - PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); - - System.out.println(newContetn.getContent()); - - - } - - private static class ValueMap{ - - private String key1; - private String key2; - - public ValueMap(String key1, String key2) { - this.key1 = key1; - this.key2 = key2; - } - - public String getKey1() { - return key1; - } - - public String getKey2() { - return key2; - } - - - } + + @Test + public void test() { + Map map = Maps.newHashMap(); + map.put("key1", 1121 + ""); + map.put("key2", "value2"); + + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + } + + @Test + public void test2() { + ValueMap map = new ValueMap("1122", "value2"); + + PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + + + System.out.println(Jsons.toJson(content)); + + PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); + + System.out.println(newContetn.getContent()); + + + } + + private static class ValueMap { + + private String key1; + private String key2; + + public ValueMap(String key1, String key2) { + this.key1 = key1; + this.key2 = key2; + } + + public String getKey1() { + return key1; + } + + public String getKey2() { + return key2; + } + + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java new file mode 100644 index 00000000..fbbe8199 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -0,0 +1,71 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.push; + +import com.mpush.api.push.PushContent; +import com.mpush.api.push.PushContent.PushType; +import com.mpush.api.push.PushSender; +import com.mpush.api.router.ClientLocation; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; + +import java.util.Arrays; +import java.util.concurrent.locks.LockSupport; + +/** + * Created by ohun on 2016/1/7. + * + * @author ohun@live.cn + */ +public class PushClientTestMain { + public static void main(String[] args) throws Exception { + Logs.init(); + PushSender sender = PushSender.create(); + sender.start().get(); + for (int i = 0; i < 1; i++) { + PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); + content.setMsgId("msgId_" + (i % 2)); + Thread.sleep(1000); + sender.send(Jsons.toJson(content), Arrays.asList("user-0","doctor43test"), new PushSender.Callback() { + @Override + public void onSuccess(String userId, ClientLocation location) { + System.err.println("push onSuccess userId=" + userId); + } + + @Override + public void onFailure(String userId, ClientLocation location) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId, ClientLocation location) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId, ClientLocation location) { + System.err.println("push onTimeout userId=" + userId); + } + }); + } + LockSupport.park(); + } + +} \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java index fe122a9f..69be00b2 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java @@ -1,51 +1,64 @@ -package com.mpush.test.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +package com.mpush.test.redis; -import com.mpush.tools.redis.consistenthash.ConsistentHash; -import com.mpush.tools.redis.consistenthash.Node; +import com.mpush.cache.redis.hash.ConsistentHash; +import com.mpush.cache.redis.hash.Node; import org.junit.Test; - import redis.clients.util.Hashing; import redis.clients.util.MurmurHash; +import java.util.*; + public class ConsistentHashTest { - - private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 - - @Test - public void test(){ - Map map = new HashMap();// 每台真实机器节点上保存的记录条数 - List nodes = new ArrayList();// 真实机器节点 - // 10台真实机器节点集群 - for (int i = 1; i <= 10; i++) { - map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 - Node node = new Node(IP_PREFIX + i, "node" + i); - nodes.add(node); - } - Hashing hashFunction = new MurmurHash(); // hash函数实例 - ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 - // 将5000条记录尽可能均匀的存储到10台机器节点 - for (int i = 0; i < 5000; i++) { - // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 - String data = UUID.randomUUID().toString() + i; - // 通过记录找到真实机器节点 - Node node = consistentHash.get(data); - // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 - // 每台真实机器节点上保存的记录条数加1 - map.put(node.getIp(), map.get(node.getIp()) + 1); - } - // 打印每台真实机器节点保存的记录条数 - for (int i = 1; i <= 10; i++) { - System.out.println(IP_PREFIX + i + "节点记录条数:" - + map.get("192.168.1." + i)); - } - - } + + private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 + + @Test + public void test() { + Map map = new HashMap();// 每台真实机器节点上保存的记录条数 + List nodes = new ArrayList();// 真实机器节点 + // 10台真实机器节点集群 + for (int i = 1; i <= 10; i++) { + map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 + Node node = new Node(IP_PREFIX + i, "node" + i); + nodes.add(node); + } + Hashing hashFunction = new MurmurHash(); // hash函数实例 + ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 + // 将5000条记录尽可能均匀的存储到10台机器节点 + for (int i = 0; i < 5000; i++) { + // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 + String data = UUID.randomUUID().toString() + i; + // 通过记录找到真实机器节点 + Node node = consistentHash.get(data); + // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 + // 每台真实机器节点上保存的记录条数加1 + map.put(node.getIp(), map.get(node.getIp()) + 1); + } + // 打印每台真实机器节点保存的记录条数 + for (int i = 1; i <= 10; i++) { + System.out.println(IP_PREFIX + i + "节点记录条数:" + + map.get("192.168.1." + i)); + } + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java index c52c091c..43a30342 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java @@ -1,13 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; -import com.mpush.tools.MPushUtil; +import com.mpush.tools.Utils; import org.junit.Test; public class MPushUtilTest { - - @Test - public void getIp() throws Exception{ - System.out.println(MPushUtil.getExtranetAddress()); - } + + @Test + public void getIp() throws Exception { + System.out.println(Utils.getExtranetAddress()); + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java index a2913fa5..f3cf52e6 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java @@ -1,59 +1,73 @@ -package com.mpush.test.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.List; -import java.util.concurrent.locks.LockSupport; +package com.mpush.test.redis; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.tools.redis.pubsub.Subscriber; -import com.mpush.tools.redis.RedisRegister; +import com.mpush.cache.redis.RedisGroup; +import com.mpush.cache.redis.RedisServer; +import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.cache.redis.manager.ZKRedisClusterManager; +import com.mpush.cache.redis.mq.Subscriber; import org.junit.Before; import org.junit.Test; -import com.google.common.collect.Lists; -import com.mpush.tools.spi.ServiceContainer; +import java.util.concurrent.locks.LockSupport; public class PubSubTest { - - private RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - + + private ZKRedisClusterManager redisClusterManager = ZKRedisClusterManager.I; + @Before - public void init(){ - RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - RedisGroup group = new RedisGroup(); - group.addRedisNode(node); - List listGroup = Lists.newArrayList(group); - redisRegister.init(listGroup); + public void init() { + RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + RedisGroup group = new RedisGroup(); + group.addRedisNode(node); + redisClusterManager.addGroup(group); + } + + @Test + public void subpubTest() { + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); } - - @Test - public void subpubTest(){ - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - } - - @Test - public void pubsubTest(){ - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - } - - @Test - public void pubTest(){ - RedisManage.publish("/hello/123", "123"); - RedisManage.publish("/hello/124", "124"); - } - - @Test - public void subTest(){ - RedisManage.subscribe(Subscriber.holder, "/hello/123"); - RedisManage.subscribe(Subscriber.holder, "/hello/124"); - LockSupport.park(); - } - + + @Test + public void pubsubTest() { + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + } + + @Test + public void pubTest() { + RedisManager.I.publish("/hello/123", "123"); + RedisManager.I.publish("/hello/124", "124"); + } + + @Test + public void subTest() { + RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); + RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + LockSupport.park(); + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java index 13052f07..7069153a 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java @@ -1,46 +1,63 @@ -package com.mpush.test.redis; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import java.util.Date; -import java.util.HashSet; -import java.util.Set; +package com.mpush.test.redis; -import com.mpush.tools.redis.RedisPoolConfig; +import com.mpush.cache.redis.RedisClient; +import com.mpush.tools.Jsons; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.junit.Before; import org.junit.Test; - -import com.mpush.tools.Jsons; - import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + public class RedisClusterTest { - Set jedisClusterNodes = new HashSet(); - - JedisCluster cluster = null; - - @Before - public void init() { - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); - jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); - cluster = new JedisCluster(jedisClusterNodes, RedisPoolConfig.config); - } - - @Test - public void test() { - - User user = new User("huang", 18, new Date()); - cluster.set("huang", Jsons.toJson(user)); - String ret = cluster.get("huang"); - User newUser = Jsons.fromJson(ret, User.class); - System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); - - } + Set jedisClusterNodes = new HashSet(); + + JedisCluster cluster = null; + + @Before + public void init() { + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); + jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); + cluster = new JedisCluster(jedisClusterNodes, RedisClient.CONFIG); + } + + @Test + public void test() { + + User user = new User("huang", 18, new Date()); + cluster.set("huang", Jsons.toJson(user)); + String ret = cluster.get("huang"); + User newUser = Jsons.fromJson(ret, User.class); + System.out.println(ToStringBuilder.reflectionToString(newUser, ToStringStyle.JSON_STYLE)); + + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java deleted file mode 100644 index 2d376e59..00000000 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisGroupManageTest.java +++ /dev/null @@ -1,109 +0,0 @@ -//package com.mpush.tools.redis; -// -//import java.util.Date; -//import java.util.List; -// -//import MessageListener; -// -//import org.apache.commons.lang3.builder.ToStringBuilder; -//import org.apache.commons.lang3.builder.ToStringStyle; -//import org.junit.Before; -//import org.junit.Test; -// -//import MPushUtil; -//import RedisManage; -//import Subscriber; -//import ServiceContainer; -//import com.mpush.tools.zk.ServerApp; -// -//public class RedisGroupManageTest { -// -// ServerApp app = new ServerApp(MPushUtil.getLocalIp(), 3000); -// List groupList = null; -// -// RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo"); -// RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo"); -// -// RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class); -// -// @Before -// public void init() { -// groupList = redisRegister.getGroupList(); -// } -// -// @Test -// public void testGetRedisGroup() { -// for (RedisGroup group : groupList) { -// for (RedisNode node : group.getRedisNodeList()) { -// System.out.println(group + ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE)); -// } -// -// } -// } -// -// @Test -// public void testAdd() { -// User user = RedisManage.get("huang2", User.class); -// if (user == null) { -// user = new User("hi", 10, new Date()); -// RedisManage.set("huang2", user); -// user = RedisManage.get("huang2", User.class); -// } -// System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); -// -// User nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// -// RedisManage.del("huang2"); -// -// nowUser = RedisUtil.get(node2, "huang2", User.class); -// if (nowUser == null) { -// System.out.println("node2 nowUser is null"); -// } else { -// System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); -// } -// -// nowUser = RedisUtil.get(node, "huang2", User.class); -// System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); -// -// } -// -// @Test -// public void testPub() { -// for (int i = 0; i < 20; i++) { -// User user = new User("pub" + i, 10, new Date()); -// RedisManage.publish("channel1", user); -// RedisManage.publish("channel2", user); -// } -// } -// -// @Test -// public void testSub() { -// RedisManage.subscribe(new MessageListener() { -// @Override -// public void onMessage(String channel, String message) { -// System.out.printf("on message channel=%s, message=%s%n", channel, message); -// } -// }, "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// @Test -// public void testSub2() { -// RedisManage.subscribe(new Subscriber(), "channel1", "channel2"); -// try { -// Thread.sleep(Integer.MAX_VALUE); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } -// -// -//} diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index c8c4a45f..89ef5cb3 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -1,199 +1,215 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.RedisServer; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; +import redis.clients.jedis.Jedis; + import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.junit.Test; +public class RedisUtilTest { -import com.google.common.collect.Lists; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.RedisUtil; -import redis.clients.jedis.Jedis; + RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + + List nodeList = Lists.newArrayList(node); + + @Test + public void testAddAndGetAndDelete() { + Jedis jedis = RedisClient.getClient(node); + jedis.set("hi", "huang"); + + String ret = jedis.get("hi"); + System.out.println(ret); + + jedis.del("hi"); + ret = jedis.get("hi"); + if (ret == null) { + System.out.println("ret is null"); + } else { + System.out.println("ret is not null:" + ret); + } + + } + + @Test + public void testJedisPool() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 0; i < 10; i++) { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + } + } + + @Test + public void testJedisPool2() { + // 最大连接数是8,因此,获取10个链接会抛错误 + List jedisList = Lists.newArrayList(); + for (int i = 1; i <= 8; i++) { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + } + + System.out.println(jedisList.size()); + + try { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + System.out.println("first get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + // 关闭一个链接 + RedisClient.close(jedisList.get(0)); + + try { + Jedis jedis = RedisClient.getClient(node); + jedisList.add(jedis); + System.out.println("second get jedis success"); + } catch (Exception e) { + System.out.println(e); + } + + System.out.println(jedisList.size()); + } + + @Test + public void testKV() { + User user = new User("huang", 18, new Date()); + RedisClient.set(nodeList, "test", user); + + User nowUser = RedisClient.get(node, "test", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisClient.get(node, "test", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + RedisClient.del(nodeList, "test"); + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + RedisClient.set(nodeList, "test", user, 10); + + try { + Thread.sleep(12000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.get(node, "test", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void hashTest() { + + User user = new User("huang", 18, new Date()); + + RedisClient.hset(nodeList, "hashhuang", "hi", user); + + User nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + + Map ret = RedisClient.hgetAll(node, "hashhuang", User.class); + Iterator> iterator = ret.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + User val = entry.getValue(); + System.out.println("all:" + key + "," + ToStringBuilder.reflectionToString(val)); + } + + RedisClient.hdel(nodeList, "hashhuang", "hi"); + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + if (nowUser == null) { + System.out.println("node2 nowUser is null"); + } else { + System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); + } + + nowUser = RedisClient.hget(node, "hashhuang", "hi", User.class); + if (nowUser == null) { + System.out.println("node nowUser is null"); + } else { + System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); + } + + } + + @Test + public void testSet() { +// System.out.println(RedisClient.sCard(node, RedisKey.getUserOnlineKey())); + +// List onlineUserIdList = RedisClient.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); +// System.out.println(onlineUserIdList.size()); -public class RedisUtilTest { + } - - - RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo"); - - List nodeList = Lists.newArrayList(node); - - @Test - public void testAddAndGetAndDelete() { - Jedis jedis = RedisUtil.getClient(node); - jedis.set("hi", "huang"); - - String ret = jedis.get("hi"); - System.out.println(ret); - - jedis.del("hi"); - ret = jedis.get("hi"); - if (ret == null) { - System.out.println("ret is null"); - } else { - System.out.println("ret is not null:" + ret); - } - - } - - @Test - public void testJedisPool() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 0; i < 10; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - } - - @Test - public void testJedisPool2() { - // 最大连接数是8,因此,获取10个链接会抛错误 - List jedisList = Lists.newArrayList(); - for (int i = 1; i <= 8; i++) { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - } - - System.out.println(jedisList.size()); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("first get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - // 关闭一个链接 - RedisUtil.close(jedisList.get(0)); - - try { - Jedis jedis = RedisUtil.getClient(node); - jedisList.add(jedis); - System.out.println("second get jedis success"); - } catch (Exception e) { - System.out.println(e); - } - - System.out.println(jedisList.size()); - } - - @Test - public void testKV() { - User user = new User("huang", 18, new Date()); - RedisUtil.set(nodeList, "test", user); - - User nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.get(node, "test", User.class); - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - - RedisUtil.del(nodeList, "test"); - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - RedisUtil.set(nodeList, "test", user, 10); - - try { - Thread.sleep(12000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node2 nowUser is null"); - } else { - System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.get(node, "test", User.class); - if (nowUser == null) { - System.out.println("node nowUser is null"); - } else { - System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void hashTest(){ - - User user = new User("huang", 18, new Date()); - - RedisUtil.hset(nodeList, "hashhuang", "hi", user); - - User nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node1:"+ToStringBuilder.reflectionToString(nowUser)); - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - - Map ret = RedisUtil.hgetAll(node, "hashhuang",User.class); - Iterator> iterator = ret.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String key = entry.getKey(); - User val = entry.getValue(); - System.out.println("all:"+key+","+ToStringBuilder.reflectionToString(val)); - } - - RedisUtil.hdel(nodeList, "hashhuang", "hi"); - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node2 nowUser is null"); - }else{ - System.out.println("node2:"+ToStringBuilder.reflectionToString(nowUser)); - } - - nowUser = RedisUtil.hget(node, "hashhuang", "hi", User.class); - if(nowUser==null){ - System.out.println("node nowUser is null"); - }else{ - System.out.println("node:"+ToStringBuilder.reflectionToString(nowUser)); - } - - } - - @Test - public void testSet(){ -// System.out.println(RedisUtil.sCard(node, RedisKey.getUserOnlineKey())); - -// List onlineUserIdList = RedisUtil.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); -// System.out.println(onlineUserIdList.size()); - - } - - @Test - public void testlist(){ -// RedisUtil.del(nodeList, RedisKey.getUserOfflineKey()); - } - - @Test - public void testsortedset(){ -// RedisUtil.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); - -// long len =RedisUtil.zCard(node, RedisKey.getUserOnlineKey()); + @Test + public void testlist() { +// RedisClient.del(nodeList, RedisKey.getUserOfflineKey()); + } + + @Test + public void testsortedset() { +// RedisClient.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); + +// long len =RedisClient.zCard(node, RedisKey.getUserOnlineKey()); // System.out.println(len); - } + } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/User.java b/mpush-test/src/test/java/com/mpush/test/redis/User.java index 180ebf1e..f9279eee 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/User.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/User.java @@ -1,40 +1,64 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.redis; import java.io.Serializable; import java.util.Date; -public class User implements Serializable{ - - private static final long serialVersionUID = -6269129553435313118L; - - private String name; - private int age; - private Date date; - - public User(String name, int age, Date date) { - this.name = name; - this.age = age; - this.date = date; - } - - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public int getAge() { - return age; - } - public void setAge(int age) { - this.age = age; - } - public Date getDate() { - return date; - } - public void setDate(Date date) { - this.date = date; - } - +public class User implements Serializable { + + private static final long serialVersionUID = -6269129553435313118L; + + private String name; + private int age; + private Date date; + + public User(String name, int age, Date date) { + this.name = name; + this.age = age; + this.date = date; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + } diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java new file mode 100644 index 00000000..199de67d --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -0,0 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.test.sever; + +import com.mpush.bootstrap.Main; + +/** + * Created by yxx on 2016/5/16. + * + * @author ohun@live.cn + */ +public class ServerTestMain { + public static void main(String[] args) { + Main.main(args); + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java index 5965f67d..5e3433de 100644 --- a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java @@ -1,31 +1,50 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.test.util; +import com.mpush.tools.Utils; +import org.junit.Test; + import java.net.URI; import java.net.URISyntaxException; -import com.mpush.tools.MPushUtil; -import org.junit.Test; - public class TelnetTest { - @Test - public void test(){ - boolean ret = MPushUtil.telnet("120.27.196.68", 82); - System.out.println(ret); - } - - @Test - public void test2(){ - boolean ret = MPushUtil.telnet("120.27.196.68", 80); - System.out.println(ret); - } - - @Test - public void uriTest() throws URISyntaxException{ - String url = "http://127.0.0.1"; - URI uri = new URI(url); - System.out.println(uri.getPort()); - } - - + @Test + public void test() { + boolean ret = Utils.checkHealth("120.27.196.68", 82); + System.out.println(ret); + } + + @Test + public void test2() { + boolean ret = Utils.checkHealth("120.27.196.68", 80); + System.out.println(ret); + } + + @Test + public void uriTest() throws URISyntaxException { + String url = "http://127.0.0.1"; + URI uri = new URI(url); + System.out.println(uri.getPort()); + } + + } diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf new file mode 100644 index 00000000..4c50b7c1 --- /dev/null +++ b/mpush-test/src/test/resources/application.conf @@ -0,0 +1,6 @@ +mp.log.dir=${user.dir}/mpush-test/target/logs +mp.log.level=debug +mp.zk.namespace=mpush +mp.zk.server-address="111.1.57.148:5666" +mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} +mp.core.compress-threshold=10k \ No newline at end of file diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index ea3f95d0..0d67de0e 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - + + System.out + UTF-8 + DEBUG - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - debug - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + debug + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory b/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory new file mode 100644 index 00000000..93607f48 --- /dev/null +++ b/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory @@ -0,0 +1 @@ +com.mpush.client.push.PushClientFactory diff --git a/mpush-tools/.gitignore b/mpush-tools/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-tools/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 218927c0..31ae2c40 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -14,7 +14,24 @@ ${mpush-tools-version} jar mpush-tools - + + + + src/main/resources + + **/* + + false + + + ../conf + + reference.conf + + false + + + com.google.code.gson @@ -28,54 +45,37 @@ org.apache.commons commons-lang3 - - redis.clients - jedis + org.aeonbits.owner + owner + + + com.typesafe + config + + + ${mpush.groupId} + mpush-api org.slf4j slf4j-api - ${mpush.groupId} - mpush-log + org.slf4j + jcl-over-slf4j - org.aeonbits.owner - owner + ch.qos.logback + logback-classic - commons-net - commons-net + commons-logging + commons-logging + + + log4j + log4j - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.3 - - - package - - shade - - - true - true - - - com.shinemo.mpush:mpush-tools - - - - - - - - diff --git a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java b/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java deleted file mode 100644 index 27497d15..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/ConsoleLog.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.mpush.tools; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * Created by yxx on 2016/5/15. - * - * @author ohun@live.cn - */ -public final class ConsoleLog { - private static final String TAG = "[mpush] "; - private static final DateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); - - public static void d(String s, Object... args) { - System.out.printf(format.format(new Date()) + " [D] " + TAG + s + '\n', args); - } - - public static void i(String s, Object... args) { - System.out.printf(format.format(new Date()) + " [I] " + TAG + s + '\n', args); - } - - public static void w(String s, Object... args) { - System.err.printf(format.format(new Date()) + " [W] " + TAG + s + '\n', args); - } - - public static void e(Throwable e, String s, Object... args) { - System.err.printf(format.format(new Date()) + " [E] " + TAG + s + '\n', args); - e.printStackTrace(); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Constants.java b/mpush-tools/src/main/java/com/mpush/tools/Constants.java deleted file mode 100644 index 458c6068..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/Constants.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.mpush.tools; - -import java.nio.charset.Charset; - -/** - * Created by ohun on 2015/12/25. - * - * @author ohun@live.cn - */ -public interface Constants { - Charset UTF_8 = Charset.forName("UTF-8"); - byte[] EMPTY_BYTES = new byte[0]; - - int MIN_BOSS_POOL_SIZE = 50; - int MAX_BOSS_POOL_SIZE = 100; - int BOSS_THREAD_QUEUE_SIZE = 10000; - - int MIN_WORK_POOL_SIZE = 50; - int MAX_WORK_POOL_SIZE = 150; - int WORK_THREAD_QUEUE_SIZE = 10000; - - int BIZ_POOL_SIZE = 20; - - int EVENT_BUS_POOL_SIZE = 10; - - int REDIS_POOL_SIZE = 3; - int REDIS_THREAD_QUEUE_SIZE = 10000; - - //redis - int REDIS_TIMEOUT = 2000; - int REDIS_MAX_TOTAL = 8; - int REDIS_MAX_IDLE = 4; - int REDIS_MIN_IDLE = 1; - int REDIS_MAX_WAIT_MILLIS = 5000; - int REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 300000; - int REDIS_NUM_TESTS_PER_EVICTION_RUN = 3; - int REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; - boolean REDIS_TEST_ON_BORROW = false; - boolean REDIS_TEST_ON_RETURN = false; - boolean REDIS_TEST_WHILE_IDLE = false; -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java deleted file mode 100644 index 342c312a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/GenericsUtil.java +++ /dev/null @@ -1,139 +0,0 @@ -package com.mpush.tools; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - - -public class GenericsUtil { - - /** - * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * - * @param clazz clazz 需要反射的类,该类必须继承范型父类 - * @param index 泛型参数所在索引,从0开始. - * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz, int index) { - Type genType = clazz.getGenericSuperclass();//得到泛型父类 - //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class - - if (!(genType instanceof ParameterizedType)) { - return Object.class; - } - //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 - Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); - if (index >= params.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - if (!(params[index] instanceof Class)) { - return Object.class; - } - return (Class) params[index]; - } - /** - * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * - * @param clazz clazz 需要反射的类,该类必须继承泛型父类 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz) { - return getSuperClassGenericType(clazz, 0); - } - /** - * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} - * - * @param method 方法 - * @param index 泛型参数所在索引,从0开始. - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method, int index) { - Type returnType = method.getGenericReturnType(); - if(returnType instanceof ParameterizedType){ - ParameterizedType type = (ParameterizedType) returnType; - Type[] typeArguments = type.getActualTypeArguments(); - if (index >= typeArguments.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class)typeArguments[index]; - } - return Object.class; - } - /** - * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} - * - * @param method 方法 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method) { - return getMethodGenericReturnType(method, 0); - } - - /** - * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 - * @param index 第几个输入参数 - * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method, int index) { - List> results = new ArrayList>(); - Type[] genericParameterTypes = method.getGenericParameterTypes(); - if (index >= genericParameterTypes.length ||index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - Type genericParameterType = genericParameterTypes[index]; - if(genericParameterType instanceof ParameterizedType){ - ParameterizedType aType = (ParameterizedType) genericParameterType; - Type[] parameterArgTypes = aType.getActualTypeArguments(); - for(Type parameterArgType : parameterArgTypes){ - Class parameterArgClass = (Class) parameterArgType; - results.add(parameterArgClass); - } - return results; - } - return results; - } - /** - * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 - * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method) { - return getMethodGenericParameterTypes(method, 0); - } - /** - * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @param index 泛型参数所在索引,从0开始. - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field, int index) { - Type genericFieldType = field.getGenericType(); - - if(genericFieldType instanceof ParameterizedType){ - ParameterizedType aType = (ParameterizedType) genericFieldType; - Type[] fieldArgTypes = aType.getActualTypeArguments(); - if (index >= fieldArgTypes.length || index < 0) { - throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class)fieldArgTypes[index]; - } - return Object.class; - } - /** - * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field) { - return getFieldGenericType(field, 0); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java deleted file mode 100644 index 1bffb0b0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/JVMUtil.java +++ /dev/null @@ -1,136 +0,0 @@ -package com.mpush.tools; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.lang.management.ManagementFactory; -import java.security.AccessController; -import java.security.PrivilegedExceptionAction; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Executors; - -import javax.management.MBeanServer; -import javax.management.ObjectName; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.sun.management.HotSpotDiagnosticMXBean; - -public class JVMUtil { - - private static final Logger log = LoggerFactory.getLogger(JVMUtil.class); - - private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; - private static volatile HotSpotDiagnosticMXBean hotspotMBean; - - private static Object lock = new Object(); - - public static void jstack(OutputStream stream) throws Exception { - try { - Map map = Thread.getAllStackTraces(); - Iterator> ite = map.entrySet().iterator(); - while (ite.hasNext()) { - Map.Entry entry = ite.next(); - StackTraceElement[] elements = entry.getValue(); - if (elements != null && elements.length > 0) { - String threadName = entry.getKey().getName(); - stream.write(("Thread Name :[" + threadName + "]\n").getBytes()); - for (StackTraceElement el : elements) { - String stack = el.toString() + "\n"; - stream.write(stack.getBytes()); - } - stream.write("\n".getBytes()); - } - } - } catch (Exception e) { - throw e; - } - } - - public static void dumpJstack(final String jvmPath){ - Executors.newSingleThreadExecutor().execute(new Runnable() { - @Override - public void run() { - String logPath = jvmPath; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis()+"-jstack.log")); - JVMUtil.jstack(jstackStream); - } catch (Throwable t) { - log.error("Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } - } - } - } - }); - } - - private static HotSpotDiagnosticMXBean getHotspotMBean() { - try { - return AccessController.doPrivileged(new PrivilegedExceptionAction() { - public HotSpotDiagnosticMXBean run() throws Exception { - MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - Set s = server.queryNames(new ObjectName(HOTSPOT_BEAN_NAME), null); - Iterator itr = s.iterator(); - if (itr.hasNext()) { - ObjectName name = itr.next(); - HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, - name.toString(), HotSpotDiagnosticMXBean.class); - return bean; - } else { - return null; - } - } - }); - } catch (Exception e) { - log.error("getHotspotMBean Error!", e); - return null; - } - } - - private static void initHotspotMBean() throws Exception { - if (hotspotMBean == null) { - synchronized (lock) { - if (hotspotMBean == null) { - hotspotMBean = getHotspotMBean(); - } - } - } - } - - public static void jMap(String fileName, boolean live) { - File f = new File(fileName, System.currentTimeMillis()+"-jmap.log"); - String currentFileName = f.getPath(); - try { - initHotspotMBean(); - if (f.exists()) { - f.delete(); - } - - hotspotMBean.dumpHeap(currentFileName, live); - } catch (Exception e) { - log.error("dumpHeap Error!"+currentFileName, e); - } - } - - public static void dumpJmap(final String jvmPath){ - - Executors.newSingleThreadExecutor().execute(new Runnable() { - @Override - public void run() { - jMap(jvmPath, false); - } - }); - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java index 5b7e726e..b5963e34 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -1,8 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; import com.google.gson.Gson; import com.google.gson.GsonBuilder; - +import com.mpush.api.Constants; +import com.mpush.tools.common.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,10 +118,4 @@ private static void append(Map.Entry entry, StringBuilder sb) { sb.append(':'); sb.append('"').append(value).append('"'); } - - public static void main(String[] args) { - String tet = "7"; - Long l = Jsons.fromJson(tet, Long.class); - System.out.println(l); - } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/Pair.java deleted file mode 100644 index 23019945..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/Pair.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.mpush.tools; - -/** - * Created by ohun on 2015/12/24. - * - * @author ohun@live.cn - */ -public final class Pair { - public final K key; - public final V value; - - public Pair(K key, V value) { - this.key = key; - this.value = value; - } - - public K first() { - return key; - } - - public V second() { - return value; - } - - public static Pair of(K k, V v) { - return new Pair<>(k, v); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java similarity index 80% rename from mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java rename to mpush-tools/src/main/java/com/mpush/tools/Utils.java index 5e846c06..6a041dfd 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/MPushUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -1,13 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; -import com.mpush.tools.config.ConfigCenter; -import org.apache.commons.net.telnet.TelnetClient; +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.NetworkInterface; +import java.net.Socket; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -18,8 +38,8 @@ * * @author ohun@live.cn */ -public final class MPushUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(MPushUtil.class); +public final class Utils { + private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); private static String LOCAL_IP; @@ -42,13 +62,6 @@ public static String getLocalIp() { return LOCAL_IP; } - public static int getHeartbeat(int min, int max) { - return Math.max( - ConfigCenter.I.minHeartbeat(), - Math.min(max, ConfigCenter.I.maxHeartbeat()) - ); - } - /** * 获取本机ip * 只获取第一块网卡绑定的ip地址 @@ -105,20 +118,7 @@ public static String getExtranetAddress() { } catch (Throwable e) { LOGGER.error("getExtranetAddress exception", e); } - - - String localIp = getInetAddress(); - String remoteIp = null; - Map mapping = ConfigCenter.I.remoteIpMapping(); - if (mapping != null) { - remoteIp = mapping.get(localIp); - } - - if (remoteIp == null) { - remoteIp = localIp; - } - - return remoteIp; + return null; } public static String headerToString(Map headers) { @@ -157,21 +157,14 @@ public static Map headerFromString(String headersString) { return headers; } - public static boolean telnet(String ip, int port) { - TelnetClient client = new TelnetClient(); + public static boolean checkHealth(String ip, int port) { try { - client.connect(ip, port); + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(ip, port), 1000); + socket.close(); return true; - } catch (Exception e) { + } catch (IOException e) { return false; - } finally { - try { - if (client.isConnected()) { - client.disconnect(); - } - } catch (IOException e) { - LOGGER.error("disconnect error", e); - } } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java new file mode 100644 index 00000000..f8db214d --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java @@ -0,0 +1,164 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + + +public class GenericsUtil { + + /** + * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * + * @param clazz clazz 需要反射的类,该类必须继承范型父类 + * @param index 泛型参数所在索引,从0开始. + * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getSuperClassGenericType(Class clazz, int index) { + Type genType = clazz.getGenericSuperclass();//得到泛型父类 + //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class + + if (!(genType instanceof ParameterizedType)) { + return Object.class; + } + //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 + Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); + if (index >= params.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + if (!(params[index] instanceof Class)) { + return Object.class; + } + return (Class) params[index]; + } + + /** + * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport + * + * @param clazz clazz 需要反射的类,该类必须继承泛型父类 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getSuperClassGenericType(Class clazz) { + return getSuperClassGenericType(clazz, 0); + } + + /** + * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} + * + * @param method 方法 + * @param index 泛型参数所在索引,从0开始. + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getMethodGenericReturnType(Method method, int index) { + Type returnType = method.getGenericReturnType(); + if (returnType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) returnType; + Type[] typeArguments = type.getActualTypeArguments(); + if (index >= typeArguments.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class) typeArguments[index]; + } + return Object.class; + } + + /** + * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} + * + * @param method 方法 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getMethodGenericReturnType(Method method) { + return getMethodGenericReturnType(method, 0); + } + + /** + * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} + * + * @param method 方法 + * @param index 第几个输入参数 + * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 + */ + public static List> getMethodGenericParameterTypes(Method method, int index) { + List> results = new ArrayList>(); + Type[] genericParameterTypes = method.getGenericParameterTypes(); + if (index >= genericParameterTypes.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + Type genericParameterType = genericParameterTypes[index]; + if (genericParameterType instanceof ParameterizedType) { + ParameterizedType aType = (ParameterizedType) genericParameterType; + Type[] parameterArgTypes = aType.getActualTypeArguments(); + for (Type parameterArgType : parameterArgTypes) { + Class parameterArgClass = (Class) parameterArgType; + results.add(parameterArgClass); + } + return results; + } + return results; + } + + /** + * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} + * + * @param method 方法 + * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 + */ + public static List> getMethodGenericParameterTypes(Method method) { + return getMethodGenericParameterTypes(method, 0); + } + + /** + * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; + * + * @param field 字段 + * @param index 泛型参数所在索引,从0开始. + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getFieldGenericType(Field field, int index) { + Type genericFieldType = field.getGenericType(); + + if (genericFieldType instanceof ParameterizedType) { + ParameterizedType aType = (ParameterizedType) genericFieldType; + Type[] fieldArgTypes = aType.getActualTypeArguments(); + if (index >= fieldArgTypes.length || index < 0) { + throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); + } + return (Class) fieldArgTypes[index]; + } + return Object.class; + } + + /** + * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; + * + * @param field 字段 + * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class + */ + public static Class getFieldGenericType(Field field) { + return getFieldGenericType(field, 0); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java similarity index 66% rename from mpush-tools/src/main/java/com/mpush/tools/IOUtils.java rename to mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java index f322e8f8..da840eec 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java @@ -1,5 +1,25 @@ -package com.mpush.tools; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; +import com.mpush.api.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,9 +49,9 @@ public static void close(Closeable closeable) { } public static byte[] compress(byte[] data) { - - Profiler.enter("start compress"); - + + Profiler.enter("start compress"); + ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); DeflaterOutputStream zipOut = new DeflaterOutputStream(out); try { @@ -48,8 +68,8 @@ public static byte[] compress(byte[] data) { return out.toByteArray(); } - public static byte[] uncompress(byte[] data) { - Profiler.enter("start uncompress"); + public static byte[] decompress(byte[] data) { + Profiler.enter("start decompress"); InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4); byte[] buffer = new byte[1024]; @@ -59,7 +79,7 @@ public static byte[] uncompress(byte[] data) { out.write(buffer, 0, length); } } catch (IOException e) { - LOGGER.error("uncompress ex", e); + LOGGER.error("decompress ex", e); return Constants.EMPTY_BYTES; } finally { close(zipIn); diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java new file mode 100644 index 00000000..34159ba4 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -0,0 +1,187 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +import com.sun.management.HotSpotDiagnosticMXBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.management.MBeanServer; +import javax.management.ObjectName; +import java.io.*; +import java.lang.management.*; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class JVMUtil { + private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; + + private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class); + + private static HotSpotDiagnosticMXBean hotSpotMXBean; + private static ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + + public static void jstack(OutputStream stream) throws Exception { + PrintStream out = new PrintStream(stream); + boolean cpuTimeEnabled = threadMXBean.isThreadCpuTimeSupported() && threadMXBean.isThreadCpuTimeEnabled(); + Map map = Thread.getAllStackTraces(); + + for (Map.Entry entry : map.entrySet()) { + Thread t = entry.getKey(); + StackTraceElement[] elements = entry.getValue(); + + ThreadInfo tt = threadMXBean.getThreadInfo(t.getId()); + long tid = t.getId(); + Thread.State state = t.getState(); + long cpuTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadCpuTime(tid) / 1000000 : -1; + long userTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadUserTime(tid) / 1000000 : -1; + + out.printf("%s id=%d state=%s deamon=%s priority=%s cpu[total=%sms,user=%sms]", t.getName(), + tid, t.getState(), t.isDaemon(), t.getPriority(), cpuTimeMillis, userTimeMillis); + final LockInfo lock = tt.getLockInfo(); + if (lock != null && state != Thread.State.BLOCKED) { + out.printf("%n - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()); + out.printf("%n - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()); + } else if (lock != null && state == Thread.State.BLOCKED) { + out.printf("%n - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), + lock.getClassName()); + } + + if (tt.isSuspended()) { + out.print(" (suspended)"); + } + + if (tt.isInNative()) { + out.print(" (running in native)"); + } + + out.println(); + if (tt.getLockOwnerName() != null) { + out.printf(" owned by %s id=%d%n", tt.getLockOwnerName(), tt.getLockOwnerId()); + } + + final MonitorInfo[] monitors = tt.getLockedMonitors(); + + for (int i = 0; i < elements.length; i++) { + final StackTraceElement element = elements[i]; + out.printf(" at %s%n", element); + for (int j = 1; j < monitors.length; j++) { + final MonitorInfo monitor = monitors[j]; + if (monitor.getLockedStackDepth() == i) { + out.printf(" - locked %s%n", monitor); + } + } + } + + out.println(); + + final LockInfo[] locks = tt.getLockedSynchronizers(); + if (locks.length > 0) { + out.printf(" Locked synchronizers: count = %d%n", locks.length); + for (LockInfo l : locks) { + out.printf(" - %s%n", l); + } + out.println(); + } + } + } + + public static void dumpJstack(final String jvmPath) { + new Thread((new Runnable() { + @Override + public void run() { + String logPath = jvmPath; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); + JVMUtil.jstack(jstackStream); + } catch (Throwable t) { + LOGGER.error("Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { + } + } + } + } + })).start(); + } + + private static HotSpotDiagnosticMXBean getHotSpotMXBean() { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + public HotSpotDiagnosticMXBean run() throws Exception { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + Set s = server.queryNames(new ObjectName(HOT_SPOT_BEAN_NAME), null); + Iterator itr = s.iterator(); + if (itr.hasNext()) { + ObjectName name = itr.next(); + HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, + name.toString(), HotSpotDiagnosticMXBean.class); + return bean; + } else { + return null; + } + } + }); + } catch (Exception e) { + LOGGER.error("getHotSpotMXBean Error!", e); + return null; + } + } + + private static void initHotSpotMBean() throws Exception { + if (hotSpotMXBean == null) { + synchronized (JVMUtil.class) { + if (hotSpotMXBean == null) { + hotSpotMXBean = getHotSpotMXBean(); + } + } + } + } + + public static void jMap(String fileName, boolean live) { + File f = new File(fileName, System.currentTimeMillis() + "-jmap.LOGGER"); + String currentFileName = f.getPath(); + try { + initHotSpotMBean(); + if (f.exists()) { + f.delete(); + } + hotSpotMXBean.dumpHeap(currentFileName, live); + } catch (Exception e) { + LOGGER.error("dumpHeap Error!" + currentFileName, e); + } + } + + public static void dumpJmap(final String jvmPath) { + new Thread(new Runnable() { + @Override + public void run() { + jMap(jvmPath, false); + } + }).start(); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java b/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java new file mode 100644 index 00000000..f32cdb9f --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Pair.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +/** + * Created by ohun on 2015/12/24. + * + * @author ohun@live.cn + */ +public final class Pair { + public final K key; + public final V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K first() { + return key; + } + + public V second() { + return value; + } + + public static Pair of(K k, V v) { + return new Pair<>(k, v); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java similarity index 93% rename from mpush-tools/src/main/java/com/mpush/tools/Profiler.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java index d4a3d1fb..0d8f29c2 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java @@ -1,20 +1,39 @@ -package com.mpush.tools; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +import org.apache.commons.lang3.StringUtils; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.apache.commons.lang3.StringUtils; - /** * 用来测试并统计线程执行时间的工具。 */ -@SuppressWarnings(value={"rawtypes","unchecked"}) +@SuppressWarnings(value = {"rawtypes", "unchecked"}) public class Profiler { - private static final ThreadLocal entryStack = new ThreadLocal(); + private static final ThreadLocal entryStack = new ThreadLocal(); public static final String EMPTY_STRING = ""; /** @@ -30,7 +49,7 @@ public static void start() { * @param message 第一个entry的信息 */ - public static void start(String message) { + public static void start(String message) { entryStack.set(new Entry(message, null, null)); } @@ -193,7 +212,7 @@ private Entry(Object message, Entry parentEntry, Entry firstEntry) { this.message = message; this.startTime = System.currentTimeMillis(); this.parentEntry = parentEntry; - this.firstEntry = (Entry) firstEntry == null ? this : firstEntry; + this.firstEntry = firstEntry == null ? this : firstEntry; this.baseTime = (firstEntry == null) ? 0 : firstEntry.startTime; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Strings.java b/mpush-tools/src/main/java/com/mpush/tools/common/Strings.java similarity index 52% rename from mpush-tools/src/main/java/com/mpush/tools/Strings.java rename to mpush-tools/src/main/java/com/mpush/tools/common/Strings.java index c81fc531..c7e0dac4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Strings.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Strings.java @@ -1,4 +1,23 @@ -package com.mpush.tools; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; /** * Created by ohun on 2015/12/23. diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java new file mode 100644 index 00000000..112cf893 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java @@ -0,0 +1,94 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.common; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by yxx on 2016/5/26. + * + * @author ohun@live.cn (夜色) + */ +public final class TimeLine { + private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + private final TimePoint root = new TimePoint("root"); + private final String name; + private TimePoint current = root; + + public TimeLine() { + name = "TimeLine"; + } + + public TimeLine(String name) { + this.name = name; + } + + public void begin() { + addTimePoint("begin"); + } + + public void addTimePoint(String name) { + current = current.next = new TimePoint(name); + } + + public void end() { + addTimePoint("end"); + } + + public void clean() { + root.next = null; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(name); + if (root.next != null) { + sb.append('[').append(current.point - root.next.point).append(']'); + } + sb.append('{'); + TimePoint next = root; + while ((next = next.next) != null) { + sb.append(next.toString()); + } + sb.append('}'); + return sb.toString(); + } + + private static class TimePoint { + private final String name; + private final long point = System.currentTimeMillis(); + private TimePoint next; + + public TimePoint(String name) { + this.name = name; + } + + public void setNext(TimePoint next) { + this.next = next; + } + + @Override + public String toString() { + String header = name + "[" + formatter.format(new Date(point)) + "]"; + if (next == null) return header; + return header + " --" + (next.point - point) + "(ms)--> "; + } + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java new file mode 100644 index 00000000..2f597c11 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -0,0 +1,345 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config; + +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.tools.config.data.RedisGroup; +import com.mpush.tools.config.data.RedisServer; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigList; +import com.typesafe.config.ConfigObject; + +import java.io.File; +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static com.typesafe.config.ConfigBeanFactory.create; +import static java.util.stream.Collectors.toCollection; + +/** + * Created by yxx on 2016/5/20. + * + * @author ohun@live.cn + */ +public interface CC { + Config cfg = load(); + + static Config load() { + Config config = ConfigFactory.load(); + String custom_conf = "mp.conf"; + if (config.hasPath(custom_conf)) { + File file = new File(config.getString(custom_conf)); + if (file.exists()) { + Config custom = ConfigFactory.parseFile(file); + config = custom.withFallback(config); + } + } + return config; + } + + interface mp { + Config cfg = CC.cfg.getObject("mp").toConfig(); + String log_dir = cfg.getString("log.dir"); + String log_level = cfg.getString("log.level"); + + interface core { + Config cfg = mp.cfg.getObject("core").toConfig(); + + int session_expired_time = (int) cfg.getDuration("session-expired-time").getSeconds(); + + int max_heartbeat = (int) cfg.getDuration("max-heartbeat", TimeUnit.MILLISECONDS); + + int max_packet_size = (int) cfg.getMemorySize("max-packet-size").toBytes(); + + int min_heartbeat = (int) cfg.getDuration("min-heartbeat", TimeUnit.MILLISECONDS); + + long compress_threshold = cfg.getBytes("compress-threshold"); + + int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times"); + + String epoll_provider = cfg.getString("epoll-provider"); + } + + interface net { + Config cfg = mp.cfg.getObject("net").toConfig(); + + int connect_server_port = cfg.getInt("connect-server-port"); + int gateway_server_port = cfg.getInt("gateway-server-port"); + int admin_server_port = cfg.getInt("admin-server-port"); + + interface public_ip_mapping { + + Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); + + static String getString(String localIp) { + return (String) mappings.getOrDefault(localIp, localIp); + } + + } + + interface traffic_shaping { + Config cfg = net.cfg.getObject("traffic-shaping").toConfig(); + + interface gateway_client { + Config cfg = traffic_shaping.cfg.getObject("gateway-client").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + + interface gateway_server { + Config cfg = traffic_shaping.cfg.getObject("gateway-server").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + + interface connect_server { + Config cfg = traffic_shaping.cfg.getObject("connect-server").toConfig(); + boolean enabled = cfg.getBoolean("enabled"); + long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS); + long write_global_limit = cfg.getBytes("write-global-limit"); + long read_global_limit = cfg.getBytes("read-global-limit"); + long write_channel_limit = cfg.getBytes("write-channel-limit"); + long read_channel_limit = cfg.getBytes("read-channel-limit"); + } + } + } + + interface security { + + Config cfg = mp.cfg.getObject("security").toConfig(); + + int aes_key_length = cfg.getInt("aes-key-length"); + + String public_key = cfg.getString("public-key"); + + String private_key = cfg.getString("private-key"); + + int ras_key_length = cfg.getInt("ras-key-length"); + + } + + interface thread { + + Config cfg = mp.cfg.getObject("thread").toConfig(); + + interface pool { + + Config cfg = thread.cfg.getObject("pool").toConfig(); + + interface boss { + Config cfg = pool.cfg.getObject("boss").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface work { + Config cfg = pool.cfg.getObject("work").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface event_bus { + Config cfg = pool.cfg.getObject("event-bus").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface http_proxy { + Config cfg = pool.cfg.getObject("http-proxy").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface biz { + Config cfg = pool.cfg.getObject("biz").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface mq { + Config cfg = pool.cfg.getObject("mq").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + + } + + interface push_callback { + Config cfg = pool.cfg.getObject("push-callback").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + } + + } + + interface zk { + + Config cfg = mp.cfg.getObject("zk").toConfig(); + + int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS); + + String local_cache_path = cfg.getString("local-cache-path"); + + int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS); + + String namespace = cfg.getString("namespace"); + + String digest = cfg.getString("digest"); + + String server_address = cfg.getString("server-address"); + + interface retry { + + Config cfg = zk.cfg.getObject("retry").toConfig(); + + int maxRetries = cfg.getInt("maxRetries"); + + int baseSleepTimeMs = (int) cfg.getDuration("baseSleepTimeMs", TimeUnit.MILLISECONDS); + + int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS); + } + + } + + interface redis { + Config cfg = mp.cfg.getObject("redis").toConfig(); + + boolean write_to_zk = cfg.getBoolean("write-to-zk"); + + List cluster_group = cfg.getList("cluster-group") + .stream() + .map(v -> new RedisGroup(ConfigList.class.cast(v) + .stream() + .map(cv -> create(ConfigObject.class.cast(cv).toConfig(), RedisServer.class)) + .collect(toCollection(ArrayList::new)) + ) + ) + .collect(toCollection(ArrayList::new)); + + interface config { + + Config cfg = redis.cfg.getObject("config").toConfig(); + + boolean jmxEnabled = cfg.getBoolean("jmxEnabled"); + + int minIdle = cfg.getInt("minIdle"); + + boolean testOnReturn = cfg.getBoolean("testOnReturn"); + + long softMinEvictableIdleTimeMillis = cfg.getDuration("softMinEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); + + boolean testOnBorrow = cfg.getBoolean("testOnBorrow"); + + boolean testWhileIdle = cfg.getBoolean("testWhileIdle"); + + long maxWaitMillis = cfg.getDuration("maxWaitMillis", TimeUnit.MILLISECONDS); + + String jmxNameBase = cfg.getString("jmxNameBase"); + + int numTestsPerEvictionRun = (int) cfg.getDuration("numTestsPerEvictionRun", TimeUnit.MILLISECONDS); + + String jmxNamePrefix = cfg.getString("jmxNamePrefix"); + + long minEvictableIdleTimeMillis = cfg.getDuration("minEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); + + boolean blockWhenExhausted = cfg.getBoolean("blockWhenExhausted"); + + boolean fairness = cfg.getBoolean("fairness"); + + long timeBetweenEvictionRunsMillis = cfg.getDuration("timeBetweenEvictionRunsMillis", TimeUnit.MILLISECONDS); + + boolean testOnCreate = cfg.getBoolean("testOnCreate"); + + int maxIdle = cfg.getInt("maxIdle"); + + boolean lifo = cfg.getBoolean("lifo"); + + int maxTotal = cfg.getInt("maxTotal"); + + } + + } + + interface http { + + Config cfg = mp.cfg.getObject("http").toConfig(); + boolean proxy_enabled = cfg.getBoolean("proxy-enabled"); + int default_read_timeout = (int) cfg.getDuration("default-read-timeout", TimeUnit.MILLISECONDS); + int max_conn_per_host = cfg.getInt("max-conn-per-host"); + + + long max_content_length = cfg.getBytes("max-content-length"); + + Map> dns_mapping = loadMapping(); + + static Map> loadMapping() { + Map> map = new HashMap<>(); + cfg.getObject("dns-mapping").forEach((s, v) -> + map.put(s, ConfigList.class.cast(v) + .stream() + .map(cv -> DnsMapping.parse((String) cv.unwrapped())) + .collect(toCollection(ArrayList::new)) + ) + ); + return map; + } + + } + + interface monitor { + Config cfg = mp.cfg.getObject("monitor").toConfig(); + String dump_dir = cfg.getString("dump-dir"); + boolean dump_stack = cfg.getBoolean("dump-stack"); + boolean print_log = cfg.getBoolean("print-log"); + Duration dump_period = cfg.getDuration("dump-period"); + } + + interface spi { + Config cfg = mp.cfg.getObject("spi").toConfig(); + String thread_pool_factory = cfg.getString("thread-pool-factory"); + String dns_mapping_manager = cfg.getString("dns-mapping-manager"); + } + } +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java deleted file mode 100644 index 29d7588a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigCenter.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.mpush.tools.config; - - -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.dns.DnsMapping; -import org.aeonbits.owner.Config; -import org.aeonbits.owner.Config.Sources; -import org.aeonbits.owner.ConfigFactory; - -import java.util.List; -import java.util.Map; - -/** - * 针对每个配置项,建议各个对象自己持有,不建议每次都通过ConfigCenter获取,有性能损耗 - */ -@Sources({ - "classpath:config.properties", - "file:/${user.dir}/config.properties" -}) -public interface ConfigCenter extends Config { - - ConfigCenter I = ConfigFactory.create(ConfigCenter.class); - - /** - * 最大包长度 - * - * @return - */ - @Key("max_packet_size") - @DefaultValue("10240") - int maxPacketSize(); - - /** - * 包启用压缩特性阈值 - * - * @return - */ - @Key("compress_limit") - @DefaultValue("1024") - int compressLimit(); - - /** - * 最小心跳间隔 10s - * - * @return - */ - @Key("min_heartbeat") - @DefaultValue("10000") - int minHeartbeat(); - - /** - * 最大心跳间隔 10s - * - * @return - */ - @Key("max_heartbeat") - @DefaultValue("180000") - int maxHeartbeat(); - - /** - * 最大心跳超时次数 - * - * @return - */ - @Key("max_hb_timeout_times") - @DefaultValue("2") - int maxHBTimeoutTimes(); - - /** - * 快速重连session超时时间 - * - * @return - */ - @Key("session_expired_time") - @DefaultValue("86400") - int sessionExpiredTime(); - - /** - * RSA密钥长度 - * - * @return - */ - @Key("ras_key_length") - @DefaultValue("1024") - int rsaKeyLength(); - - /** - * AES密钥长度 - * - * @return - */ - @Key("aes_key_length") - @DefaultValue("16") - int aesKeyLength(); - - /** - * 长连接服务端口 - * - * @return - */ - @Key("connection_server_port") - @DefaultValue("3000") - int connectionServerPort(); - - /** - * 网关服务端口 - * - * @return - */ - @Key("gateway_server_port") - @DefaultValue("4000") - int gatewayServerPort(); - - - /** - * 控制台服务端口 - * - * @return - */ - @Key("admin_port") - @DefaultValue("4001") - int adminPort(); - - /** - * RSA私钥 - * - * @return - */ - @Key("private_key") - @DefaultValue("MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=") - String privateKey(); - - /** - * RSA公钥 - * - * @return - */ - @Key("public_key") - @DefaultValue("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB") - String publicKey(); - - /** - * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd - * 多台机器用“,”分割 - * - * @return - */ - @Deprecated - @Key("redis_ip") - @DefaultValue("127.0.0.1:6379:ShineMoIpo") - String redisIp(); - - /** - * zookeeper机器,格式ip:port - * - * @return - */ - @Key("zk_ip") - @DefaultValue("127.0.0.1:2181") - String zkIp(); - - /** - * zookeeper 空间 - * - * @return - */ - @Key("zk_namespace") - @DefaultValue("mpush") - String zkNamespace(); - - /** - * zookeeper 权限密码 - * - * @return - */ - @Key("zk_digest") - @DefaultValue("shinemoIpo") - String zkDigest(); - - /** - * redis集群机器列表格式ip:port:pwd,ip:port:pwd,ip:port:pwd - * 多台机器用“;”分割 - * - * @return - */ - @Separator(";") - @Key("redis_group") - @ConverterClass(RedisGroupConverter.class) - List redisGroups(); - - /** - * 自动把配置的redis机器集群写入到zk - * - * @return - */ - @Key("force_write_redis_group_info") - boolean forceWriteRedisGroupInfo(); - - @Key("scan_conn_task_cycle") - @DefaultValue("59000") - long scanConnTaskCycle(); - - @Key("jvm_log_path") - @DefaultValue("/opt/shinemo/mpush/") - String logPath(); - - @Key("http_proxy_enable") - @DefaultValue("false") - boolean httpProxyEnable(); - - @Key("dns_mapping") - @ConverterClass(DnsMappingConverter.class) - Map> dnsMapping(); - - @Key("max_http_client_conn_count_per_host") - @DefaultValue("5") - int maxHttpConnCountPerHost(); - - //10s - @Key("http_default_read_timeout") - @DefaultValue("10000") - int httpDefaultReadTimeout(); - - @Key("online_and_offline_listener_ip") - @DefaultValue("127.0.0.1") - String onlineAndOfflineListenerIp(); - - @Key("skip_dump") - @DefaultValue("true") - boolean skipDump(); - - /** - * 本机IP到外网Ip的映射 格式localIp:remoteIp,localIp:remoteIp - * - * @return - */ - @Key("remote_ip_mapping") - @ConverterClass(MapConverter.class) - Map remoteIpMapping(); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java new file mode 100644 index 00000000..7f305bf3 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -0,0 +1,55 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config; + +import com.mpush.tools.Utils; + +import static com.mpush.tools.Utils.getInetAddress; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ConfigManager { + public static final ConfigManager I = new ConfigManager(); + + private ConfigManager() { + } + + public int getHeartbeat(int min, int max) { + return Math.max( + CC.mp.core.min_heartbeat, + Math.min(max, CC.mp.core.max_heartbeat) + ); + } + + public String getLocalIp() { + return Utils.getLocalIp(); + } + + public String getPublicIp() { + String localIp = getInetAddress(); + + String remoteIp = CC.mp.net.public_ip_mapping.getString(localIp); + + return remoteIp; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java deleted file mode 100644 index 7696bbbb..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/DnsMappingConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.mpush.tools.config; - -import com.google.common.base.Function; -import com.google.common.base.Splitter; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.primitives.Ints; -import com.mpush.tools.dns.DnsMapping; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public class DnsMappingConverter implements Converter>>{ - - private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); - - @Override - public Map> convert(Method method, String input) { - - log.warn("method:"+method.getName()+","+input); - Map map = Splitter.on(';').withKeyValueSeparator('=').split(input); - Map> result = Maps.newConcurrentMap(); - for (Map.Entry entry : map.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - List dnsMappings = Lists.transform(Arrays.asList(value.split(",")), new Function() { - @Override - public DnsMapping apply(String ipAndPort) { - if(ipAndPort.contains(":")){ - String[] temp = ipAndPort.split(":"); - return new DnsMapping(temp[0], Ints.tryParse(temp[1])); - }else{ - return new DnsMapping(ipAndPort, 80); - } - } - }); - result.put(key, dnsMappings); - } - return result; - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java deleted file mode 100644 index f16e05a2..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/MapConverter.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.mpush.tools.config; - -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Map; - -/** - * Created by yxx on 2016/5/12. - * - * @author ohun@live.cn - */ -public class MapConverter implements Converter> { - private static final Logger log = LoggerFactory.getLogger(DnsMappingConverter.class); - - @Override - public Map convert(Method method, String input) { - log.warn("method:" + method.getName() + "," + input); - if (Strings.isNullOrEmpty(input)) return Collections.emptyMap(); - return Splitter.on(',').withKeyValueSeparator(':').split(input); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java b/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java deleted file mode 100644 index 7ca15bc0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/config/RedisGroupConverter.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.mpush.tools.config; - -import java.lang.reflect.Method; - -import com.mpush.tools.redis.RedisGroup; -import org.aeonbits.owner.Converter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.redis.RedisNode; - -public class RedisGroupConverter implements Converter{ - - private static final Logger log = LoggerFactory.getLogger(RedisGroupConverter.class); - - @Override - public RedisGroup convert(Method method, String input) { - - log.warn("method:"+method.getName()+","+input); - - - RedisGroup group = new RedisGroup(); - - String[] chunks = input.split(","); - for (String chunk : chunks) { - String[] entry = chunk.split(":"); - String ip = entry[0].trim(); - String port = entry[1].trim(); - // 如果配置了redis密码(redis_group = 111.1.57.148:6379:ShineMoIpo)才设置密码 - // 否则密码为空,JedisPool可以兼容两种情况 - String password = null; - if(entry.length >=3){ - password = entry[2].trim(); - } - RedisNode node = new RedisNode(ip, Integer.parseInt(port), password); - group.addRedisNode(node); - } - return group; - } - - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java new file mode 100644 index 00000000..30369c96 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config.data; + +import java.util.Collections; +import java.util.List; + + +/** + * redis 组 + */ +public class RedisGroup { + public List redisNodeList = Collections.emptyList(); + + public RedisGroup() { + } + + public RedisGroup(List redisNodeList) { + this.redisNodeList = redisNodeList; + } + + @Override + public String toString() { + return "RedisGroup{" + + "redisNodeList=" + redisNodeList + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java new file mode 100644 index 00000000..451efdf6 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java @@ -0,0 +1,90 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.config.data; + +/** + * redis 相关的配置信息 + */ +public class RedisServer { + public String host; + public int port; + public String password; + + public RedisServer() { + } + + public RedisServer(String host, int port, String password) { + this.host = host; + this.port = port; + this.password = password; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + RedisServer server = (RedisServer) o; + + if (port != server.port) return false; + return host.equals(server.host); + + } + + @Override + public int hashCode() { + int result = host.hashCode(); + result = 31 * result + port; + return result; + } + + @Override + public String toString() { + return "RedisServer{" + + "host='" + host + '\'' + + ", port=" + port + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index bc9eb644..80050e8f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; import org.slf4j.Logger; @@ -9,6 +28,7 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; +import java.util.Arrays; /** * Created by ohun on 2015/12/25. @@ -39,26 +59,38 @@ public static SecretKey getSecretKey(byte[] seed) throws Exception { public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); + return encrypt(data, zeroIv, key); + } + + public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { + IvParameterSpec zeroIv = new IvParameterSpec(iv); + SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + return decrypt(data, zeroIv, key); + } + + public static byte[] encrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); - cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); + cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { - LOGGER.error("encrypt ex, decryptKey=" + encryptKey, e); - throw new RuntimeException("AES encrypt ex", e); + LOGGER.error("AES encrypt ex, iv={}, key={}", + Arrays.toString(zeroIv.getIV()), + Arrays.toString(keySpec.getEncoded()), e); + throw new CryptoException("AES encrypt ex", e); } } - public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { - IvParameterSpec zeroIv = new IvParameterSpec(iv); - SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); + public static byte[] decrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); - cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); + cipher.init(Cipher.DECRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { - LOGGER.error("decrypt ex, decryptKey=" + decryptKey, e); - throw new RuntimeException("AES decrypt ex", e); + LOGGER.error("AES decrypt ex, iv={}, key={}", + Arrays.toString(zeroIv.getIV()), + Arrays.toString(keySpec.getEncoded()), e); + throw new CryptoException("AES decrypt ex", e); } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java index 207fd55e..370c72f4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java @@ -22,12 +22,11 @@ * * */ - package com.mpush.tools.crypto; import java.io.FilterOutputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -41,53 +40,54 @@ * as specified in * RFC 4648 and * RFC 2045. - * + *

*

    *
  • Basic *

    Uses "The Base64 Alphabet" as specified in Table 1 of - * RFC 4648 and RFC 2045 for encoding and decoding operation. - * The encoder does not add any line feed (line separator) - * character. The decoder rejects data that contains characters - * outside the base64 alphabet.

  • - * + * RFC 4648 and RFC 2045 for encoding and decoding operation. + * The encoder does not add any line feed (line separator) + * character. The decoder rejects data that contains characters + * outside the base64 alphabet.

    + *

    *

  • URL and Filename safe *

    Uses the "URL and Filename safe Base64 Alphabet" as specified - * in Table 2 of RFC 4648 for encoding and decoding. The - * encoder does not add any line feed (line separator) character. - * The decoder rejects data that contains characters outside the - * base64 alphabet.

  • - * + * in Table 2 of RFC 4648 for encoding and decoding. The + * encoder does not add any line feed (line separator) character. + * The decoder rejects data that contains characters outside the + * base64 alphabet.

    + *

    *

  • MIME *

    Uses the "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045 for encoding and decoding operation. The encoded output - * must be represented in lines of no more than 76 characters each - * and uses a carriage return {@code '\r'} followed immediately by - * a linefeed {@code '\n'} as the line separator. No line separator - * is added to the end of the encoded output. All line separators - * or other characters not found in the base64 alphabet table are - * ignored in decoding operation.

  • + * RFC 2045 for encoding and decoding operation. The encoded output + * must be represented in lines of no more than 76 characters each + * and uses a carriage return {@code '\r'} followed immediately by + * a linefeed {@code '\n'} as the line separator. No line separator + * is added to the end of the encoded output. All line separators + * or other characters not found in the base64 alphabet table are + * ignored in decoding operation.

    *
- * + *

*

Unless otherwise noted, passing a {@code null} argument to a * method of this class will cause a {@link java.lang.NullPointerException * NullPointerException} to be thrown. * - * @author Xueming Shen - * @since 1.8 + * @author Xueming Shen + * @since 1.8 */ public class Base64 { - private Base64() {} + private Base64() { + } /** * Returns a {@link Encoder} that encodes using the * Basic type base64 encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getEncoder() { - return Encoder.RFC4648; + return Encoder.RFC4648; } /** @@ -95,17 +95,17 @@ public static Encoder getEncoder() { * URL and Filename safe type base64 * encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getUrlEncoder() { - return Encoder.RFC4648_URLSAFE; + return Encoder.RFC4648_URLSAFE; } /** * Returns a {@link Encoder} that encodes using the * MIME type base64 encoding scheme. * - * @return A Base64 encoder. + * @return A Base64 encoder. */ public static Encoder getMimeEncoder() { return Encoder.RFC2045; @@ -116,41 +116,37 @@ public static Encoder getMimeEncoder() { * MIME type base64 encoding scheme * with specified line length and line separators. * - * @param lineLength - * the length of each output line (rounded down to nearest multiple - * of 4). If {@code lineLength <= 0} the output will not be separated - * in lines - * @param lineSeparator - * the line separator for each output line - * - * @return A Base64 encoder. - * - * @throws IllegalArgumentException if {@code lineSeparator} includes any - * character of "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045. + * @param lineLength the length of each output line (rounded down to nearest multiple + * of 4). If {@code lineLength <= 0} the output will not be separated + * in lines + * @param lineSeparator the line separator for each output line + * @return A Base64 encoder. + * @throws IllegalArgumentException if {@code lineSeparator} includes any + * character of "The Base64 Alphabet" as specified in Table 1 of + * RFC 2045. */ public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) { - Objects.requireNonNull(lineSeparator); - int[] base64 = Decoder.fromBase64; - for (byte b : lineSeparator) { - if (base64[b & 0xff] != -1) - throw new IllegalArgumentException( - "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); - } - if (lineLength <= 0) { - return Encoder.RFC4648; - } - return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); + Objects.requireNonNull(lineSeparator); + int[] base64 = Decoder.fromBase64; + for (byte b : lineSeparator) { + if (base64[b & 0xff] != -1) + throw new IllegalArgumentException( + "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); + } + if (lineLength <= 0) { + return Encoder.RFC4648; + } + return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); } /** * Returns a {@link Decoder} that decodes using the * Basic type base64 encoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getDecoder() { - return Decoder.RFC4648; + return Decoder.RFC4648; } /** @@ -158,36 +154,36 @@ public static Decoder getDecoder() { * URL and Filename safe type base64 * encoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getUrlDecoder() { - return Decoder.RFC4648_URLSAFE; + return Decoder.RFC4648_URLSAFE; } /** * Returns a {@link Decoder} that decodes using the * MIME type base64 decoding scheme. * - * @return A Base64 decoder. + * @return A Base64 decoder. */ public static Decoder getMimeDecoder() { - return Decoder.RFC2045; + return Decoder.RFC2045; } /** * This class implements an encoder for encoding byte data using * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - * + *

*

Instances of {@link Encoder} class are safe for use by * multiple concurrent threads. - * + *

*

Unless otherwise noted, passing a {@code null} argument to * a method of this class will cause a * {@link java.lang.NullPointerException NullPointerException} to * be thrown. * - * @see Decoder - * @since 1.8 + * @see Decoder + * @since 1.8 */ public static class Encoder { @@ -209,11 +205,11 @@ private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648). */ private static final char[] toBase64 = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; /** @@ -222,15 +218,15 @@ private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { * '_'. This table is used when BASE64_URL is specified. */ private static final char[] toBase64URL = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' }; private static final int MIMELINEMAX = 76; - private static final byte[] CRLF = new byte[] {'\r', '\n'}; + private static final byte[] CRLF = new byte[]{'\r', '\n'}; static final Encoder RFC4648 = new Encoder(false, null, -1, true); static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true); @@ -254,17 +250,16 @@ private final int outLength(int srclen) { * byte array using the {@link Base64} encoding scheme. The returned byte * array is of the length of the resulting bytes. * - * @param src - * the byte array to encode - * @return A newly-allocated byte array containing the resulting - * encoded bytes. + * @param src the byte array to encode + * @return A newly-allocated byte array containing the resulting + * encoded bytes. */ public byte[] encode(byte[] src) { int len = outLength(src.length); // dst array size byte[] dst = new byte[len]; int ret = encode0(src, 0, src.length, dst); if (ret != dst.length) - return Arrays.copyOf(dst, ret); + return Arrays.copyOf(dst, ret); return dst; } @@ -272,45 +267,41 @@ public byte[] encode(byte[] src) { * Encodes all bytes from the specified byte array using the * {@link Base64} encoding scheme, writing the resulting bytes to the * given output byte array, starting at offset 0. - * + *

*

It is the responsibility of the invoker of this method to make * sure the output byte array {@code dst} has enough space for encoding * all bytes from the input byte array. No bytes will be written to the * output byte array if the output byte array is not big enough. * - * @param src - * the byte array to encode - * @param dst - * the output byte array - * @return The number of bytes written to the output byte array - * - * @throws IllegalArgumentException if {@code dst} does not have enough - * space for encoding all input bytes. + * @param src the byte array to encode + * @param dst the output byte array + * @return The number of bytes written to the output byte array + * @throws IllegalArgumentException if {@code dst} does not have enough + * space for encoding all input bytes. */ public int encode(byte[] src, byte[] dst) { int len = outLength(src.length); // dst array size if (dst.length < len) throw new IllegalArgumentException( - "Output byte array is too small for encoding all input bytes"); + "Output byte array is too small for encoding all input bytes"); return encode0(src, 0, src.length, dst); } /** * Encodes the specified byte array into a String using the {@link Base64} * encoding scheme. - * + *

*

This method first encodes all input bytes into a base64 encoded * byte array and then constructs a new String by using the encoded byte * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1 * ISO-8859-1} charset. - * + *

*

In other words, an invocation of this method has exactly the same * effect as invoking * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}. * - * @param src - * the byte array to encode - * @return A String containing the resulting Base64 encoded characters + * @param src the byte array to encode + * @return A String containing the resulting Base64 encoded characters */ @SuppressWarnings("deprecation") public String encodeToString(byte[] src) { @@ -322,15 +313,14 @@ public String encodeToString(byte[] src) { * Encodes all remaining bytes from the specified byte buffer into * a newly-allocated ByteBuffer using the {@link Base64} encoding * scheme. - * + *

* Upon return, the source buffer's position will be updated to * its limit; its limit will not have been changed. The returned * output buffer's position will be zero and its limit will be the * number of resulting encoded bytes. * - * @param buffer - * the source ByteBuffer to encode - * @return A newly-allocated byte buffer containing the encoded bytes. + * @param buffer the source ByteBuffer to encode + * @return A newly-allocated byte buffer containing the encoded bytes. */ public ByteBuffer encode(ByteBuffer buffer) { int len = outLength(buffer.remaining()); @@ -338,9 +328,9 @@ public ByteBuffer encode(ByteBuffer buffer) { int ret = 0; if (buffer.hasArray()) { ret = encode0(buffer.array(), - buffer.arrayOffset() + buffer.position(), - buffer.arrayOffset() + buffer.limit(), - dst); + buffer.arrayOffset() + buffer.position(), + buffer.arrayOffset() + buffer.limit(), + dst); buffer.position(buffer.limit()); } else { byte[] src = new byte[buffer.remaining()]; @@ -348,41 +338,40 @@ public ByteBuffer encode(ByteBuffer buffer) { ret = encode0(src, 0, src.length, dst); } if (ret != dst.length) - dst = Arrays.copyOf(dst, ret); + dst = Arrays.copyOf(dst, ret); return ByteBuffer.wrap(dst); } /** * Wraps an output stream for encoding byte data using the {@link Base64} * encoding scheme. - * + *

*

It is recommended to promptly close the returned output stream after * use, during which it will flush all possible leftover bytes to the underlying * output stream. Closing the returned output stream will close the underlying * output stream. * - * @param os - * the output stream. - * @return the output stream for encoding the byte data into the - * specified Base64 encoded format + * @param os the output stream. + * @return the output stream for encoding the byte data into the + * specified Base64 encoded format */ public OutputStream wrap(OutputStream os) { Objects.requireNonNull(os); return new EncOutputStream(os, isURL ? toBase64URL : toBase64, - newline, linemax, doPadding); + newline, linemax, doPadding); } /** * Returns an encoder instance that encodes equivalently to this one, * but without adding any padding character at the end of the encoded * byte data. - * + *

*

The encoding scheme of this encoder instance is unaffected by * this invocation. The returned encoder instance should be used for * non-padding encoding operation. * * @return an equivalent encoder that encodes without adding any - * padding character at the end + * padding character at the end */ public Encoder withoutPadding() { if (!doPadding) @@ -395,42 +384,42 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { int sp = off; int slen = (end - off) / 3 * 3; int sl = off + slen; - if (linemax > 0 && slen > linemax / 4 * 3) + if (linemax > 0 && slen > linemax / 4 * 3) slen = linemax / 4 * 3; int dp = 0; while (sp < sl) { int sl0 = Math.min(sp + slen, sl); - for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) { + for (int sp0 = sp, dp0 = dp; sp0 < sl0; ) { int bits = (src[sp0++] & 0xff) << 16 | - (src[sp0++] & 0xff) << 8 | - (src[sp0++] & 0xff); - dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f]; - dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f]; - dst[dp0++] = (byte)base64[(bits >>> 6) & 0x3f]; - dst[dp0++] = (byte)base64[bits & 0x3f]; + (src[sp0++] & 0xff) << 8 | + (src[sp0++] & 0xff); + dst[dp0++] = (byte) base64[(bits >>> 18) & 0x3f]; + dst[dp0++] = (byte) base64[(bits >>> 12) & 0x3f]; + dst[dp0++] = (byte) base64[(bits >>> 6) & 0x3f]; + dst[dp0++] = (byte) base64[bits & 0x3f]; } int dlen = (sl0 - sp) / 3 * 4; dp += dlen; sp = sl0; if (dlen == linemax && sp < end) { - for (byte b : newline){ + for (byte b : newline) { dst[dp++] = b; } } } if (sp < end) { // 1 or 2 leftover bytes int b0 = src[sp++] & 0xff; - dst[dp++] = (byte)base64[b0 >> 2]; + dst[dp++] = (byte) base64[b0 >> 2]; if (sp == end) { - dst[dp++] = (byte)base64[(b0 << 4) & 0x3f]; + dst[dp++] = (byte) base64[(b0 << 4) & 0x3f]; if (doPadding) { dst[dp++] = '='; dst[dp++] = '='; } } else { int b1 = src[sp++] & 0xff; - dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; - dst[dp++] = (byte)base64[(b1 << 2) & 0x3f]; + dst[dp++] = (byte) base64[(b0 << 4) & 0x3f | (b1 >> 4)]; + dst[dp++] = (byte) base64[(b1 << 2) & 0x3f]; if (doPadding) { dst[dp++] = '='; } @@ -443,7 +432,7 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { /** * This class implements a decoder for decoding byte data using the * Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - * + *

*

The Base64 padding character {@code '='} is accepted and * interpreted as the end of the encoded byte data, but is not * required. So if the final unit of the encoded byte data only has @@ -454,17 +443,17 @@ private int encode0(byte[] src, int off, int end, byte[] dst) { * present, otherwise {@code IllegalArgumentException} ( * {@code IOException} when reading from a Base64 stream) is thrown * during decoding. - * + *

*

Instances of {@link Decoder} class are safe for use by * multiple concurrent threads. - * + *

*

Unless otherwise noted, passing a {@code null} argument to * a method of this class will cause a * {@link java.lang.NullPointerException NullPointerException} to * be thrown. * - * @see Encoder - * @since 1.8 + * @see Encoder + * @since 1.8 */ public static class Decoder { @@ -482,9 +471,9 @@ private Decoder(boolean isURL, boolean isMIME) { * their 6-bit positive integer equivalents. Characters that * are not in the Base64 alphabet but fall within the bounds of * the array are encoded to -1. - * */ private static final int[] fromBase64 = new int[256]; + static { Arrays.fill(fromBase64, -1); for (int i = 0; i < Encoder.toBase64.length; i++) @@ -505,9 +494,9 @@ private Decoder(boolean isURL, boolean isMIME) { fromBase64URL['='] = -2; } - static final Decoder RFC4648 = new Decoder(false, false); + static final Decoder RFC4648 = new Decoder(false, false); static final Decoder RFC4648_URLSAFE = new Decoder(true, false); - static final Decoder RFC2045 = new Decoder(false, true); + static final Decoder RFC2045 = new Decoder(false, true); /** * Decodes all bytes from the input byte array using the {@link Base64} @@ -515,13 +504,9 @@ private Decoder(boolean isURL, boolean isMIME) { * byte array. The returned byte array is of the length of the resulting * bytes. * - * @param src - * the byte array to decode - * - * @return A newly-allocated byte array containing the decoded bytes. - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme + * @param src the byte array to decode + * @return A newly-allocated byte array containing the decoded bytes. + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme */ public byte[] decode(byte[] src) { byte[] dst = new byte[outLength(src, 0, src.length)]; @@ -535,17 +520,13 @@ public byte[] decode(byte[] src) { /** * Decodes a Base64 encoded String into a newly-allocated byte array * using the {@link Base64} encoding scheme. - * + *

*

An invocation of this method has exactly the same effect as invoking * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))} * - * @param src - * the string to decode - * - * @return A newly-allocated byte array containing the decoded bytes. - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme + * @param src the string to decode + * @return A newly-allocated byte array containing the decoded bytes. + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme */ public byte[] decode(String src) { return decode(src.getBytes(StandardCharsets.ISO_8859_1)); @@ -555,55 +536,46 @@ public byte[] decode(String src) { * Decodes all bytes from the input byte array using the {@link Base64} * encoding scheme, writing the results into the given output byte array, * starting at offset 0. - * + *

*

It is the responsibility of the invoker of this method to make * sure the output byte array {@code dst} has enough space for decoding * all bytes from the input byte array. No bytes will be be written to * the output byte array if the output byte array is not big enough. - * + *

*

If the input byte array is not in valid Base64 encoding scheme * then some bytes may have been written to the output byte array before * IllegalargumentException is thrown. * - * @param src - * the byte array to decode - * @param dst - * the output byte array - * - * @return The number of bytes written to the output byte array - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme, or {@code dst} - * does not have enough space for decoding all input bytes. + * @param src the byte array to decode + * @param dst the output byte array + * @return The number of bytes written to the output byte array + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme, or {@code dst} + * does not have enough space for decoding all input bytes. */ public int decode(byte[] src, byte[] dst) { int len = outLength(src, 0, src.length); if (dst.length < len) throw new IllegalArgumentException( - "Output byte array is too small for decoding all input bytes"); + "Output byte array is too small for decoding all input bytes"); return decode0(src, 0, src.length, dst); } /** * Decodes all bytes from the input byte buffer using the {@link Base64} * encoding scheme, writing the results into a newly-allocated ByteBuffer. - * + *

*

Upon return, the source buffer's position will be updated to * its limit; its limit will not have been changed. The returned * output buffer's position will be zero and its limit will be the * number of resulting decoded bytes - * + *

*

{@code IllegalArgumentException} is thrown if the input buffer * is not in valid Base64 encoding scheme. The position of the input * buffer will not be advanced in this case. * - * @param buffer - * the ByteBuffer to decode - * - * @return A newly-allocated byte buffer containing the decoded bytes - * - * @throws IllegalArgumentException - * if {@code src} is not in valid Base64 scheme. + * @param buffer the ByteBuffer to decode + * @return A newly-allocated byte buffer containing the decoded bytes + * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme. */ public ByteBuffer decode(ByteBuffer buffer) { int pos0 = buffer.position(); @@ -631,18 +603,16 @@ public ByteBuffer decode(ByteBuffer buffer) { /** * Returns an input stream for decoding {@link Base64} encoded byte stream. - * + *

*

The {@code read} methods of the returned {@code InputStream} will * throw {@code IOException} when reading bytes that cannot be decoded. - * + *

*

Closing the returned input stream will close the underlying * input stream. * - * @param is - * the input stream - * - * @return the input stream for decoding the specified Base64 encoded - * byte stream + * @param is the input stream + * @return the input stream for decoding the specified Base64 encoded + * byte stream */ public InputStream wrap(InputStream is) { Objects.requireNonNull(is); @@ -659,7 +629,7 @@ private int outLength(byte[] src, int sp, int sl) { if (isMIME && base64[0] == -1) return 0; throw new IllegalArgumentException( - "Input byte[] should at least have 2 bytes for base64 bytes"); + "Input byte[] should at least have 2 bytes for base64 bytes"); } if (isMIME) { // scan all bytes to fill out all non-alphabet. a performance @@ -682,7 +652,7 @@ private int outLength(byte[] src, int sp, int sl) { paddings++; } } - if (paddings == 0 && (len & 0x3) != 0) + if (paddings == 0 && (len & 0x3) != 0) paddings = 4 - (len & 0x3); return 3 * ((len + 3) / 4) - paddings; } @@ -702,9 +672,9 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { // xx= shiftto==6&&sp==sl missing last = // xx=y shiftto==6 last is not = if (shiftto == 6 && (sp == sl || src[sp++] != '=') || - shiftto == 18) { + shiftto == 18) { throw new IllegalArgumentException( - "Input byte array has wrong 4-byte ending unit"); + "Input byte array has wrong 4-byte ending unit"); } break; } @@ -712,29 +682,29 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { continue; else throw new IllegalArgumentException( - "Illegal base64 character " + - Integer.toString(src[sp - 1], 16)); + "Illegal base64 character " + + Integer.toString(src[sp - 1], 16)); } bits |= (b << shiftto); shiftto -= 6; if (shiftto < 0) { - dst[dp++] = (byte)(bits >> 16); - dst[dp++] = (byte)(bits >> 8); - dst[dp++] = (byte)(bits); + dst[dp++] = (byte) (bits >> 16); + dst[dp++] = (byte) (bits >> 8); + dst[dp++] = (byte) (bits); shiftto = 18; bits = 0; } } // reached end of byte array or hit padding '=' characters. if (shiftto == 6) { - dst[dp++] = (byte)(bits >> 16); + dst[dp++] = (byte) (bits >> 16); } else if (shiftto == 0) { - dst[dp++] = (byte)(bits >> 16); - dst[dp++] = (byte)(bits >> 8); + dst[dp++] = (byte) (bits >> 16); + dst[dp++] = (byte) (bits >> 8); } else if (shiftto == 12) { // dangling single "x", incorrectly encoded. throw new IllegalArgumentException( - "Last unit does not have enough valid bits"); + "Last unit does not have enough valid bits"); } // anything left is invalid, if is not MIME. // if MIME, ignore all non-base64 character @@ -742,7 +712,7 @@ private int decode0(byte[] src, int sp, int sl, byte[] dst) { if (isMIME && base64[src[sp++]] < 0) continue; throw new IllegalArgumentException( - "Input byte array has incorrect ending byte at " + sp); + "Input byte array has incorrect ending byte at " + sp); } return dp; } @@ -775,7 +745,7 @@ private static class EncOutputStream extends FilterOutputStream { @Override public void write(int b) throws IOException { byte[] buf = new byte[1]; - buf[0] = (byte)(b & 0xff); + buf[0] = (byte) (b & 0xff); write(buf, 0, 1); } @@ -817,14 +787,14 @@ public void write(byte[] b, int off, int len) throws IOException { while (nBits24-- > 0) { checkNewline(); int bits = (b[off++] & 0xff) << 16 | - (b[off++] & 0xff) << 8 | - (b[off++] & 0xff); + (b[off++] & 0xff) << 8 | + (b[off++] & 0xff); out.write(base64[(bits >>> 18) & 0x3f]); out.write(base64[(bits >>> 12) & 0x3f]); - out.write(base64[(bits >>> 6) & 0x3f]); + out.write(base64[(bits >>> 6) & 0x3f]); out.write(base64[bits & 0x3f]); linepos += 4; - } + } if (leftover == 1) { b0 = b[off++] & 0xff; } else if (leftover == 2) { @@ -851,7 +821,7 @@ public void close() throws IOException { out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); out.write(base64[(b1 << 2) & 0x3f]); if (doPadding) { - out.write('='); + out.write('='); } } leftover = 0; @@ -870,9 +840,9 @@ private static class DecInputStream extends InputStream { private final int[] base64; // base64 -> byte mapping private int bits = 0; // 24-bit buffer for decoding private int nextin = 18; // next available "off" in "bits" for input; - // -> 18, 12, 6, 0 + // -> 18, 12, 6, 0 private int nextout = -8; // next available "off" in "bits" for output; - // -> 8, 0, -8 (no byte for output) + // -> 8, 0, -8 (no byte for output) private boolean eof = false; private boolean closed = false; @@ -902,7 +872,7 @@ public int read(byte[] b, int off, int len) throws IOException { do { if (len == 0) return off - oldOff; - b[off++] = (byte)(bits >> nextout); + b[off++] = (byte) (bits >> nextout); len--; nextout -= 8; } while (nextout >= 0); @@ -917,14 +887,14 @@ public int read(byte[] b, int off, int len) throws IOException { throw new IOException("Base64 stream has one un-decoded dangling byte."); // treat ending xx/xxx without padding character legal. // same logic as v == '=' below - b[off++] = (byte)(bits >> (16)); + b[off++] = (byte) (bits >> (16)); len--; if (nextin == 0) { // only one padding byte if (len == 0) { // no enough output space bits >>= 8; // shift to lowest byte nextout = 0; } else { - b[off++] = (byte) (bits >> 8); + b[off++] = (byte) (bits >> 8); } } } @@ -939,17 +909,17 @@ public int read(byte[] b, int off, int len) throws IOException { // xx= shiftto==6 && missing last '=' // xx=y or last is not '=' if (nextin == 18 || nextin == 12 || - nextin == 6 && is.read() != '=') { + nextin == 6 && is.read() != '=') { throw new IOException("Illegal base64 ending sequence:" + nextin); } - b[off++] = (byte)(bits >> (16)); + b[off++] = (byte) (bits >> (16)); len--; if (nextin == 0) { // only one padding byte if (len == 0) { // no enough output space bits >>= 8; // shift to lowest byte nextout = 0; } else { - b[off++] = (byte) (bits >> 8); + b[off++] = (byte) (bits >> 8); } } eof = true; @@ -960,14 +930,14 @@ public int read(byte[] b, int off, int len) throws IOException { continue; else throw new IOException("Illegal base64 character " + - Integer.toString(v, 16)); + Integer.toString(v, 16)); } bits |= (v << nextin); if (nextin == 0) { nextin = 18; // clear for next nextout = 16; while (nextout >= 0) { - b[off++] = (byte)(bits >> nextout); + b[off++] = (byte) (bits >> nextout); len--; nextout -= 8; if (len == 0 && nextout >= 0) { // don't clean "bits" diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index c7280a07..3c2f1393 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -1,25 +1,29 @@ -package com.mpush.tools.crypto; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.crypto; -import com.mpush.tools.Constants; -import java.io.*; +import com.mpush.api.Constants; -/** - *

- * BASE64编码解码工具包 - *

- *

- * 依赖javabase64-1.3.1.jar - *

- */ public class Base64Utils { - /** - * 文件读取缓冲区大小 - */ - private static final int CACHE_SIZE = 1024; - /** *

* BASE64字符串解码为二进制数据 @@ -30,7 +34,7 @@ public class Base64Utils { * @throws Exception */ public static byte[] decode(String base64) throws Exception { - return Base64.getDecoder().decode(base64); + return Base64.getDecoder().decode(base64.getBytes(Constants.UTF_8)); } /** @@ -46,90 +50,4 @@ public static String encode(byte[] bytes) throws Exception { return new String(Base64.getEncoder().encode(bytes), Constants.UTF_8); } - /** - *

- * 将文件编码为BASE64字符串 - *

- *

- * 大文件慎用,可能会导致内存溢出 - *

- * - * @param filePath 文件绝对路径 - * @return - * @throws Exception - */ - public static String encodeFile(String filePath) throws Exception { - byte[] bytes = fileToByte(filePath); - return encode(bytes); - } - - /** - *

- * BASE64字符串转回文件 - *

- * - * @param filePath 文件绝对路径 - * @param base64 编码字符串 - * @throws Exception - */ - public static void decodeToFile(String filePath, String base64) throws Exception { - byte[] bytes = decode(base64); - byteArrayToFile(bytes, filePath); - } - - /** - *

- * 文件转换为二进制数组 - *

- * - * @param filePath 文件路径 - * @return - * @throws Exception - */ - public static byte[] fileToByte(String filePath) throws Exception { - byte[] data = new byte[0]; - File file = new File(filePath); - if (file.exists()) { - FileInputStream in = new FileInputStream(file); - ByteArrayOutputStream out = new ByteArrayOutputStream(2048); - byte[] cache = new byte[CACHE_SIZE]; - int nRead = 0; - while ((nRead = in.read(cache)) != -1) { - out.write(cache, 0, nRead); - out.flush(); - } - out.close(); - in.close(); - data = out.toByteArray(); - } - return data; - } - - /** - *

- * 二进制数据写文件 - *

- * - * @param bytes 二进制数据 - * @param filePath 文件生成目录 - */ - public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { - InputStream in = new ByteArrayInputStream(bytes); - File destFile = new File(filePath); - if (!destFile.getParentFile().exists()) { - destFile.getParentFile().mkdirs(); - } - destFile.createNewFile(); - OutputStream out = new FileOutputStream(destFile); - byte[] cache = new byte[CACHE_SIZE]; - int nRead = 0; - while ((nRead = in.read(cache)) != -1) { - out.write(cache, 0, nRead); - out.flush(); - } - out.close(); - in.close(); - } - - } \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java new file mode 100644 index 00000000..b7a39da8 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/CryptoException.java @@ -0,0 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.crypto; + +/** + * Created by ohun on 2015/12/23. + * + * @author ohun@live.cn + */ +public class CryptoException extends RuntimeException { + + private static final long serialVersionUID = 368277451733324220L; + + public CryptoException(String message) { + super(message); + } + + public CryptoException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index 02b2ff17..536f9ba8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -1,8 +1,27 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; -import com.mpush.tools.Constants; -import com.mpush.tools.IOUtils; -import com.mpush.tools.Strings; +import com.mpush.api.Constants; +import com.mpush.tools.common.IOUtils; +import com.mpush.tools.common.Strings; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index e3201180..b1edd6db 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -1,7 +1,26 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; -import com.mpush.tools.Constants; -import com.mpush.tools.Pair; +import com.mpush.api.Constants; +import com.mpush.tools.common.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -169,7 +188,7 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { LOGGER.error("getPublicKey ex modulus={}, exponent={}", modulus, exponent, e); - throw new RuntimeException("Get PublicKey ex", e); + throw new CryptoException("Get PublicKey ex", e); } } @@ -192,7 +211,7 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { LOGGER.error("getPrivateKey ex modulus={}, exponent={}", modulus, exponent, e); - throw new RuntimeException("Get PrivateKey ex", e); + throw new CryptoException("Get PrivateKey ex", e); } } @@ -215,7 +234,7 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { return doFinal(cipher, data, key_len - 11); } catch (Exception e) { LOGGER.error("encryptByPublicKey ex", e); - throw new RuntimeException("RSA encrypt ex", e); + throw new CryptoException("RSA encrypt ex", e); } } @@ -237,7 +256,7 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) return doFinal(cipher, data, key_len); } catch (Exception e) { LOGGER.error("decryptByPrivateKey ex", e); - throw new RuntimeException("RSA decrypt ex", e); + throw new CryptoException("RSA decrypt ex", e); } } @@ -347,8 +366,12 @@ public static void main(String[] args) throws Exception { byte[] ming = "123456789".getBytes(Constants.UTF_8); System.out.println("明文:" + new String(ming, Constants.UTF_8)); //使用模和指数生成公钥和私钥 - RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); + RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); + System.out.println("privateKey=" + priKey); + System.out.println("publicKey=" + pubKey); + System.out.println("privateKey=" + priKey); + System.out.println("publicKey=" + pubKey); //加密后的密文 byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey); System.out.println("密文:" + new String(mi, Constants.UTF_8)); diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java b/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java deleted file mode 100644 index 6f541b3a..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/DnsMapping.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.mpush.tools.dns; - - -public class DnsMapping { - - private String ip; - private int port; - - public DnsMapping(String ip, int port) { - this.ip = ip; - this.port = port; - } - - public String getIp() { - return ip; - } - public int getPort() { - return port; - } - - @Override - public String toString() { - return ip+":"+port; - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java b/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java deleted file mode 100644 index 905f4e96..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/dns/manage/DnsMappingManage.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.mpush.tools.dns.manage; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.mpush.tools.Jsons; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.dns.DnsMapping; - -public class DnsMappingManage { - - private static final Logger LOG = LoggerFactory.getLogger(DnsMappingManage.class); - - private DnsMappingManage() { - } - - public static final DnsMappingManage holder = new DnsMappingManage(); - - private Map> all = Maps.newConcurrentMap(); - private Map> available = Maps.newConcurrentMap(); - - private Worker worker = new Worker(); - - private ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); - - public void init() { - LOG.error("start init dnsMapping"); - all.putAll(ConfigCenter.I.dnsMapping()); - available.putAll(ConfigCenter.I.dnsMapping()); - pool.scheduleAtFixedRate(worker, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns - LOG.error("end init dnsMapping"); - } - - public void update(Map> nowAvailable) { - available = nowAvailable; - } - - public Map> getAll() { - return all; - } - - public DnsMapping translate(String origin) { - if (available.isEmpty()) - return null; - List list = available.get(origin); - if (list == null || list.isEmpty()) - return null; - int L = list.size(); - if (L == 1) - return list.get(0); - return list.get((int) (Math.random() * L % L)); - } - - public void shutdown() { - pool.shutdown(); - } - - public static class Worker implements Runnable { - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - - log.debug("start dns mapping telnet"); - - Map> all = DnsMappingManage.holder.getAll(); - - Map> available = Maps.newConcurrentMap(); - - for (Map.Entry> entry : all.entrySet()) { - String key = entry.getKey(); - List value = entry.getValue(); - List nowValue = Lists.newArrayList(); - for (DnsMapping temp : value) { - boolean canTelnet = MPushUtil.telnet(temp.getIp(), temp.getPort()); - if (canTelnet) { - nowValue.add(temp); - } else { - log.error("dns can not reachable:" + Jsons.toJson(temp)); - } - } - available.put(key, nowValue); - } - - DnsMappingManage.holder.update(available); - - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/Event.java b/mpush-tools/src/main/java/com/mpush/tools/event/Event.java deleted file mode 100644 index afabfdee..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/Event.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.mpush.tools.event; - -public class Event { - - private final EventType eventType; - private final Object source; - - public Event(final EventType eventType, final Object source) { - this.eventType = eventType; - this.source = source; - } - - public EventType getEventType() { - return eventType; - } - - public Object getSource() { - return source; - } - -} diff --git a/mpush-common/src/main/java/com/mpush/common/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java similarity index 52% rename from mpush-common/src/main/java/com/mpush/common/EventBus.java rename to mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index e27184c4..f547073d 100644 --- a/mpush-common/src/main/java/com/mpush/common/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -1,11 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -package com.mpush.common; +package com.mpush.tools.event; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; import com.mpush.api.event.Event; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; +import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,12 +35,12 @@ * @author ohun@live.cn */ public class EventBus { - private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); - public static final EventBus INSTANCE = new EventBus(); + private final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); + public static final EventBus I = new EventBus(); private final com.google.common.eventbus.EventBus eventBus; public EventBus() { - Executor executor = ThreadPoolManager.eventBusExecutor; + Executor executor = ThreadPoolManager.I.getEventBusExecutor(); eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { @@ -35,7 +53,6 @@ public void post(Event event) { eventBus.post(event); } - public void register(Object bean) { eventBus.register(bean); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java new file mode 100644 index 00000000..c7e7ccab --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventConsumer.java @@ -0,0 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.event; + +public abstract class EventConsumer { + + public EventConsumer() { + EventBus.I.register(this); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java deleted file mode 100644 index fbcb19ee..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventDispatcher.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.tools.event; - -import java.util.ArrayList; -import java.util.List; - -public class EventDispatcher { - - private static final List listeners = new ArrayList(); - - public static void addEventListener(EventListener listener) { - listeners.add(listener); - } - - public static void fireEvent(Event event) { - for (EventListener listener : listeners) { - listener.onEvent(event); - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java deleted file mode 100644 index 5a9b6d82..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.mpush.tools.event; - -public interface EventListener { - - public void onEvent(Event event); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java deleted file mode 100644 index ccd77b3f..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.mpush.tools.event; - -public enum EventType { - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java b/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java deleted file mode 100644 index c5326f53..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/exception/ZKException.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mpush.tools.exception; - -/** - * Created by yxx on 2016/5/14. - * - * @author ohun@live.cn - */ -public class ZKException extends RuntimeException { - - public ZKException() { - } - - public ZKException(String message) { - super(message); - } - - public ZKException(String message, Throwable cause) { - super(message, cause); - } - - public ZKException(Throwable cause) { - super(cause); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java new file mode 100644 index 00000000..44fb5e5e --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -0,0 +1,60 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.log; + +import com.mpush.tools.config.CC; +import com.typesafe.config.ConfigRenderOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2016/5/16. + * + * @author ohun@live.cn + */ +public interface Logs { + boolean logInit = init(); + + static boolean init() { + if (logInit) return true; + System.setProperty("log.home", CC.mp.log_dir); + System.setProperty("log.root.level", CC.mp.log_level); + LoggerFactory + .getLogger("console") + .info(CC.mp.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true))); + return true; + } + + Logger Console = LoggerFactory.getLogger("console"), + + Conn = LoggerFactory.getLogger("mpush.conn.log"), + + Monitor = LoggerFactory.getLogger("mpush.monitor.log"), + + PUSH = LoggerFactory.getLogger("mpush.push.log"), + + HB = LoggerFactory.getLogger("mpush.heartbeat.log"), + + REDIS = LoggerFactory.getLogger("mpush.redis.log"), + + ZK = LoggerFactory.getLogger("mpush.zk.log"), + + HTTP = LoggerFactory.getLogger("mpush.http.log"); +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java deleted file mode 100644 index 73b69ec3..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisGroup.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.mpush.tools.redis; - -import java.util.List; - -import com.google.common.collect.Lists; - - -/** - * redis 组 - * - */ -public class RedisGroup { - - private List redisNodeList; - - public List getRedisNodeList() { - return redisNodeList; - } - - public void setRedisNodeList(List redisNodeList) { - this.redisNodeList = redisNodeList; - } - - public void addRedisNode(RedisNode node){ - if(redisNodeList==null){ - redisNodeList = Lists.newArrayList(); - } - redisNodeList.add(node); - } - - public void remove(int i){ - if(redisNodeList!=null){ - redisNodeList.remove(i); - } - } - - public void clear(){ - if(redisNodeList!=null){ - redisNodeList.clear(); - } - } - - public RedisNode get(String key){ - int i = key.hashCode() %redisNodeList.size(); - return redisNodeList.get(i); - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java deleted file mode 100644 index 82f3de80..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisNode.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.mpush.tools.redis; - -/** - * redis 相关的配置信息 - * - */ -public class RedisNode { - - private String ip; - private int port; - private String password; - public String getIp() { - return ip; - } - public void setIp(String ip) { - this.ip = ip; - } - public int getPort() { - return port; - } - public void setPort(int port) { - this.port = port; - } - public String getPassword() { - return password; - } - public void setPassword(String password) { - this.password = password; - } - - public RedisNode(String ip, int port, String password) { - this.ip = ip; - this.port = port; - this.password = password; - } - @Override - public String toString() { - return "RedisNode [ip=" + ip + ", port=" + port + ", password=" + password + "]"; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java deleted file mode 100644 index b1a1e383..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisPoolConfig.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.mpush.tools.redis; - -import com.mpush.tools.Constants; - -import redis.clients.jedis.JedisPoolConfig; - -public class RedisPoolConfig { - - public static JedisPoolConfig config = new JedisPoolConfig(); - - static{ - //连接池中最大连接数。高版本:maxTotal,低版本:maxActive - config.setMaxTotal(Constants.REDIS_MAX_TOTAL); - //连接池中最大空闲的连接数 - config.setMaxIdle(Constants.REDIS_MAX_IDLE); - //连接池中最少空闲的连接数 - config.setMinIdle(Constants.REDIS_MIN_IDLE); - //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait - config.setMaxWaitMillis(Constants.REDIS_MAX_WAIT_MILLIS); - //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 - config.setMinEvictableIdleTimeMillis(Constants.REDIS_MIN_EVICTABLE_IDLE_TIME_MILLIS); - //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 - config.setNumTestsPerEvictionRun(Constants.REDIS_NUM_TESTS_PER_EVICTION_RUN); - //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 - config.setTimeBetweenEvictionRunsMillis(Constants.REDIS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); - //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. - config.setTestOnBorrow(Constants.REDIS_TEST_ON_BORROW); - //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 - config.setTestOnReturn(Constants.REDIS_TEST_ON_RETURN); - //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. - config.setTestWhileIdle(Constants.REDIS_TEST_WHILE_IDLE); - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java b/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java deleted file mode 100644 index 834babfa..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/RedisRegister.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mpush.tools.redis; - -import com.mpush.tools.spi.SPI; - -import java.util.List; - -@SPI("redisRegister") -public interface RedisRegister { - - void init(List group); - - List getGroupList(); - - RedisNode randomGetRedisNode(String key); - - List hashSet(String key); -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java deleted file mode 100644 index c50d3eac..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/ConsistentHash.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.mpush.tools.redis.consistenthash; - -import java.util.Collection; -import java.util.SortedMap; -import java.util.TreeMap; - -import redis.clients.util.Hashing; - -public class ConsistentHash { - - private final Hashing hash; - private final int numberOfReplicas; - private final SortedMap circle = new TreeMap(); - - public ConsistentHash(Hashing hash, int numberOfReplicas, - Collection nodes) { - super(); - this.hash = hash; - this.numberOfReplicas = numberOfReplicas; - for (Node node : nodes) { - add(node); - } - } - - /** - * 增加真实机器节点 - * - * @param node - */ - public void add(Node node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.put(this.hash.hash(node.toString() + i), node); - } - } - - /** - * 删除真实机器节点 - * - * @param node - */ - public void remove(String node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.remove(this.hash.hash(node.toString() + i)); - } - } - - /** - * 取得真实机器节点 - * - * @param key - * @return - */ - public Node get(String key) { - if (circle.isEmpty()) { - return null; - } - long hash = this.hash.hash(key); - if (!circle.containsKey(hash)) { - SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 - hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); - } - return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java b/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java deleted file mode 100644 index 59beaacb..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/consistenthash/Node.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mpush.tools.redis.consistenthash; - -public class Node { - - private String ip; //机器ip - private String name;//名字 - - public Node(String ip, String name) { - this.ip = ip; - this.name = name; - } - - public String getIp() { - return ip; - } - public void setIp(String ip) { - this.ip = ip; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java b/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java deleted file mode 100644 index 71a5765d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/jedis/services/JedisRegisterManager.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.mpush.tools.redis.jedis.services; - -import com.google.common.collect.Lists; -import com.mpush.log.Logs; - -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisRegister; - -import java.util.Collections; -import java.util.List; - -public class JedisRegisterManager implements RedisRegister { - - private static List groups = Lists.newArrayList(); - - /** - * zk 启动的时候需要调用这个 - */ - @Override - public void init(List group) { - if (group == null || group.isEmpty()) { - Logs.REDIS.info("init redis client error, redis server is none."); - throw new RuntimeException("init redis client error, redis server is none."); - } - groups = group; - printGroupList(); - } - - - @Override - public List getGroupList() { - return Collections.unmodifiableList(groups); - } - - private void printGroupList() { - for (RedisGroup app : groups) { - Logs.REDIS.info(Jsons.toJson(app)); - } - } - - public int groupSize() { - return groups.size(); - } - - /** - * 随机获取一个redis 实例 - * - * @param key - * @return - */ - @Override - public RedisNode randomGetRedisNode(String key) { - int size = groupSize(); - if (size == 1) return groups.get(0).get(key); - int i = (int) ((Math.random() % size) * size); - RedisGroup group = groups.get(i); - return group.get(key); - } - - /** - * 写操作的时候,获取所有redis 实例 - * - * @param key - * @return - */ - @Override - public List hashSet(String key) { - List nodeList = Lists.newArrayList(); - for (RedisGroup group : groups) { - RedisNode node = group.get(key); - nodeList.add(node); - } - return nodeList; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java deleted file mode 100644 index 428bf120..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/ListenerDispatcher.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.mpush.tools.redis.listener; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.mpush.log.Logs; - -import com.mpush.tools.redis.manage.RedisManage; -import com.mpush.tools.redis.pubsub.Subscriber; -import com.mpush.tools.thread.threadpool.ThreadPoolManager; - -public class ListenerDispatcher implements MessageListener { - - public static final ListenerDispatcher INSTANCE = new ListenerDispatcher(); - - private Map> subscribes = Maps.newTreeMap(); - - private ListenerDispatcher(){} - - private Executor executor = ThreadPoolManager.redisExecutor; - - @Override - public void onMessage(final String channel, final String message) { - List listeners = subscribes.get(channel); - if (listeners == null) { - Logs.REDIS.info("cannot find listener:%s,%s", channel,message); - return; - } - for (final MessageListener listener : listeners) { - executor.execute(new Runnable() { - @Override - public void run() { - listener.onMessage(channel, message); - } - }); - } - } - - public void subscribe(String channel, MessageListener listener) { - List listeners = subscribes.get(channel); - if (listeners == null) { - listeners = Lists.newArrayList(); - subscribes.put(channel, listeners); - } - listeners.add(listener); - RedisManage.subscribe(Subscriber.holder, channel); - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java b/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java deleted file mode 100644 index 627bfefc..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/listener/MessageListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mpush.tools.redis.listener; - - -public interface MessageListener { - - void onMessage(String channel, String message); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java b/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java deleted file mode 100644 index cc0c9790..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/manage/RedisManage.java +++ /dev/null @@ -1,289 +0,0 @@ -package com.mpush.tools.redis.manage; - -import com.google.common.collect.Sets; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisNode; -import com.mpush.tools.redis.RedisRegister; -import com.mpush.tools.redis.RedisUtil; -import com.mpush.tools.spi.ServiceContainer; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPubSub; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * redis 对外封装接口 - */ -public class RedisManage { - - private static final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - - public static long incr(String key, Integer time) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incr(nodeList, key, time); - } - - public static long incrBy(String key, long delt) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.incrBy(nodeList, key, delt); - } - - /********************* - * k v redis start - ********************************/ - - public static T get(String key, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.get(node, key, clazz); - } - - public static void set(String key, T value) { - set(key, value, null); - } - - public static void set(String key, T value, Integer time) { - String jsonValue = Jsons.toJson(value); - set(key, jsonValue, time); - } - - /** - * @param key - * @param value - * @param time seconds - */ - public static void set(String key, String value, Integer time) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.set(nodeList, key, value, time); - } - - public static void del(String key) { - - List nodeList = redisRegister.hashSet(key); - RedisUtil.del(nodeList, key); - - } - - /*********************k v redis end********************************/ - - - /********************* - * hash redis start - ********************************/ - public static void hset(String namespace, String key, String value) { - - List nodeList = redisRegister.hashSet(key); - RedisUtil.hset(nodeList, namespace, key, value); - - } - - public static void hset(String namespace, String key, T value) { - hset(namespace, key, Jsons.toJson(value)); - } - - public static T hget(String namespace, String key, Class clazz) { - - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hget(node, namespace, key, clazz); - - } - - public static void hdel(String namespace, String key) { - List nodeList = redisRegister.hashSet(namespace); - RedisUtil.hdel(nodeList, namespace, key); - } - - public static Map hgetAll(String namespace) { - - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace); - - } - - public static Map hgetAll(String namespace, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hgetAll(node, namespace, clazz); - } - - /** - * 返回 key 指定的哈希集中所有字段的名字。 - * - * @return - */ - public static Set hkeys(String namespace) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hkeys(node, namespace); - } - - /** - * 返回 key 指定的哈希集中指定字段的值 - * - * @param key - * @param clazz - * @return - */ - public static List hmget(String namespace, Class clazz, String... key) { - RedisNode node = redisRegister.randomGetRedisNode(namespace); - return RedisUtil.hmget(node, namespace, clazz, key); - } - - /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 - * - * @param hash - * @param time - */ - public static void hmset(String namespace, Map hash, Integer time) { - List nodeList = redisRegister.hashSet(namespace); - RedisUtil.hmset(nodeList, namespace, hash, time); - } - - public static void hmset(String namespace, Map hash) { - hmset(namespace, hash, null); - } - - - /*********************hash redis end********************************/ - - - /*********************list redis start********************************/ - /** - * 从队列的左边入队 - */ - public static void lpush(String key, String value) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.lpush(nodeList, key, value); - } - - public static void lpush(String key, T value) { - lpush(key, Jsons.toJson(value)); - } - - /** - * 从队列的右边入队 - */ - public static void rpush(String key, String value) { - List nodeList = redisRegister.hashSet(key); - RedisUtil.rpush(nodeList, key, value); - } - - public static void rpush(String key, T value) { - rpush(key, Jsons.toJson(value)); - } - - /** - * 移除并且返回 key 对应的 list 的第一个元素 - */ - public static T lpop(String key, Class clazz) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.lpop(nodeList, key, clazz); - } - - /** - * 从队列的右边出队一个元素 - */ - public static T rpop(String key, Class clazz) { - List nodeList = redisRegister.hashSet(key); - return RedisUtil.rpop(nodeList, key, clazz); - } - - - /** - * 从列表中获取指定返回的元素 - * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List lrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.lrange(node, key, start, end, clazz); - } - - /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 - */ - public static long llen(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.llen(node, key); - } - - public static void lrem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.lRem(nodeList, key, jsonValue); - } - - public static void publish(String channel, T message) { - - RedisNode node = redisRegister.randomGetRedisNode(channel); - RedisUtil.publish(node, channel, message); - - } - - public static void subscribe(JedisPubSub pubsub, String... channels) { - - Set set = Sets.newHashSet(); - for (String channel : channels) { - List nodeList = redisRegister.hashSet(channel); - set.addAll(nodeList); - } - - RedisUtil.subscribe(set, pubsub, channels); - } - - public static void sAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sAdd(nodeList, key, jsonValue); - } - - public static Long sCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.sCard(node, key); - } - - public static void sRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.sRem(nodeList, key, jsonValue); - } - - public static List sScan(String key, int start, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.sScan(node, key, clazz, start); - } - - public static void zAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.zAdd(nodeList, key, jsonValue); - } - - public static Long zCard(String key) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zCard(node, key); - } - - public static void zRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = redisRegister.hashSet(key); - RedisUtil.zRem(nodeList, key, jsonValue); - } - - public static List zrange(String key, int start, int end, Class clazz) { - RedisNode node = redisRegister.randomGetRedisNode(key); - return RedisUtil.zrange(node, key, start, end, clazz); - } - - public static void test(List groupList) { - for (RedisGroup group : groupList) { - for (RedisNode node : group.getRedisNodeList()) { - Jedis jedis = RedisUtil.getClient(node); - if (jedis == null) throw new RuntimeException("init redis sever error."); - jedis.close(); - } - } - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java b/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java deleted file mode 100644 index 54534522..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/redis/pubsub/Subscriber.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.mpush.tools.redis.pubsub; - -import com.mpush.log.Logs; - -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.listener.ListenerDispatcher; - -import redis.clients.jedis.JedisPubSub; - -public class Subscriber extends JedisPubSub { - - private static ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE; - - public static Subscriber holder = new Subscriber(); - - private Subscriber(){} - - @Override - public void onMessage(String channel, String message) { - Logs.REDIS.info("onMessage:{},{}", channel,message); - dispatcher.onMessage(channel, message); - super.onMessage(channel, message); - } - - @Override - public void onPMessage(String pattern, String channel, String message) { - Logs.REDIS.info("onPMessage:{},{},{}",pattern,channel,message); - super.onPMessage(pattern, channel, message); - } - - @Override - public void onPSubscribe(String pattern, int subscribedChannels) { - Logs.REDIS.info("onPSubscribe:{},{}",pattern,subscribedChannels); - super.onPSubscribe(pattern, subscribedChannels); - } - - @Override - public void onPUnsubscribe(String pattern, int subscribedChannels) { - Logs.REDIS.info("onPUnsubscribe:{},{}",pattern,subscribedChannels); - super.onPUnsubscribe(pattern, subscribedChannels); - } - - @Override - public void onSubscribe(String channel, int subscribedChannels) { - Logs.REDIS.info("onSubscribe:{},{}",channel,subscribedChannels); - super.onSubscribe(channel, subscribedChannels); - } - - @Override - public void onUnsubscribe(String channel, int subscribedChannels) { - Logs.REDIS.info("onUnsubscribe:{},{}",channel,subscribedChannels); - super.onUnsubscribe(channel, subscribedChannels); - } - - - @Override - public void unsubscribe() { - Logs.REDIS.info("unsubscribe"); - super.unsubscribe(); - } - - @Override - public void unsubscribe(String... channels) { - Logs.REDIS.info("unsubscribe:{}", Jsons.toJson(channels)); - super.unsubscribe(channels); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java b/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java deleted file mode 100644 index e212649e..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/spi/SPI.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.mpush.tools.spi; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE }) -public @interface SPI { - - String value() default ""; - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 39fa5f51..4e4f407e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -1,47 +1,56 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.thread; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -public class NamedThreadFactory implements ThreadFactory{ - - private static final AtomicInteger poolNum = new AtomicInteger(1); - - private final AtomicInteger threadNum = new AtomicInteger(1); - - private final ThreadGroup group; - private final String namePre; - private final boolean isDaemon; - - public NamedThreadFactory(){ - this("pool"); - } - - public NamedThreadFactory(String prefix){ - this(prefix,true); - } - - public NamedThreadFactory(String prefix,boolean daemon) { - SecurityManager manager = System.getSecurityManager(); - if(manager!=null){ - group = manager.getThreadGroup(); - }else{ - group = Thread.currentThread().getThreadGroup(); - } - isDaemon = daemon; - namePre = prefix+"-"+poolNum.getAndIncrement()+"-thread-"; - } - - /** - * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 - */ - @Override - public Thread newThread(Runnable runnable) { - Thread t = new Thread(group, runnable,namePre+threadNum.getAndIncrement(),0); - t.setContextClassLoader(NamedThreadFactory.class.getClassLoader()); - t.setPriority(Thread.MAX_PRIORITY); - t.setDaemon(isDaemon); - return t; - } +import static com.mpush.tools.thread.ThreadNames.THREAD_NAME_PREFIX; + +/** + * Created by xiaoxu.yxx on 2015/7/19. + */ +public final class NamedThreadFactory implements ThreadFactory { + protected final AtomicInteger threadNumber = new AtomicInteger(1); + protected final String namePrefix; + protected final ThreadGroup group; + + + public NamedThreadFactory() { + this(THREAD_NAME_PREFIX); + } + + public NamedThreadFactory(final String namePrefix) { + this.namePrefix = namePrefix; + this.group = Thread.currentThread().getThreadGroup(); + } + + public Thread newThread(String name, Runnable r) { + return new Thread(group, r, name); + } + @Override + public Thread newThread(Runnable r) { + Thread t = newThread(namePrefix + threadNumber.getAndIncrement(), r); + if (t.isDaemon()) + t.setDaemon(false); + return t; + } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java new file mode 100644 index 00000000..adb945ba --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java @@ -0,0 +1,61 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class PoolThreadFactory implements ThreadFactory { + private static final AtomicInteger poolNum = new AtomicInteger(1); + + private final AtomicInteger threadNum = new AtomicInteger(1); + + private final ThreadGroup group; + private final String namePre; + private final boolean isDaemon; + + public PoolThreadFactory(String prefix) { + this(prefix, true); + } + + public PoolThreadFactory(String prefix, boolean daemon) { + SecurityManager manager = System.getSecurityManager(); + if (manager != null) { + group = manager.getThreadGroup(); + } else { + group = Thread.currentThread().getThreadGroup(); + } + isDaemon = daemon; + namePre = prefix + "p-" + poolNum.getAndIncrement() + "-t-"; + } + + /** + * stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数 + */ + @Override + public Thread newThread(Runnable runnable) { + Thread t = new Thread(group, runnable, namePre + threadNum.getAndIncrement(), 0); + t.setContextClassLoader(PoolThreadFactory.class.getClassLoader()); + t.setPriority(Thread.MAX_PRIORITY); + t.setDaemon(isDaemon); + return t; + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java deleted file mode 100644 index 63fe7d72..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNameSpace.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.tools.thread; - -public class ThreadNameSpace { - - /** - * netty boss 线程 - */ - public static final String NETTY_BOSS = "mp-boss"; - - /** - * netty worker 线程 - */ - public static final String NETTY_WORKER = "mp-worker"; - - public static final String NETTY_HTTP = "mp-http"; - - public static final String EVENT_BUS = "mp-event-bus"; - - public static final String REDIS = "mp-redis"; - - public static final String ZK = "mp-zk"; - - public static final String BIZ = "mp-biz"; - - /** - * connection 定期检测线程 - */ - public static final String NETTY_TIMER = "mp-timer"; - - public static final String getUniqueName(String serviceName) { - return "mp-sn-" + serviceName; - } - - public static final String THREAD_NAME_PREFIX = "mp-t-"; - - public static final String getServerName(String serverName){ - if(serverName.equals("ConnectionServer")){ - return "mp-start-cs"; - }else if(serverName.equals("GatewayServer")){ - return "mp-start-gs"; - }else{ - return "mp-start-unknow"; - } - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java new file mode 100644 index 00000000..a1d49ad6 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -0,0 +1,53 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread; + +public final class ThreadNames { + public static final String NS = "mp"; + public static final String THREAD_NAME_PREFIX = NS + "-t-"; + + /** + * netty boss 线程 + */ + public static final String T_SERVER_BOSS = NS + "-boss-"; + + /** + * netty worker 线程 + */ + public static final String T_SERVER_WORKER = NS + "-worker-"; + + public static final String T_HTTP_CLIENT = NS + "-http-"; + + public static final String T_EVENT_BUS = NS + "-event-"; + + public static final String T_MQ = NS + "-mq-"; + + public static final String T_ZK = NS + "-zk-"; + + public static final String T_BIZ = NS + "-biz-"; + public static final String T_PUSH_CALLBACK = NS + "-push-cb-"; + public static final String T_PUSH_REQ_TIMER = NS + "-push-timer-"; + + /** + * connection 定期检测线程 + */ + public static final String T_NETTY_TIMER = NS + "-timer-"; + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java new file mode 100644 index 00000000..aad039ea --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java @@ -0,0 +1,34 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ +package com.mpush.tools.thread.pool; + +import java.util.concurrent.*; + +/** + * Created by yxx on 2016/5/29. + * + * @author ohun@live.cn (夜色) + */ +public class DefaultExecutor extends ThreadPoolExecutor { + + public DefaultExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java new file mode 100644 index 00000000..ef83ac45 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java @@ -0,0 +1,120 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.PoolThreadFactory; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import static com.mpush.tools.thread.ThreadNames.*; + +/** + * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 + */ +public class DefaultThreadPoolFactory implements ThreadPoolFactory { + + private Executor get(ThreadPoolConfig config) { + String name = config.getName(); + int corePoolSize = config.getCorePoolSize(); + int maxPoolSize = config.getMaxPoolSize(); + int keepAliveSeconds = config.getKeepAliveSeconds(); + + BlockingQueue queue = config.getQueue(); + ThreadFactory threadFactory = new PoolThreadFactory(name); + + return new DefaultExecutor(corePoolSize + , maxPoolSize + , keepAliveSeconds + , TimeUnit.SECONDS + , queue + , threadFactory + , new DumpThreadRejectedHandler(config)); + } + + @Override + public Executor get(String name) { + final ThreadPoolConfig config; + switch (name) { + case SERVER_BOSS: + config = ThreadPoolConfig + .build(T_SERVER_BOSS) + .setCorePoolSize(CC.mp.thread.pool.boss.min) + .setMaxPoolSize(CC.mp.thread.pool.boss.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.boss.queue_size); + break; + case SERVER_WORK: + config = ThreadPoolConfig + .build(T_SERVER_WORKER) + .setCorePoolSize(CC.mp.thread.pool.work.min) + .setMaxPoolSize(CC.mp.thread.pool.work.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.work.queue_size); + break; + case HTTP_CLIENT_WORK: + config = ThreadPoolConfig + .buildFixed(T_HTTP_CLIENT, + CC.mp.thread.pool.http_proxy.min, + CC.mp.thread.pool.http_proxy.queue_size + ) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_DISCARD); + break; + case EVENT_BUS: + config = ThreadPoolConfig + .buildFixed(T_EVENT_BUS, + CC.mp.thread.pool.event_bus.min, + CC.mp.thread.pool.event_bus.queue_size + ); + break; + case MQ: + config = ThreadPoolConfig + .buildFixed(T_MQ, + CC.mp.thread.pool.mq.min, + CC.mp.thread.pool.mq.queue_size + ); + break; + case PUSH_CALLBACK: + config = ThreadPoolConfig + .build(T_PUSH_CALLBACK) + .setCorePoolSize(CC.mp.thread.pool.push_callback.min) + .setMaxPoolSize(CC.mp.thread.pool.push_callback.max) + .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) + .setQueueCapacity(CC.mp.thread.pool.push_callback.queue_size) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); + break; + default: + case BIZ: + config = ThreadPoolConfig + .build(T_BIZ) + .setCorePoolSize(CC.mp.thread.pool.biz.min) + .setMaxPoolSize(CC.mp.thread.pool.biz.max) + .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) + .setQueueCapacity(CC.mp.thread.pool.biz.queue_size); + break; + } + + return get(config); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java new file mode 100644 index 00000000..f5d5165d --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -0,0 +1,74 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import com.mpush.tools.common.JVMUtil; +import com.mpush.tools.config.CC; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; + +import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_ABORT; +import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS; + +public class DumpThreadRejectedHandler implements RejectedExecutionHandler { + + private final static Logger LOGGER = LoggerFactory.getLogger(DumpThreadRejectedHandler.class); + + private volatile boolean dumping = false; + + private static final String DUMP_DIR = CC.mp.monitor.dump_dir; + + private final ThreadPoolConfig poolConfig; + + private final int rejectedPolicy; + + public DumpThreadRejectedHandler(ThreadPoolConfig poolConfig) { + this.poolConfig = poolConfig; + this.rejectedPolicy = poolConfig.getRejectedPolicy(); + } + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { + LOGGER.warn("one task rejected, poolConfig={}, poolInfo={}", poolConfig, ThreadPoolManager.getPoolInfo(e)); + if (!dumping) { + dumping = true; + dumpJVMInfo(); + } + + if (rejectedPolicy == REJECTED_POLICY_ABORT) { + throw new RejectedExecutionException("one task rejected, pool=" + poolConfig.getName()); + } else if (rejectedPolicy == REJECTED_POLICY_CALLER_RUNS) { + if (!e.isShutdown()) { + r.run(); + } + } + } + + private void dumpJVMInfo() { + LOGGER.error("start dump jvm info"); + JVMUtil.dumpJstack(DUMP_DIR + "/" + poolConfig.getName()); + LOGGER.error("end dump jvm info"); + } +} + diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java new file mode 100644 index 00000000..d82cf1a0 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -0,0 +1,135 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; + +public class ThreadPoolConfig { + public static final int REJECTED_POLICY_ABORT = 0; + public static final int REJECTED_POLICY_DISCARD = 1; + public static final int REJECTED_POLICY_CALLER_RUNS = 2; + private String name;//名字 + private int corePoolSize; //最小线程大小 + private int maxPoolSize; //最大线程大小 + private int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) + private int keepAliveSeconds;// 存活时间 + private int rejectedPolicy = REJECTED_POLICY_ABORT; + + public ThreadPoolConfig(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public ThreadPoolConfig setName(String name) { + this.name = name; + return this; + } + + public int getCorePoolSize() { + return corePoolSize; + } + + public ThreadPoolConfig setCorePoolSize(int corePoolSize) { + this.corePoolSize = corePoolSize; + return this; + } + + public int getMaxPoolSize() { + return maxPoolSize; + } + + public ThreadPoolConfig setMaxPoolSize(int maxPoolSize) { + this.maxPoolSize = maxPoolSize; + return this; + } + + public int getQueueCapacity() { + return queueCapacity; + } + + public ThreadPoolConfig setQueueCapacity(int queueCapacity) { + this.queueCapacity = queueCapacity; + return this; + } + + public int getKeepAliveSeconds() { + return keepAliveSeconds; + } + + public ThreadPoolConfig setKeepAliveSeconds(long keepAliveSeconds) { + this.keepAliveSeconds = (int) keepAliveSeconds; + return this; + } + + public int getRejectedPolicy() { + return rejectedPolicy; + } + + public ThreadPoolConfig setRejectedPolicy(int rejectedPolicy) { + this.rejectedPolicy = rejectedPolicy; + return this; + } + + public static ThreadPoolConfig buildFixed(String name, int threads, int queueCapacity) { + return new ThreadPoolConfig(name) + .setCorePoolSize(threads) + .setMaxPoolSize(threads) + .setQueueCapacity(queueCapacity) + .setKeepAliveSeconds(0); + } + + public static ThreadPoolConfig buildCached(String name) { + return new ThreadPoolConfig(name) + .setKeepAliveSeconds(0); + } + + public static ThreadPoolConfig build(String name) { + return new ThreadPoolConfig(name); + } + + + public BlockingQueue getQueue() { + BlockingQueue blockingQueue; + if (queueCapacity == 0) { + blockingQueue = new SynchronousQueue<>(); + } else if (queueCapacity < 0) { + blockingQueue = new LinkedBlockingQueue<>(); + } else { + blockingQueue = new LinkedBlockingQueue<>(queueCapacity); + } + return blockingQueue; + } + + @Override + public String toString() { + return "ThreadPoolConfig{" + + "name='" + name + '\'' + + ", corePoolSize=" + corePoolSize + + ", maxPoolSize=" + maxPoolSize + + ", queueCapacity=" + queueCapacity + + ", keepAliveSeconds=" + keepAliveSeconds + + '}'; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java new file mode 100644 index 00000000..45b28813 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -0,0 +1,135 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.thread.pool; + +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.tools.thread.NamedThreadFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import static com.mpush.tools.config.CC.mp.spi.thread_pool_factory; + +public class ThreadPoolManager { + public static final ThreadPoolManager I = new ThreadPoolManager(); + + private final ThreadPoolFactory threadPoolFactory = SpiLoader.load(ThreadPoolFactory.class, thread_pool_factory); + private final NamedThreadFactory threadFactory = new NamedThreadFactory(); + + private Executor bossExecutor; + private Executor workExecutor; + private Executor bizExecutor; + private Executor eventBusExecutor; + private Executor redisExecutor; + private Executor httpExecutor; + private Executor pushCallbackExecutor; + + public final Thread newThread(String name, Runnable target) { + return threadFactory.newThread(name, target); + } + + public Executor getHttpExecutor() { + if (httpExecutor == null) { + synchronized (this) { + httpExecutor = threadPoolFactory.get(ThreadPoolFactory.HTTP_CLIENT_WORK); + } + } + return httpExecutor; + } + + public Executor getRedisExecutor() { + if (redisExecutor == null) { + synchronized (this) { + redisExecutor = threadPoolFactory.get(ThreadPoolFactory.MQ); + } + } + return redisExecutor; + } + + public Executor getEventBusExecutor() { + if (eventBusExecutor == null) { + synchronized (this) { + eventBusExecutor = threadPoolFactory.get(ThreadPoolFactory.EVENT_BUS); + } + } + return eventBusExecutor; + } + + public Executor getBizExecutor() { + if (bizExecutor == null) { + synchronized (this) { + bizExecutor = threadPoolFactory.get(ThreadPoolFactory.BIZ); + } + } + return bizExecutor; + } + + public Executor getWorkExecutor() { + if (workExecutor == null) { + synchronized (this) { + workExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_WORK); + } + } + return workExecutor; + } + + public Executor getBossExecutor() { + if (bossExecutor == null) { + synchronized (this) { + bossExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_BOSS); + } + } + return bossExecutor; + } + + public Executor getPushCallbackExecutor() { + if (pushCallbackExecutor == null) { + synchronized (this) { + pushCallbackExecutor = threadPoolFactory.get(ThreadPoolFactory.PUSH_CALLBACK); + } + } + return pushCallbackExecutor; + } + + public Map getActivePools() { + Map map = new HashMap<>(); + if (bossExecutor != null) map.put("bossExecutor", bossExecutor); + if (workExecutor != null) map.put("workExecutor", workExecutor); + if (bizExecutor != null) map.put("bizExecutor", bizExecutor); + if (eventBusExecutor != null) map.put("eventBusExecutor", eventBusExecutor); + if (redisExecutor != null) map.put("redisExecutor", redisExecutor); + if (httpExecutor != null) map.put("httpExecutor", httpExecutor); + if (pushCallbackExecutor != null) map.put("pushCallbackExecutor", pushCallbackExecutor); + return map; + } + + public static Map getPoolInfo(ThreadPoolExecutor executor) { + Map info = new HashMap<>(); + info.put("corePoolSize", executor.getCorePoolSize()); + info.put("maxPoolSize", executor.getMaximumPoolSize()); + info.put("activeCount(workingThread)", executor.getActiveCount()); + info.put("poolSize(workThread)", executor.getPoolSize()); + info.put("queueSize(blockedTask)", executor.getQueue().size()); + return info; + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java deleted file mode 100644 index 88255500..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/IgnoreRunsPolicy.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadPoolExecutor; - -import com.mpush.tools.JVMUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.config.ConfigCenter; - -public class IgnoreRunsPolicy implements RejectedExecutionHandler{ - - private final static Logger log = LoggerFactory.getLogger(IgnoreRunsPolicy.class); - - private volatile boolean dump = false; - - private static final String preFixPath = ConfigCenter.I.logPath(); - - private final ThreadPoolContext context; - - public IgnoreRunsPolicy(ThreadPoolContext context) { - this.context = context; - } - - @Override - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - dumpJVMInfo(); - throw new RejectedExecutionException(); - } - - private void dumpJVMInfo(){ - if (!dump) { - dump = true; - log.error("start dump jvm info"); - JVMUtil.dumpJstack(preFixPath+"/"+context.getName()); - log.error("end dump jvm info"); - } - } -} - diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java deleted file mode 100644 index da079304..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPool.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import java.util.concurrent.Executor; - -import com.mpush.tools.spi.SPI; - -@SPI("cachedThreadPool") -public interface ThreadPool { - - public Executor getExecutor(ThreadPoolContext context); - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java deleted file mode 100644 index 5823bad0..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolContext.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import com.mpush.tools.Constants; -import com.mpush.tools.thread.ThreadNameSpace; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; - - -public class ThreadPoolContext { - - private final String name;//名字 - private final int corePoolSize; //最小线程大小 - private final int maxPoolSize; //最大线程大小 - private final int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数) - private final int keepAliveSeconds;// 存活时间 - - public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POOL_SIZE, 60*5,Constants.BOSS_THREAD_QUEUE_SIZE); - - public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5,Constants.WORK_THREAD_QUEUE_SIZE); - - public static ThreadPoolContext HTTP_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.NETTY_HTTP, Constants.BIZ_POOL_SIZE); - - public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE); - - public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE); - - public static ThreadPoolContext REDIS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.REDIS, Constants.REDIS_POOL_SIZE,Constants.REDIS_THREAD_QUEUE_SIZE); - - public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) { - this.name = name; - this.corePoolSize = corePoolSize; - this.maxPoolSize = maxPoolSize; - this.queueCapacity = queueCapacity; - this.keepAliveSeconds = keepAliveSeconds; - } - - public String getName() { - return name; - } - - public int getCorePoolSize() { - return corePoolSize; - } - - public int getMaxPoolSize() { - return maxPoolSize; - } - - public int getQueueCapacity() { - return queueCapacity; - } - - public int getKeepAliveSeconds() { - return keepAliveSeconds; - } - - @Override - public String toString() { - return "ThreadPoolContext [name=" + name + ", corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity + ", keepAliveSeconds=" + keepAliveSeconds - + "]"; - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java deleted file mode 100644 index db2b1d79..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/ThreadPoolManager.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mpush.tools.thread.threadpool; - -import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Executor; - -public class ThreadPoolManager { - - private static final Map poolCache = new HashMap(); - - private static ThreadPool cachedThreadPool = new CachedThreadPool(); - - private static ThreadPool fixedThreadPool = new FixedThreadPool(); - - public static Executor bossExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.BOSS_THREAD_POOL); - public static Executor workExecutor = cachedThreadPool.getExecutor(ThreadPoolContext.WORK_THREAD_POOL); - public static Executor bizExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.BIZ_THREAD_POOL); - public static Executor eventBusExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.EVENT_BUS_THREAD_POOL); - public static Executor redisExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.REDIS_THREAD_POOL); - - public static Executor httpExecutor = fixedThreadPool.getExecutor(ThreadPoolContext.HTTP_THREAD_POOL); - - static { - poolCache.put(ThreadPoolContext.BOSS_THREAD_POOL.getName(), bossExecutor); - poolCache.put(ThreadPoolContext.WORK_THREAD_POOL.getName(), workExecutor); - poolCache.put(ThreadPoolContext.BIZ_THREAD_POOL.getName(), bizExecutor); - poolCache.put(ThreadPoolContext.EVENT_BUS_THREAD_POOL.getName(), eventBusExecutor); - poolCache.put(ThreadPoolContext.REDIS_THREAD_POOL.getName(), redisExecutor); - poolCache.put(ThreadPoolContext.HTTP_THREAD_POOL.getName(), httpExecutor); - } - - public static final Map getPool() { - return poolCache; - } - - public static final Thread newThread(String name, Runnable target) { - Thread thread = new Thread(target, name); - return thread; - } - - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java deleted file mode 100644 index e432d46d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPool.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.mpush.tools.thread.threadpool.cached; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.mpush.tools.thread.threadpool.ThreadPoolContext; -import com.mpush.tools.thread.threadpool.ThreadPool; - -/** - * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 - * - */ -public class CachedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - - String name = context.getName(); - int corePoolSize = context.getCorePoolSize(); - int maxPoolSize = context.getMaxPoolSize(); - int queueCapacity = context.getQueueCapacity(); - int keepAliveSeconds = context.getKeepAliveSeconds(); - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - BlockingQueue blockingQueue = null; - if(queueCapacity == 0){ - blockingQueue = new SynchronousQueue(); - }else if(queueCapacity<0){ - blockingQueue = new LinkedBlockingQueue(); - }else{ - blockingQueue = new LinkedBlockingQueue(queueCapacity); - } - - return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java deleted file mode 100644 index def69d45..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/cached/CachedThreadPoolContext.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.tools.thread.threadpool.cached; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -public class CachedThreadPoolContext extends ThreadPoolContext { - - public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) { - this(name, corePoolSize, maxPoolSize, keepAliveSeconds, 0); - } - - public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity) { - super(name, corePoolSize, maxPoolSize, queueCapacity, keepAliveSeconds); - } - - public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds){ - return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds); - } - - public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds,int queueCapacity){ - return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds,queueCapacity); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java deleted file mode 100644 index 743ec71d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPool.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mpush.tools.thread.threadpool.fixed; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import com.mpush.tools.thread.NamedThreadFactory; -import com.mpush.tools.thread.threadpool.IgnoreRunsPolicy; -import com.mpush.tools.thread.threadpool.ThreadPool; -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -/** - *此线程池启动时即创建固定大小的线程数,不做任何伸缩 - * - */ -public class FixedThreadPool implements ThreadPool { - - @Override - public Executor getExecutor(ThreadPoolContext context) { - String name = context.getName(); - int corePoolSize = context.getCorePoolSize(); - int queueCapacity = context.getQueueCapacity(); - - BlockingQueue blockingQueue = null; - if (queueCapacity == 0) { - blockingQueue = new SynchronousQueue(); - } else if (queueCapacity < 0) { - blockingQueue = new LinkedBlockingQueue(); - } else { - blockingQueue = new LinkedBlockingQueue(queueCapacity); - } - - final ThreadFactory threadFactory = new NamedThreadFactory(name); - - return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context)); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java b/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java deleted file mode 100644 index 09cc9305..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/threadpool/fixed/FixedThreadPoolContext.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mpush.tools.thread.threadpool.fixed; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; - -public class FixedThreadPoolContext extends ThreadPoolContext { - - public FixedThreadPoolContext(String name, int threads,int queueCapacity) { - super(name, threads, 0, queueCapacity, 0); - } - - public FixedThreadPoolContext(String name, int threads) { - super(name, threads, 0, -1, 0); - } - - public static FixedThreadPoolContext create(String name,int threads){ - return new FixedThreadPoolContext(name, threads); - } - - public static FixedThreadPoolContext create(String name,int threads,int queueCapacity){ - return new FixedThreadPoolContext(name, threads,queueCapacity); - } - -} diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory new file mode 100644 index 00000000..be69a4a6 --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory @@ -0,0 +1 @@ +com.mpush.tools.thread.pool.DefaultThreadPoolFactory \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister b/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister deleted file mode 100644 index ff33af00..00000000 --- a/mpush-tools/src/main/resources/META-INF/services/com.mpush.tools.redis.RedisRegister +++ /dev/null @@ -1 +0,0 @@ -com.mpush.tools.redis.jedis.services.JedisRegisterManager \ No newline at end of file diff --git a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java index cdab4c60..7e63de28 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/IOUtilsTest.java @@ -1,5 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools; +import com.mpush.tools.common.IOUtils; import org.junit.Test; /** @@ -46,12 +66,12 @@ public void testCompress() throws Exception { for (int i = 0; i < 100; i++) { IOUtils.compress(s); } - System.out.println((System.currentTimeMillis()-t1)/100); + System.out.println((System.currentTimeMillis() - t1) / 100); System.out.println("src:" + s.length); byte[] out = IOUtils.compress(s); System.out.println("compress:" + out.length); - byte[] ss = IOUtils.uncompress(out); - System.out.println("uncompress:" + ss.length); + byte[] ss = IOUtils.decompress(out); + System.out.println("decompress:" + ss.length); } @Test diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java index 36d4b0d6..a57f89dd 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/AESUtilsTest.java @@ -1,6 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.tools.crypto; -import com.mpush.tools.Constants; +import com.mpush.api.Constants; import org.junit.Test; import java.util.Random; diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java index 84c7eec6..d723f7c6 100644 --- a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java +++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java @@ -1,7 +1,25 @@ -package com.mpush.tools.crypto; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ -import com.mpush.tools.Pair; +package com.mpush.tools.crypto; +import com.mpush.tools.common.Pair; import org.junit.Before; import org.junit.Test; diff --git a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java b/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java deleted file mode 100644 index 45d7a82f..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/delayqueue/Exam.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.mpush.tools.delayqueue; - -import java.util.Iterator; -import java.util.Random; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.DelayQueue; -import java.util.concurrent.Delayed; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * 考试时间为120分钟,30分钟后才可交卷,初始化考生完成试卷时间最小应为30分钟 - * 对于能够在120分钟内交卷的考生,如何实现这些考生交卷 - * 对于120分钟内没有完成考试的考生,在120分钟考试时间到后需要让他们强制交卷 - * 在所有的考生都交完卷后,需要将控制线程关闭 - * - * 实现思想:用DelayQueue存储考生(Student类),每一个考生都有自己的名字和完成试卷的时间, - * Teacher线程对DelayQueue进行监控,收取完成试卷小于120分钟的学生的试卷。 - * 当考试时间120分钟到时,先关闭Teacher线程,然后强制DelayQueue中还存在的考生交卷。 - * 每一个考生交卷都会进行一次countDownLatch.countDown(),当countDownLatch.await()不再阻塞说明所有考生都交完卷了,而后结束考试。 - * - */ - -public class Exam { - - public static void main(String[] args) throws InterruptedException { - int studentNumber = 20; - CountDownLatch countDownLatch = new CountDownLatch(studentNumber+1); - DelayQueue students = new DelayQueue(); - Random random = new Random(); - for (int i = 0; i < studentNumber; i++) { - students.put(new Student("student"+(i+1), 30+random.nextInt(120),countDownLatch)); - } - Thread teacherThread =new Thread(new Teacher(students)); - students.put(new EndExam(students, 120,countDownLatch,teacherThread)); - teacherThread.start(); - countDownLatch.await(); - System.out.println(" 考试时间到,全部交卷!"); - } - -} - -class Student implements Runnable,Delayed{ - - private String name; - private long workTime; //考试时间 - private long submitTime; //交卷时间 - private boolean isForce = false; - private CountDownLatch countDownLatch; - - public Student() { - } - - public Student(String name,long workTime,CountDownLatch countDownLatch){ - this.name = name; - this.workTime = workTime; - this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS)+System.nanoTime(); - this.countDownLatch = countDownLatch; - } - - @Override - public int compareTo(Delayed o) { - if(o == null || ! (o instanceof Student)) return 1; - if(o == this) return 0; - Student s = (Student)o; - if (this.workTime > s.workTime) { - return 1; - }else if (this.workTime == s.workTime) { - return 0; - }else { - return -1; - } - } - - @Override - public long getDelay(TimeUnit unit) { - return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS); - } - - @Override - public void run() { - if (isForce) { - System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 120分钟" ); - }else { - System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 "+workTime +" 分钟"); - } - countDownLatch.countDown(); - } - - public boolean isForce() { - return isForce; - } - - public void setForce(boolean isForce) { - this.isForce = isForce; - } - -} - -class EndExam extends Student{ - - private DelayQueue students; - private CountDownLatch countDownLatch; - private Thread teacherThread; - - public EndExam(DelayQueue students, long workTime, CountDownLatch countDownLatch,Thread teacherThread) { - super("强制收卷", workTime,countDownLatch); - this.students = students; - this.countDownLatch = countDownLatch; - this.teacherThread = teacherThread; - } - - - - @Override - public void run() { - // TODO Auto-generated method stub - - teacherThread.interrupt(); - Student tmpStudent; - for (Iterator iterator2 = students.iterator(); iterator2.hasNext();) { - tmpStudent = iterator2.next(); - tmpStudent.setForce(true); - tmpStudent.run(); - } - countDownLatch.countDown(); - } - -} - - -class Teacher implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Teacher.class); - - private DelayQueue students; - - public Teacher(DelayQueue students) { - this.students = students; - } - - @Override - public void run() { - try{ - log.warn(" test start "); - while(!Thread.interrupted()){ - students.take().run(); - } - }catch(Exception e){ - log.warn("exception :",e); - } - } -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java b/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java deleted file mode 100644 index f9907f95..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/OwnerTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.mpush.tools.owner; - -import com.mpush.tools.config.ConfigCenter; -import org.aeonbits.owner.ConfigFactory; -import org.junit.Test; - -public class OwnerTest { - - @Test - public void test1(){ - - ServerConfig cfg = ConfigFactory.create(ServerConfig.class); - - System.out.println(cfg.zkDigest()); - - System.out.println(cfg.zkIp()); - - System.out.println(cfg.hello()); - - System.out.println(cfg.maxHbTimeoutTimes()); - - System.out.println(cfg.test()); - - Integer tset = cfg.testnotexist(); - if(tset == null){ - System.out.println("not exist"); - }else{ - System.out.println(tset); - } - } - - @Test - public void test2(){ - - System.out.println(ConfigCenter.I.zkIp()); - - System.out.println("aesKeyLength:"+ConfigCenter.I.aesKeyLength()); - - System.out.println("compressLimit:"+ConfigCenter.I.compressLimit()); - - System.out.println("connectionServerPort:"+ConfigCenter.I.connectionServerPort()); - - System.out.println("gatewayServerPort:"+ConfigCenter.I.gatewayServerPort()); - - System.out.println("maxHBTimeoutTimes:"+ConfigCenter.I.maxHBTimeoutTimes()); - - System.out.println(ConfigCenter.I.maxHeartbeat()); - - System.out.println(ConfigCenter.I.maxPacketSize()); - - System.out.println(ConfigCenter.I.minHeartbeat()); - - System.out.println(ConfigCenter.I.privateKey()); - - System.out.println(ConfigCenter.I.publicKey()); - - System.out.println(ConfigCenter.I.rsaKeyLength()); - - System.out.println(ConfigCenter.I.redisIp()); - - System.out.println(ConfigCenter.I.sessionExpiredTime()); - - System.out.println(ConfigCenter.I.zkDigest()); - - - - System.out.println(ConfigCenter.I.zkNamespace()); - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java b/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java deleted file mode 100644 index e3060630..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/owner/ServerConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.mpush.tools.owner; - -import org.aeonbits.owner.Config; -import org.aeonbits.owner.Config.Sources; - - -@Sources({"classpath:serverconfig.properties"}) -public interface ServerConfig extends Config{ - - @Key("zk_ip") - @DefaultValue("zkIp") - public String zkIp(); - - @Key("zk_digest") - public String zkDigest(); - - @Key("hello") - @DefaultValue("hello world") - public String hello(); - - @Key("max_hb_timeout_times") - public int maxHbTimeoutTimes(); - - @Key("test") - @DefaultValue("10") - public int test(); - - public Integer testnotexist(); - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java b/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java deleted file mode 100644 index 9cf9a807..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/SpiTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mpush.tools.spi; - - -import com.mpush.tools.spi.test.TestService; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - - -public class SpiTest { - - private static Executor pool = Executors.newCachedThreadPool(); - - @Test - public void baseTest() { - TestService testService = ServiceContainer.load(TestService.class); - System.out.println(testService.sayHi(" huang")); - - ServiceContainer.load(TestService.class); - } - - @Ignore - @Test - public void listTest() { - - - } - - @Ignore - @Test - public void mulThreadTest() throws InterruptedException { - pool.execute(new Worker()); - pool.execute(new Worker()); - pool.execute(new ListWorker()); - pool.execute(new ListWorker()); - Thread.sleep(Integer.MAX_VALUE); - } - - - private static final class Worker implements Runnable { - - @Override - public void run() { - } - - } - - private static final class ListWorker implements Runnable { - - @Override - public void run() { - - - } - - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java deleted file mode 100644 index 8460ca51..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mpush.tools.spi.test; - -import com.mpush.tools.spi.SPI; - - -@SPI("test1") -public interface TestService { - - public String sayHi(String name); - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java deleted file mode 100644 index a23cf509..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mpush.tools.spi.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TestServiceImpl implements TestService { - - private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); - - public TestServiceImpl() { - log.warn("init"); - } - - @Override - public String sayHi(String name) { - return "TestServiceImpl1 hi,"+name; - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java b/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java deleted file mode 100644 index 575e4316..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/spi/test/TestServiceImpl2.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mpush.tools.spi.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class TestServiceImpl2 implements TestService { - - private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); - - public TestServiceImpl2() { - log.warn("init"); - } - - @Override - public String sayHi(String name) { - return "TestServiceImpl2 hi,"+name; - } - -} diff --git a/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java b/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java deleted file mode 100644 index 1e69324d..00000000 --- a/mpush-tools/src/test/java/com/mpush/tools/thread/SyncTest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.mpush.tools.thread; - - -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; - -import com.mpush.tools.thread.threadpool.ThreadPoolContext; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mpush.tools.thread.threadpool.ThreadPool; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPool; -import com.mpush.tools.thread.threadpool.cached.CachedThreadPoolContext; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPool; -import com.mpush.tools.thread.threadpool.fixed.FixedThreadPoolContext; - -public class SyncTest { - - //pri MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIvbj8CvXqWcXAeNw7yruM2TZPfIkp2JIbPwpptAMt6t6zoByRLDjPkbkFVbJrmmto5dGLFCQHz9NM24gb5R9U5dotApgCYFabbocIUx+mkUcd+ui+SZ5yTTyXtVhqKBFGqCTEB73S7b5y0xof/r781EZWYA3sh47pNXVYisRh7rAgMBAAECgYARri8NH95qN0sXFV/iUR8qtfB0tqF6UuS0175oMAR+TCRJkAI4YgpHT6m+cKiDncTEWJaPih2W73embiXQxpGpJt0HKegmKF5RiSU8iXjbFQvmlfTRrgo7qLIjgqUxaM0h+ef0p/T3EV+HZ8sk2bHZPd5OzTcAx1UOSgz88VEDEQJBAONTXI88w+cIkeS5uDDCDjV5S5ljQCBmBTlwIp0UCLDZ0KQDFCiOM54ltgcsMrKQFyj2EwTWsevbikTP3KRmXzMCQQCdf78HkzGnGAJUzPchIHvQBC1Q95X002rYPxNrlF86iU7n1fan++xuGDTYnz+eNRKJFEY85SQq0eld0KI57qFpAkAZ9Lu90ydfKthVsGr6jj3HF0ltgyqgSGXSUB5zpwTzBHvRLlTP6KS2KwIkwYQsZU1vrOExDT6VeqTIBJ/h2ZqHAkAW9dKRdiHc7CEa365/Q88I6jL5BL71rAR9deSM4FppnC7GmWiV4KH9AsZhdgW+OJp1JWF/6x+0pllQ9eNQcrtRAkBczJJ5l3BtCE+VToy16DPPQCfyDpb6dmyR4IkTwECWMomz9jnt1fK7nETfBeeP4ySea8jrUCgZdu06iPtoLCAK - //pub MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL24/Ar16lnFwHjcO8q7jNk2T3yJKdiSGz8KabQDLeres6AckSw4z5G5BVWya5praOXRixQkB8/TTNuIG+UfVOXaLQKYAmBWm26HCFMfppFHHfrovkmeck08l7VYaigRRqgkxAe90u2+ctMaH/6+/NRGVmAN7IeO6TV1WIrEYe6wIDAQAB - - final static ThreadPool cachedThreadPool = new CachedThreadPool(); - final static ThreadPool fixedThreadPool = new FixedThreadPool(); - - // 最小线程1,最大线程2,使用sync blockqueue,线程缓存5分钟 - final static ThreadPoolContext cachedThreadPoolContext = CachedThreadPoolContext.create("cached-test", 1, 2, 60*5); - - // 最小线程1,最大线程2,使用LinkedBlockingQueue,线程缓存5分钟 - final static ThreadPoolContext cachedThreadPoolContext2 = CachedThreadPoolContext.create("cached-test2", 1, 2, 60*5,1); - - //线程数1,blockqueue大小为2 - final static ThreadPoolContext fixedThreadPoolContext = FixedThreadPoolContext.create("fix-test",1,2); - - final static Executor cachedExecutor = cachedThreadPool.getExecutor(cachedThreadPoolContext); - - final static Executor fixedExecutor = fixedThreadPool.getExecutor(fixedThreadPoolContext); - - final static Executor cachedExecutor2 = cachedThreadPool.getExecutor(cachedThreadPoolContext2); - - Worker worker1 = new Worker(); - Worker worker2 = new Worker(); - Worker worker3 = new Worker(); - Worker worker4 = new Worker(); - Worker worker5 = new Worker(); - - Monitor monitor = new Monitor(fixedExecutor,cachedExecutor); - - - @Before - public void init(){ - monitor.start(); - } - - /** - * 容量为2,因此最多可以接收三个任务。第四个任务开始抛异常 - */ - @Test - public void testFixed(){ - fixedExecutor.execute(worker1); - fixedExecutor.execute(worker2); - fixedExecutor.execute(worker3); - fixedExecutor.execute(worker4); - fixedExecutor.execute(worker5); - } - - /** - * 最小线程1,最大线程2,没有缓冲队列。所以,第三个任务提交的时候,就已经抛错了。 - */ - @Test - public void testCached(){ - cachedExecutor.execute(worker1); - cachedExecutor.execute(worker2); - cachedExecutor.execute(worker3); - cachedExecutor.execute(worker4); - cachedExecutor.execute(worker5); - } - - /** - * 最小线程1,最大线程2,缓冲队列长度为1。所以,第四个任务提交的时候,就已经抛错了。 - */ - @Test - public void testCached2(){ - cachedExecutor2.execute(worker1); - cachedExecutor2.execute(worker2); - cachedExecutor2.execute(worker3); - cachedExecutor2.execute(worker4); - cachedExecutor2.execute(worker5); - } - -} - - -class Worker implements Runnable{ - - private static final Logger log = LoggerFactory.getLogger(Worker.class); - - @Override - public void run() { - log.warn("start run Worker:"+Thread.currentThread().getName()); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - log.warn("end run Worker:"+Thread.currentThread().getName()); - } -} - -class Monitor extends Thread{ - - private static final Logger log = LoggerFactory.getLogger(Monitor.class); - - private ThreadPoolExecutor fixedExecutor; - private ThreadPoolExecutor cachedExecutor; - - public Monitor(Executor fixedExecutor,Executor cachedExecutor) { - this.fixedExecutor = (ThreadPoolExecutor)fixedExecutor; - this.cachedExecutor = (ThreadPoolExecutor)cachedExecutor; - } - - @Override - public void run() { - - while(true){ - StringBuilder sb = new StringBuilder(); - sb.append("[fixedExecutor]"+"coreThreadNums:" + fixedExecutor.getCorePoolSize() + " maxThreadNums:" + fixedExecutor.getMaximumPoolSize() + " activityThreadNums:" - + fixedExecutor.getActiveCount()); - log.error(sb.toString()); - - StringBuilder sb2 = new StringBuilder(); - sb2.append("[cachedExecutor]"+"coreThreadNums:" + cachedExecutor.getCorePoolSize() + " maxThreadNums:" + cachedExecutor.getMaximumPoolSize() + " activityThreadNums:" - + cachedExecutor.getActiveCount()); - log.error(sb2.toString()); - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - - } - - } - -} diff --git a/mpush-tools/src/test/resources/logback.xml b/mpush-tools/src/test/resources/logback.xml index 3e167126..c1d001d8 100644 --- a/mpush-tools/src/test/resources/logback.xml +++ b/mpush-tools/src/test/resources/logback.xml @@ -1,30 +1,30 @@ - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN + + System.out + UTF-8 + + INFO - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + - - - - + + System.err + UTF-8 + + WARN + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + + diff --git a/mpush-zk/.gitignore b/mpush-zk/.gitignore deleted file mode 100644 index b83d2226..00000000 --- a/mpush-zk/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index d727e523..81b578f6 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -1,12 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; -import com.mpush.log.Logs; -import com.mpush.tools.ConsoleLog; -import com.mpush.tools.Constants; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; -import com.mpush.tools.exception.ZKException; -import com.mpush.zk.listener.ZKDataChangeListener; +import com.mpush.api.Constants; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.log.Logs; +import com.mpush.zk.listener.ZKNodeCacheWatcher; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; @@ -16,7 +33,6 @@ import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.CloseableUtils; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; @@ -26,7 +42,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; -public class ZKClient { +public class ZKClient extends BaseService { public static final ZKClient I = I(); private ZKConfig zkConfig; private CuratorFramework client; @@ -38,25 +54,40 @@ private synchronized static ZKClient I() { } private ZKClient() { - try { - init(); - } catch (Exception e) { - throw new ZKException("init zk error, config=" + zkConfig, e); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + client.start(); + Logs.Console.error("init zk client waiting for connected..."); + if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { + throw new ZKException("init zk error, config=" + zkConfig); } + initLocalCache(zkConfig.getLocalCachePath()); + addConnectionStateListener(); + listener.onSuccess(zkConfig.getHosts()); + Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); + Logs.Console.error("init zk client success..."); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (cache != null) cache.close(); + TimeUnit.MILLISECONDS.sleep(600); + client.close(); } /** * 初始化 */ - private void init() throws Exception { - zkConfig = ZKConfig.build(ConfigCenter.I.zkIp()) - .setDigest(ConfigCenter.I.zkDigest()) - .setNamespace(ConfigCenter.I.zkNamespace()); - ConsoleLog.i("init zk client, config=" + zkConfig); + @Override + public void init() { + if (zkConfig != null) return; + zkConfig = ZKConfig.build(); Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) - .retryPolicy(new ExponentialBackoffRetry(zkConfig.getMinTime(), zkConfig.getMaxRetry(), zkConfig.getMaxTime())) + .retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepMs())) .namespace(zkConfig.getNamespace()); if (zkConfig.getConnectionTimeout() > 0) { @@ -82,29 +113,16 @@ public List getAclForPath(final String path) { }); } client = builder.build(); - client.start(); - ConsoleLog.i("init zk client waiting for connected..."); - if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { - throw new ZKException("init zk error, config=" + zkConfig); - } - initLocalCache(zkConfig.getLocalCachePath()); - registerConnectionLostListener(); - Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); - - ConsoleLog.i("init zk client success..."); + Logs.Console.error("init zk client, config=" + zkConfig); } // 注册连接状态监听器 - private void registerConnectionLostListener() { + private void addConnectionStateListener() { client.getConnectionStateListenable().addListener(new ConnectionStateListener() { //TODO need close jvm? @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - if (ConnectionState.LOST == newState) { - Logs.ZK.info("{} lost connection", MPushUtil.getInetAddress()); - } else if (ConnectionState.RECONNECTED == newState) { - Logs.ZK.info("{} reconnected", MPushUtil.getInetAddress()); - } + Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); } }); } @@ -115,25 +133,6 @@ private void initLocalCache(String cachePath) throws Exception { cache.start(); } - private void waitClose() { - try { - Thread.sleep(600); - } catch (final InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - - /** - * 关闭 - */ - public void close() { - if (null != cache) { - cache.close(); - } - waitClose(); - CloseableUtils.closeQuietly(client); - } - /** * 获取数据,先从本地获取,本地找不到,从远程获取 * @@ -293,12 +292,12 @@ public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); } catch (final Exception ex) { - Logs.ZK.error("remove:{}", key, ex); + Logs.ZK.error("removeAndClose:{}", key, ex); throw new ZKException(ex); } } - public void registerListener(ZKDataChangeListener listener) { + public void registerListener(ZKNodeCacheWatcher listener) { cache.getListenable().addListener(listener); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java index efa19e4d..8843d450 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -1,6 +1,25 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.config.CC.mp.zk; public class ZKConfig { public static final int ZK_MAX_RETRY = 3; @@ -16,11 +35,11 @@ public class ZKConfig { private String namespace; - private int maxRetry = ZK_MAX_RETRY; + private int maxRetries = ZK_MAX_RETRY; - private int minTime = ZK_MIN_TIME; + private int baseSleepTimeMs = ZK_MIN_TIME; - private int maxTime = ZK_MAX_TIME; + private int maxSleepMs = ZK_MAX_TIME; private int sessionTimeout = ZK_SESSION_TIMEOUT; @@ -32,8 +51,17 @@ public ZKConfig(String hosts) { this.hosts = hosts; } - public static ZKConfig build(String hosts) { - return new ZKConfig(hosts); + public static ZKConfig build() { + return new ZKConfig(zk.server_address) + .setConnectionTimeout(zk.connectionTimeoutMs) + .setDigest(zk.digest) + .setLocalCachePath(zk.local_cache_path) + .setMaxRetries(zk.retry.maxRetries) + .setMaxSleepMs(zk.retry.maxSleepMs) + .setBaseSleepTimeMs(zk.retry.baseSleepTimeMs) + .setNamespace(zk.namespace) + .setSessionTimeout(zk.sessionTimeoutMs) + ; } public String getHosts() { @@ -54,30 +82,30 @@ public ZKConfig setNamespace(String namespace) { return this; } - public int getMaxRetry() { - return maxRetry; + public int getMaxRetries() { + return maxRetries; } - public ZKConfig setMaxRetry(int maxRetry) { - this.maxRetry = maxRetry; + public ZKConfig setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; return this; } - public int getMinTime() { - return minTime; + public int getBaseSleepTimeMs() { + return baseSleepTimeMs; } - public ZKConfig setMinTime(int minTime) { - this.minTime = minTime; + public ZKConfig setBaseSleepTimeMs(int baseSleepTimeMs) { + this.baseSleepTimeMs = baseSleepTimeMs; return this; } - public int getMaxTime() { - return maxTime; + public int getMaxSleepMs() { + return maxSleepMs; } - public ZKConfig setMaxTime(int maxTime) { - this.maxTime = maxTime; + public ZKConfig setMaxSleepMs(int maxSleepMs) { + this.maxSleepMs = maxSleepMs; return this; } @@ -123,9 +151,9 @@ public String toString() { "hosts='" + hosts + '\'' + ", digest='" + digest + '\'' + ", namespace='" + namespace + '\'' + - ", maxRetry=" + maxRetry + - ", minTime=" + minTime + - ", maxTime=" + maxTime + + ", maxRetries=" + maxRetries + + ", baseSleepTimeMs=" + baseSleepTimeMs + + ", maxSleepMs=" + maxSleepMs + ", sessionTimeout=" + sessionTimeout + ", connectionTimeout=" + connectionTimeout + ", localCachePath='" + localCachePath + '\'' + diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKException.java b/mpush-zk/src/main/java/com/mpush/zk/ZKException.java new file mode 100644 index 00000000..92f3bbff --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKException.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public class ZKException extends RuntimeException { + + public ZKException() { + } + + public ZKException(String message) { + super(message); + } + + public ZKException(String message, Throwable cause) { + super(message, cause); + } + + public ZKException(Throwable cause) { + super(cause); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java b/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java deleted file mode 100644 index 1040b0cb..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKNodeManager.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mpush.zk; - -import com.mpush.tools.spi.SPI; - -import java.util.Collection; - -@SPI("connectionServerManage") -public interface ZKNodeManager { - - void addOrUpdate(String fullPath, T application); - - void remove(String fullPath); - - Collection getList(); - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index c13f1a08..ed1c7aa0 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -1,3 +1,22 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + package com.mpush.zk; @@ -5,29 +24,28 @@ public enum ZKPath { REDIS_SERVER("/redis", "machine", "redis注册的地方"), - CONNECTION_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), - PUSH_SERVER("/ps/hosts", "machine", "push server服务器应用注册的路径"), + CONNECT_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"); - ZKPath(String path, String name, String desc) { - this.path = path; + ZKPath(String root, String name, String desc) { + this.root = root; this.name = name; } - private final String path; + private final String root; private final String name; - public String getPath() { - return path; + public String getRootPath() { + return root; } - public String getWatchPath() { - return path + ZKPaths.PATH_SEPARATOR + name; + public String getNodePath() { + return root + ZKPaths.PATH_SEPARATOR + name; } //根据从zk中获取的app的值,拼装全路径 - public String getFullPath(String nodeName) { - return path + ZKPaths.PATH_SEPARATOR + nodeName; + public String getFullPath(String tail) { + return root + ZKPaths.PATH_SEPARATOR + tail; } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java new file mode 100644 index 00000000..c4a3023c --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.mpush.zk.node.ZKNode; + +import java.util.Collection; +import java.util.List; + +public interface ZKNodeCache { + + void addAll(List list); + + void put(String fullPath, T node); + + T remove(String fullPath); + + Collection values(); + + void clear(); + +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java new file mode 100644 index 00000000..338704ab --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java @@ -0,0 +1,62 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.mpush.zk.node.ZKRedisNode; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ZKRedisNodeCache implements ZKNodeCache { + + private List nodes = Collections.emptyList(); + + @Override + public void addAll(List list) { + nodes = list; + } + + @Override + public void put(String fullPath, ZKRedisNode node) { + throw new UnsupportedOperationException("can not put one redis node, name=" + fullPath); + } + + @Override + public ZKRedisNode remove(String fullPath) { + nodes = Collections.emptyList(); + return null; + } + + @Override + public Collection values() { + return nodes; + } + + @Override + public void clear() { + nodes = Collections.emptyList(); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java new file mode 100644 index 00000000..b41a271f --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java @@ -0,0 +1,81 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.google.common.collect.Maps; +import com.mpush.zk.node.ZKServerNode; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class ZKServerNodeCache implements ZKNodeCache { + + private final Logger logger = LoggerFactory.getLogger(ZKServerNodeCache.class); + + protected final Map cache = Maps.newConcurrentMap(); + + @Override + public void addAll(List list) { + + } + + @Override + public void put(String fullPath, ZKServerNode node) { + if (StringUtils.isNotBlank(fullPath) && node != null) { + cache.put(fullPath, node); + } else { + logger.error("fullPath is null or application is null"); + } + printList(); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = cache.remove(fullPath); + printList(); + return node; + } + + @Override + public Collection values() { + return Collections.unmodifiableCollection(cache.values()); + } + + @Override + public void clear() { + cache.clear(); + } + + private void printList() { + for (ZKServerNode app : cache.values()) { + logger.warn(app.toString()); + } + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java deleted file mode 100644 index 2945a837..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDataChangeListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.mpush.zk.listener; - -import com.mpush.log.Logs; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; - -public abstract class ZKDataChangeListener implements TreeCacheListener { - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - String path = null == event.getData() ? "" : event.getData().getPath(); - if (path.isEmpty()) { - return; - } - Logs.ZK.info("DataChangeListener:{},{},namespace:{}", path, listenerPath(), client.getNamespace()); - if (path.startsWith(listenerPath())) { - dataChanged(client, event, path); - } - } - - public abstract void initData(); - - public abstract void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception; - - public abstract String listenerPath(); -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java new file mode 100644 index 00000000..4cd661cd --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -0,0 +1,76 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; + +public abstract class ZKNodeCacheWatcher implements TreeCacheListener { + + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + ChildData data = event.getData(); + if (data == null) return; + String path = data.getPath(); + if (Strings.isNullOrEmpty(path)) return; + if (path.startsWith(watchPath())) { + switch (event.getType()) { + case NODE_ADDED: + onNodeAdded(path, data.getData()); + break; + case NODE_REMOVED: + onNodeRemoved(path, data.getData()); + break; + case NODE_UPDATED: + onNodeUpdated(path, data.getData()); + break; + } + } + Logs.ZK.info("ZK node data change={}, name={}, listener={}, ns={}", event.getType(), path, watchPath(), client.getNamespace()); + } + + public final void beginWatch() { + beforeWatch(); + ZKClient.I.registerListener(this); + } + + public abstract String watchPath(); + + protected void beforeWatch() { + + } + + protected void onNodeAdded(String path, byte[] data) { + + } + + protected void onNodeRemoved(String path, byte[] data) { + + } + + protected void onNodeUpdated(String path, byte[] data) { + + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java deleted file mode 100644 index d7266ad4..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeListener.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.mpush.zk.listener; - -import com.google.common.base.Strings; -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKPath; -import com.mpush.tools.Jsons; -import com.mpush.tools.redis.RedisGroup; -import com.mpush.tools.redis.RedisRegister; -import com.mpush.tools.spi.ServiceContainer; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collections; -import java.util.List; - -/** - * redis 监控 - */ -public class ZKRedisNodeListener extends ZKDataChangeListener { - private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeListener.class); - - private final RedisRegister redisRegister = ServiceContainer.load(RedisRegister.class); - - // 获取redis列表 - private void _initData() { - logger.warn("start init redis data"); - List group = getRedisGroup(ZKPath.REDIS_SERVER.getPath()); - redisRegister.init(group); - logger.warn("end init redis data"); - } - - private void dataRemove(ChildData data) { - _initData(); - } - - private void dataAddOrUpdate(ChildData data) { - _initData(); - } - - @SuppressWarnings("unchecked") - private List getRedisGroup(String fullPath) { - String rawGroup = ZKClient.I.get(fullPath); - if (Strings.isNullOrEmpty(rawGroup)) - return Collections.EMPTY_LIST; - List group = Jsons.fromJsonToList(rawGroup, RedisGroup[].class); - if (group == null) - return Collections.EMPTY_LIST; - return group; - } - - @Override - public void initData() { - _initData(); - } - - @Override - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - logger.warn("RedisPathListener other path:" + data + "," + event.getType().name() + "," + data); - } - - } - - @Override - public String listenerPath() { - return ZKPath.REDIS_SERVER.getPath(); - } -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java new file mode 100644 index 00000000..ef805ff0 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java @@ -0,0 +1,79 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.Jsons; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKRedisNodeCache; +import com.mpush.zk.node.ZKRedisNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * redis 监控 + */ +public class ZKRedisNodeWatcher extends ZKNodeCacheWatcher { + + private final Logger logger = LoggerFactory.getLogger(ZKRedisNodeWatcher.class); + + private final ZKRedisNodeCache cache = new ZKRedisNodeCache(); + + private void refresh() { + String rawGroup = ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); + logger.warn("refresh zk redis node cache, data=" + rawGroup); + if (Strings.isNullOrEmpty(rawGroup)) return; + ZKRedisNode[] group = Jsons.fromJson(rawGroup, ZKRedisNode[].class); + if (group == null) return; + cache.addAll(Arrays.asList(group)); + } + + @Override + protected void beforeWatch() { + refresh(); + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + refresh(); + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + refresh(); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + refresh(); + } + + @Override + public String watchPath() { + return ZKPath.REDIS_SERVER.getRootPath(); + } + + public ZKRedisNodeCache getCache() { + return cache; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java deleted file mode 100644 index 8bc0a15b..00000000 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeListener.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.mpush.zk.listener; - -import com.mpush.zk.ZKClient; -import com.mpush.zk.ZKNodeManager; -import com.mpush.zk.ZKServerNode; -import com.mpush.tools.Jsons; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public abstract class ZKServerNodeListener> extends ZKDataChangeListener { - - private final Logger log = LoggerFactory.getLogger(ZKServerNodeListener.class); - - public void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) throws Exception { - String data = ""; - if (event.getData() != null) { - data = ToStringBuilder.reflectionToString(event.getData(), ToStringStyle.MULTI_LINE_STYLE); - } - if (Type.NODE_ADDED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else if (Type.NODE_REMOVED == event.getType()) { - dataRemove(event.getData()); - } else if (Type.NODE_UPDATED == event.getType()) { - dataAddOrUpdate(event.getData()); - } else { - log.warn(this.getClass().getSimpleName() + "other path:" + path + "," + event.getType().name() + "," + data); - } - } - - public void initData() { - log.warn(" start init " + this.getClass().getSimpleName() + " server data"); - initData0(); - log.warn(" end init " + this.getClass().getSimpleName() + " server data"); - } - - public abstract String getRegisterPath(); - - public abstract String getFullPath(String raw); - - public abstract T getManager(); - - private void initData0() { - // 获取机器列表 - List rawData = ZKClient.I.getChildrenKeys(getRegisterPath()); - for (String raw : rawData) { - String fullPath = getFullPath(raw); - ZKServerNode app = getServerNode(fullPath); - getManager().addOrUpdate(fullPath, app); - } - } - - private void dataRemove(ChildData data) { - String path = data.getPath(); - getManager().remove(path); - } - - private void dataAddOrUpdate(ChildData data) { - String path = data.getPath(); - byte[] rawData = data.getData(); - ZKServerNode serverApp = Jsons.fromJson(rawData, ZKServerNode.class); - getManager().addOrUpdate(path, serverApp); - } - - private ZKServerNode getServerNode(String fullPath) { - String rawApp = ZKClient.I.get(fullPath); - ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); - return app; - } - - -} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java new file mode 100644 index 00000000..a88b6c14 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java @@ -0,0 +1,101 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.zk.node.ZKServerNode; + +import java.util.List; + +public final class ZKServerNodeWatcher extends ZKNodeCacheWatcher { + private final ZKPath path; + private final ZKServerNodeCache cache; + + public static ZKServerNodeWatcher buildConnect() { + return new ZKServerNodeWatcher(ZKPath.CONNECT_SERVER); + } + + public static ZKServerNodeWatcher buildGateway() { + return new ZKServerNodeWatcher(ZKPath.GATEWAY_SERVER); + } + + public static ZKServerNodeWatcher build(ZKPath path, ZKServerNodeCache cache) { + return new ZKServerNodeWatcher(path, cache); + } + + public ZKServerNodeWatcher(ZKPath path) { + this.path = path; + this.cache = new ZKServerNodeCache(); + } + + public ZKServerNodeWatcher(ZKPath path, ZKServerNodeCache cache) { + this.path = path; + this.cache = cache; + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + ZKServerNode serverApp = Jsons.fromJson(data, ZKServerNode.class); + cache.put(path, serverApp); + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + cache.remove(path); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + ZKServerNode serverApp = Jsons.fromJson(data, ZKServerNode.class); + cache.put(path, serverApp); + } + + @Override + public String watchPath() { + return path.getNodePath(); + } + + @Override + protected void beforeWatch() { + Logs.Console.error("start init zk server data"); + List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); + for (String raw : rawData) { + String fullPath = path.getFullPath(raw); + ZKServerNode app = getServerNode(fullPath); + cache.put(fullPath, app); + } + Logs.Console.error("end init zk server data"); + } + + private ZKServerNode getServerNode(String fullPath) { + String rawApp = ZKClient.I.get(fullPath); + ZKServerNode app = Jsons.fromJson(rawApp, ZKServerNode.class); + return app; + } + + public ZKServerNodeCache getCache() { + return cache; + } + +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java new file mode 100644 index 00000000..7a279ead --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public interface ZKNode { + String encode(); +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java new file mode 100644 index 00000000..0d30efec --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; + +import com.mpush.tools.Jsons; +import com.mpush.tools.config.data.RedisGroup; + +/** + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ +public class ZKRedisNode extends RedisGroup implements ZKNode { + private transient String zkPath; + + public String getZkPath() { + return zkPath; + } + + public void setZkPath(String zkPath) { + this.zkPath = zkPath; + } + + @Override + public String encode() { + return Jsons.toJson(this); + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java similarity index 57% rename from mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java rename to mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index b56dba59..73d24e84 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -1,13 +1,35 @@ -package com.mpush.zk; +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; -import com.mpush.tools.MPushUtil; -import com.mpush.tools.config.ConfigCenter; +import com.mpush.tools.Jsons; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; +import com.mpush.tools.config.ConfigManager; +import com.mpush.zk.ZKPath; /** * 系统配置 */ -public class ZKServerNode { +public class ZKServerNode implements ZKNode { private String ip; @@ -28,17 +50,17 @@ public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { } public static ZKServerNode csNode() { - return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.I.connectionServerPort(), - MPushUtil.getExtranetAddress(), - ZKPath.CONNECTION_SERVER.getWatchPath()); + return new ZKServerNode(Utils.getLocalIp(), + CC.mp.net.connect_server_port, + ConfigManager.I.getPublicIp(), + ZKPath.CONNECT_SERVER.getNodePath()); } public static ZKServerNode gsNode() { - return new ZKServerNode(MPushUtil.getLocalIp(), - ConfigCenter.I.gatewayServerPort(), + return new ZKServerNode(Utils.getLocalIp(), + CC.mp.net.gateway_server_port, null, - ZKPath.GATEWAY_SERVER.getWatchPath()); + ZKPath.GATEWAY_SERVER.getNodePath()); } public String getIp() { @@ -99,10 +121,15 @@ public int hashCode() { @Override public String toString() { return "ZKServerNode{" + - "ip='" + ip + '\'' + + "host='" + ip + '\'' + ", port=" + port + ", extranetIp='" + extranetIp + '\'' + ", zkPath='" + zkPath + '\'' + '}'; } + + @Override + public String encode() { + return Jsons.toJson(this); + } } diff --git a/pom.xml b/pom.xml index da4d2d61..5c4d8401 100644 --- a/pom.xml +++ b/pom.xml @@ -40,28 +40,28 @@ mpush-client mpush-test mpush-monitor - mpush-log mpush-zk + mpush-cache UTF-8 UTF-8 UTF-8 - 1.7 + 1.8 com.mpush - 0.0.1 - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} - ${mpush-version} + 0.0.1 + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} + ${mpush.version} 5.0.0.Alpha2 linux-x86_64 @@ -93,6 +93,11 @@ netty-codec-http ${netty.version} + + io.netty + netty-handler + 5.0.0.Alpha2 + @@ -127,60 +132,60 @@ + + ${mpush.groupId} + mpush-test + ${mpush.version} + ${mpush.groupId} mpush-api - ${mpush-api-version} + ${mpush.version} ${mpush.groupId} mpush-tools - ${mpush-tools-version} + ${mpush.version} ${mpush.groupId} mpush-common - ${mpush-common-version} + ${mpush.version} ${mpush.groupId} mpush-netty - ${mpush-netty-version} + ${mpush.version} ${mpush.groupId} mpush-core - ${mpush-core-version} + ${mpush.version} ${mpush.groupId} mpush-client - ${mpush-client-version} + ${mpush.version} ${mpush.groupId} mpush-monitor - ${mpush-monitor-version} - - - ${mpush.groupId} - mpush-log - ${mpush-log-version} + ${mpush.version} ${mpush.groupId} - mpush-test - ${mpush-test-version} + mpush-boot + ${mpush.version} ${mpush.groupId} - mpush-boot - ${mpush-boot-version} + mpush-zk + ${mpush.version} ${mpush.groupId} - mpush-zk - ${mpush-zk-version} + mpush-cache + ${mpush.version} @@ -204,18 +209,14 @@ commons-logging commons-logging 1.1.3 + provided log4j log4j 1.2.17 + provided - - org.logback-extensions - logback-ext-spring - 0.1.2 - - @@ -234,31 +235,16 @@ gson 2.5 - org.aeonbits.owner owner 1.0.9 - - - org.codehaus.janino - janino - 2.7.8 - - - args4j - args4j - 2.33 - - - - commons-net - commons-net - 3.4 + com.typesafe + config + 1.3.0 - @@ -303,12 +289,6 @@ - - daily - - daily - - dev @@ -319,15 +299,9 @@ - pre - - pre - - - - online + pub - online + pub From 228b4f85b66c09af190824f89ac9c162fb34ce61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 20 Aug 2016 13:11:31 +0800 Subject: [PATCH 564/890] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=AB=AF=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/client/push/PushClient.java | 3 +++ .../com/mpush/core/handler/AdminHandler.java | 18 +++++++++++++----- .../mpush/test/push/PushClientTestMain.java | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index e053fbd0..9b69df55 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -66,6 +66,9 @@ public FutureTask send(byte[] content, String userId, Callback callback return PushRequest .build(this) .setCallback(callback) + .setUserId(userId) + .setContent(content) + .setTimeout(DEFAULT_TIMEOUT) .offline(); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 039e4d4f..bdc19126 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -38,7 +38,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.PrintWriter; import java.io.Serializable; +import java.io.StringWriter; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -73,11 +75,17 @@ protected void messageReceived(ChannelHandlerContext ctx, String request) throws args = Arrays.copyOfRange(cmd_args, 1, cmd_args.length); } } - - Object result = args != null ? command.handler(ctx, args) : command.handler(ctx, arg); - ChannelFuture future = ctx.writeAndFlush(result + EOL + EOL); - if (command == Command.quit) { - future.addListener(ChannelFutureListener.CLOSE); + try { + Object result = args != null ? command.handler(ctx, args) : command.handler(ctx, arg); + ChannelFuture future = ctx.writeAndFlush(result + EOL + EOL); + if (command == Command.quit) { + future.addListener(ChannelFutureListener.CLOSE); + } + } catch (Throwable throwable) { + ctx.writeAndFlush(throwable.getLocalizedMessage() + EOL + EOL); + StringWriter writer = new StringWriter(1024); + throwable.printStackTrace(new PrintWriter(writer)); + ctx.writeAndFlush(writer.toString()); } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 29b4b7bd..fbbe8199 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -43,7 +43,7 @@ public static void main(String[] args) throws Exception { PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); content.setMsgId("msgId_" + (i % 2)); Thread.sleep(1000); - sender.send(Jsons.toJson(content), Arrays.asList("user-0"), new PushSender.Callback() { + sender.send(Jsons.toJson(content), Arrays.asList("user-0","doctor43test"), new PushSender.Callback() { @Override public void onSuccess(String userId, ClientLocation location) { System.err.println("push onSuccess userId=" + userId); From 5e005c598095594381678185f7aea4dae13751e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 00:10:55 +0800 Subject: [PATCH 565/890] =?UTF-8?q?=E7=B1=BB=E5=BA=93=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-tools/pom.xml | 4 -- pom.xml | 94 ++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 53 deletions(-) diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 31ae2c40..df0ec478 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -45,10 +45,6 @@ org.apache.commons commons-lang3 - - org.aeonbits.owner - owner - com.typesafe config diff --git a/pom.xml b/pom.xml index 5c4d8401..8d4074e8 100644 --- a/pom.xml +++ b/pom.xml @@ -96,39 +96,8 @@ io.netty netty-handler - 5.0.0.Alpha2 - - - - - - com.google.guava - guava - 19.0 - - - - junit - junit - 4.10 - - - org.apache.curator - curator-recipes - 2.9.1 - - - netty - io.netty - - - - - org.apache.curator - curator-x-discovery - 2.9.1 + ${netty.version} - @@ -193,17 +162,17 @@ org.slf4j slf4j-api - 1.7.5 + 1.7.21 org.slf4j jcl-over-slf4j - 1.7.5 + 1.7.21 ch.qos.logback logback-classic - 1.0.13 + 1.1.7 commons-logging @@ -217,34 +186,61 @@ 1.2.17 provided - - - - - redis.clients - jedis - 2.8.0 - + + + org.apache.commons commons-lang3 3.4 - com.google.code.gson - gson - 2.5 + com.google.guava + guava + 19.0 + - org.aeonbits.owner - owner - 1.0.9 + org.apache.curator + curator-recipes + 2.9.1 + + + netty + io.netty + + + + org.apache.curator + curator-x-discovery + 2.9.1 + + + + redis.clients + jedis + 2.9.0 + + + + com.google.code.gson + gson + 2.7 + + com.typesafe config 1.3.0 + + + junit + junit + 4.10 + test + From 7ee055ab640a46502e3fed1b548d633130bac1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 00:10:55 +0800 Subject: [PATCH 566/890] =?UTF-8?q?=E7=B1=BB=E5=BA=93=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-tools/pom.xml | 4 -- pom.xml | 94 ++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 53 deletions(-) diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 31ae2c40..df0ec478 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -45,10 +45,6 @@ org.apache.commons commons-lang3 - - org.aeonbits.owner - owner - com.typesafe config diff --git a/pom.xml b/pom.xml index 5c4d8401..8d4074e8 100644 --- a/pom.xml +++ b/pom.xml @@ -96,39 +96,8 @@ io.netty netty-handler - 5.0.0.Alpha2 - - - - - - com.google.guava - guava - 19.0 - - - - junit - junit - 4.10 - - - org.apache.curator - curator-recipes - 2.9.1 - - - netty - io.netty - - - - - org.apache.curator - curator-x-discovery - 2.9.1 + ${netty.version} - @@ -193,17 +162,17 @@ org.slf4j slf4j-api - 1.7.5 + 1.7.21 org.slf4j jcl-over-slf4j - 1.7.5 + 1.7.21 ch.qos.logback logback-classic - 1.0.13 + 1.1.7 commons-logging @@ -217,34 +186,61 @@ 1.2.17 provided - - - - - redis.clients - jedis - 2.8.0 - + + + org.apache.commons commons-lang3 3.4 - com.google.code.gson - gson - 2.5 + com.google.guava + guava + 19.0 + - org.aeonbits.owner - owner - 1.0.9 + org.apache.curator + curator-recipes + 2.9.1 + + + netty + io.netty + + + + org.apache.curator + curator-x-discovery + 2.9.1 + + + + redis.clients + jedis + 2.9.0 + + + + com.google.code.gson + gson + 2.7 + + com.typesafe config 1.3.0 + + + junit + junit + 4.10 + test + From 4a0f20dfc15ebc38706eaab3714e34607bdc1814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 00:11:53 +0800 Subject: [PATCH 567/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=AD=E5=BF=83=20?= =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/conf-dev.properties | 3 +- conf/conf-pub.properties | 3 +- conf/reference.conf | 73 +++++++++++-------- mpush-boot/src/main/resources/mpush.conf | 14 ++++ .../src/main/resources/application.conf | 16 ++++ .../src/test/resources/application.conf | 15 +++- .../main/java/com/mpush/tools/config/CC.java | 6 +- 7 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 mpush-client/src/main/resources/application.conf diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties index 4a36a6eb..5197fe31 100644 --- a/conf/conf-dev.properties +++ b/conf/conf-dev.properties @@ -1 +1,2 @@ -log.level=debug \ No newline at end of file +log.level=debug +min.hb=10s \ No newline at end of file diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties index a5051a64..6b55c1f1 100644 --- a/conf/conf-pub.properties +++ b/conf/conf-pub.properties @@ -1 +1,2 @@ -log.level=warn \ No newline at end of file +log.level=warn +min.hb=3m \ No newline at end of file diff --git a/conf/reference.conf b/conf/reference.conf index 263e7849..86e7117b 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -11,34 +11,39 @@ ############################################################################################################## mp { + #日志配置 log.level=warn log.dir=${user.dir}/../logs + #核心配置 core { - max-packet-size=10k//系统允许传输的最大包的大小 - compress-threshold=10k//数据包启用压缩的临界值,超过该值后对数据进行压缩 - min-heartbeat=10s - max-heartbeat=3m - max-hb-timeout-times=2//允许的心跳连续超时的最大次数 - session-expired-time=1d//用于快速重连的session 过期时间默认1天 - epoll-provider=netty//nio:jdk 自带,netty:有netty实现 + max-packet-size=10k //系统允许传输的最大包的大小 + compress-threshold=10k //数据包启用压缩的临界值,超过该值后对数据进行压缩 + min-heartbeat=3m //最小心跳间隔 + max-heartbeat=3m //最大心跳间隔 + max-hb-timeout-times=2 //允许的心跳连续超时的最大次数 + session-expired-time=1d //用于快速重连的session 过期时间默认1天 + epoll-provider=netty //nio:jdk自带,netty:由netty实现 } + #安全配置 security { + #rsa 私钥, 公钥 key长度为1024;生成方式可以使用open-ssh或者使用工具类com.mpush.tools.crypto.RSAUtils#main private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" - aes-key-length=16 - ras-key-length=1024 + aes-key-length=16 //AES key 长度 + ras-key-length=1024 //RSA key 长度 } + #网络配置 net { - connect-server-port=3000 - gateway-server-port=3001 - admin-server-port=3002 - public-host-mapping {//本机局域网IP和公网IP的映射关系 - "10.1.0.32":"111.1.57.148" + connect-server-port=3000 //长链接服务对外端口, 公网端口 + gateway-server-port=3001 //网关服务端口, 内部端口 + admin-server-port=3002 //控制台服务端口, 内部端口 + public-host-mapping { //本机局域网IP和公网IP的映射关系 + "127.0.0.1":"111.1.32.137" } - traffic-shaping { + traffic-shaping { //流量整形配置 gateway-client { enabled:true check-interval:100ms @@ -68,6 +73,7 @@ mp { } } + #Zookeeper配置 zk { server-address="127.0.0.1:2181" namespace=mpush @@ -85,14 +91,15 @@ mp { sessionTimeoutMs=5s } + #Redis集群配置 redis { write-to-zk=true #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 cluster-group:[ [ { - host:"111.1.57.148" - port:6379 + host:"127.0.0.1" + port:2181 password:ShineMoIpo } ] @@ -119,25 +126,27 @@ mp { } } + #HTTP代理配置 http { - proxy-enabled=false - max-conn-per-host=5 - default-read-timeout=10s - max-content-length=5m - dns-mapping { - "mpush.com":["127.0.0.1:8080","127.0.0.1:8081"] + proxy-enabled=false //启用Http代理 + max-conn-per-host=5 //每个域名的最大链接数, 建议web服务nginx超时时间设长一点, 以便保持长链接 + default-read-timeout=10s //请求超时时间 + max-content-length=5m //response body 最大大小 + dns-mapping { //域名映射外网地址转内部IP + "mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] } } + #线程池配置 thread { pool { - boss { + boss { //netty boss min:4 max:16 queue-size:1000 } - work { + work { //netty boss min:8 max:32 queue-size:1000 @@ -155,19 +164,19 @@ mp { queue-size:1000 } - biz { + biz { //其他业务 min:4 max:64 queue-size:10 } - mq { + mq { //用户上下线消息, 踢人等 min:2 max:4 queue-size:10000 } - push-callback { + push-callback { //消息推送 min:2 max:2 queue-size:0 @@ -175,13 +184,15 @@ mp { } } + #系统监控配置 monitor { dump-dir=/tmp/logs/mpush/ - dump-stack=false - dump-period=1m - print-log=true + dump-stack=false //是否定时dump堆栈 + dump-period=1m //多久监控一次 + print-log=true //是否打印监控日志 } + #SPI扩展配置 spi { thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 6ee565ff..ff238f12 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,4 +1,18 @@ mp.log.level=${log.level} +min-heartbeat=${min.hb} mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" +mp.zk.server-address="127.0.0.1:2181" mp.zk.namespace=mpush +mp.redis={ + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:6379 + password:"your redis password" + } + ] + ] +} \ No newline at end of file diff --git a/mpush-client/src/main/resources/application.conf b/mpush-client/src/main/resources/application.conf new file mode 100644 index 00000000..4e43e48b --- /dev/null +++ b/mpush-client/src/main/resources/application.conf @@ -0,0 +1,16 @@ +mp.log.level=warn +mp.zk.server-address="127.0.0.1:2181" +mp.zk.namespace=mpush +mp.redis={ + write-to-zk=false + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:6379 + password:"your redis password" + } + ] + ] +} \ No newline at end of file diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 4c50b7c1..274eac51 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -1,6 +1,19 @@ mp.log.dir=${user.dir}/mpush-test/target/logs mp.log.level=debug +min-heartbeat=10s mp.zk.namespace=mpush mp.zk.server-address="111.1.57.148:5666" mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} -mp.core.compress-threshold=10k \ No newline at end of file +mp.core.compress-threshold=10k +mp.redis { + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"111.1.57.148" + port:6379 + password:ShineMoIpo + } + ] + ] +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 2f597c11..b431ba27 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -39,6 +39,7 @@ import static java.util.stream.Collectors.toCollection; /** + * mpush 配置中心 * Created by yxx on 2016/5/20. * * @author ohun@live.cn @@ -48,7 +49,7 @@ public interface CC { static Config load() { Config config = ConfigFactory.load(); - String custom_conf = "mp.conf"; + String custom_conf = "mp.conf";//值来自jvm启动参数指定 if (config.hasPath(custom_conf)) { File file = new File(config.getString(custom_conf)); if (file.exists()) { @@ -94,9 +95,8 @@ interface public_ip_mapping { Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); static String getString(String localIp) { - return (String) mappings.getOrDefault(localIp, localIp); + return (String) mappings.get(localIp); } - } interface traffic_shaping { From 256a7408da75ce354ec58b508415f76feab8fa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 00:11:53 +0800 Subject: [PATCH 568/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=AD=E5=BF=83=20?= =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/conf-dev.properties | 3 +- conf/conf-pub.properties | 3 +- conf/reference.conf | 73 +++++++++++-------- mpush-boot/src/main/resources/mpush.conf | 14 ++++ .../src/main/resources/application.conf | 16 ++++ .../src/test/resources/application.conf | 15 +++- .../main/java/com/mpush/tools/config/CC.java | 6 +- 7 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 mpush-client/src/main/resources/application.conf diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties index 4a36a6eb..5197fe31 100644 --- a/conf/conf-dev.properties +++ b/conf/conf-dev.properties @@ -1 +1,2 @@ -log.level=debug \ No newline at end of file +log.level=debug +min.hb=10s \ No newline at end of file diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties index a5051a64..6b55c1f1 100644 --- a/conf/conf-pub.properties +++ b/conf/conf-pub.properties @@ -1 +1,2 @@ -log.level=warn \ No newline at end of file +log.level=warn +min.hb=3m \ No newline at end of file diff --git a/conf/reference.conf b/conf/reference.conf index 263e7849..86e7117b 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -11,34 +11,39 @@ ############################################################################################################## mp { + #日志配置 log.level=warn log.dir=${user.dir}/../logs + #核心配置 core { - max-packet-size=10k//系统允许传输的最大包的大小 - compress-threshold=10k//数据包启用压缩的临界值,超过该值后对数据进行压缩 - min-heartbeat=10s - max-heartbeat=3m - max-hb-timeout-times=2//允许的心跳连续超时的最大次数 - session-expired-time=1d//用于快速重连的session 过期时间默认1天 - epoll-provider=netty//nio:jdk 自带,netty:有netty实现 + max-packet-size=10k //系统允许传输的最大包的大小 + compress-threshold=10k //数据包启用压缩的临界值,超过该值后对数据进行压缩 + min-heartbeat=3m //最小心跳间隔 + max-heartbeat=3m //最大心跳间隔 + max-hb-timeout-times=2 //允许的心跳连续超时的最大次数 + session-expired-time=1d //用于快速重连的session 过期时间默认1天 + epoll-provider=netty //nio:jdk自带,netty:由netty实现 } + #安全配置 security { + #rsa 私钥, 公钥 key长度为1024;生成方式可以使用open-ssh或者使用工具类com.mpush.tools.crypto.RSAUtils#main private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" - aes-key-length=16 - ras-key-length=1024 + aes-key-length=16 //AES key 长度 + ras-key-length=1024 //RSA key 长度 } + #网络配置 net { - connect-server-port=3000 - gateway-server-port=3001 - admin-server-port=3002 - public-host-mapping {//本机局域网IP和公网IP的映射关系 - "10.1.0.32":"111.1.57.148" + connect-server-port=3000 //长链接服务对外端口, 公网端口 + gateway-server-port=3001 //网关服务端口, 内部端口 + admin-server-port=3002 //控制台服务端口, 内部端口 + public-host-mapping { //本机局域网IP和公网IP的映射关系 + "127.0.0.1":"111.1.32.137" } - traffic-shaping { + traffic-shaping { //流量整形配置 gateway-client { enabled:true check-interval:100ms @@ -68,6 +73,7 @@ mp { } } + #Zookeeper配置 zk { server-address="127.0.0.1:2181" namespace=mpush @@ -85,14 +91,15 @@ mp { sessionTimeoutMs=5s } + #Redis集群配置 redis { write-to-zk=true #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 cluster-group:[ [ { - host:"111.1.57.148" - port:6379 + host:"127.0.0.1" + port:2181 password:ShineMoIpo } ] @@ -119,25 +126,27 @@ mp { } } + #HTTP代理配置 http { - proxy-enabled=false - max-conn-per-host=5 - default-read-timeout=10s - max-content-length=5m - dns-mapping { - "mpush.com":["127.0.0.1:8080","127.0.0.1:8081"] + proxy-enabled=false //启用Http代理 + max-conn-per-host=5 //每个域名的最大链接数, 建议web服务nginx超时时间设长一点, 以便保持长链接 + default-read-timeout=10s //请求超时时间 + max-content-length=5m //response body 最大大小 + dns-mapping { //域名映射外网地址转内部IP + "mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] } } + #线程池配置 thread { pool { - boss { + boss { //netty boss min:4 max:16 queue-size:1000 } - work { + work { //netty boss min:8 max:32 queue-size:1000 @@ -155,19 +164,19 @@ mp { queue-size:1000 } - biz { + biz { //其他业务 min:4 max:64 queue-size:10 } - mq { + mq { //用户上下线消息, 踢人等 min:2 max:4 queue-size:10000 } - push-callback { + push-callback { //消息推送 min:2 max:2 queue-size:0 @@ -175,13 +184,15 @@ mp { } } + #系统监控配置 monitor { dump-dir=/tmp/logs/mpush/ - dump-stack=false - dump-period=1m - print-log=true + dump-stack=false //是否定时dump堆栈 + dump-period=1m //多久监控一次 + print-log=true //是否打印监控日志 } + #SPI扩展配置 spi { thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 6ee565ff..ff238f12 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,4 +1,18 @@ mp.log.level=${log.level} +min-heartbeat=${min.hb} mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" +mp.zk.server-address="127.0.0.1:2181" mp.zk.namespace=mpush +mp.redis={ + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:6379 + password:"your redis password" + } + ] + ] +} \ No newline at end of file diff --git a/mpush-client/src/main/resources/application.conf b/mpush-client/src/main/resources/application.conf new file mode 100644 index 00000000..4e43e48b --- /dev/null +++ b/mpush-client/src/main/resources/application.conf @@ -0,0 +1,16 @@ +mp.log.level=warn +mp.zk.server-address="127.0.0.1:2181" +mp.zk.namespace=mpush +mp.redis={ + write-to-zk=false + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:6379 + password:"your redis password" + } + ] + ] +} \ No newline at end of file diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 4c50b7c1..274eac51 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -1,6 +1,19 @@ mp.log.dir=${user.dir}/mpush-test/target/logs mp.log.level=debug +min-heartbeat=10s mp.zk.namespace=mpush mp.zk.server-address="111.1.57.148:5666" mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} -mp.core.compress-threshold=10k \ No newline at end of file +mp.core.compress-threshold=10k +mp.redis { + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"111.1.57.148" + port:6379 + password:ShineMoIpo + } + ] + ] +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 2f597c11..b431ba27 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -39,6 +39,7 @@ import static java.util.stream.Collectors.toCollection; /** + * mpush 配置中心 * Created by yxx on 2016/5/20. * * @author ohun@live.cn @@ -48,7 +49,7 @@ public interface CC { static Config load() { Config config = ConfigFactory.load(); - String custom_conf = "mp.conf"; + String custom_conf = "mp.conf";//值来自jvm启动参数指定 if (config.hasPath(custom_conf)) { File file = new File(config.getString(custom_conf)); if (file.exists()) { @@ -94,9 +95,8 @@ interface public_ip_mapping { Map mappings = net.cfg.getObject("public-host-mapping").unwrapped(); static String getString(String localIp) { - return (String) mappings.getOrDefault(localIp, localIp); + return (String) mappings.get(localIp); } - } interface traffic_shaping { From 76a75a4cc3231c92bdbee41acfd14a3ee0618a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 00:13:09 +0800 Subject: [PATCH 569/890] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=85=AC=E7=BD=91?= =?UTF-8?q?=E5=9C=B0=E5=9D=80bug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/tools/Utils.java | 19 +++++++++++---- .../com/mpush/tools/config/ConfigManager.java | 24 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index 6a041dfd..fe7759d1 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -70,15 +70,17 @@ public static String getLocalIp() { */ public static String getInetAddress() { try { - Profiler.enter("start get inet addresss"); + Profiler.enter("start get inet address"); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; + InetAddress address; while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); Enumeration addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { address = addresses.nextElement(); - if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && address.isSiteLocalAddress()) { + if (!address.isLoopbackAddress() + && address.getHostAddress().indexOf(":") == -1 + && address.isSiteLocalAddress()) { return address.getHostAddress(); } } @@ -100,16 +102,23 @@ public static String getExtranetIp() { return EXTRANET_IP; } + /** + * 获取外网IP + * + * @return + */ public static String getExtranetAddress() { try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; + InetAddress address; while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); Enumeration addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { address = addresses.nextElement(); - if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && !address.isSiteLocalAddress()) { + if (!address.isLoopbackAddress() + && address.getHostAddress().indexOf(":") == -1 + && !address.isSiteLocalAddress()) { return address.getHostAddress(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java index 7f305bf3..1319282e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -20,8 +20,7 @@ package com.mpush.tools.config; import com.mpush.tools.Utils; - -import static com.mpush.tools.Utils.getInetAddress; +import com.mpush.tools.config.CC.mp.net.public_ip_mapping; /** * Created by yxx on 2016/5/18. @@ -41,15 +40,30 @@ public int getHeartbeat(int min, int max) { ); } + /** + * 获取内网IP地址 + * + * @return + */ public String getLocalIp() { return Utils.getLocalIp(); } + /** + * 获取外网IP地址 + * + * @return + */ public String getPublicIp() { - String localIp = getInetAddress(); - String remoteIp = CC.mp.net.public_ip_mapping.getString(localIp); + String localIp = Utils.getLocalIp(); + + String remoteIp = public_ip_mapping.getString(localIp); + + if (remoteIp == null) { + remoteIp = Utils.getExtranetIp(); + } - return remoteIp; + return remoteIp == null ? localIp : remoteIp; } } From c2cb56cc49d9bdeebeedec4bed1b887fca037c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 00:13:09 +0800 Subject: [PATCH 570/890] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=85=AC=E7=BD=91?= =?UTF-8?q?=E5=9C=B0=E5=9D=80bug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/tools/Utils.java | 19 +++++++++++---- .../com/mpush/tools/config/ConfigManager.java | 24 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index 6a041dfd..fe7759d1 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -70,15 +70,17 @@ public static String getLocalIp() { */ public static String getInetAddress() { try { - Profiler.enter("start get inet addresss"); + Profiler.enter("start get inet address"); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; + InetAddress address; while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); Enumeration addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { address = addresses.nextElement(); - if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && address.isSiteLocalAddress()) { + if (!address.isLoopbackAddress() + && address.getHostAddress().indexOf(":") == -1 + && address.isSiteLocalAddress()) { return address.getHostAddress(); } } @@ -100,16 +102,23 @@ public static String getExtranetIp() { return EXTRANET_IP; } + /** + * 获取外网IP + * + * @return + */ public static String getExtranetAddress() { try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address = null; + InetAddress address; while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); Enumeration addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { address = addresses.nextElement(); - if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1 && !address.isSiteLocalAddress()) { + if (!address.isLoopbackAddress() + && address.getHostAddress().indexOf(":") == -1 + && !address.isSiteLocalAddress()) { return address.getHostAddress(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java index 7f305bf3..1319282e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -20,8 +20,7 @@ package com.mpush.tools.config; import com.mpush.tools.Utils; - -import static com.mpush.tools.Utils.getInetAddress; +import com.mpush.tools.config.CC.mp.net.public_ip_mapping; /** * Created by yxx on 2016/5/18. @@ -41,15 +40,30 @@ public int getHeartbeat(int min, int max) { ); } + /** + * 获取内网IP地址 + * + * @return + */ public String getLocalIp() { return Utils.getLocalIp(); } + /** + * 获取外网IP地址 + * + * @return + */ public String getPublicIp() { - String localIp = getInetAddress(); - String remoteIp = CC.mp.net.public_ip_mapping.getString(localIp); + String localIp = Utils.getLocalIp(); + + String remoteIp = public_ip_mapping.getString(localIp); + + if (remoteIp == null) { + remoteIp = Utils.getExtranetIp(); + } - return remoteIp; + return remoteIp == null ? localIp : remoteIp; } } From b91764f3a3fd9ad341d6f678bc9a6b4a3fb092a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 00:13:37 +0800 Subject: [PATCH 571/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/zk/ZKClient.java | 51 ++++++++----------- .../java/com/mpush/zk/cache/ZKNodeCache.java | 7 +++ .../mpush/zk/listener/ZKNodeCacheWatcher.java | 6 +++ .../main/java/com/mpush/zk/node/ZKNode.java | 6 ++- .../java/com/mpush/zk/node/ZKRedisNode.java | 7 +-- .../java/com/mpush/zk/node/ZKServerNode.java | 12 ++--- 6 files changed, 46 insertions(+), 43 deletions(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 81b578f6..0ac488a4 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -30,15 +30,12 @@ import org.apache.curator.framework.api.ACLProvider; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.state.ConnectionState; -import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.concurrent.TimeUnit; @@ -118,13 +115,10 @@ public List getAclForPath(final String path) { // 注册连接状态监听器 private void addConnectionStateListener() { - client.getConnectionStateListenable().addListener(new ConnectionStateListener() { - //TODO need close jvm? - @Override - public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); - } - }); + client.getConnectionStateListenable() + .addListener((cli, newState) + -> Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", + newState, newState.isConnected())); } // 本地缓存 @@ -159,7 +153,7 @@ public String get(final String key) { public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Constants.UTF_8); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("getFromRemote:{}", key, ex); return null; } @@ -174,15 +168,9 @@ public String getFromRemote(final String key) { public List getChildrenKeys(final String key) { try { List result = client.getChildren().forPath(key); - Collections.sort(result, new Comparator() { - - @Override - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); + Collections.sort(result, (o1, o2) -> o2.compareTo(o1)); return result; - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("getChildrenKeys:{}", key, ex); return Collections.emptyList(); } @@ -197,7 +185,7 @@ public int compare(final String o1, final String o2) { public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("isExisted:{}", key, ex); return false; } @@ -211,12 +199,12 @@ public boolean isExisted(final String key) { */ public void registerPersist(final String key, final String value) { try { - if (!isExisted(key)) { - client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); - } else { + if (isExisted(key)) { update(key, value); + } else { + client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); } - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persist:{},{}", key, value, ex); throw new ZKException(ex); } @@ -230,8 +218,13 @@ public void registerPersist(final String key, final String value) { */ public void update(final String key, final String value) { try { + /*TransactionOp op = client.transactionOp(); + client.transaction().forOperations( + op.check().forPath(key), + op.setData().forPath(key, value.getBytes(Constants.UTF_8)) + );*/ client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Constants.UTF_8)).and().commit(); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("update:{},{}", key, value, ex); throw new ZKException(ex); } @@ -249,7 +242,7 @@ public void registerEphemeral(final String key, final String value) { client.delete().deletingChildrenIfNeeded().forPath(key); } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Constants.UTF_8)); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persistEphemeral:{},{}", key, value, ex); throw new ZKException(ex); } @@ -263,7 +256,7 @@ public void registerEphemeral(final String key, final String value) { public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persistEphemeralSequential:{},{}", key, value, ex); throw new ZKException(ex); } @@ -277,7 +270,7 @@ public void registerEphemeralSequential(final String key, final String value) { public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persistEphemeralSequential:{}", key, ex); throw new ZKException(ex); } @@ -291,7 +284,7 @@ public void registerEphemeralSequential(final String key) { public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("removeAndClose:{}", key, ex); throw new ZKException(ex); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java index c4a3023c..5a6bbcb3 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -24,6 +24,13 @@ import java.util.Collection; import java.util.List; +/** + * ZK节点缓存方案, 不同等节点应该有不同实现 + *

+ * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ public interface ZKNodeCache { void addAll(List list); diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java index 4cd661cd..67331f57 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -27,6 +27,12 @@ import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; +/** + * 缓存节点变化监听 + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ public abstract class ZKNodeCacheWatcher implements TreeCacheListener { @Override diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java index 7a279ead..bc7de9bc 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -19,11 +19,15 @@ package com.mpush.zk.node; +import com.mpush.tools.Jsons; + /** * Created by yxx on 2016/5/17. * * @author ohun@live.cn */ public interface ZKNode { - String encode(); + default String encode() { + return Jsons.toJson(this); + } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java index 0d30efec..16d2ef23 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -19,10 +19,10 @@ package com.mpush.zk.node; -import com.mpush.tools.Jsons; import com.mpush.tools.config.data.RedisGroup; /** + * Redis 节点配置 * Created by yxx on 2016/5/18. * * @author ohun@live.cn @@ -37,9 +37,4 @@ public String getZkPath() { public void setZkPath(String zkPath) { this.zkPath = zkPath; } - - @Override - public String encode() { - return Jsons.toJson(this); - } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index 73d24e84..f3b23ec5 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -20,14 +20,17 @@ package com.mpush.zk.node; -import com.mpush.tools.Jsons; import com.mpush.tools.Utils; import com.mpush.tools.config.CC; import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKPath; /** - * 系统配置 + * MPUSH server 节点配置 + *

+ * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn */ public class ZKServerNode implements ZKNode { @@ -127,9 +130,4 @@ public String toString() { ", zkPath='" + zkPath + '\'' + '}'; } - - @Override - public String encode() { - return Jsons.toJson(this); - } } From cc22f9a82fdefc77759512a868f397ca13422d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 00:13:37 +0800 Subject: [PATCH 572/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/zk/ZKClient.java | 51 ++++++++----------- .../java/com/mpush/zk/cache/ZKNodeCache.java | 7 +++ .../mpush/zk/listener/ZKNodeCacheWatcher.java | 6 +++ .../main/java/com/mpush/zk/node/ZKNode.java | 6 ++- .../java/com/mpush/zk/node/ZKRedisNode.java | 7 +-- .../java/com/mpush/zk/node/ZKServerNode.java | 12 ++--- 6 files changed, 46 insertions(+), 43 deletions(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 81b578f6..0ac488a4 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -30,15 +30,12 @@ import org.apache.curator.framework.api.ACLProvider; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.state.ConnectionState; -import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.concurrent.TimeUnit; @@ -118,13 +115,10 @@ public List getAclForPath(final String path) { // 注册连接状态监听器 private void addConnectionStateListener() { - client.getConnectionStateListenable().addListener(new ConnectionStateListener() { - //TODO need close jvm? - @Override - public void stateChanged(final CuratorFramework client, final ConnectionState newState) { - Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); - } - }); + client.getConnectionStateListenable() + .addListener((cli, newState) + -> Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", + newState, newState.isConnected())); } // 本地缓存 @@ -159,7 +153,7 @@ public String get(final String key) { public String getFromRemote(final String key) { try { return new String(client.getData().forPath(key), Constants.UTF_8); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("getFromRemote:{}", key, ex); return null; } @@ -174,15 +168,9 @@ public String getFromRemote(final String key) { public List getChildrenKeys(final String key) { try { List result = client.getChildren().forPath(key); - Collections.sort(result, new Comparator() { - - @Override - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); + Collections.sort(result, (o1, o2) -> o2.compareTo(o1)); return result; - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("getChildrenKeys:{}", key, ex); return Collections.emptyList(); } @@ -197,7 +185,7 @@ public int compare(final String o1, final String o2) { public boolean isExisted(final String key) { try { return null != client.checkExists().forPath(key); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("isExisted:{}", key, ex); return false; } @@ -211,12 +199,12 @@ public boolean isExisted(final String key) { */ public void registerPersist(final String key, final String value) { try { - if (!isExisted(key)) { - client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); - } else { + if (isExisted(key)) { update(key, value); + } else { + client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes()); } - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persist:{},{}", key, value, ex); throw new ZKException(ex); } @@ -230,8 +218,13 @@ public void registerPersist(final String key, final String value) { */ public void update(final String key, final String value) { try { + /*TransactionOp op = client.transactionOp(); + client.transaction().forOperations( + op.check().forPath(key), + op.setData().forPath(key, value.getBytes(Constants.UTF_8)) + );*/ client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Constants.UTF_8)).and().commit(); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("update:{},{}", key, value, ex); throw new ZKException(ex); } @@ -249,7 +242,7 @@ public void registerEphemeral(final String key, final String value) { client.delete().deletingChildrenIfNeeded().forPath(key); } client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Constants.UTF_8)); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persistEphemeral:{},{}", key, value, ex); throw new ZKException(ex); } @@ -263,7 +256,7 @@ public void registerEphemeral(final String key, final String value) { public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persistEphemeralSequential:{},{}", key, value, ex); throw new ZKException(ex); } @@ -277,7 +270,7 @@ public void registerEphemeralSequential(final String key, final String value) { public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("persistEphemeralSequential:{}", key, ex); throw new ZKException(ex); } @@ -291,7 +284,7 @@ public void registerEphemeralSequential(final String key) { public void remove(final String key) { try { client.delete().deletingChildrenIfNeeded().forPath(key); - } catch (final Exception ex) { + } catch (Exception ex) { Logs.ZK.error("removeAndClose:{}", key, ex); throw new ZKException(ex); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java index c4a3023c..5a6bbcb3 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -24,6 +24,13 @@ import java.util.Collection; import java.util.List; +/** + * ZK节点缓存方案, 不同等节点应该有不同实现 + *

+ * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ public interface ZKNodeCache { void addAll(List list); diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java index 4cd661cd..67331f57 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -27,6 +27,12 @@ import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; +/** + * 缓存节点变化监听 + * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn + */ public abstract class ZKNodeCacheWatcher implements TreeCacheListener { @Override diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java index 7a279ead..bc7de9bc 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -19,11 +19,15 @@ package com.mpush.zk.node; +import com.mpush.tools.Jsons; + /** * Created by yxx on 2016/5/17. * * @author ohun@live.cn */ public interface ZKNode { - String encode(); + default String encode() { + return Jsons.toJson(this); + } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java index 0d30efec..16d2ef23 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -19,10 +19,10 @@ package com.mpush.zk.node; -import com.mpush.tools.Jsons; import com.mpush.tools.config.data.RedisGroup; /** + * Redis 节点配置 * Created by yxx on 2016/5/18. * * @author ohun@live.cn @@ -37,9 +37,4 @@ public String getZkPath() { public void setZkPath(String zkPath) { this.zkPath = zkPath; } - - @Override - public String encode() { - return Jsons.toJson(this); - } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index 73d24e84..f3b23ec5 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -20,14 +20,17 @@ package com.mpush.zk.node; -import com.mpush.tools.Jsons; import com.mpush.tools.Utils; import com.mpush.tools.config.CC; import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKPath; /** - * 系统配置 + * MPUSH server 节点配置 + *

+ * Created by yxx on 2016/5/18. + * + * @author ohun@live.cn */ public class ZKServerNode implements ZKNode { @@ -127,9 +130,4 @@ public String toString() { ", zkPath='" + zkPath + '\'' + '}'; } - - @Override - public String encode() { - return Jsons.toJson(this); - } } From 12ac9845cfb45512b84695bbaf6266232276e039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 00:14:52 +0800 Subject: [PATCH 573/890] =?UTF-8?q?java8=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/bootstrap/job/ServerBoot.java | 35 ++++++++--------- .../connect/ConnClientChannelHandler.java | 18 ++++----- .../com/mpush/netty/client/NettyClient.java | 17 ++++----- .../mpush/netty/http/HttpConnectionPool.java | 4 +- .../com/mpush/netty/http/NettyHttpClient.java | 36 ++++++++---------- .../com/mpush/netty/server/NettyServer.java | 17 ++++----- .../java/com/mpush/tools/common/JVMUtil.java | 38 ++++++++----------- .../java/com/mpush/tools/event/EventBus.java | 8 +--- 8 files changed, 71 insertions(+), 102 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 86bde932..4ae1e89f 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -44,27 +44,24 @@ public ServerBoot(Server server, ZKServerNode node) { @Override public void run() { final String serverName = server.getClass().getSimpleName(); - ThreadPoolManager.I.newThread(serverName, new Runnable() { - @Override - public void run() { - server.init(); - server.start(new Listener() { - @Override - public void onSuccess(Object... args) { - Logs.Console.error("start " + serverName + " success listen:" + args[0]); - if (node != null) { - registerServerToZk(node.getZkPath(), Jsons.toJson(node)); - } - next(); + ThreadPoolManager.I.newThread(serverName, () -> { + server.init(); + server.start(new Listener() { + @Override + public void onSuccess(Object... args) { + Logs.Console.error("start " + serverName + " success listen:" + args[0]); + if (node != null) { + registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } + next(); + } - @Override - public void onFailure(Throwable cause) { - Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); - System.exit(-1); - } - }); - } + @Override + public void onFailure(Throwable cause) { + Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); + System.exit(-1); + } + }); }).start(); } diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 7daaf8ba..93994eba 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -21,6 +21,7 @@ import com.google.common.collect.Maps; +import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.protocol.Command; @@ -108,7 +109,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception message.send(); } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); - LOGGER.warn("receive an push message, content=" + message.content); + LOGGER.warn("receive an push message, content=" + new String(message.content, Constants.UTF_8)); } else if (command == Command.HEARTBEAT) { LOGGER.warn("receive a heartbeat pong..."); } else { @@ -217,16 +218,13 @@ public void run(Timeout timeout) throws Exception { final Channel channel = connection.getChannel(); if (channel.isActive()) { ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); - } else { - LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); - } - HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); + channelFuture.addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + } else { + LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); } + HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); }); } else { LOGGER.error("connection was closed, connection={}", connection); diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index b3efb889..0854143c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -69,16 +69,13 @@ public void initChannel(SocketChannel ch) throws Exception { }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - future.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - if (listener != null) listener.onSuccess(port); - LOGGER.info("start netty client success, host={}, port={}", host, port); - } else { - if (listener != null) listener.onFailure(future.cause()); - LOGGER.error("start netty client failure, host={}, port={}", host, port, future.cause()); - } + future.addListener((ChannelFutureListener) f -> { + if (f.isSuccess()) { + if (listener != null) listener.onSuccess(port); + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + if (listener != null) listener.onFailure(f.cause()); + LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); } }); } else { diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java index ce55224e..dcbf3dbd 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java @@ -77,9 +77,7 @@ public void attachHost(String host, Channel channel) { } public void close() { - for (Channel channel : channelPool.values()) { - channel.close(); - } + channelPool.values().forEach(Channel::close); channelPool.clear(); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index 9af0db7e..e62093ab 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -82,17 +82,14 @@ public void request(RequestContext context) throws Exception { final long startCreate = System.currentTimeMillis(); LOGGER.debug("create new channel, host={}", host); ChannelFuture f = b.connect(host, port); - f.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); - if (future.isSuccess()) {//3.1.把请求写到http server - writeRequest(future.channel(), context); - } else {//3.2如果链接创建失败,直接返回客户端网关超时 - context.tryDone(); - context.onFailure(504, "Gateway Timeout"); - LOGGER.warn("create new channel failure, request={}", context); - } + f.addListener((ChannelFutureListener) future -> { + LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); + if (future.isSuccess()) {//3.1.把请求写到http server + writeRequest(future.channel(), context); + } else {//3.2如果链接创建失败,直接返回客户端网关超时 + context.tryDone(); + context.onFailure(504, "Gateway Timeout"); + LOGGER.warn("create new channel failure, request={}", context); } }); } else { @@ -104,16 +101,13 @@ public void operationComplete(ChannelFuture future) throws Exception { private void writeRequest(Channel channel, RequestContext context) { channel.attr(requestKey).set(context); pool.attachHost(context.host, channel); - channel.writeAndFlush(context.request).addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - RequestContext info = future.channel().attr(requestKey).getAndRemove(); - info.tryDone(); - info.onFailure(503, "Service Unavailable"); - LOGGER.debug("request failure request={}", info); - pool.tryRelease(future.channel()); - } + channel.writeAndFlush(context.request).addListener((ChannelFutureListener) future -> { + if (!future.isSuccess()) { + RequestContext info = future.channel().attr(requestKey).getAndRemove(); + info.tryDone(); + info.onFailure(503, "Service Unavailable"); + LOGGER.debug("request failure request={}", info); + pool.tryRelease(future.channel()); } }); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 0b242517..c32553b4 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -157,16 +157,13 @@ public void initChannel(SocketChannel ch) throws Exception { /*** * 绑定端口并启动去接收进来的连接 */ - ChannelFuture f = b.bind(port).sync().addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - Logs.Console.error("server start success on:{}", port); - if (listener != null) listener.onSuccess(port); - } else { - Logs.Console.error("server start failure on:{}", port, future.cause()); - if (listener != null) listener.onFailure(future.cause()); - } + ChannelFuture f = b.bind(port).sync().addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + Logs.Console.error("server start success on:{}", port); + if (listener != null) listener.onSuccess(port); + } else { + Logs.Console.error("server start failure on:{}", port, future.cause()); + if (listener != null) listener.onFailure(future.cause()); } }); if (f.isSuccess()) { diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java index 34159ba4..2fb583bb 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -107,26 +107,23 @@ public static void jstack(OutputStream stream) throws Exception { } public static void dumpJstack(final String jvmPath) { - new Thread((new Runnable() { - @Override - public void run() { - String logPath = jvmPath; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); - JVMUtil.jstack(jstackStream); - } catch (Throwable t) { - LOGGER.error("Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } + new Thread((() -> { + String logPath = jvmPath; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); + JVMUtil.jstack(jstackStream); + } catch (Throwable t) { + LOGGER.error("Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { } } } - })).start(); + }),"mp-monitor-dump-jstack-thread").start(); } private static HotSpotDiagnosticMXBean getHotSpotMXBean() { @@ -177,11 +174,6 @@ public static void jMap(String fileName, boolean live) { } public static void dumpJmap(final String jvmPath) { - new Thread(new Runnable() { - @Override - public void run() { - jMap(jvmPath, false); - } - }).start(); + new Thread(() -> jMap(jvmPath, false), "mp-monitor-dump-jmap-thread").start(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index f547073d..4454f453 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -41,12 +41,8 @@ public class EventBus { public EventBus() { Executor executor = ThreadPoolManager.I.getEventBusExecutor(); - eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { - @Override - public void handleException(Throwable exception, SubscriberExceptionContext context) { - LOGGER.error("event bus subscriber ex", exception); - } - }); + eventBus = new AsyncEventBus(executor, (exception, context) + -> LOGGER.error("event bus subscriber ex", exception)); } public void post(Event event) { From 5cd94b064e144fc6812872a7d44c742ec7dcfbd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 00:14:52 +0800 Subject: [PATCH 574/890] =?UTF-8?q?java8=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/bootstrap/job/ServerBoot.java | 35 ++++++++--------- .../connect/ConnClientChannelHandler.java | 18 ++++----- .../com/mpush/netty/client/NettyClient.java | 17 ++++----- .../mpush/netty/http/HttpConnectionPool.java | 4 +- .../com/mpush/netty/http/NettyHttpClient.java | 36 ++++++++---------- .../com/mpush/netty/server/NettyServer.java | 17 ++++----- .../java/com/mpush/tools/common/JVMUtil.java | 38 ++++++++----------- .../java/com/mpush/tools/event/EventBus.java | 8 +--- 8 files changed, 71 insertions(+), 102 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 86bde932..4ae1e89f 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -44,27 +44,24 @@ public ServerBoot(Server server, ZKServerNode node) { @Override public void run() { final String serverName = server.getClass().getSimpleName(); - ThreadPoolManager.I.newThread(serverName, new Runnable() { - @Override - public void run() { - server.init(); - server.start(new Listener() { - @Override - public void onSuccess(Object... args) { - Logs.Console.error("start " + serverName + " success listen:" + args[0]); - if (node != null) { - registerServerToZk(node.getZkPath(), Jsons.toJson(node)); - } - next(); + ThreadPoolManager.I.newThread(serverName, () -> { + server.init(); + server.start(new Listener() { + @Override + public void onSuccess(Object... args) { + Logs.Console.error("start " + serverName + " success listen:" + args[0]); + if (node != null) { + registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } + next(); + } - @Override - public void onFailure(Throwable cause) { - Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); - System.exit(-1); - } - }); - } + @Override + public void onFailure(Throwable cause) { + Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); + System.exit(-1); + } + }); }).start(); } diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 7daaf8ba..93994eba 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -21,6 +21,7 @@ import com.google.common.collect.Maps; +import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.protocol.Command; @@ -108,7 +109,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception message.send(); } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); - LOGGER.warn("receive an push message, content=" + message.content); + LOGGER.warn("receive an push message, content=" + new String(message.content, Constants.UTF_8)); } else if (command == Command.HEARTBEAT) { LOGGER.warn("receive a heartbeat pong..."); } else { @@ -217,16 +218,13 @@ public void run(Timeout timeout) throws Exception { final Channel channel = connection.getChannel(); if (channel.isActive()) { ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); - } else { - LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); - } - HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); + channelFuture.addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + } else { + LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); } + HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); }); } else { LOGGER.error("connection was closed, connection={}", connection); diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java index b3efb889..0854143c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java @@ -69,16 +69,13 @@ public void initChannel(SocketChannel ch) throws Exception { }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - future.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - if (listener != null) listener.onSuccess(port); - LOGGER.info("start netty client success, host={}, port={}", host, port); - } else { - if (listener != null) listener.onFailure(future.cause()); - LOGGER.error("start netty client failure, host={}, port={}", host, port, future.cause()); - } + future.addListener((ChannelFutureListener) f -> { + if (f.isSuccess()) { + if (listener != null) listener.onSuccess(port); + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + if (listener != null) listener.onFailure(f.cause()); + LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); } }); } else { diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java index ce55224e..dcbf3dbd 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java @@ -77,9 +77,7 @@ public void attachHost(String host, Channel channel) { } public void close() { - for (Channel channel : channelPool.values()) { - channel.close(); - } + channelPool.values().forEach(Channel::close); channelPool.clear(); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index 9af0db7e..e62093ab 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -82,17 +82,14 @@ public void request(RequestContext context) throws Exception { final long startCreate = System.currentTimeMillis(); LOGGER.debug("create new channel, host={}", host); ChannelFuture f = b.connect(host, port); - f.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); - if (future.isSuccess()) {//3.1.把请求写到http server - writeRequest(future.channel(), context); - } else {//3.2如果链接创建失败,直接返回客户端网关超时 - context.tryDone(); - context.onFailure(504, "Gateway Timeout"); - LOGGER.warn("create new channel failure, request={}", context); - } + f.addListener((ChannelFutureListener) future -> { + LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); + if (future.isSuccess()) {//3.1.把请求写到http server + writeRequest(future.channel(), context); + } else {//3.2如果链接创建失败,直接返回客户端网关超时 + context.tryDone(); + context.onFailure(504, "Gateway Timeout"); + LOGGER.warn("create new channel failure, request={}", context); } }); } else { @@ -104,16 +101,13 @@ public void operationComplete(ChannelFuture future) throws Exception { private void writeRequest(Channel channel, RequestContext context) { channel.attr(requestKey).set(context); pool.attachHost(context.host, channel); - channel.writeAndFlush(context.request).addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - RequestContext info = future.channel().attr(requestKey).getAndRemove(); - info.tryDone(); - info.onFailure(503, "Service Unavailable"); - LOGGER.debug("request failure request={}", info); - pool.tryRelease(future.channel()); - } + channel.writeAndFlush(context.request).addListener((ChannelFutureListener) future -> { + if (!future.isSuccess()) { + RequestContext info = future.channel().attr(requestKey).getAndRemove(); + info.tryDone(); + info.onFailure(503, "Service Unavailable"); + LOGGER.debug("request failure request={}", info); + pool.tryRelease(future.channel()); } }); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 0b242517..c32553b4 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -157,16 +157,13 @@ public void initChannel(SocketChannel ch) throws Exception { /*** * 绑定端口并启动去接收进来的连接 */ - ChannelFuture f = b.bind(port).sync().addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (future.isSuccess()) { - Logs.Console.error("server start success on:{}", port); - if (listener != null) listener.onSuccess(port); - } else { - Logs.Console.error("server start failure on:{}", port, future.cause()); - if (listener != null) listener.onFailure(future.cause()); - } + ChannelFuture f = b.bind(port).sync().addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + Logs.Console.error("server start success on:{}", port); + if (listener != null) listener.onSuccess(port); + } else { + Logs.Console.error("server start failure on:{}", port, future.cause()); + if (listener != null) listener.onFailure(future.cause()); } }); if (f.isSuccess()) { diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java index 34159ba4..2fb583bb 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -107,26 +107,23 @@ public static void jstack(OutputStream stream) throws Exception { } public static void dumpJstack(final String jvmPath) { - new Thread((new Runnable() { - @Override - public void run() { - String logPath = jvmPath; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); - JVMUtil.jstack(jstackStream); - } catch (Throwable t) { - LOGGER.error("Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } + new Thread((() -> { + String logPath = jvmPath; + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); + JVMUtil.jstack(jstackStream); + } catch (Throwable t) { + LOGGER.error("Dump JVM cache Error!", t); + } finally { + if (jstackStream != null) { + try { + jstackStream.close(); + } catch (IOException e) { } } } - })).start(); + }),"mp-monitor-dump-jstack-thread").start(); } private static HotSpotDiagnosticMXBean getHotSpotMXBean() { @@ -177,11 +174,6 @@ public static void jMap(String fileName, boolean live) { } public static void dumpJmap(final String jvmPath) { - new Thread(new Runnable() { - @Override - public void run() { - jMap(jvmPath, false); - } - }).start(); + new Thread(() -> jMap(jvmPath, false), "mp-monitor-dump-jmap-thread").start(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java index f547073d..4454f453 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java +++ b/mpush-tools/src/main/java/com/mpush/tools/event/EventBus.java @@ -41,12 +41,8 @@ public class EventBus { public EventBus() { Executor executor = ThreadPoolManager.I.getEventBusExecutor(); - eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { - @Override - public void handleException(Throwable exception, SubscriberExceptionContext context) { - LOGGER.error("event bus subscriber ex", exception); - } - }); + eventBus = new AsyncEventBus(executor, (exception, context) + -> LOGGER.error("event bus subscriber ex", exception)); } public void post(Event event) { From 2e3dc69a4321f57f6e1649ae34e0f83360cd9e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 00:15:44 +0800 Subject: [PATCH 575/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=81=9C=E6=AD=A2=20?= =?UTF-8?q?=E4=BC=98=E9=9B=85=E5=85=B3=E9=97=AD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/bootstrap/Main.java | 35 ++++++++++++++++--- .../com/mpush/bootstrap/ServerLauncher.java | 23 ++++++------ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 2a0550ff..607f1052 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -20,9 +20,10 @@ package com.mpush.bootstrap; import com.mpush.tools.log.Logs; +import sun.misc.Signal; +import sun.misc.SignalHandler; public class Main { - public static void main(String[] args) { Logs.init(); Logs.Console.error("launch mpush server..."); @@ -32,11 +33,35 @@ public static void main(String[] args) { } private static void addHook(final ServerLauncher launcher) { - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { + Hook hook = new Hook(launcher); + //Signal.handle(new Signal("USR2"), hook); + Runtime.getRuntime().addShutdownHook(new Thread(hook, "mpush-hook-thread")); + } + + private static class Hook implements Runnable, SignalHandler { + private final ServerLauncher launcher; + + private Hook(ServerLauncher launcher) { + this.launcher = launcher; + } + + @Override + public void run() { + stop(); + } + + @Override + public void handle(Signal signal) { + stop(); + } + + private void stop() { + try { launcher.stop(); - Logs.Console.error("jvm exit, all server stopped..."); + } catch (Exception e) { + Logs.Console.error("mpush server stop ex", e); } - }); + Logs.Console.error("jvm exit, all server stopped..."); + } } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 18edd96b..3b34ba87 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,7 +20,8 @@ package com.mpush.bootstrap; -import com.mpush.api.service.Server; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Service; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; @@ -30,6 +31,8 @@ import com.mpush.zk.ZKClient; import com.mpush.zk.node.ZKServerNode; +import java.util.concurrent.TimeUnit; + /** * Created by yxx on 2016/5/14. * @@ -61,17 +64,17 @@ public void start() { chain.run(); } - public void stop() { - stopServer(connectServer); - stopServer(gatewayServer); - stopServer(adminServer); - ZKClient.I.stop(); - MonitorService.I.stop(); + public void stop() throws Exception { + stopService(connectServer); + stopService(gatewayServer); + stopService(adminServer); + stopService(ZKClient.I); + stopService(MonitorService.I); } - private void stopServer(Server server) { - if (server != null) { - server.stop(null); + private void stopService(Service service) throws Exception { + if (service != null) { + service.stop().get(1, TimeUnit.MINUTES); } } } From d48a252ab3060b219f94c1e6e1db77ada22dcce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 00:15:44 +0800 Subject: [PATCH 576/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=81=9C=E6=AD=A2=20?= =?UTF-8?q?=E4=BC=98=E9=9B=85=E5=85=B3=E9=97=AD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/bootstrap/Main.java | 35 ++++++++++++++++--- .../com/mpush/bootstrap/ServerLauncher.java | 23 ++++++------ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 2a0550ff..607f1052 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -20,9 +20,10 @@ package com.mpush.bootstrap; import com.mpush.tools.log.Logs; +import sun.misc.Signal; +import sun.misc.SignalHandler; public class Main { - public static void main(String[] args) { Logs.init(); Logs.Console.error("launch mpush server..."); @@ -32,11 +33,35 @@ public static void main(String[] args) { } private static void addHook(final ServerLauncher launcher) { - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { + Hook hook = new Hook(launcher); + //Signal.handle(new Signal("USR2"), hook); + Runtime.getRuntime().addShutdownHook(new Thread(hook, "mpush-hook-thread")); + } + + private static class Hook implements Runnable, SignalHandler { + private final ServerLauncher launcher; + + private Hook(ServerLauncher launcher) { + this.launcher = launcher; + } + + @Override + public void run() { + stop(); + } + + @Override + public void handle(Signal signal) { + stop(); + } + + private void stop() { + try { launcher.stop(); - Logs.Console.error("jvm exit, all server stopped..."); + } catch (Exception e) { + Logs.Console.error("mpush server stop ex", e); } - }); + Logs.Console.error("jvm exit, all server stopped..."); + } } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 18edd96b..3b34ba87 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,7 +20,8 @@ package com.mpush.bootstrap; -import com.mpush.api.service.Server; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Service; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; @@ -30,6 +31,8 @@ import com.mpush.zk.ZKClient; import com.mpush.zk.node.ZKServerNode; +import java.util.concurrent.TimeUnit; + /** * Created by yxx on 2016/5/14. * @@ -61,17 +64,17 @@ public void start() { chain.run(); } - public void stop() { - stopServer(connectServer); - stopServer(gatewayServer); - stopServer(adminServer); - ZKClient.I.stop(); - MonitorService.I.stop(); + public void stop() throws Exception { + stopService(connectServer); + stopService(gatewayServer); + stopService(adminServer); + stopService(ZKClient.I); + stopService(MonitorService.I); } - private void stopServer(Server server) { - if (server != null) { - server.stop(null); + private void stopService(Service service) throws Exception { + if (service != null) { + service.stop().get(1, TimeUnit.MINUTES); } } } From c9ce4dc18c9b205549434bb973fa55e668b7a1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Sun, 21 Aug 2016 22:03:12 +0800 Subject: [PATCH 577/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8,?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E6=B5=81=E7=A8=8B=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/{mp-env.cmd => env-mp.cmd} | 0 bin/{mp-env.sh => env-mp.sh} | 48 +++---- bin/mp.cmd | 2 +- bin/mp.sh | 123 +++++++++++------- bin/set-env.sh | 2 +- .../com/mpush/bootstrap/ServerLauncher.java | 37 ++---- .../com/mpush/bootstrap/job/BootChain.java | 24 +++- .../java/com/mpush/bootstrap/job/BootJob.java | 17 ++- .../mpush/bootstrap/job/HttpProxyBoot.java | 20 ++- .../com/mpush/bootstrap/job/LastBoot.java | 14 +- .../com/mpush/bootstrap/job/MonitorBoot.java | 12 +- .../com/mpush/bootstrap/job/RedisBoot.java | 12 +- .../com/mpush/bootstrap/job/ServerBoot.java | 29 ++++- .../java/com/mpush/bootstrap/job/ZKBoot.java | 12 +- .../com/mpush/cache/redis/RedisClient.java | 6 +- .../cache/redis/manager/RedisManager.java | 6 +- 16 files changed, 232 insertions(+), 132 deletions(-) rename bin/{mp-env.cmd => env-mp.cmd} (100%) rename bin/{mp-env.sh => env-mp.sh} (67%) diff --git a/bin/mp-env.cmd b/bin/env-mp.cmd similarity index 100% rename from bin/mp-env.cmd rename to bin/env-mp.cmd diff --git a/bin/mp-env.sh b/bin/env-mp.sh similarity index 67% rename from bin/mp-env.sh rename to bin/env-mp.sh index 618dee65..66fc23ee 100644 --- a/bin/mp-env.sh +++ b/bin/env-mp.sh @@ -18,42 +18,42 @@ # This script should be sourced into other mpush # scripts to setup the env variables -# We use MPCFGDIR if defined, +# We use MP_CFG_DIR if defined, # otherwise we use /etc/mp # or the conf directory that is # a sibling of this script's directory -MPBINDIR="${MPBINDIR:-/usr/bin}" -MPUSH_PREFIX="${MPBINDIR}/.." +MP_BIN_DIR="${MP_BIN_DIR:-/usr/bin}" +MPUSH_PREFIX="${MP_BIN_DIR}/.." -if [ "x$MPCFGDIR" = "x" ] +if [ "x$MP_CFG_DIR" = "x" ] then if [ -e "${MPUSH_PREFIX}/conf" ]; then - MPCFGDIR="$MPBINDIR/../conf" + MP_CFG_DIR="$MP_BIN_DIR/../conf" else - MPCFGDIR="$MPBINDIR/../etc/mpush" + MP_CFG_DIR="$MP_BIN_DIR/../etc/mpush" fi fi -if [ -f "${MPBINDIR}/set-env.sh" ]; then - . "${MPBINDIR}/set-env.sh" +if [ -f "${MP_BIN_DIR}/set-env.sh" ]; then + . "${MP_BIN_DIR}/set-env.sh" fi -if [ "x$MPCFG" = "x" ] +if [ "x$MP_CFG" = "x" ] then - MPCFG="mpush.conf" + MP_CFG="mpush.conf" fi -MPCFG="$MPCFGDIR/$MPCFG" +MP_CFG="$MP_CFG_DIR/$MP_CFG" -if [ -f "$MPBINDIR/java.env" ] +if [ -f "$MP_BIN_DIR/java.env" ] then - . "$MPBINDIR/java.env" + . "$MP_BIN_DIR/java.env" fi -if [ "x${MP_DATADIR}" = "x" ] +if [ "x${MP_DATA_DIR}" = "x" ] then - MP_DATADIR="${MPUSH_PREFIX}/tmp" + MP_DATA_DIR="${MPUSH_PREFIX}/tmp" fi if [ "x${MP_LOG_DIR}" = "x" ] @@ -74,39 +74,39 @@ fi #add the conf dir to classpath -CLASSPATH="$MPCFGDIR:$CLASSPATH" +CLASSPATH="$MP_CFG_DIR:$CLASSPATH" -for i in "$MPBINDIR"/../src/java/lib/*.jar +for i in "$MP_BIN_DIR"/../src/java/lib/*.jar do CLASSPATH="$i:$CLASSPATH" done #make it work in the binary package -#(use array for LIBPATH to account for spaces within wildcard expansion) +#(use array for LIB_PATH to account for spaces within wildcard expansion) if [ -e "${MPUSH_PREFIX}"/share/mpush/mpush-*.jar ]; then - LIBPATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) + LIB_PATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) else #release tarball format - for i in "$MPBINDIR"/../mpush-*.jar + for i in "$MP_BIN_DIR"/../mpush-*.jar do CLASSPATH="$i:$CLASSPATH" done - LIBPATH=("${MPBINDIR}"/../lib/*.jar) + LIB_PATH=("${MP_BIN_DIR}"/../lib/*.jar) fi -for i in "${LIBPATH[@]}" +for i in "${LIB_PATH[@]}" do CLASSPATH="$i:$CLASSPATH" done #make it work for developers -for d in "$MPBINDIR"/../build/lib/*.jar +for d in "$MP_BIN_DIR"/../build/lib/*.jar do CLASSPATH="$d:$CLASSPATH" done #make it work for developers -CLASSPATH="$MPBINDIR/../build/classes:$CLASSPATH" +CLASSPATH="$MP_BIN_DIR/../build/classes:$CLASSPATH" case "`uname`" in diff --git a/bin/mp.cmd b/bin/mp.cmd index f578f4e0..5a1b8448 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -15,7 +15,7 @@ REM See the License for the specific language governing permissions and REM limitations under the License. setlocal -call "%~dp0mpEnv.cmd" +call "%~dp0env-mp.cmd" set MPMAIN="-jar ../boot.jar" echo on diff --git a/bin/mp.sh b/bin/mp.sh index c40e2d3d..abd8d845 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -24,14 +24,14 @@ # use POSTIX interface, symlink is followed automatically -MPBIN="${BASH_SOURCE-$0}" -MPBIN="$(dirname "${MPBIN}")" -MPBINDIR="$(cd "${MPBIN}"; pwd)" +MP_BIN="${BASH_SOURCE-$0}" +MP_BIN="$(dirname "${MP_BIN}")" +MP_BIN_DIR="$(cd "${MP_BIN}"; pwd)" -if [ -e "$MPBIN/../libexec/mp-env.sh" ]; then - . "$MPBINDIR/../libexec/mp-env.sh" +if [ -e "$MP_BIN/../libexec/env-mp.sh" ]; then + . "$MP_BIN_DIR/../libexec/env-mp.sh" else - . "$MPBINDIR/mp-env.sh" + . "$MP_BIN_DIR/env-mp.sh" fi # See the following page for extensive details on setting @@ -51,7 +51,7 @@ then # for some reason these two options are necessary on jdk6 on Ubuntu # accord to the docs they are not necessary, but otw jconsole cannot # do a local attach - MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" + MP_MAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" else if [ "x$JMXAUTH" = "x" ] then @@ -69,41 +69,41 @@ then echo "MPush remote JMX authenticate set to $JMXAUTH" >&2 echo "MPush remote JMX ssl set to $JMXSSL" >&2 echo "MPush remote JMX log4j set to $JMXLOG4J" >&2 - MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" + MP_MAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" fi else echo "JMX disabled by user request" >&2 - MPMAIN="" + MP_MAIN="" fi -MPMAIN="$MPMAIN -jar $MPBINDIR/bootstrap.jar" +MP_MAIN="$MP_MAIN -jar $MP_BIN_DIR/bootstrap.jar" -if [ "x$SERVER_JVMFLAGS" != "x" ] +if [ "x$SERVER_JVM_FLAGS" != "x" ] then - JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" + JVM_FLAGS="$SERVER_JVM_FLAGS $JVM_FLAGS" fi if [ "x$2" != "x" ] then - MPCFG="$MPCFGDIR/$2" + MP_CFG="$MP_CFG_DIR/$2" fi -# if we give a more complicated path to the config, don't screw around in $MPCFGDIR -if [ "x$(dirname "$MPCFG")" != "x$MPCFGDIR" ] +# if we give a more complicated path to the config, don't screw around in $MP_CFG_DIR +if [ "x$(dirname "$MP_CFG")" != "x$MP_CFG_DIR" ] then - MPCFG="$2" + MP_CFG="$2" fi if $cygwin then - MPCFG=`cygpath -wp "$MPCFG"` + MP_CFG=`cygpath -wp "$MP_CFG"` # cygwin has a "kill" in the shell itself, gets confused KILL=/bin/kill else KILL=kill fi -echo "Using config: $MPCFG" >&2 +echo "Using config: $MP_CFG" >&2 case "$OSTYPE" in *solaris*) @@ -113,15 +113,15 @@ case "$OSTYPE" in GREP=grep ;; esac -if [ -z "$MPPIDFILE" ]; then -# MP_DATADIR="$($GREP "^[[:space:]]*dataDir" "$MPCFG" | sed -e 's/.*=//')" - if [ ! -d "$MP_DATADIR" ]; then - mkdir -p "$MP_DATADIR" +if [ -z "$MP_PID_FILE" ]; then +# MP_DATA_DIR="$($GREP "^[[:space:]]*dataDir" "$MP_CFG" | sed -e 's/.*=//')" + if [ ! -d "$MP_DATA_DIR" ]; then + mkdir -p "$MP_DATA_DIR" fi - MPPIDFILE="$MP_DATADIR/mpush_server.pid" + MP_PID_FILE="$MP_DATA_DIR/mpush_server.pid" else # ensure it exists, otw stop will fail - mkdir -p "$(dirname "$MPPIDFILE")" + mkdir -p "$(dirname "$MP_PID_FILE")" fi if [ ! -w "$MP_LOG_DIR" ] ; then @@ -134,22 +134,22 @@ _MP_DAEMON_OUT="$MP_LOG_DIR/mpush.out" case $1 in start) echo -n "Starting mpush ... " - if [ -f "$MPPIDFILE" ]; then - if kill -0 `cat "$MPPIDFILE"` > /dev/null 2>&1; then - echo $command already running as process `cat "$MPPIDFILE"`. + if [ -f "$MP_PID_FILE" ]; then + if kill -0 `cat "$MP_PID_FILE"` > /dev/null 2>&1; then + echo $command already running as process `cat "$MP_PID_FILE"`. exit 0 fi fi - nohup "$JAVA" "-Dmp.conf=$MPCFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS $MPMAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & + nohup "$JAVA" "-Dmp.conf=$MP_CFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & if [ $? -eq 0 ] then case "$OSTYPE" in *solaris*) - /bin/echo "${!}\\c" > "$MPPIDFILE" + /bin/echo "${!}\\c" > "$MP_PID_FILE" ;; *) - /bin/echo -n $! > "$MPPIDFILE" + /bin/echo -n $! > "$MP_PID_FILE" ;; esac if [ $? -eq 0 ]; @@ -171,24 +171,53 @@ start-foreground) MP_CMD=("$JAVA") fi "${MP_CMD[@]}" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS $MPMAIN "-Dmp.conf=$MPCFG" + -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN "-Dmp.conf=$MP_CFG" ;; print-cmd) - echo "\"$JAVA\" $MPMAIN " - echo "\"-Dmp.conf=$MPCFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " - echo "$JVMFLAGS " + echo "\"$JAVA\" $MP_MAIN " + echo "\"-Dmp.conf=$MP_CFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " + echo "$JVM_FLAGS " echo "-cp \"$CLASSPATH\" " echo "> \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" ;; stop) - echo -n "Stopping mpush ... " - if [ ! -f "$MPPIDFILE" ] + echo "Stopping mpush ... " + if [ ! -f "$MP_PID_FILE" ] then - echo "no mpush to stop (could not find file $MPPIDFILE)" + echo "no mpush to stop (could not find file $MP_PID_FILE)" else - $KILL -9 $(cat "$MPPIDFILE") - rm "$MPPIDFILE" - echo STOPPED + $KILL -15 $(cat "$MP_PID_FILE") + SLEEP=30 + SLEEP_COUNT=1 + while [ $SLEEP -ge 0 ]; do + kill -0 $(cat "$MP_PID_FILE") >/dev/null 2>&1 + if [ $? -gt 0 ]; then + rm -f "$MP_PID_FILE" >/dev/null 2>&1 + if [ $? != 0 ]; then + if [ -w "$MP_PID_FILE" ]; then + cat /dev/null > "$MP_PID_FILE" + else + echo "The PID file could not be removed or cleared." + fi + fi + echo STOPPED + break + fi + if [ $SLEEP -gt 0 ]; then + echo "waiting ... $SLEEP_COUNT" + sleep 1 + fi + if [ $SLEEP -eq 0 ]; then + echo "MPUSH did not stop in time." + echo "To aid diagnostics a thread dump has been written to standard out." + kill -3 `cat "$MP_PID_FILE"` + echo "force stop MPUSH." + kill -9 `cat "$MP_PID_FILE"` + echo STOPPED + fi + SLEEP=`expr $SLEEP - 1` + SLEEP_COUNT=`expr $SLEEP_COUNT + 1` + done fi exit 0 ;; @@ -196,7 +225,7 @@ upgrade) shift echo "upgrading the servers to 3.*" "$JAVA" "-Dmpush.log.dir=${MP_LOG_DIR}" "-Dmpush.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS com.mpush.tools.upgrade.UpgradeMain ${@} + -cp "$CLASSPATH" $JVM_FLAGS com.mpush.tools.upgrade.UpgradeMain ${@} echo "Upgrading ... " ;; restart) @@ -207,14 +236,14 @@ restart) ;; status) # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output - clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MP_CFG" | sed -e 's/.*=//'` if ! [ $clientPortAddress ] then - clientPortAddress="localhost" + clientPortAddress="localhost" fi - clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + clientPort=`$GREP "^[[:space:]]*connect-server-port[^[:alpha:]]" "$MP_CFG" | sed -e 's/.*=//'` STAT=`"$JAVA" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS org.apache.mpush.client.FourLetterWordMain \ + -cp "$CLASSPATH" $JVM_FLAGS org.apache.mpush.client.FourLetterWordMain \ $clientPortAddress $clientPort srvr 2> /dev/null \ | $GREP Mode` if [ "x$STAT" = "x" ] @@ -229,4 +258,4 @@ status) *) echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2 -esac +esac \ No newline at end of file diff --git a/bin/set-env.sh b/bin/set-env.sh index 5325b967..9f7bf9b1 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -1,2 +1,2 @@ #!/usr/bin/env bash -#JVMFLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file +#JVM_FLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 3b34ba87..bfa5d493 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,7 +20,6 @@ package com.mpush.bootstrap; -import com.mpush.api.service.BaseService; import com.mpush.api.service.Service; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; @@ -33,25 +32,24 @@ import java.util.concurrent.TimeUnit; +import static com.mpush.tools.config.CC.mp.net.admin_server_port; + /** * Created by yxx on 2016/5/14. * * @author ohun@live.cn */ -public class ServerLauncher { - private final ZKServerNode csNode = ZKServerNode.csNode(); - - private final ZKServerNode gsNode = ZKServerNode.gsNode(); - - private final ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); +public final class ServerLauncher { - private final GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); + private final BootChain chain = BootChain.chain(); - private final AdminServer adminServer = new AdminServer(CC.mp.net.admin_server_port, connectServer, gatewayServer); + public ServerLauncher() { + ZKServerNode csNode = ZKServerNode.csNode(); + ZKServerNode gsNode = ZKServerNode.gsNode(); + ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); + GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); + AdminServer adminServer = new AdminServer(admin_server_port, connectServer, gatewayServer); - - public void start() { - BootChain chain = BootChain.chain(); chain.boot() .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 .setNext(new RedisBoot())//2.注册redis sever 到ZK @@ -61,20 +59,13 @@ public void start() { .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns .setNext(new MonitorBoot())//7.启动监控 .setNext(new LastBoot());//8.启动结束 - chain.run(); } - public void stop() throws Exception { - stopService(connectServer); - stopService(gatewayServer); - stopService(adminServer); - stopService(ZKClient.I); - stopService(MonitorService.I); + public void start() { + chain.start(); } - private void stopService(Service service) throws Exception { - if (service != null) { - service.stop().get(1, TimeUnit.MINUTES); - } + public void stop() { + chain.stop(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index 32544f85..523d59ad 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -26,11 +26,15 @@ * * @author ohun@live.cn */ -public class BootChain { - private BootJob first = first(); +public final class BootChain { + private final BootJob first = first(); - public void run() { - first.run(); + public void start() { + first.start(); + } + + public void stop() { + first.stop(); } public static BootChain chain() { @@ -40,9 +44,15 @@ public static BootChain chain() { private BootJob first() { return new BootJob() { @Override - public void run() { - Logs.Console.error("begin run bootstrap chain..."); - next(); + public void start() { + Logs.Console.error("begin start bootstrap chain..."); + startNext(); + } + + @Override + protected void stop() { + Logs.Console.error("begin stop bootstrap chain..."); + stopNext(); } }; } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java index 32bb5b36..3e332429 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -29,12 +29,21 @@ public abstract class BootJob { private BootJob next; - abstract void run(); + protected abstract void start(); - public void next() { + protected abstract void stop(); + + public void startNext() { + if (next != null) { + Logs.Console.error("start next bootstrap job [" + next.getClass().getSimpleName() + "]"); + next.start(); + } + } + + public void stopNext() { if (next != null) { - Logs.Console.error("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); - next.run(); + Logs.Console.error("stop next bootstrap job [" + next.getClass().getSimpleName() + "]"); + next.stop(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index c1a01431..eec56ad6 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -21,20 +21,30 @@ import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.net.DnsMappingManager; -import com.mpush.common.net.HttpProxyDnsMappingManager; import com.mpush.tools.config.CC; +import static com.mpush.tools.config.CC.mp.spi.dns_mapping_manager; + /** * Created by yxx on 2016/5/15. * * @author ohun@live.cn */ -public class HttpProxyBoot extends BootJob { +public final class HttpProxyBoot extends BootJob { + + @Override + protected void start() { + if (CC.mp.http.proxy_enabled) { + SpiLoader.load(DnsMappingManager.class, dns_mapping_manager).start(); + } + startNext(); + } + @Override - void run() { + protected void stop() { if (CC.mp.http.proxy_enabled) { - SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager).start(); + SpiLoader.load(DnsMappingManager.class, dns_mapping_manager).stop(); } - next(); + stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index bc194877..48ebad40 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -27,13 +27,21 @@ * * @author ohun@live.cn */ -public class LastBoot extends BootJob { +public final class LastBoot extends BootJob { @Override - public void run() { + protected void start() { UserManager.I.clearUserOnlineData(); - Logs.Console.error("end run bootstrap chain..."); + Logs.Console.error("end start bootstrap chain..."); Logs.Console.error("==================================================================="); Logs.Console.error("====================MPUSH SERVER START SUCCESS====================="); Logs.Console.error("==================================================================="); } + + @Override + protected void stop() { + Logs.Console.error("end stop bootstrap chain..."); + Logs.Console.error("==================================================================="); + Logs.Console.error("====================MPUSH SERVER STOPPED SUCCESS====================="); + Logs.Console.error("==================================================================="); + } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java index d0b59ae7..ea2aa1a3 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -26,10 +26,16 @@ * * @author ohun@live.cn */ -public class MonitorBoot extends BootJob { +public final class MonitorBoot extends BootJob { @Override - void run() { + protected void start() { MonitorService.I.start(); - next(); + startNext(); + } + + @Override + protected void stop() { + MonitorService.I.stop(); + stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index e98ca3ee..687d70c5 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -26,11 +26,17 @@ * * @author ohun@live.cn */ -public class RedisBoot extends BootJob { +public final class RedisBoot extends BootJob { @Override - public void run() { + protected void start() { RedisManager.I.init(); - next(); + startNext(); + } + + @Override + protected void stop() { + RedisManager.I.close(); + stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 4ae1e89f..de470d6b 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -21,18 +21,26 @@ import com.mpush.api.service.Listener; import com.mpush.api.service.Server; +import com.mpush.core.server.AdminServer; +import com.mpush.core.server.ConnectionServer; +import com.mpush.core.server.GatewayServer; import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import com.mpush.tools.thread.pool.ThreadPoolManager; import com.mpush.zk.ZKClient; import com.mpush.zk.node.ZKServerNode; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + /** * Created by yxx on 2016/5/14. * * @author ohun@live.cn */ -public class ServerBoot extends BootJob { +public final class ServerBoot extends BootJob { private final Server server; private final ZKServerNode node; @@ -42,8 +50,8 @@ public ServerBoot(Server server, ZKServerNode node) { } @Override - public void run() { - final String serverName = server.getClass().getSimpleName(); + public void start() { + String serverName = server.getClass().getSimpleName(); ThreadPoolManager.I.newThread(serverName, () -> { server.init(); server.start(new Listener() { @@ -53,7 +61,7 @@ public void onSuccess(Object... args) { if (node != null) { registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } - next(); + startNext(); } @Override @@ -65,8 +73,17 @@ public void onFailure(Throwable cause) { }).start(); } - //step7 注册应用到zk - public void registerServerToZk(String path, String value) { + @Override + protected void stop() { + try { + server.stop().get(1, TimeUnit.MINUTES); + } catch (Exception e) { + } + stopNext(); + } + + //注册应用到zk + private void registerServerToZk(String path, String value) { ZKClient.I.registerEphemeralSequential(path, value); Logs.Console.error("register server node=" + value + " to zk name=" + path); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java index e8c33d1d..861b3f64 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -28,14 +28,14 @@ * * @author ohun@live.cn */ -public class ZKBoot extends BootJob { +public final class ZKBoot extends BootJob { @Override - public void run() { + protected void start() { ZKClient.I.start(new Listener() { @Override public void onSuccess(Object... args) { - next(); + startNext(); } @Override @@ -44,4 +44,10 @@ public void onFailure(Throwable cause) { } }); } + + @Override + protected void stop() { + ZKClient.I.stop(); + stopNext(); + } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 2ff4c2cf..e9ded643 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -25,12 +25,13 @@ import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import redis.clients.jedis.*; +import redis.clients.util.Pool; import java.util.*; import static redis.clients.jedis.Protocol.DEFAULT_TIMEOUT; -public class RedisClient { +public final class RedisClient { public static final JedisPoolConfig CONFIG = buildConfig(); private static final Map POOL_MAP = Maps.newConcurrentMap(); @@ -763,4 +764,7 @@ private static List toList(Collection value, Class clazz) { return null; } + public static void destroy() { + POOL_MAP.values().forEach(Pool::close); + } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 50c10e21..a145863a 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -34,7 +34,7 @@ /** * redis 对外封装接口 */ -public class RedisManager { +public final class RedisManager { public static final RedisManager I = new RedisManager(); private final RedisClusterManager clusterManager = ZKRedisClusterManager.I; @@ -315,4 +315,8 @@ public void test(List groupList) { } } } + + public void close() { + RedisClient.destroy(); + } } From 0f471f1a5d3f79c0fb4371afbad9770f473bca7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 21 Aug 2016 22:03:12 +0800 Subject: [PATCH 578/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8,?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E6=B5=81=E7=A8=8B=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/{mp-env.cmd => env-mp.cmd} | 0 bin/{mp-env.sh => env-mp.sh} | 48 +++---- bin/mp.cmd | 2 +- bin/mp.sh | 123 +++++++++++------- bin/set-env.sh | 2 +- .../com/mpush/bootstrap/ServerLauncher.java | 37 ++---- .../com/mpush/bootstrap/job/BootChain.java | 24 +++- .../java/com/mpush/bootstrap/job/BootJob.java | 17 ++- .../mpush/bootstrap/job/HttpProxyBoot.java | 20 ++- .../com/mpush/bootstrap/job/LastBoot.java | 14 +- .../com/mpush/bootstrap/job/MonitorBoot.java | 12 +- .../com/mpush/bootstrap/job/RedisBoot.java | 12 +- .../com/mpush/bootstrap/job/ServerBoot.java | 29 ++++- .../java/com/mpush/bootstrap/job/ZKBoot.java | 12 +- .../com/mpush/cache/redis/RedisClient.java | 6 +- .../cache/redis/manager/RedisManager.java | 6 +- 16 files changed, 232 insertions(+), 132 deletions(-) rename bin/{mp-env.cmd => env-mp.cmd} (100%) rename bin/{mp-env.sh => env-mp.sh} (67%) diff --git a/bin/mp-env.cmd b/bin/env-mp.cmd similarity index 100% rename from bin/mp-env.cmd rename to bin/env-mp.cmd diff --git a/bin/mp-env.sh b/bin/env-mp.sh similarity index 67% rename from bin/mp-env.sh rename to bin/env-mp.sh index 618dee65..66fc23ee 100644 --- a/bin/mp-env.sh +++ b/bin/env-mp.sh @@ -18,42 +18,42 @@ # This script should be sourced into other mpush # scripts to setup the env variables -# We use MPCFGDIR if defined, +# We use MP_CFG_DIR if defined, # otherwise we use /etc/mp # or the conf directory that is # a sibling of this script's directory -MPBINDIR="${MPBINDIR:-/usr/bin}" -MPUSH_PREFIX="${MPBINDIR}/.." +MP_BIN_DIR="${MP_BIN_DIR:-/usr/bin}" +MPUSH_PREFIX="${MP_BIN_DIR}/.." -if [ "x$MPCFGDIR" = "x" ] +if [ "x$MP_CFG_DIR" = "x" ] then if [ -e "${MPUSH_PREFIX}/conf" ]; then - MPCFGDIR="$MPBINDIR/../conf" + MP_CFG_DIR="$MP_BIN_DIR/../conf" else - MPCFGDIR="$MPBINDIR/../etc/mpush" + MP_CFG_DIR="$MP_BIN_DIR/../etc/mpush" fi fi -if [ -f "${MPBINDIR}/set-env.sh" ]; then - . "${MPBINDIR}/set-env.sh" +if [ -f "${MP_BIN_DIR}/set-env.sh" ]; then + . "${MP_BIN_DIR}/set-env.sh" fi -if [ "x$MPCFG" = "x" ] +if [ "x$MP_CFG" = "x" ] then - MPCFG="mpush.conf" + MP_CFG="mpush.conf" fi -MPCFG="$MPCFGDIR/$MPCFG" +MP_CFG="$MP_CFG_DIR/$MP_CFG" -if [ -f "$MPBINDIR/java.env" ] +if [ -f "$MP_BIN_DIR/java.env" ] then - . "$MPBINDIR/java.env" + . "$MP_BIN_DIR/java.env" fi -if [ "x${MP_DATADIR}" = "x" ] +if [ "x${MP_DATA_DIR}" = "x" ] then - MP_DATADIR="${MPUSH_PREFIX}/tmp" + MP_DATA_DIR="${MPUSH_PREFIX}/tmp" fi if [ "x${MP_LOG_DIR}" = "x" ] @@ -74,39 +74,39 @@ fi #add the conf dir to classpath -CLASSPATH="$MPCFGDIR:$CLASSPATH" +CLASSPATH="$MP_CFG_DIR:$CLASSPATH" -for i in "$MPBINDIR"/../src/java/lib/*.jar +for i in "$MP_BIN_DIR"/../src/java/lib/*.jar do CLASSPATH="$i:$CLASSPATH" done #make it work in the binary package -#(use array for LIBPATH to account for spaces within wildcard expansion) +#(use array for LIB_PATH to account for spaces within wildcard expansion) if [ -e "${MPUSH_PREFIX}"/share/mpush/mpush-*.jar ]; then - LIBPATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) + LIB_PATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) else #release tarball format - for i in "$MPBINDIR"/../mpush-*.jar + for i in "$MP_BIN_DIR"/../mpush-*.jar do CLASSPATH="$i:$CLASSPATH" done - LIBPATH=("${MPBINDIR}"/../lib/*.jar) + LIB_PATH=("${MP_BIN_DIR}"/../lib/*.jar) fi -for i in "${LIBPATH[@]}" +for i in "${LIB_PATH[@]}" do CLASSPATH="$i:$CLASSPATH" done #make it work for developers -for d in "$MPBINDIR"/../build/lib/*.jar +for d in "$MP_BIN_DIR"/../build/lib/*.jar do CLASSPATH="$d:$CLASSPATH" done #make it work for developers -CLASSPATH="$MPBINDIR/../build/classes:$CLASSPATH" +CLASSPATH="$MP_BIN_DIR/../build/classes:$CLASSPATH" case "`uname`" in diff --git a/bin/mp.cmd b/bin/mp.cmd index f578f4e0..5a1b8448 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -15,7 +15,7 @@ REM See the License for the specific language governing permissions and REM limitations under the License. setlocal -call "%~dp0mpEnv.cmd" +call "%~dp0env-mp.cmd" set MPMAIN="-jar ../boot.jar" echo on diff --git a/bin/mp.sh b/bin/mp.sh index c40e2d3d..abd8d845 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -24,14 +24,14 @@ # use POSTIX interface, symlink is followed automatically -MPBIN="${BASH_SOURCE-$0}" -MPBIN="$(dirname "${MPBIN}")" -MPBINDIR="$(cd "${MPBIN}"; pwd)" +MP_BIN="${BASH_SOURCE-$0}" +MP_BIN="$(dirname "${MP_BIN}")" +MP_BIN_DIR="$(cd "${MP_BIN}"; pwd)" -if [ -e "$MPBIN/../libexec/mp-env.sh" ]; then - . "$MPBINDIR/../libexec/mp-env.sh" +if [ -e "$MP_BIN/../libexec/env-mp.sh" ]; then + . "$MP_BIN_DIR/../libexec/env-mp.sh" else - . "$MPBINDIR/mp-env.sh" + . "$MP_BIN_DIR/env-mp.sh" fi # See the following page for extensive details on setting @@ -51,7 +51,7 @@ then # for some reason these two options are necessary on jdk6 on Ubuntu # accord to the docs they are not necessary, but otw jconsole cannot # do a local attach - MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" + MP_MAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" else if [ "x$JMXAUTH" = "x" ] then @@ -69,41 +69,41 @@ then echo "MPush remote JMX authenticate set to $JMXAUTH" >&2 echo "MPush remote JMX ssl set to $JMXSSL" >&2 echo "MPush remote JMX log4j set to $JMXLOG4J" >&2 - MPMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" + MP_MAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dmpush.jmx.log4j.disable=$JMXLOG4J" fi else echo "JMX disabled by user request" >&2 - MPMAIN="" + MP_MAIN="" fi -MPMAIN="$MPMAIN -jar $MPBINDIR/bootstrap.jar" +MP_MAIN="$MP_MAIN -jar $MP_BIN_DIR/bootstrap.jar" -if [ "x$SERVER_JVMFLAGS" != "x" ] +if [ "x$SERVER_JVM_FLAGS" != "x" ] then - JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" + JVM_FLAGS="$SERVER_JVM_FLAGS $JVM_FLAGS" fi if [ "x$2" != "x" ] then - MPCFG="$MPCFGDIR/$2" + MP_CFG="$MP_CFG_DIR/$2" fi -# if we give a more complicated path to the config, don't screw around in $MPCFGDIR -if [ "x$(dirname "$MPCFG")" != "x$MPCFGDIR" ] +# if we give a more complicated path to the config, don't screw around in $MP_CFG_DIR +if [ "x$(dirname "$MP_CFG")" != "x$MP_CFG_DIR" ] then - MPCFG="$2" + MP_CFG="$2" fi if $cygwin then - MPCFG=`cygpath -wp "$MPCFG"` + MP_CFG=`cygpath -wp "$MP_CFG"` # cygwin has a "kill" in the shell itself, gets confused KILL=/bin/kill else KILL=kill fi -echo "Using config: $MPCFG" >&2 +echo "Using config: $MP_CFG" >&2 case "$OSTYPE" in *solaris*) @@ -113,15 +113,15 @@ case "$OSTYPE" in GREP=grep ;; esac -if [ -z "$MPPIDFILE" ]; then -# MP_DATADIR="$($GREP "^[[:space:]]*dataDir" "$MPCFG" | sed -e 's/.*=//')" - if [ ! -d "$MP_DATADIR" ]; then - mkdir -p "$MP_DATADIR" +if [ -z "$MP_PID_FILE" ]; then +# MP_DATA_DIR="$($GREP "^[[:space:]]*dataDir" "$MP_CFG" | sed -e 's/.*=//')" + if [ ! -d "$MP_DATA_DIR" ]; then + mkdir -p "$MP_DATA_DIR" fi - MPPIDFILE="$MP_DATADIR/mpush_server.pid" + MP_PID_FILE="$MP_DATA_DIR/mpush_server.pid" else # ensure it exists, otw stop will fail - mkdir -p "$(dirname "$MPPIDFILE")" + mkdir -p "$(dirname "$MP_PID_FILE")" fi if [ ! -w "$MP_LOG_DIR" ] ; then @@ -134,22 +134,22 @@ _MP_DAEMON_OUT="$MP_LOG_DIR/mpush.out" case $1 in start) echo -n "Starting mpush ... " - if [ -f "$MPPIDFILE" ]; then - if kill -0 `cat "$MPPIDFILE"` > /dev/null 2>&1; then - echo $command already running as process `cat "$MPPIDFILE"`. + if [ -f "$MP_PID_FILE" ]; then + if kill -0 `cat "$MP_PID_FILE"` > /dev/null 2>&1; then + echo $command already running as process `cat "$MP_PID_FILE"`. exit 0 fi fi - nohup "$JAVA" "-Dmp.conf=$MPCFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS $MPMAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & + nohup "$JAVA" "-Dmp.conf=$MP_CFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & if [ $? -eq 0 ] then case "$OSTYPE" in *solaris*) - /bin/echo "${!}\\c" > "$MPPIDFILE" + /bin/echo "${!}\\c" > "$MP_PID_FILE" ;; *) - /bin/echo -n $! > "$MPPIDFILE" + /bin/echo -n $! > "$MP_PID_FILE" ;; esac if [ $? -eq 0 ]; @@ -171,24 +171,53 @@ start-foreground) MP_CMD=("$JAVA") fi "${MP_CMD[@]}" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS $MPMAIN "-Dmp.conf=$MPCFG" + -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN "-Dmp.conf=$MP_CFG" ;; print-cmd) - echo "\"$JAVA\" $MPMAIN " - echo "\"-Dmp.conf=$MPCFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " - echo "$JVMFLAGS " + echo "\"$JAVA\" $MP_MAIN " + echo "\"-Dmp.conf=$MP_CFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " + echo "$JVM_FLAGS " echo "-cp \"$CLASSPATH\" " echo "> \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" ;; stop) - echo -n "Stopping mpush ... " - if [ ! -f "$MPPIDFILE" ] + echo "Stopping mpush ... " + if [ ! -f "$MP_PID_FILE" ] then - echo "no mpush to stop (could not find file $MPPIDFILE)" + echo "no mpush to stop (could not find file $MP_PID_FILE)" else - $KILL -9 $(cat "$MPPIDFILE") - rm "$MPPIDFILE" - echo STOPPED + $KILL -15 $(cat "$MP_PID_FILE") + SLEEP=30 + SLEEP_COUNT=1 + while [ $SLEEP -ge 0 ]; do + kill -0 $(cat "$MP_PID_FILE") >/dev/null 2>&1 + if [ $? -gt 0 ]; then + rm -f "$MP_PID_FILE" >/dev/null 2>&1 + if [ $? != 0 ]; then + if [ -w "$MP_PID_FILE" ]; then + cat /dev/null > "$MP_PID_FILE" + else + echo "The PID file could not be removed or cleared." + fi + fi + echo STOPPED + break + fi + if [ $SLEEP -gt 0 ]; then + echo "waiting ... $SLEEP_COUNT" + sleep 1 + fi + if [ $SLEEP -eq 0 ]; then + echo "MPUSH did not stop in time." + echo "To aid diagnostics a thread dump has been written to standard out." + kill -3 `cat "$MP_PID_FILE"` + echo "force stop MPUSH." + kill -9 `cat "$MP_PID_FILE"` + echo STOPPED + fi + SLEEP=`expr $SLEEP - 1` + SLEEP_COUNT=`expr $SLEEP_COUNT + 1` + done fi exit 0 ;; @@ -196,7 +225,7 @@ upgrade) shift echo "upgrading the servers to 3.*" "$JAVA" "-Dmpush.log.dir=${MP_LOG_DIR}" "-Dmpush.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS com.mpush.tools.upgrade.UpgradeMain ${@} + -cp "$CLASSPATH" $JVM_FLAGS com.mpush.tools.upgrade.UpgradeMain ${@} echo "Upgrading ... " ;; restart) @@ -207,14 +236,14 @@ restart) ;; status) # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output - clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$MP_CFG" | sed -e 's/.*=//'` if ! [ $clientPortAddress ] then - clientPortAddress="localhost" + clientPortAddress="localhost" fi - clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$MPCFG" | sed -e 's/.*=//'` + clientPort=`$GREP "^[[:space:]]*connect-server-port[^[:alpha:]]" "$MP_CFG" | sed -e 's/.*=//'` STAT=`"$JAVA" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVMFLAGS org.apache.mpush.client.FourLetterWordMain \ + -cp "$CLASSPATH" $JVM_FLAGS org.apache.mpush.client.FourLetterWordMain \ $clientPortAddress $clientPort srvr 2> /dev/null \ | $GREP Mode` if [ "x$STAT" = "x" ] @@ -229,4 +258,4 @@ status) *) echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2 -esac +esac \ No newline at end of file diff --git a/bin/set-env.sh b/bin/set-env.sh index 5325b967..9f7bf9b1 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -1,2 +1,2 @@ #!/usr/bin/env bash -#JVMFLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file +#JVM_FLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 3b34ba87..bfa5d493 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,7 +20,6 @@ package com.mpush.bootstrap; -import com.mpush.api.service.BaseService; import com.mpush.api.service.Service; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; @@ -33,25 +32,24 @@ import java.util.concurrent.TimeUnit; +import static com.mpush.tools.config.CC.mp.net.admin_server_port; + /** * Created by yxx on 2016/5/14. * * @author ohun@live.cn */ -public class ServerLauncher { - private final ZKServerNode csNode = ZKServerNode.csNode(); - - private final ZKServerNode gsNode = ZKServerNode.gsNode(); - - private final ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); +public final class ServerLauncher { - private final GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); + private final BootChain chain = BootChain.chain(); - private final AdminServer adminServer = new AdminServer(CC.mp.net.admin_server_port, connectServer, gatewayServer); + public ServerLauncher() { + ZKServerNode csNode = ZKServerNode.csNode(); + ZKServerNode gsNode = ZKServerNode.gsNode(); + ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); + GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); + AdminServer adminServer = new AdminServer(admin_server_port, connectServer, gatewayServer); - - public void start() { - BootChain chain = BootChain.chain(); chain.boot() .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 .setNext(new RedisBoot())//2.注册redis sever 到ZK @@ -61,20 +59,13 @@ public void start() { .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns .setNext(new MonitorBoot())//7.启动监控 .setNext(new LastBoot());//8.启动结束 - chain.run(); } - public void stop() throws Exception { - stopService(connectServer); - stopService(gatewayServer); - stopService(adminServer); - stopService(ZKClient.I); - stopService(MonitorService.I); + public void start() { + chain.start(); } - private void stopService(Service service) throws Exception { - if (service != null) { - service.stop().get(1, TimeUnit.MINUTES); - } + public void stop() { + chain.stop(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index 32544f85..523d59ad 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -26,11 +26,15 @@ * * @author ohun@live.cn */ -public class BootChain { - private BootJob first = first(); +public final class BootChain { + private final BootJob first = first(); - public void run() { - first.run(); + public void start() { + first.start(); + } + + public void stop() { + first.stop(); } public static BootChain chain() { @@ -40,9 +44,15 @@ public static BootChain chain() { private BootJob first() { return new BootJob() { @Override - public void run() { - Logs.Console.error("begin run bootstrap chain..."); - next(); + public void start() { + Logs.Console.error("begin start bootstrap chain..."); + startNext(); + } + + @Override + protected void stop() { + Logs.Console.error("begin stop bootstrap chain..."); + stopNext(); } }; } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java index 32bb5b36..3e332429 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -29,12 +29,21 @@ public abstract class BootJob { private BootJob next; - abstract void run(); + protected abstract void start(); - public void next() { + protected abstract void stop(); + + public void startNext() { + if (next != null) { + Logs.Console.error("start next bootstrap job [" + next.getClass().getSimpleName() + "]"); + next.start(); + } + } + + public void stopNext() { if (next != null) { - Logs.Console.error("run next bootstrap job [" + next.getClass().getSimpleName() + "]"); - next.run(); + Logs.Console.error("stop next bootstrap job [" + next.getClass().getSimpleName() + "]"); + next.stop(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index c1a01431..eec56ad6 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -21,20 +21,30 @@ import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.net.DnsMappingManager; -import com.mpush.common.net.HttpProxyDnsMappingManager; import com.mpush.tools.config.CC; +import static com.mpush.tools.config.CC.mp.spi.dns_mapping_manager; + /** * Created by yxx on 2016/5/15. * * @author ohun@live.cn */ -public class HttpProxyBoot extends BootJob { +public final class HttpProxyBoot extends BootJob { + + @Override + protected void start() { + if (CC.mp.http.proxy_enabled) { + SpiLoader.load(DnsMappingManager.class, dns_mapping_manager).start(); + } + startNext(); + } + @Override - void run() { + protected void stop() { if (CC.mp.http.proxy_enabled) { - SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager).start(); + SpiLoader.load(DnsMappingManager.class, dns_mapping_manager).stop(); } - next(); + stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index bc194877..48ebad40 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -27,13 +27,21 @@ * * @author ohun@live.cn */ -public class LastBoot extends BootJob { +public final class LastBoot extends BootJob { @Override - public void run() { + protected void start() { UserManager.I.clearUserOnlineData(); - Logs.Console.error("end run bootstrap chain..."); + Logs.Console.error("end start bootstrap chain..."); Logs.Console.error("==================================================================="); Logs.Console.error("====================MPUSH SERVER START SUCCESS====================="); Logs.Console.error("==================================================================="); } + + @Override + protected void stop() { + Logs.Console.error("end stop bootstrap chain..."); + Logs.Console.error("==================================================================="); + Logs.Console.error("====================MPUSH SERVER STOPPED SUCCESS====================="); + Logs.Console.error("==================================================================="); + } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java index d0b59ae7..ea2aa1a3 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/MonitorBoot.java @@ -26,10 +26,16 @@ * * @author ohun@live.cn */ -public class MonitorBoot extends BootJob { +public final class MonitorBoot extends BootJob { @Override - void run() { + protected void start() { MonitorService.I.start(); - next(); + startNext(); + } + + @Override + protected void stop() { + MonitorService.I.stop(); + stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index e98ca3ee..687d70c5 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -26,11 +26,17 @@ * * @author ohun@live.cn */ -public class RedisBoot extends BootJob { +public final class RedisBoot extends BootJob { @Override - public void run() { + protected void start() { RedisManager.I.init(); - next(); + startNext(); + } + + @Override + protected void stop() { + RedisManager.I.close(); + stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 4ae1e89f..de470d6b 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -21,18 +21,26 @@ import com.mpush.api.service.Listener; import com.mpush.api.service.Server; +import com.mpush.core.server.AdminServer; +import com.mpush.core.server.ConnectionServer; +import com.mpush.core.server.GatewayServer; import com.mpush.tools.Jsons; +import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import com.mpush.tools.thread.pool.ThreadPoolManager; import com.mpush.zk.ZKClient; import com.mpush.zk.node.ZKServerNode; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + /** * Created by yxx on 2016/5/14. * * @author ohun@live.cn */ -public class ServerBoot extends BootJob { +public final class ServerBoot extends BootJob { private final Server server; private final ZKServerNode node; @@ -42,8 +50,8 @@ public ServerBoot(Server server, ZKServerNode node) { } @Override - public void run() { - final String serverName = server.getClass().getSimpleName(); + public void start() { + String serverName = server.getClass().getSimpleName(); ThreadPoolManager.I.newThread(serverName, () -> { server.init(); server.start(new Listener() { @@ -53,7 +61,7 @@ public void onSuccess(Object... args) { if (node != null) { registerServerToZk(node.getZkPath(), Jsons.toJson(node)); } - next(); + startNext(); } @Override @@ -65,8 +73,17 @@ public void onFailure(Throwable cause) { }).start(); } - //step7 注册应用到zk - public void registerServerToZk(String path, String value) { + @Override + protected void stop() { + try { + server.stop().get(1, TimeUnit.MINUTES); + } catch (Exception e) { + } + stopNext(); + } + + //注册应用到zk + private void registerServerToZk(String path, String value) { ZKClient.I.registerEphemeralSequential(path, value); Logs.Console.error("register server node=" + value + " to zk name=" + path); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java index e8c33d1d..861b3f64 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -28,14 +28,14 @@ * * @author ohun@live.cn */ -public class ZKBoot extends BootJob { +public final class ZKBoot extends BootJob { @Override - public void run() { + protected void start() { ZKClient.I.start(new Listener() { @Override public void onSuccess(Object... args) { - next(); + startNext(); } @Override @@ -44,4 +44,10 @@ public void onFailure(Throwable cause) { } }); } + + @Override + protected void stop() { + ZKClient.I.stop(); + stopNext(); + } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index 2ff4c2cf..e9ded643 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -25,12 +25,13 @@ import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import redis.clients.jedis.*; +import redis.clients.util.Pool; import java.util.*; import static redis.clients.jedis.Protocol.DEFAULT_TIMEOUT; -public class RedisClient { +public final class RedisClient { public static final JedisPoolConfig CONFIG = buildConfig(); private static final Map POOL_MAP = Maps.newConcurrentMap(); @@ -763,4 +764,7 @@ private static List toList(Collection value, Class clazz) { return null; } + public static void destroy() { + POOL_MAP.values().forEach(Pool::close); + } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 50c10e21..a145863a 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -34,7 +34,7 @@ /** * redis 对外封装接口 */ -public class RedisManager { +public final class RedisManager { public static final RedisManager I = new RedisManager(); private final RedisClusterManager clusterManager = ZKRedisClusterManager.I; @@ -315,4 +315,8 @@ public void test(List groupList) { } } } + + public void close() { + RedisClient.destroy(); + } } From 612fe108aa2fdf1fa6eb6198eae875364b6c9517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=80=8D=E6=97=AD?= Date: Mon, 22 Aug 2016 12:11:28 +0800 Subject: [PATCH 579/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8,?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E6=B5=81=E7=A8=8B=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/mpush.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index ff238f12..4f5b2ba0 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,5 +1,6 @@ mp.log.level=${log.level} min-heartbeat=${min.hb} +mp.net.connect-server-port=3000 mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" mp.zk.server-address="127.0.0.1:2181" From 8db7fdb0f3c25ced6710a6af924f2ea6c218dfa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 22 Aug 2016 12:11:28 +0800 Subject: [PATCH 580/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8,?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E6=B5=81=E7=A8=8B=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/mpush.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index ff238f12..4f5b2ba0 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,5 +1,6 @@ mp.log.level=${log.level} min-heartbeat=${min.hb} +mp.net.connect-server-port=3000 mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" mp.zk.server-address="127.0.0.1:2181" From bb6c1fe884205b6391842158df1e89c252d50947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 22 Aug 2016 14:55:05 +0800 Subject: [PATCH 581/890] =?UTF-8?q?Netty=20=E9=99=8D=E7=BA=A7=E5=88=B04.x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/client/connect/ConnClientChannelHandler.java | 2 +- .../com/mpush/client/gateway/GatewayClientChannelHandler.java | 3 ++- .../src/main/java/com/mpush/core/handler/AdminHandler.java | 2 +- .../main/java/com/mpush/core/handler/HttpProxyHandler.java | 2 +- .../main/java/com/mpush/core/server/ServerChannelHandler.java | 4 ++-- .../src/main/java/com/mpush/netty/http/HttpClientHandler.java | 4 ++-- .../src/main/java/com/mpush/netty/http/RequestContext.java | 3 ++- pom.xml | 2 +- 8 files changed, 12 insertions(+), 10 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 93994eba..def53d76 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -52,7 +52,7 @@ * @author ohun@live.cn */ @ChannelHandler.Sharable -public final class ConnClientChannelHandler extends ChannelHandlerAdapter { +public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.T_NETTY_TIMER)); diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java index cfb9bcd9..4abe6790 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -31,6 +31,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +43,7 @@ * @author ohun@live.cn */ @ChannelHandler.Sharable -public final class GatewayClientChannelHandler extends ChannelHandlerAdapter { +public final class GatewayClientChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index bdc19126..af697163 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -62,7 +62,7 @@ public AdminHandler(AdminServer adminServer) { } @Override - protected void messageReceived(ChannelHandlerContext ctx, String request) throws Exception { + protected void channelRead0(ChannelHandlerContext ctx, String request) throws Exception { Command command = Command.help; String arg = null; String[] args = null; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index efc014e2..a1ad9985 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -119,7 +119,7 @@ public void onResponse(HttpResponse httpResponse) { .from(request) .setStatusCode(httpResponse.status().code()) .setReasonPhrase(httpResponse.status().reasonPhrase().toString()); - for (Map.Entry entry : httpResponse.headers()) { + for (Map.Entry entry : httpResponse.headers()) { response.addHeader(entry.getKey().toString(), entry.getValue().toString()); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 9aa7c078..800dab32 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -30,8 +30,8 @@ import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,7 +41,7 @@ * @author ohun@live.cn */ @ChannelHandler.Sharable -public final class ServerChannelHandler extends ChannelHandlerAdapter { +public final class ServerChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java index 26f6a31e..5f9efa50 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java @@ -1,8 +1,8 @@ package com.mpush.netty.http; import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.*; import io.netty.util.IllegalReferenceCountException; import io.netty.util.ReferenceCountUtil; @@ -13,7 +13,7 @@ import java.net.URLDecoder; @ChannelHandler.Sharable -public class HttpClientHandler extends ChannelHandlerAdapter { +public class HttpClientHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); private final NettyHttpClient client; diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java index 915a09b3..08bcbbd3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java @@ -48,8 +48,9 @@ public RequestContext(FullHttpRequest request, HttpCallback callback) { } private int parseTimeout() { - String timeout = request.headers().getAndRemoveAndConvert(Constants.HTTP_HEAD_READ_TIMEOUT); + String timeout = request.headers().get(Constants.HTTP_HEAD_READ_TIMEOUT); if (timeout != null) { + request.headers().remove(Constants.HTTP_HEAD_READ_TIMEOUT); Integer integer = Ints.tryParse(timeout); if (integer != null && integer > 0) { return integer; diff --git a/pom.xml b/pom.xml index 8d4074e8..a79e5bd4 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ ${mpush.version} ${mpush.version} ${mpush.version} - 5.0.0.Alpha2 + 4.1.4.Final linux-x86_64 From 9ed7cd073d61bd409ad6b77cd1810ac4b94a91c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 22 Aug 2016 14:58:18 +0800 Subject: [PATCH 582/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/mpush.conf | 2 +- mpush-test/src/test/resources/application.conf | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 4f5b2ba0..b01e4300 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,5 +1,5 @@ mp.log.level=${log.level} -min-heartbeat=${min.hb} +mp.min-heartbeat=${min.hb} mp.net.connect-server-port=3000 mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 274eac51..1f651b11 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -1,11 +1,13 @@ mp.log.dir=${user.dir}/mpush-test/target/logs mp.log.level=debug -min-heartbeat=10s +mp.min-heartbeat=10s +mp.max-heartbeat=10s mp.zk.namespace=mpush mp.zk.server-address="111.1.57.148:5666" mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} mp.core.compress-threshold=10k mp.redis { + write-to-zk=true #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 cluster-group:[ [ From 46bdac56d460ced05e04785448f783ccd0f378e6 Mon Sep 17 00:00:00 2001 From: ohun Date: Mon, 22 Aug 2016 17:33:36 +0800 Subject: [PATCH 583/890] Update README.md --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f5a812c9..c63da430 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,60 @@ -# mpush -mobile push +# 服务部署 + +###### 说明:mpush 服务只依赖于zookeeper和redis,当然还有JDK>=1.8 + +1. 安装```jdk 1.8``` 以上版本并设置```%JAVA_HOME%``` + +2. 安装```zookeeper``` (安装配置步骤略) + +3. 安装```Redis``` (安装配置步骤略) + +4. 下载mpush server正式包[https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz](https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz) + +5. 解压下载的tar包```tar -zvxf mpush-release-0.0.1.tar.gz``` 到 mpush 目录, 结构如下 + + ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:30 bin```启动脚本 + + ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 conf```配置文件 + + ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:29 lib``` 核心类库 + + ```-rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE``` + + ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:32 logs``` 日志目录 + + ```-rw-rw-r-- 1 shinemo shinemo 21 May 31 11:07 README.md``` + + ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 tmp``` + +6. 修改 conf 目录下的 ```vi mpush.conf```文件, ```mpush.conf```里的配置项会覆盖同目录下的```reference.conf```文件 + ```java + #主要修改以下配置 + mp.net.connect-server-port=3000//长链接服务对外端口, 公网端口 + mp.zk.server-address="127.0.0.1:2181"//zk 机器的地址 + mp.redis={//redis 相关配置 + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:6379 + password:"your redis password" + } + ] + ] + } + //还有用于安全加密的RSA mp.security.private-key 和 mp.security.public-key 等... + ``` + 如果要修改其他配置请参照reference.conf文件 + +7. 给bin目录下的脚本增加执行权限```chmod u+x *.sh``` + +8. 执行``` ./mp.sh start``` 启动服务, 查看帮助```./mp.sh``` 目前支持的命令: + + ```Usage: ./mp.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}``` + + ```set-env.sh```用于增加和修改jvm启动参数,比如堆内存、开启远程调试端口、开启jmx等 + +9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 + +10. 未完待续... From 16efb82c21afeb47f90f5713c909e45c7b2f0bdb Mon Sep 17 00:00:00 2001 From: ohun Date: Mon, 22 Aug 2016 17:38:15 +0800 Subject: [PATCH 584/890] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c63da430..fe6d2ad2 100644 --- a/README.md +++ b/README.md @@ -49,11 +49,11 @@ 7. 给bin目录下的脚本增加执行权限```chmod u+x *.sh``` -8. 执行``` ./mp.sh start``` 启动服务, 查看帮助```./mp.sh``` 目前支持的命令: +8. 执行```./mp.sh start``` 启动服务, 查看帮助```./mp.sh``` 目前支持的命令: ```Usage: ./mp.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}``` - ```set-env.sh```用于增加和修改jvm启动参数,比如堆内存、开启远程调试端口、开启jmx等 + ```set-env.sh``` 用于增加和修改jvm启动参数,比如堆内存、开启远程调试端口、开启jmx等 9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 From 89a67c2c43a2f4406e5b1e4130c8fa6f7c730953 Mon Sep 17 00:00:00 2001 From: ohun Date: Mon, 22 Aug 2016 17:53:41 +0800 Subject: [PATCH 585/890] Update README.md --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fe6d2ad2..73c20469 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,17 @@ -# 服务部署 +## [详细教程](https://mpusher.github.io/docs) + +[https://mpusher.github.io/docs](https://mpusher.github.io/docs) + +## 源码测试 +1. ```git clone https://github.com/mpusher/mpush.git``` +2. 导入到eclipse或Intellij IDEA +3. 打开```mpush-test```目录所有到测试代码都在该模块下 +4. 修改配置文件```src/test/resource/application.conf```文件修改方式参照 服务部署第6点 +5. 运行```com.mpush.test.sever.ServerTestMain.java```启动长链接服务 +6. 运行```com.mpush.test.client.ConnClientTestMain.java``` 模拟一个客户端 +7. 运行```com.mpush.test.push.PushClientTestMain``` 模拟给用户下发消息 + +## 服务部署 ###### 说明:mpush 服务只依赖于zookeeper和redis,当然还有JDK>=1.8 From 3656a74819a411def75debebe2590f307fb9f112 Mon Sep 17 00:00:00 2001 From: ohun Date: Mon, 22 Aug 2016 18:16:06 +0800 Subject: [PATCH 586/890] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 73c20469..d58b3a25 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ 5. 运行```com.mpush.test.sever.ServerTestMain.java```启动长链接服务 6. 运行```com.mpush.test.client.ConnClientTestMain.java``` 模拟一个客户端 7. 运行```com.mpush.test.push.PushClientTestMain``` 模拟给用户下发消息 +8. 可以在控制台观察日志看服务是否正常运行,消息是否下发成功 ## 服务部署 From 115cfecfd42517042a37867353cd6f51ecd287c7 Mon Sep 17 00:00:00 2001 From: ohun Date: Mon, 22 Aug 2016 18:18:09 +0800 Subject: [PATCH 587/890] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d58b3a25..f4124660 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## 源码测试 1. ```git clone https://github.com/mpusher/mpush.git``` 2. 导入到eclipse或Intellij IDEA -3. 打开```mpush-test```目录所有到测试代码都在该模块下 +3. 打开```mpush-test```模块,所有的测试代码都在该模块下 4. 修改配置文件```src/test/resource/application.conf```文件修改方式参照 服务部署第6点 5. 运行```com.mpush.test.sever.ServerTestMain.java```启动长链接服务 6. 运行```com.mpush.test.client.ConnClientTestMain.java``` 模拟一个客户端 From 38014b0f469d8fe7eb0dd2fc58606b5a29f07178 Mon Sep 17 00:00:00 2001 From: ohun Date: Tue, 23 Aug 2016 15:43:08 +0800 Subject: [PATCH 588/890] Update README.md --- README.md | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f4124660..db5006df 100644 --- a/README.md +++ b/README.md @@ -24,21 +24,14 @@ 4. 下载mpush server正式包[https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz](https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz) -5. 解压下载的tar包```tar -zvxf mpush-release-0.0.1.tar.gz``` 到 mpush 目录, 结构如下 - - ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:30 bin```启动脚本 - - ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 conf```配置文件 - - ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:29 lib``` 核心类库 - - ```-rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE``` - - ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:32 logs``` 日志目录 - - ```-rw-rw-r-- 1 shinemo shinemo 21 May 31 11:07 README.md``` - - ```drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 tmp``` +5. 解压下载的tar包`tar -zvxf mpush-release-0.0.1.tar.gz`到 mpush 目录, 结构如下 + > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:30 bin **启动脚本** + > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 conf **配置文件** + > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:29 lib **核心类库** + > * -rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE + > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:32 logs **日志目录** + > * -rw-rw-r-- 1 shinemo shinemo 21 May 31 11:07 README.md + > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 tmp 6. 修改 conf 目录下的 ```vi mpush.conf```文件, ```mpush.conf```里的配置项会覆盖同目录下的```reference.conf```文件 ```java From 7815f9a924fb9359aa69f55e57f5844e039dd31e Mon Sep 17 00:00:00 2001 From: ohun Date: Tue, 23 Aug 2016 17:52:45 +0800 Subject: [PATCH 589/890] Update README.md --- README.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index db5006df..9fabdce4 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,16 @@ 4. 下载mpush server正式包[https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz](https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz) 5. 解压下载的tar包`tar -zvxf mpush-release-0.0.1.tar.gz`到 mpush 目录, 结构如下 - > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:30 bin **启动脚本** - > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 conf **配置文件** - > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:29 lib **核心类库** - > * -rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE - > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:32 logs **日志目录** - > * -rw-rw-r-- 1 shinemo shinemo 21 May 31 11:07 README.md - > * drwxrwxr-x 2 shinemo shinemo 4096 Aug 20 09:52 tmp + + >

+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:30 bin —> 启动脚本
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:52 conf —> 配置文件
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:29 lib —> 核心类库
+   >-rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:32 logs —> 日志目录
+   >-rw-rw-r-- 1 shinemo shinemo    21 May 31 11:07 README.md
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:52 tmp
+   >
6. 修改 conf 目录下的 ```vi mpush.conf```文件, ```mpush.conf```里的配置项会覆盖同目录下的```reference.conf```文件 ```java From c7eb8da91a27716927c4950d6a805f60a3a19210 Mon Sep 17 00:00:00 2001 From: ohun Date: Thu, 25 Aug 2016 09:49:50 +0800 Subject: [PATCH 590/890] Update README.md --- README.md | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 204 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9fabdce4..d0515b0f 100644 --- a/README.md +++ b/README.md @@ -67,4 +67,207 @@ 9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 -10. 未完待续... +10. 配置文件详解 + ```java + ################################################################################################################## +# +# NOTICE: +# +# 系统配置文件,所有列出的项是系统所支持全部配置项 +# 如果要覆盖某项的值可以添加到mpush.conf中。 +# +# 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 +# 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 +# +############################################################################################################## + +mp { + #日志配置 + log.level=warn + log.dir=${user.dir}/../logs + + #核心配置 + core { + max-packet-size=10k //系统允许传输的最大包的大小 + compress-threshold=10k //数据包启用压缩的临界值,超过该值后对数据进行压缩 + min-heartbeat=3m //最小心跳间隔 + max-heartbeat=3m //最大心跳间隔 + max-hb-timeout-times=2 //允许的心跳连续超时的最大次数 + session-expired-time=1d //用于快速重连的session 过期时间默认1天 + epoll-provider=netty //nio:jdk自带,netty:由netty实现 + } + + #安全配置 + security { + #rsa 私钥, 公钥 key长度为1024;生成方式可以使用open-ssh或者使用工具类com.mpush.tools.crypto.RSAUtils#main + private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" + public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" + aes-key-length=16 //AES key 长度 + ras-key-length=1024 //RSA key 长度 + } + + #网络配置 + net { + connect-server-port=3000 //长链接服务对外端口, 公网端口 + gateway-server-port=3001 //网关服务端口, 内部端口 + admin-server-port=3002 //控制台服务端口, 内部端口 + public-host-mapping { //本机局域网IP和公网IP的映射关系 + "127.0.0.1":"111.1.32.137" + } + traffic-shaping { //流量整形配置 + gateway-client { + enabled:true + check-interval:100ms + write-global-limit:1k + read-global-limit:0 + write-channel-limit:256b + read-channel-limit:0 + } + + gateway-server { + enabled:true + check-interval:100ms + write-global-limit:0 + read-global-limit:10k + write-channel-limit:0 + read-channel-limit:0.5k + } + + connect-server { + enabled:false + check-interval:100ms + write-global-limit:0 + read-global-limit:100k + write-channel-limit:3k + read-channel-limit:3k + } + } + } + + #Zookeeper配置 + zk { + server-address="127.0.0.1:2181" + namespace=mpush + digest=mpush + local-cache-path=/ + retry { + #initial amount of time to wait between retries + baseSleepTimeMs=3s + #max number of times to retry + maxRetries=3 + #max time in ms to sleep on each retry + maxSleepMs=5s + } + connectionTimeoutMs=5s + sessionTimeoutMs=5s + } + + #Redis集群配置 + redis { + write-to-zk=true + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:2181 + password:ShineMoIpo + } + ] + ] + config { + maxTotal:8, + maxIdle:4, + minIdle:1, + lifo:true, + fairness:false, + maxWaitMillis:5000, + minEvictableIdleTimeMillis:300000, + softMinEvictableIdleTimeMillis:1800000, + numTestsPerEvictionRun:3, + testOnCreate:false, + testOnBorrow:false, + testOnReturn:false, + testWhileIdle:false, + timeBetweenEvictionRunsMillis:60000, + blockWhenExhausted:true, + jmxEnabled:true, + jmxNamePrefix:pool, + jmxNameBase:pool + } + } + + #HTTP代理配置 + http { + proxy-enabled=false //启用Http代理 + max-conn-per-host=5 //每个域名的最大链接数, 建议web服务nginx超时时间设长一点, 以便保持长链接 + default-read-timeout=10s //请求超时时间 + max-content-length=5m //response body 最大大小 + dns-mapping { //域名映射外网地址转内部IP + "mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] + } + } + + #线程池配置 + thread { + pool { + boss { //netty boss + min:4 + max:16 + queue-size:1000 + } + + work { //netty boss + min:8 + max:32 + queue-size:1000 + } + + event-bus { + min:4 + max:4 + queue-size:10000 //大量的online,offline, + } + + http-proxy { + min:8 + max:64 + queue-size:1000 + } + + biz { //其他业务 + min:4 + max:64 + queue-size:10 + } + + mq { //用户上下线消息, 踢人等 + min:2 + max:4 + queue-size:10000 + } + + push-callback { //消息推送 + min:2 + max:2 + queue-size:0 + } + } + } + + #系统监控配置 + monitor { + dump-dir=/tmp/logs/mpush/ + dump-stack=false //是否定时dump堆栈 + dump-period=1m //多久监控一次 + print-log=true //是否打印监控日志 + } + + #SPI扩展配置 + spi { + thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" + dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" + } +} +``` +11. 未完待续... From 925605d708310cf763a4b9249a0181bb74a7a079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 26 Aug 2016 10:33:26 +0800 Subject: [PATCH 591/890] add deploy to maven central --- mpush-api/pom.xml | 11 +- mpush-boot/pom.xml | 105 +- mpush-cache/pom.xml | 10 +- mpush-client/pom.xml | 12 +- mpush-common/pom.xml | 7 +- mpush-core/pom.xml | 13 +- mpush-monitor/pom.xml | 5 +- mpush-netty/pom.xml | 9 +- mpush-test/pom.xml | 9 +- mpush-tools/pom.xml | 9 +- .../src/main/java/com/mpush/tools/Utils.java | 4 +- .../com/mpush/tools/common/GenericsUtil.java | 164 --- .../java/com/mpush/tools/common/Profiler.java | 4 - .../com/mpush/tools/config/ConfigManager.java | 4 +- .../java/com/mpush/tools/crypto/AESUtils.java | 10 +- .../java/com/mpush/tools/crypto/Base64.java | 970 ------------------ .../com/mpush/tools/crypto/Base64Utils.java | 14 +- .../java/com/mpush/tools/crypto/MD5Utils.java | 15 +- .../java/com/mpush/tools/crypto/RSAUtils.java | 73 +- mpush-zk/pom.xml | 11 +- pom.xml | 70 +- 21 files changed, 237 insertions(+), 1292 deletions(-) delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java delete mode 100644 mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 141fb582..10b1b0e9 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -2,18 +2,21 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-api ${mpush-api-version} - mpush-api jar + mpush-api + MPUSH消息推送系统Api模块 + https://github.com/mpusher/mpush diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index cb0b9a07..f08f5ef1 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -1,16 +1,20 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} + mpush-boot ${mpush-boot-version} - mpush-boot jar + mpush-boot + MPUSH消息推送系统启动模块 + https://github.com/mpusher/mpush @@ -41,48 +45,55 @@ true - - - maven-jar-plugin - - - - false - - - - true - - ../lib/ - - com.mpush.bootstrap.Main - - - - - - package - - - - - maven-assembly-plugin - 2.6 - - mpush - - assembly.xml - - - - - package - - single - - - - - + + + zip + + + + maven-jar-plugin + + + + false + + + + true + + ../lib/ + + com.mpush.bootstrap.Main + + + + + + package + + + + + maven-assembly-plugin + 2.6 + + mpush + + assembly.xml + + + + + package + + single + + + + + + + + diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml index 0f1b4dae..47b606d5 100644 --- a/mpush-cache/pom.xml +++ b/mpush-cache/pom.xml @@ -2,16 +2,20 @@ + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-cache ${mpush.version} + jar + mpush-cache + MPUSH消息推送系统缓存模块 + https://github.com/mpusher/mpush diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index c62a39b4..b3d06def 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -2,18 +2,22 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-client ${mpush.version} - mpush-client jar + mpush-client + MPUSH消息推送系统服务端SDK + https://github.com/mpusher/mpush + ${mpush.groupId} diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index d97f639b..e817ff02 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -4,16 +4,17 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> mpush - com.mpush + com.github.mpusher 1.0 4.0.0 - ${mpush.groupId} mpush-common ${mpush.version} - mpush-common jar + mpush-common + MPUSH消息推送系统基础模块 + https://github.com/mpusher/mpush diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 174efd1a..c291b7d5 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -2,17 +2,22 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} + mpush-core ${mpush-core-version} - mpush-core jar + mpush-core + MPUSH消息推送系统核心模块 + https://github.com/mpusher/mpush + ${mpush.groupId} diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 886b22fc..29f25721 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -2,16 +2,17 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> mpush - com.mpush + com.github.mpusher 1.0 4.0.0 - ${mpush.groupId} mpush-monitor ${mpush-monitor-version} jar mpush-monitor + MPUSH消息推送系统监控模块 + https://github.com/mpusher/mpush diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 4fb8bc19..f4ddf0e4 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -2,18 +2,21 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-netty ${mpush-netty-version} jar mpush-netty + MPUSH消息推送系统Netty模块 + https://github.com/mpusher/mpush diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 7a1312d7..ddf0a8f5 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -1,17 +1,20 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-test ${mpush-test-version} jar mpush-test + MPUSH消息推送系统测试模块 + https://github.com/mpusher/mpush diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index df0ec478..60bd7219 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -2,18 +2,21 @@ + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-tools ${mpush-tools-version} jar mpush-tools + MPUSH消息推送系统工具库 + https://github.com/mpusher/mpush + diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index fe7759d1..65c439a7 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -66,7 +66,7 @@ public static String getLocalIp() { * 获取本机ip * 只获取第一块网卡绑定的ip地址 * - * @return + * @return 本机ip */ public static String getInetAddress() { try { @@ -105,7 +105,7 @@ public static String getExtranetIp() { /** * 获取外网IP * - * @return + * @return 外网IP */ public static String getExtranetAddress() { try { diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java deleted file mode 100644 index f8db214d..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/common/GenericsUtil.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.tools.common; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - - -public class GenericsUtil { - - /** - * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * - * @param clazz clazz 需要反射的类,该类必须继承范型父类 - * @param index 泛型参数所在索引,从0开始. - * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz, int index) { - Type genType = clazz.getGenericSuperclass();//得到泛型父类 - //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class - - if (!(genType instanceof ParameterizedType)) { - return Object.class; - } - //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport就返回Buyer和Contact类型 - Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); - if (index >= params.length || index < 0) { - throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); - } - if (!(params[index] instanceof Class)) { - return Object.class; - } - return (Class) params[index]; - } - - /** - * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport - * - * @param clazz clazz 需要反射的类,该类必须继承泛型父类 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getSuperClassGenericType(Class clazz) { - return getSuperClassGenericType(clazz, 0); - } - - /** - * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map getNames(){} - * - * @param method 方法 - * @param index 泛型参数所在索引,从0开始. - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method, int index) { - Type returnType = method.getGenericReturnType(); - if (returnType instanceof ParameterizedType) { - ParameterizedType type = (ParameterizedType) returnType; - Type[] typeArguments = type.getActualTypeArguments(); - if (index >= typeArguments.length || index < 0) { - throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class) typeArguments[index]; - } - return Object.class; - } - - /** - * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map getNames(){} - * - * @param method 方法 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getMethodGenericReturnType(Method method) { - return getMethodGenericReturnType(method, 0); - } - - /** - * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 - * @param index 第几个输入参数 - * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method, int index) { - List> results = new ArrayList>(); - Type[] genericParameterTypes = method.getGenericParameterTypes(); - if (index >= genericParameterTypes.length || index < 0) { - throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); - } - Type genericParameterType = genericParameterTypes[index]; - if (genericParameterType instanceof ParameterizedType) { - ParameterizedType aType = (ParameterizedType) genericParameterType; - Type[] parameterArgTypes = aType.getActualTypeArguments(); - for (Type parameterArgType : parameterArgTypes) { - Class parameterArgClass = (Class) parameterArgType; - results.add(parameterArgClass); - } - return results; - } - return results; - } - - /** - * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map maps, List names){} - * - * @param method 方法 - * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 - */ - public static List> getMethodGenericParameterTypes(Method method) { - return getMethodGenericParameterTypes(method, 0); - } - - /** - * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @param index 泛型参数所在索引,从0开始. - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field, int index) { - Type genericFieldType = field.getGenericType(); - - if (genericFieldType instanceof ParameterizedType) { - ParameterizedType aType = (ParameterizedType) genericFieldType; - Type[] fieldArgTypes = aType.getActualTypeArguments(); - if (index >= fieldArgTypes.length || index < 0) { - throw new RuntimeException("你输入的索引" + (index < 0 ? "不能小于0" : "超出了参数的总数")); - } - return (Class) fieldArgTypes[index]; - } - return Object.class; - } - - /** - * 通过反射,获得Field泛型参数的实际类型. 如: public Map names; - * - * @param field 字段 - * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回Object.class - */ - public static Class getFieldGenericType(Field field) { - return getFieldGenericType(field, 0); - } - -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java index 0d8f29c2..57e2c9d6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java @@ -64,7 +64,6 @@ public static void start(Message message) { /** * 清除计时器。 - *

*

* 清除以后必须再次调用start方可重新计时。 *

@@ -217,9 +216,6 @@ private Entry(Object message, Entry parentEntry, Entry firstEntry) { : firstEntry.startTime; } - /** - * 取得entry的信息。 - */ public String getMessage() { String messageString = null; diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java index 1319282e..8e952d95 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigManager.java @@ -43,7 +43,7 @@ public int getHeartbeat(int min, int max) { /** * 获取内网IP地址 * - * @return + * @return 内网IP地址 */ public String getLocalIp() { return Utils.getLocalIp(); @@ -52,7 +52,7 @@ public String getLocalIp() { /** * 获取外网IP地址 * - * @return + * @return 外网IP地址 */ public String getPublicIp() { diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index 80050e8f..38c5d1e3 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -40,15 +40,7 @@ public final class AESUtils { public static final String KEY_ALGORITHM = "AES"; public static final String KEY_ALGORITHM_PADDING = "AES/CBC/PKCS5Padding"; - /** - *

- * 生成密钥 - *

- * - * @param seed 密钥种子 - * @return - * @throws Exception - */ + public static SecretKey getSecretKey(byte[] seed) throws Exception { SecureRandom secureRandom = new SecureRandom(seed); KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java deleted file mode 100644 index 370c72f4..00000000 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64.java +++ /dev/null @@ -1,970 +0,0 @@ -/* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. - * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - */ -package com.mpush.tools.crypto; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Objects; - -/** - * This class consists exclusively of static methods for obtaining - * encoders and decoders for the Base64 encoding scheme. The - * implementation of this class supports the following types of Base64 - * as specified in - * RFC 4648 and - * RFC 2045. - *

- *

    - *
  • Basic - *

    Uses "The Base64 Alphabet" as specified in Table 1 of - * RFC 4648 and RFC 2045 for encoding and decoding operation. - * The encoder does not add any line feed (line separator) - * character. The decoder rejects data that contains characters - * outside the base64 alphabet.

  • - *

    - *

  • URL and Filename safe - *

    Uses the "URL and Filename safe Base64 Alphabet" as specified - * in Table 2 of RFC 4648 for encoding and decoding. The - * encoder does not add any line feed (line separator) character. - * The decoder rejects data that contains characters outside the - * base64 alphabet.

  • - *

    - *

  • MIME - *

    Uses the "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045 for encoding and decoding operation. The encoded output - * must be represented in lines of no more than 76 characters each - * and uses a carriage return {@code '\r'} followed immediately by - * a linefeed {@code '\n'} as the line separator. No line separator - * is added to the end of the encoded output. All line separators - * or other characters not found in the base64 alphabet table are - * ignored in decoding operation.

  • - *
- *

- *

Unless otherwise noted, passing a {@code null} argument to a - * method of this class will cause a {@link java.lang.NullPointerException - * NullPointerException} to be thrown. - * - * @author Xueming Shen - * @since 1.8 - */ - -public class Base64 { - - private Base64() { - } - - /** - * Returns a {@link Encoder} that encodes using the - * Basic type base64 encoding scheme. - * - * @return A Base64 encoder. - */ - public static Encoder getEncoder() { - return Encoder.RFC4648; - } - - /** - * Returns a {@link Encoder} that encodes using the - * URL and Filename safe type base64 - * encoding scheme. - * - * @return A Base64 encoder. - */ - public static Encoder getUrlEncoder() { - return Encoder.RFC4648_URLSAFE; - } - - /** - * Returns a {@link Encoder} that encodes using the - * MIME type base64 encoding scheme. - * - * @return A Base64 encoder. - */ - public static Encoder getMimeEncoder() { - return Encoder.RFC2045; - } - - /** - * Returns a {@link Encoder} that encodes using the - * MIME type base64 encoding scheme - * with specified line length and line separators. - * - * @param lineLength the length of each output line (rounded down to nearest multiple - * of 4). If {@code lineLength <= 0} the output will not be separated - * in lines - * @param lineSeparator the line separator for each output line - * @return A Base64 encoder. - * @throws IllegalArgumentException if {@code lineSeparator} includes any - * character of "The Base64 Alphabet" as specified in Table 1 of - * RFC 2045. - */ - public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) { - Objects.requireNonNull(lineSeparator); - int[] base64 = Decoder.fromBase64; - for (byte b : lineSeparator) { - if (base64[b & 0xff] != -1) - throw new IllegalArgumentException( - "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); - } - if (lineLength <= 0) { - return Encoder.RFC4648; - } - return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); - } - - /** - * Returns a {@link Decoder} that decodes using the - * Basic type base64 encoding scheme. - * - * @return A Base64 decoder. - */ - public static Decoder getDecoder() { - return Decoder.RFC4648; - } - - /** - * Returns a {@link Decoder} that decodes using the - * URL and Filename safe type base64 - * encoding scheme. - * - * @return A Base64 decoder. - */ - public static Decoder getUrlDecoder() { - return Decoder.RFC4648_URLSAFE; - } - - /** - * Returns a {@link Decoder} that decodes using the - * MIME type base64 decoding scheme. - * - * @return A Base64 decoder. - */ - public static Decoder getMimeDecoder() { - return Decoder.RFC2045; - } - - /** - * This class implements an encoder for encoding byte data using - * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - *

- *

Instances of {@link Encoder} class are safe for use by - * multiple concurrent threads. - *

- *

Unless otherwise noted, passing a {@code null} argument to - * a method of this class will cause a - * {@link java.lang.NullPointerException NullPointerException} to - * be thrown. - * - * @see Decoder - * @since 1.8 - */ - public static class Encoder { - - private final byte[] newline; - private final int linemax; - private final boolean isURL; - private final boolean doPadding; - - private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) { - this.isURL = isURL; - this.newline = newline; - this.linemax = linemax; - this.doPadding = doPadding; - } - - /** - * This array is a lookup table that translates 6-bit positive integer - * index values into their "Base64 Alphabet" equivalents as specified - * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648). - */ - private static final char[] toBase64 = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' - }; - - /** - * It's the lookup table for "URL and Filename safe Base64" as specified - * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and - * '_'. This table is used when BASE64_URL is specified. - */ - private static final char[] toBase64URL = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' - }; - - private static final int MIMELINEMAX = 76; - private static final byte[] CRLF = new byte[]{'\r', '\n'}; - - static final Encoder RFC4648 = new Encoder(false, null, -1, true); - static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true); - static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true); - - private final int outLength(int srclen) { - int len = 0; - if (doPadding) { - len = 4 * ((srclen + 2) / 3); - } else { - int n = srclen % 3; - len = 4 * (srclen / 3) + (n == 0 ? 0 : n + 1); - } - if (linemax > 0) // line separators - len += (len - 1) / linemax * newline.length; - return len; - } - - /** - * Encodes all bytes from the specified byte array into a newly-allocated - * byte array using the {@link Base64} encoding scheme. The returned byte - * array is of the length of the resulting bytes. - * - * @param src the byte array to encode - * @return A newly-allocated byte array containing the resulting - * encoded bytes. - */ - public byte[] encode(byte[] src) { - int len = outLength(src.length); // dst array size - byte[] dst = new byte[len]; - int ret = encode0(src, 0, src.length, dst); - if (ret != dst.length) - return Arrays.copyOf(dst, ret); - return dst; - } - - /** - * Encodes all bytes from the specified byte array using the - * {@link Base64} encoding scheme, writing the resulting bytes to the - * given output byte array, starting at offset 0. - *

- *

It is the responsibility of the invoker of this method to make - * sure the output byte array {@code dst} has enough space for encoding - * all bytes from the input byte array. No bytes will be written to the - * output byte array if the output byte array is not big enough. - * - * @param src the byte array to encode - * @param dst the output byte array - * @return The number of bytes written to the output byte array - * @throws IllegalArgumentException if {@code dst} does not have enough - * space for encoding all input bytes. - */ - public int encode(byte[] src, byte[] dst) { - int len = outLength(src.length); // dst array size - if (dst.length < len) - throw new IllegalArgumentException( - "Output byte array is too small for encoding all input bytes"); - return encode0(src, 0, src.length, dst); - } - - /** - * Encodes the specified byte array into a String using the {@link Base64} - * encoding scheme. - *

- *

This method first encodes all input bytes into a base64 encoded - * byte array and then constructs a new String by using the encoded byte - * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1 - * ISO-8859-1} charset. - *

- *

In other words, an invocation of this method has exactly the same - * effect as invoking - * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}. - * - * @param src the byte array to encode - * @return A String containing the resulting Base64 encoded characters - */ - @SuppressWarnings("deprecation") - public String encodeToString(byte[] src) { - byte[] encoded = encode(src); - return new String(encoded, 0, 0, encoded.length); - } - - /** - * Encodes all remaining bytes from the specified byte buffer into - * a newly-allocated ByteBuffer using the {@link Base64} encoding - * scheme. - *

- * Upon return, the source buffer's position will be updated to - * its limit; its limit will not have been changed. The returned - * output buffer's position will be zero and its limit will be the - * number of resulting encoded bytes. - * - * @param buffer the source ByteBuffer to encode - * @return A newly-allocated byte buffer containing the encoded bytes. - */ - public ByteBuffer encode(ByteBuffer buffer) { - int len = outLength(buffer.remaining()); - byte[] dst = new byte[len]; - int ret = 0; - if (buffer.hasArray()) { - ret = encode0(buffer.array(), - buffer.arrayOffset() + buffer.position(), - buffer.arrayOffset() + buffer.limit(), - dst); - buffer.position(buffer.limit()); - } else { - byte[] src = new byte[buffer.remaining()]; - buffer.get(src); - ret = encode0(src, 0, src.length, dst); - } - if (ret != dst.length) - dst = Arrays.copyOf(dst, ret); - return ByteBuffer.wrap(dst); - } - - /** - * Wraps an output stream for encoding byte data using the {@link Base64} - * encoding scheme. - *

- *

It is recommended to promptly close the returned output stream after - * use, during which it will flush all possible leftover bytes to the underlying - * output stream. Closing the returned output stream will close the underlying - * output stream. - * - * @param os the output stream. - * @return the output stream for encoding the byte data into the - * specified Base64 encoded format - */ - public OutputStream wrap(OutputStream os) { - Objects.requireNonNull(os); - return new EncOutputStream(os, isURL ? toBase64URL : toBase64, - newline, linemax, doPadding); - } - - /** - * Returns an encoder instance that encodes equivalently to this one, - * but without adding any padding character at the end of the encoded - * byte data. - *

- *

The encoding scheme of this encoder instance is unaffected by - * this invocation. The returned encoder instance should be used for - * non-padding encoding operation. - * - * @return an equivalent encoder that encodes without adding any - * padding character at the end - */ - public Encoder withoutPadding() { - if (!doPadding) - return this; - return new Encoder(isURL, newline, linemax, false); - } - - private int encode0(byte[] src, int off, int end, byte[] dst) { - char[] base64 = isURL ? toBase64URL : toBase64; - int sp = off; - int slen = (end - off) / 3 * 3; - int sl = off + slen; - if (linemax > 0 && slen > linemax / 4 * 3) - slen = linemax / 4 * 3; - int dp = 0; - while (sp < sl) { - int sl0 = Math.min(sp + slen, sl); - for (int sp0 = sp, dp0 = dp; sp0 < sl0; ) { - int bits = (src[sp0++] & 0xff) << 16 | - (src[sp0++] & 0xff) << 8 | - (src[sp0++] & 0xff); - dst[dp0++] = (byte) base64[(bits >>> 18) & 0x3f]; - dst[dp0++] = (byte) base64[(bits >>> 12) & 0x3f]; - dst[dp0++] = (byte) base64[(bits >>> 6) & 0x3f]; - dst[dp0++] = (byte) base64[bits & 0x3f]; - } - int dlen = (sl0 - sp) / 3 * 4; - dp += dlen; - sp = sl0; - if (dlen == linemax && sp < end) { - for (byte b : newline) { - dst[dp++] = b; - } - } - } - if (sp < end) { // 1 or 2 leftover bytes - int b0 = src[sp++] & 0xff; - dst[dp++] = (byte) base64[b0 >> 2]; - if (sp == end) { - dst[dp++] = (byte) base64[(b0 << 4) & 0x3f]; - if (doPadding) { - dst[dp++] = '='; - dst[dp++] = '='; - } - } else { - int b1 = src[sp++] & 0xff; - dst[dp++] = (byte) base64[(b0 << 4) & 0x3f | (b1 >> 4)]; - dst[dp++] = (byte) base64[(b1 << 2) & 0x3f]; - if (doPadding) { - dst[dp++] = '='; - } - } - } - return dp; - } - } - - /** - * This class implements a decoder for decoding byte data using the - * Base64 encoding scheme as specified in RFC 4648 and RFC 2045. - *

- *

The Base64 padding character {@code '='} is accepted and - * interpreted as the end of the encoded byte data, but is not - * required. So if the final unit of the encoded byte data only has - * two or three Base64 characters (without the corresponding padding - * character(s) padded), they are decoded as if followed by padding - * character(s). If there is a padding character present in the - * final unit, the correct number of padding character(s) must be - * present, otherwise {@code IllegalArgumentException} ( - * {@code IOException} when reading from a Base64 stream) is thrown - * during decoding. - *

- *

Instances of {@link Decoder} class are safe for use by - * multiple concurrent threads. - *

- *

Unless otherwise noted, passing a {@code null} argument to - * a method of this class will cause a - * {@link java.lang.NullPointerException NullPointerException} to - * be thrown. - * - * @see Encoder - * @since 1.8 - */ - public static class Decoder { - - private final boolean isURL; - private final boolean isMIME; - - private Decoder(boolean isURL, boolean isMIME) { - this.isURL = isURL; - this.isMIME = isMIME; - } - - /** - * Lookup table for decoding unicode characters drawn from the - * "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into - * their 6-bit positive integer equivalents. Characters that - * are not in the Base64 alphabet but fall within the bounds of - * the array are encoded to -1. - */ - private static final int[] fromBase64 = new int[256]; - - static { - Arrays.fill(fromBase64, -1); - for (int i = 0; i < Encoder.toBase64.length; i++) - fromBase64[Encoder.toBase64[i]] = i; - fromBase64['='] = -2; - } - - /** - * Lookup table for decoding "URL and Filename safe Base64 Alphabet" - * as specified in Table2 of the RFC 4648. - */ - private static final int[] fromBase64URL = new int[256]; - - static { - Arrays.fill(fromBase64URL, -1); - for (int i = 0; i < Encoder.toBase64URL.length; i++) - fromBase64URL[Encoder.toBase64URL[i]] = i; - fromBase64URL['='] = -2; - } - - static final Decoder RFC4648 = new Decoder(false, false); - static final Decoder RFC4648_URLSAFE = new Decoder(true, false); - static final Decoder RFC2045 = new Decoder(false, true); - - /** - * Decodes all bytes from the input byte array using the {@link Base64} - * encoding scheme, writing the results into a newly-allocated output - * byte array. The returned byte array is of the length of the resulting - * bytes. - * - * @param src the byte array to decode - * @return A newly-allocated byte array containing the decoded bytes. - * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme - */ - public byte[] decode(byte[] src) { - byte[] dst = new byte[outLength(src, 0, src.length)]; - int ret = decode0(src, 0, src.length, dst); - if (ret != dst.length) { - dst = Arrays.copyOf(dst, ret); - } - return dst; - } - - /** - * Decodes a Base64 encoded String into a newly-allocated byte array - * using the {@link Base64} encoding scheme. - *

- *

An invocation of this method has exactly the same effect as invoking - * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))} - * - * @param src the string to decode - * @return A newly-allocated byte array containing the decoded bytes. - * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme - */ - public byte[] decode(String src) { - return decode(src.getBytes(StandardCharsets.ISO_8859_1)); - } - - /** - * Decodes all bytes from the input byte array using the {@link Base64} - * encoding scheme, writing the results into the given output byte array, - * starting at offset 0. - *

- *

It is the responsibility of the invoker of this method to make - * sure the output byte array {@code dst} has enough space for decoding - * all bytes from the input byte array. No bytes will be be written to - * the output byte array if the output byte array is not big enough. - *

- *

If the input byte array is not in valid Base64 encoding scheme - * then some bytes may have been written to the output byte array before - * IllegalargumentException is thrown. - * - * @param src the byte array to decode - * @param dst the output byte array - * @return The number of bytes written to the output byte array - * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme, or {@code dst} - * does not have enough space for decoding all input bytes. - */ - public int decode(byte[] src, byte[] dst) { - int len = outLength(src, 0, src.length); - if (dst.length < len) - throw new IllegalArgumentException( - "Output byte array is too small for decoding all input bytes"); - return decode0(src, 0, src.length, dst); - } - - /** - * Decodes all bytes from the input byte buffer using the {@link Base64} - * encoding scheme, writing the results into a newly-allocated ByteBuffer. - *

- *

Upon return, the source buffer's position will be updated to - * its limit; its limit will not have been changed. The returned - * output buffer's position will be zero and its limit will be the - * number of resulting decoded bytes - *

- *

{@code IllegalArgumentException} is thrown if the input buffer - * is not in valid Base64 encoding scheme. The position of the input - * buffer will not be advanced in this case. - * - * @param buffer the ByteBuffer to decode - * @return A newly-allocated byte buffer containing the decoded bytes - * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme. - */ - public ByteBuffer decode(ByteBuffer buffer) { - int pos0 = buffer.position(); - try { - byte[] src; - int sp, sl; - if (buffer.hasArray()) { - src = buffer.array(); - sp = buffer.arrayOffset() + buffer.position(); - sl = buffer.arrayOffset() + buffer.limit(); - buffer.position(buffer.limit()); - } else { - src = new byte[buffer.remaining()]; - buffer.get(src); - sp = 0; - sl = src.length; - } - byte[] dst = new byte[outLength(src, sp, sl)]; - return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst)); - } catch (IllegalArgumentException iae) { - buffer.position(pos0); - throw iae; - } - } - - /** - * Returns an input stream for decoding {@link Base64} encoded byte stream. - *

- *

The {@code read} methods of the returned {@code InputStream} will - * throw {@code IOException} when reading bytes that cannot be decoded. - *

- *

Closing the returned input stream will close the underlying - * input stream. - * - * @param is the input stream - * @return the input stream for decoding the specified Base64 encoded - * byte stream - */ - public InputStream wrap(InputStream is) { - Objects.requireNonNull(is); - return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME); - } - - private int outLength(byte[] src, int sp, int sl) { - int[] base64 = isURL ? fromBase64URL : fromBase64; - int paddings = 0; - int len = sl - sp; - if (len == 0) - return 0; - if (len < 2) { - if (isMIME && base64[0] == -1) - return 0; - throw new IllegalArgumentException( - "Input byte[] should at least have 2 bytes for base64 bytes"); - } - if (isMIME) { - // scan all bytes to fill out all non-alphabet. a performance - // trade-off of pre-scan or Arrays.copyOf - int n = 0; - while (sp < sl) { - int b = src[sp++] & 0xff; - if (b == '=') { - len -= (sl - sp + 1); - break; - } - if ((b = base64[b]) == -1) - n++; - } - len -= n; - } else { - if (src[sl - 1] == '=') { - paddings++; - if (src[sl - 2] == '=') - paddings++; - } - } - if (paddings == 0 && (len & 0x3) != 0) - paddings = 4 - (len & 0x3); - return 3 * ((len + 3) / 4) - paddings; - } - - private int decode0(byte[] src, int sp, int sl, byte[] dst) { - int[] base64 = isURL ? fromBase64URL : fromBase64; - int dp = 0; - int bits = 0; - int shiftto = 18; // pos of first byte of 4-byte atom - while (sp < sl) { - int b = src[sp++] & 0xff; - if ((b = base64[b]) < 0) { - if (b == -2) { // padding byte '=' - // = shiftto==18 unnecessary padding - // x= shiftto==12 a dangling single x - // x to be handled together with non-padding case - // xx= shiftto==6&&sp==sl missing last = - // xx=y shiftto==6 last is not = - if (shiftto == 6 && (sp == sl || src[sp++] != '=') || - shiftto == 18) { - throw new IllegalArgumentException( - "Input byte array has wrong 4-byte ending unit"); - } - break; - } - if (isMIME) // skip if for rfc2045 - continue; - else - throw new IllegalArgumentException( - "Illegal base64 character " + - Integer.toString(src[sp - 1], 16)); - } - bits |= (b << shiftto); - shiftto -= 6; - if (shiftto < 0) { - dst[dp++] = (byte) (bits >> 16); - dst[dp++] = (byte) (bits >> 8); - dst[dp++] = (byte) (bits); - shiftto = 18; - bits = 0; - } - } - // reached end of byte array or hit padding '=' characters. - if (shiftto == 6) { - dst[dp++] = (byte) (bits >> 16); - } else if (shiftto == 0) { - dst[dp++] = (byte) (bits >> 16); - dst[dp++] = (byte) (bits >> 8); - } else if (shiftto == 12) { - // dangling single "x", incorrectly encoded. - throw new IllegalArgumentException( - "Last unit does not have enough valid bits"); - } - // anything left is invalid, if is not MIME. - // if MIME, ignore all non-base64 character - while (sp < sl) { - if (isMIME && base64[src[sp++]] < 0) - continue; - throw new IllegalArgumentException( - "Input byte array has incorrect ending byte at " + sp); - } - return dp; - } - } - - /* - * An output stream for encoding bytes into the Base64. - */ - private static class EncOutputStream extends FilterOutputStream { - - private int leftover = 0; - private int b0, b1, b2; - private boolean closed = false; - - private final char[] base64; // byte->base64 mapping - private final byte[] newline; // line separator, if needed - private final int linemax; - private final boolean doPadding;// whether or not to pad - private int linepos = 0; - - EncOutputStream(OutputStream os, char[] base64, - byte[] newline, int linemax, boolean doPadding) { - super(os); - this.base64 = base64; - this.newline = newline; - this.linemax = linemax; - this.doPadding = doPadding; - } - - @Override - public void write(int b) throws IOException { - byte[] buf = new byte[1]; - buf[0] = (byte) (b & 0xff); - write(buf, 0, 1); - } - - private void checkNewline() throws IOException { - if (linepos == linemax) { - out.write(newline); - linepos = 0; - } - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - if (closed) - throw new IOException("Stream is closed"); - if (off < 0 || len < 0 || off + len > b.length) - throw new ArrayIndexOutOfBoundsException(); - if (len == 0) - return; - if (leftover != 0) { - if (leftover == 1) { - b1 = b[off++] & 0xff; - len--; - if (len == 0) { - leftover++; - return; - } - } - b2 = b[off++] & 0xff; - len--; - checkNewline(); - out.write(base64[b0 >> 2]); - out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); - out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]); - out.write(base64[b2 & 0x3f]); - linepos += 4; - } - int nBits24 = len / 3; - leftover = len - (nBits24 * 3); - while (nBits24-- > 0) { - checkNewline(); - int bits = (b[off++] & 0xff) << 16 | - (b[off++] & 0xff) << 8 | - (b[off++] & 0xff); - out.write(base64[(bits >>> 18) & 0x3f]); - out.write(base64[(bits >>> 12) & 0x3f]); - out.write(base64[(bits >>> 6) & 0x3f]); - out.write(base64[bits & 0x3f]); - linepos += 4; - } - if (leftover == 1) { - b0 = b[off++] & 0xff; - } else if (leftover == 2) { - b0 = b[off++] & 0xff; - b1 = b[off++] & 0xff; - } - } - - @Override - public void close() throws IOException { - if (!closed) { - closed = true; - if (leftover == 1) { - checkNewline(); - out.write(base64[b0 >> 2]); - out.write(base64[(b0 << 4) & 0x3f]); - if (doPadding) { - out.write('='); - out.write('='); - } - } else if (leftover == 2) { - checkNewline(); - out.write(base64[b0 >> 2]); - out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); - out.write(base64[(b1 << 2) & 0x3f]); - if (doPadding) { - out.write('='); - } - } - leftover = 0; - out.close(); - } - } - } - - /* - * An input stream for decoding Base64 bytes - */ - private static class DecInputStream extends InputStream { - - private final InputStream is; - private final boolean isMIME; - private final int[] base64; // base64 -> byte mapping - private int bits = 0; // 24-bit buffer for decoding - private int nextin = 18; // next available "off" in "bits" for input; - // -> 18, 12, 6, 0 - private int nextout = -8; // next available "off" in "bits" for output; - // -> 8, 0, -8 (no byte for output) - private boolean eof = false; - private boolean closed = false; - - DecInputStream(InputStream is, int[] base64, boolean isMIME) { - this.is = is; - this.base64 = base64; - this.isMIME = isMIME; - } - - private byte[] sbBuf = new byte[1]; - - @Override - public int read() throws IOException { - return read(sbBuf, 0, 1) == -1 ? -1 : sbBuf[0] & 0xff; - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - if (closed) - throw new IOException("Stream is closed"); - if (eof && nextout < 0) // eof and no leftover - return -1; - if (off < 0 || len < 0 || len > b.length - off) - throw new IndexOutOfBoundsException(); - int oldOff = off; - if (nextout >= 0) { // leftover output byte(s) in bits buf - do { - if (len == 0) - return off - oldOff; - b[off++] = (byte) (bits >> nextout); - len--; - nextout -= 8; - } while (nextout >= 0); - bits = 0; - } - while (len > 0) { - int v = is.read(); - if (v == -1) { - eof = true; - if (nextin != 18) { - if (nextin == 12) - throw new IOException("Base64 stream has one un-decoded dangling byte."); - // treat ending xx/xxx without padding character legal. - // same logic as v == '=' below - b[off++] = (byte) (bits >> (16)); - len--; - if (nextin == 0) { // only one padding byte - if (len == 0) { // no enough output space - bits >>= 8; // shift to lowest byte - nextout = 0; - } else { - b[off++] = (byte) (bits >> 8); - } - } - } - if (off == oldOff) - return -1; - else - return off - oldOff; - } - if (v == '=') { // padding byte(s) - // = shiftto==18 unnecessary padding - // x= shiftto==12 dangling x, invalid unit - // xx= shiftto==6 && missing last '=' - // xx=y or last is not '=' - if (nextin == 18 || nextin == 12 || - nextin == 6 && is.read() != '=') { - throw new IOException("Illegal base64 ending sequence:" + nextin); - } - b[off++] = (byte) (bits >> (16)); - len--; - if (nextin == 0) { // only one padding byte - if (len == 0) { // no enough output space - bits >>= 8; // shift to lowest byte - nextout = 0; - } else { - b[off++] = (byte) (bits >> 8); - } - } - eof = true; - break; - } - if ((v = base64[v]) == -1) { - if (isMIME) // skip if for rfc2045 - continue; - else - throw new IOException("Illegal base64 character " + - Integer.toString(v, 16)); - } - bits |= (v << nextin); - if (nextin == 0) { - nextin = 18; // clear for next - nextout = 16; - while (nextout >= 0) { - b[off++] = (byte) (bits >> nextout); - len--; - nextout -= 8; - if (len == 0 && nextout >= 0) { // don't clean "bits" - return off - oldOff; - } - } - bits = 0; - } else { - nextin -= 6; - } - } - return off - oldOff; - } - - @Override - public int available() throws IOException { - if (closed) - throw new IOException("Stream is closed"); - return is.available(); // TBD: - } - - @Override - public void close() throws IOException { - if (!closed) { - closed = true; - is.close(); - } - } - } -} diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index 3c2f1393..5e452ebe 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -22,6 +22,8 @@ import com.mpush.api.Constants; +import java.util.Base64; + public class Base64Utils { /** @@ -29,9 +31,9 @@ public class Base64Utils { * BASE64字符串解码为二进制数据 *

* - * @param base64 - * @return - * @throws Exception + * @param base64 base64 + * @return 源二进制数据 + * @throws Exception Exception */ public static byte[] decode(String base64) throws Exception { return Base64.getDecoder().decode(base64.getBytes(Constants.UTF_8)); @@ -42,9 +44,9 @@ public static byte[] decode(String base64) throws Exception { * 二进制数据编码为BASE64字符串 *

* - * @param bytes - * @return - * @throws Exception + * @param bytes base64 + * @return BASE64后的二进制数据 + * @throws Exception Exception */ public static String encode(byte[] bytes) throws Exception { return new String(Base64.getEncoder().encode(bytes), Constants.UTF_8); diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java index 536f9ba8..08932041 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/MD5Utils.java @@ -86,14 +86,6 @@ private static String toHex(byte[] bytes) { return buffer.toString(); } - - /** - * HmacSHA1 加密 - * - * @param data - * @param encryptKey - * @return - */ public static String hmacSha1(String data, String encryptKey) { final String HMAC_SHA1 = "HmacSHA1"; SecretKeySpec signingKey = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), HMAC_SHA1); @@ -107,12 +99,7 @@ public static String hmacSha1(String data, String encryptKey) { } } - /** - * HmacSHA1 加密 - * - * @param data - * @return - */ + public static String sha1(String data) { try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index b1edd6db..885aada3 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -39,9 +39,8 @@ /** * RSA公钥/私钥/签名工具包 - *

- * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式
- * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,
+ * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式 + * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密 * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 */ public final class RSAUtils { @@ -81,7 +80,7 @@ public final class RSAUtils { /** * 生成公钥和私钥 * - * @throws NoSuchAlgorithmException + * @return 公钥和私钥 */ public static Pair genKeyPair() { try { @@ -100,9 +99,9 @@ public static Pair genKeyPair() { /** * 编码密钥,便于存储 * - * @param key - * @return - * @throws Exception + * @param key 密钥 + * @return base64后的字符串 + * @throws Exception Exception */ public static String encodeBase64(Key key) throws Exception { return Base64Utils.encode(key.getEncoded()); @@ -111,9 +110,9 @@ public static String encodeBase64(Key key) throws Exception { /** * 从字符串解码私钥 * - * @param key - * @return - * @throws Exception + * @param key 密钥 + * @return base64后的字符串 + * @throws Exception Exception */ public static PrivateKey decodePrivateKey(String key) throws Exception { byte[] keyBytes = Base64Utils.decode(key); @@ -125,9 +124,9 @@ public static PrivateKey decodePrivateKey(String key) throws Exception { /** * 从字符串解码公钥 * - * @param publicKey - * @return - * @throws Exception + * @param publicKey 公钥 + * @return 公钥 + * @throws Exception Exception */ public static PublicKey decodePublicKey(String publicKey) throws Exception { byte[] keyBytes = Base64Utils.decode(publicKey); @@ -142,8 +141,8 @@ public static PublicKey decodePublicKey(String publicKey) throws Exception { * * @param data 已加密数据 * @param privateKey 私钥(BASE64编码) - * @return - * @throws Exception + * @return 私钥 + * @throws Exception Exception */ public static String sign(byte[] data, String privateKey) throws Exception { Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); @@ -158,8 +157,8 @@ public static String sign(byte[] data, String privateKey) throws Exception { * @param data 已加密数据 * @param publicKey 公钥(BASE64编码) * @param sign 数字签名 - * @return - * @throws Exception + * @return 是否通过校验 + * @throws Exception Exception */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); @@ -177,7 +176,7 @@ public static boolean verify(byte[] data, String publicKey, String sign) throws * * @param modulus 模 * @param exponent 指数 - * @return + * @return 公钥 */ public static RSAPublicKey getPublicKey(String modulus, String exponent) { try { @@ -200,7 +199,7 @@ public static RSAPublicKey getPublicKey(String modulus, String exponent) { * * @param modulus 模 * @param exponent 指数 - * @return + * @return 私钥 */ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { try { @@ -218,10 +217,9 @@ public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { /** * 公钥加密 * - * @param data - * @param publicKey - * @return - * @throws Exception + * @param data 待加密数据 + * @param publicKey 公钥 + * @return 加密后的值 */ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { try { @@ -241,10 +239,9 @@ public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey publicKey) { /** * 私钥解密 * - * @param data - * @param privateKey - * @return - * @throws Exception + * @param data 待加密数据 + * @param privateKey 私钥 + * @return 解密后的值 */ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) { try { @@ -265,9 +262,9 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) * 解密要求密文最大长度为128字节, * 所以在加密和解密的过程中需要分块进行。 * - * @param cipher - * @param data - * @return + * @param cipher 密钥 + * @param data 待处理的数据 + * @return 处理后的值 * @throws BadPaddingException * @throws IllegalBlockSizeException */ @@ -295,8 +292,8 @@ private static int getTmpArrayLength(int L) { * * @param data 已加密数据 * @param privateKey 私钥(BASE64编码) - * @return - * @throws Exception + * @return 解密后的值 + * @throws Exception Exception */ public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception { PrivateKey key = decodePrivateKey(privateKey); @@ -310,8 +307,8 @@ public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws * * @param data 已加密数据 * @param publicKey 公钥(BASE64编码) - * @return - * @throws Exception + * @return 解密后的值 + * @throws Exception Exception */ public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Exception { PublicKey key = decodePublicKey(publicKey); @@ -325,8 +322,8 @@ public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Ex * * @param data 源数据 * @param publicKey 公钥(BASE64编码) - * @return - * @throws Exception + * @return 加密后的值 + * @throws Exception Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { PublicKey key = decodePublicKey(publicKey); @@ -341,8 +338,8 @@ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Ex * * @param data 源数据 * @param privateKey 私钥(BASE64编码) - * @return - * @throws Exception + * @return 加密后的值 + * @throws Exception Exception */ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception { PrivateKey key = decodePrivateKey(privateKey); diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml index aa427ea6..025c4495 100644 --- a/mpush-zk/pom.xml +++ b/mpush-zk/pom.xml @@ -2,16 +2,21 @@ + + 4.0.0 + mpush - com.mpush + com.github.mpusher 1.0 - 4.0.0 - ${mpush.groupId} mpush-zk ${mpush-zk-version} + jar + mpush-zookeeper + MPUSH消息推送系统zookeeper集群控制模块 + https://github.com/mpusher/mpush diff --git a/pom.xml b/pom.xml index a79e5bd4..3f889aff 100644 --- a/pom.xml +++ b/pom.xml @@ -4,10 +4,19 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.mpush + + org.sonatype.oss + oss-parent + 9 + + + com.github.mpusher mpush pom 1.0 + mpush + MPUSH消息推送系统 + https://github.com/mpusher/mpush @@ -16,12 +25,14 @@ repo + master git@github.com:mpusher/mpush.git scm:git@github.com:mpusher/mpush.git - scm:ggit@github.com:mpusher/mpush.git + scm:git@github.com:mpusher/mpush.git + ohun @@ -49,8 +60,8 @@ UTF-8 UTF-8 1.8 - com.mpush - 0.0.1 + com.github.mpusher + 0.0.2 ${mpush.version} ${mpush.version} ${mpush.version} @@ -285,6 +296,7 @@ + dev @@ -300,5 +312,55 @@ pub + + release + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2 + + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + + + sign-artifacts + verify + + sign + + + + + + + From 013b4b12cb149b836a6619cab960db0fe7853373 Mon Sep 17 00:00:00 2001 From: ohun Date: Fri, 26 Aug 2016 21:55:46 +0800 Subject: [PATCH 592/890] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0515b0f..baf1be30 100644 --- a/README.md +++ b/README.md @@ -67,9 +67,9 @@ 9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 -10. 配置文件详解 +## 配置文件详解 ```java - ################################################################################################################## +################################################################################################################## # # NOTICE: # From 0ef0f69d933771e0746df99c375029ed765b0d67 Mon Sep 17 00:00:00 2001 From: ohun Date: Sat, 27 Aug 2016 09:17:15 +0800 Subject: [PATCH 593/890] Update README.md --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index baf1be30..522076d8 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,18 @@ ```set-env.sh``` 用于增加和修改jvm启动参数,比如堆内存、开启远程调试端口、开启jmx等 -9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 - +9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 +10. 集成部署,比如集成到现有web工程一起部署到tomcat,可以添加如下依赖 + + ```xml + + com.github.mpusher + mpush-boot + 0.0.2 + + ``` + 启动入口`com.mpush.bootstrap.ServerLauncher.java` + ## 配置文件详解 ```java ################################################################################################################## From 07eec7b9176c8a1570d3fabe8814daaab4067c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 28 Aug 2016 13:24:40 +0800 Subject: [PATCH 594/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9=E4=BC=98?= =?UTF-8?q?=E5=8C=96,?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 4 +-- mpush-boot/src/main/resources/mpush.conf | 2 +- .../connect/ConnClientChannelHandler.java | 31 ++++++++++--------- .../src/main/resources/application.conf | 16 ---------- .../src/test/resources/application.conf | 7 ++--- pom.xml | 4 --- 6 files changed, 23 insertions(+), 41 deletions(-) delete mode 100644 mpush-client/src/main/resources/application.conf diff --git a/conf/reference.conf b/conf/reference.conf index 86e7117b..57ae3c05 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -99,8 +99,8 @@ mp { [ { host:"127.0.0.1" - port:2181 - password:ShineMoIpo + port:6379 +# password:ShineMoIpo //密码, 没有可以不设置 } ] ] diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index b01e4300..ea735b95 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -12,7 +12,7 @@ mp.redis={ { host:"127.0.0.1" port:6379 - password:"your redis password" +# password:"your redis password" //密码, 没有可以不设置 } ] ] diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index def53d76..83157123 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -70,7 +70,6 @@ public Connection getConnection() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { connection.updateLastReadTime(); - //加密 if (msg instanceof Packet) { Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); @@ -80,7 +79,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); startHeartBeat(message.heartbeat); - LOGGER.warn("会话密钥:{},message={}", sessionKey, message); + LOGGER.warn(">>> handshake success, message={}, sessionKey={}", message, sessionKey); bindUser(clientConfig); saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); } else if (command == Command.FAST_CONNECT) { @@ -93,32 +92,32 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); startHeartBeat(message.heartbeat); bindUser(clientConfig); - LOGGER.warn("fast connect success, message=" + message); + LOGGER.warn(">>> fast connect success, message=" + message); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); + LOGGER.error(">>> receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); ctx.close(); } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error packet=" + errorMessage); + LOGGER.error(">>> receive an error packet=" + errorMessage); } else if (command == Command.BIND) { OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.warn("receive an success packet=" + okMessage); + LOGGER.warn(">>> receive an success packet=" + okMessage); HttpRequestMessage message = new HttpRequestMessage(connection); message.uri = "http://baidu.com"; message.send(); } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); - LOGGER.warn("receive an push message, content=" + new String(message.content, Constants.UTF_8)); + LOGGER.warn(">>> receive an push message, content=" + new String(message.content, Constants.UTF_8)); } else if (command == Command.HEARTBEAT) { - LOGGER.warn("receive a heartbeat pong..."); + LOGGER.warn(">>> receive a heartbeat pong..."); } else { - LOGGER.warn("receive a message, type=" + command + "," + packet); + LOGGER.warn(">>> receive a message, type=" + command + "," + packet); } } - LOGGER.warn("update currentTime:" + ctx.channel() + "," + msg); + LOGGER.debug("update currentTime:" + ctx.channel() + "," + msg); } @Override @@ -131,7 +130,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.info("client connect channel={}", ctx.channel()); connection.init(ctx.channel(), true); - handshake(clientConfig); + tryFastConnect(); } @Override @@ -176,12 +175,14 @@ private void tryFastConnect() { handshake(clientConfig); } }); + LOGGER.debug("<<< send fast connect message={}", message); } private void bindUser(ClientConfig client) { BindUserMessage message = new BindUserMessage(connection); message.userId = client.getUserId(); message.send(); + LOGGER.debug("<<< send bind user message={}", message); } private void saveToRedisForFastConnection(ClientConfig client, String sessionId, Long expireTime, byte[] sessionKey) { @@ -193,6 +194,7 @@ private void saveToRedisForFastConnection(ClientConfig client, String sessionId, RedisManager.I.set(key, map, 60 * 5); //5分钟 } + @SuppressWarnings("unchecked") private Map getFastConnectionInfo(String deviceId) { String key = RedisKey.getDeviceIdKey(deviceId); return RedisManager.I.get(key, Map.class); @@ -208,9 +210,10 @@ private void handshake(ClientConfig client) { message.osVersion = client.getOsVersion(); message.timestamp = System.currentTimeMillis(); message.send(); + LOGGER.debug("<<< send handshake message={}", message); } - public void startHeartBeat(final int heartbeat) throws Exception { + private void startHeartBeat(final int heartbeat) throws Exception { HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { @@ -219,8 +222,8 @@ public void run(Timeout timeout) throws Exception { if (channel.isActive()) { ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); channelFuture.addListener((ChannelFutureListener) future -> { - if (future.isSuccess()) { - LOGGER.debug("client send msg hb success:" + channel.remoteAddress().toString()); + if (!future.isSuccess()) { + LOGGER.debug("<<< send heartbeat ping... " + channel.remoteAddress().toString()); } else { LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); } diff --git a/mpush-client/src/main/resources/application.conf b/mpush-client/src/main/resources/application.conf deleted file mode 100644 index 4e43e48b..00000000 --- a/mpush-client/src/main/resources/application.conf +++ /dev/null @@ -1,16 +0,0 @@ -mp.log.level=warn -mp.zk.server-address="127.0.0.1:2181" -mp.zk.namespace=mpush -mp.redis={ - write-to-zk=false - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[ - [ - { - host:"127.0.0.1" - port:6379 - password:"your redis password" - } - ] - ] -} \ No newline at end of file diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 1f651b11..defb91de 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -3,8 +3,7 @@ mp.log.level=debug mp.min-heartbeat=10s mp.max-heartbeat=10s mp.zk.namespace=mpush -mp.zk.server-address="111.1.57.148:5666" -mp.net.public-host-mapping={"172.17.42.1":"111.1.57.148"} +mp.zk.server-address="10.0.10.47:2181" mp.core.compress-threshold=10k mp.redis { write-to-zk=true @@ -12,9 +11,9 @@ mp.redis { cluster-group:[ [ { - host:"111.1.57.148" + host:"10.0.10.53" port:6379 - password:ShineMoIpo + password:shinemo123 } ] ] diff --git a/pom.xml b/pom.xml index 3f889aff..ce2b4d76 100644 --- a/pom.xml +++ b/pom.xml @@ -261,10 +261,6 @@ junit test - - org.slf4j - slf4j-api - From 2799ed7bc61c800104e56dafc678762cb073bcf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 30 Aug 2016 08:30:46 +0800 Subject: [PATCH 595/890] =?UTF-8?q?=E7=AE=80=E5=8C=96redis=20=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E9=85=8D=E7=BD=AE=E9=A1=B9,=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E5=8F=96=E6=B6=88=E5=AF=86=E7=A0=81=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/test/configcenter/ConfigCenterTest.java | 3 +-- mpush-test/src/test/resources/application.conf | 12 ++---------- .../src/main/java/com/mpush/tools/config/CC.java | 11 ++++++----- .../com/mpush/tools/config/data/RedisServer.java | 9 +++++++++ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index 97bedcc4..fa613266 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -36,8 +36,7 @@ public void setUp() throws Exception { @Test public void testKey() { //String t = ConfigKey.app_env.getString(); - System.out.println(CC.mp.security.aes_key_length); - System.out.println(CC.mp.redis.cluster_group); + System.out.println(CC.mp.redis.cluster_group.size()); } @Test diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index defb91de..4f1acc4c 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -7,14 +7,6 @@ mp.zk.server-address="10.0.10.47:2181" mp.core.compress-threshold=10k mp.redis { write-to-zk=true - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[ - [ - { - host:"10.0.10.53" - port:6379 - password:shinemo123 - } - ] - ] + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 + cluster-group:[["10.0.10.53:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port } \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index b431ba27..01f3ab4f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -249,11 +249,12 @@ interface redis { boolean write_to_zk = cfg.getBoolean("write-to-zk"); List cluster_group = cfg.getList("cluster-group") - .stream() - .map(v -> new RedisGroup(ConfigList.class.cast(v) - .stream() - .map(cv -> create(ConfigObject.class.cast(cv).toConfig(), RedisServer.class)) - .collect(toCollection(ArrayList::new)) + .stream()//第一纬度数组 + .map(v -> new RedisGroup( + ConfigList.class.cast(v)//第二纬度数组 + .stream() + .map(cv -> RedisServer.from(cv.unwrapped().toString()))//把字符串转换成 RedisServer + .collect(toCollection(ArrayList::new)) ) ) .collect(toCollection(ArrayList::new)); diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java index 451efdf6..73f50277 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java @@ -60,6 +60,15 @@ public void setPassword(String password) { this.password = password; } + public static RedisServer from(String config) { + String[] array = config.split(":"); + if (array.length == 2) { + return new RedisServer(array[0], Integer.parseInt(array[1]), null); + } else { + return new RedisServer(array[0], Integer.parseInt(array[1]), array[2]); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; From 34a23cc1ec8b92ae2dc417c824f440da01394e3e Mon Sep 17 00:00:00 2001 From: ohun Date: Tue, 30 Aug 2016 08:36:14 +0800 Subject: [PATCH 596/890] Update README.md --- README.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 522076d8..825a6a69 100644 --- a/README.md +++ b/README.md @@ -42,16 +42,8 @@ mp.net.connect-server-port=3000//长链接服务对外端口, 公网端口 mp.zk.server-address="127.0.0.1:2181"//zk 机器的地址 mp.redis={//redis 相关配置 - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[ - [ - { - host:"127.0.0.1" - port:6379 - password:"your redis password" - } - ] - ] + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password可以不设置密码 } //还有用于安全加密的RSA mp.security.private-key 和 mp.security.public-key 等... ``` From 733d6ac36bb1b297f50c4a287cf4c7be036e23e9 Mon Sep 17 00:00:00 2001 From: ohun Date: Tue, 30 Aug 2016 08:40:11 +0800 Subject: [PATCH 597/890] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 825a6a69..4fdcd946 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ mp.zk.server-address="127.0.0.1:2181"//zk 机器的地址 mp.redis={//redis 相关配置 #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password可以不设置密码 + cluster-group:[["127.0.0.1:6379"]]//格式ip:port:password,密码可以不设置ip:port } //还有用于安全加密的RSA mp.security.private-key 和 mp.security.public-key 等... ``` From c48c61fa2970168ef640cf53b7d0519013f53585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 30 Aug 2016 08:40:28 +0800 Subject: [PATCH 598/890] =?UTF-8?q?=E7=AE=80=E5=8C=96redis=20=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E9=85=8D=E7=BD=AE=E9=A1=B9,=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E5=8F=96=E6=B6=88=E5=AF=86=E7=A0=81=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 57ae3c05..c151a3c5 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -95,15 +95,7 @@ mp { redis { write-to-zk=true #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[ - [ - { - host:"127.0.0.1" - port:6379 -# password:ShineMoIpo //密码, 没有可以不设置 - } - ] - ] + cluster-group:[["127.0.0.1:6379:your password","127.0.0.1:6379:your password"]]//格式ip:port:password,密码可以不设置ip:port config { maxTotal:8, maxIdle:4, From 457e07d61b97b5d22d6af86b19ab6ba10bd56213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 31 Aug 2016 10:48:47 +0800 Subject: [PATCH 599/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E5=90=84=E6=A8=A1=E5=9D=97maven=E4=BE=9D=E8=B5=96=E5=85=B3?= =?UTF-8?q?=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- mpush-boot/pom.xml | 4 ---- .../java/com/mpush/bootstrap/job/LastBoot.java | 1 - .../java/com/mpush/bootstrap/job/RedisBoot.java | 2 ++ mpush-client/pom.xml | 8 -------- mpush-common/pom.xml | 12 ------------ .../java/com/mpush/common/user/UserManager.java | 15 +++++++++------ mpush-monitor/pom.xml | 4 ---- mpush-netty/pom.xml | 4 ---- mpush-tools/pom.xml | 8 ++++---- 10 files changed, 16 insertions(+), 44 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index c151a3c5..2c26b35a 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -77,7 +77,7 @@ mp { zk { server-address="127.0.0.1:2181" namespace=mpush - digest=mpush + digest=mpush //addauth digest mpush local-cache-path=/ retry { #initial amount of time to wait between retries diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index f08f5ef1..1be5d4e2 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -25,10 +25,6 @@ ${mpush.groupId} mpush-monitor - - ${mpush.groupId} - mpush-zk - diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index 48ebad40..1297330e 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -30,7 +30,6 @@ public final class LastBoot extends BootJob { @Override protected void start() { - UserManager.I.clearUserOnlineData(); Logs.Console.error("end start bootstrap chain..."); Logs.Console.error("==================================================================="); Logs.Console.error("====================MPUSH SERVER START SUCCESS====================="); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index 687d70c5..5cb57d22 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -20,6 +20,7 @@ package com.mpush.bootstrap.job; import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.user.UserManager; /** * Created by yxx on 2016/5/14. @@ -31,6 +32,7 @@ public final class RedisBoot extends BootJob { @Override protected void start() { RedisManager.I.init(); + UserManager.I.clearUserOnlineData(); startNext(); } diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index b3d06def..ee54b6d7 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -19,10 +19,6 @@ https://github.com/mpusher/mpush - - ${mpush.groupId} - mpush-api - ${mpush.groupId} mpush-netty @@ -31,9 +27,5 @@ ${mpush.groupId} mpush-common - - ${mpush.groupId} - mpush-cache - diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index e817ff02..c5143336 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -17,18 +17,6 @@ https://github.com/mpusher/mpush - - ${mpush.groupId} - mpush-api - - - ${mpush.groupId} - mpush-tools - - - ${mpush.groupId} - mpush-zk - ${mpush.groupId} mpush-cache diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java index 7a9d8372..e179fa0e 100644 --- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -34,11 +34,7 @@ public final class UserManager { private final String ONLINE_KEY = RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()); - public UserManager() { - clearUserOnlineData(); - } - - public void clearUserOnlineData() { + public void clearUserOnlineData() { RedisManager.I.del(ONLINE_KEY); } @@ -52,12 +48,19 @@ public void recordUserOffline(String userId) { LOGGER.info("user offline {}", userId); } - //在线用户 + //在线用户数量 public long getOnlineUserNum() { Long value = RedisManager.I.zCard(ONLINE_KEY); return value == null ? 0 : value; } + //在线用户数量 + public long getOnlineUserNum(String publicIP) { + String ONLINE_KEY = RedisKey.getUserOnlineKey(publicIP); + Long value = RedisManager.I.zCard(ONLINE_KEY); + return value == null ? 0 : value; + } + //在线用户列表 public List getOnlineUserList(int start, int size) { if (size < 10) { diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 29f25721..1f2133f3 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -15,10 +15,6 @@ https://github.com/mpusher/mpush - - ${mpush.groupId} - mpush-api - ${mpush.groupId} mpush-tools diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index f4ddf0e4..3bec1e1c 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -19,10 +19,6 @@ https://github.com/mpusher/mpush - - ${mpush.groupId} - mpush-api - ${mpush.groupId} mpush-tools diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 60bd7219..ef29dbd2 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -36,6 +36,10 @@ + + ${mpush.groupId} + mpush-api + com.google.code.gson gson @@ -52,10 +56,6 @@ com.typesafe config - - ${mpush.groupId} - mpush-api - org.slf4j slf4j-api From 5307aa8dbef0e4e88aa185fdadfbb593285c36cb Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 11:05:50 +0800 Subject: [PATCH 600/890] Initial commit --- README.md | 274 +----------------------------------------------------- 1 file changed, 1 insertion(+), 273 deletions(-) diff --git a/README.md b/README.md index d0515b0f..79ea502c 100644 --- a/README.md +++ b/README.md @@ -1,273 +1 @@ -## [详细教程](https://mpusher.github.io/docs) - -[https://mpusher.github.io/docs](https://mpusher.github.io/docs) - -## 源码测试 -1. ```git clone https://github.com/mpusher/mpush.git``` -2. 导入到eclipse或Intellij IDEA -3. 打开```mpush-test```模块,所有的测试代码都在该模块下 -4. 修改配置文件```src/test/resource/application.conf```文件修改方式参照 服务部署第6点 -5. 运行```com.mpush.test.sever.ServerTestMain.java```启动长链接服务 -6. 运行```com.mpush.test.client.ConnClientTestMain.java``` 模拟一个客户端 -7. 运行```com.mpush.test.push.PushClientTestMain``` 模拟给用户下发消息 -8. 可以在控制台观察日志看服务是否正常运行,消息是否下发成功 - -## 服务部署 - -###### 说明:mpush 服务只依赖于zookeeper和redis,当然还有JDK>=1.8 - -1. 安装```jdk 1.8``` 以上版本并设置```%JAVA_HOME%``` - -2. 安装```zookeeper``` (安装配置步骤略) - -3. 安装```Redis``` (安装配置步骤略) - -4. 下载mpush server正式包[https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz](https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz) - -5. 解压下载的tar包`tar -zvxf mpush-release-0.0.1.tar.gz`到 mpush 目录, 结构如下 - - >

-   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:30 bin —> 启动脚本
-   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:52 conf —> 配置文件
-   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:29 lib —> 核心类库
-   >-rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE
-   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:32 logs —> 日志目录
-   >-rw-rw-r-- 1 shinemo shinemo    21 May 31 11:07 README.md
-   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:52 tmp
-   >
- -6. 修改 conf 目录下的 ```vi mpush.conf```文件, ```mpush.conf```里的配置项会覆盖同目录下的```reference.conf```文件 - ```java - #主要修改以下配置 - mp.net.connect-server-port=3000//长链接服务对外端口, 公网端口 - mp.zk.server-address="127.0.0.1:2181"//zk 机器的地址 - mp.redis={//redis 相关配置 - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[ - [ - { - host:"127.0.0.1" - port:6379 - password:"your redis password" - } - ] - ] - } - //还有用于安全加密的RSA mp.security.private-key 和 mp.security.public-key 等... - ``` - 如果要修改其他配置请参照reference.conf文件 - -7. 给bin目录下的脚本增加执行权限```chmod u+x *.sh``` - -8. 执行```./mp.sh start``` 启动服务, 查看帮助```./mp.sh``` 目前支持的命令: - - ```Usage: ./mp.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}``` - - ```set-env.sh``` 用于增加和修改jvm启动参数,比如堆内存、开启远程调试端口、开启jmx等 - -9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 - -10. 配置文件详解 - ```java - ################################################################################################################## -# -# NOTICE: -# -# 系统配置文件,所有列出的项是系统所支持全部配置项 -# 如果要覆盖某项的值可以添加到mpush.conf中。 -# -# 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 -# 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 -# -############################################################################################################## - -mp { - #日志配置 - log.level=warn - log.dir=${user.dir}/../logs - - #核心配置 - core { - max-packet-size=10k //系统允许传输的最大包的大小 - compress-threshold=10k //数据包启用压缩的临界值,超过该值后对数据进行压缩 - min-heartbeat=3m //最小心跳间隔 - max-heartbeat=3m //最大心跳间隔 - max-hb-timeout-times=2 //允许的心跳连续超时的最大次数 - session-expired-time=1d //用于快速重连的session 过期时间默认1天 - epoll-provider=netty //nio:jdk自带,netty:由netty实现 - } - - #安全配置 - security { - #rsa 私钥, 公钥 key长度为1024;生成方式可以使用open-ssh或者使用工具类com.mpush.tools.crypto.RSAUtils#main - private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" - public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" - aes-key-length=16 //AES key 长度 - ras-key-length=1024 //RSA key 长度 - } - - #网络配置 - net { - connect-server-port=3000 //长链接服务对外端口, 公网端口 - gateway-server-port=3001 //网关服务端口, 内部端口 - admin-server-port=3002 //控制台服务端口, 内部端口 - public-host-mapping { //本机局域网IP和公网IP的映射关系 - "127.0.0.1":"111.1.32.137" - } - traffic-shaping { //流量整形配置 - gateway-client { - enabled:true - check-interval:100ms - write-global-limit:1k - read-global-limit:0 - write-channel-limit:256b - read-channel-limit:0 - } - - gateway-server { - enabled:true - check-interval:100ms - write-global-limit:0 - read-global-limit:10k - write-channel-limit:0 - read-channel-limit:0.5k - } - - connect-server { - enabled:false - check-interval:100ms - write-global-limit:0 - read-global-limit:100k - write-channel-limit:3k - read-channel-limit:3k - } - } - } - - #Zookeeper配置 - zk { - server-address="127.0.0.1:2181" - namespace=mpush - digest=mpush - local-cache-path=/ - retry { - #initial amount of time to wait between retries - baseSleepTimeMs=3s - #max number of times to retry - maxRetries=3 - #max time in ms to sleep on each retry - maxSleepMs=5s - } - connectionTimeoutMs=5s - sessionTimeoutMs=5s - } - - #Redis集群配置 - redis { - write-to-zk=true - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[ - [ - { - host:"127.0.0.1" - port:2181 - password:ShineMoIpo - } - ] - ] - config { - maxTotal:8, - maxIdle:4, - minIdle:1, - lifo:true, - fairness:false, - maxWaitMillis:5000, - minEvictableIdleTimeMillis:300000, - softMinEvictableIdleTimeMillis:1800000, - numTestsPerEvictionRun:3, - testOnCreate:false, - testOnBorrow:false, - testOnReturn:false, - testWhileIdle:false, - timeBetweenEvictionRunsMillis:60000, - blockWhenExhausted:true, - jmxEnabled:true, - jmxNamePrefix:pool, - jmxNameBase:pool - } - } - - #HTTP代理配置 - http { - proxy-enabled=false //启用Http代理 - max-conn-per-host=5 //每个域名的最大链接数, 建议web服务nginx超时时间设长一点, 以便保持长链接 - default-read-timeout=10s //请求超时时间 - max-content-length=5m //response body 最大大小 - dns-mapping { //域名映射外网地址转内部IP - "mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] - } - } - - #线程池配置 - thread { - pool { - boss { //netty boss - min:4 - max:16 - queue-size:1000 - } - - work { //netty boss - min:8 - max:32 - queue-size:1000 - } - - event-bus { - min:4 - max:4 - queue-size:10000 //大量的online,offline, - } - - http-proxy { - min:8 - max:64 - queue-size:1000 - } - - biz { //其他业务 - min:4 - max:64 - queue-size:10 - } - - mq { //用户上下线消息, 踢人等 - min:2 - max:4 - queue-size:10000 - } - - push-callback { //消息推送 - min:2 - max:2 - queue-size:0 - } - } - } - - #系统监控配置 - monitor { - dump-dir=/tmp/logs/mpush/ - dump-stack=false //是否定时dump堆栈 - dump-period=1m //多久监控一次 - print-log=true //是否打印监控日志 - } - - #SPI扩展配置 - spi { - thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" - dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" - } -} -``` -11. 未完待续... +#mpush From 53df434240ec702d7177e707f23a1e0f78fcfe2e Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 11:06:56 +0800 Subject: [PATCH 601/890] Update README.md --- README.md | 276 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 275 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 79ea502c..07e2c859 100644 --- a/README.md +++ b/README.md @@ -1 +1,275 @@ -#mpush +## [详细教程](https://mpusher.github.io/docs) + +[https://mpusher.github.io/docs](https://mpusher.github.io/docs) + +## 源码测试 +1. ```git clone https://github.com/mpusher/mpush.git``` +2. 导入到eclipse或Intellij IDEA +3. 打开```mpush-test```模块,所有的测试代码都在该模块下 +4. 修改配置文件```src/test/resource/application.conf```文件修改方式参照 服务部署第6点 +5. 运行```com.mpush.test.sever.ServerTestMain.java```启动长链接服务 +6. 运行```com.mpush.test.client.ConnClientTestMain.java``` 模拟一个客户端 +7. 运行```com.mpush.test.push.PushClientTestMain``` 模拟给用户下发消息 +8. 可以在控制台观察日志看服务是否正常运行,消息是否下发成功 + +## 服务部署 + +###### 说明:mpush 服务只依赖于zookeeper和redis,当然还有JDK>=1.8 + +1. 安装```jdk 1.8``` 以上版本并设置```%JAVA_HOME%``` + +2. 安装```zookeeper``` (安装配置步骤略) + +3. 安装```Redis``` (安装配置步骤略) + +4. 下载mpush server正式包[https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz](https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz) + +5. 解压下载的tar包`tar -zvxf mpush-release-0.0.1.tar.gz`到 mpush 目录, 结构如下 + + >
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:30 bin —> 启动脚本
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:52 conf —> 配置文件
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:29 lib —> 核心类库
+   >-rw-rw-r-- 1 shinemo shinemo 11357 May 31 11:07 LICENSE
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:32 logs —> 日志目录
+   >-rw-rw-r-- 1 shinemo shinemo    21 May 31 11:07 README.md
+   >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:52 tmp
+   >
+ +6. 修改 conf 目录下的 ```vi mpush.conf```文件, ```mpush.conf```里的配置项会覆盖同目录下的```reference.conf```文件 + ```java + #主要修改以下配置 + mp.net.connect-server-port=3000//长链接服务对外端口, 公网端口 + mp.zk.server-address="127.0.0.1:2181"//zk 机器的地址 + mp.redis={//redis 相关配置 + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[["127.0.0.1:6379"]]//格式ip:port:password,密码可以不设置ip:port + } + //还有用于安全加密的RSA mp.security.private-key 和 mp.security.public-key 等... + ``` + 如果要修改其他配置请参照reference.conf文件 + +7. 给bin目录下的脚本增加执行权限```chmod u+x *.sh``` + +8. 执行```./mp.sh start``` 启动服务, 查看帮助```./mp.sh``` 目前支持的命令: + + ```Usage: ./mp.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}``` + + ```set-env.sh``` 用于增加和修改jvm启动参数,比如堆内存、开启远程调试端口、开启jmx等 + +9. ```cd logs```目录,```cat mpush.out```查看服务是否启动成功 +10. 集成部署,比如集成到现有web工程一起部署到tomcat,可以添加如下依赖 + + ```xml + + com.github.mpusher + mpush-boot + 0.0.2 + + ``` + 启动入口`com.mpush.bootstrap.ServerLauncher.java` + +## 配置文件详解 + ```java +################################################################################################################## +# +# NOTICE: +# +# 系统配置文件,所有列出的项是系统所支持全部配置项 +# 如果要覆盖某项的值可以添加到mpush.conf中。 +# +# 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 +# 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 +# +############################################################################################################## + +mp { + #日志配置 + log.level=warn + log.dir=${user.dir}/../logs + + #核心配置 + core { + max-packet-size=10k //系统允许传输的最大包的大小 + compress-threshold=10k //数据包启用压缩的临界值,超过该值后对数据进行压缩 + min-heartbeat=3m //最小心跳间隔 + max-heartbeat=3m //最大心跳间隔 + max-hb-timeout-times=2 //允许的心跳连续超时的最大次数 + session-expired-time=1d //用于快速重连的session 过期时间默认1天 + epoll-provider=netty //nio:jdk自带,netty:由netty实现 + } + + #安全配置 + security { + #rsa 私钥, 公钥 key长度为1024;生成方式可以使用open-ssh或者使用工具类com.mpush.tools.crypto.RSAUtils#main + private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" + public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" + aes-key-length=16 //AES key 长度 + ras-key-length=1024 //RSA key 长度 + } + + #网络配置 + net { + connect-server-port=3000 //长链接服务对外端口, 公网端口 + gateway-server-port=3001 //网关服务端口, 内部端口 + admin-server-port=3002 //控制台服务端口, 内部端口 + public-host-mapping { //本机局域网IP和公网IP的映射关系 + "127.0.0.1":"111.1.32.137" + } + traffic-shaping { //流量整形配置 + gateway-client { + enabled:true + check-interval:100ms + write-global-limit:1k + read-global-limit:0 + write-channel-limit:256b + read-channel-limit:0 + } + + gateway-server { + enabled:true + check-interval:100ms + write-global-limit:0 + read-global-limit:10k + write-channel-limit:0 + read-channel-limit:0.5k + } + + connect-server { + enabled:false + check-interval:100ms + write-global-limit:0 + read-global-limit:100k + write-channel-limit:3k + read-channel-limit:3k + } + } + } + + #Zookeeper配置 + zk { + server-address="127.0.0.1:2181" + namespace=mpush + digest=mpush + local-cache-path=/ + retry { + #initial amount of time to wait between retries + baseSleepTimeMs=3s + #max number of times to retry + maxRetries=3 + #max time in ms to sleep on each retry + maxSleepMs=5s + } + connectionTimeoutMs=5s + sessionTimeoutMs=5s + } + + #Redis集群配置 + redis { + write-to-zk=true + #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 + cluster-group:[ + [ + { + host:"127.0.0.1" + port:2181 + password:ShineMoIpo + } + ] + ] + config { + maxTotal:8, + maxIdle:4, + minIdle:1, + lifo:true, + fairness:false, + maxWaitMillis:5000, + minEvictableIdleTimeMillis:300000, + softMinEvictableIdleTimeMillis:1800000, + numTestsPerEvictionRun:3, + testOnCreate:false, + testOnBorrow:false, + testOnReturn:false, + testWhileIdle:false, + timeBetweenEvictionRunsMillis:60000, + blockWhenExhausted:true, + jmxEnabled:true, + jmxNamePrefix:pool, + jmxNameBase:pool + } + } + + #HTTP代理配置 + http { + proxy-enabled=false //启用Http代理 + max-conn-per-host=5 //每个域名的最大链接数, 建议web服务nginx超时时间设长一点, 以便保持长链接 + default-read-timeout=10s //请求超时时间 + max-content-length=5m //response body 最大大小 + dns-mapping { //域名映射外网地址转内部IP + "mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] + } + } + + #线程池配置 + thread { + pool { + boss { //netty boss + min:4 + max:16 + queue-size:1000 + } + + work { //netty boss + min:8 + max:32 + queue-size:1000 + } + + event-bus { + min:4 + max:4 + queue-size:10000 //大量的online,offline, + } + + http-proxy { + min:8 + max:64 + queue-size:1000 + } + + biz { //其他业务 + min:4 + max:64 + queue-size:10 + } + + mq { //用户上下线消息, 踢人等 + min:2 + max:4 + queue-size:10000 + } + + push-callback { //消息推送 + min:2 + max:2 + queue-size:0 + } + } + } + + #系统监控配置 + monitor { + dump-dir=/tmp/logs/mpush/ + dump-stack=false //是否定时dump堆栈 + dump-period=1m //多久监控一次 + print-log=true //是否打印监控日志 + } + + #SPI扩展配置 + spi { + thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory" + dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager" + } +} +``` +11. 未完待续... From 20dac888d6806383fa7685848086d6968df7364a Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 11:07:38 +0800 Subject: [PATCH 602/890] Update README.md From 4e6c7bc08551a739cb0d5f62b4f0a6a876bebabf Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 13:07:25 +0800 Subject: [PATCH 603/890] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07e2c859..60113be0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## [详细教程](https://mpusher.github.io/docs) -[https://mpusher.github.io/docs](https://mpusher.github.io/docs) +官网:[https://mpusher.github.io](https://mpusher.github.io) ## 源码测试 1. ```git clone https://github.com/mpusher/mpush.git``` From d232ca9035eb9b86d382fb4594fae68809fb8976 Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 13:20:49 +0800 Subject: [PATCH 604/890] Update README.md From 33b380486ad0b32367ef8d9bcb2c05333963c442 Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 13:28:29 +0800 Subject: [PATCH 605/890] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 60113be0..11598f34 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ ## [详细教程](https://mpusher.github.io/docs) 官网:[https://mpusher.github.io](https://mpusher.github.io) +QQ群:114583699 ## 源码测试 1. ```git clone https://github.com/mpusher/mpush.git``` From 0056b85f01bfe18e1d40574710915d884ec4fa3c Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 13:33:27 +0800 Subject: [PATCH 606/890] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fdcd946..3d8f51f4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ ## [详细教程](https://mpusher.github.io/docs) -[https://mpusher.github.io/docs](https://mpusher.github.io/docs) +* 官网:[https://mpusher.github.io](https://mpusher.github.io) +* 文档:[https://mpusher.github.io/docs](https://mpusher.github.io/docs) +* QQ群:__114583699__ ## 源码测试 1. ```git clone https://github.com/mpusher/mpush.git``` From 82cc7549342b2fbd9448bddc37244c8f0a5588ae Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 31 Aug 2016 13:35:22 +0800 Subject: [PATCH 607/890] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d8f51f4..091a4ac8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ * 官网:[https://mpusher.github.io](https://mpusher.github.io) * 文档:[https://mpusher.github.io/docs](https://mpusher.github.io/docs) -* QQ群:__114583699__ +* QQ群:__114583699__ MPUSH开源消息推送系统 ## 源码测试 1. ```git clone https://github.com/mpusher/mpush.git``` From 409a9b4d024cbda620a46200fcf226e356d4cebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 31 Aug 2016 13:57:27 +0800 Subject: [PATCH 608/890] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=98=93=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connection/mpns/ConnClientTestMain.java | 69 ------------------- .../mpns/ConnectTestClientBoot.java | 38 ---------- 2 files changed, 107 deletions(-) delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java deleted file mode 100644 index 9452bccc..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnClientTestMain.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.test.connection.mpns; - -import com.mpush.api.service.Client; -import com.mpush.client.connect.ClientConfig; -import com.mpush.client.connect.ConnectClient; -import com.mpush.common.security.CipherBox; -import com.mpush.zk.node.ZKServerNode; - -import java.util.List; -import java.util.concurrent.locks.LockSupport; - -public class ConnClientTestMain { - - public static void main(String[] args) throws InterruptedException { - - ConnectTestClientBoot main = new ConnectTestClientBoot(); - main.start(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - - for (int i = 0; i < 1000; i++) { - String clientVersion = "1.0." + i; - String osName = "android"; - String osVersion = "1.0.1"; - String userId = "uh-" + i; - String deviceId = "test-device-id-" + i; - String cipher = ""; - byte[] clientKey = CipherBox.I.randomAESKey(); - byte[] iv = CipherBox.I.randomAESIV(); - - ClientConfig config = new ClientConfig(); - config.setClientKey(clientKey); - config.setIv(iv); - config.setClientVersion(clientVersion); - config.setDeviceId(deviceId); - config.setOsName(osName); - config.setOsVersion(osVersion); - config.setUserId(userId); - config.setCipher(cipher); - Client client = new ConnectClient(server.getIp(), server.getPort(), config); - Thread.sleep(100); - } - - LockSupport.park(); - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java b/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java deleted file mode 100644 index d927e1c6..00000000 --- a/mpush-test/src/test/java/com/mpush/test/connection/mpns/ConnectTestClientBoot.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.test.connection.mpns; - -import com.google.common.collect.Lists; -import com.mpush.zk.listener.ZKServerNodeWatcher; -import com.mpush.zk.node.ZKServerNode; - -import java.util.List; - -public class ConnectTestClientBoot { - private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); - - public void start() { - listener.beginWatch(); - } - - public List getServers() { - return Lists.newArrayList(listener.getCache().values()); - } -} From ec9a1082b56079b47c9e053274669404465a9856 Mon Sep 17 00:00:00 2001 From: ohun Date: Sun, 4 Sep 2016 10:00:58 +0800 Subject: [PATCH 609/890] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 091a4ac8..67c285a5 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ 3. 安装```Redis``` (安装配置步骤略) -4. 下载mpush server正式包[https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz](https://github.com/mpusher/mpush/releases/download/0.0.1/mpush-release-0.0.1.tar.gz) +4. 下载mpush server 最新的正式包[https://github.com/mpusher/mpush/releases](https://github.com/mpusher/mpush/releases) -5. 解压下载的tar包`tar -zvxf mpush-release-0.0.1.tar.gz`到 mpush 目录, 结构如下 +5. 解压下载的tar包`tar -zvxf mpush-release-0.0.2.tar.gz`到 mpush 目录, 结构如下 >
    >drwxrwxr-x 2 shinemo shinemo  4096 Aug 20 09:30 bin —> 启动脚本

From ceebc8f0841c6b94f9466616ff52d09deaa33bc4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Sun, 4 Sep 2016 19:30:08 +0800
Subject: [PATCH 610/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96,?=
 =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9F=A5=E8=AF=A2=E6=9F=90=E5=8F=B0=E6=9C=BA?=
 =?UTF-8?q?=E5=99=A8=E7=9A=84=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 bin/mp.sh                                           | 13 +------------
 .../java/com/mpush/common/user/UserManager.java     |  4 ++--
 pom.xml                                             |  1 -
 3 files changed, 3 insertions(+), 15 deletions(-)

diff --git a/bin/mp.sh b/bin/mp.sh
index abd8d845..2e9e8f08 100644
--- a/bin/mp.sh
+++ b/bin/mp.sh
@@ -242,18 +242,7 @@ status)
        	clientPortAddress="localhost"
     fi
     clientPort=`$GREP "^[[:space:]]*connect-server-port[^[:alpha:]]" "$MP_CFG" | sed -e 's/.*=//'`
-    STAT=`"$JAVA" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \
-             -cp "$CLASSPATH" $JVM_FLAGS org.apache.mpush.client.FourLetterWordMain \
-             $clientPortAddress $clientPort srvr 2> /dev/null    \
-          | $GREP Mode`
-    if [ "x$STAT" = "x" ]
-    then
-        echo "Error contacting service. It is probably not running."
-        exit 1
-    else
-        echo $STAT
-        exit 0
-    fi
+    telnet 127.0.0.1 3002
     ;;
 *)
     echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2
diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java
index e179fa0e..1a4e7981 100644
--- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java
+++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java
@@ -56,8 +56,8 @@ public long getOnlineUserNum() {
 
     //在线用户数量
     public long getOnlineUserNum(String publicIP) {
-        String ONLINE_KEY = RedisKey.getUserOnlineKey(publicIP);
-        Long value = RedisManager.I.zCard(ONLINE_KEY);
+        String online_key = RedisKey.getUserOnlineKey(publicIP);
+        Long value = RedisManager.I.zCard(online_key);
         return value == null ? 0 : value;
     }
 
diff --git a/pom.xml b/pom.xml
index ce2b4d76..303f7bb9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -292,7 +292,6 @@
     
 
     
-
         
             dev
             

From 53c7bf8da15b003affd15bd33f445cd7ad084e0e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 09:28:13 +0800
Subject: [PATCH 611/890] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96,?=
 =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=9F=E6=88=90RAS=E5=AF=86=E9=92=A5?=
 =?UTF-8?q?=E7=9A=84=E8=84=9A=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 bin/env-mp.cmd                                |  2 +-
 bin/mp.cmd                                    |  2 +-
 bin/mp.sh                                     |  2 +-
 bin/rsa.sh                                    | 37 ++++++++++++++++++
 conf/reference.conf                           |  5 +--
 .../main/java/com/mpush/tools/config/CC.java  |  6 +--
 .../java/com/mpush/tools/crypto/RSAUtils.java | 38 +++++++++++++++----
 .../com/mpush/tools/crypto/RSAUtilsTest.java  | 14 +------
 8 files changed, 76 insertions(+), 30 deletions(-)
 create mode 100644 bin/rsa.sh

diff --git a/bin/env-mp.cmd b/bin/env-mp.cmd
index fa39e14c..bca3e566 100644
--- a/bin/env-mp.cmd
+++ b/bin/env-mp.cmd
@@ -30,7 +30,7 @@ SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH%
 REM make it work for developers
 SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH%
 
-set MPCFG=%MPCFGDIR%\zoo.cfg
+set MPCFG=%MPCFGDIR%\mpush.cfg
 
 @REM setup java environment variables
 
diff --git a/bin/mp.cmd b/bin/mp.cmd
index 5a1b8448..56b0e445 100644
--- a/bin/mp.cmd
+++ b/bin/mp.cmd
@@ -19,6 +19,6 @@ call "%~dp0env-mp.cmd"
 
 set MPMAIN="-jar ../boot.jar"
 echo on
-call %JAVA% "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% "%MPCFG%" %*
+call %JAVA% "-Dmp.conf=%MPCFG%" "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% %*
 
 endlocal
diff --git a/bin/mp.sh b/bin/mp.sh
index 2e9e8f08..589412bc 100644
--- a/bin/mp.sh
+++ b/bin/mp.sh
@@ -204,7 +204,7 @@ stop)
           break
         fi
         if [ $SLEEP -gt 0 ]; then
-          echo "waiting ... $SLEEP_COUNT"
+          echo "stopping ... $SLEEP_COUNT"
           sleep 1
         fi
         if [ $SLEEP -eq 0 ]; then
diff --git a/bin/rsa.sh b/bin/rsa.sh
new file mode 100644
index 00000000..472ddbe6
--- /dev/null
+++ b/bin/rsa.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# use POSTIX interface, symlink is followed automatically
+MP_BIN="${BASH_SOURCE-$0}"
+MP_BIN="$(dirname "${MP_BIN}")"
+MP_BIN_DIR="$(cd "${MP_BIN}"; pwd)"
+
+if [ -e "$MP_BIN/../libexec/env-mp.sh" ]; then
+  . "$MP_BIN_DIR/../libexec/env-mp.sh"
+else
+  . "$MP_BIN_DIR/env-mp.sh"
+fi
+
+if [ $1 -gt 1024 ] ;then
+    echo "use rsa key size $1"
+    keySize = $1
+else
+    echo "use rsa key size 1024"
+    keySize = 1024
+fi
+
+"$JAVA" -cp "$CLASSPATH" com.mpush.tools.crypto.RSAUtils $keySize
\ No newline at end of file
diff --git a/conf/reference.conf b/conf/reference.conf
index 2c26b35a..dcc9c5e6 100644
--- a/conf/reference.conf
+++ b/conf/reference.conf
@@ -32,7 +32,6 @@ mp {
         private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA="
         public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB"
         aes-key-length=16 //AES key 长度
-        ras-key-length=1024 //RSA key 长度
     }
 
     #网络配置
@@ -93,9 +92,9 @@ mp {
 
     #Redis集群配置
     redis {
-        write-to-zk=true
+        write-to-zk=false
         #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器
-        cluster-group:[["127.0.0.1:6379:your password","127.0.0.1:6379:your password"]]//格式ip:port:password,密码可以不设置ip:port
+        cluster-group:[]//[["127.0.0.1:6379:your password"]]格式ip:port:password,密码可以不设置ip:port
         config {
             maxTotal:8,
             maxIdle:4,
diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java
index 01f3ab4f..74ad3683 100644
--- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java
+++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java
@@ -48,8 +48,8 @@ public interface CC {
     Config cfg = load();
 
     static Config load() {
-        Config config = ConfigFactory.load();
-        String custom_conf = "mp.conf";//值来自jvm启动参数指定
+        Config config = ConfigFactory.load();//扫描加载所有可用的配置文件
+        String custom_conf = "mp.conf";//加载自定义配置, 值来自jvm启动参数指定-Dmp.conf
         if (config.hasPath(custom_conf)) {
             File file = new File(config.getString(custom_conf));
             if (file.exists()) {
@@ -144,8 +144,6 @@ interface security {
 
             String private_key = cfg.getString("private-key");
 
-            int ras_key_length = cfg.getInt("ras-key-length");
-
         }
 
         interface thread {
diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java
index 885aada3..24852f13 100644
--- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java
+++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java
@@ -49,7 +49,7 @@ public final class RSAUtils {
     /**
      * 密钥位数
      */
-    private static final int RAS_KEY_SIZE = 1024;
+    public static final int RAS_KEY_SIZE = 1024;
 
     /**
      * 加密算法RSA
@@ -82,10 +82,10 @@ public final class RSAUtils {
      *
      * @return 公钥和私钥
      */
-    public static Pair genKeyPair() {
+    public static Pair genKeyPair(int rsaKeySize) {
         try {
             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
-            keyPairGen.initialize(RAS_KEY_SIZE);
+            keyPairGen.initialize(rsaKeySize);
             KeyPair keyPair = keyPairGen.generateKeyPair();
             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
@@ -348,8 +348,8 @@ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws
         return doFinal(cipher, data, MAX_ENCRYPT_BLOCK);
     }
 
-    public static void main(String[] args) throws Exception {
-        Pair pair = RSAUtils.genKeyPair();
+    private static void test() {
+        Pair pair = RSAUtils.genKeyPair(RAS_KEY_SIZE);
         //生成公钥和私钥
         RSAPublicKey publicKey = pair.key;
         RSAPrivateKey privateKey = pair.value;
@@ -367,8 +367,6 @@ public static void main(String[] args) throws Exception {
         RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent);
         System.out.println("privateKey=" + priKey);
         System.out.println("publicKey=" + pubKey);
-        System.out.println("privateKey=" + priKey);
-        System.out.println("publicKey=" + pubKey);
         //加密后的密文
         byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey);
         System.out.println("密文:" + new String(mi, Constants.UTF_8));
@@ -376,4 +374,30 @@ public static void main(String[] args) throws Exception {
         ming = RSAUtils.decryptByPrivateKey(mi, priKey);
         System.out.println("解密:" + new String(ming, Constants.UTF_8));
     }
+
+    public static void main(String[] args) throws Exception {
+        int keySize = RAS_KEY_SIZE;
+        if (args.length > 0) keySize = Integer.parseInt(args[0]);
+        if (keySize < RAS_KEY_SIZE) keySize = RAS_KEY_SIZE;
+        Pair pair = RSAUtils.genKeyPair(keySize);
+        //生成公钥和私钥
+        RSAPublicKey publicKey = pair.key;
+        RSAPrivateKey privateKey = pair.value;
+
+        System.out.println("key generate success!");
+
+        System.out.println("privateKey=" + RSAUtils.encodeBase64(privateKey));
+        System.out.println("publicKey=" + RSAUtils.encodeBase64(publicKey));
+
+        //明文
+        byte[] ming = "这是一段测试文字。。。。".getBytes(Constants.UTF_8);
+        System.out.println("明文:" + new String(ming, Constants.UTF_8));
+
+        //加密后的密文
+        byte[] mi = RSAUtils.encryptByPublicKey(ming, publicKey);
+        System.out.println("密文:" + new String(mi, Constants.UTF_8));
+        //解密后的明文
+        ming = RSAUtils.decryptByPrivateKey(mi, privateKey);
+        System.out.println("解密:" + new String(ming, Constants.UTF_8));
+    }
 }
\ No newline at end of file
diff --git a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java
index d723f7c6..6aa9a241 100644
--- a/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java
+++ b/mpush-tools/src/test/java/com/mpush/tools/crypto/RSAUtilsTest.java
@@ -36,21 +36,14 @@ public class RSAUtilsTest {
     String publicKey;
     String privateKey;
 
-    String publicKey2;
-    String privateKey2;
-
     @Before
     public void setUp() throws Exception {
         try {
-            Pair pair = RSAUtils.genKeyPair();
+            Pair pair = RSAUtils.genKeyPair(RSAUtils.RAS_KEY_SIZE);
             publicKey = RSAUtils.encodeBase64(pair.key);
             privateKey = RSAUtils.encodeBase64(pair.value);
             System.out.println("公钥: \n\r" + publicKey);
             System.out.println("私钥: \n\r" + privateKey);
-
-            pair = RSAUtils.genKeyPair();
-            publicKey2 = RSAUtils.encodeBase64(pair.key);
-            privateKey2 = RSAUtils.encodeBase64(pair.value);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -65,11 +58,6 @@ public void testGetKeys() throws Exception {
         byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
         String target = new String(decodedData);
         System.out.println("解密后:\n" + target);
-
-        decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey2);
-        target = new String(decodedData);
-        System.out.println("解密后2:\n" + target);
-
     }
 
     @Test

From 95f4670fa37a8529c82b3ef0962155f4eea87725 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 09:32:57 +0800
Subject: [PATCH 612/890] =?UTF-8?q?redis=20=E9=9B=86=E7=BE=A4=E9=85=8D?=
 =?UTF-8?q?=E7=BD=AE=E9=A1=B9=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mpush-test/src/test/resources/application.conf | 1 -
 1 file changed, 1 deletion(-)

diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf
index 4f1acc4c..3f221c6a 100644
--- a/mpush-test/src/test/resources/application.conf
+++ b/mpush-test/src/test/resources/application.conf
@@ -6,7 +6,6 @@ mp.zk.namespace=mpush
 mp.zk.server-address="10.0.10.47:2181"
 mp.core.compress-threshold=10k
 mp.redis {
-    write-to-zk=true
     #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器
     cluster-group:[["10.0.10.53:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port
 }
\ No newline at end of file

From 7b72fadcce9ae2c904c4f693043cf4c1ccf0a489 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 09:57:37 +0800
Subject: [PATCH 613/890] =?UTF-8?q?ConcurrentHashMapV8=E5=88=87=E6=8D=A2?=
 =?UTF-8?q?=E5=88=B0jdk=E8=87=AA=E5=B8=A6=E7=9A=84=E5=AE=9E=E7=8E=B0,?=
 =?UTF-8?q?=E5=9B=A0=E4=B8=BA=E7=9B=AE=E5=89=8DJDK=E6=9C=80=E4=BD=8E?=
 =?UTF-8?q?=E8=A6=81=E6=B1=82=E6=98=AF1.8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../java/com/mpush/client/push/PushRequestBus.java   |  3 +--
 .../com/mpush/core/router/LocalRouterManager.java    |  4 ++--
 .../mpush/core/server/ServerConnectionManager.java   | 12 +++++-------
 3 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java
index fbb846c0..45ebbdde 100644
--- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java
+++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java
@@ -22,7 +22,6 @@
 import com.mpush.api.push.PushException;
 import com.mpush.tools.thread.PoolThreadFactory;
 import com.mpush.tools.thread.pool.ThreadPoolManager;
-import io.netty.util.internal.chmv8.ConcurrentHashMapV8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -39,7 +38,7 @@
 public class PushRequestBus {
     public static final PushRequestBus I = new PushRequestBus();
     private final Logger logger = LoggerFactory.getLogger(PushRequestBus.class);
-    private final Map reqQueue = new ConcurrentHashMapV8<>(1024);
+    private final Map reqQueue = new ConcurrentHashMap<>(1024);
     private final Executor executor = ThreadPoolManager.I.getPushCallbackExecutor();
     private final ScheduledExecutorService scheduledExecutor;
 
diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java
index 57de36a4..352e3e38 100644
--- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java
+++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java
@@ -28,11 +28,11 @@
 import com.mpush.api.router.RouterManager;
 import com.mpush.tools.event.EventBus;
 import com.mpush.tools.event.EventConsumer;
-import io.netty.util.internal.chmv8.ConcurrentHashMapV8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Created by ohun on 2015/12/23.
@@ -46,7 +46,7 @@ public final class LocalRouterManager extends EventConsumer implements RouterMan
     /**
      * 本地路由表
      */
-    private final Map> routers = new ConcurrentHashMapV8<>();
+    private final Map> routers = new ConcurrentHashMap<>();
 
     @Override
     public LocalRouter register(String userId, LocalRouter router) {
diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java
index f18d9f91..0664a103 100644
--- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java
+++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java
@@ -33,9 +33,9 @@
 import io.netty.util.Timeout;
 import io.netty.util.Timer;
 import io.netty.util.TimerTask;
-import io.netty.util.internal.chmv8.ConcurrentHashMapV8;
 
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
 
@@ -46,7 +46,7 @@
  * @author ohun@live.cn
  */
 public final class ServerConnectionManager implements ConnectionManager {
-    private final ConcurrentMap connections = new ConcurrentHashMapV8<>();
+    private final ConcurrentMap connections = new ConcurrentHashMap<>();
 
     private Timer timer;
 
@@ -62,9 +62,7 @@ public void init() {
     @Override
     public void destroy() {
         if (timer != null) timer.stop();
-        for (Connection connection : connections.values()) {
-            connection.close();
-        }
+        connections.values().forEach(Connection::close);
         connections.clear();
     }
 
@@ -103,11 +101,11 @@ private class HeartbeatCheckTask implements TimerTask {
         private int timeoutTimes = 0;
         private final Connection connection;
 
-        public HeartbeatCheckTask(Connection connection) {
+        HeartbeatCheckTask(Connection connection) {
             this.connection = connection;
         }
 
-        public void startTimeout() {
+        void startTimeout() {
             int timeout = connection.getSessionContext().heartbeat;
             timer.newTimeout(this, timeout > 0 ? timeout : CC.mp.core.min_heartbeat, TimeUnit.MILLISECONDS);
         }

From 5e0f5f54ee5e316ac7062b835e9b2fb0313e9a58 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 09:58:14 +0800
Subject: [PATCH 614/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../main/java/com/mpush/cache/redis/manager/RedisManager.java   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java
index a145863a..b5d2b060 100644
--- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java
+++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java
@@ -40,7 +40,7 @@ public final class RedisManager {
     private final RedisClusterManager clusterManager = ZKRedisClusterManager.I;
 
     public void init() {
-        ZKRedisClusterManager.I.init();
+        clusterManager.init();
         test(clusterManager.getGroupList());
     }
 

From e5beefd0fccbe215e1520eac49a59fad6eb3a54d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 12:05:42 +0800
Subject: [PATCH 615/890] ADD ACK

---
 .../java/com/mpush/api/protocol/Command.java  |  3 +
 .../java/com/mpush/api/protocol/Packet.java   |  5 +-
 .../java/com/mpush/api/push/AckModel.java     | 38 +++++++++
 .../java/com/mpush/api/push/PushCallback.java | 14 ++++
 .../java/com/mpush/api/push/PushSender.java   | 19 ++---
 .../redis/manager/ZKRedisClusterManager.java  | 17 ++--
 .../connect/ConnClientChannelHandler.java     |  6 ++
 .../com/mpush/client/push/PushClient.java     | 24 +++++-
 .../com/mpush/client/push/PushRequest.java    | 14 +++-
 .../main/java/com/mpush/common/ErrorCode.java |  1 +
 .../com/mpush/common/message/AckMessage.java  | 56 +++++++++++++
 .../com/mpush/common/message/PushMessage.java |  7 ++
 .../message/gateway/GatewayPushMessage.java   |  4 +
 .../java/com/mpush/core/ack/AckCallback.java  | 31 +++++++
 .../java/com/mpush/core/ack/AckContext.java   | 84 +++++++++++++++++++
 .../com/mpush/core/ack/AckMessageQueue.java   | 65 ++++++++++++++
 .../com/mpush/core/handler/AckHandler.java    | 61 ++++++++++++++
 .../core/handler/FastConnectHandler.java      |  2 +-
 .../core/handler/GatewayPushHandler.java      | 62 +++++++++++---
 .../mpush/core/server/ConnectionServer.java   |  1 +
 .../com/mpush/netty/codec/PacketDecoder.java  |  3 +-
 .../mpush/test/push/PushClientTestMain.java   | 43 ++++++----
 .../com/mpush/tools/thread/ThreadNames.java   |  1 +
 23 files changed, 500 insertions(+), 61 deletions(-)
 create mode 100644 mpush-api/src/main/java/com/mpush/api/push/AckModel.java
 create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushCallback.java
 create mode 100644 mpush-common/src/main/java/com/mpush/common/message/AckMessage.java
 create mode 100644 mpush-core/src/main/java/com/mpush/core/ack/AckCallback.java
 create mode 100644 mpush-core/src/main/java/com/mpush/core/ack/AckContext.java
 create mode 100644 mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java
 create mode 100644 mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java

diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java
index ad2e739a..6cce4164 100644
--- a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java
+++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java
@@ -21,6 +21,8 @@
 
 /**
  * Created by ohun on 2015/12/22.
+ *
+ * @author ohun@live.cn
  */
 public enum Command {
     HEARTBEAT(1),
@@ -45,6 +47,7 @@ public enum Command {
     GATEWAY_CHAT(20),
     GROUP(21),
     GATEWAY_GROUP(22),
+    ACK(23),
     UNKNOWN(-1);
 
     Command(int cmd) {
diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java
index d9921802..0707a8f9 100644
--- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java
+++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java
@@ -30,12 +30,15 @@
  */
 public final class Packet {
     public static final int HEADER_LEN = 13;
+
     public static final byte FLAG_CRYPTO = 0x01;
     public static final byte FLAG_COMPRESS = 0x02;
+    public static final byte FLAG_ACK = 0x04;
+    public static final byte FLAG_AUTO_ACK = 0x08;
 
     public static final byte HB_PACKET_BYTE = -33;
     public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE};
-    public static final Packet HB_PACKE = new Packet(Command.HEARTBEAT);
+    public static final Packet HB_PACKET = new Packet(Command.HEARTBEAT);
 
     public byte cmd; //命令
     public short cc; //校验码 暂时没有用到
diff --git a/mpush-api/src/main/java/com/mpush/api/push/AckModel.java b/mpush-api/src/main/java/com/mpush/api/push/AckModel.java
new file mode 100644
index 00000000..94592259
--- /dev/null
+++ b/mpush-api/src/main/java/com/mpush/api/push/AckModel.java
@@ -0,0 +1,38 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.api.push;
+
+import com.mpush.api.protocol.Packet;
+
+/**
+ * Created by ohun on 16/9/6.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public enum AckModel {
+    NO_ACK((byte) 0),//不需要ACK
+    AUTO_ACK(Packet.FLAG_AUTO_ACK),//客户端收到消息后自动确认消息
+    BIZ_ACK(Packet.FLAG_ACK);//由客户端业务自己确认消息是否到达
+    public final byte flag;
+
+    AckModel(byte flag) {
+        this.flag = flag;
+    }
+}
diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java
new file mode 100644
index 00000000..4eb56f9b
--- /dev/null
+++ b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java
@@ -0,0 +1,14 @@
+package com.mpush.api.push;
+
+import com.mpush.api.router.ClientLocation;
+
+public interface PushCallback {
+
+    void onSuccess(String userId, ClientLocation location);
+
+    void onFailure(String userId, ClientLocation location);
+
+    void onOffline(String userId, ClientLocation location);
+
+    void onTimeout(String userId, ClientLocation location);
+}
\ No newline at end of file
diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java
index 3c20996e..e77dc15e 100644
--- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java
+++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java
@@ -19,7 +19,7 @@
 
 package com.mpush.api.push;
 
-import com.mpush.api.router.ClientLocation;
+import com.mpush.api.protocol.Packet;
 import com.mpush.api.service.Service;
 import com.mpush.api.spi.SpiLoader;
 import com.mpush.api.spi.client.PusherFactory;
@@ -38,21 +38,16 @@ static PushSender create() {
         return SpiLoader.load(PusherFactory.class).get();
     }
 
-    void send(String content, Collection userIds, Callback callback);
+    void send(String content, Collection userIds, PushCallback callback);
 
-    FutureTask send(String content, String userId, Callback callback);
+    FutureTask send(String content, String userId, PushCallback callback);
 
-    void send(byte[] content, Collection userIds, Callback callback);
+    void send(byte[] content, Collection userIds, PushCallback callback);
 
-    FutureTask send(byte[] content, String userId, Callback callback);
+    FutureTask send(byte[] content, String userId, PushCallback callback);
 
-    interface Callback {
-        void onSuccess(String userId, ClientLocation location);
+    void send(byte[] content, Collection userIds, AckModel ackModel, PushCallback callback);
 
-        void onFailure(String userId, ClientLocation location);
+    FutureTask send(byte[] content, String userId, AckModel ackModel, PushCallback callback);
 
-        void onOffline(String userId, ClientLocation location);
-
-        void onTimeout(String userId, ClientLocation location);
-    }
 }
diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
index 54abe851..413e59ca 100644
--- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
+++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
@@ -54,14 +54,9 @@ public void init() {
         Logs.Console.error("begin init redis cluster");
         if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running.");
         List groupList = CC.mp.redis.cluster_group;
+
         if (groupList.size() > 0) {
-            if (CC.mp.redis.write_to_zk) {
-                register(groupList);
-            } else if (!ZKClient.I.isExisted(REDIS_SERVER.getRootPath())) {
-                register(groupList);
-            } else if (Strings.isNullOrEmpty(ZKClient.I.get(REDIS_SERVER.getRootPath()))) {
-                register(groupList);
-            }
+            register(groupList);
         }
 
         ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher();
@@ -120,8 +115,12 @@ public List hashSet(String key) {
 
     private void register(List groupList) {
         String data = Jsons.toJson(groupList);
-        ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data);
-        Logs.Console.error("register redis server group success, group=" + data);
+        if (CC.mp.redis.write_to_zk //强制刷新ZK
+                || !ZKClient.I.isExisted(REDIS_SERVER.getRootPath())//redis节点不存在
+                || !ZKClient.I.get(REDIS_SERVER.getRootPath()).equals(data)) {//数据有变更
+            ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data);
+            Logs.Console.error("register redis server group success, group=" + data);
+        }
     }
 
     public void addGroup(RedisGroup group) {
diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java
index 83157123..c1e3a6d4 100644
--- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java
+++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java
@@ -109,6 +109,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
             } else if (command == Command.PUSH) {
                 PushMessage message = new PushMessage(packet, connection);
                 LOGGER.warn(">>> receive an push message, content=" + new String(message.content, Constants.UTF_8));
+
+                if (message.needAck()) {
+                    AckMessage.from(message).sendRaw();
+                    LOGGER.warn(">>> send ack success for sessionId={}", message.getSessionId());
+                }
+
             } else if (command == Command.HEARTBEAT) {
                 LOGGER.warn(">>> receive a heartbeat pong...");
             } else {
diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java
index 9b69df55..f661b755 100644
--- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java
+++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java
@@ -21,6 +21,8 @@
 
 import com.mpush.api.Constants;
 import com.mpush.api.connection.Connection;
+import com.mpush.api.push.AckModel;
+import com.mpush.api.push.PushCallback;
 import com.mpush.api.push.PushSender;
 import com.mpush.api.service.BaseService;
 import com.mpush.api.service.Listener;
@@ -43,28 +45,41 @@
     private final GatewayClientFactory factory = GatewayClientFactory.I;
     private final ConnectionRouterManager routerManager = ConnectionRouterManager.I;
 
-    public void send(String content, Collection userIds, Callback callback) {
+    public void send(String content, Collection userIds, PushCallback callback) {
         send(content.getBytes(Constants.UTF_8), userIds, callback);
     }
 
     @Override
-    public FutureTask send(String content, String userId, Callback callback) {
+    public FutureTask send(String content, String userId, PushCallback callback) {
         return send(content.getBytes(Constants.UTF_8), userId, callback);
     }
 
     @Override
-    public void send(byte[] content, Collection userIds, Callback callback) {
+    public void send(byte[] content, Collection userIds, PushCallback callback) {
         for (String userId : userIds) {
             send(content, userId, callback);
         }
     }
 
     @Override
-    public FutureTask send(byte[] content, String userId, Callback callback) {
+    public FutureTask send(byte[] content, String userId, PushCallback callback) {
+        return send(content, userId, AckModel.NO_ACK, callback);
+    }
+
+    @Override
+    public void send(byte[] content, Collection userIds, AckModel ackModel, PushCallback callback) {
+        for (String userId : userIds) {
+            send(content, userId, ackModel, callback);
+        }
+    }
+
+    @Override
+    public FutureTask send(byte[] content, String userId, AckModel ackModel, PushCallback callback) {
         Set routers = routerManager.lookupAll(userId);
         if (routers == null || routers.isEmpty()) {
             return PushRequest
                     .build(this)
+                    .setAckModel(ackModel)
                     .setCallback(callback)
                     .setUserId(userId)
                     .setContent(content)
@@ -76,6 +91,7 @@ public FutureTask send(byte[] content, String userId, Callback callback
         for (RemoteRouter router : routers) {
             task = PushRequest
                     .build(this)
+                    .setAckModel(ackModel)
                     .setCallback(callback)
                     .setUserId(userId)
                     .setContent(content)
diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java
index 3df50970..95c50edc 100644
--- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java
+++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java
@@ -20,6 +20,8 @@
 package com.mpush.client.push;
 
 import com.mpush.api.connection.Connection;
+import com.mpush.api.push.AckModel;
+import com.mpush.api.push.PushCallback;
 import com.mpush.api.push.PushSender;
 import com.mpush.api.router.ClientLocation;
 import com.mpush.common.message.gateway.GatewayPushMessage;
@@ -52,7 +54,8 @@ private enum Status {init, success, failure, offline, timeout}
 
     private final PushClient client;
 
-    private PushSender.Callback callback;
+    private AckModel ackModel;
+    private PushCallback callback;
     private String userId;
     private byte[] content;
     private long timeout;
@@ -83,6 +86,8 @@ private void sendToConnServer(RemoteRouter router) {
         timeLine.addTimePoint("send-to-gateway-begin");
 
         GatewayPushMessage pushMessage = new GatewayPushMessage(userId, location.getClientType(), content, gatewayConn);
+        pushMessage.getPacket().addFlag(ackModel.flag);
+
         timeLine.addTimePoint("put-request-bus");
         future = PushRequestBus.I.put(pushMessage.getSessionId(), this);
 
@@ -183,7 +188,7 @@ public static PushRequest build(PushClient client) {
         return new PushRequest(client);
     }
 
-    public PushRequest setCallback(PushSender.Callback callback) {
+    public PushRequest setCallback(PushCallback callback) {
         this.callback = callback;
         return this;
     }
@@ -203,6 +208,11 @@ public PushRequest setTimeout(long timeout) {
         return this;
     }
 
+    public PushRequest setAckModel(AckModel ackModel) {
+        this.ackModel = ackModel;
+        return this;
+    }
+
     @Override
     public String toString() {
         return "PushRequest{" +
diff --git a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java
index 6bcc470f..81ef719b 100644
--- a/mpush-common/src/main/java/com/mpush/common/ErrorCode.java
+++ b/mpush-common/src/main/java/com/mpush/common/ErrorCode.java
@@ -28,6 +28,7 @@ public enum ErrorCode {
     OFFLINE(1, "user offline"),
     PUSH_CLIENT_FAILURE(2, "push to client failure"),
     ROUTER_CHANGE(3, "router change"),
+    ACK_TIMEOUT(4, "ack timeout"),
     DISPATCH_ERROR(100, "handle message error"),
     UNSUPPORTED_CMD(101, "unsupported command"),
     UNKNOWN(-1, "unknown");
diff --git a/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java b/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java
new file mode 100644
index 00000000..d5046e67
--- /dev/null
+++ b/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java
@@ -0,0 +1,56 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.common.message;
+
+import com.mpush.api.connection.Connection;
+import com.mpush.api.protocol.Command;
+import com.mpush.api.protocol.Packet;
+import io.netty.buffer.ByteBuf;
+
+/**
+ * Created by ohun on 16/9/5.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public class AckMessage extends ByteBufMessage {
+
+    public AckMessage(Packet packet, Connection connection) {
+        super(packet, connection);
+    }
+
+    @Override
+    public void decode(ByteBuf body) {
+    }
+
+    @Override
+    public void encode(ByteBuf body) {
+    }
+
+    public static AckMessage from(BaseMessage src) {
+        return new AckMessage(new Packet(Command.ACK, src.getSessionId()), src.connection);
+    }
+
+    @Override
+    public String toString() {
+        return "AckMessage{" +
+                "packet=" + packet +
+                '}';
+    }
+}
diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java
index b93a252b..c7fbb550 100644
--- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java
+++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java
@@ -22,6 +22,7 @@
 import com.mpush.api.Constants;
 import com.mpush.api.connection.Connection;
 import com.mpush.api.protocol.Packet;
+import com.mpush.common.message.gateway.GatewayPushMessage;
 
 import java.util.Arrays;
 
@@ -55,6 +56,12 @@ public byte[] encode() {
         return content;
     }
 
+
+    public boolean needAck() {
+        return packet.hasFlag(Packet.FLAG_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
+    }
+
+
     @Override
     public String toString() {
         return "PushMessage{" +
diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
index ead4f160..afb65dfe 100644
--- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
+++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
@@ -62,6 +62,10 @@ public void encode(ByteBuf body) {
         encodeBytes(body, content);
     }
 
+    public boolean needAck() {
+        return packet.hasFlag(Packet.FLAG_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
+    }
+
     @Override
     public void send() {
         super.sendRaw();
diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckCallback.java b/mpush-core/src/main/java/com/mpush/core/ack/AckCallback.java
new file mode 100644
index 00000000..0c9ef045
--- /dev/null
+++ b/mpush-core/src/main/java/com/mpush/core/ack/AckCallback.java
@@ -0,0 +1,31 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.core.ack;
+
+/**
+ * Created by ohun on 16/9/6.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public interface AckCallback {
+    void onSuccess(AckContext context);
+
+    void onTimeout(AckContext context);
+}
diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java
new file mode 100644
index 00000000..e175f024
--- /dev/null
+++ b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java
@@ -0,0 +1,84 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.core.ack;
+
+import com.mpush.common.message.BaseMessage;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Created by ohun on 16/9/5.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public final class AckContext implements Runnable {
+    private final AtomicBoolean done = new AtomicBoolean(false);
+
+    public final int gatewayMessageId;
+    public final byte cmd;
+    private AckCallback callback;
+    /*package*/ int pushMessageId;
+
+    public AckContext(int gatewayMessageId, byte cmd) {
+        this.gatewayMessageId = gatewayMessageId;
+        this.cmd = cmd;
+    }
+
+    public static AckContext from(BaseMessage message) {
+        return new AckContext(message.getSessionId(), message.getPacket().cmd);
+    }
+
+    public boolean tryDone() {
+        return done.compareAndSet(false, true);
+    }
+
+    public AckContext setCallback(AckCallback callback) {
+        this.callback = callback;
+        return this;
+    }
+
+    public void success() {
+        if (tryDone()) {
+            callback.onSuccess(this);
+        }
+    }
+
+    public void timeout() {
+        AckContext context = AckMessageQueue.I.getAndRemove(pushMessageId);
+        if (context != null && tryDone()) {
+            callback.onTimeout(this);
+        }
+    }
+
+    @Override
+    public String toString() {
+        return "AckContext{" +
+                "gatewayMessageId=" + gatewayMessageId +
+                "pushMessageId=" + pushMessageId +
+                '}';
+    }
+
+    @Override
+    public void run() {
+        if (tryDone()) {
+            callback.onTimeout(this);
+        }
+    }
+}
diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java
new file mode 100644
index 00000000..0ca7e4b1
--- /dev/null
+++ b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java
@@ -0,0 +1,65 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.core.ack;
+
+import com.mpush.api.protocol.Packet;
+import com.mpush.api.push.PushException;
+import com.mpush.common.ErrorCode;
+import com.mpush.common.message.ErrorMessage;
+import com.mpush.common.message.OkMessage;
+import com.mpush.tools.log.Logs;
+import com.mpush.tools.thread.PoolThreadFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.*;
+
+import static com.mpush.api.protocol.Command.ERROR;
+import static com.mpush.api.protocol.Command.OK;
+import static com.mpush.tools.thread.ThreadNames.T_ARK_REQ_TIMER;
+
+/**
+ * Created by ohun on 16/9/5.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public final class AckMessageQueue {
+    private final Logger logger = LoggerFactory.getLogger(AckMessageQueue.class);
+    public static final AckMessageQueue I = new AckMessageQueue();
+    private final ConcurrentMap queue = new ConcurrentHashMap<>();
+    private final ScheduledExecutorService scheduledExecutor;
+
+    private AckMessageQueue() {
+        scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_ARK_REQ_TIMER), (r, e) -> {
+            logger.error("one ack context was rejected, context=" + r);
+        });
+    }
+
+    public void put(int sessionId, AckContext context) {
+        queue.put(sessionId, context);
+        context.pushMessageId = sessionId;
+        scheduledExecutor.schedule(context, 3, TimeUnit.SECONDS);
+    }
+
+    public AckContext getAndRemove(int sessionId) {
+        return queue.remove(sessionId);
+    }
+
+}
diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java
new file mode 100644
index 00000000..bd06a0d2
--- /dev/null
+++ b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java
@@ -0,0 +1,61 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.core.handler;
+
+import com.mpush.api.connection.Connection;
+import com.mpush.api.protocol.Packet;
+import com.mpush.common.handler.BaseMessageHandler;
+import com.mpush.common.message.AckMessage;
+import com.mpush.common.message.OkMessage;
+import com.mpush.core.ack.AckContext;
+import com.mpush.core.ack.AckMessageQueue;
+import com.mpush.tools.log.Logs;
+
+import static com.mpush.api.protocol.Command.OK;
+
+/**
+ * Created by ohun on 16/9/5.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public class AckHandler extends BaseMessageHandler {
+
+    @Override
+    public AckMessage decode(Packet packet, Connection connection) {
+        return new AckMessage(packet, connection);
+    }
+
+    @Override
+    public void handle(AckMessage message) {
+        AckContext context = AckMessageQueue.I.getAndRemove(message.getSessionId());
+        if (context == null) {
+            Logs.PUSH.info("receive client ack, but timeout message={}", message);
+            return;
+        }
+
+        Connection connection = context.connection;
+        if (!connection.isConnected()) {
+            Logs.PUSH.info("receive client ack, gateway connection is closed, message={}, context={}", message, context);
+            return;
+        }
+
+        context.success();
+    }
+}
diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java
index bdf0abba..3e92d0c3 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java
@@ -50,7 +50,7 @@ public void handle(FastConnectMessage message) {
         if (session == null) {
             //1.没查到说明session已经失效了
             ErrorMessage.from(message).setReason("session expired").send();
-            Logs.Conn.info("fast connect failure, session is expired, sessionId={}, deviceId={}"
+            Logs.Conn.info("fast connect failure, session is expired, gatewayMessageId={}, deviceId={}"
                     , message.sessionId, message.deviceId);
         } else if (!session.context.deviceId.equals(message.deviceId)) {
             //2.非法的设备, 当前设备不是上次生成session时的设备
diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
index dc7cfe78..ea75ae00 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
@@ -21,17 +21,23 @@
 
 import com.mpush.api.connection.Connection;
 import com.mpush.api.protocol.Packet;
+import com.mpush.common.ErrorCode;
 import com.mpush.common.handler.BaseMessageHandler;
 import com.mpush.common.message.ErrorMessage;
 import com.mpush.common.message.OkMessage;
 import com.mpush.common.message.PushMessage;
 import com.mpush.common.message.gateway.GatewayPushMessage;
 import com.mpush.common.router.RemoteRouter;
+import com.mpush.core.ack.AckCallback;
+import com.mpush.core.ack.AckContext;
+import com.mpush.core.ack.AckMessageQueue;
 import com.mpush.core.router.LocalRouter;
 import com.mpush.core.router.RouterCenter;
 import com.mpush.tools.Utils;
 import com.mpush.tools.log.Logs;
 
+import static com.mpush.api.protocol.Command.ERROR;
+import static com.mpush.api.protocol.Command.OK;
 import static com.mpush.common.ErrorCode.*;
 
 /**
@@ -80,8 +86,8 @@ public void handle(GatewayPushMessage message) {
      */
     private boolean checkLocal(final GatewayPushMessage message) {
         String userId = message.userId;
-        int deviceId = message.clientType;
-        LocalRouter router = RouterCenter.I.getLocalRouterManager().lookup(userId, deviceId);
+        int clientType = message.clientType;
+        LocalRouter router = RouterCenter.I.getLocalRouterManager().lookup(userId, clientType);
 
         //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器
         if (router == null) return false;
@@ -94,24 +100,29 @@ private boolean checkLocal(final GatewayPushMessage message) {
             Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection);
 
             //删除已经失效的本地路由
-            RouterCenter.I.getLocalRouterManager().unRegister(userId, deviceId);
+            RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType);
 
             return false;
         }
 
         //3.链接可用,直接下发消息到手机客户端
         PushMessage pushMessage = new PushMessage(message.content, connection);
+        pushMessage.getPacket().flags = message.getPacket().flags;
 
         pushMessage.send(future -> {
-            if (future.isSuccess()) {
-                //推送成功
-                OkMessage.from(message).setData(userId + ',' + deviceId).send();
+            if (future.isSuccess()) {//推送成功
+
+                if (message.needAck()) {//需要客户端ACK
+                    AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message));
+                } else {
+                    OkMessage.from(message).setData(userId + ',' + clientType).sendRaw();
+                }
 
                 Logs.PUSH.info("gateway push message to client success, message={}", message);
 
-            } else {
-                //推送失败
-                ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + deviceId).send();
+            } else {//推送失败
+
+                ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw();
 
                 Logs.PUSH.info("gateway push message to client failure, message={}", message);
             }
@@ -135,7 +146,7 @@ private void checkRemote(GatewayPushMessage message) {
         // 1.如果远程路由信息也不存在, 说明用户此时不在线,
         if (router == null) {
 
-            ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send();
+            ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw();
 
             Logs.PUSH.info("gateway push, router not exists user offline, message={}", message);
 
@@ -145,7 +156,7 @@ private void checkRemote(GatewayPushMessage message) {
         //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存
         if (Utils.getLocalIp().equals(router.getRouteValue().getHost())) {
 
-            ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).send();
+            ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw();
 
             //删除失效的远程缓存
             RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType);
@@ -156,9 +167,36 @@ private void checkRemote(GatewayPushMessage message) {
         }
 
         //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推
-        ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).send();
+        ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).sendRaw();
 
         Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, router);
 
     }
+
+
+    private AckContext buildAckContext(GatewayPushMessage message) {
+        Connection gatewayConnection = message.getConnection();
+        String userId = message.userId;
+        int clientType = message.clientType;
+
+        return AckContext.from(message)
+                .setCallback(new AckCallback() {
+                    @Override
+                    public void onSuccess(AckContext ctx) {
+                        OkMessage okMessage = new OkMessage(ctx.cmd, new Packet(OK, ctx.gatewayMessageId), gatewayConnection);
+                        okMessage.setData(userId + ',' + clientType);
+                        okMessage.sendRaw();
+                        Logs.PUSH.info("receive client ack and response gateway client success, context={}", ctx);
+                    }
+
+                    @Override
+                    public void onTimeout(AckContext ctx) {
+                        ErrorMessage errorMessage = new ErrorMessage(ctx.cmd, new Packet(ERROR, ctx.gatewayMessageId), gatewayConnection);
+                        errorMessage.setData(userId + ',' + clientType);
+                        errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT);
+                        errorMessage.sendRaw();
+                        Logs.PUSH.info("push message success but client not ack, context={}", ctx);
+                    }
+                });
+    }
 }
diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java
index b691e027..c0d1d8a9 100644
--- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java
+++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java
@@ -65,6 +65,7 @@ public void init() {
         receiver.register(Command.BIND, new BindUserHandler());
         receiver.register(Command.UNBIND, new UnbindUserHandler());
         receiver.register(Command.FAST_CONNECT, new FastConnectHandler());
+        receiver.register(Command.ACK, new AckHandler());
 
         if (CC.mp.http.proxy_enabled) {
             httpClient = new NettyHttpClient();
diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java
index 158678e9..5b608782 100644
--- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java
+++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java
@@ -19,7 +19,6 @@
 
 package com.mpush.netty.codec;
 
-import com.mpush.api.protocol.Command;
 import com.mpush.api.protocol.Packet;
 import com.mpush.tools.config.CC;
 import io.netty.buffer.ByteBuf;
@@ -46,7 +45,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t
     private void decodeHeartbeat(ByteBuf in, List out) {
         while (in.isReadable()) {
             if (in.readByte() == Packet.HB_PACKET_BYTE) {
-                out.add(Packet.HB_PACKE);
+                out.add(Packet.HB_PACKET);
             } else {
                 in.readerIndex(in.readerIndex() - 1);
                 break;
diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java
index fbbe8199..b6ab81c2 100644
--- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java
+++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java
@@ -19,6 +19,9 @@
 
 package com.mpush.test.push;
 
+import com.mpush.api.Constants;
+import com.mpush.api.push.AckModel;
+import com.mpush.api.push.PushCallback;
 import com.mpush.api.push.PushContent;
 import com.mpush.api.push.PushContent.PushType;
 import com.mpush.api.push.PushSender;
@@ -42,28 +45,32 @@ public static void main(String[] args) throws Exception {
         for (int i = 0; i < 1; i++) {
             PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i);
             content.setMsgId("msgId_" + (i % 2));
+
             Thread.sleep(1000);
-            sender.send(Jsons.toJson(content), Arrays.asList("user-0","doctor43test"), new PushSender.Callback() {
-                @Override
-                public void onSuccess(String userId, ClientLocation location) {
-                    System.err.println("push onSuccess userId=" + userId);
-                }
+            sender.send(Jsons.toJson(content).getBytes(Constants.UTF_8),
+                    Arrays.asList("user-0", "doctor43test"),
+                    AckModel.AUTO_ACK,
+                    new PushCallback() {
+                        @Override
+                        public void onSuccess(String userId, ClientLocation location) {
+                            System.err.println("push onSuccess userId=" + userId);
+                        }
 
-                @Override
-                public void onFailure(String userId, ClientLocation location) {
-                    System.err.println("push onFailure userId=" + userId);
-                }
+                        @Override
+                        public void onFailure(String userId, ClientLocation location) {
+                            System.err.println("push onFailure userId=" + userId);
+                        }
 
-                @Override
-                public void onOffline(String userId, ClientLocation location) {
-                    System.err.println("push onOffline userId=" + userId);
-                }
+                        @Override
+                        public void onOffline(String userId, ClientLocation location) {
+                            System.err.println("push onOffline userId=" + userId);
+                        }
 
-                @Override
-                public void onTimeout(String userId, ClientLocation location) {
-                    System.err.println("push onTimeout userId=" + userId);
-                }
-            });
+                        @Override
+                        public void onTimeout(String userId, ClientLocation location) {
+                            System.err.println("push onTimeout userId=" + userId);
+                        }
+                    });
         }
         LockSupport.park();
     }
diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java
index a1d49ad6..79cbcdb7 100644
--- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java
+++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java
@@ -44,6 +44,7 @@ public final class ThreadNames {
     public static final String T_BIZ = NS + "-biz-";
     public static final String T_PUSH_CALLBACK = NS + "-push-cb-";
     public static final String T_PUSH_REQ_TIMER = NS + "-push-timer-";
+    public static final String T_ARK_REQ_TIMER = NS + "-ack-timer-";
 
     /**
      * connection 定期检测线程

From ebb85047beb4946e02423481a1d8b14cba3e58fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 12:09:38 +0800
Subject: [PATCH 616/890] ADD ACK

---
 .../src/main/java/com/mpush/core/handler/AckHandler.java | 6 ------
 .../java/com/mpush/core/handler/GatewayPushHandler.java  | 9 +++++++++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java
index bd06a0d2..8d2f0855 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java
@@ -50,12 +50,6 @@ public void handle(AckMessage message) {
             return;
         }
 
-        Connection connection = context.connection;
-        if (!connection.isConnected()) {
-            Logs.PUSH.info("receive client ack, gateway connection is closed, message={}, context={}", message, context);
-            return;
-        }
-
         context.success();
     }
 }
diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
index ea75ae00..989e074d 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
@@ -183,6 +183,11 @@ private AckContext buildAckContext(GatewayPushMessage message) {
                 .setCallback(new AckCallback() {
                     @Override
                     public void onSuccess(AckContext ctx) {
+                        if (!gatewayConnection.isConnected()) {
+                            Logs.PUSH.info("receive client ack, gateway connection is closed, context={}", ctx);
+                            return;
+                        }
+
                         OkMessage okMessage = new OkMessage(ctx.cmd, new Packet(OK, ctx.gatewayMessageId), gatewayConnection);
                         okMessage.setData(userId + ',' + clientType);
                         okMessage.sendRaw();
@@ -191,6 +196,10 @@ public void onSuccess(AckContext ctx) {
 
                     @Override
                     public void onTimeout(AckContext ctx) {
+                        if (!gatewayConnection.isConnected()) {
+                            Logs.PUSH.info("receive client ack, gateway connection is closed, context={}", ctx);
+                            return;
+                        }
                         ErrorMessage errorMessage = new ErrorMessage(ctx.cmd, new Packet(ERROR, ctx.gatewayMessageId), gatewayConnection);
                         errorMessage.setData(userId + ',' + clientType);
                         errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT);

From 0aa6da668b89ef07ec251ad826c189777a4db92a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 12:15:04 +0800
Subject: [PATCH 617/890] ADD ACK

---
 .../main/java/com/mpush/core/handler/GatewayPushHandler.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
index 989e074d..764de6e3 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
@@ -112,7 +112,7 @@ private boolean checkLocal(final GatewayPushMessage message) {
         pushMessage.send(future -> {
             if (future.isSuccess()) {//推送成功
 
-                if (message.needAck()) {//需要客户端ACK
+                if (message.needAck()) {//需要客户端ACK, 消息进队列等待客户端响应ACK
                     AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message));
                 } else {
                     OkMessage.from(message).setData(userId + ',' + clientType).sendRaw();

From d75148aeead635e5049118b843f6d286fceaaf33 Mon Sep 17 00:00:00 2001
From: "laien.liang" 
Date: Tue, 6 Sep 2016 14:18:30 +0800
Subject: [PATCH 618/890] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../src/main/java/com/mpush/bootstrap/ServerLauncher.java | 8 +-------
 .../src/main/java/com/mpush/bootstrap/job/BootJob.java    | 4 ++--
 mpush-zk/src/main/java/com/mpush/zk/ZKClient.java         | 3 +--
 3 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java
index bfa5d493..4be649a0 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java
@@ -20,18 +20,12 @@
 package com.mpush.bootstrap;
 
 
-import com.mpush.api.service.Service;
 import com.mpush.bootstrap.job.*;
 import com.mpush.core.server.AdminServer;
 import com.mpush.core.server.ConnectionServer;
 import com.mpush.core.server.GatewayServer;
-import com.mpush.monitor.service.MonitorService;
-import com.mpush.tools.config.CC;
-import com.mpush.zk.ZKClient;
 import com.mpush.zk.node.ZKServerNode;
 
-import java.util.concurrent.TimeUnit;
-
 import static com.mpush.tools.config.CC.mp.net.admin_server_port;
 
 /**
@@ -56,7 +50,7 @@ public ServerLauncher() {
                 .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务
                 .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务
                 .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务
-                .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns
+                .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dnsgit
                 .setNext(new MonitorBoot())//7.启动监控
                 .setNext(new LastBoot());//8.启动结束
     }
diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java
index 3e332429..fa2d94ce 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java
@@ -35,14 +35,14 @@ public abstract class BootJob {
 
     public void startNext() {
         if (next != null) {
-            Logs.Console.error("start next bootstrap job [" + next.getClass().getSimpleName() + "]");
+            Logs.Console.error("start next bootstrap job [{}]", next.getClass().getSimpleName());
             next.start();
         }
     }
 
     public void stopNext() {
         if (next != null) {
-            Logs.Console.error("stop next bootstrap job [" + next.getClass().getSimpleName() + "]");
+            Logs.Console.error("stop next bootstrap job [{}]", next.getClass().getSimpleName());
             next.stop();
         }
     }
diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
index 0ac488a4..d1d2780e 100644
--- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
+++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
@@ -46,8 +46,7 @@ public class ZKClient extends BaseService {
     private TreeCache cache;
 
     private synchronized static ZKClient I() {
-        if (I == null) return new ZKClient();
-        else return I;
+        return I == null ? new ZKClient() : I;
     }
 
     private ZKClient() {

From 705f9fa9596872eea45962577e3a4b2b71f5369e Mon Sep 17 00:00:00 2001
From: "laien.liang" 
Date: Tue, 6 Sep 2016 14:43:43 +0800
Subject: [PATCH 619/890] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=A5=E5=BF=97?=
 =?UTF-8?q?=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../src/main/java/com/mpush/bootstrap/Main.java |  2 +-
 .../java/com/mpush/bootstrap/job/BootChain.java |  4 ++--
 .../java/com/mpush/bootstrap/job/BootJob.java   |  4 ++--
 .../java/com/mpush/bootstrap/job/LastBoot.java  | 17 ++++++++---------
 .../com/mpush/bootstrap/job/ServerBoot.java     | 11 +++--------
 .../redis/manager/ZKRedisClusterManager.java    |  7 +++----
 .../com/mpush/netty/server/NettyServer.java     |  8 ++++----
 .../src/main/java/com/mpush/zk/ZKClient.java    |  6 +++---
 8 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
index 607f1052..5b2e0dbe 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
@@ -26,7 +26,7 @@
 public class Main {
     public static void main(String[] args) {
         Logs.init();
-        Logs.Console.error("launch mpush server...");
+        Logs.Console.info("launch mpush server...");
         ServerLauncher launcher = new ServerLauncher();
         launcher.start();
         addHook(launcher);
diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java
index 523d59ad..e73f11ed 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java
@@ -45,13 +45,13 @@ private BootJob first() {
         return new BootJob() {
             @Override
             public void start() {
-                Logs.Console.error("begin start bootstrap chain...");
+                Logs.Console.info("begin start bootstrap chain...");
                 startNext();
             }
 
             @Override
             protected void stop() {
-                Logs.Console.error("begin stop bootstrap chain...");
+                Logs.Console.info("begin stop bootstrap chain...");
                 stopNext();
             }
         };
diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java
index fa2d94ce..c616c5bb 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java
@@ -35,14 +35,14 @@ public abstract class BootJob {
 
     public void startNext() {
         if (next != null) {
-            Logs.Console.error("start next bootstrap job [{}]", next.getClass().getSimpleName());
+            Logs.Console.info("start next bootstrap job [{}]", next.getClass().getSimpleName());
             next.start();
         }
     }
 
     public void stopNext() {
         if (next != null) {
-            Logs.Console.error("stop next bootstrap job [{}]", next.getClass().getSimpleName());
+            Logs.Console.info("stop next bootstrap job [{}]", next.getClass().getSimpleName());
             next.stop();
         }
     }
diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java
index 1297330e..89b2fa24 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java
@@ -19,7 +19,6 @@
 
 package com.mpush.bootstrap.job;
 
-import com.mpush.common.user.UserManager;
 import com.mpush.tools.log.Logs;
 
 /**
@@ -30,17 +29,17 @@
 public final class LastBoot extends BootJob {
     @Override
     protected void start() {
-        Logs.Console.error("end start bootstrap chain...");
-        Logs.Console.error("===================================================================");
-        Logs.Console.error("====================MPUSH SERVER START SUCCESS=====================");
-        Logs.Console.error("===================================================================");
+        Logs.Console.info("end start bootstrap chain...");
+        Logs.Console.info("===================================================================");
+        Logs.Console.info("====================MPUSH SERVER START SUCCESS=====================");
+        Logs.Console.info("===================================================================");
     }
 
     @Override
     protected void stop() {
-        Logs.Console.error("end stop bootstrap chain...");
-        Logs.Console.error("===================================================================");
-        Logs.Console.error("====================MPUSH SERVER STOPPED SUCCESS=====================");
-        Logs.Console.error("===================================================================");
+        Logs.Console.info("end stop bootstrap chain...");
+        Logs.Console.info("===================================================================");
+        Logs.Console.info("====================MPUSH SERVER STOPPED SUCCESS=====================");
+        Logs.Console.info("===================================================================");
     }
 }
diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java
index de470d6b..c603003d 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java
@@ -21,19 +21,13 @@
 
 import com.mpush.api.service.Listener;
 import com.mpush.api.service.Server;
-import com.mpush.core.server.AdminServer;
-import com.mpush.core.server.ConnectionServer;
-import com.mpush.core.server.GatewayServer;
 import com.mpush.tools.Jsons;
-import com.mpush.tools.config.CC;
 import com.mpush.tools.log.Logs;
 import com.mpush.tools.thread.pool.ThreadPoolManager;
 import com.mpush.zk.ZKClient;
 import com.mpush.zk.node.ZKServerNode;
 
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 
 /**
  * Created by yxx on 2016/5/14.
@@ -57,7 +51,7 @@ public void start() {
             server.start(new Listener() {
                 @Override
                 public void onSuccess(Object... args) {
-                    Logs.Console.error("start " + serverName + " success listen:" + args[0]);
+                    Logs.Console.info("start {} success listen:{}", serverName, args[0]);
                     if (node != null) {
                         registerServerToZk(node.getZkPath(), Jsons.toJson(node));
                     }
@@ -78,6 +72,7 @@ protected void stop() {
         try {
             server.stop().get(1, TimeUnit.MINUTES);
         } catch (Exception e) {
+            Logs.Console.error("stop server error:", e);
         }
         stopNext();
     }
@@ -85,6 +80,6 @@ protected void stop() {
     //注册应用到zk
     private void registerServerToZk(String path, String value) {
         ZKClient.I.registerEphemeralSequential(path, value);
-        Logs.Console.error("register server node=" + value + " to zk name=" + path);
+        Logs.Console.info("register server node={} to zk name={}", value, path);
     }
 }
diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
index 413e59ca..8e6ae69f 100644
--- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
+++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
@@ -19,7 +19,6 @@
 
 package com.mpush.cache.redis.manager;
 
-import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
 import com.mpush.cache.redis.RedisException;
 import com.mpush.cache.redis.RedisGroup;
@@ -51,7 +50,7 @@ private ZKRedisClusterManager() {
      */
     @Override
     public void init() {
-        Logs.Console.error("begin init redis cluster");
+        Logs.Console.info("begin init redis cluster");
         if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running.");
         List groupList = CC.mp.redis.cluster_group;
 
@@ -70,7 +69,7 @@ public void init() {
             groups.add(RedisGroup.from(node));
         }
         if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null");
-        Logs.Console.error("init redis cluster success...");
+        Logs.Console.info("init redis cluster success...");
     }
 
     @Override
@@ -119,7 +118,7 @@ private void register(List groupList) {
                 || !ZKClient.I.isExisted(REDIS_SERVER.getRootPath())//redis节点不存在
                 || !ZKClient.I.get(REDIS_SERVER.getRootPath()).equals(data)) {//数据有变更
             ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data);
-            Logs.Console.error("register redis server group success, group=" + data);
+            Logs.Console.info("register redis server group success, group={}", data);
         }
     }
 
diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java
index c32553b4..a171d3fd 100644
--- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java
+++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java
@@ -82,10 +82,10 @@ public void stop(Listener listener) {
             Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName());
             return;
         }
-        Logs.Console.error("try shutdown {}...", this.getClass().getSimpleName());
+        Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName());
         if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly();
         if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly();
-        Logs.Console.error("{} shutdown success.", this.getClass().getSimpleName());
+        Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName());
         if (listener != null) {
             listener.onSuccess(port);
         }
@@ -159,10 +159,10 @@ public void initChannel(SocketChannel ch) throws Exception {
              */
             ChannelFuture f = b.bind(port).sync().addListener((ChannelFutureListener) future -> {
                 if (future.isSuccess()) {
-                    Logs.Console.error("server start success on:{}", port);
+                    Logs.Console.info("server start success on:{}", port);
                     if (listener != null) listener.onSuccess(port);
                 } else {
-                    Logs.Console.error("server start failure on:{}", port, future.cause());
+                    Logs.Console.info("server start failure on:{}", port, future.cause());
                     if (listener != null) listener.onFailure(future.cause());
                 }
             });
diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
index d1d2780e..73be4cc2 100644
--- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
+++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
@@ -55,7 +55,7 @@ private ZKClient() {
     @Override
     protected void doStart(Listener listener) throws Throwable {
         client.start();
-        Logs.Console.error("init zk client waiting for connected...");
+        Logs.Console.info("init zk client waiting for connected...");
         if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) {
             throw new ZKException("init zk error, config=" + zkConfig);
         }
@@ -63,7 +63,7 @@ protected void doStart(Listener listener) throws Throwable {
         addConnectionStateListener();
         listener.onSuccess(zkConfig.getHosts());
         Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts());
-        Logs.Console.error("init zk client success...");
+        Logs.Console.info("init zk client success...");
     }
 
     @Override
@@ -109,7 +109,7 @@ public List getAclForPath(final String path) {
                     });
         }
         client = builder.build();
-        Logs.Console.error("init zk client, config=" + zkConfig);
+        Logs.Console.info("init zk client, config={}", zkConfig.toString());
     }
 
     // 注册连接状态监听器

From c5784c48d90d5c7c95b9196f12c43136f132cb0c Mon Sep 17 00:00:00 2001
From: "laien.liang" 
Date: Tue, 6 Sep 2016 17:03:09 +0800
Subject: [PATCH 620/890] =?UTF-8?q?1=E3=80=81=E5=90=AF=E5=8A=A8=E6=97=A5?=
 =?UTF-8?q?=E5=BF=97=E4=BF=AE=E6=94=B9=202=E3=80=81=E5=A2=9E=E5=8A=A0colle?=
 =?UTF-8?q?tions4=E5=B7=A5=E5=85=B7=E5=8C=85=203=E3=80=81=E4=BD=BF?=
 =?UTF-8?q?=E7=94=A8java8=E4=B8=AD=E7=9A=84stream?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../com/mpush/api/service/BaseService.java    |  4 ++--
 .../main/java/com/mpush/bootstrap/Main.java   |  2 +-
 .../redis/manager/ZKRedisClusterManager.java  | 22 ++++++++-----------
 .../src/test/resources/application.conf       |  4 ++--
 mpush-tools/pom.xml                           |  4 ++++
 pom.xml                                       |  6 +++++
 6 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java
index 8d63c14b..5e045a2a 100644
--- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java
+++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java
@@ -47,7 +47,7 @@ protected void tryStart(Listener listener, Function function) {
             try {
                 init();
                 function.apply(listener);
-                listener.onSuccess("service " + this.getClass().getSimpleName() + " start success");
+                listener.onSuccess(String.format("service %s start success", this.getClass().getSimpleName()));
             } catch (Throwable e) {
                 listener.onFailure(e);
                 throw new ServiceException(e);
@@ -62,7 +62,7 @@ protected void tryStop(Listener listener, Function function) {
         if (started.compareAndSet(true, false)) {
             try {
                 function.apply(listener);
-                listener.onSuccess("service " + this.getClass().getSimpleName() + " stop success");
+                listener.onSuccess(String.format("service %s stop success", this.getClass().getSimpleName()));
             } catch (Throwable e) {
                 listener.onFailure(e);
                 throw new ServiceException(e);
diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
index 5b2e0dbe..84450f8b 100644
--- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
+++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
@@ -61,7 +61,7 @@ private void stop() {
             } catch (Exception e) {
                 Logs.Console.error("mpush server stop ex", e);
             }
-            Logs.Console.error("jvm exit, all server stopped...");
+            Logs.Console.info("jvm exit, all server stopped...");
         }
     }
 }
diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
index 8e6ae69f..1730e2f6 100644
--- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
+++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java
@@ -19,7 +19,6 @@
 
 package com.mpush.cache.redis.manager;
 
-import com.google.common.collect.Lists;
 import com.mpush.cache.redis.RedisException;
 import com.mpush.cache.redis.RedisGroup;
 import com.mpush.cache.redis.RedisServer;
@@ -29,11 +28,13 @@
 import com.mpush.zk.ZKClient;
 import com.mpush.zk.listener.ZKRedisNodeWatcher;
 import com.mpush.zk.node.ZKRedisNode;
+import org.apache.commons.collections4.CollectionUtils;
 
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import static com.mpush.zk.ZKPath.REDIS_SERVER;
 
@@ -54,20 +55,20 @@ public void init() {
         if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running.");
         List groupList = CC.mp.redis.cluster_group;
 
-        if (groupList.size() > 0) {
+        if (CollectionUtils.isNotEmpty(groupList)) {
             register(groupList);
         }
 
         ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher();
         watcher.beginWatch();
         Collection nodes = watcher.getCache().values();
-        if (nodes == null || nodes.isEmpty()) {
-            Logs.REDIS.info("init redis client error, redis server is none.");
+        if (CollectionUtils.isEmpty(nodes)) {
+            Logs.REDIS.error("init redis client error, redis server is none.");
             throw new RedisException("init redis client error, redis server is none.");
         }
-        for (ZKRedisNode node : nodes) {
-            groups.add(RedisGroup.from(node));
-        }
+
+        nodes.stream().map(RedisGroup::from).forEach(groups::add);
+
         if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null");
         Logs.Console.info("init redis cluster success...");
     }
@@ -104,12 +105,7 @@ public RedisServer randomGetRedisNode(String key) {
      */
     @Override
     public List hashSet(String key) {
-        List nodeList = Lists.newArrayList();
-        for (RedisGroup group : groups) {
-            RedisServer node = group.get(key);
-            nodeList.add(node);
-        }
-        return nodeList;
+        return groups.stream().map(g -> g.get(key)).collect(Collectors.toList());
     }
 
     private void register(List groupList) {
diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf
index 3f221c6a..0cbd171e 100644
--- a/mpush-test/src/test/resources/application.conf
+++ b/mpush-test/src/test/resources/application.conf
@@ -3,9 +3,9 @@ mp.log.level=debug
 mp.min-heartbeat=10s
 mp.max-heartbeat=10s
 mp.zk.namespace=mpush
-mp.zk.server-address="10.0.10.47:2181"
+mp.zk.server-address="10.199.241.96:2181"
 mp.core.compress-threshold=10k
 mp.redis {
     #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器
-    cluster-group:[["10.0.10.53:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port
+    cluster-group:[["10.199.241.96:6379:test"]]//格式是ip:port:password,密码可以没有ip:port
 }
\ No newline at end of file
diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml
index ef29dbd2..03dcafd9 100644
--- a/mpush-tools/pom.xml
+++ b/mpush-tools/pom.xml
@@ -76,5 +76,9 @@
             log4j
             log4j
         
+        
+            org.apache.commons
+            commons-collections4
+        
     
 
diff --git a/pom.xml b/pom.xml
index 303f7bb9..06d76227 100644
--- a/pom.xml
+++ b/pom.xml
@@ -252,6 +252,12 @@
                 4.10
                 test
             
+            
+            
+                org.apache.commons
+                commons-collections4
+                4.1
+            
         
     
 

From 5335cdca226f16e5014a61dfa409fd6cae9511c4 Mon Sep 17 00:00:00 2001
From: "laien.liang" 
Date: Tue, 6 Sep 2016 17:05:54 +0800
Subject: [PATCH 621/890] =?UTF-8?q?1=E3=80=81=E5=90=AF=E5=8A=A8=E6=97=A5?=
 =?UTF-8?q?=E5=BF=97=E4=BF=AE=E6=94=B9=202=E3=80=81=E5=A2=9E=E5=8A=A0colle?=
 =?UTF-8?q?tions4=E5=B7=A5=E5=85=B7=E5=8C=85=203=E3=80=81=E4=BD=BF?=
 =?UTF-8?q?=E7=94=A8java8=E4=B8=AD=E7=9A=84stream?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mpush-test/src/test/resources/application.conf | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf
index 0cbd171e..8525babe 100644
--- a/mpush-test/src/test/resources/application.conf
+++ b/mpush-test/src/test/resources/application.conf
@@ -3,9 +3,9 @@ mp.log.level=debug
 mp.min-heartbeat=10s
 mp.max-heartbeat=10s
 mp.zk.namespace=mpush
-mp.zk.server-address="10.199.241.96:2181"
+mp.zk.server-address="127.0.0.1:2181"
 mp.core.compress-threshold=10k
 mp.redis {
     #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器
-    cluster-group:[["10.199.241.96:6379:test"]]//格式是ip:port:password,密码可以没有ip:port
+    cluster-group:[["127.0.0.1:6379:test"]]//格式是ip:port:password,密码可以没有ip:port
 }
\ No newline at end of file

From a19f76f078733390d3ec5843c2f8ffa484184000 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 17:43:06 +0800
Subject: [PATCH 622/890] ADD ACK

---
 .../src/main/java/com/mpush/api/protocol/Packet.java |  2 +-
 .../src/main/java/com/mpush/api/push/AckModel.java   |  2 +-
 mpush-boot/src/main/resources/mpush.conf             | 12 ++----------
 .../java/com/mpush/common/message/PushMessage.java   |  8 +-------
 .../common/message/gateway/GatewayPushMessage.java   |  2 +-
 5 files changed, 6 insertions(+), 20 deletions(-)

diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java
index 0707a8f9..2cf26f1e 100644
--- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java
+++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java
@@ -33,7 +33,7 @@ public final class Packet {
 
     public static final byte FLAG_CRYPTO = 0x01;
     public static final byte FLAG_COMPRESS = 0x02;
-    public static final byte FLAG_ACK = 0x04;
+    public static final byte FLAG_BIZ_ACK = 0x04;
     public static final byte FLAG_AUTO_ACK = 0x08;
 
     public static final byte HB_PACKET_BYTE = -33;
diff --git a/mpush-api/src/main/java/com/mpush/api/push/AckModel.java b/mpush-api/src/main/java/com/mpush/api/push/AckModel.java
index 94592259..aff9843a 100644
--- a/mpush-api/src/main/java/com/mpush/api/push/AckModel.java
+++ b/mpush-api/src/main/java/com/mpush/api/push/AckModel.java
@@ -29,7 +29,7 @@
 public enum AckModel {
     NO_ACK((byte) 0),//不需要ACK
     AUTO_ACK(Packet.FLAG_AUTO_ACK),//客户端收到消息后自动确认消息
-    BIZ_ACK(Packet.FLAG_ACK);//由客户端业务自己确认消息是否到达
+    BIZ_ACK(Packet.FLAG_BIZ_ACK);//由客户端业务自己确认消息是否到达
     public final byte flag;
 
     AckModel(byte flag) {
diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf
index ea735b95..0f3e58b9 100644
--- a/mpush-boot/src/main/resources/mpush.conf
+++ b/mpush-boot/src/main/resources/mpush.conf
@@ -6,14 +6,6 @@ mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24ju
 mp.zk.server-address="127.0.0.1:2181"
 mp.zk.namespace=mpush
 mp.redis={
-    #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器
-    cluster-group:[
-        [
-            {
-                host:"127.0.0.1"
-                port:6379
-#               password:"your redis password" //密码, 没有可以不设置
-            }
-        ]
-    ]
+    #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器
+    cluster-group:[["10.0.10.53:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port
 }
\ No newline at end of file
diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java
index c7fbb550..40978cde 100644
--- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java
+++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java
@@ -19,12 +19,8 @@
 
 package com.mpush.common.message;
 
-import com.mpush.api.Constants;
 import com.mpush.api.connection.Connection;
 import com.mpush.api.protocol.Packet;
-import com.mpush.common.message.gateway.GatewayPushMessage;
-
-import java.util.Arrays;
 
 import static com.mpush.api.protocol.Command.PUSH;
 
@@ -56,12 +52,10 @@ public byte[] encode() {
         return content;
     }
 
-
     public boolean needAck() {
-        return packet.hasFlag(Packet.FLAG_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
+        return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
     }
 
-
     @Override
     public String toString() {
         return "PushMessage{" +
diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
index afb65dfe..59b65283 100644
--- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
+++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
@@ -63,7 +63,7 @@ public void encode(ByteBuf body) {
     }
 
     public boolean needAck() {
-        return packet.hasFlag(Packet.FLAG_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
+        return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
     }
 
     @Override

From d27b0359073fed292a8ea72d8b9468e3d88fd1bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 17:50:38 +0800
Subject: [PATCH 623/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mpush-boot/src/main/resources/mpush.conf | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf
index ea735b95..d806cbdc 100644
--- a/mpush-boot/src/main/resources/mpush.conf
+++ b/mpush-boot/src/main/resources/mpush.conf
@@ -6,14 +6,6 @@ mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24ju
 mp.zk.server-address="127.0.0.1:2181"
 mp.zk.namespace=mpush
 mp.redis={
-    #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器
-    cluster-group:[
-        [
-            {
-                host:"127.0.0.1"
-                port:6379
-#               password:"your redis password" //密码, 没有可以不设置
-            }
-        ]
-    ]
+    #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器
+    cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port
 }
\ No newline at end of file

From 8efa7c4b05473850e7137b74169ff4ac0e176c1f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 17:57:18 +0800
Subject: [PATCH 624/890] =?UTF-8?q?=E8=BF=99=E4=B8=AA=E6=96=87=E4=BB=B6?=
 =?UTF-8?q?=E4=BB=A5=E5=90=8E=E4=B8=8D=E8=A6=81=E6=8F=90=E4=BA=A4=E4=BA=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore                                     | 1 +
 mpush-test/src/test/resources/application.conf | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index a0043cc4..f7299b4b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,3 +23,4 @@
 
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 hs_err_pid*
+mpush-test/src/test/resources/application.conf
\ No newline at end of file
diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf
index 8525babe..ea89e4ab 100644
--- a/mpush-test/src/test/resources/application.conf
+++ b/mpush-test/src/test/resources/application.conf
@@ -7,5 +7,5 @@ mp.zk.server-address="127.0.0.1:2181"
 mp.core.compress-threshold=10k
 mp.redis {
     #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器
-    cluster-group:[["127.0.0.1:6379:test"]]//格式是ip:port:password,密码可以没有ip:port
+    cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port
 }
\ No newline at end of file

From df2ea758d77723ce7e0b6cfd0b31c03e4d963ea3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 18:16:27 +0800
Subject: [PATCH 625/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mpush-core/src/main/java/com/mpush/core/ack/AckContext.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java
index e175f024..e5dc38e1 100644
--- a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java
+++ b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java
@@ -71,7 +71,7 @@ public void timeout() {
     public String toString() {
         return "AckContext{" +
                 "gatewayMessageId=" + gatewayMessageId +
-                "pushMessageId=" + pushMessageId +
+                ", pushMessageId=" + pushMessageId +
                 '}';
     }
 

From 65b8fc142fb2cdaee2dec3365508356553c25403 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Tue, 6 Sep 2016 18:21:20 +0800
Subject: [PATCH 626/890] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=E5=8D=87?=
 =?UTF-8?q?=E7=BA=A7=E5=88=B00.0.3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 06d76227..fdac5b89 100644
--- a/pom.xml
+++ b/pom.xml
@@ -61,7 +61,7 @@
         UTF-8
         1.8
         com.github.mpusher
-        0.0.2
+        0.0.3
         ${mpush.version}
         ${mpush.version}
         ${mpush.version}

From 307cd443d822c2931a9c7f05426223a6ffaca6c5 Mon Sep 17 00:00:00 2001
From: "laien.liang" 
Date: Wed, 7 Sep 2016 12:47:21 +0800
Subject: [PATCH 627/890] =?UTF-8?q?logger=E5=BA=94=E4=B8=BAstatic?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../src/main/java/com/mpush/netty/server/NettyServer.java   | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java
index a171d3fd..9d8cc644 100644
--- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java
+++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java
@@ -49,7 +49,7 @@
  */
 public abstract class NettyServer extends BaseService implements Server {
 
-    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+    private static final  Logger logger = LoggerFactory.getLogger(NettyServer.class);
 
     public enum State {Created, Initialized, Starting, Started, Shutdown}
 
@@ -157,12 +157,12 @@ public void initChannel(SocketChannel ch) throws Exception {
             /***
              * 绑定端口并启动去接收进来的连接
              */
-            ChannelFuture f = b.bind(port).sync().addListener((ChannelFutureListener) future -> {
+            ChannelFuture f = b.bind(port).sync().addListener(future -> {
                 if (future.isSuccess()) {
                     Logs.Console.info("server start success on:{}", port);
                     if (listener != null) listener.onSuccess(port);
                 } else {
-                    Logs.Console.info("server start failure on:{}", port, future.cause());
+                    Logs.Console.error("server start failure on:{}", port, future.cause());
                     if (listener != null) listener.onFailure(future.cause());
                 }
             });

From 1ac12969df41ab92a585477d18d4eb18f189ead2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= 
Date: Mon, 12 Sep 2016 21:32:10 +0800
Subject: [PATCH 628/890] =?UTF-8?q?Push=E6=8E=A8=E9=80=81=E6=8E=A5?=
 =?UTF-8?q?=E5=8F=A3=E8=A7=84=E8=8C=83=20=E5=A2=9E=E5=8A=A0=E5=85=A8?=
 =?UTF-8?q?=E7=BD=91=E6=8E=A8=E9=80=81=20=E5=A2=9E=E5=8A=A0=E6=8C=89tag?=
 =?UTF-8?q?=E6=8E=A8=E9=80=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 conf/reference.conf                           |   3 +-
 .../main/java/com/mpush/api/push/MsgType.java |  23 +++
 .../java/com/mpush/api/push/PushCallback.java |  44 ++++-
 .../java/com/mpush/api/push/PushContext.java  | 164 ++++++++++++++++++
 .../push/{PushContent.java => PushMsg.java}   |  40 +----
 .../java/com/mpush/api/push/PushSender.java   |  37 ++--
 .../gateway/GatewayClientChannelHandler.java  |   2 +-
 .../client/gateway/GatewayClientFactory.java  |   6 +
 .../com/mpush/client/push/PushClient.java     |  86 ++++-----
 .../com/mpush/client/push/PushRequest.java    |  66 +++++--
 .../message/gateway/GatewayPushMessage.java   |  38 +++-
 .../com/mpush/core/handler/AdminHandler.java  |   4 +-
 .../core/handler/GatewayPushHandler.java      |  91 +++++++++-
 .../mpush/test/client/ConnClientTestMain.java |   2 +-
 .../java/com/mpush/test/gson/GsonTest.java    |  10 +-
 .../mpush/test/push/PushClientTestMain.java   |  71 ++++----
 16 files changed, 520 insertions(+), 167 deletions(-)
 create mode 100644 mpush-api/src/main/java/com/mpush/api/push/MsgType.java
 create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushContext.java
 rename mpush-api/src/main/java/com/mpush/api/push/{PushContent.java => PushMsg.java} (63%)

diff --git a/conf/reference.conf b/conf/reference.conf
index dcc9c5e6..9304f842 100644
--- a/conf/reference.conf
+++ b/conf/reference.conf
@@ -40,7 +40,8 @@ mp {
         gateway-server-port=3001 //网关服务端口, 内部端口
         admin-server-port=3002 //控制台服务端口, 内部端口
         public-host-mapping { //本机局域网IP和公网IP的映射关系
-            "127.0.0.1":"111.1.32.137"
+            "10.0.10.156":"111.1.32.137"
+            "10.0.10.166":"111.1.33.138"
         }
         traffic-shaping { //流量整形配置
             gateway-client {
diff --git a/mpush-api/src/main/java/com/mpush/api/push/MsgType.java b/mpush-api/src/main/java/com/mpush/api/push/MsgType.java
new file mode 100644
index 00000000..8ac3309a
--- /dev/null
+++ b/mpush-api/src/main/java/com/mpush/api/push/MsgType.java
@@ -0,0 +1,23 @@
+package com.mpush.api.push;
+
+public enum MsgType {
+    NOTIFICATION("提醒", 1),//会在通知栏显示
+    MESSAGE("消息", 2),//不会在通知栏显示,业务自定义消息
+    NOTIFICATION_AND_MESSAGE("提醒+消息", 3);//1+2
+
+    MsgType(String desc, int value) {
+        this.desc = desc;
+        this.value = value;
+    }
+
+    private final String desc;
+    private final int value;
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public int getValue() {
+        return value;
+    }
+}
\ No newline at end of file
diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java
index 4eb56f9b..7060b4ee 100644
--- a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java
+++ b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java
@@ -2,13 +2,55 @@
 
 import com.mpush.api.router.ClientLocation;
 
+import java.util.List;
+
+/**
+ * Created by ohun on 2015/12/30.
+ *
+ * @author ohun@live.cn
+ */
 public interface PushCallback {
 
-    void onSuccess(String userId, ClientLocation location);
+    /**
+     * 推送成功, 指定用户推送时重写此方法
+     *
+     * @param userId   成功的用户
+     * @param location 用户所在机器
+     */
+    default void onSuccess(String userId, ClientLocation location) {
+
+    }
 
+    /**
+     * 推送失败
+     *
+     * @param userId   推送用户
+     * @param location 用户所在机器
+     */
     void onFailure(String userId, ClientLocation location);
 
+    /**
+     * 推送用户不在线
+     *
+     * @param userId   推送用户
+     * @param location 用户所在机器
+     */
     void onOffline(String userId, ClientLocation location);
 
+    /**
+     * 推送超时
+     *
+     * @param userId   推送用户
+     * @param location 用户所在机器
+     */
     void onTimeout(String userId, ClientLocation location);
+
+    /**
+     * 推送成功, 广播时重写此方法
+     *
+     * @param userIds 推送成功的用户列表
+     */
+    default void onSuccess(List userIds) {
+
+    }
 }
\ No newline at end of file
diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java
new file mode 100644
index 00000000..d57d7b29
--- /dev/null
+++ b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java
@@ -0,0 +1,164 @@
+/*
+ * (C) Copyright 2015-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *     ohun@live.cn (夜色)
+ */
+
+package com.mpush.api.push;
+
+import com.mpush.api.Constants;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Created by ohun on 16/9/8.
+ *
+ * @author ohun@live.cn (夜色)
+ */
+public class PushContext {
+    /**
+     * 待推送的内容
+     */
+    private byte[] context;
+
+    /**
+     * 待推送的消息
+     */
+    private PushMsg pushMsg;
+
+    /**
+     * 目标用户
+     */
+    private String userId;
+
+    /**
+     * 目标用户,批量
+     */
+    private List userIds;
+
+    /**
+     * 用户标签过滤
+     */
+    private Set tags;
+
+    /**
+     * 消息ack模式
+     */
+    private AckModel ackModel = AckModel.NO_ACK;
+
+    /**
+     * 推送成功后的回调
+     */
+    private PushCallback callback;
+
+    /**
+     * 全网广播在线用户
+     */
+    private boolean broadcast = false;
+
+    /**
+     * 推送超时时间
+     */
+    private long timeout = 3000;
+
+    public PushContext(byte[] context) {
+        this.context = context;
+    }
+
+    public PushContext(PushMsg pushMsg) {
+        this.pushMsg = pushMsg;
+    }
+
+    public static PushContext build(String msg) {
+        return new PushContext(msg.getBytes(Constants.UTF_8));
+    }
+
+    public static PushContext build(PushMsg msg) {
+        return new PushContext(msg);
+    }
+
+    public byte[] getContext() {
+        return context;
+    }
+
+    public String getUserId() {
+        return userId;
+    }
+
+    public PushContext setUserId(String userId) {
+        this.userId = userId;
+        return this;
+    }
+
+    public List getUserIds() {
+        return userIds;
+    }
+
+    public PushContext setUserIds(List userIds) {
+        this.userIds = userIds;
+        return this;
+    }
+
+    public AckModel getAckModel() {
+        return ackModel;
+    }
+
+    public PushContext setAckModel(AckModel ackModel) {
+        this.ackModel = ackModel;
+        return this;
+    }
+
+    public PushCallback getCallback() {
+        return callback;
+    }
+
+    public PushContext setCallback(PushCallback callback) {
+        this.callback = callback;
+        return this;
+    }
+
+    public PushMsg getPushMsg() {
+        return pushMsg;
+    }
+
+    public boolean isBroadcast() {
+        return broadcast;
+    }
+
+    public PushContext setBroadcast(boolean broadcast) {
+        this.broadcast = broadcast;
+        return this;
+    }
+
+    public long getTimeout() {
+        return timeout;
+    }
+
+    public PushContext setTimeout(long timeout) {
+        this.timeout = timeout;
+        return this;
+    }
+
+    public Set getTags() {
+        return tags;
+    }
+
+    public PushContext setTags(Set tags) {
+        this.tags = tags;
+        return this;
+    }
+}
diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java b/mpush-api/src/main/java/com/mpush/api/push/PushMsg.java
similarity index 63%
rename from mpush-api/src/main/java/com/mpush/api/push/PushContent.java
rename to mpush-api/src/main/java/com/mpush/api/push/PushMsg.java
index e4e741c3..f269f674 100644
--- a/mpush-api/src/main/java/com/mpush/api/push/PushContent.java
+++ b/mpush-api/src/main/java/com/mpush/api/push/PushMsg.java
@@ -19,8 +19,6 @@
 
 package com.mpush.api.push;
 
-import java.io.Serializable;
-
 
 /**
  * msgId、msgType 必填
@@ -33,20 +31,19 @@
  * msgType=3 :消息+提醒
  * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
  */
-public final class PushContent implements Serializable {
-    private static final long serialVersionUID = -1805329333995385960L;
+public final class PushMsg {
+    private final MsgType msgType; //type
     private String msgId; //返回使用
     private String content; //content
-    private int msgType; //type
 
-    public PushContent(int msgType) {
+    public PushMsg(MsgType msgType) {
         this.msgType = msgType;
     }
 
-    public static PushContent build(PushType msgType, String content) {
-        PushContent pushContent = new PushContent(msgType.getValue());
-        pushContent.setContent(content);
-        return pushContent;
+    public static PushMsg build(MsgType msgType, String content) {
+        PushMsg pushMessage = new PushMsg(msgType);
+        pushMessage.setContent(content);
+        return pushMessage;
     }
 
     public String getMsgId() {
@@ -54,7 +51,7 @@ public String getMsgId() {
     }
 
     public int getMsgType() {
-        return msgType;
+        return msgType.getValue();
     }
 
     public void setMsgId(String msgId) {
@@ -69,26 +66,5 @@ public void setContent(String content) {
         this.content = content;
     }
 
-    public enum PushType {
-        NOTIFICATION("提醒", 1),
-        MESSAGE("消息", 2),
-        NOTIFICATIONANDMESSAGE("提醒+消息", 3);
-
-        PushType(String desc, int value) {
-            this.desc = desc;
-            this.value = value;
-        }
-
-        private final String desc;
-        private final int value;
-
-        public String getDesc() {
-            return desc;
-        }
-
-        public int getValue() {
-            return value;
-        }
-    }
 
 }
\ No newline at end of file
diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java
index e77dc15e..03c015f5 100644
--- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java
+++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java
@@ -34,20 +34,37 @@
  */
 public interface PushSender extends Service {
 
+    /**
+     * 创建PushSender实例
+     *
+     * @return PushSender
+     */
     static PushSender create() {
         return SpiLoader.load(PusherFactory.class).get();
     }
 
-    void send(String content, Collection userIds, PushCallback callback);
+    /**
+     * 推送push消息
+     *
+     * @param context 推送参数
+     * @return FutureTask 可用于同步调用
+     */
+    FutureTask send(PushContext context);
 
-    FutureTask send(String content, String userId, PushCallback callback);
-
-    void send(byte[] content, Collection userIds, PushCallback callback);
-
-    FutureTask send(byte[] content, String userId, PushCallback callback);
-
-    void send(byte[] content, Collection userIds, AckModel ackModel, PushCallback callback);
-
-    FutureTask send(byte[] content, String userId, AckModel ackModel, PushCallback callback);
+    default FutureTask send(String context, String userId, PushCallback callback) {
+        return send(PushContext
+                .build(context)
+                .setUserId(userId)
+                .setCallback(callback)
+        );
+    }
 
+    default FutureTask send(String context, String userId, AckModel ackModel, PushCallback callback) {
+        return send(PushContext
+                .build(context)
+                .setAckModel(ackModel)
+                .setUserId(userId)
+                .setCallback(callback)
+        );
+    }
 }
diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java
index 4abe6790..d4548f33 100644
--- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java
+++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java
@@ -83,7 +83,7 @@ private void handPush(OkMessage ok, ErrorMessage error, Packet packet) {
         }
 
         if (ok != null) {//推送成功
-            request.success();
+            request.success(ok.data);
         } else if (error != null) {//推送失败
             LOGGER.warn("receive an error gateway response, message={}", error);
             if (error.code == OFFLINE.errorCode) {//用户离线
diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java
index 6714c4d9..a4b58ed4 100644
--- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java
+++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java
@@ -28,7 +28,9 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Collection;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * Created by yxx on 2016/5/17.
@@ -123,4 +125,8 @@ public void onFailure(Throwable cause) {
             }
         });
     }
+
+    public Collection getAllConnections() {
+        return ip_client.values().stream().map(GatewayClient::getConnection).collect(Collectors.toList());
+    }
 }
diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java
index f661b755..57dcdfae 100644
--- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java
+++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java
@@ -21,15 +21,14 @@
 
 import com.mpush.api.Constants;
 import com.mpush.api.connection.Connection;
-import com.mpush.api.push.AckModel;
-import com.mpush.api.push.PushCallback;
-import com.mpush.api.push.PushSender;
+import com.mpush.api.push.*;
 import com.mpush.api.service.BaseService;
 import com.mpush.api.service.Listener;
 import com.mpush.cache.redis.manager.RedisManager;
 import com.mpush.client.gateway.GatewayClientFactory;
 import com.mpush.common.router.ConnectionRouterManager;
 import com.mpush.common.router.RemoteRouter;
+import com.mpush.tools.Jsons;
 import com.mpush.zk.ZKClient;
 import com.mpush.zk.listener.ZKServerNodeWatcher;
 
@@ -40,69 +39,50 @@
 
 import static com.mpush.zk.ZKPath.GATEWAY_SERVER;
 
-/*package*/ class PushClient extends BaseService implements PushSender {
+/*package*/ final class PushClient extends BaseService implements PushSender {
     private static final int DEFAULT_TIMEOUT = 3000;
     private final GatewayClientFactory factory = GatewayClientFactory.I;
     private final ConnectionRouterManager routerManager = ConnectionRouterManager.I;
 
-    public void send(String content, Collection userIds, PushCallback callback) {
-        send(content.getBytes(Constants.UTF_8), userIds, callback);
-    }
-
-    @Override
-    public FutureTask send(String content, String userId, PushCallback callback) {
-        return send(content.getBytes(Constants.UTF_8), userId, callback);
-    }
-
-    @Override
-    public void send(byte[] content, Collection userIds, PushCallback callback) {
-        for (String userId : userIds) {
-            send(content, userId, callback);
+    private FutureTask send0(PushContext ctx) {
+        if (ctx.isBroadcast()) {
+            return PushRequest.build(this, ctx).broadcast();
+        } else {
+            Set routers = routerManager.lookupAll(ctx.getUserId());
+            if (routers == null || routers.isEmpty()) {
+                return PushRequest.build(this, ctx).offline();
+            }
+            FutureTask task = null;
+            for (RemoteRouter router : routers) {
+                task = PushRequest.build(this, ctx).send(router);
+            }
+            return task;
         }
     }
 
     @Override
-    public FutureTask send(byte[] content, String userId, PushCallback callback) {
-        return send(content, userId, AckModel.NO_ACK, callback);
-    }
-
-    @Override
-    public void send(byte[] content, Collection userIds, AckModel ackModel, PushCallback callback) {
-        for (String userId : userIds) {
-            send(content, userId, ackModel, callback);
+    public FutureTask send(PushContext ctx) {
+        if (ctx.getUserId() != null) {
+            return send0(ctx);
+        } else if (ctx.getUserIds() != null) {
+            FutureTask task = null;
+            for (String userId : ctx.getUserIds()) {
+                task = send0(ctx.setUserId(userId));
+            }
+            return task;
+        } else if (ctx.isBroadcast()) {
+            return send0(ctx.setUserId(null));
+        } else {
+            throw new PushException("param error.");
         }
     }
 
-    @Override
-    public FutureTask send(byte[] content, String userId, AckModel ackModel, PushCallback callback) {
-        Set routers = routerManager.lookupAll(userId);
-        if (routers == null || routers.isEmpty()) {
-            return PushRequest
-                    .build(this)
-                    .setAckModel(ackModel)
-                    .setCallback(callback)
-                    .setUserId(userId)
-                    .setContent(content)
-                    .setTimeout(DEFAULT_TIMEOUT)
-                    .offline();
-
-        }
-        FutureTask task = null;
-        for (RemoteRouter router : routers) {
-            task = PushRequest
-                    .build(this)
-                    .setAckModel(ackModel)
-                    .setCallback(callback)
-                    .setUserId(userId)
-                    .setContent(content)
-                    .setTimeout(DEFAULT_TIMEOUT)
-                    .send(router);
-        }
-        return task;
+    Connection getGatewayConnection(String host) {
+        return factory.getConnection(host);
     }
 
-    public Connection getGatewayConnection(String host) {
-        return factory.getConnection(host);
+    Collection getAllConnections() {
+        return factory.getAllConnections();
     }
 
     @Override
diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java
index 95c50edc..4cad9559 100644
--- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java
+++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java
@@ -19,14 +19,14 @@
 
 package com.mpush.client.push;
 
+import com.mpush.api.Constants;
 import com.mpush.api.connection.Connection;
-import com.mpush.api.push.AckModel;
-import com.mpush.api.push.PushCallback;
-import com.mpush.api.push.PushSender;
+import com.mpush.api.push.*;
 import com.mpush.api.router.ClientLocation;
 import com.mpush.common.message.gateway.GatewayPushMessage;
 import com.mpush.common.router.ConnectionRouterManager;
 import com.mpush.common.router.RemoteRouter;
+import com.mpush.tools.Jsons;
 import com.mpush.tools.common.TimeLine;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -61,6 +61,7 @@ private enum Status {init, success, failure, offline, timeout}
     private long timeout;
     private ClientLocation location;
     private Future future;
+    private String result;
 
     private void sendToConnServer(RemoteRouter router) {
         timeLine.addTimePoint("lookup-remote");
@@ -85,19 +86,18 @@ private void sendToConnServer(RemoteRouter router) {
 
         timeLine.addTimePoint("send-to-gateway-begin");
 
-        GatewayPushMessage pushMessage = new GatewayPushMessage(userId, location.getClientType(), content, gatewayConn);
-        pushMessage.getPacket().addFlag(ackModel.flag);
-
-        timeLine.addTimePoint("put-request-bus");
-        future = PushRequestBus.I.put(pushMessage.getSessionId(), this);
+        GatewayPushMessage pushMessage =
+                new GatewayPushMessage(userId, content, gatewayConn)
+                        .setClientType(location.getClientType())
+                        .addFlag(ackModel.flag);
 
         pushMessage.sendRaw(f -> {
             timeLine.addTimePoint("send-to-gateway-end");
-            if (f.isSuccess()) {
-            } else {
-                failure();
-            }
+            if (!f.isSuccess()) failure();
         });
+
+        timeLine.addTimePoint("put-request-bus");
+        future = PushRequestBus.I.put(pushMessage.getSessionId(), this);
     }
 
     private void submit(Status status) {
@@ -119,7 +119,11 @@ private void submit(Status status) {
     public void run() {
         switch (status.get()) {
             case success:
-                callback.onSuccess(userId, location);
+                if (userId == null) {
+                    callback.onSuccess(Jsons.fromJsonToList(result, String[].class));
+                } else {
+                    callback.onSuccess(userId, location);
+                }
                 break;
             case failure:
                 callback.onFailure(userId, location);
@@ -163,11 +167,27 @@ public FutureTask offline() {
         return this;
     }
 
+    public FutureTask broadcast() {
+        timeLine.begin();
+        client.getAllConnections().forEach(conn -> {
+            GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, conn)
+                    .addFlag(ackModel.flag);
+
+            pushMessage.sendRaw(f -> {
+                if (!f.isSuccess()) failure();
+            });
+
+            future = PushRequestBus.I.put(pushMessage.getSessionId(), this);
+        });
+        return this;
+    }
+
     public void timeout() {
         submit(Status.timeout);
     }
 
-    public void success() {
+    public void success(String data) {
+        this.result = data;
         submit(Status.success);
     }
 
@@ -184,8 +204,22 @@ public PushRequest(PushClient client) {
         this.client = client;
     }
 
-    public static PushRequest build(PushClient client) {
-        return new PushRequest(client);
+    public static PushRequest build(PushClient client, PushContext ctx) {
+        byte[] content = ctx.getContext();
+        PushMsg msg = ctx.getPushMsg();
+        if (msg != null) {
+            String json = Jsons.toJson(msg);
+            if (json != null) {
+                content = json.getBytes(Constants.UTF_8);
+            }
+        }
+        return new PushRequest(client)
+                .setAckModel(ctx.getAckModel())
+                .setUserId(ctx.getUserId())
+                .setContent(content)
+                .setTimeout(ctx.getTimeout())
+                .setCallback(ctx.getCallback());
+
     }
 
     public PushRequest setCallback(PushCallback callback) {
diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
index 59b65283..7b3d79a8 100644
--- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
+++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java
@@ -19,12 +19,16 @@
 
 package com.mpush.common.message.gateway;
 
+import com.google.gson.reflect.TypeToken;
 import com.mpush.api.connection.Connection;
 import com.mpush.api.protocol.Packet;
 import com.mpush.common.message.ByteBufMessage;
+import com.mpush.tools.Jsons;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelFutureListener;
 
+import java.util.Set;
+
 import static com.mpush.api.protocol.Command.GATEWAY_PUSH;
 
 /**
@@ -34,13 +38,13 @@
  */
 public class GatewayPushMessage extends ByteBufMessage {
     public String userId;
+    public Set tags;
     public int clientType;
     public byte[] content;
 
-    public GatewayPushMessage(String userId, int clientType, byte[] content, Connection connection) {
+    public GatewayPushMessage(String userId, byte[] content, Connection connection) {
         super(new Packet(GATEWAY_PUSH, genSessionId()), connection);
         this.userId = userId;
-        this.clientType = clientType;
         this.content = content;
     }
 
@@ -51,6 +55,7 @@ public GatewayPushMessage(Packet message, Connection connection) {
     @Override
     public void decode(ByteBuf body) {
         userId = decodeString(body);
+        tags = decodeSet(body);
         clientType = decodeInt(body);
         content = decodeBytes(body);
     }
@@ -58,10 +63,37 @@ public void decode(ByteBuf body) {
     @Override
     public void encode(ByteBuf body) {
         encodeString(body, userId);
+        encodeSet(body, tags);
         encodeInt(body, clientType);
         encodeBytes(body, content);
     }
 
+    private Set decodeSet(ByteBuf body) {
+        String json = decodeString(body);
+        if (json == null) return null;
+        return Jsons.fromJson(json, new TypeToken>() {
+        }.getType());
+    }
+
+    private void encodeSet(ByteBuf body, Set field) {
+        String json = field == null ? null : Jsons.toJson(field);
+        encodeString(body, json);
+    }
+
+    public GatewayPushMessage setClientType(int clientType) {
+        this.clientType = clientType;
+        return this;
+    }
+
+    public GatewayPushMessage addFlag(byte flag) {
+        packet.addFlag(flag);
+        return this;
+    }
+
+    public boolean isBroadcast() {
+        return userId == null;
+    }
+
     public boolean needAck() {
         return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
     }
@@ -80,7 +112,7 @@ public void send(ChannelFutureListener listener) {
     public String toString() {
         return "GatewayPushMessage{" +
                 "userId='" + userId + '\'' +
-                "clientType='" + clientType + '\'' +
+                ", clientType='" + clientType + '\'' +
                 ", content='" + content.length + '\'' +
                 '}';
     }
diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java
index af697163..96162abc 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java
@@ -91,7 +91,7 @@ protected void channelRead0(ChannelHandlerContext ctx, String request) throws Ex
 
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
-        ctx.write("welcome to " + Utils.getInetAddress() + "!" + EOL);
+        ctx.write("welcome to " + Utils.getLocalIp() + "!" + EOL);
         ctx.write("It is " + new Date() + " now." + EOL + EOL);
         ctx.flush();
     }
@@ -236,7 +236,7 @@ public String handler(ChannelHandlerContext ctx, String args) {
                         LOGGER.info("delete connection server success:{}", data);
                         removeSuccess = true;
                     } else {
-                        LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), Utils.getInetAddress());
+                        LOGGER.info("delete connection server failed: required host:{}, but:{}", serverNode.getIp(), Utils.getLocalIp());
                     }
                 }
                 if (removeSuccess) {
diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
index 764de6e3..1ecc8dc2 100644
--- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
+++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java
@@ -28,14 +28,26 @@
 import com.mpush.common.message.PushMessage;
 import com.mpush.common.message.gateway.GatewayPushMessage;
 import com.mpush.common.router.RemoteRouter;
+import com.mpush.common.user.UserManager;
 import com.mpush.core.ack.AckCallback;
 import com.mpush.core.ack.AckContext;
 import com.mpush.core.ack.AckMessageQueue;
 import com.mpush.core.router.LocalRouter;
+import com.mpush.core.router.LocalRouterManager;
 import com.mpush.core.router.RouterCenter;
+import com.mpush.tools.Jsons;
 import com.mpush.tools.Utils;
+import com.mpush.tools.common.Pair;
 import com.mpush.tools.log.Logs;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.atomic.AtomicInteger;
+
 import static com.mpush.api.protocol.Command.ERROR;
 import static com.mpush.api.protocol.Command.OK;
 import static com.mpush.common.ErrorCode.*;
@@ -68,12 +80,79 @@ public GatewayPushMessage decode(Packet packet, Connection connection) {
      * 2.如果用户真在另一台机器,让PushClient清理下本地缓存后,重新推送 (解决场景1,3)
      * 

* - * @param message + * @param message message */ @Override public void handle(GatewayPushMessage message) { - if (!checkLocal(message)) { - checkRemote(message); + if (message.isBroadcast()) { + sendBroadcast(message); + } else { + if (!checkLocal(message)) { + checkRemote(message); + } + } + } + + /** + * 广播所有在线用户 + * + * @param message message + */ + private void sendBroadcast(GatewayPushMessage message) { + Set sendUserIds = new CopyOnWriteArraySet<>(); + LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); + AtomicInteger tasks = new AtomicInteger();//总任务数, 0表示任务全部结束 + long begin = System.currentTimeMillis(); + for (int start = 0, limit = 1000; ; start += limit) { + List userIds = UserManager.I.getOnlineUserList(start, limit); + tasks.addAndGet(userIds.size());//增加任务数 + + userIds.forEach(userId -> routerManager.lookupAll(userId).forEach(router -> { + Connection connection = router.getRouteValue(); + int clientType = router.getClientType(); + + if (connection.isConnected()) { + //TODO check tags sessionContext ? + + //3.链接可用,直接下发消息到手机客户端 + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.getPacket().flags = message.getPacket().flags; + + pushMessage.send(future -> { + + if (!sendUserIds.contains(userId)) { + tasks.decrementAndGet();//完成一个任务 + } + + if (future.isSuccess()) {//推送成功 + sendUserIds.add(userId); + Logs.PUSH.info("gateway broadcast client success, userId={}, message={}", userId, message); + + } else {//推送失败 + Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); + } + + if (tasks.get() == 0) {//任务全部结束 + Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); + } + }); + } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 + Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); + + tasks.decrementAndGet();//完成一个任务 + + //删除已经失效的本地路由 + RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); + + if (tasks.get() == 0) {//任务全部结束 + Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); + } + } + })); + + if (userIds.size() != limit) break;//查询完毕 } } @@ -81,8 +160,8 @@ public void handle(GatewayPushMessage message) { * 检查本地路由,如果存在并且链接可用直接推送 * 否则要检查下远程路由 * - * @param message - * @return + * @param message message + * @return true/false true:success */ private boolean checkLocal(final GatewayPushMessage message) { String userId = message.userId; @@ -136,7 +215,7 @@ private boolean checkLocal(final GatewayPushMessage message) { * 如果是本机直接删除路由信息 * 如果是其他机器让PushClient重推 * - * @param message + * @param message message */ private void checkRemote(GatewayPushMessage message) { String userId = message.userId; diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index c8913e04..bfbf4dec 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -42,7 +42,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ZKServerNode server = serverList.get(index); //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); - for (int i = 0; i < 1; i++) { + for (int i = 0; i < 100; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java index 0a89949e..fd2ff99f 100644 --- a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java +++ b/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java @@ -20,8 +20,8 @@ package com.mpush.test.gson; import com.google.common.collect.Maps; -import com.mpush.api.push.PushContent; -import com.mpush.api.push.PushContent.PushType; +import com.mpush.api.push.MsgType; +import com.mpush.api.push.PushMsg; import com.mpush.tools.Jsons; import org.junit.Test; @@ -35,7 +35,7 @@ public void test() { map.put("key1", 1121 + ""); map.put("key2", "value2"); - PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + PushMsg content = PushMsg.build(MsgType.MESSAGE, Jsons.toJson(map)); System.out.println(Jsons.toJson(content)); @@ -46,12 +46,12 @@ public void test() { public void test2() { ValueMap map = new ValueMap("1122", "value2"); - PushContent content = PushContent.build(PushType.MESSAGE, Jsons.toJson(map)); + PushMsg content = PushMsg.build(MsgType.MESSAGE, Jsons.toJson(map)); System.out.println(Jsons.toJson(content)); - PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class); + PushMsg newContetn = Jsons.fromJson(Jsons.toJson(content), PushMsg.class); System.out.println(newContetn.getContent()); diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index b6ab81c2..7fcdbbdb 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -19,18 +19,12 @@ package com.mpush.test.push; -import com.mpush.api.Constants; -import com.mpush.api.push.AckModel; -import com.mpush.api.push.PushCallback; -import com.mpush.api.push.PushContent; -import com.mpush.api.push.PushContent.PushType; -import com.mpush.api.push.PushSender; +import com.mpush.api.push.*; import com.mpush.api.router.ClientLocation; -import com.mpush.tools.Jsons; import com.mpush.tools.log.Logs; -import java.util.Arrays; -import java.util.concurrent.locks.LockSupport; +import java.util.List; +import java.util.concurrent.FutureTask; /** * Created by ohun on 2016/1/7. @@ -42,37 +36,42 @@ public static void main(String[] args) throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().get(); - for (int i = 0; i < 1; i++) { - PushContent content = PushContent.build(PushType.MESSAGE, "this a first push." + i); - content.setMsgId("msgId_" + (i % 2)); + PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); + msg.setMsgId("msgId_0"); - Thread.sleep(1000); - sender.send(Jsons.toJson(content).getBytes(Constants.UTF_8), - Arrays.asList("user-0", "doctor43test"), - AckModel.AUTO_ACK, - new PushCallback() { - @Override - public void onSuccess(String userId, ClientLocation location) { - System.err.println("push onSuccess userId=" + userId); - } + PushContext context = PushContext.build(msg) + .setBroadcast(true) + .setTimeout(100000) + .setCallback(new PushCallback() { - @Override - public void onFailure(String userId, ClientLocation location) { - System.err.println("push onFailure userId=" + userId); - } + @Override + public void onSuccess(List userIds) { + System.err.println("push onSuccess userId=" + userIds); + } - @Override - public void onOffline(String userId, ClientLocation location) { - System.err.println("push onOffline userId=" + userId); - } + @Override + public void onSuccess(String userId, ClientLocation location) { + System.err.println("push onSuccess userId=" + userId); + } - @Override - public void onTimeout(String userId, ClientLocation location) { - System.err.println("push onTimeout userId=" + userId); - } - }); - } - LockSupport.park(); + @Override + public void onFailure(String userId, ClientLocation location) { + System.err.println("push onFailure userId=" + userId); + } + + @Override + public void onOffline(String userId, ClientLocation location) { + System.err.println("push onOffline userId=" + userId); + } + + @Override + public void onTimeout(String userId, ClientLocation location) { + System.err.println("push onTimeout userId=" + userId); + } + }); + Thread.sleep(1000); + FutureTask future = sender.send(context); + future.get(); } } \ No newline at end of file From 871ed0ae134316e9b1f8424a5daca59bef7166b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 12 Sep 2016 21:33:32 +0800 Subject: [PATCH 629/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96,?= =?UTF-8?q?=20=E8=8E=B7=E5=8F=96=E6=9C=AC=E6=9C=BAIP=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redis/manager/ZKRedisClusterManager.java | 42 ++++++------ .../mpush/core/handler/HttpProxyHandler.java | 4 +- .../MPushUtilTest.java => util/IPTest.java} | 22 ++++--- .../src/main/java/com/mpush/tools/Utils.java | 64 ++++++------------- .../src/main/java/com/mpush/zk/ZKClient.java | 6 ++ 5 files changed, 64 insertions(+), 74 deletions(-) rename mpush-test/src/test/java/com/mpush/test/{redis/MPushUtilTest.java => util/IPTest.java} (65%) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index 1730e2f6..834175f0 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -34,12 +34,14 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import static com.mpush.zk.ZKPath.REDIS_SERVER; public class ZKRedisClusterManager implements RedisClusterManager { public static final ZKRedisClusterManager I = new ZKRedisClusterManager(); + private AtomicBoolean init = new AtomicBoolean(false); private ZKRedisClusterManager() { } @@ -51,26 +53,28 @@ private ZKRedisClusterManager() { */ @Override public void init() { - Logs.Console.info("begin init redis cluster"); - if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); - List groupList = CC.mp.redis.cluster_group; - - if (CollectionUtils.isNotEmpty(groupList)) { - register(groupList); - } - - ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); - watcher.beginWatch(); - Collection nodes = watcher.getCache().values(); - if (CollectionUtils.isEmpty(nodes)) { - Logs.REDIS.error("init redis client error, redis server is none."); - throw new RedisException("init redis client error, redis server is none."); + if (init.compareAndSet(false, true)) { + Logs.Console.info("begin init redis cluster"); + if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); + List groupList = CC.mp.redis.cluster_group; + + if (CollectionUtils.isNotEmpty(groupList)) { + register(groupList); + } + + ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); + watcher.beginWatch(); + Collection nodes = watcher.getCache().values(); + if (CollectionUtils.isEmpty(nodes)) { + Logs.REDIS.error("init redis client error, redis server is none."); + throw new RedisException("init redis client error, redis server is none."); + } + + nodes.stream().map(RedisGroup::from).forEach(groups::add); + + if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); + Logs.Console.info("init redis cluster success..."); } - - nodes.stream().map(RedisGroup::from).forEach(groups::add); - - if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); - Logs.Console.info("init redis cluster success..."); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index a1ad9985..003b9c7d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -118,9 +118,9 @@ public void onResponse(HttpResponse httpResponse) { HttpResponseMessage response = HttpResponseMessage .from(request) .setStatusCode(httpResponse.status().code()) - .setReasonPhrase(httpResponse.status().reasonPhrase().toString()); + .setReasonPhrase(httpResponse.status().reasonPhrase()); for (Map.Entry entry : httpResponse.headers()) { - response.addHeader(entry.getKey().toString(), entry.getValue().toString()); + response.addHeader(entry.getKey(), entry.getValue()); } if (httpResponse instanceof FullHttpResponse) { diff --git a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java b/mpush-test/src/test/java/com/mpush/test/util/IPTest.java similarity index 65% rename from mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java rename to mpush-test/src/test/java/com/mpush/test/util/IPTest.java index 43a30342..c58d34b7 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/MPushUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/util/IPTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -14,19 +14,25 @@ * limitations under the License. * * Contributors: - * ohun@live.cn (夜色) + * ohun@live.cn (夜色) */ -package com.mpush.test.redis; +package com.mpush.test.util; import com.mpush.tools.Utils; import org.junit.Test; -public class MPushUtilTest { - +/** + * Created by ohun on 16/9/8. + * + * @author ohun@live.cn (夜色) + */ +public class IPTest { @Test - public void getIp() throws Exception { - System.out.println(Utils.getExtranetAddress()); - } + public void getLocalIP() throws Exception { + System.out.println(Utils.getLocalIp()); + System.out.println(Utils.getExtranetIp()); + } } + diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index 65c439a7..a27f7d24 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -57,78 +57,52 @@ public static boolean isLocalHost(String host) { public static String getLocalIp() { if (LOCAL_IP == null) { - LOCAL_IP = getInetAddress(); + LOCAL_IP = getInetAddress(true); } return LOCAL_IP; } /** - * 获取本机ip * 只获取第一块网卡绑定的ip地址 * - * @return 本机ip + * @param getLocal 局域网IP + * @return ip */ - public static String getInetAddress() { + public static String getInetAddress(boolean getLocal) { try { - Profiler.enter("start get inet address"); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address; while (interfaces.hasMoreElements()) { - NetworkInterface ni = interfaces.nextElement(); - Enumeration addresses = ni.getInetAddresses(); + Enumeration addresses = interfaces.nextElement().getInetAddresses(); while (addresses.hasMoreElements()) { - address = addresses.nextElement(); - if (!address.isLoopbackAddress() - && address.getHostAddress().indexOf(":") == -1 - && address.isSiteLocalAddress()) { - return address.getHostAddress(); + InetAddress address = addresses.nextElement(); + if (address.isLoopbackAddress()) continue; + if (address.getHostAddress().contains(":")) continue; + if (getLocal) { + if (address.isSiteLocalAddress()) { + return address.getHostAddress(); + } + } else { + if (!address.isSiteLocalAddress()) { + return address.getHostAddress(); + } } } } LOGGER.warn("getInetAddress is null"); - return "127.0.0.1"; + return getLocal ? "127.0.0.1" : null; } catch (Throwable e) { LOGGER.error("getInetAddress exception", e); - return "127.0.0.1"; - } finally { - Profiler.release(); + return getLocal ? "127.0.0.1" : null; } } public static String getExtranetIp() { if (EXTRANET_IP == null) { - EXTRANET_IP = getExtranetAddress(); + EXTRANET_IP = getInetAddress(false); } return EXTRANET_IP; } - /** - * 获取外网IP - * - * @return 外网IP - */ - public static String getExtranetAddress() { - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress address; - while (interfaces.hasMoreElements()) { - NetworkInterface ni = interfaces.nextElement(); - Enumeration addresses = ni.getInetAddresses(); - while (addresses.hasMoreElements()) { - address = addresses.nextElement(); - if (!address.isLoopbackAddress() - && address.getHostAddress().indexOf(":") == -1 - && !address.isSiteLocalAddress()) { - return address.getHostAddress(); - } - } - } - LOGGER.warn("getExtranetAddress is null"); - } catch (Throwable e) { - LOGGER.error("getExtranetAddress exception", e); - } - return null; - } public static String headerToString(Map headers) { if (headers != null && headers.size() > 0) { diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 73be4cc2..b4640917 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -52,6 +52,12 @@ private synchronized static ZKClient I() { private ZKClient() { } + @Override + public void start(Listener listener) { + if (isRunning()) return; + super.start(listener); + } + @Override protected void doStart(Listener listener) throws Throwable { client.start(); From d0208ab3de99b909dcc46e1d3110907e351829bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 13 Sep 2016 14:24:42 +0800 Subject: [PATCH 630/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/bootstrap/Main.java | 42 +++++-------------- .../mpush/core/server/ConnectionServer.java | 6 ++- .../core/server/ServerChannelHandler.java | 5 +++ 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 84450f8b..a9bc77f1 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -20,8 +20,6 @@ package com.mpush.bootstrap; import com.mpush.tools.log.Logs; -import sun.misc.Signal; -import sun.misc.SignalHandler; public class Main { public static void main(String[] args) { @@ -32,36 +30,18 @@ public static void main(String[] args) { addHook(launcher); } - private static void addHook(final ServerLauncher launcher) { - Hook hook = new Hook(launcher); - //Signal.handle(new Signal("USR2"), hook); - Runtime.getRuntime().addShutdownHook(new Thread(hook, "mpush-hook-thread")); - } - - private static class Hook implements Runnable, SignalHandler { - private final ServerLauncher launcher; - - private Hook(ServerLauncher launcher) { - this.launcher = launcher; - } - - @Override - public void run() { - stop(); - } + private static void addHook(ServerLauncher launcher) { + Runtime.getRuntime().addShutdownHook( + new Thread(() -> { - @Override - public void handle(Signal signal) { - stop(); - } + try { + launcher.stop(); + } catch (Exception e) { + Logs.Console.error("mpush server stop ex", e); + } + Logs.Console.info("jvm exit, all service stopped..."); - private void stop() { - try { - launcher.stop(); - } catch (Exception e) { - Logs.Console.error("mpush server stop ex", e); - } - Logs.Console.info("jvm exit, all server stopped..."); - } + }, "mpush-shutdown-hook-thread") + ); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index c0d1d8a9..f0d8dee1 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -43,6 +43,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn (夜色) */ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; @@ -88,7 +90,9 @@ public void stop(Listener listener) { trafficShapingHandler.release(); } super.stop(listener); - if (httpClient != null) httpClient.stop(); + if (httpClient != null && httpClient.isRunning()) { + httpClient.stop(); + } connectionManager.destroy(); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 800dab32..17f7b598 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -97,4 +97,9 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { EventBus.I.post(new ConnectionCloseEvent(connection)); Logs.Conn.info("client disconnect channel={}, connection={}", ctx.channel(), connection); } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + super.userEventTriggered(ctx, evt); + } } \ No newline at end of file From e5efed103be33f06b804389000aa14a33fa10e3e Mon Sep 17 00:00:00 2001 From: ohun Date: Thu, 15 Sep 2016 00:03:57 +0800 Subject: [PATCH 631/890] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 67c285a5..428efbb6 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ * 文档:[https://mpusher.github.io/docs](https://mpusher.github.io/docs) * QQ群:__114583699__ MPUSH开源消息推送系统 +## 各服务依赖关系图 +![](https://mpusher.github.io/docs/服务依赖关系.png) + ## 源码测试 1. ```git clone https://github.com/mpusher/mpush.git``` 2. 导入到eclipse或Intellij IDEA From 3767709fd52272255ee06e89acc6ba26c3000935 Mon Sep 17 00:00:00 2001 From: ohun Date: Thu, 15 Sep 2016 00:05:00 +0800 Subject: [PATCH 632/890] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 428efbb6..8a44acd9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ * 文档:[https://mpusher.github.io/docs](https://mpusher.github.io/docs) * QQ群:__114583699__ MPUSH开源消息推送系统 -## 各服务依赖关系图 +## 服务调用关系 ![](https://mpusher.github.io/docs/服务依赖关系.png) ## 源码测试 From 3f18cf5488b8e3911c5798a4ec0cb608d33cabc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 19 Sep 2016 15:17:58 +0800 Subject: [PATCH 633/890] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=96=B9=E5=BC=8F=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E7=94=B1=E5=8E=9F=E6=9D=A5=E7=9A=84=E7=9B=B4=E6=8E=A5=E5=88=A0?= =?UTF-8?q?=E9=99=A4=EF=BC=8C=E6=94=B9=E4=B8=BAconnId=3Dnull,=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E7=94=A8=E6=88=B7=E5=B7=B2=E7=BB=8F=E4=B8=8B=E7=BA=BF?= =?UTF-8?q?=EF=BC=8C=E5=85=B6=E4=BB=96=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/api/event/Topics.java | 31 +++++++++++++++ .../java/com/mpush/api/push/PushResult.java | 32 ++++++++++++++++ .../com/mpush/api/router/ClientLocation.java | 13 +++++++ .../com/mpush/cache/redis/RedisClient.java | 9 ++++- .../com/mpush/cache/redis/RedisGroup.java | 1 + .../com/mpush/client/push/PushClient.java | 15 +++----- .../com/mpush/client/push/PushRequest.java | 17 ++++----- .../client/user/UserStatusChangeListener.java | 38 +++++-------------- ...er.java => CachedRemoteRouterManager.java} | 22 +++++------ .../com/mpush/common/router/RemoteRouter.java | 8 ++++ .../common/router/RemoteRouterManager.java | 23 +++++++---- .../com/mpush/common/user/UserManager.java | 4 +- .../core/handler/GatewayPushHandler.java | 16 ++++---- .../com/mpush/core/router/KickRemoteMsg.java | 2 + .../mpush/core/router/LocalRouterManager.java | 10 ++--- .../com/mpush/core/router/RouterCenter.java | 14 +------ .../core/router/RouterChangeListener.java | 33 ++++++++-------- ...neListener.java => UserEventConsumer.java} | 19 ++++------ .../mpush/test/client/ConnClientTestMain.java | 4 +- .../mpush/test/push/PushClientTestMain.java | 3 +- mpush-test/src/test/resources/logback.xml | 2 +- .../java/com/mpush/tools/common/TimeLine.java | 21 ++++++++-- 22 files changed, 205 insertions(+), 132 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/event/Topics.java create mode 100644 mpush-api/src/main/java/com/mpush/api/push/PushResult.java rename mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java => mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java (59%) rename mpush-common/src/main/java/com/mpush/common/router/{ConnectionRouterManager.java => CachedRemoteRouterManager.java} (73%) rename mpush-core/src/main/java/com/mpush/core/router/{UserOnlineOfflineListener.java => UserEventConsumer.java} (74%) diff --git a/mpush-api/src/main/java/com/mpush/api/event/Topics.java b/mpush-api/src/main/java/com/mpush/api/event/Topics.java new file mode 100644 index 00000000..fc6815d5 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/event/Topics.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.event; + +/** + * Created by ohun on 16/9/19. + * + * @author ohun@live.cn (夜色) + */ +public interface Topics { + String ONLINE_CHANNEL = "/mpush/online/"; + + String OFFLINE_CHANNEL = "/mpush/offline/"; +} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java new file mode 100644 index 00000000..97de4d05 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java @@ -0,0 +1,32 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.push; + +import com.mpush.api.router.ClientLocation; + +/** + * Created by ohun on 16/9/17. + * + * @author ohun@live.cn (夜色) + */ +public class PushResult { + private Object[] timeLine; + private ClientLocation location; +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index ca1f4102..a3fba473 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -107,6 +107,19 @@ public int getClientType() { return clientType; } + public boolean isOnline() { + return connId != null; + } + + public boolean isOffline() { + return connId == null; + } + + public ClientLocation offline() { + this.connId = null; + return this; + } + public static ClientLocation from(Connection connection) { SessionContext context = connection.getSessionContext(); ClientLocation location = new ClientLocation(); diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java index e9ded643..9bd0ca90 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java @@ -63,8 +63,13 @@ private static JedisPoolConfig buildConfig() { public static Jedis getClient(RedisServer node) { JedisPool pool = POOL_MAP.get(node); if (pool == null) { - pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), DEFAULT_TIMEOUT, node.getPassword()); - POOL_MAP.put(node, pool); + synchronized (POOL_MAP) { + pool = POOL_MAP.get(node); + if (pool == null) { + pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), DEFAULT_TIMEOUT, node.getPassword()); + POOL_MAP.put(node, pool); + } + } } return pool.getResource(); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java index cd11ef06..61a2128d 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java @@ -55,6 +55,7 @@ public void clear() { } public RedisServer get(String key) { + if (redisServerList.size() == 1) return redisServerList.get(0); int i = key.hashCode() % redisServerList.size(); return redisServerList.get(i); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 57dcdfae..e01e4e5e 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -19,21 +19,18 @@ package com.mpush.client.push; -import com.mpush.api.Constants; import com.mpush.api.connection.Connection; import com.mpush.api.push.*; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.gateway.GatewayClientFactory; -import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.CachedRemoteRouterManager; import com.mpush.common.router.RemoteRouter; -import com.mpush.tools.Jsons; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; import java.util.Collection; -import java.util.Objects; import java.util.Set; import java.util.concurrent.FutureTask; @@ -42,19 +39,19 @@ /*package*/ final class PushClient extends BaseService implements PushSender { private static final int DEFAULT_TIMEOUT = 3000; private final GatewayClientFactory factory = GatewayClientFactory.I; - private final ConnectionRouterManager routerManager = ConnectionRouterManager.I; + private final CachedRemoteRouterManager routerManager = CachedRemoteRouterManager.I; private FutureTask send0(PushContext ctx) { if (ctx.isBroadcast()) { return PushRequest.build(this, ctx).broadcast(); } else { - Set routers = routerManager.lookupAll(ctx.getUserId()); - if (routers == null || routers.isEmpty()) { + Set remoteRouters = routerManager.lookupAll(ctx.getUserId()); + if (remoteRouters == null || remoteRouters.isEmpty()) { return PushRequest.build(this, ctx).offline(); } FutureTask task = null; - for (RemoteRouter router : routers) { - task = PushRequest.build(this, ctx).send(router); + for (RemoteRouter remoteRouter : remoteRouters) { + task = PushRequest.build(this, ctx).send(remoteRouter); } return task; } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 4cad9559..c6bbc8c0 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -24,14 +24,13 @@ import com.mpush.api.push.*; import com.mpush.api.router.ClientLocation; import com.mpush.common.message.gateway.GatewayPushMessage; -import com.mpush.common.router.ConnectionRouterManager; +import com.mpush.common.router.CachedRemoteRouterManager; import com.mpush.common.router.RemoteRouter; import com.mpush.tools.Jsons; import com.mpush.tools.common.TimeLine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -63,10 +62,10 @@ private enum Status {init, success, failure, offline, timeout} private Future future; private String result; - private void sendToConnServer(RemoteRouter router) { + private void sendToConnServer(RemoteRouter remoteRouter) { timeLine.addTimePoint("lookup-remote"); - if (router == null) { + if (remoteRouter == null || remoteRouter.isOffline()) { //1.1没有查到说明用户已经下线 offline(); return; @@ -76,7 +75,7 @@ private void sendToConnServer(RemoteRouter router) { //2.通过网关连接,把消息发送到所在机器 - location = router.getRouteValue(); + location = remoteRouter.getRouteValue(); Connection gatewayConn = client.getGatewayConnection(location.getHost()); if (gatewayConn == null) { LOGGER.error("get gateway connection failure, location={}", location); @@ -154,15 +153,15 @@ public FutureTask send(RemoteRouter router) { public void redirect() { timeLine.addTimePoint("redirect"); LOGGER.warn("user route has changed, userId={}, location={}", userId, location); - ConnectionRouterManager.I.invalidateLocalCache(userId); + CachedRemoteRouterManager.I.invalidateLocalCache(userId); if (status.get() == Status.init) {//表示任务还没有完成,还可以重新发送 - RemoteRouter route = ConnectionRouterManager.I.lookup(userId, location.getClientType()); - send(route); + RemoteRouter remoteRouter = CachedRemoteRouterManager.I.lookup(userId, location.getClientType()); + send(remoteRouter); } } public FutureTask offline() { - ConnectionRouterManager.I.invalidateLocalCache(userId); + CachedRemoteRouterManager.I.invalidateLocalCache(userId); submit(Status.offline); return this; } diff --git a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java b/mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java similarity index 59% rename from mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java rename to mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java index e619ddf4..e43375bf 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/UserChangeListener.java +++ b/mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java @@ -17,56 +17,38 @@ * ohun@live.cn (夜色) */ -package com.mpush.common.router; +package com.mpush.client.user; import com.mpush.cache.redis.listener.ListenerDispatcher; import com.mpush.cache.redis.listener.MessageListener; -import com.mpush.cache.redis.manager.RedisManager; import com.mpush.tools.Utils; -import com.mpush.tools.event.EventConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.mpush.api.event.Topics.OFFLINE_CHANNEL; +import static com.mpush.api.event.Topics.ONLINE_CHANNEL; + /** * Created by ohun on 2016/1/4. * * @author ohun@live.cn */ -public class UserChangeListener extends EventConsumer implements MessageListener { - - private static final Logger LOGGER = LoggerFactory.getLogger(UserChangeListener.class); - - public static final String ONLINE_CHANNEL = "/mpush/online/"; +public class UserStatusChangeListener implements MessageListener { - public static final String OFFLINE_CHANNEL = "/mpush/offline/"; + private static final Logger LOGGER = LoggerFactory.getLogger(UserStatusChangeListener.class); //只需要一台机器注册online、offline 消息通道 - public UserChangeListener() { + public UserStatusChangeListener() { if ("127.0.0.1".equals(Utils.getLocalIp())) { - ListenerDispatcher.I.subscribe(getOnlineChannel(), this); - ListenerDispatcher.I.subscribe(getOfflineChannel(), this); + ListenerDispatcher.I.subscribe(ONLINE_CHANNEL, this); + ListenerDispatcher.I.subscribe(OFFLINE_CHANNEL, this); } else { LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", Utils.getLocalIp()); } } - public String getOnlineChannel() { - return ONLINE_CHANNEL; - } - - public String getOfflineChannel() { - return OFFLINE_CHANNEL; - } - - public void userOnline(String userId) { - RedisManager.I.publish(getOnlineChannel(), userId); - } - - public void userOffline(String userId) { - RedisManager.I.publish(getOnlineChannel(), userId); - } - @Override public void onMessage(String channel, String message) { + } } diff --git a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/CachedRemoteRouterManager.java similarity index 73% rename from mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java rename to mpush-common/src/main/java/com/mpush/common/router/CachedRemoteRouterManager.java index fec83fde..75720dba 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ConnectionRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/CachedRemoteRouterManager.java @@ -21,17 +21,17 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import com.mpush.api.router.ClientType; import java.util.Set; import java.util.concurrent.TimeUnit; /** * Created by ohun on 2016/1/4. + * + * @author ohun@live.cn (夜色) */ -public final class ConnectionRouterManager extends RemoteRouterManager { - public static final ConnectionRouterManager I = new ConnectionRouterManager(); - // TODO: 2015/12/30 可以增加一层本地缓存,防止疯狂查询redis, 但是要注意失效问题及数据不一致问题 +public final class CachedRemoteRouterManager extends RemoteRouterManager { + public static final CachedRemoteRouterManager I = new CachedRemoteRouterManager(); private final Cache> cache = CacheBuilder .newBuilder() .expireAfterWrite(5, TimeUnit.MINUTES) @@ -42,19 +42,19 @@ public final class ConnectionRouterManager extends RemoteRouterManager { public Set lookupAll(String userId) { Set cached = cache.getIfPresent(userId); if (cached != null) return cached; - Set router = super.lookupAll(userId); - if (router != null) { - cache.put(userId, router); + Set remoteRouters = super.lookupAll(userId); + if (remoteRouters != null) { + cache.put(userId, remoteRouters); } - return router; + return remoteRouters; } @Override public RemoteRouter lookup(String userId, int clientType) { Set cached = this.lookupAll(userId); - for (RemoteRouter router : cached) { - if (router.getRouteValue().getClientType() == clientType) { - return router; + for (RemoteRouter remoteRouter : cached) { + if (remoteRouter.getRouteValue().getClientType() == clientType) { + return remoteRouter; } } return null; diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java index d7d9efed..d3a0b1ac 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouter.java @@ -34,6 +34,14 @@ public RemoteRouter(ClientLocation clientLocation) { this.clientLocation = clientLocation; } + public boolean isOnline(){ + return clientLocation.isOnline(); + } + + public boolean isOffline(){ + return clientLocation.isOffline(); + } + @Override public ClientLocation getRouteValue() { return clientLocation; diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index ef71818d..8d6546f7 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -47,23 +47,30 @@ public class RemoteRouterManager extends EventConsumer implements RouterManager< @Override public RemoteRouter register(String userId, RemoteRouter router) { - LOGGER.info("register remote router success userId={}, router={}", userId, router); String key = RedisKey.getUserKey(userId); String field = Integer.toString(router.getRouteValue().getClientType()); ClientLocation old = RedisManager.I.hget(key, field, ClientLocation.class); - if (old != null) { - RedisManager.I.hdel(key, field); - } RedisManager.I.hset(key, field, router.getRouteValue()); + LOGGER.info("register remote router success userId={}, newRouter={}, oldRoute={}", userId, router, old); return old == null ? null : new RemoteRouter(old); } + /** + * 目前的实现方式是非原子操作(get->set),可能会有并发问题,虽然概率很低 + * 后续考虑采用lua脚本,实现原子操作 + * + * @param userId 用户ID + * @param clientType 客户端类型 + * @return 删除路由是否成功 + */ @Override public boolean unRegister(String userId, int clientType) { String key = RedisKey.getUserKey(userId); String field = Integer.toString(clientType); - RedisManager.I.hdel(key, field); - LOGGER.info("unRegister remote router success userId={}, clientType={}", userId, clientType); + ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); + if (location == null || location.isOffline()) return true; + RedisManager.I.hset(key, field, location.offline()); + LOGGER.info("unRegister remote router success userId={}, route={}", userId, location); return true; } @@ -99,12 +106,12 @@ void on(ConnectionCloseEvent event) { String key = RedisKey.getUserKey(userId); String field = Integer.toString(context.getClientType()); ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); - if (location == null) return; + if (location == null || location.isOffline()) return; String connId = connection.getId(); //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 if (connId.equals(location.getConnId())) { - RedisManager.I.hdel(key, field); + RedisManager.I.hset(key, field, location.offline()); LOGGER.info("clean disconnected remote route, userId={}, route={}", userId, location); } else { LOGGER.info("clean disconnected remote route, not clean:userId={}, route={}", userId, location); diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java index 1a4e7981..eb1411a0 100644 --- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -38,12 +38,12 @@ public void clearUserOnlineData() { RedisManager.I.del(ONLINE_KEY); } - public void recordUserOnline(String userId) { + public void addToOnlineList(String userId) { RedisManager.I.zAdd(ONLINE_KEY, userId); LOGGER.info("user online {}", userId); } - public void recordUserOffline(String userId) { + public void remFormOnlineList(String userId) { RedisManager.I.zRem(ONLINE_KEY, userId); LOGGER.info("user offline {}", userId); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 1ecc8dc2..6d825ace 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -166,12 +166,12 @@ private void sendBroadcast(GatewayPushMessage message) { private boolean checkLocal(final GatewayPushMessage message) { String userId = message.userId; int clientType = message.clientType; - LocalRouter router = RouterCenter.I.getLocalRouterManager().lookup(userId, clientType); + LocalRouter localRouter = RouterCenter.I.getLocalRouterManager().lookup(userId, clientType); //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 - if (router == null) return false; + if (localRouter == null) return false; - Connection connection = router.getRouteValue(); + Connection connection = localRouter.getRouteValue(); //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { @@ -220,10 +220,10 @@ private boolean checkLocal(final GatewayPushMessage message) { private void checkRemote(GatewayPushMessage message) { String userId = message.userId; int clientType = message.clientType; - RemoteRouter router = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); + RemoteRouter remoteRouter = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); // 1.如果远程路由信息也不存在, 说明用户此时不在线, - if (router == null) { + if (remoteRouter == null || remoteRouter.isOffline()) { ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); @@ -233,14 +233,14 @@ private void checkRemote(GatewayPushMessage message) { } //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 - if (Utils.getLocalIp().equals(router.getRouteValue().getHost())) { + if (Utils.getLocalIp().equals(remoteRouter.getRouteValue().getHost())) { ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); //删除失效的远程缓存 RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); - Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, router); + Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, remoteRouter); return; } @@ -248,7 +248,7 @@ private void checkRemote(GatewayPushMessage message) { //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).sendRaw(); - Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, router); + Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, remoteRouter); } diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java index f670f428..74baad58 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java @@ -29,6 +29,7 @@ public final class KickRemoteMsg { public String userId; public String deviceId; + public String connId; public int clientType; public String targetServer; @@ -37,6 +38,7 @@ public String toString() { return "KickRemoteMsg{" + "userId='" + userId + '\'' + ", deviceId='" + deviceId + '\'' + + ", connId='" + connId + '\'' + ", clientType='" + clientType + '\'' + ", targetServer='" + targetServer + '\'' + '}'; diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 352e3e38..2ed8d22c 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -90,17 +90,17 @@ void on(ConnectionCloseEvent event) { EventBus.I.post(new UserOfflineEvent(event.connection, userId)); int clientType = context.getClientType(); - LocalRouter router = routers.getOrDefault(userId, EMPTY).get(clientType); - if (router == null) return; + LocalRouter localRouter = routers.getOrDefault(userId, EMPTY).get(clientType); + if (localRouter == null) return; String connId = connection.getId(); //2.检测下,是否是同一个链接, 如果客户端重连,老的路由会被新的链接覆盖 - if (connId.equals(router.getRouteValue().getId())) { + if (connId.equals(localRouter.getRouteValue().getId())) { //3.删除路由 routers.getOrDefault(userId, EMPTY).remove(clientType); - LOGGER.info("clean disconnected local route, userId={}, route={}", userId, router); + LOGGER.info("clean disconnected local route, userId={}, route={}", userId, localRouter); } else { //如果不相等,则log一下 - LOGGER.info("clean disconnected local route, not clean:userId={}, route={}", userId, router); + LOGGER.info("clean disconnected local route, not clean:userId={}, route={}", userId, localRouter); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index 0140d2a4..2a5c1694 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -22,7 +22,6 @@ import com.mpush.api.connection.Connection; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; -import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; @@ -31,8 +30,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Set; - /** * Created by ohun on 2015/12/23. * @@ -45,7 +42,7 @@ public final class RouterCenter { private final LocalRouterManager localRouterManager = new LocalRouterManager(); private final RemoteRouterManager remoteRouterManager = new RemoteRouterManager(); private final RouterChangeListener routerChangeListener = new RouterChangeListener(); - private final UserOnlineOfflineListener userOnlineOfflineListener = new UserOnlineOfflineListener(); + private final UserEventConsumer userEventConsumer = new UserEventConsumer(); /** @@ -77,7 +74,7 @@ public boolean register(String userId, Connection connection) { LOGGER.info("register router success, find old local router={}, userId={}", oldLocalRouter, userId); } - if (oldRemoteRouter != null) { + if (oldRemoteRouter != null && oldRemoteRouter.isOnline()) { EventBus.I.post(new RouterChangeEvent(userId, oldRemoteRouter)); LOGGER.info("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId); } @@ -97,13 +94,6 @@ public Router lookup(String userId, int clientType) { return remote; } - public Set> lookupAll(String userId) { - Set locals = localRouterManager.lookupAll(userId); - if (locals != null) return locals; - Set remotes = remoteRouterManager.lookupAll(userId); - return remotes; - } - public LocalRouterManager getLocalRouterManager() { return localRouterManager; } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index e0f5b61f..2f59ec69 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -24,9 +24,7 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.RouterChangeEvent; import com.mpush.api.router.ClientLocation; -import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; -import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.listener.ListenerDispatcher; import com.mpush.cache.redis.listener.MessageListener; import com.mpush.cache.redis.manager.RedisManager; @@ -34,7 +32,6 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.tools.Jsons; import com.mpush.tools.Utils; -import com.mpush.tools.config.ConfigManager; import com.mpush.tools.event.EventConsumer; import com.mpush.tools.log.Logs; @@ -98,10 +95,10 @@ public void kickLocal(final String userId, final LocalRouter router) { * 如果client连续2次链接到同一台机器上就有会出现这中情况 * * @param userId - * @param router + * @param remoteRouter */ - public void kickRemote(String userId, RemoteRouter router) { - ClientLocation location = router.getRouteValue(); + public void kickRemote(String userId, RemoteRouter remoteRouter) { + ClientLocation location = remoteRouter.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(Utils.getLocalIp())) { Logs.Conn.info("kick remote user but router in local, userId={}", userId); @@ -112,6 +109,7 @@ public void kickRemote(String userId, RemoteRouter router) { //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 KickRemoteMsg msg = new KickRemoteMsg(); msg.deviceId = location.getDeviceId(); + msg.connId = location.getConnId(); msg.clientType = location.getClientType(); msg.targetServer = location.getHost(); msg.userId = userId; @@ -136,15 +134,18 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //2.查询本地路由,找到要被踢下线的链接,并删除该本地路由 String userId = msg.userId; int clientType = msg.clientType; - LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); - LocalRouter router = routerManager.lookup(userId, clientType); - if (router != null) { + LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter localRouter = localRouterManager.lookup(userId, clientType); + if (localRouter != null) { Logs.Conn.info("receive kick remote msg, msg={}", msg); - //2.1删除本地路由信息 - routerManager.unRegister(userId, clientType); - //2.2发送踢人消息到客户端 - kickLocal(userId, router); - remStatUser(userId); + if (localRouter.getRouteValue().getId().equals(msg.connId)) {//二次校验,防止误杀 + //2.1删除本地路由信息 + localRouterManager.unRegister(userId, clientType); + //2.2发送踢人消息到客户端 + kickLocal(userId, localRouter); + } else { + Logs.Conn.warn("kick router connId was wrong, localRouter={}, msg={}", localRouter, msg); + } } else { Logs.Conn.info("no local router find, kick failure, msg={}", msg); } @@ -163,8 +164,4 @@ public void onMessage(String channel, String message) { Logs.Conn.info("receive an error redis channel={}", channel); } } - - private void remStatUser(String userId) { - RedisManager.I.zRem(RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()), userId); - } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java b/mpush-core/src/main/java/com/mpush/core/router/UserEventConsumer.java similarity index 74% rename from mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java rename to mpush-core/src/main/java/com/mpush/core/router/UserEventConsumer.java index d0987f04..5451df4f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/UserOnlineOfflineListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/UserEventConsumer.java @@ -24,32 +24,27 @@ import com.mpush.api.event.UserOnlineEvent; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.user.UserManager; -import com.mpush.tools.event.EventBus; +import com.mpush.tools.event.EventConsumer; + +import static com.mpush.api.event.Topics.OFFLINE_CHANNEL; +import static com.mpush.api.event.Topics.ONLINE_CHANNEL; /** * Created by ohun on 2015/12/23. * * @author ohun@live.cn */ -public final class UserOnlineOfflineListener { - - public static final String ONLINE_CHANNEL = "/mpush/online/"; - - public static final String OFFLINE_CHANNEL = "/mpush/offline/"; - - public UserOnlineOfflineListener() { - EventBus.I.register(this); - } +/*package*/ final class UserEventConsumer extends EventConsumer { @Subscribe void on(UserOnlineEvent event) { - UserManager.I.recordUserOnline(event.getUserId()); + UserManager.I.addToOnlineList(event.getUserId()); RedisManager.I.publish(ONLINE_CHANNEL, event.getUserId()); } @Subscribe void on(UserOfflineEvent event) { - UserManager.I.recordUserOffline(event.getUserId()); + UserManager.I.remFormOnlineList(event.getUserId()); RedisManager.I.publish(OFFLINE_CHANNEL, event.getUserId()); } } diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index bfbf4dec..02379dd2 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -26,6 +26,7 @@ import com.mpush.tools.log.Logs; import com.mpush.zk.node.ZKServerNode; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; @@ -41,8 +42,7 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ZKServerNode server = serverList.get(index); - //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 1; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 7fcdbbdb..d0ae3750 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -40,7 +40,8 @@ public static void main(String[] args) throws Exception { msg.setMsgId("msgId_0"); PushContext context = PushContext.build(msg) - .setBroadcast(true) + .setBroadcast(false) + .setUserId("user-0") .setTimeout(100000) .setCallback(new PushCallback() { diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index 0d67de0e..95ebebbe 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -23,7 +23,7 @@ - + diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java index 112cf893..eb1923a0 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java @@ -30,6 +30,7 @@ public final class TimeLine { private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); private final TimePoint root = new TimePoint("root"); private final String name; + private int pointCount; private TimePoint current = root; public TimeLine() { @@ -46,6 +47,7 @@ public void begin() { public void addTimePoint(String name) { current = current.next = new TimePoint(name); + pointCount++; } public void end() { @@ -60,7 +62,7 @@ public void clean() { public String toString() { StringBuilder sb = new StringBuilder(name); if (root.next != null) { - sb.append('[').append(current.point - root.next.point).append(']'); + sb.append('[').append(current.time - root.next.time).append(']'); } sb.append('{'); TimePoint next = root; @@ -71,9 +73,20 @@ public String toString() { return sb.toString(); } + public Object[] getTimePoints() { + Object[] arrays = new Object[2 * pointCount]; + int i = 0; + TimePoint next = root; + while ((next = next.next) != null) { + arrays[i++] = next.name; + arrays[i++] = next.time; + } + return arrays; + } + private static class TimePoint { private final String name; - private final long point = System.currentTimeMillis(); + private final long time = System.currentTimeMillis(); private TimePoint next; public TimePoint(String name) { @@ -86,9 +99,9 @@ public void setNext(TimePoint next) { @Override public String toString() { - String header = name + "[" + formatter.format(new Date(point)) + "]"; + String header = name + "[" + formatter.format(new Date(time)) + "]"; if (next == null) return header; - return header + " --" + (next.point - point) + "(ms)--> "; + return header + " --" + (next.time - time) + "(ms)--> "; } } } From 9425030856387b50ff3aef64815d0732291513c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 13:50:11 +0800 Subject: [PATCH 634/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8DZK=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=96=AD=E5=BC=80=E9=87=8D=E8=BF=9E=E5=90=8E=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E8=8A=82=E7=82=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/zk/ZKClient.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index b4640917..0d4114bd 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -30,13 +30,13 @@ import org.apache.curator.framework.api.ACLProvider; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.concurrent.TimeUnit; public class ZKClient extends BaseService { @@ -44,6 +44,7 @@ public class ZKClient extends BaseService { private ZKConfig zkConfig; private CuratorFramework client; private TreeCache cache; + private Map ephemeralNodes = new LinkedHashMap<>(); private synchronized static ZKClient I() { return I == null ? new ZKClient() : I; @@ -120,10 +121,12 @@ public List getAclForPath(final String path) { // 注册连接状态监听器 private void addConnectionStateListener() { - client.getConnectionStateListenable() - .addListener((cli, newState) - -> Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", - newState, newState.isConnected())); + client.getConnectionStateListenable().addListener((cli, newState) -> { + if (newState == ConnectionState.RECONNECTED) { + ephemeralNodes.forEach(this::registerEphemeralSequential); + } + Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); + }); } // 本地缓存 @@ -261,6 +264,7 @@ public void registerEphemeral(final String key, final String value) { public void registerEphemeralSequential(final String key, final String value) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); + ephemeralNodes.put(key, value); } catch (Exception ex) { Logs.ZK.error("persistEphemeralSequential:{},{}", key, value, ex); throw new ZKException(ex); From 58750b6d289a42a9860d67d63f77044236a09d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 16:01:02 +0800 Subject: [PATCH 635/890] upgrade version -> 0.0.4 --- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 4 ++++ pom.xml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 0d4114bd..7b7fb0e8 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -46,6 +46,10 @@ public class ZKClient extends BaseService { private TreeCache cache; private Map ephemeralNodes = new LinkedHashMap<>(); + public static ZKClient get() { + return I; + } + private synchronized static ZKClient I() { return I == null ? new ZKClient() : I; } diff --git a/pom.xml b/pom.xml index fdac5b89..fae48c0a 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ UTF-8 1.8 com.github.mpusher - 0.0.3 + 0.0.4 ${mpush.version} ${mpush.version} ${mpush.version} From 0f926858921863e8f1c1e4c146e5016033dcb8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 17:33:27 +0800 Subject: [PATCH 636/890] =?UTF-8?q?=E6=8E=A8=E9=80=81API=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/push/PushCallback.java | 34 ++++++-- .../java/com/mpush/api/push/PushResult.java | 87 ++++++++++++++++++- .../com/mpush/client/push/PushRequest.java | 34 +++----- .../mpush/test/push/PushClientTestMain.java | 28 +----- 4 files changed, 130 insertions(+), 53 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java index 7060b4ee..1dfa1158 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java @@ -11,6 +11,27 @@ */ public interface PushCallback { + default void onResult(PushResult result) { + switch (result.resultCode) { + case PushResult.CODE_SUCCESS: + if (result.userId != null) { + onSuccess(result.userId, result.location); + } else { + onSuccess(result.userIds); + } + break; + case PushResult.CODE_FAILURE: + onFailure(result.userId, result.location); + break; + case PushResult.CODE_OFFLINE: + onOffline(result.userId, result.location); + break; + case PushResult.CODE_TIMEOUT: + onTimeout(result.userId, result.location); + break; + } + } + /** * 推送成功, 指定用户推送时重写此方法 * @@ -18,7 +39,6 @@ public interface PushCallback { * @param location 用户所在机器 */ default void onSuccess(String userId, ClientLocation location) { - } /** @@ -27,7 +47,8 @@ default void onSuccess(String userId, ClientLocation location) { * @param userId 推送用户 * @param location 用户所在机器 */ - void onFailure(String userId, ClientLocation location); + default void onFailure(String userId, ClientLocation location) { + } /** * 推送用户不在线 @@ -35,7 +56,8 @@ default void onSuccess(String userId, ClientLocation location) { * @param userId 推送用户 * @param location 用户所在机器 */ - void onOffline(String userId, ClientLocation location); + default void onOffline(String userId, ClientLocation location) { + } /** * 推送超时 @@ -43,14 +65,14 @@ default void onSuccess(String userId, ClientLocation location) { * @param userId 推送用户 * @param location 用户所在机器 */ - void onTimeout(String userId, ClientLocation location); + default void onTimeout(String userId, ClientLocation location) { + } /** * 推送成功, 广播时重写此方法 * * @param userIds 推送成功的用户列表 */ - default void onSuccess(List userIds) { - + default void onSuccess(String[] userIds) { } } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java index 97de4d05..9a6c53cf 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java @@ -21,12 +21,95 @@ import com.mpush.api.router.ClientLocation; +import java.util.Arrays; + /** * Created by ohun on 16/9/17. * * @author ohun@live.cn (夜色) */ public class PushResult { - private Object[] timeLine; - private ClientLocation location; + public static final int CODE_SUCCESS = 1; + public static final int CODE_FAILURE = 2; + public static final int CODE_OFFLINE = 3; + public static final int CODE_TIMEOUT = 4; + public int resultCode; + public String userId; + public String[] userIds; + public Object[] timeLine; + public ClientLocation location; + + public PushResult(int resultCode) { + this.resultCode = resultCode; + } + + public int getResultCode() { + return resultCode; + } + + public PushResult setResultCode(int resultCode) { + this.resultCode = resultCode; + return this; + } + + public String getUserId() { + return userId; + } + + public PushResult setUserId(String userId) { + this.userId = userId; + return this; + } + + public String[] getUserIds() { + return userIds; + } + + public PushResult setUserIds(String[] userIds) { + this.userIds = userIds; + return this; + } + + public Object[] getTimeLine() { + return timeLine; + } + + public PushResult setTimeLine(Object[] timeLine) { + this.timeLine = timeLine; + return this; + } + + public ClientLocation getLocation() { + return location; + } + + public PushResult setLocation(ClientLocation location) { + this.location = location; + return this; + } + + public String getResultDesc() { + switch (resultCode) { + case CODE_SUCCESS: + return "success"; + case CODE_FAILURE: + return "failure"; + case CODE_OFFLINE: + return "offline"; + case CODE_TIMEOUT: + return "timeout"; + } + return Integer.toString(CODE_TIMEOUT); + } + + @Override + public String toString() { + return "PushResult{" + + "resultCode=" + getResultDesc() + + ", userId='" + userId + '\'' + + ", userIds=" + Arrays.toString(userIds) + + ", timeLine=" + Arrays.toString(timeLine) + + ", location=" + location + + '}'; + } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index c6bbc8c0..8835d3ae 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -65,6 +65,10 @@ private enum Status {init, success, failure, offline, timeout} private void sendToConnServer(RemoteRouter remoteRouter) { timeLine.addTimePoint("lookup-remote"); + if (remoteRouter != null) { + location = remoteRouter.getRouteValue(); + } + if (remoteRouter == null || remoteRouter.isOffline()) { //1.1没有查到说明用户已经下线 offline(); @@ -75,7 +79,6 @@ private void sendToConnServer(RemoteRouter remoteRouter) { //2.通过网关连接,把消息发送到所在机器 - location = remoteRouter.getRouteValue(); Connection gatewayConn = client.getGatewayConnection(location.getHost()); if (gatewayConn == null) { LOGGER.error("get gateway connection failure, location={}", location); @@ -116,26 +119,15 @@ private void submit(Status status) { @Override public void run() { - switch (status.get()) { - case success: - if (userId == null) { - callback.onSuccess(Jsons.fromJsonToList(result, String[].class)); - } else { - callback.onSuccess(userId, location); - } - break; - case failure: - callback.onFailure(userId, location); - break; - case offline: - callback.onOffline(userId, location); - break; - case timeout: - callback.onTimeout(userId, location); - break; - case init://从定时任务过来的,超时时间到了 - submit(Status.timeout); - break; + if (status.get() == Status.init) {//从定时任务过来的,超时时间到了 + submit(Status.timeout); + } else { + callback.onResult(new PushResult(status.get().ordinal()) + .setUserId(userId) + .setUserIds(userId == null ? Jsons.fromJson(result, String[].class) : null) + .setLocation(location) + .setTimeLine(timeLine.getTimePoints()) + ); } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index d0ae3750..f5b99e83 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -23,6 +23,7 @@ import com.mpush.api.router.ClientLocation; import com.mpush.tools.log.Logs; +import java.util.Arrays; import java.util.List; import java.util.concurrent.FutureTask; @@ -41,33 +42,12 @@ public static void main(String[] args) throws Exception { PushContext context = PushContext.build(msg) .setBroadcast(false) - .setUserId("user-0") + .setUserIds(Arrays.asList("user-0", "user-1")) .setTimeout(100000) .setCallback(new PushCallback() { - - @Override - public void onSuccess(List userIds) { - System.err.println("push onSuccess userId=" + userIds); - } - - @Override - public void onSuccess(String userId, ClientLocation location) { - System.err.println("push onSuccess userId=" + userId); - } - - @Override - public void onFailure(String userId, ClientLocation location) { - System.err.println("push onFailure userId=" + userId); - } - - @Override - public void onOffline(String userId, ClientLocation location) { - System.err.println("push onOffline userId=" + userId); - } - @Override - public void onTimeout(String userId, ClientLocation location) { - System.err.println("push onTimeout userId=" + userId); + public void onResult(PushResult result) { + System.err.println(result); } }); Thread.sleep(1000); From 49a6b51192fea51b9299a02cd7d26e9295d788ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 17:40:54 +0800 Subject: [PATCH 637/890] merge dev --- mpush-boot/.DS_Store | Bin 0 -> 6148 bytes mpush-test/src/test/resources/application.conf | 5 +++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 mpush-boot/.DS_Store diff --git a/mpush-boot/.DS_Store b/mpush-boot/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e16584e446cf02a612834c5e7d3c9e5fc7400875 GIT binary patch literal 6148 zcmeHK&1%~~5Z-m%RH|aoA%z@L(7id>?IlOyCYMsELBTmB!Cj#xUJ$Dwt!;ua=#&@e zW8?wy04@1>h2EOvK{~U$4l!=8ttmS&`;BI2W~Fb%{t!ZFca(Jq2?-${C}P8h<~M@< zs7unY9z?2ZaH7&lb(ZEux)kjktH=P(-8xZ(k&>K}h54gf=~*r+)o%ZwjkV^B^_QOi zD%fZRt#Bh~1t-a{m?Vm+QPpMTAlyzw##2@vicAd;xZKbAn9I-QC#HCLguw?zkwbv7 zS=twTG6*+66jCuEd8zA}8pnr1X2qmijAT|FKQBB8U-wErhSVODye}$|id?93UDO6@ zyT{ZJ2J^zo_k75jPTM7Js{Mi0-986uNc&%gFmuyvBlb;Nhef~ z40Y_t)ZS329v%FV4kt7==vv2sV_=biHS-wa{=fg{`~PB+dvXjo2L3AsXmc;w>%u45 zy>;p1aIcL(zks4(Ty5|v1q{6vLo9B^TcBFNAE5ylTdWO&2gDr&G!3qC4E$9FegP#; BazOw9 literal 0 HcmV?d00001 diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index ea89e4ab..2007985e 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -3,9 +3,10 @@ mp.log.level=debug mp.min-heartbeat=10s mp.max-heartbeat=10s mp.zk.namespace=mpush -mp.zk.server-address="127.0.0.1:2181" +mp.zk.server-address="10.0.10.47:2181" mp.core.compress-threshold=10k +mp.http.proxy-enabled=true mp.redis { #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 - cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port + cluster-group:[["10.0.10.53:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port } \ No newline at end of file From d98690031dcd76726525c649034d11e1fd9befb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 17:44:49 +0800 Subject: [PATCH 638/890] merge dev --- .gitignore | 21 ++++++++++----------- mpush-boot/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 10 insertions(+), 11 deletions(-) delete mode 100644 mpush-boot/.DS_Store diff --git a/.gitignore b/.gitignore index f7299b4b..75fc2041 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,15 @@ *.class *.classpath *.project -*/.settings/* -*/target/* -*/bin/* -*/WebContent/* +*/.settings/ +*/target/ +*/bin/ +*/WebContent/ +*/.DS_Store -.idea/* -*.jar -*.war -*.ear -*.iml -./target/* +.idea/ + +.target/ # Mobile Tools for Java (J2ME) .mtj.tmp/ @@ -20,7 +18,8 @@ *.jar *.war *.ear +*.iml +.DS_Store # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -mpush-test/src/test/resources/application.conf \ No newline at end of file diff --git a/mpush-boot/.DS_Store b/mpush-boot/.DS_Store deleted file mode 100644 index e16584e446cf02a612834c5e7d3c9e5fc7400875..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK&1%~~5Z-m%RH|aoA%z@L(7id>?IlOyCYMsELBTmB!Cj#xUJ$Dwt!;ua=#&@e zW8?wy04@1>h2EOvK{~U$4l!=8ttmS&`;BI2W~Fb%{t!ZFca(Jq2?-${C}P8h<~M@< zs7unY9z?2ZaH7&lb(ZEux)kjktH=P(-8xZ(k&>K}h54gf=~*r+)o%ZwjkV^B^_QOi zD%fZRt#Bh~1t-a{m?Vm+QPpMTAlyzw##2@vicAd;xZKbAn9I-QC#HCLguw?zkwbv7 zS=twTG6*+66jCuEd8zA}8pnr1X2qmijAT|FKQBB8U-wErhSVODye}$|id?93UDO6@ zyT{ZJ2J^zo_k75jPTM7Js{Mi0-986uNc&%gFmuyvBlb;Nhef~ z40Y_t)ZS329v%FV4kt7==vv2sV_=biHS-wa{=fg{`~PB+dvXjo2L3AsXmc;w>%u45 zy>;p1aIcL(zks4(Ty5|v1q{6vLo9B^TcBFNAE5ylTdWO&2gDr&G!3qC4E$9FegP#; BazOw9 From ee05e172ecefa631250c25ec6781ae5a6e2631ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 18:21:18 +0800 Subject: [PATCH 639/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/common/router/RemoteRouterManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 8d6546f7..9f77f96a 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -56,7 +56,7 @@ public RemoteRouter register(String userId, RemoteRouter router) { } /** - * 目前的实现方式是非原子操作(get->set),可能会有并发问题,虽然概率很低 + * 目前的实现方式是非原子操作(get:set),可能会有并发问题,虽然概率很低 * 后续考虑采用lua脚本,实现原子操作 * * @param userId 用户ID From be55675771e07c2cb06b55ffbd98865e0f9c53d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 18:25:56 +0800 Subject: [PATCH 640/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- mpush-test/src/test/resources/application.conf | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 75fc2041..61f3420f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ .idea/ -.target/ +target/ # Mobile Tools for Java (J2ME) .mtj.tmp/ @@ -23,3 +23,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +mpush-test/src/test/resources/application.conf diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 2007985e..9731b8ba 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -3,10 +3,10 @@ mp.log.level=debug mp.min-heartbeat=10s mp.max-heartbeat=10s mp.zk.namespace=mpush -mp.zk.server-address="10.0.10.47:2181" +mp.zk.server-address="127.0.0.1:2181" mp.core.compress-threshold=10k mp.http.proxy-enabled=true mp.redis { #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 - cluster-group:[["10.0.10.53:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port + cluster-group:[["127.0.0.1:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port } \ No newline at end of file From 45959b39ca1b03ce457ce55a15f6a1488b14e993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 20 Sep 2016 18:26:08 +0800 Subject: [PATCH 641/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/src/test/resources/application.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 9731b8ba..234c28d9 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -8,5 +8,5 @@ mp.core.compress-threshold=10k mp.http.proxy-enabled=true mp.redis { #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 - cluster-group:[["127.0.0.1:6379:shinemo123"]]//格式是ip:port:password,密码可以没有ip:port + cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port } \ No newline at end of file From 9bded60a44c0113716c8ebbe5af0b3e3faeb50e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 21 Sep 2016 14:32:42 +0800 Subject: [PATCH 642/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=90=AF=E7=94=A8http=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/mpush.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index d806cbdc..8f50b3a0 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -5,6 +5,7 @@ mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JY mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" mp.zk.server-address="127.0.0.1:2181" mp.zk.namespace=mpush +mp.http.proxy-enabled=true mp.redis={ #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port From 1d8de44b93482b960f909ab1e9e787e9294997d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 21 Sep 2016 21:12:59 +0800 Subject: [PATCH 643/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 2 +- mpush-boot/pom.xml | 2 +- mpush-boot/src/main/resources/mpush.conf | 5 +++++ mpush-cache/pom.xml | 2 +- mpush-client/pom.xml | 2 +- mpush-common/pom.xml | 2 +- mpush-core/pom.xml | 2 +- mpush-monitor/pom.xml | 2 +- mpush-netty/pom.xml | 2 +- mpush-test/pom.xml | 2 +- mpush-tools/pom.xml | 2 +- mpush-zk/pom.xml | 2 +- pom.xml | 4 ++-- 13 files changed, 18 insertions(+), 13 deletions(-) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 10b1b0e9..8299261d 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-api diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 1be5d4e2..ef5f4f68 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-boot diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 8f50b3a0..1ca79baf 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -9,4 +9,9 @@ mp.http.proxy-enabled=true mp.redis={ #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port +} + +mp.net.public-host-mapping { //本机局域网IP和公网IP的映射关系 + "10.0.10.156":"111.1.32.137" + "10.0.10.166":"111.1.33.138" } \ No newline at end of file diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml index 47b606d5..cec0d4ca 100644 --- a/mpush-cache/pom.xml +++ b/mpush-cache/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-cache diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index ee54b6d7..67a869a3 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-client diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index c5143336..17b0f88c 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -5,7 +5,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 4.0.0 diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index c291b7d5..72c82d51 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-core diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 1f2133f3..a1b044d2 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -3,7 +3,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 4.0.0 diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 3bec1e1c..fa69f799 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-netty diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index ddf0a8f5..7be6a76c 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-test diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 03dcafd9..ca4a49ce 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-tools diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml index 025c4495..f8b25191 100644 --- a/mpush-zk/pom.xml +++ b/mpush-zk/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 1.0 + 0.0.4 mpush-zk diff --git a/pom.xml b/pom.xml index fae48c0a..3558af90 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.github.mpusher mpush pom - 1.0 + 0.0.4 mpush MPUSH消息推送系统 https://github.com/mpusher/mpush @@ -138,7 +138,7 @@ ${mpush.version} - ${mpush.groupId} + ${project.groupId} mpush-core ${mpush.version} From 303ed349fc3faf57ae6e3aca7e6facbb9d95817d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 22 Sep 2016 09:58:52 +0800 Subject: [PATCH 644/890] add changelog --- Changelog.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Changelog.md diff --git a/Changelog.md b/Changelog.md new file mode 100644 index 00000000..0496e93c --- /dev/null +++ b/Changelog.md @@ -0,0 +1,17 @@ +#### v0.0.4: + +1. push client API 调整 +2. push 接口增加了全网推送功能 +3. 用户下线后路由信息不再删除,而是修改为下线状态 +4. 修复ZK Client临时节点断开后,不能恢复注册的bug +5. 其他bug fix + +#### v0.0.3 + +1. 增加了消息ACK功能 +2. 修复脚本换行问题 +3. bug fix + +### v0.0.2 + +1. 增加多端同时在线攻能 From 38e86ad71c64eb8d6b79e4284703329c4e52c33b Mon Sep 17 00:00:00 2001 From: ohun Date: Fri, 23 Sep 2016 16:23:05 +0800 Subject: [PATCH 645/890] Update README.md --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 8a44acd9..468216f0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,18 @@ * 文档:[https://mpusher.github.io/docs](https://mpusher.github.io/docs) * QQ群:__114583699__ MPUSH开源消息推送系统 +## 源码 +* 总目录 https://github.com/mpusher/ +* server https://github.com/mpusher/mpush +* alloc https://github.com/mpusher/alloc +* mpns https://github.com/mpusher/alloc +* java-client https://github.com/mpusher/mpush-client-java +* android sdk&demo https://github.com/mpusher/mpush-android +* IOS sdk(swift)https://github.com/mpusher/mpush-client-swift +* IOS sdk(OC)https://github.com/mpusher/mpush-client-oc + +ps:由于源码分别在github和码云有两份,最新的代码以github为主 + ## 服务调用关系 ![](https://mpusher.github.io/docs/服务依赖关系.png) From 104197760f0b039a345b06ee64fae846b9c262de Mon Sep 17 00:00:00 2001 From: ohun Date: Fri, 23 Sep 2016 16:24:00 +0800 Subject: [PATCH 646/890] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 468216f0..495aa1ed 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ * mpns https://github.com/mpusher/alloc * java-client https://github.com/mpusher/mpush-client-java * android sdk&demo https://github.com/mpusher/mpush-android -* IOS sdk(swift)https://github.com/mpusher/mpush-client-swift -* IOS sdk(OC)https://github.com/mpusher/mpush-client-oc +* IOS sdk(swift) https://github.com/mpusher/mpush-client-swift +* IOS sdk(OC) https://github.com/mpusher/mpush-client-oc ps:由于源码分别在github和码云有两份,最新的代码以github为主 From 8fe228a5a7cade79c04f22b3ef153a6d43f8d097 Mon Sep 17 00:00:00 2001 From: ohun Date: Fri, 23 Sep 2016 16:32:42 +0800 Subject: [PATCH 647/890] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 495aa1ed..b52ae3aa 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,14 @@ * QQ群:__114583699__ MPUSH开源消息推送系统 ## 源码 -* 总目录 https://github.com/mpusher/ -* server https://github.com/mpusher/mpush -* alloc https://github.com/mpusher/alloc -* mpns https://github.com/mpusher/alloc -* java-client https://github.com/mpusher/mpush-client-java -* android sdk&demo https://github.com/mpusher/mpush-android -* IOS sdk(swift) https://github.com/mpusher/mpush-client-swift -* IOS sdk(OC) https://github.com/mpusher/mpush-client-oc +* 总目录 [https://github.com/mpusher/](https://github.com/mpusher/) +* server [https://github.com/mpusher/mpush](https://github.com/mpusher/mpush) 服务端源码 +* alloc [https://github.com/mpusher/alloc](https://github.com/mpusher/alloc) 调度器源码 +* mpns [https://github.com/mpusher/mpns](https://github.com/mpusher/mpns) 个性化推送中心源码 +* java-client [https://github.com/mpusher/mpush-client-java](https://github.com/mpusher/mpush-client-java) 纯java客户端源码 +* android sdk&demo [https://github.com/mpusher/mpush-android](https://github.com/mpusher/mpush-android) 安卓SDK和DEMO源码 +* IOS sdk(swift) [https://github.com/mpusher/mpush-client-swift](https://github.com/mpusher/mpush-client-swift) swift版客户端源码 +* IOS sdk(OC) [https://github.com/mpusher/mpush-client-oc](https://github.com/mpusher/mpush-client-oc) Object C 客户端源码 ps:由于源码分别在github和码云有两份,最新的代码以github为主 From a886638225c12f7629f70f20c714b2191ea0714f Mon Sep 17 00:00:00 2001 From: ohun Date: Fri, 23 Sep 2016 20:18:02 +0800 Subject: [PATCH 648/890] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b52ae3aa..462f52d9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ * QQ群:__114583699__ MPUSH开源消息推送系统 ## 源码 -* 总目录 [https://github.com/mpusher/](https://github.com/mpusher/) +* group [https://github.com/mpusher/](https://github.com/mpusher/) 源代码空间 * server [https://github.com/mpusher/mpush](https://github.com/mpusher/mpush) 服务端源码 * alloc [https://github.com/mpusher/alloc](https://github.com/mpusher/alloc) 调度器源码 * mpns [https://github.com/mpusher/mpns](https://github.com/mpusher/mpns) 个性化推送中心源码 From 9d7b5eee4e6e96df151348f39b8a3c2a58eba3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 23 Sep 2016 23:23:16 +0800 Subject: [PATCH 649/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Dns=20ZK=20node?= =?UTF-8?q?=EF=BC=8CZKClient=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/spi/net/DnsMapping.java | 17 +++- .../com/mpush/bootstrap/job/ServerBoot.java | 12 +-- mpush-client/.gitignore | 1 - .../connect/ConnClientChannelHandler.java | 22 +++-- .../net/HttpProxyDnsMappingManager.java | 24 ++++- .../src/main/java/com/mpush/zk/ZKClient.java | 23 +++-- .../src/main/java/com/mpush/zk/ZKPath.java | 23 ++++- .../main/java/com/mpush/zk/ZKRegister.java | 92 ++++++++++++++++++ .../com/mpush/zk/cache/ZKDnsNodeCache.java | 97 +++++++++++++++++++ .../java/com/mpush/zk/cache/ZKNodeCache.java | 2 + .../com/mpush/zk/cache/ZKRedisNodeCache.java | 5 + .../com/mpush/zk/cache/ZKServerNodeCache.java | 23 +++-- .../mpush/zk/listener/ZKDnsNodeWatcher.java | 93 ++++++++++++++++++ .../mpush/zk/listener/ZKNodeCacheWatcher.java | 10 +- .../java/com/mpush/zk/node/ZKDnsNode.java | 77 +++++++++++++++ .../main/java/com/mpush/zk/node/ZKNode.java | 5 + .../java/com/mpush/zk/node/ZKServerNode.java | 3 +- 17 files changed, 483 insertions(+), 46 deletions(-) delete mode 100644 mpush-client/.gitignore create mode 100644 mpush-zk/src/main/java/com/mpush/zk/ZKRegister.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/cache/ZKDnsNodeCache.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java create mode 100644 mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java diff --git a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java index 0270d4d3..bd451d03 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/net/DnsMapping.java @@ -24,8 +24,11 @@ import java.util.Objects; public class DnsMapping { - private String ip; - private int port; + protected String ip; + protected int port; + + public DnsMapping() { + } public DnsMapping(String ip, int port) { this.ip = ip; @@ -40,6 +43,16 @@ public int getPort() { return port; } + public DnsMapping setIp(String ip) { + this.ip = ip; + return this; + } + + public DnsMapping setPort(int port) { + this.port = port; + return this; + } + public static DnsMapping parse(String addr) { String[] host_port = Objects.requireNonNull(addr, "dns mapping can not be null") .split(":"); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index c603003d..3d013b34 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -25,6 +25,7 @@ import com.mpush.tools.log.Logs; import com.mpush.tools.thread.pool.ThreadPoolManager; import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKRegister; import com.mpush.zk.node.ZKServerNode; import java.util.concurrent.TimeUnit; @@ -52,8 +53,9 @@ public void start() { @Override public void onSuccess(Object... args) { Logs.Console.info("start {} success listen:{}", serverName, args[0]); - if (node != null) { - registerServerToZk(node.getZkPath(), Jsons.toJson(node)); + if (node != null) {//注册应用到zk + ZKRegister.build().setEphemeral(true).setNode(node).register(); + Logs.Console.info("register server node={} to zk path={}", node, node.getNodePath()); } startNext(); } @@ -76,10 +78,4 @@ protected void stop() { } stopNext(); } - - //注册应用到zk - private void registerServerToZk(String path, String value) { - ZKClient.I.registerEphemeralSequential(path, value); - Logs.Console.info("register server node={} to zk name={}", value, path); - } } diff --git a/mpush-client/.gitignore b/mpush-client/.gitignore deleted file mode 100644 index ae3c1726..00000000 --- a/mpush-client/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index c1e3a6d4..d937bcf9 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -43,6 +43,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -101,11 +102,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception ErrorMessage errorMessage = new ErrorMessage(packet, connection); LOGGER.error(">>> receive an error packet=" + errorMessage); } else if (command == Command.BIND) { - OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.warn(">>> receive an success packet=" + okMessage); - HttpRequestMessage message = new HttpRequestMessage(connection); - message.uri = "http://baidu.com"; - message.send(); + } else if (command == Command.PUSH) { PushMessage message = new PushMessage(packet, connection); LOGGER.warn(">>> receive an push message, content=" + new String(message.content, Constants.UTF_8)); @@ -117,8 +114,19 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.HEARTBEAT) { LOGGER.warn(">>> receive a heartbeat pong..."); - } else { - LOGGER.warn(">>> receive a message, type=" + command + "," + packet); + } else if (command == Command.OK) { + OkMessage okMessage = new OkMessage(packet, connection); + LOGGER.warn(">>> receive an success packet=" + okMessage); + Map headers = new HashMap<>(); + headers.put(Constants.HTTP_HEAD_READ_TIMEOUT, "10000"); + HttpRequestMessage message = new HttpRequestMessage(connection); + message.headers = headers; + message.uri = "http://baidu.com"; + message.send(); + } else if (command == Command.HTTP_PROXY) { + HttpResponseMessage message = new HttpResponseMessage(packet, connection); + LOGGER.warn(">>> receive a http response, message={}, body={}", + message, message.body == null ? null : new String(message.body, Constants.UTF_8)); } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index f1ad04ea..7bc17b78 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -27,6 +27,12 @@ import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; +import com.mpush.zk.ZKPath; +import com.mpush.zk.ZKRegister; +import com.mpush.zk.cache.ZKDnsNodeCache; +import com.mpush.zk.listener.ZKDnsNodeWatcher; +import com.mpush.zk.node.ZKDnsNode; +import com.mpush.zk.node.ZKRedisNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,17 +46,20 @@ public class HttpProxyDnsMappingManager extends BaseService implements DnsMappingManager, Runnable { private final Logger logger = LoggerFactory.getLogger(HttpProxyDnsMappingManager.class); - - public HttpProxyDnsMappingManager() { - } + private final ZKDnsNodeWatcher watcher = new ZKDnsNodeWatcher(); + private final ZKDnsNodeCache cache = watcher.getCache(); private final Map> all = Maps.newConcurrentMap(); private Map> available = Maps.newConcurrentMap(); private ScheduledExecutorService executorService; + public HttpProxyDnsMappingManager() { + } + @Override protected void doStart(Listener listener) throws Throwable { + watcher.startWatch(); if (all.size() > 0) { executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns @@ -86,8 +95,13 @@ public Map> getAll() { } public DnsMapping lookup(String origin) { - if (available.isEmpty()) return null; - List list = available.get(origin); + List list = cache.get(origin); + + if (list == null || list.isEmpty()) { + if (available.isEmpty()) return null; + list = available.get(origin); + } + if (list == null || list.isEmpty()) return null; int L = list.size(); if (L == 1) return list.get(0); diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 7b7fb0e8..f185d75f 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -89,8 +89,10 @@ protected void doStop(Listener listener) throws Throwable { */ @Override public void init() { - if (zkConfig != null) return; - zkConfig = ZKConfig.build(); + if (client != null) return; + if (zkConfig == null) { + zkConfig = ZKConfig.build(); + } Builder builder = CuratorFrameworkFactory .builder() .connectString(zkConfig.getHosts()) @@ -163,12 +165,14 @@ public String get(final String key) { * @return */ public String getFromRemote(final String key) { - try { - return new String(client.getData().forPath(key), Constants.UTF_8); - } catch (Exception ex) { - Logs.ZK.error("getFromRemote:{}", key, ex); - return null; + if (isExisted(key)) { + try { + return new String(client.getData().forPath(key), Constants.UTF_8); + } catch (Exception ex) { + Logs.ZK.error("getFromRemote:{}", key, ex); + } } + return null; } /** @@ -310,4 +314,9 @@ public void registerListener(ZKNodeCacheWatcher listener) { public ZKConfig getZKConfig() { return zkConfig; } + + public ZKClient setZKConfig(ZKConfig zkConfig) { + this.zkConfig = zkConfig; + return this; + } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index ed1c7aa0..ef768961 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -22,10 +22,13 @@ import org.apache.curator.utils.ZKPaths; +import static org.apache.curator.utils.ZKPaths.PATH_SEPARATOR; + public enum ZKPath { REDIS_SERVER("/redis", "machine", "redis注册的地方"), CONNECT_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), - GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"); + GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"), + DNS_MAPPING("/dns/mapping", "machine", "dns mapping服务器应用注册的路径"); ZKPath(String root, String name, String desc) { this.root = root; @@ -40,12 +43,24 @@ public String getRootPath() { } public String getNodePath() { - return root + ZKPaths.PATH_SEPARATOR + name; + return root + PATH_SEPARATOR + name; + } + + public String getNodePath(String... tails) { + String path = getNodePath(); + for (String tail : tails) { + path += (PATH_SEPARATOR + tail); + } + return path; } //根据从zk中获取的app的值,拼装全路径 - public String getFullPath(String tail) { - return root + ZKPaths.PATH_SEPARATOR + tail; + public String getFullPath(String childPaths) { + return root + PATH_SEPARATOR + childPaths; + } + + public String getTail(String childPaths) { + return ZKPaths.getNodeFromPath(childPaths); } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKRegister.java b/mpush-zk/src/main/java/com/mpush/zk/ZKRegister.java new file mode 100644 index 00000000..92f517ef --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKRegister.java @@ -0,0 +1,92 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk; + +import com.mpush.zk.node.ZKNode; + +import java.util.Objects; + +/** + * Created by ohun on 16/9/22. + * + * @author ohun@live.cn (夜色) + */ +public final class ZKRegister { + private ZKNode node; + private ZKPath path; + private boolean ephemeral = true; + private ZKClient client; + + public void register() { + Objects.requireNonNull(this.node); + String path = node.getNodePath(); + if (path == null) { + Objects.requireNonNull(this.path); + path = this.path.getNodePath(); + } + if (ephemeral) { + client.registerEphemeralSequential(path, node.encode()); + } else { + client.registerPersist(path, node.encode()); + } + } + + public static ZKRegister build() { + ZKRegister register = new ZKRegister(); + register.client = ZKClient.I; + return register; + } + + public ZKClient getClient() { + return client; + } + + public ZKRegister setClient(ZKClient client) { + this.client = client; + return this; + } + + public ZKNode getNode() { + return node; + } + + public ZKRegister setNode(ZKNode node) { + this.node = node; + return this; + } + + public ZKPath getPath() { + return path; + } + + public ZKRegister setPath(ZKPath path) { + this.path = path; + return this; + } + + public boolean isEphemeral() { + return ephemeral; + } + + public ZKRegister setEphemeral(boolean ephemeral) { + this.ephemeral = ephemeral; + return this; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKDnsNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKDnsNodeCache.java new file mode 100644 index 00000000..4fcb6de7 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKDnsNodeCache.java @@ -0,0 +1,97 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.cache; + +import com.google.common.collect.Maps; +import com.mpush.zk.node.ZKDnsNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; + +import static java.util.stream.Collectors.groupingBy; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class ZKDnsNodeCache implements ZKNodeCache { + + private final Logger logger = LoggerFactory.getLogger(ZKDnsNodeCache.class); + + protected final Map nodes = Maps.newConcurrentMap(); + + protected final Map> mappings = Maps.newConcurrentMap(); + + @Override + public void addAll(List list) { + } + + @Override + public void put(String path, ZKDnsNode node) { + nodes.put(path, node); + List nodes = mappings.get(node.getOrigin()); + if (nodes == null) { + nodes = new ArrayList<>(); + mappings.put(node.getOrigin(), nodes); + } + nodes.add(node); + printCache(); + } + + @Override + public ZKDnsNode remove(String path) { + ZKDnsNode node = nodes.remove(path); + if (node != null) { + List nodes = mappings.get(node.getOrigin()); + if (nodes != null) { + nodes.remove(node); + } + printCache(); + } + return node; + } + + @Override + public Collection values() { + return nodes.values(); + } + + @Override + public void clear() { + nodes.clear(); + } + + @Override + public int size() { + return nodes.size(); + } + + public List get(String origin) { + return mappings.get(origin); + } + + private void printCache() { + for (ZKDnsNode node : nodes.values()) { + logger.warn(node.toString()); + } + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java index 5a6bbcb3..46703d33 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKNodeCache.java @@ -43,4 +43,6 @@ public interface ZKNodeCache { void clear(); + int size(); + } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java index 338704ab..e0e93f25 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKRedisNodeCache.java @@ -59,4 +59,9 @@ public Collection values() { public void clear() { nodes = Collections.emptyList(); } + + @Override + public int size() { + return nodes.size(); + } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java index b41a271f..e49121a0 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java @@ -39,7 +39,7 @@ public class ZKServerNodeCache implements ZKNodeCache { private final Logger logger = LoggerFactory.getLogger(ZKServerNodeCache.class); - protected final Map cache = Maps.newConcurrentMap(); + protected final Map nodes = Maps.newConcurrentMap(); @Override public void addAll(List list) { @@ -49,32 +49,37 @@ public void addAll(List list) { @Override public void put(String fullPath, ZKServerNode node) { if (StringUtils.isNotBlank(fullPath) && node != null) { - cache.put(fullPath, node); + nodes.put(fullPath, node); } else { logger.error("fullPath is null or application is null"); } - printList(); + printCache(); } @Override public ZKServerNode remove(String fullPath) { - ZKServerNode node = cache.remove(fullPath); - printList(); + ZKServerNode node = nodes.remove(fullPath); + printCache(); return node; } @Override public Collection values() { - return Collections.unmodifiableCollection(cache.values()); + return Collections.unmodifiableCollection(nodes.values()); } @Override public void clear() { - cache.clear(); + nodes.clear(); } - private void printList() { - for (ZKServerNode app : cache.values()) { + @Override + public int size() { + return nodes.size(); + } + + private void printCache() { + for (ZKServerNode app : nodes.values()) { logger.warn(app.toString()); } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java new file mode 100644 index 00000000..2feac3af --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java @@ -0,0 +1,93 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.listener; + +import com.google.common.base.Strings; +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.ZKPath; +import com.mpush.zk.cache.ZKDnsNodeCache; +import com.mpush.zk.cache.ZKRedisNodeCache; +import com.mpush.zk.node.ZKDnsNode; +import com.mpush.zk.node.ZKRedisNode; +import com.mpush.zk.node.ZKServerNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; + +/** + * redis 监控 + */ +public class ZKDnsNodeWatcher extends ZKNodeCacheWatcher { + + private final Logger logger = LoggerFactory.getLogger(ZKDnsNodeWatcher.class); + + private final ZKDnsNodeCache cache = new ZKDnsNodeCache(); + + + @Override + protected void beforeWatch() { + Logs.Console.error("start init zk dns data"); + List rawData = ZKClient.I.getChildrenKeys(ZKPath.DNS_MAPPING.getRootPath()); + for (String raw : rawData) { + String fullPath = ZKPath.DNS_MAPPING.getFullPath(raw); + cache.put(fullPath, getZKNode(fullPath)); + } + Logs.Console.error("end init zk dns data"); + } + + private ZKDnsNode getZKNode(String fullPath) { + String rawData = ZKClient.I.get(fullPath); + return Jsons.fromJson(rawData, ZKDnsNode.class); + } + + @Override + protected void onNodeAdded(String path, byte[] data) { + ZKDnsNode node = Jsons.fromJson(data, ZKDnsNode.class); + if (node != null) { + cache.put(path, node); + } + } + + @Override + protected void onNodeRemoved(String path, byte[] data) { + cache.remove(path); + } + + @Override + protected void onNodeUpdated(String path, byte[] data) { + ZKDnsNode node = Jsons.fromJson(data, ZKDnsNode.class); + if (node != null) { + cache.put(path, node); + } + } + + @Override + public String watchPath() { + return ZKPath.DNS_MAPPING.getRootPath(); + } + + public ZKDnsNodeCache getCache() { + return cache; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java index 67331f57..7c755830 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -53,13 +53,19 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc onNodeUpdated(path, data.getData()); break; } + Logs.ZK.info("ZK node data change={}, nodePath={}, watchPath={}, ns={}", event.getType(), path, watchPath(), client.getNamespace()); } - Logs.ZK.info("ZK node data change={}, name={}, listener={}, ns={}", event.getType(), path, watchPath(), client.getNamespace()); } - public final void beginWatch() { + public final ZKNodeCacheWatcher startWatch() { beforeWatch(); ZKClient.I.registerListener(this); + return this; + } + + @Deprecated + public final void beginWatch() { + startWatch(); } public abstract String watchPath(); diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java new file mode 100644 index 00000000..443f1e07 --- /dev/null +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java @@ -0,0 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.zk.node; + +import com.mpush.api.spi.net.DnsMapping; +import com.mpush.zk.ZKPath; + +/** + * Created by ohun on 16/9/22. + * + * @author ohun@live.cn (夜色) + */ +public class ZKDnsNode extends DnsMapping implements ZKNode { + private String origin; + + public ZKDnsNode() { + } + + public ZKDnsNode(String origin, String ip, int port) { + super(ip, port); + this.origin = origin; + } + + public String getOrigin() { + return origin; + } + + public ZKDnsNode setOrigin(String origin) { + this.origin = origin; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ZKDnsNode node = (ZKDnsNode) o; + + if (port != node.port) return false; + return ip.equals(node.ip); + + } + + @Override + public int hashCode() { + int result = ip.hashCode(); + result = 31 * result + port; + return result; + } + + @Override + public String toString() { + return "ZKDnsNode{" + + "origin='" + origin + '\'' + + ", ip='" + ip + '\'' + + ", port=" + port + + '}'; + } +} diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java index bc7de9bc..988fc02e 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKNode.java @@ -20,6 +20,7 @@ package com.mpush.zk.node; import com.mpush.tools.Jsons; +import com.mpush.zk.ZKPath; /** * Created by yxx on 2016/5/17. @@ -30,4 +31,8 @@ public interface ZKNode { default String encode() { return Jsons.toJson(this); } + + default String getNodePath() { + return null; + } } diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index f3b23ec5..c24b0945 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -93,7 +93,8 @@ public ZKServerNode setExtranetIp(String extranetIp) { return this; } - public String getZkPath() { + @Override + public String getNodePath() { return zkPath; } From 90c0310d88dccebee091e1d8563401c3fe6d8379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 23 Sep 2016 23:25:06 +0800 Subject: [PATCH 650/890] =?UTF-8?q?=E7=BB=91=E5=AE=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=8C=E6=A0=A1=E9=AA=8C=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=EF=BC=8C=E4=BB=A5=E5=8F=8A=E6=9C=AA=E8=A7=A3?= =?UTF-8?q?=E7=BB=91=EF=BC=8C=E5=B0=B1=E7=BB=91=E5=AE=9A=E7=9A=84=E5=9C=BA?= =?UTF-8?q?=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/handler/BindUserHandler.java | 78 +++++++++++++ .../mpush/core/handler/UnbindUserHandler.java | 107 ------------------ .../mpush/core/server/ConnectionServer.java | 2 +- 3 files changed, 79 insertions(+), 108 deletions(-) delete mode 100644 mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index b02c3adf..6da39fb0 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -22,12 +22,18 @@ import com.google.common.base.Strings; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; +import com.mpush.api.event.UserOfflineEvent; import com.mpush.api.event.UserOnlineEvent; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; import com.mpush.common.message.OkMessage; +import com.mpush.common.router.RemoteRouter; +import com.mpush.common.router.RemoteRouterManager; +import com.mpush.core.router.LocalRouter; +import com.mpush.core.router.LocalRouterManager; import com.mpush.core.router.RouterCenter; import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; @@ -46,6 +52,14 @@ public BindUserMessage decode(Packet packet, Connection connection) { @Override public void handle(BindUserMessage message) { + if (message.getPacket().cmd == Command.BIND.cmd) { + bind(message); + } else { + unbind(message); + } + } + + private void bind(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); Logs.Conn.info("bind user failure for invalid param, session={}", message.getConnection().getSessionContext()); @@ -54,6 +68,16 @@ public void handle(BindUserMessage message) { //1.绑定用户时先看下是否握手成功 SessionContext context = message.getConnection().getSessionContext(); if (context.handshakeOk()) { + //处理重复绑定问题 + if (context.userId != null) { + if (message.userId.equals(context.userId)) { + OkMessage.from(message).setData("bind success").send(); + return; + } else { + unbind(message); + } + } + //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 boolean success = RouterCenter.I.register(message.userId, message.getConnection()); if (success) { @@ -72,4 +96,58 @@ public void handle(BindUserMessage message) { Logs.Conn.info("bind user failure for not handshake, userId={}, session={}", message.userId, context); } } + + /** + * 目前是以用户维度来存储路由信息的,所以在删除路由信息时要判断下是否是同一个设备 + * 后续可以修改为按设备来存储路由信息。 + * + * @param message + */ + private void unbind(BindUserMessage message) { + if (Strings.isNullOrEmpty(message.userId)) { + ErrorMessage.from(message).setReason("invalid param").close(); + Logs.Conn.info("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); + return; + } + //1.解绑用户时先看下是否握手成功 + SessionContext context = message.getConnection().getSessionContext(); + if (context.handshakeOk()) { + //2.先删除远程路由, 必须是同一个设备才允许解绑 + boolean unRegisterSuccess = true; + int clientType = context.getClientType(); + String userId = message.userId; + RemoteRouterManager remoteRouterManager = RouterCenter.I.getRemoteRouterManager(); + RemoteRouter remoteRouter = remoteRouterManager.lookup(userId, clientType); + if (remoteRouter != null) { + String deviceId = remoteRouter.getRouteValue().getDeviceId(); + if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 + unRegisterSuccess = remoteRouterManager.unRegister(userId, clientType); + } + } + + //3.删除本地路由信息 + LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); + LocalRouter localRouter = localRouterManager.lookup(userId, clientType); + if (localRouter != null) { + String deviceId = localRouter.getRouteValue().getSessionContext().deviceId; + if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 + unRegisterSuccess = localRouterManager.unRegister(userId, clientType) && unRegisterSuccess; + } + } + + //4.路由删除成功,广播用户下线事件 + if (unRegisterSuccess) { + context.userId = null; + EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); + OkMessage.from(message).setData("unbind success").send(); + Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); + } else { + ErrorMessage.from(message).setReason("unbind failed").send(); + Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); + } + } else { + ErrorMessage.from(message).setReason("not handshake").close(); + Logs.Conn.info("unbind user failure not handshake, userId={}, session={}", message.userId, context); + } + } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java deleted file mode 100644 index ad8739e8..00000000 --- a/mpush-core/src/main/java/com/mpush/core/handler/UnbindUserHandler.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.core.handler; - -import com.google.common.base.Strings; -import com.mpush.api.connection.Connection; -import com.mpush.api.connection.SessionContext; -import com.mpush.api.event.UserOfflineEvent; -import com.mpush.api.protocol.Packet; -import com.mpush.common.handler.BaseMessageHandler; -import com.mpush.common.message.BindUserMessage; -import com.mpush.common.message.ErrorMessage; -import com.mpush.common.message.OkMessage; -import com.mpush.common.router.RemoteRouter; -import com.mpush.common.router.RemoteRouterManager; -import com.mpush.core.router.LocalRouter; -import com.mpush.core.router.LocalRouterManager; -import com.mpush.core.router.RouterCenter; -import com.mpush.tools.event.EventBus; -import com.mpush.tools.log.Logs; - - -/** - * Created by ohun on 2015/12/23. - * - * @author ohun@live.cn - */ -public final class UnbindUserHandler extends BaseMessageHandler { - - @Override - public BindUserMessage decode(Packet packet, Connection connection) { - return new BindUserMessage(packet, connection); - } - - /** - * 目前是以用户维度来存储路由信息的,所以在删除路由信息时要判断下是否是同一个设备 - * 后续可以修改为按设备来存储路由信息。 - * - * @param message - */ - @Override - public void handle(BindUserMessage message) { - if (Strings.isNullOrEmpty(message.userId)) { - ErrorMessage.from(message).setReason("invalid param").close(); - Logs.Conn.info("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); - return; - } - - //1.解绑用户时先看下是否握手成功 - SessionContext context = message.getConnection().getSessionContext(); - if (context.handshakeOk()) { - //2.先删除远程路由, 必须是同一个设备才允许解绑 - boolean unRegisterSuccess = true; - int clientType = context.getClientType(); - String userId = message.userId; - RemoteRouterManager remoteRouterManager = RouterCenter.I.getRemoteRouterManager(); - RemoteRouter remoteRouter = remoteRouterManager.lookup(userId, clientType); - if (remoteRouter != null) { - String deviceId = remoteRouter.getRouteValue().getDeviceId(); - if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = remoteRouterManager.unRegister(userId, clientType); - } - } - - //3.删除本地路由信息 - LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); - LocalRouter localRouter = localRouterManager.lookup(userId, clientType); - if (localRouter != null) { - String deviceId = localRouter.getRouteValue().getSessionContext().deviceId; - if (context.deviceId.equals(deviceId)) {//判断是否是同一个设备 - unRegisterSuccess = localRouterManager.unRegister(userId, clientType) && unRegisterSuccess; - } - } - - //4.路由删除成功,广播用户下线事件 - if (unRegisterSuccess) { - context.userId = null; - EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); - OkMessage.from(message).setData("unbind success").send(); - Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); - } else { - ErrorMessage.from(message).setReason("unbind failed").send(); - Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); - } - } else { - ErrorMessage.from(message).setReason("not handshake").close(); - Logs.Conn.info("unbind user failure not handshake, userId={}, session={}", message.userId, context); - } - } -} diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index f0d8dee1..be0696dc 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -65,7 +65,7 @@ public void init() { receiver.register(Command.HEARTBEAT, new HeartBeatHandler()); receiver.register(Command.HANDSHAKE, new HandshakeHandler()); receiver.register(Command.BIND, new BindUserHandler()); - receiver.register(Command.UNBIND, new UnbindUserHandler()); + receiver.register(Command.UNBIND, new BindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); receiver.register(Command.ACK, new AckHandler()); From 8e90c648351aa36a3465e49b6774831ffd0de3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 23 Sep 2016 23:25:59 +0800 Subject: [PATCH 651/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/reference.conf b/conf/reference.conf index 9304f842..4b196b92 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -125,7 +125,7 @@ mp { default-read-timeout=10s //请求超时时间 max-content-length=5m //response body 最大大小 dns-mapping { //域名映射外网地址转内部IP - "mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] + //"mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] } } From 7ba457f13b73ca673dc3c9710298ee472baa999d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 23 Sep 2016 23:33:52 +0800 Subject: [PATCH 652/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/mpush.conf | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 1ca79baf..25395e11 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,17 +1,16 @@ mp.log.level=${log.level} mp.min-heartbeat=${min.hb} -mp.net.connect-server-port=3000 mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" mp.zk.server-address="127.0.0.1:2181" mp.zk.namespace=mpush -mp.http.proxy-enabled=true mp.redis={ #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port } - -mp.net.public-host-mapping { //本机局域网IP和公网IP的映射关系 - "10.0.10.156":"111.1.32.137" - "10.0.10.166":"111.1.33.138" +mp.http.proxy-enabled=true //启用Http代理功能 +mp.net.connect-server-port=3000 +mp.net.public-host-mapping { //本机局域网IP和公网IP的映射关系,请添加实际的IP + "10.0.10.156":"111.1.32.137"//请修改成实际的IP + "10.0.10.166":"111.1.33.138"//请修改成实际的IP } \ No newline at end of file From a648b94c042a3249db00e918a877d8f1a16eb6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 27 Sep 2016 18:09:15 +0800 Subject: [PATCH 653/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9start-foreground=20?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.sh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bin/mp.sh b/bin/mp.sh index 589412bc..47326483 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -166,12 +166,8 @@ start) fi ;; start-foreground) - MP_CMD=(exec "$JAVA") - if [ "${MP_NOEXEC}" != "" ]; then - MP_CMD=("$JAVA") - fi - "${MP_CMD[@]}" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN "-Dmp.conf=$MP_CFG" + "$JAVA" "-Dmp.conf=$MP_CFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN ;; print-cmd) echo "\"$JAVA\" $MP_MAIN " From 9d2478914d669d94c947adc99d726a441623c39a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 27 Sep 2016 18:10:11 +0800 Subject: [PATCH 654/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8DACK=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=E9=94=99=E8=AF=AFbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-core/src/main/java/com/mpush/core/ack/AckContext.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java index e5dc38e1..d9146d84 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java @@ -77,8 +77,6 @@ public String toString() { @Override public void run() { - if (tryDone()) { - callback.onTimeout(this); - } + timeout(); } } From 0a78b958ab98df211359de95e36679ffc4f79e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 27 Sep 2016 18:12:09 +0800 Subject: [PATCH 655/890] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E5=88=B00.0.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 2 +- mpush-boot/pom.xml | 2 +- mpush-cache/pom.xml | 2 +- mpush-client/pom.xml | 2 +- mpush-common/pom.xml | 2 +- mpush-core/pom.xml | 2 +- mpush-monitor/pom.xml | 2 +- mpush-netty/pom.xml | 2 +- mpush-test/pom.xml | 2 +- mpush-tools/pom.xml | 2 +- mpush-zk/pom.xml | 2 +- pom.xml | 4 ++-- 12 files changed, 13 insertions(+), 13 deletions(-) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 8299261d..29c3251f 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-api diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index ef5f4f68..3b55635b 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-boot diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml index cec0d4ca..79618137 100644 --- a/mpush-cache/pom.xml +++ b/mpush-cache/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-cache diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 67a869a3..900d68e7 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-client diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 17b0f88c..a28df536 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -5,7 +5,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 4.0.0 diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 72c82d51..e507f9dd 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-core diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index a1b044d2..ae7c78c2 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -3,7 +3,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 4.0.0 diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index fa69f799..5e172411 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-netty diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 7be6a76c..6ac785e9 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-test diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index ca4a49ce..68179693 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-tools diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml index f8b25191..2d65d728 100644 --- a/mpush-zk/pom.xml +++ b/mpush-zk/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.4 + 0.0.5 mpush-zk diff --git a/pom.xml b/pom.xml index 3558af90..21dc1c81 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.github.mpusher mpush pom - 0.0.4 + 0.0.5 mpush MPUSH消息推送系统 https://github.com/mpusher/mpush @@ -61,7 +61,7 @@ UTF-8 1.8 com.github.mpusher - 0.0.4 + 0.0.5 ${mpush.version} ${mpush.version} ${mpush.version} From 16345e1b50f6bd3215c1d0b7be907217f74dc104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 27 Sep 2016 18:15:19 +0800 Subject: [PATCH 656/890] =?UTF-8?q?redis=20=E5=A2=9E=E5=8A=A0=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 6 +- .../com/mpush/bootstrap/job/RedisBoot.java | 2 +- .../com/mpush/cache/redis/RedisClient.java | 775 ------------------ .../com/mpush/cache/redis/RedisGroup.java | 70 -- .../java/com/mpush/cache/redis/RedisKey.java | 7 +- .../com/mpush/cache/redis/RedisServer.java | 18 +- .../connection/RedisConnectionFactory.java | 325 ++++++++ .../cache/redis/hash/ConsistentHash.java | 85 -- .../java/com/mpush/cache/redis/hash/Node.java | 48 -- .../redis/manager/RedisClusterManager.java | 7 +- .../cache/redis/manager/RedisManager.java | 358 ++++---- .../redis/manager/ZKRedisClusterManager.java | 84 +- .../com/mpush/client/push/PushClient.java | 5 +- .../com/mpush/client/push/PushRequestBus.java | 27 +- .../net/HttpProxyDnsMappingManager.java | 6 +- .../core/handler/FastConnectHandler.java | 2 +- .../mpush/core/handler/HandshakeHandler.java | 4 +- .../mpush/test/client/ConnectClientBoot.java | 2 +- .../test/configcenter/ConfigCenterTest.java | 2 +- .../mpush/test/push/PushClientTestMain.java | 8 +- .../mpush/test/redis/ConsistentHashTest.java | 64 -- .../java/com/mpush/test/redis/PubSubTest.java | 9 - .../mpush/test/redis/RedisClusterTest.java | 4 +- .../com/mpush/test/redis/RedisUtilTest.java | 8 +- .../main/java/com/mpush/tools/config/CC.java | 68 +- .../mpush/tools/config/ConfigBeanImpl.java | 238 ++++++ .../mpush/tools/config/data/RedisGroup.java | 4 +- .../data/{RedisServer.java => RedisNode.java} | 24 +- .../com/mpush/tools/crypto/Base64Utils.java | 6 +- .../tools/thread/NamedThreadFactory.java | 2 + .../src/main/java/com/mpush/zk/ZKClient.java | 1 + .../mpush/zk/listener/ZKNodeCacheWatcher.java | 4 +- .../java/com/mpush/zk/node/ZKRedisNode.java | 4 +- 33 files changed, 897 insertions(+), 1380 deletions(-) delete mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java delete mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java create mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java delete mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java delete mode 100644 mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java delete mode 100644 mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/config/ConfigBeanImpl.java rename mpush-tools/src/main/java/com/mpush/tools/config/data/{RedisServer.java => RedisNode.java} (73%) diff --git a/conf/reference.conf b/conf/reference.conf index 4b196b92..6d000564 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -95,7 +95,9 @@ mp { redis { write-to-zk=false #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[]//[["127.0.0.1:6379:your password"]]格式ip:port:password,密码可以不设置ip:port + password=""//your password + cluster-model=single//single,cluster + nodes:[]//["127.0.0.1:6379"]格式ip:port:password,密码可以不设置ip:port config { maxTotal:8, maxIdle:4, @@ -112,7 +114,7 @@ mp { testWhileIdle:false, timeBetweenEvictionRunsMillis:60000, blockWhenExhausted:true, - jmxEnabled:true, + jmxEnabled:false, jmxNamePrefix:pool, jmxNameBase:pool } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index 5cb57d22..f4366716 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -38,7 +38,7 @@ protected void start() { @Override protected void stop() { - RedisManager.I.close(); + RedisManager.I.destroy(); stopNext(); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java deleted file mode 100644 index 9bd0ca90..00000000 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisClient.java +++ /dev/null @@ -1,775 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.cache.redis; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.mpush.tools.Jsons; -import com.mpush.tools.config.CC; -import com.mpush.tools.log.Logs; -import redis.clients.jedis.*; -import redis.clients.util.Pool; - -import java.util.*; - -import static redis.clients.jedis.Protocol.DEFAULT_TIMEOUT; - -public final class RedisClient { - public static final JedisPoolConfig CONFIG = buildConfig(); - private static final Map POOL_MAP = Maps.newConcurrentMap(); - - private static JedisPoolConfig buildConfig() { - JedisPoolConfig config = new JedisPoolConfig(); - //连接池中最大连接数。高版本:maxTotal,低版本:maxActive - config.setMaxTotal(CC.mp.redis.config.maxTotal); - //连接池中最大空闲的连接数 - config.setMaxIdle(CC.mp.redis.config.maxIdle); - //连接池中最少空闲的连接数 - config.setMinIdle(CC.mp.redis.config.minIdle); - //当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait - config.setMaxWaitMillis(CC.mp.redis.config.maxWaitMillis); - //连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 - config.setMinEvictableIdleTimeMillis(CC.mp.redis.config.minEvictableIdleTimeMillis); - //对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 - config.setNumTestsPerEvictionRun(CC.mp.redis.config.numTestsPerEvictionRun); - //“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1 - config.setTimeBetweenEvictionRunsMillis(CC.mp.redis.config.timeBetweenEvictionRunsMillis); - //testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. - config.setTestOnBorrow(CC.mp.redis.config.testOnBorrow); - //testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值 - config.setTestOnReturn(CC.mp.redis.config.testOnReturn); - //testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. - config.setTestWhileIdle(CC.mp.redis.config.testWhileIdle); - return config; - } - - public static Jedis getClient(RedisServer node) { - JedisPool pool = POOL_MAP.get(node); - if (pool == null) { - synchronized (POOL_MAP) { - pool = POOL_MAP.get(node); - if (pool == null) { - pool = new JedisPool(CONFIG, node.getHost(), node.getPort(), DEFAULT_TIMEOUT, node.getPassword()); - POOL_MAP.put(node, pool); - } - } - } - return pool.getResource(); - } - - public static void close(Jedis jedis) { - if (jedis != null) jedis.close(); - } - - public static long incr(List nodeList, String key, Integer time) { - long incrRet = -1; - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - long ret = jedis.incr(key); - if (ret == 1 && time != null) { - jedis.expire(key, time); - } - incrRet = ret; - } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, time, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - return incrRet; - - } - - public static long incrBy(List nodeList, String key, long delt) { - long incrRet = -1; - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - long ret = jedis.incrBy(key, delt); - incrRet = ret; - } catch (Exception e) { - Logs.REDIS.error("redis incr exception:{}, {}, {}, {}", key, delt, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - return incrRet; - - } - - /********************* k v redis start ********************************/ - /** - * @param node redis实例 - * @param key - * @param clazz - * @return - */ - @SuppressWarnings("unchecked") - public static T get(RedisServer node, String key, Class clazz) { - - String value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.get(key); - } catch (Exception e) { - Logs.REDIS.error("redis get exception:{}, {}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - if (clazz == String.class) return (T) value; - return Jsons.fromJson(value, clazz); - } - - public static void set(List nodeList, String key, String value) { - set(nodeList, key, value, null); - } - - public static void set(List nodeList, String key, T value) { - set(nodeList, key, value, null); - } - - public static void set(List nodeList, String key, T value, Integer time) { - String jsonValue = Jsons.toJson(value); - set(nodeList, key, jsonValue, time); - } - - /** - * @param nodeList - * @param key - * @param value - * @param time seconds - */ - public static void set(List nodeList, String key, String value, Integer time) { - if (time == null) { - time = -1; - } - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.set(key, value); - if (time > 0) { - jedis.expire(key, time); - } - } catch (Exception e) { - Logs.REDIS.error("redis set exception:{}, {}, {}, {}", key, value, time, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static void del(List nodeList, String key) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.del(key); - } catch (Exception e) { - Logs.REDIS.error("redis del exception:{}, {}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - /********************* k v redis end ********************************/ - - /********************* - * hash redis start - ********************************/ - public static void hset(List nodeList, String key, String field, String value) { - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.hset(key, field, value); - } catch (Exception e) { - Logs.REDIS.error("redis hset exception:{}, {}, {}, {}", key, field, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static void hset(List nodeList, String key, String field, T value) { - hset(nodeList, key, field, Jsons.toJson(value)); - } - - public static T hget(RedisServer node, String key, String field, Class clazz) { - - String value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.hget(key, field); - } catch (Exception e) { - Logs.REDIS.error("redis hget exception:{}, {}", key, field, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return Jsons.fromJson(value, clazz); - - } - - public static void hdel(List nodeList, String key, String field) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.hdel(key, field); - } catch (Exception e) { - Logs.REDIS.error("redis hdel exception:{}, {}, {}", key, field, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static Map hgetAll(RedisServer node, String key) { - Map result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hgetAll(key); - } catch (Exception e) { - Logs.REDIS.error("redis hgetAll exception:{}, {}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return result; - } - - public static Map hgetAll(RedisServer node, String key, Class clazz) { - Map result = hgetAll(node, key); - if (result != null) { - Map newMap = Maps.newHashMap(); - Iterator> iterator = result.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - String k = entry.getKey(); - String v = entry.getValue(); - newMap.put(k, Jsons.fromJson(v, clazz)); - } - return newMap; - } else { - return null; - } - } - - /** - * 返回 key 指定的哈希集中所有字段的名字。 - * - * @param node - * @param key - * @return - */ - public static Set hkeys(RedisServer node, String key) { - Set result = null; - Jedis jedis = null; - try { - jedis = getClient(node); - result = jedis.hkeys(key); - } catch (Exception e) { - Logs.REDIS.error("redis hkeys exception:{},{}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - - } - return result; - } - - /** - * 返回 key 指定的哈希集中指定字段的值 - * - * @param node - * @param fields - * @param clazz - * @return - */ - public static List hmget(RedisServer node, String key, Class clazz, String... fields) { - - List value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.hmget(key, fields); - } catch (Exception e) { - Logs.REDIS.error("redis hmget exception:{},{},{}", key, fields, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return toList(value, clazz); - - } - - /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key - * 关联 - * - * @param nodeList - * @param hash - * @param time - */ - public static void hmset(List nodeList, String key, Map hash, Integer time) { - - if (time == null) { - time = -1; - } - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.hmset(key, hash); - if (time > 0) { - jedis.expire(key, time); - } - - } catch (Exception e) { - Logs.REDIS.error("redis hmset exception:{},{},{}", key, time, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - public static void hmset(List nodeList, String key, Map hash) { - hmset(nodeList, key, hash, null); - } - - /********************* hash redis end ********************************/ - - /********************* list redis start ********************************/ - /** - * 从队列的左边入队 - */ - public static void lpush(List nodeList, String key, String value) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.lpush(key, value); - } catch (Exception e) { - Logs.REDIS.error("redis lpush exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - public static void lpush(List nodeList, String key, T value) { - - lpush(nodeList, key, Jsons.toJson(value)); - - } - - /** - * 从队列的右边入队 - */ - public static void rpush(List nodeList, String key, String value) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.rpush(key, value); - } catch (Exception e) { - Logs.REDIS.error("redis rpush exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - public static void rpush(List nodeList, String key, T value) { - rpush(nodeList, key, Jsons.toJson(value)); - } - - /** - * 移除并且返回 key 对应的 list 的第一个元素 - */ - public static T lpop(List nodeList, String key, Class clazz) { - String retValue = null; - for (RedisServer node : nodeList) { - Jedis jedis = null; - String vaule = null; - try { - jedis = getClient(node); - vaule = jedis.lpop(key); - retValue = vaule; - } catch (Exception e) { - Logs.REDIS.error("redis lpop exception:{},{}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - return Jsons.fromJson(retValue, clazz); - } - - /** - * 从队列的右边出队一个元素 - */ - public static T rpop(List nodeList, String key, Class clazz) { - String retValue = null; - for (RedisServer node : nodeList) { - Jedis jedis = null; - String vaule = null; - try { - jedis = getClient(node); - vaule = jedis.rpop(key); - retValue = vaule; - } catch (Exception e) { - Logs.REDIS.error("redis rpop exception:{},{}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - return Jsons.fromJson(retValue, clazz); - } - - /** - * 从列表中获取指定返回的元素 start 和 end - * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List lrange(RedisServer node, String key, int start, int end, Class clazz) { - List value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.lrange(key, start, end); - } catch (Exception e) { - Logs.REDIS.error("redis lrange exception:{},{},{},{}", key, start, end, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return toList(value, clazz); - } - - /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key - * 里的值不是一个list的话,会返回error。 - */ - public static long llen(RedisServer node, String key) { - - long len = 0; - Jedis jedis = null; - try { - jedis = getClient(node); - len = jedis.llen(key); - } catch (Exception e) { - Logs.REDIS.error("redis llen exception:{},{}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return len; - } - - /** - * 移除表中所有与 value 相等的值 - * - * @param nodeList - * @param key - * @param value - */ - public static void lRem(List nodeList, String key, String value) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.lrem(key, 0, value); - } catch (Exception e) { - Logs.REDIS.error("redis lrem exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - /********************* list redis end ********************************/ - - /********************* - * mq redis start - ********************************/ - - - public static void publish(RedisServer node, String channel, T message) { - Jedis jedis = null; - String value = null; - if (message instanceof String) { - value = (String) message; - } else { - value = Jsons.toJson(message); - } - try { - jedis = getClient(node); - jedis.publish(channel, value); - } catch (Exception e) { - Logs.REDIS.error("redis publish exception:{},{},{}", value, Jsons.toJson(node), Jsons.toJson(channel), e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - public static void subscribe(Set nodeList, final JedisPubSub pubsub, final String... channels) { - for (final RedisServer node : nodeList) { - new Thread(new Runnable() { - @Override - public void run() { - subscribe(node, pubsub, channels); - } - }, node.getHost() + "_" + Jsons.toJson(channels) - ).start(); - } - } - - public static void subscribe(RedisServer node, JedisPubSub pubsub, String... channel) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.subscribe(pubsub, channel); - } catch (Exception e) { - Logs.REDIS.error("redis subscribe exception:{},{}", Jsons.toJson(node), Jsons.toJson(channel), e); - } finally { - // 返还到连接池 - close(jedis); - } - - } - - /********************* - * set redis start - ********************************/ - /** - * @param nodeList - * @param key - * @param value - */ - public static void sAdd(List nodeList, String key, String value) { - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.sadd(key, value); - } catch (Exception e) { - Logs.REDIS.error("redis sadd exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - /** - * @param node 返回个数 - * @param key - * @return - */ - public static Long sCard(RedisServer node, String key) { - - Long value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.scard(key); - } catch (Exception e) { - Logs.REDIS.error("redis scard exception:{},{}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return value; - } - - public static void sRem(List nodeList, String key, String value) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.srem(key, value); - } catch (Exception e) { - Logs.REDIS.error("redis srem exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - /** - * 默认使用每页10个 - * - * @param node - * @param key - * @param clazz - * @return - */ - public static List sScan(RedisServer node, String key, Class clazz, int start) { - - List value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - ScanResult sscanResult = jedis.sscan(key, start + "", new ScanParams().count(10)); - if (sscanResult != null && sscanResult.getResult() != null) { - value = sscanResult.getResult(); - } - } catch (Exception e) { - Logs.REDIS.error("redis sscan exception:{},{},{}", key, start, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return toList(value, clazz); - } - - /********************* - * sorted set - ********************************/ - /** - * @param nodeList - * @param key - * @param value - */ - public static void zAdd(List nodeList, String key, String value) { - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.zadd(key, 0, value); - } catch (Exception e) { - Logs.REDIS.error("redis zadd exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - } - - /** - * @param node 返回个数 - * @param key - * @return - */ - public static Long zCard(RedisServer node, String key) { - - Long value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.zcard(key); - } catch (Exception e) { - Logs.REDIS.error("redis zcard exception:{},{}", key, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - return value; - } - - public static void zRem(List nodeList, String key, String value) { - - for (RedisServer node : nodeList) { - Jedis jedis = null; - try { - jedis = getClient(node); - jedis.zrem(key, value); - } catch (Exception e) { - Logs.REDIS.error("redis srem exception:{},{},{}", key, value, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - } - - } - - /** - * 从列表中获取指定返回的元素 start 和 end - * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 - * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 - */ - public static List zrange(RedisServer node, String key, int start, int end, Class clazz) { - Set value = null; - Jedis jedis = null; - try { - jedis = getClient(node); - value = jedis.zrange(key, start, end); - } catch (Exception e) { - Logs.REDIS.error("redis zrange exception:{},{},{},{}", key, start, end, node, e); - } finally { - // 返还到连接池 - close(jedis); - } - - return toList(value, clazz); - } - - private static List toList(Collection value, Class clazz) { - if (value != null) { - List newValue = Lists.newArrayList(); - for (String temp : value) { - newValue.add(Jsons.fromJson(temp, clazz)); - } - return newValue; - } - return null; - } - - public static void destroy() { - POOL_MAP.values().forEach(Pool::close); - } -} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java deleted file mode 100644 index 61a2128d..00000000 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisGroup.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.cache.redis; - -import com.google.common.collect.Lists; - -import java.util.List; - - -/** - * redis 组 - */ -public class RedisGroup { - private List redisServerList = Lists.newArrayList(); - - public List getRedisServerList() { - return redisServerList; - } - - public void setRedisServerList(List redisServerList) { - this.redisServerList = redisServerList; - } - - public void addRedisNode(RedisServer node) { - redisServerList.add(node); - } - - public void remove(int i) { - if (redisServerList != null) { - redisServerList.remove(i); - } - } - - public void clear() { - if (redisServerList != null) { - redisServerList.clear(); - } - } - - public RedisServer get(String key) { - if (redisServerList.size() == 1) return redisServerList.get(0); - int i = key.hashCode() % redisServerList.size(); - return redisServerList.get(i); - } - - public static RedisGroup from(com.mpush.tools.config.data.RedisGroup node) { - RedisGroup group = new RedisGroup(); - for (com.mpush.tools.config.data.RedisServer rs : node.redisNodeList) { - group.addRedisNode(new RedisServer(rs.getHost(), rs.getPort(), rs.getPassword())); - } - return group; - } -} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index b4b82e8a..62168f7d 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -29,7 +29,8 @@ public final class RedisKey { private static final String USER_ONLINE_KEY = "mp_u_ol_"; - private static final String CONN_NUM_ = "mp_cn_"; + public static final String SESSION_AES_KEY = "mp_s_a_k"; + public static final String SESSION_AES_SEQ_KEY = "mp_s_a_s_k"; public static final String getUserKey(String userId) { return USER_PREFIX + userId; @@ -48,9 +49,5 @@ public static final String getUserOnlineKey(String extranetAddress) { return USER_ONLINE_KEY + extranetAddress; } -// public static final String getConnNum(String extranetAddress) { -// return CONN_NUM_ + extranetAddress; -// } - } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java index 88a4abdc..f12cdcb9 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisServer.java @@ -19,13 +19,25 @@ package com.mpush.cache.redis; +import com.mpush.tools.config.data.RedisNode; +import com.mpush.zk.node.ZKRedisNode; +import redis.clients.jedis.HostAndPort; + /** * redis 相关的配置信息 */ -public class RedisServer extends com.mpush.tools.config.data.RedisServer { +public class RedisServer extends RedisNode { + + public RedisServer(String ip, int port) { + super(ip, port); + } + + public HostAndPort convert() { + return new HostAndPort(host, port); + } - public RedisServer(String ip, int port, String password) { - super(ip, port, password); + public static RedisServer from(ZKRedisNode node) { + return new RedisServer(node.host, node.port); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java b/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java new file mode 100644 index 00000000..f52ed70d --- /dev/null +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java @@ -0,0 +1,325 @@ +/* + * Copyright 2011-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mpush.cache.redis.connection; + +import com.mpush.cache.redis.RedisServer; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import redis.clients.jedis.*; +import redis.clients.util.Pool; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Connection factory creating Jedis based connections. + * + * @author Costin Leau + * @author Thomas Darimont + * @author Christoph Strobl + * @author Mark Paluch + */ +public class RedisConnectionFactory { + + private final static Logger log = LoggerFactory.getLogger(RedisConnectionFactory.class); + + private JedisShardInfo shardInfo; + private String hostName = "localhost"; + private int port = Protocol.DEFAULT_PORT; + private int timeout = Protocol.DEFAULT_TIMEOUT; + private String password; + private Pool pool; + private JedisPoolConfig poolConfig = new JedisPoolConfig(); + private int dbIndex = 0; + private JedisCluster cluster; + private List redisServers; + private boolean isCluster = false; + + /** + * Constructs a new JedisConnectionFactory instance with default settings (default connection pooling, no + * shard information). + */ + public RedisConnectionFactory() { + } + + /** + * Returns a Jedis instance to be used as a Redis connection. The instance can be newly created or retrieved from a + * pool. + */ + protected Jedis fetchJedisConnector() { + try { + + if (pool != null) { + return pool.getResource(); + } + Jedis jedis = new Jedis(getShardInfo()); + // force initialization (see Jedis issue #82) + jedis.connect(); + return jedis; + } catch (Exception ex) { + throw new RuntimeException("Cannot get Jedis connection", ex); + } + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void init() { + if (shardInfo == null) { + shardInfo = new JedisShardInfo(hostName, port); + + if (StringUtils.isNotEmpty(password)) { + shardInfo.setPassword(password); + } + + if (timeout > 0) { + shardInfo.setConnectionTimeout(timeout); + } + } + + if (redisServers.size() == 1) { + this.pool = createPool(); + } else { + this.cluster = createCluster(); + } + } + + private Pool createPool() { + return createRedisPool(); + } + + + /** + * Creates {@link JedisPool}. + * + * @return + * @since 1.4 + */ + protected Pool createRedisPool() { + return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(), + getShardInfo().getSoTimeout(), getShardInfo().getPassword()); + } + + private JedisCluster createCluster() { + return createCluster(this.redisServers, this.poolConfig); + } + + /** + * @param poolConfig can be {@literal null}. + * @return + * @since 1.7 + */ + protected JedisCluster createCluster(List servers, GenericObjectPoolConfig poolConfig) { + + Set hostAndPort = servers + .stream() + .map(RedisServer::convert) + .collect(Collectors.toSet()); + + int redirects = 5; + + if (StringUtils.isNotEmpty(getPassword())) { + throw new IllegalArgumentException("Jedis does not support password protected Redis Cluster configurations!"); + } + + if (poolConfig != null) { + return new JedisCluster(hostAndPort, timeout, redirects, poolConfig); + } + return new JedisCluster(hostAndPort, timeout, redirects, poolConfig); + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.DisposableBean#destroy() + */ + public void destroy() { + if (pool != null) { + try { + pool.destroy(); + } catch (Exception ex) { + log.warn("Cannot properly close Jedis pool", ex); + } + pool = null; + } + if (cluster != null) { + try { + cluster.close(); + } catch (Exception ex) { + log.warn("Cannot properly close Jedis cluster", ex); + } + cluster = null; + } + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisConnectionFactory#getConnection() + */ + public Jedis getJedisConnection() { + return fetchJedisConnector(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisConnectionFactory#getClusterConnection() + */ + public JedisCluster getClusterConnection() { + return cluster; + } + + public boolean isCluster() { + return isCluster; + } + + /** + * Returns the Redis hostName. + * + * @return Returns the hostName + */ + public String getHostName() { + return hostName; + } + + /** + * Sets the Redis hostName. + * + * @param hostName The hostName to set. + */ + public void setHostName(String hostName) { + this.hostName = hostName; + } + + /** + * Returns the password used for authenticating with the Redis server. + * + * @return password for authentication + */ + public String getPassword() { + return password; + } + + /** + * Sets the password used for authenticating with the Redis server. + * + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * Returns the port used to connect to the Redis instance. + * + * @return Redis port. + */ + public int getPort() { + return port; + + } + + /** + * Sets the port used to connect to the Redis instance. + * + * @param port Redis port + */ + public void setPort(int port) { + this.port = port; + } + + /** + * Returns the shardInfo. + * + * @return Returns the shardInfo + */ + public JedisShardInfo getShardInfo() { + return shardInfo; + } + + /** + * Sets the shard info for this factory. + * + * @param shardInfo The shardInfo to set. + */ + public void setShardInfo(JedisShardInfo shardInfo) { + this.shardInfo = shardInfo; + } + + /** + * Returns the timeout. + * + * @return Returns the timeout + */ + public int getTimeout() { + return timeout; + } + + /** + * @param timeout The timeout to set. + */ + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + /** + * Returns the poolConfig. + * + * @return Returns the poolConfig + */ + public JedisPoolConfig getPoolConfig() { + return poolConfig; + } + + /** + * Sets the pool configuration for this factory. + * + * @param poolConfig The poolConfig to set. + */ + public void setPoolConfig(JedisPoolConfig poolConfig) { + this.poolConfig = poolConfig; + } + + /** + * Returns the index of the database. + * + * @return Returns the database index + */ + public int getDatabase() { + return dbIndex; + } + + /** + * Sets the index of the database used by this connection factory. Default is 0. + * + * @param index database index + */ + public void setDatabase(int index) { + this.dbIndex = index; + } + + public void setCluster(boolean cluster) { + isCluster = cluster; + } + + public void setRedisServers(List redisServers) { + this.redisServers = redisServers; + } + + +} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java deleted file mode 100644 index ee43587a..00000000 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/ConsistentHash.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.cache.redis.hash; - -import redis.clients.util.Hashing; - -import java.util.Collection; -import java.util.SortedMap; -import java.util.TreeMap; - -public class ConsistentHash { - - private final Hashing hash; - private final int numberOfReplicas; - private final SortedMap circle = new TreeMap(); - - public ConsistentHash(Hashing hash, int numberOfReplicas, - Collection nodes) { - super(); - this.hash = hash; - this.numberOfReplicas = numberOfReplicas; - for (Node node : nodes) { - add(node); - } - } - - /** - * 增加真实机器节点 - * - * @param node - */ - public void add(Node node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.put(this.hash.hash(node.toString() + i), node); - } - } - - /** - * 删除真实机器节点 - * - * @param node - */ - public void remove(String node) { - for (int i = 0; i < this.numberOfReplicas; i++) { - circle.remove(this.hash.hash(node.toString() + i)); - } - } - - /** - * 取得真实机器节点 - * - * @param key - * @return - */ - public Node get(String key) { - if (circle.isEmpty()) { - return null; - } - long hash = this.hash.hash(key); - if (!circle.containsKey(hash)) { - SortedMap tailMap = circle.tailMap(hash);// 沿环的顺时针找到一个虚拟节点 - hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); - } - return circle.get(hash); // 返回该虚拟节点对应的真实机器节点的信息 - } - - -} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java b/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java deleted file mode 100644 index 295dfbf3..00000000 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/hash/Node.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.cache.redis.hash; - -public class Node { - - private String ip; //机器ip - private String name;//名字 - - public Node(String ip, String name) { - this.ip = ip; - this.name = name; - } - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java index 9c90877b..8126c85f 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisClusterManager.java @@ -19,7 +19,6 @@ package com.mpush.cache.redis.manager; -import com.mpush.cache.redis.RedisGroup; import com.mpush.cache.redis.RedisServer; import java.util.List; @@ -28,9 +27,5 @@ public interface RedisClusterManager { void init(); - List getGroupList(); - - RedisServer randomGetRedisNode(String key); - - List hashSet(String key); + List getServers(); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index b5d2b060..85373178 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -19,17 +19,17 @@ package com.mpush.cache.redis.manager; -import com.google.common.collect.Sets; -import com.mpush.cache.redis.RedisClient; -import com.mpush.cache.redis.RedisGroup; -import com.mpush.cache.redis.RedisServer; +import com.google.common.collect.Lists; +import com.mpush.cache.redis.connection.RedisConnectionFactory; import com.mpush.tools.Jsons; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPubSub; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import redis.clients.jedis.*; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; /** * redis 对外封装接口 @@ -37,39 +37,85 @@ public final class RedisManager { public static final RedisManager I = new RedisManager(); - private final RedisClusterManager clusterManager = ZKRedisClusterManager.I; + private RedisConnectionFactory factory = new RedisConnectionFactory(); public void init() { + RedisClusterManager clusterManager = new ZKRedisClusterManager(); clusterManager.init(); - test(clusterManager.getGroupList()); + factory.setPassword(CC.mp.redis.password); + factory.setPoolConfig(CC.mp.redis.getPoolConfig(JedisPoolConfig.class)); + factory.setRedisServers(clusterManager.getServers()); + factory.setCluster(CC.mp.redis.isCluster()); + factory.init(); + test(); } - public long incr(String key, Integer time) { - List nodeList = clusterManager.hashSet(key); - return RedisClient.incr(nodeList, key, time); + + private R call(Function function, R d) { + if (factory.isCluster()) { + try { + return function.apply(factory.getClusterConnection()); + } catch (Exception e) { + Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + } + } else { + try (Jedis jedis = factory.getJedisConnection()) { + return function.apply(jedis); + } catch (Exception e) { + Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + } + } + return d; } - public long incrBy(String key, long delt) { - List nodeList = clusterManager.hashSet(key); - return RedisClient.incrBy(nodeList, key, delt); + private void call(Consumer consumer) { + if (factory.isCluster()) { + try { + consumer.accept(factory.getClusterConnection()); + } catch (Exception e) { + Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + } + } else { + try (Jedis jedis = factory.getJedisConnection()) { + consumer.accept(jedis); + } catch (Exception e) { + Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + } + } } - /********************* - * k v redis start - ********************************/ + public long incr(String key) { + return call(jedis -> jedis.incr(key), 0L); + } + public long incrBy(String key, long delt) { + return call(jedis -> jedis.incrBy(key, delt), 0L); + } + + /********************* k v redis start ********************************/ + /** + * @param key + * @param clazz + * @return + */ + @SuppressWarnings("unchecked") public T get(String key, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.get(node, key, clazz); + String value = call(jedis -> jedis.get(key), null); + if (value == null) return null; + if (clazz == String.class) return (T) value; + return Jsons.fromJson(value, clazz); + } + + public void set(String key, String value) { + set(key, value, 0); } public void set(String key, T value) { - set(key, value, null); + set(key, value, 0); } - public void set(String key, T value, Integer time) { - String jsonValue = Jsons.toJson(value); - set(key, jsonValue, time); + public void set(String key, T value, int time) { + set(key, Jsons.toJson(value), time); } /** @@ -77,67 +123,64 @@ public void set(String key, T value, Integer time) { * @param value * @param time seconds */ - public void set(String key, String value, Integer time) { - List nodeList = clusterManager.hashSet(key); - RedisClient.set(nodeList, key, value, time); + public void set(String key, String value, int time) { + call(jedis -> { + jedis.set(key, value); + if (time > 0) { + jedis.expire(key, time); + } + }); } public void del(String key) { - - List nodeList = clusterManager.hashSet(key); - RedisClient.del(nodeList, key); - + call(jedis -> jedis.del(key)); } - /*********************k v redis end********************************/ - + /********************* k v redis end ********************************/ /********************* * hash redis start ********************************/ public void hset(String key, String field, String value) { - - List nodeList = clusterManager.hashSet(field); - RedisClient.hset(nodeList, key, field, value); - + call(jedis -> jedis.hset(key, field, value)); } public void hset(String key, String field, T value) { hset(key, field, Jsons.toJson(value)); } + @SuppressWarnings("unchecked") public T hget(String key, String field, Class clazz) { - - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.hget(node, key, field, clazz); - + String value = call(jedis -> jedis.hget(key, field), null); + if (value == null) return null; + if (clazz == String.class) return (T) value; + return Jsons.fromJson(value, clazz); } public void hdel(String key, String field) { - List nodeList = clusterManager.hashSet(key); - RedisClient.hdel(nodeList, key, field); + call(jedis -> jedis.hdel(key, field)); } public Map hgetAll(String key) { - - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.hgetAll(node, key); - + return call(jedis -> jedis.hgetAll(key), Collections.emptyMap()); } public Map hgetAll(String key, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.hgetAll(node, key, clazz); + Map result = hgetAll(key); + if (result.isEmpty()) return Collections.emptyMap(); + Map newMap = new HashMap<>(result.size()); + result.forEach((k, v) -> newMap.put(k, Jsons.fromJson(v, clazz))); + return newMap; } /** * 返回 key 指定的哈希集中所有字段的名字。 * + * @param key * @return */ public Set hkeys(String key) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.hkeys(node, key); + return call(jedis -> jedis.hkeys(key), Collections.emptySet()); } /** @@ -148,36 +191,41 @@ public Set hkeys(String key) { * @return */ public List hmget(String key, Class clazz, String... fields) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.hmget(node, key, clazz, fields); + return call(jedis -> jedis.hmget(key, fields), Collections.emptyList()) + .stream() + .map(s -> Jsons.fromJson(s, clazz)) + .collect(Collectors.toList()); + } /** - * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联 + * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key + * 关联 * * @param hash * @param time */ - public void hmset(String key, Map hash, Integer time) { - List nodeList = clusterManager.hashSet(key); - RedisClient.hmset(nodeList, key, hash, time); + public void hmset(String key, Map hash, int time) { + call(jedis -> { + jedis.hmset(key, hash); + if (time > 0) { + jedis.expire(key, time); + } + }); } public void hmset(String key, Map hash) { - hmset(key, hash, null); + hmset(key, hash, 0); } + /********************* hash redis end ********************************/ - /*********************hash redis end********************************/ - - - /*********************list redis start********************************/ + /********************* list redis start ********************************/ /** * 从队列的左边入队 */ public void lpush(String key, String value) { - List nodeList = clusterManager.hashSet(key); - RedisClient.lpush(nodeList, key, value); + call(jedis -> jedis.lpush(key, value)); } public void lpush(String key, T value) { @@ -188,8 +236,7 @@ public void lpush(String key, T value) { * 从队列的右边入队 */ public void rpush(String key, String value) { - List nodeList = clusterManager.hashSet(key); - RedisClient.rpush(nodeList, key, value); + call(jedis -> jedis.lpush(key, value)); } public void rpush(String key, T value) { @@ -199,124 +246,161 @@ public void rpush(String key, T value) { /** * 移除并且返回 key 对应的 list 的第一个元素 */ + @SuppressWarnings("unchecked") public T lpop(String key, Class clazz) { - List nodeList = clusterManager.hashSet(key); - return RedisClient.lpop(nodeList, key, clazz); + String value = call(jedis -> jedis.lpop(key), null); + if (value == null) return null; + if (clazz == String.class) return (T) value; + return Jsons.fromJson(value, clazz); } /** * 从队列的右边出队一个元素 */ + @SuppressWarnings("unchecked") public T rpop(String key, Class clazz) { - List nodeList = clusterManager.hashSet(key); - return RedisClient.rpop(nodeList, key, clazz); + String value = call(jedis -> jedis.rpop(key), null); + if (value == null) return null; + if (clazz == String.class) return (T) value; + return Jsons.fromJson(value, clazz); } - /** - * 从列表中获取指定返回的元素 - * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 从列表中获取指定返回的元素 start 和 end + * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 */ public List lrange(String key, int start, int end, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.lrange(node, key, start, end, clazz); + return call(jedis -> jedis.lrange(key, start, end), Collections.emptyList()) + .stream() + .map(s -> Jsons.fromJson(s, clazz)) + .collect(Collectors.toList()); } /** - * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。 + * 返回存储在 key 里的list的长度。 如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key + * 里的值不是一个list的话,会返回error。 */ public long llen(String key) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.llen(node, key); + return call(jedis -> jedis.llen(key), 0L); } - public void lrem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = clusterManager.hashSet(key); - RedisClient.lRem(nodeList, key, jsonValue); + /** + * 移除表中所有与 value 相等的值 + * + * @param key + * @param value + */ + public void lRem(String key, String value) { + call(jedis -> jedis.lrem(key, 0, value)); } - public void publish(String channel, T message) { - - RedisServer node = clusterManager.randomGetRedisNode(channel); - RedisClient.publish(node, channel, message); + /********************* list redis end ********************************/ - } + /********************* + * mq redis start + ********************************/ - public void subscribe(JedisPubSub pubsub, String... channels) { - Set set = Sets.newHashSet(); - for (String channel : channels) { - List nodeList = clusterManager.hashSet(channel); - set.addAll(nodeList); - } + public void publish(String channel, T message) { + call(jedis -> ((MultiKeyCommands) jedis).publish(channel, message instanceof String ? (String) message : Jsons.toJson(message))); + } - RedisClient.subscribe(set, pubsub, channels); + public void subscribe(final JedisPubSub pubsub, final String... channels) { + new Thread(() -> call(jedis -> + Arrays.stream(channels).forEach(channel -> ((MultiKeyCommands) jedis).subscribe(pubsub, channel)) + ), Arrays.toString(channels)).start(); } - public void sAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = clusterManager.hashSet(key); - RedisClient.sAdd(nodeList, key, jsonValue); + /********************* + * set redis start + ********************************/ + /** + * @param key + * @param value + */ + public void sAdd(String key, String value) { + call(jedis -> jedis.sadd(key, value)); } - public Long sCard(String key) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.sCard(node, key); + /** + * @param key + * @return + */ + public long sCard(String key) { + return call(jedis -> jedis.scard(key), 0L); } - public void sRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = clusterManager.hashSet(key); - RedisClient.sRem(nodeList, key, jsonValue); + public void sRem(String key, String value) { + call(jedis -> jedis.srem(key, value)); } - public List sScan(String key, int start, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.sScan(node, key, clazz, start); + /** + * 默认使用每页10个 + * + * @param key + * @param clazz + * @return + */ + public List sScan(String key, Class clazz, int start) { + List list = call(jedis -> jedis.sscan(key, Integer.toString(start), new ScanParams().count(10)).getResult(), null); + return toList(list, clazz); } - public void zAdd(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = clusterManager.hashSet(key); - RedisClient.zAdd(nodeList, key, jsonValue); + /********************* + * sorted set + ********************************/ + /** + * @param key + * @param value + */ + public void zAdd(String key, String value) { + call(jedis -> jedis.zadd(key, 0, value)); } + /** + * @param key + * @return + */ public Long zCard(String key) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.zCard(node, key); + return call(jedis -> jedis.zcard(key), 0L); } - public void zRem(String key, T value) { - String jsonValue = Jsons.toJson(value); - List nodeList = clusterManager.hashSet(key); - RedisClient.zRem(nodeList, key, jsonValue); + public void zRem(String key, String value) { + call(jedis -> jedis.zrem(key, value)); } + /** + * 从列表中获取指定返回的元素 start 和 end + * 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 + * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。 + */ public List zrange(String key, int start, int end, Class clazz) { - RedisServer node = clusterManager.randomGetRedisNode(key); - return RedisClient.zrange(node, key, start, end, clazz); + Set value = call(jedis -> jedis.zrange(key, start, end), null); + return toList(value, clazz); } - public void test(List groupList) { - if (groupList == null || groupList.isEmpty()) { - throw new RuntimeException("init redis sever error."); - } - for (RedisGroup group : groupList) { - List list = group.getRedisServerList(); - if (list == null || list.isEmpty()) { - throw new RuntimeException("init redis sever error."); - } - for (RedisServer node : list) { - Jedis jedis = RedisClient.getClient(node); - if (jedis == null) throw new RuntimeException("init redis sever error."); - jedis.close(); + private List toList(Collection value, Class clazz) { + if (value != null) { + List newValue = Lists.newArrayList(); + for (String temp : value) { + newValue.add(Jsons.fromJson(temp, clazz)); } + return newValue; } + return null; + } + + public void destroy() { + if (factory != null) factory.destroy(); } - public void close() { - RedisClient.destroy(); + public void test() { + JedisCluster cluster = factory.getClusterConnection(); + Jedis jedis = factory.getJedisConnection(); + if (cluster == null && jedis == null) { + throw new RuntimeException("init redis sever error."); + } + if (jedis != null) jedis.close(); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index 834175f0..b0ebba9a 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -20,100 +20,58 @@ package com.mpush.cache.redis.manager; import com.mpush.cache.redis.RedisException; -import com.mpush.cache.redis.RedisGroup; import com.mpush.cache.redis.RedisServer; import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; +import com.mpush.tools.config.data.RedisNode; import com.mpush.tools.log.Logs; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKRedisNodeWatcher; import com.mpush.zk.node.ZKRedisNode; import org.apache.commons.collections4.CollectionUtils; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import static com.mpush.zk.ZKPath.REDIS_SERVER; public class ZKRedisClusterManager implements RedisClusterManager { - public static final ZKRedisClusterManager I = new ZKRedisClusterManager(); - private AtomicBoolean init = new AtomicBoolean(false); + private final ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); - private ZKRedisClusterManager() { + public ZKRedisClusterManager() { } - private final List groups = new ArrayList<>(); - /** * zk 启动的时候需要调用这个 */ @Override public void init() { - if (init.compareAndSet(false, true)) { - Logs.Console.info("begin init redis cluster"); - if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); - List groupList = CC.mp.redis.cluster_group; - - if (CollectionUtils.isNotEmpty(groupList)) { - register(groupList); - } - - ZKRedisNodeWatcher watcher = new ZKRedisNodeWatcher(); - watcher.beginWatch(); - Collection nodes = watcher.getCache().values(); - if (CollectionUtils.isEmpty(nodes)) { - Logs.REDIS.error("init redis client error, redis server is none."); - throw new RedisException("init redis client error, redis server is none."); - } + Logs.Console.info("begin init redis cluster"); + if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); - nodes.stream().map(RedisGroup::from).forEach(groups::add); - - if (groups.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); - Logs.Console.info("init redis cluster success..."); + if (CollectionUtils.isNotEmpty(CC.mp.redis.nodes)) { + register(CC.mp.redis.nodes); } - } - @Override - public List getGroupList() { - return Collections.unmodifiableList(groups); - } - - public int groupSize() { - return groups.size(); - } + watcher.watch(); + Collection nodes = watcher.getCache().values(); + if (CollectionUtils.isEmpty(nodes)) { + Logs.REDIS.error("init redis client error, redis server is none."); + throw new RedisException("init redis client error, redis server is none."); + } - /** - * 随机获取一个redis 实例 - * - * @param key - * @return - */ - @Override - public RedisServer randomGetRedisNode(String key) { - int size = groupSize(); - if (size == 1) return groups.get(0).get(key); - int i = (int) ((Math.random() % size) * size); - RedisGroup group = groups.get(i); - return group.get(key); + if (nodes.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); + Logs.Console.info("init redis cluster success..."); } - /** - * 写操作的时候,获取所有redis 实例 - * - * @param key - * @return - */ @Override - public List hashSet(String key) { - return groups.stream().map(g -> g.get(key)).collect(Collectors.toList()); + public List getServers() { + return watcher.getCache().values().stream().map(RedisServer::from).collect(Collectors.toList()); } - private void register(List groupList) { - String data = Jsons.toJson(groupList); + private void register(List nodes) { + String data = Jsons.toJson(nodes); if (CC.mp.redis.write_to_zk //强制刷新ZK || !ZKClient.I.isExisted(REDIS_SERVER.getRootPath())//redis节点不存在 || !ZKClient.I.get(REDIS_SERVER.getRootPath()).equals(data)) {//数据有变更 @@ -121,8 +79,4 @@ private void register(List groupList) { Logs.Console.info("register redis server group success, group={}", data); } } - - public void addGroup(RedisGroup group) { - groups.add(group); - } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index e01e4e5e..685d632b 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -86,13 +86,16 @@ Collection getAllConnections() { protected void doStart(Listener listener) throws Throwable { ZKClient.I.start(listener); RedisManager.I.init(); - ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).beginWatch(); + ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).watch(); + PushRequestBus.I.start(listener); } @Override protected void doStop(Listener listener) throws Throwable { factory.clear(); ZKClient.I.stop(listener); + RedisManager.I.destroy(); + PushRequestBus.I.stop(listener); } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 45ebbdde..28c444c7 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -20,6 +20,8 @@ package com.mpush.client.push; import com.mpush.api.push.PushException; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.tools.thread.PoolThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; @@ -35,18 +37,14 @@ * * @author ohun@live.cn */ -public class PushRequestBus { +public class PushRequestBus extends BaseService { public static final PushRequestBus I = new PushRequestBus(); private final Logger logger = LoggerFactory.getLogger(PushRequestBus.class); private final Map reqQueue = new ConcurrentHashMap<>(1024); - private final Executor executor = ThreadPoolManager.I.getPushCallbackExecutor(); - private final ScheduledExecutorService scheduledExecutor; + private Executor executor; + private ScheduledExecutorService scheduledExecutor; private PushRequestBus() { - scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { - logger.error("one push request was rejected, request=" + r); - throw new PushException("one push request was rejected. request=" + r); - }); } public Future put(int sessionId, PushRequest request) { @@ -61,4 +59,19 @@ public PushRequest getAndRemove(int sessionId) { public void asyncCall(Runnable runnable) { executor.execute(runnable); } + + @Override + protected void doStart(Listener listener) throws Throwable { + executor = ThreadPoolManager.I.getPushCallbackExecutor(); + scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { + logger.error("one push request was rejected, request=" + r); + throw new PushException("one push request was rejected. request=" + r); + }); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + scheduledExecutor.shutdown(); + ((ExecutorService)executor).shutdown(); + } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index 7bc17b78..72a323a6 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -27,12 +27,8 @@ import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; -import com.mpush.zk.ZKPath; -import com.mpush.zk.ZKRegister; import com.mpush.zk.cache.ZKDnsNodeCache; import com.mpush.zk.listener.ZKDnsNodeWatcher; -import com.mpush.zk.node.ZKDnsNode; -import com.mpush.zk.node.ZKRedisNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +55,7 @@ public HttpProxyDnsMappingManager() { @Override protected void doStart(Listener listener) throws Throwable { - watcher.startWatch(); + watcher.watch(); if (all.size() > 0) { executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 3e92d0c3..98c3bade 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -45,7 +45,7 @@ public FastConnectMessage decode(Packet packet, Connection connection) { @Override public void handle(FastConnectMessage message) { //从缓存中心查询session - ReusableSession session = ReusableSessionManager.INSTANCE.querySession(message.sessionId); + ReusableSession session = ReusableSessionManager.I.querySession(message.sessionId); if (session == null) { //1.没查到说明session已经失效了 diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 17879376..842324ae 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -76,7 +76,7 @@ public void handle(HandshakeMessage message) { context.changeCipher(new AesCipher(clientKey, iv)); //4.生成可复用session, 用于快速重连 - ReusableSession session = ReusableSessionManager.INSTANCE.genSession(context); + ReusableSession session = ReusableSessionManager.I.genSession(context); //5.计算心跳时间 int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); @@ -101,7 +101,7 @@ public void handle(HandshakeMessage message) { .setHeartbeat(heartbeat); //9.保存可复用session到Redis, 用于快速重连 - ReusableSessionManager.INSTANCE.cacheSession(session); + ReusableSessionManager.I.cacheSession(session); //10.触发握手成功事件 EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java index 940dd8c3..885d6aa8 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java @@ -33,7 +33,7 @@ public class ConnectClientBoot { public void run() { ZKClient.I.start(); RedisManager.I.init(); - listener.beginWatch(); + listener.watch(); } public List getServers() { diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index fa613266..a9ca5e50 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -36,7 +36,7 @@ public void setUp() throws Exception { @Test public void testKey() { //String t = ConfigKey.app_env.getString(); - System.out.println(CC.mp.redis.cluster_group.size()); + System.out.println(CC.mp.redis.nodes.size()); } @Test diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index f5b99e83..0031538a 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -37,6 +37,7 @@ public static void main(String[] args) throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().get(); + Thread.sleep(5000); PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); msg.setMsgId("msgId_0"); @@ -50,9 +51,10 @@ public void onResult(PushResult result) { System.err.println(result); } }); - Thread.sleep(1000); - FutureTask future = sender.send(context); - future.get(); + /*FutureTask future = sender.send(context); + future.get();*/ + sender.stop().get(); + System.out.println("stop"); } } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java b/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java deleted file mode 100644 index 69be00b2..00000000 --- a/mpush-test/src/test/java/com/mpush/test/redis/ConsistentHashTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.test.redis; - -import com.mpush.cache.redis.hash.ConsistentHash; -import com.mpush.cache.redis.hash.Node; -import org.junit.Test; -import redis.clients.util.Hashing; -import redis.clients.util.MurmurHash; - -import java.util.*; - -public class ConsistentHashTest { - - private static final String IP_PREFIX = "192.168.1.";// 机器节点IP前缀 - - @Test - public void test() { - Map map = new HashMap();// 每台真实机器节点上保存的记录条数 - List nodes = new ArrayList();// 真实机器节点 - // 10台真实机器节点集群 - for (int i = 1; i <= 10; i++) { - map.put(IP_PREFIX + i, 0);// 每台真实机器节点上保存的记录条数初始为0 - Node node = new Node(IP_PREFIX + i, "node" + i); - nodes.add(node); - } - Hashing hashFunction = new MurmurHash(); // hash函数实例 - ConsistentHash consistentHash = new ConsistentHash(hashFunction, 100, Collections.unmodifiableCollection(nodes));// 每台真实机器引入100个虚拟节点 - // 将5000条记录尽可能均匀的存储到10台机器节点 - for (int i = 0; i < 5000; i++) { - // 产生随机一个字符串当做一条记录,可以是其它更复杂的业务对象,比如随机字符串相当于 - String data = UUID.randomUUID().toString() + i; - // 通过记录找到真实机器节点 - Node node = consistentHash.get(data); - // 再这里可以能过其它工具将记录存储真实机器节点上,比如MemoryCache等 - // 每台真实机器节点上保存的记录条数加1 - map.put(node.getIp(), map.get(node.getIp()) + 1); - } - // 打印每台真实机器节点保存的记录条数 - for (int i = 1; i <= 10; i++) { - System.out.println(IP_PREFIX + i + "节点记录条数:" - + map.get("192.168.1." + i)); - } - - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java index f3cf52e6..5f1246c0 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java @@ -19,10 +19,7 @@ package com.mpush.test.redis; -import com.mpush.cache.redis.RedisGroup; -import com.mpush.cache.redis.RedisServer; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.cache.redis.manager.ZKRedisClusterManager; import com.mpush.cache.redis.mq.Subscriber; import org.junit.Before; import org.junit.Test; @@ -31,14 +28,8 @@ public class PubSubTest { - private ZKRedisClusterManager redisClusterManager = ZKRedisClusterManager.I; - @Before public void init() { - RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); - RedisGroup group = new RedisGroup(); - group.addRedisNode(node); - redisClusterManager.addGroup(group); } @Test diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java index 7069153a..726bf53b 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java @@ -19,10 +19,10 @@ package com.mpush.test.redis; -import com.mpush.cache.redis.RedisClient; import com.mpush.tools.Jsons; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.HostAndPort; @@ -46,7 +46,7 @@ public void init() { jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); - cluster = new JedisCluster(jedisClusterNodes, RedisClient.CONFIG); + cluster = new JedisCluster(jedisClusterNodes, new GenericObjectPoolConfig()); } @Test diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index 89ef5cb3..260a479d 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -15,12 +15,13 @@ * * Contributors: * ohun@live.cn (夜色) - */ + *//* + package com.mpush.test.redis; import com.google.common.collect.Lists; -import com.mpush.cache.redis.RedisClient; +import com.mpush.cache.redis.client.RedisClient; import com.mpush.cache.redis.RedisServer; import org.apache.commons.lang3.builder.ToStringBuilder; import org.junit.Test; @@ -34,7 +35,7 @@ public class RedisUtilTest { - RedisServer node = new RedisServer("127.0.0.1", 6379, "shinemoIpo"); + RedisServer node = new RedisServer("127.0.0.1", 6379); List nodeList = Lists.newArrayList(node); @@ -213,3 +214,4 @@ public void testsortedset() { } } +*/ diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 74ad3683..a61a2696 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -20,12 +20,8 @@ package com.mpush.tools.config; import com.mpush.api.spi.net.DnsMapping; -import com.mpush.tools.config.data.RedisGroup; -import com.mpush.tools.config.data.RedisServer; -import com.typesafe.config.Config; -import com.typesafe.config.ConfigFactory; -import com.typesafe.config.ConfigList; -import com.typesafe.config.ConfigObject; +import com.mpush.tools.config.data.RedisNode; +import com.typesafe.config.*; import java.io.File; import java.time.Duration; @@ -35,7 +31,6 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import static com.typesafe.config.ConfigBeanFactory.create; import static java.util.stream.Collectors.toCollection; /** @@ -238,67 +233,27 @@ interface retry { int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS); } - } interface redis { Config cfg = mp.cfg.getObject("redis").toConfig(); boolean write_to_zk = cfg.getBoolean("write-to-zk"); + String password = cfg.getString("password"); + String clusterModel = cfg.getString("cluster-model"); - List cluster_group = cfg.getList("cluster-group") + List nodes = cfg.getList("nodes") .stream()//第一纬度数组 - .map(v -> new RedisGroup( - ConfigList.class.cast(v)//第二纬度数组 - .stream() - .map(cv -> RedisServer.from(cv.unwrapped().toString()))//把字符串转换成 RedisServer - .collect(toCollection(ArrayList::new)) - ) - ) + .map(v -> RedisNode.from(v.unwrapped().toString())) .collect(toCollection(ArrayList::new)); - interface config { - - Config cfg = redis.cfg.getObject("config").toConfig(); - - boolean jmxEnabled = cfg.getBoolean("jmxEnabled"); - - int minIdle = cfg.getInt("minIdle"); - - boolean testOnReturn = cfg.getBoolean("testOnReturn"); - - long softMinEvictableIdleTimeMillis = cfg.getDuration("softMinEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); - - boolean testOnBorrow = cfg.getBoolean("testOnBorrow"); - - boolean testWhileIdle = cfg.getBoolean("testWhileIdle"); - - long maxWaitMillis = cfg.getDuration("maxWaitMillis", TimeUnit.MILLISECONDS); - - String jmxNameBase = cfg.getString("jmxNameBase"); - - int numTestsPerEvictionRun = (int) cfg.getDuration("numTestsPerEvictionRun", TimeUnit.MILLISECONDS); - - String jmxNamePrefix = cfg.getString("jmxNamePrefix"); - - long minEvictableIdleTimeMillis = cfg.getDuration("minEvictableIdleTimeMillis", TimeUnit.MILLISECONDS); - - boolean blockWhenExhausted = cfg.getBoolean("blockWhenExhausted"); - - boolean fairness = cfg.getBoolean("fairness"); - - long timeBetweenEvictionRunsMillis = cfg.getDuration("timeBetweenEvictionRunsMillis", TimeUnit.MILLISECONDS); - - boolean testOnCreate = cfg.getBoolean("testOnCreate"); - - int maxIdle = cfg.getInt("maxIdle"); - - boolean lifo = cfg.getBoolean("lifo"); - - int maxTotal = cfg.getInt("maxTotal"); - + static boolean isCluster() { + return "cluster".equals(clusterModel); } + static T getPoolConfig(Class clazz) { + return ConfigBeanImpl.createInternal(cfg.getObject("config").toConfig(), clazz); + } } interface http { @@ -324,7 +279,6 @@ static Map> loadMapping() { ); return map; } - } interface monitor { diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/ConfigBeanImpl.java b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigBeanImpl.java new file mode 100644 index 00000000..c35ca70c --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/config/ConfigBeanImpl.java @@ -0,0 +1,238 @@ +package com.mpush.tools.config; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.time.Duration; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigObject; +import com.typesafe.config.ConfigList; +import com.typesafe.config.ConfigException; +import com.typesafe.config.ConfigMemorySize; +import com.typesafe.config.ConfigValue; +import com.typesafe.config.ConfigValueType; +import com.typesafe.config.impl.ConfigImplUtil; + +/** + * Internal implementation detail, not ABI stable, do not touch. + * For use only by the {@link com.typesafe.config} package. + */ +public final class ConfigBeanImpl { + + /** + * This is public ONLY for use by the "config" package, DO NOT USE this ABI + * may change. + * + * @param type of the bean + * @param config config to use + * @param clazz class of the bean + * @return the bean instance + */ + public static T createInternal(Config config, Class clazz) { + + Map configProps = new HashMap(); + Map originalNames = new HashMap(); + for (Map.Entry configProp : config.root().entrySet()) { + String originalName = configProp.getKey(); + String camelName = toCamelCase(originalName); + // if a setting is in there both as some hyphen name and the camel name, + // the camel one wins + if (originalNames.containsKey(camelName) && !originalName.equals(camelName)) { + // if we aren't a camel name to start with, we lose. + // if we are or we are the first matching key, we win. + } else { + configProps.put(camelName, (ConfigValue) configProp.getValue()); + originalNames.put(camelName, originalName); + } + } + + BeanInfo beanInfo = null; + try { + beanInfo = Introspector.getBeanInfo(clazz); + } catch (IntrospectionException e) { + throw new ConfigException.BadBean("Could not get bean information for class " + clazz.getName(), e); + } + + try { + List beanProps = new ArrayList(); + for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) { + if (beanProp.getReadMethod() == null || beanProp.getWriteMethod() == null) { + continue; + } + beanProps.add(beanProp); + } + + // Fill in the bean instance + T bean = clazz.newInstance(); + for (PropertyDescriptor beanProp : beanProps) { + Method setter = beanProp.getWriteMethod(); + Type parameterType = setter.getGenericParameterTypes()[0]; + Class parameterClass = setter.getParameterTypes()[0]; + String configKey = originalNames.get(beanProp.getName()); + if (configKey != null) { + Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configKey); + setter.invoke(bean, unwrapped); + } + } + return bean; + } catch (InstantiationException e) { + throw new ConfigException.BadBean(clazz.getName() + " needs a public no-args constructor to be used as a bean", e); + } catch (IllegalAccessException e) { + throw new ConfigException.BadBean(clazz.getName() + " getters and setters are not accessible, they must be for use as a bean", e); + } catch (InvocationTargetException e) { + throw new ConfigException.BadBean("Calling bean method on " + clazz.getName() + " caused an exception", e); + } + } + + // we could magically make this work in many cases by doing + // getAnyRef() (or getValue().unwrapped()), but anytime we + // rely on that, we aren't doing the type conversions Config + // usually does, and we will throw ClassCastException instead + // of a nicer error message giving the name of the bad + // setting. So, instead, we only support a limited number of + // types plus you can always use Object, ConfigValue, Config, + // ConfigObject, etc. as an escape hatch. + private static Object getValue(Class beanClass, Type parameterType, Class parameterClass, Config config, + String configPropName) { + if (parameterClass == Boolean.class || parameterClass == boolean.class) { + return config.getBoolean(configPropName); + } else if (parameterClass == Integer.class || parameterClass == int.class) { + return config.getInt(configPropName); + } else if (parameterClass == Double.class || parameterClass == double.class) { + return config.getDouble(configPropName); + } else if (parameterClass == Long.class || parameterClass == long.class) { + return config.getLong(configPropName); + } else if (parameterClass == String.class) { + return config.getString(configPropName); + } else if (parameterClass == Duration.class) { + return config.getDuration(configPropName); + } else if (parameterClass == ConfigMemorySize.class) { + return config.getMemorySize(configPropName); + } else if (parameterClass == Object.class) { + return config.getAnyRef(configPropName); + } else if (parameterClass == List.class) { + return getListValue(beanClass, parameterType, parameterClass, config, configPropName); + } else if (parameterClass == Map.class) { + // we could do better here, but right now we don't. + Type[] typeArgs = ((ParameterizedType) parameterType).getActualTypeArguments(); + if (typeArgs[0] != String.class || typeArgs[1] != Object.class) { + throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported Map<" + typeArgs[0] + "," + typeArgs[1] + ">, only Map is supported right now"); + } + return config.getObject(configPropName).unwrapped(); + } else if (parameterClass == Config.class) { + return config.getConfig(configPropName); + } else if (parameterClass == ConfigObject.class) { + return config.getObject(configPropName); + } else if (parameterClass == ConfigValue.class) { + return config.getValue(configPropName); + } else if (parameterClass == ConfigList.class) { + return config.getList(configPropName); + } else if (hasAtLeastOneBeanProperty(parameterClass)) { + return createInternal(config.getConfig(configPropName), parameterClass); + } else { + throw new ConfigException.BadBean("Bean property " + configPropName + " of class " + beanClass.getName() + " has unsupported type " + parameterType); + } + } + + private static Object getListValue(Class beanClass, Type parameterType, Class parameterClass, Config config, String configPropName) { + Type elementType = ((ParameterizedType) parameterType).getActualTypeArguments()[0]; + + if (elementType == Boolean.class) { + return config.getBooleanList(configPropName); + } else if (elementType == Integer.class) { + return config.getIntList(configPropName); + } else if (elementType == Double.class) { + return config.getDoubleList(configPropName); + } else if (elementType == Long.class) { + return config.getLongList(configPropName); + } else if (elementType == String.class) { + return config.getStringList(configPropName); + } else if (elementType == Duration.class) { + return config.getDurationList(configPropName); + } else if (elementType == ConfigMemorySize.class) { + return config.getMemorySizeList(configPropName); + } else if (elementType == Object.class) { + return config.getAnyRefList(configPropName); + } else if (elementType == Config.class) { + return config.getConfigList(configPropName); + } else if (elementType == ConfigObject.class) { + return config.getObjectList(configPropName); + } else if (elementType == ConfigValue.class) { + return config.getList(configPropName); + } else { + throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported list element type " + elementType); + } + } + + // null if we can't easily say; this is heuristic/best-effort + private static ConfigValueType getValueTypeOrNull(Class parameterClass) { + if (parameterClass == Boolean.class || parameterClass == boolean.class) { + return ConfigValueType.BOOLEAN; + } else if (parameterClass == Integer.class || parameterClass == int.class) { + return ConfigValueType.NUMBER; + } else if (parameterClass == Double.class || parameterClass == double.class) { + return ConfigValueType.NUMBER; + } else if (parameterClass == Long.class || parameterClass == long.class) { + return ConfigValueType.NUMBER; + } else if (parameterClass == String.class) { + return ConfigValueType.STRING; + } else if (parameterClass == Duration.class) { + return null; + } else if (parameterClass == ConfigMemorySize.class) { + return null; + } else if (parameterClass == List.class) { + return ConfigValueType.LIST; + } else if (parameterClass == Map.class) { + return ConfigValueType.OBJECT; + } else if (parameterClass == Config.class) { + return ConfigValueType.OBJECT; + } else if (parameterClass == ConfigObject.class) { + return ConfigValueType.OBJECT; + } else if (parameterClass == ConfigList.class) { + return ConfigValueType.LIST; + } else { + return null; + } + } + + private static boolean hasAtLeastOneBeanProperty(Class clazz) { + BeanInfo beanInfo = null; + try { + beanInfo = Introspector.getBeanInfo(clazz); + } catch (IntrospectionException e) { + return false; + } + + for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) { + if (beanProp.getReadMethod() != null && beanProp.getWriteMethod() != null) { + return true; + } + } + + return false; + } + + private static String toCamelCase(String originalName) { + String[] words = originalName.split("-+"); + StringBuilder nameBuilder = new StringBuilder(originalName.length()); + for (String word : words) { + if (nameBuilder.length() == 0) { + nameBuilder.append(word); + } else { + nameBuilder.append(word.substring(0, 1).toUpperCase()); + nameBuilder.append(word.substring(1)); + } + } + return nameBuilder.toString(); + } +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java index 30369c96..afb3ca04 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisGroup.java @@ -27,12 +27,12 @@ * redis 组 */ public class RedisGroup { - public List redisNodeList = Collections.emptyList(); + public List redisNodeList = Collections.emptyList(); public RedisGroup() { } - public RedisGroup(List redisNodeList) { + public RedisGroup(List redisNodeList) { this.redisNodeList = redisNodeList; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisNode.java similarity index 73% rename from mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java rename to mpush-tools/src/main/java/com/mpush/tools/config/data/RedisNode.java index 73f50277..6819dad7 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisServer.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/data/RedisNode.java @@ -22,18 +22,16 @@ /** * redis 相关的配置信息 */ -public class RedisServer { +public class RedisNode { public String host; public int port; - public String password; - public RedisServer() { + public RedisNode() { } - public RedisServer(String host, int port, String password) { + public RedisNode(String host, int port) { this.host = host; this.port = port; - this.password = password; } public String getHost() { @@ -52,20 +50,13 @@ public void setPort(int port) { this.port = port; } - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - public static RedisServer from(String config) { + public static RedisNode from(String config) { String[] array = config.split(":"); if (array.length == 2) { - return new RedisServer(array[0], Integer.parseInt(array[1]), null); + return new RedisNode(array[0], Integer.parseInt(array[1])); } else { - return new RedisServer(array[0], Integer.parseInt(array[1]), array[2]); + return new RedisNode(array[0], Integer.parseInt(array[1])); } } @@ -74,7 +65,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RedisServer server = (RedisServer) o; + RedisNode server = (RedisNode) o; if (port != server.port) return false; return host.equals(server.host); @@ -93,7 +84,6 @@ public String toString() { return "RedisServer{" + "host='" + host + '\'' + ", port=" + port + - ", password='" + password + '\'' + '}'; } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java index 5e452ebe..13116c9f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/Base64Utils.java @@ -33,9 +33,8 @@ public class Base64Utils { * * @param base64 base64 * @return 源二进制数据 - * @throws Exception Exception */ - public static byte[] decode(String base64) throws Exception { + public static byte[] decode(String base64) { return Base64.getDecoder().decode(base64.getBytes(Constants.UTF_8)); } @@ -46,9 +45,8 @@ public static byte[] decode(String base64) throws Exception { * * @param bytes base64 * @return BASE64后的二进制数据 - * @throws Exception Exception */ - public static String encode(byte[] bytes) throws Exception { + public static String encode(byte[] bytes) { return new String(Base64.getEncoder().encode(bytes), Constants.UTF_8); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 4e4f407e..3219353c 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -26,6 +26,8 @@ /** * Created by xiaoxu.yxx on 2015/7/19. + * + * @author ohun@live.cn (夜色) */ public final class NamedThreadFactory implements ThreadFactory { protected final AtomicInteger threadNumber = new AtomicInteger(1); diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index f185d75f..aad23b79 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -183,6 +183,7 @@ public String getFromRemote(final String key) { */ public List getChildrenKeys(final String key) { try { + if (!isExisted(key)) return Collections.emptyList(); List result = client.getChildren().forPath(key); Collections.sort(result, (o1, o2) -> o2.compareTo(o1)); return result; diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java index 7c755830..9518c983 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKNodeCacheWatcher.java @@ -57,7 +57,7 @@ public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exc } } - public final ZKNodeCacheWatcher startWatch() { + public ZKNodeCacheWatcher watch() { beforeWatch(); ZKClient.I.registerListener(this); return this; @@ -65,7 +65,7 @@ public final ZKNodeCacheWatcher startWatch() { @Deprecated public final void beginWatch() { - startWatch(); + watch(); } public abstract String watchPath(); diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java index 16d2ef23..6afb2f6f 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKRedisNode.java @@ -19,7 +19,7 @@ package com.mpush.zk.node; -import com.mpush.tools.config.data.RedisGroup; +import com.mpush.tools.config.data.RedisNode; /** * Redis 节点配置 @@ -27,7 +27,7 @@ * * @author ohun@live.cn */ -public class ZKRedisNode extends RedisGroup implements ZKNode { +public class ZKRedisNode extends RedisNode implements ZKNode { private transient String zkPath; public String getZkPath() { From 1eb70f87c93d082e1805a46ce833c80a20394c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 28 Sep 2016 09:04:51 +0800 Subject: [PATCH 657/890] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/session/ReusableSessionManager.java | 2 +- ...ectClientBoot.java => ConnClientBoot.java} | 23 ++++-- .../mpush/test/client/ConnClientTestMain.java | 16 ++--- .../test/client/ConnClientTestMain2.java | 71 ------------------- .../mpush/test/push/PushClientTestMain.java | 8 +-- 5 files changed, 28 insertions(+), 92 deletions(-) rename mpush-test/src/test/java/com/mpush/test/client/{ConnectClientBoot.java => ConnClientBoot.java} (64%) delete mode 100644 mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java diff --git a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java index 5b9394bc..5c328c19 100644 --- a/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/session/ReusableSessionManager.java @@ -32,7 +32,7 @@ * @author ohun@live.cn */ public final class ReusableSessionManager { - public static final ReusableSessionManager INSTANCE = new ReusableSessionManager(); + public static final ReusableSessionManager I = new ReusableSessionManager(); private int expiredTime = CC.mp.core.session_expired_time; public boolean cacheSession(ReusableSession session) { diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java similarity index 64% rename from mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java rename to mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java index 885d6aa8..a4699f72 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnectClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java @@ -20,6 +20,8 @@ package com.mpush.test.client; import com.google.common.collect.Lists; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; @@ -27,16 +29,23 @@ import java.util.List; -public class ConnectClientBoot { - private final ZKServerNodeWatcher listener = ZKServerNodeWatcher.buildConnect(); +public final class ConnClientBoot extends BaseService { + private final ZKServerNodeWatcher watcher = ZKServerNodeWatcher.buildConnect(); - public void run() { - ZKClient.I.start(); + public List getServers() { + return Lists.newArrayList(watcher.getCache().values()); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + ZKClient.I.start(listener); RedisManager.I.init(); - listener.watch(); + watcher.watch(); } - public List getServers() { - return Lists.newArrayList(listener.getCache().values()); + @Override + protected void doStop(Listener listener) throws Throwable { + ZKClient.I.stop(); + RedisManager.I.destroy(); } } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index 02379dd2..cd2d87aa 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -20,11 +20,16 @@ package com.mpush.test.client; import com.mpush.api.service.Client; +import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.connect.ClientConfig; import com.mpush.client.connect.ConnectClient; import com.mpush.common.security.CipherBox; import com.mpush.tools.log.Logs; +import com.mpush.zk.ZKClient; +import com.mpush.zk.listener.ZKServerNodeWatcher; import com.mpush.zk.node.ZKServerNode; +import org.junit.After; +import org.junit.Before; import java.util.ArrayList; import java.util.List; @@ -34,11 +39,10 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { - Logs.init(); - ConnectClientBoot main = new ConnectClientBoot(); - main.run(); + ConnClientBoot boot = new ConnClientBoot(); + boot.start().get(); - List serverList = main.getServers(); + List serverList = boot.getServers(); int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ZKServerNode server = serverList.get(index); @@ -62,9 +66,5 @@ public static void main(String[] args) throws Exception { Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); client.start().get(10, TimeUnit.SECONDS); } - - LockSupport.park(); - } - } diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java deleted file mode 100644 index 5abd1cc5..00000000 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain2.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.test.client; - -import com.mpush.api.service.Client; -import com.mpush.client.connect.ClientConfig; -import com.mpush.client.connect.ConnectClient; -import com.mpush.common.security.CipherBox; -import com.mpush.tools.log.Logs; -import com.mpush.zk.node.ZKServerNode; - -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.LockSupport; - -public class ConnClientTestMain2 { - - public static void main(String[] args) throws Exception { - Logs.init(); - ConnectClientBoot main = new ConnectClientBoot(); - main.run(); - - List serverList = main.getServers(); - - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - //server = new ZKServerNode("127.0.0.1", 3000, "127.0.0.1", null); - - ClientConfig config = new ClientConfig(); - config.setClientKey(CipherBox.I.randomAESKey()); - config.setIv(CipherBox.I.randomAESIV()); - config.setClientVersion("1.0.0"); - config.setDeviceId("android-device-id-1"); - config.setOsName("android"); - config.setOsVersion("1.0.1"); - config.setUserId("user-0"); - Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); - client.start().get(10, TimeUnit.SECONDS); - - config = new ClientConfig(); - config.setClientKey(CipherBox.I.randomAESKey()); - config.setIv(CipherBox.I.randomAESIV()); - config.setClientVersion("1.0.0"); - config.setDeviceId("pc-device-id-2"); - config.setOsName("pc"); - config.setOsVersion("1.0.1"); - config.setUserId("user-0"); - client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); - client.start().get(10, TimeUnit.SECONDS); - - LockSupport.park(); - } - -} diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 0031538a..4246c178 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -37,7 +37,6 @@ public static void main(String[] args) throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().get(); - Thread.sleep(5000); PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); msg.setMsgId("msgId_0"); @@ -51,10 +50,9 @@ public void onResult(PushResult result) { System.err.println(result); } }); - /*FutureTask future = sender.send(context); - future.get();*/ - sender.stop().get(); - System.out.println("stop"); + FutureTask future = sender.send(context); + future.get(); + //sender.stop().get(); } } \ No newline at end of file From 9ce4375d988dd2fcb3e8303cb9210819cff12409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 28 Sep 2016 09:07:36 +0800 Subject: [PATCH 658/890] =?UTF-8?q?redis=20=E5=A2=9E=E5=8A=A0=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/src/test/resources/application.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 234c28d9..1a237dd8 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -8,5 +8,5 @@ mp.core.compress-threshold=10k mp.http.proxy-enabled=true mp.redis { #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 - cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port + nodes:["127.0.0.1:6379"]//格式是ip:port,密码可以没有ip:port } \ No newline at end of file From 8bd2ea02e65248439af627e38006a9e4ceb5089c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 28 Sep 2016 09:32:40 +0800 Subject: [PATCH 659/890] =?UTF-8?q?redis=20=E5=A2=9E=E5=8A=A0=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/tools/common/JVMUtil.java | 25 ++++++++----------- .../tools/thread/NamedThreadFactory.java | 2 +- .../mpush/tools/thread/PoolThreadFactory.java | 2 +- .../com/mpush/tools/thread/ThreadNames.java | 24 +++++++++--------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java index 2fb583bb..596251a8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -27,6 +27,9 @@ import javax.management.ObjectName; import java.io.*; import java.lang.management.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.AccessController; import java.security.PrivilegedExceptionAction; import java.util.Iterator; @@ -108,22 +111,14 @@ public static void jstack(OutputStream stream) throws Exception { public static void dumpJstack(final String jvmPath) { new Thread((() -> { - String logPath = jvmPath; - FileOutputStream jstackStream = null; - try { - jstackStream = new FileOutputStream(new File(logPath, System.currentTimeMillis() + "-jstack.LOGGER")); - JVMUtil.jstack(jstackStream); + File file = new File(jvmPath, System.currentTimeMillis() + "-jstack.log"); + file.mkdirs(); + try (FileOutputStream out = new FileOutputStream(file)) { + JVMUtil.jstack(out); } catch (Throwable t) { LOGGER.error("Dump JVM cache Error!", t); - } finally { - if (jstackStream != null) { - try { - jstackStream.close(); - } catch (IOException e) { - } - } } - }),"mp-monitor-dump-jstack-thread").start(); + }), "mp-monitor-dump-jstack-t").start(); } private static HotSpotDiagnosticMXBean getHotSpotMXBean() { @@ -160,7 +155,7 @@ private static void initHotSpotMBean() throws Exception { } public static void jMap(String fileName, boolean live) { - File f = new File(fileName, System.currentTimeMillis() + "-jmap.LOGGER"); + File f = new File(fileName, System.currentTimeMillis() + "-jmap.log"); String currentFileName = f.getPath(); try { initHotSpotMBean(); @@ -174,6 +169,6 @@ public static void jMap(String fileName, boolean live) { } public static void dumpJmap(final String jvmPath) { - new Thread(() -> jMap(jvmPath, false), "mp-monitor-dump-jmap-thread").start(); + new Thread(() -> jMap(jvmPath, false), "mp-monitor-dump-jmap-t").start(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 3219353c..24be691f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -50,7 +50,7 @@ public Thread newThread(String name, Runnable r) { @Override public Thread newThread(Runnable r) { - Thread t = newThread(namePrefix + threadNumber.getAndIncrement(), r); + Thread t = newThread(namePrefix + "_" + threadNumber.getAndIncrement(), r); if (t.isDaemon()) t.setDaemon(false); return t; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java index adb945ba..c662e089 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java @@ -43,7 +43,7 @@ public PoolThreadFactory(String prefix, boolean daemon) { group = Thread.currentThread().getThreadGroup(); } isDaemon = daemon; - namePre = prefix + "p-" + poolNum.getAndIncrement() + "-t-"; + namePre = prefix + "-p-" + poolNum.getAndIncrement() + "-t-"; } /** diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index 79cbcdb7..a28df05e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -21,34 +21,34 @@ public final class ThreadNames { public static final String NS = "mp"; - public static final String THREAD_NAME_PREFIX = NS + "-t-"; + public static final String THREAD_NAME_PREFIX = NS + "-t"; /** * netty boss 线程 */ - public static final String T_SERVER_BOSS = NS + "-boss-"; + public static final String T_SERVER_BOSS = NS + "-boss"; /** * netty worker 线程 */ - public static final String T_SERVER_WORKER = NS + "-worker-"; + public static final String T_SERVER_WORKER = NS + "-worker"; - public static final String T_HTTP_CLIENT = NS + "-http-"; + public static final String T_HTTP_CLIENT = NS + "-http"; - public static final String T_EVENT_BUS = NS + "-event-"; + public static final String T_EVENT_BUS = NS + "-event"; - public static final String T_MQ = NS + "-mq-"; + public static final String T_MQ = NS + "-mq"; - public static final String T_ZK = NS + "-zk-"; + public static final String T_ZK = NS + "-zk"; - public static final String T_BIZ = NS + "-biz-"; - public static final String T_PUSH_CALLBACK = NS + "-push-cb-"; - public static final String T_PUSH_REQ_TIMER = NS + "-push-timer-"; - public static final String T_ARK_REQ_TIMER = NS + "-ack-timer-"; + public static final String T_BIZ = NS + "-biz"; + public static final String T_PUSH_CALLBACK = NS + "-push-cb"; + public static final String T_PUSH_REQ_TIMER = NS + "-push-timer"; + public static final String T_ARK_REQ_TIMER = NS + "-ack-timer"; /** * connection 定期检测线程 */ - public static final String T_NETTY_TIMER = NS + "-timer-"; + public static final String T_NETTY_TIMER = NS + "-timer"; } From f5de8c1d98969e584bd2f7ff61cc4b06c630a1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 29 Sep 2016 16:12:55 +0800 Subject: [PATCH 660/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9windows=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=91=BD=E4=BB=A4bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/env-mp.cmd | 4 ++-- bin/mp.cmd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/env-mp.cmd b/bin/env-mp.cmd index bca3e566..46f05757 100644 --- a/bin/env-mp.cmd +++ b/bin/env-mp.cmd @@ -15,7 +15,7 @@ REM See the License for the specific language governing permissions and REM limitations under the License. set MPCFGDIR=%~dp0%..\conf -set MP_LOG_DIR=%~dp0%.. +set MP_LOG_DIR=%~dp0%..\logs set MP_LOG4J_PROP=INFO,CONSOLE REM for sanity sake assume Java 1.6 @@ -30,7 +30,7 @@ SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% REM make it work for developers SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% -set MPCFG=%MPCFGDIR%\mpush.cfg +set MPCFG=%MPCFGDIR%\mpush.conf @REM setup java environment variables diff --git a/bin/mp.cmd b/bin/mp.cmd index 56b0e445..ffcabd63 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -17,7 +17,7 @@ REM limitations under the License. setlocal call "%~dp0env-mp.cmd" -set MPMAIN="-jar ../boot.jar" +set MPMAIN="-jar %~dp0bootstrap.jar" echo on call %JAVA% "-Dmp.conf=%MPCFG%" "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% %* From ef8533c4ce067121ef50e5397213d8933bb9ca85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:10:34 +0800 Subject: [PATCH 661/890] =?UTF-8?q?redis=20=E5=A2=9E=E5=8A=A0=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/mpush.conf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 25395e11..27af6af8 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -6,7 +6,8 @@ mp.zk.server-address="127.0.0.1:2181" mp.zk.namespace=mpush mp.redis={ #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 - cluster-group:[["127.0.0.1:6379"]]//格式是ip:port:password,密码可以没有ip:port + nodes:["127.0.0.1:6379"]//格式是ip:port + cluster-model=single//single,cluster } mp.http.proxy-enabled=true //启用Http代理功能 mp.net.connect-server-port=3000 From 0b83a63bc7468355c599ffbdcd8e020b706c13c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:18:40 +0800 Subject: [PATCH 662/890] =?UTF-8?q?=E8=A7=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/netty/codec/PacketDecoder.java | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 5b608782..153ab950 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -54,47 +54,42 @@ private void decodeHeartbeat(ByteBuf in, List out) { } private void decodeFrames(ByteBuf in, List out) throws Exception { - try { - while (in.readableBytes() >= Packet.HEADER_LEN) { - //1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 - in.markReaderIndex(); - out.add(decodeFrame(in)); + while (in.readableBytes() >= Packet.HEADER_LEN) { + //1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 + in.markReaderIndex(); + Packet packet = decodeFrame(in); + if (packet != null) { + out.add(packet); + } else { + //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 + in.resetReaderIndex(); } - } catch (DecodeException e) { - //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 - in.resetReaderIndex(); } } private Packet decodeFrame(ByteBuf in) throws Exception { - int bufferSize = in.readableBytes(); + int readableBytes = in.readableBytes(); int bodyLength = in.readInt(); - if (bufferSize < (bodyLength + Packet.HEADER_LEN)) { - throw new DecodeException("invalid frame"); + if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { + return null; } return readPacket(in, bodyLength); } private Packet readPacket(ByteBuf in, int bodyLength) { - byte command = in.readByte(); - short cc = in.readShort(); - byte flags = in.readByte(); - int sessionId = in.readInt(); - byte lrc = in.readByte(); - byte[] body = null; + Packet packet = new Packet(in.readByte());//read cmd + packet.cc = in.readShort();//read cc + packet.flags = in.readByte();//read flags + packet.sessionId = in.readInt();//read sessionId + packet.lrc = in.readByte();//read lrc + + //read body if (bodyLength > 0) { if (bodyLength > maxPacketSize) { - throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); + throw new RuntimeException("error packet body length over limit:" + bodyLength); } - body = new byte[bodyLength]; - in.readBytes(body); + in.readBytes(packet.body = new byte[bodyLength]); } - Packet packet = new Packet(command); - packet.cc = cc; - packet.flags = flags; - packet.sessionId = sessionId; - packet.lrc = lrc; - packet.body = body; return packet; } } From 4cf76d772d6c9bfa5a57d08c839cf5e835d0f76e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:28:51 +0800 Subject: [PATCH 663/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0IoRate=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/netty/server/NettyServer.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 9d8cc644..793cf504 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -49,7 +49,7 @@ */ public abstract class NettyServer extends BaseService implements Server { - private static final Logger logger = LoggerFactory.getLogger(NettyServer.class); + private static final Logger logger = LoggerFactory.getLogger(NettyServer.class); public enum State {Created, Initialized, Starting, Started, Shutdown} @@ -103,7 +103,7 @@ public void start(final Listener listener) { } } - private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { + private void createServer(Listener listener, EventLoopGroup boss, EventLoopGroup work, Class clazz) { /*** * NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器, * Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。 @@ -189,6 +189,7 @@ public void initChannel(SocketChannel ch) throws Exception { private void createNioServer(Listener listener) { NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, getBossExecutor()); NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, getWorkExecutor()); + workerGroup.setIoRatio(getIoRate()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } @@ -196,6 +197,7 @@ private void createNioServer(Listener listener) { private void createEpollServer(Listener listener) { EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, getBossExecutor()); EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, getWorkExecutor()); + workerGroup.setIoRatio(getIoRate()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -244,10 +246,8 @@ protected Executor getWorkExecutor() { return null; } - private boolean useNettyEpoll() { - if (!"netty".equals(CC.mp.core.epoll_provider)) return false; - String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); - return name.startsWith("linux"); + protected int getIoRate() { + return 70; } @Override @@ -259,4 +259,10 @@ protected void doStart(Listener listener) throws Throwable { protected void doStop(Listener listener) throws Throwable { } + + private boolean useNettyEpoll() { + if (!"netty".equals(CC.mp.core.epoll_provider)) return false; + String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); + return name.startsWith("linux"); + } } From 3ca53fae2292dbe153b6fb966db64aaf71227462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:29:50 +0800 Subject: [PATCH 664/890] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E8=BF=9E=E6=8E=A5=E5=BB=BA=E7=AB=8B=E5=90=8E=E5=B0=B1?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E8=AE=A1=E7=AE=97=E5=BF=83=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/connection/ConnectionManager.java | 2 ++ .../mpush/core/server/ConnectionServer.java | 2 +- .../com/mpush/core/server/GatewayServer.java | 2 +- .../core/server/ServerConnectionManager.java | 29 ++++++++++++------- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index 58f12e76..bc859147 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -25,6 +25,8 @@ /** * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn (夜色) */ public interface ConnectionManager { diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index be0696dc..e697c55d 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -50,7 +50,7 @@ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; private GlobalChannelTrafficShapingHandler trafficShapingHandler; - private ConnectionManager connectionManager = new ServerConnectionManager(); + private ConnectionManager connectionManager = new ServerConnectionManager(true); private HttpClient httpClient; public ConnectionServer(int port) { diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index fb7976ee..63a8a4d7 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -52,7 +52,7 @@ public void init() { super.init(); MessageDispatcher receiver = new MessageDispatcher(); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); - connectionManager = new ServerConnectionManager(); + connectionManager = new ServerConnectionManager(false); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); if (enabled) { trafficShapingHandler = new GlobalChannelTrafficShapingHandler( diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 0664a103..248c40f5 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -48,15 +48,25 @@ public final class ServerConnectionManager implements ConnectionManager { private final ConcurrentMap connections = new ConcurrentHashMap<>(); - private Timer timer; + private HashedWheelTimer timer; + private final boolean heartbeatCheck; + + public ServerConnectionManager() { + this.heartbeatCheck = true; + } + + public ServerConnectionManager(boolean heartbeatCheck) { + this.heartbeatCheck = heartbeatCheck; + } @Override public void init() { - //每秒钟走一步,一个心跳周期内走一圈 - long tickDuration = 1000;//1s - int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); - this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); - EventBus.I.register(this); + if (heartbeatCheck) { + EventBus.I.register(this); + long tickDuration = TimeUnit.SECONDS.toMillis(1);//1s 每秒钟走一步,一个心跳周期内大致走一圈 + int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); + this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); + } } @Override @@ -74,6 +84,7 @@ public Connection get(final Channel channel) { @Override public void add(Connection connection) { connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); + if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); } @Override @@ -90,10 +101,9 @@ public List getConnections() { return Lists.newArrayList(connections.values()); } - @Subscribe + //@Subscribe void on(HandshakeEvent event) { - HeartbeatCheckTask task = new HeartbeatCheckTask(event.connection); - task.startTimeout(); + new HeartbeatCheckTask(event.connection).startTimeout(); } private class HeartbeatCheckTask implements TimerTask { @@ -126,7 +136,6 @@ public void run(Timeout timeout) throws Exception { } } else { timeoutTimes = 0; - //Logs.HB.info("client heartbeat timeout times reset 0, connection={}", connection); } startTimeout(); } From cfb9e4c01ca8a2ba2d7cbd39dbcc31cee3ad2a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:30:59 +0800 Subject: [PATCH 665/890] =?UTF-8?q?redis=20=E5=A2=9E=E5=8A=A0=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- .../redis/connection/RedisConnectionFactory.java | 10 +++++++--- .../com/mpush/cache/redis/manager/RedisManager.java | 12 +++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 6d000564..1c050148 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -126,7 +126,7 @@ mp { max-conn-per-host=5 //每个域名的最大链接数, 建议web服务nginx超时时间设长一点, 以便保持长链接 default-read-timeout=10s //请求超时时间 max-content-length=5m //response body 最大大小 - dns-mapping { //域名映射外网地址转内部IP + dns-mapping { //域名映射外网地址转内部IP, 域名部分不包含端口号 //"mpush.com":["127.0.0.1:8080", "127.0.0.1:8081"] } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java b/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java index f52ed70d..ab57ff9f 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/connection/RedisConnectionFactory.java @@ -24,6 +24,7 @@ import redis.clients.util.Pool; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -94,10 +95,10 @@ public void init() { } } - if (redisServers.size() == 1) { - this.pool = createPool(); - } else { + if (isCluster) { this.cluster = createCluster(); + } else { + this.pool = createPool(); } } @@ -318,7 +319,10 @@ public void setCluster(boolean cluster) { } public void setRedisServers(List redisServers) { + Objects.requireNonNull(redisServers); this.redisServers = redisServers; + this.hostName = redisServers.get(0).getHost(); + this.port = redisServers.get(0).getPort(); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 85373178..266ca9fc 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -396,11 +396,13 @@ public void destroy() { } public void test() { - JedisCluster cluster = factory.getClusterConnection(); - Jedis jedis = factory.getJedisConnection(); - if (cluster == null && jedis == null) { - throw new RuntimeException("init redis sever error."); + if (factory.isCluster()) { + JedisCluster cluster = factory.getClusterConnection(); + if (cluster == null) throw new RuntimeException("init redis cluster error."); + } else { + Jedis jedis = factory.getJedisConnection(); + if (jedis == null) throw new RuntimeException("init redis error, can not get connection."); + jedis.close(); } - if (jedis != null) jedis.close(); } } From 5e72fc3ce904d355ed34a3e5d62b7994eeaf419b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:32:16 +0800 Subject: [PATCH 666/890] =?UTF-8?q?sessionId=20=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BALongAdder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/common/message/BaseMessage.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 8c3bd221..fb49f847 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -28,6 +28,7 @@ import io.netty.channel.ChannelFutureListener; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.LongAdder; /** * Created by ohun on 2015/12/28. @@ -35,7 +36,7 @@ * @author ohun@live.cn */ public abstract class BaseMessage implements Message { - private static final AtomicInteger ID_SEQ = new AtomicInteger(); + private static final LongAdder ID_SEQ = new LongAdder(); protected final Packet packet; protected final Connection connection; @@ -136,7 +137,8 @@ public void close() { } protected static int genSessionId() { - return ID_SEQ.incrementAndGet(); + ID_SEQ.increment(); + return ID_SEQ.intValue(); } public int getSessionId() { From 54768daa23e4cf677933d3eba423a12157c3f4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 10:33:00 +0800 Subject: [PATCH 667/890] =?UTF-8?q?=E4=BD=BF=E7=94=A8java8=20CompletableFu?= =?UTF-8?q?ture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/connection/Connection.java | 2 ++ .../com/mpush/api/service/BaseService.java | 16 ++++------ .../java/com/mpush/api/service/Service.java | 6 ++-- .../mpush/test/push/PushClientTestMain.java | 32 +++++++++---------- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index 7eee23f2..cc9ff7dc 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -26,6 +26,8 @@ /** * Created by ohun on 2015/12/22. + * + * @author ohun@live.cn (夜色) */ public interface Connection { int STATUS_NEW = 0; diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java index 5e045a2a..0062f4ab 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -19,8 +19,7 @@ package com.mpush.api.service; -import java.util.concurrent.Future; -import java.util.concurrent.FutureTask; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -72,13 +71,13 @@ protected void tryStop(Listener listener, Function function) { } } - public final Future start() { + public final CompletableFuture start() { FutureListener listener = new FutureListener(); start(listener); return listener; } - public final Future stop() { + public final CompletableFuture stop() { FutureListener listener = new FutureListener(); stop(listener); return listener; @@ -114,31 +113,28 @@ public FutureListener wrap(Listener l) { return new FutureListener(l); } - protected class FutureListener extends FutureTask implements Listener { + protected class FutureListener extends CompletableFuture implements Listener { private final Listener l;// 防止Listener被重复执行 public FutureListener() { - super(BaseService.this::isRunning); this.l = null; } public FutureListener(Listener l) { - super(BaseService.this::isRunning); this.l = l; } @Override public void onSuccess(Object... args) { if (isDone()) return;// 防止Listener被重复执行 - set(started.get()); + complete(started.get()); if (l != null) l.onSuccess(args); } @Override public void onFailure(Throwable cause) { if (isDone()) return;// 防止Listener被重复执行 - set(started.get()); - setException(cause); + completeExceptionally(cause); if (l != null) l.onFailure(cause); throw new ServiceException(cause); } diff --git a/mpush-api/src/main/java/com/mpush/api/service/Service.java b/mpush-api/src/main/java/com/mpush/api/service/Service.java index c158751b..c192bf3c 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/Service.java +++ b/mpush-api/src/main/java/com/mpush/api/service/Service.java @@ -19,7 +19,7 @@ package com.mpush.api.service; -import java.util.concurrent.Future; +import java.util.concurrent.CompletableFuture; /** * Created by yxx on 2016/5/17. @@ -32,9 +32,9 @@ public interface Service { void stop(Listener listener); - Future start(); + CompletableFuture start(); - Future stop(); + CompletableFuture stop(); void init(); diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 4246c178..ef35d54a 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -36,23 +36,23 @@ public class PushClientTestMain { public static void main(String[] args) throws Exception { Logs.init(); PushSender sender = PushSender.create(); - sender.start().get(); - PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); - msg.setMsgId("msgId_0"); + sender.start().whenComplete((success, throwable) -> { - PushContext context = PushContext.build(msg) - .setBroadcast(false) - .setUserIds(Arrays.asList("user-0", "user-1")) - .setTimeout(100000) - .setCallback(new PushCallback() { - @Override - public void onResult(PushResult result) { - System.err.println(result); - } - }); - FutureTask future = sender.send(context); - future.get(); - //sender.stop().get(); + PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); + msg.setMsgId("msgId_0"); + + PushContext context = PushContext.build(msg) + .setBroadcast(false) + .setUserIds(Arrays.asList("user-0", "user-1")) + .setTimeout(100000) + .setCallback(new PushCallback() { + @Override + public void onResult(PushResult result) { + System.err.println(result); + } + }); + FutureTask future = sender.send(context); + }); } } \ No newline at end of file From 163331327edfdafd9b733245efff21f78bc94806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 16:28:31 +0800 Subject: [PATCH 668/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E9=87=8D=E6=96=B0=E6=B3=A8=E5=86=8C=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/zk/ZKClient.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index aad23b79..8fa2ae41 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -129,7 +129,7 @@ public List getAclForPath(final String path) { private void addConnectionStateListener() { client.getConnectionStateListenable().addListener((cli, newState) -> { if (newState == ConnectionState.RECONNECTED) { - ephemeralNodes.forEach(this::registerEphemeralSequential); + ephemeralNodes.forEach(this::reRegisterEphemeralSequential); } Logs.ZK.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); }); @@ -269,17 +269,28 @@ public void registerEphemeral(final String key, final String value) { * 注册临时顺序数据 * * @param key + * @param value + * @param cacheNode */ - public void registerEphemeralSequential(final String key, final String value) { + private void registerEphemeralSequential(final String key, final String value, boolean cacheNode) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); - ephemeralNodes.put(key, value); + if (cacheNode) ephemeralNodes.put(key, value); } catch (Exception ex) { Logs.ZK.error("persistEphemeralSequential:{},{}", key, value, ex); throw new ZKException(ex); } } + private void reRegisterEphemeralSequential(final String key, final String value) { + registerEphemeralSequential(key, value, false); + } + + public void registerEphemeralSequential(final String key, final String value) { + registerEphemeralSequential(key, value, true); + } + + /** * 注册临时顺序数据 * From 41a86672ff7da2dccd182d899398a15aa33a664b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 8 Oct 2016 16:30:11 +0800 Subject: [PATCH 669/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E9=87=8D=E6=96=B0=E6=B3=A8=E5=86=8C=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 8fa2ae41..bcec88ce 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -270,7 +270,7 @@ public void registerEphemeral(final String key, final String value) { * * @param key * @param value - * @param cacheNode + * @param cacheNode 第一次注册时设置为true, 连接断开重新注册时设置为false */ private void registerEphemeralSequential(final String key, final String value, boolean cacheNode) { try { From a48d7fc19147fed94e7a9c26af926f9c6a4113aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 9 Oct 2016 17:47:28 +0800 Subject: [PATCH 670/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/bootstrap/ServerLauncher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 4be649a0..d548725f 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -50,7 +50,7 @@ public ServerLauncher() { .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 - .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dnsgit + .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns .setNext(new MonitorBoot())//7.启动监控 .setNext(new LastBoot());//8.启动结束 } From c595a5446fd00b7b2894cfb4025a6ad4b9ef6e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 11 Oct 2016 18:00:33 +0800 Subject: [PATCH 671/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=89=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/connection/SessionContext.java | 1 + .../mpush/api/event/ConnectionCloseEvent.java | 2 + .../net/HttpProxyDnsMappingManager.java | 17 ++--- .../mpush/core/handler/BindUserHandler.java | 2 + .../core/handler/GatewayPushHandler.java | 69 ++++++++++--------- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index 46fb0647..6b8cb2f0 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -32,6 +32,7 @@ public final class SessionContext { public String clientVersion; public String deviceId; public String userId; + public String tags; public int heartbeat; public Cipher cipher; private int clientType; diff --git a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java index a31dea4b..4d2779a1 100644 --- a/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java +++ b/mpush-api/src/main/java/com/mpush/api/event/ConnectionCloseEvent.java @@ -23,6 +23,8 @@ /** * Created by ohun on 2016/1/10. + * + * @author ohun@live.cn (夜色) */ public final class ConnectionCloseEvent implements Event { public final Connection connection; diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index 72a323a6..e4cb77c5 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -109,20 +109,17 @@ public void run() { logger.debug("start dns mapping checkHealth"); Map> all = this.getAll(); Map> available = Maps.newConcurrentMap(); - for (Map.Entry> entry : all.entrySet()) { - String key = entry.getKey(); - List value = entry.getValue(); + all.forEach((key, dnsMappings) -> { List nowValue = Lists.newArrayList(); - for (DnsMapping temp : value) { - boolean isOk = checkHealth(temp.getIp(), temp.getPort()); - if (isOk) { - nowValue.add(temp); + dnsMappings.forEach(dnsMapping -> { + if (checkHealth(dnsMapping.getIp(), dnsMapping.getPort())) { + nowValue.add(dnsMapping); } else { - logger.error("dns can not reachable:" + Jsons.toJson(temp)); + logger.error("dns can not reachable:" + Jsons.toJson(dnsMapping)); } - } + }); available.put(key, nowValue); - } + }); this.update(available); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 6da39fb0..418c01ee 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -82,6 +82,7 @@ private void bind(BindUserMessage message) { boolean success = RouterCenter.I.register(message.userId, message.getConnection()); if (success) { context.userId = message.userId; + context.tags = message.tags; EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").send(); Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); @@ -138,6 +139,7 @@ private void unbind(BindUserMessage message) { //4.路由删除成功,广播用户下线事件 if (unRegisterSuccess) { context.userId = null; + context.tags = null; EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").send(); Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 6d825ace..4088f4b6 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -100,57 +100,63 @@ public void handle(GatewayPushMessage message) { */ private void sendBroadcast(GatewayPushMessage message) { Set sendUserIds = new CopyOnWriteArraySet<>(); + Set tagList = message.tags; LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); AtomicInteger tasks = new AtomicInteger();//总任务数, 0表示任务全部结束 long begin = System.currentTimeMillis(); for (int start = 0, limit = 1000; ; start += limit) { List userIds = UserManager.I.getOnlineUserList(start, limit); tasks.addAndGet(userIds.size());//增加任务数 + userIds.forEach(userId -> { + for (LocalRouter router : routerManager.lookupAll(userId)) { + Connection connection = router.getRouteValue(); + int clientType = router.getClientType(); + + //2.按标签过滤, + String tags = connection.getSessionContext().tags; + if (tagList != null && tags != null) { + if (tagList.stream().noneMatch(tags::contains)) break; + } - userIds.forEach(userId -> routerManager.lookupAll(userId).forEach(router -> { - Connection connection = router.getRouteValue(); - int clientType = router.getClientType(); + if (connection.isConnected()) { + //3.链接可用,直接下发消息到手机客户端 + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.getPacket().flags = message.getPacket().flags; - if (connection.isConnected()) { - //TODO check tags sessionContext ? + pushMessage.send(future -> { - //3.链接可用,直接下发消息到手机客户端 - PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.getPacket().flags = message.getPacket().flags; + if (!sendUserIds.contains(userId)) { + tasks.decrementAndGet();//完成一个任务 + } - pushMessage.send(future -> { + if (future.isSuccess()) {//推送成功 + sendUserIds.add(userId); + Logs.PUSH.info("gateway broadcast client success, userId={}, message={}", userId, message); - if (!sendUserIds.contains(userId)) { - tasks.decrementAndGet();//完成一个任务 - } + } else {//推送失败 + Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); + } - if (future.isSuccess()) {//推送成功 - sendUserIds.add(userId); - Logs.PUSH.info("gateway broadcast client success, userId={}, message={}", userId, message); + if (tasks.get() == 0) {//任务全部结束 + Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); + } + }); + } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 + Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); - } else {//推送失败 - Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); - } + tasks.decrementAndGet();//完成一个任务 + + //删除已经失效的本地路由 + RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); if (tasks.get() == 0) {//任务全部结束 Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); } - }); - } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 - Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); - - tasks.decrementAndGet();//完成一个任务 - - //删除已经失效的本地路由 - RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); - - if (tasks.get() == 0) {//任务全部结束 - Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); - OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); } } - })); + }); if (userIds.size() != limit) break;//查询完毕 } @@ -163,6 +169,7 @@ private void sendBroadcast(GatewayPushMessage message) { * @param message message * @return true/false true:success */ + private boolean checkLocal(final GatewayPushMessage message) { String userId = message.userId; int clientType = message.clientType; From 5ef5034108104d36bbe4f431f7d4e6be858fabc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 11 Oct 2016 18:02:58 +0800 Subject: [PATCH 672/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=89=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/src/main/java/com/mpush/api/push/PushContext.java | 2 +- .../main/java/com/mpush/core/handler/GatewayPushHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java index d57d7b29..13d95c28 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java @@ -51,7 +51,7 @@ public class PushContext { private List userIds; /** - * 用户标签过滤 + * 用户标签过滤,目前只有include, 后续会增加exclude */ private Set tags; diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 4088f4b6..f1ba23c8 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -112,7 +112,7 @@ private void sendBroadcast(GatewayPushMessage message) { Connection connection = router.getRouteValue(); int clientType = router.getClientType(); - //2.按标签过滤, + //2.按标签过滤,目前只有include,后续会增加exclude String tags = connection.getSessionContext().tags; if (tagList != null && tags != null) { if (tagList.stream().noneMatch(tags::contains)) break; From fddd9ac6a48334eb3d5dfa43697823e4ab74581e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 12 Oct 2016 10:55:00 +0800 Subject: [PATCH 673/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ack=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/api/push/PushContext.java | 6 +++--- .../src/main/java/com/mpush/client/push/PushRequest.java | 5 +++-- .../mpush/common/message/gateway/GatewayPushMessage.java | 9 +++++++++ .../main/java/com/mpush/core/ack/AckMessageQueue.java | 6 ++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java index 13d95c28..f7039341 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java @@ -73,7 +73,7 @@ public class PushContext { /** * 推送超时时间 */ - private long timeout = 3000; + private int timeout = 3000; public PushContext(byte[] context) { this.context = context; @@ -144,11 +144,11 @@ public PushContext setBroadcast(boolean broadcast) { return this; } - public long getTimeout() { + public int getTimeout() { return timeout; } - public PushContext setTimeout(long timeout) { + public PushContext setTimeout(int timeout) { this.timeout = timeout; return this; } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 8835d3ae..dfbc2b61 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -57,7 +57,7 @@ private enum Status {init, success, failure, offline, timeout} private PushCallback callback; private String userId; private byte[] content; - private long timeout; + private int timeout; private ClientLocation location; private Future future; private String result; @@ -91,6 +91,7 @@ private void sendToConnServer(RemoteRouter remoteRouter) { GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, gatewayConn) .setClientType(location.getClientType()) + .setTimeout(timeout - 500) .addFlag(ackModel.flag); pushMessage.sendRaw(f -> { @@ -228,7 +229,7 @@ public PushRequest setContent(byte[] content) { return this; } - public PushRequest setTimeout(long timeout) { + public PushRequest setTimeout(int timeout) { this.timeout = timeout; return this; } diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 7b3d79a8..483ebf03 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -40,6 +40,7 @@ public class GatewayPushMessage extends ByteBufMessage { public String userId; public Set tags; public int clientType; + public int timeout; public byte[] content; public GatewayPushMessage(String userId, byte[] content, Connection connection) { @@ -57,6 +58,7 @@ public void decode(ByteBuf body) { userId = decodeString(body); tags = decodeSet(body); clientType = decodeInt(body); + timeout = decodeInt(body); content = decodeBytes(body); } @@ -65,6 +67,7 @@ public void encode(ByteBuf body) { encodeString(body, userId); encodeSet(body, tags); encodeInt(body, clientType); + encodeInt(body, timeout); encodeBytes(body, content); } @@ -90,6 +93,11 @@ public GatewayPushMessage addFlag(byte flag) { return this; } + public GatewayPushMessage setTimeout(int timeout) { + this.timeout = timeout; + return this; + } + public boolean isBroadcast() { return userId == null; } @@ -113,6 +121,7 @@ public String toString() { return "GatewayPushMessage{" + "userId='" + userId + '\'' + ", clientType='" + clientType + '\'' + + ", timeout='" + timeout + '\'' + ", content='" + content.length + '\'' + '}'; } diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java index 0ca7e4b1..a3282539 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java @@ -42,7 +42,9 @@ */ public final class AckMessageQueue { private final Logger logger = LoggerFactory.getLogger(AckMessageQueue.class); + private static final int DEFAULT_TIMEOUT = 3000; public static final AckMessageQueue I = new AckMessageQueue(); + private final ConcurrentMap queue = new ConcurrentHashMap<>(); private final ScheduledExecutorService scheduledExecutor; @@ -52,10 +54,10 @@ private AckMessageQueue() { }); } - public void put(int sessionId, AckContext context) { + public void put(int sessionId, AckContext context, int timeout) { queue.put(sessionId, context); context.pushMessageId = sessionId; - scheduledExecutor.schedule(context, 3, TimeUnit.SECONDS); + scheduledExecutor.schedule(context, timeout > 0 ? timeout : DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); } public AckContext getAndRemove(int sessionId) { From 1fa59932de1a7c84728465114ecc2840e092ccda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 12 Oct 2016 10:55:37 +0800 Subject: [PATCH 674/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ack=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/com/mpush/test/push/PushClientTestMain.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index ef35d54a..5b55c4aa 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -43,8 +43,9 @@ public static void main(String[] args) throws Exception { PushContext context = PushContext.build(msg) .setBroadcast(false) + .setAckModel(AckModel.AUTO_ACK) .setUserIds(Arrays.asList("user-0", "user-1")) - .setTimeout(100000) + .setTimeout(2000) .setCallback(new PushCallback() { @Override public void onResult(PushResult result) { From 65016b2d4318361ca648eb2179c1b3175fc3350c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 12 Oct 2016 10:57:01 +0800 Subject: [PATCH 675/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=81=E9=87=8F?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=EF=BC=8Ctcp=20=E5=8F=91=E9=80=81=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA=E7=9B=91=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/api/Message.java | 10 +++ .../core/handler/GatewayPushHandler.java | 62 ++++++++++++------- .../mpush/core/server/ConnectionServer.java | 30 +++++++-- .../com/mpush/core/server/GatewayServer.java | 36 ++++++++++- .../core/server/ServerConnectionManager.java | 2 +- .../netty/connection/NettyConnection.java | 15 +++-- 6 files changed, 119 insertions(+), 36 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java index 581c48fb..84939320 100644 --- a/mpush-api/src/main/java/com/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -32,8 +32,18 @@ public interface Message { Connection getConnection(); + /** + * 发送当前message, 并根据情况最body进行数据压缩、加密 + * + * @param listener 发送成功后的回调 + */ void send(ChannelFutureListener listener); + /** + * 发送当前message, 不会对body进行数据压缩、加密, 原样发送 + * + * @param listener 发送成功后的回调 + */ void sendRaw(ChannelFutureListener listener); Packet getPacket(); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index f1ba23c8..3ae74c1d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -119,29 +119,35 @@ private void sendBroadcast(GatewayPushMessage message) { } if (connection.isConnected()) { - //3.链接可用,直接下发消息到手机客户端 - PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.getPacket().flags = message.getPacket().flags; - pushMessage.send(future -> { - - if (!sendUserIds.contains(userId)) { - tasks.decrementAndGet();//完成一个任务 - } - - if (future.isSuccess()) {//推送成功 - sendUserIds.add(userId); - Logs.PUSH.info("gateway broadcast client success, userId={}, message={}", userId, message); - - } else {//推送失败 - Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); - } - - if (tasks.get() == 0) {//任务全部结束 - Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); - OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); - } - }); + if (connection.getChannel().isWritable()) {//检测TCP缓冲区是否已满且写队列超过最高阀值 + //3.链接可用,直接下发消息到手机客户端 + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.getPacket().flags = message.getPacket().flags; + + pushMessage.send(future -> { + + if (!sendUserIds.contains(userId)) { + tasks.decrementAndGet();//完成一个任务 + } + + if (future.isSuccess()) {//推送成功 + sendUserIds.add(userId); + Logs.PUSH.info("gateway broadcast client success, userId={}, message={}", userId, message); + + } else {//推送失败 + Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); + } + + if (tasks.get() == 0) {//任务全部结束 + Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); + } + }); + } else { + tasks.decrementAndGet();//完成一个任务 + Logs.PUSH.info("gateway broadcast client failure, send too busy, userId={}, message={}", userId, message); + } } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); @@ -191,7 +197,15 @@ private boolean checkLocal(final GatewayPushMessage message) { return false; } - //3.链接可用,直接下发消息到手机客户端 + //3.检测TCP缓冲区是否已满且写队列超过最高阀值 + if (!connection.getChannel().isWritable()) { + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); + + Logs.PUSH.info("gateway push message to client failure, send too busy, message={}", message); + return true; + } + + //4.链接可用,直接下发消息到手机客户端 PushMessage pushMessage = new PushMessage(message.content, connection); pushMessage.getPacket().flags = message.getPacket().flags; @@ -199,7 +213,7 @@ private boolean checkLocal(final GatewayPushMessage message) { if (future.isSuccess()) {//推送成功 if (message.needAck()) {//需要客户端ACK, 消息进队列等待客户端响应ACK - AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message)); + AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message), message.timeout); } else { OkMessage.from(message).setData(userId + ',' + clientType).sendRaw(); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index e697c55d..d199af99 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -34,6 +34,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; +import io.netty.channel.WriteBufferWaterMark; import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; import java.util.concurrent.Executor; @@ -75,10 +76,10 @@ public void init() { } channelHandler = new ServerChannelHandler(true, connectionManager, receiver); - if (enabled) { + if (CC.mp.net.traffic_shaping.connect_server.enabled) {//启用流量整形,限流 trafficShapingHandler = new GlobalChannelTrafficShapingHandler( - Executors.newSingleThreadScheduledExecutor() - , write_global_limit, read_global_limit, + Executors.newSingleThreadScheduledExecutor(), + write_global_limit, read_global_limit, write_channel_limit, read_channel_limit, check_interval); } @@ -117,7 +118,8 @@ protected void initPipeline(ChannelPipeline pipeline) { @Override protected void initOptions(ServerBootstrap b) { super.initOptions(b); - /*** + + /** * 你可以设置这里指定的通道实现的配置参数。 * 我们正在写一个TCP/IP的服务端, * 因此我们被允许设置socket的参数选项比如tcpNoDelay和keepAlive。 @@ -132,6 +134,26 @@ protected void initOptions(ServerBootstrap b) { */ b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); + + /** + * 这个坑其实也不算坑,只是因为懒,该做的事情没做。一般来讲我们的业务如果比较小的时候我们用同步处理,等业务到一定规模的时候,一个优化手段就是异步化。 + * 异步化是提高吞吐量的一个很好的手段。但是,与异步相比,同步有天然的负反馈机制,也就是如果后端慢了,前面也会跟着慢起来,可以自动的调节。 + * 但是异步就不同了,异步就像决堤的大坝一样,洪水是畅通无阻。如果这个时候没有进行有效的限流措施就很容易把后端冲垮。 + * 如果一下子把后端冲垮倒也不是最坏的情况,就怕把后端冲的要死不活。 + * 这个时候,后端就会变得特别缓慢,如果这个时候前面的应用使用了一些无界的资源等,就有可能把自己弄死。 + * 那么现在要介绍的这个坑就是关于Netty里的ChannelOutboundBuffer这个东西的。 + * 这个buffer是用在netty向channel write数据的时候,有个buffer缓冲,这样可以提高网络的吞吐量(每个channel有一个这样的buffer)。 + * 初始大小是32(32个元素,不是指字节),但是如果超过32就会翻倍,一直增长。 + * 大部分时候是没有什么问题的,但是在碰到对端非常慢(对端慢指的是对端处理TCP包的速度变慢,比如对端负载特别高的时候就有可能是这个情况)的时候就有问题了, + * 这个时候如果还是不断地写数据,这个buffer就会不断地增长,最后就有可能出问题了(我们的情况是开始吃swap,最后进程被linux killer干掉了)。 + * 为什么说这个地方是坑呢,因为大部分时候我们往一个channel写数据会判断channel是否active,但是往往忽略了这种慢的情况。 + * + * 那这个问题怎么解决呢?其实ChannelOutboundBuffer虽然无界,但是可以给它配置一个高水位线和低水位线, + * 当buffer的大小超过高水位线的时候对应channel的isWritable就会变成false, + * 当buffer的大小低于低水位线的时候,isWritable就会变成true。所以应用应该判断isWritable,如果是false就不要再写数据了。 + * 高水位线和低水位线是字节数,默认高水位是64K,低水位是32K,我们可以根据我们的应用需要支持多少连接数和系统资源进行合理规划。 + */ + b.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 63a8a4d7..0a41c29c 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -24,8 +24,12 @@ import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; import com.mpush.netty.server.NettyServer; +import com.mpush.tools.config.CC; +import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; +import io.netty.channel.WriteBufferWaterMark; import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; import java.util.concurrent.Executors; @@ -54,10 +58,11 @@ public void init() { receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); connectionManager = new ServerConnectionManager(false); channelHandler = new ServerChannelHandler(false, connectionManager, receiver); - if (enabled) { + + if (CC.mp.net.traffic_shaping.gateway_server.enabled) {//启用流量整形,限流 trafficShapingHandler = new GlobalChannelTrafficShapingHandler( - Executors.newSingleThreadScheduledExecutor() - , write_global_limit, read_global_limit, + Executors.newSingleThreadScheduledExecutor(), + write_global_limit, read_global_limit, write_channel_limit, read_channel_limit, check_interval); } @@ -82,6 +87,31 @@ protected void initPipeline(ChannelPipeline pipeline) { } } + @Override + protected void initOptions(ServerBootstrap b) { + super.initOptions(b); + + /** + * 这个坑其实也不算坑,只是因为懒,该做的事情没做。一般来讲我们的业务如果比较小的时候我们用同步处理,等业务到一定规模的时候,一个优化手段就是异步化。 + * 异步化是提高吞吐量的一个很好的手段。但是,与异步相比,同步有天然的负反馈机制,也就是如果后端慢了,前面也会跟着慢起来,可以自动的调节。 + * 但是异步就不同了,异步就像决堤的大坝一样,洪水是畅通无阻。如果这个时候没有进行有效的限流措施就很容易把后端冲垮。 + * 如果一下子把后端冲垮倒也不是最坏的情况,就怕把后端冲的要死不活。 + * 这个时候,后端就会变得特别缓慢,如果这个时候前面的应用使用了一些无界的资源等,就有可能把自己弄死。 + * 那么现在要介绍的这个坑就是关于Netty里的ChannelOutboundBuffer这个东西的。 + * 这个buffer是用在netty向channel write数据的时候,有个buffer缓冲,这样可以提高网络的吞吐量(每个channel有一个这样的buffer)。 + * 初始大小是32(32个元素,不是指字节),但是如果超过32就会翻倍,一直增长。 + * 大部分时候是没有什么问题的,但是在碰到对端非常慢(对端慢指的是对端处理TCP包的速度变慢,比如对端负载特别高的时候就有可能是这个情况)的时候就有问题了, + * 这个时候如果还是不断地写数据,这个buffer就会不断地增长,最后就有可能出问题了(我们的情况是开始吃swap,最后进程被linux killer干掉了)。 + * 为什么说这个地方是坑呢,因为大部分时候我们往一个channel写数据会判断channel是否active,但是往往忽略了这种慢的情况。 + * + * 那这个问题怎么解决呢?其实ChannelOutboundBuffer虽然无界,但是可以给它配置一个高水位线和低水位线, + * 当buffer的大小超过高水位线的时候对应channel的isWritable就会变成false, + * 当buffer的大小低于低水位线的时候,isWritable就会变成true。所以应用应该判断isWritable,如果是false就不要再写数据了。 + * 高水位线和低水位线是字节数,默认高水位是64K,低水位是32K,我们可以根据我们的应用需要支持多少连接数和系统资源进行合理规划。 + */ + b.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 1024 * 1024)); + } + @Override public ChannelHandler getChannelHandler() { return channelHandler; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 248c40f5..5533f6da 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -84,7 +84,7 @@ public Connection get(final Channel channel) { @Override public void add(Connection connection) { connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); - if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); + //if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); } @Override diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index a963c00c..7333762a 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -77,12 +77,19 @@ public ChannelFuture send(Packet packet) { @Override public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { - if (channel.isActive() && channel.isWritable()) { + if (channel.isActive()) { + ChannelFuture future = channel.writeAndFlush(packet).addListener(this); + if (listener != null) { - return channel.writeAndFlush(packet).addListener(listener).addListener(this); - } else { - return channel.writeAndFlush(packet).addListener(this); + future.addListener(listener); + } + + if (channel.isWritable()) { + return future; } + + //阻塞调用线程? + return future.awaitUninterruptibly(); } else { return this.close(); } From 97bc2b0714507ba2d200e50e79486d65ed11846c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 12 Oct 2016 11:02:36 +0800 Subject: [PATCH 676/890] =?UTF-8?q?=E8=A7=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/netty/codec/PacketDecoder.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 153ab950..6e793d0c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -57,13 +57,15 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { while (in.readableBytes() >= Packet.HEADER_LEN) { //1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 in.markReaderIndex(); + Packet packet = decodeFrame(in); - if (packet != null) { - out.add(packet); - } else { + if (packet == null) { //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 in.resetReaderIndex(); + break; } + + out.add(packet); } } From ee3befd336558bf82f3c070b7337a6d8a0de1544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 13 Oct 2016 22:05:11 +0800 Subject: [PATCH 677/890] =?UTF-8?q?=E6=96=B0=E5=A2=9Eclient=20push=20?= =?UTF-8?q?=E4=B8=8A=E8=A1=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/common/message/PushMessage.java | 4 ++ .../mpush/core/handler/ClientPushHandler.java | 52 +++++++++++++++++++ .../mpush/core/server/ConnectionServer.java | 1 + 3 files changed, 57 insertions(+) create mode 100644 mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index 40978cde..4c79280d 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -52,6 +52,10 @@ public byte[] encode() { return content; } + public boolean autoAck() { + return packet.hasFlag(Packet.FLAG_AUTO_ACK); + } + public boolean needAck() { return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java new file mode 100644 index 00000000..7a2987f5 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java @@ -0,0 +1,52 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.handler; + + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.AckMessage; +import com.mpush.common.message.PushMessage; +import com.mpush.tools.log.Logs; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn (夜色) + */ +public final class ClientPushHandler extends BaseMessageHandler { + + @Override + public PushMessage decode(Packet packet, Connection connection) { + return new PushMessage(packet, connection); + } + + @Override + public void handle(PushMessage message) { + Logs.PUSH.info(">>> receive client push message={}", message); + + if (message.autoAck()) { + AckMessage.from(message).sendRaw(); + Logs.PUSH.info("<<< send ack for push message={}", message); + } + //biz code write here + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index d199af99..59774d16 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -68,6 +68,7 @@ public void init() { receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.UNBIND, new BindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); + receiver.register(Command.PUSH, new ClientPushHandler()); receiver.register(Command.ACK, new AckHandler()); if (CC.mp.http.proxy_enabled) { From 155dbd7eb982c2782590a440bdfae986cb584152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 13 Oct 2016 22:05:39 +0800 Subject: [PATCH 678/890] =?UTF-8?q?redis=20=E5=89=8D=E7=BC=80=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/cache/redis/RedisKey.java | 24 +++++++++---------- .../common/router/RemoteRouterManager.java | 11 ++++----- .../com/mpush/common/user/UserManager.java | 14 +++++------ .../com/mpush/test/redis/RedisUtilTest.java | 8 +++---- 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 62168f7d..9bff61f9 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -21,33 +21,31 @@ public final class RedisKey { - private static final String USER_PREFIX = "mp_uc_"; + private static final String USER_PREFIX = "mp:ur:";//用户路由 - private static final String SESSION_PREFIX = "mp_s_"; + private static final String SESSION_PREFIX = "mp:rs:";//可复用session - private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp_f_c_d_"; + private static final String FAST_CONNECTION_DEVICE_PREFIX = "mp:fcd:"; - private static final String USER_ONLINE_KEY = "mp_u_ol_"; + private static final String ONLINE_USER_LIST_KEY_PREFIX = "mp:oul:";//在线用户列表 - public static final String SESSION_AES_KEY = "mp_s_a_k"; - public static final String SESSION_AES_SEQ_KEY = "mp_s_a_s_k"; + public static final String SESSION_AES_KEY = "mp:sa"; + public static final String SESSION_AES_SEQ_KEY = "mp:sas"; - public static final String getUserKey(String userId) { + public static String getUserRouteKey(String userId) { return USER_PREFIX + userId; } - public static final String getSessionKey(String sessionId) { + public static String getSessionKey(String sessionId) { return SESSION_PREFIX + sessionId; } - //for fast connection test - public static final String getDeviceIdKey(String deviceId) { + public static String getDeviceIdKey(String deviceId) { return FAST_CONNECTION_DEVICE_PREFIX + deviceId; } - public static final String getUserOnlineKey(String extranetAddress) { - return USER_ONLINE_KEY + extranetAddress; + public static String getOnlineUserListKey(String publicIP) { + return ONLINE_USER_LIST_KEY_PREFIX + publicIP; } - } diff --git a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java index 9f77f96a..562fb9ab 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java +++ b/mpush-common/src/main/java/com/mpush/common/router/RemoteRouterManager.java @@ -24,7 +24,6 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.router.ClientLocation; -import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; @@ -47,7 +46,7 @@ public class RemoteRouterManager extends EventConsumer implements RouterManager< @Override public RemoteRouter register(String userId, RemoteRouter router) { - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserRouteKey(userId); String field = Integer.toString(router.getRouteValue().getClientType()); ClientLocation old = RedisManager.I.hget(key, field, ClientLocation.class); RedisManager.I.hset(key, field, router.getRouteValue()); @@ -65,7 +64,7 @@ public RemoteRouter register(String userId, RemoteRouter router) { */ @Override public boolean unRegister(String userId, int clientType) { - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserRouteKey(userId); String field = Integer.toString(clientType); ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); if (location == null || location.isOffline()) return true; @@ -76,7 +75,7 @@ public boolean unRegister(String userId, int clientType) { @Override public Set lookupAll(String userId) { - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserRouteKey(userId); Map values = RedisManager.I.hgetAll(key, ClientLocation.class); if (values == null || values.isEmpty()) return Collections.emptySet(); return values.values().stream().map(RemoteRouter::new).collect(Collectors.toSet()); @@ -84,7 +83,7 @@ public Set lookupAll(String userId) { @Override public RemoteRouter lookup(String userId, int clientType) { - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserRouteKey(userId); String field = Integer.toString(clientType); ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); LOGGER.info("lookup remote router userId={}, router={}", userId, location); @@ -103,7 +102,7 @@ void on(ConnectionCloseEvent event) { SessionContext context = connection.getSessionContext(); String userId = context.userId; if (userId == null) return; - String key = RedisKey.getUserKey(userId); + String key = RedisKey.getUserRouteKey(userId); String field = Integer.toString(context.getClientType()); ClientLocation location = RedisManager.I.hget(key, field, ClientLocation.class); if (location == null || location.isOffline()) return; diff --git a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java index eb1411a0..68b63fd1 100644 --- a/mpush-common/src/main/java/com/mpush/common/user/UserManager.java +++ b/mpush-common/src/main/java/com/mpush/common/user/UserManager.java @@ -32,31 +32,31 @@ public final class UserManager { private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class); public static final UserManager I = new UserManager(); - private final String ONLINE_KEY = RedisKey.getUserOnlineKey(ConfigManager.I.getPublicIp()); + private final String onlineUserListKey = RedisKey.getOnlineUserListKey(ConfigManager.I.getPublicIp()); public void clearUserOnlineData() { - RedisManager.I.del(ONLINE_KEY); + RedisManager.I.del(onlineUserListKey); } public void addToOnlineList(String userId) { - RedisManager.I.zAdd(ONLINE_KEY, userId); + RedisManager.I.zAdd(onlineUserListKey, userId); LOGGER.info("user online {}", userId); } public void remFormOnlineList(String userId) { - RedisManager.I.zRem(ONLINE_KEY, userId); + RedisManager.I.zRem(onlineUserListKey, userId); LOGGER.info("user offline {}", userId); } //在线用户数量 public long getOnlineUserNum() { - Long value = RedisManager.I.zCard(ONLINE_KEY); + Long value = RedisManager.I.zCard(onlineUserListKey); return value == null ? 0 : value; } //在线用户数量 public long getOnlineUserNum(String publicIP) { - String online_key = RedisKey.getUserOnlineKey(publicIP); + String online_key = RedisKey.getOnlineUserListKey(publicIP); Long value = RedisManager.I.zCard(online_key); return value == null ? 0 : value; } @@ -66,6 +66,6 @@ public List getOnlineUserList(int start, int size) { if (size < 10) { size = 10; } - return RedisManager.I.zrange(ONLINE_KEY, start, size - 1, String.class); + return RedisManager.I.zrange(onlineUserListKey, start, size - 1, String.class); } } diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java index 260a479d..24859f82 100644 --- a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java +++ b/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java @@ -193,9 +193,9 @@ public void hashTest() { @Test public void testSet() { -// System.out.println(RedisClient.sCard(node, RedisKey.getUserOnlineKey())); +// System.out.println(RedisClient.sCard(node, RedisKey.getOnlineUserListKey())); -// List onlineUserIdList = RedisClient.sScan(node, RedisKey.getUserOnlineKey(), String.class, 0); +// List onlineUserIdList = RedisClient.sScan(node, RedisKey.getOnlineUserListKey(), String.class, 0); // System.out.println(onlineUserIdList.size()); } @@ -207,9 +207,9 @@ public void testlist() { @Test public void testsortedset() { -// RedisClient.zAdd(nodeList, RedisKey.getUserOnlineKey(), "doctor1test"); +// RedisClient.zAdd(nodeList, RedisKey.getOnlineUserListKey(), "doctor1test"); -// long len =RedisClient.zCard(node, RedisKey.getUserOnlineKey()); +// long len =RedisClient.zCard(node, RedisKey.getOnlineUserListKey()); // System.out.println(len); } From c940cc687e513288f321c62c73393cd8626aead7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 14 Oct 2016 11:01:10 +0800 Subject: [PATCH 679/890] =?UTF-8?q?SPI=20=E4=BC=98=E5=8C=96=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B3=A8=E8=A7=A3=EF=BC=8C=E5=A4=9A=E4=B8=AA=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=8F=AF=E4=BB=A5=E6=8C=87=E5=AE=9A=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/api/spi/Spi.java | 48 +++++++++++++++++++ .../java/com/mpush/api/spi/SpiLoader.java | 23 +++++++-- .../mpush/client/push/PushClientFactory.java | 2 + .../net/HttpProxyDnsMappingManager.java | 6 +-- .../common/security/RsaCipherFactory.java | 2 + .../thread/pool/DefaultThreadPoolFactory.java | 2 + .../pool/DumpThreadRejectedHandler.java | 4 +- 7 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/Spi.java diff --git a/mpush-api/src/main/java/com/mpush/api/spi/Spi.java b/mpush-api/src/main/java/com/mpush/api/spi/Spi.java new file mode 100644 index 00000000..0758d6b6 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/Spi.java @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi; + +import java.lang.annotation.*; + +/** + * Created by ohun on 16/10/14. + * + * @author ohun@live.cn (夜色) + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Spi { + + /** + * SPI name + * + * @return name + */ + String value() default ""; + + /** + * 排序顺序 + * + * @return sortNo + */ + int order() default 0; + +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index 59df059f..e93c24af 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -19,14 +19,16 @@ package com.mpush.api.spi; -import java.util.Iterator; -import java.util.Map; -import java.util.ServiceLoader; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; public final class SpiLoader { private static final Map CACHE = new ConcurrentHashMap<>(); + public static void clear() { + CACHE.clear(); + } + public static T load(Class clazz) { return load(clazz, null); } @@ -65,9 +67,20 @@ public static T load0(Class clazz, String name) { private static T filterByName(ServiceLoader factories, String name) { Iterator it = factories.iterator(); if (name == null) { - if (it.hasNext()) { - return it.next(); + List list = new ArrayList(2); + while (it.hasNext()) { + list.add(it.next()); + } + if (list.size() > 1) { + list.sort((o1, o2) -> { + Spi spi1 = o1.getClass().getAnnotation(Spi.class); + Spi spi2 = o2.getClass().getAnnotation(Spi.class); + int order1 = spi1 == null ? 0 : spi1.order(); + int order2 = spi2 == null ? 0 : spi2.order(); + return order1 - order2; + }); } + if (list.size() > 0) return list.get(0); } else { while (it.hasNext()) { T t = it.next(); diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java index 5e91639d..c7fe082d 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClientFactory.java @@ -20,6 +20,7 @@ package com.mpush.client.push; import com.mpush.api.push.PushSender; +import com.mpush.api.spi.Spi; import com.mpush.api.spi.client.PusherFactory; /** @@ -27,6 +28,7 @@ * * @author ohun@live.cn */ +@Spi public class PushClientFactory implements PusherFactory { private static PushClient CLIENT; diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index e4cb77c5..594ecf1c 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -23,6 +23,7 @@ import com.google.common.collect.Maps; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; +import com.mpush.api.spi.Spi; import com.mpush.api.spi.net.DnsMapping; import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.tools.Jsons; @@ -40,6 +41,7 @@ import static com.mpush.tools.Utils.checkHealth; +@Spi(order = 1) public class HttpProxyDnsMappingManager extends BaseService implements DnsMappingManager, Runnable { private final Logger logger = LoggerFactory.getLogger(HttpProxyDnsMappingManager.class); private final ZKDnsNodeWatcher watcher = new ZKDnsNodeWatcher(); @@ -71,10 +73,8 @@ protected void doStop(Listener listener) throws Throwable { @Override public void init() { - logger.error("start init dnsMapping"); all.putAll(CC.mp.http.dns_mapping); available.putAll(CC.mp.http.dns_mapping); - logger.error("end init dnsMapping"); } @Override @@ -115,7 +115,7 @@ public void run() { if (checkHealth(dnsMapping.getIp(), dnsMapping.getPort())) { nowValue.add(dnsMapping); } else { - logger.error("dns can not reachable:" + Jsons.toJson(dnsMapping)); + logger.warn("dns can not reachable:" + Jsons.toJson(dnsMapping)); } }); available.put(key, nowValue); diff --git a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java index 441f5006..acf57a44 100644 --- a/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java +++ b/mpush-common/src/main/java/com/mpush/common/security/RsaCipherFactory.java @@ -20,6 +20,7 @@ package com.mpush.common.security; import com.mpush.api.connection.Cipher; +import com.mpush.api.spi.Spi; import com.mpush.api.spi.core.CipherFactory; /** @@ -27,6 +28,7 @@ * * @author ohun@live.cn */ +@Spi public class RsaCipherFactory implements CipherFactory { private static final RsaCipher RSA_CIPHER = RsaCipher.create(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java index ef83ac45..3dfba9f2 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java @@ -19,6 +19,7 @@ package com.mpush.tools.thread.pool; +import com.mpush.api.spi.Spi; import com.mpush.api.spi.common.ThreadPoolFactory; import com.mpush.tools.config.CC; import com.mpush.tools.thread.PoolThreadFactory; @@ -33,6 +34,7 @@ /** * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 */ +@Spi(order = 1) public class DefaultThreadPoolFactory implements ThreadPoolFactory { private Executor get(ThreadPoolConfig config) { diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java index f5d5165d..ee1741df 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -66,9 +66,9 @@ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { } private void dumpJVMInfo() { - LOGGER.error("start dump jvm info"); + LOGGER.info("start dump jvm info"); JVMUtil.dumpJstack(DUMP_DIR + "/" + poolConfig.getName()); - LOGGER.error("end dump jvm info"); + LOGGER.info("end dump jvm info"); } } From 062de345fa8376422ba85f9e53572c13009e06b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 14 Oct 2016 11:01:32 +0800 Subject: [PATCH 680/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/src/test/resources/logback.xml | 172 +++++++++++++++++- .../com/mpush/zk/cache/ZKServerNodeCache.java | 2 +- .../mpush/zk/listener/ZKRedisNodeWatcher.java | 2 +- 3 files changed, 165 insertions(+), 11 deletions(-) diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index 95ebebbe..33a9b023 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -1,30 +1,184 @@ - + + + + System.err + + WARN + + true + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + info + ACCEPT + DENY + + System.out + true + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + + + debug + ACCEPT + DENY + + System.out + true + + %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + + + + + System.out UTF-8 DEBUG - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + - + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + + + System.out + UTF-8 + + DEBUG + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + + System.err UTF-8 - debug + DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{HH:mm:ss.SSS} - %msg%n - + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java index e49121a0..e9b0e266 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java +++ b/mpush-zk/src/main/java/com/mpush/zk/cache/ZKServerNodeCache.java @@ -51,7 +51,7 @@ public void put(String fullPath, ZKServerNode node) { if (StringUtils.isNotBlank(fullPath) && node != null) { nodes.put(fullPath, node); } else { - logger.error("fullPath is null or application is null"); + logger.error("fullPath is null or zkNode is null"); } printCache(); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java index ef805ff0..71a6bb7c 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKRedisNodeWatcher.java @@ -41,7 +41,7 @@ public class ZKRedisNodeWatcher extends ZKNodeCacheWatcher { private void refresh() { String rawGroup = ZKClient.I.get(ZKPath.REDIS_SERVER.getRootPath()); - logger.warn("refresh zk redis node cache, data=" + rawGroup); + logger.info("refresh zk redis node cache, data=" + rawGroup); if (Strings.isNullOrEmpty(rawGroup)) return; ZKRedisNode[] group = Jsons.fromJson(rawGroup, ZKRedisNode[].class); if (group == null) return; From 221f41123b12a55b2689b5d212a8ba91649aeb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 14 Oct 2016 11:37:37 +0800 Subject: [PATCH 681/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/core/handler/AckHandler.java | 2 +- .../java/com/mpush/core/handler/BindUserHandler.java | 4 ++-- .../com/mpush/core/handler/FastConnectHandler.java | 2 +- .../com/mpush/core/handler/GatewayPushHandler.java | 12 ++++++------ .../com/mpush/core/handler/HandshakeHandler.java | 2 +- .../com/mpush/core/handler/HttpProxyHandler.java | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java index 8d2f0855..ed58d166 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java @@ -46,7 +46,7 @@ public AckMessage decode(Packet packet, Connection connection) { public void handle(AckMessage message) { AckContext context = AckMessageQueue.I.getAndRemove(message.getSessionId()); if (context == null) { - Logs.PUSH.info("receive client ack, but timeout message={}", message); + Logs.PUSH.info(">>> receive client ack, but timeout message={}", message); return; } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 418c01ee..d36a0bfb 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -85,7 +85,7 @@ private void bind(BindUserMessage message) { context.tags = message.tags; EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").send(); - Logs.Conn.info("bind user success, userId={}, session={}", message.userId, context); + Logs.Conn.info(">>> bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.I.unRegister(message.userId, context.getClientType()); @@ -142,7 +142,7 @@ private void unbind(BindUserMessage message) { context.tags = null; EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").send(); - Logs.Conn.info("unbind user success, userId={}, session={}", userId, context); + Logs.Conn.info(">>> unbind user success, userId={}, session={}", userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").send(); Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 98c3bade..6b1411cd 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -68,7 +68,7 @@ public void handle(FastConnectMessage message) { .from(message) .setHeartbeat(heartbeat) .send(); - Logs.Conn.info("fast connect success, session={}", session.context); + Logs.Conn.info(">>> fast connect success, session={}", session.context); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 3ae74c1d..a0b39dcf 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -133,7 +133,7 @@ private void sendBroadcast(GatewayPushMessage message) { if (future.isSuccess()) {//推送成功 sendUserIds.add(userId); - Logs.PUSH.info("gateway broadcast client success, userId={}, message={}", userId, message); + Logs.PUSH.info("<<< gateway broadcast client success, userId={}, message={}", userId, message); } else {//推送失败 Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); @@ -218,7 +218,7 @@ private boolean checkLocal(final GatewayPushMessage message) { OkMessage.from(message).setData(userId + ',' + clientType).sendRaw(); } - Logs.PUSH.info("gateway push message to client success, message={}", message); + Logs.PUSH.info("<<< gateway push message to client success, message={}", message); } else {//推送失败 @@ -284,27 +284,27 @@ private AckContext buildAckContext(GatewayPushMessage message) { @Override public void onSuccess(AckContext ctx) { if (!gatewayConnection.isConnected()) { - Logs.PUSH.info("receive client ack, gateway connection is closed, context={}", ctx); + Logs.PUSH.info(">>> receive client ack, gateway connection is closed, context={}", ctx); return; } OkMessage okMessage = new OkMessage(ctx.cmd, new Packet(OK, ctx.gatewayMessageId), gatewayConnection); okMessage.setData(userId + ',' + clientType); okMessage.sendRaw(); - Logs.PUSH.info("receive client ack and response gateway client success, context={}", ctx); + Logs.PUSH.info(">>> receive client ack and response gateway client success, context={}", ctx); } @Override public void onTimeout(AckContext ctx) { if (!gatewayConnection.isConnected()) { - Logs.PUSH.info("receive client ack, gateway connection is closed, context={}", ctx); + Logs.PUSH.info("push message timeout client not ack, gateway connection is closed, context={}", ctx); return; } ErrorMessage errorMessage = new ErrorMessage(ctx.cmd, new Packet(ERROR, ctx.gatewayMessageId), gatewayConnection); errorMessage.setData(userId + ',' + clientType); errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT); errorMessage.sendRaw(); - Logs.PUSH.info("push message success but client not ack, context={}", ctx); + Logs.PUSH.info("push message timeout client not ack, context={}", ctx); } }); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 842324ae..f638297d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -105,6 +105,6 @@ public void handle(HandshakeMessage message) { //10.触发握手成功事件 EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); - Logs.Conn.info("handshake success, session={}", context); + Logs.Conn.info(">>> handshake success, session={}", context); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 003b9c7d..28642d95 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -133,7 +133,7 @@ public void onResponse(HttpResponse httpResponse) { } } response.send(); - LOGGER.debug("callback success request={}, response={}", request, response); + LOGGER.debug("<<< callback success request={}, response={}", request, response); } @Override From a039372731ba691166ee4b78b00766b1f13c506f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:17:25 +0800 Subject: [PATCH 682/890] =?UTF-8?q?ClientPushHandler=20SPI=20=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/spi/handler/PushHandlerFactory.java | 31 +++++++++++++++++++ .../mpush/core/server/ConnectionServer.java | 4 ++- ...m.mpush.api.spi.handler.PushHandlerFactory | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java create mode 100644 mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.PushHandlerFactory diff --git a/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java new file mode 100644 index 00000000..5c2cd919 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.handler; + +import com.mpush.api.MessageHandler; +import com.mpush.api.spi.Factory; + +/** + * Created by ohun on 16/10/14. + * + * @author ohun@live.cn (夜色) + */ +public interface PushHandlerFactory extends Factory { +} diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 59774d16..a1284bde 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -23,6 +23,8 @@ import com.mpush.api.connection.ConnectionManager; import com.mpush.api.protocol.Command; import com.mpush.api.service.Listener; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.handler.PushHandlerFactory; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.*; import com.mpush.netty.http.HttpClient; @@ -68,7 +70,7 @@ public void init() { receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.UNBIND, new BindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - receiver.register(Command.PUSH, new ClientPushHandler()); + receiver.register(Command.PUSH, SpiLoader.load(PushHandlerFactory.class).get()); receiver.register(Command.ACK, new AckHandler()); if (CC.mp.http.proxy_enabled) { diff --git a/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.PushHandlerFactory b/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.PushHandlerFactory new file mode 100644 index 00000000..42b566cc --- /dev/null +++ b/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.PushHandlerFactory @@ -0,0 +1 @@ +com.mpush.core.handler.ClientPushHandler \ No newline at end of file From 7ae338696d83a0aab642399bc68d7880a7c2ee52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:18:30 +0800 Subject: [PATCH 683/890] =?UTF-8?q?Server=20=E5=85=B3=E9=97=AD=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E5=85=88=E5=85=B3=E9=97=ADmain=20reactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/netty/server/NettyServer.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java index 793cf504..f5bb02bf 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java @@ -77,14 +77,13 @@ public boolean isRunning() { @Override public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { - IllegalStateException e = new IllegalStateException("server was already shutdown."); - if (listener != null) listener.onFailure(e); + if (listener != null) listener.onFailure(new IllegalStateException("server was already shutdown.")); Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName()); return; } Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); - if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly(); - if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly(); + if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly();//要先关闭接收连接的main reactor + if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly();//再关闭处理业务的sub reactor Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); if (listener != null) { listener.onSuccess(port); From b7582192891f9526e74f853f87f066b0736ecd01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:19:22 +0800 Subject: [PATCH 684/890] =?UTF-8?q?=E8=A7=A3=E7=A0=81=E4=BC=98=E5=8C=96,?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=BE=AA=E7=8E=AF=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/netty/codec/PacketDecoder.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 6e793d0c..8b49a0b4 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -54,7 +54,7 @@ private void decodeHeartbeat(ByteBuf in, List out) { } private void decodeFrames(ByteBuf in, List out) throws Exception { - while (in.readableBytes() >= Packet.HEADER_LEN) { + if (in.readableBytes() >= Packet.HEADER_LEN) { //1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 in.markReaderIndex(); @@ -62,7 +62,6 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { if (packet == null) { //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 in.resetReaderIndex(); - break; } out.add(packet); From 47a387823af957b3954b76394c16c70debf393cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:19:49 +0800 Subject: [PATCH 685/890] =?UTF-8?q?ClientPushHandler=20SPI=20=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/core/handler/ClientPushHandler.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java index 7a2987f5..f278758e 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java @@ -20,8 +20,11 @@ package com.mpush.core.handler; +import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.api.spi.Spi; +import com.mpush.api.spi.handler.PushHandlerFactory; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.AckMessage; import com.mpush.common.message.PushMessage; @@ -32,7 +35,8 @@ * * @author ohun@live.cn (夜色) */ -public final class ClientPushHandler extends BaseMessageHandler { +@Spi(order = 1) +public final class ClientPushHandler extends BaseMessageHandler implements PushHandlerFactory { @Override public PushMessage decode(Packet packet, Connection connection) { @@ -49,4 +53,9 @@ public void handle(PushMessage message) { } //biz code write here } + + @Override + public MessageHandler get() { + return this; + } } From 9c11bdd591a619184c6dded67252fe2cce2e9a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:21:22 +0800 Subject: [PATCH 686/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E5=BC=80=E5=85=B3=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 ++ mpush-tools/src/main/java/com/mpush/tools/config/CC.java | 3 +++ 2 files changed, 5 insertions(+) diff --git a/conf/reference.conf b/conf/reference.conf index 1c050148..731454a4 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -184,6 +184,8 @@ mp { dump-stack=false //是否定时dump堆栈 dump-period=1m //多久监控一次 print-log=true //是否打印监控日志 + profile-enabled=true //开启性能监控 + profile-slowly-duration=10ms //耗时超过10ms打印日志 } #SPI扩展配置 diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index a61a2696..74517f45 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -20,6 +20,7 @@ package com.mpush.tools.config; import com.mpush.api.spi.net.DnsMapping; +import com.mpush.tools.common.Profiler; import com.mpush.tools.config.data.RedisNode; import com.typesafe.config.*; @@ -287,6 +288,8 @@ interface monitor { boolean dump_stack = cfg.getBoolean("dump-stack"); boolean print_log = cfg.getBoolean("print-log"); Duration dump_period = cfg.getDuration("dump-period"); + boolean profile_enabled = cfg.getBoolean("profile-enabled"); + Duration profile_slowly_duration = cfg.getDuration("profile-slowly-duration"); } interface spi { From 34763796183b62b6650baf441cb516e5abd52932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:22:24 +0800 Subject: [PATCH 687/890] =?UTF-8?q?-Dio.netty.noKeySetOptimization=3Dfalse?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/set-env.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/set-env.sh b/bin/set-env.sh index 9f7bf9b1..b667a001 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -1,2 +1,3 @@ #!/usr/bin/env bash -#JVM_FLAGS="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008 -Dio.netty.leakDetectionLevel=advanced" \ No newline at end of file +JVM_FLAGS="-Dio.netty.leakDetectionLevel=advanced -Dio.netty.noKeySetOptimization=false" +JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" \ No newline at end of file From 8505f49365399cdd75282e0f05c89938e9da56f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:35:44 +0800 Subject: [PATCH 688/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0netty=E4=BC=98?= =?UTF-8?q?=E5=8C=96jvm=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/set-env.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bin/set-env.sh b/bin/set-env.sh index b667a001..f619ad47 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -1,3 +1,16 @@ #!/usr/bin/env bash + +#io.netty.leakDetectionLevel +#netty的内存泄露检测分为四级: +#DISABLED: 不进行内存泄露的检测; +#SIMPLE: 抽样检测,且只对部分方法调用进行记录,消耗较小,有泄漏时可能会延迟报告,默认级别; +#ADVANCED: 抽样检测,记录对象最近几次的调用记录,有泄漏时可能会延迟报告; +#PARANOID: 每次创建一个对象时都进行泄露检测,且会记录对象最近的详细调用记录。是比较激进的内存泄露检测级别,消耗最大,建议只在测试时使用。 + +#Dio.netty.noKeySetOptimization +#是否禁用nio Selector.selectedKeys优化, 通过反射实现, 默认false + JVM_FLAGS="-Dio.netty.leakDetectionLevel=advanced -Dio.netty.noKeySetOptimization=false" -JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" \ No newline at end of file + +#开启远程调试 +#JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" \ No newline at end of file From 88d3d88bed72b87d4640ccaa48c11be5af562ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:37:04 +0800 Subject: [PATCH 689/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=80=A7=E8=83=BD=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/logback.xml | 18 +++++++++ mpush-test/src/test/resources/logback.xml | 38 +++++++++++++------ .../main/java/com/mpush/tools/log/Logs.java | 4 +- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml index 7b8ebb77..807cee5e 100644 --- a/mpush-boot/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -158,6 +158,20 @@ + + + ${log.home}/profile-mpush.log + true + + ${log.home}/profile-mpush.log.%d{yyyy-MM-dd} + + 10 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + + + System.out UTF-8 @@ -207,6 +221,10 @@ + + + + diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index 33a9b023..2bbd9024 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -9,7 +9,7 @@ true - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -23,7 +23,7 @@ System.out true - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -37,7 +37,7 @@ System.out true - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n + %d{HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -49,7 +49,7 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[monitor]- %msg%n @@ -61,7 +61,7 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[connection]- %msg%n @@ -73,7 +73,7 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[push]- %msg%n @@ -85,7 +85,7 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[heartbeat]- %msg%n @@ -97,7 +97,7 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[redis]- %msg%n @@ -109,7 +109,7 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[http]- %msg%n @@ -121,7 +121,19 @@ DEBUG - %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[zk]- %msg%n + + + + + + System.err + UTF-8 + + DEBUG + + + %d{HH:mm:ss.SSS} -[profile]- %msg%n @@ -132,7 +144,7 @@ DEBUG - %d{HH:mm:ss.SSS} - %msg%n + %d{HH:mm:ss.SSS} -[console]- %msg%n @@ -175,6 +187,10 @@ + + + + diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index 44fb5e5e..f53b236a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -56,5 +56,7 @@ static boolean init() { ZK = LoggerFactory.getLogger("mpush.zk.log"), - HTTP = LoggerFactory.getLogger("mpush.http.log"); + HTTP = LoggerFactory.getLogger("mpush.http.log"), + + PROFILE = LoggerFactory.getLogger("mpush.profile.log"); } From 610f717682e28a5ed69e4e499799679035662468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 11:38:52 +0800 Subject: [PATCH 690/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E5=88=86=E6=9E=90=E4=B8=8E=E7=9B=91=E6=8E=A7=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/common/MessageDispatcher.java | 2 +- .../common/handler/BaseMessageHandler.java | 12 ++--- .../com/mpush/common/message/BaseMessage.java | 5 ++ .../mpush/common/message/ByteBufMessage.java | 14 +++-- .../com/mpush/core/handler/AdminHandler.java | 18 ++++++- .../mpush/core/handler/BindUserHandler.java | 10 ++-- .../core/handler/FastConnectHandler.java | 9 ++-- .../core/handler/GatewayPushHandler.java | 1 + .../mpush/core/handler/HttpProxyHandler.java | 2 +- .../core/server/ServerChannelHandler.java | 17 +++--- .../java/com/mpush/tools/common/IOUtils.java | 4 +- .../java/com/mpush/tools/common/Profiler.java | 52 ++++++++++++------- .../java/com/mpush/tools/crypto/AESUtils.java | 7 +++ 13 files changed, 103 insertions(+), 50 deletions(-) diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 4cfda9b0..712100ed 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -53,8 +53,8 @@ public void register(Command command, MessageHandler handler) { public void onReceive(Packet packet, Connection connection) { MessageHandler handler = handlers.get(packet.cmd); if (handler != null) { + Profiler.enter("time cost on [dispatch]"); try { - Profiler.enter("start handle:" + handler.getClass().getSimpleName()); handler.handle(packet, connection); } catch (Throwable throwable) { LOGGER.error("dispatch message ex, packet={}, connect={}, body={}" diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index 181e3d5c..dcd78cc6 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -37,14 +37,14 @@ public abstract class BaseMessageHandler implements MessageHa public abstract void handle(T message); public void handle(Packet packet, Connection connection) { + Profiler.enter("time cost on [message decode]"); T t = decode(packet, connection); + Profiler.release(); + if (t != null) { - try { - Profiler.enter("start handle"); - handle(t); - } finally { - Profiler.release(); - } + Profiler.enter("time cost on [handle]"); + handle(t); + Profiler.release(); } } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index fb49f847..56e9b1da 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -24,6 +24,7 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; import com.mpush.tools.common.IOUtils; +import com.mpush.tools.common.Profiler; import com.mpush.tools.config.CC; import io.netty.channel.ChannelFutureListener; @@ -65,12 +66,16 @@ protected void decodeBody() { } packet.body = tmp; + Profiler.enter("time cost on [body decode]"); decode(packet.body); + Profiler.release(); } } protected void encodeBody() { + Profiler.enter("time cost on [body encode]"); byte[] tmp = encode(); + Profiler.release(); if (tmp != null && tmp.length > 0) { //1.压缩 if (tmp.length > CC.mp.core.compress_threshold) { diff --git a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java index bb7ecb83..d149ddf9 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ByteBufMessage.java @@ -43,11 +43,15 @@ public void decode(byte[] body) { @Override public byte[] encode() { - ByteBuf body = Unpooled.buffer(); - encode(body); - byte[] bytes = new byte[body.readableBytes()]; - body.readBytes(bytes); - return bytes; + ByteBuf body = connection.getChannel().alloc().heapBuffer(); + try { + encode(body); + byte[] bytes = new byte[body.readableBytes()]; + body.readBytes(bytes); + return bytes; + } finally { + body.release(); + } } public abstract void decode(ByteBuf body); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 96162abc..9f658981 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -28,6 +28,7 @@ import com.mpush.core.server.AdminServer; import com.mpush.tools.Jsons; import com.mpush.tools.Utils; +import com.mpush.tools.common.Profiler; import com.mpush.tools.config.CC; import com.mpush.tools.config.ConfigManager; import com.mpush.zk.ZKClient; @@ -118,6 +119,7 @@ public String handler(ChannelHandlerContext ctx, String args) { buf.append("push:, push test msg to client" + EOL); buf.append("conf:[key] show config info" + EOL); buf.append("monitor:[mxBean] show system monitor" + EOL); + buf.append("profile:<1,0> enable/disable profile" + EOL); return buf.toString(); } }, @@ -203,9 +205,9 @@ public String handler(ChannelHandlerContext ctx, String args) { push { @Override public String handler(ChannelHandlerContext ctx, String... args) throws Exception { - Boolean success = PushSender.create().send(args[1], args[0], null).get(5, TimeUnit.SECONDS); + //Boolean success = PushSender.create().send(args[1], args[0], null).get(5, TimeUnit.SECONDS); - return success.toString(); + return "unsupported"; } }, conf { @@ -220,6 +222,18 @@ public String handler(ChannelHandlerContext ctx, String args) { return "key [" + args + "] not find in config"; } }, + profile { + @Override + public String handler(ChannelHandlerContext ctx, String args) throws Exception { + if (args == null || "0".equals(args)) { + Profiler.enable(false); + return "Profiler disabled"; + } else { + Profiler.enable(true); + return "Profiler enabled"; + } + } + }, rcs { @Override public String handler(ChannelHandlerContext ctx, String args) { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index d36a0bfb..069e956e 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -35,6 +35,7 @@ import com.mpush.core.router.LocalRouter; import com.mpush.core.router.LocalRouterManager; import com.mpush.core.router.RouterCenter; +import com.mpush.tools.common.Profiler; import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; @@ -71,7 +72,7 @@ private void bind(BindUserMessage message) { //处理重复绑定问题 if (context.userId != null) { if (message.userId.equals(context.userId)) { - OkMessage.from(message).setData("bind success").send(); + OkMessage.from(message).setData("bind success").sendRaw(); return; } else { unbind(message); @@ -84,7 +85,7 @@ private void bind(BindUserMessage message) { context.userId = message.userId; context.tags = message.tags; EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); - OkMessage.from(message).setData("bind success").send(); + OkMessage.from(message).setData("bind success").sendRaw(); Logs.Conn.info(">>> bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 @@ -125,7 +126,6 @@ private void unbind(BindUserMessage message) { unRegisterSuccess = remoteRouterManager.unRegister(userId, clientType); } } - //3.删除本地路由信息 LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); LocalRouter localRouter = localRouterManager.lookup(userId, clientType); @@ -141,10 +141,10 @@ private void unbind(BindUserMessage message) { context.userId = null; context.tags = null; EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); - OkMessage.from(message).setData("unbind success").send(); + OkMessage.from(message).setData("unbind success").sendRaw(); Logs.Conn.info(">>> unbind user success, userId={}, session={}", userId, context); } else { - ErrorMessage.from(message).setReason("unbind failed").send(); + ErrorMessage.from(message).setReason("unbind failed").sendRaw(); Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); } } else { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 6b1411cd..46f36457 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -27,6 +27,7 @@ import com.mpush.common.message.FastConnectOkMessage; import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; +import com.mpush.tools.common.Profiler; import com.mpush.tools.config.ConfigManager; import com.mpush.tools.log.Logs; @@ -45,8 +46,9 @@ public FastConnectMessage decode(Packet packet, Connection connection) { @Override public void handle(FastConnectMessage message) { //从缓存中心查询session + Profiler.enter("time cost on [query session]"); ReusableSession session = ReusableSessionManager.I.querySession(message.sessionId); - + Profiler.release(); if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); @@ -63,11 +65,12 @@ public void handle(FastConnectMessage message) { session.context.setHeartbeat(heartbeat); message.getConnection().setSessionContext(session.context); - + Profiler.enter("time cost on [send FastConnectOkMessage]"); FastConnectOkMessage .from(message) .setHeartbeat(heartbeat) - .send(); + .sendRaw(); + Profiler.release(); Logs.Conn.info(">>> fast connect success, session={}", session.context); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index a0b39dcf..654678ba 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -104,6 +104,7 @@ private void sendBroadcast(GatewayPushMessage message) { LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); AtomicInteger tasks = new AtomicInteger();//总任务数, 0表示任务全部结束 long begin = System.currentTimeMillis(); + //TODO 考虑使用线程池批量推送 for (int start = 0, limit = 1000; ; start += limit) { List userIds = UserManager.I.getOnlineUserList(start, limit); tasks.addAndGet(userIds.size());//增加任务数 diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 28642d95..b7fca21e 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -70,7 +70,6 @@ public HttpRequestMessage decode(Packet packet, Connection connection) { @Override public void handle(HttpRequestMessage message) { try { - Profiler.enter("start http proxy handler"); //1.参数校验 String method = message.getMethod(); String uri = message.uri; @@ -91,6 +90,7 @@ public void handle(HttpRequestMessage message) { setHeaders(request, message);//处理header setBody(request, message);//处理body + Profiler.enter("time cost on [HttpClient]"); //4.发送请求 httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 17f7b598..bf6c0e67 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -24,9 +24,11 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.ConnectionCloseEvent; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import com.mpush.netty.connection.NettyConnection; import com.mpush.tools.common.Profiler; +import com.mpush.tools.config.CC; import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; import io.netty.channel.ChannelHandler; @@ -45,6 +47,7 @@ public final class ServerChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); + private static final long profile_slowly_limit = CC.mp.monitor.profile_slowly_duration.toMillis(); /** * 是否启用加密 */ @@ -60,17 +63,19 @@ public ServerChannelHandler(boolean security, ConnectionManager connectionManage @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + Packet packet = (Packet) msg; + byte cmd = packet.cmd; + try { - Profiler.start("channel read:"); + Profiler.start("time cost on [channel read]: " + packet.toString()); Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("channelRead channel={}, connection={}, packet={}", ctx.channel(), connection, msg); + LOGGER.debug("channelRead channel={}, connection={}, packet={}", ctx.channel(), connection.getSessionContext(), msg); connection.updateLastReadTime(); - receiver.onReceive((Packet) msg, connection); + receiver.onReceive(packet, connection); } finally { Profiler.release(); - long duration = Profiler.getDuration(); - if (duration > 80) { - LOGGER.error("channel read busy:" + duration + "," + Profiler.dump()); + if (Profiler.getDuration() > profile_slowly_limit) { + Logs.PROFILE.info("Read Packet[cmd={}] Slowly: \n{}", Command.toCMD(cmd), Profiler.dump()); } Profiler.reset(); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java index da840eec..34a4533b 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/IOUtils.java @@ -50,7 +50,7 @@ public static void close(Closeable closeable) { public static byte[] compress(byte[] data) { - Profiler.enter("start compress"); + Profiler.enter("time cost on [compress]"); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); DeflaterOutputStream zipOut = new DeflaterOutputStream(out); @@ -69,7 +69,7 @@ public static byte[] compress(byte[] data) { } public static byte[] decompress(byte[] data) { - Profiler.enter("start decompress"); + Profiler.enter("time cost on [decompress]"); InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4); byte[] buffer = new byte[1024]; diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java index 57e2c9d6..67b3f0b4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java @@ -32,10 +32,16 @@ */ @SuppressWarnings(value = {"rawtypes", "unchecked"}) public class Profiler { + private static volatile boolean enabled = true; private static final ThreadLocal entryStack = new ThreadLocal(); public static final String EMPTY_STRING = ""; + public static void enable(boolean enabled) { + Profiler.enabled = enabled; + reset(); + } + /** * 开始计时。 */ @@ -48,9 +54,8 @@ public static void start() { * * @param message 第一个entry的信息 */ - public static void start(String message) { - entryStack.set(new Entry(message, null, null)); + if (enabled) entryStack.set(new Entry(message, null, null)); } /** @@ -59,7 +64,7 @@ public static void start(String message) { * @param message 第一个entry的信息 */ public static void start(Message message) { - entryStack.set(new Entry(message, null, null)); + if (enabled) entryStack.set(new Entry(message, null, null)); } /** @@ -78,10 +83,12 @@ public static void reset() { * @param message 新entry的信息 */ public static void enter(String message) { - Entry currentEntry = getCurrentEntry(); + if (enabled) { + Entry currentEntry = getCurrentEntry(); - if (currentEntry != null) { - currentEntry.enterSubEntry(message); + if (currentEntry != null) { + currentEntry.enterSubEntry(message); + } } } @@ -91,10 +98,12 @@ public static void enter(String message) { * @param message 新entry的信息 */ public static void enter(Message message) { - Entry currentEntry = getCurrentEntry(); + if (enabled) { + Entry currentEntry = getCurrentEntry(); - if (currentEntry != null) { - currentEntry.enterSubEntry(message); + if (currentEntry != null) { + currentEntry.enterSubEntry(message); + } } } @@ -102,10 +111,12 @@ public static void enter(Message message) { * 结束最近的一个entry,记录结束时间。 */ public static void release() { - Entry currentEntry = getCurrentEntry(); + if (enabled) { + Entry currentEntry = getCurrentEntry(); - if (currentEntry != null) { - currentEntry.release(); + if (currentEntry != null) { + currentEntry.release(); + } } } @@ -115,13 +126,16 @@ public static void release() { * @return 耗费的总时间,如果未开始计时,则返回-1 */ public static long getDuration() { - Entry entry = (Entry) entryStack.get(); + if (enabled) { + Entry entry = (Entry) entryStack.get(); - if (entry != null) { - return entry.getDuration(); - } else { - return -1; + if (entry != null) { + return entry.getDuration(); + } else { + return -1; + } } + return -1; } /** @@ -414,7 +428,7 @@ public String toString() { * @return 字符串表示的entry */ private String toString(String prefix1, String prefix2) { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); toString(buffer, prefix1, prefix2); @@ -428,7 +442,7 @@ private String toString(String prefix1, String prefix2) { * @param prefix1 首行前缀 * @param prefix2 后续行前缀 */ - private void toString(StringBuffer buffer, String prefix1, String prefix2) { + private void toString(StringBuilder buffer, String prefix1, String prefix2) { buffer.append(prefix1); String message = getMessage(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java index 38c5d1e3..ad0b1705 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/AESUtils.java @@ -19,6 +19,7 @@ package com.mpush.tools.crypto; +import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,6 +63,7 @@ public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { public static byte[] encrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { + Profiler.enter("time cost on [aes encrypt]: data length=" + data.length); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); @@ -70,11 +72,14 @@ public static byte[] encrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec Arrays.toString(zeroIv.getIV()), Arrays.toString(keySpec.getEncoded()), e); throw new CryptoException("AES encrypt ex", e); + } finally { + Profiler.release(); } } public static byte[] decrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { + Profiler.enter("time cost on [aes decrypt]: data length=" + data.length); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.DECRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); @@ -83,6 +88,8 @@ public static byte[] decrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec Arrays.toString(zeroIv.getIV()), Arrays.toString(keySpec.getEncoded()), e); throw new CryptoException("AES decrypt ex", e); + } finally { + Profiler.release(); } } } From 3615417e400ac613b4db448062e25981b7d3028d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 13:00:49 +0800 Subject: [PATCH 691/890] =?UTF-8?q?=E5=8A=A0=E5=85=A5javassist=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-tools/pom.xml | 4 ++++ pom.xml | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 68179693..ea8d1190 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -80,5 +80,9 @@ org.apache.commons commons-collections4 + + org.javassist + javassist + diff --git a/pom.xml b/pom.xml index 21dc1c81..0937e38b 100644 --- a/pom.xml +++ b/pom.xml @@ -200,6 +200,13 @@ + + + junit + junit + 4.10 + test + org.apache.commons commons-lang3 @@ -245,19 +252,18 @@ config 1.3.0 - - - junit - junit - 4.10 - test - org.apache.commons commons-collections4 4.1 + + + org.javassist + javassist + 3.21.0-GA + From aa3a980ab7b95a8d2e88065eec0fbc09e4f9a5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 13:01:26 +0800 Subject: [PATCH 692/890] =?UTF-8?q?shutdown=20=E5=91=BD=E4=BB=A4=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/core/handler/AdminHandler.java | 48 +++++++------------ .../com/mpush/core/server/AdminServer.java | 10 ---- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 9f658981..1a16c044 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -53,13 +53,12 @@ public final class AdminHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LoggerFactory.getLogger(AdminHandler.class); - private static final String EOL = "\r\n"; private static AdminServer adminServer; public AdminHandler(AdminServer adminServer) { - this.adminServer = adminServer; + AdminHandler.adminServer = adminServer; } @Override @@ -106,21 +105,19 @@ public enum Command { help { @Override public String handler(ChannelHandlerContext ctx, String args) { - StringBuilder buf = new StringBuilder(); - buf.append("Option Description" + EOL); - buf.append("------ -----------" + EOL); - buf.append("help show help" + EOL); - buf.append("quit exit console mode" + EOL); - buf.append("shutdown stop mpush server" + EOL); - buf.append("restart restart mpush server" + EOL); - buf.append("zk: query zk node" + EOL); - buf.append("count: count conn num or online user count" + EOL); - buf.append("route: show user route info" + EOL); - buf.append("push:, push test msg to client" + EOL); - buf.append("conf:[key] show config info" + EOL); - buf.append("monitor:[mxBean] show system monitor" + EOL); - buf.append("profile:<1,0> enable/disable profile" + EOL); - return buf.toString(); + return "Option Description" + EOL + + "------ -----------" + EOL + + "help show help" + EOL + + "quit exit console mode" + EOL + + "shutdown stop mpush server" + EOL + + "restart restart mpush server" + EOL + + "zk: query zk node" + EOL + + "count: count conn num or online user count" + EOL + + "route: show user route info" + EOL + + "push:, push test msg to client" + EOL + + "conf:[key] show config info" + EOL + + "monitor:[mxBean] show system monitor" + EOL + + "profile:<1,0> enable/disable profile" + EOL; } }, quit { @@ -132,21 +129,8 @@ public String handler(ChannelHandlerContext ctx, String args) { shutdown { @Override public String handler(ChannelHandlerContext ctx, String args) { - ctx.writeAndFlush("try close connect server..."); - adminServer.getConnectionServer().stop(new Listener() { - @Override - public void onSuccess(Object... args) { - ctx.writeAndFlush("connect server close success" + EOL); - adminServer.stop(null);//这个一定要在System.exit之前调用,不然jvm 会卡死 @see com.mpush.bootstrap.Main#addHook - System.exit(0); - } - - @Override - public void onFailure(Throwable cause) { - ctx.writeAndFlush("connect server close failure, msg=" + cause.getLocalizedMessage()); - } - }); - return null; + new Thread(() -> System.exit(0)).start(); + return "try close connect server..."; } }, restart { diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index 589d7474..eae85738 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -50,16 +50,6 @@ protected void initPipeline(ChannelPipeline pipeline) { super.initPipeline(pipeline); } - @Override - protected Executor getBossExecutor() { - return ThreadPoolManager.I.getWorkExecutor(); - } - - @Override - protected Executor getWorkExecutor() { - return ThreadPoolManager.I.getWorkExecutor(); - } - @Override public ChannelHandler getChannelHandler() { return adminHandler; From 998d008a720adf333a8940720e21b7a9c75e447e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 13:02:40 +0800 Subject: [PATCH 693/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0netty=E4=BC=98?= =?UTF-8?q?=E5=8C=96jvm=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/set-env.sh | 4 ++-- .../src/test/java/com/mpush/test/sever/ServerTestMain.java | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/set-env.sh b/bin/set-env.sh index f619ad47..eb607aae 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -#io.netty.leakDetectionLevel +#io.netty.leakDetection.level #netty的内存泄露检测分为四级: #DISABLED: 不进行内存泄露的检测; #SIMPLE: 抽样检测,且只对部分方法调用进行记录,消耗较小,有泄漏时可能会延迟报告,默认级别; @@ -10,7 +10,7 @@ #Dio.netty.noKeySetOptimization #是否禁用nio Selector.selectedKeys优化, 通过反射实现, 默认false -JVM_FLAGS="-Dio.netty.leakDetectionLevel=advanced -Dio.netty.noKeySetOptimization=false" +JVM_FLAGS="-Dio.netty.leakDetection.level=advanced -Dio.netty.noKeySetOptimization=false" #开启远程调试 #JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java index 199de67d..672bc195 100644 --- a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -28,6 +28,8 @@ */ public class ServerTestMain { public static void main(String[] args) { + System.setProperty("io.netty.leakDetection.level", "PARANOID"); + System.setProperty("io.netty.noKeySetOptimization", "false"); Main.main(args); } } From 04eda0928f1d981d2ba41af66bc96f7ca1ecf881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 13:26:23 +0800 Subject: [PATCH 694/890] add log --- Changelog.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Changelog.md b/Changelog.md index 0496e93c..4578ea58 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,25 @@ +#### v0.0.5: + +1. zk client 代码优化,修改临时节点重复注册问题,增加DNS ZK Node +2. 绑定用户调整,校验重复绑定,以及未解绑,就绑定的场景 +3. 脚步修改start-foreground不能加载配置项bug, 修改windows启动命令bug +4. 修复ACK超时方法调用错误bug,增加ack超时时间设置 +5. redis 增加redis3.x集群支持 +6. 解码优化, 不再抛出解码异常,取消循环解码 +7. Netty Server 增加IoRate设置,优雅停止流程优化,先关闭main reactor +8. 心跳优化,连接建立后就开始计算心跳 +9. sessionId生成器性能优化,采用jdk8 LongAdder +10. Service模块开始使用java8 CompletableFuture +11. 全网推送增加按标签过滤推送用户 +12. 增加流量控制,tcp发送缓冲区检测代码优化 +13. 新增client push上行功能, 并支持用户以SPI方式集成自己的Handler +14. SPI模块优化增加@Spi注解,多个实现可以指定顺序 +15. Profile 性能分析模块优化,增加性能监控开关配置,加入javassist优化性能 +16. 其他bug fix + + + + #### v0.0.4: 1. push client API 调整 From 47c6286939511804c842d28cdc4dcec2d3c5a219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 14:27:28 +0800 Subject: [PATCH 695/890] add log --- Changelog.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Changelog.md b/Changelog.md index 4578ea58..ae3b1b62 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,20 +1,20 @@ #### v0.0.5: -1. zk client 代码优化,修改临时节点重复注册问题,增加DNS ZK Node +1. redis 增加redis3.x集群支持, 配置项不再兼容 2. 绑定用户调整,校验重复绑定,以及未解绑,就绑定的场景 -3. 脚步修改start-foreground不能加载配置项bug, 修改windows启动命令bug -4. 修复ACK超时方法调用错误bug,增加ack超时时间设置 -5. redis 增加redis3.x集群支持 -6. 解码优化, 不再抛出解码异常,取消循环解码 -7. Netty Server 增加IoRate设置,优雅停止流程优化,先关闭main reactor -8. 心跳优化,连接建立后就开始计算心跳 -9. sessionId生成器性能优化,采用jdk8 LongAdder -10. Service模块开始使用java8 CompletableFuture -11. 全网推送增加按标签过滤推送用户 -12. 增加流量控制,tcp发送缓冲区检测代码优化 -13. 新增client push上行功能, 并支持用户以SPI方式集成自己的Handler -14. SPI模块优化增加@Spi注解,多个实现可以指定顺序 -15. Profile 性能分析模块优化,增加性能监控开关配置,加入javassist优化性能 +3. 新增client push上行功能, 并支持用户以SPI方式集成自己的Handler +4. 全网推送增加按标签过滤推送用户 +5. 增加流量控制,tcp发送缓冲区检测代码优化 +6. 修复ACK超时方法调用错误bug,增加ack超时时间设置 +7. 解码优化, 不再抛出解码异常,取消循环解码 +8. NettyServer 增加IoRate设置,优雅停止流程优化,先关闭main reactor +9. 心跳优化,连接建立后就开始计算心跳 +10. sessionId生成器性能优化,采用jdk8 LongAdder +11. Service模块开始使用java8 CompletableFuture +12. SPI模块优化增加@Spi注解,多个实现可以指定顺序 +13. Profile 性能分析模块优化,增加性能监控开关配置,加入javassist优化性能 +14. zk client 代码优化,修改临时节点重复注册问题,增加DNS ZK Node +15. 脚步修改start-foreground不能加载配置项bug, 修改windows启动命令bug 16. 其他bug fix From 3059d31e27fcd32f0eb77b76a9a3e9091afca462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 15:38:51 +0800 Subject: [PATCH 696/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E5=88=86=E6=9E=90=E4=B8=8E=E7=9B=91=E6=8E=A7=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/core/handler/HttpProxyHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index b7fca21e..567e451f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -85,12 +85,13 @@ public void handle(HttpRequestMessage message) { //2.url转换 uri = doDnsMapping(uri); + Profiler.enter("time cost on [create FullHttpRequest]"); //3.包装成HTTP request FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.valueOf(method), uri); setHeaders(request, message);//处理header setBody(request, message);//处理body - Profiler.enter("time cost on [HttpClient]"); + Profiler.enter("time cost on [HttpClient.request]"); //4.发送请求 httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { From c3e78e181ec1f133992d55ec6fb857ed56c2d5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 16:25:24 +0800 Subject: [PATCH 697/890] =?UTF-8?q?restart=20sleep=20=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=BC=A9=E7=9F=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/mp.sh b/bin/mp.sh index 47326483..44c07f88 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -227,7 +227,7 @@ upgrade) restart) shift "$0" stop ${@} - sleep 5 + sleep 1 "$0" start ${@} ;; status) From 154fd96bdb58f00410650728b58ee8c7db0af1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 15 Oct 2016 18:54:36 +0800 Subject: [PATCH 698/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwindows=20=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.cmd | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/mp.cmd b/bin/mp.cmd index ffcabd63..c7918f44 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -15,10 +15,11 @@ REM See the License for the specific language governing permissions and REM limitations under the License. setlocal + call "%~dp0env-mp.cmd" -set MPMAIN="-jar %~dp0bootstrap.jar" -echo on +set MPMAIN=-jar %~dp0bootstrap.jar + call %JAVA% "-Dmp.conf=%MPCFG%" "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% %* endlocal From 8c6382f35f12fe4f2a36a295db881e0d664b9626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 18 Oct 2016 20:16:29 +0800 Subject: [PATCH 699/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=89=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E8=BF=87=E6=BB=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=A0=87=E7=AD=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/client/push/PushRequest.java | 10 ++++++++++ .../common/message/gateway/GatewayPushMessage.java | 5 +++++ .../java/com/mpush/core/handler/BindUserHandler.java | 1 + .../com/mpush/core/handler/GatewayPushHandler.java | 11 +++++++++-- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 2 +- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index dfbc2b61..0a747c30 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -31,6 +31,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -54,6 +55,7 @@ private enum Status {init, success, failure, offline, timeout} private final PushClient client; private AckModel ackModel; + private Set tags; private PushCallback callback; private String userId; private byte[] content; @@ -92,6 +94,7 @@ private void sendToConnServer(RemoteRouter remoteRouter) { new GatewayPushMessage(userId, content, gatewayConn) .setClientType(location.getClientType()) .setTimeout(timeout - 500) + .setTags(tags) .addFlag(ackModel.flag); pushMessage.sendRaw(f -> { @@ -163,6 +166,7 @@ public FutureTask broadcast() { timeLine.begin(); client.getAllConnections().forEach(conn -> { GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, conn) + .setTags(tags) .addFlag(ackModel.flag); pushMessage.sendRaw(f -> { @@ -208,6 +212,7 @@ public static PushRequest build(PushClient client, PushContext ctx) { return new PushRequest(client) .setAckModel(ctx.getAckModel()) .setUserId(ctx.getUserId()) + .setTags(ctx.getTags()) .setContent(content) .setTimeout(ctx.getTimeout()) .setCallback(ctx.getCallback()); @@ -239,6 +244,11 @@ public PushRequest setAckModel(AckModel ackModel) { return this; } + public PushRequest setTags(Set tags) { + this.tags = tags; + return this; + } + @Override public String toString() { return "PushRequest{" + diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 483ebf03..81ce00b5 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -98,6 +98,11 @@ public GatewayPushMessage setTimeout(int timeout) { return this; } + public GatewayPushMessage setTags(Set tags) { + this.tags = tags; + return this; + } + public boolean isBroadcast() { return userId == null; } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 069e956e..25b0c86f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -72,6 +72,7 @@ private void bind(BindUserMessage message) { //处理重复绑定问题 if (context.userId != null) { if (message.userId.equals(context.userId)) { + context.tags = message.tags; OkMessage.from(message).setData("bind success").sendRaw(); return; } else { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 654678ba..00759c8d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -116,7 +116,14 @@ private void sendBroadcast(GatewayPushMessage message) { //2.按标签过滤,目前只有include,后续会增加exclude String tags = connection.getSessionContext().tags; if (tagList != null && tags != null) { - if (tagList.stream().noneMatch(tags::contains)) break; + if (tagList.stream().noneMatch(tags::contains)) { + tasks.decrementAndGet(); + if (tasks.get() == 0) {//任务全部结束 + Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); + } + continue; + } } if (connection.isConnected()) { @@ -124,7 +131,7 @@ private void sendBroadcast(GatewayPushMessage message) { if (connection.getChannel().isWritable()) {//检测TCP缓冲区是否已满且写队列超过最高阀值 //3.链接可用,直接下发消息到手机客户端 PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.getPacket().flags = message.getPacket().flags; + //pushMessage.getPacket().flags = message.getPacket().flags; pushMessage.send(future -> { diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index bcec88ce..5f17aac5 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -72,9 +72,9 @@ protected void doStart(Listener listener) throws Throwable { } initLocalCache(zkConfig.getLocalCachePath()); addConnectionStateListener(); - listener.onSuccess(zkConfig.getHosts()); Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); Logs.Console.info("init zk client success..."); + listener.onSuccess(zkConfig.getHosts()); } @Override From bb45ecaf7e991648d7f4b965bb2fba4077cc160f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 19 Oct 2016 17:10:59 +0800 Subject: [PATCH 700/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96add?= =?UTF-8?q?=20TooLongFrameException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/netty/codec/PacketDecoder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 8b49a0b4..fc5c1929 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -24,6 +24,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.TooLongFrameException; import java.util.List; @@ -87,7 +88,7 @@ private Packet readPacket(ByteBuf in, int bodyLength) { //read body if (bodyLength > 0) { if (bodyLength > maxPacketSize) { - throw new RuntimeException("error packet body length over limit:" + bodyLength); + throw new TooLongFrameException("packet body length over limit:" + bodyLength); } in.readBytes(packet.body = new byte[bodyLength]); } From 84be2937c5a42a2d7ec6225e6d0ffc40d8467a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 19 Oct 2016 17:11:44 +0800 Subject: [PATCH 701/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96,?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 1 + mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java | 1 + 2 files changed, 2 insertions(+) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 5f17aac5..1e5085c2 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -82,6 +82,7 @@ protected void doStop(Listener listener) throws Throwable { if (cache != null) cache.close(); TimeUnit.MILLISECONDS.sleep(600); client.close(); + Logs.Console.info("zk client closed..."); } /** diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java index 443f1e07..6da5d700 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKDnsNode.java @@ -33,6 +33,7 @@ public class ZKDnsNode extends DnsMapping implements ZKNode { public ZKDnsNode() { } + //ip 可以通过Utils.getInetAddress(true)获取。 public ZKDnsNode(String origin, String ip, int port) { super(ip, port); this.origin = origin; From 57fb81d9cb2672fc171c70a7de2d514e13e4eb5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 19 Oct 2016 17:22:51 +0800 Subject: [PATCH 702/890] =?UTF-8?q?SPI=E6=A8=A1=E5=9D=97=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0ServerEventListener,=20BindValidator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/ServerEventListener.java | 48 +++++++++++++++++++ .../mpush/api/event/ServerShutdownEvent.java | 28 +++++++++++ .../mpush/api/event/ServerStartupEvent.java | 28 +++++++++++ .../java/com/mpush/api/push/PushSender.java | 5 +- .../java/com/mpush/api/spi/SpiLoader.java | 1 + .../mpush/api/spi/client/PusherFactory.java | 5 +- .../com/mpush/api/spi/core/CipherFactory.java | 5 +- .../spi/core/ServerEventListenerFactory.java | 35 ++++++++++++++ .../mpush/api/spi/handler/BindValidator.java | 29 +++++++++++ .../api/spi/handler/BindValidatorFactory.java | 34 +++++++++++++ .../api/spi/handler/PushHandlerFactory.java | 6 ++- .../com/mpush/bootstrap/job/BootChain.java | 20 +------- .../com/mpush/bootstrap/job/FirstJob.java | 47 ++++++++++++++++++ .../mpush/bootstrap/job/HttpProxyBoot.java | 7 +-- .../com/mpush/bootstrap/job/LastBoot.java | 7 ++- .../mpush/core/handler/BindUserHandler.java | 20 ++++++-- .../mpush/core/handler/HttpProxyHandler.java | 5 +- .../server/DefaultServerEventListener.java | 38 +++++++++++++++ ...sh.api.spi.core.ServerEventListenerFactory | 1 + ...mpush.api.spi.handler.BindValidatorFactory | 1 + .../netty/connection/NettyConnection.java | 8 ++-- .../com.mpush.api.spi.common.ExecutorFactory | 1 + ...com.mpush.api.spi.common.ThreadPoolFactory | 1 - 23 files changed, 336 insertions(+), 44 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/ServerEventListener.java create mode 100644 mpush-api/src/main/java/com/mpush/api/event/ServerShutdownEvent.java create mode 100644 mpush-api/src/main/java/com/mpush/api/event/ServerStartupEvent.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/core/ServerEventListenerFactory.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidator.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidatorFactory.java create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java create mode 100644 mpush-core/src/main/java/com/mpush/core/server/DefaultServerEventListener.java create mode 100644 mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.core.ServerEventListenerFactory create mode 100644 mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.BindValidatorFactory create mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ExecutorFactory delete mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory diff --git a/mpush-api/src/main/java/com/mpush/api/ServerEventListener.java b/mpush-api/src/main/java/com/mpush/api/ServerEventListener.java new file mode 100644 index 00000000..171b8e05 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/ServerEventListener.java @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api; + +import com.mpush.api.event.*; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +public interface ServerEventListener { + + default void on(ServerStartupEvent event) { + } + + default void on(ServerShutdownEvent server) { + } + + default void on(RouterChangeEvent event) { + } + + default void on(KickUserEvent event) { + } + + default void on(UserOnlineEvent event) { + } + + default void on(UserOfflineEvent event) { + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/event/ServerShutdownEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ServerShutdownEvent.java new file mode 100644 index 00000000..4a4e1d29 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/event/ServerShutdownEvent.java @@ -0,0 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.event; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +public final class ServerShutdownEvent implements Event { +} diff --git a/mpush-api/src/main/java/com/mpush/api/event/ServerStartupEvent.java b/mpush-api/src/main/java/com/mpush/api/event/ServerStartupEvent.java new file mode 100644 index 00000000..1ffd3a79 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/event/ServerStartupEvent.java @@ -0,0 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.event; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +public final class ServerStartupEvent implements Event { +} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java index 03c015f5..ad8aa842 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -19,12 +19,9 @@ package com.mpush.api.push; -import com.mpush.api.protocol.Packet; import com.mpush.api.service.Service; -import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.client.PusherFactory; -import java.util.Collection; import java.util.concurrent.FutureTask; /** @@ -40,7 +37,7 @@ public interface PushSender extends Service { * @return PushSender */ static PushSender create() { - return SpiLoader.load(PusherFactory.class).get(); + return PusherFactory.create(); } /** diff --git a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index e93c24af..43699ea3 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -33,6 +33,7 @@ public static T load(Class clazz) { return load(clazz, null); } + @SuppressWarnings("unchecked") public static T load(Class clazz, String name) { String key = clazz.getName(); Object o = CACHE.get(key); diff --git a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java index 6abcd77c..cf8ab3b1 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/client/PusherFactory.java @@ -21,6 +21,7 @@ import com.mpush.api.push.PushSender; import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; /** * Created by yxx on 2016/5/18. @@ -28,5 +29,7 @@ * @author ohun@live.cn */ public interface PusherFactory extends Factory { - + static PushSender create() { + return SpiLoader.load(PusherFactory.class).get(); + } } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java index b3a486b9..c151eaa7 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/core/CipherFactory.java @@ -21,6 +21,7 @@ import com.mpush.api.connection.Cipher; import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; /** * Created by yxx on 2016/5/19. @@ -28,5 +29,7 @@ * @author ohun@live.cn */ public interface CipherFactory extends Factory { - Cipher get(); + static Cipher create() { + return SpiLoader.load(CipherFactory.class).get(); + } } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/core/ServerEventListenerFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/core/ServerEventListenerFactory.java new file mode 100644 index 00000000..0cdb40bf --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/core/ServerEventListenerFactory.java @@ -0,0 +1,35 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.core; + +import com.mpush.api.ServerEventListener; +import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +public interface ServerEventListenerFactory extends Factory { + static ServerEventListener create() { + return SpiLoader.load(ServerEventListenerFactory.class).get(); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidator.java b/mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidator.java new file mode 100644 index 00000000..448672ae --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidator.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.handler; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +public interface BindValidator { + boolean validate(String userId); +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidatorFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidatorFactory.java new file mode 100644 index 00000000..4d9f3c34 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/handler/BindValidatorFactory.java @@ -0,0 +1,34 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.handler; + +import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +public interface BindValidatorFactory extends Factory { + static BindValidator create() { + return SpiLoader.load(BindValidatorFactory.class).get(); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java index 5c2cd919..9452940f 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/handler/PushHandlerFactory.java @@ -21,11 +21,15 @@ import com.mpush.api.MessageHandler; import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; /** * Created by ohun on 16/10/14. * * @author ohun@live.cn (夜色) */ -public interface PushHandlerFactory extends Factory { +public interface PushHandlerFactory extends Factory { + static MessageHandler create() { + return SpiLoader.load(PushHandlerFactory.class).get(); + } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index e73f11ed..6960fccd 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -19,15 +19,13 @@ package com.mpush.bootstrap.job; -import com.mpush.tools.log.Logs; - /** * Created by yxx on 2016/5/15. * * @author ohun@live.cn */ public final class BootChain { - private final BootJob first = first(); + private final BootJob first = new FirstJob(); public void start() { first.start(); @@ -41,22 +39,6 @@ public static BootChain chain() { return new BootChain(); } - private BootJob first() { - return new BootJob() { - @Override - public void start() { - Logs.Console.info("begin start bootstrap chain..."); - startNext(); - } - - @Override - protected void stop() { - Logs.Console.info("begin stop bootstrap chain..."); - stopNext(); - } - }; - } - public BootJob boot() { return first; } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java new file mode 100644 index 00000000..0e0b5e9e --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.spi.core.ServerEventListenerFactory; +import com.mpush.tools.log.Logs; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +/*package*/ final class FirstJob extends BootJob { + + public FirstJob() { + ServerEventListenerFactory.create(); + } + + @Override + public void start() { + Logs.Console.info("begin start bootstrap chain..."); + startNext(); + } + + @Override + protected void stop() { + Logs.Console.info("begin stop bootstrap chain..."); + stopNext(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index eec56ad6..2959d509 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -19,12 +19,9 @@ package com.mpush.bootstrap.job; -import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.tools.config.CC; -import static com.mpush.tools.config.CC.mp.spi.dns_mapping_manager; - /** * Created by yxx on 2016/5/15. * @@ -35,7 +32,7 @@ public final class HttpProxyBoot extends BootJob { @Override protected void start() { if (CC.mp.http.proxy_enabled) { - SpiLoader.load(DnsMappingManager.class, dns_mapping_manager).start(); + DnsMappingManager.create().start(); } startNext(); } @@ -43,7 +40,7 @@ protected void start() { @Override protected void stop() { if (CC.mp.http.proxy_enabled) { - SpiLoader.load(DnsMappingManager.class, dns_mapping_manager).stop(); + DnsMappingManager.create().stop(); } stopNext(); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java index 89b2fa24..c32ad6a2 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/LastBoot.java @@ -19,6 +19,9 @@ package com.mpush.bootstrap.job; +import com.mpush.api.event.ServerShutdownEvent; +import com.mpush.api.event.ServerStartupEvent; +import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; /** @@ -29,6 +32,7 @@ public final class LastBoot extends BootJob { @Override protected void start() { + EventBus.I.post(new ServerStartupEvent()); Logs.Console.info("end start bootstrap chain..."); Logs.Console.info("==================================================================="); Logs.Console.info("====================MPUSH SERVER START SUCCESS====================="); @@ -37,9 +41,10 @@ protected void start() { @Override protected void stop() { + EventBus.I.post(new ServerShutdownEvent()); Logs.Console.info("end stop bootstrap chain..."); Logs.Console.info("==================================================================="); - Logs.Console.info("====================MPUSH SERVER STOPPED SUCCESS====================="); + Logs.Console.info("====================MPUSH SERVER STOPPED SUCCESS==================="); Logs.Console.info("==================================================================="); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 25b0c86f..8946bc38 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -26,6 +26,9 @@ import com.mpush.api.event.UserOnlineEvent; import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; +import com.mpush.api.spi.Spi; +import com.mpush.api.spi.handler.BindValidator; +import com.mpush.api.spi.handler.BindValidatorFactory; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.BindUserMessage; import com.mpush.common.message.ErrorMessage; @@ -35,7 +38,6 @@ import com.mpush.core.router.LocalRouter; import com.mpush.core.router.LocalRouterManager; import com.mpush.core.router.RouterCenter; -import com.mpush.tools.common.Profiler; import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; @@ -45,6 +47,7 @@ * @author ohun@live.cn */ public final class BindUserHandler extends BaseMessageHandler { + private BindValidator validator = BindValidatorFactory.create(); @Override public BindUserMessage decode(Packet packet, Connection connection) { @@ -74,14 +77,14 @@ private void bind(BindUserMessage message) { if (message.userId.equals(context.userId)) { context.tags = message.tags; OkMessage.from(message).setData("bind success").sendRaw(); + Logs.Conn.info(">>> rebind user success, userId={}, session={}", message.userId, context); return; } else { unbind(message); } } - //2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份 - boolean success = RouterCenter.I.register(message.userId, message.getConnection()); + boolean success = validator.validate(message.userId) && RouterCenter.I.register(message.userId, message.getConnection()); if (success) { context.userId = message.userId; context.tags = message.tags; @@ -153,4 +156,15 @@ private void unbind(BindUserMessage message) { Logs.Conn.info("unbind user failure not handshake, userId={}, session={}", message.userId, context); } } + + + @Spi(order = 1) + public static class DefaultBindValidatorFactory implements BindValidatorFactory { + private final BindValidator validator = userId -> true; + + @Override + public BindValidator get() { + return validator; + } + } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 567e451f..f72e43ad 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -22,18 +22,15 @@ import com.google.common.base.Strings; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.net.DnsMapping; import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.HttpRequestMessage; import com.mpush.common.message.HttpResponseMessage; -import com.mpush.common.net.HttpProxyDnsMappingManager; import com.mpush.netty.http.HttpCallback; import com.mpush.netty.http.HttpClient; import com.mpush.netty.http.RequestContext; import com.mpush.tools.common.Profiler; -import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; @@ -55,7 +52,7 @@ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = Logs.HTTP; private final HttpClient httpClient; - private final DnsMappingManager dnsMappingManager = SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager); + private final DnsMappingManager dnsMappingManager = DnsMappingManager.create(); public HttpProxyHandler(HttpClient httpClient) { this.httpClient = httpClient; diff --git a/mpush-core/src/main/java/com/mpush/core/server/DefaultServerEventListener.java b/mpush-core/src/main/java/com/mpush/core/server/DefaultServerEventListener.java new file mode 100644 index 00000000..462ab08a --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/server/DefaultServerEventListener.java @@ -0,0 +1,38 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.server; + +import com.mpush.api.ServerEventListener; +import com.mpush.api.spi.Spi; +import com.mpush.api.spi.core.ServerEventListenerFactory; + +/** + * Created by ohun on 16/10/19. + * + * @author ohun@live.cn (夜色) + */ +@Spi(order = 1) +public final class DefaultServerEventListener implements ServerEventListener, ServerEventListenerFactory { + + @Override + public ServerEventListener get() { + return this; + } +} diff --git a/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.core.ServerEventListenerFactory b/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.core.ServerEventListenerFactory new file mode 100644 index 00000000..a5a2c8e1 --- /dev/null +++ b/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.core.ServerEventListenerFactory @@ -0,0 +1 @@ +com.mpush.core.server.DefaultServerEventListener \ No newline at end of file diff --git a/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.BindValidatorFactory b/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.BindValidatorFactory new file mode 100644 index 00000000..edbcba68 --- /dev/null +++ b/mpush-core/src/main/resources/META-INF/services/com.mpush.api.spi.handler.BindValidatorFactory @@ -0,0 +1 @@ +com.mpush.core.handler.BindUserHandler$DefaultBindValidatorFactory \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 7333762a..518d2c85 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -19,10 +19,10 @@ package com.mpush.netty.connection; +import com.mpush.api.connection.Cipher; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; -import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.core.CipherFactory; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; @@ -37,7 +37,7 @@ */ public final class NettyConnection implements Connection, ChannelFutureListener { private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class); - private static final CipherFactory factory = SpiLoader.load(CipherFactory.class); + private static final Cipher RSA_CIPHER = CipherFactory.create(); private SessionContext context; private Channel channel; private volatile int status = STATUS_NEW; @@ -50,8 +50,8 @@ public void init(Channel channel, boolean security) { this.context = new SessionContext(); this.lastReadTime = System.currentTimeMillis(); this.status = STATUS_CONNECTED; - if (security && factory != null) { - this.context.changeCipher(factory.get()); + if (security) { + this.context.changeCipher(RSA_CIPHER); } } diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ExecutorFactory b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ExecutorFactory new file mode 100644 index 00000000..611cc71c --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ExecutorFactory @@ -0,0 +1 @@ +com.mpush.tools.thread.pool.DefaultExecutorFactory \ No newline at end of file diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory deleted file mode 100644 index be69a4a6..00000000 --- a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.ThreadPoolFactory +++ /dev/null @@ -1 +0,0 @@ -com.mpush.tools.thread.pool.DefaultThreadPoolFactory \ No newline at end of file From cbc9bf825e045cff3e717f84a6d0d4e002d18a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 19 Oct 2016 17:24:29 +0800 Subject: [PATCH 703/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=8F=8A=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=BC=98=E5=8C=96=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 16 ++++++++-------- .../src/main/java/com/mpush/bootstrap/Main.java | 2 +- .../src/main/java/com/mpush/tools/log/Logs.java | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 731454a4..405419ae 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -28,7 +28,7 @@ mp { #安全配置 security { - #rsa 私钥, 公钥 key长度为1024;生成方式可以使用open-ssh或者使用工具类com.mpush.tools.crypto.RSAUtils#main + #rsa 私钥、公钥key长度为1024;可以使用脚本bin/rsa.sh生成, @see com.mpush.tools.crypto.RSAUtils#main private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" aes-key-length=16 //AES key 长度 @@ -47,9 +47,9 @@ mp { gateway-client { enabled:true check-interval:100ms - write-global-limit:1k + write-global-limit:30k read-global-limit:0 - write-channel-limit:256b + write-channel-limit:3k read-channel-limit:0 } @@ -57,9 +57,9 @@ mp { enabled:true check-interval:100ms write-global-limit:0 - read-global-limit:10k + read-global-limit:30k write-channel-limit:0 - read-channel-limit:0.5k + read-channel-limit:3k } connect-server { @@ -135,13 +135,13 @@ mp { thread { pool { boss { //netty boss - min:4 - max:16 + min:2 + max:8 queue-size:1000 } work { //netty boss - min:8 + min:4 max:32 queue-size:1000 } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index a9bc77f1..55048fe4 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -39,7 +39,7 @@ private static void addHook(ServerLauncher launcher) { } catch (Exception e) { Logs.Console.error("mpush server stop ex", e); } - Logs.Console.info("jvm exit, all service stopped..."); + Logs.Console.info("jvm exit, all service stopped."); }, "mpush-shutdown-hook-thread") ); diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index f53b236a..352c4fe4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -46,7 +46,7 @@ static boolean init() { Conn = LoggerFactory.getLogger("mpush.conn.log"), - Monitor = LoggerFactory.getLogger("mpush.monitor.log"), + MONITOR = LoggerFactory.getLogger("mpush.monitor.log"), PUSH = LoggerFactory.getLogger("mpush.push.log"), From fe4f5f23f86cbb369715b342814fced4e1ace3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 19 Oct 2016 17:25:16 +0800 Subject: [PATCH 704/890] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C=E7=BA=BF=E7=A8=8B=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...dPoolFactory.java => ExecutorFactory.java} | 8 ++++- .../cache/redis/manager/RedisManager.java | 5 +-- .../connect/ConnClientChannelHandler.java | 4 +-- .../mpush/client/gateway/GatewayClient.java | 10 ++++-- .../com/mpush/client/push/PushRequestBus.java | 4 +-- .../net/HttpProxyDnsMappingManager.java | 2 +- .../com/mpush/core/ack/AckMessageQueue.java | 12 ++----- .../mpush/core/server/ConnectionServer.java | 11 +++++-- .../com/mpush/core/server/GatewayServer.java | 8 ++++- .../mpush/monitor/service/MonitorService.java | 32 +++++++++++-------- .../java/com/mpush/tools/common/JVMUtil.java | 23 +++++++------ ...ctory.java => NamedPoolThreadFactory.java} | 10 +++--- .../tools/thread/NamedThreadFactory.java | 12 +++++-- .../com/mpush/tools/thread/ThreadNames.java | 4 ++- .../tools/thread/pool/DefaultExecutor.java | 6 +++- ...ctory.java => DefaultExecutorFactory.java} | 12 +++---- .../tools/thread/pool/ThreadPoolConfig.java | 2 +- .../tools/thread/pool/ThreadPoolManager.java | 21 ++++++------ 18 files changed, 111 insertions(+), 75 deletions(-) rename mpush-api/src/main/java/com/mpush/api/spi/common/{ThreadPoolFactory.java => ExecutorFactory.java} (85%) rename mpush-tools/src/main/java/com/mpush/tools/thread/{PoolThreadFactory.java => NamedPoolThreadFactory.java} (84%) rename mpush-tools/src/main/java/com/mpush/tools/thread/pool/{DefaultThreadPoolFactory.java => DefaultExecutorFactory.java} (93%) diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java similarity index 85% rename from mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java rename to mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java index 84d394cb..8aa7c53f 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/common/ThreadPoolFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java @@ -19,6 +19,8 @@ package com.mpush.api.spi.common; +import com.mpush.api.spi.SpiLoader; + import java.util.concurrent.Executor; /** @@ -26,7 +28,7 @@ * * @author ohun@live.cn */ -public interface ThreadPoolFactory { +public interface ExecutorFactory { String SERVER_BOSS = "sb"; String SERVER_WORK = "sw"; String HTTP_CLIENT_WORK = "hcw"; @@ -36,4 +38,8 @@ public interface ThreadPoolFactory { String BIZ = "b"; Executor get(String name); + + static ExecutorFactory create() { + return SpiLoader.load(ExecutorFactory.class); + } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 266ca9fc..751202b5 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -24,6 +24,7 @@ import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; import redis.clients.jedis.*; import java.util.*; @@ -307,9 +308,9 @@ public void publish(String channel, T message) { } public void subscribe(final JedisPubSub pubsub, final String... channels) { - new Thread(() -> call(jedis -> + ThreadPoolManager.I.newThread(Arrays.toString(channels), (() -> call(jedis -> Arrays.stream(channels).forEach(channel -> ((MultiKeyCommands) jedis).subscribe(pubsub, channel)) - ), Arrays.toString(channels)).start(); + ))).start(); } /********************* diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index d937bcf9..7691e3eb 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -33,7 +33,7 @@ import com.mpush.common.security.CipherBox; import com.mpush.netty.connection.NettyConnection; import com.mpush.tools.event.EventBus; -import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.ThreadNames; import io.netty.channel.*; import io.netty.util.HashedWheelTimer; @@ -55,7 +55,7 @@ @ChannelHandler.Sharable public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); - private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new PoolThreadFactory(ThreadNames.T_NETTY_TIMER)); + private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedPoolThreadFactory(ThreadNames.T_NETTY_TIMER)); private final Connection connection = new NettyConnection(); private final ClientConfig clientConfig; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java index 4b423d04..e248c7bd 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -22,13 +22,16 @@ import com.mpush.api.connection.Connection; import com.mpush.api.service.Listener; import com.mpush.netty.client.NettyClient; +import com.mpush.tools.thread.NamedPoolThreadFactory; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_client.*; +import static com.mpush.tools.thread.ThreadNames.T_TRAFFIC_SHAPING; /** * Created by yxx on 2016/5/17. @@ -38,13 +41,15 @@ public class GatewayClient extends NettyClient { private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); private GlobalChannelTrafficShapingHandler trafficShapingHandler; + private ScheduledExecutorService trafficShapingExecutor; public GatewayClient(String host, int port) { super(host, port); if (enabled) { + trafficShapingExecutor = Executors.newSingleThreadScheduledExecutor(new NamedPoolThreadFactory(T_TRAFFIC_SHAPING)); trafficShapingHandler = new GlobalChannelTrafficShapingHandler( - Executors.newSingleThreadScheduledExecutor() - , write_global_limit, read_global_limit, + trafficShapingExecutor, + write_global_limit, read_global_limit, write_channel_limit, read_channel_limit, check_interval); } @@ -71,6 +76,7 @@ protected void initPipeline(ChannelPipeline pipeline) { protected void doStop(Listener listener) throws Throwable { if (trafficShapingHandler != null) { trafficShapingHandler.release(); + trafficShapingExecutor.shutdown(); } super.doStop(listener); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 28c444c7..54e7155d 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -22,7 +22,7 @@ import com.mpush.api.push.PushException; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; -import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,7 +63,7 @@ public void asyncCall(Runnable runnable) { @Override protected void doStart(Listener listener) throws Throwable { executor = ThreadPoolManager.I.getPushCallbackExecutor(); - scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { + scheduledExecutor = new ScheduledThreadPoolExecutor(1, new NamedPoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { logger.error("one push request was rejected, request=" + r); throw new PushException("one push request was rejected. request=" + r); }); diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index 594ecf1c..657f987e 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -106,7 +106,7 @@ public DnsMapping lookup(String origin) { @Override public void run() { - logger.debug("start dns mapping checkHealth"); + logger.debug("do dns mapping checkHealth ..."); Map> all = this.getAll(); Map> available = Maps.newConcurrentMap(); all.forEach((key, dnsMappings) -> { diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java index a3282539..098edc72 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java @@ -19,20 +19,12 @@ package com.mpush.core.ack; -import com.mpush.api.protocol.Packet; -import com.mpush.api.push.PushException; -import com.mpush.common.ErrorCode; -import com.mpush.common.message.ErrorMessage; -import com.mpush.common.message.OkMessage; -import com.mpush.tools.log.Logs; -import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.NamedPoolThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.*; -import static com.mpush.api.protocol.Command.ERROR; -import static com.mpush.api.protocol.Command.OK; import static com.mpush.tools.thread.ThreadNames.T_ARK_REQ_TIMER; /** @@ -49,7 +41,7 @@ public final class AckMessageQueue { private final ScheduledExecutorService scheduledExecutor; private AckMessageQueue() { - scheduledExecutor = new ScheduledThreadPoolExecutor(1, new PoolThreadFactory(T_ARK_REQ_TIMER), (r, e) -> { + scheduledExecutor = new ScheduledThreadPoolExecutor(1, new NamedPoolThreadFactory(T_ARK_REQ_TIMER), (r, e) -> { logger.error("one ack context was rejected, context=" + r); }); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index a1284bde..1f8db861 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -23,7 +23,6 @@ import com.mpush.api.connection.ConnectionManager; import com.mpush.api.protocol.Command; import com.mpush.api.service.Listener; -import com.mpush.api.spi.SpiLoader; import com.mpush.api.spi.handler.PushHandlerFactory; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.*; @@ -31,6 +30,7 @@ import com.mpush.netty.http.NettyHttpClient; import com.mpush.netty.server.NettyServer; import com.mpush.tools.config.CC; +import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; @@ -41,8 +41,10 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import static com.mpush.tools.config.CC.mp.net.traffic_shaping.connect_server.*; +import static com.mpush.tools.thread.ThreadNames.T_TRAFFIC_SHAPING; /** * Created by ohun on 2015/12/30. @@ -52,6 +54,7 @@ public final class ConnectionServer extends NettyServer { private ServerChannelHandler channelHandler; private GlobalChannelTrafficShapingHandler trafficShapingHandler; + private ScheduledExecutorService trafficShapingExecutor; private ConnectionManager connectionManager = new ServerConnectionManager(true); private HttpClient httpClient; @@ -70,7 +73,7 @@ public void init() { receiver.register(Command.BIND, new BindUserHandler()); receiver.register(Command.UNBIND, new BindUserHandler()); receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); - receiver.register(Command.PUSH, SpiLoader.load(PushHandlerFactory.class).get()); + receiver.register(Command.PUSH, PushHandlerFactory.create()); receiver.register(Command.ACK, new AckHandler()); if (CC.mp.http.proxy_enabled) { @@ -80,8 +83,9 @@ public void init() { channelHandler = new ServerChannelHandler(true, connectionManager, receiver); if (CC.mp.net.traffic_shaping.connect_server.enabled) {//启用流量整形,限流 + trafficShapingExecutor = Executors.newSingleThreadScheduledExecutor(new NamedPoolThreadFactory(T_TRAFFIC_SHAPING)); trafficShapingHandler = new GlobalChannelTrafficShapingHandler( - Executors.newSingleThreadScheduledExecutor(), + trafficShapingExecutor, write_global_limit, read_global_limit, write_channel_limit, read_channel_limit, check_interval); @@ -92,6 +96,7 @@ public void init() { public void stop(Listener listener) { if (trafficShapingHandler != null) { trafficShapingHandler.release(); + trafficShapingExecutor.shutdown(); } super.stop(listener); if (httpClient != null && httpClient.isRunning()) { diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 0a41c29c..8632054b 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -25,6 +25,7 @@ import com.mpush.core.handler.GatewayPushHandler; import com.mpush.netty.server.NettyServer; import com.mpush.tools.config.CC; +import com.mpush.tools.thread.NamedPoolThreadFactory; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -33,8 +34,10 @@ import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import static com.mpush.tools.config.CC.mp.net.traffic_shaping.gateway_server.*; +import static com.mpush.tools.thread.ThreadNames.T_TRAFFIC_SHAPING; /** * Created by ohun on 2015/12/30. @@ -46,6 +49,7 @@ public final class GatewayServer extends NettyServer { private ServerChannelHandler channelHandler; private ServerConnectionManager connectionManager; private GlobalChannelTrafficShapingHandler trafficShapingHandler; + private ScheduledExecutorService trafficShapingExecutor; public GatewayServer(int port) { super(port); @@ -60,8 +64,9 @@ public void init() { channelHandler = new ServerChannelHandler(false, connectionManager, receiver); if (CC.mp.net.traffic_shaping.gateway_server.enabled) {//启用流量整形,限流 + trafficShapingExecutor = Executors.newSingleThreadScheduledExecutor(new NamedPoolThreadFactory(T_TRAFFIC_SHAPING)); trafficShapingHandler = new GlobalChannelTrafficShapingHandler( - Executors.newSingleThreadScheduledExecutor(), + trafficShapingExecutor, write_global_limit, read_global_limit, write_channel_limit, read_channel_limit, check_interval); @@ -72,6 +77,7 @@ public void init() { public void stop(Listener listener) { if (trafficShapingHandler != null) { trafficShapingHandler.release(); + trafficShapingExecutor.shutdown(); } super.stop(listener); if (connectionManager != null) { diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java index 3ce12611..ed784171 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -27,6 +27,7 @@ import com.mpush.tools.common.JVMUtil; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,27 +37,32 @@ public class MonitorService extends BaseService implements Runnable { private final Logger logger = LoggerFactory.getLogger(this.getClass()); public static final MonitorService I = new MonitorService(); - private static final int firstJstack = 2, secondJstack = 4, thirdJstack = 6, firstJmap = 4; + private static final int FIRST_DUMP_JSTACK_LOAD_AVG = 2, + SECOND_DUMP_JSTACK_LOAD_AVG = 4, + THIRD_DUMP_JSTACK_LOAD_AVG = 6, + FIRST_DUMP_JMAP_LOAD_AVG = 4; private static final String dumpLogDir = CC.mp.monitor.dump_dir; private static final boolean dumpEnabled = CC.mp.monitor.dump_stack; private static final boolean printLog = CC.mp.monitor.print_log; private static final long dumpPeriod = CC.mp.monitor.dump_period.getSeconds(); - private boolean dumpFirstJstack = false; - private boolean dumpSecondJstack = false; - private boolean dumpThirdJstack = false; - private boolean dumpJmap = false; + private volatile boolean dumpFirstJstack = false; + private volatile boolean dumpSecondJstack = false; + private volatile boolean dumpThirdJstack = false; + private volatile boolean dumpJmap = false; private final ResultCollector collector = new ResultCollector(); + private Thread thread; + @Override public void run() { while (isRunning()) { MonitorResult result = collector.collect(); if (printLog) { - Logs.Monitor.info(result.toJson()); + Logs.MONITOR.info(result.toJson()); } if (dumpEnabled) { @@ -66,7 +72,7 @@ public void run() { try { TimeUnit.SECONDS.sleep(dumpPeriod); } catch (InterruptedException e) { - stop(); + if (isRunning()) stop(); } } } @@ -74,40 +80,40 @@ public void run() { @Override protected void doStart(Listener listener) throws Throwable { if (printLog || dumpEnabled) { - Thread thread = new Thread(this, "mp-t-monitor"); + thread = ThreadPoolManager.I.newThread("monitor", this); thread.start(); } } @Override protected void doStop(Listener listener) throws Throwable { - logger.error("monitor service stopped!"); + if (thread != null && thread.isAlive()) thread.interrupt(); } private void dump() { double load = JVMInfo.I.load(); - if (load > firstJstack) { + if (load > FIRST_DUMP_JSTACK_LOAD_AVG) { if (!dumpFirstJstack) { dumpFirstJstack = true; JVMUtil.dumpJstack(dumpLogDir); } } - if (load > secondJstack) { + if (load > SECOND_DUMP_JSTACK_LOAD_AVG) { if (!dumpSecondJstack) { dumpSecondJstack = true; JVMUtil.dumpJmap(dumpLogDir); } } - if (load > thirdJstack) { + if (load > THIRD_DUMP_JSTACK_LOAD_AVG) { if (!dumpThirdJstack) { dumpThirdJstack = true; JVMUtil.dumpJmap(dumpLogDir); } } - if (load > firstJmap) { + if (load > FIRST_DUMP_JMAP_LOAD_AVG) { if (!dumpJmap) { dumpJmap = true; JVMUtil.dumpJmap(dumpLogDir); diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java index 596251a8..02a74d72 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/JVMUtil.java @@ -19,6 +19,9 @@ package com.mpush.tools.common; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.ThreadNames; +import com.mpush.tools.thread.pool.ThreadPoolManager; import com.sun.management.HotSpotDiagnosticMXBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -110,15 +113,17 @@ public static void jstack(OutputStream stream) throws Exception { } public static void dumpJstack(final String jvmPath) { - new Thread((() -> { - File file = new File(jvmPath, System.currentTimeMillis() + "-jstack.log"); - file.mkdirs(); - try (FileOutputStream out = new FileOutputStream(file)) { - JVMUtil.jstack(out); - } catch (Throwable t) { - LOGGER.error("Dump JVM cache Error!", t); + ThreadPoolManager.I.newThread("dump-jstack-t", (() -> { + File path = new File(jvmPath); + if (path.exists() || path.mkdirs()) { + File file = new File(path, System.currentTimeMillis() + "-jstack.log"); + try (FileOutputStream out = new FileOutputStream(file)) { + JVMUtil.jstack(out); + } catch (Throwable t) { + LOGGER.error("Dump JVM cache Error!", t); + } } - }), "mp-monitor-dump-jstack-t").start(); + })).start(); } private static HotSpotDiagnosticMXBean getHotSpotMXBean() { @@ -169,6 +174,6 @@ public static void jMap(String fileName, boolean live) { } public static void dumpJmap(final String jvmPath) { - new Thread(() -> jMap(jvmPath, false), "mp-monitor-dump-jmap-t").start(); + ThreadPoolManager.I.newThread("dump-jmap-t", () -> jMap(jvmPath, false)).start(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java similarity index 84% rename from mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java index c662e089..1917e933 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/PoolThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java @@ -22,7 +22,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -public class PoolThreadFactory implements ThreadFactory { +public class NamedPoolThreadFactory implements ThreadFactory { private static final AtomicInteger poolNum = new AtomicInteger(1); private final AtomicInteger threadNum = new AtomicInteger(1); @@ -31,11 +31,11 @@ public class PoolThreadFactory implements ThreadFactory { private final String namePre; private final boolean isDaemon; - public PoolThreadFactory(String prefix) { + public NamedPoolThreadFactory(String prefix) { this(prefix, true); } - public PoolThreadFactory(String prefix, boolean daemon) { + public NamedPoolThreadFactory(String prefix, boolean daemon) { SecurityManager manager = System.getSecurityManager(); if (manager != null) { group = manager.getThreadGroup(); @@ -52,8 +52,8 @@ public PoolThreadFactory(String prefix, boolean daemon) { @Override public Thread newThread(Runnable runnable) { Thread t = new Thread(group, runnable, namePre + threadNum.getAndIncrement(), 0); - t.setContextClassLoader(PoolThreadFactory.class.getClassLoader()); - t.setPriority(Thread.MAX_PRIORITY); + t.setContextClassLoader(NamedPoolThreadFactory.class.getClassLoader()); + t.setPriority(Thread.NORM_PRIORITY); t.setDaemon(isDaemon); return t; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 24be691f..27cfcb9d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -45,14 +45,22 @@ public NamedThreadFactory(final String namePrefix) { } public Thread newThread(String name, Runnable r) { - return new Thread(group, r, name); + return new Thread(group, r, namePrefix + "-" + threadNumber.getAndIncrement() + "-" + name); } @Override public Thread newThread(Runnable r) { - Thread t = newThread(namePrefix + "_" + threadNumber.getAndIncrement(), r); + Thread t = newThread(namePrefix + "-" + threadNumber.getAndIncrement(), r); if (t.isDaemon()) t.setDaemon(false); return t; } + + public static NamedThreadFactory build() { + return new NamedThreadFactory(); + } + + public static NamedThreadFactory build(String namePrefix) { + return new NamedThreadFactory(namePrefix); + } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index a28df05e..313e8420 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -32,6 +32,7 @@ public final class ThreadNames { * netty worker 线程 */ public static final String T_SERVER_WORKER = NS + "-worker"; + public static final String T_TRAFFIC_SHAPING = NS + "-traffic-shaping"; public static final String T_HTTP_CLIENT = NS + "-http"; @@ -42,9 +43,10 @@ public final class ThreadNames { public static final String T_ZK = NS + "-zk"; public static final String T_BIZ = NS + "-biz"; - public static final String T_PUSH_CALLBACK = NS + "-push-cb"; + public static final String T_PUSH_CALLBACK = NS + "-push-callback"; public static final String T_PUSH_REQ_TIMER = NS + "-push-timer"; public static final String T_ARK_REQ_TIMER = NS + "-ack-timer"; + public static final String T_MONITOR = NS + "-monitor"; /** * connection 定期检测线程 diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java index aad039ea..b7af1b06 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java @@ -27,7 +27,11 @@ */ public class DefaultExecutor extends ThreadPoolExecutor { - public DefaultExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { + public DefaultExecutor(int corePoolSize, int maximumPoolSize, + long keepAliveTime, TimeUnit unit, + BlockingQueue workQueue, + ThreadFactory threadFactory, + RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java similarity index 93% rename from mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java rename to mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index 3dfba9f2..925f948b 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultThreadPoolFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -20,9 +20,9 @@ package com.mpush.tools.thread.pool; import com.mpush.api.spi.Spi; -import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.api.spi.common.ExecutorFactory; import com.mpush.tools.config.CC; -import com.mpush.tools.thread.PoolThreadFactory; +import com.mpush.tools.thread.NamedPoolThreadFactory; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; @@ -35,23 +35,21 @@ * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 */ @Spi(order = 1) -public class DefaultThreadPoolFactory implements ThreadPoolFactory { +public class DefaultExecutorFactory implements ExecutorFactory { private Executor get(ThreadPoolConfig config) { String name = config.getName(); int corePoolSize = config.getCorePoolSize(); int maxPoolSize = config.getMaxPoolSize(); int keepAliveSeconds = config.getKeepAliveSeconds(); - - BlockingQueue queue = config.getQueue(); - ThreadFactory threadFactory = new PoolThreadFactory(name); + BlockingQueue queue = config.getQueue(); return new DefaultExecutor(corePoolSize , maxPoolSize , keepAliveSeconds , TimeUnit.SECONDS , queue - , threadFactory + , new NamedPoolThreadFactory(name) , new DumpThreadRejectedHandler(config)); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java index d82cf1a0..e49d3c83 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -110,7 +110,7 @@ public static ThreadPoolConfig build(String name) { } - public BlockingQueue getQueue() { + public BlockingQueue getQueue() { BlockingQueue blockingQueue; if (queueCapacity == 0) { blockingQueue = new SynchronousQueue<>(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 45b28813..d06da262 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -19,8 +19,7 @@ package com.mpush.tools.thread.pool; -import com.mpush.api.spi.SpiLoader; -import com.mpush.api.spi.common.ThreadPoolFactory; +import com.mpush.api.spi.common.ExecutorFactory; import com.mpush.tools.thread.NamedThreadFactory; import java.util.HashMap; @@ -28,12 +27,10 @@ import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -import static com.mpush.tools.config.CC.mp.spi.thread_pool_factory; - public class ThreadPoolManager { public static final ThreadPoolManager I = new ThreadPoolManager(); - private final ThreadPoolFactory threadPoolFactory = SpiLoader.load(ThreadPoolFactory.class, thread_pool_factory); + private final ExecutorFactory executorFactory = ExecutorFactory.create(); private final NamedThreadFactory threadFactory = new NamedThreadFactory(); private Executor bossExecutor; @@ -51,7 +48,7 @@ public final Thread newThread(String name, Runnable target) { public Executor getHttpExecutor() { if (httpExecutor == null) { synchronized (this) { - httpExecutor = threadPoolFactory.get(ThreadPoolFactory.HTTP_CLIENT_WORK); + httpExecutor = executorFactory.get(ExecutorFactory.HTTP_CLIENT_WORK); } } return httpExecutor; @@ -60,7 +57,7 @@ public Executor getHttpExecutor() { public Executor getRedisExecutor() { if (redisExecutor == null) { synchronized (this) { - redisExecutor = threadPoolFactory.get(ThreadPoolFactory.MQ); + redisExecutor = executorFactory.get(ExecutorFactory.MQ); } } return redisExecutor; @@ -69,7 +66,7 @@ public Executor getRedisExecutor() { public Executor getEventBusExecutor() { if (eventBusExecutor == null) { synchronized (this) { - eventBusExecutor = threadPoolFactory.get(ThreadPoolFactory.EVENT_BUS); + eventBusExecutor = executorFactory.get(ExecutorFactory.EVENT_BUS); } } return eventBusExecutor; @@ -78,7 +75,7 @@ public Executor getEventBusExecutor() { public Executor getBizExecutor() { if (bizExecutor == null) { synchronized (this) { - bizExecutor = threadPoolFactory.get(ThreadPoolFactory.BIZ); + bizExecutor = executorFactory.get(ExecutorFactory.BIZ); } } return bizExecutor; @@ -87,7 +84,7 @@ public Executor getBizExecutor() { public Executor getWorkExecutor() { if (workExecutor == null) { synchronized (this) { - workExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_WORK); + workExecutor = executorFactory.get(ExecutorFactory.SERVER_WORK); } } return workExecutor; @@ -96,7 +93,7 @@ public Executor getWorkExecutor() { public Executor getBossExecutor() { if (bossExecutor == null) { synchronized (this) { - bossExecutor = threadPoolFactory.get(ThreadPoolFactory.SERVER_BOSS); + bossExecutor = executorFactory.get(ExecutorFactory.SERVER_BOSS); } } return bossExecutor; @@ -105,7 +102,7 @@ public Executor getBossExecutor() { public Executor getPushCallbackExecutor() { if (pushCallbackExecutor == null) { synchronized (this) { - pushCallbackExecutor = threadPoolFactory.get(ThreadPoolFactory.PUSH_CALLBACK); + pushCallbackExecutor = executorFactory.get(ExecutorFactory.PUSH_CALLBACK); } } return pushCallbackExecutor; From c57865e6a68ed72d810d98aec74e023c94d0654c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 20 Oct 2016 09:43:10 +0800 Subject: [PATCH 705/890] =?UTF-8?q?=E8=A7=A3=E7=A0=81=E4=BC=98=E5=8C=96,?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=BE=AA=E7=8E=AF=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/netty/codec/PacketDecoder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index fc5c1929..2780b7b4 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -60,12 +60,12 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { in.markReaderIndex(); Packet packet = decodeFrame(in); - if (packet == null) { + if (packet != null) { + out.add(packet); + } else { //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 in.resetReaderIndex(); } - - out.add(packet); } } From d1b5ed35169691e478e8502415664dadb6442e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 20 Oct 2016 11:25:18 +0800 Subject: [PATCH 706/890] =?UTF-8?q?=E8=A7=A3=E7=A0=81=E4=BC=98=E5=8C=96,?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=BE=AA=E7=8E=AF=E8=A7=A3=E7=A0=81,bugFix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/netty/codec/PacketDecoder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 8b49a0b4..fd029919 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -59,12 +59,12 @@ private void decodeFrames(ByteBuf in, List out) throws Exception { in.markReaderIndex(); Packet packet = decodeFrame(in); - if (packet == null) { + if (packet != null) { + out.add(packet); + } else { //2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 in.resetReaderIndex(); } - - out.add(packet); } } From 233b1136bf46c923f5fa921463f6629bdb1e998d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 20 Oct 2016 15:21:35 +0800 Subject: [PATCH 707/890] =?UTF-8?q?IP=E5=9C=B0=E5=9D=80=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-tools/src/main/java/com/mpush/tools/Utils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index a27f7d24..7a02044e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -19,7 +19,6 @@ package com.mpush.tools; -import com.mpush.tools.common.Profiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,7 +81,7 @@ public static String getInetAddress(boolean getLocal) { return address.getHostAddress(); } } else { - if (!address.isSiteLocalAddress()) { + if (!address.isSiteLocalAddress() && !address.isLoopbackAddress()) { return address.getHostAddress(); } } From cc40ff92ad494ec2e043066980ed36297adf501c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 22 Oct 2016 01:01:05 +0800 Subject: [PATCH 708/890] =?UTF-8?q?=E7=BD=91=E5=85=B3=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0UDP=E5=8F=8A=E7=BB=84=E6=92=AD=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 6 +- .../java/com/mpush/api/protocol/Packet.java | 12 +- .../com/mpush/api/protocol/UDPPacket.java | 61 +++++++++ .../com/mpush/bootstrap/ServerLauncher.java | 8 +- .../com/mpush/bootstrap/job/ServerBoot.java | 2 - .../mpush/client/connect/ConnectClient.java | 4 +- .../mpush/client/gateway/GatewayClient.java | 4 +- .../gateway/GatewayClientChannelHandler.java | 6 +- .../gateway/GatewayConnectionFactory.java | 54 ++++++++ ....java => GatewayTCPConnectionFactory.java} | 48 ++++--- .../gateway/GatewayUDPConnectionFactory.java | 106 +++++++++++++++ .../client/gateway/GatewayUDPConnector.java | 84 ++++++++++++ .../gateway/handler/GatewayErrorHandler.java | 44 ++++++ .../gateway/handler/GatewayOKHandler.java | 46 +++++++ .../com/mpush/client/push/PushClient.java | 33 ++--- .../com/mpush/client/push/PushRequest.java | 57 ++++---- .../com/mpush/common/MessageDispatcher.java | 29 +++- .../mpush/common/message/ErrorMessage.java | 5 +- .../com/mpush/common/message/OkMessage.java | 3 +- .../message/gateway/GatewayPushMessage.java | 30 ++++- .../java/com/mpush/core/ack/AckContext.java | 11 +- .../com/mpush/core/handler/AdminHandler.java | 1 + .../core/handler/GatewayPushHandler.java | 57 ++++---- .../com/mpush/core/server/AdminServer.java | 14 +- .../mpush/core/server/ConnectionServer.java | 6 +- .../com/mpush/core/server/GatewayServer.java | 6 +- .../core/server/GatewayUDPConnector.java | 77 +++++++++++ .../{NettyClient.java => NettyTCPClient.java} | 99 ++++++++------ .../com/mpush/netty/codec/PacketDecoder.java | 18 ++- .../com/mpush/netty/codec/PacketEncoder.java | 12 ++ .../netty/connection/NettyConnection.java | 7 +- .../{NettyServer.java => NettyTCPServer.java} | 24 ++-- .../mpush/netty/udp/NettyUDPConnector.java | 123 +++++++++++++++++ .../mpush/netty/udp/UDPChannelHandler.java | 117 ++++++++++++++++ .../java/com/mpush/netty/MulticastTest.java | 72 ++++++++++ .../java/com/mpush/netty/MulticastTest2.java | 125 ++++++++++++++++++ .../mpush/test/client/ConnClientTestMain.java | 5 +- .../mpush/test/push/PushClientTestMain.java | 8 +- .../com/mpush/test/sever/ServerTestMain.java | 9 +- .../src/main/java/com/mpush/tools/Utils.java | 33 ++++- .../main/java/com/mpush/tools/config/CC.java | 21 ++- 41 files changed, 1274 insertions(+), 213 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java rename mpush-client/src/main/java/com/mpush/client/gateway/{GatewayClientFactory.java => GatewayTCPConnectionFactory.java} (80%) create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java create mode 100644 mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java rename mpush-netty/src/main/java/com/mpush/netty/client/{NettyClient.java => NettyTCPClient.java} (50%) rename mpush-netty/src/main/java/com/mpush/netty/server/{NettyServer.java => NettyTCPServer.java} (94%) create mode 100644 mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java create mode 100644 mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java create mode 100644 mpush-test/src/test/java/com/mpush/netty/MulticastTest.java create mode 100644 mpush-test/src/test/java/com/mpush/netty/MulticastTest2.java diff --git a/conf/reference.conf b/conf/reference.conf index 405419ae..c552cb54 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -37,8 +37,12 @@ mp { #网络配置 net { connect-server-port=3000 //长链接服务对外端口, 公网端口 - gateway-server-port=3001 //网关服务端口, 内部端口 admin-server-port=3002 //控制台服务端口, 内部端口 + gateway-server-port=3001 //网关服务端口, 内部端口 + gateway-client-port=4000 //UDP 客户端端口 + gateway-server-net=tcp //网关服务使用的网络类型tcp/udp + gateway-server-multicast="239.239.239.88" + gateway-client-multicast="239.239.239.99" public-host-mapping { //本机局域网IP和公网IP的映射关系 "10.0.10.156":"111.1.32.137" "10.0.10.166":"111.1.33.138" diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 2cf26f1e..38a0b188 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -22,13 +22,15 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import java.net.InetSocketAddress; + /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) * * @author ohun@live.cn */ -public final class Packet { +public class Packet { public static final int HEADER_LEN = 13; public static final byte FLAG_CRYPTO = 0x01; @@ -110,6 +112,14 @@ public boolean validLrc() { return (lrc ^ calcLrc()) == 0; } + public InetSocketAddress sender() { + return null; + } + + public Packet response(Command command) { + return new Packet(command, sessionId); + } + @Override public String toString() { return "Packet{" + diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java new file mode 100644 index 00000000..66f14842 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java @@ -0,0 +1,61 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.protocol; + +import java.net.InetSocketAddress; + +/** + * Created by ohun on 16/10/21. + * + * @author ohun@live.cn (夜色) + */ +public final class UDPPacket extends Packet { + private final InetSocketAddress sender; + + public UDPPacket(byte cmd, InetSocketAddress sender) { + super(cmd); + this.sender = sender; + } + + public UDPPacket(byte cmd, int sessionId, InetSocketAddress sender) { + super(cmd, sessionId); + this.sender = sender; + } + + public UDPPacket(Command cmd, InetSocketAddress sender) { + super(cmd); + this.sender = sender; + } + + public UDPPacket(Command cmd, int sessionId, InetSocketAddress sender) { + super(cmd, sessionId); + this.sender = sender; + } + + @Override + public InetSocketAddress sender() { + return sender; + } + + @Override + public Packet response(Command command) { + return new UDPPacket(command, sessionId, sender); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index d548725f..24029571 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,13 +20,17 @@ package com.mpush.bootstrap; +import com.mpush.api.service.Server; +import com.mpush.api.service.Service; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.GatewayServer; +import com.mpush.core.server.GatewayUDPConnector; import com.mpush.zk.node.ZKServerNode; import static com.mpush.tools.config.CC.mp.net.admin_server_port; +import static com.mpush.tools.config.CC.mp.net.udpGateway; /** * Created by yxx on 2016/5/14. @@ -41,8 +45,8 @@ public ServerLauncher() { ZKServerNode csNode = ZKServerNode.csNode(); ZKServerNode gsNode = ZKServerNode.gsNode(); ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); - GatewayServer gatewayServer = new GatewayServer(gsNode.getPort()); - AdminServer adminServer = new AdminServer(admin_server_port, connectServer, gatewayServer); + Server gatewayServer = udpGateway() ? new GatewayUDPConnector(gsNode.getPort()) : new GatewayServer(gsNode.getPort()); + AdminServer adminServer = new AdminServer(admin_server_port, connectServer); chain.boot() .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 3d013b34..07ca87f0 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -21,10 +21,8 @@ import com.mpush.api.service.Listener; import com.mpush.api.service.Server; -import com.mpush.tools.Jsons; import com.mpush.tools.log.Logs; import com.mpush.tools.thread.pool.ThreadPoolManager; -import com.mpush.zk.ZKClient; import com.mpush.zk.ZKRegister; import com.mpush.zk.node.ZKServerNode; diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java index 0a69d57d..7d072e97 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnectClient.java @@ -21,11 +21,11 @@ import com.google.common.eventbus.Subscribe; import com.mpush.api.event.ConnectionCloseEvent; -import com.mpush.netty.client.NettyClient; +import com.mpush.netty.client.NettyTCPClient; import com.mpush.tools.event.EventBus; import io.netty.channel.ChannelHandler; -public class ConnectClient extends NettyClient { +public class ConnectClient extends NettyTCPClient { private final ConnClientChannelHandler handler; public ConnectClient(String host, int port, ClientConfig config) { diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java index e248c7bd..8703def4 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -21,7 +21,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.service.Listener; -import com.mpush.netty.client.NettyClient; +import com.mpush.netty.client.NettyTCPClient; import com.mpush.tools.thread.NamedPoolThreadFactory; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; @@ -38,7 +38,7 @@ * * @author ohun@live.cn */ -public class GatewayClient extends NettyClient { +public class GatewayClient extends NettyTCPClient { private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); private GlobalChannelTrafficShapingHandler trafficShapingHandler; private ScheduledExecutorService trafficShapingExecutor; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java index d4548f33..85ab9a4c 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java @@ -63,19 +63,19 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } - private void handleOK(OkMessage message) { + public static void handleOK(OkMessage message) { if (message.cmd == Command.GATEWAY_PUSH.cmd) { handPush(message, null, message.getPacket()); } } - private void handleError(ErrorMessage message) { + public static void handleError(ErrorMessage message) { if (message.cmd == Command.GATEWAY_PUSH.cmd) { handPush(null, message, message.getPacket()); } } - private void handPush(OkMessage ok, ErrorMessage error, Packet packet) { + private static void handPush(OkMessage ok, ErrorMessage error, Packet packet) { PushRequest request = PushRequestBus.I.getAndRemove(packet.sessionId); if (request == null) { LOGGER.warn("receive a gateway response, but request has timeout. ok={}, error={}", ok, error); diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java new file mode 100644 index 00000000..a5d6bb0a --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java @@ -0,0 +1,54 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.zk.cache.ZKServerNodeCache; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.function.Consumer; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public abstract class GatewayConnectionFactory extends ZKServerNodeCache { + + final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public void init() { + + } + + abstract public Connection getConnection(String ip); + + abstract public T getNode(String ip); + + abstract public Collection getAllNode(); + + abstract public boolean send(String host, Consumer consumer); + + abstract public void broadcast(Consumer consumer); + +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayTCPConnectionFactory.java similarity index 80% rename from mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java rename to mpush-client/src/main/java/com/mpush/client/gateway/GatewayTCPConnectionFactory.java index a4b58ed4..5a1b9111 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayTCPConnectionFactory.java @@ -23,13 +23,12 @@ import com.mpush.api.connection.Connection; import com.mpush.api.service.Client; import com.mpush.api.service.Listener; -import com.mpush.zk.cache.ZKServerNodeCache; +import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.zk.node.ZKServerNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.Collection; import java.util.Map; +import java.util.function.Consumer; import java.util.stream.Collectors; /** @@ -37,10 +36,7 @@ * * @author ohun@live.cn */ -public class GatewayClientFactory extends ZKServerNodeCache { - public static final GatewayClientFactory I = new GatewayClientFactory(); - - private final Logger logger = LoggerFactory.getLogger(GatewayClientFactory.class); +public class GatewayTCPConnectionFactory extends GatewayConnectionFactory { private final Map ip_client = Maps.newConcurrentMap(); @@ -66,14 +62,7 @@ public void clear() { } } - public GatewayClient getClient(String ip) { - GatewayClient client = ip_client.get(ip); - if (client == null) { - return null;//TODO create client - } - return client; - } - + @Override public Connection getConnection(String ip) { GatewayClient client = ip_client.get(ip); if (client == null) { @@ -87,6 +76,31 @@ public Connection getConnection(String ip) { return null; } + @Override + public Connection getNode(String ip) { + return getConnection(ip); + } + + @Override + public Collection getAllNode() { + return ip_client.values().stream().map(GatewayClient::getConnection).collect(Collectors.toList()); + } + + @Override + public boolean send(String ip, Consumer consumer) { + Connection connection = getConnection(ip); + if (connection == null) return false; + consumer.accept(GatewayPushMessage.build(connection)); + return true; + } + + @Override + public void broadcast(Consumer consumer) { + ip_client.forEach((s, client) -> { + consumer.accept(GatewayPushMessage.build(client.getConnection())); + }); + } + private void restartClient(final GatewayClient client) { ip_client.remove(client.getHost()); client.stop(new Listener() { @@ -125,8 +139,4 @@ public void onFailure(Throwable cause) { } }); } - - public Collection getAllConnections() { - return ip_client.values().stream().map(GatewayClient::getConnection).collect(Collectors.toList()); - } } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java new file mode 100644 index 00000000..5b5abe44 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java @@ -0,0 +1,106 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.google.common.collect.Maps; +import com.mpush.api.connection.Connection; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import com.mpush.zk.node.ZKServerNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetSocketAddress; +import java.util.Collection; +import java.util.Map; +import java.util.function.Consumer; + +import static com.mpush.tools.config.CC.mp.net.gateway_server_multicast; +import static com.mpush.tools.config.CC.mp.net.gateway_server_port; + +/** + * Created by yxx on 2016/5/17. + * + * @author ohun@live.cn + */ +public class GatewayUDPConnectionFactory extends GatewayConnectionFactory { + + private final Logger logger = LoggerFactory.getLogger(GatewayUDPConnectionFactory.class); + + private final Map ip_address = Maps.newConcurrentMap(); + + private final GatewayUDPConnector connector = new GatewayUDPConnector(); + + private final InetSocketAddress multicastRecipient = new InetSocketAddress(gateway_server_multicast, gateway_server_port); + + @Override + public void init() { + super.init(); + ThreadPoolManager.I.newThread("udp-client", connector::start).start(); + } + + @Override + public void put(String fullPath, ZKServerNode node) { + super.put(fullPath, node); + ip_address.put(node.getIp(), new InetSocketAddress(node.getIp(), node.getPort())); + } + + @Override + public ZKServerNode remove(String fullPath) { + ZKServerNode node = super.remove(fullPath); + logger.warn("Gateway Server zkNode={} was removed.", node); + return node; + } + + @Override + public void clear() { + super.clear(); + ip_address.clear(); + } + + @Override + public Connection getConnection(String ip) { + return connector.getConnection(); + } + + @Override + public InetSocketAddress getNode(String ip) { + return ip_address.get(ip); + } + + @Override + public Collection getAllNode() { + return ip_address.values(); + } + + @Override + public boolean send(String host, Consumer consumer) { + InetSocketAddress recipient = ip_address.get(host); + if (recipient == null) return false; + consumer.accept(GatewayPushMessage.build(connector.getConnection(), recipient)); + return true; + } + + @Override + public void broadcast(Consumer consumer) { + consumer.accept(GatewayPushMessage.build(connector.getConnection(), multicastRecipient)); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java new file mode 100644 index 00000000..1b531ff8 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java @@ -0,0 +1,84 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; +import com.mpush.client.gateway.handler.GatewayErrorHandler; +import com.mpush.client.gateway.handler.GatewayOKHandler; +import com.mpush.common.MessageDispatcher; +import com.mpush.netty.udp.UDPChannelHandler; +import com.mpush.netty.udp.NettyUDPConnector; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelOption; + +import static com.mpush.common.MessageDispatcher.POLICY_LOG; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public final class GatewayUDPConnector extends NettyUDPConnector { + + private UDPChannelHandler channelHandler; + + public GatewayUDPConnector() { + super(CC.mp.net.gateway_client_port); + } + + @Override + public void init() { + super.init(); + MessageDispatcher receiver = new MessageDispatcher(POLICY_LOG); + receiver.register(Command.OK, new GatewayOKHandler()); + receiver.register(Command.ERROR, new GatewayErrorHandler()); + channelHandler = new UDPChannelHandler(receiver); + channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_client_multicast)); + channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface()); + } + + + @Override + public void stop(Listener listener) { + super.stop(listener); + } + + + @Override + protected void initOptions(Bootstrap b) { + super.initOptions(b); + b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true); + b.option(ChannelOption.IP_MULTICAST_TTL, 255); + } + + @Override + public ChannelHandler getChannelHandler() { + return channelHandler; + } + + public Connection getConnection() { + return channelHandler.getConnection(); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java new file mode 100644 index 00000000..e9250b83 --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java @@ -0,0 +1,44 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway.handler; + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.client.gateway.GatewayClientChannelHandler; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.ErrorMessage; + +/** + * Created by ohun on 16/10/21. + * + * @author ohun@live.cn (夜色) + */ +public final class GatewayErrorHandler extends BaseMessageHandler { + + @Override + public ErrorMessage decode(Packet packet, Connection connection) { + return new ErrorMessage(packet, connection); + } + + @Override + public void handle(ErrorMessage message) { + GatewayClientChannelHandler.handleError(message); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java new file mode 100644 index 00000000..8591341b --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java @@ -0,0 +1,46 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway.handler; + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.client.gateway.GatewayClientChannelHandler; +import com.mpush.client.push.PushRequest; +import com.mpush.client.push.PushRequestBus; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.handler.OkMessageHandler; +import com.mpush.common.message.OkMessage; + +/** + * Created by ohun on 16/10/21. + * + * @author ohun@live.cn (夜色) + */ +public final class GatewayOKHandler extends BaseMessageHandler { + @Override + public OkMessage decode(Packet packet, Connection connection) { + return new OkMessage(packet, connection); + } + + @Override + public void handle(OkMessage message) { + GatewayClientChannelHandler.handleOK(message); + } +} diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 685d632b..a342e134 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -19,39 +19,38 @@ package com.mpush.client.push; -import com.mpush.api.connection.Connection; import com.mpush.api.push.*; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.client.gateway.GatewayClientFactory; +import com.mpush.client.gateway.GatewayConnectionFactory; +import com.mpush.client.gateway.GatewayTCPConnectionFactory; +import com.mpush.client.gateway.GatewayUDPConnectionFactory; import com.mpush.common.router.CachedRemoteRouterManager; import com.mpush.common.router.RemoteRouter; +import com.mpush.tools.config.CC; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; -import java.util.Collection; import java.util.Set; import java.util.concurrent.FutureTask; import static com.mpush.zk.ZKPath.GATEWAY_SERVER; /*package*/ final class PushClient extends BaseService implements PushSender { - private static final int DEFAULT_TIMEOUT = 3000; - private final GatewayClientFactory factory = GatewayClientFactory.I; - private final CachedRemoteRouterManager routerManager = CachedRemoteRouterManager.I; + private GatewayConnectionFactory factory; private FutureTask send0(PushContext ctx) { if (ctx.isBroadcast()) { - return PushRequest.build(this, ctx).broadcast(); + return PushRequest.build(factory, ctx).broadcast(); } else { - Set remoteRouters = routerManager.lookupAll(ctx.getUserId()); + Set remoteRouters = CachedRemoteRouterManager.I.lookupAll(ctx.getUserId()); if (remoteRouters == null || remoteRouters.isEmpty()) { - return PushRequest.build(this, ctx).offline(); + return PushRequest.build(factory, ctx).offline(); } FutureTask task = null; for (RemoteRouter remoteRouter : remoteRouters) { - task = PushRequest.build(this, ctx).send(remoteRouter); + task = PushRequest.build(factory, ctx).send(remoteRouter); } return task; } @@ -74,12 +73,13 @@ public FutureTask send(PushContext ctx) { } } - Connection getGatewayConnection(String host) { - return factory.getConnection(host); - } - - Collection getAllConnections() { - return factory.getAllConnections(); + @Override + public void init() { + if (CC.mp.net.udpGateway()) { + factory = new GatewayUDPConnectionFactory(); + } else { + factory = new GatewayTCPConnectionFactory(); + } } @Override @@ -87,6 +87,7 @@ protected void doStart(Listener listener) throws Throwable { ZKClient.I.start(listener); RedisManager.I.init(); ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).watch(); + factory.init(); PushRequestBus.I.start(listener); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 0a747c30..78c4b545 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -20,9 +20,9 @@ package com.mpush.client.push; import com.mpush.api.Constants; -import com.mpush.api.connection.Connection; import com.mpush.api.push.*; import com.mpush.api.router.ClientLocation; +import com.mpush.client.gateway.GatewayConnectionFactory; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.common.router.CachedRemoteRouterManager; import com.mpush.common.router.RemoteRouter; @@ -36,6 +36,7 @@ import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; /** * Created by ohun on 2015/12/30. @@ -52,7 +53,7 @@ private enum Status {init, success, failure, offline, timeout} private final AtomicReference status = new AtomicReference<>(Status.init); private final TimeLine timeLine = new TimeLine("Push-Time-Line"); - private final PushClient client; + private final GatewayConnectionFactory connectionFactory; private AckModel ackModel; private Set tags; @@ -81,29 +82,34 @@ private void sendToConnServer(RemoteRouter remoteRouter) { //2.通过网关连接,把消息发送到所在机器 - Connection gatewayConn = client.getGatewayConnection(location.getHost()); - if (gatewayConn == null) { - LOGGER.error("get gateway connection failure, location={}", location); - failure(); - return; - } timeLine.addTimePoint("send-to-gateway-begin"); - GatewayPushMessage pushMessage = - new GatewayPushMessage(userId, content, gatewayConn) + boolean success = connectionFactory.send(location.getHost(), new Consumer() { + @Override + public void accept(GatewayPushMessage pushMessage) { + pushMessage + .setUserId(userId) + .setContent(content) .setClientType(location.getClientType()) .setTimeout(timeout - 500) .setTags(tags) .addFlag(ackModel.flag); - pushMessage.sendRaw(f -> { - timeLine.addTimePoint("send-to-gateway-end"); - if (!f.isSuccess()) failure(); + pushMessage.sendRaw(f -> { + timeLine.addTimePoint("send-to-gateway-end"); + if (!f.isSuccess()) failure(); + }); + PushRequest.this.content = null;//释放内存 + timeLine.addTimePoint("put-request-bus"); + future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + } }); - timeLine.addTimePoint("put-request-bus"); - future = PushRequestBus.I.put(pushMessage.getSessionId(), this); + if (!success) { + LOGGER.error("get gateway connection failure, location={}", location); + failure(); + } } private void submit(Status status) { @@ -164,8 +170,11 @@ public FutureTask offline() { public FutureTask broadcast() { timeLine.begin(); - client.getAllConnections().forEach(conn -> { - GatewayPushMessage pushMessage = new GatewayPushMessage(userId, content, conn) + + Consumer consumer = pushMessage -> { + pushMessage + .setUserId(userId) + .setContent(content) .setTags(tags) .addFlag(ackModel.flag); @@ -173,8 +182,10 @@ public FutureTask broadcast() { if (!f.isSuccess()) failure(); }); - future = PushRequestBus.I.put(pushMessage.getSessionId(), this); - }); + future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + }; + + connectionFactory.broadcast(consumer); return this; } @@ -195,12 +206,12 @@ public long getTimeout() { return timeout; } - public PushRequest(PushClient client) { + public PushRequest(GatewayConnectionFactory factory) { super(NONE); - this.client = client; + this.connectionFactory = factory; } - public static PushRequest build(PushClient client, PushContext ctx) { + public static PushRequest build(GatewayConnectionFactory factory, PushContext ctx) { byte[] content = ctx.getContext(); PushMsg msg = ctx.getPushMsg(); if (msg != null) { @@ -209,7 +220,7 @@ public static PushRequest build(PushClient client, PushContext ctx) { content = json.getBytes(Constants.UTF_8); } } - return new PushRequest(client) + return new PushRequest(factory) .setAckModel(ctx.getAckModel()) .setUserId(ctx.getUserId()) .setTags(ctx.getTags()) diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index 712100ed..c9056c5a 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -42,8 +42,20 @@ * @author ohun@live.cn */ public final class MessageDispatcher implements PacketReceiver { - public static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); + public static final int POLICY_REJECT = 2; + public static final int POLICY_LOG = 1; + public static final int POLICY_IGNORE = 0; + private static final Logger LOGGER = LoggerFactory.getLogger(MessageDispatcher.class); private final Map handlers = new HashMap<>(); + private final int unsupportedPolicy; + + public MessageDispatcher() { + unsupportedPolicy = POLICY_REJECT; + } + + public MessageDispatcher(int unsupportedPolicy) { + this.unsupportedPolicy = unsupportedPolicy; + } public void register(Command command, MessageHandler handler) { handlers.put(command.cmd, handler); @@ -67,12 +79,15 @@ public void onReceive(Packet packet, Connection connection) { Profiler.release(); } } else { - LOGGER.error("dispatch message failure unsupported cmd, packet={}, connect={}, body={}" - , packet, connection); - ErrorMessage - .from(packet, connection) - .setErrorCode(UNSUPPORTED_CMD) - .close(); + if (unsupportedPolicy > POLICY_IGNORE) { + LOGGER.error("dispatch message failure unsupported cmd, packet={}, connect={}, body={}", packet, connection); + if (unsupportedPolicy == POLICY_REJECT) { + ErrorMessage + .from(packet, connection) + .setErrorCode(UNSUPPORTED_CMD) + .close(); + } + } } } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index 9126e3aa..e2289485 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -64,12 +64,11 @@ public void encode(ByteBuf body) { } public static ErrorMessage from(BaseMessage src) { - return new ErrorMessage(src.packet.cmd, new Packet(ERROR - , src.packet.sessionId), src.connection); + return new ErrorMessage(src.packet.cmd, src.packet.response(ERROR), src.connection); } public static ErrorMessage from(Packet src, Connection connection) { - return new ErrorMessage(src.cmd, new Packet(ERROR, src.sessionId), connection); + return new ErrorMessage(src.cmd, src.response(ERROR), connection); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index 5edcb2d9..0feb1296 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -59,8 +59,7 @@ public void encode(ByteBuf body) { } public static OkMessage from(BaseMessage src) { - return new OkMessage(src.packet.cmd, new Packet(OK - , src.packet.sessionId), src.connection); + return new OkMessage(src.packet.cmd, src.packet.response(OK), src.connection); } public OkMessage setCode(byte code) { diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 81ce00b5..de8cb56f 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -22,11 +22,13 @@ import com.google.gson.reflect.TypeToken; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.api.protocol.UDPPacket; import com.mpush.common.message.ByteBufMessage; import com.mpush.tools.Jsons; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; +import java.net.InetSocketAddress; import java.util.Set; import static com.mpush.api.protocol.Command.GATEWAY_PUSH; @@ -43,16 +45,20 @@ public class GatewayPushMessage extends ByteBufMessage { public int timeout; public byte[] content; - public GatewayPushMessage(String userId, byte[] content, Connection connection) { - super(new Packet(GATEWAY_PUSH, genSessionId()), connection); - this.userId = userId; - this.content = content; - } - public GatewayPushMessage(Packet message, Connection connection) { super(message, connection); } + public static GatewayPushMessage build(Connection connection, InetSocketAddress sender) { + Packet packet = new UDPPacket(GATEWAY_PUSH, genSessionId(), sender); + return new GatewayPushMessage(packet, connection); + } + + public static GatewayPushMessage build(Connection connection) { + Packet packet = new Packet(GATEWAY_PUSH, genSessionId()); + return new GatewayPushMessage(packet, connection); + } + @Override public void decode(ByteBuf body) { userId = decodeString(body); @@ -83,6 +89,16 @@ private void encodeSet(ByteBuf body, Set field) { encodeString(body, json); } + public GatewayPushMessage setUserId(String userId) { + this.userId = userId; + return this; + } + + public GatewayPushMessage setContent(byte[] content) { + this.content = content; + return this; + } + public GatewayPushMessage setClientType(int clientType) { this.clientType = clientType; return this; @@ -127,7 +143,7 @@ public String toString() { "userId='" + userId + '\'' + ", clientType='" + clientType + '\'' + ", timeout='" + timeout + '\'' + - ", content='" + content.length + '\'' + + ", content='" + (content == null ? 0 : content.length) + '\'' + '}'; } } diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java index d9146d84..dc1651ed 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java @@ -31,18 +31,10 @@ public final class AckContext implements Runnable { private final AtomicBoolean done = new AtomicBoolean(false); - public final int gatewayMessageId; - public final byte cmd; private AckCallback callback; /*package*/ int pushMessageId; - public AckContext(int gatewayMessageId, byte cmd) { - this.gatewayMessageId = gatewayMessageId; - this.cmd = cmd; - } - - public static AckContext from(BaseMessage message) { - return new AckContext(message.getSessionId(), message.getPacket().cmd); + public AckContext() { } public boolean tryDone() { @@ -70,7 +62,6 @@ public void timeout() { @Override public String toString() { return "AckContext{" + - "gatewayMessageId=" + gatewayMessageId + ", pushMessageId=" + pushMessageId + '}'; } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index 1a16c044..cc84cf5d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -87,6 +87,7 @@ protected void channelRead0(ChannelHandlerContext ctx, String request) throws Ex throwable.printStackTrace(new PrintWriter(writer)); ctx.writeAndFlush(writer.toString()); } + LOGGER.info("receive admin command={}", request); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 00759c8d..2b652274 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -283,37 +283,34 @@ private void checkRemote(GatewayPushMessage message) { private AckContext buildAckContext(GatewayPushMessage message) { - Connection gatewayConnection = message.getConnection(); - String userId = message.userId; - int clientType = message.clientType; - - return AckContext.from(message) - .setCallback(new AckCallback() { - @Override - public void onSuccess(AckContext ctx) { - if (!gatewayConnection.isConnected()) { - Logs.PUSH.info(">>> receive client ack, gateway connection is closed, context={}", ctx); - return; - } + message.getPacket().body = null;//内存释放 + message.content = null;//内存释放 + return new AckContext().setCallback(new AckCallback() { + @Override + public void onSuccess(AckContext ctx) { + if (!message.getConnection().isConnected()) { + Logs.PUSH.info(">>> receive client ack, gateway connection is closed, context={}", ctx); + return; + } - OkMessage okMessage = new OkMessage(ctx.cmd, new Packet(OK, ctx.gatewayMessageId), gatewayConnection); - okMessage.setData(userId + ',' + clientType); - okMessage.sendRaw(); - Logs.PUSH.info(">>> receive client ack and response gateway client success, context={}", ctx); - } + OkMessage okMessage = OkMessage.from(message); + okMessage.setData(message.userId + ',' + message.clientType); + okMessage.sendRaw(); + Logs.PUSH.info(">>> receive client ack and response gateway client success, context={}", ctx); + } - @Override - public void onTimeout(AckContext ctx) { - if (!gatewayConnection.isConnected()) { - Logs.PUSH.info("push message timeout client not ack, gateway connection is closed, context={}", ctx); - return; - } - ErrorMessage errorMessage = new ErrorMessage(ctx.cmd, new Packet(ERROR, ctx.gatewayMessageId), gatewayConnection); - errorMessage.setData(userId + ',' + clientType); - errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT); - errorMessage.sendRaw(); - Logs.PUSH.info("push message timeout client not ack, context={}", ctx); - } - }); + @Override + public void onTimeout(AckContext ctx) { + if (!message.getConnection().isConnected()) { + Logs.PUSH.info("push message timeout client not ack, gateway connection is closed, context={}", ctx); + return; + } + ErrorMessage errorMessage = ErrorMessage.from(message); + errorMessage.setData(message.userId + ',' + message.clientType); + errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT); + errorMessage.sendRaw(); + Logs.PUSH.info("push message timeout client not ack, context={}", ctx); + } + }); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index eae85738..ac79c873 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -20,8 +20,7 @@ package com.mpush.core.server; import com.mpush.core.handler.AdminHandler; -import com.mpush.netty.server.NettyServer; -import com.mpush.tools.thread.pool.ThreadPoolManager; +import com.mpush.netty.server.NettyTCPServer; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.DelimiterBasedFrameDecoder; @@ -29,18 +28,14 @@ import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; -import java.util.concurrent.Executor; - -public final class AdminServer extends NettyServer { +public final class AdminServer extends NettyTCPServer { private final ConnectionServer connectionServer; - private final GatewayServer gatewayServer; private final AdminHandler adminHandler; - public AdminServer(int port, ConnectionServer connectionServer, GatewayServer gatewayServer) { + public AdminServer(int port, ConnectionServer connectionServer) { super(port); this.connectionServer = connectionServer; - this.gatewayServer = gatewayServer; this.adminHandler = new AdminHandler(this); } @@ -69,7 +64,4 @@ public ConnectionServer getConnectionServer() { return connectionServer; } - public GatewayServer getGatewayServer() { - return gatewayServer; - } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 1f8db861..441183e5 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -28,7 +28,7 @@ import com.mpush.core.handler.*; import com.mpush.netty.http.HttpClient; import com.mpush.netty.http.NettyHttpClient; -import com.mpush.netty.server.NettyServer; +import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; @@ -51,7 +51,7 @@ * * @author ohun@live.cn (夜色) */ -public final class ConnectionServer extends NettyServer { +public final class ConnectionServer extends NettyTCPServer { private ServerChannelHandler channelHandler; private GlobalChannelTrafficShapingHandler trafficShapingHandler; private ScheduledExecutorService trafficShapingExecutor; @@ -94,11 +94,11 @@ public void init() { @Override public void stop(Listener listener) { + super.stop(listener); if (trafficShapingHandler != null) { trafficShapingHandler.release(); trafficShapingExecutor.shutdown(); } - super.stop(listener); if (httpClient != null && httpClient.isRunning()) { httpClient.stop(); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 8632054b..9d7b835f 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -23,7 +23,7 @@ import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.GatewayPushHandler; -import com.mpush.netty.server.NettyServer; +import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; import io.netty.bootstrap.ServerBootstrap; @@ -44,7 +44,7 @@ * * @author ohun@live.cn */ -public final class GatewayServer extends NettyServer { +public final class GatewayServer extends NettyTCPServer { private ServerChannelHandler channelHandler; private ServerConnectionManager connectionManager; @@ -75,11 +75,11 @@ public void init() { @Override public void stop(Listener listener) { + super.stop(listener); if (trafficShapingHandler != null) { trafficShapingHandler.release(); trafficShapingExecutor.shutdown(); } - super.stop(listener); if (connectionManager != null) { connectionManager.destroy(); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java new file mode 100644 index 00000000..fe121eb9 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java @@ -0,0 +1,77 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.server; + +import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; +import com.mpush.common.MessageDispatcher; +import com.mpush.core.handler.GatewayPushHandler; +import com.mpush.netty.udp.UDPChannelHandler; +import com.mpush.netty.udp.NettyUDPConnector; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelOption; + +import static com.mpush.common.MessageDispatcher.POLICY_LOG; + +/** + * Created by ohun on 2015/12/30. + * + * @author ohun@live.cn + */ +public final class GatewayUDPConnector extends NettyUDPConnector { + + private UDPChannelHandler channelHandler; + + public GatewayUDPConnector(int port) { + super(port); + } + + @Override + public void init() { + super.init(); + MessageDispatcher receiver = new MessageDispatcher(POLICY_LOG); + receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); + channelHandler = new UDPChannelHandler(receiver); + channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_server_multicast)); + channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface()); + } + + @Override + public void stop(Listener listener) { + super.stop(listener); + } + + @Override + protected void initOptions(Bootstrap b) { + super.initOptions(b); + b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);//默认情况下,当本机发送组播数据到某个网络接口时,在IP层,数据会回送到本地的回环接口,选项IP_MULTICAST_LOOP用于控制数据是否回送到本地的回环接口 + //b.option(ChannelOption.IP_MULTICAST_IF, false);//选项IP_MULTICAST_IF用于设置组播的默认默认网络接口,会从给定的网络接口发送,另一个网络接口会忽略此数据,参数addr是希望多播输出接口的IP地址,使用INADDR_ANY地址回送到默认接口。 + b.option(ChannelOption.IP_MULTICAST_TTL, 255);//选项IP_MULTICAST_TTL允许设置超时TTL,范围为0~255之间的任何值,例如: + //b.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 1024 * 1024)); + } + + @Override + public ChannelHandler getChannelHandler() { + return channelHandler; + } +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java similarity index 50% rename from mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java rename to mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java index 0854143c..93fcfd16 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java @@ -25,62 +25,76 @@ import com.mpush.api.service.ServiceException; import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; +import io.netty.channel.epoll.EpollEventLoopGroup; +import io.netty.channel.epoll.EpollServerSocketChannel; +import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; +import java.util.concurrent.Executor; -public abstract class NettyClient extends BaseService implements Client { - private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); +public abstract class NettyTCPClient extends BaseService implements Client { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyTCPClient.class); private final String host; private final int port; private EventLoopGroup workerGroup; - public NettyClient(String host, int port) { + public NettyTCPClient(String host, int port) { this.host = host; this.port = port; } - @Override - public void start(final Listener listener) { - if (started.compareAndSet(false, true)) { - Bootstrap bootstrap = new Bootstrap(); - workerGroup = new NioEventLoopGroup(); - bootstrap.group(workerGroup)// - .option(ChannelOption.TCP_NODELAY, true)// - .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .channel(NioSocketChannel.class) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); - - bootstrap.handler(new ChannelInitializer() { // (4) - @Override - public void initChannel(SocketChannel ch) throws Exception { - initPipeline(ch.pipeline()); - } - }); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - future.addListener((ChannelFutureListener) f -> { - if (f.isSuccess()) { - if (listener != null) listener.onSuccess(port); - LOGGER.info("start netty client success, host={}, port={}", host, port); - } else { - if (listener != null) listener.onFailure(f.cause()); - LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); - } - }); - } else { - listener.onFailure(new ServiceException("client has started!")); - } + private void createClient(Listener listener, EventLoopGroup workerGroup, Class clazz) { + this.workerGroup = workerGroup; + Bootstrap b = new Bootstrap(); + b.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000) + .channel(clazz); + + b.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + initPipeline(ch.pipeline()); + } + }); + + ChannelFuture future = b.connect(new InetSocketAddress(host, port)); + future.addListener(f -> { + if (f.isSuccess()) { + if (listener != null) listener.onSuccess(port); + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + if (listener != null) listener.onFailure(f.cause()); + LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); + } + }); + } + + private void createNioClient(Listener listener) { + NioEventLoopGroup workerGroup = new NioEventLoopGroup(1, getWorkExecutor()); + workerGroup.setIoRatio(getIoRate()); + createClient(listener, workerGroup, NioSocketChannel.class); + } + + private void createEpollClient(Listener listener) { + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(1, getWorkExecutor()); + workerGroup.setIoRatio(getIoRate()); + createClient(listener, workerGroup, EpollSocketChannel.class); } protected void initPipeline(ChannelPipeline pipeline) { @@ -97,12 +111,23 @@ protected ChannelHandler getEncoder() { return PacketEncoder.INSTANCE; } + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + + protected int getIoRate() { + return 70; + } public abstract ChannelHandler getChannelHandler(); @Override protected void doStart(Listener listener) throws Throwable { - + if (CC.mp.core.useNettyEpoll()) { + createEpollClient(listener); + } else { + createNioClient(listener); + } } @Override diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 2780b7b4..891f4569 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -20,9 +20,11 @@ package com.mpush.netty.codec; import com.mpush.api.protocol.Packet; +import com.mpush.api.protocol.UDPPacket; import com.mpush.tools.config.CC; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.socket.DatagramPacket; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.TooLongFrameException; @@ -75,11 +77,21 @@ private Packet decodeFrame(ByteBuf in) throws Exception { if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { return null; } - return readPacket(in, bodyLength); + return readPacket(new Packet(in.readByte()), in, bodyLength); } - private Packet readPacket(ByteBuf in, int bodyLength) { - Packet packet = new Packet(in.readByte());//read cmd + public static Packet decodeFrame(DatagramPacket datagram) throws Exception { + ByteBuf in = datagram.content(); + int readableBytes = in.readableBytes(); + int bodyLength = in.readInt(); + if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { + return null; + } + return readPacket(new UDPPacket(in.readByte() + , datagram.sender()), in, bodyLength); + } + + private static Packet readPacket(Packet packet, ByteBuf in, int bodyLength) { packet.cc = in.readShort();//read cc packet.flags = in.readByte();//read flags packet.sessionId = in.readInt();//read sessionId diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index ae6bfed3..ec554df9 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -22,6 +22,7 @@ import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; @@ -38,6 +39,17 @@ public final class PacketEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { + encodeFrame(packet, out); + } + + public static ByteBuf encode(Channel channel, Packet packet) { + int capacity = packet.cmd == Command.HEARTBEAT.cmd ? 1 : Packet.HEADER_LEN + packet.getBodyLength(); + ByteBuf out = channel.alloc().buffer(capacity, capacity); + encodeFrame(packet, out); + return out; + } + + public static void encodeFrame(Packet packet, ByteBuf out) { if (packet.cmd == Command.HEARTBEAT.cmd) { out.writeByte(Packet.HB_PACKET_BYTE); } else { diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 518d2c85..b7ba976e 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -24,9 +24,11 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.protocol.Packet; import com.mpush.api.spi.core.CipherFactory; +import com.mpush.netty.codec.PacketEncoder; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.socket.DatagramPacket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,7 +80,10 @@ public ChannelFuture send(Packet packet) { @Override public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { if (channel.isActive()) { - ChannelFuture future = channel.writeAndFlush(packet).addListener(this); + + Object msg = packet.sender() == null ? packet : new DatagramPacket(PacketEncoder.encode(channel, packet), packet.sender()); + + ChannelFuture future = channel.writeAndFlush(msg).addListener(this); if (listener != null) { future.addListener(listener); diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java similarity index 94% rename from mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java rename to mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index f5bb02bf..437e9469 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -47,9 +47,9 @@ * * @author ohun@live.cn */ -public abstract class NettyServer extends BaseService implements Server { +public abstract class NettyTCPServer extends BaseService implements Server { - private static final Logger logger = LoggerFactory.getLogger(NettyServer.class); + private final Logger logger = LoggerFactory.getLogger(this.getClass()); public enum State {Created, Initialized, Starting, Started, Shutdown} @@ -59,7 +59,7 @@ public enum State {Created, Initialized, Starting, Started, Shutdown} protected EventLoopGroup bossGroup; protected EventLoopGroup workerGroup; - public NettyServer(int port) { + public NettyTCPServer(int port) { this.port = port; } @@ -95,7 +95,7 @@ public void start(final Listener listener) { if (!serverState.compareAndSet(State.Initialized, State.Starting)) { throw new IllegalStateException("Server already started or have not init"); } - if (useNettyEpoll()) { + if (CC.mp.core.useNettyEpoll()) { createEpollServer(listener); } else { createNioServer(listener); @@ -146,7 +146,7 @@ private void createServer(Listener listener, EventLoopGroup boss, EventLoopGroup */ b.childHandler(new ChannelInitializer() { // (4) @Override - public void initChannel(SocketChannel ch) throws Exception { + public void initChannel(SocketChannel ch) throws Exception {//每连上一个链接调用一次 initPipeline(ch.pipeline()); } }); @@ -192,7 +192,6 @@ private void createNioServer(Listener listener) { createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } - @SuppressWarnings("unused") private void createEpollServer(Listener listener) { EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, getBossExecutor()); EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, getWorkExecutor()); @@ -228,9 +227,14 @@ protected ChannelHandler getDecoder() { } protected ChannelHandler getEncoder() { - return PacketEncoder.INSTANCE; + return PacketEncoder.INSTANCE;//每连上一个链接调用一次, 所有用单利 } + /** + * 每连上一个链接调用一次 + * + * @param pipeline + */ protected void initPipeline(ChannelPipeline pipeline) { pipeline.addLast("decoder", getDecoder()); pipeline.addLast("encoder", getEncoder()); @@ -258,10 +262,4 @@ protected void doStart(Listener listener) throws Throwable { protected void doStop(Listener listener) throws Throwable { } - - private boolean useNettyEpoll() { - if (!"netty".equals(CC.mp.core.epoll_provider)) return false; - String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); - return name.startsWith("linux"); - } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java new file mode 100644 index 00000000..3a4bc1c9 --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java @@ -0,0 +1,123 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.udp; + +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.api.service.Server; +import com.mpush.api.service.ServiceException; +import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.*; +import io.netty.channel.epoll.EpollDatagramChannel; +import io.netty.channel.epoll.EpollEventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.DatagramChannel; +import io.netty.channel.socket.nio.NioDatagramChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Locale; +import java.util.concurrent.Executor; + +import static io.netty.channel.socket.InternetProtocolFamily.IPv4; + +/** + * Created by ohun on 16/10/20. + * + * @author ohun@live.cn (夜色) + */ +public abstract class NettyUDPConnector extends BaseService implements Server { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + protected final int port; + private EventLoopGroup eventLoopGroup; + + public NettyUDPConnector(int port) { + this.port = port; + } + + @Override + protected void doStart(Listener listener) throws Throwable { + createNioServer(listener); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); + if (eventLoopGroup != null) eventLoopGroup.shutdownGracefully().syncUninterruptibly(); + Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); + } + + private void createServer(Listener listener, EventLoopGroup eventLoopGroup, ChannelFactory channelFactory) { + this.eventLoopGroup = eventLoopGroup; + try { + Bootstrap b = new Bootstrap(); + b.group(eventLoopGroup)//默认是根据机器情况创建Channel,如果机器支持ipv6,则无法使用ipv4的地址加入组播 + .channelFactory(channelFactory) + .option(ChannelOption.SO_BROADCAST, true) + .handler(getChannelHandler()); + + initOptions(b); + + ChannelFuture f = b.bind(port).sync();//直接绑定端口,不要指定host,不然收不到组播消息 + + if (f.isSuccess()) { + Logs.Console.info("udp server start success on:{}", port); + if (listener != null) listener.onSuccess(port); + + f.channel().closeFuture().sync(); + } else { + Logs.Console.error("udp server start failure on:{}", port, f.cause()); + if (listener != null) listener.onFailure(f.cause()); + } + } catch (Exception e) { + logger.error("udp server start exception", e); + if (listener != null) listener.onFailure(e); + throw new ServiceException("udp server start exception, port=" + port, e); + } finally { + stop(); + } + } + + private void createNioServer(Listener listener) { + NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1, getWorkExecutor()); + createServer(listener, eventLoopGroup, () -> new NioDatagramChannel(IPv4)); + } + + @SuppressWarnings("unused") + private void createEpollServer(Listener listener) { + EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(1, getWorkExecutor()); + createServer(listener, eventLoopGroup, EpollDatagramChannel::new); + } + + protected void initOptions(Bootstrap b) { + b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + } + + public abstract ChannelHandler getChannelHandler(); + + protected Executor getWorkExecutor() { + return ThreadPoolManager.I.getWorkExecutor(); + } + +} diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java new file mode 100644 index 00000000..1d0aaf12 --- /dev/null +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java @@ -0,0 +1,117 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty.udp; + +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.netty.codec.PacketDecoder; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.log.Logs; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.socket.DatagramChannel; +import io.netty.channel.socket.DatagramPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.NetworkInterface; + +/** + * Created by ohun on 16/10/21. + * + * @author ohun@live.cn (夜色) + */ +@ChannelHandler.Sharable +public final class UDPChannelHandler extends ChannelInboundHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(UDPChannelHandler.class); + private final NettyConnection connection = new NettyConnection(); + private final PacketReceiver receiver; + private InetAddress multicastAddress; + private NetworkInterface networkInterface; + + public UDPChannelHandler(PacketReceiver receiver) { + this.receiver = receiver; + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + connection.init(ctx.channel(), false); + if (multicastAddress != null) { + ((DatagramChannel) ctx.channel()).joinGroup(multicastAddress, networkInterface, null).addListener(future -> { + if (future.isSuccess()) { + Logs.Conn.info("join multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); + } else { + LOGGER.error("join multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); + } + }); + } + Logs.Conn.info("init udp channel={}", ctx.channel()); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + if (multicastAddress != null) { + ((DatagramChannel) ctx.channel()).leaveGroup(multicastAddress, networkInterface, null).addListener(future -> { + if (future.isSuccess()) { + Logs.Conn.info("leave multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); + } else { + LOGGER.error("leave multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); + } + }); + } + Logs.Conn.info("disconnect udp channel={}, connection={}", ctx.channel(), connection); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + DatagramPacket datagramPacket = (DatagramPacket) msg; + Packet packet = PacketDecoder.decodeFrame(datagramPacket); + receiver.onReceive(packet, connection); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + Logs.Conn.error("udp channel caught an exception channel={}, connection={}", ctx.channel(), connection); + LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); + } + + public UDPChannelHandler setMulticastAddress(InetAddress multicastAddress) { + if (!multicastAddress.isMulticastAddress()) { + throw new IllegalArgumentException(multicastAddress + "not a multicastAddress"); + } + + this.multicastAddress = multicastAddress; + return this; + } + + public UDPChannelHandler setNetworkInterface(NetworkInterface networkInterface) { + this.networkInterface = networkInterface; + return this; + } + + public Connection getConnection() { + return connection; + } +} diff --git a/mpush-test/src/test/java/com/mpush/netty/MulticastTest.java b/mpush-test/src/test/java/com/mpush/netty/MulticastTest.java new file mode 100644 index 00000000..b4a783b2 --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/netty/MulticastTest.java @@ -0,0 +1,72 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty; + +import com.mpush.tools.Utils; +import org.junit.Test; + +import java.io.IOException; +import java.net.*; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; + +/** + * Created by ohun on 16/10/21. + * + * @author ohun@live.cn (夜色) + */ +public final class MulticastTest { + @Test + public void TestServer() throws Exception { + //接受组播和发送组播的数据报服务都要把组播地址添加进来 + String host = "239.239.239.88";//多播地址 + int port = 9998; + InetAddress group = InetAddress.getByName(host); + + DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET); + channel.bind(new InetSocketAddress(port)); + channel.join(group, Utils.getLocalNetworkInterface()); + ByteBuffer buffer = ByteBuffer.allocate(1024); + SocketAddress sender = channel.receive(buffer); + buffer.flip(); + byte[] data = new byte[buffer.remaining()]; + buffer.get(data); + System.out.println(new String(data)); + + } + + @Test + public void testSend() throws Exception { + String host = "239.239.239.99";//多播地址 + int port = 9999; + InetAddress group = InetAddress.getByName(host); + String message = "test-multicastSocket"; + + DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET); + channel.configureBlocking(true); + channel.bind(new InetSocketAddress(port)); + channel.join(group, Utils.getLocalNetworkInterface()); + + InetSocketAddress sender = new InetSocketAddress("239.239.239.99", 4000); + channel.send(ByteBuffer.wrap(message.getBytes()), sender); + + channel.close(); + } +} diff --git a/mpush-test/src/test/java/com/mpush/netty/MulticastTest2.java b/mpush-test/src/test/java/com/mpush/netty/MulticastTest2.java new file mode 100644 index 00000000..5e7f88dc --- /dev/null +++ b/mpush-test/src/test/java/com/mpush/netty/MulticastTest2.java @@ -0,0 +1,125 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.netty; + +import com.mpush.tools.Utils; +import org.junit.Test; + +import java.io.IOException; +import java.net.*; + +/** + * Created by ohun on 16/10/21. + * + * @author ohun@live.cn (夜色) + */ +public final class MulticastTest2 { + @Test + public void TestServer() throws Exception { + //接受组播和发送组播的数据报服务都要把组播地址添加进来 + String host = "239.239.239.99";//多播地址 + int port = 9998; + int length = 1024; + byte[] buf = new byte[length]; + MulticastSocket ms = null; + DatagramPacket dp = null; + StringBuffer sbuf = new StringBuffer(); + try { + ms = new MulticastSocket(port); + dp = new DatagramPacket(buf, length); + + //加入多播地址 + ms.joinGroup(new InetSocketAddress(host, port), Utils.getLocalNetworkInterface()); + System.out.println("监听多播端口打开:"); + ms.receive(dp); + ms.close(); + int i; + for (i = 0; i < 1024; i++) { + if (buf[i] == 0) { + break; + } + sbuf.append((char) buf[i]); + } + System.out.println("收到多播消息:" + sbuf.toString()); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Test + public void testSend2() throws Exception { + String host = "239.239.239.99";//多播地址 + int port = 9998; + String message = "test-multicastSocket"; + //接受组播和发送组播的数据报服务都要把组播地址添加进来 + int length = 1024; + byte[] buf = new byte[length]; + MulticastSocket ms = null; + DatagramPacket dp = null; + StringBuffer sbuf = new StringBuffer(); + try { + ms = new MulticastSocket(port); + dp = new DatagramPacket(buf, length); + InetAddress group = InetAddress.getByName(host); + //加入多播地址 + ms.joinGroup(new InetSocketAddress("239.239.239.88", 9999), Utils.getLocalNetworkInterface()); + System.out.println("监听多播端口打开:"); + DatagramPacket dp2 = new DatagramPacket(message.getBytes(), message.length(), group, port); + ms.send(dp2); + ms.receive(dp); + ms.close(); + int i; + for (i = 0; i < 1024; i++) { + if (buf[i] == 0) { + break; + } + sbuf.append((char) buf[i]); + } + System.out.println("收到多播消息:" + sbuf.toString()); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Test + public void testSend() throws Exception { + String host = "239.239.239.99";//多播地址 + int port = 9998; + String message = "test-multicastSocket"; + try { + InetAddress group = InetAddress.getByName(host); + MulticastSocket s = new MulticastSocket(); + //加入多播组 + s.joinGroup(new InetSocketAddress(host, port), Utils.getLocalNetworkInterface()); + DatagramPacket dp = new DatagramPacket(message.getBytes(), message.length(), group, port); + s.send(dp); + s.close(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index cd2d87aa..5f1701a1 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -30,6 +30,7 @@ import com.mpush.zk.node.ZKServerNode; import org.junit.After; import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -38,7 +39,8 @@ public class ConnClientTestMain { - public static void main(String[] args) throws Exception { + @Test + public void testConnClient() throws Exception { ConnClientBoot boot = new ConnClientBoot(); boot.start().get(); @@ -66,5 +68,6 @@ public static void main(String[] args) throws Exception { Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); client.start().get(10, TimeUnit.SECONDS); } + LockSupport.park(); } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 5b55c4aa..d73b5bfe 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -22,10 +22,13 @@ import com.mpush.api.push.*; import com.mpush.api.router.ClientLocation; import com.mpush.tools.log.Logs; +import org.junit.Test; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; +import java.util.concurrent.locks.LockSupport; /** * Created by ohun on 2016/1/7. @@ -33,7 +36,8 @@ * @author ohun@live.cn */ public class PushClientTestMain { - public static void main(String[] args) throws Exception { + @Test + public void testPush() throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().whenComplete((success, throwable) -> { @@ -54,6 +58,8 @@ public void onResult(PushResult result) { }); FutureTask future = sender.send(context); }); + + LockSupport.park(); } } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java index 672bc195..1fd2ba13 100644 --- a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -20,6 +20,9 @@ package com.mpush.test.sever; import com.mpush.bootstrap.Main; +import org.junit.Test; + +import java.util.concurrent.locks.LockSupport; /** * Created by yxx on 2016/5/16. @@ -27,9 +30,11 @@ * @author ohun@live.cn */ public class ServerTestMain { - public static void main(String[] args) { + @Test + public void testServer() { System.setProperty("io.netty.leakDetection.level", "PARANOID"); System.setProperty("io.netty.noKeySetOptimization", "false"); - Main.main(args); + Main.main(null); + LockSupport.park(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index a27f7d24..33125d67 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -24,10 +24,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.NetworkInterface; -import java.net.Socket; +import java.net.*; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -62,6 +59,34 @@ public static String getLocalIp() { return LOCAL_IP; } + public static NetworkInterface getLocalNetworkInterface() { + Enumeration interfaces; + try { + interfaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + throw new RuntimeException("NetworkInterface not found", e); + } + while (interfaces.hasMoreElements()) { + NetworkInterface networkInterface = interfaces.nextElement(); + Enumeration addresses = networkInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + InetAddress address = addresses.nextElement(); + if (address.isLoopbackAddress()) continue; + if (address.getHostAddress().contains(":")) continue; + if (address.isSiteLocalAddress()) return networkInterface; + } + } + throw new RuntimeException("NetworkInterface not found"); + } + + public static InetAddress getInetAddress(String host) { + try { + return InetAddress.getByName(host); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("UnknownHost " + host, e); + } + } + /** * 只获取第一块网卡绑定的ip地址 * diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 74517f45..053c38b7 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -26,10 +26,7 @@ import java.io.File; import java.time.Duration; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; import static java.util.stream.Collectors.toCollection; @@ -77,6 +74,12 @@ interface core { int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times"); String epoll_provider = cfg.getString("epoll-provider"); + + static boolean useNettyEpoll() { + if (!"netty".equals(CC.mp.core.epoll_provider)) return false; + String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim(); + return name.startsWith("linux");//只在linux下使用netty提供的epoll库 + } } interface net { @@ -85,6 +88,16 @@ interface net { int connect_server_port = cfg.getInt("connect-server-port"); int gateway_server_port = cfg.getInt("gateway-server-port"); int admin_server_port = cfg.getInt("admin-server-port"); + int gateway_client_port = cfg.getInt("gateway-client-port"); + + String gateway_server_net = cfg.getString("gateway-server-net"); + String gateway_server_multicast = cfg.getString("gateway-server-multicast"); + String gateway_client_multicast = cfg.getString("gateway-client-multicast"); + + static boolean udpGateway() { + return "udp".equals(gateway_server_net); + } + interface public_ip_mapping { From 010fd7970d19fdca5353e9ac5968510cea9ed56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 22 Oct 2016 01:36:40 +0800 Subject: [PATCH 709/890] =?UTF-8?q?=E7=BD=91=E5=85=B3=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0UDP=E5=8F=8A=E7=BB=84=E6=92=AD=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/GatewayUDPConnectionFactory.java | 25 +++++++++++++++++-- .../com/mpush/client/push/PushClient.java | 2 +- .../mpush/test/push/PushClientTestMain.java | 3 --- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java index 5b5abe44..8484259d 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java @@ -21,6 +21,7 @@ import com.google.common.collect.Maps; import com.mpush.api.connection.Connection; +import com.mpush.api.service.Listener; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.tools.config.CC; import com.mpush.tools.thread.pool.ThreadPoolManager; @@ -31,6 +32,10 @@ import java.net.InetSocketAddress; import java.util.Collection; import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; +import java.util.concurrent.locks.Lock; +import java.util.function.BiConsumer; import java.util.function.Consumer; import static com.mpush.tools.config.CC.mp.net.gateway_server_multicast; @@ -53,8 +58,24 @@ public class GatewayUDPConnectionFactory extends GatewayConnectionFactory connector.start(new Listener() { + @Override + public void onSuccess(Object... args) { + latch.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + latch.countDown(); + } + })).start(); + + //同步 + try { + latch.await(); + } catch (InterruptedException e) { + } } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index a342e134..7e8b727d 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -84,10 +84,10 @@ public void init() { @Override protected void doStart(Listener listener) throws Throwable { + factory.init(); ZKClient.I.start(listener); RedisManager.I.init(); ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).watch(); - factory.init(); PushRequestBus.I.start(listener); } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index d73b5bfe..aaef3bd1 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -20,13 +20,10 @@ package com.mpush.test.push; import com.mpush.api.push.*; -import com.mpush.api.router.ClientLocation; import com.mpush.tools.log.Logs; import org.junit.Test; import java.util.Arrays; -import java.util.List; -import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import java.util.concurrent.locks.LockSupport; From 36cb5709693b7b75abaaf827705631de4c022050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 22 Oct 2016 20:37:47 +0800 Subject: [PATCH 710/890] =?UTF-8?q?=E7=BD=91=E5=85=B3=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0UDP=E5=8F=8A=E7=BB=84=E6=92=AD=E6=94=AF?= =?UTF-8?q?=E6=8C=81,=20PushClient=E6=A8=A1=E5=9D=97=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 4 +- .../java/com/mpush/api/protocol/Packet.java | 3 + .../com/mpush/api/protocol/UDPPacket.java | 19 ++- .../com/mpush/api/service/BaseService.java | 25 ++-- .../main/java/com/mpush/api/spi/Factory.java | 5 +- .../mpush/client/gateway/GatewayClient.java | 11 +- .../gateway/GatewayClientChannelHandler.java | 121 ------------------ .../GatewayConnectionFactory.java | 18 ++- .../GatewayTCPConnectionFactory.java | 30 ++--- .../GatewayUDPConnectionFactory.java | 73 +++++------ .../handler/GatewayClientChannelHandler.java | 80 ++++++++++++ .../gateway/handler/GatewayErrorHandler.java | 26 +++- .../gateway/handler/GatewayOKHandler.java | 16 ++- .../com/mpush/client/push/PushClient.java | 18 +-- .../com/mpush/client/push/PushRequest.java | 88 ++++++------- .../mpush/common/memory/PacketFactory.java | 57 +++++++++ .../message/gateway/GatewayPushMessage.java | 9 +- .../mpush/netty/server/NettyTCPServer.java | 2 +- .../mpush/netty/udp/NettyUDPConnector.java | 11 +- .../mpush/test/push/PushClientTestMain.java | 6 + .../java/com/mpush/tools/common/Holder.java | 48 +++++++ 21 files changed, 389 insertions(+), 281 deletions(-) delete mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java rename mpush-client/src/main/java/com/mpush/client/gateway/{ => connection}/GatewayConnectionFactory.java (68%) rename mpush-client/src/main/java/com/mpush/client/gateway/{ => connection}/GatewayTCPConnectionFactory.java (79%) rename mpush-client/src/main/java/com/mpush/client/gateway/{ => connection}/GatewayUDPConnectionFactory.java (62%) create mode 100644 mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java create mode 100644 mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/Holder.java diff --git a/conf/reference.conf b/conf/reference.conf index c552cb54..6e934cb1 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -41,8 +41,8 @@ mp { gateway-server-port=3001 //网关服务端口, 内部端口 gateway-client-port=4000 //UDP 客户端端口 gateway-server-net=tcp //网关服务使用的网络类型tcp/udp - gateway-server-multicast="239.239.239.88" - gateway-client-multicast="239.239.239.99" + gateway-server-multicast="239.239.239.88" //239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效 + gateway-client-multicast="239.239.239.99" //239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效 public-host-mapping { //本机局域网IP和公网IP的映射关系 "10.0.10.156":"111.1.32.137" "10.0.10.166":"111.1.33.138" diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 38a0b188..252609d5 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -116,6 +116,9 @@ public InetSocketAddress sender() { return null; } + public void sender(InetSocketAddress sender) { + } + public Packet response(Command command) { return new Packet(command, sessionId); } diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java index 66f14842..8a69c27c 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java @@ -27,26 +27,28 @@ * @author ohun@live.cn (夜色) */ public final class UDPPacket extends Packet { - private final InetSocketAddress sender; + private InetSocketAddress sender; public UDPPacket(byte cmd, InetSocketAddress sender) { super(cmd); this.sender = sender; } - public UDPPacket(byte cmd, int sessionId, InetSocketAddress sender) { + public UDPPacket(Command cmd, int sessionId, InetSocketAddress sender) { super(cmd, sessionId); this.sender = sender; } - public UDPPacket(Command cmd, InetSocketAddress sender) { + public UDPPacket(byte cmd) { super(cmd); - this.sender = sender; } - public UDPPacket(Command cmd, int sessionId, InetSocketAddress sender) { + public UDPPacket(Command cmd) { + super(cmd); + } + + public UDPPacket(Command cmd, int sessionId) { super(cmd, sessionId); - this.sender = sender; } @Override @@ -54,6 +56,11 @@ public InetSocketAddress sender() { return sender; } + @Override + public void sender(InetSocketAddress sender) { + this.sender = sender; + } + @Override public Packet response(Command command) { return new UDPPacket(command, sessionId, sender); diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java index 0062f4ab..efcf945b 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -20,6 +20,7 @@ package com.mpush.api.service; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -40,13 +41,15 @@ public boolean isRunning() { return started.get(); } - protected void tryStart(Listener listener, Function function) { - listener = wrap(listener); + protected void tryStart(Listener l, Function function) { + FutureListener listener = wrap(l); if (started.compareAndSet(false, true)) { try { init(); function.apply(listener); - listener.onSuccess(String.format("service %s start success", this.getClass().getSimpleName())); + if (!listener.isDone()) { + listener.onSuccess(String.format("service %s start success", this.getClass().getSimpleName())); + } } catch (Throwable e) { listener.onFailure(e); throw new ServiceException(e); @@ -56,12 +59,14 @@ protected void tryStart(Listener listener, Function function) { } } - protected void tryStop(Listener listener, Function function) { - listener = wrap(listener); + protected void tryStop(Listener l, Function function) { + FutureListener listener = wrap(l); if (started.compareAndSet(true, false)) { try { function.apply(listener); - listener.onSuccess(String.format("service %s stop success", this.getClass().getSimpleName())); + if (!listener.isDone()) { + listener.onSuccess(String.format("service %s stop success", this.getClass().getSimpleName())); + } } catch (Throwable e) { listener.onFailure(e); throw new ServiceException(e); @@ -93,9 +98,13 @@ public void stop(Listener listener) { tryStop(listener, this::doStop); } - protected abstract void doStart(Listener listener) throws Throwable; + protected void doStart(Listener listener) throws Throwable { - protected abstract void doStop(Listener listener) throws Throwable; + } + + protected void doStop(Listener listener) throws Throwable { + + } protected interface Function { void apply(Listener l) throws Throwable; diff --git a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java index 3e99ce53..800b270e 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/Factory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/Factory.java @@ -19,11 +19,12 @@ package com.mpush.api.spi; +import java.util.function.Supplier; + /** * Created by yxx on 2016/5/18. * * @author ohun@live.cn */ -public interface Factory { - T get(); +public interface Factory extends Supplier { } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java index 8703def4..cefc1f6f 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClient.java @@ -20,7 +20,12 @@ package com.mpush.client.gateway; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; import com.mpush.api.service.Listener; +import com.mpush.client.gateway.handler.GatewayClientChannelHandler; +import com.mpush.client.gateway.handler.GatewayErrorHandler; +import com.mpush.client.gateway.handler.GatewayOKHandler; +import com.mpush.common.MessageDispatcher; import com.mpush.netty.client.NettyTCPClient; import com.mpush.tools.thread.NamedPoolThreadFactory; import io.netty.channel.ChannelHandler; @@ -39,12 +44,16 @@ * @author ohun@live.cn */ public class GatewayClient extends NettyTCPClient { - private final GatewayClientChannelHandler handler = new GatewayClientChannelHandler(); + private final GatewayClientChannelHandler handler; private GlobalChannelTrafficShapingHandler trafficShapingHandler; private ScheduledExecutorService trafficShapingExecutor; public GatewayClient(String host, int port) { super(host, port); + MessageDispatcher dispatcher = new MessageDispatcher(); + dispatcher.register(Command.OK, new GatewayOKHandler()); + dispatcher.register(Command.ERROR, new GatewayErrorHandler()); + this.handler = new GatewayClientChannelHandler(dispatcher); if (enabled) { trafficShapingExecutor = Executors.newSingleThreadScheduledExecutor(new NamedPoolThreadFactory(T_TRAFFIC_SHAPING)); trafficShapingHandler = new GlobalChannelTrafficShapingHandler( diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java deleted file mode 100644 index 85ab9a4c..00000000 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayClientChannelHandler.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.client.gateway; - - -import com.mpush.api.connection.Connection; -import com.mpush.api.protocol.Command; -import com.mpush.api.protocol.Packet; -import com.mpush.client.push.PushRequest; -import com.mpush.client.push.PushRequestBus; -import com.mpush.common.message.ErrorMessage; -import com.mpush.common.message.OkMessage; -import com.mpush.netty.connection.NettyConnection; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.mpush.common.ErrorCode.*; - -/** - * Created by ohun on 2015/12/19. - * - * @author ohun@live.cn - */ -@ChannelHandler.Sharable -public final class GatewayClientChannelHandler extends ChannelInboundHandlerAdapter { - - private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); - - private final Connection connection = new NettyConnection(); - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - LOGGER.info("receive gateway packet={}, channel={}", msg, ctx.channel()); - connection.updateLastReadTime(); - if (msg instanceof Packet) { - Packet packet = (Packet) msg; - if (packet.cmd == Command.OK.cmd) { - handleOK(new OkMessage(packet, connection)); - } else if (packet.cmd == Command.ERROR.cmd) { - handleError(new ErrorMessage(packet, connection)); - } - } - } - - public static void handleOK(OkMessage message) { - if (message.cmd == Command.GATEWAY_PUSH.cmd) { - handPush(message, null, message.getPacket()); - } - } - - public static void handleError(ErrorMessage message) { - if (message.cmd == Command.GATEWAY_PUSH.cmd) { - handPush(null, message, message.getPacket()); - } - } - - private static void handPush(OkMessage ok, ErrorMessage error, Packet packet) { - PushRequest request = PushRequestBus.I.getAndRemove(packet.sessionId); - if (request == null) { - LOGGER.warn("receive a gateway response, but request has timeout. ok={}, error={}", ok, error); - return; - } - - if (ok != null) {//推送成功 - request.success(ok.data); - } else if (error != null) {//推送失败 - LOGGER.warn("receive an error gateway response, message={}", error); - if (error.code == OFFLINE.errorCode) {//用户离线 - request.offline(); - } else if (error.code == PUSH_CLIENT_FAILURE.errorCode) {//下发到客户端失败 - request.failure(); - } else if (error.code == ROUTER_CHANGE.errorCode) {//用户路由信息更改 - request.redirect(); - } - } - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - connection.close(); - LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); - connection.init(ctx.channel(), false); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - connection.close(); - LOGGER.info("client disconnect channel={}", ctx.channel()); - //TODO notify gateway-client-factory to removeAndClose this gateway-client - } - - public Connection getConnection() { - return connection; - } -} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java similarity index 68% rename from mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java rename to mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java index a5d6bb0a..e3709a43 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java @@ -17,9 +17,11 @@ * ohun@live.cn (夜色) */ -package com.mpush.client.gateway; +package com.mpush.client.gateway.connection; import com.mpush.api.connection.Connection; +import com.mpush.api.service.Listener; +import com.mpush.common.message.BaseMessage; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.zk.cache.ZKServerNodeCache; import org.slf4j.Logger; @@ -27,28 +29,24 @@ import java.util.Collection; import java.util.function.Consumer; +import java.util.function.Function; /** * Created by yxx on 2016/5/17. * * @author ohun@live.cn */ -public abstract class GatewayConnectionFactory extends ZKServerNodeCache { +public abstract class GatewayConnectionFactory extends ZKServerNodeCache { final Logger logger = LoggerFactory.getLogger(this.getClass()); - public void init() { - + public void init(Listener listener) { } abstract public Connection getConnection(String ip); - abstract public T getNode(String ip); - - abstract public Collection getAllNode(); - - abstract public boolean send(String host, Consumer consumer); + abstract public Function send(Function creator, Function sender); - abstract public void broadcast(Consumer consumer); + abstract public void broadcast(Function creator, Consumer sender); } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayTCPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java similarity index 79% rename from mpush-client/src/main/java/com/mpush/client/gateway/GatewayTCPConnectionFactory.java rename to mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java index 5a1b9111..537c1900 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayTCPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java @@ -17,26 +17,26 @@ * ohun@live.cn (夜色) */ -package com.mpush.client.gateway; +package com.mpush.client.gateway.connection; import com.google.common.collect.Maps; import com.mpush.api.connection.Connection; import com.mpush.api.service.Client; import com.mpush.api.service.Listener; -import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.client.gateway.GatewayClient; +import com.mpush.common.message.BaseMessage; import com.mpush.zk.node.ZKServerNode; -import java.util.Collection; import java.util.Map; import java.util.function.Consumer; -import java.util.stream.Collectors; +import java.util.function.Function; /** * Created by yxx on 2016/5/17. * * @author ohun@live.cn */ -public class GatewayTCPConnectionFactory extends GatewayConnectionFactory { +public class GatewayTCPConnectionFactory extends GatewayConnectionFactory { private final Map ip_client = Maps.newConcurrentMap(); @@ -76,28 +76,16 @@ public Connection getConnection(String ip) { return null; } - @Override - public Connection getNode(String ip) { - return getConnection(ip); - } - - @Override - public Collection getAllNode() { - return ip_client.values().stream().map(GatewayClient::getConnection).collect(Collectors.toList()); - } @Override - public boolean send(String ip, Consumer consumer) { - Connection connection = getConnection(ip); - if (connection == null) return false; - consumer.accept(GatewayPushMessage.build(connection)); - return true; + public Function send(Function creator, Function sender) { + return creator.compose(this::getConnection).andThen(sender); } @Override - public void broadcast(Consumer consumer) { + public void broadcast(Function creator, Consumer sender) { ip_client.forEach((s, client) -> { - consumer.accept(GatewayPushMessage.build(client.getConnection())); + sender.accept(creator.apply(client.getConnection())); }); } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java similarity index 62% rename from mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java rename to mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java index 8484259d..16f0f885 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java @@ -17,26 +17,23 @@ * ohun@live.cn (夜色) */ -package com.mpush.client.gateway; +package com.mpush.client.gateway.connection; import com.google.common.collect.Maps; import com.mpush.api.connection.Connection; import com.mpush.api.service.Listener; -import com.mpush.common.message.gateway.GatewayPushMessage; -import com.mpush.tools.config.CC; +import com.mpush.client.gateway.GatewayUDPConnector; +import com.mpush.common.message.BaseMessage; +import com.mpush.tools.common.Holder; import com.mpush.tools.thread.pool.ThreadPoolManager; import com.mpush.zk.node.ZKServerNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; -import java.util.Collection; import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Semaphore; -import java.util.concurrent.locks.Lock; -import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.function.Function; import static com.mpush.tools.config.CC.mp.net.gateway_server_multicast; import static com.mpush.tools.config.CC.mp.net.gateway_server_port; @@ -46,7 +43,7 @@ * * @author ohun@live.cn */ -public class GatewayUDPConnectionFactory extends GatewayConnectionFactory { +public class GatewayUDPConnectionFactory extends GatewayConnectionFactory { private final Logger logger = LoggerFactory.getLogger(GatewayUDPConnectionFactory.class); @@ -57,25 +54,8 @@ public class GatewayUDPConnectionFactory extends GatewayConnectionFactory connector.start(new Listener() { - @Override - public void onSuccess(Object... args) { - latch.countDown(); - } - - @Override - public void onFailure(Throwable cause) { - latch.countDown(); - } - })).start(); - - //同步 - try { - latch.await(); - } catch (InterruptedException e) { - } + public void init(Listener listener) { + ThreadPoolManager.I.newThread("udp-client", () -> connector.start(listener)).start(); } @Override @@ -95,6 +75,7 @@ public ZKServerNode remove(String fullPath) { public void clear() { super.clear(); ip_address.clear(); + connector.stop(); } @Override @@ -103,25 +84,31 @@ public Connection getConnection(String ip) { } @Override - public InetSocketAddress getNode(String ip) { - return ip_address.get(ip); - } + public Function send(Function creator, Function sender) { - @Override - public Collection getAllNode() { - return ip_address.values(); - } + Holder holder = new Holder<>(); - @Override - public boolean send(String host, Consumer consumer) { - InetSocketAddress recipient = ip_address.get(host); - if (recipient == null) return false; - consumer.accept(GatewayPushMessage.build(connector.getConnection(), recipient)); - return true; + Function getConn = host -> { + InetSocketAddress recipient = ip_address.get(host); + if (recipient == null) return null; + holder.set(recipient); + return connector.getConnection(); + }; + + Function setRecipientFun = message -> { + if (message != null) { + message.getPacket().sender(holder.get()); + } + return message; + }; + + return creator.compose(getConn).andThen(setRecipientFun).andThen(sender); } @Override - public void broadcast(Consumer consumer) { - consumer.accept(GatewayPushMessage.build(connector.getConnection(), multicastRecipient)); + public void broadcast(Function creator, Consumer sender) { + M message = creator.apply(connector.getConnection()); + message.getPacket().sender(multicastRecipient); + sender.accept(message); } } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java new file mode 100644 index 00000000..31d369cf --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java @@ -0,0 +1,80 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.gateway.handler; + + +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.netty.connection.NettyConnection; +import com.mpush.tools.log.Logs; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by ohun on 2015/12/19. + * + * @author ohun@live.cn + */ +@ChannelHandler.Sharable +public final class GatewayClientChannelHandler extends ChannelInboundHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(GatewayClientChannelHandler.class); + + private final Connection connection = new NettyConnection(); + + private final PacketReceiver receiver; + + public GatewayClientChannelHandler(PacketReceiver receiver) { + this.receiver = receiver; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + Logs.Conn.info("receive gateway packet={}, channel={}", msg, ctx.channel()); + Packet packet = (Packet) msg; + receiver.onReceive(packet, connection); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connection.close(); + LOGGER.error("caught an ex, channel={}", ctx.channel(), cause); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + Logs.Conn.info("client connect channel={}", ctx.channel()); + connection.init(ctx.channel(), false); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connection.close(); + Logs.Conn.info("client disconnect channel={}", ctx.channel()); + } + + public Connection getConnection() { + return connection; + } +} \ No newline at end of file diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java index e9250b83..e9eca7f6 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayErrorHandler.java @@ -20,10 +20,17 @@ package com.mpush.client.gateway.handler; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.client.gateway.GatewayClientChannelHandler; +import com.mpush.client.push.PushRequest; +import com.mpush.client.push.PushRequestBus; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.ErrorMessage; +import com.mpush.tools.log.Logs; + +import static com.mpush.common.ErrorCode.OFFLINE; +import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.mpush.common.ErrorCode.ROUTER_CHANGE; /** * Created by ohun on 16/10/21. @@ -39,6 +46,21 @@ public ErrorMessage decode(Packet packet, Connection connection) { @Override public void handle(ErrorMessage message) { - GatewayClientChannelHandler.handleError(message); + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + PushRequest request = PushRequestBus.I.getAndRemove(message.getSessionId()); + if (request == null) { + Logs.PUSH.warn("receive a gateway response, but request has timeout. message={}", message); + return; + } + + Logs.PUSH.warn("receive an error gateway response, message={}", message); + if (message.code == OFFLINE.errorCode) {//用户离线 + request.offline(); + } else if (message.code == PUSH_CLIENT_FAILURE.errorCode) {//下发到客户端失败 + request.failure(); + } else if (message.code == ROUTER_CHANGE.errorCode) {//用户路由信息更改 + request.redirect(); + } + } } } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java index 8591341b..6a445481 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java @@ -20,13 +20,15 @@ package com.mpush.client.gateway.handler; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; -import com.mpush.client.gateway.GatewayClientChannelHandler; import com.mpush.client.push.PushRequest; import com.mpush.client.push.PushRequestBus; import com.mpush.common.handler.BaseMessageHandler; -import com.mpush.common.handler.OkMessageHandler; import com.mpush.common.message.OkMessage; +import com.mpush.tools.log.Logs; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by ohun on 16/10/21. @@ -34,6 +36,7 @@ * @author ohun@live.cn (夜色) */ public final class GatewayOKHandler extends BaseMessageHandler { + @Override public OkMessage decode(Packet packet, Connection connection) { return new OkMessage(packet, connection); @@ -41,6 +44,13 @@ public OkMessage decode(Packet packet, Connection connection) { @Override public void handle(OkMessage message) { - GatewayClientChannelHandler.handleOK(message); + if (message.cmd == Command.GATEWAY_PUSH.cmd) { + PushRequest request = PushRequestBus.I.getAndRemove(message.getSessionId()); + if (request == null) { + Logs.PUSH.warn("receive a gateway response, but request has timeout. message={}", message); + return; + } + request.success(message.data);//推送成功 + } } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 7e8b727d..d5efd2b6 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -19,13 +19,15 @@ package com.mpush.client.push; -import com.mpush.api.push.*; +import com.mpush.api.push.PushContext; +import com.mpush.api.push.PushException; +import com.mpush.api.push.PushSender; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.client.gateway.GatewayConnectionFactory; -import com.mpush.client.gateway.GatewayTCPConnectionFactory; -import com.mpush.client.gateway.GatewayUDPConnectionFactory; +import com.mpush.client.gateway.connection.GatewayConnectionFactory; +import com.mpush.client.gateway.connection.GatewayTCPConnectionFactory; +import com.mpush.client.gateway.connection.GatewayUDPConnectionFactory; import com.mpush.common.router.CachedRemoteRouterManager; import com.mpush.common.router.RemoteRouter; import com.mpush.tools.config.CC; @@ -84,17 +86,17 @@ public void init() { @Override protected void doStart(Listener listener) throws Throwable { - factory.init(); - ZKClient.I.start(listener); + ZKClient.I.start(); RedisManager.I.init(); ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).watch(); - PushRequestBus.I.start(listener); + PushRequestBus.I.start(); + factory.init(listener); } @Override protected void doStop(Listener listener) throws Throwable { factory.clear(); - ZKClient.I.stop(listener); + ZKClient.I.stop(); RedisManager.I.destroy(); PushRequestBus.I.stop(listener); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 78c4b545..262924a9 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -22,7 +22,7 @@ import com.mpush.api.Constants; import com.mpush.api.push.*; import com.mpush.api.router.ClientLocation; -import com.mpush.client.gateway.GatewayConnectionFactory; +import com.mpush.client.gateway.connection.GatewayConnectionFactory; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.common.router.CachedRemoteRouterManager; import com.mpush.common.router.RemoteRouter; @@ -36,7 +36,6 @@ import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Consumer; /** * Created by ohun on 2015/12/30. @@ -78,38 +77,40 @@ private void sendToConnServer(RemoteRouter remoteRouter) { return; } - timeLine.addTimePoint("get-gateway-conn"); + timeLine.addTimePoint("check-gateway-conn"); //2.通过网关连接,把消息发送到所在机器 - timeLine.addTimePoint("send-to-gateway-begin"); - - boolean success = connectionFactory.send(location.getHost(), new Consumer() { - @Override - public void accept(GatewayPushMessage pushMessage) { - pushMessage - .setUserId(userId) - .setContent(content) - .setClientType(location.getClientType()) - .setTimeout(timeout - 500) - .setTags(tags) - .addFlag(ackModel.flag); - - pushMessage.sendRaw(f -> { - timeLine.addTimePoint("send-to-gateway-end"); - if (!f.isSuccess()) failure(); - }); - PushRequest.this.content = null;//释放内存 - timeLine.addTimePoint("put-request-bus"); - future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); - } - }); - - if (!success) { - LOGGER.error("get gateway connection failure, location={}", location); - failure(); - } + connectionFactory.send( + connection -> { + if (connection == null) { + LOGGER.error("get gateway connection failure, location={}", location); + failure(); + return null; + } + + return GatewayPushMessage.build(connection) + .setUserId(userId) + .setContent(content) + .setClientType(location.getClientType()) + .setTimeout(timeout - 500) + .setTags(tags) + .addFlag(ackModel.flag); + }, + pushMessage -> { + if (pushMessage != null) { + timeLine.addTimePoint("send-to-gateway-begin"); + pushMessage.sendRaw(f -> { + timeLine.addTimePoint("send-to-gateway-end"); + if (!f.isSuccess()) failure(); + }); + PushRequest.this.content = null;//释放内存 + future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + } + return null; + } + ).apply(location.getHost()); } private void submit(Status status) { @@ -171,21 +172,22 @@ public FutureTask offline() { public FutureTask broadcast() { timeLine.begin(); - Consumer consumer = pushMessage -> { - pushMessage - .setUserId(userId) - .setContent(content) - .setTags(tags) - .addFlag(ackModel.flag); - - pushMessage.sendRaw(f -> { - if (!f.isSuccess()) failure(); - }); + connectionFactory.broadcast( + connection -> GatewayPushMessage + .build(connection) + .setUserId(userId) + .setContent(content) + .setTags(tags) + .addFlag(ackModel.flag), - future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); - }; + pushMessage -> { + pushMessage.sendRaw(f -> { + if (!f.isSuccess()) failure(); + }); + future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + } + ); - connectionFactory.broadcast(consumer); return this; } diff --git a/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java b/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java new file mode 100644 index 00000000..2ae63b83 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java @@ -0,0 +1,57 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.memory; + +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.api.protocol.UDPPacket; +import com.mpush.tools.config.CC; + +/** + * Created by ohun on 16/10/22. + * + * @author ohun@live.cn (夜色) + */ +public interface PacketFactory { + PacketFactory FACTORY = CC.mp.net.udpGateway() ? new UDPPacketCreator() : new TCPPacketCreator(); + + static Packet get(Command command) { + return FACTORY.create(command); + } + + Packet create(Command command); +} + + +class TCPPacketCreator implements PacketFactory { + + @Override + public Packet create(Command command) { + return new Packet(command); + } +} + +class UDPPacketCreator implements PacketFactory { + + @Override + public Packet create(Command command) { + return new UDPPacket(command); + } +} \ No newline at end of file diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index de8cb56f..0a270e86 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -23,6 +23,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import com.mpush.api.protocol.UDPPacket; +import com.mpush.common.memory.PacketFactory; import com.mpush.common.message.ByteBufMessage; import com.mpush.tools.Jsons; import io.netty.buffer.ByteBuf; @@ -49,13 +50,9 @@ public GatewayPushMessage(Packet message, Connection connection) { super(message, connection); } - public static GatewayPushMessage build(Connection connection, InetSocketAddress sender) { - Packet packet = new UDPPacket(GATEWAY_PUSH, genSessionId(), sender); - return new GatewayPushMessage(packet, connection); - } - public static GatewayPushMessage build(Connection connection) { - Packet packet = new Packet(GATEWAY_PUSH, genSessionId()); + Packet packet = PacketFactory.get(GATEWAY_PUSH); + packet.sessionId = genSessionId(); return new GatewayPushMessage(packet, connection); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index 437e9469..f9e1465b 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -77,7 +77,7 @@ public boolean isRunning() { @Override public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { - if (listener != null) listener.onFailure(new IllegalStateException("server was already shutdown.")); + if (listener != null) listener.onFailure(new ServiceException("server was already shutdown.")); Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName()); return; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java index 3a4bc1c9..b0de7314 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java @@ -63,9 +63,12 @@ protected void doStart(Listener listener) throws Throwable { @Override protected void doStop(Listener listener) throws Throwable { - Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); - if (eventLoopGroup != null) eventLoopGroup.shutdownGracefully().syncUninterruptibly(); - Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); + if (isRunning()) { + Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); + if (eventLoopGroup != null) eventLoopGroup.shutdownGracefully().syncUninterruptibly(); + Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); + listener.onSuccess(port); + } } private void createServer(Listener listener, EventLoopGroup eventLoopGroup, ChannelFactory channelFactory) { @@ -95,7 +98,7 @@ private void createServer(Listener listener, EventLoopGroup eventLoopGroup, Chan if (listener != null) listener.onFailure(e); throw new ServiceException("udp server start exception, port=" + port, e); } finally { - stop(); + stop(null); } } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index aaef3bd1..fdf0fd10 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; /** @@ -38,6 +39,10 @@ public void testPush() throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().whenComplete((success, throwable) -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); msg.setMsgId("msgId_0"); @@ -50,6 +55,7 @@ public void testPush() throws Exception { .setCallback(new PushCallback() { @Override public void onResult(PushResult result) { + System.out.println(); System.err.println(result); } }); diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Holder.java b/mpush-tools/src/main/java/com/mpush/tools/common/Holder.java new file mode 100644 index 00000000..f271d2e3 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Holder.java @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.tools.common; + +/** + * Created by ohun on 16/10/22. + * + * @author ohun@live.cn (夜色) + */ +public final class Holder { + private T t; + + public Holder() { + } + + public Holder(T t) { + this.t = t; + } + + public static Holder of(T t) { + return new Holder<>(t); + } + + public T get() { + return t; + } + + public void set(T t) { + this.t = t; + } +} From c18cd3e3423cf3aa8b4d473e18323d9e1d31b533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 23 Oct 2016 00:05:59 +0800 Subject: [PATCH 711/890] =?UTF-8?q?Service=E6=A8=A1=E5=9D=97=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=90=AF=E5=8A=A8/=E5=81=9C=E6=AD=A2=EF=BC=8C?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/service/BaseService.java | 39 ++++++++++++++----- .../java/com/mpush/api/service/Service.java | 4 ++ .../com/mpush/bootstrap/job/ServerBoot.java | 6 +-- .../GatewayTCPConnectionFactory.java | 5 +-- .../com/mpush/client/push/PushClient.java | 6 +-- .../com/mpush/client/push/PushRequest.java | 5 +-- .../com/mpush/client/push/PushRequestBus.java | 4 +- .../net/HttpProxyDnsMappingManager.java | 3 ++ .../core/server/GatewayUDPConnector.java | 5 --- .../mpush/monitor/service/MonitorService.java | 2 + .../mpush/netty/client/NettyTCPClient.java | 1 + .../com/mpush/netty/http/NettyHttpClient.java | 2 + .../mpush/netty/server/NettyTCPServer.java | 15 +------ .../mpush/netty/udp/NettyUDPConnector.java | 16 ++++---- .../com/mpush/test/client/ConnClientBoot.java | 20 ++++++++-- .../mpush/test/push/PushClientTestMain.java | 5 --- .../src/main/java/com/mpush/zk/ZKClient.java | 1 + 17 files changed, 77 insertions(+), 62 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java index efcf945b..ad902c4b 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -20,7 +20,9 @@ package com.mpush.api.service; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -47,9 +49,7 @@ protected void tryStart(Listener l, Function function) { try { init(); function.apply(listener); - if (!listener.isDone()) { - listener.onSuccess(String.format("service %s start success", this.getClass().getSimpleName())); - } + listener.monitor(this); } catch (Throwable e) { listener.onFailure(e); throw new ServiceException(e); @@ -64,9 +64,7 @@ protected void tryStop(Listener l, Function function) { if (started.compareAndSet(true, false)) { try { function.apply(listener); - if (!listener.isDone()) { - listener.onSuccess(String.format("service %s stop success", this.getClass().getSimpleName())); - } + listener.monitor(this); } catch (Throwable e) { listener.onFailure(e); throw new ServiceException(e); @@ -88,6 +86,16 @@ public final CompletableFuture stop() { return listener; } + @Override + public final boolean syncStart() { + return start().join(); + } + + @Override + public final boolean syncStop() { + return stop().join(); + } + @Override public void start(Listener listener) { tryStart(listener, this::doStart); @@ -99,11 +107,11 @@ public void stop(Listener listener) { } protected void doStart(Listener listener) throws Throwable { - + listener.onSuccess(); } protected void doStop(Listener listener) throws Throwable { - + listener.onSuccess(); } protected interface Function { @@ -145,7 +153,20 @@ public void onFailure(Throwable cause) { if (isDone()) return;// 防止Listener被重复执行 completeExceptionally(cause); if (l != null) l.onFailure(cause); - throw new ServiceException(cause); + throw cause instanceof ServiceException + ? (ServiceException) cause + : new ServiceException(cause); + } + + public void monitor(BaseService service) { + if (isDone()) return; + runAsync(() -> { + try { + this.get(10, TimeUnit.SECONDS); + } catch (Exception e) { + this.onFailure(new ServiceException(String.format("service %s monitor timeout", service.getClass().getSimpleName()))); + } + }); } @Override diff --git a/mpush-api/src/main/java/com/mpush/api/service/Service.java b/mpush-api/src/main/java/com/mpush/api/service/Service.java index c192bf3c..6a7bc4bc 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/Service.java +++ b/mpush-api/src/main/java/com/mpush/api/service/Service.java @@ -36,6 +36,10 @@ public interface Service { CompletableFuture stop(); + boolean syncStart(); + + boolean syncStop(); + void init(); boolean isRunning(); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 07ca87f0..43cda219 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -69,11 +69,7 @@ public void onFailure(Throwable cause) { @Override protected void stop() { - try { - server.stop().get(1, TimeUnit.MINUTES); - } catch (Exception e) { - Logs.Console.error("stop server error:", e); - } + server.stop().join(); stopNext(); } } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java index 537c1900..c31f034a 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java @@ -21,6 +21,7 @@ import com.google.common.collect.Maps; import com.mpush.api.connection.Connection; +import com.mpush.api.service.BaseService; import com.mpush.api.service.Client; import com.mpush.api.service.Listener; import com.mpush.client.gateway.GatewayClient; @@ -57,9 +58,7 @@ public ZKServerNode remove(String fullPath) { @Override public void clear() { super.clear(); - for (GatewayClient client : ip_client.values()) { - client.stop(null); - } + ip_client.values().forEach(BaseService::stop); } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index d5efd2b6..ef4a5c2c 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -86,17 +86,17 @@ public void init() { @Override protected void doStart(Listener listener) throws Throwable { - ZKClient.I.start(); + ZKClient.I.syncStart(); RedisManager.I.init(); ZKServerNodeWatcher.build(GATEWAY_SERVER, factory).watch(); - PushRequestBus.I.start(); + PushRequestBus.I.syncStart(); factory.init(listener); } @Override protected void doStop(Listener listener) throws Throwable { factory.clear(); - ZKClient.I.stop(); + ZKClient.I.syncStop(); RedisManager.I.destroy(); PushRequestBus.I.stop(listener); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 262924a9..4f7adeb9 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -77,13 +77,10 @@ private void sendToConnServer(RemoteRouter remoteRouter) { return; } - timeLine.addTimePoint("check-gateway-conn"); - - //2.通过网关连接,把消息发送到所在机器 - connectionFactory.send( connection -> { + timeLine.addTimePoint("check-gateway-conn"); if (connection == null) { LOGGER.error("get gateway connection failure, location={}", location); failure(); diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 54e7155d..9437ec3f 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -67,11 +67,13 @@ protected void doStart(Listener listener) throws Throwable { logger.error("one push request was rejected, request=" + r); throw new PushException("one push request was rejected. request=" + r); }); + listener.onSuccess(); } @Override protected void doStop(Listener listener) throws Throwable { scheduledExecutor.shutdown(); - ((ExecutorService)executor).shutdown(); + ((ExecutorService) executor).shutdown(); + listener.onSuccess(); } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index 657f987e..8ae62750 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -55,6 +55,7 @@ public class HttpProxyDnsMappingManager extends BaseService implements DnsMappin public HttpProxyDnsMappingManager() { } + @Override protected void doStart(Listener listener) throws Throwable { watcher.watch(); @@ -62,6 +63,7 @@ protected void doStart(Listener listener) throws Throwable { executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns } + listener.onSuccess(); } @Override @@ -69,6 +71,7 @@ protected void doStop(Listener listener) throws Throwable { if (executorService != null) { executorService.shutdown(); } + listener.onSuccess(); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java index fe121eb9..cd5b1a34 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java @@ -56,11 +56,6 @@ public void init() { channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface()); } - @Override - public void stop(Listener listener) { - super.stop(listener); - } - @Override protected void initOptions(Bootstrap b) { super.initOptions(b); diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java index ed784171..cd221568 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -83,11 +83,13 @@ protected void doStart(Listener listener) throws Throwable { thread = ThreadPoolManager.I.newThread("monitor", this); thread.start(); } + listener.onSuccess(); } @Override protected void doStop(Listener listener) throws Throwable { if (thread != null && thread.isAlive()) thread.interrupt(); + listener.onSuccess(); } private void dump() { diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java index 93fcfd16..7ef37f4a 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java @@ -136,6 +136,7 @@ protected void doStop(Listener listener) throws Throwable { workerGroup.shutdownGracefully(); } LOGGER.error("netty client [{}] stopped.", this.getClass().getSimpleName()); + listener.onSuccess(); } public String getHost() { diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index e62093ab..b5588175 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -133,6 +133,7 @@ public void initChannel(SocketChannel ch) throws Exception { } }); timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), 1, TimeUnit.SECONDS, 64); + listener.onSuccess(); } @Override @@ -140,5 +141,6 @@ protected void doStop(Listener listener) throws Throwable { pool.close(); workerGroup.shutdownGracefully(); timer.stop(); + listener.onSuccess(); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index f9e1465b..82b5df47 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -178,10 +178,7 @@ public void initChannel(SocketChannel ch) throws Exception {//每连上一个链 if (listener != null) listener.onFailure(e); throw new ServiceException("server start exception, port=" + port, e); } finally { - /*** - * 优雅关闭 - */ - stop(null); + if (isRunning()) stop(null); } } @@ -252,14 +249,4 @@ protected Executor getWorkExecutor() { protected int getIoRate() { return 70; } - - @Override - protected void doStart(Listener listener) throws Throwable { - - } - - @Override - protected void doStop(Listener listener) throws Throwable { - - } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java index b0de7314..8c5fe516 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java @@ -63,12 +63,10 @@ protected void doStart(Listener listener) throws Throwable { @Override protected void doStop(Listener listener) throws Throwable { - if (isRunning()) { - Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); - if (eventLoopGroup != null) eventLoopGroup.shutdownGracefully().syncUninterruptibly(); - Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); - listener.onSuccess(port); - } + Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); + if (eventLoopGroup != null) eventLoopGroup.shutdownGracefully().syncUninterruptibly(); + Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); + listener.onSuccess(port); } private void createServer(Listener listener, EventLoopGroup eventLoopGroup, ChannelFactory channelFactory) { @@ -98,18 +96,18 @@ private void createServer(Listener listener, EventLoopGroup eventLoopGroup, Chan if (listener != null) listener.onFailure(e); throw new ServiceException("udp server start exception, port=" + port, e); } finally { - stop(null); + if (isRunning()) stop(); } } private void createNioServer(Listener listener) { - NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1, getWorkExecutor()); + NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, getWorkExecutor()); createServer(listener, eventLoopGroup, () -> new NioDatagramChannel(IPv4)); } @SuppressWarnings("unused") private void createEpollServer(Listener listener) { - EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(1, getWorkExecutor()); + EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(0, getWorkExecutor()); createServer(listener, eventLoopGroup, EpollDatagramChannel::new); } diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java index a4699f72..31240687 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java @@ -22,6 +22,7 @@ import com.google.common.collect.Lists; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; +import com.mpush.api.service.ServiceException; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; @@ -38,14 +39,25 @@ public List getServers() { @Override protected void doStart(Listener listener) throws Throwable { - ZKClient.I.start(listener); - RedisManager.I.init(); - watcher.watch(); + ZKClient.I.start(new Listener() { + @Override + public void onSuccess(Object... args) { + RedisManager.I.init(); + watcher.watch(); + listener.onSuccess(); + } + + @Override + public void onFailure(Throwable cause) { + listener.onFailure(cause); + } + }); } @Override protected void doStop(Listener listener) throws Throwable { - ZKClient.I.stop(); + ZKClient.I.syncStop(); RedisManager.I.destroy(); + listener.onSuccess(); } } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index fdf0fd10..2de4ca25 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -39,11 +39,6 @@ public void testPush() throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().whenComplete((success, throwable) -> { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); msg.setMsgId("msgId_0"); diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 1e5085c2..724c1874 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -83,6 +83,7 @@ protected void doStop(Listener listener) throws Throwable { TimeUnit.MILLISECONDS.sleep(600); client.close(); Logs.Console.info("zk client closed..."); + listener.onSuccess(); } /** From e772726da3e2baf75afa1bd265184ae92a48c2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 23 Oct 2016 10:56:10 +0800 Subject: [PATCH 712/890] =?UTF-8?q?Service=E6=A8=A1=E5=9D=97=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=90=AF=E5=8A=A8/=E5=81=9C=E6=AD=A2=EF=BC=8C?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connection/GatewayConnectionFactory.java | 7 +++++++ .../main/java/com/mpush/client/push/PushClient.java | 13 ++----------- .../com/mpush/test/push/PushClientTestMain.java | 9 ++++----- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java index e3709a43..b24809ee 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java @@ -21,8 +21,10 @@ import com.mpush.api.connection.Connection; import com.mpush.api.service.Listener; +import com.mpush.api.spi.Factory; import com.mpush.common.message.BaseMessage; import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.tools.config.CC; import com.mpush.zk.cache.ZKServerNodeCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +42,12 @@ public abstract class GatewayConnectionFactory extends ZKServerNodeCache { final Logger logger = LoggerFactory.getLogger(this.getClass()); + public static GatewayConnectionFactory create() { + return CC.mp.net.udpGateway() ? new GatewayUDPConnectionFactory() : new GatewayTCPConnectionFactory(); + } + public void init(Listener listener) { + listener.onSuccess(); } abstract public Connection getConnection(String ip); diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index ef4a5c2c..0e625f1a 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -40,7 +40,7 @@ import static com.mpush.zk.ZKPath.GATEWAY_SERVER; /*package*/ final class PushClient extends BaseService implements PushSender { - private GatewayConnectionFactory factory; + private final GatewayConnectionFactory factory = GatewayConnectionFactory.create(); private FutureTask send0(PushContext ctx) { if (ctx.isBroadcast()) { @@ -75,15 +75,6 @@ public FutureTask send(PushContext ctx) { } } - @Override - public void init() { - if (CC.mp.net.udpGateway()) { - factory = new GatewayUDPConnectionFactory(); - } else { - factory = new GatewayTCPConnectionFactory(); - } - } - @Override protected void doStart(Listener listener) throws Throwable { ZKClient.I.syncStart(); @@ -95,8 +86,8 @@ protected void doStart(Listener listener) throws Throwable { @Override protected void doStop(Listener listener) throws Throwable { - factory.clear(); ZKClient.I.syncStop(); + factory.clear(); RedisManager.I.destroy(); PushRequestBus.I.stop(listener); } diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 2de4ca25..3f45c995 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -43,21 +43,20 @@ public void testPush() throws Exception { msg.setMsgId("msgId_0"); PushContext context = PushContext.build(msg) - .setBroadcast(false) + .setBroadcast(true) .setAckModel(AckModel.AUTO_ACK) - .setUserIds(Arrays.asList("user-0", "user-1")) + //.setUserIds(Arrays.asList("user-0", "user-1")) .setTimeout(2000) .setCallback(new PushCallback() { @Override public void onResult(PushResult result) { - System.out.println(); - System.err.println(result); + System.err.println("\n\n" + result); } }); FutureTask future = sender.send(context); }); - LockSupport.park(); + LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3)); } } \ No newline at end of file From 9971c98b5373088c5ac97c0bb0fe9aaa3c5aecd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 23 Oct 2016 10:56:35 +0800 Subject: [PATCH 713/890] =?UTF-8?q?Service=E6=A8=A1=E5=9D=97=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=90=AF=E5=8A=A8/=E5=81=9C=E6=AD=A2=EF=BC=8C?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/com/mpush/test/push/PushClientTestMain.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 3f45c995..6aea0e7f 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -43,9 +43,9 @@ public void testPush() throws Exception { msg.setMsgId("msgId_0"); PushContext context = PushContext.build(msg) - .setBroadcast(true) + .setBroadcast(false) .setAckModel(AckModel.AUTO_ACK) - //.setUserIds(Arrays.asList("user-0", "user-1")) + .setUserIds(Arrays.asList("user-0", "user-1")) .setTimeout(2000) .setCallback(new PushCallback() { @Override From ccfd34f5eb05093aa0fd6dd26b67abd8905ac28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 24 Oct 2016 09:48:07 +0800 Subject: [PATCH 714/890] =?UTF-8?q?=E8=B8=A2=E4=BA=BA=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0UDP=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- .../java/com/mpush/api/protocol/Packet.java | 2 +- .../com/mpush/api/protocol/UDPPacket.java | 14 +- .../com/mpush/bootstrap/ServerLauncher.java | 18 +-- .../client/gateway/GatewayUDPConnector.java | 13 +- .../GatewayUDPConnectionFactory.java | 6 +- .../com/mpush/common/message/AckMessage.java | 9 +- .../com/mpush/common/message/BaseMessage.java | 6 + .../common/message/HttpRequestMessage.java | 2 +- .../common/message/HttpResponseMessage.java | 2 +- .../gateway/GatewayKickUserMessage.java | 132 ++++++++++++++++++ .../message/gateway/GatewayPushMessage.java | 2 +- .../com/mpush/common/net/KickRemoteMsg.java | 37 +++++ .../com/mpush/core/handler/AdminHandler.java | 9 +- .../core/handler/GatewayKickUserHandler.java | 43 ++++++ .../com/mpush/core/router/KickRemoteMsg.java | 46 ------ .../core/router/RedisKickRemoteMessage.java | 96 +++++++++++++ .../com/mpush/core/router/RouterCenter.java | 4 + .../core/router/RouterChangeListener.java | 50 +++++-- .../com/mpush/core/server/AdminServer.java | 31 ++-- .../mpush/core/server/ConnectionServer.java | 15 +- .../com/mpush/core/server/GatewayServer.java | 14 +- .../core/server/GatewayUDPConnector.java | 24 +++- .../src/test/resources/application.conf | 1 + .../src/main/java/com/mpush/zk/ZKClient.java | 35 +++-- 25 files changed, 482 insertions(+), 131 deletions(-) create mode 100644 mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java create mode 100644 mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java create mode 100644 mpush-core/src/main/java/com/mpush/core/handler/GatewayKickUserHandler.java delete mode 100644 mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java create mode 100644 mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java diff --git a/conf/reference.conf b/conf/reference.conf index 6e934cb1..5db692eb 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -81,7 +81,7 @@ mp { zk { server-address="127.0.0.1:2181" namespace=mpush - digest=mpush //addauth digest mpush + digest=mpush //zkCli.sh acl 命令 addauth digest mpush local-cache-path=/ retry { #initial amount of time to wait between retries diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 252609d5..f2ca0154 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -116,7 +116,7 @@ public InetSocketAddress sender() { return null; } - public void sender(InetSocketAddress sender) { + public void setRecipient(InetSocketAddress sender) { } public Packet response(Command command) { diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java index 8a69c27c..ca68cfe4 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java @@ -27,16 +27,16 @@ * @author ohun@live.cn (夜色) */ public final class UDPPacket extends Packet { - private InetSocketAddress sender; + private InetSocketAddress address; public UDPPacket(byte cmd, InetSocketAddress sender) { super(cmd); - this.sender = sender; + this.address = sender; } public UDPPacket(Command cmd, int sessionId, InetSocketAddress sender) { super(cmd, sessionId); - this.sender = sender; + this.address = sender; } public UDPPacket(byte cmd) { @@ -53,16 +53,16 @@ public UDPPacket(Command cmd, int sessionId) { @Override public InetSocketAddress sender() { - return sender; + return address; } @Override - public void sender(InetSocketAddress sender) { - this.sender = sender; + public void setRecipient(InetSocketAddress recipient) { + this.address = recipient; } @Override public Packet response(Command command) { - return new UDPPacket(command, sessionId, sender); + return new UDPPacket(command, sessionId, address); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 24029571..faf31dce 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -20,17 +20,15 @@ package com.mpush.bootstrap; -import com.mpush.api.service.Server; -import com.mpush.api.service.Service; import com.mpush.bootstrap.job.*; import com.mpush.core.server.AdminServer; import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.GatewayServer; import com.mpush.core.server.GatewayUDPConnector; -import com.mpush.zk.node.ZKServerNode; -import static com.mpush.tools.config.CC.mp.net.admin_server_port; import static com.mpush.tools.config.CC.mp.net.udpGateway; +import static com.mpush.zk.node.ZKServerNode.csNode; +import static com.mpush.zk.node.ZKServerNode.gsNode; /** * Created by yxx on 2016/5/14. @@ -42,18 +40,12 @@ public final class ServerLauncher { private final BootChain chain = BootChain.chain(); public ServerLauncher() { - ZKServerNode csNode = ZKServerNode.csNode(); - ZKServerNode gsNode = ZKServerNode.gsNode(); - ConnectionServer connectServer = new ConnectionServer(csNode.getPort()); - Server gatewayServer = udpGateway() ? new GatewayUDPConnector(gsNode.getPort()) : new GatewayServer(gsNode.getPort()); - AdminServer adminServer = new AdminServer(admin_server_port, connectServer); - chain.boot() .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 .setNext(new RedisBoot())//2.注册redis sever 到ZK - .setNext(new ServerBoot(connectServer, csNode))//3.启动长连接服务 - .setNext(new ServerBoot(gatewayServer, gsNode))//4.启动网关服务 - .setNext(new ServerBoot(adminServer, null))//5.启动控制台服务 + .setNext(new ServerBoot(ConnectionServer.I(), csNode()))//3.启动长连接服务 + .setNext(new ServerBoot(udpGateway() ? GatewayUDPConnector.I() : GatewayServer.I(), gsNode()))//4.启动网关服务 + .setNext(new ServerBoot(AdminServer.I(), null))//5.启动控制台服务 .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns .setNext(new MonitorBoot())//7.启动监控 .setNext(new LastBoot());//8.启动结束 diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java index 1b531ff8..784ad7c8 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java @@ -42,9 +42,20 @@ */ public final class GatewayUDPConnector extends NettyUDPConnector { + private static GatewayUDPConnector I; + + public static GatewayUDPConnector I() { + if (I == null) { + synchronized (GatewayUDPConnector.class) { + I = new GatewayUDPConnector(); + } + } + return I; + } + private UDPChannelHandler channelHandler; - public GatewayUDPConnector() { + private GatewayUDPConnector() { super(CC.mp.net.gateway_client_port); } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java index 16f0f885..ee2d5e05 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java @@ -49,7 +49,7 @@ public class GatewayUDPConnectionFactory extends GatewayConnectionFactory { private final Map ip_address = Maps.newConcurrentMap(); - private final GatewayUDPConnector connector = new GatewayUDPConnector(); + private final GatewayUDPConnector connector = GatewayUDPConnector.I(); private final InetSocketAddress multicastRecipient = new InetSocketAddress(gateway_server_multicast, gateway_server_port); @@ -97,7 +97,7 @@ public Function send(Function setRecipientFun = message -> { if (message != null) { - message.getPacket().sender(holder.get()); + message.setRecipient(holder.get()); } return message; }; @@ -108,7 +108,7 @@ public Function send(Function void broadcast(Function creator, Consumer sender) { M message = creator.apply(connector.getConnection()); - message.getPacket().sender(multicastRecipient); + message.setRecipient(multicastRecipient); sender.accept(message); } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java b/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java index d5046e67..b36b0075 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java @@ -29,20 +29,23 @@ * * @author ohun@live.cn (夜色) */ -public class AckMessage extends ByteBufMessage { +public final class AckMessage extends BaseMessage { public AckMessage(Packet packet, Connection connection) { super(packet, connection); } @Override - public void decode(ByteBuf body) { + public void decode(byte[] body) { + } @Override - public void encode(ByteBuf body) { + public byte[] encode() { + return null; } + public static AckMessage from(BaseMessage src) { return new AckMessage(new Packet(Command.ACK, src.getSessionId()), src.connection); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 56e9b1da..7ef4749e 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -28,6 +28,7 @@ import com.mpush.tools.config.CC; import io.netty.channel.ChannelFutureListener; +import java.net.InetSocketAddress; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.LongAdder; @@ -150,6 +151,11 @@ public int getSessionId() { return packet.sessionId; } + public BaseMessage setRecipient(InetSocketAddress recipient) { + packet.setRecipient(recipient); + return this; + } + @Override public abstract String toString(); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java index 0102f5c4..bd93b1a5 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpRequestMessage.java @@ -32,7 +32,7 @@ * * @author ohun@live.cn */ -public class HttpRequestMessage extends ByteBufMessage { +public final class HttpRequestMessage extends ByteBufMessage { public byte method; public String uri; public Map headers; diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 4fde32d6..1985515d 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -32,7 +32,7 @@ * * @author ohun@live.cn */ -public class HttpResponseMessage extends ByteBufMessage { +public final class HttpResponseMessage extends ByteBufMessage { public int statusCode; public String reasonPhrase; public Map headers = new HashMap<>(); diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java new file mode 100644 index 00000000..e08bc132 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java @@ -0,0 +1,132 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.message.gateway; + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; +import com.mpush.api.protocol.Packet; +import com.mpush.common.memory.PacketFactory; +import com.mpush.common.message.ByteBufMessage; +import com.mpush.common.net.KickRemoteMsg; +import io.netty.buffer.ByteBuf; + +import static com.mpush.api.protocol.Command.GATEWAY_KICK; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public final class GatewayKickUserMessage extends ByteBufMessage implements KickRemoteMsg { + public String userId; + public String deviceId; + public String connId; + public int clientType; + public String targetServer; + + public GatewayKickUserMessage(Packet message, Connection connection) { + super(message, connection); + } + + public static GatewayKickUserMessage build(Connection connection) { + Packet packet = PacketFactory.get(GATEWAY_KICK); + packet.sessionId = genSessionId(); + return new GatewayKickUserMessage(packet, connection); + } + + @Override + public void decode(ByteBuf body) { + userId = decodeString(body); + deviceId = decodeString(body); + connId = decodeString(body); + clientType = decodeInt(body); + targetServer = decodeString(body); + } + + @Override + public void encode(ByteBuf body) { + encodeString(body, userId); + encodeString(body, deviceId); + encodeString(body, connId); + encodeInt(body, clientType); + encodeString(body, targetServer); + } + + public GatewayKickUserMessage setUserId(String userId) { + this.userId = userId; + return this; + } + + public GatewayKickUserMessage setDeviceId(String deviceId) { + this.deviceId = deviceId; + return this; + } + + public GatewayKickUserMessage setConnId(String connId) { + this.connId = connId; + return this; + } + + public GatewayKickUserMessage setClientType(int clientType) { + this.clientType = clientType; + return this; + } + + public GatewayKickUserMessage setTargetServer(String targetServer) { + this.targetServer = targetServer; + return this; + } + + @Override + public String getUserId() { + return userId; + } + + @Override + public String getDeviceId() { + return deviceId; + } + + @Override + public String getConnId() { + return connId; + } + + @Override + public int getClientType() { + return clientType; + } + + @Override + public String getTargetServer() { + return targetServer; + } + + @Override + public String toString() { + return "GatewayKickUserMessage{" + + "userId='" + userId + '\'' + + ", deviceId='" + deviceId + '\'' + + ", connId='" + connId + '\'' + + ", clientType=" + clientType + + ", targetServer='" + targetServer + '\'' + + '}'; + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 0a270e86..4536d781 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -39,7 +39,7 @@ * * @author ohun@live.cn */ -public class GatewayPushMessage extends ByteBufMessage { +public final class GatewayPushMessage extends ByteBufMessage { public String userId; public Set tags; public int clientType; diff --git a/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java b/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java new file mode 100644 index 00000000..1b9c82ca --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.net; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public interface KickRemoteMsg { + String getUserId(); + + String getDeviceId(); + + String getConnId(); + + int getClientType(); + + String getTargetServer(); +} diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index cc84cf5d..e58a039d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -26,6 +26,7 @@ import com.mpush.common.user.UserManager; import com.mpush.core.router.RouterCenter; import com.mpush.core.server.AdminServer; +import com.mpush.core.server.ConnectionServer; import com.mpush.tools.Jsons; import com.mpush.tools.Utils; import com.mpush.tools.common.Profiler; @@ -55,12 +56,6 @@ public final class AdminHandler extends SimpleChannelInboundHandler { private static final String EOL = "\r\n"; - private static AdminServer adminServer; - - public AdminHandler(AdminServer adminServer) { - AdminHandler.adminServer = adminServer; - } - @Override protected void channelRead0(ChannelHandlerContext ctx, String request) throws Exception { Command command = Command.help; @@ -169,7 +164,7 @@ private String getNodeData(ZKPath path) { public Serializable handler(ChannelHandlerContext ctx, String args) { switch (args) { case "conn": - return adminServer.getConnectionServer().getConnectionManager().getConnections().size(); + return ConnectionServer.I().getConnectionManager().getConnections().size(); case "online": { return UserManager.I.getOnlineUserNum(); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayKickUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayKickUserHandler.java new file mode 100644 index 00000000..877ae620 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayKickUserHandler.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.handler; + +import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Packet; +import com.mpush.common.handler.BaseMessageHandler; +import com.mpush.common.message.gateway.GatewayKickUserMessage; +import com.mpush.core.router.RouterCenter; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public final class GatewayKickUserHandler extends BaseMessageHandler { + @Override + public GatewayKickUserMessage decode(Packet packet, Connection connection) { + return new GatewayKickUserMessage(packet, connection); + } + + @Override + public void handle(GatewayKickUserMessage message) { + RouterCenter.I.getRouterChangeListener().onReceiveKickRemoteMsg(message); + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java b/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java deleted file mode 100644 index 74baad58..00000000 --- a/mpush-core/src/main/java/com/mpush/core/router/KickRemoteMsg.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * (C) Copyright 2015-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * ohun@live.cn (夜色) - */ - -package com.mpush.core.router; - -import com.mpush.api.router.ClientType; - -/** - * Created by ohun on 2016/1/4. - * - * @author ohun@live.cn - */ -public final class KickRemoteMsg { - public String userId; - public String deviceId; - public String connId; - public int clientType; - public String targetServer; - - @Override - public String toString() { - return "KickRemoteMsg{" - + "userId='" + userId + '\'' - + ", deviceId='" + deviceId + '\'' - + ", connId='" + connId + '\'' - + ", clientType='" + clientType + '\'' - + ", targetServer='" + targetServer + '\'' - + '}'; - } -} diff --git a/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java b/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java new file mode 100644 index 00000000..dbd46f63 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java @@ -0,0 +1,96 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.router; + +import com.mpush.common.net.KickRemoteMsg; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +class RedisKickRemoteMessage implements KickRemoteMsg { + private String userId; + private String deviceId; + private String connId; + private int clientType; + private String targetServer; + + public RedisKickRemoteMessage setUserId(String userId) { + this.userId = userId; + return this; + } + + public RedisKickRemoteMessage setDeviceId(String deviceId) { + this.deviceId = deviceId; + return this; + } + + public RedisKickRemoteMessage setConnId(String connId) { + this.connId = connId; + return this; + } + + public RedisKickRemoteMessage setClientType(int clientType) { + this.clientType = clientType; + return this; + } + + public RedisKickRemoteMessage setTargetServer(String targetServer) { + this.targetServer = targetServer; + return this; + } + + @Override + public String getUserId() { + return userId; + } + + @Override + public String getDeviceId() { + return deviceId; + } + + @Override + public String getConnId() { + return connId; + } + + @Override + public int getClientType() { + return clientType; + } + + @Override + public String getTargetServer() { + return targetServer; + } + + @Override + public String toString() { + return "KickRemoteMsg{" + + "userId='" + userId + '\'' + + ", deviceId='" + deviceId + '\'' + + ", connId='" + connId + '\'' + + ", clientType='" + clientType + '\'' + + ", targetServer='" + targetServer + '\'' + + '}'; + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index 2a5c1694..5da043a8 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -101,4 +101,8 @@ public LocalRouterManager getLocalRouterManager() { public RemoteRouterManager getRemoteRouterManager() { return remoteRouterManager; } + + public RouterChangeListener getRouterChangeListener() { + return routerChangeListener; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index 2f59ec69..f9bc541b 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -29,12 +29,20 @@ import com.mpush.cache.redis.listener.MessageListener; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.common.message.KickUserMessage; +import com.mpush.common.message.gateway.GatewayKickUserMessage; +import com.mpush.common.net.KickRemoteMsg; import com.mpush.common.router.RemoteRouter; +import com.mpush.core.server.GatewayUDPConnector; import com.mpush.tools.Jsons; import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; import com.mpush.tools.event.EventConsumer; import com.mpush.tools.log.Logs; +import java.net.InetSocketAddress; + +import static com.mpush.tools.config.CC.mp.net.gateway_server_port; + /** * Created by ohun on 2016/1/4. * @@ -73,7 +81,7 @@ void on(RouterChangeEvent event) { * @param userId * @param router */ - public void kickLocal(final String userId, final LocalRouter router) { + private void kickLocal(final String userId, final LocalRouter router) { Connection connection = router.getRouteValue(); SessionContext context = connection.getSessionContext(); KickUserMessage message = new KickUserMessage(connection); @@ -97,7 +105,7 @@ public void kickLocal(final String userId, final LocalRouter router) { * @param userId * @param remoteRouter */ - public void kickRemote(String userId, RemoteRouter remoteRouter) { + private void kickRemote(String userId, RemoteRouter remoteRouter) { ClientLocation location = remoteRouter.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.getHost().equals(Utils.getLocalIp())) { @@ -105,15 +113,27 @@ public void kickRemote(String userId, RemoteRouter remoteRouter) { return; } - //2.发送广播 - //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 - KickRemoteMsg msg = new KickRemoteMsg(); - msg.deviceId = location.getDeviceId(); - msg.connId = location.getConnId(); - msg.clientType = location.getClientType(); - msg.targetServer = location.getHost(); - msg.userId = userId; - RedisManager.I.publish(getKickChannel(msg.targetServer), msg); + if (CC.mp.net.udpGateway()) { + Connection connection = GatewayUDPConnector.I().getConnection(); + GatewayKickUserMessage.build(connection) + .setUserId(userId) + .setClientType(location.getClientType()) + .setConnId(location.getConnId()) + .setDeviceId(location.getDeviceId()) + .setTargetServer(location.getHost()) + .setRecipient(new InetSocketAddress(location.getHost(), gateway_server_port)) + .sendRaw(); + } else { + //2.发送广播 + //TODO 远程机器可能不存在,需要确认下redis 那个通道如果机器不存在的话,是否会存在消息积压的问题。 + RedisKickRemoteMessage message = new RedisKickRemoteMessage() + .setUserId(userId) + .setClientType(location.getClientType()) + .setConnId(location.getConnId()) + .setDeviceId(location.getDeviceId()) + .setTargetServer(location.getHost()); + RedisManager.I.publish(getKickChannel(location.getHost()), message); + } } /** @@ -126,19 +146,19 @@ public void kickRemote(String userId, RemoteRouter remoteRouter) { */ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 - if (!msg.targetServer.equals(Utils.getLocalIp())) { + if (!msg.getTargetServer().equals(Utils.getLocalIp())) { Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); return; } //2.查询本地路由,找到要被踢下线的链接,并删除该本地路由 - String userId = msg.userId; - int clientType = msg.clientType; + String userId = msg.getUserId(); + int clientType = msg.getClientType(); LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); LocalRouter localRouter = localRouterManager.lookup(userId, clientType); if (localRouter != null) { Logs.Conn.info("receive kick remote msg, msg={}", msg); - if (localRouter.getRouteValue().getId().equals(msg.connId)) {//二次校验,防止误杀 + if (localRouter.getRouteValue().getId().equals(msg.getConnId())) {//二次校验,防止误杀 //2.1删除本地路由信息 localRouterManager.unRegister(userId, clientType); //2.2发送踢人消息到客户端 diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index ac79c873..9dc22f19 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -21,6 +21,7 @@ import com.mpush.core.handler.AdminHandler; import com.mpush.netty.server.NettyTCPServer; +import com.mpush.tools.config.CC; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.DelimiterBasedFrameDecoder; @@ -29,14 +30,27 @@ import io.netty.handler.codec.string.StringEncoder; public final class AdminServer extends NettyTCPServer { - private final ConnectionServer connectionServer; + private static AdminServer I; - private final AdminHandler adminHandler; + private AdminHandler adminHandler; - public AdminServer(int port, ConnectionServer connectionServer) { - super(port); - this.connectionServer = connectionServer; - this.adminHandler = new AdminHandler(this); + public static AdminServer I() { + if (I == null) { + synchronized (AdminServer.class) { + I = new AdminServer(); + } + } + return I; + } + + private AdminServer() { + super(CC.mp.net.admin_server_port); + } + + @Override + public void init() { + super.init(); + this.adminHandler = new AdminHandler(); } @Override @@ -59,9 +73,4 @@ protected ChannelHandler getDecoder() { protected ChannelHandler getEncoder() { return new StringEncoder(); } - - public ConnectionServer getConnectionServer() { - return connectionServer; - } - } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 441183e5..0235166c 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -52,6 +52,8 @@ * @author ohun@live.cn (夜色) */ public final class ConnectionServer extends NettyTCPServer { + private static ConnectionServer I; + private ServerChannelHandler channelHandler; private GlobalChannelTrafficShapingHandler trafficShapingHandler; private ScheduledExecutorService trafficShapingExecutor; @@ -59,8 +61,17 @@ public final class ConnectionServer extends NettyTCPServer { private ConnectionManager connectionManager = new ServerConnectionManager(true); private HttpClient httpClient; - public ConnectionServer(int port) { - super(port); + public static ConnectionServer I() { + if (I == null) { + synchronized (ConnectionServer.class) { + I = new ConnectionServer(); + } + } + return I; + } + + private ConnectionServer() { + super(CC.mp.net.connect_server_port); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 9d7b835f..8920ea13 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -45,14 +45,24 @@ * @author ohun@live.cn */ public final class GatewayServer extends NettyTCPServer { + private static GatewayServer I; private ServerChannelHandler channelHandler; private ServerConnectionManager connectionManager; private GlobalChannelTrafficShapingHandler trafficShapingHandler; private ScheduledExecutorService trafficShapingExecutor; - public GatewayServer(int port) { - super(port); + public static GatewayServer I() { + if (I == null) { + synchronized (GatewayServer.class) { + I = new GatewayServer(); + } + } + return I; + } + + private GatewayServer() { + super(CC.mp.net.gateway_server_port); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java index cd5b1a34..570ab07b 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java @@ -19,9 +19,11 @@ package com.mpush.core.server; +import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Command; import com.mpush.api.service.Listener; import com.mpush.common.MessageDispatcher; +import com.mpush.core.handler.GatewayKickUserHandler; import com.mpush.core.handler.GatewayPushHandler; import com.mpush.netty.udp.UDPChannelHandler; import com.mpush.netty.udp.NettyUDPConnector; @@ -40,10 +42,21 @@ */ public final class GatewayUDPConnector extends NettyUDPConnector { + private static GatewayUDPConnector I; + private UDPChannelHandler channelHandler; - public GatewayUDPConnector(int port) { - super(port); + public static GatewayUDPConnector I() { + if (I == null) { + synchronized (GatewayUDPConnector.class) { + I = new GatewayUDPConnector(); + } + } + return I; + } + + private GatewayUDPConnector() { + super(CC.mp.net.gateway_server_port); } @Override @@ -51,6 +64,7 @@ public void init() { super.init(); MessageDispatcher receiver = new MessageDispatcher(POLICY_LOG); receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler()); + receiver.register(Command.GATEWAY_KICK, new GatewayKickUserHandler()); channelHandler = new UDPChannelHandler(receiver); channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_server_multicast)); channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface()); @@ -60,8 +74,8 @@ public void init() { protected void initOptions(Bootstrap b) { super.initOptions(b); b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);//默认情况下,当本机发送组播数据到某个网络接口时,在IP层,数据会回送到本地的回环接口,选项IP_MULTICAST_LOOP用于控制数据是否回送到本地的回环接口 - //b.option(ChannelOption.IP_MULTICAST_IF, false);//选项IP_MULTICAST_IF用于设置组播的默认默认网络接口,会从给定的网络接口发送,另一个网络接口会忽略此数据,参数addr是希望多播输出接口的IP地址,使用INADDR_ANY地址回送到默认接口。 b.option(ChannelOption.IP_MULTICAST_TTL, 255);//选项IP_MULTICAST_TTL允许设置超时TTL,范围为0~255之间的任何值,例如: + //b.option(ChannelOption.IP_MULTICAST_IF, null);//选项IP_MULTICAST_IF用于设置组播的默认默认网络接口,会从给定的网络接口发送,另一个网络接口会忽略此数据,参数addr是希望多播输出接口的IP地址,使用INADDR_ANY地址回送到默认接口。 //b.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 1024 * 1024)); } @@ -69,4 +83,8 @@ protected void initOptions(Bootstrap b) { public ChannelHandler getChannelHandler() { return channelHandler; } + + public Connection getConnection() { + return channelHandler.getConnection(); + } } diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 1a237dd8..2eb02e96 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -6,6 +6,7 @@ mp.zk.namespace=mpush mp.zk.server-address="127.0.0.1:2181" mp.core.compress-threshold=10k mp.http.proxy-enabled=true +mp.net.gateway-server-net=udp mp.redis { #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 nodes:["127.0.0.1:6379"]//格式是ip:port,密码可以没有ip:port diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 724c1874..63b44af9 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -109,19 +109,28 @@ public void init() { } if (zkConfig.getDigest() != null) { - builder.authorization("digest", zkConfig.getDigest().getBytes(Constants.UTF_8)) - .aclProvider(new ACLProvider() { - - @Override - public List getDefaultAcl() { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - - @Override - public List getAclForPath(final String path) { - return ZooDefs.Ids.CREATOR_ALL_ACL; - } - }); + /* + * scheme对应于采用哪种方案来进行权限管理,zookeeper实现了一个pluggable的ACL方案,可以通过扩展scheme,来扩展ACL的机制。 + * zookeeper缺省支持下面几种scheme: + * + * world: 默认方式,相当于全世界都能访问; 它下面只有一个id, 叫anyone, world:anyone代表任何人,zookeeper中对所有人有权限的结点就是属于world:anyone的 + * auth: 代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户); 它不需要id, 只要是通过authentication的user都有权限(zookeeper支持通过kerberos来进行authencation, 也支持username/password形式的authentication) + * digest: 即用户名:密码这种方式认证,这也是业务系统中最常用的;它对应的id为username:BASE64(SHA1(password)),它需要先通过username:password形式的authentication + * ip: 使用Ip地址认证;它对应的id为客户机的IP地址,设置的时候可以设置一个ip段,比如ip:192.168.1.0/16, 表示匹配前16个bit的IP段 + * super: 在这种scheme情况下,对应的id拥有超级权限,可以做任何事情(cdrwa) + */ + builder.authorization("digest", zkConfig.getDigest().getBytes(Constants.UTF_8)); + builder.aclProvider(new ACLProvider() { + @Override + public List getDefaultAcl() { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + + @Override + public List getAclForPath(final String path) { + return ZooDefs.Ids.CREATOR_ALL_ACL; + } + }); } client = builder.build(); Logs.Console.info("init zk client, config={}", zkConfig.toString()); From 9717ee6ed49e5bc106bd3c814b1a05236bb74350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 24 Oct 2016 09:48:46 +0800 Subject: [PATCH 715/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jmx=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/monitor/jmx/MBeanInfo.java | 29 +++ .../com/mpush/monitor/jmx/MBeanRegistry.java | 177 ++++++++++++++++++ .../com/mpush/monitor/jmx/MException.java | 39 ++++ .../monitor/jmx/mxbean/ServerMXBean.java | 132 +++++++++++++ .../mpush/monitor/jmx/stats/ServerStats.java | 152 +++++++++++++++ .../com/mpush/monitor/jmx/stats/Stats.java | 94 ++++++++++ 6 files changed, 623 insertions(+) create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanInfo.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanRegistry.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/jmx/MException.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/jmx/mxbean/ServerMXBean.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/ServerStats.java create mode 100644 mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/Stats.java diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanInfo.java b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanInfo.java new file mode 100644 index 00000000..9ca63461 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanInfo.java @@ -0,0 +1,29 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.jmx; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public interface MBeanInfo { + String getName(); +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanRegistry.java b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanRegistry.java new file mode 100644 index 00000000..3c43adc7 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MBeanRegistry.java @@ -0,0 +1,177 @@ +package com.mpush.monitor.jmx; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.management.*; +import java.lang.management.ManagementFactory; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +public class MBeanRegistry { + public static final String DOMAIN = "com.mpush.MPushService"; + private static final Logger LOG = LoggerFactory.getLogger(MBeanRegistry.class); + + private static MBeanRegistry instance = new MBeanRegistry(); + + private Map mapBean2Path = new ConcurrentHashMap<>(); + + private MBeanServer mBeanServer; + + public static MBeanRegistry getInstance() { + return instance; + } + + public MBeanRegistry() { + try { + mBeanServer = ManagementFactory.getPlatformMBeanServer(); + } catch (Error e) { + // Account for running within IKVM and create a new MBeanServer + // if the PlatformMBeanServer does not exist. + mBeanServer = MBeanServerFactory.createMBeanServer(); + } + } + + /** + * Return the underlying MBeanServer that is being + * used to register MBean's. The returned MBeanServer + * may be a new empty MBeanServer if running through IKVM. + */ + public MBeanServer getPlatformMBeanServer() { + return mBeanServer; + } + + /** + * Registers a new MBean with the platform MBean server. + * + * @param bean the bean being registered + * @param parent if not null, the new bean will be registered as a child + * node of this parent. + */ + public void register(MBeanInfo bean, MBeanInfo parent) { + assert bean != null; + String path = null; + if (parent != null) { + path = mapBean2Path.get(parent); + assert path != null; + } + path = makeFullPath(path, parent); + try { + ObjectName name = makeObjectName(path, bean); + mBeanServer.registerMBean(bean, name); + mapBean2Path.put(bean, path); + } catch (JMException e) { + LOG.warn("Failed to register MBean " + bean.getName()); + throw new MException(e); + } + } + + /** + * Unregister the MBean identified by the path. + * + * @param path + * @param bean + */ + private void unregister(String path, MBeanInfo bean) { + if (path == null) return; + try { + mBeanServer.unregisterMBean(makeObjectName(path, bean)); + } catch (JMException e) { + LOG.warn("Failed to unregister MBean " + bean.getName()); + throw new MException(e); + } + } + + /** + * Unregister MBean. + * + * @param bean + */ + public void unregister(MBeanInfo bean) { + if (bean == null) return; + + String path = mapBean2Path.get(bean); + unregister(path, bean); + mapBean2Path.remove(bean); + } + + /** + * Unregister all currently registered MBeans + */ + public void unregisterAll() { + for (Map.Entry e : mapBean2Path.entrySet()) { + try { + unregister(e.getValue(), e.getKey()); + } catch (MException e1) { + LOG.warn("Error during unregister", e1); + } + } + mapBean2Path.clear(); + } + + /** + * Generate a filesystem-like path. + * + * @param prefix path prefix + * @param name path elements + * @return absolute path + */ + public String makeFullPath(String prefix, String... name) { + StringBuilder sb = new StringBuilder(prefix == null ? "/" : (prefix.equals("/") ? prefix : prefix + "/")); + boolean first = true; + for (String s : name) { + if (s == null) continue; + if (!first) { + sb.append("/"); + } else + first = false; + sb.append(s); + } + return sb.toString(); + } + + protected String makeFullPath(String prefix, MBeanInfo bean) { + return makeFullPath(prefix, bean == null ? null : bean.getName()); + } + + /** + * This takes a path, such as /a/b/c, and converts it to + * name0=a,name1=b,name2=c + */ + private int tokenize(StringBuilder sb, String path, int index) { + String[] tokens = path.split("/"); + for (String s : tokens) { + if (s.length() == 0) + continue; + sb.append("name").append(index++) + .append("=").append(s).append(","); + } + return index; + } + + /** + * Builds an MBean path and creates an ObjectName instance using the path. + * + * @param path MBean path + * @param bean the MBean instance + * @return ObjectName to be registered with the platform MBean server + */ + protected ObjectName makeObjectName(String path, MBeanInfo bean) + throws MalformedObjectNameException { + if (path == null) + return null; + StringBuilder beanName = new StringBuilder(DOMAIN).append(':'); + int counter = 0; + counter = tokenize(beanName, path, counter); + tokenize(beanName, bean.getName(), counter); + beanName.deleteCharAt(beanName.length() - 1); + try { + return new ObjectName(beanName.toString()); + } catch (MalformedObjectNameException e) { + LOG.warn("Invalid name \"" + beanName.toString() + "\" for class " + + bean.getClass().toString()); + throw e; + } + } +} \ No newline at end of file diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MException.java b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MException.java new file mode 100644 index 00000000..94fcdfe1 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/MException.java @@ -0,0 +1,39 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.jmx; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public final class MException extends RuntimeException { + public MException(String message) { + super(message); + } + + public MException(String message, Throwable cause) { + super(message, cause); + } + + public MException(Throwable cause) { + super(cause); + } +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/jmx/mxbean/ServerMXBean.java b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/mxbean/ServerMXBean.java new file mode 100644 index 00000000..defe465a --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/mxbean/ServerMXBean.java @@ -0,0 +1,132 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.jmx.mxbean; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public interface ServerMXBean { + /** + * @return the server socket port number + */ + String getClientPort(); + + /** + * @return the zookeeper server version + */ + String getVersion(); + + /** + * @return time the server was started + */ + String getStartTime(); + + /** + * @return min request latency in ms + */ + long getMinRequestLatency(); + + /** + * @return average request latency in ms + */ + long getAvgRequestLatency(); + + /** + * @return max request latency in ms + */ + long getMaxRequestLatency(); + + /** + * @return number of packets received so far + */ + long getPacketsReceived(); + + /** + * @return number of packets sent so far + */ + long getPacketsSent(); + + /** + * @return number of outstanding requests. + */ + long getOutstandingRequests(); + + /** + * Current TickTime of server in milliseconds + */ + int getTickTime(); + + /** + * Set TickTime of server in milliseconds + */ + void setTickTime(int tickTime); + + /** + * Current maxClientCnxns allowed from a particular host + */ + int getMaxClientCnxnsPerHost(); + + /** + * Set maxClientCnxns allowed from a particular host + */ + void setMaxClientCnxnsPerHost(int max); + + /** + * Current minSessionTimeout of the server in milliseconds + */ + int getMinSessionTimeout(); + + /** + * Set minSessionTimeout of server in milliseconds + */ + void setMinSessionTimeout(int min); + + /** + * Current maxSessionTimeout of the server in milliseconds + */ + int getMaxSessionTimeout(); + + /** + * Set maxSessionTimeout of server in milliseconds + */ + void setMaxSessionTimeout(int max); + + /** + * Reset packet and latency statistics + */ + void resetStatistics(); + + /** + * Reset min/avg/max latency statistics + */ + void resetLatency(); + + /** + * Reset max latency statistics only. + */ + void resetMaxLatency(); + + /** + * @return number of alive client connections + */ + long getNumAliveConnections(); +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/ServerStats.java b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/ServerStats.java new file mode 100644 index 00000000..7f106fbf --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/ServerStats.java @@ -0,0 +1,152 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.monitor.jmx.stats; + +/** + * Created by ohun on 16/10/23. + * + * @author ohun@live.cn (夜色) + */ +public final class ServerStats { + private long packetsSent; + private long packetsReceived; + private long maxLatency; + private long minLatency = Long.MAX_VALUE; + private long totalLatency = 0; + private long count = 0; + + private final Provider provider; + + public interface Provider { + long getOutstandingRequests(); + + long getLastProcessedXid(); + + String getState(); + + int getNumAliveConnections(); + } + + public ServerStats(Provider provider) { + this.provider = provider; + } + + // getters + synchronized public long getMinLatency() { + return minLatency == Long.MAX_VALUE ? 0 : minLatency; + } + + synchronized public long getAvgLatency() { + if (count != 0) { + return totalLatency / count; + } + return 0; + } + + synchronized public long getMaxLatency() { + return maxLatency; + } + + public long getOutstandingRequests() { + return provider.getOutstandingRequests(); + } + + public long getLastProcessedXid() { + return provider.getLastProcessedXid(); + } + + synchronized public long getPacketsReceived() { + return packetsReceived; + } + + synchronized public long getPacketsSent() { + return packetsSent; + } + + public String getServerState() { + return provider.getState(); + } + + /** + * The number of client connections alive to this server + */ + public int getNumAliveClientConnections() { + return provider.getNumAliveConnections(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Latency min/avg/max: " + getMinLatency() + "/" + + getAvgLatency() + "/" + getMaxLatency() + "\n"); + sb.append("Received: " + getPacketsReceived() + "\n"); + sb.append("Sent: " + getPacketsSent() + "\n"); + sb.append("Connections: " + getNumAliveClientConnections() + "\n"); + + if (provider != null) { + sb.append("Outstanding: " + getOutstandingRequests() + "\n"); + sb.append("xid: 0x" + Long.toHexString(getLastProcessedXid()) + "\n"); + } + sb.append("Mode: " + getServerState() + "\n"); + return sb.toString(); + } + + // mutators + synchronized void updateLatency(long requestCreateTime) { + long latency = System.currentTimeMillis() - requestCreateTime; + totalLatency += latency; + count++; + if (latency < minLatency) { + minLatency = latency; + } + if (latency > maxLatency) { + maxLatency = latency; + } + } + + synchronized public void resetLatency() { + totalLatency = 0; + count = 0; + maxLatency = 0; + minLatency = Long.MAX_VALUE; + } + + synchronized public void resetMaxLatency() { + maxLatency = getMinLatency(); + } + + synchronized public void incrementPacketsReceived() { + packetsReceived++; + } + + synchronized public void incrementPacketsSent() { + packetsSent++; + } + + synchronized public void resetRequestCounters() { + packetsReceived = 0; + packetsSent = 0; + } + + synchronized public void reset() { + resetLatency(); + resetRequestCounters(); + } +} diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/Stats.java b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/Stats.java new file mode 100644 index 00000000..eb8337d5 --- /dev/null +++ b/mpush-monitor/src/main/java/com/mpush/monitor/jmx/stats/Stats.java @@ -0,0 +1,94 @@ +package com.mpush.monitor.jmx.stats; + +import java.util.Date; + +/** + * Statistics on the ServerCnxn + */ +interface Stats { + /** + * Date/time the connection was established + * + * @since 3.3.0 + */ + Date getEstablished(); + + /** + * The number of requests that have been submitted but not yet + * responded to. + */ + long getOutstandingRequests(); + + /** + * Number of packets received + */ + long getPacketsReceived(); + + /** + * Number of packets sent (incl notifications) + */ + long getPacketsSent(); + + /** + * Min latency in ms + * + * @since 3.3.0 + */ + long getMinLatency(); + + /** + * Average latency in ms + * + * @since 3.3.0 + */ + long getAvgLatency(); + + /** + * Max latency in ms + * + * @since 3.3.0 + */ + long getMaxLatency(); + + /** + * Last operation performed by this connection + * + * @since 3.3.0 + */ + String getLastOperation(); + + /** + * Last cxid of this connection + * + * @since 3.3.0 + */ + long getLastCxid(); + + /** + * Last zxid of this connection + * + * @since 3.3.0 + */ + long getLastZxid(); + + /** + * Last time server sent a response to client on this connection + * + * @since 3.3.0 + */ + long getLastResponseTime(); + + /** + * Latency of last response to client on this connection in ms + * + * @since 3.3.0 + */ + long getLastLatency(); + + /** + * Reset counters + * + * @since 3.3.0 + */ + void resetStats(); +} \ No newline at end of file From c7bcfa94e23903ccfb0a6bca67a1a6a350eced4f Mon Sep 17 00:00:00 2001 From: ohun Date: Wed, 26 Oct 2016 13:51:46 +0800 Subject: [PATCH 716/890] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 462f52d9..de02009f 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ ps:由于源码分别在github和码云有两份,最新的代码以github为 mp.net.connect-server-port=3000//长链接服务对外端口, 公网端口 mp.zk.server-address="127.0.0.1:2181"//zk 机器的地址 mp.redis={//redis 相关配置 - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 - cluster-group:[["127.0.0.1:6379"]]//格式ip:port:password,密码可以不设置ip:port + nodes:["127.0.0.1:6379"] //格式是ip:port + cluster-model:single //single, cluster } //还有用于安全加密的RSA mp.security.private-key 和 mp.security.public-key 等... ``` From 91d0a73cc4a2ea591eff0e9badd0e945b0b5eea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 26 Oct 2016 17:38:56 +0800 Subject: [PATCH 717/890] =?UTF-8?q?push=E4=B8=AD=E5=BF=83=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B5=81=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.cmd | 1 + conf/reference.conf | 9 +- .../mpush/api/push/BroadcastController.java | 45 +++ .../java/com/mpush/api/push/PushContext.java | 55 +++- .../com/mpush/bootstrap/ServerLauncher.java | 1 + .../com/mpush/bootstrap/job/BootChain.java | 2 +- .../job/{FirstJob.java => FirstBoot.java} | 4 +- .../mpush/bootstrap/job/HttpProxyBoot.java | 5 + .../mpush/bootstrap/job/PushCenterBoot.java | 41 +++ .../com/mpush/bootstrap/job/RedisBoot.java | 1 + mpush-boot/src/main/resources/mpush.conf | 19 +- .../java/com/mpush/cache/redis/RedisKey.java | 5 + .../cache/redis/manager/RedisManager.java | 8 +- .../connect/ConnClientChannelHandler.java | 1 + .../com/mpush/client/push/PushRequest.java | 8 + .../common/condition/AwaysPassCondition.java | 36 +++ .../com/mpush/common/condition/Condition.java | 31 ++ .../common/condition/ScriptCondition.java | 48 ++++ .../mpush/common/condition/TagsCondition.java | 46 +++ .../message/gateway/GatewayPushMessage.java | 31 +- .../common/push/RedisBroadcastController.java | 100 +++++++ .../com/mpush/core/handler/AckHandler.java | 2 +- .../core/handler/GatewayPushHandler.java | 264 +----------------- .../mpush/core/handler/HttpProxyHandler.java | 8 +- .../mpush/core/push/BroadcastPushTask.java | 147 ++++++++++ .../com/mpush/core/push/FastFlowControl.java | 89 ++++++ .../java/com/mpush/core/push/FlowControl.java | 41 +++ .../mpush/core/push/GlobalFlowControl.java | 84 ++++++ .../mpush/core/push/OverFlowException.java | 36 +++ .../java/com/mpush/core/push/PushCenter.java | 73 +++++ .../java/com/mpush/core/push/PushTask.java | 28 ++ .../com/mpush/core/push/RedisFlowControl.java | 102 +++++++ .../mpush/core/push/SingleUserPushTask.java | 203 ++++++++++++++ .../mpush/core/router/LocalRouterManager.java | 6 +- .../mpush/core/server/ConnectionServer.java | 14 +- .../com/mpush/netty/http/NettyHttpClient.java | 10 + .../mpush/netty/udp/UDPChannelHandler.java | 1 + .../mpush/test/push/PushClientTestMain.java | 7 +- .../src/test/resources/application.conf | 16 +- .../com/mpush/tools/thread/ThreadNames.java | 3 +- .../tools/thread/pool/ThreadPoolManager.java | 1 + 41 files changed, 1314 insertions(+), 318 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/push/BroadcastController.java rename mpush-boot/src/main/java/com/mpush/bootstrap/job/{FirstJob.java => FirstBoot.java} (94%) create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/PushCenterBoot.java create mode 100644 mpush-common/src/main/java/com/mpush/common/condition/AwaysPassCondition.java create mode 100644 mpush-common/src/main/java/com/mpush/common/condition/Condition.java create mode 100644 mpush-common/src/main/java/com/mpush/common/condition/ScriptCondition.java create mode 100644 mpush-common/src/main/java/com/mpush/common/condition/TagsCondition.java create mode 100644 mpush-common/src/main/java/com/mpush/common/push/RedisBroadcastController.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/FlowControl.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/PushCenter.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/PushTask.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java create mode 100644 mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java diff --git a/bin/mp.cmd b/bin/mp.cmd index c7918f44..f4e18c90 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -13,6 +13,7 @@ REM distributed under the License is distributed on an "AS IS" BASIS, REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. REM See the License for the specific language governing permissions and REM limitations under the License. +REM java -Dmp.conf=../conf/mpush.conf -Dmp.log.dir=../logs -jar bootstrap.jar setlocal diff --git a/conf/reference.conf b/conf/reference.conf index 5db692eb..68bb86ff 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -8,7 +8,7 @@ # 配置文件格式采用HOCON格式。解析库由https://github.com/typesafehub/config提供。 # 具体可参照器说明文档,比如含有特殊字符的字符串必须用双引号包起来。 # -############################################################################################################## +################################################################################################################## mp { #日志配置 @@ -40,12 +40,12 @@ mp { admin-server-port=3002 //控制台服务端口, 内部端口 gateway-server-port=3001 //网关服务端口, 内部端口 gateway-client-port=4000 //UDP 客户端端口 - gateway-server-net=tcp //网关服务使用的网络类型tcp/udp + gateway-server-net=udp //网关服务使用的网络类型tcp/udp gateway-server-multicast="239.239.239.88" //239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效 gateway-client-multicast="239.239.239.99" //239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效 public-host-mapping { //本机局域网IP和公网IP的映射关系 - "10.0.10.156":"111.1.32.137" - "10.0.10.166":"111.1.33.138" + //"10.0.10.156":"111.1.32.137" + //"10.0.10.166":"111.1.33.138" } traffic-shaping { //流量整形配置 gateway-client { @@ -98,7 +98,6 @@ mp { #Redis集群配置 redis { write-to-zk=false - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个集群下面可以有多台机器 password=""//your password cluster-model=single//single,cluster nodes:[]//["127.0.0.1:6379"]格式ip:port:password,密码可以不设置ip:port diff --git a/mpush-api/src/main/java/com/mpush/api/push/BroadcastController.java b/mpush-api/src/main/java/com/mpush/api/push/BroadcastController.java new file mode 100644 index 00000000..bc7f7d71 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/push/BroadcastController.java @@ -0,0 +1,45 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.push; + +/** + * Created by ohun on 16/10/25. + * + * @author ohun@live.cn (夜色) + */ +public interface BroadcastController { + + String taskId(); + + int qps(); + + void updateQps(int qps); + + boolean isDone(); + + int sendCount(); + + void cancel(); + + boolean isCancelled(); + + int incSendCount(int count); + +} diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java index f7039341..85bfc6d8 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Set; +import java.util.UUID; /** * Created by ohun on 16/9/8. @@ -50,11 +51,6 @@ public class PushContext { */ private List userIds; - /** - * 用户标签过滤,目前只有include, 后续会增加exclude - */ - private Set tags; - /** * 消息ack模式 */ @@ -65,15 +61,38 @@ public class PushContext { */ private PushCallback callback; + /** + * 推送超时时间 + */ + private int timeout = 3000; + + //================================broadcast=====================================// + /** * 全网广播在线用户 */ private boolean broadcast = false; /** - * 推送超时时间 + * 用户标签过滤,目前只有include, 后续会增加exclude */ - private int timeout = 3000; + private Set tags; + + /** + * 条件表达式, 满足条件的用户会被推送,目前支持的脚步语言为js + * 可以使用的参数为 userId,tags,clientVersion,osName,osVersion + * 比如 : + * 灰度:userId % 100 < 20 + * 包含test标签:tags!=null && tags.indexOf("test")!=-1 + * 判断客户端版本号:clientVersion.indexOf("android")!=-1 && clientVersion.replace(/[^\d]/g,"") > 20 + * 等等 + */ + private String condition; + + /** + * 广播推送的时候可以考虑生成一个ID, 便于控制任务。 + */ + private String taskId; public PushContext(byte[] context) { this.context = context; @@ -91,6 +110,10 @@ public static PushContext build(PushMsg msg) { return new PushContext(msg); } + public static String genTaskId() { + return UUID.randomUUID().toString(); + } + public byte[] getContext() { return context; } @@ -161,4 +184,22 @@ public PushContext setTags(Set tags) { this.tags = tags; return this; } + + public String getCondition() { + return condition; + } + + public PushContext setCondition(String condition) { + this.condition = condition; + return this; + } + + public String getTaskId() { + return taskId; + } + + public PushContext setTaskId(String taskId) { + this.taskId = taskId; + return this; + } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index faf31dce..1de57d30 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -46,6 +46,7 @@ public ServerLauncher() { .setNext(new ServerBoot(ConnectionServer.I(), csNode()))//3.启动长连接服务 .setNext(new ServerBoot(udpGateway() ? GatewayUDPConnector.I() : GatewayServer.I(), gsNode()))//4.启动网关服务 .setNext(new ServerBoot(AdminServer.I(), null))//5.启动控制台服务 + .setNext(new PushCenterBoot())//6.启动http代理服务,解析dns .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns .setNext(new MonitorBoot())//7.启动监控 .setNext(new LastBoot());//8.启动结束 diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java index 6960fccd..749a28b8 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootChain.java @@ -25,7 +25,7 @@ * @author ohun@live.cn */ public final class BootChain { - private final BootJob first = new FirstJob(); + private final BootJob first = new FirstBoot(); public void start() { first.start(); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstBoot.java similarity index 94% rename from mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java rename to mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstBoot.java index 0e0b5e9e..908b9d1a 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/FirstBoot.java @@ -27,9 +27,9 @@ * * @author ohun@live.cn (夜色) */ -/*package*/ final class FirstJob extends BootJob { +/*package*/ final class FirstBoot extends BootJob { - public FirstJob() { + FirstBoot() { ServerEventListenerFactory.create(); } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index 2959d509..7192ed3d 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -20,6 +20,8 @@ package com.mpush.bootstrap.job; import com.mpush.api.spi.net.DnsMappingManager; +import com.mpush.core.push.PushCenter; +import com.mpush.netty.http.NettyHttpClient; import com.mpush.tools.config.CC; /** @@ -32,14 +34,17 @@ public final class HttpProxyBoot extends BootJob { @Override protected void start() { if (CC.mp.http.proxy_enabled) { + NettyHttpClient.I().syncStart(); DnsMappingManager.create().start(); } + startNext(); } @Override protected void stop() { if (CC.mp.http.proxy_enabled) { + NettyHttpClient.I().syncStop(); DnsMappingManager.create().stop(); } stopNext(); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/PushCenterBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/PushCenterBoot.java new file mode 100644 index 00000000..9a605aed --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/PushCenterBoot.java @@ -0,0 +1,41 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.core.push.PushCenter; + +/** + * Created by ohun on 16/10/25. + * + * @author ohun@live.cn (夜色) + */ +public final class PushCenterBoot extends BootJob { + @Override + protected void start() { + PushCenter.I.start(); + startNext(); + } + + @Override + protected void stop() { + PushCenter.I.stop(); + stopNext(); + } +} diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java index f4366716..32c2f103 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/RedisBoot.java @@ -39,6 +39,7 @@ protected void start() { @Override protected void stop() { RedisManager.I.destroy(); + UserManager.I.clearUserOnlineData(); stopNext(); } } diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 27af6af8..2e304bb7 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -3,15 +3,14 @@ mp.min-heartbeat=${min.hb} mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" mp.zk.server-address="127.0.0.1:2181" -mp.zk.namespace=mpush -mp.redis={ - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 - nodes:["127.0.0.1:6379"]//格式是ip:port - cluster-model=single//single,cluster +mp.redis { //redis 集群配置 + nodes:["127.0.0.1:6379"] //格式是ip:port + cluster-model:single //single, cluster } -mp.http.proxy-enabled=true //启用Http代理功能 -mp.net.connect-server-port=3000 +mp.net.gateway-server-net=udp // 网关服务使用的网络 udp/tcp +mp.net.connect-server-port=3000 //接入服务的端口号 mp.net.public-host-mapping { //本机局域网IP和公网IP的映射关系,请添加实际的IP - "10.0.10.156":"111.1.32.137"//请修改成实际的IP - "10.0.10.166":"111.1.33.138"//请修改成实际的IP -} \ No newline at end of file + //"10.0.10.156":"111.1.32.137" //请修改成实际的IP + //"10.0.10.166":"111.1.33.138" //请修改成实际的IP +} +mp.http.proxy-enabled=true //启用Http代理功能 diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java index 9bff61f9..9d31cebb 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/RedisKey.java @@ -31,6 +31,7 @@ public final class RedisKey { public static final String SESSION_AES_KEY = "mp:sa"; public static final String SESSION_AES_SEQ_KEY = "mp:sas"; + public static final String PUSH_TASK_PREFIX = "mp:pt"; public static String getUserRouteKey(String userId) { return USER_PREFIX + userId; @@ -48,4 +49,8 @@ public static String getOnlineUserListKey(String publicIP) { return ONLINE_USER_LIST_KEY_PREFIX + publicIP; } + public static String getPushTaskKey(String taskId) { + return PUSH_TASK_PREFIX + taskId; + } + } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 751202b5..cc79c2e6 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -163,7 +163,7 @@ public void hdel(String key, String field) { } public Map hgetAll(String key) { - return call(jedis -> jedis.hgetAll(key), Collections.emptyMap()); + return call(jedis -> jedis.hgetAll(key), Collections.emptyMap()); } public Map hgetAll(String key, Class clazz) { @@ -181,7 +181,7 @@ public Map hgetAll(String key, Class clazz) { * @return */ public Set hkeys(String key) { - return call(jedis -> jedis.hkeys(key), Collections.emptySet()); + return call(jedis -> jedis.hkeys(key), Collections.emptySet()); } /** @@ -219,6 +219,10 @@ public void hmset(String key, Map hash) { hmset(key, hash, 0); } + public long hincrBy(String key, String field, long value) { + return call(jedis -> jedis.hincrBy(key, field, value), 0L); + } + /********************* hash redis end ********************************/ /********************* list redis start ********************************/ diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 7691e3eb..c2ce746f 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -195,6 +195,7 @@ private void tryFastConnect() { private void bindUser(ClientConfig client) { BindUserMessage message = new BindUserMessage(connection); message.userId = client.getUserId(); + message.tags = "test"; message.send(); LOGGER.debug("<<< send bind user message={}", message); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 4f7adeb9..6bfb0b5d 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -56,6 +56,7 @@ private enum Status {init, success, failure, offline, timeout} private AckModel ackModel; private Set tags; + private String condition; private PushCallback callback; private String userId; private byte[] content; @@ -175,6 +176,7 @@ public FutureTask broadcast() { .setUserId(userId) .setContent(content) .setTags(tags) + .setCondition(condition) .addFlag(ackModel.flag), pushMessage -> { @@ -223,6 +225,7 @@ public static PushRequest build(GatewayConnectionFactory factory, PushContext ct .setAckModel(ctx.getAckModel()) .setUserId(ctx.getUserId()) .setTags(ctx.getTags()) + .setCondition(ctx.getCondition()) .setContent(content) .setTimeout(ctx.getTimeout()) .setCallback(ctx.getCallback()); @@ -259,6 +262,11 @@ public PushRequest setTags(Set tags) { return this; } + public PushRequest setCondition(String condition) { + this.condition = condition; + return this; + } + @Override public String toString() { return "PushRequest{" + diff --git a/mpush-common/src/main/java/com/mpush/common/condition/AwaysPassCondition.java b/mpush-common/src/main/java/com/mpush/common/condition/AwaysPassCondition.java new file mode 100644 index 00000000..0526cc10 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/condition/AwaysPassCondition.java @@ -0,0 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.condition; + +import java.util.Map; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class AwaysPassCondition implements Condition { + public static final Condition I = new AwaysPassCondition(); + + @Override + public boolean test(Map env) { + return true; + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/condition/Condition.java b/mpush-common/src/main/java/com/mpush/common/condition/Condition.java new file mode 100644 index 00000000..791c8aff --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/condition/Condition.java @@ -0,0 +1,31 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.condition; + +import java.util.Map; +import java.util.function.Predicate; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public interface Condition extends Predicate> { +} diff --git a/mpush-common/src/main/java/com/mpush/common/condition/ScriptCondition.java b/mpush-common/src/main/java/com/mpush/common/condition/ScriptCondition.java new file mode 100644 index 00000000..3f41c40e --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/condition/ScriptCondition.java @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.condition; + +import javax.script.*; +import java.util.Map; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class ScriptCondition implements Condition { + private static final ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); + private static final ScriptEngine jsEngine = scriptEngineManager.getEngineByName("js"); + + private final String script; + + public ScriptCondition(String script) { + this.script = script; + } + + @Override + public boolean test(Map env) { + try { + return (Boolean) jsEngine.eval(script, new SimpleBindings(env)); + } catch (Exception e) { + return false; + } + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/condition/TagsCondition.java b/mpush-common/src/main/java/com/mpush/common/condition/TagsCondition.java new file mode 100644 index 00000000..1f87776a --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/condition/TagsCondition.java @@ -0,0 +1,46 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.condition; + +import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; + +import java.util.Map; +import java.util.Set; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class TagsCondition implements Condition { + private final Set tagList; + + public TagsCondition(Set tags) { + this.tagList = tags; + } + + @Override + public boolean test(Map env) { + //2.按标签过滤,目前只有include, 后续会增加exclude + String tags = (String) env.get("tags"); + return tags != null && tagList.stream().anyMatch(tags::contains); + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java index 4536d781..165e186a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayPushMessage.java @@ -22,14 +22,13 @@ import com.google.gson.reflect.TypeToken; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.api.protocol.UDPPacket; +import com.mpush.common.condition.*; import com.mpush.common.memory.PacketFactory; import com.mpush.common.message.ByteBufMessage; import com.mpush.tools.Jsons; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; -import java.net.InetSocketAddress; import java.util.Set; import static com.mpush.api.protocol.Command.GATEWAY_PUSH; @@ -41,11 +40,14 @@ */ public final class GatewayPushMessage extends ByteBufMessage { public String userId; - public Set tags; public int clientType; public int timeout; public byte[] content; + public String taskId; + public Set tags; + public String condition; + public GatewayPushMessage(Packet message, Connection connection) { super(message, connection); } @@ -59,19 +61,23 @@ public static GatewayPushMessage build(Connection connection) { @Override public void decode(ByteBuf body) { userId = decodeString(body); - tags = decodeSet(body); clientType = decodeInt(body); timeout = decodeInt(body); content = decodeBytes(body); + taskId = decodeString(body); + tags = decodeSet(body); + condition = decodeString(body); } @Override public void encode(ByteBuf body) { encodeString(body, userId); - encodeSet(body, tags); encodeInt(body, clientType); encodeInt(body, timeout); encodeBytes(body, content); + encodeString(body, taskId); + encodeSet(body, tags); + encodeString(body, condition); } private Set decodeSet(ByteBuf body) { @@ -116,6 +122,11 @@ public GatewayPushMessage setTags(Set tags) { return this; } + public GatewayPushMessage setCondition(String condition) { + this.condition = condition; + return this; + } + public boolean isBroadcast() { return userId == null; } @@ -134,6 +145,16 @@ public void send(ChannelFutureListener listener) { super.sendRaw(listener); } + public Condition getCondition() { + if (condition != null) { + return new ScriptCondition(condition); + } + if (tags != null) { + return new TagsCondition(tags); + } + return AwaysPassCondition.I; + } + @Override public String toString() { return "GatewayPushMessage{" + diff --git a/mpush-common/src/main/java/com/mpush/common/push/RedisBroadcastController.java b/mpush-common/src/main/java/com/mpush/common/push/RedisBroadcastController.java new file mode 100644 index 00000000..628adca8 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/push/RedisBroadcastController.java @@ -0,0 +1,100 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.push; + +import com.mpush.api.push.BroadcastController; +import com.mpush.cache.redis.RedisKey; +import com.mpush.cache.redis.manager.RedisManager; + +import java.util.List; + +/** + * Created by ohun on 16/10/25. + * + * @author ohun@live.cn (夜色) + */ +public final class RedisBroadcastController implements BroadcastController { + private static final String TASK_DONE_FIELD = "d"; + private static final String TASK_SEND_COUNT_FIELD = "sc"; + private static final String TASK_CANCEL_FIELD = "c"; + private static final String TASK_QPS_FIELD = "q"; + private static final String TASK_SUCCESS_USER_ID = "sui"; + + private final String taskId; + private final String taskKey; + private final String taskSuccessUIDKey; + + public RedisBroadcastController(String taskId) { + this.taskId = taskId; + this.taskKey = RedisKey.getPushTaskKey(taskId); + this.taskSuccessUIDKey = taskId + ':' + TASK_SUCCESS_USER_ID; + } + + @Override + public String taskId() { + return taskId; + } + + @Override + public int qps() { + Integer count = RedisManager.I.hget(taskKey, TASK_QPS_FIELD, Integer.TYPE); + return count == null ? 1000 : count; + } + + @Override + public void updateQps(int qps) { + RedisManager.I.hset(taskKey, TASK_QPS_FIELD, qps); + } + + @Override + public boolean isDone() { + return Boolean.TRUE.equals(RedisManager.I.hget(taskKey, TASK_DONE_FIELD, Boolean.class)); + } + + @Override + public int sendCount() { + Integer count = RedisManager.I.hget(taskKey, TASK_SEND_COUNT_FIELD, Integer.TYPE); + return count == null ? 0 : count; + } + + @Override + public void cancel() { + RedisManager.I.hset(taskKey, TASK_CANCEL_FIELD, 1); + } + + @Override + public boolean isCancelled() { + Integer status = RedisManager.I.hget(taskKey, TASK_CANCEL_FIELD, Integer.TYPE); + return status != null && status == 1; + } + + @Override + public int incSendCount(int count) { + return (int) RedisManager.I.hincrBy(taskKey, TASK_SEND_COUNT_FIELD, count); + } + + public void success(String userId) { + RedisManager.I.lpush(taskSuccessUIDKey, userId); + } + + public List successUserIds() { + return RedisManager.I.lrange(taskSuccessUIDKey, 0, -1, String.class); + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java index ed58d166..03fa176d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java @@ -35,7 +35,7 @@ * * @author ohun@live.cn (夜色) */ -public class AckHandler extends BaseMessageHandler { +public final class AckHandler extends BaseMessageHandler { @Override public AckMessage decode(Packet packet, Connection connection) { diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 2b652274..1f86b17f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -21,36 +21,9 @@ import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; -import com.mpush.common.ErrorCode; import com.mpush.common.handler.BaseMessageHandler; -import com.mpush.common.message.ErrorMessage; -import com.mpush.common.message.OkMessage; -import com.mpush.common.message.PushMessage; import com.mpush.common.message.gateway.GatewayPushMessage; -import com.mpush.common.router.RemoteRouter; -import com.mpush.common.user.UserManager; -import com.mpush.core.ack.AckCallback; -import com.mpush.core.ack.AckContext; -import com.mpush.core.ack.AckMessageQueue; -import com.mpush.core.router.LocalRouter; -import com.mpush.core.router.LocalRouterManager; -import com.mpush.core.router.RouterCenter; -import com.mpush.tools.Jsons; -import com.mpush.tools.Utils; -import com.mpush.tools.common.Pair; -import com.mpush.tools.log.Logs; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.atomic.AtomicInteger; - -import static com.mpush.api.protocol.Command.ERROR; -import static com.mpush.api.protocol.Command.OK; -import static com.mpush.common.ErrorCode.*; +import com.mpush.core.push.*; /** * Created by ohun on 2015/12/30. @@ -59,6 +32,10 @@ */ public final class GatewayPushHandler extends BaseMessageHandler { + private final PushCenter pushCenter = PushCenter.I; + + private final GlobalFlowControl globalFlowControl = new GlobalFlowControl(1000, Integer.MAX_VALUE, 1000); + @Override public GatewayPushMessage decode(Packet packet, Connection connection) { return new GatewayPushMessage(packet, connection); @@ -85,232 +62,13 @@ public GatewayPushMessage decode(Packet packet, Connection connection) { @Override public void handle(GatewayPushMessage message) { if (message.isBroadcast()) { - sendBroadcast(message); + FlowControl flowControl + = message.taskId == null + ? new FastFlowControl(5000, 100000, 1000) //5k 10w 1s + : new RedisFlowControl(message.taskId); + pushCenter.addTask(new BroadcastPushTask(message, flowControl)); } else { - if (!checkLocal(message)) { - checkRemote(message); - } - } - } - - /** - * 广播所有在线用户 - * - * @param message message - */ - private void sendBroadcast(GatewayPushMessage message) { - Set sendUserIds = new CopyOnWriteArraySet<>(); - Set tagList = message.tags; - LocalRouterManager routerManager = RouterCenter.I.getLocalRouterManager(); - AtomicInteger tasks = new AtomicInteger();//总任务数, 0表示任务全部结束 - long begin = System.currentTimeMillis(); - //TODO 考虑使用线程池批量推送 - for (int start = 0, limit = 1000; ; start += limit) { - List userIds = UserManager.I.getOnlineUserList(start, limit); - tasks.addAndGet(userIds.size());//增加任务数 - userIds.forEach(userId -> { - for (LocalRouter router : routerManager.lookupAll(userId)) { - Connection connection = router.getRouteValue(); - int clientType = router.getClientType(); - - //2.按标签过滤,目前只有include,后续会增加exclude - String tags = connection.getSessionContext().tags; - if (tagList != null && tags != null) { - if (tagList.stream().noneMatch(tags::contains)) { - tasks.decrementAndGet(); - if (tasks.get() == 0) {//任务全部结束 - Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); - OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); - } - continue; - } - } - - if (connection.isConnected()) { - - if (connection.getChannel().isWritable()) {//检测TCP缓冲区是否已满且写队列超过最高阀值 - //3.链接可用,直接下发消息到手机客户端 - PushMessage pushMessage = new PushMessage(message.content, connection); - //pushMessage.getPacket().flags = message.getPacket().flags; - - pushMessage.send(future -> { - - if (!sendUserIds.contains(userId)) { - tasks.decrementAndGet();//完成一个任务 - } - - if (future.isSuccess()) {//推送成功 - sendUserIds.add(userId); - Logs.PUSH.info("<<< gateway broadcast client success, userId={}, message={}", userId, message); - - } else {//推送失败 - Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", userId, message); - } - - if (tasks.get() == 0) {//任务全部结束 - Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); - OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); - } - }); - } else { - tasks.decrementAndGet();//完成一个任务 - Logs.PUSH.info("gateway broadcast client failure, send too busy, userId={}, message={}", userId, message); - } - } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 - Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); - - tasks.decrementAndGet();//完成一个任务 - - //删除已经失效的本地路由 - RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); - - if (tasks.get() == 0) {//任务全部结束 - Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); - OkMessage.from(message).setData(Jsons.toJson(sendUserIds)).sendRaw(); - } - } - } - }); - - if (userIds.size() != limit) break;//查询完毕 - } - } - - /** - * 检查本地路由,如果存在并且链接可用直接推送 - * 否则要检查下远程路由 - * - * @param message message - * @return true/false true:success - */ - - private boolean checkLocal(final GatewayPushMessage message) { - String userId = message.userId; - int clientType = message.clientType; - LocalRouter localRouter = RouterCenter.I.getLocalRouterManager().lookup(userId, clientType); - - //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 - if (localRouter == null) return false; - - Connection connection = localRouter.getRouteValue(); - - //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 - if (!connection.isConnected()) { - - Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection); - - //删除已经失效的本地路由 - RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); - - return false; - } - - //3.检测TCP缓冲区是否已满且写队列超过最高阀值 - if (!connection.getChannel().isWritable()) { - ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); - - Logs.PUSH.info("gateway push message to client failure, send too busy, message={}", message); - return true; - } - - //4.链接可用,直接下发消息到手机客户端 - PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.getPacket().flags = message.getPacket().flags; - - pushMessage.send(future -> { - if (future.isSuccess()) {//推送成功 - - if (message.needAck()) {//需要客户端ACK, 消息进队列等待客户端响应ACK - AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message), message.timeout); - } else { - OkMessage.from(message).setData(userId + ',' + clientType).sendRaw(); - } - - Logs.PUSH.info("<<< gateway push message to client success, message={}", message); - - } else {//推送失败 - - ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); - - Logs.PUSH.info("gateway push message to client failure, message={}", message); - } - }); - return true; - } - - /** - * 检测远程路由, - * 如果不存在直接返回用户已经下线 - * 如果是本机直接删除路由信息 - * 如果是其他机器让PushClient重推 - * - * @param message message - */ - private void checkRemote(GatewayPushMessage message) { - String userId = message.userId; - int clientType = message.clientType; - RemoteRouter remoteRouter = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); - - // 1.如果远程路由信息也不存在, 说明用户此时不在线, - if (remoteRouter == null || remoteRouter.isOffline()) { - - ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); - - Logs.PUSH.info("gateway push, router not exists user offline, message={}", message); - - return; - } - - //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 - if (Utils.getLocalIp().equals(remoteRouter.getRouteValue().getHost())) { - - ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); - - //删除失效的远程缓存 - RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); - - Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, remoteRouter); - - return; + pushCenter.addTask(new SingleUserPushTask(message, globalFlowControl)); } - - //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 - ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).sendRaw(); - - Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, remoteRouter); - - } - - - private AckContext buildAckContext(GatewayPushMessage message) { - message.getPacket().body = null;//内存释放 - message.content = null;//内存释放 - return new AckContext().setCallback(new AckCallback() { - @Override - public void onSuccess(AckContext ctx) { - if (!message.getConnection().isConnected()) { - Logs.PUSH.info(">>> receive client ack, gateway connection is closed, context={}", ctx); - return; - } - - OkMessage okMessage = OkMessage.from(message); - okMessage.setData(message.userId + ',' + message.clientType); - okMessage.sendRaw(); - Logs.PUSH.info(">>> receive client ack and response gateway client success, context={}", ctx); - } - - @Override - public void onTimeout(AckContext ctx) { - if (!message.getConnection().isConnected()) { - Logs.PUSH.info("push message timeout client not ack, gateway connection is closed, context={}", ctx); - return; - } - ErrorMessage errorMessage = ErrorMessage.from(message); - errorMessage.setData(message.userId + ',' + message.clientType); - errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT); - errorMessage.sendRaw(); - Logs.PUSH.info("push message timeout client not ack, context={}", ctx); - } - }); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index f72e43ad..38adf07f 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -29,6 +29,7 @@ import com.mpush.common.message.HttpResponseMessage; import com.mpush.netty.http.HttpCallback; import com.mpush.netty.http.HttpClient; +import com.mpush.netty.http.NettyHttpClient; import com.mpush.netty.http.RequestContext; import com.mpush.tools.common.Profiler; import com.mpush.tools.log.Logs; @@ -51,14 +52,9 @@ */ public class HttpProxyHandler extends BaseMessageHandler { private static final Logger LOGGER = Logs.HTTP; - private final HttpClient httpClient; + private final HttpClient httpClient = NettyHttpClient.I(); private final DnsMappingManager dnsMappingManager = DnsMappingManager.create(); - public HttpProxyHandler(HttpClient httpClient) { - this.httpClient = httpClient; - this.httpClient.start(); - } - @Override public HttpRequestMessage decode(Packet packet, Connection connection) { return new HttpRequestMessage(packet, connection); diff --git a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java new file mode 100644 index 00000000..888d0896 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java @@ -0,0 +1,147 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.SessionContext; +import com.mpush.api.protocol.Packet; +import com.mpush.common.condition.AwaysPassCondition; +import com.mpush.common.condition.Condition; +import com.mpush.common.message.OkMessage; +import com.mpush.common.message.PushMessage; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.core.router.LocalRouter; +import com.mpush.core.router.RouterCenter; +import com.mpush.tools.log.Logs; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class BroadcastPushTask implements PushTask, ChannelFutureListener { + + private final long begin = System.currentTimeMillis(); + + private final AtomicInteger finishTasks = new AtomicInteger(0); + + private final FlowControl flowControl; + + private final GatewayPushMessage message; + + private final Condition condition; + + private final Iterator>> iterator; + + private Packet cachedPacket;//内存优化,因为广播所有人的消息都一样 + + public BroadcastPushTask(GatewayPushMessage message, FlowControl flowControl) { + this.message = message; + this.flowControl = flowControl; + this.condition = message.getCondition(); + this.iterator = RouterCenter.I.getLocalRouterManager().routers().entrySet().iterator(); + } + + @Override + public void run() { + boolean cancelled = false; + try { + flowControl.reset(); + iterator.forEachRemaining(entry -> { + + String userId = entry.getKey(); + entry.getValue().forEach((clientType, router) -> { + + Connection connection = router.getRouteValue(); + + if (checkCondition(condition, connection)) {//1.条件检测 + if (connection.isConnected()) { + if (connection.getChannel().isWritable()) { //检测TCP缓冲区是否已满且写队列超过最高阀值 + if (flowControl.checkQps()) { + if (cachedPacket == null) {//内存优化,因为广播所有人的消息都一样 + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.send(this); + cachedPacket = pushMessage.getPacket(); + } else {//内存优化,因为广播所有人的消息都一样 + connection.send(cachedPacket, this); + } + } else if (iterator.hasNext()) { + PushCenter.I.delayTask(flowControl.getRemaining(), this); + } + } + } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 + Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); + //删除已经失效的本地路由 + RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); + } + } + + }); + + }); + } catch (OverFlowException e) { + cancelled = true; + } + + + if (cancelled || !iterator.hasNext()) {//done + if (finishTasks.addAndGet(flowControl.total()) == 0) { + report(); + } + } + flowControl.incTotal(); + } + + private void report() { + Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + OkMessage.from(message).sendRaw();//通知发送方,广播推送完毕 + } + + private boolean checkCondition(Condition condition, Connection connection) { + if (condition == AwaysPassCondition.I) return true; + SessionContext sessionContext = connection.getSessionContext(); + Map env = new HashMap<>(); + env.put("userId", sessionContext.userId); + env.put("tags", sessionContext.tags); + env.put("clientVersion", sessionContext.clientVersion); + env.put("osName", sessionContext.osName); + env.put("osVersion", sessionContext.osVersion); + return condition.test(env); + } + + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) {//推送成功 + Logs.PUSH.info("<<< gateway broadcast client success, userId={}, message={}", message.userId, message); + } else {//推送失败 + Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", message.userId, message); + } + if (finishTasks.decrementAndGet() == 0) { + report(); + } + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java new file mode 100644 index 00000000..d5402544 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java @@ -0,0 +1,89 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class FastFlowControl implements FlowControl { + private final int limit; + private final int maxLimit; + private final int duration; + private int count; + private int total; + private long start; + + public FastFlowControl(int limit, int maxLimit, int duration) { + this.limit = limit; + this.maxLimit = maxLimit; + this.duration = duration; + } + + public FastFlowControl(int limit) { + this.limit = limit; + this.maxLimit = limit * 100; + this.duration = 1000;//1s + } + + @Override + public void reset() { + count = 0; + start = System.currentTimeMillis(); + } + + @Override + public int total() { + return total; + } + + @Override + public boolean checkQps() { + if (count < limit) { + count++; + return true; + } + + if (total > maxLimit) throw new OverFlowException(); + + if (System.currentTimeMillis() - start > duration) { + reset(); + return true; + } + return false; + } + + @Override + public int incTotal() { + return total; + } + + @Override + public int getRemaining() { + return duration - (int) (System.currentTimeMillis() - start); + } + + @Override + public String report() { + return "total:d%, count:%d, qps:d%"; + } + +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java new file mode 100644 index 00000000..362e1595 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java @@ -0,0 +1,41 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public interface FlowControl { + + void reset(); + + int total(); + + boolean checkQps() throws OverFlowException; + + int incTotal(); + + int getRemaining(); + + String report(); + +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java new file mode 100644 index 00000000..369e59f0 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java @@ -0,0 +1,84 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class GlobalFlowControl implements FlowControl { + private final int limit; + private final int maxLimit; + private final int duration; + private final AtomicInteger count = new AtomicInteger(); + private final AtomicInteger total = new AtomicInteger(); + private volatile long start; + + public GlobalFlowControl(int limit, int maxLimit, int duration) { + this.limit = limit; + this.maxLimit = maxLimit; + this.duration = duration; + } + + @Override + public void reset() { + count.set(0); + start = System.currentTimeMillis(); + } + + @Override + public int total() { + return total.get(); + } + + @Override + public boolean checkQps() { + if (count.incrementAndGet() < limit) { + return true; + } + + if (total.get() > maxLimit) throw new OverFlowException(); + + if (System.currentTimeMillis() - start > duration) { + reset(); + return true; + } + return false; + } + + @Override + public int incTotal() { + return total.get(); + } + + @Override + public int getRemaining() { + return duration - (int) (System.currentTimeMillis() - start); + } + + @Override + public String report() { + return "total:d%, count:%d, qps:d%"; + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java b/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java new file mode 100644 index 00000000..387e04fa --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java @@ -0,0 +1,36 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class OverFlowException extends RuntimeException { + + public OverFlowException() { + super(null, null, false, false); + } + + public OverFlowException(String message) { + super(message, null, false, false); + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java new file mode 100644 index 00000000..83ac841f --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java @@ -0,0 +1,73 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +import com.mpush.api.push.PushException; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.thread.NamedPoolThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import static com.mpush.tools.thread.ThreadNames.T_PUSH_CENTER_TIMER; +import static com.mpush.tools.thread.ThreadNames.T_PUSH_REQ_TIMER; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class PushCenter extends BaseService { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + public static final PushCenter I = new PushCenter(); + + private ScheduledExecutorService executor; + + private PushCenter() { + } + + public void addTask(PushTask task) { + executor.execute(task); + } + + public void delayTask(int delay, PushTask task) { + executor.schedule(task, delay, TimeUnit.MILLISECONDS); + } + + @Override + protected void doStart(Listener listener) throws Throwable { + executor = new ScheduledThreadPoolExecutor(4, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), (r, e) -> { + logger.error("one push task was rejected, task=" + r); + throw new PushException("one push request was rejected. request=" + r); + }); + listener.onSuccess(); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + executor.shutdown(); + listener.onSuccess(); + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/PushTask.java b/mpush-core/src/main/java/com/mpush/core/push/PushTask.java new file mode 100644 index 00000000..39bdf6e6 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/PushTask.java @@ -0,0 +1,28 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public interface PushTask extends Runnable { +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java new file mode 100644 index 00000000..2d17c161 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java @@ -0,0 +1,102 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +import com.mpush.api.push.BroadcastController; +import com.mpush.common.push.RedisBroadcastController; + +/** + * Created by ohun on 16/10/25. + * + * @author ohun@live.cn (夜色) + */ +public final class RedisFlowControl implements FlowControl { + + private final BroadcastController controller; + + private final int maxLimit = 100000;//10w + private final int duration = 1000; + private int limit; + private int count; + private int total; + private long start; + + public RedisFlowControl(String taskId) { + this.controller = new RedisBroadcastController(taskId); + this.limit = controller.qps(); + } + + @Override + public void reset() { + count = 0; + start = System.currentTimeMillis(); + } + + @Override + public int total() { + return total; + } + + @Override + public boolean checkQps() throws OverFlowException { + if (count < limit) { + count++; + total++; + return true; + } + + if (total() > maxLimit) { + throw new OverFlowException(); + } + + if (System.currentTimeMillis() - start > duration) { + reset(); + total++; + return true; + } + + if (controller.isCancelled()) { + throw new OverFlowException(); + } else { + limit = controller.qps(); + } + return false; + } + + @Override + public int incTotal() { + int t = total; + if (total > 0) { + total = 0; + return controller.incSendCount(t); + } + return 0; + } + + @Override + public int getRemaining() { + return duration - (int) (System.currentTimeMillis() - start); + } + + @Override + public String report() { + return ""; + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java new file mode 100644 index 00000000..3b02f548 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java @@ -0,0 +1,203 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.push; + +import com.mpush.api.connection.Connection; +import com.mpush.common.ErrorCode; +import com.mpush.common.message.ErrorMessage; +import com.mpush.common.message.OkMessage; +import com.mpush.common.message.PushMessage; +import com.mpush.common.message.gateway.GatewayPushMessage; +import com.mpush.common.router.RemoteRouter; +import com.mpush.core.ack.AckCallback; +import com.mpush.core.ack.AckContext; +import com.mpush.core.ack.AckMessageQueue; +import com.mpush.core.router.LocalRouter; +import com.mpush.core.router.RouterCenter; +import com.mpush.tools.Utils; +import com.mpush.tools.log.Logs; + +import static com.mpush.common.ErrorCode.OFFLINE; +import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; +import static com.mpush.common.ErrorCode.ROUTER_CHANGE; + +/** + * Created by ohun on 16/10/24. + * + * @author ohun@live.cn (夜色) + */ +public final class SingleUserPushTask implements PushTask { + private final FlowControl flowControl; + + private final GatewayPushMessage message; + + public SingleUserPushTask(GatewayPushMessage message, FlowControl flowControl) { + this.flowControl = flowControl; + this.message = message; + } + + @Override + public void run() { + if (!checkLocal(message)) { + checkRemote(message); + } + } + + /** + * 检查本地路由,如果存在并且链接可用直接推送 + * 否则要检查下远程路由 + * + * @param message message + * @return true/false true:success + */ + private boolean checkLocal(final GatewayPushMessage message) { + String userId = message.userId; + int clientType = message.clientType; + LocalRouter localRouter = RouterCenter.I.getLocalRouterManager().lookup(userId, clientType); + + //1.如果本机不存在,再查下远程,看用户是否登陆到其他机器 + if (localRouter == null) return false; + + Connection connection = localRouter.getRouteValue(); + + //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 + if (!connection.isConnected()) { + + Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection); + + //删除已经失效的本地路由 + RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); + + return false; + } + + //3.检测TCP缓冲区是否已满且写队列超过最高阀值 + if (!connection.getChannel().isWritable()) { + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); + + Logs.PUSH.info("gateway push message to client failure, send too busy, message={}", message); + return true; + } + + if (flowControl.checkQps()) { + //4.链接可用,直接下发消息到手机客户端 + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.getPacket().flags = message.getPacket().flags; + + pushMessage.send(future -> { + if (future.isSuccess()) {//推送成功 + + if (message.needAck()) {//需要客户端ACK, 消息进队列等待客户端响应ACK + AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message), message.timeout); + } else { + OkMessage.from(message).setData(userId + ',' + clientType).sendRaw(); + } + + Logs.PUSH.info("<<< gateway push message to client success, message={}", message); + + } else {//推送失败 + + ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); + + Logs.PUSH.info("gateway push message to client failure, message={}", message); + } + }); + } else { + PushCenter.I.delayTask(flowControl.getRemaining(), this); + } + return true; + } + + /** + * 检测远程路由, + * 如果不存在直接返回用户已经下线 + * 如果是本机直接删除路由信息 + * 如果是其他机器让PushClient重推 + * + * @param message message + */ + private void checkRemote(GatewayPushMessage message) { + String userId = message.userId; + int clientType = message.clientType; + RemoteRouter remoteRouter = RouterCenter.I.getRemoteRouterManager().lookup(userId, clientType); + + // 1.如果远程路由信息也不存在, 说明用户此时不在线, + if (remoteRouter == null || remoteRouter.isOffline()) { + + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); + + Logs.PUSH.info("gateway push, router not exists user offline, message={}", message); + + return; + } + + //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 + if (Utils.getLocalIp().equals(remoteRouter.getRouteValue().getHost())) { + + ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); + + //删除失效的远程缓存 + RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); + + Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, remoteRouter); + + return; + } + + //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 + ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).sendRaw(); + + Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, remoteRouter); + + } + + + private AckContext buildAckContext(GatewayPushMessage message) { + message.getPacket().body = null;//内存释放 + message.content = null;//内存释放 + return new AckContext().setCallback(new AckCallback() { + @Override + public void onSuccess(AckContext ctx) { + if (!message.getConnection().isConnected()) { + Logs.PUSH.info(">>> receive client ack, gateway connection is closed, context={}", ctx); + return; + } + + OkMessage okMessage = OkMessage.from(message); + okMessage.setData(message.userId + ',' + message.clientType); + okMessage.sendRaw(); + Logs.PUSH.info(">>> receive client ack and response gateway client success, context={}", ctx); + } + + @Override + public void onTimeout(AckContext ctx) { + if (!message.getConnection().isConnected()) { + Logs.PUSH.info("push message timeout client not ack, gateway connection is closed, context={}", ctx); + return; + } + ErrorMessage errorMessage = ErrorMessage.from(message); + errorMessage.setData(message.userId + ',' + message.clientType); + errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT); + errorMessage.sendRaw(); + Logs.PUSH.info("push message timeout client not ack, context={}", ctx); + } + }); + } +} diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 2ed8d22c..21bead5e 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -40,7 +40,7 @@ * @author ohun@live.cn */ public final class LocalRouterManager extends EventConsumer implements RouterManager { - public static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); + private static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); private static final Map EMPTY = Collections.unmodifiableMap(new HashMap<>(0)); /** @@ -74,6 +74,10 @@ public LocalRouter lookup(String userId, int clientType) { return router; } + public Map> routers() { + return routers; + } + /** * 监听链接关闭事件,清理失效的路由 * diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 0235166c..eb3a7984 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -26,8 +26,6 @@ import com.mpush.api.spi.handler.PushHandlerFactory; import com.mpush.common.MessageDispatcher; import com.mpush.core.handler.*; -import com.mpush.netty.http.HttpClient; -import com.mpush.netty.http.NettyHttpClient; import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; @@ -59,7 +57,6 @@ public final class ConnectionServer extends NettyTCPServer { private ScheduledExecutorService trafficShapingExecutor; private ConnectionManager connectionManager = new ServerConnectionManager(true); - private HttpClient httpClient; public static ConnectionServer I() { if (I == null) { @@ -86,10 +83,8 @@ public void init() { receiver.register(Command.FAST_CONNECT, new FastConnectHandler()); receiver.register(Command.PUSH, PushHandlerFactory.create()); receiver.register(Command.ACK, new AckHandler()); - if (CC.mp.http.proxy_enabled) { - httpClient = new NettyHttpClient(); - receiver.register(Command.HTTP_PROXY, new HttpProxyHandler(httpClient)); + receiver.register(Command.HTTP_PROXY, new HttpProxyHandler()); } channelHandler = new ServerChannelHandler(true, connectionManager, receiver); @@ -110,9 +105,6 @@ public void stop(Listener listener) { trafficShapingHandler.release(); trafficShapingExecutor.shutdown(); } - if (httpClient != null && httpClient.isRunning()) { - httpClient.stop(); - } connectionManager.destroy(); } @@ -183,8 +175,4 @@ public ChannelHandler getChannelHandler() { public ConnectionManager getConnectionManager() { return connectionManager; } - - public HttpClient getHttpClient() { - return httpClient; - } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index b5588175..1b87eb3c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -56,6 +56,7 @@ * @author ohun@live.cn */ public class NettyHttpClient extends BaseService implements HttpClient { + private static HttpClient I; private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); private static final int maxContentLength = (int) CC.mp.http.max_content_length; /*package*/ final AttributeKey requestKey = AttributeKey.newInstance("request"); @@ -64,6 +65,15 @@ public class NettyHttpClient extends BaseService implements HttpClient { private EventLoopGroup workerGroup; private Timer timer; + public static HttpClient I() { + if (I == null) { + synchronized (NettyHttpClient.class) { + I = new NettyHttpClient(); + } + } + return I; + } + @Override public void request(RequestContext context) throws Exception { URI uri = new URI(context.request.uri()); diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java index 1d0aaf12..616e8b23 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java @@ -88,6 +88,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception DatagramPacket datagramPacket = (DatagramPacket) msg; Packet packet = PacketDecoder.decodeFrame(datagramPacket); receiver.onReceive(packet, connection); + datagramPacket.release();//最后一个使用方要释放引用 } @Override diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index 6aea0e7f..c75ddee1 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -19,6 +19,7 @@ package com.mpush.test.push; +import com.google.common.collect.Sets; import com.mpush.api.push.*; import com.mpush.tools.log.Logs; import org.junit.Test; @@ -43,9 +44,11 @@ public void testPush() throws Exception { msg.setMsgId("msgId_0"); PushContext context = PushContext.build(msg) - .setBroadcast(false) + .setBroadcast(true) .setAckModel(AckModel.AUTO_ACK) - .setUserIds(Arrays.asList("user-0", "user-1")) + //.setTags(Sets.newHashSet("test")) + .setCondition("tags&&tags.indexOf('test')!=-1") + //.setUserIds(Arrays.asList("user-0", "user-1")) .setTimeout(2000) .setCallback(new PushCallback() { @Override diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index 2eb02e96..d238ea79 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -1,13 +1,11 @@ mp.log.dir=${user.dir}/mpush-test/target/logs mp.log.level=debug -mp.min-heartbeat=10s -mp.max-heartbeat=10s -mp.zk.namespace=mpush -mp.zk.server-address="127.0.0.1:2181" +mp.core.min-heartbeat=10s +mp.core.max-heartbeat=10s mp.core.compress-threshold=10k -mp.http.proxy-enabled=true -mp.net.gateway-server-net=udp -mp.redis { - #redis 集群配置,group 是个二维数组,第一层表示有多少组集群,每个组下面可以有多台机器 +mp.zk.server-address="127.0.0.1:2181" +mp.redis {// redis 集群配置 nodes:["127.0.0.1:6379"]//格式是ip:port,密码可以没有ip:port -} \ No newline at end of file +} +mp.http.proxy-enabled=true +mp.net.gateway-server-net=udp //网关服务使用的网络类型tcp/udp diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index 313e8420..4eb5b003 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -44,8 +44,9 @@ public final class ThreadNames { public static final String T_BIZ = NS + "-biz"; public static final String T_PUSH_CALLBACK = NS + "-push-callback"; - public static final String T_PUSH_REQ_TIMER = NS + "-push-timer"; + public static final String T_PUSH_REQ_TIMER = NS + "-push-req-timer"; public static final String T_ARK_REQ_TIMER = NS + "-ack-timer"; + public static final String T_PUSH_CENTER_TIMER = NS + "-push-center-timer"; public static final String T_MONITOR = NS + "-monitor"; /** diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index d06da262..754deb21 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -40,6 +40,7 @@ public class ThreadPoolManager { private Executor redisExecutor; private Executor httpExecutor; private Executor pushCallbackExecutor; + private Executor pushCenterExecutor; public final Thread newThread(String name, Runnable target) { return threadFactory.newThread(name, target); From 9a17ef3e70e4b9d6a41b3c66c323e2a149c717a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 26 Oct 2016 18:11:21 +0800 Subject: [PATCH 718/890] =?UTF-8?q?push=E4=B8=AD=E5=BF=83=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B5=81=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/src/main/java/com/mpush/api/push/PushContext.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java index 85bfc6d8..9de96fd3 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushContext.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushContext.java @@ -79,7 +79,7 @@ public class PushContext { private Set tags; /** - * 条件表达式, 满足条件的用户会被推送,目前支持的脚步语言为js + * 条件表达式, 满足条件的用户会被推送,目前支持的脚本语言为js * 可以使用的参数为 userId,tags,clientVersion,osName,osVersion * 比如 : * 灰度:userId % 100 < 20 From 99997b7d0d67318900567a3915fca4f4ae1d9aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 26 Oct 2016 22:53:35 +0800 Subject: [PATCH 719/890] =?UTF-8?q?=E5=B9=BF=E6=92=AD=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/push/PushCallback.java | 18 +++--------------- .../java/com/mpush/api/push/PushResult.java | 11 ----------- .../gateway/handler/GatewayOKHandler.java | 2 +- .../com/mpush/client/push/PushRequest.java | 11 ++++++----- 4 files changed, 10 insertions(+), 32 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java index 1dfa1158..03f9480a 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushCallback.java @@ -14,11 +14,7 @@ public interface PushCallback { default void onResult(PushResult result) { switch (result.resultCode) { case PushResult.CODE_SUCCESS: - if (result.userId != null) { - onSuccess(result.userId, result.location); - } else { - onSuccess(result.userIds); - } + onSuccess(result.userId, result.location); break; case PushResult.CODE_FAILURE: onFailure(result.userId, result.location); @@ -35,8 +31,8 @@ default void onResult(PushResult result) { /** * 推送成功, 指定用户推送时重写此方法 * - * @param userId 成功的用户 - * @param location 用户所在机器 + * @param userId 成功的用户, 如果是广播, 值为空 + * @param location 用户所在机器, 如果是广播, 值为空 */ default void onSuccess(String userId, ClientLocation location) { } @@ -67,12 +63,4 @@ default void onOffline(String userId, ClientLocation location) { */ default void onTimeout(String userId, ClientLocation location) { } - - /** - * 推送成功, 广播时重写此方法 - * - * @param userIds 推送成功的用户列表 - */ - default void onSuccess(String[] userIds) { - } } \ No newline at end of file diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java index 9a6c53cf..f5e85e36 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java @@ -35,7 +35,6 @@ public class PushResult { public static final int CODE_TIMEOUT = 4; public int resultCode; public String userId; - public String[] userIds; public Object[] timeLine; public ClientLocation location; @@ -61,15 +60,6 @@ public PushResult setUserId(String userId) { return this; } - public String[] getUserIds() { - return userIds; - } - - public PushResult setUserIds(String[] userIds) { - this.userIds = userIds; - return this; - } - public Object[] getTimeLine() { return timeLine; } @@ -107,7 +97,6 @@ public String toString() { return "PushResult{" + "resultCode=" + getResultDesc() + ", userId='" + userId + '\'' + - ", userIds=" + Arrays.toString(userIds) + ", timeLine=" + Arrays.toString(timeLine) + ", location=" + location + '}'; diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java index 6a445481..0ec163a0 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayOKHandler.java @@ -50,7 +50,7 @@ public void handle(OkMessage message) { Logs.PUSH.warn("receive a gateway response, but request has timeout. message={}", message); return; } - request.success(message.data);//推送成功 + request.success();//推送成功 } } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 6bfb0b5d..a9e28e1f 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -63,7 +63,6 @@ private enum Status {init, success, failure, offline, timeout} private int timeout; private ClientLocation location; private Future future; - private String result; private void sendToConnServer(RemoteRouter remoteRouter) { timeLine.addTimePoint("lookup-remote"); @@ -133,7 +132,6 @@ public void run() { } else { callback.onResult(new PushResult(status.get().ordinal()) .setUserId(userId) - .setUserIds(userId == null ? Jsons.fromJson(result, String[].class) : null) .setLocation(location) .setTimeLine(timeLine.getTimePoints()) ); @@ -183,7 +181,11 @@ public FutureTask broadcast() { pushMessage.sendRaw(f -> { if (!f.isSuccess()) failure(); }); - future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + if (pushMessage.taskId == null) { + future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + } else { + success(); + } } ); @@ -194,8 +196,7 @@ public void timeout() { submit(Status.timeout); } - public void success(String data) { - this.result = data; + public void success() { submit(Status.success); } From cbeb54bfb8ac3970a3c5188e4c209eda6ffb7b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 26 Oct 2016 22:54:30 +0800 Subject: [PATCH 720/890] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=AE=9A=E4=B9=89=E5=A2=9E=E5=8A=A0SPI=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/connection/SessionContext.java | 5 ++- .../mpush/api/router/ClientClassifier.java | 33 ++++++++++++++ .../com/mpush/api/router/ClientLocation.java | 2 +- .../spi/router/ClientClassifierFactory.java | 37 ++++++++++++++++ .../com/mpush/common}/router/ClientType.java | 2 +- .../router/DefaultClientClassifier.java | 43 +++++++++++++++++++ ...ush.api.spi.router.ClientClassifierFactory | 1 + .../com/mpush/core/router/LocalRouter.java | 1 - .../mpush/core/router/LocalRouterManager.java | 1 - 9 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/router/ClientClassifier.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java rename {mpush-api/src/main/java/com/mpush/api => mpush-common/src/main/java/com/mpush/common}/router/ClientType.java (97%) create mode 100644 mpush-common/src/main/java/com/mpush/common/router/DefaultClientClassifier.java create mode 100644 mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.router.ClientClassifierFactory diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index 6b8cb2f0..654d3a06 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -19,7 +19,8 @@ package com.mpush.api.connection; -import com.mpush.api.router.ClientType; + +import com.mpush.api.router.ClientClassifier; /** * Created by ohun on 2015/12/22. @@ -76,7 +77,7 @@ public boolean handshakeOk() { public int getClientType() { if (clientType == 0) { - clientType = ClientType.find(osName).type; + clientType = ClientClassifier.I.getClientType(osName); } return clientType; } diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientClassifier.java b/mpush-api/src/main/java/com/mpush/api/router/ClientClassifier.java new file mode 100644 index 00000000..406dbe91 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientClassifier.java @@ -0,0 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.router; + +import com.mpush.api.spi.router.ClientClassifierFactory; + +/** + * Created by ohun on 16/10/26. + * + * @author ohun@live.cn (夜色) + */ +public interface ClientClassifier { + ClientClassifier I = ClientClassifierFactory.create(); + + int getClientType(String osName); +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index a3fba473..e6963658 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -102,7 +102,7 @@ public void setConnId(String connId) { public int getClientType() { if (clientType == 0) { - clientType = ClientType.find(osName).type; + clientType = ClientClassifier.I.getClientType(osName); } return clientType; } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java new file mode 100644 index 00000000..7199adfc --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.router; + +import com.mpush.api.router.ClientClassifier; +import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; +import com.mpush.api.spi.net.DnsMappingManager; + +/** + * Created by ohun on 16/10/26. + * + * @author ohun@live.cn (夜色) + */ +public interface ClientClassifierFactory extends Factory { + + static ClientClassifier create() { + return SpiLoader.load(ClientClassifierFactory.class).get(); + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientType.java b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java similarity index 97% rename from mpush-api/src/main/java/com/mpush/api/router/ClientType.java rename to mpush-common/src/main/java/com/mpush/common/router/ClientType.java index 14216be5..9520d076 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientType.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java @@ -17,7 +17,7 @@ * ohun@live.cn (夜色) */ -package com.mpush.api.router; +package com.mpush.common.router; import java.util.Arrays; diff --git a/mpush-common/src/main/java/com/mpush/common/router/DefaultClientClassifier.java b/mpush-common/src/main/java/com/mpush/common/router/DefaultClientClassifier.java new file mode 100644 index 00000000..07aaf8d9 --- /dev/null +++ b/mpush-common/src/main/java/com/mpush/common/router/DefaultClientClassifier.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.common.router; + +import com.mpush.api.router.ClientClassifier; +import com.mpush.api.spi.Spi; +import com.mpush.api.spi.router.ClientClassifierFactory; + +/** + * Created by ohun on 16/10/26. + * + * @author ohun@live.cn (夜色) + */ +@Spi(order = 1) +public final class DefaultClientClassifier implements ClientClassifier, ClientClassifierFactory { + + @Override + public int getClientType(String osName) { + return ClientType.find(osName).type; + } + + @Override + public ClientClassifier get() { + return this; + } +} diff --git a/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.router.ClientClassifierFactory b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.router.ClientClassifierFactory new file mode 100644 index 00000000..a3b2f119 --- /dev/null +++ b/mpush-common/src/main/resources/META-INF/services/com.mpush.api.spi.router.ClientClassifierFactory @@ -0,0 +1 @@ +com.mpush.common.router.DefaultClientClassifier \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index 43130d9b..1592ea0e 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -20,7 +20,6 @@ package com.mpush.core.router; import com.mpush.api.connection.Connection; -import com.mpush.api.router.ClientType; import com.mpush.api.router.Router; /** diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 21bead5e..50101a8a 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -24,7 +24,6 @@ import com.mpush.api.connection.SessionContext; import com.mpush.api.event.ConnectionCloseEvent; import com.mpush.api.event.UserOfflineEvent; -import com.mpush.api.router.ClientType; import com.mpush.api.router.RouterManager; import com.mpush.tools.event.EventBus; import com.mpush.tools.event.EventConsumer; From 0ff7a22790837eb16af6d7061a32cc05111ee800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 27 Oct 2016 00:09:26 +0800 Subject: [PATCH 721/890] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=AE=9A=E4=B9=89=E5=A2=9E=E5=8A=A0SPI=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/spi/router/ClientClassifierFactory.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java index 7199adfc..a4a54a50 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/router/ClientClassifierFactory.java @@ -22,7 +22,6 @@ import com.mpush.api.router.ClientClassifier; import com.mpush.api.spi.Factory; import com.mpush.api.spi.SpiLoader; -import com.mpush.api.spi.net.DnsMappingManager; /** * Created by ohun on 16/10/26. From a32f0f0aa6663eca333edbab197138784e4d161e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 28 Oct 2016 18:06:37 +0800 Subject: [PATCH 722/890] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=AE=9A=E4=B9=89=E5=A2=9E=E5=8A=A0SPI=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/common/router/ClientType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-common/src/main/java/com/mpush/common/router/ClientType.java b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java index 9520d076..a6c9087d 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ClientType.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java @@ -41,7 +41,7 @@ public enum ClientType { } public boolean contains(String osName) { - return Arrays.stream(os).anyMatch(s -> s.equalsIgnoreCase(osName)); + return Arrays.stream(os).anyMatch(osName::contains); } public static boolean isSameClient(String osName1, String osName2) { From 892b04189ca023e2dbcc500fbadda36ea77897dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 1 Nov 2016 13:31:42 +0800 Subject: [PATCH 723/890] =?UTF-8?q?logback.xml=E7=A7=BB=E5=88=B0=E5=88=B0c?= =?UTF-8?q?onf=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/env-mp.sh | 1 + bin/mp.cmd | 18 ++++++++++++------ bin/mp.sh | 12 ++++-------- conf/conf-dev.properties | 4 +++- conf/conf-pub.properties | 4 +++- conf/reference.conf | 8 +++++--- mpush-boot/assembly.xml | 1 + mpush-boot/src/main/resources/logback.xml | 3 +-- mpush-boot/src/main/resources/mpush.conf | 6 +++--- mpush-test/src/test/resources/application.conf | 5 +++-- .../main/java/com/mpush/tools/config/CC.java | 5 +++-- .../main/java/com/mpush/tools/log/Logs.java | 3 +++ .../mpush/zk/listener/ZKDnsNodeWatcher.java | 4 ++-- 13 files changed, 44 insertions(+), 30 deletions(-) diff --git a/bin/env-mp.sh b/bin/env-mp.sh index 66fc23ee..5f5057bd 100644 --- a/bin/env-mp.sh +++ b/bin/env-mp.sh @@ -25,6 +25,7 @@ MP_BIN_DIR="${MP_BIN_DIR:-/usr/bin}" MPUSH_PREFIX="${MP_BIN_DIR}/.." +MPUSH_HOME=$MPUSH_PREFIX if [ "x$MP_CFG_DIR" = "x" ] then diff --git a/bin/mp.cmd b/bin/mp.cmd index f4e18c90..9df778b4 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -13,14 +13,20 @@ REM distributed under the License is distributed on an "AS IS" BASIS, REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. REM See the License for the specific language governing permissions and REM limitations under the License. -REM java -Dmp.conf=../conf/mpush.conf -Dmp.log.dir=../logs -jar bootstrap.jar +REM java -Dmp.conf=../conf/mpush.conf -Dmp.home=. -jar bootstrap.jar +REM setlocal -setlocal +REM call "%~dp0env-mp.cmd" + +REM set MPMAIN=-jar %~dp0bootstrap.jar + +REM call %JAVA% "-Dmp.conf=%MPCFG%" "-Dmp.home=%~dp0%.." -cp "%CLASSPATH%" %MPMAIN% %* + +REM endlocal + + +java -Dmp.conf=../conf/mpush.conf -Dmp.home=. -jar bootstrap.jar -call "%~dp0env-mp.cmd" -set MPMAIN=-jar %~dp0bootstrap.jar -call %JAVA% "-Dmp.conf=%MPCFG%" "-Dmp.log.dir=%MP_LOG_DIR%" -cp "%CLASSPATH%" %MPMAIN% %* -endlocal diff --git a/bin/mp.sh b/bin/mp.sh index 44c07f88..b9eaa14f 100644 --- a/bin/mp.sh +++ b/bin/mp.sh @@ -125,7 +125,6 @@ else fi if [ ! -w "$MP_LOG_DIR" ] ; then -echo $MP_LOG_DIR mkdir -p "$MP_LOG_DIR" fi @@ -140,8 +139,7 @@ start) exit 0 fi fi - nohup "$JAVA" "-Dmp.conf=$MP_CFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & + nohup "$JAVA" "-Dmp.home=$MPUSH_HOME" "-Dmp.conf=$MP_CFG" -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN > "$_MP_DAEMON_OUT" 2>&1 < /dev/null & if [ $? -eq 0 ] then case "$OSTYPE" in @@ -166,12 +164,11 @@ start) fi ;; start-foreground) - "$JAVA" "-Dmp.conf=$MP_CFG" "-Dmp.log.dir=${MP_LOG_DIR}" "-Dmp.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN + "$JAVA" "-Dmp.home=$MPUSH_HOME" "-Dmp.conf=$MP_CFG" -cp "$CLASSPATH" $JVM_FLAGS $MP_MAIN ;; print-cmd) echo "\"$JAVA\" $MP_MAIN " - echo "\"-Dmp.conf=$MP_CFG\" -Dmp.log.dir=\"${MP_LOG_DIR}\" -Dmp.root.logger=\"${MP_LOG4J_PROP}\" " + echo "\"-Dmp.home=$MPUSH_HOME -Dmp.conf=$MP_CFG\" " echo "$JVM_FLAGS " echo "-cp \"$CLASSPATH\" " echo "> \"$_MP_DAEMON_OUT\" 2>&1 < /dev/null" @@ -220,8 +217,7 @@ stop) upgrade) shift echo "upgrading the servers to 3.*" - "$JAVA" "-Dmpush.log.dir=${MP_LOG_DIR}" "-Dmpush.root.logger=${MP_LOG4J_PROP}" \ - -cp "$CLASSPATH" $JVM_FLAGS com.mpush.tools.upgrade.UpgradeMain ${@} + "$JAVA" -cp "$CLASSPATH" $JVM_FLAGS com.mpush.tools.upgrade.UpgradeMain ${@} echo "Upgrading ... " ;; restart) diff --git a/conf/conf-dev.properties b/conf/conf-dev.properties index 5197fe31..af07a4a1 100644 --- a/conf/conf-dev.properties +++ b/conf/conf-dev.properties @@ -1,2 +1,4 @@ log.level=debug -min.hb=10s \ No newline at end of file +min.hb=10s +rsa.privateKey=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +rsa.publicKey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB diff --git a/conf/conf-pub.properties b/conf/conf-pub.properties index 6b55c1f1..f957e52e 100644 --- a/conf/conf-pub.properties +++ b/conf/conf-pub.properties @@ -1,2 +1,4 @@ log.level=warn -min.hb=3m \ No newline at end of file +min.hb=3m +rsa.privateKey=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= +rsa.publicKey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB diff --git a/conf/reference.conf b/conf/reference.conf index 68bb86ff..e04e45e5 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -12,8 +12,10 @@ mp { #日志配置 - log.level=warn - log.dir=${user.dir}/../logs + home=${user.dir} + log-level=warn + log-dir=${mp.home}/logs + log-conf-path=${mp.home}/conf/logback.xml #核心配置 core { @@ -183,7 +185,7 @@ mp { #系统监控配置 monitor { - dump-dir=/tmp/logs/mpush/ + dump-dir=${mp.home}/tmp dump-stack=false //是否定时dump堆栈 dump-period=1m //多久监控一次 print-log=true //是否打印监控日志 diff --git a/mpush-boot/assembly.xml b/mpush-boot/assembly.xml index 24fc2cd4..68307b16 100644 --- a/mpush-boot/assembly.xml +++ b/mpush-boot/assembly.xml @@ -35,6 +35,7 @@ conf mpush.conf + logback.xml diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml index 807cee5e..d1317efc 100644 --- a/mpush-boot/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -189,7 +189,7 @@ - + @@ -225,7 +225,6 @@ - diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 2e304bb7..21e42949 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,7 +1,7 @@ -mp.log.level=${log.level} +mp.log-level=${log.level} mp.min-heartbeat=${min.hb} -mp.security.private-key="MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA=" -mp.security.public-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB" +mp.security.private-key="${rsa.privateKey}" +mp.security.public-key="${rsa.publicKey}" mp.zk.server-address="127.0.0.1:2181" mp.redis { //redis 集群配置 nodes:["127.0.0.1:6379"] //格式是ip:port diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/test/resources/application.conf index d238ea79..b6d47bf9 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/test/resources/application.conf @@ -1,5 +1,6 @@ -mp.log.dir=${user.dir}/mpush-test/target/logs -mp.log.level=debug +mp.home=${user.dir}/target +mp.log-level=debug +mp.log-conf-path=logback.xml mp.core.min-heartbeat=10s mp.core.max-heartbeat=10s mp.core.compress-threshold=10k diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 053c38b7..34635f33 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -55,8 +55,9 @@ static Config load() { interface mp { Config cfg = CC.cfg.getObject("mp").toConfig(); - String log_dir = cfg.getString("log.dir"); - String log_level = cfg.getString("log.level"); + String log_dir = cfg.getString("log-dir"); + String log_level = cfg.getString("log-level"); + String log_conf_path = cfg.getString("log-conf-path"); interface core { Config cfg = mp.cfg.getObject("core").toConfig(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index 352c4fe4..b6d1784c 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -24,6 +24,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static ch.qos.logback.classic.util.ContextInitializer.CONFIG_FILE_PROPERTY; + /** * Created by ohun on 2016/5/16. * @@ -36,6 +38,7 @@ static boolean init() { if (logInit) return true; System.setProperty("log.home", CC.mp.log_dir); System.setProperty("log.root.level", CC.mp.log_level); + System.setProperty(CONFIG_FILE_PROPERTY, CC.mp.log_conf_path); LoggerFactory .getLogger("console") .info(CC.mp.cfg.root().render(ConfigRenderOptions.concise().setFormatted(true))); diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java index 2feac3af..d8304750 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java @@ -47,13 +47,13 @@ public class ZKDnsNodeWatcher extends ZKNodeCacheWatcher { @Override protected void beforeWatch() { - Logs.Console.error("start init zk dns data"); + Logs.Console.info("start init zk dns data"); List rawData = ZKClient.I.getChildrenKeys(ZKPath.DNS_MAPPING.getRootPath()); for (String raw : rawData) { String fullPath = ZKPath.DNS_MAPPING.getFullPath(raw); cache.put(fullPath, getZKNode(fullPath)); } - Logs.Console.error("end init zk dns data"); + Logs.Console.info("end init zk dns data"); } private ZKDnsNode getZKNode(String fullPath) { From d34eeaf401abd1499d179681754135dff184f51c Mon Sep 17 00:00:00 2001 From: ohun Date: Tue, 1 Nov 2016 21:58:55 +0800 Subject: [PATCH 724/890] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de02009f..e6bbb603 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -## [详细教程](https://mpusher.github.io/docs) +## [详细教程](http://mpush.mydoc.io) * 官网:[https://mpusher.github.io](https://mpusher.github.io) -* 文档:[https://mpusher.github.io/docs](https://mpusher.github.io/docs) +* 文档:[http://mpush.mydoc.io](http://mpush.mydoc.io) * QQ群:__114583699__ MPUSH开源消息推送系统 ## 源码 From 36e9166862af8185af4a7557df775bae92b1c49d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 2 Nov 2016 22:15:26 +0800 Subject: [PATCH 725/890] =?UTF-8?q?logback.xml=E7=A7=BB=E5=88=B0=E5=88=B0c?= =?UTF-8?q?onf=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-boot/src/main/resources/logback.xml | 12 +++--- mpush-test/src/test/resources/logback.xml | 47 ++++++++++------------- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/mpush-boot/src/main/resources/logback.xml b/mpush-boot/src/main/resources/logback.xml index d1317efc..e320edaa 100644 --- a/mpush-boot/src/main/resources/logback.xml +++ b/mpush-boot/src/main/resources/logback.xml @@ -3,10 +3,7 @@ - - - @@ -174,13 +171,12 @@ System.out - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} - %msg%n - + @@ -189,6 +185,7 @@ + @@ -225,8 +222,9 @@ + - + diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index 2bbd9024..dc21b97d 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -1,5 +1,5 @@ - + @@ -7,8 +7,7 @@ WARN - true - + %d{HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -21,8 +20,8 @@ DENY System.out - true - + + %d{HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -35,8 +34,8 @@ DENY System.out - true - + + %d{HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n @@ -44,11 +43,11 @@ System.out - UTF-8 + DEBUG - + %d{HH:mm:ss.SSS} -[monitor]- %msg%n @@ -56,11 +55,11 @@ System.out - UTF-8 + DEBUG - + %d{HH:mm:ss.SSS} -[connection]- %msg%n @@ -68,11 +67,11 @@ System.out - UTF-8 + DEBUG - + %d{HH:mm:ss.SSS} -[push]- %msg%n @@ -80,11 +79,10 @@ System.out - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} -[heartbeat]- %msg%n @@ -92,11 +90,10 @@ System.out - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} -[redis]- %msg%n @@ -104,11 +101,10 @@ System.out - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} -[http]- %msg%n @@ -116,11 +112,10 @@ System.out - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} -[zk]- %msg%n @@ -128,24 +123,22 @@ System.err - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} -[profile]- %msg%n System.err - UTF-8 DEBUG - + %d{HH:mm:ss.SSS} -[console]- %msg%n - + @@ -194,7 +187,7 @@ - + From 56efc0a5fa2ce6e58e502c02a1d38fcf75192c2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 2 Nov 2016 22:15:57 +0800 Subject: [PATCH 726/890] =?UTF-8?q?push=E4=B8=AD=E5=BF=83=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B5=81=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/push/BroadcastPushTask.java | 44 +++++++++---------- .../com/mpush/core/push/FastFlowControl.java | 2 +- .../mpush/core/push/GlobalFlowControl.java | 2 +- .../mpush/core/push/OverFlowException.java | 11 +++++ .../com/mpush/core/push/RedisFlowControl.java | 4 +- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java index 888d0896..7bc99775 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java @@ -57,8 +57,6 @@ public final class BroadcastPushTask implements PushTask, ChannelFutureListener private final Iterator>> iterator; - private Packet cachedPacket;//内存优化,因为广播所有人的消息都一样 - public BroadcastPushTask(GatewayPushMessage message, FlowControl flowControl) { this.message = message; this.flowControl = flowControl; @@ -68,9 +66,21 @@ public BroadcastPushTask(GatewayPushMessage message, FlowControl flowControl) { @Override public void run() { - boolean cancelled = false; + flowControl.reset(); + boolean done = broadcast(); + if (done) {//done + if (finishTasks.addAndGet(flowControl.total()) == 0) { + report(); + } + } else {//没有结束,就延时进行下次任务 TODO 考虑优先级问题 + PushCenter.I.delayTask(flowControl.getRemaining(), this); + } + flowControl.incTotal(); + } + + + private boolean broadcast() { try { - flowControl.reset(); iterator.forEachRemaining(entry -> { String userId = entry.getKey(); @@ -81,16 +91,10 @@ public void run() { if (checkCondition(condition, connection)) {//1.条件检测 if (connection.isConnected()) { if (connection.getChannel().isWritable()) { //检测TCP缓冲区是否已满且写队列超过最高阀值 - if (flowControl.checkQps()) { - if (cachedPacket == null) {//内存优化,因为广播所有人的消息都一样 - PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.send(this); - cachedPacket = pushMessage.getPacket(); - } else {//内存优化,因为广播所有人的消息都一样 - connection.send(cachedPacket, this); - } - } else if (iterator.hasNext()) { - PushCenter.I.delayTask(flowControl.getRemaining(), this); + PushMessage pushMessage = new PushMessage(message.content, connection); + pushMessage.send(this); + if (!flowControl.checkQps()) { + throw new OverFlowException(false); } } } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 @@ -104,16 +108,10 @@ public void run() { }); } catch (OverFlowException e) { - cancelled = true; - } - - - if (cancelled || !iterator.hasNext()) {//done - if (finishTasks.addAndGet(flowControl.total()) == 0) { - report(); - } + //超出最大限制,或者遍历完毕,结束广播 + return e.isOverMaxLimit() || !iterator.hasNext(); } - flowControl.incTotal(); + return !iterator.hasNext();//遍历完毕, 广播结束 } private void report() { diff --git a/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java index d5402544..bc89bf84 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java @@ -62,7 +62,7 @@ public boolean checkQps() { return true; } - if (total > maxLimit) throw new OverFlowException(); + if (total > maxLimit) throw new OverFlowException(true); if (System.currentTimeMillis() - start > duration) { reset(); diff --git a/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java index 369e59f0..efaee48e 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java @@ -58,7 +58,7 @@ public boolean checkQps() { return true; } - if (total.get() > maxLimit) throw new OverFlowException(); + if (total.get() > maxLimit) throw new OverFlowException(true); if (System.currentTimeMillis() - start > duration) { reset(); diff --git a/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java b/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java index 387e04fa..e23c3b32 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java +++ b/mpush-core/src/main/java/com/mpush/core/push/OverFlowException.java @@ -26,11 +26,22 @@ */ public final class OverFlowException extends RuntimeException { + private boolean overMaxLimit = false; + public OverFlowException() { super(null, null, false, false); } + public OverFlowException(boolean overMaxLimit) { + super(null, null, false, false); + this.overMaxLimit = overMaxLimit; + } + public OverFlowException(String message) { super(message, null, false, false); } + + public boolean isOverMaxLimit() { + return overMaxLimit; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java index 2d17c161..06822dbe 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java @@ -63,7 +63,7 @@ public boolean checkQps() throws OverFlowException { } if (total() > maxLimit) { - throw new OverFlowException(); + throw new OverFlowException(true); } if (System.currentTimeMillis() - start > duration) { @@ -73,7 +73,7 @@ public boolean checkQps() throws OverFlowException { } if (controller.isCancelled()) { - throw new OverFlowException(); + throw new OverFlowException(true); } else { limit = controller.qps(); } From e3f38e9e2f1e0821d7f55013bd74d14286983738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 8 Nov 2016 15:53:09 +0800 Subject: [PATCH 727/890] =?UTF-8?q?log=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/cache/redis/manager/RedisManager.java | 2 ++ .../mpush/cache/redis/manager/ZKRedisClusterManager.java | 2 -- .../src/main/java/com/mpush/tools/crypto/RSAUtils.java | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index cc79c2e6..68b86235 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -41,6 +41,7 @@ public final class RedisManager { private RedisConnectionFactory factory = new RedisConnectionFactory(); public void init() { + Logs.Console.info("begin init redis"); RedisClusterManager clusterManager = new ZKRedisClusterManager(); clusterManager.init(); factory.setPassword(CC.mp.redis.password); @@ -49,6 +50,7 @@ public void init() { factory.setCluster(CC.mp.redis.isCluster()); factory.init(); test(); + Logs.Console.info("init redis success..."); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index b0ebba9a..a6ece011 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -47,7 +47,6 @@ public ZKRedisClusterManager() { */ @Override public void init() { - Logs.Console.info("begin init redis cluster"); if (!ZKClient.I.isRunning()) throw new RedisException("init redis cluster ex, ZK client not running."); if (CollectionUtils.isNotEmpty(CC.mp.redis.nodes)) { @@ -62,7 +61,6 @@ public void init() { } if (nodes.isEmpty()) throw new RedisException("init redis sever fail groupList is null"); - Logs.Console.info("init redis cluster success..."); } @Override diff --git a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java index 24852f13..2a05f465 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/crypto/RSAUtils.java @@ -269,13 +269,13 @@ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) * @throws IllegalBlockSizeException */ private static byte[] doFinal(Cipher cipher, byte[] data, int key_len) throws BadPaddingException, IllegalBlockSizeException { - int inputLen = data.length, offSet = 0; + int inputLen = data.length, offset = 0; byte[] tmp; ByteArrayOutputStream out = new ByteArrayOutputStream(getTmpArrayLength(inputLen)); while (inputLen > 0) { - tmp = cipher.doFinal(data, offSet, Math.min(key_len, inputLen)); + tmp = cipher.doFinal(data, offset, Math.min(key_len, inputLen)); out.write(tmp, 0, tmp.length); - offSet += key_len; + offset += key_len; inputLen -= key_len; } return out.toByteArray(); From 46552f238a6c5ab3c48029333518f7fa71089a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 24 Nov 2016 11:49:05 +0800 Subject: [PATCH 728/890] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 34 ++++++---- .../main/java/com/mpush/api/Constants.java | 6 +- .../main/java/com/mpush/bootstrap/Main.java | 9 +++ .../cache/redis/manager/RedisManager.java | 2 +- .../connect/ConnClientChannelHandler.java | 2 +- .../net/HttpProxyDnsMappingManager.java | 17 ++--- .../mpush/core/handler/HttpProxyHandler.java | 1 + .../java/com/mpush/core/push/PushCenter.java | 14 ++-- .../com/mpush/core/server/AdminServer.java | 5 ++ .../mpush/core/server/ConnectionServer.java | 11 +--- .../core/server/ServerConnectionManager.java | 11 ++-- .../mpush/netty/client/NettyTCPClient.java | 31 ++++++--- .../mpush/netty/http/HttpClientHandler.java | 20 +++--- .../mpush/netty/http/HttpConnectionPool.java | 8 +-- .../com/mpush/netty/http/NettyHttpClient.java | 10 ++- .../com/mpush/netty/http/RequestContext.java | 4 +- .../mpush/netty/server/NettyTCPServer.java | 66 +++++++++++++++---- .../mpush/netty/udp/NettyUDPConnector.java | 18 ++--- .../mpush/test/client/ConnClientTestMain.java | 2 +- .../src/main/java/com/mpush/tools/Jsons.java | 2 + .../main/java/com/mpush/tools/config/CC.java | 8 ++- .../tools/thread/NamedThreadFactory.java | 27 +++++--- .../com/mpush/tools/thread/ThreadNames.java | 22 ++----- pom.xml | 1 - 24 files changed, 209 insertions(+), 122 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index e04e45e5..e37930c7 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -139,16 +139,16 @@ mp { #线程池配置 thread { pool { - boss { //netty boss - min:2 - max:8 - queue-size:1000 + boss { //netty server boss + min:1 //boss 只需要一个线程即可 + max:1 + queue-size:0 } - work { //netty boss - min:4 - max:32 - queue-size:1000 + work { //netty server boss + min:0 //0表示线程数根据cpu核数动态调整(2*cpu) + max:0 + queue-size:0 } event-bus { @@ -158,15 +158,15 @@ mp { } http-proxy { - min:8 - max:64 - queue-size:1000 + min:0 //0表示线程数根据cpu核数动态调整(2*cpu) + max:0 + queue-size:0 } biz { //其他业务 - min:4 - max:64 - queue-size:10 + min:1 + max:4 + queue-size:16 } mq { //用户上下线消息, 踢人等 @@ -180,6 +180,12 @@ mp { max:2 queue-size:0 } + + push-center { //消息推送 + min:4 + max:4 + queue-size:0 + } } } diff --git a/mpush-api/src/main/java/com/mpush/api/Constants.java b/mpush-api/src/main/java/com/mpush/api/Constants.java index 27e5cece..3c7ac58d 100644 --- a/mpush-api/src/main/java/com/mpush/api/Constants.java +++ b/mpush-api/src/main/java/com/mpush/api/Constants.java @@ -20,6 +20,7 @@ package com.mpush.api; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * Created by ohun on 2015/12/23. @@ -27,9 +28,8 @@ * @author ohun@live.cn */ public interface Constants { - Charset UTF_8 = Charset.forName("UTF-8"); + Charset UTF_8 = StandardCharsets.UTF_8; byte[] EMPTY_BYTES = new byte[0]; - String HTTP_HEAD_READ_TIMEOUT = "readTimeout"; - + String EMPTY_STRING = ""; } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java index 55048fe4..a3bbfe00 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/Main.java @@ -30,6 +30,15 @@ public static void main(String[] args) { addHook(launcher); } + /** + * 注意点 + * 1.不要ShutdownHook Thread 里调用System.exit()方法,否则会造成死循环。 + * 2.如果有非守护线程,只有所有的非守护线程都结束了才会执行hook + * 3.Thread默认都是非守护线程,创建的时候要注意 + * 4.注意线程抛出的异常,如果没有被捕获都会跑到Thread.dispatchUncaughtException + * + * @param launcher + */ private static void addHook(ServerLauncher launcher) { Runtime.getRuntime().addShutdownHook( new Thread(() -> { diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 68b86235..cda33aaa 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -41,7 +41,7 @@ public final class RedisManager { private RedisConnectionFactory factory = new RedisConnectionFactory(); public void init() { - Logs.Console.info("begin init redis"); + Logs.Console.info("begin init redis..."); RedisClusterManager clusterManager = new ZKRedisClusterManager(); clusterManager.init(); factory.setPassword(CC.mp.redis.password); diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index c2ce746f..3eb55e84 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -55,7 +55,7 @@ @ChannelHandler.Sharable public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); - private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedPoolThreadFactory(ThreadNames.T_NETTY_TIMER)); + private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedPoolThreadFactory(ThreadNames.T_CONN_TIMER)); private final Connection connection = new NettyConnection(); private final ClientConfig clientConfig; diff --git a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java index 8ae62750..1cdb09a8 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java +++ b/mpush-common/src/main/java/com/mpush/common/net/HttpProxyDnsMappingManager.java @@ -28,11 +28,14 @@ import com.mpush.api.spi.net.DnsMappingManager; import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; +import com.mpush.tools.thread.NamedPoolThreadFactory; +import com.mpush.tools.thread.ThreadNames; import com.mpush.zk.cache.ZKDnsNodeCache; import com.mpush.zk.listener.ZKDnsNodeWatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URL; import java.util.List; import java.util.Map; import java.util.concurrent.Executors; @@ -52,15 +55,13 @@ public class HttpProxyDnsMappingManager extends BaseService implements DnsMappin private ScheduledExecutorService executorService; - public HttpProxyDnsMappingManager() { - } - - @Override protected void doStart(Listener listener) throws Throwable { watcher.watch(); if (all.size() > 0) { - executorService = Executors.newSingleThreadScheduledExecutor(); + executorService = Executors.newSingleThreadScheduledExecutor( + new NamedPoolThreadFactory(ThreadNames.T_HTTP_DNS_TIMER) + ); executorService.scheduleAtFixedRate(this, 1, 20, TimeUnit.SECONDS); //20秒 定时扫描dns } listener.onSuccess(); @@ -113,15 +114,15 @@ public void run() { Map> all = this.getAll(); Map> available = Maps.newConcurrentMap(); all.forEach((key, dnsMappings) -> { - List nowValue = Lists.newArrayList(); + List okList = Lists.newArrayList(); dnsMappings.forEach(dnsMapping -> { if (checkHealth(dnsMapping.getIp(), dnsMapping.getPort())) { - nowValue.add(dnsMapping); + okList.add(dnsMapping); } else { logger.warn("dns can not reachable:" + Jsons.toJson(dnsMapping)); } }); - available.put(key, nowValue); + available.put(key, okList); }); this.update(available); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index 38adf07f..a7236daf 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -194,6 +194,7 @@ private String doDnsMapping(String url) { try { uri = new URL(url); } catch (MalformedURLException e) { + //ignore e } if (uri == null) { return url; diff --git a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java index 83ac841f..9fd69683 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java @@ -22,17 +22,17 @@ import com.mpush.api.push.PushException; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; +import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import static com.mpush.tools.config.CC.mp.thread.pool.push_center.min; import static com.mpush.tools.thread.ThreadNames.T_PUSH_CENTER_TIMER; -import static com.mpush.tools.thread.ThreadNames.T_PUSH_REQ_TIMER; /** * Created by ohun on 16/10/24. @@ -58,10 +58,12 @@ public void delayTask(int delay, PushTask task) { @Override protected void doStart(Listener listener) throws Throwable { - executor = new ScheduledThreadPoolExecutor(4, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), (r, e) -> { - logger.error("one push task was rejected, task=" + r); - throw new PushException("one push request was rejected. request=" + r); - }); + executor = new ScheduledThreadPoolExecutor(min, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), + (r, e) -> { + logger.error("one push task was rejected, task=" + r); + throw new PushException("one push request was rejected. request=" + r); + } + ); listener.onSuccess(); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index 9dc22f19..7addbf8c 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -73,4 +73,9 @@ protected ChannelHandler getDecoder() { protected ChannelHandler getEncoder() { return new StringEncoder(); } + + @Override + protected int getWorkThreadNum() { + return 1; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index eb3a7984..472a3e35 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -29,7 +29,6 @@ import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; -import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -37,7 +36,6 @@ import io.netty.channel.WriteBufferWaterMark; import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler; -import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -109,13 +107,8 @@ public void stop(Listener listener) { } @Override - protected Executor getWorkExecutor() { - return ThreadPoolManager.I.getWorkExecutor(); - } - - @Override - protected Executor getBossExecutor() { - return ThreadPoolManager.I.getBossExecutor(); + protected int getWorkThreadNum() { + return CC.mp.thread.pool.work.max; } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 5533f6da..95cbc4d0 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -21,17 +21,17 @@ import com.google.common.collect.Lists; -import com.google.common.eventbus.Subscribe; import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; import com.mpush.api.event.HandshakeEvent; import com.mpush.tools.config.CC; import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.ThreadNames; import io.netty.channel.Channel; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; -import io.netty.util.Timer; import io.netty.util.TimerTask; import java.util.List; @@ -65,7 +65,10 @@ public void init() { EventBus.I.register(this); long tickDuration = TimeUnit.SECONDS.toMillis(1);//1s 每秒钟走一步,一个心跳周期内大致走一圈 int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); - this.timer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel); + this.timer = new HashedWheelTimer( + new NamedThreadFactory(ThreadNames.T_CONN_TIMER), + tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel + ); } } @@ -84,7 +87,7 @@ public Connection get(final Channel channel) { @Override public void add(Connection connection) { connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); - //if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); + if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); } @Override diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java index 7ef37f4a..c3a4339a 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java @@ -26,6 +26,7 @@ import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; import com.mpush.tools.config.CC; +import com.mpush.tools.thread.ThreadNames; import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -33,10 +34,12 @@ import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollServerSocketChannel; import io.netty.channel.epoll.EpollSocketChannel; +import io.netty.channel.epoll.Native; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.concurrent.DefaultThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -86,13 +89,17 @@ public void initChannel(SocketChannel ch) throws Exception { } private void createNioClient(Listener listener) { - NioEventLoopGroup workerGroup = new NioEventLoopGroup(1, getWorkExecutor()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup( + 1, new DefaultThreadFactory(ThreadNames.T_TCP_CLIENT) + ); workerGroup.setIoRatio(getIoRate()); createClient(listener, workerGroup, NioSocketChannel.class); } private void createEpollClient(Listener listener) { - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(1, getWorkExecutor()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup( + 1, new DefaultThreadFactory(ThreadNames.T_TCP_CLIENT) + ); workerGroup.setIoRatio(getIoRate()); createClient(listener, workerGroup, EpollSocketChannel.class); } @@ -111,25 +118,33 @@ protected ChannelHandler getEncoder() { return PacketEncoder.INSTANCE; } - protected Executor getWorkExecutor() { - return ThreadPoolManager.I.getWorkExecutor(); - } - protected int getIoRate() { - return 70; + return 50; } public abstract ChannelHandler getChannelHandler(); @Override protected void doStart(Listener listener) throws Throwable { - if (CC.mp.core.useNettyEpoll()) { + if (useNettyEpoll()) { createEpollClient(listener); } else { createNioClient(listener); } } + private boolean useNettyEpoll() { + if (CC.mp.core.useNettyEpoll()) { + try { + Native.offsetofEpollData(); + return true; + } catch (UnsatisfiedLinkError error) { + LOGGER.warn("can not load netty epoll, switch nio model."); + } + } + return false; + } + @Override protected void doStop(Listener listener) throws Throwable { if (workerGroup != null) { diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java index 5f9efa50..5fde47f8 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java @@ -13,8 +13,9 @@ import java.net.URLDecoder; @ChannelHandler.Sharable -public class HttpClientHandler extends ChannelInboundHandlerAdapter { +/*package*/ class HttpClientHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttpClient.class); + private final NettyHttpClient client; public HttpClientHandler(NettyHttpClient client) { @@ -31,7 +32,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E } finally { client.pool.tryRelease(ctx.channel()); } - LOGGER.error("http client caught an error, info={}", context, cause); + LOGGER.error("http client caught an ex, info={}", context, cause); } @Override @@ -76,13 +77,13 @@ private boolean isRedirect(HttpResponse response) { } private String getRedirectLocation(HttpRequest request, HttpResponse response) throws Exception { - String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION).toString(), "UTF-8"); + String hdr = URLDecoder.decode(response.headers().get(HttpHeaderNames.LOCATION), "UTF-8"); if (hdr != null) { if (hdr.toLowerCase().startsWith("http://") || hdr.toLowerCase().startsWith("https://")) { return hdr; } else { URL orig = new URL(request.uri()); - String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath().toString(), "UTF-8"); + String pth = orig.getPath() == null ? "/" : URLDecoder.decode(orig.getPath(), "UTF-8"); if (hdr.startsWith("/")) { pth = hdr; } else if (pth.endsWith("/")) { @@ -90,7 +91,7 @@ private String getRedirectLocation(HttpRequest request, HttpResponse response) t } else { pth += "/" + hdr; } - StringBuilder sb = new StringBuilder(orig.getProtocol().toString()); + StringBuilder sb = new StringBuilder(orig.getProtocol()); sb.append("://").append(orig.getHost()); if (orig.getPort() > 0) { sb.append(":").append(orig.getPort()); @@ -105,15 +106,16 @@ private String getRedirectLocation(HttpRequest request, HttpResponse response) t return null; } + @SuppressWarnings("unused") private HttpRequest copy(String uri, HttpRequest request) { HttpRequest nue = request; if (request instanceof DefaultFullHttpRequest) { - DefaultFullHttpRequest dfrq = (DefaultFullHttpRequest) request; + DefaultFullHttpRequest dfr = (DefaultFullHttpRequest) request; FullHttpRequest rq; try { - rq = dfrq.copy(); - } catch (IllegalReferenceCountException e) { // Empty bytebuf - rq = dfrq; + rq = dfr.copy(); + } catch (IllegalReferenceCountException e) { // Empty byteBuf + rq = dfr; } rq.setUri(uri); } else { diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java index dcbf3dbd..e2d65e84 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpConnectionPool.java @@ -33,10 +33,10 @@ * * @author ohun@live.cn (夜色) */ -public class HttpConnectionPool { +/*package*/ class HttpConnectionPool { private static final int maxConnPerHost = CC.mp.http.max_conn_per_host; - private final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPool.class); + private static final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPool.class); private final AttributeKey hostKey = AttributeKey.newInstance("host"); @@ -53,8 +53,8 @@ public synchronized Channel tryAcquire(String host) { LOGGER.debug("tryAcquire channel success, host={}", host); channel.attr(hostKey).set(host); return channel; - } else {//链接由于意外情况不可用了,keepAlive_timeout - LOGGER.error("tryAcquire channel false channel is inactive, host={}", host); + } else {//链接由于意外情况不可用了, 比如: keepAlive_timeout + LOGGER.warn("tryAcquire channel false channel is inactive, host={}", host); } } return null; diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index 1b87eb3c..aebaf198 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -23,6 +23,7 @@ import com.mpush.api.service.Listener; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedThreadFactory; +import com.mpush.tools.thread.ThreadNames; import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -36,12 +37,14 @@ import io.netty.util.AttributeKey; import io.netty.util.HashedWheelTimer; import io.netty.util.Timer; +import io.netty.util.concurrent.DefaultThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; import java.util.concurrent.TimeUnit; +import static com.mpush.tools.thread.ThreadNames.T_HTTP_TIMER; import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; import static io.netty.handler.codec.http.HttpHeaderNames.HOST; import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; @@ -124,7 +127,10 @@ private void writeRequest(Channel channel, RequestContext context) { @Override protected void doStart(Listener listener) throws Throwable { - workerGroup = new NioEventLoopGroup(0, ThreadPoolManager.I.getHttpExecutor()); + workerGroup = new NioEventLoopGroup( + CC.mp.thread.pool.http_proxy.max, + new DefaultThreadFactory(ThreadNames.T_HTTP_CLIENT) + ); b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); @@ -142,7 +148,7 @@ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this)); } }); - timer = new HashedWheelTimer(new NamedThreadFactory("http-client-timer-"), 1, TimeUnit.SECONDS, 64); + timer = new HashedWheelTimer(new NamedThreadFactory(T_HTTP_TIMER), 1, TimeUnit.SECONDS, 64); listener.onSuccess(); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java index 08bcbbd3..da2eb45b 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/RequestContext.java @@ -31,10 +31,10 @@ public class RequestContext implements TimerTask, HttpCallback { private static final int TIMEOUT = CC.mp.http.default_read_timeout; + private final long startTime = System.currentTimeMillis(); final AtomicBoolean cancelled = new AtomicBoolean(false); - final long startTime = System.currentTimeMillis(); final int readTimeout; - long endTime = startTime; + private long endTime = startTime; private String uri; private HttpCallback callback; FullHttpRequest request; diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index 82b5df47..cd570bb3 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -27,19 +27,20 @@ import com.mpush.netty.codec.PacketEncoder; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.ThreadNames; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollServerSocketChannel; +import io.netty.channel.epoll.Native; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.util.concurrent.DefaultThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Locale; -import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; /** @@ -65,7 +66,7 @@ public NettyTCPServer(int port) { public void init() { if (!serverState.compareAndSet(State.Created, State.Initialized)) { - throw new IllegalStateException("Server already init"); + throw new ServiceException("Server already init"); } } @@ -93,9 +94,9 @@ public void stop(Listener listener) { @Override public void start(final Listener listener) { if (!serverState.compareAndSet(State.Initialized, State.Starting)) { - throw new IllegalStateException("Server already started or have not init"); + throw new ServiceException("Server already started or have not init"); } - if (CC.mp.core.useNettyEpoll()) { + if (useNettyEpoll()) { createEpollServer(listener); } else { createNioServer(listener); @@ -183,15 +184,24 @@ public void initChannel(SocketChannel ch) throws Exception {//每连上一个链 } private void createNioServer(Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, getBossExecutor()); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, getWorkExecutor()); + NioEventLoopGroup bossGroup = new NioEventLoopGroup( + getBossThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_BOSS) + ); + NioEventLoopGroup workerGroup = new NioEventLoopGroup( + getWorkThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) + ); + bossGroup.setIoRatio(100); workerGroup.setIoRatio(getIoRate()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } private void createEpollServer(Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, getBossExecutor()); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, getWorkExecutor()); + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup( + getBossThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_BOSS) + ); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup( + getWorkThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) + ); workerGroup.setIoRatio(getIoRate()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -238,15 +248,45 @@ protected void initPipeline(ChannelPipeline pipeline) { pipeline.addLast("handler", getChannelHandler()); } - protected Executor getBossExecutor() { - return null; + /** + * netty 默认的Executor为ThreadPerTaskExecutor + * 线程池的使用在SingleThreadEventExecutor#doStartThread + *

+ * eventLoop.execute(runnable); + * 是比较重要的一个方法。在没有启动真正线程时, + * 它会启动线程并将待执行任务放入执行队列里面。 + * 启动真正线程(startThread())会判断是否该线程已经启动, + * 如果已经启动则会直接跳过,达到线程复用的目的 + * + * @return + */ + protected int getBossThreadNum() { + return 1; } - protected Executor getWorkExecutor() { - return null; + /** + * netty 默认的Executor为ThreadPerTaskExecutor + * 线程池的使用在SingleThreadEventExecutor#doStartThread + * + * @return + */ + protected int getWorkThreadNum() { + return 0; } protected int getIoRate() { return 70; } + + protected boolean useNettyEpoll() { + if (CC.mp.core.useNettyEpoll()) { + try { + Native.offsetofEpollData(); + return true; + } catch (UnsatisfiedLinkError error) { + logger.warn("can not load netty epoll, switch nio model."); + } + } + return false; + } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java index 8c5fe516..1885dc69 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java @@ -23,8 +23,8 @@ import com.mpush.api.service.Listener; import com.mpush.api.service.Server; import com.mpush.api.service.ServiceException; -import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.ThreadNames; import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -34,10 +34,10 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.nio.NioDatagramChannel; +import io.netty.util.concurrent.DefaultThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Locale; import java.util.concurrent.Executor; import static io.netty.channel.socket.InternetProtocolFamily.IPv4; @@ -101,13 +101,17 @@ private void createServer(Listener listener, EventLoopGroup eventLoopGroup, Chan } private void createNioServer(Listener listener) { - NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, getWorkExecutor()); - createServer(listener, eventLoopGroup, () -> new NioDatagramChannel(IPv4)); + NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup( + 0, new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) + ); + createServer(listener, eventLoopGroup, () -> new NioDatagramChannel(IPv4));//默认是根据机器情况创建Channel,如果机器支持ipv6,则无法使用ipv4的地址加入组播 } @SuppressWarnings("unused") private void createEpollServer(Listener listener) { - EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(0, getWorkExecutor()); + EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup( + 0, new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) + ); createServer(listener, eventLoopGroup, EpollDatagramChannel::new); } @@ -117,8 +121,4 @@ protected void initOptions(Bootstrap b) { public abstract ChannelHandler getChannelHandler(); - protected Executor getWorkExecutor() { - return ThreadPoolManager.I.getWorkExecutor(); - } - } diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index 5f1701a1..8bb3e53a 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -66,7 +66,7 @@ public void testConnClient() throws Exception { config.setOsVersion(osVersion); config.setUserId(userId); Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); - client.start().get(10, TimeUnit.SECONDS); + client.start(); } LockSupport.park(); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java index b5963e34..1c587421 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Jsons.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Jsons.java @@ -34,6 +34,8 @@ /** * Created by xiaoxu.yxx on 15/8/7. + * + * @author ohun@live.cn (夜色) */ public final class Jsons { private static final Logger LOGGER = LoggerFactory.getLogger(Jsons.class); diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 34635f33..3398f5ca 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -218,8 +218,14 @@ interface push_callback { int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); } - } + interface push_center { + Config cfg = pool.cfg.getObject("push-center").toConfig(); + int min = cfg.getInt("min"); + int max = cfg.getInt("max"); + int queue_size = cfg.getInt("queue-size"); + } + } } interface zk { diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 27cfcb9d..0c19918a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -30,9 +30,9 @@ * @author ohun@live.cn (夜色) */ public final class NamedThreadFactory implements ThreadFactory { - protected final AtomicInteger threadNumber = new AtomicInteger(1); - protected final String namePrefix; - protected final ThreadGroup group; + private final AtomicInteger threadNumber = new AtomicInteger(1); + private final String namePrefix; + private final ThreadGroup group; public NamedThreadFactory() { @@ -44,16 +44,27 @@ public NamedThreadFactory(final String namePrefix) { this.group = Thread.currentThread().getThreadGroup(); } + /** + * Daemon的作用是为其他线程的运行提供服务,比如说GC线程。其实User Thread线程和Daemon Thread守护线程本质上来说去没啥区别的, + * 唯一的区别之处就在虚拟机的离开:如果User Thread全部撤离,那么Daemon Thread也就没啥线程好服务的了,所以虚拟机也就退出了。 + * 守护线程并非虚拟机内部可以提供,用户也可以自行的设定守护线程,方法:public final void setDaemon(boolean on) ; + *

+ * 但是有几点需要注意: + * 1)、thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。 + *

+ * 2)、 在Daemon线程中产生的新线程也是Daemon的。 + *

+ * 3)、不是所有的应用都可以分配给Daemon线程来进行服务,比如读写操作或者计算逻辑。因为在Daemon Thread还没来的及进行操作时,虚拟机可能已经退出了。 + */ public Thread newThread(String name, Runnable r) { - return new Thread(group, r, namePrefix + "-" + threadNumber.getAndIncrement() + "-" + name); + Thread thread = new Thread(group, r, namePrefix + "-" + threadNumber.getAndIncrement() + "-" + name); + thread.setDaemon(true); + return thread; } @Override public Thread newThread(Runnable r) { - Thread t = newThread(namePrefix + "-" + threadNumber.getAndIncrement(), r); - if (t.isDaemon()) - t.setDaemon(false); - return t; + return newThread("none", r); } public static NamedThreadFactory build() { diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index 4eb5b003..251960f4 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -22,36 +22,22 @@ public final class ThreadNames { public static final String NS = "mp"; public static final String THREAD_NAME_PREFIX = NS + "-t"; - - /** - * netty boss 线程 - */ public static final String T_SERVER_BOSS = NS + "-boss"; - - /** - * netty worker 线程 - */ public static final String T_SERVER_WORKER = NS + "-worker"; public static final String T_TRAFFIC_SHAPING = NS + "-traffic-shaping"; - + public static final String T_TCP_CLIENT = NS + "-tcp-client"; public static final String T_HTTP_CLIENT = NS + "-http"; - public static final String T_EVENT_BUS = NS + "-event"; - public static final String T_MQ = NS + "-mq"; - public static final String T_ZK = NS + "-zk"; - public static final String T_BIZ = NS + "-biz"; public static final String T_PUSH_CALLBACK = NS + "-push-callback"; public static final String T_PUSH_REQ_TIMER = NS + "-push-req-timer"; public static final String T_ARK_REQ_TIMER = NS + "-ack-timer"; public static final String T_PUSH_CENTER_TIMER = NS + "-push-center-timer"; public static final String T_MONITOR = NS + "-monitor"; - - /** - * connection 定期检测线程 - */ - public static final String T_NETTY_TIMER = NS + "-timer"; + public static final String T_CONN_TIMER = NS + "-conn-check-timer"; + public static final String T_HTTP_TIMER = NS + "-http-client-timer"; + public static final String T_HTTP_DNS_TIMER = NS + "-http-dns-timer"; } diff --git a/pom.xml b/pom.xml index 0937e38b..a8768b30 100644 --- a/pom.xml +++ b/pom.xml @@ -277,7 +277,6 @@ - maven-compiler-plugin From 86462abb75c9ebf77fbdd997c4e793d8f69a1bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 24 Nov 2016 12:00:28 +0800 Subject: [PATCH 729/890] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=88=B00.0.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 2 +- mpush-boot/pom.xml | 2 +- mpush-cache/pom.xml | 2 +- mpush-client/pom.xml | 2 +- mpush-common/pom.xml | 2 +- mpush-core/pom.xml | 2 +- mpush-monitor/pom.xml | 2 +- mpush-netty/pom.xml | 2 +- mpush-test/pom.xml | 2 +- mpush-tools/pom.xml | 2 +- mpush-zk/pom.xml | 2 +- pom.xml | 4 ++-- 12 files changed, 13 insertions(+), 13 deletions(-) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 29c3251f..30485c1f 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-api diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 3b55635b..63ef2052 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-boot diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml index 79618137..ad1fd434 100644 --- a/mpush-cache/pom.xml +++ b/mpush-cache/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-cache diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 900d68e7..28550641 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-client diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index a28df536..aa025ae1 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -5,7 +5,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 4.0.0 diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index e507f9dd..59a78303 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-core diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index ae7c78c2..12f8991d 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -3,7 +3,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 4.0.0 diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 5e172411..76079c18 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-netty diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 6ac785e9..065d3656 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-test diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index ea8d1190..743c73cf 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-tools diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml index 2d65d728..fab165e4 100644 --- a/mpush-zk/pom.xml +++ b/mpush-zk/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.5 + 0.0.6 mpush-zk diff --git a/pom.xml b/pom.xml index a8768b30..e6b8fb9d 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.github.mpusher mpush pom - 0.0.5 + 0.0.6 mpush MPUSH消息推送系统 https://github.com/mpusher/mpush @@ -61,7 +61,7 @@ UTF-8 1.8 com.github.mpusher - 0.0.5 + 0.0.6 ${mpush.version} ${mpush.version} ${mpush.version} From 99ed560b002c67012b3543b348f208b386083657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 24 Nov 2016 14:54:21 +0800 Subject: [PATCH 730/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=81=E6=8E=A7?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 16 +++++++++++++ .../core/handler/GatewayPushHandler.java | 14 +++++++---- .../mpush/core/push/GlobalFlowControl.java | 2 +- .../com/mpush/core/push/RedisFlowControl.java | 5 ++-- .../test/configcenter/ConfigCenterTest.java | 2 +- .../main/java/com/mpush/tools/config/CC.java | 24 +++++++++++++++++++ 6 files changed, 54 insertions(+), 9 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index e37930c7..59a022f8 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -189,6 +189,22 @@ mp { } } + push { + flow-control { //qps = limit/(duration) + global:{ + limit:5000 //qps = 5000 + max:0 //UN limit + duration:1s //1s + } + + broadcast:{ + limit:3000 //qps = 3000 + max:100000 //10w + duration:1s //1s + } + } + } + #系统监控配置 monitor { dump-dir=${mp.home}/tmp diff --git a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java index 1f86b17f..350f9bb9 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/GatewayPushHandler.java @@ -24,6 +24,9 @@ import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.gateway.GatewayPushMessage; import com.mpush.core.push.*; +import com.mpush.tools.config.CC; + +import static com.mpush.tools.config.CC.mp.push.flow_control.*; /** * Created by ohun on 2015/12/30. @@ -34,7 +37,9 @@ public final class GatewayPushHandler extends BaseMessageHandler maxLimit) throw new OverFlowException(true); + if (maxLimit > 0 && total.get() > maxLimit) throw new OverFlowException(true); if (System.currentTimeMillis() - start > duration) { reset(); diff --git a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java index 06822dbe..875a0b24 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java @@ -31,16 +31,17 @@ public final class RedisFlowControl implements FlowControl { private final BroadcastController controller; - private final int maxLimit = 100000;//10w private final int duration = 1000; + private final int maxLimit; private int limit; private int count; private int total; private long start; - public RedisFlowControl(String taskId) { + public RedisFlowControl(String taskId, int maxLimit) { this.controller = new RedisBroadcastController(taskId); this.limit = controller.qps(); + this.maxLimit = maxLimit; } @Override diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java index a9ca5e50..ec896c16 100644 --- a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java +++ b/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java @@ -36,7 +36,7 @@ public void setUp() throws Exception { @Test public void testKey() { //String t = ConfigKey.app_env.getString(); - System.out.println(CC.mp.redis.nodes.size()); + System.out.println(CC.mp.push.flow_control.global.max); } @Test diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 3398f5ca..f2e8317c 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -302,6 +302,30 @@ static Map> loadMapping() { } } + interface push { + + Config cfg = mp.cfg.getObject("push").toConfig(); + + interface flow_control { + + Config cfg = push.cfg.getObject("flow-control").toConfig(); + + interface global { + Config cfg = flow_control.cfg.getObject("global").toConfig(); + int limit = cfg.getNumber("limit").intValue(); + int max = cfg.getInt("max"); + int duration = (int) cfg.getDuration("duration").toMillis(); + } + + interface broadcast { + Config cfg = flow_control.cfg.getObject("broadcast").toConfig(); + int limit = cfg.getInt("limit"); + int max = cfg.getInt("max"); + int duration = (int) cfg.getDuration("duration").toMillis(); + } + } + } + interface monitor { Config cfg = mp.cfg.getObject("monitor").toConfig(); String dump_dir = cfg.getString("dump-dir"); From f36acfce8bd58ef129b68032462597087ca82112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 24 Nov 2016 16:09:12 +0800 Subject: [PATCH 731/890] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/api/connection/Connection.java | 1 - .../java/com/mpush/tools/thread/NamedPoolThreadFactory.java | 2 +- .../main/java/com/mpush/tools/thread/pool/DefaultExecutor.java | 2 +- .../com/mpush/tools/thread/pool/DefaultExecutorFactory.java | 2 +- .../com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java | 2 +- .../main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java | 2 +- .../java/com/mpush/tools/thread/pool/ThreadPoolManager.java | 2 +- 7 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index cc9ff7dc..d6ef1598 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -33,7 +33,6 @@ public interface Connection { int STATUS_NEW = 0; int STATUS_CONNECTED = 1; int STATUS_DISCONNECTED = 2; - int STATUS_TIMEOUT = 3; void init(Channel channel, boolean security); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java index 1917e933..ef0c92a0 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedPoolThreadFactory.java @@ -22,7 +22,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -public class NamedPoolThreadFactory implements ThreadFactory { +public final class NamedPoolThreadFactory implements ThreadFactory { private static final AtomicInteger poolNum = new AtomicInteger(1); private final AtomicInteger threadNum = new AtomicInteger(1); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java index b7af1b06..33570c03 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutor.java @@ -25,7 +25,7 @@ * * @author ohun@live.cn (夜色) */ -public class DefaultExecutor extends ThreadPoolExecutor { +public final class DefaultExecutor extends ThreadPoolExecutor { public DefaultExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index 925f948b..c2f6396f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -35,7 +35,7 @@ * 此线程池可伸缩,线程空闲一定时间后回收,新请求重新创建线程 */ @Spi(order = 1) -public class DefaultExecutorFactory implements ExecutorFactory { +public final class DefaultExecutorFactory implements ExecutorFactory { private Executor get(ThreadPoolConfig config) { String name = config.getName(); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java index ee1741df..34e2e62d 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DumpThreadRejectedHandler.java @@ -31,7 +31,7 @@ import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_ABORT; import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS; -public class DumpThreadRejectedHandler implements RejectedExecutionHandler { +public final class DumpThreadRejectedHandler implements RejectedExecutionHandler { private final static Logger LOGGER = LoggerFactory.getLogger(DumpThreadRejectedHandler.class); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java index e49d3c83..1f2e1236 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolConfig.java @@ -23,7 +23,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.SynchronousQueue; -public class ThreadPoolConfig { +public final class ThreadPoolConfig { public static final int REJECTED_POLICY_ABORT = 0; public static final int REJECTED_POLICY_DISCARD = 1; public static final int REJECTED_POLICY_CALLER_RUNS = 2; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 754deb21..0bde5ae5 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -27,7 +27,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -public class ThreadPoolManager { +public final class ThreadPoolManager { public static final ThreadPoolManager I = new ThreadPoolManager(); private final ExecutorFactory executorFactory = ExecutorFactory.create(); From f8e6a44076fa3dfdab818d6d142b63ace20e3e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 25 Nov 2016 15:03:05 +0800 Subject: [PATCH 732/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=BF=83=E8=B7=B3=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/api/connection/SessionContext.java | 4 +++- .../java/com/mpush/core/server/ServerConnectionManager.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index 654d3a06..b965ffb7 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -22,6 +22,8 @@ import com.mpush.api.router.ClientClassifier; +import java.util.concurrent.TimeUnit; + /** * Created by ohun on 2015/12/22. * @@ -34,7 +36,7 @@ public final class SessionContext { public String deviceId; public String userId; public String tags; - public int heartbeat; + public int heartbeat = (int) TimeUnit.SECONDS.toMillis(10); public Cipher cipher; private int clientType; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 95cbc4d0..c71a426c 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -120,7 +120,7 @@ private class HeartbeatCheckTask implements TimerTask { void startTimeout() { int timeout = connection.getSessionContext().heartbeat; - timer.newTimeout(this, timeout > 0 ? timeout : CC.mp.core.min_heartbeat, TimeUnit.MILLISECONDS); + timer.newTimeout(this, timeout, TimeUnit.MILLISECONDS); } @Override From 6153018f4d39458e95af2b3f3b4716c85317d030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Nov 2016 11:21:57 +0800 Subject: [PATCH 733/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=BF=83=E8=B7=B3=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Changelog.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index ae3b1b62..bb1fe035 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,20 @@ -#### v0.0.5: +#### v0.0.6 + +1. 网关服务增加UDP及组播支持,后续网关部分的TCP部分将逐步淘汰 +2. 新增用户打标,修改标签,取消标签功能 +3. 全网推送增加按标签过滤,按条件过滤,条件表达式目前支持javascript +4. Service模块代码优化,增加同步启动/停止,超时监控 +5. 推送模块增加流控功能, 分为全局流控和广播流控,以及基于Redis实现的实时流控 +6. 由于网关采用了UDP,PushClient模块和踢人模块增加UDP支持 +7. 线程池代码优化,线程命名调整, 支持配置调整Netty boss和work的线程数 +8. 路由模块:客户端定义增加SPI支持, 用户可自定义控制多端在线策略 +9. 日志及配置项优化,增加mp.home配置项 +10. 心跳检测优化,连接一建立就开始检测心跳,防止客户端握手失败或未握手的情况 + + + + +#### v0.0.5 1. redis 增加redis3.x集群支持, 配置项不再兼容 2. 绑定用户调整,校验重复绑定,以及未解绑,就绑定的场景 @@ -20,7 +36,7 @@ -#### v0.0.4: +#### v0.0.4 1. push client API 调整 2. push 接口增加了全网推送功能 From 1b4863167563979d55e91541ecb1bbea839b1f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Nov 2016 13:34:28 +0800 Subject: [PATCH 734/890] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/set-env.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/set-env.sh b/bin/set-env.sh index eb607aae..693195e4 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -6,11 +6,12 @@ #SIMPLE: 抽样检测,且只对部分方法调用进行记录,消耗较小,有泄漏时可能会延迟报告,默认级别; #ADVANCED: 抽样检测,记录对象最近几次的调用记录,有泄漏时可能会延迟报告; #PARANOID: 每次创建一个对象时都进行泄露检测,且会记录对象最近的详细调用记录。是比较激进的内存泄露检测级别,消耗最大,建议只在测试时使用。 +JVM_FLAGS="-Dio.netty.leakDetection.level=advanced" #Dio.netty.noKeySetOptimization #是否禁用nio Selector.selectedKeys优化, 通过反射实现, 默认false -JVM_FLAGS="-Dio.netty.leakDetection.level=advanced -Dio.netty.noKeySetOptimization=false" +JVM_FLAGS="$JVM_FLAGS -Dio.netty.noKeySetOptimization=false" #开启远程调试 #JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" \ No newline at end of file From c27cd8714d5724a86867958b52edc171df510642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Nov 2016 13:56:26 +0800 Subject: [PATCH 735/890] =?UTF-8?q?ACK=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/core/ack/AckContext.java | 28 ++++++++++++------- .../com/mpush/core/ack/AckMessageQueue.java | 17 +++++++---- .../mpush/core/push/SingleUserPushTask.java | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java index dc1651ed..436db2dc 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckContext.java @@ -19,9 +19,7 @@ package com.mpush.core.ack; -import com.mpush.common.message.BaseMessage; - -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.Future; /** * Created by ohun on 16/9/5. @@ -29,16 +27,20 @@ * @author ohun@live.cn (夜色) */ public final class AckContext implements Runnable { - private final AtomicBoolean done = new AtomicBoolean(false); - private AckCallback callback; - /*package*/ int pushMessageId; + + private int sessionId; + private Future future; public AckContext() { } - public boolean tryDone() { - return done.compareAndSet(false, true); + public void setSessionId(int sessionId) { + this.sessionId = sessionId; + } + + public void setFuture(Future future) { + this.future = future; } public AckContext setCallback(AckCallback callback) { @@ -46,23 +48,29 @@ public AckContext setCallback(AckCallback callback) { return this; } + private boolean tryDone() { + return future.cancel(true); + } + public void success() { if (tryDone()) { callback.onSuccess(this); + callback = null; } } public void timeout() { - AckContext context = AckMessageQueue.I.getAndRemove(pushMessageId); + AckContext context = AckMessageQueue.I.getAndRemove(sessionId); if (context != null && tryDone()) { callback.onTimeout(this); + callback = null; } } @Override public String toString() { return "AckContext{" + - ", pushMessageId=" + pushMessageId + + ", sessionId=" + sessionId + '}'; } diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java index 098edc72..ccbc5f64 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java @@ -34,6 +34,7 @@ */ public final class AckMessageQueue { private final Logger logger = LoggerFactory.getLogger(AckMessageQueue.class); + private static final int DEFAULT_TIMEOUT = 3000; public static final AckMessageQueue I = new AckMessageQueue(); @@ -41,15 +42,19 @@ public final class AckMessageQueue { private final ScheduledExecutorService scheduledExecutor; private AckMessageQueue() { - scheduledExecutor = new ScheduledThreadPoolExecutor(1, new NamedPoolThreadFactory(T_ARK_REQ_TIMER), (r, e) -> { - logger.error("one ack context was rejected, context=" + r); - }); + scheduledExecutor = new ScheduledThreadPoolExecutor(1, + new NamedPoolThreadFactory(T_ARK_REQ_TIMER), + (r, e) -> logger.error("one ack context was rejected, context=" + r) + ); } - public void put(int sessionId, AckContext context, int timeout) { + public void add(int sessionId, AckContext context, int timeout) { queue.put(sessionId, context); - context.pushMessageId = sessionId; - scheduledExecutor.schedule(context, timeout > 0 ? timeout : DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); + context.setSessionId(sessionId); + context.setFuture(scheduledExecutor.schedule(context, + timeout > 0 ? timeout : DEFAULT_TIMEOUT, + TimeUnit.MILLISECONDS + )); } public AckContext getAndRemove(int sessionId) { diff --git a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java index 3b02f548..4b069a5f 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java @@ -105,7 +105,7 @@ private boolean checkLocal(final GatewayPushMessage message) { if (future.isSuccess()) {//推送成功 if (message.needAck()) {//需要客户端ACK, 消息进队列等待客户端响应ACK - AckMessageQueue.I.put(pushMessage.getSessionId(), buildAckContext(message), message.timeout); + AckMessageQueue.I.add(pushMessage.getSessionId(), buildAckContext(message), message.timeout); } else { OkMessage.from(message).setData(userId + ',' + clientType).sendRaw(); } From 2d263a896d896584daef0ffb9443d5164adee7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Nov 2016 16:24:05 +0800 Subject: [PATCH 736/890] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mp.cmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/mp.cmd b/bin/mp.cmd index 9df778b4..496b0fc3 100644 --- a/bin/mp.cmd +++ b/bin/mp.cmd @@ -25,7 +25,7 @@ REM call %JAVA% "-Dmp.conf=%MPCFG%" "-Dmp.home=%~dp0%.." -cp "%CLASSPATH%" %MPMA REM endlocal -java -Dmp.conf=../conf/mpush.conf -Dmp.home=. -jar bootstrap.jar +java -Dmp.conf=../conf/mpush.conf -Dmp.home=.. -jar bootstrap.jar From 97cba6573e7239c828db170d5fa8fd8358b976e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 28 Nov 2016 16:24:36 +0800 Subject: [PATCH 737/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/client/gateway/GatewayUDPConnector.java | 4 +++- .../src/main/java/com/mpush/core/server/ConnectionServer.java | 4 +++- .../src/main/java/com/mpush/core/server/GatewayServer.java | 4 +++- .../main/java/com/mpush/core/server/GatewayUDPConnector.java | 4 +++- .../src/main/java/com/mpush/netty/http/NettyHttpClient.java | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java index 784ad7c8..b43b3786 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/GatewayUDPConnector.java @@ -47,7 +47,9 @@ public final class GatewayUDPConnector extends NettyUDPConnector { public static GatewayUDPConnector I() { if (I == null) { synchronized (GatewayUDPConnector.class) { - I = new GatewayUDPConnector(); + if (I == null) { + I = new GatewayUDPConnector(); + } } } return I; diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 472a3e35..d3b8cb94 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -59,7 +59,9 @@ public final class ConnectionServer extends NettyTCPServer { public static ConnectionServer I() { if (I == null) { synchronized (ConnectionServer.class) { - I = new ConnectionServer(); + if (I == null) { + I = new ConnectionServer(); + } } } return I; diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java index 8920ea13..3bf21e3a 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayServer.java @@ -55,7 +55,9 @@ public final class GatewayServer extends NettyTCPServer { public static GatewayServer I() { if (I == null) { synchronized (GatewayServer.class) { - I = new GatewayServer(); + if (I == null) { + I = new GatewayServer(); + } } } return I; diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java index 570ab07b..04b1261d 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java @@ -49,7 +49,9 @@ public final class GatewayUDPConnector extends NettyUDPConnector { public static GatewayUDPConnector I() { if (I == null) { synchronized (GatewayUDPConnector.class) { - I = new GatewayUDPConnector(); + if (I == null) { + I = new GatewayUDPConnector(); + } } } return I; diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index aebaf198..1c54ee80 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -71,7 +71,9 @@ public class NettyHttpClient extends BaseService implements HttpClient { public static HttpClient I() { if (I == null) { synchronized (NettyHttpClient.class) { - I = new NettyHttpClient(); + if (I == null) { + I = new NettyHttpClient(); + } } } return I; From fa77a9e311372f0e4998537701e75de239fbb137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Nov 2016 11:39:41 +0800 Subject: [PATCH 738/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/core/server/ServerConnectionManager.java | 4 ---- .../main/java/com/mpush/tools/thread/NamedThreadFactory.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index c71a426c..e8d1f4b3 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -51,10 +51,6 @@ public final class ServerConnectionManager implements ConnectionManager { private HashedWheelTimer timer; private final boolean heartbeatCheck; - public ServerConnectionManager() { - this.heartbeatCheck = true; - } - public ServerConnectionManager(boolean heartbeatCheck) { this.heartbeatCheck = heartbeatCheck; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 0c19918a..12ccdf65 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -58,7 +58,7 @@ public NamedThreadFactory(final String namePrefix) { */ public Thread newThread(String name, Runnable r) { Thread thread = new Thread(group, r, namePrefix + "-" + threadNumber.getAndIncrement() + "-" + name); - thread.setDaemon(true); + thread.setDaemon(false); //设置为非守护线程,否则jvm会立即退出 return thread; } From b962963f4d1506cca64fc6381e3ec87694891ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Nov 2016 11:39:58 +0800 Subject: [PATCH 739/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/client/push/PushRequest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index a9e28e1f..e3d5e590 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -31,6 +31,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.Future; @@ -222,6 +223,9 @@ public static PushRequest build(GatewayConnectionFactory factory, PushContext ct content = json.getBytes(Constants.UTF_8); } } + + Objects.requireNonNull(content, "push content can not be null."); + return new PushRequest(factory) .setAckModel(ctx.getAckModel()) .setUserId(ctx.getUserId()) @@ -271,7 +275,7 @@ public PushRequest setCondition(String condition) { @Override public String toString() { return "PushRequest{" + - "content='" + content + '\'' + + "content='" + content.length + '\'' + ", userId='" + userId + '\'' + ", timeout=" + timeout + ", location=" + location + From 29ae98c7a817d4d09a0f99afa15f39914db15f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Nov 2016 18:27:47 +0800 Subject: [PATCH 740/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=AB=AF?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/common/router/ClientType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-common/src/main/java/com/mpush/common/router/ClientType.java b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java index a6c9087d..704e81c8 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ClientType.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java @@ -51,7 +51,7 @@ public static boolean isSameClient(String osName1, String osName2) { public static ClientType find(String osName) { for (ClientType type : values()) { - if (type.contains(osName)) return type; + if (type.contains(osName.toLowerCase())) return type; } return UNKNOWN; } From f5336b3f9507f7b1cf6e3ecd6b33f81be6184deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 29 Nov 2016 20:08:21 +0800 Subject: [PATCH 741/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=AB=AF?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/common/router/ClientType.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-common/src/main/java/com/mpush/common/router/ClientType.java b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java index 704e81c8..d6755bc7 100644 --- a/mpush-common/src/main/java/com/mpush/common/router/ClientType.java +++ b/mpush-common/src/main/java/com/mpush/common/router/ClientType.java @@ -44,15 +44,15 @@ public boolean contains(String osName) { return Arrays.stream(os).anyMatch(osName::contains); } - public static boolean isSameClient(String osName1, String osName2) { - if (osName1.equals(osName2)) return true; - return find(osName1).contains(osName2); - } - public static ClientType find(String osName) { for (ClientType type : values()) { if (type.contains(osName.toLowerCase())) return type; } return UNKNOWN; } + + public static boolean isSameClient(String osNameA, String osNameB) { + if (osNameA.equals(osNameB)) return true; + return find(osNameA).contains(osNameB); + } } From aa2fbd9b1cbf0207586107ed7a05541dc1c5e037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 1 Dec 2016 16:47:50 +0800 Subject: [PATCH 742/890] =?UTF-8?q?Service=E6=A8=A1=E5=9D=97=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=90=AF=E5=8A=A8/=E5=81=9C=E6=AD=A2=EF=BC=8C?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-zk/src/main/java/com/mpush/zk/ZKClient.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 63b44af9..74c07074 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -59,8 +59,11 @@ private ZKClient() { @Override public void start(Listener listener) { - if (isRunning()) return; - super.start(listener); + if (isRunning()) { + listener.onSuccess(); + } else { + super.start(listener); + } } @Override From cf5f9d9775096e63ad3bf4d52b59b7a0f107a7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 7 Dec 2016 22:24:51 +0800 Subject: [PATCH 743/890] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 59a022f8..a3b79c97 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -11,8 +11,10 @@ ################################################################################################################## mp { + #基础配置 + home=${user.dir} //程序工作目前 + #日志配置 - home=${user.dir} log-level=warn log-dir=${mp.home}/logs log-conf-path=${mp.home}/conf/logback.xml @@ -189,15 +191,16 @@ mp { } } + #推送消息流控 push { flow-control { //qps = limit/(duration) - global:{ + global:{ //针对非广播推送的流控,全局有效 limit:5000 //qps = 5000 max:0 //UN limit duration:1s //1s } - broadcast:{ + broadcast:{ //针对广播消息的流控,单次任务有效 limit:3000 //qps = 3000 max:100000 //10w duration:1s //1s From 79f965810348651140e7c0fcbf36e2a003e2ad68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 7 Dec 2016 22:25:57 +0800 Subject: [PATCH 744/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8D=95=E5=8F=B0=E6=9C=BA=E5=99=A8=E5=90=AF=E5=8A=A8=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/router/ClientLocation.java | 22 ++++++++++++ .../com/mpush/bootstrap/ServerLauncher.java | 7 ++-- .../connection/GatewayConnectionFactory.java | 4 +-- .../GatewayTCPConnectionFactory.java | 16 +++++---- .../GatewayUDPConnectionFactory.java | 35 ++++++++----------- .../com/mpush/client/push/PushClient.java | 6 ++-- .../com/mpush/client/push/PushRequest.java | 3 +- .../gateway/GatewayKickUserMessage.java | 15 ++++++++ .../com/mpush/common/net/KickRemoteMsg.java | 12 +++++++ .../mpush/core/push/SingleUserPushTask.java | 4 ++- .../core/router/RedisKickRemoteMessage.java | 12 +++++++ .../com/mpush/core/router/RouterCenter.java | 6 +++- .../core/router/RouterChangeListener.java | 23 ++++++------ .../mpush/netty/client/NettyTCPClient.java | 4 +++ .../mpush/test/client/ConnClientTestMain.java | 4 +++ .../mpush/test/push/PushClientTestMain.java | 12 +++++-- .../com/mpush/test/sever/ServerTestMain.java | 6 ++++ .../java/com/mpush/zk/node/ZKServerNode.java | 11 ++++-- 18 files changed, 147 insertions(+), 55 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java index e6963658..8530b602 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java +++ b/mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java @@ -34,6 +34,11 @@ public final class ClientLocation { */ private String host; + /** + * 长链接所在的机器端口 + */ + private int port; + /** * 客户端系统类型 */ @@ -68,6 +73,15 @@ public ClientLocation setHost(String host) { return this; } + public int getPort() { + return port; + } + + public ClientLocation setPort(int port) { + this.port = port; + return this; + } + public String getOsName() { return osName; } @@ -120,6 +134,14 @@ public ClientLocation offline() { return this; } + public boolean isThisPC(String host, int port) { + return this.port == port && this.host.equals(host); + } + + public String getHostAndPort() { + return host + ":" + port; + } + public static ClientLocation from(Connection connection) { SessionContext context = connection.getSessionContext(); ClientLocation location = new ClientLocation(); diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 1de57d30..7b4949f3 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -27,8 +27,7 @@ import com.mpush.core.server.GatewayUDPConnector; import static com.mpush.tools.config.CC.mp.net.udpGateway; -import static com.mpush.zk.node.ZKServerNode.csNode; -import static com.mpush.zk.node.ZKServerNode.gsNode; +import static com.mpush.zk.node.ZKServerNode.*; /** * Created by yxx on 2016/5/14. @@ -43,8 +42,8 @@ public ServerLauncher() { chain.boot() .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 .setNext(new RedisBoot())//2.注册redis sever 到ZK - .setNext(new ServerBoot(ConnectionServer.I(), csNode()))//3.启动长连接服务 - .setNext(new ServerBoot(udpGateway() ? GatewayUDPConnector.I() : GatewayServer.I(), gsNode()))//4.启动网关服务 + .setNext(new ServerBoot(ConnectionServer.I(), CS_NODE))//3.启动长连接服务 + .setNext(new ServerBoot(udpGateway() ? GatewayUDPConnector.I() : GatewayServer.I(), GS_NODE))//4.启动网关服务 .setNext(new ServerBoot(AdminServer.I(), null))//5.启动控制台服务 .setNext(new PushCenterBoot())//6.启动http代理服务,解析dns .setNext(new HttpProxyBoot())//6.启动http代理服务,解析dns diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java index b24809ee..40d07033 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java @@ -50,9 +50,9 @@ public void init(Listener listener) { listener.onSuccess(); } - abstract public Connection getConnection(String ip); + abstract public Connection getConnection(String hostAndPort); - abstract public Function send(Function creator, Function sender); + abstract public void send(String hostAndPort, Function creator, Function sender); abstract public void broadcast(Function creator, Consumer sender); diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java index c31f034a..20f38c31 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java @@ -62,8 +62,8 @@ public void clear() { } @Override - public Connection getConnection(String ip) { - GatewayClient client = ip_client.get(ip); + public Connection getConnection(String hostAndPort) { + GatewayClient client = ip_client.get(hostAndPort); if (client == null) { return null;//TODO create client } @@ -77,8 +77,10 @@ public Connection getConnection(String ip) { @Override - public Function send(Function creator, Function sender) { - return creator.compose(this::getConnection).andThen(sender); + public void send(String hostAndPort, Function creator, Function sender) { + creator.compose(this::getConnection) + .andThen(sender) + .apply(hostAndPort); } @Override @@ -89,7 +91,7 @@ public void broadcast(Function creator, C } private void restartClient(final GatewayClient client) { - ip_client.remove(client.getHost()); + ip_client.remove(client.getHostAndPort()); client.stop(new Listener() { @Override public void onSuccess(Object... args) { @@ -105,7 +107,7 @@ public void onFailure(Throwable cause) { private void removeClient(ZKServerNode node) { if (node != null) { - Client client = ip_client.remove(node.getIp()); + Client client = ip_client.remove(node.getHostAndPort()); if (client != null) { client.stop(null); } @@ -117,7 +119,7 @@ private void addClient(final String host, final int port) { client.start(new Listener() { @Override public void onSuccess(Object... args) { - ip_client.put(host, client); + ip_client.put(client.getHostAndPort(), client); } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java index ee2d5e05..5b5f37f5 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java @@ -61,7 +61,7 @@ public void init(Listener listener) { @Override public void put(String fullPath, ZKServerNode node) { super.put(fullPath, node); - ip_address.put(node.getIp(), new InetSocketAddress(node.getIp(), node.getPort())); + ip_address.put(node.getHostAndPort(), new InetSocketAddress(node.getIp(), node.getPort())); } @Override @@ -79,30 +79,23 @@ public void clear() { } @Override - public Connection getConnection(String ip) { + public Connection getConnection(String hostAndPort) { return connector.getConnection(); } + @SuppressWarnings("unchecked") @Override - public Function send(Function creator, Function sender) { - - Holder holder = new Holder<>(); - - Function getConn = host -> { - InetSocketAddress recipient = ip_address.get(host); - if (recipient == null) return null; - holder.set(recipient); - return connector.getConnection(); - }; - - Function setRecipientFun = message -> { - if (message != null) { - message.setRecipient(holder.get()); - } - return message; - }; - - return creator.compose(getConn).andThen(setRecipientFun).andThen(sender); + public void send(String hostAndPort, Function creator, Function sender) { + InetSocketAddress recipient = ip_address.get(hostAndPort); + if (recipient == null) {// gateway server 找不到,直接返回推送失败 + creator.apply(null); + return; + } + + creator.compose(this::getConnection) + .andThen(message -> (T) message.setRecipient(recipient)) + .andThen(sender) + .apply(hostAndPort); } @Override diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 0e625f1a..b0112e08 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -60,7 +60,9 @@ private FutureTask send0(PushContext ctx) { @Override public FutureTask send(PushContext ctx) { - if (ctx.getUserId() != null) { + if (ctx.isBroadcast()) { + return send0(ctx.setUserId(null)); + } else if (ctx.getUserId() != null) { return send0(ctx); } else if (ctx.getUserIds() != null) { FutureTask task = null; @@ -68,8 +70,6 @@ public FutureTask send(PushContext ctx) { task = send0(ctx.setUserId(userId)); } return task; - } else if (ctx.isBroadcast()) { - return send0(ctx.setUserId(null)); } else { throw new PushException("param error."); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index e3d5e590..db872482 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -80,6 +80,7 @@ private void sendToConnServer(RemoteRouter remoteRouter) { //2.通过网关连接,把消息发送到所在机器 connectionFactory.send( + location.getHostAndPort(), connection -> { timeLine.addTimePoint("check-gateway-conn"); if (connection == null) { @@ -108,7 +109,7 @@ private void sendToConnServer(RemoteRouter remoteRouter) { } return null; } - ).apply(location.getHost()); + ); } private void submit(Status status) { diff --git a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java index e08bc132..92798dac 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/gateway/GatewayKickUserMessage.java @@ -40,6 +40,8 @@ public final class GatewayKickUserMessage extends ByteBufMessage implements Kick public String connId; public int clientType; public String targetServer; + public int targetPort; + public GatewayKickUserMessage(Packet message, Connection connection) { super(message, connection); @@ -58,6 +60,7 @@ public void decode(ByteBuf body) { connId = decodeString(body); clientType = decodeInt(body); targetServer = decodeString(body); + targetPort = decodeInt(body); } @Override @@ -67,6 +70,7 @@ public void encode(ByteBuf body) { encodeString(body, connId); encodeInt(body, clientType); encodeString(body, targetServer); + encodeInt(body, targetPort); } public GatewayKickUserMessage setUserId(String userId) { @@ -94,6 +98,11 @@ public GatewayKickUserMessage setTargetServer(String targetServer) { return this; } + public GatewayKickUserMessage setTargetPort(int targetPort) { + this.targetPort = targetPort; + return this; + } + @Override public String getUserId() { return userId; @@ -119,6 +128,11 @@ public String getTargetServer() { return targetServer; } + @Override + public int getTargetPort() { + return targetPort; + } + @Override public String toString() { return "GatewayKickUserMessage{" + @@ -127,6 +141,7 @@ public String toString() { ", connId='" + connId + '\'' + ", clientType=" + clientType + ", targetServer='" + targetServer + '\'' + + ", targetPort=" + targetPort + '}'; } } diff --git a/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java b/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java index 1b9c82ca..695fdfbf 100644 --- a/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java +++ b/mpush-common/src/main/java/com/mpush/common/net/KickRemoteMsg.java @@ -19,6 +19,11 @@ package com.mpush.common.net; +import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; + +import static com.mpush.zk.node.ZKServerNode.GS_NODE; + /** * Created by ohun on 16/10/23. * @@ -34,4 +39,11 @@ public interface KickRemoteMsg { int getClientType(); String getTargetServer(); + + int getTargetPort(); + + default boolean isTargetPC() { + return this.getTargetPort() == GS_NODE.getPort() + && this.getTargetServer().equals(Utils.getLocalIp()); + } } diff --git a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java index 4b069a5f..3120dbce 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java @@ -32,11 +32,13 @@ import com.mpush.core.router.LocalRouter; import com.mpush.core.router.RouterCenter; import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import static com.mpush.common.ErrorCode.OFFLINE; import static com.mpush.common.ErrorCode.PUSH_CLIENT_FAILURE; import static com.mpush.common.ErrorCode.ROUTER_CHANGE; +import static com.mpush.zk.node.ZKServerNode.GS_NODE; /** * Created by ohun on 16/10/24. @@ -149,7 +151,7 @@ private void checkRemote(GatewayPushMessage message) { } //2.如果查出的远程机器是当前机器,说明路由已经失效,此时用户已下线,需要删除失效的缓存 - if (Utils.getLocalIp().equals(remoteRouter.getRouteValue().getHost())) { + if (remoteRouter.getRouteValue().isThisPC(GS_NODE.getIp(), GS_NODE.getPort())) { ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); diff --git a/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java b/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java index dbd46f63..0ef3fe4a 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RedisKickRemoteMessage.java @@ -32,6 +32,7 @@ class RedisKickRemoteMessage implements KickRemoteMsg { private String connId; private int clientType; private String targetServer; + private int targetPort; public RedisKickRemoteMessage setUserId(String userId) { this.userId = userId; @@ -58,6 +59,11 @@ public RedisKickRemoteMessage setTargetServer(String targetServer) { return this; } + public RedisKickRemoteMessage setTargetPort(int targetPort) { + this.targetPort = targetPort; + return this; + } + @Override public String getUserId() { return userId; @@ -83,6 +89,11 @@ public String getTargetServer() { return targetServer; } + @Override + public int getTargetPort() { + return targetPort; + } + @Override public String toString() { return "KickRemoteMsg{" @@ -91,6 +102,7 @@ public String toString() { + ", connId='" + connId + '\'' + ", clientType='" + clientType + '\'' + ", targetServer='" + targetServer + '\'' + + ", targetPort=" + targetPort + '}'; } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java index 5da043a8..03c5b8bd 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterCenter.java @@ -26,10 +26,13 @@ import com.mpush.common.router.RemoteRouter; import com.mpush.common.router.RemoteRouterManager; import com.mpush.tools.Utils; +import com.mpush.tools.config.CC; import com.mpush.tools.event.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.mpush.zk.node.ZKServerNode.GS_NODE; + /** * Created by ohun on 2015/12/23. * @@ -55,7 +58,8 @@ public final class RouterCenter { public boolean register(String userId, Connection connection) { ClientLocation location = ClientLocation .from(connection) - .setHost(Utils.getLocalIp()); + .setHost(Utils.getLocalIp()) + .setPort(GS_NODE.getPort()); LocalRouter localRouter = new LocalRouter(connection); RemoteRouter remoteRouter = new RemoteRouter(location); diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index f9bc541b..b689cc99 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -38,10 +38,11 @@ import com.mpush.tools.config.CC; import com.mpush.tools.event.EventConsumer; import com.mpush.tools.log.Logs; +import com.mpush.zk.node.ZKServerNode; import java.net.InetSocketAddress; -import static com.mpush.tools.config.CC.mp.net.gateway_server_port; +import static com.mpush.zk.node.ZKServerNode.GS_NODE; /** * Created by ohun on 2016/1/4. @@ -50,7 +51,7 @@ */ public final class RouterChangeListener extends EventConsumer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; - private final String kick_channel = KICK_CHANNEL_ + Utils.getLocalIp(); + private final String kick_channel = KICK_CHANNEL_ + GS_NODE.getHostAndPort(); public RouterChangeListener() { ListenerDispatcher.I.subscribe(getKickChannel(), this); @@ -60,8 +61,8 @@ public String getKickChannel() { return kick_channel; } - public String getKickChannel(String remoteIp) { - return KICK_CHANNEL_ + remoteIp; + public String getKickChannel(String hostAndPort) { + return KICK_CHANNEL_ + hostAndPort; } @Subscribe @@ -108,8 +109,8 @@ private void kickLocal(final String userId, final LocalRouter router) { private void kickRemote(String userId, RemoteRouter remoteRouter) { ClientLocation location = remoteRouter.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 - if (location.getHost().equals(Utils.getLocalIp())) { - Logs.Conn.info("kick remote user but router in local, userId={}", userId); + if (location.isThisPC(GS_NODE.getIp(), GS_NODE.getPort())) { + Logs.Conn.info("kick remote user but router in local, ignore remote broadcast, userId={}", userId); return; } @@ -121,7 +122,8 @@ private void kickRemote(String userId, RemoteRouter remoteRouter) { .setConnId(location.getConnId()) .setDeviceId(location.getDeviceId()) .setTargetServer(location.getHost()) - .setRecipient(new InetSocketAddress(location.getHost(), gateway_server_port)) + .setTargetPort(location.getPort()) + .setRecipient(new InetSocketAddress(location.getHost(), location.getPort())) .sendRaw(); } else { //2.发送广播 @@ -131,8 +133,9 @@ private void kickRemote(String userId, RemoteRouter remoteRouter) { .setClientType(location.getClientType()) .setConnId(location.getConnId()) .setDeviceId(location.getDeviceId()) - .setTargetServer(location.getHost()); - RedisManager.I.publish(getKickChannel(location.getHost()), message); + .setTargetServer(location.getHost()) + .setTargetPort(location.getPort()); + RedisManager.I.publish(getKickChannel(location.getHostAndPort()), message); } } @@ -146,7 +149,7 @@ private void kickRemote(String userId, RemoteRouter remoteRouter) { */ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 - if (!msg.getTargetServer().equals(Utils.getLocalIp())) { + if (!msg.isTargetPC()) { Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); return; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java index c3a4339a..aa2af5ce 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java @@ -162,6 +162,10 @@ public int getPort() { return port; } + public String getHostAndPort() { + return host + ":" + port; + } + @Override public String toString() { return "NettyClient{" + diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index 8bb3e53a..284b7960 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -39,6 +39,10 @@ public class ConnClientTestMain { + public static void main(String[] args) throws Exception { + new ConnClientTestMain().testConnClient(); + } + @Test public void testConnClient() throws Exception { ConnClientBoot boot = new ConnClientBoot(); diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java index c75ddee1..f655ba12 100644 --- a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java @@ -35,6 +35,11 @@ * @author ohun@live.cn */ public class PushClientTestMain { + + public static void main(String[] args) throws Exception { + new PushClientTestMain().testPush(); + } + @Test public void testPush() throws Exception { Logs.init(); @@ -44,10 +49,11 @@ public void testPush() throws Exception { msg.setMsgId("msgId_0"); PushContext context = PushContext.build(msg) - .setBroadcast(true) .setAckModel(AckModel.AUTO_ACK) + .setUserId("user-0") + .setBroadcast(false) //.setTags(Sets.newHashSet("test")) - .setCondition("tags&&tags.indexOf('test')!=-1") + //.setCondition("tags&&tags.indexOf('test')!=-1") //.setUserIds(Arrays.asList("user-0", "user-1")) .setTimeout(2000) .setCallback(new PushCallback() { @@ -59,7 +65,7 @@ public void onResult(PushResult result) { FutureTask future = sender.send(context); }); - LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3)); + LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(30)); } } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java index 1fd2ba13..327f8223 100644 --- a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java @@ -30,6 +30,11 @@ * @author ohun@live.cn */ public class ServerTestMain { + + public static void main(String[] args) { + new ServerTestMain().testServer(); + } + @Test public void testServer() { System.setProperty("io.netty.leakDetection.level", "PARANOID"); @@ -37,4 +42,5 @@ public void testServer() { Main.main(null); LockSupport.park(); } + } diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index c24b0945..dacba13e 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -34,6 +34,9 @@ */ public class ZKServerNode implements ZKNode { + public static final ZKServerNode CS_NODE = csNode(); + public static final ZKServerNode GS_NODE = gsNode(); + private String ip; private int port; @@ -52,14 +55,14 @@ public ZKServerNode(String ip, int port, String extranetIp, String zkPath) { this.zkPath = zkPath; } - public static ZKServerNode csNode() { + private static ZKServerNode csNode() { return new ZKServerNode(Utils.getLocalIp(), CC.mp.net.connect_server_port, ConfigManager.I.getPublicIp(), ZKPath.CONNECT_SERVER.getNodePath()); } - public static ZKServerNode gsNode() { + private static ZKServerNode gsNode() { return new ZKServerNode(Utils.getLocalIp(), CC.mp.net.gateway_server_port, null, @@ -103,6 +106,10 @@ public ZKServerNode setZkPath(String zkPath) { return this; } + public String getHostAndPort() { + return ip + ":" + port; + } + @Override public boolean equals(Object o) { if (this == o) return true; From a444897afebbfe9b1e5eb167de26e5378bbbd597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 10:43:31 +0800 Subject: [PATCH 745/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/client/connect/ClientConfig.java | 7 ++ .../connect/ConnClientChannelHandler.java | 88 +++++++++++-------- .../mpush/client/connect/TestStatistics.java | 44 ++++++++++ .../com/mpush/test/client/ConnClientBoot.java | 70 ++++++++++++++- .../mpush/test/client/ConnClientTestMain.java | 20 +++-- mpush-test/src/test/resources/logback.xml | 2 +- 6 files changed, 185 insertions(+), 46 deletions(-) create mode 100644 mpush-client/src/main/java/com/mpush/client/connect/TestStatistics.java diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java index ed5e5131..693a78ea 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ClientConfig.java @@ -95,4 +95,11 @@ public void setUserId(String userId) { this.userId = userId; } + @Override + public String toString() { + return "{" + + "deviceId='" + deviceId + '\'' + + ", userId='" + userId + '\'' + + '}'; + } } diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 3eb55e84..f8b3116e 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -28,6 +28,7 @@ import com.mpush.api.protocol.Packet; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.common.ErrorCode; import com.mpush.common.message.*; import com.mpush.common.security.AesCipher; import com.mpush.common.security.CipherBox; @@ -36,10 +37,7 @@ import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.ThreadNames; import io.netty.channel.*; -import io.netty.util.HashedWheelTimer; -import io.netty.util.Timeout; -import io.netty.util.Timer; -import io.netty.util.TimerTask; +import io.netty.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,9 +54,16 @@ public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedPoolThreadFactory(ThreadNames.T_CONN_TIMER)); + public static final AttributeKey CONFIG_KEY = AttributeKey.newInstance("clientConfig"); + public static final TestStatistics STATISTICS = new TestStatistics(); private final Connection connection = new NettyConnection(); - private final ClientConfig clientConfig; + private ClientConfig clientConfig; + private boolean stressingTest; + + public ConnClientChannelHandler() { + stressingTest = true; + } public ConnClientChannelHandler(ClientConfig clientConfig) { this.clientConfig = clientConfig; @@ -75,15 +80,19 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Packet packet = (Packet) msg; Command command = Command.toCMD(packet.cmd); if (command == Command.HANDSHAKE) { + int connectedNum = STATISTICS.connectedNum.incrementAndGet(); connection.getSessionContext().changeCipher(new AesCipher(clientConfig.getClientKey(), clientConfig.getIv())); HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); startHeartBeat(message.heartbeat); - LOGGER.warn(">>> handshake success, message={}, sessionKey={}", message, sessionKey); + LOGGER.info(">>> handshake success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); bindUser(clientConfig); - saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); + if (!stressingTest) { + saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); + } } else if (command == Command.FAST_CONNECT) { + int connectedNum = STATISTICS.connectedNum.incrementAndGet(); String cipherStr = clientConfig.getCipher(); String[] cs = cipherStr.split(","); byte[] key = AesCipher.toArray(cs[0]); @@ -93,7 +102,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); startHeartBeat(message.heartbeat); bindUser(clientConfig); - LOGGER.warn(">>> fast connect success, message=" + message); + LOGGER.info(">>> fast connect success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); LOGGER.error(">>> receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); @@ -101,31 +110,31 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); LOGGER.error(">>> receive an error packet=" + errorMessage); - } else if (command == Command.BIND) { - } else if (command == Command.PUSH) { + int receivePushNum = STATISTICS.receivePushNum.incrementAndGet(); + PushMessage message = new PushMessage(packet, connection); - LOGGER.warn(">>> receive an push message, content=" + new String(message.content, Constants.UTF_8)); + LOGGER.info(">>> receive an push message, content={}, receivePushNum={}", new String(message.content, Constants.UTF_8), receivePushNum); if (message.needAck()) { AckMessage.from(message).sendRaw(); - LOGGER.warn(">>> send ack success for sessionId={}", message.getSessionId()); + LOGGER.info(">>> send ack success for sessionId={}", message.getSessionId()); } } else if (command == Command.HEARTBEAT) { - LOGGER.warn(">>> receive a heartbeat pong..."); + LOGGER.info(">>> receive a heartbeat pong..."); } else if (command == Command.OK) { OkMessage okMessage = new OkMessage(packet, connection); - LOGGER.warn(">>> receive an success packet=" + okMessage); - Map headers = new HashMap<>(); - headers.put(Constants.HTTP_HEAD_READ_TIMEOUT, "10000"); - HttpRequestMessage message = new HttpRequestMessage(connection); - message.headers = headers; - message.uri = "http://baidu.com"; - message.send(); + int bindUserNum = STATISTICS.bindUserNum.get(); + if (okMessage.cmd == Command.BIND.cmd) { + bindUserNum = STATISTICS.bindUserNum.incrementAndGet(); + } + + LOGGER.info(">>> receive an success message={}, bindUserNum={}", okMessage, bindUserNum); + } else if (command == Command.HTTP_PROXY) { HttpResponseMessage message = new HttpResponseMessage(packet, connection); - LOGGER.warn(">>> receive a http response, message={}, body={}", + LOGGER.info(">>> receive a http response, message={}, body={}", message, message.body == null ? null : new String(message.body, Constants.UTF_8)); } } @@ -142,16 +151,25 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - LOGGER.info("client connect channel={}", ctx.channel()); + int clientNum = STATISTICS.clientNum.incrementAndGet(); + LOGGER.info("client connect channel={}, clientNum={}", ctx.channel(), clientNum); + if (clientConfig == null) { + clientConfig = ctx.channel().attr(CONFIG_KEY).getAndRemove(); + } connection.init(ctx.channel(), true); - tryFastConnect(); + if (stressingTest) { + handshake(); + } else { + tryFastConnect(); + } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { + int clientNum = STATISTICS.clientNum.decrementAndGet(); connection.close(); EventBus.I.post(new ConnectionCloseEvent(connection)); - LOGGER.info("client disconnect connection={}", connection); + LOGGER.info("client disconnect channel={}, clientNum={}", connection, clientNum); } private void tryFastConnect() { @@ -159,19 +177,19 @@ private void tryFastConnect() { Map sessionTickets = getFastConnectionInfo(clientConfig.getDeviceId()); if (sessionTickets == null) { - handshake(clientConfig); + handshake(); return; } String sessionId = sessionTickets.get("sessionId"); if (sessionId == null) { - handshake(clientConfig); + handshake(); return; } String expireTime = sessionTickets.get("expireTime"); if (expireTime != null) { long exp = Long.parseLong(expireTime); if (exp < System.currentTimeMillis()) { - handshake(clientConfig); + handshake(); return; } } @@ -186,7 +204,7 @@ private void tryFastConnect() { if (channelFuture.isSuccess()) { clientConfig.setCipher(cipher); } else { - handshake(clientConfig); + handshake(); } }); LOGGER.debug("<<< send fast connect message={}", message); @@ -215,14 +233,14 @@ private Map getFastConnectionInfo(String deviceId) { return RedisManager.I.get(key, Map.class); } - private void handshake(ClientConfig client) { + private void handshake() { HandshakeMessage message = new HandshakeMessage(connection); - message.clientKey = client.getClientKey(); - message.iv = client.getIv(); - message.clientVersion = client.getClientVersion(); - message.deviceId = client.getDeviceId(); - message.osName = client.getOsName(); - message.osVersion = client.getOsVersion(); + message.clientKey = clientConfig.getClientKey(); + message.iv = clientConfig.getIv(); + message.clientVersion = clientConfig.getClientVersion(); + message.deviceId = clientConfig.getDeviceId(); + message.osName = clientConfig.getOsName(); + message.osVersion = clientConfig.getOsVersion(); message.timestamp = System.currentTimeMillis(); message.send(); LOGGER.debug("<<< send handshake message={}", message); diff --git a/mpush-client/src/main/java/com/mpush/client/connect/TestStatistics.java b/mpush-client/src/main/java/com/mpush/client/connect/TestStatistics.java new file mode 100644 index 00000000..47dd683e --- /dev/null +++ b/mpush-client/src/main/java/com/mpush/client/connect/TestStatistics.java @@ -0,0 +1,44 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.client.connect; + +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Created by ohun on 2016/12/8. + * + * @author ohun@live.cn (夜色) + */ +public final class TestStatistics { + public AtomicInteger clientNum = new AtomicInteger(); + public AtomicInteger connectedNum = new AtomicInteger(); + public AtomicInteger bindUserNum = new AtomicInteger(); + public AtomicInteger receivePushNum = new AtomicInteger(); + + @Override + public String toString() { + return "TestStatistics{" + + "clientNum=" + clientNum + + ", connectedNum=" + connectedNum + + ", bindUserNum=" + bindUserNum + + ", receivePushNum=" + receivePushNum + + '}'; + } +} diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java index 31240687..c2c8fd18 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java @@ -22,20 +22,36 @@ import com.google.common.collect.Lists; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; -import com.mpush.api.service.ServiceException; import com.mpush.cache.redis.manager.RedisManager; +import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnClientChannelHandler; +import com.mpush.netty.codec.PacketDecoder; +import com.mpush.netty.codec.PacketEncoder; import com.mpush.zk.ZKClient; import com.mpush.zk.listener.ZKServerNodeWatcher; import com.mpush.zk.node.ZKServerNode; +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.AttributeKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.net.InetSocketAddress; import java.util.List; public final class ConnClientBoot extends BaseService { + private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientBoot.class); + private final ZKServerNodeWatcher watcher = ZKServerNodeWatcher.buildConnect(); + private Bootstrap bootstrap; + private NioEventLoopGroup workerGroup; - public List getServers() { - return Lists.newArrayList(watcher.getCache().values()); - } @Override protected void doStart(Listener listener) throws Throwable { @@ -52,12 +68,58 @@ public void onFailure(Throwable cause) { listener.onFailure(cause); } }); + + this.workerGroup = new NioEventLoopGroup(); + this.bootstrap = new Bootstrap(); + bootstrap.group(workerGroup)// + .option(ChannelOption.TCP_NODELAY, true)// + .option(ChannelOption.SO_REUSEADDR, true)// + .option(ChannelOption.SO_KEEPALIVE, true)// + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000) + .channel(NioSocketChannel.class); + + bootstrap.handler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast("decoder", new PacketDecoder()); + ch.pipeline().addLast("encoder", PacketEncoder.INSTANCE); + ch.pipeline().addLast("handler", new ConnClientChannelHandler()); + } + }); } @Override protected void doStop(Listener listener) throws Throwable { + if (workerGroup != null) workerGroup.shutdownGracefully(); ZKClient.I.syncStop(); RedisManager.I.destroy(); listener.onSuccess(); } + + public List getServers() { + return Lists.newArrayList(watcher.getCache().values()); + } + + + public void connect(String host, int port, ClientConfig clientConfig) { + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + future.channel().attr(ConnClientChannelHandler.CONFIG_KEY).set(clientConfig); + future.addListener(f -> { + if (f.isSuccess()) { + LOGGER.info("start netty client success, host={}, port={}", host, port); + } else { + LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); + } + }); + future.syncUninterruptibly(); + } + + public Bootstrap getBootstrap() { + return bootstrap; + } + + public NioEventLoopGroup getWorkerGroup() { + return workerGroup; + } } \ No newline at end of file diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index 284b7960..1808f02c 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -22,6 +22,7 @@ import com.mpush.api.service.Client; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.connect.ClientConfig; +import com.mpush.client.connect.ConnClientChannelHandler; import com.mpush.client.connect.ConnectClient; import com.mpush.common.security.CipherBox; import com.mpush.tools.log.Logs; @@ -34,6 +35,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; @@ -47,12 +49,10 @@ public static void main(String[] args) throws Exception { public void testConnClient() throws Exception { ConnClientBoot boot = new ConnClientBoot(); boot.start().get(); - List serverList = boot.getServers(); - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); - ZKServerNode server = serverList.get(index); - for (int i = 0; i < 1; i++) { + + for (int i = 0; i < 1000; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; @@ -69,9 +69,17 @@ public void testConnClient() throws Exception { config.setOsName(osName); config.setOsVersion(osVersion); config.setUserId(userId); - Client client = new ConnectClient(server.getExtranetIp(), server.getPort(), config); - client.start(); + + int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + ZKServerNode server = serverList.get(index); + boot.connect(server.getExtranetIp(), server.getPort(), config); + System.out.println("client num=" + i); } + + Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { + System.err.println(ConnClientChannelHandler.STATISTICS); + }, 0, 1, TimeUnit.SECONDS); + LockSupport.park(); } } diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index dc21b97d..0ee21d0b 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -142,7 +142,7 @@ - + From 78dd1d39e21a641fb5fc2dadd953a377e6da024f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 10:48:32 +0800 Subject: [PATCH 746/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/mpush/test/client/ConnClientTestMain.java | 1 + mpush-test/src/test/resources/logback.xml | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java index 1808f02c..968b7a28 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java @@ -47,6 +47,7 @@ public static void main(String[] args) throws Exception { @Test public void testConnClient() throws Exception { + Logs.init(); ConnClientBoot boot = new ConnClientBoot(); boot.start().get(); List serverList = boot.getServers(); diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/test/resources/logback.xml index 0ee21d0b..c72b5ece 100644 --- a/mpush-test/src/test/resources/logback.xml +++ b/mpush-test/src/test/resources/logback.xml @@ -1,6 +1,6 @@ - + System.err @@ -142,7 +142,7 @@ - + From 1b628238bb434e38af9dd651787f2d6c4fb6baf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 13:28:20 +0800 Subject: [PATCH 747/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/pom.xml | 36 +++++++++++++- .../java/com/mpush/netty/MulticastTest.java | 0 .../java/com/mpush/netty/MulticastTest2.java | 0 .../com/mpush/test/client/ConnClientBoot.java | 0 .../mpush/test/client/ConnClientTestMain.java | 47 ++++++++++++------- .../test/configcenter/ConfigCenterTest.java | 0 .../mpush/test/connection/body/BodyTest.java | 0 .../java/com/mpush/test/crypto/RsaTest.java | 0 .../com/mpush/test/gson/DnsMappingTest.java | 0 .../java/com/mpush/test/gson/GsonTest.java | 0 .../mpush/test/push/PushClientTestMain.java | 0 .../java/com/mpush/test/redis/PubSubTest.java | 0 .../mpush/test/redis/RedisClusterTest.java | 0 .../com/mpush/test/redis/RedisUtilTest.java | 0 .../java/com/mpush/test/redis/User.java | 0 .../com/mpush/test/sever/ServerTestMain.java | 0 .../java/com/mpush/test/util/IPTest.java | 0 .../java/com/mpush/test/util/TelnetTest.java | 0 .../{test => main}/resources/application.conf | 3 +- .../src/{test => main}/resources/logback.xml | 0 .../services/com.mpush.api.spi.PusherFactory | 0 21 files changed, 65 insertions(+), 21 deletions(-) rename mpush-test/src/{test => main}/java/com/mpush/netty/MulticastTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/netty/MulticastTest2.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/client/ConnClientBoot.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/client/ConnClientTestMain.java (69%) rename mpush-test/src/{test => main}/java/com/mpush/test/configcenter/ConfigCenterTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/connection/body/BodyTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/crypto/RsaTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/gson/DnsMappingTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/gson/GsonTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/push/PushClientTestMain.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/redis/PubSubTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/redis/RedisClusterTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/redis/RedisUtilTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/redis/User.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/sever/ServerTestMain.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/util/IPTest.java (100%) rename mpush-test/src/{test => main}/java/com/mpush/test/util/TelnetTest.java (100%) rename mpush-test/src/{test => main}/resources/application.conf (77%) rename mpush-test/src/{test => main}/resources/logback.xml (100%) rename mpush-test/src/{test => main}/resources/services/com.mpush.api.spi.PusherFactory (100%) diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index 065d3656..bf5d14d9 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -41,6 +41,40 @@ ${mpush.groupId} mpush-client + + junit + junit + compile + - + + + test + + + + maven-assembly-plugin + + + + com.mpush.test.client.ConnClientTestMain + + + + jar-with-dependencies + + + + + package + + single + + + + + + + + diff --git a/mpush-test/src/test/java/com/mpush/netty/MulticastTest.java b/mpush-test/src/main/java/com/mpush/netty/MulticastTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/netty/MulticastTest.java rename to mpush-test/src/main/java/com/mpush/netty/MulticastTest.java diff --git a/mpush-test/src/test/java/com/mpush/netty/MulticastTest2.java b/mpush-test/src/main/java/com/mpush/netty/MulticastTest2.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/netty/MulticastTest2.java rename to mpush-test/src/main/java/com/mpush/netty/MulticastTest2.java diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/client/ConnClientBoot.java rename to mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java diff --git a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java similarity index 69% rename from mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java rename to mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java index 968b7a28..78bd9d74 100644 --- a/mpush-test/src/test/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java @@ -19,21 +19,14 @@ package com.mpush.test.client; -import com.mpush.api.service.Client; -import com.mpush.cache.redis.manager.RedisManager; import com.mpush.client.connect.ClientConfig; import com.mpush.client.connect.ConnClientChannelHandler; -import com.mpush.client.connect.ConnectClient; import com.mpush.common.security.CipherBox; import com.mpush.tools.log.Logs; -import com.mpush.zk.ZKClient; -import com.mpush.zk.listener.ZKServerNodeWatcher; import com.mpush.zk.node.ZKServerNode; -import org.junit.After; -import org.junit.Before; +import org.apache.commons.lang3.math.NumberUtils; import org.junit.Test; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -42,18 +35,41 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { - new ConnClientTestMain().testConnClient(); + int count = 1, printDelay = 1; + if (args.length > 0) { + count = NumberUtils.toInt(args[0], count); + } + if (args.length > 1) { + printDelay = NumberUtils.toInt(args[1], printDelay); + } + + testConnClient(count, printDelay); } @Test public void testConnClient() throws Exception { + testConnClient(1, 1); + LockSupport.park(); + } + + private static void testConnClient(int count, int printDelay) throws Exception { Logs.init(); ConnClientBoot boot = new ConnClientBoot(); boot.start().get(); List serverList = boot.getServers(); + if (serverList.isEmpty()) { + boot.stop(); + System.out.println("no mpush server."); + return; + } + Executors + .newSingleThreadScheduledExecutor() + .scheduleAtFixedRate(() -> System.err.println(ConnClientChannelHandler.STATISTICS) + , 3, printDelay, TimeUnit.SECONDS + ); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < count; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; @@ -71,16 +87,11 @@ public void testConnClient() throws Exception { config.setOsVersion(osVersion); config.setUserId(userId); - int index = (int) ((Math.random() % serverList.size()) * serverList.size()); + int L = serverList.size(); + int index = (int) ((Math.random() % L) * L); ZKServerNode server = serverList.get(index); + boot.connect(server.getExtranetIp(), server.getPort(), config); - System.out.println("client num=" + i); } - - Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { - System.err.println(ConnClientChannelHandler.STATISTICS); - }, 0, 1, TimeUnit.SECONDS); - - LockSupport.park(); } } diff --git a/mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java b/mpush-test/src/main/java/com/mpush/test/configcenter/ConfigCenterTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/configcenter/ConfigCenterTest.java rename to mpush-test/src/main/java/com/mpush/test/configcenter/ConfigCenterTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java b/mpush-test/src/main/java/com/mpush/test/connection/body/BodyTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/connection/body/BodyTest.java rename to mpush-test/src/main/java/com/mpush/test/connection/body/BodyTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java b/mpush-test/src/main/java/com/mpush/test/crypto/RsaTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/crypto/RsaTest.java rename to mpush-test/src/main/java/com/mpush/test/crypto/RsaTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java b/mpush-test/src/main/java/com/mpush/test/gson/DnsMappingTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/gson/DnsMappingTest.java rename to mpush-test/src/main/java/com/mpush/test/gson/DnsMappingTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java b/mpush-test/src/main/java/com/mpush/test/gson/GsonTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/gson/GsonTest.java rename to mpush-test/src/main/java/com/mpush/test/gson/GsonTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/push/PushClientTestMain.java rename to mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java diff --git a/mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/main/java/com/mpush/test/redis/PubSubTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/redis/PubSubTest.java rename to mpush-test/src/main/java/com/mpush/test/redis/PubSubTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java b/mpush-test/src/main/java/com/mpush/test/redis/RedisClusterTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/redis/RedisClusterTest.java rename to mpush-test/src/main/java/com/mpush/test/redis/RedisClusterTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java b/mpush-test/src/main/java/com/mpush/test/redis/RedisUtilTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/redis/RedisUtilTest.java rename to mpush-test/src/main/java/com/mpush/test/redis/RedisUtilTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/redis/User.java b/mpush-test/src/main/java/com/mpush/test/redis/User.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/redis/User.java rename to mpush-test/src/main/java/com/mpush/test/redis/User.java diff --git a/mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/main/java/com/mpush/test/sever/ServerTestMain.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/sever/ServerTestMain.java rename to mpush-test/src/main/java/com/mpush/test/sever/ServerTestMain.java diff --git a/mpush-test/src/test/java/com/mpush/test/util/IPTest.java b/mpush-test/src/main/java/com/mpush/test/util/IPTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/util/IPTest.java rename to mpush-test/src/main/java/com/mpush/test/util/IPTest.java diff --git a/mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java b/mpush-test/src/main/java/com/mpush/test/util/TelnetTest.java similarity index 100% rename from mpush-test/src/test/java/com/mpush/test/util/TelnetTest.java rename to mpush-test/src/main/java/com/mpush/test/util/TelnetTest.java diff --git a/mpush-test/src/test/resources/application.conf b/mpush-test/src/main/resources/application.conf similarity index 77% rename from mpush-test/src/test/resources/application.conf rename to mpush-test/src/main/resources/application.conf index b6d47bf9..8034e981 100644 --- a/mpush-test/src/test/resources/application.conf +++ b/mpush-test/src/main/resources/application.conf @@ -1,5 +1,5 @@ mp.home=${user.dir}/target -mp.log-level=debug +mp.log-level=warn mp.log-conf-path=logback.xml mp.core.min-heartbeat=10s mp.core.max-heartbeat=10s @@ -9,4 +9,3 @@ mp.redis {// redis 集群配置 nodes:["127.0.0.1:6379"]//格式是ip:port,密码可以没有ip:port } mp.http.proxy-enabled=true -mp.net.gateway-server-net=udp //网关服务使用的网络类型tcp/udp diff --git a/mpush-test/src/test/resources/logback.xml b/mpush-test/src/main/resources/logback.xml similarity index 100% rename from mpush-test/src/test/resources/logback.xml rename to mpush-test/src/main/resources/logback.xml diff --git a/mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory b/mpush-test/src/main/resources/services/com.mpush.api.spi.PusherFactory similarity index 100% rename from mpush-test/src/test/resources/services/com.mpush.api.spi.PusherFactory rename to mpush-test/src/main/resources/services/com.mpush.api.spi.PusherFactory From cb1d10015a95f5fc86a03ea6174fab2ea642dc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 13:51:46 +0800 Subject: [PATCH 748/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/test/client/ConnClientBoot.java | 6 +++--- .../mpush/test/client/ConnClientTestMain.java | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java index c2c8fd18..c28c109e 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java @@ -102,17 +102,17 @@ public List getServers() { } - public void connect(String host, int port, ClientConfig clientConfig) { + public ChannelFuture connect(String host, int port, ClientConfig clientConfig) { ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - future.channel().attr(ConnClientChannelHandler.CONFIG_KEY).set(clientConfig); future.addListener(f -> { if (f.isSuccess()) { + future.channel().attr(ConnClientChannelHandler.CONFIG_KEY).set(clientConfig); LOGGER.info("start netty client success, host={}, port={}", host, port); } else { LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); } }); - future.syncUninterruptibly(); + return future; } public Bootstrap getBootstrap() { diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java index 78bd9d74..9c780e52 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java @@ -24,6 +24,8 @@ import com.mpush.common.security.CipherBox; import com.mpush.tools.log.Logs; import com.mpush.zk.node.ZKServerNode; +import io.netty.channel.ChannelFuture; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; import org.junit.Test; @@ -36,6 +38,7 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { int count = 1, printDelay = 1; + boolean sync = true; if (args.length > 0) { count = NumberUtils.toInt(args[0], count); } @@ -43,16 +46,20 @@ public static void main(String[] args) throws Exception { printDelay = NumberUtils.toInt(args[1], printDelay); } - testConnClient(count, printDelay); + if (args.length > 2) { + sync = !"1".equals(args[2]); + } + + testConnClient(count, printDelay, sync); } @Test public void testConnClient() throws Exception { - testConnClient(1, 1); + testConnClient(1, 1, true); LockSupport.park(); } - private static void testConnClient(int count, int printDelay) throws Exception { + private static void testConnClient(int count, int printDelay, boolean sync) throws Exception { Logs.init(); ConnClientBoot boot = new ConnClientBoot(); boot.start().get(); @@ -91,7 +98,8 @@ private static void testConnClient(int count, int printDelay) throws Exception { int index = (int) ((Math.random() % L) * L); ZKServerNode server = serverList.get(index); - boot.connect(server.getExtranetIp(), server.getPort(), config); + ChannelFuture future = boot.connect(server.getExtranetIp(), server.getPort(), config); + if (sync) future.awaitUninterruptibly(); } } } From c92f84e6c09fbe5e739b9d48998afcd35aad6417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 14:29:19 +0800 Subject: [PATCH 749/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/client/connect/ConnClientChannelHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index f8b3116e..2168b84c 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -50,7 +50,6 @@ * * @author ohun@live.cn */ -@ChannelHandler.Sharable public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientChannelHandler.class); private static final Timer HASHED_WHEEL_TIMER = new HashedWheelTimer(new NamedPoolThreadFactory(ThreadNames.T_CONN_TIMER)); From a6c7a90710acbeeb4045ed66a6c1aeb0630b2b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 14:47:29 +0800 Subject: [PATCH 750/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/test/client/ConnClientTestMain.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java index 9c780e52..2e4b94cc 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java @@ -37,7 +37,7 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { - int count = 1, printDelay = 1; + int count = 5000, printDelay = 1; boolean sync = true; if (args.length > 0) { count = NumberUtils.toInt(args[0], count); From 208c91aeaaf165eceea75aa7aed7a2a77aba0718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 16:59:32 +0800 Subject: [PATCH 751/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/client/connect/ConnClientChannelHandler.java | 10 ++++------ .../java/com/mpush/test/client/ConnClientBoot.java | 3 ++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 2168b84c..35b3152c 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -28,7 +28,6 @@ import com.mpush.api.protocol.Packet; import com.mpush.cache.redis.RedisKey; import com.mpush.cache.redis.manager.RedisManager; -import com.mpush.common.ErrorCode; import com.mpush.common.message.*; import com.mpush.common.security.AesCipher; import com.mpush.common.security.CipherBox; @@ -41,7 +40,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -58,10 +56,10 @@ public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter private final Connection connection = new NettyConnection(); private ClientConfig clientConfig; - private boolean stressingTest; + private boolean perfTest; public ConnClientChannelHandler() { - stressingTest = true; + perfTest = true; } public ConnClientChannelHandler(ClientConfig clientConfig) { @@ -87,7 +85,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception startHeartBeat(message.heartbeat); LOGGER.info(">>> handshake success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); bindUser(clientConfig); - if (!stressingTest) { + if (!perfTest) { saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); } } else if (command == Command.FAST_CONNECT) { @@ -156,7 +154,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { clientConfig = ctx.channel().attr(CONFIG_KEY).getAndRemove(); } connection.init(ctx.channel(), true); - if (stressingTest) { + if (perfTest) { handshake(); } else { tryFastConnect(); diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java index c28c109e..47b54a85 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java @@ -44,6 +44,7 @@ import java.net.InetSocketAddress; import java.util.List; +import java.util.concurrent.TimeUnit; public final class ConnClientBoot extends BaseService { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientBoot.class); @@ -76,7 +77,7 @@ public void onFailure(Throwable cause) { .option(ChannelOption.SO_REUSEADDR, true)// .option(ChannelOption.SO_KEEPALIVE, true)// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60 * 1000) .channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer() { // (4) From f0584d8bfe8e462cd2addf17e61d38dc8327dae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 20:20:47 +0800 Subject: [PATCH 752/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djedis=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=AF=BC=E8=87=B4=E7=9A=84=E8=AE=A2=E9=98=85bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/cache/redis/manager/RedisManager.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index cda33aaa..de194862 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -314,9 +314,15 @@ public void publish(String channel, T message) { } public void subscribe(final JedisPubSub pubsub, final String... channels) { - ThreadPoolManager.I.newThread(Arrays.toString(channels), (() -> call(jedis -> - Arrays.stream(channels).forEach(channel -> ((MultiKeyCommands) jedis).subscribe(pubsub, channel)) - ))).start(); + ThreadPoolManager.I.newThread(Arrays.toString(channels), + () -> call(jedis -> { + if (jedis instanceof MultiKeyCommands) { + Arrays.stream(channels).forEach(channel -> ((MultiKeyCommands) jedis).subscribe(pubsub, channel)); + } else if (jedis instanceof MultiKeyJedisClusterCommands) { + Arrays.stream(channels).forEach(channel -> ((MultiKeyJedisClusterCommands) jedis).subscribe(pubsub, channel)); + } + }) + ).start(); } /********************* From cad727b9703953b64a9526a0b8e2f9ce5793fe96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 22:29:17 +0800 Subject: [PATCH 753/890] =?UTF-8?q?=E5=8E=8B=E6=B5=8B=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E5=8E=8B=E6=B5=8B=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/connection/Connection.java | 6 ++- mpush-boot/src/main/resources/mpush.conf | 2 +- .../cache/redis/manager/RedisManager.java | 8 +-- .../connect/ConnClientChannelHandler.java | 54 +++++++++++++------ .../core/server/ServerConnectionManager.java | 4 +- .../netty/connection/NettyConnection.java | 15 +++--- .../com/mpush/test/client/ConnClientBoot.java | 7 +-- .../mpush/test/client/ConnClientTestMain.java | 9 ++-- .../src/main/java/com/mpush/tools/Utils.java | 2 +- 9 files changed, 64 insertions(+), 43 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index d6ef1598..8013a32e 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -50,11 +50,13 @@ public interface Connection { boolean isConnected(); - boolean heartbeatTimeout(); + boolean isReadTimeout(); + + boolean isWriteTimeout(); void updateLastReadTime(); - long getLastReadTime(); + void updateLastWriteTime(); Channel getChannel(); diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 21e42949..d65256d8 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -1,5 +1,5 @@ mp.log-level=${log.level} -mp.min-heartbeat=${min.hb} +mp.core.min-heartbeat=${min.hb} mp.security.private-key="${rsa.privateKey}" mp.security.public-key="${rsa.publicKey}" mp.zk.server-address="127.0.0.1:2181" diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index de194862..164b4fd6 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -313,13 +313,13 @@ public void publish(String channel, T message) { call(jedis -> ((MultiKeyCommands) jedis).publish(channel, message instanceof String ? (String) message : Jsons.toJson(message))); } - public void subscribe(final JedisPubSub pubsub, final String... channels) { - ThreadPoolManager.I.newThread(Arrays.toString(channels), + public void subscribe(final JedisPubSub pubsub, final String channel) { + ThreadPoolManager.I.newThread(channel, () -> call(jedis -> { if (jedis instanceof MultiKeyCommands) { - Arrays.stream(channels).forEach(channel -> ((MultiKeyCommands) jedis).subscribe(pubsub, channel)); + ((MultiKeyCommands) jedis).subscribe(pubsub, channel); } else if (jedis instanceof MultiKeyJedisClusterCommands) { - Arrays.stream(channels).forEach(channel -> ((MultiKeyJedisClusterCommands) jedis).subscribe(pubsub, channel)); + ((MultiKeyJedisClusterCommands) jedis).subscribe(pubsub, channel); } }) ).start(); diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 35b3152c..38d76c63 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -40,9 +40,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.time.Clock; +import java.time.ZonedDateTime; import java.util.Map; import java.util.concurrent.TimeUnit; + /** * Created by ohun on 2015/12/19. * @@ -57,6 +60,7 @@ public final class ConnClientChannelHandler extends ChannelInboundHandlerAdapter private final Connection connection = new NettyConnection(); private ClientConfig clientConfig; private boolean perfTest; + private int hbTimeoutTimes; public ConnClientChannelHandler() { perfTest = true; @@ -82,7 +86,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); - startHeartBeat(message.heartbeat); + connection.getSessionContext().setHeartbeat(message.heartbeat); + startHeartBeat(message.heartbeat - 1000); LOGGER.info(">>> handshake success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); bindUser(clientConfig); if (!perfTest) { @@ -97,7 +102,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().changeCipher(new AesCipher(key, iv)); FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); - startHeartBeat(message.heartbeat); + connection.getSessionContext().setHeartbeat(message.heartbeat); + startHeartBeat(message.heartbeat - 1000); bindUser(clientConfig); LOGGER.info(">>> fast connect success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); } else if (command == Command.KICK) { @@ -150,8 +156,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { int clientNum = STATISTICS.clientNum.incrementAndGet(); LOGGER.info("client connect channel={}, clientNum={}", ctx.channel(), clientNum); - if (clientConfig == null) { + while (clientConfig == null) { clientConfig = ctx.channel().attr(CONFIG_KEY).getAndRemove(); + if (clientConfig == null) TimeUnit.SECONDS.sleep(1); } connection.init(ctx.channel(), true); if (perfTest) { @@ -212,6 +219,7 @@ private void bindUser(ClientConfig client) { message.userId = client.getUserId(); message.tags = "test"; message.send(); + connection.getSessionContext().setUserId(client.getUserId()); LOGGER.debug("<<< send bind user message={}", message); } @@ -247,22 +255,34 @@ private void startHeartBeat(final int heartbeat) throws Exception { HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { - final TimerTask self = this; - final Channel channel = connection.getChannel(); - if (channel.isActive()) { - ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); - channelFuture.addListener((ChannelFutureListener) future -> { - if (!future.isSuccess()) { - LOGGER.debug("<<< send heartbeat ping... " + channel.remoteAddress().toString()); - } else { - LOGGER.warn("client send msg hb false:" + channel.remoteAddress().toString(), future.cause()); - } - HASHED_WHEEL_TIMER.newTimeout(self, heartbeat, TimeUnit.MILLISECONDS); - }); - } else { - LOGGER.error("connection was closed, connection={}", connection); + if (connection.isConnected() && healthCheck()) { + HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS); } } }, heartbeat, TimeUnit.MILLISECONDS); } + + private boolean healthCheck() { + + if (connection.isReadTimeout()) { + hbTimeoutTimes++; + LOGGER.warn("heartbeat timeout times={}, client={}", hbTimeoutTimes, connection); + } else { + hbTimeoutTimes = 0; + } + + if (hbTimeoutTimes >= 2) { + LOGGER.warn("heartbeat timeout times={} over limit={}, client={}", hbTimeoutTimes, 2, connection); + hbTimeoutTimes = 0; + connection.close(); + return false; + } + + if (connection.isWriteTimeout()) { + LOGGER.info("<<< send heartbeat ping..."); + connection.send(Packet.HB_PACKET); + } + + return true; + } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index e8d1f4b3..1c8de4ff 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -125,10 +125,10 @@ public void run(Timeout timeout) throws Exception { Logs.HB.info("connection was disconnected, heartbeat timeout times={}, connection={}", timeoutTimes, connection); return; } - if (connection.heartbeatTimeout()) { + if (connection.isReadTimeout()) { if (++timeoutTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); - Logs.HB.info("client heartbeat timeout times={}, do close connection={}", timeoutTimes, connection); + Logs.HB.warn("client heartbeat timeout times={}, do close connection={}", timeoutTimes, connection); return; } else { Logs.HB.info("client heartbeat timeout times={}, connection={}", timeoutTimes, connection); diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index b7ba976e..a4e48e2c 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -113,19 +113,18 @@ public boolean isConnected() { } @Override - public boolean heartbeatTimeout() { - long between = System.currentTimeMillis() - lastReadTime; - return context.heartbeat > 0 && between > context.heartbeat; + public boolean isReadTimeout() { + return System.currentTimeMillis() - lastReadTime > context.heartbeat + 1000; } @Override - public void updateLastReadTime() { - lastReadTime = System.currentTimeMillis(); + public boolean isWriteTimeout() { + return System.currentTimeMillis() - lastWriteTime > context.heartbeat - 1000; } @Override - public long getLastReadTime() { - return lastReadTime; + public void updateLastReadTime() { + lastReadTime = System.currentTimeMillis(); } @Override @@ -137,11 +136,11 @@ public void operationComplete(ChannelFuture future) throws Exception { } } + @Override public void updateLastWriteTime() { lastWriteTime = System.currentTimeMillis(); } - @Override public String toString() { return "NettyConnection [context=" + context diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java index 47b54a85..2ce0ff5d 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java @@ -38,13 +38,13 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.util.AttributeKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; import java.util.List; -import java.util.concurrent.TimeUnit; + +import static com.mpush.client.connect.ConnClientChannelHandler.CONFIG_KEY; public final class ConnClientBoot extends BaseService { private static final Logger LOGGER = LoggerFactory.getLogger(ConnClientBoot.class); @@ -105,9 +105,10 @@ public List getServers() { public ChannelFuture connect(String host, int port, ClientConfig clientConfig) { ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + if (future.channel() != null) future.channel().attr(CONFIG_KEY).set(clientConfig); future.addListener(f -> { if (f.isSuccess()) { - future.channel().attr(ConnClientChannelHandler.CONFIG_KEY).set(clientConfig); + future.channel().attr(CONFIG_KEY).set(clientConfig); LOGGER.info("start netty client success, host={}, port={}", host, port); } else { LOGGER.error("start netty client failure, host={}, port={}", host, port, f.cause()); diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java index 2e4b94cc..d1ac4aa0 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java @@ -25,7 +25,6 @@ import com.mpush.tools.log.Logs; import com.mpush.zk.node.ZKServerNode; import io.netty.channel.ChannelFuture; -import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; import org.junit.Test; @@ -37,7 +36,7 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { - int count = 5000, printDelay = 1; + int count = 10, printDelay = 1; boolean sync = true; if (args.length > 0) { count = NumberUtils.toInt(args[0], count); @@ -70,9 +69,9 @@ private static void testConnClient(int count, int printDelay, boolean sync) thro return; } - Executors - .newSingleThreadScheduledExecutor() - .scheduleAtFixedRate(() -> System.err.println(ConnClientChannelHandler.STATISTICS) + Executors.newSingleThreadScheduledExecutor() + .scheduleAtFixedRate( + () -> System.err.println(ConnClientChannelHandler.STATISTICS) , 3, printDelay, TimeUnit.SECONDS ); diff --git a/mpush-tools/src/main/java/com/mpush/tools/Utils.java b/mpush-tools/src/main/java/com/mpush/tools/Utils.java index 33125d67..c79ed447 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/Utils.java +++ b/mpush-tools/src/main/java/com/mpush/tools/Utils.java @@ -113,7 +113,7 @@ public static String getInetAddress(boolean getLocal) { } } } - LOGGER.warn("getInetAddress is null"); + LOGGER.debug("getInetAddress is null"); return getLocal ? "127.0.0.1" : null; } catch (Throwable e) { LOGGER.error("getInetAddress exception", e); From 9de88b9ad593fa3bc594ac9eab4e8c29e14e8c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 8 Dec 2016 22:34:59 +0800 Subject: [PATCH 754/890] =?UTF-8?q?=E5=8E=8B=E6=B5=8B=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E5=8E=8B=E6=B5=8B=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/client/connect/ConnClientChannelHandler.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 38d76c63..9458adc6 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -35,13 +35,12 @@ import com.mpush.tools.event.EventBus; import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.ThreadNames; -import io.netty.channel.*; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.time.Clock; -import java.time.ZonedDateTime; import java.util.Map; import java.util.concurrent.TimeUnit; From d5db5d7e140bd4fe27c652ccf575ca3ba36b7970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 11:31:44 +0800 Subject: [PATCH 755/890] =?UTF-8?q?zookeeper=20client=20=E5=8D=87=E7=BA=A7?= =?UTF-8?q?2.11.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index e6b8fb9d..da3ca408 100644 --- a/pom.xml +++ b/pom.xml @@ -221,7 +221,7 @@ org.apache.curator curator-recipes - 2.9.1 + 2.11.1 netty @@ -232,7 +232,7 @@ org.apache.curator curator-x-discovery - 2.9.1 + 2.11.1 From 7b5a934c2d170f98e8c6d35229576bc6dac88b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 13:18:58 +0800 Subject: [PATCH 756/890] =?UTF-8?q?=E7=B1=BB=E5=BA=93=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index da3ca408..c41c7576 100644 --- a/pom.xml +++ b/pom.xml @@ -207,11 +207,18 @@ 4.10 test + + + org.apache.commons + commons-collections4 + 4.1 + org.apache.commons commons-lang3 - 3.4 + 3.5 + com.google.guava guava @@ -244,19 +251,13 @@ com.google.code.gson gson - 2.7 + 2.8.0 com.typesafe config - 1.3.0 - - - - org.apache.commons - commons-collections4 - 4.1 + 1.3.1 From e940987980d7149b7e847de70a40993bc7f68d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 13:41:17 +0800 Subject: [PATCH 757/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djedis=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=AF=BC=E8=87=B4=E7=9A=84=E8=AE=A2=E9=98=85bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/cache/redis/manager/RedisManager.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index 164b4fd6..f9529250 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -310,7 +310,14 @@ public void lRem(String key, String value) { public void publish(String channel, T message) { - call(jedis -> ((MultiKeyCommands) jedis).publish(channel, message instanceof String ? (String) message : Jsons.toJson(message))); + String msg = message instanceof String ? (String) message : Jsons.toJson(message); + call(jedis -> { + if (jedis instanceof MultiKeyCommands) { + ((MultiKeyCommands) jedis).publish(channel, msg); + } else if (jedis instanceof MultiKeyJedisClusterCommands) { + ((MultiKeyJedisClusterCommands) jedis).publish(channel, msg); + } + }); } public void subscribe(final JedisPubSub pubsub, final String channel) { From 0636b400bc454d925c868ea1881bb4363ec87961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 13:42:49 +0800 Subject: [PATCH 758/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djedis=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=AF=BC=E8=87=B4=E7=9A=84=E8=AE=A2=E9=98=85bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/cache/redis/manager/RedisManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index f9529250..eaf43fcb 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -59,13 +59,13 @@ private R call(Function function, R d) { try { return function.apply(factory.getClusterConnection()); } catch (Exception e) { - Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + Logs.REDIS.error("redis ex", e); } } else { try (Jedis jedis = factory.getJedisConnection()) { return function.apply(jedis); } catch (Exception e) { - Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + Logs.REDIS.error("redis ex", e); } } return d; @@ -76,13 +76,13 @@ private void call(Consumer consumer) { try { consumer.accept(factory.getClusterConnection()); } catch (Exception e) { - Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + Logs.REDIS.error("redis ex", e); } } else { try (Jedis jedis = factory.getJedisConnection()) { consumer.accept(jedis); } catch (Exception e) { - Logs.REDIS.error("redis ex:{}, {}, {}, {}", e); + Logs.REDIS.error("redis ex", e); } } } From 9f4a555d6a9085d8ccddf397d4db7e79941d460c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 23:18:36 +0800 Subject: [PATCH 759/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9connId=E4=B8=BA?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=94=AF=E4=B8=80ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/core/server/ServerConnectionManager.java | 6 +++--- .../java/com/mpush/netty/connection/NettyConnection.java | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 1c8de4ff..d34e849c 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -77,18 +77,18 @@ public void destroy() { @Override public Connection get(final Channel channel) { - return connections.get(channel.id().asShortText()); + return connections.get(channel.id().asLongText()); } @Override public void add(Connection connection) { - connections.putIfAbsent(connection.getChannel().id().asShortText(), connection); + connections.putIfAbsent(connection.getChannel().id().asLongText(), connection); if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); } @Override public Connection removeAndClose(Channel channel) { - Connection connection = connections.remove(channel.id().asShortText()); + Connection connection = connections.remove(channel.id().asLongText()); if (connection != null) { connection.close(); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index a4e48e2c..40f2378e 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -25,6 +25,7 @@ import com.mpush.api.protocol.Packet; import com.mpush.api.spi.core.CipherFactory; import com.mpush.netty.codec.PacketEncoder; +import com.mpush.tools.log.Logs; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -69,7 +70,7 @@ public SessionContext getSessionContext() { @Override public String getId() { - return channel.id().asShortText(); + return channel.id().asLongText(); } @Override @@ -133,6 +134,7 @@ public void operationComplete(ChannelFuture future) throws Exception { lastWriteTime = System.currentTimeMillis(); } else { LOGGER.error("connection send msg error", future.cause()); + Logs.CONN.error("connection send msg error={}, conn={}", future.cause().getMessage(), this); } } @@ -143,8 +145,8 @@ public void updateLastWriteTime() { @Override public String toString() { - return "NettyConnection [context=" + context - + ", channel=" + channel + return "[channel=" + channel + + ", context=" + context + ", status=" + status + ", lastReadTime=" + lastReadTime + ", lastWriteTime=" + lastWriteTime @@ -156,5 +158,4 @@ public Channel getChannel() { return channel; } - } From 1437f270fa186c718b834e8689c15ac9feed99f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 23:21:22 +0800 Subject: [PATCH 760/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 +- .../mpush/api/connection/SessionContext.java | 15 ++++++------ .../java/com/mpush/api/protocol/Packet.java | 2 +- .../handler/GatewayClientChannelHandler.java | 6 ++--- .../com/mpush/common/MessageDispatcher.java | 6 ++++- .../mpush/core/handler/BindUserHandler.java | 18 +++++++-------- .../core/handler/FastConnectHandler.java | 10 ++++---- .../mpush/core/handler/HandshakeHandler.java | 6 ++--- .../mpush/core/handler/HttpProxyHandler.java | 16 ++++++++----- .../mpush/core/push/BroadcastPushTask.java | 8 +++---- .../mpush/core/push/SingleUserPushTask.java | 23 ++++++++++--------- .../core/router/RouterChangeListener.java | 19 ++++++++------- .../core/server/ServerChannelHandler.java | 21 ++++++----------- .../mpush/netty/http/HttpClientHandler.java | 4 +++- .../mpush/netty/udp/UDPChannelHandler.java | 10 ++++---- .../main/java/com/mpush/tools/config/CC.java | 2 +- .../main/java/com/mpush/tools/log/Logs.java | 2 +- .../src/main/java/com/mpush/zk/ZKClient.java | 6 ++--- .../src/main/java/com/mpush/zk/ZKConfig.java | 14 +++++------ 19 files changed, 97 insertions(+), 93 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index a3b79c97..2fe3ed7b 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -86,7 +86,7 @@ mp { server-address="127.0.0.1:2181" namespace=mpush digest=mpush //zkCli.sh acl 命令 addauth digest mpush - local-cache-path=/ + watch-path=/mpush retry { #initial amount of time to wait between retries baseSleepTimeMs=3s diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index b965ffb7..ff9ceab0 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -86,12 +86,13 @@ public int getClientType() { @Override public String toString() { - return "SessionContext [osName=" + osName - + ", osVersion=" + osVersion - + ", clientVersion=" + clientVersion - + ", deviceId=" + deviceId - + ", heartbeat=" + heartbeat - + "]"; + return "{" + + "osName='" + osName + '\'' + + ", osVersion='" + osVersion + '\'' + + ", deviceId='" + deviceId + '\'' + + ", userId='" + userId + '\'' + + ", tags='" + tags + '\'' + + ", heartbeat=" + heartbeat + + '}'; } - } diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index f2ca0154..d84422db 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -125,7 +125,7 @@ public Packet response(Command command) { @Override public String toString() { - return "Packet{" + + return "{" + "cmd=" + cmd + ", cc=" + cc + ", flags=" + flags + diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java index 31d369cf..1f01ad6b 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java @@ -51,7 +51,7 @@ public GatewayClientChannelHandler(PacketReceiver receiver) { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Logs.Conn.info("receive gateway packet={}, channel={}", msg, ctx.channel()); + Logs.CONN.info("receive gateway packet={}, channel={}", msg, ctx.channel()); Packet packet = (Packet) msg; receiver.onReceive(packet, connection); } @@ -64,14 +64,14 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client connect channel={}", ctx.channel()); + Logs.CONN.info("client connect channel={}", ctx.channel()); connection.init(ctx.channel(), false); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { connection.close(); - Logs.Conn.info("client disconnect channel={}", ctx.channel()); + Logs.CONN.info("client disconnect channel={}", ctx.channel()); } public Connection getConnection() { diff --git a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java index c9056c5a..f3fedd27 100644 --- a/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java +++ b/mpush-common/src/main/java/com/mpush/common/MessageDispatcher.java @@ -26,6 +26,7 @@ import com.mpush.api.protocol.Packet; import com.mpush.common.message.ErrorMessage; import com.mpush.tools.common.Profiler; +import com.mpush.tools.log.Logs; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,6 +72,8 @@ public void onReceive(Packet packet, Connection connection) { } catch (Throwable throwable) { LOGGER.error("dispatch message ex, packet={}, connect={}, body={}" , packet, connection, Arrays.toString(packet.body), throwable); + Logs.CONN.error("dispatch message ex, packet={}, connect={}, body={}, error={}" + , packet, connection, Arrays.toString(packet.body), throwable.getMessage()); ErrorMessage .from(packet, connection) .setErrorCode(DISPATCH_ERROR) @@ -80,7 +83,8 @@ public void onReceive(Packet packet, Connection connection) { } } else { if (unsupportedPolicy > POLICY_IGNORE) { - LOGGER.error("dispatch message failure unsupported cmd, packet={}, connect={}, body={}", packet, connection); + Logs.CONN.error("dispatch message failure, cmd={} unsupported, packet={}, connect={}, body={}" + , Command.toCMD(packet.cmd), packet, connection); if (unsupportedPolicy == POLICY_REJECT) { ErrorMessage .from(packet, connection) diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 8946bc38..67dab5b0 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -66,7 +66,7 @@ public void handle(BindUserMessage message) { private void bind(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - Logs.Conn.info("bind user failure for invalid param, session={}", message.getConnection().getSessionContext()); + Logs.CONN.error("bind user failure for invalid param, connection={}", message.getConnection()); return; } //1.绑定用户时先看下是否握手成功 @@ -77,7 +77,7 @@ private void bind(BindUserMessage message) { if (message.userId.equals(context.userId)) { context.tags = message.tags; OkMessage.from(message).setData("bind success").sendRaw(); - Logs.Conn.info(">>> rebind user success, userId={}, session={}", message.userId, context); + Logs.CONN.info(">>> rebind user success, userId={}, session={}", message.userId, context); return; } else { unbind(message); @@ -90,16 +90,16 @@ private void bind(BindUserMessage message) { context.tags = message.tags; EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").sendRaw(); - Logs.Conn.info(">>> bind user success, userId={}, session={}", message.userId, context); + Logs.CONN.info(">>> bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.I.unRegister(message.userId, context.getClientType()); ErrorMessage.from(message).setReason("bind failed").close(); - Logs.Conn.info("bind user failure, userId={}, session={}", message.userId, context); + Logs.CONN.info("bind user failure, userId={}, session={}", message.userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - Logs.Conn.info("bind user failure for not handshake, userId={}, session={}", message.userId, context); + Logs.CONN.error("bind user failure not handshake, userId={}, connection={}", message.userId, message.getConnection()); } } @@ -112,7 +112,7 @@ private void bind(BindUserMessage message) { private void unbind(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - Logs.Conn.info("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); + Logs.CONN.error("unbind user failure invalid param, session={}", message.getConnection().getSessionContext()); return; } //1.解绑用户时先看下是否握手成功 @@ -146,14 +146,14 @@ private void unbind(BindUserMessage message) { context.tags = null; EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").sendRaw(); - Logs.Conn.info(">>> unbind user success, userId={}, session={}", userId, context); + Logs.CONN.info(">>> unbind user success, userId={}, session={}", userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").sendRaw(); - Logs.Conn.info("unbind user failure, register router failure, userId={}, session={}", userId, context); + Logs.CONN.error("unbind user failure, unRegister router failure, userId={}, session={}", userId, context); } } else { ErrorMessage.from(message).setReason("not handshake").close(); - Logs.Conn.info("unbind user failure not handshake, userId={}, session={}", message.userId, context); + Logs.CONN.error("unbind user failure not handshake, userId={}, session={}", message.userId, context); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index 46f36457..e4cd4164 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -52,13 +52,13 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - Logs.Conn.info("fast connect failure, session is expired, gatewayMessageId={}, deviceId={}" - , message.sessionId, message.deviceId); + Logs.CONN.warn("fast connect failure, session is expired, sessionId={}, deviceId={}, connection={}" + , message.sessionId, message.deviceId, message.getConnection().getChannel()); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - Logs.Conn.info("fast connect failure, not the same device, deviceId={}, session={}" - , message.deviceId, session.context); + Logs.CONN.warn("fast connect failure, not the same device, deviceId={}, session={}, connection={}" + , message.deviceId, session.context, message.getConnection().getChannel()); } else { //3.校验成功,重新计算心跳,完成快速重连 int heartbeat = ConfigManager.I.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); @@ -71,7 +71,7 @@ public void handle(FastConnectMessage message) { .setHeartbeat(heartbeat) .sendRaw(); Profiler.release(); - Logs.Conn.info(">>> fast connect success, session={}", session.context); + Logs.CONN.info(">>> fast connect success, session={}", session.context); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index f638297d..86adfb27 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -61,14 +61,14 @@ public void handle(HandshakeMessage message) { || iv.length != CipherBox.I.getAesKeyLength() || clientKey.length != CipherBox.I.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - Logs.Conn.info("handshake failure, message={}", message.toString()); + Logs.CONN.error("handshake failure, message={}, connection={}", message, message.getConnection()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - Logs.Conn.info("handshake failure, repeat handshake, session={}", message.getConnection().getSessionContext()); + Logs.CONN.warn("handshake failure, repeat handshake, session={}", message.getConnection().getSessionContext()); return; } @@ -105,6 +105,6 @@ public void handle(HandshakeMessage message) { //10.触发握手成功事件 EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); - Logs.Conn.info(">>> handshake success, session={}", context); + Logs.CONN.info(">>> handshake success, connection={}", message.getConnection()); } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java index a7236daf..79c3297d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HttpProxyHandler.java @@ -36,6 +36,7 @@ import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.*; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; import java.net.MalformedURLException; @@ -51,7 +52,7 @@ * @author ohun@live.cn */ public class HttpProxyHandler extends BaseMessageHandler { - private static final Logger LOGGER = Logs.HTTP; + private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyHandler.class); private final HttpClient httpClient = NettyHttpClient.I(); private final DnsMappingManager dnsMappingManager = DnsMappingManager.create(); @@ -72,7 +73,7 @@ public void handle(HttpRequestMessage message) { .setStatusCode(400) .setReasonPhrase("Bad Request") .sendRaw(); - LOGGER.warn("request url is empty!"); + Logs.HTTP.warn("receive bad request url is empty, request={}", message); } //2.url转换 @@ -94,6 +95,7 @@ public void handle(HttpRequestMessage message) { .setReasonPhrase("Bad Gateway") .sendRaw(); LOGGER.error("send request ex, message=" + message, e); + Logs.HTTP.error("send proxy request ex, request={}, error={}", message, e.getMessage()); } finally { Profiler.release(); } @@ -127,7 +129,7 @@ public void onResponse(HttpResponse httpResponse) { } } response.send(); - LOGGER.debug("<<< callback success request={}, response={}", request, response); + Logs.HTTP.info("send proxy request success end request={}, response={}", request, response); } @Override @@ -137,7 +139,7 @@ public void onFailure(int statusCode, String reasonPhrase) { .setStatusCode(statusCode) .setReasonPhrase(reasonPhrase) .sendRaw(); - LOGGER.warn("callback failure request={}, response={}", request, statusCode + ":" + reasonPhrase); + Logs.HTTP.warn("send proxy request failure end request={}, response={}", request, statusCode + ":" + reasonPhrase); } @Override @@ -147,7 +149,9 @@ public void onException(Throwable throwable) { .setStatusCode(502) .setReasonPhrase("Bad Gateway") .sendRaw(); - LOGGER.error("callback exception request={}, response={}", request, 502, throwable); + + LOGGER.error("send proxy request ex end request={}, response={}", request, 502, throwable); + Logs.HTTP.error("send proxy request ex end request={}, response={}, error={}", request, 502, throwable.getMessage()); } @Override @@ -157,7 +161,7 @@ public void onTimeout() { .setStatusCode(408) .setReasonPhrase("Request Timeout") .sendRaw(); - LOGGER.warn("callback timeout request={}, response={}", request, 408); + Logs.HTTP.warn("send proxy request timeout end request={}, response={}", request, 408); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java index 7bc99775..699fee26 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java @@ -98,7 +98,7 @@ private boolean broadcast() { } } } else { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 - Logs.PUSH.info("gateway broadcast, router in local but disconnect, message={}", message); + Logs.PUSH.warn("[broadcast] find router in local but conn disconnect, message={}, conn={}", message, connection); //删除已经失效的本地路由 RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); } @@ -115,7 +115,7 @@ private boolean broadcast() { } private void report() { - Logs.PUSH.info("gateway broadcast finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); + Logs.PUSH.info("[broadcast] task finished, cost={}, message={}", (System.currentTimeMillis() - begin), message); OkMessage.from(message).sendRaw();//通知发送方,广播推送完毕 } @@ -134,9 +134,9 @@ private boolean checkCondition(Condition condition, Connection connection) { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) {//推送成功 - Logs.PUSH.info("<<< gateway broadcast client success, userId={}, message={}", message.userId, message); + Logs.PUSH.info("[broadcast] push message to client success, userId={}, message={}", message.userId, message); } else {//推送失败 - Logs.PUSH.info("gateway broadcast client failure, userId={}, message={}", message.userId, message); + Logs.PUSH.warn("[broadcast] push message to client failure, userId={}, message={}, conn={}", message.userId, message, future.channel()); } if (finishTasks.decrementAndGet() == 0) { report(); diff --git a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java index 3120dbce..8e2df4bf 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java @@ -82,7 +82,7 @@ private boolean checkLocal(final GatewayPushMessage message) { //2.如果链接失效,先删除本地失效的路由,再查下远程路由,看用户是否登陆到其他机器 if (!connection.isConnected()) { - Logs.PUSH.info("gateway push, router in local but disconnect, message={}", message, connection); + Logs.PUSH.warn("[SingleUserPush] find local router but conn disconnected, message={}, conn={}", message, connection); //删除已经失效的本地路由 RouterCenter.I.getLocalRouterManager().unRegister(userId, clientType); @@ -94,7 +94,7 @@ private boolean checkLocal(final GatewayPushMessage message) { if (!connection.getChannel().isWritable()) { ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); - Logs.PUSH.info("gateway push message to client failure, send too busy, message={}", message); + Logs.PUSH.error("[SingleUserPush] push message to client failure, tcp sender too busy, message={}, conn={}", message, connection); return true; } @@ -112,13 +112,13 @@ private boolean checkLocal(final GatewayPushMessage message) { OkMessage.from(message).setData(userId + ',' + clientType).sendRaw(); } - Logs.PUSH.info("<<< gateway push message to client success, message={}", message); + Logs.PUSH.info("[SingleUserPush] push message to client success, message={}", message); } else {//推送失败 ErrorMessage.from(message).setErrorCode(PUSH_CLIENT_FAILURE).setData(userId + ',' + clientType).sendRaw(); - Logs.PUSH.info("gateway push message to client failure, message={}", message); + Logs.PUSH.error("[SingleUserPush] push message to client failure, message={}, conn={}", message, connection); } }); } else { @@ -145,7 +145,7 @@ private void checkRemote(GatewayPushMessage message) { ErrorMessage.from(message).setErrorCode(OFFLINE).setData(userId + ',' + clientType).sendRaw(); - Logs.PUSH.info("gateway push, router not exists user offline, message={}", message); + Logs.PUSH.info("[SingleUserPush] remote router not exists user offline, message={}", message); return; } @@ -158,7 +158,8 @@ private void checkRemote(GatewayPushMessage message) { //删除失效的远程缓存 RouterCenter.I.getRemoteRouterManager().unRegister(userId, clientType); - Logs.PUSH.info("gateway push error remote is local, userId={}, clientType={}, router={}", userId, clientType, remoteRouter); + Logs.PUSH.info("[SingleUserPush] find remote router in this pc, but local router not exists, userId={}, clientType={}, router={}" + , userId, clientType, remoteRouter); return; } @@ -166,7 +167,7 @@ private void checkRemote(GatewayPushMessage message) { //3.否则说明用户已经跑到另外一台机器上了;路由信息发生更改,让PushClient重推 ErrorMessage.from(message).setErrorCode(ROUTER_CHANGE).setData(userId + ',' + clientType).sendRaw(); - Logs.PUSH.info("gateway push, router in remote userId={}, clientType={}, router={}", userId, clientType, remoteRouter); + Logs.PUSH.info("[SingleUserPush] find router in another pc, userId={}, clientType={}, router={}", userId, clientType, remoteRouter); } @@ -178,27 +179,27 @@ private AckContext buildAckContext(GatewayPushMessage message) { @Override public void onSuccess(AckContext ctx) { if (!message.getConnection().isConnected()) { - Logs.PUSH.info(">>> receive client ack, gateway connection is closed, context={}", ctx); + Logs.PUSH.warn("receive client ack, gateway connection is closed, context={}", ctx); return; } OkMessage okMessage = OkMessage.from(message); okMessage.setData(message.userId + ',' + message.clientType); okMessage.sendRaw(); - Logs.PUSH.info(">>> receive client ack and response gateway client success, context={}", ctx); + Logs.PUSH.info("receive client ack and response gateway client success, context={}", ctx); } @Override public void onTimeout(AckContext ctx) { if (!message.getConnection().isConnected()) { - Logs.PUSH.info("push message timeout client not ack, gateway connection is closed, context={}", ctx); + Logs.PUSH.warn("push message timeout client not ack, gateway connection is closed, context={}", ctx); return; } ErrorMessage errorMessage = ErrorMessage.from(message); errorMessage.setData(message.userId + ',' + message.clientType); errorMessage.setErrorCode(ErrorCode.ACK_TIMEOUT); errorMessage.sendRaw(); - Logs.PUSH.info("push message timeout client not ack, context={}", ctx); + Logs.PUSH.warn("push message timeout client not ack, context={}", ctx); } }); } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index b689cc99..78e0ec1f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -38,7 +38,6 @@ import com.mpush.tools.config.CC; import com.mpush.tools.event.EventConsumer; import com.mpush.tools.log.Logs; -import com.mpush.zk.node.ZKServerNode; import java.net.InetSocketAddress; @@ -90,9 +89,9 @@ private void kickLocal(final String userId, final LocalRouter router) { message.userId = userId; message.send(future -> { if (future.isSuccess()) { - Logs.Conn.info("kick local connection success, userId={}, router={}", userId, router); + Logs.CONN.info("kick local connection success, userId={}, router={}, conn={}", userId, router, connection); } else { - Logs.Conn.info("kick local connection failure, userId={}, router={}", userId, router); + Logs.CONN.warn("kick local connection failure, userId={}, router={}, conn={}", userId, router, connection); } }); } @@ -110,7 +109,7 @@ private void kickRemote(String userId, RemoteRouter remoteRouter) { ClientLocation location = remoteRouter.getRouteValue(); //1.如果目标机器是当前机器,就不要再发送广播了,直接忽略 if (location.isThisPC(GS_NODE.getIp(), GS_NODE.getPort())) { - Logs.Conn.info("kick remote user but router in local, ignore remote broadcast, userId={}", userId); + Logs.CONN.debug("kick remote router in local pc, ignore remote broadcast, userId={}", userId); return; } @@ -150,7 +149,7 @@ private void kickRemote(String userId, RemoteRouter remoteRouter) { public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { //1.如果当前机器不是目标机器,直接忽略 if (!msg.isTargetPC()) { - Logs.Conn.info("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); + Logs.CONN.error("receive kick remote msg, target server error, localIp={}, msg={}", Utils.getLocalIp(), msg); return; } @@ -160,17 +159,17 @@ public void onReceiveKickRemoteMsg(KickRemoteMsg msg) { LocalRouterManager localRouterManager = RouterCenter.I.getLocalRouterManager(); LocalRouter localRouter = localRouterManager.lookup(userId, clientType); if (localRouter != null) { - Logs.Conn.info("receive kick remote msg, msg={}", msg); + Logs.CONN.info("receive kick remote msg, msg={}", msg); if (localRouter.getRouteValue().getId().equals(msg.getConnId())) {//二次校验,防止误杀 //2.1删除本地路由信息 localRouterManager.unRegister(userId, clientType); //2.2发送踢人消息到客户端 kickLocal(userId, localRouter); } else { - Logs.Conn.warn("kick router connId was wrong, localRouter={}, msg={}", localRouter, msg); + Logs.CONN.warn("kick router failure target connId not match, localRouter={}, msg={}", localRouter, msg); } } else { - Logs.Conn.info("no local router find, kick failure, msg={}", msg); + Logs.CONN.warn("kick router failure can't find local router, msg={}", msg); } } @@ -181,10 +180,10 @@ public void onMessage(String channel, String message) { if (msg != null) { onReceiveKickRemoteMsg(msg); } else { - Logs.Conn.info("receive an error kick message={}", message); + Logs.CONN.warn("receive an error kick message={}", message); } } else { - Logs.Conn.info("receive an error redis channel={}", channel); + Logs.CONN.warn("receive an error redis channel={}", channel); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index bf6c0e67..355cd4bd 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -48,10 +48,8 @@ public final class ServerChannelHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); private static final long profile_slowly_limit = CC.mp.monitor.profile_slowly_duration.toMillis(); - /** - * 是否启用加密 - */ - private final boolean security; + + private final boolean security; //是否启用加密 private final ConnectionManager connectionManager; private final PacketReceiver receiver; @@ -69,7 +67,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception try { Profiler.start("time cost on [channel read]: " + packet.toString()); Connection connection = connectionManager.get(ctx.channel()); - LOGGER.debug("channelRead channel={}, connection={}, packet={}", ctx.channel(), connection.getSessionContext(), msg); + LOGGER.debug("channelRead conn={}, packet={}", ctx.channel(), connection.getSessionContext(), msg); connection.updateLastReadTime(); receiver.onReceive(packet, connection); } finally { @@ -84,13 +82,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { Connection connection = connectionManager.removeAndClose(ctx.channel()); - Logs.Conn.error("client exceptionCaught channel={}, connection={}", ctx.channel(), connection); - LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); + Logs.CONN.error("client caught ex, conn={}", connection); + LOGGER.error("caught an ex, channel={}, conn={}", ctx.channel(), connection, cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - Logs.Conn.info("client connect channel={}", ctx.channel()); + Logs.CONN.info("client connected conn={}", ctx.channel()); Connection connection = new NettyConnection(); connection.init(ctx.channel(), security); connectionManager.add(connection); @@ -100,11 +98,6 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception { Connection connection = connectionManager.removeAndClose(ctx.channel()); EventBus.I.post(new ConnectionCloseEvent(connection)); - Logs.Conn.info("client disconnect channel={}, connection={}", ctx.channel(), connection); - } - - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - super.userEventTriggered(ctx, evt); + Logs.CONN.info("client disconnected conn={}", connection); } } \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java index 5fde47f8..5246e7f8 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/HttpClientHandler.java @@ -40,6 +40,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception RequestContext context = ctx.channel().attr(client.requestKey).getAndRemove(); try { if (context != null && context.tryDone()) { + LOGGER.debug("receive server response, request={}, response={}", context, msg); HttpResponse response = (HttpResponse) msg; if (isRedirect(response)) { if (context.onRedirect(response)) { @@ -53,7 +54,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } context.onResponse(response); - LOGGER.debug("request done request={}", context); + } else { + LOGGER.warn("receive server response but timeout, request={}, response={}", context, msg); } } finally { client.pool.tryRelease(ctx.channel()); diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java index 616e8b23..915db217 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java @@ -59,13 +59,13 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { if (multicastAddress != null) { ((DatagramChannel) ctx.channel()).joinGroup(multicastAddress, networkInterface, null).addListener(future -> { if (future.isSuccess()) { - Logs.Conn.info("join multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); + Logs.CONN.info("join multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); } else { LOGGER.error("join multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); } }); } - Logs.Conn.info("init udp channel={}", ctx.channel()); + Logs.CONN.info("init udp channel={}", ctx.channel()); } @Override @@ -74,13 +74,13 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (multicastAddress != null) { ((DatagramChannel) ctx.channel()).leaveGroup(multicastAddress, networkInterface, null).addListener(future -> { if (future.isSuccess()) { - Logs.Conn.info("leave multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); + Logs.CONN.info("leave multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); } else { LOGGER.error("leave multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); } }); } - Logs.Conn.info("disconnect udp channel={}, connection={}", ctx.channel(), connection); + Logs.CONN.info("disconnect udp channel={}, connection={}", ctx.channel(), connection); } @Override @@ -94,7 +94,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connection.close(); - Logs.Conn.error("udp channel caught an exception channel={}, connection={}", ctx.channel(), connection); + Logs.CONN.error("udp channel caught an exception channel={}, connection={}", ctx.channel(), connection); LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index f2e8317c..5a3e23ee 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -234,7 +234,7 @@ interface zk { int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS); - String local_cache_path = cfg.getString("local-cache-path"); + String watch_path = cfg.getString("watch-path"); int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS); diff --git a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java index b6d1784c..3c5d229f 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java +++ b/mpush-tools/src/main/java/com/mpush/tools/log/Logs.java @@ -47,7 +47,7 @@ static boolean init() { Logger Console = LoggerFactory.getLogger("console"), - Conn = LoggerFactory.getLogger("mpush.conn.log"), + CONN = LoggerFactory.getLogger("mpush.conn.log"), MONITOR = LoggerFactory.getLogger("mpush.monitor.log"), diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 74c07074..37e3da8e 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -73,7 +73,7 @@ protected void doStart(Listener listener) throws Throwable { if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { throw new ZKException("init zk error, config=" + zkConfig); } - initLocalCache(zkConfig.getLocalCachePath()); + initLocalCache(zkConfig.getWatchPath()); addConnectionStateListener(); Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); Logs.Console.info("init zk client success..."); @@ -150,8 +150,8 @@ private void addConnectionStateListener() { } // 本地缓存 - private void initLocalCache(String cachePath) throws Exception { - cache = new TreeCache(client, cachePath); + private void initLocalCache(String watchRootPath) throws Exception { + cache = new TreeCache(client, watchRootPath); cache.start(); } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java index 8843d450..c1814958 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKConfig.java @@ -45,7 +45,7 @@ public class ZKConfig { private int connectionTimeout = ZK_CONNECTION_TIMEOUT; - private String localCachePath = ZK_DEFAULT_CACHE_PATH; + private String watchPath = ZK_DEFAULT_CACHE_PATH; public ZKConfig(String hosts) { this.hosts = hosts; @@ -55,7 +55,7 @@ public static ZKConfig build() { return new ZKConfig(zk.server_address) .setConnectionTimeout(zk.connectionTimeoutMs) .setDigest(zk.digest) - .setLocalCachePath(zk.local_cache_path) + .setWatchPath(zk.watch_path) .setMaxRetries(zk.retry.maxRetries) .setMaxSleepMs(zk.retry.maxSleepMs) .setBaseSleepTimeMs(zk.retry.baseSleepTimeMs) @@ -136,12 +136,12 @@ public ZKConfig setDigest(String digest) { return this; } - public String getLocalCachePath() { - return localCachePath; + public String getWatchPath() { + return watchPath; } - public ZKConfig setLocalCachePath(String localCachePath) { - this.localCachePath = localCachePath; + public ZKConfig setWatchPath(String watchPath) { + this.watchPath = watchPath; return this; } @@ -156,7 +156,7 @@ public String toString() { ", maxSleepMs=" + maxSleepMs + ", sessionTimeout=" + sessionTimeout + ", connectionTimeout=" + connectionTimeout + - ", localCachePath='" + localCachePath + '\'' + + ", watchPath='" + watchPath + '\'' + '}'; } } From f7fb03eec30f113a2c0d2d0f64a832de8474f636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 23:41:56 +0800 Subject: [PATCH 761/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/core/server/ServerConnectionManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index d34e849c..c6282518 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -122,13 +122,13 @@ void startTimeout() { @Override public void run(Timeout timeout) throws Exception { if (!connection.isConnected()) { - Logs.HB.info("connection was disconnected, heartbeat timeout times={}, connection={}", timeoutTimes, connection); + Logs.HB.info("heartbeat timeout times={}, connection disconnected, conn={}", timeoutTimes, connection); return; } if (connection.isReadTimeout()) { if (++timeoutTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); - Logs.HB.warn("client heartbeat timeout times={}, do close connection={}", timeoutTimes, connection); + Logs.HB.warn("client heartbeat timeout times={}, do close conn={}", timeoutTimes, connection); return; } else { Logs.HB.info("client heartbeat timeout times={}, connection={}", timeoutTimes, connection); From 0ae7225c1ba0cc3c333141c2a816d02fabdb4480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 9 Dec 2016 23:48:53 +0800 Subject: [PATCH 762/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9connId=E4=B8=BA?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=94=AF=E4=B8=80ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/core/server/ServerConnectionManager.java | 11 ++++++----- .../com/mpush/netty/connection/NettyConnection.java | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index c6282518..f2554192 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -30,6 +30,7 @@ import com.mpush.tools.thread.NamedThreadFactory; import com.mpush.tools.thread.ThreadNames; import io.netty.channel.Channel; +import io.netty.channel.ChannelId; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; import io.netty.util.TimerTask; @@ -46,7 +47,7 @@ * @author ohun@live.cn */ public final class ServerConnectionManager implements ConnectionManager { - private final ConcurrentMap connections = new ConcurrentHashMap<>(); + private final ConcurrentMap connections = new ConcurrentHashMap<>(); private HashedWheelTimer timer; private final boolean heartbeatCheck; @@ -76,19 +77,19 @@ public void destroy() { } @Override - public Connection get(final Channel channel) { - return connections.get(channel.id().asLongText()); + public Connection get(Channel channel) { + return connections.get(channel.id()); } @Override public void add(Connection connection) { - connections.putIfAbsent(connection.getChannel().id().asLongText(), connection); + connections.putIfAbsent(connection.getChannel().id(), connection); if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); } @Override public Connection removeAndClose(Channel channel) { - Connection connection = connections.remove(channel.id().asLongText()); + Connection connection = connections.remove(channel.id()); if (connection != null) { connection.close(); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 40f2378e..2211fdb5 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -70,7 +70,7 @@ public SessionContext getSessionContext() { @Override public String getId() { - return channel.id().asLongText(); + return channel.id().asShortText(); } @Override From ec1fc00b7e2715bfb539a182ffe8a15b42feafd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 10 Dec 2016 23:16:44 +0800 Subject: [PATCH 763/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connect/ConnClientChannelHandler.java | 18 +++++++++--------- .../com/mpush/core/handler/AckHandler.java | 2 +- .../mpush/core/handler/BindUserHandler.java | 10 +++++----- .../mpush/core/handler/ClientPushHandler.java | 2 +- .../mpush/core/handler/FastConnectHandler.java | 6 +++--- .../mpush/core/handler/HandshakeHandler.java | 6 +++--- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 9458adc6..b02bbf4f 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -87,7 +87,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); connection.getSessionContext().setHeartbeat(message.heartbeat); startHeartBeat(message.heartbeat - 1000); - LOGGER.info(">>> handshake success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); + LOGGER.info("handshake success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); bindUser(clientConfig); if (!perfTest) { saveToRedisForFastConnection(clientConfig, message.sessionId, message.expireTime, sessionKey); @@ -104,27 +104,27 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().setHeartbeat(message.heartbeat); startHeartBeat(message.heartbeat - 1000); bindUser(clientConfig); - LOGGER.info(">>> fast connect success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); + LOGGER.info("fast connect success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error(">>> receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); + LOGGER.error("receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); ctx.close(); } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error(">>> receive an error packet=" + errorMessage); + LOGGER.error("receive an error packet=" + errorMessage); } else if (command == Command.PUSH) { int receivePushNum = STATISTICS.receivePushNum.incrementAndGet(); PushMessage message = new PushMessage(packet, connection); - LOGGER.info(">>> receive an push message, content={}, receivePushNum={}", new String(message.content, Constants.UTF_8), receivePushNum); + LOGGER.info("receive an push message, content={}, receivePushNum={}", new String(message.content, Constants.UTF_8), receivePushNum); if (message.needAck()) { AckMessage.from(message).sendRaw(); - LOGGER.info(">>> send ack success for sessionId={}", message.getSessionId()); + LOGGER.info("send ack success for sessionId={}", message.getSessionId()); } } else if (command == Command.HEARTBEAT) { - LOGGER.info(">>> receive a heartbeat pong..."); + LOGGER.info("receive a heartbeat pong..."); } else if (command == Command.OK) { OkMessage okMessage = new OkMessage(packet, connection); int bindUserNum = STATISTICS.bindUserNum.get(); @@ -132,11 +132,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception bindUserNum = STATISTICS.bindUserNum.incrementAndGet(); } - LOGGER.info(">>> receive an success message={}, bindUserNum={}", okMessage, bindUserNum); + LOGGER.info("receive an success message={}, bindUserNum={}", okMessage, bindUserNum); } else if (command == Command.HTTP_PROXY) { HttpResponseMessage message = new HttpResponseMessage(packet, connection); - LOGGER.info(">>> receive a http response, message={}, body={}", + LOGGER.info("receive a http response, message={}, body={}", message, message.body == null ? null : new String(message.body, Constants.UTF_8)); } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java index 03fa176d..b3fdb46b 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AckHandler.java @@ -46,7 +46,7 @@ public AckMessage decode(Packet packet, Connection connection) { public void handle(AckMessage message) { AckContext context = AckMessageQueue.I.getAndRemove(message.getSessionId()); if (context == null) { - Logs.PUSH.info(">>> receive client ack, but timeout message={}", message); + Logs.PUSH.info("receive client ack, but timeout message={}", message); return; } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 67dab5b0..890fb78d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -66,7 +66,7 @@ public void handle(BindUserMessage message) { private void bind(BindUserMessage message) { if (Strings.isNullOrEmpty(message.userId)) { ErrorMessage.from(message).setReason("invalid param").close(); - Logs.CONN.error("bind user failure for invalid param, connection={}", message.getConnection()); + Logs.CONN.error("bind user failure for invalid param, conn={}", message.getConnection()); return; } //1.绑定用户时先看下是否握手成功 @@ -77,7 +77,7 @@ private void bind(BindUserMessage message) { if (message.userId.equals(context.userId)) { context.tags = message.tags; OkMessage.from(message).setData("bind success").sendRaw(); - Logs.CONN.info(">>> rebind user success, userId={}, session={}", message.userId, context); + Logs.CONN.info("rebind user success, userId={}, session={}", message.userId, context); return; } else { unbind(message); @@ -90,7 +90,7 @@ private void bind(BindUserMessage message) { context.tags = message.tags; EventBus.I.post(new UserOnlineEvent(message.getConnection(), message.userId)); OkMessage.from(message).setData("bind success").sendRaw(); - Logs.CONN.info(">>> bind user success, userId={}, session={}", message.userId, context); + Logs.CONN.info("bind user success, userId={}, session={}", message.userId, context); } else { //3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功 RouterCenter.I.unRegister(message.userId, context.getClientType()); @@ -99,7 +99,7 @@ private void bind(BindUserMessage message) { } } else { ErrorMessage.from(message).setReason("not handshake").close(); - Logs.CONN.error("bind user failure not handshake, userId={}, connection={}", message.userId, message.getConnection()); + Logs.CONN.error("bind user failure not handshake, userId={}, conn={}", message.userId, message.getConnection()); } } @@ -146,7 +146,7 @@ private void unbind(BindUserMessage message) { context.tags = null; EventBus.I.post(new UserOfflineEvent(message.getConnection(), userId)); OkMessage.from(message).setData("unbind success").sendRaw(); - Logs.CONN.info(">>> unbind user success, userId={}, session={}", userId, context); + Logs.CONN.info("unbind user success, userId={}, session={}", userId, context); } else { ErrorMessage.from(message).setReason("unbind failed").sendRaw(); Logs.CONN.error("unbind user failure, unRegister router failure, userId={}, session={}", userId, context); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java index f278758e..b2b84389 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java @@ -45,7 +45,7 @@ public PushMessage decode(Packet packet, Connection connection) { @Override public void handle(PushMessage message) { - Logs.PUSH.info(">>> receive client push message={}", message); + Logs.PUSH.info("receive client push message={}", message); if (message.autoAck()) { AckMessage.from(message).sendRaw(); diff --git a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java index e4cd4164..0ab40499 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/FastConnectHandler.java @@ -52,12 +52,12 @@ public void handle(FastConnectMessage message) { if (session == null) { //1.没查到说明session已经失效了 ErrorMessage.from(message).setReason("session expired").send(); - Logs.CONN.warn("fast connect failure, session is expired, sessionId={}, deviceId={}, connection={}" + Logs.CONN.warn("fast connect failure, session is expired, sessionId={}, deviceId={}, conn={}" , message.sessionId, message.deviceId, message.getConnection().getChannel()); } else if (!session.context.deviceId.equals(message.deviceId)) { //2.非法的设备, 当前设备不是上次生成session时的设备 ErrorMessage.from(message).setReason("invalid device").send(); - Logs.CONN.warn("fast connect failure, not the same device, deviceId={}, session={}, connection={}" + Logs.CONN.warn("fast connect failure, not the same device, deviceId={}, session={}, conn={}" , message.deviceId, session.context, message.getConnection().getChannel()); } else { //3.校验成功,重新计算心跳,完成快速重连 @@ -71,7 +71,7 @@ public void handle(FastConnectMessage message) { .setHeartbeat(heartbeat) .sendRaw(); Profiler.release(); - Logs.CONN.info(">>> fast connect success, session={}", session.context); + Logs.CONN.info("fast connect success, session={}", session.context); } } } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 86adfb27..168d8c02 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -61,14 +61,14 @@ public void handle(HandshakeMessage message) { || iv.length != CipherBox.I.getAesKeyLength() || clientKey.length != CipherBox.I.getAesKeyLength()) { ErrorMessage.from(message).setReason("Param invalid").close(); - Logs.CONN.error("handshake failure, message={}, connection={}", message, message.getConnection()); + Logs.CONN.error("handshake failure, message={}, conn={}", message, message.getConnection()); return; } //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { - Logs.CONN.warn("handshake failure, repeat handshake, session={}", message.getConnection().getSessionContext()); + Logs.CONN.warn("handshake failure, repeat handshake, conn={}", message.getConnection()); return; } @@ -105,6 +105,6 @@ public void handle(HandshakeMessage message) { //10.触发握手成功事件 EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); - Logs.CONN.info(">>> handshake success, connection={}", message.getConnection()); + Logs.CONN.info("handshake success, conn={}", message.getConnection()); } } \ No newline at end of file From 3cb84865c4ce0c3b35ec57a6df0e649b1e083311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 10 Dec 2016 23:17:59 +0800 Subject: [PATCH 764/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/client/connect/ConnClientChannelHandler.java | 8 ++++---- .../java/com/mpush/core/handler/ClientPushHandler.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index b02bbf4f..f6651988 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -210,7 +210,7 @@ private void tryFastConnect() { handshake(); } }); - LOGGER.debug("<<< send fast connect message={}", message); + LOGGER.debug("send fast connect message={}", message); } private void bindUser(ClientConfig client) { @@ -219,7 +219,7 @@ private void bindUser(ClientConfig client) { message.tags = "test"; message.send(); connection.getSessionContext().setUserId(client.getUserId()); - LOGGER.debug("<<< send bind user message={}", message); + LOGGER.debug("send bind user message={}", message); } private void saveToRedisForFastConnection(ClientConfig client, String sessionId, Long expireTime, byte[] sessionKey) { @@ -247,7 +247,7 @@ private void handshake() { message.osVersion = clientConfig.getOsVersion(); message.timestamp = System.currentTimeMillis(); message.send(); - LOGGER.debug("<<< send handshake message={}", message); + LOGGER.debug("send handshake message={}", message); } private void startHeartBeat(final int heartbeat) throws Exception { @@ -278,7 +278,7 @@ private boolean healthCheck() { } if (connection.isWriteTimeout()) { - LOGGER.info("<<< send heartbeat ping..."); + LOGGER.info("send heartbeat ping..."); connection.send(Packet.HB_PACKET); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java index b2b84389..c5e5a1af 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/ClientPushHandler.java @@ -49,7 +49,7 @@ public void handle(PushMessage message) { if (message.autoAck()) { AckMessage.from(message).sendRaw(); - Logs.PUSH.info("<<< send ack for push message={}", message); + Logs.PUSH.info("send ack for push message={}", message); } //biz code write here } From ee2d9e5679c5c242b3db7a558b137267aeb58842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sat, 10 Dec 2016 23:23:40 +0800 Subject: [PATCH 765/890] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index c41c7576..a300c270 100644 --- a/pom.xml +++ b/pom.xml @@ -207,7 +207,7 @@ 4.10 test - + org.apache.commons commons-collections4 @@ -228,7 +228,7 @@ org.apache.curator curator-recipes - 2.11.1 + 2.11.1 netty @@ -239,15 +239,15 @@ org.apache.curator curator-x-discovery - 2.11.1 + 2.11.1 - + redis.clients jedis 2.9.0 - + com.google.code.gson gson From 22ddc32e8cff1001444dc8be988e412ae9563058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 11 Dec 2016 15:27:06 +0800 Subject: [PATCH 766/890] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E5=88=B0v0.6.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Changelog.md | 14 +++++++ mpush-api/pom.xml | 2 +- mpush-boot/pom.xml | 2 +- mpush-cache/pom.xml | 2 +- mpush-client/pom.xml | 2 +- .../connect/ConnClientChannelHandler.java | 9 ++++- mpush-common/pom.xml | 2 +- mpush-core/pom.xml | 2 +- mpush-monitor/pom.xml | 2 +- mpush-netty/pom.xml | 2 +- mpush-test/pom.xml | 2 +- .../mpush/test/client/ConnClientTestMain.java | 37 ++++++++++++------- .../src/main/resources/application.conf | 8 ++++ mpush-tools/pom.xml | 2 +- mpush-zk/pom.xml | 2 +- pom.xml | 4 +- 16 files changed, 67 insertions(+), 27 deletions(-) diff --git a/Changelog.md b/Changelog.md index bb1fe035..7464b677 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,17 @@ +#### v0.6.1 + +1. 产品版本策略修改,主版本前移一位,最后一位用于小版本bug fix +2. 新增支持单机多实例部署方式 +3. 升级依赖类库,解决由于版本升级引起的jedis和zk兼容性问题 +4. 核心日志打印内容优化,更利于问题排查 +5. 修复connId高并发下可能重复的bug +6. 增加压测代码,优化测试模块 +7. 配置文件优化,增加相应的注释说明 +8. 其他bug fix及代码优化 + + + + #### v0.0.6 1. 网关服务增加UDP及组播支持,后续网关部分的TCP部分将逐步淘汰 diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 30485c1f..0acbc2b5 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-api diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 63ef2052..6cff159f 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-boot diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml index ad1fd434..100b6fdc 100644 --- a/mpush-cache/pom.xml +++ b/mpush-cache/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-cache diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 28550641..275d4149 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-client diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index f6651988..f36bda65 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -155,10 +155,17 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public void channelActive(ChannelHandlerContext ctx) throws Exception { int clientNum = STATISTICS.clientNum.incrementAndGet(); LOGGER.info("client connect channel={}, clientNum={}", ctx.channel(), clientNum); - while (clientConfig == null) { + + for (int i = 0; i < 3; i++) { + if (clientConfig != null) break; clientConfig = ctx.channel().attr(CONFIG_KEY).getAndRemove(); if (clientConfig == null) TimeUnit.SECONDS.sleep(1); } + + if (clientConfig == null) { + throw new NullPointerException("client config is null, channel=" + ctx.channel()); + } + connection.init(ctx.channel(), true); if (perfTest) { handshake(); diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index aa025ae1..8fe53da9 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -5,7 +5,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 4.0.0 diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index 59a78303..8aac1b99 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-core diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index 12f8991d..f6f13c50 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -3,7 +3,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 4.0.0 diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 76079c18..a4f9d064 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-netty diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index bf5d14d9..c1e72394 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-test diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java index d1ac4aa0..49fbb07d 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientTestMain.java @@ -36,32 +36,41 @@ public class ConnClientTestMain { public static void main(String[] args) throws Exception { - int count = 10, printDelay = 1; + int count = 10; + String userPrefix = ""; + int printDelay = 1; boolean sync = true; + if (args.length > 0) { count = NumberUtils.toInt(args[0], count); } + if (args.length > 1) { - printDelay = NumberUtils.toInt(args[1], printDelay); + userPrefix = args[1]; } if (args.length > 2) { - sync = !"1".equals(args[2]); + printDelay = NumberUtils.toInt(args[2], printDelay); + } + + if (args.length > 3) { + sync = !"1".equals(args[3]); } - testConnClient(count, printDelay, sync); + testConnClient(count, userPrefix, printDelay, sync); } @Test public void testConnClient() throws Exception { - testConnClient(1, 1, true); + testConnClient(1, "", 1, true); LockSupport.park(); } - private static void testConnClient(int count, int printDelay, boolean sync) throws Exception { + private static void testConnClient(int count, String userPrefix, int printDelay, boolean sync) throws Exception { Logs.init(); ConnClientBoot boot = new ConnClientBoot(); boot.start().get(); + List serverList = boot.getServers(); if (serverList.isEmpty()) { boot.stop(); @@ -69,18 +78,20 @@ private static void testConnClient(int count, int printDelay, boolean sync) thro return; } - Executors.newSingleThreadScheduledExecutor() - .scheduleAtFixedRate( - () -> System.err.println(ConnClientChannelHandler.STATISTICS) - , 3, printDelay, TimeUnit.SECONDS - ); + if (printDelay > 0) { + Executors.newSingleThreadScheduledExecutor() + .scheduleAtFixedRate( + () -> System.err.println(ConnClientChannelHandler.STATISTICS) + , 3, printDelay, TimeUnit.SECONDS + ); + } for (int i = 0; i < count; i++) { String clientVersion = "1.0." + i; String osName = "android"; String osVersion = "1.0.1"; - String userId = "user-" + i; - String deviceId = "test-device-id-" + i; + String userId = userPrefix + "user-" + i; + String deviceId = userPrefix + "test-device-id-" + i; byte[] clientKey = CipherBox.I.randomAESKey(); byte[] iv = CipherBox.I.randomAESIV(); diff --git a/mpush-test/src/main/resources/application.conf b/mpush-test/src/main/resources/application.conf index 8034e981..c684b7dd 100644 --- a/mpush-test/src/main/resources/application.conf +++ b/mpush-test/src/main/resources/application.conf @@ -9,3 +9,11 @@ mp.redis {// redis 集群配置 nodes:["127.0.0.1:6379"]//格式是ip:port,密码可以没有ip:port } mp.http.proxy-enabled=true + +mp.net { + gateway-server-net=udp //网关服务使用的网络类型tcp/udp + connect-server-port=3000 //长链接服务对外端口, 公网端口 + gateway-server-port=3001 //网关服务端口, 内部端口 + gateway-client-port=4000 //UDP客户端端口, 内部端口 + admin-server-port=3002 //控制台服务端口, 内部端口 +} diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 743c73cf..866804c0 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-tools diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml index fab165e4..b7bae4f0 100644 --- a/mpush-zk/pom.xml +++ b/mpush-zk/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.0.6 + 0.6.1 mpush-zk diff --git a/pom.xml b/pom.xml index a300c270..f61c0750 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.github.mpusher mpush pom - 0.0.6 + 0.6.1 mpush MPUSH消息推送系统 https://github.com/mpusher/mpush @@ -61,7 +61,7 @@ UTF-8 1.8 com.github.mpusher - 0.0.6 + 0.6.1 ${mpush.version} ${mpush.version} ${mpush.version} From 5e6f706ebdf231b5a089338e2008bdda575866fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 11 Dec 2016 15:41:15 +0800 Subject: [PATCH 767/890] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E5=88=B0v0.6.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/connect/ConnClientChannelHandler.java | 14 +++++++------- mpush-test/src/main/resources/application.conf | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index f36bda65..337740ed 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -107,7 +107,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.info("fast connect success, clientConfig={}, connectedNum={}", clientConfig, connectedNum); } else if (command == Command.KICK) { KickUserMessage message = new KickUserMessage(packet, connection); - LOGGER.error("receive kick user userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); + LOGGER.error("receive kick user msg userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); ctx.close(); } else if (command == Command.ERROR) { ErrorMessage errorMessage = new ErrorMessage(packet, connection); @@ -116,7 +116,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception int receivePushNum = STATISTICS.receivePushNum.incrementAndGet(); PushMessage message = new PushMessage(packet, connection); - LOGGER.info("receive an push message, content={}, receivePushNum={}", new String(message.content, Constants.UTF_8), receivePushNum); + LOGGER.info("receive push message, content={}, receivePushNum={}" + , new String(message.content, Constants.UTF_8), receivePushNum); if (message.needAck()) { AckMessage.from(message).sendRaw(); @@ -124,7 +125,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } else if (command == Command.HEARTBEAT) { - LOGGER.info("receive a heartbeat pong..."); + LOGGER.info("receive heartbeat pong..."); } else if (command == Command.OK) { OkMessage okMessage = new OkMessage(packet, connection); int bindUserNum = STATISTICS.bindUserNum.get(); @@ -132,17 +133,16 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception bindUserNum = STATISTICS.bindUserNum.incrementAndGet(); } - LOGGER.info("receive an success message={}, bindUserNum={}", okMessage, bindUserNum); + LOGGER.info("receive {}, bindUserNum={}", okMessage, bindUserNum); } else if (command == Command.HTTP_PROXY) { HttpResponseMessage message = new HttpResponseMessage(packet, connection); - LOGGER.info("receive a http response, message={}, body={}", + LOGGER.info("receive http response, message={}, body={}", message, message.body == null ? null : new String(message.body, Constants.UTF_8)); } } - - LOGGER.debug("update currentTime:" + ctx.channel() + "," + msg); + LOGGER.debug("receive package={}, chanel={}", msg, ctx.channel()); } @Override diff --git a/mpush-test/src/main/resources/application.conf b/mpush-test/src/main/resources/application.conf index c684b7dd..fec3f945 100644 --- a/mpush-test/src/main/resources/application.conf +++ b/mpush-test/src/main/resources/application.conf @@ -1,8 +1,8 @@ mp.home=${user.dir}/target -mp.log-level=warn +mp.log-level=info mp.log-conf-path=logback.xml -mp.core.min-heartbeat=10s -mp.core.max-heartbeat=10s +mp.core.min-heartbeat=30s +mp.core.max-heartbeat=30s mp.core.compress-threshold=10k mp.zk.server-address="127.0.0.1:2181" mp.redis {// redis 集群配置 From 0e67f51d668df4c9db884e5050c985ed6fa04d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 11 Dec 2016 16:50:51 +0800 Subject: [PATCH 768/890] =?UTF-8?q?udp=20gateway=20=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=B8=8B=E5=8F=96=E6=B6=88redis=E8=B8=A2=E4=BA=BA=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/cache/redis/listener/ListenerDispatcher.java | 7 +------ .../java/com/mpush/core/router/RouterChangeListener.java | 7 +++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java index f74f1c27..75b45f0e 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -54,12 +54,7 @@ public void onMessage(final String channel, final String message) { } public void subscribe(String channel, MessageListener listener) { - List listeners = subscribes.get(channel); - if (listeners == null) { - listeners = Lists.newArrayList(); - subscribes.put(channel, listeners); - } - listeners.add(listener); + subscribes.computeIfAbsent(channel, k -> Lists.newArrayList()).add(listener); RedisManager.I.subscribe(Subscriber.holder, channel); } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index 78e0ec1f..ecd9787f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -51,9 +51,12 @@ public final class RouterChangeListener extends EventConsumer implements MessageListener { public static final String KICK_CHANNEL_ = "/mpush/kick/"; private final String kick_channel = KICK_CHANNEL_ + GS_NODE.getHostAndPort(); + private final boolean udpGateway = CC.mp.net.udpGateway(); public RouterChangeListener() { - ListenerDispatcher.I.subscribe(getKickChannel(), this); + if (!udpGateway) { + ListenerDispatcher.I.subscribe(getKickChannel(), this); + } } public String getKickChannel() { @@ -113,7 +116,7 @@ private void kickRemote(String userId, RemoteRouter remoteRouter) { return; } - if (CC.mp.net.udpGateway()) { + if (udpGateway) { Connection connection = GatewayUDPConnector.I().getConnection(); GatewayKickUserMessage.build(connection) .setUserId(userId) From a2acb4b87349942b5c26af9580a3925ae52c5fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 12 Dec 2016 11:33:25 +0800 Subject: [PATCH 769/890] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=8A=A0=E5=85=A5GC=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/env-mp.sh | 48 ++++++++++++------------------------------------ bin/set-env.sh | 10 +++++++++- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/bin/env-mp.sh b/bin/env-mp.sh index 5f5057bd..d136fe1e 100644 --- a/bin/env-mp.sh +++ b/bin/env-mp.sh @@ -36,6 +36,16 @@ then fi fi +if [ "x${MP_DATA_DIR}" = "x" ] +then + MP_DATA_DIR="${MPUSH_PREFIX}/tmp" +fi + +if [ "x${MP_LOG_DIR}" = "x" ] +then + MP_LOG_DIR="${MPUSH_PREFIX}/logs" +fi + if [ -f "${MP_BIN_DIR}/set-env.sh" ]; then . "${MP_BIN_DIR}/set-env.sh" fi @@ -52,16 +62,6 @@ then . "$MP_BIN_DIR/java.env" fi -if [ "x${MP_DATA_DIR}" = "x" ] -then - MP_DATA_DIR="${MPUSH_PREFIX}/tmp" -fi - -if [ "x${MP_LOG_DIR}" = "x" ] -then - MP_LOG_DIR="${MPUSH_PREFIX}/logs" -fi - if [ "x${MP_LOG4J_PROP}" = "x" ] then MP_LOG4J_PROP="INFO,CONSOLE" @@ -77,22 +77,10 @@ fi #add the conf dir to classpath CLASSPATH="$MP_CFG_DIR:$CLASSPATH" -for i in "$MP_BIN_DIR"/../src/java/lib/*.jar -do - CLASSPATH="$i:$CLASSPATH" -done - #make it work in the binary package #(use array for LIB_PATH to account for spaces within wildcard expansion) -if [ -e "${MPUSH_PREFIX}"/share/mpush/mpush-*.jar ]; then - LIB_PATH=("${MPUSH_PREFIX}"/share/mpush/*.jar) -else - #release tarball format - for i in "$MP_BIN_DIR"/../mpush-*.jar - do - CLASSPATH="$i:$CLASSPATH" - done - LIB_PATH=("${MP_BIN_DIR}"/../lib/*.jar) +if [ -e "${MPUSH_PREFIX}"/../lib/plugins/*.jar ]; then + LIB_PATH=("${MPUSH_PREFIX}"/../lib/plugins/*.jar) fi for i in "${LIB_PATH[@]}" @@ -100,16 +88,6 @@ do CLASSPATH="$i:$CLASSPATH" done -#make it work for developers -for d in "$MP_BIN_DIR"/../build/lib/*.jar -do - CLASSPATH="$d:$CLASSPATH" -done - -#make it work for developers -CLASSPATH="$MP_BIN_DIR/../build/classes:$CLASSPATH" - - case "`uname`" in CYGWIN*) cygwin=true ;; *) cygwin=false ;; @@ -119,5 +97,3 @@ if $cygwin then CLASSPATH=`cygpath -wp "$CLASSPATH"` fi - -#echo "CLASSPATH=$CLASSPATH" \ No newline at end of file diff --git a/bin/set-env.sh b/bin/set-env.sh index 693195e4..81854e2c 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -14,4 +14,12 @@ JVM_FLAGS="-Dio.netty.leakDetection.level=advanced" JVM_FLAGS="$JVM_FLAGS -Dio.netty.noKeySetOptimization=false" #开启远程调试 -#JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" \ No newline at end of file +#JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" + +#GC配置 +#运行模式 整个堆内存大小 GC算法 +#JVM_FLAGS="$JVM_FLAGS -server -Xmx1024m -Xms1024m -XX:+UseG1GC -XX:MaxGCPauseMillis=200" +#GC日志 发生OOM时创建堆内存转储文件 +#JVM_FLAGS="$JVM_FLAGS -Xloggc:$MP_LOG_DIR/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps" +#发生OOM后的操作 +#JVM_FLAGS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$MP_LOG_DIR -XX:OnOutOfMemoryError=$MP_BIN_DIR/restart.sh" \ No newline at end of file From 4c101f748311ee7ba2e2b8c2aa35dd37158f55ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 12 Dec 2016 11:34:07 +0800 Subject: [PATCH 770/890] =?UTF-8?q?=E6=8E=92=E9=99=A4=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E5=88=B0maven=E4=B8=AD=E5=A4=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-test/pom.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index c1e72394..2b269b3f 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -47,6 +47,18 @@ compile + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + + + test From 882ed425ca369a34b845abd6b8725e91c530b5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 12 Dec 2016 11:35:32 +0800 Subject: [PATCH 771/890] =?UTF-8?q?=E5=86=85=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/src/main/java/com/mpush/api/protocol/Command.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java index 6cce4164..26326eee 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java @@ -56,8 +56,10 @@ public enum Command { public final byte cmd; + private static final Command[] values = values(); + public static Command toCMD(byte b) { - if (b > 0 && b < values().length) return values()[b - 1]; + if (b > 0 && b < values.length) return values[b - 1]; return UNKNOWN; } } From 2325db06d04f643907e0c075d5638278a19a0acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Mon, 12 Dec 2016 22:20:45 +0800 Subject: [PATCH 772/890] =?UTF-8?q?=E5=86=85=E5=AD=98=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E5=9E=83=E5=9C=BE=E5=9B=9E=E6=94=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/connection/Connection.java | 6 +- .../api/connection/ConnectionManager.java | 2 +- .../mpush/api/connection/SessionContext.java | 8 +- .../com/mpush/core/handler/AdminHandler.java | 2 +- .../com/mpush/core/router/LocalRouter.java | 8 +- .../mpush/core/router/LocalRouterManager.java | 10 +- .../core/server/ServerConnectionManager.java | 98 +++++++++++++------ .../netty/connection/NettyConnection.java | 4 +- 8 files changed, 88 insertions(+), 50 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java index 8013a32e..760bdd4f 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/Connection.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/Connection.java @@ -30,9 +30,9 @@ * @author ohun@live.cn (夜色) */ public interface Connection { - int STATUS_NEW = 0; - int STATUS_CONNECTED = 1; - int STATUS_DISCONNECTED = 2; + byte STATUS_NEW = 0; + byte STATUS_CONNECTED = 1; + byte STATUS_DISCONNECTED = 2; void init(Channel channel, boolean security); diff --git a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java index bc859147..896f0edd 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/ConnectionManager.java @@ -36,7 +36,7 @@ public interface ConnectionManager { void add(Connection connection); - List getConnections(); + int getConnNum(); void init(); diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index ff9ceab0..a26042fe 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -22,8 +22,6 @@ import com.mpush.api.router.ClientClassifier; -import java.util.concurrent.TimeUnit; - /** * Created by ohun on 2015/12/22. * @@ -36,9 +34,9 @@ public final class SessionContext { public String deviceId; public String userId; public String tags; - public int heartbeat = (int) TimeUnit.SECONDS.toMillis(10); + public int heartbeat = 10000;// 10s public Cipher cipher; - private int clientType; + private byte clientType; public void changeCipher(Cipher cipher) { this.cipher = cipher; @@ -79,7 +77,7 @@ public boolean handshakeOk() { public int getClientType() { if (clientType == 0) { - clientType = ClientClassifier.I.getClientType(osName); + clientType = (byte) ClientClassifier.I.getClientType(osName); } return clientType; } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java index e58a039d..5ba7284e 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/AdminHandler.java @@ -164,7 +164,7 @@ private String getNodeData(ZKPath path) { public Serializable handler(ChannelHandlerContext ctx, String args) { switch (args) { case "conn": - return ConnectionServer.I().getConnectionManager().getConnections().size(); + return ConnectionServer.I().getConnectionManager().getConnNum(); case "online": { return UserManager.I.getOnlineUserNum(); } diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java index 1592ea0e..165cd5ac 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouter.java @@ -29,15 +29,13 @@ */ public final class LocalRouter implements Router { private final Connection connection; - private final int clientType; public LocalRouter(Connection connection) { this.connection = connection; - this.clientType = connection.getSessionContext().getClientType(); } public int getClientType() { - return clientType; + return connection.getSessionContext().getClientType(); } @Override @@ -57,13 +55,13 @@ public boolean equals(Object o) { LocalRouter that = (LocalRouter) o; - return clientType == that.clientType; + return getClientType() == that.getClientType(); } @Override public int hashCode() { - return Integer.hashCode(clientType); + return Integer.hashCode(getClientType()); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java index 50101a8a..473acb1e 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java +++ b/mpush-core/src/main/java/com/mpush/core/router/LocalRouterManager.java @@ -30,7 +30,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** @@ -40,7 +43,7 @@ */ public final class LocalRouterManager extends EventConsumer implements RouterManager { private static final Logger LOGGER = LoggerFactory.getLogger(LocalRouterManager.class); - private static final Map EMPTY = Collections.unmodifiableMap(new HashMap<>(0)); + private static final Map EMPTY = new HashMap<>(0); /** * 本地路由表 @@ -50,8 +53,7 @@ public final class LocalRouterManager extends EventConsumer implements RouterMan @Override public LocalRouter register(String userId, LocalRouter router) { LOGGER.info("register local router success userId={}, router={}", userId, router); - //add online userId - return routers.computeIfAbsent(userId, s -> new HashMap<>()).put(router.getClientType(), router); + return routers.computeIfAbsent(userId, s -> new HashMap<>(1)).put(router.getClientType(), router); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index f2554192..ddbcb0d4 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -20,12 +20,9 @@ package com.mpush.core.server; -import com.google.common.collect.Lists; import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; -import com.mpush.api.event.HandshakeEvent; import com.mpush.tools.config.CC; -import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; import com.mpush.tools.thread.NamedThreadFactory; import com.mpush.tools.thread.ThreadNames; @@ -35,7 +32,6 @@ import io.netty.util.Timeout; import io.netty.util.TimerTask; -import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; @@ -47,10 +43,10 @@ * @author ohun@live.cn */ public final class ServerConnectionManager implements ConnectionManager { - private final ConcurrentMap connections = new ConcurrentHashMap<>(); - - private HashedWheelTimer timer; + private final ConcurrentMap connections = new ConcurrentHashMap<>(); + private final ConnectionHolder DEFAULT = new SimpleConnectionHolder(null); private final boolean heartbeatCheck; + private HashedWheelTimer timer; public ServerConnectionManager(boolean heartbeatCheck) { this.heartbeatCheck = heartbeatCheck; @@ -59,7 +55,6 @@ public ServerConnectionManager(boolean heartbeatCheck) { @Override public void init() { if (heartbeatCheck) { - EventBus.I.register(this); long tickDuration = TimeUnit.SECONDS.toMillis(1);//1s 每秒钟走一步,一个心跳周期内大致走一圈 int ticksPerWheel = (int) (CC.mp.core.max_heartbeat / tickDuration); this.timer = new HashedWheelTimer( @@ -71,61 +66,93 @@ public void init() { @Override public void destroy() { - if (timer != null) timer.stop(); - connections.values().forEach(Connection::close); + if (timer != null) { + timer.stop(); + } + connections.values().forEach(ConnectionHolder::close); connections.clear(); } @Override public Connection get(Channel channel) { - return connections.get(channel.id()); + return connections.getOrDefault(channel.id(), DEFAULT).get(); } @Override public void add(Connection connection) { - connections.putIfAbsent(connection.getChannel().id(), connection); - if (heartbeatCheck) new HeartbeatCheckTask(connection).startTimeout(); + if (heartbeatCheck) { + connections.putIfAbsent(connection.getChannel().id(), new HeartbeatCheckTask(connection)); + } else { + connections.putIfAbsent(connection.getChannel().id(), new SimpleConnectionHolder(connection)); + } } @Override public Connection removeAndClose(Channel channel) { - Connection connection = connections.remove(channel.id()); - if (connection != null) { - connection.close(); + ConnectionHolder holder = connections.remove(channel.id()); + if (holder != null) { + Connection connection = holder.get(); + holder.close(); + return connection; } - return connection; + return null; } @Override - public List getConnections() { - return Lists.newArrayList(connections.values()); + public int getConnNum() { + return connections.size(); } - //@Subscribe - void on(HandshakeEvent event) { - new HeartbeatCheckTask(event.connection).startTimeout(); - } + private interface ConnectionHolder { + Connection get(); - private class HeartbeatCheckTask implements TimerTask { + void close(); + } - private int timeoutTimes = 0; + private static class SimpleConnectionHolder implements ConnectionHolder { private final Connection connection; - HeartbeatCheckTask(Connection connection) { + private SimpleConnectionHolder(Connection connection) { + this.connection = connection; + } + + @Override + public Connection get() { + return connection; + } + + @Override + public void close() { + if (connection != null) { + connection.close(); + } + } + } + + + private class HeartbeatCheckTask implements ConnectionHolder, TimerTask { + + private byte timeoutTimes = 0; + private Connection connection; + + private HeartbeatCheckTask(Connection connection) { this.connection = connection; } void startTimeout() { - int timeout = connection.getSessionContext().heartbeat; - timer.newTimeout(this, timeout, TimeUnit.MILLISECONDS); + if (connection != null && connection.isConnected()) { + int timeout = connection.getSessionContext().heartbeat; + timer.newTimeout(this, timeout, TimeUnit.MILLISECONDS); + } } @Override public void run(Timeout timeout) throws Exception { - if (!connection.isConnected()) { + if (connection == null || !connection.isConnected()) { Logs.HB.info("heartbeat timeout times={}, connection disconnected, conn={}", timeoutTimes, connection); return; } + if (connection.isReadTimeout()) { if (++timeoutTimes > CC.mp.core.max_hb_timeout_times) { connection.close(); @@ -139,5 +166,18 @@ public void run(Timeout timeout) throws Exception { } startTimeout(); } + + @Override + public void close() { + if (connection != null) { + connection.close(); + connection = null; + } + } + + @Override + public Connection get() { + return connection; + } } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 2211fdb5..7e407f2e 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -43,7 +43,7 @@ public final class NettyConnection implements Connection, ChannelFutureListener private static final Cipher RSA_CIPHER = CipherFactory.create(); private SessionContext context; private Channel channel; - private volatile int status = STATUS_NEW; + private volatile byte status = STATUS_NEW; private long lastReadTime; private long lastWriteTime; @@ -110,7 +110,7 @@ public ChannelFuture close() { @Override public boolean isConnected() { - return status == STATUS_CONNECTED || channel.isActive(); + return status == STATUS_CONNECTED; } @Override From f453ab29286666e4ffb4de8f31f485ca6ed8caad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 13 Dec 2016 18:24:28 +0800 Subject: [PATCH 773/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=81=E6=8E=A7?= =?UTF-8?q?=E6=80=BB=E6=95=B0=E8=AE=A1=E6=95=B0bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GatewayTCPConnectionFactory.java | 4 +- .../handler/GatewayClientChannelHandler.java | 2 +- .../com/mpush/client/push/PushRequest.java | 7 ++- .../mpush/core/push/BroadcastPushTask.java | 3 +- .../com/mpush/core/push/FastFlowControl.java | 7 +-- .../java/com/mpush/core/push/FlowControl.java | 2 +- .../mpush/core/push/GlobalFlowControl.java | 7 +-- .../com/mpush/core/push/RedisFlowControl.java | 5 +-- .../netty/connection/NettyConnection.java | 4 +- .../mpush/test/push/PushClientTestMain.java | 44 +++++++++++-------- 10 files changed, 44 insertions(+), 41 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java index 20f38c31..3abdf1f1 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java @@ -85,9 +85,7 @@ public void send(String hostAndPort, Function void broadcast(Function creator, Consumer sender) { - ip_client.forEach((s, client) -> { - sender.accept(creator.apply(client.getConnection())); - }); + ip_client.forEach((s, client) -> sender.accept(creator.apply(client.getConnection()))); } private void restartClient(final GatewayClient client) { diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java index 1f01ad6b..88b7e1c3 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/handler/GatewayClientChannelHandler.java @@ -64,7 +64,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - Logs.CONN.info("client connect channel={}", ctx.channel()); + Logs.CONN.info("client connected channel={}", ctx.channel()); connection.init(ctx.channel(), false); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index db872482..1037534a 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -181,7 +181,12 @@ public FutureTask broadcast() { pushMessage -> { pushMessage.sendRaw(f -> { - if (!f.isSuccess()) failure(); + if (f.isSuccess()) { + LOGGER.debug("send broadcast to gateway server success, userId={}, conn={}", userId, f.channel()); + } else { + failure(); + LOGGER.error("send broadcast to gateway server failure, userId={}, conn={}", userId, f.channel(), f.cause()); + } }); if (pushMessage.taskId == null) { future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); diff --git a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java index 699fee26..a2024c7c 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java @@ -21,7 +21,6 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; -import com.mpush.api.protocol.Packet; import com.mpush.common.condition.AwaysPassCondition; import com.mpush.common.condition.Condition; import com.mpush.common.message.OkMessage; @@ -75,7 +74,7 @@ public void run() { } else {//没有结束,就延时进行下次任务 TODO 考虑优先级问题 PushCenter.I.delayTask(flowControl.getRemaining(), this); } - flowControl.incTotal(); + flowControl.end(); } diff --git a/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java index bc89bf84..0d4d00d1 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/FastFlowControl.java @@ -59,6 +59,7 @@ public int total() { public boolean checkQps() { if (count < limit) { count++; + total++; return true; } @@ -66,16 +67,12 @@ public boolean checkQps() { if (System.currentTimeMillis() - start > duration) { reset(); + total++; return true; } return false; } - @Override - public int incTotal() { - return total; - } - @Override public int getRemaining() { return duration - (int) (System.currentTimeMillis() - start); diff --git a/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java index 362e1595..531ce8c4 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/FlowControl.java @@ -32,7 +32,7 @@ public interface FlowControl { boolean checkQps() throws OverFlowException; - int incTotal(); + default void end(){}; int getRemaining(); diff --git a/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java index c68d8251..1bf10780 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/GlobalFlowControl.java @@ -55,6 +55,7 @@ public int total() { @Override public boolean checkQps() { if (count.incrementAndGet() < limit) { + total.incrementAndGet(); return true; } @@ -62,16 +63,12 @@ public boolean checkQps() { if (System.currentTimeMillis() - start > duration) { reset(); + total.incrementAndGet(); return true; } return false; } - @Override - public int incTotal() { - return total.get(); - } - @Override public int getRemaining() { return duration - (int) (System.currentTimeMillis() - start); diff --git a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java index 875a0b24..14270aae 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java +++ b/mpush-core/src/main/java/com/mpush/core/push/RedisFlowControl.java @@ -82,13 +82,12 @@ public boolean checkQps() throws OverFlowException { } @Override - public int incTotal() { + public void end() { int t = total; if (total > 0) { total = 0; - return controller.incSendCount(t); + controller.incSendCount(t); } - return 0; } @Override diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 7e407f2e..81d6a48e 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -29,6 +29,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelPromise; import io.netty.channel.socket.DatagramPacket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -94,7 +95,8 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { return future; } - //阻塞调用线程? + //阻塞调用线程还是抛异常? + //return channel.newPromise().setFailure(new RuntimeException("send data too busy")); return future.awaitUninterruptibly(); } else { return this.close(); diff --git a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java index f655ba12..4164cb71 100644 --- a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java @@ -44,26 +44,32 @@ public static void main(String[] args) throws Exception { public void testPush() throws Exception { Logs.init(); PushSender sender = PushSender.create(); - sender.start().whenComplete((success, throwable) -> { - PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); - msg.setMsgId("msgId_0"); + sender.start().join(); - PushContext context = PushContext.build(msg) - .setAckModel(AckModel.AUTO_ACK) - .setUserId("user-0") - .setBroadcast(false) - //.setTags(Sets.newHashSet("test")) - //.setCondition("tags&&tags.indexOf('test')!=-1") - //.setUserIds(Arrays.asList("user-0", "user-1")) - .setTimeout(2000) - .setCallback(new PushCallback() { - @Override - public void onResult(PushResult result) { - System.err.println("\n\n" + result); - } - }); - FutureTask future = sender.send(context); - }); + PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); + msg.setMsgId("msgId_0"); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + PushContext context = PushContext.build(msg) + .setAckModel(AckModel.AUTO_ACK) + //.setUserId("user-0") + .setBroadcast(true) + //.setTags(Sets.newHashSet("test")) + //.setCondition("tags&&tags.indexOf('test')!=-1") + //.setUserIds(Arrays.asList("user-0", "user-1")) + .setTimeout(20000) + .setCallback(new PushCallback() { + @Override + public void onResult(PushResult result) { + System.err.println("\n\n" + result); + } + }); + FutureTask future = sender.send(context); LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(30)); } From ee9b8f6c244f4ccf627a9e5ce4bb49d8d376f18d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Tue, 13 Dec 2016 19:29:05 +0800 Subject: [PATCH 774/890] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E5=88=B0v0.6.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Changelog.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 7464b677..fd062bc7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,7 +7,9 @@ 5. 修复connId高并发下可能重复的bug 6. 增加压测代码,优化测试模块 7. 配置文件优化,增加相应的注释说明 -8. 其他bug fix及代码优化 +8. 修复流控发送计数引起的bug +9. 优化内存占用,连接断开后立即释放Connection的引用 +10. 其他bug fix及代码优化 From 9a719602643d1c5293ba13d7ecb92c017cd91325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 14 Dec 2016 14:19:57 +0800 Subject: [PATCH 775/890] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E6=B5=81=E9=87=8F=E6=95=B4=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 2fe3ed7b..3b819311 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -53,7 +53,7 @@ mp { } traffic-shaping { //流量整形配置 gateway-client { - enabled:true + enabled:false check-interval:100ms write-global-limit:30k read-global-limit:0 @@ -62,7 +62,7 @@ mp { } gateway-server { - enabled:true + enabled:false check-interval:100ms write-global-limit:0 read-global-limit:30k From 71d202ba4fd8a350bf3f678967cdbb80011f6415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 14 Dec 2016 14:21:21 +0800 Subject: [PATCH 776/890] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84Keep-alive=E8=AE=BE=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/core/server/ConnectionServer.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index d3b8cb94..c4f43e4a 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -38,6 +38,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import static com.mpush.tools.config.CC.mp.net.traffic_shaping.connect_server.*; import static com.mpush.tools.thread.ThreadNames.T_TRAFFIC_SHAPING; @@ -125,12 +126,6 @@ protected void initPipeline(ChannelPipeline pipeline) { protected void initOptions(ServerBootstrap b) { super.initOptions(b); - /** - * 你可以设置这里指定的通道实现的配置参数。 - * 我们正在写一个TCP/IP的服务端, - * 因此我们被允许设置socket的参数选项比如tcpNoDelay和keepAlive。 - * 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。 - */ b.option(ChannelOption.SO_BACKLOG, 1024); /** From e137bc2a0d3c2c9e076ac733eb722a36be612943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 14 Dec 2016 14:27:51 +0800 Subject: [PATCH 777/890] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84Keep-alive=E8=AE=BE=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/core/server/GatewayUDPConnector.java | 4 ++-- .../com/mpush/netty/server/NettyTCPServer.java | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java index 04b1261d..a5aa1c83 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java +++ b/mpush-core/src/main/java/com/mpush/core/server/GatewayUDPConnector.java @@ -76,8 +76,8 @@ public void init() { protected void initOptions(Bootstrap b) { super.initOptions(b); b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);//默认情况下,当本机发送组播数据到某个网络接口时,在IP层,数据会回送到本地的回环接口,选项IP_MULTICAST_LOOP用于控制数据是否回送到本地的回环接口 - b.option(ChannelOption.IP_MULTICAST_TTL, 255);//选项IP_MULTICAST_TTL允许设置超时TTL,范围为0~255之间的任何值,例如: - //b.option(ChannelOption.IP_MULTICAST_IF, null);//选项IP_MULTICAST_IF用于设置组播的默认默认网络接口,会从给定的网络接口发送,另一个网络接口会忽略此数据,参数addr是希望多播输出接口的IP地址,使用INADDR_ANY地址回送到默认接口。 + b.option(ChannelOption.IP_MULTICAST_TTL, 255);//选项IP_MULTICAST_TTL允许设置超时TTL,范围为0~255之间的任何值 + //b.option(ChannelOption.IP_MULTICAST_IF, null);//选项IP_MULTICAST_IF用于设置组播的默认网络接口,会从给定的网络接口发送,另一个网络接口会忽略此数据,参数addr是希望多播输出接口的IP地址,使用INADDR_ANY地址回送到默认接口。 //b.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 1024 * 1024)); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index cd570bb3..be4d5a9f 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -206,15 +206,13 @@ private void createEpollServer(Listener listener) { createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } + /*** + * option()是提供给NioServerSocketChannel用来接收进来的连接。 + * childOption()是提供给由父管道ServerChannel接收到的连接, + * 在这个例子中也是NioServerSocketChannel。 + */ protected void initOptions(ServerBootstrap b) { - - /*** - * option()是提供给NioServerSocketChannel用来接收进来的连接。 - * childOption()是提供给由父管道ServerChannel接收到的连接, - * 在这个例子中也是NioServerSocketChannel。 - */ - b.childOption(ChannelOption.SO_KEEPALIVE, true); - + //b.childOption(ChannelOption.SO_KEEPALIVE, false);// 使用应用层心跳 /** * 在Netty 4中实现了一个新的ByteBuf内存池,它是一个纯Java版本的 jemalloc (Facebook也在用)。 From 65f3c52d75a493df6565d1547d502bc0970b3392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 14 Dec 2016 14:35:39 +0800 Subject: [PATCH 778/890] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84Keep-alive=E8=AE=BE=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/netty/client/NettyTCPClient.java | 2 -- .../src/main/java/com/mpush/test/client/ConnClientBoot.java | 1 - 2 files changed, 3 deletions(-) diff --git a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java index aa2af5ce..ee16175f 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/client/NettyTCPClient.java @@ -64,11 +64,9 @@ private void createClient(Listener listener, EventLoopGroup workerGroup, Class() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { diff --git a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java index 2ce0ff5d..4857b3df 100644 --- a/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java +++ b/mpush-test/src/main/java/com/mpush/test/client/ConnClientBoot.java @@ -75,7 +75,6 @@ public void onFailure(Throwable cause) { bootstrap.group(workerGroup)// .option(ChannelOption.TCP_NODELAY, true)// .option(ChannelOption.SO_REUSEADDR, true)// - .option(ChannelOption.SO_KEEPALIVE, true)// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60 * 1000) .channel(NioSocketChannel.class); From 415467ffd28bb593f0975213ec3e73a1613aafeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 14 Dec 2016 16:03:14 +0800 Subject: [PATCH 779/890] =?UTF-8?q?=E4=BF=AE=E6=94=B9EventBus=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=9D=9E=E5=9B=BA=E5=AE=9A=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/thread/pool/DefaultExecutorFactory.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index c2f6396f..e308d084 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -83,10 +83,11 @@ public Executor get(String name) { break; case EVENT_BUS: config = ThreadPoolConfig - .buildFixed(T_EVENT_BUS, - CC.mp.thread.pool.event_bus.min, - CC.mp.thread.pool.event_bus.queue_size - ); + .build(T_EVENT_BUS) + .setCorePoolSize(CC.mp.thread.pool.event_bus.min) + .setMaxPoolSize(CC.mp.thread.pool.event_bus.max) + .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) + .setQueueCapacity(CC.mp.thread.pool.event_bus.queue_size); break; case MQ: config = ThreadPoolConfig From 8b6d2b2e6f76025709cbd62086657bb749c90f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 14 Dec 2016 23:23:41 +0800 Subject: [PATCH 780/890] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=8F=8A=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/protocol/Command.java | 3 +- .../mpush/api/spi/common/ExecutorFactory.java | 1 + .../mpush/bootstrap/job/HttpProxyBoot.java | 1 - .../com/mpush/bootstrap/job/ServerBoot.java | 37 +++++------ .../java/com/mpush/bootstrap/job/ZKBoot.java | 4 ++ .../redis/listener/ListenerDispatcher.java | 22 ++++++- .../cache/redis/manager/RedisManager.java | 4 +- .../redis/manager/ZKRedisClusterManager.java | 2 +- .../com/mpush/cache/redis/mq/Subscriber.java | 9 +-- .../connection/GatewayConnectionFactory.java | 4 +- .../GatewayTCPConnectionFactory.java | 14 ++-- .../GatewayUDPConnectionFactory.java | 20 +++--- .../com/mpush/client/push/PushRequest.java | 65 ++++++++++--------- .../client/user/UserStatusChangeListener.java | 4 +- .../java/com/mpush/core/push/PushCenter.java | 18 ++--- .../core/router/RouterChangeListener.java | 2 +- .../com/mpush/core/server/AdminServer.java | 13 ++++ .../mpush/core/server/ConnectionServer.java | 11 ++++ .../core/server/ServerConnectionManager.java | 17 +++-- .../monitor/quota/impl/JVMThreadPool.java | 6 +- .../mpush/monitor/service/MonitorService.java | 4 +- .../netty/connection/NettyConnection.java | 5 ++ .../mpush/netty/server/NettyTCPServer.java | 63 ++++++++---------- .../mpush/netty/udp/NettyUDPConnector.java | 31 ++++----- .../mpush/netty/udp/UDPChannelHandler.java | 11 ++-- .../java/com/mpush/test/redis/PubSubTest.java | 13 ++-- .../com/mpush/test/sever/ServerTestMain.java | 8 ++- .../tools/thread/NamedThreadFactory.java | 2 +- .../com/mpush/tools/thread/ThreadNames.java | 13 ++-- .../thread/pool/DefaultExecutorFactory.java | 46 +++---------- .../tools/thread/pool/ThreadPoolManager.java | 56 ++++------------ .../src/main/java/com/mpush/zk/ZKClient.java | 12 ++-- .../mpush/zk/listener/ZKDnsNodeWatcher.java | 13 +--- .../zk/listener/ZKServerNodeWatcher.java | 4 +- 34 files changed, 264 insertions(+), 274 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java index 26326eee..d4a7a2af 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Command.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Command.java @@ -56,9 +56,8 @@ public enum Command { public final byte cmd; - private static final Command[] values = values(); - public static Command toCMD(byte b) { + Command[] values = values(); if (b > 0 && b < values.length) return values[b - 1]; return UNKNOWN; } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java index 8aa7c53f..742c8b55 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java @@ -33,6 +33,7 @@ public interface ExecutorFactory { String SERVER_WORK = "sw"; String HTTP_CLIENT_WORK = "hcw"; String PUSH_CALLBACK = "pc"; + String PUSH_TIMER = "pt"; String EVENT_BUS = "eb"; String MQ = "r"; String BIZ = "b"; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java index 7192ed3d..3d15b8ef 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/HttpProxyBoot.java @@ -20,7 +20,6 @@ package com.mpush.bootstrap.job; import com.mpush.api.spi.net.DnsMappingManager; -import com.mpush.core.push.PushCenter; import com.mpush.netty.http.NettyHttpClient; import com.mpush.tools.config.CC; diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 43cda219..05dbc740 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -44,32 +44,31 @@ public ServerBoot(Server server, ZKServerNode node) { @Override public void start() { - String serverName = server.getClass().getSimpleName(); - ThreadPoolManager.I.newThread(serverName, () -> { - server.init(); - server.start(new Listener() { - @Override - public void onSuccess(Object... args) { - Logs.Console.info("start {} success listen:{}", serverName, args[0]); - if (node != null) {//注册应用到zk - ZKRegister.build().setEphemeral(true).setNode(node).register(); - Logs.Console.info("register server node={} to zk path={}", node, node.getNodePath()); - } - startNext(); + server.init(); + server.start(new Listener() { + @Override + public void onSuccess(Object... args) { + Logs.Console.info("start {} success on:{}", server.getClass().getSimpleName(), args[0]); + if (node != null) {//注册应用到zk + ZKRegister.build().setEphemeral(true).setNode(node).register(); + Logs.ZK.info("register {} to zk server success.", node); } + startNext(); + } - @Override - public void onFailure(Throwable cause) { - Logs.Console.error("start " + serverName + " failure, jvm exit with code -1", cause); - System.exit(-1); - } - }); - }).start(); + @Override + public void onFailure(Throwable cause) { + Logs.Console.error("start {} failure, jvm exit with code -1", server.getClass().getSimpleName(), cause); + System.exit(-1); + } + }); } @Override protected void stop() { + Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); server.stop().join(); + Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); stopNext(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java index 861b3f64..77647fef 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ZKBoot.java @@ -21,6 +21,7 @@ import com.mpush.api.service.Listener; import com.mpush.bootstrap.BootException; +import com.mpush.tools.log.Logs; import com.mpush.zk.ZKClient; /** @@ -32,9 +33,11 @@ public final class ZKBoot extends BootJob { @Override protected void start() { + Logs.Console.info("init zk client waiting for connected..."); ZKClient.I.start(new Listener() { @Override public void onSuccess(Object... args) { + Logs.Console.info("init zk client success..."); startNext(); } @@ -48,6 +51,7 @@ public void onFailure(Throwable cause) { @Override protected void stop() { ZKClient.I.stop(); + Logs.Console.info("zk client closed..."); stopNext(); } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java index 75b45f0e..ffef5551 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/listener/ListenerDispatcher.java @@ -32,12 +32,26 @@ public class ListenerDispatcher implements MessageListener { - public static final ListenerDispatcher I = new ListenerDispatcher(); + private static ListenerDispatcher I; private final Map> subscribes = Maps.newTreeMap(); private final Executor executor = ThreadPoolManager.I.getRedisExecutor(); + private final Subscriber subscriber = new Subscriber(); + + + public static ListenerDispatcher I() { + if (I == null) { + synchronized (ListenerDispatcher.class) { + if (I == null) { + I = new ListenerDispatcher(); + } + } + } + return I; + } + private ListenerDispatcher() { } @@ -55,6 +69,10 @@ public void onMessage(final String channel, final String message) { public void subscribe(String channel, MessageListener listener) { subscribes.computeIfAbsent(channel, k -> Lists.newArrayList()).add(listener); - RedisManager.I.subscribe(Subscriber.holder, channel); + RedisManager.I.subscribe(subscriber, channel); + } + + public Subscriber getSubscriber() { + return subscriber; } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java index eaf43fcb..fbb8ccdd 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/RedisManager.java @@ -41,7 +41,7 @@ public final class RedisManager { private RedisConnectionFactory factory = new RedisConnectionFactory(); public void init() { - Logs.Console.info("begin init redis..."); + Logs.REDIS.info("begin init redis..."); RedisClusterManager clusterManager = new ZKRedisClusterManager(); clusterManager.init(); factory.setPassword(CC.mp.redis.password); @@ -50,7 +50,7 @@ public void init() { factory.setCluster(CC.mp.redis.isCluster()); factory.init(); test(); - Logs.Console.info("init redis success..."); + Logs.REDIS.info("init redis success..."); } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java index a6ece011..749f1b60 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/manager/ZKRedisClusterManager.java @@ -74,7 +74,7 @@ private void register(List nodes) { || !ZKClient.I.isExisted(REDIS_SERVER.getRootPath())//redis节点不存在 || !ZKClient.I.get(REDIS_SERVER.getRootPath()).equals(data)) {//数据有变更 ZKClient.I.registerPersist(REDIS_SERVER.getRootPath(), data); - Logs.Console.info("register redis server group success, group={}", data); + Logs.REDIS.info("register redis server group success, group={}", data); } } } diff --git a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java index 95afbcf6..154b705b 100644 --- a/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java +++ b/mpush-cache/src/main/java/com/mpush/cache/redis/mq/Subscriber.java @@ -24,14 +24,9 @@ import com.mpush.tools.log.Logs; import redis.clients.jedis.JedisPubSub; -public class Subscriber extends JedisPubSub { +public final class Subscriber extends JedisPubSub { - private static ListenerDispatcher dispatcher = ListenerDispatcher.I; - - public static Subscriber holder = new Subscriber(); - - private Subscriber() { - } + private ListenerDispatcher dispatcher = ListenerDispatcher.I(); @Override public void onMessage(String channel, String message) { diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java index 40d07033..c7f6d28e 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayConnectionFactory.java @@ -52,8 +52,8 @@ public void init(Listener listener) { abstract public Connection getConnection(String hostAndPort); - abstract public void send(String hostAndPort, Function creator, Function sender); + abstract public boolean send(String hostAndPort, Function creator, Consumer sender); - abstract public void broadcast(Function creator, Consumer sender); + abstract public boolean broadcast(Function creator, Consumer sender); } diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java index 3abdf1f1..15946325 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayTCPConnectionFactory.java @@ -77,15 +77,19 @@ public Connection getConnection(String hostAndPort) { @Override - public void send(String hostAndPort, Function creator, Function sender) { - creator.compose(this::getConnection) - .andThen(sender) - .apply(hostAndPort); + public boolean send(String hostAndPort, Function creator, Consumer sender) { + Connection connection = getConnection(hostAndPort); + if (connection == null) return false;// gateway server 找不到,直接返回推送失败 + + sender.accept(creator.apply(connection)); + return true; } @Override - public void broadcast(Function creator, Consumer sender) { + public boolean broadcast(Function creator, Consumer sender) { + if (ip_client.isEmpty()) return false; ip_client.forEach((s, client) -> sender.accept(creator.apply(client.getConnection()))); + return true; } private void restartClient(final GatewayClient client) { diff --git a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java index 5b5f37f5..d57d64e9 100644 --- a/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java +++ b/mpush-client/src/main/java/com/mpush/client/gateway/connection/GatewayUDPConnectionFactory.java @@ -85,23 +85,21 @@ public Connection getConnection(String hostAndPort) { @SuppressWarnings("unchecked") @Override - public void send(String hostAndPort, Function creator, Function sender) { + public boolean send(String hostAndPort, Function creator, Consumer sender) { InetSocketAddress recipient = ip_address.get(hostAndPort); - if (recipient == null) {// gateway server 找不到,直接返回推送失败 - creator.apply(null); - return; - } - - creator.compose(this::getConnection) - .andThen(message -> (T) message.setRecipient(recipient)) - .andThen(sender) - .apply(hostAndPort); + if (recipient == null) return false;// gateway server 找不到,直接返回推送失败 + + M message = creator.apply(connector.getConnection()); + message.setRecipient(recipient); + sender.accept(message); + return true; } @Override - public void broadcast(Function creator, Consumer sender) { + public boolean broadcast(Function creator, Consumer sender) { M message = creator.apply(connector.getConnection()); message.setRecipient(multicastRecipient); sender.accept(message); + return true; } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 1037534a..b56bd8d2 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -43,7 +43,7 @@ * * @author ohun@live.cn */ -public class PushRequest extends FutureTask { +public final class PushRequest extends FutureTask { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); private static final Callable NONE = () -> Boolean.FALSE; @@ -78,38 +78,39 @@ private void sendToConnServer(RemoteRouter remoteRouter) { return; } + timeLine.addTimePoint("check-gateway-conn"); //2.通过网关连接,把消息发送到所在机器 - connectionFactory.send( + boolean success = connectionFactory.send( location.getHostAndPort(), - connection -> { - timeLine.addTimePoint("check-gateway-conn"); - if (connection == null) { - LOGGER.error("get gateway connection failure, location={}", location); - failure(); - return null; - } - - return GatewayPushMessage.build(connection) - .setUserId(userId) - .setContent(content) - .setClientType(location.getClientType()) - .setTimeout(timeout - 500) - .setTags(tags) - .addFlag(ackModel.flag); - }, + connection -> GatewayPushMessage + .build(connection) + .setUserId(userId) + .setContent(content) + .setClientType(location.getClientType()) + .setTimeout(timeout - 500) + .setTags(tags) + .addFlag(ackModel.flag) + , pushMessage -> { - if (pushMessage != null) { - timeLine.addTimePoint("send-to-gateway-begin"); - pushMessage.sendRaw(f -> { - timeLine.addTimePoint("send-to-gateway-end"); - if (!f.isSuccess()) failure(); - }); - PushRequest.this.content = null;//释放内存 - future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); - } - return null; + timeLine.addTimePoint("send-to-gateway-begin"); + pushMessage.sendRaw(f -> { + timeLine.addTimePoint("send-to-gateway-end"); + if (f.isSuccess()) { + LOGGER.debug("send to gateway server success, location={}, conn={}", location, f.channel()); + } else { + LOGGER.error("send to gateway server failure, location={}, conn={}", location, f.channel(), f.cause()); + failure(); + } + }); + PushRequest.this.content = null;//释放内存 + future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); } ); + + if (!success) { + LOGGER.error("get gateway connection failure, location={}", location); + failure(); + } } private void submit(Status status) { @@ -170,7 +171,7 @@ public FutureTask offline() { public FutureTask broadcast() { timeLine.begin(); - connectionFactory.broadcast( + boolean success = connectionFactory.broadcast( connection -> GatewayPushMessage .build(connection) .setUserId(userId) @@ -188,6 +189,7 @@ public FutureTask broadcast() { LOGGER.error("send broadcast to gateway server failure, userId={}, conn={}", userId, f.channel(), f.cause()); } }); + if (pushMessage.taskId == null) { future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); } else { @@ -196,6 +198,11 @@ public FutureTask broadcast() { } ); + if (!success) { + LOGGER.error("get gateway connection failure when broadcast."); + failure(); + } + return this; } diff --git a/mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java b/mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java index e43375bf..95041bc1 100644 --- a/mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java +++ b/mpush-client/src/main/java/com/mpush/client/user/UserStatusChangeListener.java @@ -40,8 +40,8 @@ public class UserStatusChangeListener implements MessageListener { //只需要一台机器注册online、offline 消息通道 public UserStatusChangeListener() { if ("127.0.0.1".equals(Utils.getLocalIp())) { - ListenerDispatcher.I.subscribe(ONLINE_CHANNEL, this); - ListenerDispatcher.I.subscribe(OFFLINE_CHANNEL, this); + ListenerDispatcher.I().subscribe(ONLINE_CHANNEL, this); + ListenerDispatcher.I().subscribe(OFFLINE_CHANNEL, this); } else { LOGGER.error("UserChangeListener is not localhost,required:{}, but:{}", "127.0.0.1", Utils.getLocalIp()); } diff --git a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java index 9fd69683..46fe25ab 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java @@ -19,21 +19,15 @@ package com.mpush.core.push; -import com.mpush.api.push.PushException; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; -import com.mpush.tools.config.CC; -import com.mpush.tools.thread.NamedPoolThreadFactory; +import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static com.mpush.tools.config.CC.mp.thread.pool.push_center.min; -import static com.mpush.tools.thread.ThreadNames.T_PUSH_CENTER_TIMER; - /** * Created by ohun on 16/10/24. * @@ -50,6 +44,7 @@ private PushCenter() { public void addTask(PushTask task) { executor.execute(task); + logger.debug("add new task to push center, task={}", task); } public void delayTask(int delay, PushTask task) { @@ -58,18 +53,15 @@ public void delayTask(int delay, PushTask task) { @Override protected void doStart(Listener listener) throws Throwable { - executor = new ScheduledThreadPoolExecutor(min, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), - (r, e) -> { - logger.error("one push task was rejected, task=" + r); - throw new PushException("one push request was rejected. request=" + r); - } - ); + executor = ThreadPoolManager.I.getPushCenterTimer(); + logger.info("push center start success"); listener.onSuccess(); } @Override protected void doStop(Listener listener) throws Throwable { executor.shutdown(); + logger.info("push center stop success"); listener.onSuccess(); } } diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index ecd9787f..e80e3f4f 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -55,7 +55,7 @@ public final class RouterChangeListener extends EventConsumer implements Message public RouterChangeListener() { if (!udpGateway) { - ListenerDispatcher.I.subscribe(getKickChannel(), this); + ListenerDispatcher.I().subscribe(getKickChannel(), this); } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java index 7addbf8c..cce72438 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/AdminServer.java @@ -22,6 +22,7 @@ import com.mpush.core.handler.AdminHandler; import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; +import com.mpush.tools.thread.ThreadNames; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.DelimiterBasedFrameDecoder; @@ -29,6 +30,8 @@ import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; +import java.util.concurrent.ThreadFactory; + public final class AdminServer extends NettyTCPServer { private static AdminServer I; @@ -78,4 +81,14 @@ protected ChannelHandler getEncoder() { protected int getWorkThreadNum() { return 1; } + + @Override + protected String getBossThreadName() { + return ThreadNames.T_ADMIN_BOSS; + } + + @Override + protected String getWorkThreadName() { + return ThreadNames.T_ADMIN_WORKER; + } } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index c4f43e4a..6442d506 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -29,6 +29,7 @@ import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; +import com.mpush.tools.thread.ThreadNames; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -114,6 +115,16 @@ protected int getWorkThreadNum() { return CC.mp.thread.pool.work.max; } + @Override + protected String getBossThreadName() { + return ThreadNames.T_CONN_BOSS; + } + + @Override + protected String getWorkThreadName() { + return ThreadNames.T_CONN_WORKER; + } + @Override protected void initPipeline(ChannelPipeline pipeline) { super.initPipeline(pipeline); diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index ddbcb0d4..8612cf76 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -46,10 +46,12 @@ public final class ServerConnectionManager implements ConnectionManager { private final ConcurrentMap connections = new ConcurrentHashMap<>(); private final ConnectionHolder DEFAULT = new SimpleConnectionHolder(null); private final boolean heartbeatCheck; + private final ConnectionHolderFactory holderFactory; private HashedWheelTimer timer; public ServerConnectionManager(boolean heartbeatCheck) { this.heartbeatCheck = heartbeatCheck; + this.holderFactory = heartbeatCheck ? HeartbeatCheckTask::new : SimpleConnectionHolder::new; } @Override @@ -80,11 +82,7 @@ public Connection get(Channel channel) { @Override public void add(Connection connection) { - if (heartbeatCheck) { - connections.putIfAbsent(connection.getChannel().id(), new HeartbeatCheckTask(connection)); - } else { - connections.putIfAbsent(connection.getChannel().id(), new SimpleConnectionHolder(connection)); - } + connections.putIfAbsent(connection.getChannel().id(), holderFactory.create(connection)); } @Override @@ -140,6 +138,8 @@ private HeartbeatCheckTask(Connection connection) { } void startTimeout() { + Connection connection = this.connection; + if (connection != null && connection.isConnected()) { int timeout = connection.getSessionContext().heartbeat; timer.newTimeout(this, timeout, TimeUnit.MILLISECONDS); @@ -148,6 +148,8 @@ void startTimeout() { @Override public void run(Timeout timeout) throws Exception { + Connection connection = this.connection; + if (connection == null || !connection.isConnected()) { Logs.HB.info("heartbeat timeout times={}, connection disconnected, conn={}", timeoutTimes, connection); return; @@ -180,4 +182,9 @@ public Connection get() { return connection; } } + + @FunctionalInterface + private interface ConnectionHolderFactory { + ConnectionHolder create(Connection connection); + } } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index 52bcd554..e6af9142 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -40,8 +40,10 @@ public Object monitor(Object... args) { Map pool = ThreadPoolManager.I.getActivePools(); for (Map.Entry entry : pool.entrySet()) { String serviceName = entry.getKey(); - ThreadPoolExecutor executor = (ThreadPoolExecutor) entry.getValue(); - map.put(serviceName, ThreadPoolManager.getPoolInfo(executor)); + Executor executor = entry.getValue(); + if (executor instanceof ThreadPoolExecutor) { + map.put(serviceName, ThreadPoolManager.getPoolInfo((ThreadPoolExecutor) executor)); + } } return map; } diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java index cd221568..e93d8620 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/service/MonitorService.java @@ -27,6 +27,7 @@ import com.mpush.tools.common.JVMUtil; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; +import com.mpush.tools.thread.ThreadNames; import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,7 +81,8 @@ public void run() { @Override protected void doStart(Listener listener) throws Throwable { if (printLog || dumpEnabled) { - thread = ThreadPoolManager.I.newThread("monitor", this); + thread = ThreadPoolManager.I.newThread(ThreadNames.T_MONITOR, this); + thread.setDaemon(true); thread.start(); } listener.onSuccess(); diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index 81d6a48e..a648473b 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -99,6 +99,11 @@ public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { //return channel.newPromise().setFailure(new RuntimeException("send data too busy")); return future.awaitUninterruptibly(); } else { + if (listener != null) { + channel.newPromise() + .addListener(listener) + .setFailure(new RuntimeException("connection is disconnected")); + } return this.close(); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index be4d5a9f..539d6054 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -26,7 +26,6 @@ import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketEncoder; import com.mpush.tools.config.CC; -import com.mpush.tools.log.Logs; import com.mpush.tools.thread.ThreadNames; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -41,6 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicReference; /** @@ -79,13 +79,13 @@ public boolean isRunning() { public void stop(Listener listener) { if (!serverState.compareAndSet(State.Started, State.Shutdown)) { if (listener != null) listener.onFailure(new ServiceException("server was already shutdown.")); - Logs.Console.error("{} was already shutdown.", this.getClass().getSimpleName()); + logger.error("{} was already shutdown.", this.getClass().getSimpleName()); return; } - Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); + logger.info("try shutdown {}...", this.getClass().getSimpleName()); if (bossGroup != null) bossGroup.shutdownGracefully().syncUninterruptibly();//要先关闭接收连接的main reactor if (workerGroup != null) workerGroup.shutdownGracefully().syncUninterruptibly();//再关闭处理业务的sub reactor - Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); + logger.info("{} shutdown success.", this.getClass().getSimpleName()); if (listener != null) { listener.onSuccess(port); } @@ -157,51 +157,34 @@ public void initChannel(SocketChannel ch) throws Exception {//每连上一个链 /*** * 绑定端口并启动去接收进来的连接 */ - ChannelFuture f = b.bind(port).sync().addListener(future -> { + b.bind(port).addListener(future -> { if (future.isSuccess()) { - Logs.Console.info("server start success on:{}", port); + serverState.set(State.Started); + logger.info("server start success on:{}", port); if (listener != null) listener.onSuccess(port); } else { - Logs.Console.error("server start failure on:{}", port, future.cause()); + logger.error("server start failure on:{}", port, future.cause()); if (listener != null) listener.onFailure(future.cause()); } }); - if (f.isSuccess()) { - serverState.set(State.Started); - /** - * 这里会一直等待,直到socket被关闭 - */ - f.channel().closeFuture().sync(); - } - } catch (Exception e) { logger.error("server start exception", e); if (listener != null) listener.onFailure(e); throw new ServiceException("server start exception, port=" + port, e); - } finally { - if (isRunning()) stop(null); } } private void createNioServer(Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup( - getBossThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_BOSS) - ); - NioEventLoopGroup workerGroup = new NioEventLoopGroup( - getWorkThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) - ); + NioEventLoopGroup bossGroup = new NioEventLoopGroup(getBossThreadNum(), getBossThreadFactory()); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(getWorkThreadNum(), getWorkThreadFactory()); bossGroup.setIoRatio(100); workerGroup.setIoRatio(getIoRate()); createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } private void createEpollServer(Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup( - getBossThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_BOSS) - ); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup( - getWorkThreadNum(), new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) - ); + EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(getBossThreadNum(), getBossThreadFactory()); + EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(getWorkThreadNum(), getWorkThreadFactory()); workerGroup.setIoRatio(getIoRate()); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -258,20 +241,30 @@ protected void initPipeline(ChannelPipeline pipeline) { * * @return */ + protected ThreadFactory getBossThreadFactory() { + return new DefaultThreadFactory(getBossThreadName()); + } + + protected ThreadFactory getWorkThreadFactory() { + return new DefaultThreadFactory(getWorkThreadName()); + } + protected int getBossThreadNum() { return 1; } - /** - * netty 默认的Executor为ThreadPerTaskExecutor - * 线程池的使用在SingleThreadEventExecutor#doStartThread - * - * @return - */ protected int getWorkThreadNum() { return 0; } + protected String getBossThreadName() { + return ThreadNames.T_BOSS; + } + + protected String getWorkThreadName() { + return ThreadNames.T_WORKER; + } + protected int getIoRate() { return 70; } diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java index 1885dc69..0b31a203 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/NettyUDPConnector.java @@ -63,9 +63,9 @@ protected void doStart(Listener listener) throws Throwable { @Override protected void doStop(Listener listener) throws Throwable { - Logs.Console.info("try shutdown {}...", this.getClass().getSimpleName()); + logger.info("try shutdown {}...", this.getClass().getSimpleName()); if (eventLoopGroup != null) eventLoopGroup.shutdownGracefully().syncUninterruptibly(); - Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); + logger.info("{} shutdown success.", this.getClass().getSimpleName()); listener.onSuccess(port); } @@ -80,29 +80,26 @@ private void createServer(Listener listener, EventLoopGroup eventLoopGroup, Chan initOptions(b); - ChannelFuture f = b.bind(port).sync();//直接绑定端口,不要指定host,不然收不到组播消息 - - if (f.isSuccess()) { - Logs.Console.info("udp server start success on:{}", port); - if (listener != null) listener.onSuccess(port); - - f.channel().closeFuture().sync(); - } else { - Logs.Console.error("udp server start failure on:{}", port, f.cause()); - if (listener != null) listener.onFailure(f.cause()); - } + //直接绑定端口,不要指定host,不然收不到组播消息 + b.bind(port).addListener(future -> { + if (future.isSuccess()) { + logger.info("udp server start success on:{}", port); + if (listener != null) listener.onSuccess(port); + } else { + logger.error("udp server start failure on:{}", port, future.cause()); + if (listener != null) listener.onFailure(future.cause()); + } + }); } catch (Exception e) { logger.error("udp server start exception", e); if (listener != null) listener.onFailure(e); throw new ServiceException("udp server start exception, port=" + port, e); - } finally { - if (isRunning()) stop(); } } private void createNioServer(Listener listener) { NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup( - 0, new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) + 0, new DefaultThreadFactory(ThreadNames.T_GATEWAY_WORKER) ); createServer(listener, eventLoopGroup, () -> new NioDatagramChannel(IPv4));//默认是根据机器情况创建Channel,如果机器支持ipv6,则无法使用ipv4的地址加入组播 } @@ -110,7 +107,7 @@ private void createNioServer(Listener listener) { @SuppressWarnings("unused") private void createEpollServer(Listener listener) { EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup( - 0, new DefaultThreadFactory(ThreadNames.T_SERVER_WORKER) + 0, new DefaultThreadFactory(ThreadNames.T_GATEWAY_WORKER) ); createServer(listener, eventLoopGroup, EpollDatagramChannel::new); } diff --git a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java index 915db217..da1f4312 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java +++ b/mpush-netty/src/main/java/com/mpush/netty/udp/UDPChannelHandler.java @@ -59,13 +59,13 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { if (multicastAddress != null) { ((DatagramChannel) ctx.channel()).joinGroup(multicastAddress, networkInterface, null).addListener(future -> { if (future.isSuccess()) { - Logs.CONN.info("join multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); + LOGGER.info("join multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); } else { LOGGER.error("join multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); } }); } - Logs.CONN.info("init udp channel={}", ctx.channel()); + LOGGER.info("init udp channel={}", ctx.channel()); } @Override @@ -74,13 +74,13 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (multicastAddress != null) { ((DatagramChannel) ctx.channel()).leaveGroup(multicastAddress, networkInterface, null).addListener(future -> { if (future.isSuccess()) { - Logs.CONN.info("leave multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); + LOGGER.info("leave multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); } else { LOGGER.error("leave multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); } }); } - Logs.CONN.info("disconnect udp channel={}, connection={}", ctx.channel(), connection); + LOGGER.info("disconnect udp channel={}, connection={}", ctx.channel(), connection); } @Override @@ -94,8 +94,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { connection.close(); - Logs.CONN.error("udp channel caught an exception channel={}, connection={}", ctx.channel(), connection); - LOGGER.error("caught an ex, channel={}, connection={}", ctx.channel(), connection, cause); + LOGGER.error("udp handler caught an exception, channel={}, conn={}", ctx.channel(), connection, cause); } public UDPChannelHandler setMulticastAddress(InetAddress multicastAddress) { diff --git a/mpush-test/src/main/java/com/mpush/test/redis/PubSubTest.java b/mpush-test/src/main/java/com/mpush/test/redis/PubSubTest.java index 5f1246c0..0db30c58 100644 --- a/mpush-test/src/main/java/com/mpush/test/redis/PubSubTest.java +++ b/mpush-test/src/main/java/com/mpush/test/redis/PubSubTest.java @@ -19,6 +19,7 @@ package com.mpush.test.redis; +import com.mpush.cache.redis.listener.ListenerDispatcher; import com.mpush.cache.redis.manager.RedisManager; import com.mpush.cache.redis.mq.Subscriber; import org.junit.Before; @@ -34,8 +35,8 @@ public void init() { @Test public void subpubTest() { - RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); - RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + RedisManager.I.subscribe(ListenerDispatcher.I().getSubscriber(), "/hello/123"); + RedisManager.I.subscribe(ListenerDispatcher.I().getSubscriber(), "/hello/124"); RedisManager.I.publish("/hello/123", "123"); RedisManager.I.publish("/hello/124", "124"); } @@ -44,8 +45,8 @@ public void subpubTest() { public void pubsubTest() { RedisManager.I.publish("/hello/123", "123"); RedisManager.I.publish("/hello/124", "124"); - RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); - RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + RedisManager.I.subscribe(ListenerDispatcher.I().getSubscriber(), "/hello/123"); + RedisManager.I.subscribe(ListenerDispatcher.I().getSubscriber(), "/hello/124"); } @Test @@ -56,8 +57,8 @@ public void pubTest() { @Test public void subTest() { - RedisManager.I.subscribe(Subscriber.holder, "/hello/123"); - RedisManager.I.subscribe(Subscriber.holder, "/hello/124"); + RedisManager.I.subscribe(ListenerDispatcher.I().getSubscriber(), "/hello/123"); + RedisManager.I.subscribe(ListenerDispatcher.I().getSubscriber(), "/hello/124"); LockSupport.park(); } diff --git a/mpush-test/src/main/java/com/mpush/test/sever/ServerTestMain.java b/mpush-test/src/main/java/com/mpush/test/sever/ServerTestMain.java index 327f8223..a0201874 100644 --- a/mpush-test/src/main/java/com/mpush/test/sever/ServerTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/sever/ServerTestMain.java @@ -32,15 +32,19 @@ public class ServerTestMain { public static void main(String[] args) { - new ServerTestMain().testServer(); + start(); } @Test public void testServer() { + start(); + LockSupport.park(); + } + + public static void start() { System.setProperty("io.netty.leakDetection.level", "PARANOID"); System.setProperty("io.netty.noKeySetOptimization", "false"); Main.main(null); - LockSupport.park(); } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java index 12ccdf65..fb6abbd1 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/NamedThreadFactory.java @@ -58,7 +58,7 @@ public NamedThreadFactory(final String namePrefix) { */ public Thread newThread(String name, Runnable r) { Thread thread = new Thread(group, r, namePrefix + "-" + threadNumber.getAndIncrement() + "-" + name); - thread.setDaemon(false); //设置为非守护线程,否则jvm会立即退出 + thread.setDaemon(true); //设置为非守护线程,否则jvm会立即退出 return thread; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index 251960f4..03166688 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -22,11 +22,16 @@ public final class ThreadNames { public static final String NS = "mp"; public static final String THREAD_NAME_PREFIX = NS + "-t"; - public static final String T_SERVER_BOSS = NS + "-boss"; - public static final String T_SERVER_WORKER = NS + "-worker"; + public static final String T_BOSS = NS + "-boss"; + public static final String T_WORKER = NS + "-boss"; + public static final String T_CONN_BOSS = NS + "-conn-boss"; + public static final String T_ADMIN_BOSS = NS + "-admin-boss"; + public static final String T_CONN_WORKER = NS + "-conn-work"; + public static final String T_ADMIN_WORKER = NS + "-admin-work"; + public static final String T_GATEWAY_WORKER = NS + "-gateway-work"; public static final String T_TRAFFIC_SHAPING = NS + "-traffic-shaping"; public static final String T_TCP_CLIENT = NS + "-tcp-client"; - public static final String T_HTTP_CLIENT = NS + "-http"; + public static final String T_HTTP_CLIENT = NS + "-http-client-work"; public static final String T_EVENT_BUS = NS + "-event"; public static final String T_MQ = NS + "-mq"; public static final String T_ZK = NS + "-zk"; @@ -35,9 +40,9 @@ public final class ThreadNames { public static final String T_PUSH_REQ_TIMER = NS + "-push-req-timer"; public static final String T_ARK_REQ_TIMER = NS + "-ack-timer"; public static final String T_PUSH_CENTER_TIMER = NS + "-push-center-timer"; - public static final String T_MONITOR = NS + "-monitor"; public static final String T_CONN_TIMER = NS + "-conn-check-timer"; public static final String T_HTTP_TIMER = NS + "-http-client-timer"; public static final String T_HTTP_DNS_TIMER = NS + "-http-dns-timer"; + public static final String T_MONITOR = "monitor"; } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index e308d084..5edd663a 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -19,16 +19,15 @@ package com.mpush.tools.thread.pool; +import com.mpush.api.push.PushException; import com.mpush.api.spi.Spi; import com.mpush.api.spi.common.ExecutorFactory; import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; +import static com.mpush.tools.config.CC.mp.thread.pool.push_center.min; import static com.mpush.tools.thread.ThreadNames.*; /** @@ -57,30 +56,6 @@ private Executor get(ThreadPoolConfig config) { public Executor get(String name) { final ThreadPoolConfig config; switch (name) { - case SERVER_BOSS: - config = ThreadPoolConfig - .build(T_SERVER_BOSS) - .setCorePoolSize(CC.mp.thread.pool.boss.min) - .setMaxPoolSize(CC.mp.thread.pool.boss.max) - .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) - .setQueueCapacity(CC.mp.thread.pool.boss.queue_size); - break; - case SERVER_WORK: - config = ThreadPoolConfig - .build(T_SERVER_WORKER) - .setCorePoolSize(CC.mp.thread.pool.work.min) - .setMaxPoolSize(CC.mp.thread.pool.work.max) - .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) - .setQueueCapacity(CC.mp.thread.pool.work.queue_size); - break; - case HTTP_CLIENT_WORK: - config = ThreadPoolConfig - .buildFixed(T_HTTP_CLIENT, - CC.mp.thread.pool.http_proxy.min, - CC.mp.thread.pool.http_proxy.queue_size - ) - .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_DISCARD); - break; case EVENT_BUS: config = ThreadPoolConfig .build(T_EVENT_BUS) @@ -105,15 +80,14 @@ public Executor get(String name) { .setQueueCapacity(CC.mp.thread.pool.push_callback.queue_size) .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); break; + case PUSH_TIMER: + return new ScheduledThreadPoolExecutor(min, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), + (r, e) -> { + throw new PushException("one push request was rejected. task=" + r); + } + ); default: - case BIZ: - config = ThreadPoolConfig - .build(T_BIZ) - .setCorePoolSize(CC.mp.thread.pool.biz.min) - .setMaxPoolSize(CC.mp.thread.pool.biz.max) - .setKeepAliveSeconds(TimeUnit.MINUTES.toSeconds(5)) - .setQueueCapacity(CC.mp.thread.pool.biz.queue_size); - break; + throw new IllegalArgumentException("no executor for " + name); } return get(config); diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 0bde5ae5..4e7b2466 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -25,6 +25,8 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadPoolExecutor; public final class ThreadPoolManager { @@ -33,28 +35,15 @@ public final class ThreadPoolManager { private final ExecutorFactory executorFactory = ExecutorFactory.create(); private final NamedThreadFactory threadFactory = new NamedThreadFactory(); - private Executor bossExecutor; - private Executor workExecutor; - private Executor bizExecutor; private Executor eventBusExecutor; private Executor redisExecutor; - private Executor httpExecutor; private Executor pushCallbackExecutor; - private Executor pushCenterExecutor; + private ScheduledExecutorService pushCenterTimer; public final Thread newThread(String name, Runnable target) { return threadFactory.newThread(name, target); } - public Executor getHttpExecutor() { - if (httpExecutor == null) { - synchronized (this) { - httpExecutor = executorFactory.get(ExecutorFactory.HTTP_CLIENT_WORK); - } - } - return httpExecutor; - } - public Executor getRedisExecutor() { if (redisExecutor == null) { synchronized (this) { @@ -73,51 +62,30 @@ public Executor getEventBusExecutor() { return eventBusExecutor; } - public Executor getBizExecutor() { - if (bizExecutor == null) { - synchronized (this) { - bizExecutor = executorFactory.get(ExecutorFactory.BIZ); - } - } - return bizExecutor; - } - - public Executor getWorkExecutor() { - if (workExecutor == null) { - synchronized (this) { - workExecutor = executorFactory.get(ExecutorFactory.SERVER_WORK); - } - } - return workExecutor; - } - - public Executor getBossExecutor() { - if (bossExecutor == null) { + public Executor getPushCallbackExecutor() { + if (pushCallbackExecutor == null) { synchronized (this) { - bossExecutor = executorFactory.get(ExecutorFactory.SERVER_BOSS); + pushCallbackExecutor = executorFactory.get(ExecutorFactory.PUSH_CALLBACK); } } - return bossExecutor; + return pushCallbackExecutor; } - public Executor getPushCallbackExecutor() { - if (pushCallbackExecutor == null) { + public ScheduledExecutorService getPushCenterTimer() { + if (pushCenterTimer == null) { synchronized (this) { - pushCallbackExecutor = executorFactory.get(ExecutorFactory.PUSH_CALLBACK); + pushCenterTimer = (ScheduledExecutorService) executorFactory.get(ExecutorFactory.PUSH_TIMER); } } - return pushCallbackExecutor; + return pushCenterTimer; } public Map getActivePools() { Map map = new HashMap<>(); - if (bossExecutor != null) map.put("bossExecutor", bossExecutor); - if (workExecutor != null) map.put("workExecutor", workExecutor); - if (bizExecutor != null) map.put("bizExecutor", bizExecutor); if (eventBusExecutor != null) map.put("eventBusExecutor", eventBusExecutor); if (redisExecutor != null) map.put("redisExecutor", redisExecutor); - if (httpExecutor != null) map.put("httpExecutor", httpExecutor); if (pushCallbackExecutor != null) map.put("pushCallbackExecutor", pushCallbackExecutor); + if (pushCenterTimer != null) map.put("pushCenterTimer", pushCenterTimer); return map; } diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 37e3da8e..f996e9cd 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -36,7 +36,10 @@ import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; -import java.util.*; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; public class ZKClient extends BaseService { @@ -69,14 +72,13 @@ public void start(Listener listener) { @Override protected void doStart(Listener listener) throws Throwable { client.start(); - Logs.Console.info("init zk client waiting for connected..."); + Logs.ZK.info("init zk client waiting for connected..."); if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) { throw new ZKException("init zk error, config=" + zkConfig); } initLocalCache(zkConfig.getWatchPath()); addConnectionStateListener(); Logs.ZK.info("zk client start success, server lists is:{}", zkConfig.getHosts()); - Logs.Console.info("init zk client success..."); listener.onSuccess(zkConfig.getHosts()); } @@ -85,7 +87,7 @@ protected void doStop(Listener listener) throws Throwable { if (cache != null) cache.close(); TimeUnit.MILLISECONDS.sleep(600); client.close(); - Logs.Console.info("zk client closed..."); + Logs.ZK.info("zk client closed..."); listener.onSuccess(); } @@ -136,7 +138,7 @@ public List getAclForPath(final String path) { }); } client = builder.build(); - Logs.Console.info("init zk client, config={}", zkConfig.toString()); + Logs.ZK.info("init zk client, config={}", zkConfig.toString()); } // 注册连接状态监听器 diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java index d8304750..b05d5b47 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKDnsNodeWatcher.java @@ -19,20 +19,13 @@ package com.mpush.zk.listener; -import com.google.common.base.Strings; import com.mpush.tools.Jsons; import com.mpush.tools.log.Logs; import com.mpush.zk.ZKClient; import com.mpush.zk.ZKPath; import com.mpush.zk.cache.ZKDnsNodeCache; -import com.mpush.zk.cache.ZKRedisNodeCache; import com.mpush.zk.node.ZKDnsNode; -import com.mpush.zk.node.ZKRedisNode; -import com.mpush.zk.node.ZKServerNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.util.Arrays; import java.util.List; /** @@ -40,20 +33,18 @@ */ public class ZKDnsNodeWatcher extends ZKNodeCacheWatcher { - private final Logger logger = LoggerFactory.getLogger(ZKDnsNodeWatcher.class); - private final ZKDnsNodeCache cache = new ZKDnsNodeCache(); @Override protected void beforeWatch() { - Logs.Console.info("start init zk dns data"); + Logs.ZK.info("start init zk dns data"); List rawData = ZKClient.I.getChildrenKeys(ZKPath.DNS_MAPPING.getRootPath()); for (String raw : rawData) { String fullPath = ZKPath.DNS_MAPPING.getFullPath(raw); cache.put(fullPath, getZKNode(fullPath)); } - Logs.Console.info("end init zk dns data"); + Logs.ZK.info("end init zk dns data"); } private ZKDnsNode getZKNode(String fullPath) { diff --git a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java index a88b6c14..dd9f3bab 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java +++ b/mpush-zk/src/main/java/com/mpush/zk/listener/ZKServerNodeWatcher.java @@ -78,14 +78,14 @@ public String watchPath() { @Override protected void beforeWatch() { - Logs.Console.error("start init zk server data"); + Logs.ZK.info("start init zk server data"); List rawData = ZKClient.I.getChildrenKeys(path.getRootPath()); for (String raw : rawData) { String fullPath = path.getFullPath(raw); ZKServerNode app = getServerNode(fullPath); cache.put(fullPath, app); } - Logs.Console.error("end init zk server data"); + Logs.ZK.info("end init zk server data"); } private ZKServerNode getServerNode(String fullPath) { From 331f65a1b8761579003d8edc17fc9458b13bb119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 15 Dec 2016 15:06:22 +0800 Subject: [PATCH 781/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPushRequest=E9=87=8CT?= =?UTF-8?q?imeLine=E5=A4=9A=E7=BA=BF=E7=A8=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/client/push/PushRequest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index b56bd8d2..186a68f7 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -115,15 +115,13 @@ private void sendToConnServer(RemoteRouter remoteRouter) { private void submit(Status status) { if (this.status.compareAndSet(Status.init, status)) {//防止重复调用 + timeLine.end(); if (future != null) future.cancel(true); if (callback != null) { PushRequestBus.I.asyncCall(this); - } else { - LOGGER.warn("callback is null"); } super.set(this.status.get() == Status.success); } - timeLine.end(); LOGGER.info("push request {} end, userId={}, content={}, location={}, timeLine={}" , status, userId, content, location, timeLine); } From 3e016b949668b334173e1b1ef925d731899bc207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 15 Dec 2016 22:57:58 +0800 Subject: [PATCH 782/890] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E9=85=8D=E7=BD=AE=E5=8F=8A=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 48 ++--------- .../mpush/api/spi/common/ExecutorFactory.java | 5 +- .../com/mpush/client/push/PushRequestBus.java | 23 ++--- .../com/mpush/core/ack/AckMessageQueue.java | 32 ++++--- .../java/com/mpush/core/push/PushCenter.java | 5 +- .../mpush/core/server/ConnectionServer.java | 11 ++- .../monitor/quota/impl/JVMThreadPool.java | 16 ++-- .../com/mpush/netty/http/NettyHttpClient.java | 6 +- .../mpush/netty/server/NettyTCPServer.java | 4 + .../mpush/test/push/PushClientTestMain.java | 47 +++++----- .../main/java/com/mpush/tools/config/CC.java | 51 ++--------- .../com/mpush/tools/thread/ThreadNames.java | 5 +- .../thread/pool/DefaultExecutorFactory.java | 35 ++++---- .../tools/thread/pool/ThreadPoolManager.java | 86 +++++++++++-------- 14 files changed, 171 insertions(+), 203 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 3b819311..3dad203f 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -141,53 +141,23 @@ mp { #线程池配置 thread { pool { - boss { //netty server boss - min:1 //boss 只需要一个线程即可 - max:1 - queue-size:0 - } - - work { //netty server boss - min:0 //0表示线程数根据cpu核数动态调整(2*cpu) - max:0 - queue-size:0 - } - - event-bus { - min:4 - max:4 - queue-size:10000 //大量的online,offline, - } - - http-proxy { - min:0 //0表示线程数根据cpu核数动态调整(2*cpu) - max:0 - queue-size:0 - } + conn-work:0 //0表示线程数根据cpu核数动态调整(2*cpu) + http-work:0 //http proxy netty client work pool + push-task:2 //消息推送任务处理 + push-client:4 //消息推送回调处理,该线程池在客户端运行 + ack-timer:1 //处理ACK消息超时 - biz { //其他业务 + event-bus { //用户处理内部事件分发 min:1 - max:4 - queue-size:16 + max:16 + queue-size:10000 //大量的online,offline, } mq { //用户上下线消息, 踢人等 - min:2 + min:1 max:4 queue-size:10000 } - - push-callback { //消息推送 - min:2 - max:2 - queue-size:0 - } - - push-center { //消息推送 - min:4 - max:4 - queue-size:0 - } } } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java index 742c8b55..d8a8f8f7 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java @@ -32,8 +32,9 @@ public interface ExecutorFactory { String SERVER_BOSS = "sb"; String SERVER_WORK = "sw"; String HTTP_CLIENT_WORK = "hcw"; - String PUSH_CALLBACK = "pc"; - String PUSH_TIMER = "pt"; + String PUSH_CLIENT = "pc"; + String PUSH_TASK = "pt"; + String ACK_TIMER = "at"; String EVENT_BUS = "eb"; String MQ = "r"; String BIZ = "b"; diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index 9437ec3f..d82c9a08 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -19,18 +19,17 @@ package com.mpush.client.push; -import com.mpush.api.push.PushException; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; -import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; -import java.util.concurrent.*; - -import static com.mpush.tools.thread.ThreadNames.T_PUSH_REQ_TIMER; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * Created by ohun on 2015/12/30. @@ -41,7 +40,6 @@ public class PushRequestBus extends BaseService { public static final PushRequestBus I = new PushRequestBus(); private final Logger logger = LoggerFactory.getLogger(PushRequestBus.class); private final Map reqQueue = new ConcurrentHashMap<>(1024); - private Executor executor; private ScheduledExecutorService scheduledExecutor; private PushRequestBus() { @@ -57,23 +55,20 @@ public PushRequest getAndRemove(int sessionId) { } public void asyncCall(Runnable runnable) { - executor.execute(runnable); + scheduledExecutor.execute(runnable); } @Override protected void doStart(Listener listener) throws Throwable { - executor = ThreadPoolManager.I.getPushCallbackExecutor(); - scheduledExecutor = new ScheduledThreadPoolExecutor(1, new NamedPoolThreadFactory(T_PUSH_REQ_TIMER), (r, e) -> { - logger.error("one push request was rejected, request=" + r); - throw new PushException("one push request was rejected. request=" + r); - }); + scheduledExecutor = ThreadPoolManager.I.getPushClientTimer(); listener.onSuccess(); } @Override protected void doStop(Listener listener) throws Throwable { - scheduledExecutor.shutdown(); - ((ExecutorService) executor).shutdown(); + if (scheduledExecutor != null) { + scheduledExecutor.shutdown(); + } listener.onSuccess(); } } diff --git a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java index ccbc5f64..d7fb6232 100644 --- a/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java +++ b/mpush-core/src/main/java/com/mpush/core/ack/AckMessageQueue.java @@ -19,33 +19,32 @@ package com.mpush.core.ack; -import com.mpush.tools.thread.NamedPoolThreadFactory; +import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; +import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.*; - -import static com.mpush.tools.thread.ThreadNames.T_ARK_REQ_TIMER; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * Created by ohun on 16/9/5. * * @author ohun@live.cn (夜色) */ -public final class AckMessageQueue { +public final class AckMessageQueue extends BaseService { private final Logger logger = LoggerFactory.getLogger(AckMessageQueue.class); private static final int DEFAULT_TIMEOUT = 3000; public static final AckMessageQueue I = new AckMessageQueue(); private final ConcurrentMap queue = new ConcurrentHashMap<>(); - private final ScheduledExecutorService scheduledExecutor; + private ScheduledExecutorService scheduledExecutor; private AckMessageQueue() { - scheduledExecutor = new ScheduledThreadPoolExecutor(1, - new NamedPoolThreadFactory(T_ARK_REQ_TIMER), - (r, e) -> logger.error("one ack context was rejected, context=" + r) - ); } public void add(int sessionId, AckContext context, int timeout) { @@ -61,4 +60,17 @@ public AckContext getAndRemove(int sessionId) { return queue.remove(sessionId); } + @Override + protected void doStart(Listener listener) throws Throwable { + scheduledExecutor = ThreadPoolManager.I.getAckTimer(); + super.doStart(listener); + } + + @Override + protected void doStop(Listener listener) throws Throwable { + if (scheduledExecutor != null) { + scheduledExecutor.shutdown(); + } + super.doStop(listener); + } } diff --git a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java index 46fe25ab..fee8db7c 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java +++ b/mpush-core/src/main/java/com/mpush/core/push/PushCenter.java @@ -21,6 +21,7 @@ import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; +import com.mpush.core.ack.AckMessageQueue; import com.mpush.tools.thread.pool.ThreadPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +54,8 @@ public void delayTask(int delay, PushTask task) { @Override protected void doStart(Listener listener) throws Throwable { - executor = ThreadPoolManager.I.getPushCenterTimer(); + executor = ThreadPoolManager.I.getPushTaskTimer(); + AckMessageQueue.I.start(); logger.info("push center start success"); listener.onSuccess(); } @@ -61,6 +63,7 @@ protected void doStart(Listener listener) throws Throwable { @Override protected void doStop(Listener listener) throws Throwable { executor.shutdown(); + AckMessageQueue.I.stop(); logger.info("push center stop success"); listener.onSuccess(); } diff --git a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java index 6442d506..4260cf30 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ConnectionServer.java @@ -30,6 +30,7 @@ import com.mpush.tools.config.CC; import com.mpush.tools.thread.NamedPoolThreadFactory; import com.mpush.tools.thread.ThreadNames; +import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; @@ -100,6 +101,14 @@ public void init() { } } + @Override + public void start(Listener listener) { + super.start(listener); + if (this.workerGroup != null) {// 增加线程池监控 + ThreadPoolManager.I.register("conn-worker", this.workerGroup); + } + } + @Override public void stop(Listener listener) { super.stop(listener); @@ -112,7 +121,7 @@ public void stop(Listener listener) { @Override protected int getWorkThreadNum() { - return CC.mp.thread.pool.work.max; + return CC.mp.thread.pool.conn_work; } @Override diff --git a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java index e6af9142..d88a522f 100644 --- a/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java +++ b/mpush-monitor/src/main/java/com/mpush/monitor/quota/impl/JVMThreadPool.java @@ -21,30 +21,34 @@ import com.mpush.monitor.quota.ThreadPoolQuota; import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.channel.EventLoopGroup; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; +import static com.mpush.tools.thread.pool.ThreadPoolManager.getPoolInfo; + public class JVMThreadPool implements ThreadPoolQuota { public static final JVMThreadPool I = new JVMThreadPool(); private JVMThreadPool() { } - @Override public Object monitor(Object... args) { - Map map = new HashMap<>(); - Map pool = ThreadPoolManager.I.getActivePools(); - for (Map.Entry entry : pool.entrySet()) { + Map result = new HashMap<>(); + Map pools = ThreadPoolManager.I.getActivePools(); + for (Map.Entry entry : pools.entrySet()) { String serviceName = entry.getKey(); Executor executor = entry.getValue(); if (executor instanceof ThreadPoolExecutor) { - map.put(serviceName, ThreadPoolManager.getPoolInfo((ThreadPoolExecutor) executor)); + result.put(serviceName, getPoolInfo((ThreadPoolExecutor) executor)); + } else if (executor instanceof EventLoopGroup) { + result.put(serviceName, getPoolInfo((EventLoopGroup) executor)); } } - return map; + return result; } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java index 1c54ee80..804b1067 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java +++ b/mpush-netty/src/main/java/com/mpush/netty/http/NettyHttpClient.java @@ -44,6 +44,7 @@ import java.net.URI; import java.util.concurrent.TimeUnit; +import static com.mpush.tools.config.CC.mp.thread.pool.http_work; import static com.mpush.tools.thread.ThreadNames.T_HTTP_TIMER; import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; import static io.netty.handler.codec.http.HttpHeaderNames.HOST; @@ -129,10 +130,7 @@ private void writeRequest(Channel channel, RequestContext context) { @Override protected void doStart(Listener listener) throws Throwable { - workerGroup = new NioEventLoopGroup( - CC.mp.thread.pool.http_proxy.max, - new DefaultThreadFactory(ThreadNames.T_HTTP_CLIENT) - ); + workerGroup = new NioEventLoopGroup(http_work, new DefaultThreadFactory(ThreadNames.T_HTTP_CLIENT)); b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index 539d6054..dd4fdb6e 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -280,4 +280,8 @@ protected boolean useNettyEpoll() { } return false; } + + public EventLoopGroup getWorkerGroup() { + return workerGroup; + } } diff --git a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java index 4164cb71..bee7de11 100644 --- a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java @@ -46,30 +46,33 @@ public void testPush() throws Exception { PushSender sender = PushSender.create(); sender.start().join(); - PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); - msg.setMsgId("msgId_0"); + for (int i = 0; i < 10; i++) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } + PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); + msg.setMsgId("msgId_" + i); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } - PushContext context = PushContext.build(msg) - .setAckModel(AckModel.AUTO_ACK) - //.setUserId("user-0") - .setBroadcast(true) - //.setTags(Sets.newHashSet("test")) - //.setCondition("tags&&tags.indexOf('test')!=-1") - //.setUserIds(Arrays.asList("user-0", "user-1")) - .setTimeout(20000) - .setCallback(new PushCallback() { - @Override - public void onResult(PushResult result) { - System.err.println("\n\n" + result); - } - }); - FutureTask future = sender.send(context); + PushContext context = PushContext.build(msg) + .setAckModel(AckModel.AUTO_ACK) + .setUserId("user-" + i) + .setBroadcast(false) + //.setTags(Sets.newHashSet("test")) + //.setCondition("tags&&tags.indexOf('test')!=-1") + //.setUserIds(Arrays.asList("user-0", "user-1")) + .setTimeout(2000) + .setCallback(new PushCallback() { + @Override + public void onResult(PushResult result) { + System.err.println("\n\n" + result); + } + }); + FutureTask future = sender.send(context); + } LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(30)); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index 5a3e23ee..c9c0c14c 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -164,21 +164,11 @@ interface pool { Config cfg = thread.cfg.getObject("pool").toConfig(); - interface boss { - Config cfg = pool.cfg.getObject("boss").toConfig(); - int min = cfg.getInt("min"); - int max = cfg.getInt("max"); - int queue_size = cfg.getInt("queue-size"); - - } - - interface work { - Config cfg = pool.cfg.getObject("work").toConfig(); - int min = cfg.getInt("min"); - int max = cfg.getInt("max"); - int queue_size = cfg.getInt("queue-size"); - - } + int conn_work = cfg.getInt("conn-work"); + int http_work = cfg.getInt("http-work"); + int push_task = cfg.getInt("push-task"); + int push_client = cfg.getInt("push-client"); + int ack_timer = cfg.getInt("ack-timer"); interface event_bus { Config cfg = pool.cfg.getObject("event-bus").toConfig(); @@ -188,42 +178,11 @@ interface event_bus { } - interface http_proxy { - Config cfg = pool.cfg.getObject("http-proxy").toConfig(); - int min = cfg.getInt("min"); - int max = cfg.getInt("max"); - int queue_size = cfg.getInt("queue-size"); - - } - - interface biz { - Config cfg = pool.cfg.getObject("biz").toConfig(); - int min = cfg.getInt("min"); - int max = cfg.getInt("max"); - int queue_size = cfg.getInt("queue-size"); - - } - interface mq { Config cfg = pool.cfg.getObject("mq").toConfig(); int min = cfg.getInt("min"); int max = cfg.getInt("max"); int queue_size = cfg.getInt("queue-size"); - - } - - interface push_callback { - Config cfg = pool.cfg.getObject("push-callback").toConfig(); - int min = cfg.getInt("min"); - int max = cfg.getInt("max"); - int queue_size = cfg.getInt("queue-size"); - } - - interface push_center { - Config cfg = pool.cfg.getObject("push-center").toConfig(); - int min = cfg.getInt("min"); - int max = cfg.getInt("max"); - int queue_size = cfg.getInt("queue-size"); } } } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java index 03166688..36ab997e 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/ThreadNames.java @@ -34,11 +34,8 @@ public final class ThreadNames { public static final String T_HTTP_CLIENT = NS + "-http-client-work"; public static final String T_EVENT_BUS = NS + "-event"; public static final String T_MQ = NS + "-mq"; - public static final String T_ZK = NS + "-zk"; - public static final String T_BIZ = NS + "-biz"; - public static final String T_PUSH_CALLBACK = NS + "-push-callback"; - public static final String T_PUSH_REQ_TIMER = NS + "-push-req-timer"; public static final String T_ARK_REQ_TIMER = NS + "-ack-timer"; + public static final String T_PUSH_CLIENT_TIMER = NS + "-push-client-timer"; public static final String T_PUSH_CENTER_TIMER = NS + "-push-center-timer"; public static final String T_CONN_TIMER = NS + "-conn-check-timer"; public static final String T_HTTP_TIMER = NS + "-http-client-timer"; diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index 5edd663a..68919566 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -23,11 +23,14 @@ import com.mpush.api.spi.Spi; import com.mpush.api.spi.common.ExecutorFactory; import com.mpush.tools.config.CC; +import com.mpush.tools.log.Logs; import com.mpush.tools.thread.NamedPoolThreadFactory; import java.util.concurrent.*; -import static com.mpush.tools.config.CC.mp.thread.pool.push_center.min; +import static com.mpush.tools.config.CC.mp.thread.pool.ack_timer; +import static com.mpush.tools.config.CC.mp.thread.pool.push_client; +import static com.mpush.tools.config.CC.mp.thread.pool.push_task; import static com.mpush.tools.thread.ThreadNames.*; /** @@ -66,26 +69,26 @@ public Executor get(String name) { break; case MQ: config = ThreadPoolConfig - .buildFixed(T_MQ, - CC.mp.thread.pool.mq.min, - CC.mp.thread.pool.mq.queue_size - ); - break; - case PUSH_CALLBACK: - config = ThreadPoolConfig - .build(T_PUSH_CALLBACK) - .setCorePoolSize(CC.mp.thread.pool.push_callback.min) - .setMaxPoolSize(CC.mp.thread.pool.push_callback.max) + .build(T_MQ) + .setCorePoolSize(CC.mp.thread.pool.mq.min) + .setMaxPoolSize(CC.mp.thread.pool.mq.max) .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) - .setQueueCapacity(CC.mp.thread.pool.push_callback.queue_size) - .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); + .setQueueCapacity(CC.mp.thread.pool.mq.queue_size); break; - case PUSH_TIMER: - return new ScheduledThreadPoolExecutor(min, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), + case PUSH_CLIENT: + return new ScheduledThreadPoolExecutor(push_client, new NamedPoolThreadFactory(T_PUSH_CLIENT_TIMER), + (r, e) -> r.run() // run caller thread + ); + case PUSH_TASK: + return new ScheduledThreadPoolExecutor(push_task, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), (r, e) -> { - throw new PushException("one push request was rejected. task=" + r); + throw new PushException("one push task was rejected. task=" + r); } ); + case ACK_TIMER: + return new ScheduledThreadPoolExecutor(ack_timer, new NamedPoolThreadFactory(T_ARK_REQ_TIMER), + (r, e) -> Logs.PUSH.error("one ack context was rejected, context=" + r) + ); default: throw new IllegalArgumentException("no executor for " + name); } diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java index 4e7b2466..40d4c3ca 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/ThreadPoolManager.java @@ -21,11 +21,16 @@ import com.mpush.api.spi.common.ExecutorFactory; import com.mpush.tools.thread.NamedThreadFactory; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.SingleThreadEventLoop; +import io.netty.util.concurrent.EventExecutor; +import io.netty.util.concurrent.ThreadProperties; import java.util.HashMap; import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadPoolExecutor; @@ -35,62 +40,47 @@ public final class ThreadPoolManager { private final ExecutorFactory executorFactory = ExecutorFactory.create(); private final NamedThreadFactory threadFactory = new NamedThreadFactory(); - private Executor eventBusExecutor; - private Executor redisExecutor; - private Executor pushCallbackExecutor; - private ScheduledExecutorService pushCenterTimer; + private final Map pools = new ConcurrentHashMap<>(); public final Thread newThread(String name, Runnable target) { return threadFactory.newThread(name, target); } public Executor getRedisExecutor() { - if (redisExecutor == null) { - synchronized (this) { - redisExecutor = executorFactory.get(ExecutorFactory.MQ); - } - } - return redisExecutor; + return pools.computeIfAbsent("mq", s -> executorFactory.get(ExecutorFactory.MQ)); } public Executor getEventBusExecutor() { - if (eventBusExecutor == null) { - synchronized (this) { - eventBusExecutor = executorFactory.get(ExecutorFactory.EVENT_BUS); - } - } - return eventBusExecutor; + return pools.computeIfAbsent("event-bus", s -> executorFactory.get(ExecutorFactory.EVENT_BUS)); } - public Executor getPushCallbackExecutor() { - if (pushCallbackExecutor == null) { - synchronized (this) { - pushCallbackExecutor = executorFactory.get(ExecutorFactory.PUSH_CALLBACK); - } - } - return pushCallbackExecutor; + public ScheduledExecutorService getPushClientTimer() { + return (ScheduledExecutorService) pools.computeIfAbsent("push-client-timer" + , s -> executorFactory.get(ExecutorFactory.PUSH_CLIENT)); } - public ScheduledExecutorService getPushCenterTimer() { - if (pushCenterTimer == null) { - synchronized (this) { - pushCenterTimer = (ScheduledExecutorService) executorFactory.get(ExecutorFactory.PUSH_TIMER); - } - } - return pushCenterTimer; + public ScheduledExecutorService getPushTaskTimer() { + return (ScheduledExecutorService) pools.computeIfAbsent("push-task-timer" + , s -> executorFactory.get(ExecutorFactory.PUSH_TASK)); + } + + public ScheduledExecutorService getAckTimer() { + return (ScheduledExecutorService) pools.computeIfAbsent("ack-timer" + , s -> executorFactory.get(ExecutorFactory.ACK_TIMER)); + } + + public void register(String name, Executor executor) { + Objects.requireNonNull(name); + Objects.requireNonNull(executor); + pools.put(name, executor); } public Map getActivePools() { - Map map = new HashMap<>(); - if (eventBusExecutor != null) map.put("eventBusExecutor", eventBusExecutor); - if (redisExecutor != null) map.put("redisExecutor", redisExecutor); - if (pushCallbackExecutor != null) map.put("pushCallbackExecutor", pushCallbackExecutor); - if (pushCenterTimer != null) map.put("pushCenterTimer", pushCenterTimer); - return map; + return pools; } public static Map getPoolInfo(ThreadPoolExecutor executor) { - Map info = new HashMap<>(); + Map info = new HashMap<>(5); info.put("corePoolSize", executor.getCorePoolSize()); info.put("maxPoolSize", executor.getMaximumPoolSize()); info.put("activeCount(workingThread)", executor.getActiveCount()); @@ -98,4 +88,24 @@ public static Map getPoolInfo(ThreadPoolExecutor executor) { info.put("queueSize(blockedTask)", executor.getQueue().size()); return info; } + + public static Map getPoolInfo(EventLoopGroup executors) { + Map info = new HashMap<>(3); + int poolSize = 0, queueSize = 0, activeCount = 0; + for (EventExecutor e : executors) { + poolSize++; + if (e instanceof SingleThreadEventLoop) { + SingleThreadEventLoop executor = (SingleThreadEventLoop) e; + queueSize += executor.pendingTasks(); + ThreadProperties tp = executor.threadProperties(); + if (tp.state() == Thread.State.RUNNABLE) { + activeCount++; + } + } + } + info.put("poolSize(workThread)", poolSize); + info.put("activeCount(workingThread)", activeCount); + info.put("queueSize(blockedTask)", queueSize); + return info; + } } From d2eede1e69d8a34bbc2d9ab2c350115e538f4dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 16:28:51 +0800 Subject: [PATCH 783/890] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E9=85=8D=E7=BD=AE=E5=8F=8A=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/tools/thread/pool/DefaultExecutorFactory.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index 68919566..6b3d27d6 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -65,7 +65,8 @@ public Executor get(String name) { .setCorePoolSize(CC.mp.thread.pool.event_bus.min) .setMaxPoolSize(CC.mp.thread.pool.event_bus.max) .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) - .setQueueCapacity(CC.mp.thread.pool.event_bus.queue_size); + .setQueueCapacity(CC.mp.thread.pool.event_bus.queue_size) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); break; case MQ: config = ThreadPoolConfig @@ -73,7 +74,9 @@ public Executor get(String name) { .setCorePoolSize(CC.mp.thread.pool.mq.min) .setMaxPoolSize(CC.mp.thread.pool.mq.max) .setKeepAliveSeconds(TimeUnit.SECONDS.toSeconds(10)) - .setQueueCapacity(CC.mp.thread.pool.mq.queue_size); + .setQueueCapacity(CC.mp.thread.pool.mq.queue_size) + .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); + ; break; case PUSH_CLIENT: return new ScheduledThreadPoolExecutor(push_client, new NamedPoolThreadFactory(T_PUSH_CLIENT_TIMER), From 9b62629367b2dbad8d68b2260d38cd0c40a470d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 16:30:02 +0800 Subject: [PATCH 784/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/set-env.sh | 19 +++++++++++++------ .../core/server/ServerChannelHandler.java | 5 +++-- .../core/server/ServerConnectionManager.java | 8 +++++++- .../java/com/mpush/tools/common/Profiler.java | 8 ++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/bin/set-env.sh b/bin/set-env.sh index 81854e2c..2f7b5396 100644 --- a/bin/set-env.sh +++ b/bin/set-env.sh @@ -1,22 +1,29 @@ #!/usr/bin/env bash +#1. Netty 相关关设置项 -#io.netty.leakDetection.level +#-Dio.netty.leakDetection.level #netty的内存泄露检测分为四级: #DISABLED: 不进行内存泄露的检测; #SIMPLE: 抽样检测,且只对部分方法调用进行记录,消耗较小,有泄漏时可能会延迟报告,默认级别; #ADVANCED: 抽样检测,记录对象最近几次的调用记录,有泄漏时可能会延迟报告; #PARANOID: 每次创建一个对象时都进行泄露检测,且会记录对象最近的详细调用记录。是比较激进的内存泄露检测级别,消耗最大,建议只在测试时使用。 -JVM_FLAGS="-Dio.netty.leakDetection.level=advanced" -#Dio.netty.noKeySetOptimization +#-Dio.netty.selectorAutoRebuildThreshold=512 默认512 +#在NIO中通过Selector的轮询当前是否有IO事件,根据JDK NIO api描述,Selector的select方法会一直阻塞,直到IO事件达到或超时,但是在Linux平台上这里有时会出现问题,在某些场景下select方法会直接返回,即使没有超时并且也没有IO事件到达,这就是著名的epoll bug,这是一个比较严重的bug,它会导致线程陷入死循环,会让CPU飙到100%,极大地影响系统的可靠性,到目前为止,JDK都没有完全解决这个问题。 +#但是Netty有效的规避了这个问题,经过实践证明,epoll bug已Netty框架解决,Netty的处理方式是这样的: +#记录select空转的次数,定义一个阀值,这个阀值默认是512,可以在应用层通过设置系统属性io.netty.selectorAutoRebuildThreshold传入,当空转的次数超过了这个阀值,重新构建新Selector,将老Selector上注册的Channel转移到新建的Selector上,关闭老Selector,用新的Selector代替老Selector,详细实现可以查看NioEventLoop中的selector和rebuildSelector方法: + +#-Dio.netty.noKeySetOptimization #是否禁用nio Selector.selectedKeys优化, 通过反射实现, 默认false -JVM_FLAGS="$JVM_FLAGS -Dio.netty.noKeySetOptimization=false" +JVM_FLAGS="-Dio.netty.leakDetection.level=advanced" + +#2. 开启远程调试 -#开启远程调试 #JVM_FLAGS="$JVM_FLAGS -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8008" -#GC配置 +#3. GC配置 + #运行模式 整个堆内存大小 GC算法 #JVM_FLAGS="$JVM_FLAGS -server -Xmx1024m -Xms1024m -XX:+UseG1GC -XX:MaxGCPauseMillis=200" #GC日志 发生OOM时创建堆内存转储文件 diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java index 355cd4bd..43180d4c 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerChannelHandler.java @@ -65,7 +65,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception byte cmd = packet.cmd; try { - Profiler.start("time cost on [channel read]: " + packet.toString()); + Profiler.start("time cost on [channel read]: ", packet.toString()); Connection connection = connectionManager.get(ctx.channel()); LOGGER.debug("channelRead conn={}, packet={}", ctx.channel(), connection.getSessionContext(), msg); connection.updateLastReadTime(); @@ -81,9 +81,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - Connection connection = connectionManager.removeAndClose(ctx.channel()); + Connection connection = connectionManager.get(ctx.channel()); Logs.CONN.error("client caught ex, conn={}", connection); LOGGER.error("caught an ex, channel={}, conn={}", ctx.channel(), connection, cause); + ctx.close(); } @Override diff --git a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java index 8612cf76..e42af6c3 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java +++ b/mpush-core/src/main/java/com/mpush/core/server/ServerConnectionManager.java @@ -22,6 +22,7 @@ import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; +import com.mpush.netty.connection.NettyConnection; import com.mpush.tools.config.CC; import com.mpush.tools.log.Logs; import com.mpush.tools.thread.NamedThreadFactory; @@ -93,7 +94,12 @@ public Connection removeAndClose(Channel channel) { holder.close(); return connection; } - return null; + + //add default + Connection connection = new NettyConnection(); + connection.init(channel, false); + connection.close(); + return connection; } @Override diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java index 67b3f0b4..56491e0c 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Profiler.java @@ -46,7 +46,7 @@ public static void enable(boolean enabled) { * 开始计时。 */ public static void start() { - start((String) null); + start(EMPTY_STRING); } /** @@ -54,8 +54,8 @@ public static void start() { * * @param message 第一个entry的信息 */ - public static void start(String message) { - if (enabled) entryStack.set(new Entry(message, null, null)); + public static void start(String message, Object... args) { + if (enabled) entryStack.set(new Entry(String.format(message, args), null, null)); } /** @@ -74,7 +74,7 @@ public static void start(Message message) { *

*/ public static void reset() { - entryStack.set(null); + entryStack.remove(); } /** From a9a8e059954c0d31517feb018d573557da23209b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 16:58:17 +0800 Subject: [PATCH 785/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0websocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/connection/SessionContext.java | 4 + .../java/com/mpush/api/protocol/Packet.java | 9 +- .../common/handler/BaseMessageHandler.java | 13 ++ .../com/mpush/common/message/AckMessage.java | 1 + .../mpush/core/handler/HandshakeHandler.java | 43 +++++- .../core/server/WebSocketChannelHandler.java | 68 ++++++++ .../mpush/core/server/WebSocketServer.java | 145 ++++++++++++++++++ .../java/com/mpush/tools/common/Reflects.java | 50 ++++++ 8 files changed, 325 insertions(+), 8 deletions(-) create mode 100644 mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java create mode 100644 mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/Reflects.java diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index a26042fe..9ebcce09 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -82,6 +82,10 @@ public int getClientType() { return clientType; } + public boolean isSecurity() { + return cipher != null; + } + @Override public String toString() { return "{" + diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index d84422db..9ab022ef 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -33,10 +33,11 @@ public class Packet { public static final int HEADER_LEN = 13; - public static final byte FLAG_CRYPTO = 0x01; - public static final byte FLAG_COMPRESS = 0x02; - public static final byte FLAG_BIZ_ACK = 0x04; - public static final byte FLAG_AUTO_ACK = 0x08; + public static final byte FLAG_CRYPTO = 1; + public static final byte FLAG_COMPRESS = 2; + public static final byte FLAG_BIZ_ACK = 4; + public static final byte FLAG_AUTO_ACK = 8; + public static final byte FLAG_JSON_BODY = 16; public static final byte HB_PACKET_BYTE = -33; public static final byte[] HB_PACKET_BYTES = new byte[]{HB_PACKET_BYTE}; diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index dcd78cc6..db2d2b6b 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -24,7 +24,11 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.tools.Jsons; import com.mpush.tools.common.Profiler; +import com.mpush.tools.common.Reflects; + +import static com.mpush.api.protocol.Packet.FLAG_JSON_BODY; /** * Created by ohun on 2015/12/22. @@ -32,6 +36,8 @@ * @author ohun@live.cn */ public abstract class BaseMessageHandler implements MessageHandler { + protected Class clazz = Reflects.getSuperClassGenericType(this.getClass(), 0); + public abstract T decode(Packet packet, Connection connection); public abstract void handle(T message); @@ -47,4 +53,11 @@ public void handle(Packet packet, Connection connection) { Profiler.release(); } } + + protected T decode0(Packet packet, Connection connection) { + if (packet.hasFlag(FLAG_JSON_BODY)) { + Jsons.fromJson(packet.body, clazz); + return Jsons.fromJson(packet.body, clazz); + } + } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java b/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java index b36b0075..61f452aa 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/AckMessage.java @@ -31,6 +31,7 @@ */ public final class AckMessage extends BaseMessage { + public AckMessage(Packet packet, Connection connection) { super(packet, connection); } diff --git a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java index 168d8c02..f6484884 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/HandshakeHandler.java @@ -22,7 +22,6 @@ import com.google.common.base.Strings; import com.mpush.api.connection.Connection; import com.mpush.api.connection.SessionContext; -import com.mpush.api.event.HandshakeEvent; import com.mpush.api.protocol.Packet; import com.mpush.common.handler.BaseMessageHandler; import com.mpush.common.message.ErrorMessage; @@ -33,7 +32,6 @@ import com.mpush.core.session.ReusableSession; import com.mpush.core.session.ReusableSessionManager; import com.mpush.tools.config.ConfigManager; -import com.mpush.tools.event.EventBus; import com.mpush.tools.log.Logs; /** @@ -50,7 +48,14 @@ public HandshakeMessage decode(Packet packet, Connection connection) { @Override public void handle(HandshakeMessage message) { + if (message.getConnection().getSessionContext().isSecurity()) { + doSecurity(message); + } else { + doInsecurity(message); + } + } + private void doSecurity(HandshakeMessage message) { byte[] iv = message.iv;//AES密钥向量16位 byte[] clientKey = message.clientKey;//客户端随机数16位 byte[] serverKey = CipherBox.I.randomAESKey();//服务端随机数16位 @@ -68,6 +73,7 @@ public void handle(HandshakeMessage message) { //2.重复握手判断 SessionContext context = message.getConnection().getSessionContext(); if (message.deviceId.equals(context.deviceId)) { + ErrorMessage.from(message).setReason("repeat handshake").send(); Logs.CONN.warn("handshake failure, repeat handshake, conn={}", message.getConnection()); return; } @@ -103,8 +109,37 @@ public void handle(HandshakeMessage message) { //9.保存可复用session到Redis, 用于快速重连 ReusableSessionManager.I.cacheSession(session); - //10.触发握手成功事件 - EventBus.I.post(new HandshakeEvent(message.getConnection(), heartbeat)); Logs.CONN.info("handshake success, conn={}", message.getConnection()); } + + private void doInsecurity(HandshakeMessage message) { + + //1.校验客户端消息字段 + if (Strings.isNullOrEmpty(message.deviceId)) { + ErrorMessage.from(message).setReason("Param invalid").close(); + Logs.CONN.error("handshake failure, message={}, conn={}", message, message.getConnection()); + return; + } + + //2.重复握手判断 + SessionContext context = message.getConnection().getSessionContext(); + if (message.deviceId.equals(context.deviceId)) { + ErrorMessage.from(message).setReason("repeat handshake").send(); + Logs.CONN.warn("handshake failure, repeat handshake, conn={}", message.getConnection()); + return; + } + + //6.响应握手成功消息 + HandshakeOkMessage.from(message).send(); + + //8.保存client信息到当前连接 + context.setOsName(message.osName) + .setOsVersion(message.osVersion) + .setClientVersion(message.clientVersion) + .setDeviceId(message.deviceId) + .setHeartbeat(Integer.MAX_VALUE); + + Logs.CONN.info("handshake success, conn={}", message.getConnection()); + + } } \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java new file mode 100644 index 00000000..4c3ba030 --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java @@ -0,0 +1,68 @@ +package com.mpush.core.server; + +import com.mpush.api.PacketReceiver; +import com.mpush.api.connection.Connection; +import com.mpush.api.connection.ConnectionManager; +import com.mpush.netty.connection.NettyConnection; +import com.shinemo.signin.amc.common.PacketReceiver; +import com.shinemo.signin.amc.server.conn.ConnManager; +import com.shinemo.signin.amc.server.conn.Connection; +import com.shinemo.signin.amc.server.message.MessageReceiver; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Echoes uppercase content of text frames. + */ +public class WebSocketChannelHandler extends SimpleChannelInboundHandler { + private final Logger logger = LoggerFactory.getLogger(WebSocketChannelHandler.class.getSimpleName()); + + private final ConnectionManager connectionManager; + private final PacketReceiver receiver; + + public WebSocketChannelHandler(ConnectionManager connectionManager, PacketReceiver receiver) { + this.connectionManager = connectionManager; + this.receiver = receiver; + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { + if (frame instanceof TextWebSocketFrame) { + String request = ((TextWebSocketFrame) frame).text(); + receiver.onReceive(request, ctx.channel()); + } else { + String message = "unsupported frame type: " + frame.getClass().getName(); + throw new UnsupportedOperationException(message); + } + } + + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + Connection connection = new NettyConnection(); + connection.init(ctx.channel(), false); + connectionManager.add(connection); + logger.info("connect active, channel={}", ctx.channel()); + super.channelActive(ctx); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + connectionManager.removeAndClose(ctx.channel()); + super.channelInactive(ctx); + logger.info("connect inactive, channel={}", ctx.channel()); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + connectionManager.removeAndClose(ctx.channel()); + super.exceptionCaught(ctx, cause); + logger.info("connect ex, channel={}", ctx.channel(), cause); + } +} \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java b/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java new file mode 100644 index 00000000..6079262b --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java @@ -0,0 +1,145 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.core.server; + +import com.mpush.api.connection.ConnectionManager; +import com.mpush.api.protocol.Command; +import com.mpush.api.service.Listener; +import com.mpush.api.spi.handler.PushHandlerFactory; +import com.mpush.common.MessageDispatcher; +import com.mpush.core.handler.*; +import com.mpush.netty.server.NettyTCPServer; +import com.mpush.tools.config.CC; +import com.mpush.tools.thread.ThreadNames; +import com.mpush.tools.thread.pool.ThreadPoolManager; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.WriteBufferWaterMark; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; +import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler; + +/** + * Created by ohun on 2016/12/16. + * + * @author ohun@live.cn (夜色) + */ +public final class WebSocketServer extends NettyTCPServer { + public WebSocketServer(int port) { + super(port); + } + + private static WebSocketServer I; + + private ServerChannelHandler channelHandler; + + private ConnectionManager connectionManager = new ServerConnectionManager(true); + + public static WebSocketServer I() { + if (I == null) { + synchronized (ConnectionServer.class) { + if (I == null) { + I = new WebSocketServer(); + } + } + } + return I; + } + + private WebSocketServer() { + super(8080); + } + + @Override + public void init() { + super.init(); + connectionManager.init(); + MessageDispatcher receiver = new MessageDispatcher(); + receiver.register(Command.HANDSHAKE, new HandshakeHandler()); + receiver.register(Command.BIND, new BindUserHandler()); + receiver.register(Command.UNBIND, new BindUserHandler()); + receiver.register(Command.PUSH, PushHandlerFactory.create()); + receiver.register(Command.ACK, new AckHandler()); + if (CC.mp.http.proxy_enabled) { + receiver.register(Command.HTTP_PROXY, new HttpProxyHandler()); + } + channelHandler = new ServerChannelHandler(false, connectionManager, receiver); + } + + @Override + public void start(Listener listener) { + super.start(listener); + if (this.workerGroup != null) {// 增加线程池监控 + ThreadPoolManager.I.register("conn-worker", this.workerGroup); + } + } + + @Override + public void stop(Listener listener) { + super.stop(listener); + connectionManager.destroy(); + } + + @Override + protected int getWorkThreadNum() { + return CC.mp.thread.pool.conn_work; + } + + @Override + protected String getBossThreadName() { + return ThreadNames.T_CONN_BOSS; + } + + @Override + protected String getWorkThreadName() { + return ThreadNames.T_CONN_WORKER; + } + + @Override + protected void initPipeline(ChannelPipeline pipeline) { + pipeline.addLast(new HttpServerCodec()); + pipeline.addLast(new HttpObjectAggregator(65536)); + pipeline.addLast(new WebSocketServerCompressionHandler()); + pipeline.addLast(new WebSocketServerProtocolHandler("/", null, true)); + pipeline.addLast("handler", getChannelHandler()); + } + + @Override + protected void initOptions(ServerBootstrap b) { + super.initOptions(b); + b.option(ChannelOption.SO_BACKLOG, 1024); + b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); + b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); + b.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT); + } + + @Override + public ChannelHandler getChannelHandler() { + return channelHandler; + } + + public ConnectionManager getConnectionManager() { + return connectionManager; + } + +} diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/Reflects.java b/mpush-tools/src/main/java/com/mpush/tools/common/Reflects.java new file mode 100644 index 00000000..9818b36a --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/Reflects.java @@ -0,0 +1,50 @@ +package com.mpush.tools.common; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Reflects { + + /** + * 获得超类的参数类型,取第一个参数类型 + * + * @param clazz 超类类型 + */ + public static Class getSuperClassGenericType(final Class clazz, int index) { + return getGenericType(clazz.getGenericSuperclass(), index); + } + + public static Class getFieldGenericType(final Field field, final int index) { + return getGenericType(field.getGenericType(), index); + } + + public static List getMethodGenericTypes(final Method method, final int paramIndex) { + return getGenericTypes(method.getGenericParameterTypes()[paramIndex]); + } + + + public static Class getGenericType(Type genType, int index) { + List params = getGenericTypes(genType); + if (index >= params.size() || index < 0) return null; + return params.get(index); + } + + public static List getGenericTypes(Type genType) { + if (!(genType instanceof ParameterizedType)) return Collections.emptyList(); + Type[] types = ((ParameterizedType) genType).getActualTypeArguments(); + List list = new ArrayList(types.length); + for (Type type : types) { + if (type instanceof Class) list.add((Class) type); + else if (type instanceof ParameterizedType) { + Type type1 = ((ParameterizedType) type).getRawType(); + if (type1 instanceof Class) list.add((Class) type1); + } + } + return list; + } +} \ No newline at end of file From 9bb07a8d0580114953927b596dd05255b6407542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 17:49:58 +0800 Subject: [PATCH 786/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0websocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/protocol/Packet.java | 4 ++ .../com/mpush/api/protocol/TextPacket.java | 43 +++++++++++++++++++ .../common/handler/BaseMessageHandler.java | 19 +++++--- .../com/mpush/common/message/BaseMessage.java | 15 ++++++- 4 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 9ab022ef..77350801 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -80,6 +80,10 @@ public boolean hasFlag(byte flag) { return (flags & flag) != 0; } + public T getBody() { + return (T) body; + } + public short calcCheckCode() { short checkCode = 0; if (body != null) { diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java new file mode 100644 index 00000000..d67e1cfc --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java @@ -0,0 +1,43 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.protocol; + +/** + * Created by ohun on 2016/12/16. + * + * @author ohun@live.cn (夜色) + */ +public final class TextPacket extends Packet { + public String body; + + public TextPacket(byte cmd) { + super(cmd); + } + + public TextPacket(byte cmd, int sessionId) { + super(cmd, sessionId); + } + + @Override + @SuppressWarnings("unchecked") + public String getBody() { + return body; + } +} diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index db2d2b6b..5eeebe8c 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -24,6 +24,7 @@ import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; +import com.mpush.common.message.BaseMessage; import com.mpush.tools.Jsons; import com.mpush.tools.common.Profiler; import com.mpush.tools.common.Reflects; @@ -35,8 +36,9 @@ * * @author ohun@live.cn */ -public abstract class BaseMessageHandler implements MessageHandler { - protected Class clazz = Reflects.getSuperClassGenericType(this.getClass(), 0); +public abstract class BaseMessageHandler implements MessageHandler { + @SuppressWarnings("unchecked") + private Class clazz = Reflects.getSuperClassGenericType(this.getClass(), 0); public abstract T decode(Packet packet, Connection connection); @@ -44,7 +46,7 @@ public abstract class BaseMessageHandler implements MessageHa public void handle(Packet packet, Connection connection) { Profiler.enter("time cost on [message decode]"); - T t = decode(packet, connection); + T t = decode0(packet, connection); Profiler.release(); if (t != null) { @@ -56,8 +58,15 @@ public void handle(Packet packet, Connection connection) { protected T decode0(Packet packet, Connection connection) { if (packet.hasFlag(FLAG_JSON_BODY)) { - Jsons.fromJson(packet.body, clazz); - return Jsons.fromJson(packet.body, clazz); + String body = packet.getBody(); + T t = Jsons.fromJson(body, clazz); + if (t != null) { + t.setPacket(packet); + t.setConnection(connection); + } + return Jsons.fromJson(body, clazz); + } else { + return decode(packet, connection); } } } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 7ef4749e..50c5db8b 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -39,8 +39,11 @@ */ public abstract class BaseMessage implements Message { private static final LongAdder ID_SEQ = new LongAdder(); - protected final Packet packet; - protected final Connection connection; + protected Packet packet; + protected Connection connection; + + public BaseMessage() { + } public BaseMessage(Packet packet, Connection connection) { this.packet = packet; @@ -156,6 +159,14 @@ public BaseMessage setRecipient(InetSocketAddress recipient) { return this; } + public void setPacket(Packet packet) { + this.packet = packet; + } + + public void setConnection(Connection connection) { + this.connection = connection; + } + @Override public abstract String toString(); } From adc9300e5c4b42419f51d10bb9f8325eae13554c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 21:59:38 +0800 Subject: [PATCH 787/890] =?UTF-8?q?Timer=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0setRemoveOnCancelPolicy(true)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../thread/pool/DefaultExecutorFactory.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java index 6b3d27d6..435058b0 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java +++ b/mpush-tools/src/main/java/com/mpush/tools/thread/pool/DefaultExecutorFactory.java @@ -78,20 +78,27 @@ public Executor get(String name) { .setRejectedPolicy(ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS); ; break; - case PUSH_CLIENT: - return new ScheduledThreadPoolExecutor(push_client, new NamedPoolThreadFactory(T_PUSH_CLIENT_TIMER), - (r, e) -> r.run() // run caller thread + case PUSH_CLIENT: { + ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(push_client + , new NamedPoolThreadFactory(T_PUSH_CLIENT_TIMER), (r, e) -> r.run() // run caller thread ); + executor.setRemoveOnCancelPolicy(true); + return executor; + } case PUSH_TASK: return new ScheduledThreadPoolExecutor(push_task, new NamedPoolThreadFactory(T_PUSH_CENTER_TIMER), (r, e) -> { throw new PushException("one push task was rejected. task=" + r); } ); - case ACK_TIMER: - return new ScheduledThreadPoolExecutor(ack_timer, new NamedPoolThreadFactory(T_ARK_REQ_TIMER), + case ACK_TIMER: { + ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(ack_timer, + new NamedPoolThreadFactory(T_ARK_REQ_TIMER), (r, e) -> Logs.PUSH.error("one ack context was rejected, context=" + r) ); + executor.setRemoveOnCancelPolicy(true); + return executor; + } default: throw new IllegalArgumentException("no executor for " + name); } From 3390b3b8dc5423b6d438f862b534aaacc85689ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 22:01:10 +0800 Subject: [PATCH 788/890] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9EPushResult=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E5=85=BC=E5=AE=B9=E6=97=A9=E6=9C=9F=E7=9A=84=E8=BF=94?= =?UTF-8?q?=E5=9B=9EBoolean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/src/main/java/com/mpush/api/push/PushSender.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java index ad8aa842..2d0261b7 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushSender.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushSender.java @@ -46,9 +46,9 @@ static PushSender create() { * @param context 推送参数 * @return FutureTask 可用于同步调用 */ - FutureTask send(PushContext context); + FutureTask send(PushContext context); - default FutureTask send(String context, String userId, PushCallback callback) { + default FutureTask send(String context, String userId, PushCallback callback) { return send(PushContext .build(context) .setUserId(userId) @@ -56,7 +56,7 @@ default FutureTask send(String context, String userId, PushCallback cal ); } - default FutureTask send(String context, String userId, AckModel ackModel, PushCallback callback) { + default FutureTask send(String context, String userId, AckModel ackModel, PushCallback callback) { return send(PushContext .build(context) .setAckModel(ackModel) From a0955f6019c9df113ff33452501e657aa9393b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 22:29:55 +0800 Subject: [PATCH 789/890] =?UTF-8?q?PushClient=E4=BB=BB=E5=8A=A1=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mpush/api/push/PushResult.java | 2 +- .../com/mpush/client/push/PushClient.java | 9 +-- .../com/mpush/client/push/PushRequest.java | 72 +++++++++++++------ .../com/mpush/client/push/PushRequestBus.java | 5 +- .../mpush/test/push/PushClientTestMain.java | 11 ++- .../java/com/mpush/tools/common/TimeLine.java | 9 ++- 6 files changed, 65 insertions(+), 43 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java index f5e85e36..55dc23c7 100644 --- a/mpush-api/src/main/java/com/mpush/api/push/PushResult.java +++ b/mpush-api/src/main/java/com/mpush/api/push/PushResult.java @@ -98,7 +98,7 @@ public String toString() { "resultCode=" + getResultDesc() + ", userId='" + userId + '\'' + ", timeLine=" + Arrays.toString(timeLine) + - ", location=" + location + + ", " + location + '}'; } } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index b0112e08..ee92d056 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -21,6 +21,7 @@ import com.mpush.api.push.PushContext; import com.mpush.api.push.PushException; +import com.mpush.api.push.PushResult; import com.mpush.api.push.PushSender; import com.mpush.api.service.BaseService; import com.mpush.api.service.Listener; @@ -42,7 +43,7 @@ /*package*/ final class PushClient extends BaseService implements PushSender { private final GatewayConnectionFactory factory = GatewayConnectionFactory.create(); - private FutureTask send0(PushContext ctx) { + private FutureTask send0(PushContext ctx) { if (ctx.isBroadcast()) { return PushRequest.build(factory, ctx).broadcast(); } else { @@ -50,7 +51,7 @@ private FutureTask send0(PushContext ctx) { if (remoteRouters == null || remoteRouters.isEmpty()) { return PushRequest.build(factory, ctx).offline(); } - FutureTask task = null; + FutureTask task = null; for (RemoteRouter remoteRouter : remoteRouters) { task = PushRequest.build(factory, ctx).send(remoteRouter); } @@ -59,13 +60,13 @@ private FutureTask send0(PushContext ctx) { } @Override - public FutureTask send(PushContext ctx) { + public FutureTask send(PushContext ctx) { if (ctx.isBroadcast()) { return send0(ctx.setUserId(null)); } else if (ctx.getUserId() != null) { return send0(ctx); } else if (ctx.getUserIds() != null) { - FutureTask task = null; + FutureTask task = null; for (String userId : ctx.getUserIds()) { task = send0(ctx.setUserId(userId)); } diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index 186a68f7..ea1dd97b 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -34,6 +34,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.atomic.AtomicReference; @@ -43,10 +44,10 @@ * * @author ohun@live.cn */ -public final class PushRequest extends FutureTask { +public final class PushRequest extends FutureTask { private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class); - private static final Callable NONE = () -> Boolean.FALSE; + private static final Callable NONE = () -> new PushResult(PushResult.CODE_FAILURE); private enum Status {init, success, failure, offline, timeout} @@ -63,7 +64,9 @@ private enum Status {init, success, failure, offline, timeout} private byte[] content; private int timeout; private ClientLocation location; + private int sessionId; private Future future; + private PushResult result; private void sendToConnServer(RemoteRouter remoteRouter) { timeLine.addTimePoint("lookup-remote"); @@ -103,7 +106,8 @@ private void sendToConnServer(RemoteRouter remoteRouter) { } }); PushRequest.this.content = null;//释放内存 - future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + sessionId = pushMessage.getSessionId(); + future = PushRequestBus.I.put(sessionId, PushRequest.this); } ); @@ -115,27 +119,38 @@ private void sendToConnServer(RemoteRouter remoteRouter) { private void submit(Status status) { if (this.status.compareAndSet(Status.init, status)) {//防止重复调用 - timeLine.end(); - if (future != null) future.cancel(true); - if (callback != null) { - PushRequestBus.I.asyncCall(this); + boolean isTimeoutEnd = status == Status.timeout;//任务是否超时结束 + + if (future != null && !isTimeoutEnd) {//是超时结束任务不用再取消一次 + future.cancel(true);//取消超时任务 + } + + this.timeLine.end();//结束时间流统计 + super.set(getResult());//设置同步调用的返回结果 + + if (callback != null) {//回调callback + if (isTimeoutEnd) {//超时结束时,当前线程已经是线程池里的线程,直接调用callback + callback.onResult(getResult()); + } else {//非超时结束时,当前线程为Netty线程池,要异步执行callback + PushRequestBus.I.asyncCall(this);//会执行run方法 + } } - super.set(this.status.get() == Status.success); } - LOGGER.info("push request {} end, userId={}, content={}, location={}, timeLine={}" - , status, userId, content, location, timeLine); + LOGGER.info("push request {} end, {}, {}, {}", status, userId, location, timeLine); } + /** + * run方法会有两个地方的线程调用 + * 1. 任务超时时会调用,见PushRequestBus.I.put(sessionId, PushRequest.this); + * 2. 异步执行callback的时候,见PushRequestBus.I.asyncCall(this); + */ @Override public void run() { - if (status.get() == Status.init) {//从定时任务过来的,超时时间到了 - submit(Status.timeout); + //判断任务是否超时,如果超时了此时状态是init,否则应该是其他状态, 因为从submit方法过来的状态都不是init + if (status.get() == Status.init) { + timeout(); } else { - callback.onResult(new PushResult(status.get().ordinal()) - .setUserId(userId) - .setLocation(location) - .setTimeLine(timeLine.getTimePoints()) - ); + callback.onResult(getResult()); } } @@ -144,7 +159,7 @@ public boolean cancel(boolean mayInterruptIfRunning) { throw new UnsupportedOperationException(); } - public FutureTask send(RemoteRouter router) { + public FutureTask send(RemoteRouter router) { timeLine.begin(); sendToConnServer(router); return this; @@ -160,13 +175,13 @@ public void redirect() { } } - public FutureTask offline() { + public FutureTask offline() { CachedRemoteRouterManager.I.invalidateLocalCache(userId); submit(Status.offline); return this; } - public FutureTask broadcast() { + public FutureTask broadcast() { timeLine.begin(); boolean success = connectionFactory.broadcast( @@ -189,7 +204,8 @@ public FutureTask broadcast() { }); if (pushMessage.taskId == null) { - future = PushRequestBus.I.put(pushMessage.getSessionId(), PushRequest.this); + sessionId = pushMessage.getSessionId(); + future = PushRequestBus.I.put(sessionId, PushRequest.this); } else { success(); } @@ -205,7 +221,9 @@ public FutureTask broadcast() { } public void timeout() { - submit(Status.timeout); + if (PushRequestBus.I.getAndRemove(sessionId) != null) { + submit(Status.timeout); + } } public void success() { @@ -248,6 +266,16 @@ public static PushRequest build(GatewayConnectionFactory factory, PushContext ct } + private PushResult getResult() { + if (result == null) { + result = new PushResult(status.get().ordinal()) + .setUserId(userId) + .setLocation(location) + .setTimeLine(timeLine.getTimePoints()); + } + return result; + } + public PushRequest setCallback(PushCallback callback) { this.callback = callback; return this; diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java index d82c9a08..9adb4320 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequestBus.java @@ -26,10 +26,7 @@ import org.slf4j.LoggerFactory; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Future; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; /** * Created by ohun on 2015/12/30. diff --git a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java index bee7de11..c89d2122 100644 --- a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java @@ -45,18 +45,13 @@ public void testPush() throws Exception { Logs.init(); PushSender sender = PushSender.create(); sender.start().join(); + Thread.sleep(1000); for (int i = 0; i < 10; i++) { PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); msg.setMsgId("msgId_" + i); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - PushContext context = PushContext.build(msg) .setAckModel(AckModel.AUTO_ACK) .setUserId("user-" + i) @@ -71,7 +66,9 @@ public void onResult(PushResult result) { System.err.println("\n\n" + result); } }); - FutureTask future = sender.send(context); + FutureTask future = sender.send(context); + + //System.err.println("\n\n" + future.get()); } LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(30)); diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java index eb1923a0..ff52a9c3 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java +++ b/mpush-tools/src/main/java/com/mpush/tools/common/TimeLine.java @@ -27,7 +27,7 @@ * @author ohun@live.cn (夜色) */ public final class TimeLine { - private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + private static final SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS"); private final TimePoint root = new TimePoint("root"); private final String name; private int pointCount; @@ -62,7 +62,7 @@ public void clean() { public String toString() { StringBuilder sb = new StringBuilder(name); if (root.next != null) { - sb.append('[').append(current.time - root.next.time).append(']'); + sb.append('[').append(current.time - root.next.time).append(']').append("(ms)"); } sb.append('{'); TimePoint next = root; @@ -99,9 +99,8 @@ public void setNext(TimePoint next) { @Override public String toString() { - String header = name + "[" + formatter.format(new Date(time)) + "]"; - if (next == null) return header; - return header + " --" + (next.time - time) + "(ms)--> "; + if (next == null) return name; + return name + " --(" + (next.time - time) + "ms) --> "; } } } From ccfe84354f6dc49494c70cb1eaa47acd3bf2b626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Fri, 16 Dec 2016 22:35:18 +0800 Subject: [PATCH 790/890] =?UTF-8?q?PushClient=E4=BB=BB=E5=8A=A1=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/client/push/PushRequest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java index ea1dd97b..caaa011c 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushRequest.java @@ -314,7 +314,7 @@ public PushRequest setCondition(String condition) { @Override public String toString() { return "PushRequest{" + - "content='" + content.length + '\'' + + "content='" + (content == null ? -1 : content.length) + '\'' + ", userId='" + userId + '\'' + ", timeout=" + timeout + ", location=" + location + From 27df79bd2edeae135f534632f44ee32ffe65dfdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 18 Dec 2016 11:38:10 +0800 Subject: [PATCH 791/890] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E5=B1=82=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0websocket=E5=8D=8F=E8=AE=AE=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 2 + mpush-api/pom.xml | 6 + .../src/main/java/com/mpush/api/Message.java | 4 + .../com/mpush/api/protocol/JsonPacket.java | 90 +++++++++ .../java/com/mpush/api/protocol/Packet.java | 46 ++++- .../com/mpush/api/protocol/UDPPacket.java | 14 ++ .../java/com/mpush/api/spi/SpiLoader.java | 2 +- .../mpush/api/spi/common/ExecutorFactory.java | 4 - .../java/com/mpush/api/spi/common/Json.java | 33 +++ .../common/JsonFactory.java} | 24 +-- .../com/mpush/bootstrap/ServerLauncher.java | 8 +- .../java/com/mpush/bootstrap/job/BootJob.java | 23 ++- .../com/mpush/bootstrap/job/ServerBoot.java | 5 + mpush-boot/src/main/resources/mpush.conf | 1 + mpush-common/pom.xml | 4 + .../common/handler/BaseMessageHandler.java | 24 +-- .../mpush/common/memory/PacketFactory.java | 19 +- .../com/mpush/common/message/BaseMessage.java | 106 +++++++--- .../mpush/common/message/BindUserMessage.java | 8 + .../mpush/common/message/ErrorMessage.java | 13 ++ .../common/message/FastConnectOkMessage.java | 5 +- .../common/message/HandshakeMessage.java | 9 + .../common/message/HandshakeOkMessage.java | 5 +- .../common/message/HttpResponseMessage.java | 5 +- .../com/mpush/common/message/OkMessage.java | 12 ++ .../com/mpush/common/message/PushMessage.java | 38 +++- .../mpush/core/push/BroadcastPushTask.java | 6 +- .../mpush/core/push/SingleUserPushTask.java | 4 +- .../core/server/WebSocketChannelHandler.java | 10 +- .../server/WebSocketIndexPageHandler.java | 85 ++++++++ .../mpush/core/server/WebSocketServer.java | 44 ++-- mpush-core/src/main/resources/index.html | 189 ++++++++++++++++++ .../com/mpush/netty/codec/PacketDecoder.java | 38 ++-- .../com/mpush/netty/codec/PacketEncoder.java | 27 +-- .../netty/connection/NettyConnection.java | 4 +- .../mpush/netty/server/NettyTCPServer.java | 41 +++- .../mpush/test/push/PushClientTestMain.java | 2 +- .../src/main/resources/application.conf | 3 +- .../services/com.mpush.api.spi.PusherFactory | 1 - .../tools/common/DefaultJsonFactory.java | 24 +++ .../main/java/com/mpush/tools/config/CC.java | 6 + .../com.mpush.api.spi.common.JsonFactory | 1 + .../com.mpush.tools.spi.test.TestService | 2 - mpush-tools/src/test/resources/logback.xml | 30 --- .../test/resources/serverconfig.properties | 10 - .../src/main/java/com/mpush/zk/ZKPath.java | 1 + .../java/com/mpush/zk/node/ZKServerNode.java | 9 + pom.xml | 13 ++ 48 files changed, 816 insertions(+), 244 deletions(-) create mode 100644 mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java create mode 100644 mpush-api/src/main/java/com/mpush/api/spi/common/Json.java rename mpush-api/src/main/java/com/mpush/api/{protocol/TextPacket.java => spi/common/JsonFactory.java} (65%) create mode 100644 mpush-core/src/main/java/com/mpush/core/server/WebSocketIndexPageHandler.java create mode 100644 mpush-core/src/main/resources/index.html delete mode 100644 mpush-test/src/main/resources/services/com.mpush.api.spi.PusherFactory create mode 100644 mpush-tools/src/main/java/com/mpush/tools/common/DefaultJsonFactory.java create mode 100644 mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.JsonFactory delete mode 100644 mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService delete mode 100644 mpush-tools/src/test/resources/logback.xml delete mode 100644 mpush-tools/src/test/resources/serverconfig.properties diff --git a/conf/reference.conf b/conf/reference.conf index 3dad203f..bca52a7e 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -47,6 +47,8 @@ mp { gateway-server-net=udp //网关服务使用的网络类型tcp/udp gateway-server-multicast="239.239.239.88" //239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效 gateway-client-multicast="239.239.239.99" //239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效 + ws-server-port=0 //websocket对外端口, 公网端口, 0表示禁用websocket + ws-path="/" //websocket path public-host-mapping { //本机局域网IP和公网IP的映射关系 //"10.0.10.156":"111.1.32.137" //"10.0.10.166":"111.1.33.138" diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 0acbc2b5..c7a3f72c 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -22,6 +22,12 @@ io.netty netty-transport + compile + + + io.netty + netty-codec-http + compile diff --git a/mpush-api/src/main/java/com/mpush/api/Message.java b/mpush-api/src/main/java/com/mpush/api/Message.java index 84939320..4aa1ddf4 100644 --- a/mpush-api/src/main/java/com/mpush/api/Message.java +++ b/mpush-api/src/main/java/com/mpush/api/Message.java @@ -32,6 +32,10 @@ public interface Message { Connection getConnection(); + void decodeBody(); + + void encodeBody(); + /** * 发送当前message, 并根据情况最body进行数据压缩、加密 * diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java new file mode 100644 index 00000000..61b4c499 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java @@ -0,0 +1,90 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.protocol; + + +import com.mpush.api.Constants; +import com.mpush.api.spi.common.Json; +import com.mpush.api.spi.common.JsonFactory; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; + +import java.util.Map; + +/** + * Created by ohun on 2016/12/16. + * + * @author ohun@live.cn (夜色) + */ +public final class JsonPacket extends Packet { + + public Map body; + + public JsonPacket() { + super(Command.UNKNOWN); + this.addFlag(FLAG_JSON_BODY); + } + + public JsonPacket(Command cmd, int sessionId) { + super(cmd, sessionId); + this.addFlag(FLAG_JSON_BODY); + } + + @Override + @SuppressWarnings("unchecked") + public Map getBody() { + return body; + } + + @Override + @SuppressWarnings("unchecked") + public void setBody(T body) { + this.body = (Map) body; + } + + @Override + public int getBodyLength() { + return body == null ? 0 : body.size(); + } + + @Override + public Packet response(Command command) { + return new JsonPacket(command, sessionId); + } + + @Override + public Object toFrame(Channel channel) { + byte[] json = Json.JSON.toJson(this).getBytes(Constants.UTF_8); + return new TextWebSocketFrame(Unpooled.wrappedBuffer(json)); + } + + @Override + public String toString() { + return "JsonPacket{" + + "cmd=" + cmd + + ", cc=" + cc + + ", flags=" + flags + + ", sessionId=" + sessionId + + ", lrc=" + lrc + + ", body=" + body + + '}'; + } +} diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java index 77350801..f2a38d37 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/Packet.java @@ -21,6 +21,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import java.net.InetSocketAddress; @@ -44,11 +45,11 @@ public class Packet { public static final Packet HB_PACKET = new Packet(Command.HEARTBEAT); public byte cmd; //命令 - public short cc; //校验码 暂时没有用到 + transient public short cc; //校验码 暂时没有用到 public byte flags; //特性,如是否加密,是否压缩等 public int sessionId; // 会话id。客户端生成。 - public byte lrc; // 校验,纵向冗余校验。只校验head - public byte[] body; + transient public byte lrc; // 校验,纵向冗余校验。只校验head + transient public byte[] body; public Packet(byte cmd) { this.cmd = cmd; @@ -84,6 +85,10 @@ public T getBody() { return (T) body; } + public void setBody(T body) { + this.body = (byte[]) body; + } + public short calcCheckCode() { short checkCode = 0; if (body != null) { @@ -109,7 +114,7 @@ public byte calcLrc() { return lrc; } - public boolean vaildCheckCode() { + public boolean validCheckCode() { return calcCheckCode() == cc; } @@ -128,6 +133,39 @@ public Packet response(Command command) { return new Packet(command, sessionId); } + public Object toFrame(Channel channel) { + return this; + } + + public static Packet decodePacket(Packet packet, ByteBuf in, int bodyLength) { + packet.cc = in.readShort();//read cc + packet.flags = in.readByte();//read flags + packet.sessionId = in.readInt();//read sessionId + packet.lrc = in.readByte();//read lrc + + //read body + if (bodyLength > 0) { + in.readBytes(packet.body = new byte[bodyLength]); + } + return packet; + } + + public static void encodePacket(Packet packet, ByteBuf out) { + if (packet.cmd == Command.HEARTBEAT.cmd) { + out.writeByte(Packet.HB_PACKET_BYTE); + } else { + out.writeInt(packet.getBodyLength()); + out.writeByte(packet.cmd); + out.writeShort(packet.cc); + out.writeByte(packet.flags); + out.writeInt(packet.sessionId); + out.writeByte(packet.lrc); + if (packet.getBodyLength() > 0) { + out.writeBytes(packet.body); + } + } + } + @Override public String toString() { return "{" + diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java index ca68cfe4..74ce4ee1 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/UDPPacket.java @@ -19,8 +19,14 @@ package com.mpush.api.protocol; +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.socket.DatagramPacket; + import java.net.InetSocketAddress; +import static com.mpush.api.protocol.Command.HEARTBEAT; + /** * Created by ohun on 16/10/21. * @@ -65,4 +71,12 @@ public void setRecipient(InetSocketAddress recipient) { public Packet response(Command command) { return new UDPPacket(command, sessionId, address); } + + @Override + public Object toFrame(Channel channel) { + int capacity = cmd == HEARTBEAT.cmd ? 1 : HEADER_LEN + getBodyLength(); + ByteBuf out = channel.alloc().buffer(capacity, capacity); + encodePacket(this, out); + return new DatagramPacket(out, sender()); + } } diff --git a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java index 43699ea3..16b301a2 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/SpiLoader.java @@ -49,7 +49,7 @@ public static T load(Class clazz, String name) { return load0(clazz, name); } - public static T load0(Class clazz, String name) { + public static T load0(Class clazz, String name) { ServiceLoader factories = ServiceLoader.load(clazz); T t = filterByName(factories, name); diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java index d8a8f8f7..a0733742 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/ExecutorFactory.java @@ -29,15 +29,11 @@ * @author ohun@live.cn */ public interface ExecutorFactory { - String SERVER_BOSS = "sb"; - String SERVER_WORK = "sw"; - String HTTP_CLIENT_WORK = "hcw"; String PUSH_CLIENT = "pc"; String PUSH_TASK = "pt"; String ACK_TIMER = "at"; String EVENT_BUS = "eb"; String MQ = "r"; - String BIZ = "b"; Executor get(String name); diff --git a/mpush-api/src/main/java/com/mpush/api/spi/common/Json.java b/mpush-api/src/main/java/com/mpush/api/spi/common/Json.java new file mode 100644 index 00000000..a4c41a08 --- /dev/null +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/Json.java @@ -0,0 +1,33 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.api.spi.common; + +/** + * Created by ohun on 2016/12/17. + * + * @author ohun@live.cn (夜色) + */ +public interface Json { + Json JSON = JsonFactory.create(); + + T fromJson(String json, Class clazz); + + String toJson(Object json); +} diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java b/mpush-api/src/main/java/com/mpush/api/spi/common/JsonFactory.java similarity index 65% rename from mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java rename to mpush-api/src/main/java/com/mpush/api/spi/common/JsonFactory.java index d67e1cfc..5384fe4f 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/TextPacket.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/common/JsonFactory.java @@ -17,27 +17,19 @@ * ohun@live.cn (夜色) */ -package com.mpush.api.protocol; +package com.mpush.api.spi.common; + +import com.mpush.api.spi.Factory; +import com.mpush.api.spi.SpiLoader; /** - * Created by ohun on 2016/12/16. + * Created by ohun on 2016/12/17. * * @author ohun@live.cn (夜色) */ -public final class TextPacket extends Packet { - public String body; - - public TextPacket(byte cmd) { - super(cmd); - } - - public TextPacket(byte cmd, int sessionId) { - super(cmd, sessionId); - } +public interface JsonFactory extends Factory { - @Override - @SuppressWarnings("unchecked") - public String getBody() { - return body; + static Json create() { + return SpiLoader.load(JsonFactory.class).get(); } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 7b4949f3..3b66779e 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -21,12 +21,11 @@ import com.mpush.bootstrap.job.*; -import com.mpush.core.server.AdminServer; -import com.mpush.core.server.ConnectionServer; -import com.mpush.core.server.GatewayServer; -import com.mpush.core.server.GatewayUDPConnector; +import com.mpush.core.server.*; +import com.mpush.tools.config.CC; import static com.mpush.tools.config.CC.mp.net.udpGateway; +import static com.mpush.tools.config.CC.mp.net.wsEnabled; import static com.mpush.zk.node.ZKServerNode.*; /** @@ -43,6 +42,7 @@ public ServerLauncher() { .setNext(new ZKBoot())//1.启动ZK节点数据变化监听 .setNext(new RedisBoot())//2.注册redis sever 到ZK .setNext(new ServerBoot(ConnectionServer.I(), CS_NODE))//3.启动长连接服务 + .setNext(() -> new ServerBoot(WebSocketServer.I(), WS_NODE), wsEnabled())//4.启动websocket连接服务 .setNext(new ServerBoot(udpGateway() ? GatewayUDPConnector.I() : GatewayServer.I(), GS_NODE))//4.启动网关服务 .setNext(new ServerBoot(AdminServer.I(), null))//5.启动控制台服务 .setNext(new PushCenterBoot())//6.启动http代理服务,解析dns diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java index c616c5bb..c9d3644b 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/BootJob.java @@ -21,13 +21,15 @@ import com.mpush.tools.log.Logs; +import java.util.function.Supplier; + /** * Created by yxx on 2016/5/14. * * @author ohun@live.cn */ public abstract class BootJob { - private BootJob next; + protected BootJob next; protected abstract void start(); @@ -35,14 +37,14 @@ public abstract class BootJob { public void startNext() { if (next != null) { - Logs.Console.info("start next bootstrap job [{}]", next.getClass().getSimpleName()); + Logs.Console.info("start next bootstrap job [{}]", getNextName()); next.start(); } } public void stopNext() { if (next != null) { - Logs.Console.info("stop next bootstrap job [{}]", next.getClass().getSimpleName()); + Logs.Console.info("stop next bootstrap job [{}]", getNextName()); next.stop(); } } @@ -51,4 +53,19 @@ public BootJob setNext(BootJob next) { this.next = next; return next; } + + public BootJob setNext(Supplier next, boolean enabled) { + if (enabled) { + return setNext(next.get()); + } + return this; + } + + protected String getNextName() { + return next.getName(); + } + + protected String getName() { + return this.getClass().getSimpleName(); + } } diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java index 05dbc740..d50618cc 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServerBoot.java @@ -71,4 +71,9 @@ protected void stop() { Logs.Console.info("{} shutdown success.", this.getClass().getSimpleName()); stopNext(); } + + @Override + protected String getName() { + return super.getName() + '(' + server.getClass().getSimpleName() + ')'; + } } diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index d65256d8..2f9d0ccd 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -7,6 +7,7 @@ mp.redis { //redis 集群配置 nodes:["127.0.0.1:6379"] //格式是ip:port cluster-model:single //single, cluster } +mp.net.ws-server-port=0 //websocket对外端口, 0表示禁用websocket mp.net.gateway-server-net=udp // 网关服务使用的网络 udp/tcp mp.net.connect-server-port=3000 //接入服务的端口号 mp.net.public-host-mapping { //本机局域网IP和公网IP的映射关系,请添加实际的IP diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index 8fe53da9..2547b6bb 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -21,6 +21,10 @@ ${mpush.groupId} mpush-cache + + org.mapstruct + mapstruct-jdk8 + diff --git a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java index 5eeebe8c..7c47194a 100644 --- a/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java +++ b/mpush-common/src/main/java/com/mpush/common/handler/BaseMessageHandler.java @@ -20,16 +20,11 @@ package com.mpush.common.handler; -import com.mpush.api.Message; import com.mpush.api.MessageHandler; import com.mpush.api.connection.Connection; import com.mpush.api.protocol.Packet; import com.mpush.common.message.BaseMessage; -import com.mpush.tools.Jsons; import com.mpush.tools.common.Profiler; -import com.mpush.tools.common.Reflects; - -import static com.mpush.api.protocol.Packet.FLAG_JSON_BODY; /** * Created by ohun on 2015/12/22. @@ -37,8 +32,6 @@ * @author ohun@live.cn */ public abstract class BaseMessageHandler implements MessageHandler { - @SuppressWarnings("unchecked") - private Class clazz = Reflects.getSuperClassGenericType(this.getClass(), 0); public abstract T decode(Packet packet, Connection connection); @@ -46,7 +39,8 @@ public abstract class BaseMessageHandler implements Messa public void handle(Packet packet, Connection connection) { Profiler.enter("time cost on [message decode]"); - T t = decode0(packet, connection); + T t = decode(packet, connection); + if (t != null) t.decodeBody(); Profiler.release(); if (t != null) { @@ -55,18 +49,4 @@ public void handle(Packet packet, Connection connection) { Profiler.release(); } } - - protected T decode0(Packet packet, Connection connection) { - if (packet.hasFlag(FLAG_JSON_BODY)) { - String body = packet.getBody(); - T t = Jsons.fromJson(body, clazz); - if (t != null) { - t.setPacket(packet); - t.setConnection(connection); - } - return Jsons.fromJson(body, clazz); - } else { - return decode(packet, connection); - } - } } diff --git a/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java b/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java index 2ae63b83..9fbe30ce 100644 --- a/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java +++ b/mpush-common/src/main/java/com/mpush/common/memory/PacketFactory.java @@ -30,28 +30,11 @@ * @author ohun@live.cn (夜色) */ public interface PacketFactory { - PacketFactory FACTORY = CC.mp.net.udpGateway() ? new UDPPacketCreator() : new TCPPacketCreator(); + PacketFactory FACTORY = CC.mp.net.udpGateway() ? UDPPacket::new : Packet::new; static Packet get(Command command) { return FACTORY.create(command); } Packet create(Command command); -} - - -class TCPPacketCreator implements PacketFactory { - - @Override - public Packet create(Command command) { - return new Packet(command); - } -} - -class UDPPacketCreator implements PacketFactory { - - @Override - public Packet create(Command command) { - return new UDPPacket(command); - } } \ No newline at end of file diff --git a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java index 50c5db8b..834e33be 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BaseMessage.java @@ -29,7 +29,7 @@ import io.netty.channel.ChannelFutureListener; import java.net.InetSocketAddress; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.Map; import java.util.concurrent.atomic.LongAdder; /** @@ -38,45 +38,72 @@ * @author ohun@live.cn */ public abstract class BaseMessage implements Message { + private static final byte STATUS_DECODED = 1; + private static final byte STATUS_ENCODED = 2; private static final LongAdder ID_SEQ = new LongAdder(); - protected Packet packet; - protected Connection connection; - - public BaseMessage() { - } + transient protected Packet packet; + transient protected Connection connection; + transient private byte status = 0; public BaseMessage(Packet packet, Connection connection) { this.packet = packet; this.connection = connection; - decodeBody(); } - protected void decodeBody() { - if (packet.body != null && packet.body.length > 0) { - //1.解密 - byte[] tmp = packet.body; - if (packet.hasFlag(Packet.FLAG_CRYPTO)) { - if (connection.getSessionContext().cipher != null) { - tmp = connection.getSessionContext().cipher.decrypt(tmp); + @Override + public void decodeBody() { + if ((status & STATUS_DECODED) == 0) { + status |= STATUS_DECODED; + + if (packet.getBodyLength() > 0) { + if (packet.hasFlag(Packet.FLAG_JSON_BODY)) { + decodeJsonBody0(); + } else { + decodeBinaryBody0(); } } - //2.解压 - if (packet.hasFlag(Packet.FLAG_COMPRESS)) { - tmp = IOUtils.decompress(tmp); + + } + } + + @Override + public void encodeBody() { + if ((status & STATUS_ENCODED) == 0) { + status |= STATUS_ENCODED; + + if (packet.hasFlag(Packet.FLAG_JSON_BODY)) { + encodeJsonBody0(); + } else { + encodeBinaryBody0(); } + } + + } - if (tmp.length == 0) { - throw new RuntimeException("message decode ex"); + private void decodeBinaryBody0() { + //1.解密 + byte[] tmp = packet.body; + if (packet.hasFlag(Packet.FLAG_CRYPTO)) { + if (connection.getSessionContext().cipher != null) { + tmp = connection.getSessionContext().cipher.decrypt(tmp); } + } + //2.解压 + if (packet.hasFlag(Packet.FLAG_COMPRESS)) { + tmp = IOUtils.decompress(tmp); + } - packet.body = tmp; - Profiler.enter("time cost on [body decode]"); - decode(packet.body); - Profiler.release(); + if (tmp.length == 0) { + throw new RuntimeException("message decode ex"); } + + packet.body = tmp; + Profiler.enter("time cost on [body decode]"); + decode(packet.body); + Profiler.release(); } - protected void encodeBody() { + private void encodeBinaryBody0() { Profiler.enter("time cost on [body encode]"); byte[] tmp = encode(); Profiler.release(); @@ -103,12 +130,37 @@ protected void encodeBody() { } } + private void decodeJsonBody0() { + Map body = packet.getBody(); + decodeJsonBody(body); + } + + private void encodeJsonBody0() { + packet.setBody(encodeJsonBody()); + } + + private void encodeBodyRaw() { + if ((status & STATUS_ENCODED) == 0) { + status |= STATUS_ENCODED; + + if (packet.hasFlag(Packet.FLAG_JSON_BODY)) { + encodeJsonBody0(); + } else { + packet.body = encode(); + } + } + } + public abstract void decode(byte[] body); public abstract byte[] encode(); - public Packet createResponse() { - return new Packet(packet.cmd, packet.sessionId); + protected void decodeJsonBody(Map body) { + + } + + protected Map encodeJsonBody() { + return null; } @Override @@ -129,7 +181,7 @@ public void send(ChannelFutureListener listener) { @Override public void sendRaw(ChannelFutureListener listener) { - packet.body = encode(); + encodeBodyRaw(); connection.send(packet, listener); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java index 18a10f6a..803c819a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/BindUserMessage.java @@ -24,6 +24,8 @@ import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.Map; + /** * Created by ohun on 2015/12/28. * @@ -56,6 +58,12 @@ public void encode(ByteBuf body) { encodeString(body, tags); } + @Override + public void decodeJsonBody(Map body) { + userId = (String) body.get("userId"); + tags = (String) body.get("tags"); + } + @Override public String toString() { return "BindUserMessage{" + diff --git a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java index e2289485..90a4f794 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/ErrorMessage.java @@ -25,6 +25,9 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; +import java.util.HashMap; +import java.util.Map; + import static com.mpush.api.protocol.Command.ERROR; /** @@ -63,6 +66,16 @@ public void encode(ByteBuf body) { encodeString(body, data); } + @Override + protected Map encodeJsonBody() { + Map body = new HashMap<>(4); + if (cmd > 0) body.put("cmd", cmd); + if (code > 0) body.put("code", code); + if (reason != null) body.put("reason", reason); + if (data != null) body.put("data", data); + return body; + } + public static ErrorMessage from(BaseMessage src) { return new ErrorMessage(src.packet.cmd, src.packet.response(ERROR), src.connection); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java index 917f206c..85cb9e29 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/FastConnectOkMessage.java @@ -20,9 +20,12 @@ package com.mpush.common.message; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import static com.mpush.api.protocol.Command.FAST_CONNECT; + /** * Created by ohun on 2015/12/28. * @@ -36,7 +39,7 @@ public FastConnectOkMessage(Packet message, Connection connection) { } public static FastConnectOkMessage from(BaseMessage src) { - return new FastConnectOkMessage(src.createResponse(), src.connection); + return new FastConnectOkMessage(src.packet.response(FAST_CONNECT), src.connection); } @Override diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java index 02d20c5b..cabd2ac4 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeMessage.java @@ -24,6 +24,7 @@ import io.netty.buffer.ByteBuf; import java.util.Arrays; +import java.util.Map; import static com.mpush.api.protocol.Command.HANDSHAKE; @@ -76,6 +77,14 @@ public void encode(ByteBuf body) { encodeLong(body, timestamp); } + @Override + public void decodeJsonBody(Map body) { + deviceId = (String) body.get("deviceId"); + osName = (String) body.get("osName"); + osVersion = (String) body.get("osVersion"); + clientVersion = (String) body.get("clientVersion"); + } + @Override public String toString() { return "HandshakeMessage{" + diff --git a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java index 26fa9ca3..fe9ef9af 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HandshakeOkMessage.java @@ -20,11 +20,14 @@ package com.mpush.common.message; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; import java.util.Arrays; +import static com.mpush.api.protocol.Command.HANDSHAKE; + /** * Created by ohun on 2015/12/27. * @@ -57,7 +60,7 @@ public void encode(ByteBuf body) { } public static HandshakeOkMessage from(BaseMessage src) { - return new HandshakeOkMessage(src.createResponse(), src.connection); + return new HandshakeOkMessage(src.packet.response(HANDSHAKE), src.connection); } public HandshakeOkMessage setServerKey(byte[] serverKey) { diff --git a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java index 1985515d..6185b5b0 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/HttpResponseMessage.java @@ -20,6 +20,7 @@ package com.mpush.common.message; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.Command; import com.mpush.api.protocol.Packet; import com.mpush.tools.Utils; import io.netty.buffer.ByteBuf; @@ -27,6 +28,8 @@ import java.util.HashMap; import java.util.Map; +import static com.mpush.api.protocol.Command.HTTP_PROXY; + /** * Created by ohun on 2016/2/15. * @@ -59,7 +62,7 @@ public void encode(ByteBuf body) { } public static HttpResponseMessage from(HttpRequestMessage src) { - return new HttpResponseMessage(src.createResponse(), src.connection); + return new HttpResponseMessage(src.packet.response(HTTP_PROXY), src.connection); } public HttpResponseMessage setStatusCode(int statusCode) { diff --git a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java index 0feb1296..f3b4788a 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/OkMessage.java @@ -23,6 +23,9 @@ import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.HashMap; +import java.util.Map; + import static com.mpush.api.protocol.Command.OK; /** @@ -58,6 +61,15 @@ public void encode(ByteBuf body) { encodeString(body, data); } + @Override + public Map encodeJsonBody() { + Map body = new HashMap<>(3); + if (cmd > 0) body.put("cmd", cmd); + if (code > 0) body.put("code", code); + if (data != null) body.put("data", data); + return body; + } + public static OkMessage from(BaseMessage src) { return new OkMessage(src.packet.cmd, src.packet.response(OK), src.connection); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java index 4c79280d..48a75a68 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/PushMessage.java @@ -19,9 +19,15 @@ package com.mpush.common.message; +import com.mpush.api.Constants; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.JsonPacket; import com.mpush.api.protocol.Packet; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + import static com.mpush.api.protocol.Command.PUSH; /** @@ -37,9 +43,12 @@ public PushMessage(Packet packet, Connection connection) { super(packet, connection); } - public PushMessage(byte[] content, Connection connection) { - super(new Packet(PUSH, genSessionId()), connection); - this.content = content; + public static PushMessage build(Connection connection) { + if (connection.getSessionContext().isSecurity()) { + return new PushMessage(new Packet(PUSH, genSessionId()), connection); + } else { + return new PushMessage(new JsonPacket(PUSH, genSessionId()), connection); + } } @Override @@ -52,6 +61,22 @@ public byte[] encode() { return content; } + @Override + public void decodeJsonBody(Map body) { + String content = (String) body.get("content"); + if (content != null) { + this.content = content.getBytes(Constants.UTF_8); + } + } + + @Override + public Map encodeJsonBody() { + if (content != null) { + return Collections.singletonMap("content", new String(content, Constants.UTF_8)); + } + return null; + } + public boolean autoAck() { return packet.hasFlag(Packet.FLAG_AUTO_ACK); } @@ -60,6 +85,13 @@ public boolean needAck() { return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK); } + public PushMessage setContent(byte[] content) { + this.content = content; + return this; + } + + + @Override public String toString() { return "PushMessage{" + diff --git a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java index a2024c7c..d71dac4d 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/BroadcastPushTask.java @@ -90,8 +90,10 @@ private boolean broadcast() { if (checkCondition(condition, connection)) {//1.条件检测 if (connection.isConnected()) { if (connection.getChannel().isWritable()) { //检测TCP缓冲区是否已满且写队列超过最高阀值 - PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.send(this); + PushMessage + .build(connection) + .setContent(message.content) + .send(this); if (!flowControl.checkQps()) { throw new OverFlowException(false); } diff --git a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java index 8e2df4bf..8e54f879 100644 --- a/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java +++ b/mpush-core/src/main/java/com/mpush/core/push/SingleUserPushTask.java @@ -100,8 +100,8 @@ private boolean checkLocal(final GatewayPushMessage message) { if (flowControl.checkQps()) { //4.链接可用,直接下发消息到手机客户端 - PushMessage pushMessage = new PushMessage(message.content, connection); - pushMessage.getPacket().flags = message.getPacket().flags; + PushMessage pushMessage = PushMessage.build(connection).setContent(message.content); + pushMessage.getPacket().addFlag(message.getPacket().flags); pushMessage.send(future -> { if (future.isSuccess()) {//推送成功 diff --git a/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java b/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java index 4c3ba030..4c333e4e 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/server/WebSocketChannelHandler.java @@ -3,11 +3,8 @@ import com.mpush.api.PacketReceiver; import com.mpush.api.connection.Connection; import com.mpush.api.connection.ConnectionManager; +import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.connection.NettyConnection; -import com.shinemo.signin.amc.common.PacketReceiver; -import com.shinemo.signin.amc.server.conn.ConnManager; -import com.shinemo.signin.amc.server.conn.Connection; -import com.shinemo.signin.amc.server.message.MessageReceiver; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -20,6 +17,7 @@ /** * Echoes uppercase content of text frames. */ +@ChannelHandler.Sharable public class WebSocketChannelHandler extends SimpleChannelInboundHandler { private final Logger logger = LoggerFactory.getLogger(WebSocketChannelHandler.class.getSimpleName()); @@ -34,8 +32,8 @@ public WebSocketChannelHandler(ConnectionManager connectionManager, PacketReceiv @Override protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { if (frame instanceof TextWebSocketFrame) { - String request = ((TextWebSocketFrame) frame).text(); - receiver.onReceive(request, ctx.channel()); + String text = ((TextWebSocketFrame) frame).text(); + receiver.onReceive(PacketDecoder.decodeFrame(text), connectionManager.get(ctx.channel())); } else { String message = "unsupported frame type: " + frame.getClass().getName(); throw new UnsupportedOperationException(message); diff --git a/mpush-core/src/main/java/com/mpush/core/server/WebSocketIndexPageHandler.java b/mpush-core/src/main/java/com/mpush/core/server/WebSocketIndexPageHandler.java new file mode 100644 index 00000000..f158562e --- /dev/null +++ b/mpush-core/src/main/java/com/mpush/core/server/WebSocketIndexPageHandler.java @@ -0,0 +1,85 @@ +package com.mpush.core.server; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; +import io.netty.handler.codec.http.*; +import io.netty.handler.ssl.SslHandler; +import io.netty.util.CharsetUtil; + +import java.io.InputStream; + +import static io.netty.handler.codec.http.HttpMethod.GET; +import static io.netty.handler.codec.http.HttpResponseStatus.*; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +/** + * Outputs index page content. + */ +@ChannelHandler.Sharable +public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler { + + @Override + protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { + // Handle a bad request. + if (!req.decoderResult().isSuccess()) { + sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); + return; + } + + // Allow only GET methods. + if (req.method() != GET) { + sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); + return; + } + + // Send the index page + if ("/".equals(req.uri()) || "/index.html".equals(req.uri())) { + ByteBuf content = getContent(); + FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); + + res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); + HttpUtil.setContentLength(res, content.readableBytes()); + + sendHttpResponse(ctx, req, res); + } else { + sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND)); + } + ctx.pipeline().remove(this); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + + private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { + // Generate an error page if response getStatus code is not OK (200). + if (res.status().code() != 200) { + ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); + res.content().writeBytes(buf); + buf.release(); + HttpUtil.setContentLength(res, res.content().readableBytes()); + } + + // Send the response and close the connection if necessary. + ChannelFuture f = ctx.channel().writeAndFlush(res); + if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { + f.addListener(ChannelFutureListener.CLOSE); + } + } + + public ByteBuf getContent() { + try (InputStream in = this.getClass().getResourceAsStream("/index.html")) { + byte[] data = new byte[in.available()]; + if (in.read(data) > 0) { + return Unpooled.wrappedBuffer(data); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + return Unpooled.EMPTY_BUFFER; + } + +} \ No newline at end of file diff --git a/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java b/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java index 6079262b..48c86c0e 100644 --- a/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java +++ b/mpush-core/src/main/java/com/mpush/core/server/WebSocketServer.java @@ -24,16 +24,16 @@ import com.mpush.api.service.Listener; import com.mpush.api.spi.handler.PushHandlerFactory; import com.mpush.common.MessageDispatcher; -import com.mpush.core.handler.*; +import com.mpush.core.handler.AckHandler; +import com.mpush.core.handler.BindUserHandler; +import com.mpush.core.handler.HandshakeHandler; import com.mpush.netty.server.NettyTCPServer; import com.mpush.tools.config.CC; -import com.mpush.tools.thread.ThreadNames; -import com.mpush.tools.thread.pool.ThreadPoolManager; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; -import io.netty.channel.WriteBufferWaterMark; +import io.netty.channel.EventLoopGroup; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; @@ -51,7 +51,7 @@ public WebSocketServer(int port) { private static WebSocketServer I; - private ServerChannelHandler channelHandler; + private ChannelHandler channelHandler; private ConnectionManager connectionManager = new ServerConnectionManager(true); @@ -67,7 +67,7 @@ public static WebSocketServer I() { } private WebSocketServer() { - super(8080); + super(CC.mp.net.ws_server_port); } @Override @@ -80,18 +80,7 @@ public void init() { receiver.register(Command.UNBIND, new BindUserHandler()); receiver.register(Command.PUSH, PushHandlerFactory.create()); receiver.register(Command.ACK, new AckHandler()); - if (CC.mp.http.proxy_enabled) { - receiver.register(Command.HTTP_PROXY, new HttpProxyHandler()); - } - channelHandler = new ServerChannelHandler(false, connectionManager, receiver); - } - - @Override - public void start(Listener listener) { - super.start(listener); - if (this.workerGroup != null) {// 增加线程池监控 - ThreadPoolManager.I.register("conn-worker", this.workerGroup); - } + channelHandler = new WebSocketChannelHandler(connectionManager, receiver); } @Override @@ -101,18 +90,13 @@ public void stop(Listener listener) { } @Override - protected int getWorkThreadNum() { - return CC.mp.thread.pool.conn_work; - } - - @Override - protected String getBossThreadName() { - return ThreadNames.T_CONN_BOSS; + public EventLoopGroup getBossGroup() { + return ConnectionServer.I().getBossGroup(); } @Override - protected String getWorkThreadName() { - return ThreadNames.T_CONN_WORKER; + public EventLoopGroup getWorkerGroup() { + return ConnectionServer.I().getWorkerGroup(); } @Override @@ -120,8 +104,9 @@ protected void initPipeline(ChannelPipeline pipeline) { pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new WebSocketServerCompressionHandler()); - pipeline.addLast(new WebSocketServerProtocolHandler("/", null, true)); - pipeline.addLast("handler", getChannelHandler()); + pipeline.addLast(new WebSocketServerProtocolHandler(CC.mp.net.ws_path, null, true)); + pipeline.addLast(new WebSocketIndexPageHandler()); + pipeline.addLast(getChannelHandler()); } @Override @@ -130,7 +115,6 @@ protected void initOptions(ServerBootstrap b) { b.option(ChannelOption.SO_BACKLOG, 1024); b.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); b.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); - b.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT); } @Override diff --git a/mpush-core/src/main/resources/index.html b/mpush-core/src/main/resources/index.html new file mode 100644 index 00000000..126c9137 --- /dev/null +++ b/mpush-core/src/main/resources/index.html @@ -0,0 +1,189 @@ + + + + + MPush WebSocket Client + + + + +
+ + +
+ + +

+ +
+ + + \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java index 891f4569..627b84d1 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketDecoder.java @@ -20,7 +20,9 @@ package com.mpush.netty.codec; import com.mpush.api.protocol.Packet; +import com.mpush.api.protocol.JsonPacket; import com.mpush.api.protocol.UDPPacket; +import com.mpush.tools.Jsons; import com.mpush.tools.config.CC; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -28,8 +30,11 @@ import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.TooLongFrameException; +import java.util.HashMap; import java.util.List; +import static com.mpush.api.protocol.Packet.decodePacket; + /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) @@ -77,33 +82,26 @@ private Packet decodeFrame(ByteBuf in) throws Exception { if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { return null; } - return readPacket(new Packet(in.readByte()), in, bodyLength); + if (bodyLength > maxPacketSize) { + throw new TooLongFrameException("packet body length over limit:" + bodyLength); + } + return decodePacket(new Packet(in.readByte()), in, bodyLength); } - public static Packet decodeFrame(DatagramPacket datagram) throws Exception { - ByteBuf in = datagram.content(); + public static Packet decodeFrame(DatagramPacket frame) throws Exception { + ByteBuf in = frame.content(); int readableBytes = in.readableBytes(); int bodyLength = in.readInt(); if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { return null; } - return readPacket(new UDPPacket(in.readByte() - , datagram.sender()), in, bodyLength); - } - private static Packet readPacket(Packet packet, ByteBuf in, int bodyLength) { - packet.cc = in.readShort();//read cc - packet.flags = in.readByte();//read flags - packet.sessionId = in.readInt();//read sessionId - packet.lrc = in.readByte();//read lrc + return decodePacket(new UDPPacket(in.readByte() + , frame.sender()), in, bodyLength); + } - //read body - if (bodyLength > 0) { - if (bodyLength > maxPacketSize) { - throw new TooLongFrameException("packet body length over limit:" + bodyLength); - } - in.readBytes(packet.body = new byte[bodyLength]); - } - return packet; + public static Packet decodeFrame(String frame) throws Exception { + if (frame == null) return null; + return Jsons.fromJson(frame, JsonPacket.class); } -} +} \ No newline at end of file diff --git a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java index ec554df9..22da7677 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java +++ b/mpush-netty/src/main/java/com/mpush/netty/codec/PacketEncoder.java @@ -27,6 +27,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import static com.mpush.api.protocol.Packet.encodePacket; + /** * Created by ohun on 2015/12/19. * length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) @@ -39,29 +41,6 @@ public final class PacketEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { - encodeFrame(packet, out); - } - - public static ByteBuf encode(Channel channel, Packet packet) { - int capacity = packet.cmd == Command.HEARTBEAT.cmd ? 1 : Packet.HEADER_LEN + packet.getBodyLength(); - ByteBuf out = channel.alloc().buffer(capacity, capacity); - encodeFrame(packet, out); - return out; - } - - public static void encodeFrame(Packet packet, ByteBuf out) { - if (packet.cmd == Command.HEARTBEAT.cmd) { - out.writeByte(Packet.HB_PACKET_BYTE); - } else { - out.writeInt(packet.getBodyLength()); - out.writeByte(packet.cmd); - out.writeShort(packet.cc); - out.writeByte(packet.flags); - out.writeInt(packet.sessionId); - out.writeByte(packet.lrc); - if (packet.getBodyLength() > 0) { - out.writeBytes(packet.body); - } - } + encodePacket(packet, out); } } diff --git a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java index a648473b..03d8d0ba 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java +++ b/mpush-netty/src/main/java/com/mpush/netty/connection/NettyConnection.java @@ -83,9 +83,7 @@ public ChannelFuture send(Packet packet) { public ChannelFuture send(Packet packet, final ChannelFutureListener listener) { if (channel.isActive()) { - Object msg = packet.sender() == null ? packet : new DatagramPacket(PacketEncoder.encode(channel, packet), packet.sender()); - - ChannelFuture future = channel.writeAndFlush(msg).addListener(this); + ChannelFuture future = channel.writeAndFlush(packet.toFrame(channel)).addListener(this); if (listener != null) { future.addListener(listener); diff --git a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java index dd4fdb6e..1a3863cc 100644 --- a/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java +++ b/mpush-netty/src/main/java/com/mpush/netty/server/NettyTCPServer.java @@ -175,17 +175,40 @@ public void initChannel(SocketChannel ch) throws Exception {//每连上一个链 } private void createNioServer(Listener listener) { - NioEventLoopGroup bossGroup = new NioEventLoopGroup(getBossThreadNum(), getBossThreadFactory()); - NioEventLoopGroup workerGroup = new NioEventLoopGroup(getWorkThreadNum(), getWorkThreadFactory()); - bossGroup.setIoRatio(100); - workerGroup.setIoRatio(getIoRate()); + EventLoopGroup bossGroup = getBossGroup(); + EventLoopGroup workerGroup = getWorkerGroup(); + + if (bossGroup == null) { + NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(getBossThreadNum(), getBossThreadFactory()); + nioEventLoopGroup.setIoRatio(100); + bossGroup = nioEventLoopGroup; + } + + if (workerGroup == null) { + NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(getWorkThreadNum(), getWorkThreadFactory()); + nioEventLoopGroup.setIoRatio(getIoRate()); + workerGroup = nioEventLoopGroup; + } + createServer(listener, bossGroup, workerGroup, NioServerSocketChannel.class); } private void createEpollServer(Listener listener) { - EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(getBossThreadNum(), getBossThreadFactory()); - EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(getWorkThreadNum(), getWorkThreadFactory()); - workerGroup.setIoRatio(getIoRate()); + EventLoopGroup bossGroup = getBossGroup(); + EventLoopGroup workerGroup = getWorkerGroup(); + + if (bossGroup == null) { + EpollEventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(getBossThreadNum(), getBossThreadFactory()); + epollEventLoopGroup.setIoRatio(100); + bossGroup = epollEventLoopGroup; + } + + if (workerGroup == null) { + EpollEventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(getWorkThreadNum(), getWorkThreadFactory()); + epollEventLoopGroup.setIoRatio(getIoRate()); + workerGroup = epollEventLoopGroup; + } + createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); } @@ -281,6 +304,10 @@ protected boolean useNettyEpoll() { return false; } + public EventLoopGroup getBossGroup() { + return bossGroup; + } + public EventLoopGroup getWorkerGroup() { return workerGroup; } diff --git a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java index c89d2122..1c021db2 100644 --- a/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java +++ b/mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain.java @@ -47,7 +47,7 @@ public void testPush() throws Exception { sender.start().join(); Thread.sleep(1000); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 1; i++) { PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); msg.setMsgId("msgId_" + i); diff --git a/mpush-test/src/main/resources/application.conf b/mpush-test/src/main/resources/application.conf index fec3f945..22155610 100644 --- a/mpush-test/src/main/resources/application.conf +++ b/mpush-test/src/main/resources/application.conf @@ -1,5 +1,5 @@ mp.home=${user.dir}/target -mp.log-level=info +mp.log-level=warn mp.log-conf-path=logback.xml mp.core.min-heartbeat=30s mp.core.max-heartbeat=30s @@ -16,4 +16,5 @@ mp.net { gateway-server-port=3001 //网关服务端口, 内部端口 gateway-client-port=4000 //UDP客户端端口, 内部端口 admin-server-port=3002 //控制台服务端口, 内部端口 + ws-server-port=8080 //websocket对外端口, 0表示禁用websocket } diff --git a/mpush-test/src/main/resources/services/com.mpush.api.spi.PusherFactory b/mpush-test/src/main/resources/services/com.mpush.api.spi.PusherFactory deleted file mode 100644 index 93607f48..00000000 --- a/mpush-test/src/main/resources/services/com.mpush.api.spi.PusherFactory +++ /dev/null @@ -1 +0,0 @@ -com.mpush.client.push.PushClientFactory diff --git a/mpush-tools/src/main/java/com/mpush/tools/common/DefaultJsonFactory.java b/mpush-tools/src/main/java/com/mpush/tools/common/DefaultJsonFactory.java new file mode 100644 index 00000000..3d5935a3 --- /dev/null +++ b/mpush-tools/src/main/java/com/mpush/tools/common/DefaultJsonFactory.java @@ -0,0 +1,24 @@ +package com.mpush.tools.common; + +import com.mpush.api.spi.Spi; +import com.mpush.api.spi.common.Json; +import com.mpush.api.spi.common.JsonFactory; +import com.mpush.tools.Jsons; + +@Spi +public final class DefaultJsonFactory implements JsonFactory, Json { + @Override + public T fromJson(String json, Class clazz) { + return Jsons.fromJson(json, clazz); + } + + @Override + public String toJson(Object json) { + return Jsons.toJson(json); + } + + @Override + public Json get() { + return this; + } +} \ No newline at end of file diff --git a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java index c9c0c14c..fd82dec8 100644 --- a/mpush-tools/src/main/java/com/mpush/tools/config/CC.java +++ b/mpush-tools/src/main/java/com/mpush/tools/config/CC.java @@ -94,11 +94,17 @@ interface net { String gateway_server_net = cfg.getString("gateway-server-net"); String gateway_server_multicast = cfg.getString("gateway-server-multicast"); String gateway_client_multicast = cfg.getString("gateway-client-multicast"); + int ws_server_port = cfg.getInt("ws-server-port"); + String ws_path = cfg.getString("ws-path"); static boolean udpGateway() { return "udp".equals(gateway_server_net); } + static boolean wsEnabled() { + return ws_server_port > 0; + } + interface public_ip_mapping { diff --git a/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.JsonFactory b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.JsonFactory new file mode 100644 index 00000000..07ae4798 --- /dev/null +++ b/mpush-tools/src/main/resources/META-INF/services/com.mpush.api.spi.common.JsonFactory @@ -0,0 +1 @@ +com.mpush.tools.common.DefaultJsonFactory \ No newline at end of file diff --git a/mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService b/mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService deleted file mode 100644 index d914afd4..00000000 --- a/mpush-tools/src/test/resources/META-INF/services/com.mpush.tools.spi.test.TestService +++ /dev/null @@ -1,2 +0,0 @@ -test1=com.mpush.tools.spi.test.TestServiceImpl -test2=com.mpush.tools.spi.test.TestServiceImpl2 \ No newline at end of file diff --git a/mpush-tools/src/test/resources/logback.xml b/mpush-tools/src/test/resources/logback.xml deleted file mode 100644 index c1d001d8..00000000 --- a/mpush-tools/src/test/resources/logback.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - System.out - UTF-8 - - INFO - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - System.err - UTF-8 - - WARN - - - %d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n - - - - - - - - - diff --git a/mpush-tools/src/test/resources/serverconfig.properties b/mpush-tools/src/test/resources/serverconfig.properties deleted file mode 100644 index 50ba973a..00000000 --- a/mpush-tools/src/test/resources/serverconfig.properties +++ /dev/null @@ -1,10 +0,0 @@ -zk_ip=10.1.20.74:2181 -zk_namespace=mpush -zk_digest=shinemoIpo -max_packet_size=10240 -compress_limit=10240 -min_heartbeat=10000 -max_heartbeat=180000 -max_hb_timeout_times=2 -private_key=MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAKCE8JYKhsbydMPbiO7BJVq1pbuJWJHFxOR7L8Hv3ZVkSG4eNC8DdwAmDHYu/wadfw0ihKFm2gKDcLHp5yz5UQ8PZ8FyDYvgkrvGV0ak4nc40QDJWws621dm01e/INlGKOIStAAsxOityCLv0zm5Vf3+My/YaBvZcB5mGUsPbx8fAgEAAoGAAy0+WanRqwRHXUzt89OsupPXuNNqBlCEqgTqGAt4Nimq6Ur9u2R1KXKXUotxjp71Ubw6JbuUWvJg+5Rmd9RjT0HOUEQF3rvzEepKtaraPhV5ejEIrB+nJWNfGye4yzLdfEXJBGUQzrG+wNe13izfRNXI4dN/6Q5npzqaqv0E1CkCAQACAQACAQACAQACAQA= -public_key=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB \ No newline at end of file diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java index ef768961..fd7613ad 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKPath.java @@ -28,6 +28,7 @@ public enum ZKPath { REDIS_SERVER("/redis", "machine", "redis注册的地方"), CONNECT_SERVER("/cs/hosts", "machine", "connection server服务器应用注册的路径"), GATEWAY_SERVER("/gs/hosts", "machine", "gateway server服务器应用注册的路径"), + WS_SERVER("/ws/hosts", "machine", "websocket server服务器应用注册的路径"), DNS_MAPPING("/dns/mapping", "machine", "dns mapping服务器应用注册的路径"); ZKPath(String root, String name, String desc) { diff --git a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java index dacba13e..a2ac0464 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java +++ b/mpush-zk/src/main/java/com/mpush/zk/node/ZKServerNode.java @@ -36,6 +36,7 @@ public class ZKServerNode implements ZKNode { public static final ZKServerNode CS_NODE = csNode(); public static final ZKServerNode GS_NODE = gsNode(); + public static final ZKServerNode WS_NODE = wsNode(); private String ip; @@ -69,6 +70,14 @@ private static ZKServerNode gsNode() { ZKPath.GATEWAY_SERVER.getNodePath()); } + private static ZKServerNode wsNode() { + return new ZKServerNode(Utils.getLocalIp(), + CC.mp.net.ws_server_port, + ConfigManager.I.getPublicIp(), + ZKPath.WS_SERVER.getNodePath()); + } + + public String getIp() { return ip; } diff --git a/pom.xml b/pom.xml index f61c0750..c08837c8 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,7 @@ ${mpush.version} 4.1.4.Final linux-x86_64 + 1.1.0.Final @@ -265,6 +266,11 @@ javassist 3.21.0-GA + + org.mapstruct + mapstruct-jdk8 + ${org.mapstruct.version} + @@ -285,6 +291,13 @@ ${java.version} ${java.version} ${java.encoding} + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + From 125cbf9e3964cedff63a5c88175a7d6291b578f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 18 Dec 2016 11:39:25 +0800 Subject: [PATCH 792/890] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A7=A3=E7=BB=91?= =?UTF-8?q?=E7=94=A8=E6=88=B7bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/core/handler/BindUserHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java index 890fb78d..cc5d6f3d 100644 --- a/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java +++ b/mpush-core/src/main/java/com/mpush/core/handler/BindUserHandler.java @@ -121,7 +121,7 @@ private void unbind(BindUserMessage message) { //2.先删除远程路由, 必须是同一个设备才允许解绑 boolean unRegisterSuccess = true; int clientType = context.getClientType(); - String userId = message.userId; + String userId = context.userId; RemoteRouterManager remoteRouterManager = RouterCenter.I.getRemoteRouterManager(); RemoteRouter remoteRouter = remoteRouterManager.lookup(userId, clientType); if (remoteRouter != null) { From 4438e3a1004e8b91e3cc0df4d25ae809d62a4c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 18 Dec 2016 12:56:10 +0800 Subject: [PATCH 793/890] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E5=B1=82=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0websocket=E5=8D=8F=E8=AE=AE=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/protocol/JsonPacket.java | 5 ++ .../connect/ConnClientChannelHandler.java | 16 ++-- .../mpush/common/message/KickUserMessage.java | 25 +++++- .../core/router/RouterChangeListener.java | 2 +- mpush-core/src/main/resources/index.html | 87 ++++++++++++++++--- 5 files changed, 111 insertions(+), 24 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java b/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java index 61b4c499..ecbb16ff 100644 --- a/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java +++ b/mpush-api/src/main/java/com/mpush/api/protocol/JsonPacket.java @@ -43,6 +43,11 @@ public JsonPacket() { this.addFlag(FLAG_JSON_BODY); } + public JsonPacket(Command cmd) { + super(cmd); + this.addFlag(FLAG_JSON_BODY); + } + public JsonPacket(Command cmd, int sessionId) { super(cmd, sessionId); this.addFlag(FLAG_JSON_BODY); diff --git a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java index 337740ed..0e3c1ca6 100644 --- a/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java +++ b/mpush-client/src/main/java/com/mpush/client/connect/ConnClientChannelHandler.java @@ -83,6 +83,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception int connectedNum = STATISTICS.connectedNum.incrementAndGet(); connection.getSessionContext().changeCipher(new AesCipher(clientConfig.getClientKey(), clientConfig.getIv())); HandshakeOkMessage message = new HandshakeOkMessage(packet, connection); + message.decodeBody(); byte[] sessionKey = CipherBox.I.mixKey(clientConfig.getClientKey(), message.serverKey); connection.getSessionContext().changeCipher(new AesCipher(sessionKey, clientConfig.getIv())); connection.getSessionContext().setHeartbeat(message.heartbeat); @@ -101,6 +102,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception connection.getSessionContext().changeCipher(new AesCipher(key, iv)); FastConnectOkMessage message = new FastConnectOkMessage(packet, connection); + message.decodeBody(); connection.getSessionContext().setHeartbeat(message.heartbeat); startHeartBeat(message.heartbeat - 1000); bindUser(clientConfig); @@ -110,12 +112,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception LOGGER.error("receive kick user msg userId={}, deviceId={}, message={},", clientConfig.getUserId(), clientConfig.getDeviceId(), message); ctx.close(); } else if (command == Command.ERROR) { - ErrorMessage errorMessage = new ErrorMessage(packet, connection); - LOGGER.error("receive an error packet=" + errorMessage); + ErrorMessage message = new ErrorMessage(packet, connection); + message.decodeBody(); + LOGGER.error("receive an error packet=" + message); } else if (command == Command.PUSH) { int receivePushNum = STATISTICS.receivePushNum.incrementAndGet(); PushMessage message = new PushMessage(packet, connection); + message.decodeBody(); LOGGER.info("receive push message, content={}, receivePushNum={}" , new String(message.content, Constants.UTF_8), receivePushNum); @@ -127,16 +131,18 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else if (command == Command.HEARTBEAT) { LOGGER.info("receive heartbeat pong..."); } else if (command == Command.OK) { - OkMessage okMessage = new OkMessage(packet, connection); + OkMessage message = new OkMessage(packet, connection); + message.decodeBody(); int bindUserNum = STATISTICS.bindUserNum.get(); - if (okMessage.cmd == Command.BIND.cmd) { + if (message.cmd == Command.BIND.cmd) { bindUserNum = STATISTICS.bindUserNum.incrementAndGet(); } - LOGGER.info("receive {}, bindUserNum={}", okMessage, bindUserNum); + LOGGER.info("receive {}, bindUserNum={}", message, bindUserNum); } else if (command == Command.HTTP_PROXY) { HttpResponseMessage message = new HttpResponseMessage(packet, connection); + message.decodeBody(); LOGGER.info("receive http response, message={}, body={}", message, message.body == null ? null : new String(message.body, Constants.UTF_8)); } diff --git a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java index 15571038..3e4216e4 100644 --- a/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java +++ b/mpush-common/src/main/java/com/mpush/common/message/KickUserMessage.java @@ -20,10 +20,15 @@ package com.mpush.common.message; import com.mpush.api.connection.Connection; +import com.mpush.api.protocol.JsonPacket; import com.mpush.api.protocol.Packet; import io.netty.buffer.ByteBuf; +import java.util.HashMap; +import java.util.Map; + import static com.mpush.api.protocol.Command.KICK; +import static com.mpush.api.protocol.Command.PUSH; /** * Created by ohun on 2015/12/29. @@ -34,14 +39,18 @@ public class KickUserMessage extends ByteBufMessage { public String deviceId; public String userId; - public KickUserMessage(Connection connection) { - super(new Packet(KICK), connection); - } - public KickUserMessage(Packet message, Connection connection) { super(message, connection); } + public static KickUserMessage build(Connection connection) { + if (connection.getSessionContext().isSecurity()) { + return new KickUserMessage(new Packet(KICK), connection); + } else { + return new KickUserMessage(new JsonPacket(KICK), connection); + } + } + @Override public void decode(ByteBuf body) { deviceId = decodeString(body); @@ -54,6 +63,14 @@ public void encode(ByteBuf body) { encodeString(body, userId); } + @Override + protected Map encodeJsonBody() { + Map body = new HashMap<>(2); + body.put("deviceId", deviceId); + body.put("userId", userId); + return body; + } + @Override public String toString() { return "KickUserMessage{" + diff --git a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java index e80e3f4f..63364812 100644 --- a/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java +++ b/mpush-core/src/main/java/com/mpush/core/router/RouterChangeListener.java @@ -87,7 +87,7 @@ void on(RouterChangeEvent event) { private void kickLocal(final String userId, final LocalRouter router) { Connection connection = router.getRouteValue(); SessionContext context = connection.getSessionContext(); - KickUserMessage message = new KickUserMessage(connection); + KickUserMessage message = KickUserMessage.build(connection); message.deviceId = context.deviceId; message.userId = userId; message.send(future -> { diff --git a/mpush-core/src/main/resources/index.html b/mpush-core/src/main/resources/index.html index 126c9137..31f9e044 100644 --- a/mpush-core/src/main/resources/index.html +++ b/mpush-core/src/main/resources/index.html @@ -9,8 +9,23 @@

diff --git a/mpush-test/src/main/resources/application.conf b/mpush-test/src/main/resources/application.conf index 182b0300..fe0f38e9 100644 --- a/mpush-test/src/main/resources/application.conf +++ b/mpush-test/src/main/resources/application.conf @@ -16,5 +16,5 @@ mp.net { gateway-server-port=3001 //网关服务端口, 内部端口 gateway-client-port=4000 //UDP客户端端口, 内部端口 admin-server-port=3002 //控制台服务端口, 内部端口 - ws-server-port=8080 //websocket对外端口, 0表示禁用websocket + ws-server-port=8008 //websocket对外端口, 0表示禁用websocket } From 8cac649dac47c9401bddd796bcfe39d69e7d5aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 17 Aug 2017 15:47:09 +0800 Subject: [PATCH 882/890] =?UTF-8?q?=E5=BC=80=E6=94=BEPushClient=E6=96=B9?= =?UTF-8?q?=E4=BE=BFspring=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mpush/client/push/PushClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java index 5bcb5057..01288f09 100644 --- a/mpush-client/src/main/java/com/mpush/client/push/PushClient.java +++ b/mpush-client/src/main/java/com/mpush/client/push/PushClient.java @@ -36,7 +36,7 @@ import java.util.Set; import java.util.concurrent.FutureTask; -/*package*/ final class PushClient extends BaseService implements PushSender { +public final class PushClient extends BaseService implements PushSender { private MPushClient mPushClient; From 78b648264426317e6f170e40499952fe7f34c268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 17 Aug 2017 15:48:33 +0800 Subject: [PATCH 883/890] =?UTF-8?q?mpush=E7=89=88=E6=9C=AC=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=88=B00.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpush-api/pom.xml | 2 +- mpush-boot/pom.xml | 2 +- mpush-cache/pom.xml | 2 +- mpush-client/pom.xml | 2 +- mpush-common/pom.xml | 2 +- mpush-core/pom.xml | 2 +- mpush-monitor/pom.xml | 2 +- mpush-netty/pom.xml | 2 +- mpush-test/pom.xml | 2 +- mpush-tools/pom.xml | 10 +++++++++- mpush-zk/pom.xml | 2 +- pom.xml | 30 ++++++++++++++++++++++-------- 12 files changed, 41 insertions(+), 19 deletions(-) diff --git a/mpush-api/pom.xml b/mpush-api/pom.xml index 9a1c2115..3361a1a6 100644 --- a/mpush-api/pom.xml +++ b/mpush-api/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-boot/pom.xml b/mpush-boot/pom.xml index 1098356c..a1dbf97d 100644 --- a/mpush-boot/pom.xml +++ b/mpush-boot/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-cache/pom.xml b/mpush-cache/pom.xml index 2e3b73e4..e56028e2 100644 --- a/mpush-cache/pom.xml +++ b/mpush-cache/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-client/pom.xml b/mpush-client/pom.xml index 3b8a4fe8..0607b657 100644 --- a/mpush-client/pom.xml +++ b/mpush-client/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-common/pom.xml b/mpush-common/pom.xml index fc6ff878..32e09191 100644 --- a/mpush-common/pom.xml +++ b/mpush-common/pom.xml @@ -5,7 +5,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml 4.0.0 diff --git a/mpush-core/pom.xml b/mpush-core/pom.xml index ac61d5c0..ed413586 100644 --- a/mpush-core/pom.xml +++ b/mpush-core/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-monitor/pom.xml b/mpush-monitor/pom.xml index f1c0bb04..a7f817ff 100644 --- a/mpush-monitor/pom.xml +++ b/mpush-monitor/pom.xml @@ -3,7 +3,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml 4.0.0 diff --git a/mpush-netty/pom.xml b/mpush-netty/pom.xml index 59812a67..3b1c6035 100644 --- a/mpush-netty/pom.xml +++ b/mpush-netty/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-test/pom.xml b/mpush-test/pom.xml index f6345f81..8484478a 100644 --- a/mpush-test/pom.xml +++ b/mpush-test/pom.xml @@ -6,7 +6,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/mpush-tools/pom.xml b/mpush-tools/pom.xml index 942d1cfc..52338882 100644 --- a/mpush-tools/pom.xml +++ b/mpush-tools/pom.xml @@ -7,7 +7,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml @@ -64,6 +64,14 @@ org.slf4j jcl-over-slf4j + + org.slf4j + log4j-over-slf4j + + + org.slf4j + jul-to-slf4j + ch.qos.logback logback-classic diff --git a/mpush-zk/pom.xml b/mpush-zk/pom.xml index 0efb945a..1695489e 100644 --- a/mpush-zk/pom.xml +++ b/mpush-zk/pom.xml @@ -8,7 +8,7 @@ mpush com.github.mpusher - 0.7.1 + 0.8.0 ../pom.xml diff --git a/pom.xml b/pom.xml index 37aae7bc..dde964c5 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.github.mpusher mpush pom - 0.7.1 + 0.8.0 mpush MPUSH消息推送系统 https://github.com/mpusher/mpush @@ -71,6 +71,7 @@ UTF-8 1.8 4.1.8.Final + 1.7.25 linux-x86_64 @@ -180,17 +181,30 @@ org.slf4j slf4j-api - 1.7.22 + ${slf4j.version} + + + org.slf4j + log4j-over-slf4j + ${slf4j.version} + + + + org.slf4j + jul-to-slf4j + ${slf4j.version} + + org.slf4j jcl-over-slf4j - 1.7.22 + ${slf4j.version} ch.qos.logback logback-classic - 1.1.8 + 1.2.3 commons-logging @@ -218,7 +232,7 @@ org.apache.commons commons-lang3 - 3.5 + 3.6 @@ -230,7 +244,7 @@ com.alibaba fastjson - 1.2.23 + 1.2.36 @@ -283,7 +297,7 @@ maven-compiler-plugin - 3.6.1 + 3.6.2 ${java.version} ${java.version} @@ -299,7 +313,7 @@ maven-surefire-plugin - 2.19.1 + 2.20 true From afad0bbedbecc24cc159bff3f32a8390c2b49e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 17 Aug 2017 17:11:06 +0800 Subject: [PATCH 884/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E6=80=A7=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/common/ServerEventListener.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java b/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java index 6202cc9a..1be1794b 100644 --- a/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java +++ b/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java @@ -29,21 +29,57 @@ */ public interface ServerEventListener extends Plugin { + /** + * 该事件通过guava EventBus发出,实现接口的方法必须增加 + * + * @Subscribe 和 @AllowConcurrentEvents注解, + * 并在构造函数调用EventBus.register(this); + */ default void on(ServerStartupEvent event) { } + /** + * 该事件通过guava EventBus发出,实现接口的方法必须增加 + * + * @Subscribe 和 @AllowConcurrentEvents注解, + * 并在构造函数调用EventBus.register(this); + */ default void on(ServerShutdownEvent server) { } + /** + * 该事件通过guava EventBus发出,实现接口的方法必须增加 + * + * @Subscribe 和 @AllowConcurrentEvents注解, + * 并在构造函数调用EventBus.register(this); + */ default void on(RouterChangeEvent event) { } + /** + * 该事件通过guava EventBus发出,实现接口的方法必须增加 + * + * @Subscribe 和 @AllowConcurrentEvents注解, + * 并在构造函数调用EventBus.register(this); + */ default void on(KickUserEvent event) { } + /** + * 该事件通过guava EventBus发出,实现接口的方法必须增加 + * + * @Subscribe 和 @AllowConcurrentEvents注解, + * 并在构造函数调用EventBus.register(this); + */ default void on(UserOnlineEvent event) { } + /** + * 该事件通过guava EventBus发出,实现接口的方法必须增加 + * + * @Subscribe 和 @AllowConcurrentEvents注解, + * 并在构造函数调用EventBus.register(this); + */ default void on(UserOfflineEvent event) { } } From 215b1735a1323d42a477daa56ed2155295f7f5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Thu, 17 Aug 2017 17:11:26 +0800 Subject: [PATCH 885/890] =?UTF-8?q?mpush=E7=89=88=E6=9C=AC=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=88=B00.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Changelog.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Changelog.md b/Changelog.md index 255ae9f2..94fe66d4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,19 @@ +#### v0.8.0 + +1. 增加本地ip和外网ip配置项 +2. ConnServer和GatewayServer增加bind ip和register ip 配置项 +3. ConnServer增加权重等扩展属性配置项 +4. 系统模块化重构:SPI增加Plugin接口及其init方法,增加MPushContext对象,方便插件初始化时控制系统内部对象 +5. 广播推送增加RedisBroadcastController存储推送结果到redis, 并通过redis控制推送任务 +6. 启动脚本优化修复不能加载自定义SPI的bug +7. EventBus订阅方法增加@AllowConcurrentEvents注解,提高高并发性能 +8. 代码优化,当GatewayClient启动时从ZK获取的链接为空时,后续会尝试重新获取 +9. 优化ServerLauncher和PushClient代码,方便自定义系统配置和spring方式启动 +10. 依赖类库升级,日志优化,及其他bug fix + + + + #### v0.7.1 1. 修复网关客户端获取连接失败bug From b7417ef52bde83959108cadf01272db918f57713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Sun, 20 Aug 2017 11:36:56 +0800 Subject: [PATCH 886/890] =?UTF-8?q?mpush=E7=89=88=E6=9C=AC=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=88=B00.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mpush/test/spi/SimpleCacheMangerFactory.java | 2 +- .../main/java/com/mpush/test/spi/SimpleDiscoveryFactory.java | 2 +- .../src/main/java/com/mpush/test/spi/SimpleMQClientFactory.java | 2 +- .../src/main/java/com/mpush/test/spi/SimpleRegistryFactory.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mpush-test/src/main/java/com/mpush/test/spi/SimpleCacheMangerFactory.java b/mpush-test/src/main/java/com/mpush/test/spi/SimpleCacheMangerFactory.java index 10143b5a..598e2ac6 100644 --- a/mpush-test/src/main/java/com/mpush/test/spi/SimpleCacheMangerFactory.java +++ b/mpush-test/src/main/java/com/mpush/test/spi/SimpleCacheMangerFactory.java @@ -28,7 +28,7 @@ * * @author ohun@live.cn (夜色) */ -@Spi(order = 0) +@Spi(order = 2) public final class SimpleCacheMangerFactory implements CacheManagerFactory { @Override public CacheManager get() { diff --git a/mpush-test/src/main/java/com/mpush/test/spi/SimpleDiscoveryFactory.java b/mpush-test/src/main/java/com/mpush/test/spi/SimpleDiscoveryFactory.java index cc6138db..3165d582 100644 --- a/mpush-test/src/main/java/com/mpush/test/spi/SimpleDiscoveryFactory.java +++ b/mpush-test/src/main/java/com/mpush/test/spi/SimpleDiscoveryFactory.java @@ -28,7 +28,7 @@ * * @author ohun@live.cn (夜色) */ -@Spi(order = 0) +@Spi(order = 2) public final class SimpleDiscoveryFactory implements ServiceDiscoveryFactory { @Override public ServiceDiscovery get() { diff --git a/mpush-test/src/main/java/com/mpush/test/spi/SimpleMQClientFactory.java b/mpush-test/src/main/java/com/mpush/test/spi/SimpleMQClientFactory.java index 39a4e59e..0d49236e 100644 --- a/mpush-test/src/main/java/com/mpush/test/spi/SimpleMQClientFactory.java +++ b/mpush-test/src/main/java/com/mpush/test/spi/SimpleMQClientFactory.java @@ -28,7 +28,7 @@ * * @author ohun@live.cn (夜色) */ -@Spi(order = 0) +@Spi(order = 2) public final class SimpleMQClientFactory implements com.mpush.api.spi.common.MQClientFactory { private MQClient mqClient = new MQClient() { @Override diff --git a/mpush-test/src/main/java/com/mpush/test/spi/SimpleRegistryFactory.java b/mpush-test/src/main/java/com/mpush/test/spi/SimpleRegistryFactory.java index 1fd9521a..28ffdb0d 100644 --- a/mpush-test/src/main/java/com/mpush/test/spi/SimpleRegistryFactory.java +++ b/mpush-test/src/main/java/com/mpush/test/spi/SimpleRegistryFactory.java @@ -28,7 +28,7 @@ * * @author ohun@live.cn (夜色) */ -@Spi(order = 0) +@Spi(order = 2) public final class SimpleRegistryFactory implements ServiceRegistryFactory { @Override public ServiceRegistry get() { From 9d5ad8353fe991179e136640ad740068a05dc0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 23 Aug 2017 09:30:28 +0800 Subject: [PATCH 887/890] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E4=B8=8E=E5=8F=91?= =?UTF-8?q?=E7=8E=B0=E5=90=AF=E5=8A=A8=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/bootstrap/ServerLauncher.java | 1 + .../bootstrap/job/ServiceDiscoveryBoot.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 mpush-boot/src/main/java/com/mpush/bootstrap/job/ServiceDiscoveryBoot.java diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java index 3831edbb..4d8476cf 100644 --- a/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java @@ -57,6 +57,7 @@ public void init() { chain.boot() .setNext(new CacheManagerBoot())//1.初始化缓存模块 .setNext(new ServiceRegistryBoot())//2.启动服务注册与发现模块 + .setNext(new ServiceDiscoveryBoot())//2.启动服务注册与发现模块 .setNext(new ServerBoot(mPushServer.getConnectionServer(), mPushServer.getConnServerNode()))//3.启动接入服务 .setNext(() -> new ServerBoot(mPushServer.getWebsocketServer(), mPushServer.getWebsocketServerNode()), wsEnabled())//4.启动websocket接入服务 .setNext(() -> new ServerBoot(mPushServer.getUdpGatewayServer(), mPushServer.getGatewayServerNode()), udpGateway())//5.启动udp网关服务 diff --git a/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServiceDiscoveryBoot.java b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServiceDiscoveryBoot.java new file mode 100644 index 00000000..67c397bd --- /dev/null +++ b/mpush-boot/src/main/java/com/mpush/bootstrap/job/ServiceDiscoveryBoot.java @@ -0,0 +1,47 @@ +/* + * (C) Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * ohun@live.cn (夜色) + */ + +package com.mpush.bootstrap.job; + +import com.mpush.api.spi.common.ServiceDiscoveryFactory; +import com.mpush.api.spi.common.ServiceRegistryFactory; +import com.mpush.api.srd.ServiceDiscovery; +import com.mpush.tools.log.Logs; + +/** + * Created by yxx on 2016/5/14. + * + * @author ohun@live.cn + */ +public final class ServiceDiscoveryBoot extends BootJob { + + @Override + protected void start() { + Logs.Console.info("init service discovery waiting for connected..."); + ServiceDiscoveryFactory.create().syncStart(); + startNext(); + } + + @Override + protected void stop() { + stopNext(); + ServiceDiscoveryFactory.create().syncStop(); + Logs.Console.info("service discovery closed..."); + } +} From 22ebf5249a0fb61ad47ca5a65e8381de11023ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 23 Aug 2017 09:31:20 +0800 Subject: [PATCH 888/890] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E4=B8=8E=E5=81=9C=E6=AD=A2=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mpush/api/service/BaseService.java | 45 +++++++++++++++++-- .../com/mpush/api/service/FutureListener.java | 7 ++- .../com/mpush/test/spi/FileCacheManger.java | 2 + .../main/java/com/mpush/test/spi/FileSrd.java | 22 ++++++++- .../src/main/java/com/mpush/zk/ZKClient.java | 9 ++++ 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java index 8337574f..9e8ffcdb 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -46,13 +46,17 @@ protected void tryStart(Listener l, FunctionEx function) { try { init(); function.apply(listener); - listener.monitor(this); + listener.monitor(this);//主要用于异步,否则应该放置在function.apply(listener)之前 } catch (Throwable e) { listener.onFailure(e); throw new ServiceException(e); } } else { - listener.onFailure(new ServiceException("service already started.")); + if (throwIfStarted()) { + listener.onFailure(new ServiceException("service already started.")); + } else { + listener.onSuccess(); + } } } @@ -61,13 +65,17 @@ protected void tryStop(Listener l, FunctionEx function) { if (started.compareAndSet(true, false)) { try { function.apply(listener); - listener.monitor(this); + listener.monitor(this);//主要用于异步,否则应该放置在function.apply(listener)之前 } catch (Throwable e) { listener.onFailure(e); throw new ServiceException(e); } } else { - listener.onFailure(new ServiceException("service already stopped.")); + if (throwIfStopped()) { + listener.onFailure(new ServiceException("service already stopped.")); + } else { + listener.onSuccess(); + } } } @@ -111,6 +119,35 @@ protected void doStop(Listener listener) throws Throwable { listener.onSuccess(); } + /** + * 控制当服务已经启动后,重复调用start方法,是否抛出服务已经启动异常 + * 默认是true + * + * @return true:抛出异常 + */ + protected boolean throwIfStarted() { + return true; + } + + /** + * 控制当服务已经停止后,重复调用stop方法,是否抛出服务已经停止异常 + * 默认是true + * + * @return true:抛出异常 + */ + protected boolean throwIfStopped() { + return true; + } + + /** + * 服务启动停止,超时时间, 默认是10s + * + * @return 超时时间 + */ + protected int timeoutMillis() { + return 1000 * 10; + } + protected interface FunctionEx { void apply(Listener l) throws Throwable; } diff --git a/mpush-api/src/main/java/com/mpush/api/service/FutureListener.java b/mpush-api/src/main/java/com/mpush/api/service/FutureListener.java index b14b8310..4b07d32c 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/FutureListener.java +++ b/mpush-api/src/main/java/com/mpush/api/service/FutureListener.java @@ -35,11 +35,16 @@ public void onFailure(Throwable cause) { : new ServiceException(cause); } + /** + * 防止服务长时间卡在某个地方,增加超时监控 + * + * @param service 服务 + */ public void monitor(BaseService service) { if (isDone()) return;// 防止Listener被重复执行 runAsync(() -> { try { - this.get(10, TimeUnit.SECONDS); + this.get(service.timeoutMillis(), TimeUnit.MILLISECONDS); } catch (Exception e) { this.onFailure(new ServiceException(String.format("service %s monitor timeout", service.getClass().getSimpleName()))); } diff --git a/mpush-test/src/main/java/com/mpush/test/spi/FileCacheManger.java b/mpush-test/src/main/java/com/mpush/test/spi/FileCacheManger.java index f6d8d791..9563b869 100644 --- a/mpush-test/src/main/java/com/mpush/test/spi/FileCacheManger.java +++ b/mpush-test/src/main/java/com/mpush/test/spi/FileCacheManger.java @@ -22,6 +22,7 @@ import com.mpush.api.Constants; import com.mpush.api.spi.common.CacheManager; import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; import java.nio.file.Files; import java.nio.file.Path; @@ -47,6 +48,7 @@ public final class FileCacheManger implements CacheManager { @Override public void init() { + Logs.Console.warn("你正在使用的CacheManager只能用于源码测试,生产环境请使用redis 3.x."); try { Path dir = Paths.get(this.getClass().getResource("/").toURI()); this.cacheFile = Paths.get(dir.toString(), "cache.dat"); diff --git a/mpush-test/src/main/java/com/mpush/test/spi/FileSrd.java b/mpush-test/src/main/java/com/mpush/test/spi/FileSrd.java index 9bc5216e..0ee43903 100644 --- a/mpush-test/src/main/java/com/mpush/test/spi/FileSrd.java +++ b/mpush-test/src/main/java/com/mpush/test/spi/FileSrd.java @@ -21,8 +21,10 @@ import com.google.common.collect.Lists; import com.mpush.api.service.BaseService; +import com.mpush.api.service.Listener; import com.mpush.api.srd.*; import com.mpush.tools.Jsons; +import com.mpush.tools.log.Logs; import java.util.List; @@ -36,8 +38,26 @@ public final class FileSrd extends BaseService implements ServiceRegistry, Servi public static final FileSrd I = new FileSrd(); @Override - public void init() { + public void start(Listener listener) { + if (isRunning()) { + listener.onSuccess(); + } else { + super.start(listener); + } + } + @Override + public void stop(Listener listener) { + if (isRunning()) { + super.stop(listener); + } else { + listener.onSuccess(); + } + } + + @Override + public void init() { + Logs.Console.warn("你正在使用的ServiceRegistry和ServiceDiscovery只能用于源码测试,生产环境请使用zookeeper."); } @Override diff --git a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java index 1b95148e..f947ce55 100644 --- a/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java +++ b/mpush-zk/src/main/java/com/mpush/zk/ZKClient.java @@ -63,6 +63,15 @@ public void start(Listener listener) { } } + @Override + public void stop(Listener listener) { + if (isRunning()) { + super.stop(listener); + } else { + listener.onSuccess(); + } + } + @Override protected void doStart(Listener listener) throws Throwable { client.start(); From 295c402b209521442e391a2d24f90c1c893257af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 23 Aug 2017 09:52:06 +0800 Subject: [PATCH 889/890] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/reference.conf | 4 ++-- .../main/java/com/mpush/api/connection/SessionContext.java | 4 ++++ mpush-boot/src/main/resources/mpush.conf | 6 ++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/conf/reference.conf b/conf/reference.conf index 15c7888d..90c0bfb6 100644 --- a/conf/reference.conf +++ b/conf/reference.conf @@ -40,8 +40,8 @@ mp { #网络配置 net { - local-ip="" //本地ip, 默认取第一个网卡 - public-ip="" //外网ip, 默认取第一个网卡 + local-ip="" //本地ip, 默认取第一个网卡的本地IP + public-ip="" //外网ip, 默认取第一个网卡的外网IP connect-server-bind-ip="" //connSrv 绑定的本地ip (默认anyLocalAddress 0.0.0.0 or ::0) connect-server-register-ip=${mp.net.public-ip} //公网ip, 注册到zk中的ip, 默认是public-ip diff --git a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java index 9ebcce09..a68361e2 100644 --- a/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java +++ b/mpush-api/src/main/java/com/mpush/api/connection/SessionContext.java @@ -88,6 +88,10 @@ public boolean isSecurity() { @Override public String toString() { + if (userId == null && deviceId == null) { + return ""; + } + return "{" + "osName='" + osName + '\'' + ", osVersion='" + osVersion + '\'' + diff --git a/mpush-boot/src/main/resources/mpush.conf b/mpush-boot/src/main/resources/mpush.conf index 57accdff..9debe252 100644 --- a/mpush-boot/src/main/resources/mpush.conf +++ b/mpush-boot/src/main/resources/mpush.conf @@ -7,11 +7,9 @@ mp.redis { //redis 集群配置 nodes:["127.0.0.1:6379"] //格式是ip:port cluster-model:single //single, cluster } +mp.net.local-ip="" //本地ip, 默认取第一个网卡的本地IP +mp.net.public-ip="" //外网ip, 默认取第一个网卡的外网IP mp.net.ws-server-port=0 //websocket对外端口, 0表示禁用websocket mp.net.gateway-server-net=tcp // 网关服务使用的网络 udp/tcp mp.net.connect-server-port=3000 //接入服务的端口号 -mp.net.public-host-mapping { //本机局域网IP和公网IP的映射关系,请添加实际的IP - //"10.0.10.156":"111.1.32.137" //请修改成实际的IP - //"10.0.10.166":"111.1.33.138" //请修改成实际的IP -} mp.http.proxy-enabled=true //启用Http代理功能 From 265731380bf45deed193f0e3aa98664acc31d4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E8=89=B2?= Date: Wed, 23 Aug 2017 11:46:46 +0800 Subject: [PATCH 890/890] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpush/api/common/ServerEventListener.java | 12 +++++----- .../com/mpush/api/router/RouterManager.java | 24 +++++++++---------- .../com/mpush/api/service/BaseService.java | 4 ++-- .../com/mpush/api/spi/push/PushListener.java | 21 ++++++++++------ 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java b/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java index 1be1794b..1af33838 100644 --- a/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java +++ b/mpush-api/src/main/java/com/mpush/api/common/ServerEventListener.java @@ -32,7 +32,7 @@ public interface ServerEventListener extends Plugin { /** * 该事件通过guava EventBus发出,实现接口的方法必须增加 * - * @Subscribe 和 @AllowConcurrentEvents注解, + * @Subscribe 和 @AllowConcurrentEvents注解, * 并在构造函数调用EventBus.register(this); */ default void on(ServerStartupEvent event) { @@ -41,7 +41,7 @@ default void on(ServerStartupEvent event) { /** * 该事件通过guava EventBus发出,实现接口的方法必须增加 * - * @Subscribe 和 @AllowConcurrentEvents注解, + * @Subscribe 和 @AllowConcurrentEvents注解, * 并在构造函数调用EventBus.register(this); */ default void on(ServerShutdownEvent server) { @@ -50,7 +50,7 @@ default void on(ServerShutdownEvent server) { /** * 该事件通过guava EventBus发出,实现接口的方法必须增加 * - * @Subscribe 和 @AllowConcurrentEvents注解, + * @Subscribe 和 @AllowConcurrentEvents注解, * 并在构造函数调用EventBus.register(this); */ default void on(RouterChangeEvent event) { @@ -59,7 +59,7 @@ default void on(RouterChangeEvent event) { /** * 该事件通过guava EventBus发出,实现接口的方法必须增加 * - * @Subscribe 和 @AllowConcurrentEvents注解, + * @Subscribe 和 @AllowConcurrentEvents注解, * 并在构造函数调用EventBus.register(this); */ default void on(KickUserEvent event) { @@ -68,7 +68,7 @@ default void on(KickUserEvent event) { /** * 该事件通过guava EventBus发出,实现接口的方法必须增加 * - * @Subscribe 和 @AllowConcurrentEvents注解, + * @Subscribe 和 @AllowConcurrentEvents注解, * 并在构造函数调用EventBus.register(this); */ default void on(UserOnlineEvent event) { @@ -77,7 +77,7 @@ default void on(UserOnlineEvent event) { /** * 该事件通过guava EventBus发出,实现接口的方法必须增加 * - * @Subscribe 和 @AllowConcurrentEvents注解, + * @Subscribe 和 @AllowConcurrentEvents注解, * 并在构造函数调用EventBus.register(this); */ default void on(UserOfflineEvent event) { diff --git a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java index cd1bc7e3..7402935f 100644 --- a/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java +++ b/mpush-api/src/main/java/com/mpush/api/router/RouterManager.java @@ -31,35 +31,35 @@ public interface RouterManager { /** * 注册路由 * - * @param userId - * @param router - * @return + * @param userId 用户ID + * @param router 新路由 + * @return 如果有旧的的路由信息则返回之,否则返回空。 */ R register(String userId, R router); /** * 删除路由 * - * @param userId - * @param clientType - * @return + * @param userId 用户ID + * @param clientType 客户端类型 + * @return true:成功,false:失败 */ boolean unRegister(String userId, int clientType); /** * 查询路由 * - * @param userId - * @return + * @param userId 用户ID + * @return userId对应的所有的路由信息 */ Set lookupAll(String userId); /** - * 查询路由 + * 查询指定设备类型的用户路由信息 * - * @param userId - * @param clientType - * @return + * @param userId 用户ID + * @param clientType 客户端类型 + * @return 指定类型的路由信息 */ R lookup(String userId, int clientType); } diff --git a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java index 9e8ffcdb..b967e894 100644 --- a/mpush-api/src/main/java/com/mpush/api/service/BaseService.java +++ b/mpush-api/src/main/java/com/mpush/api/service/BaseService.java @@ -155,8 +155,8 @@ protected interface FunctionEx { /** * 防止Listener被重复执行 * - * @param l - * @return + * @param l listener + * @return FutureListener */ public FutureListener wrap(Listener l) { if (l == null) return new FutureListener(started); diff --git a/mpush-api/src/main/java/com/mpush/api/spi/push/PushListener.java b/mpush-api/src/main/java/com/mpush/api/spi/push/PushListener.java index fb3b6ae2..22a7f368 100644 --- a/mpush-api/src/main/java/com/mpush/api/spi/push/PushListener.java +++ b/mpush-api/src/main/java/com/mpush/api/spi/push/PushListener.java @@ -8,49 +8,56 @@ public interface PushListener extends Plugin { * 消息下发成功后回调 * 如果消息需要ACK则该方法不会被调用 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onSuccess(T message, Object[] timePoints); /** * 收到客户端ACK后回调 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onAckSuccess(T message, Object[] timePoints); /** * 广播消息推送全部结束后回调 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onBroadcastComplete(T message, Object[] timePoints); /** * 消息下发失败后回调 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onFailure(T message, Object[] timePoints); /** * 推送消息发现用户不在线时回调 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onOffline(T message, Object[] timePoints); /** * 推送消息发现用户不在当前机器时回调 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onRedirect(T message, Object[] timePoints); /** * 发送消息超时或等待客户端ACK超时时回调 * - * @param message 要下发的消息 + * @param message 要下发的消息 + * @param timePoints 消息流转时间节点 */ void onTimeout(T message, Object[] timePoints); } \ No newline at end of file